<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>Hi,<br>
<br>
</div>
<div class="moz-cite-prefix">On 25/3/19 10:36 pm, Saurabh Bora
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAH+MckD2im-Ew76Wpk0b3_gj0LNMFF_=1sdQRHdRkZ9wt1DSuA@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">Thanks Michael,
<div>It seems there is some problem with the input image itself.
I tried different image of 640*480 resolution and code worked
just fine.</div>
</div>
</blockquote>
<div>For frame sizes that aren't a multiple of 4 bytes, you need to
round up and add padding - GStreamer buffers have an implicit
stride that's the nearest multiple of 4. Alternatively, you can
add a GstVideoMeta to the GstBuffer describing the actual stride.</div>
<div><br>
</div>
<div>Cheers,</div>
<div>Jan.<br>
</div>
<blockquote type="cite"
cite="mid:CAH+MckD2im-Ew76Wpk0b3_gj0LNMFF_=1sdQRHdRkZ9wt1DSuA@mail.gmail.com">
<div dir="ltr">
<div><br>
</div>
<div>Regards,</div>
<div>Saurabh Bora</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Fri, Mar 15, 2019 at 2:52
AM Michael Gruner <<a
href="mailto:michael.gruner@ridgerun.com"
moz-do-not-send="true">michael.gruner@ridgerun.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="overflow-wrap: break-word;">Can you run your app
with warning logging enabled? Any of the following should
work:
<div><br>
</div>
<div>./app --gst-debug=2</div>
<div><br>
</div>
<div>Or</div>
<div><br>
</div>
<div>GST_DEBUG=2 ./app</div>
<div><br>
</div>
<div><br>
<div><br>
<blockquote type="cite">
<div>On Mar 14, 2019, at 2:17 PM, Saurabh Bora <<a
href="mailto:saurabh9bora@gmail.com"
target="_blank" moz-do-not-send="true">saurabh9bora@gmail.com</a>>
wrote:</div>
<br
class="gmail-m_159555576828944386Apple-interchange-newline">
<div>
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">Hi Experts!,
<div><br>
</div>
<div>Thanks!! Michael, I tried the approach
suggested by you and it works fine. I could
verify the conversion of cv::Mat to
GstBuffer and vice versa. On cv::write I get
exact image that I had copied to GstBuffer.</div>
<div><br>
</div>
<div>However, I am unable to get correct image
on autovidesink. With current code below, I
see a skewed and distorted grayscale image.
Note that I had to set width and height of
videocaps on appsrc element as 133, 133
respectively, even though dimesnions of my
input image is 134*134. If I set width and
height as 134, 134 in videocaps, I see a
black videosink.</div>
<div><br>
</div>
<div>//----------------------------------------------------------</div>
<div>
<div>#include <gst/gst.h></div>
<div>#include <opencv2/opencv.hpp></div>
<div>#include <opencv2/highgui.hpp></div>
<div><br>
</div>
<div>static GMainLoop *loop;</div>
<div><br>
</div>
<div>void buffer_destroy(gpointer data)</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat*
done = (cv::Mat*)data;</div>
<div><span style="white-space:pre-wrap"> </span>delete
done;</div>
<div>}</div>
<div><br>
</div>
<div>static void</div>
<div>cb_need_data(GstElement *appsrc,</div>
<div><span style="white-space:pre-wrap"> </span>guint
unused_size,</div>
<div><span style="white-space:pre-wrap"> </span>gpointer
user_data)</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>static
GstClockTime timestamp = 0;</div>
<div><span style="white-space:pre-wrap"> </span>GstBuffer
*buffer;</div>
<div><span style="white-space:pre-wrap"> </span>GstFlowReturn
ret;</div>
<div><span style="white-space:pre-wrap"> </span>char
*data1;</div>
<div><span style="white-space:pre-wrap"> </span>GstMapInfo
map;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat*
img = new
cv::Mat(cv::imread("C:/Users/212730892.LOGON/Desktop/lena.jpg"));</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>std::cout
<< "cb_need_data called" <<
std::endl;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>gsize
sizeInBytes = img->total() *
img->elemSize();</div>
<div><span style="white-space:pre-wrap"> </span>std::cout
<< "Size getting copied: " <<
sizeInBytes << std::endl;</div>
<div><span style="white-space:pre-wrap"> </span>buffer
=
gst_buffer_new_wrapped_full((GstMemoryFlags)0,
(gpointer)(img->data), sizeInBytes, 0,
sizeInBytes, (gpointer)img,
(GDestroyNotify)buffer_destroy);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>gsize
bufferSize = gst_buffer_get_size(buffer);</div>
<div><span style="white-space:pre-wrap"> </span>std::cout
<< "Buffer Size: " <<
bufferSize << std::endl;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//Convert
GstBuffer back to cv::Mat, to check if
data was correctly copied</div>
<div><span style="white-space:pre-wrap"> </span>GstMapInfo
info;</div>
<div><span style="white-space:pre-wrap"> </span>gst_buffer_map(buffer,
&info, GST_MAP_READ);</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
img2(cv::Size(img->size().height,
img->size().width), CV_8UC3,
(char*)(info.data));</div>
<div><span style="white-space:pre-wrap"> </span>cv::imwrite("C:/Users/212730892.LOGON/Desktop/dummy1.jpg",
img2);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_OFFSET(buffer)
= 0;</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_PTS(buffer)
= timestamp;</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_DURATION(buffer)
= gst_util_uint64_scale_int(1, GST_SECOND,
30);</div>
<div><span style="white-space:pre-wrap"> </span>//GST_BUFFER_DURATION(buffer)
= GST_CLOCK_TIME_NONE;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>timestamp
+= GST_BUFFER_DURATION(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_emit_by_name(appsrc,
"push-buffer", buffer, &ret);</div>
<div><span style="white-space:pre-wrap"> </span>gst_buffer_unref(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(ret != GST_FLOW_OK) {</div>
<div><span style="white-space:pre-wrap"> </span>/*
something wrong, stop pushing */</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_quit(loop);</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div>}</div>
<div><br>
</div>
<div>gint</div>
<div>main(gint argc,</div>
<div><span style="white-space:pre-wrap"> </span>gchar
*argv[])</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>GstElement
*pipeline, *appsrc, *conv, *videosink;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
init GStreamer */</div>
<div><span style="white-space:pre-wrap"> </span>gst_init(&argc,
&argv);</div>
<div><span style="white-space:pre-wrap"> </span>loop
= g_main_loop_new(NULL, FALSE);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup pipeline */</div>
<div><span style="white-space:pre-wrap"> </span>pipeline
= gst_pipeline_new("pipeline");</div>
<div><span style="white-space:pre-wrap"> </span>appsrc
= gst_element_factory_make("appsrc",
"source");</div>
<div><span style="white-space:pre-wrap"> </span>conv
= gst_element_factory_make("videoconvert",
"conv");</div>
<div><span style="white-space:pre-wrap"> </span>videosink
=
gst_element_factory_make("autovideosink",
"videosink");</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),
"caps",</div>
<div><span style="white-space:pre-wrap"> </span>gst_caps_new_simple("video/x-raw",</div>
<div><span style="white-space:pre-wrap"> </span>"format",
G_TYPE_STRING, "RGB",</div>
<div><span style="white-space:pre-wrap"> </span>"width",
G_TYPE_INT, 133,</div>
<div><span style="white-space:pre-wrap"> </span>"height",
G_TYPE_INT, 133,</div>
<div><span style="white-space:pre-wrap"> </span>"framerate",
GST_TYPE_FRACTION, 1, 30,</div>
<div><span style="white-space:pre-wrap"> </span>NULL),
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_bin_add_many(GST_BIN(pipeline),
appsrc, conv, videosink, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_link_many(appsrc,
conv, videosink, NULL);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup appsrc */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),</div>
<div><span style="white-space:pre-wrap"> </span>"stream-type",
0,</div>
<div><span style="white-space:pre-wrap"> </span>"format",
GST_FORMAT_TIME, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_connect(appsrc,
"need-data", G_CALLBACK(cb_need_data),
NULL);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
play */</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_set_state(pipeline,
GST_STATE_PLAYING);</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_run(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(gst_element_set_state(pipeline,
GST_STATE_NULL) ==
GST_STATE_CHANGE_FAILURE)</div>
<div><span style="white-space:pre-wrap"> </span>{</div>
<div><span style="white-space:pre-wrap"> </span>g_printerr("Unable
to set the pipeline to the playing
state.\n");</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//
Free resources</div>
<div><span style="white-space:pre-wrap"> </span>gst_object_unref(GST_OBJECT(pipeline));</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_unref(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>return
0;</div>
<div>}</div>
</div>
<div>//-------------------------------------------------------------------------</div>
<div><br>
</div>
<div>I am new to gstreamer framework and it
would be a great help if someone could point
me in right direction.</div>
</div>
</div>
<div><br>
</div>
<div>Thanks and Regards,</div>
<div>Saurabh Bora</div>
<div><br>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Wed, Mar
13, 2019 at 11:48 PM Michael Gruner <<a
href="mailto:michael.gruner@ridgerun.com"
target="_blank" moz-do-not-send="true">michael.gruner@ridgerun.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote"
style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="auto">It looks like img2, being a
local variable, ends its life when
“cb_need_data” ends. That leaves the pointer
in “buffer” invalid. For this one you’re
better off with the copy.
<div><br>
</div>
<div>Another option is to alloc img2 on the
heap, and use gst_buffer_new_wrapped_full
to carry img2 along with buffer and free
then together.<br>
<br>
<div
id="gmail-m_159555576828944386gmail-m_7923909520158695290AppleMailSignature"
dir="ltr">Michael</div>
<div
id="gmail-m_159555576828944386gmail-m_7923909520158695290AppleMailSignature"
dir="ltr"><a
href="http://www.ridgerun.com/"
target="_blank" moz-do-not-send="true">www.ridgerun.com</a></div>
<div dir="ltr"><br>
On Mar 13, 2019, at 10:51 AM, Saurabh
Bora <<a
href="mailto:saurabh9bora@gmail.com"
target="_blank" moz-do-not-send="true">saurabh9bora@gmail.com</a>>
wrote:<br>
<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">Hi Michael,
<div><br>
</div>
<div>Thanks! for your input. I
tried what you suggested as
below:</div>
<div><br>
</div>
<div>//---------------------------------------------------------------</div>
<div>
<div>#include <gst/gst.h></div>
<div>#include
<opencv2/opencv.hpp></div>
<div>#include
<opencv2/highgui.hpp></div>
<div>#include
<opencv2/imgproc/imgproc.hpp></div>
<div><br>
</div>
<div>static GMainLoop *loop;</div>
<div><br>
</div>
<div>static void</div>
<div>cb_need_data(GstElement
*appsrc,</div>
<div><span style="white-space:pre-wrap"> </span>guint
unused_size,</div>
<div><span style="white-space:pre-wrap"> </span>gpointer
user_data)</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>static
GstClockTime timestamp = 0;</div>
<div><span style="white-space:pre-wrap"> </span>GstBuffer
*buffer;</div>
<div><span style="white-space:pre-wrap"> </span>guint
size, depth, height, width,
step, channels;</div>
<div><span style="white-space:pre-wrap"> </span>GstFlowReturn
ret;</div>
<div><span style="white-space:pre-wrap"> </span>guchar
*data1;</div>
<div><span style="white-space:pre-wrap"> </span>GstMapInfo
map;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
img2;</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
img =
cv::imread("C:/Users/212730892.LOGON/Desktop/lena.jpg");</div>
<div><span style="white-space:pre-wrap"> </span>cv::cvtColor(img,
img2, cv::COLOR_BGR2RGB);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>height
= img.size().height;</div>
<div><span style="white-space:pre-wrap"> </span>width
= img.size().width;</div>
<div><span style="white-space:pre-wrap"> </span>//step
= img.widthStep;</div>
<div><span style="white-space:pre-wrap"> </span>channels
= img.channels();</div>
<div><span style="white-space:pre-wrap"> </span>//depth
= img->depth;</div>
<div><span style="white-space:pre-wrap"> </span>data1
= (guchar *)img.data;</div>
<div><span style="white-space:pre-wrap"> </span>size
= height * width * channels *
sizeof(guchar);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>std::cout
<< "cb_need_data called"
<< std::endl;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>buffer
=
gst_buffer_new_wrapped(img2.data,
img2.total() *
img2.elemSize());</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>GstMapInfo
info;</div>
<div><span style="white-space:pre-wrap"> </span>gst_buffer_map(buffer,
&info, GST_MAP_READ);</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
mat(height, width, CV_8UC3,
info.data);</div>
<div><span style="white-space:pre-wrap"> </span>cv::imshow("Output",
mat);</div>
<div><span style="white-space:pre-wrap"> </span>//gst_buffer_unmap(buffer,
&info);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_PTS(buffer)
= timestamp;</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_DURATION(buffer)
= gst_util_uint64_scale_int(1,
GST_SECOND, 2);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>timestamp
+=
GST_BUFFER_DURATION(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_emit_by_name(appsrc,
"push-buffer", buffer,
&ret);</div>
<div><span style="white-space:pre-wrap"> </span>//gst_buffer_unref(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(ret != GST_FLOW_OK) {</div>
<div><span style="white-space:pre-wrap"> </span>/*
something wrong, stop pushing
*/</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_quit(loop);</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div>}</div>
<div><br>
</div>
<div>gint</div>
<div>main(gint argc,</div>
<div><span style="white-space:pre-wrap"> </span>gchar
*argv[])</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>GstElement
*pipeline, *appsrc, *conv,
*videosink;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
init GStreamer */</div>
<div><span style="white-space:pre-wrap"> </span>gst_init(&argc,
&argv);</div>
<div><span style="white-space:pre-wrap"> </span>loop
= g_main_loop_new(NULL,
FALSE);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup pipeline */</div>
<div><span style="white-space:pre-wrap"> </span>pipeline
=
gst_pipeline_new("pipeline");</div>
<div><span style="white-space:pre-wrap"> </span>appsrc
=
gst_element_factory_make("appsrc",
"source");</div>
<div><span style="white-space:pre-wrap"> </span>conv
=
gst_element_factory_make("videoconvert",
"conv");</div>
<div><span style="white-space:pre-wrap"> </span>videosink
=
gst_element_factory_make("autovideosink",
"videosink");</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),
"caps",</div>
<div><span style="white-space:pre-wrap"> </span>gst_caps_new_simple("video/x-raw",</div>
<div><span style="white-space:pre-wrap"> </span>"format",
G_TYPE_STRING, "RGB",</div>
<div><span style="white-space:pre-wrap"> </span>"width",
G_TYPE_INT, 134,</div>
<div><span style="white-space:pre-wrap"> </span>"height",
G_TYPE_INT, 134,</div>
<div><span style="white-space:pre-wrap"> </span>"framerate",
GST_TYPE_FRACTION, 1, 1,</div>
<div><span style="white-space:pre-wrap"> </span>NULL),
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_bin_add_many(GST_BIN(pipeline),
appsrc, conv, videosink,
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_link_many(appsrc,
conv, videosink, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>//g_object_set
(videosink, "device",
"/dev/video0", NULL);</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup appsrc */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),</div>
<div><span style="white-space:pre-wrap"> </span>"stream-type",
0,</div>
<div><span style="white-space:pre-wrap"> </span>"format",
GST_FORMAT_TIME, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_connect(appsrc,
"need-data",
G_CALLBACK(cb_need_data),
NULL);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
play */</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_set_state(pipeline,
GST_STATE_PLAYING);</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_run(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(gst_element_set_state(pipeline, GST_STATE_NULL) ==
GST_STATE_CHANGE_FAILURE)</div>
<div><span style="white-space:pre-wrap"> </span>{</div>
<div><span style="white-space:pre-wrap"> </span>g_printerr("Unable
to set the pipeline to the
playing state.\n");</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//
Free resources</div>
<div><span style="white-space:pre-wrap"> </span>gst_object_unref(GST_OBJECT(pipeline));</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_unref(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>return
0;</div>
<div>}</div>
</div>
<div>//----------------------------------------------------------------------------------------------</div>
<div><br>
</div>
<div>It still does not work as
expected. It would be a great
help if you could point out
where I am going wrong.</div>
<div>Thanks!!</div>
</div>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On
Wed, Mar 13, 2019 at 7:40 PM
Michael Gruner <<a
href="mailto:michael.gruner@ridgerun.com"
target="_blank"
moz-do-not-send="true">michael.gruner@ridgerun.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote"
style="margin:0px 0px 0px
0.8ex;border-left:1px solid
rgb(204,204,204);padding-left:1ex">
<div dir="auto">Hi Saurabh
<div><br>
</div>
<div>Yo may convert from a
cv::Mat to a GstBuffer without
a memory copy by using
gst_buffer_new_wrapped:</div>
<div><br>
</div>
<div><a
href="https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstBuffer.html#gst-buffer-new-wrapped"
target="_blank"
moz-do-not-send="true">https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstBuffer.html#gst-buffer-new-wrapped</a></div>
<div><br>
</div>
<div>Something like: <br>
buffer =
gst_buffer_new_wrapped
(mat.data,
mat.total()*mat.elemSize());</div>
<div><br>
</div>
<div>In order to make a cv::Mat
from a GstBuffer you first
need to take the data out of
the buffer. </div>
<div><br>
</div>
<div>GstMapInfo info;</div>
<div>gst_buffer_map (buffer,
&info, GST_MAP_READ); //
or write</div>
<div>mat = cv::Mat(height,
width, CV_8C3, map.data); //
change your format accordingly</div>
<div>...</div>
<div>gst_buffer_unmap (buffer,
&info);</div>
<div><br>
</div>
<div>Hope it helps.</div>
<div><br>
</div>
<div>
<div
id="gmail-m_159555576828944386gmail-m_7923909520158695290gmail-m_3626855293976323275AppleMailSignature"
dir="ltr">Michael</div>
<div
id="gmail-m_159555576828944386gmail-m_7923909520158695290gmail-m_3626855293976323275AppleMailSignature"
dir="ltr"><a
href="http://www.ridgerun.com/"
target="_blank"
moz-do-not-send="true">www.ridgerun.com</a></div>
<div dir="ltr"><br>
On Mar 13, 2019, at 7:15 AM,
Saurabh Bora <<a
href="mailto:saurabh9bora@gmail.com"
target="_blank"
moz-do-not-send="true">saurabh9bora@gmail.com</a>>
wrote:<br>
<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">Hi
Experts!!,
<div><br>
</div>
<div>I am trying to
read opencv images
locally and push
them into gstreamer
pipeline. I am using
appsrc element with
"need-data" signal
attached to
call-back function.</div>
<div>Please look at
the code below. </div>
<div><br>
</div>
<div>//------------------------------------------------------------------------------------------------------</div>
<div>
<div>#include
<gst/gst.h></div>
<div>#include
<opencv2/opencv.hpp></div>
<div>#include
<opencv2/highgui.hpp></div>
<div><br>
</div>
<div>static
GMainLoop *loop;</div>
<div><br>
</div>
<div>static void</div>
<div>cb_need_data(GstElement
*appsrc,</div>
<div><span style="white-space:pre-wrap"> </span>guint
unused_size,</div>
<div><span style="white-space:pre-wrap"> </span>gpointer
user_data)</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>static
GstClockTime
timestamp = 0;</div>
<div><span style="white-space:pre-wrap"> </span>GstBuffer
*buffer;</div>
<div><span style="white-space:pre-wrap"> </span>guint
size, depth,
height, width,
step, channels;</div>
<div><span style="white-space:pre-wrap"> </span>GstFlowReturn
ret;</div>
<div><span style="white-space:pre-wrap"> </span>guchar
*data1;</div>
<div><span style="white-space:pre-wrap"> </span>GstMapInfo
map;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
img =
cv::imread("C:/Users/212730892.LOGON/Desktop/lena.jpg");</div>
<div><span style="white-space:pre-wrap"> </span>height
=
img.size().height;</div>
<div><span style="white-space:pre-wrap"> </span>width
=
img.size().width;</div>
<div><span style="white-space:pre-wrap"> </span>//step
= img.widthStep;</div>
<div><span style="white-space:pre-wrap"> </span>channels
= img.channels();</div>
<div><span style="white-space:pre-wrap"> </span>//depth
= img->depth;</div>
<div><span style="white-space:pre-wrap"> </span>data1
= (guchar
*)img.data;</div>
<div><span style="white-space:pre-wrap"> </span>size
= height * width*
channels;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//
Copy cv::Mat to
GstBuffer</div>
<div><span style="white-space:pre-wrap"> </span>buffer
=
gst_buffer_new_allocate(NULL,
size, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_buffer_map(buffer,
&map,
GST_MAP_WRITE);</div>
<div><span style="white-space:pre-wrap"> </span>memcpy((guchar
*)map.data, data1,
gst_buffer_get_size(buffer));</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//Convert
GstBuffer back to
cv::Mat and write
it, to check if
data was correctly
copied. //This is
where I get to
know that data is
not correct.</div>
<div><span style="white-space:pre-wrap"> </span>cv::Mat
img2(cv::Size(134,
134), CV_8UC3,
(char*)(buffer));</div>
<div><span style="white-space:pre-wrap"> </span>cv::imwrite("C:/Users/212730892.LOGON/Desktop/dummy1.jpg",
img2);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_PTS(buffer)
= timestamp;</div>
<div><span style="white-space:pre-wrap"> </span>GST_BUFFER_DURATION(buffer)
=
gst_util_uint64_scale_int(1,
GST_SECOND, 2);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>timestamp
+=
GST_BUFFER_DURATION(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_emit_by_name(appsrc,
"push-buffer",
buffer, &ret);</div>
<div><span style="white-space:pre-wrap"> </span>//gst_buffer_unref(buffer);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(ret !=
GST_FLOW_OK) {</div>
<div><span style="white-space:pre-wrap"> </span>/*
something wrong,
stop pushing */</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_quit(loop);</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div>}</div>
<div><br>
</div>
<div>gint</div>
<div>main(gint
argc,</div>
<div><span style="white-space:pre-wrap"> </span>gchar
*argv[])</div>
<div>{</div>
<div><span style="white-space:pre-wrap"> </span>GstElement
*pipeline,
*appsrc, *conv,
*videosink;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
init GStreamer */</div>
<div><span style="white-space:pre-wrap"> </span>gst_init(&argc,
&argv);</div>
<div><span style="white-space:pre-wrap"> </span>loop
=
g_main_loop_new(NULL,
FALSE);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup pipeline */</div>
<div><span style="white-space:pre-wrap"> </span>pipeline
=
gst_pipeline_new("pipeline");</div>
<div><span style="white-space:pre-wrap"> </span>appsrc
=
gst_element_factory_make("appsrc",
"source");</div>
<div><span style="white-space:pre-wrap"> </span>conv
=
gst_element_factory_make("videoconvert",
"conv");</div>
<div><span style="white-space:pre-wrap"> </span>videosink
=
gst_element_factory_make("autovideosink",
"videosink");</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),
"caps",</div>
<div><span style="white-space:pre-wrap"> </span>gst_caps_new_simple("video/x-raw",</div>
<div><span style="white-space:pre-wrap"> </span>"format",
G_TYPE_STRING,
"RGB",</div>
<div><span style="white-space:pre-wrap"> </span>"width",
G_TYPE_INT, 134,</div>
<div><span style="white-space:pre-wrap"> </span>"height",
G_TYPE_INT, 134,</div>
<div><span style="white-space:pre-wrap"> </span>"framerate",
GST_TYPE_FRACTION,
1, 1,</div>
<div><span style="white-space:pre-wrap"> </span>NULL),
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_bin_add_many(GST_BIN(pipeline),
appsrc, conv,
videosink, NULL);</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_link_many(appsrc,
conv, videosink,
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>//g_object_set
(videosink,
"device",
"/dev/video0",
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>/*
setup appsrc */</div>
<div><span style="white-space:pre-wrap"> </span>g_object_set(G_OBJECT(appsrc),</div>
<div><span style="white-space:pre-wrap"> </span>"stream-type",
0,</div>
<div><span style="white-space:pre-wrap"> </span>"format",
GST_FORMAT_TIME,
NULL);</div>
<div><span style="white-space:pre-wrap"> </span>g_signal_connect(appsrc,
"need-data",
G_CALLBACK(cb_need_data),
NULL);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>/*
play */</div>
<div><span style="white-space:pre-wrap"> </span>gst_element_set_state(pipeline,
GST_STATE_PLAYING);</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_run(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>if
(gst_element_set_state(pipeline, GST_STATE_NULL) ==
GST_STATE_CHANGE_FAILURE)</div>
<div><span style="white-space:pre-wrap"> </span>{</div>
<div><span style="white-space:pre-wrap"> </span>g_printerr("Unable
to set the
pipeline to the
playing
state.\n");</div>
<div><span style="white-space:pre-wrap"> </span>}</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>//
Free resources</div>
<div><span style="white-space:pre-wrap"> </span>gst_object_unref(GST_OBJECT(pipeline));</div>
<div><span style="white-space:pre-wrap"> </span>g_main_loop_unref(loop);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"> </span>return
0;</div>
<div>}</div>
</div>
<div>
<div>//-------------------------------------------------------------------------------------------------------------------------------------</div>
<div><br>
</div>
<div>In
cb_need_data, the
part where copying
I am copying to
GstBuffer seems to
be wrong and it
does not work as
expected. </div>
<div>Can anyone,
please help me
understand, how
can I copy the
image data
perfectly and pass
it downstream to
other elements
without losing any
data?</div>
<div><br>
</div>
-- <br>
<div dir="ltr"
class="gmail-m_159555576828944386gmail-m_7923909520158695290gmail-m_3626855293976323275gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">Thanks
and Regards,</div>
<div dir="ltr">Saurabh
Bora<br>
<div><br>
</div>
<div>PH NO :
7038166900</div>
<div>EMAIL : <a
href="mailto:saurabh9bora@gmail.com" style="font-size:12.8px"
target="_blank"
moz-do-not-send="true">saurabh9bora@gmail.com</a></div>
<div>
<a
href="mailto:saurabh9bora@outlook.com"
target="_blank" moz-do-not-send="true">saurabh9bora@outlook.com</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote type="cite">
<div dir="ltr"><span>_______________________________________________</span><br>
<span>gstreamer-devel
mailing list</span><br>
<span><a
href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank"
moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a></span><br>
<span><a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
target="_blank"
moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></span></div>
</blockquote>
</div>
</div>
_______________________________________________<br>
gstreamer-devel mailing list<br>
<a
href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank"
moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a><br>
<a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
rel="noreferrer" target="_blank"
moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr"
class="gmail-m_159555576828944386gmail-m_7923909520158695290gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">Thanks
and Regards,</div>
<div dir="ltr">Saurabh
Bora<br>
<div><br>
</div>
<div>PH NO :
7038166900</div>
<div>EMAIL : <a
href="mailto:saurabh9bora@gmail.com"
style="font-size:12.8px" target="_blank" moz-do-not-send="true">saurabh9bora@gmail.com</a></div>
<div> <a
href="mailto:saurabh9bora@outlook.com"
target="_blank"
moz-do-not-send="true">saurabh9bora@outlook.com</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<blockquote type="cite">
<div dir="ltr"><span>_______________________________________________</span><br>
<span>gstreamer-devel mailing list</span><br>
<span><a
href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank"
moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a></span><br>
<span><a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
target="_blank"
moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></span></div>
</blockquote>
</div>
</div>
_______________________________________________<br>
gstreamer-devel mailing list<br>
<a
href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank" moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a><br>
<a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
rel="noreferrer" target="_blank"
moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr"
class="gmail-m_159555576828944386gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">Thanks and Regards,</div>
<div dir="ltr">Saurabh Bora<br>
<div><br>
</div>
<div>PH NO : 7038166900</div>
<div>EMAIL : <a
href="mailto:saurabh9bora@gmail.com"
style="font-size:12.8px"
target="_blank"
moz-do-not-send="true">saurabh9bora@gmail.com</a></div>
<div> <a
href="mailto:saurabh9bora@outlook.com"
target="_blank"
moz-do-not-send="true">saurabh9bora@outlook.com</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
_______________________________________________<br>
gstreamer-devel mailing list<br>
<a
href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank" moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a><br>
<a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
target="_blank" moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></div>
</blockquote>
</div>
<br>
</div>
</div>
_______________________________________________<br>
gstreamer-devel mailing list<br>
<a href="mailto:gstreamer-devel@lists.freedesktop.org"
target="_blank" moz-do-not-send="true">gstreamer-devel@lists.freedesktop.org</a><br>
<a
href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel"
rel="noreferrer" target="_blank" moz-do-not-send="true">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></blockquote>
</div>
<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr" class="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">Thanks and Regards,</div>
<div dir="ltr">Saurabh Bora<br>
<div><br>
</div>
<div>PH NO : 7038166900</div>
<div>EMAIL : <a
href="mailto:saurabh9bora@gmail.com"
style="font-size:12.8px" target="_blank"
moz-do-not-send="true">saurabh9bora@gmail.com</a></div>
<div> <a
href="mailto:saurabh9bora@outlook.com"
target="_blank" moz-do-not-send="true">saurabh9bora@outlook.com</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
gstreamer-devel mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gstreamer-devel@lists.freedesktop.org">gstreamer-devel@lists.freedesktop.org</a>
<a class="moz-txt-link-freetext" href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel">https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a></pre>
</blockquote>
</body>
</html>