<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"> </span>guint unused_size,</div><div><span style="white-space:pre"> </span>gpointer user_data)</div><div>{</div><div><span style="white-space:pre"> </span>static GstClockTime timestamp = 0;</div><div><span style="white-space:pre"> </span>GstBuffer *buffer;</div><div><span style="white-space:pre"> </span>guint size, depth, height, width, step, channels;</div><div><span style="white-space:pre"> </span>GstFlowReturn ret;</div><div><span style="white-space:pre"> </span>guchar *data1;</div><div><span style="white-space:pre"> </span>GstMapInfo map;</div><div><br></div><div><span style="white-space:pre"> </span>cv::Mat img2;</div><div><span style="white-space:pre"> </span>cv::Mat img = cv::imread("C:/Users/212730892.LOGON/Desktop/lena.jpg");</div><div><span style="white-space:pre"> </span>cv::cvtColor(img, img2, cv::COLOR_BGR2RGB);</div><div><br></div><div><span style="white-space:pre"> </span>height = img.size().height;</div><div><span style="white-space:pre"> </span>width = img.size().width;</div><div><span style="white-space:pre"> </span>//step = img.widthStep;</div><div><span style="white-space:pre"> </span>channels = img.channels();</div><div><span style="white-space:pre"> </span>//depth = img->depth;</div><div><span style="white-space:pre"> </span>data1 = (guchar *)img.data;</div><div><span style="white-space:pre"> </span>size = height * width * channels * sizeof(guchar);</div><div><br></div><div><span style="white-space:pre"> </span>std::cout << "cb_need_data called" << std::endl;</div><div><br></div><div><span style="white-space:pre"> </span>buffer = gst_buffer_new_wrapped(img2.data, img2.total() * img2.elemSize());</div><div><br></div><div><span style="white-space:pre"> </span>GstMapInfo info;</div><div><span style="white-space:pre"> </span>gst_buffer_map(buffer, &info, GST_MAP_READ);</div><div><span style="white-space:pre"> </span>cv::Mat mat(height, width, CV_8UC3, info.data);</div><div><span style="white-space:pre"> </span>cv::imshow("Output", mat);</div><div><span style="white-space:pre"> </span>//gst_buffer_unmap(buffer, &info);</div><div><br></div><div><span style="white-space:pre"> </span>GST_BUFFER_PTS(buffer) = timestamp;</div><div><span style="white-space:pre"> </span>GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int(1, GST_SECOND, 2);</div><div><br></div><div><span style="white-space:pre"> </span>timestamp += GST_BUFFER_DURATION(buffer);</div><div><br></div><div><span style="white-space:pre"> </span>g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);</div><div><span style="white-space:pre"> </span>//gst_buffer_unref(buffer);</div><div><br></div><div><span style="white-space:pre"> </span>if (ret != GST_FLOW_OK) {</div><div><span style="white-space:pre"> </span>/* something wrong, stop pushing */</div><div><span style="white-space:pre"> </span>g_main_loop_quit(loop);</div><div><span style="white-space:pre"> </span>}</div><div>}</div><div><br></div><div>gint</div><div>main(gint argc,</div><div><span style="white-space:pre"> </span>gchar *argv[])</div><div>{</div><div><span style="white-space:pre"> </span>GstElement *pipeline, *appsrc, *conv, *videosink;</div><div><br></div><div><span style="white-space:pre"> </span>/* init GStreamer */</div><div><span style="white-space:pre"> </span>gst_init(&argc, &argv);</div><div><span style="white-space:pre"> </span>loop = g_main_loop_new(NULL, FALSE);</div><div><br></div><div><span style="white-space:pre"> </span>/* setup pipeline */</div><div><span style="white-space:pre"> </span>pipeline = gst_pipeline_new("pipeline");</div><div><span style="white-space:pre"> </span>appsrc = gst_element_factory_make("appsrc", "source");</div><div><span style="white-space:pre"> </span>conv = gst_element_factory_make("videoconvert", "conv");</div><div><span style="white-space:pre"> </span>videosink = gst_element_factory_make("autovideosink", "videosink");</div><div><br></div><div><span style="white-space:pre"> </span>/* setup */</div><div><span style="white-space:pre"> </span>g_object_set(G_OBJECT(appsrc), "caps",</div><div><span style="white-space:pre"> </span>gst_caps_new_simple("video/x-raw",</div><div><span style="white-space:pre"> </span>"format", G_TYPE_STRING, "RGB",</div><div><span style="white-space:pre"> </span>"width", G_TYPE_INT, 134,</div><div><span style="white-space:pre"> </span>"height", G_TYPE_INT, 134,</div><div><span style="white-space:pre"> </span>"framerate", GST_TYPE_FRACTION, 1, 1,</div><div><span style="white-space:pre"> </span>NULL), NULL);</div><div><span style="white-space:pre"> </span>gst_bin_add_many(GST_BIN(pipeline), appsrc, conv, videosink, NULL);</div><div><span style="white-space:pre"> </span>gst_element_link_many(appsrc, conv, videosink, NULL);</div><div><span style="white-space:pre"> </span>//g_object_set (videosink, "device", "/dev/video0", NULL);</div><div><span style="white-space:pre"> </span>/* setup appsrc */</div><div><span style="white-space:pre"> </span>g_object_set(G_OBJECT(appsrc),</div><div><span style="white-space:pre"> </span>"stream-type", 0,</div><div><span style="white-space:pre"> </span>"format", GST_FORMAT_TIME, NULL);</div><div><span style="white-space:pre"> </span>g_signal_connect(appsrc, "need-data", G_CALLBACK(cb_need_data), NULL);</div><div><br></div><div><span style="white-space:pre"> </span>/* play */</div><div><span style="white-space:pre"> </span>gst_element_set_state(pipeline, GST_STATE_PLAYING);</div><div><span style="white-space:pre"> </span>g_main_loop_run(loop);</div><div><br></div><div><span style="white-space:pre"> </span>if (gst_element_set_state(pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE)</div><div><span style="white-space:pre"> </span>{</div><div><span style="white-space:pre"> </span>g_printerr("Unable to set the pipeline to the playing state.\n");</div><div><span style="white-space:pre"> </span>}</div><div><br></div><div><span style="white-space:pre"> </span>// Free resources</div><div><span style="white-space:pre"> </span>gst_object_unref(GST_OBJECT(pipeline));</div><div><span style="white-space:pre"> </span>g_main_loop_unref(loop);</div><div><br></div><div><span style="white-space:pre"> </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">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">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_3626855293976323275AppleMailSignature" dir="ltr">Michael</div><div id="gmail-m_3626855293976323275AppleMailSignature" dir="ltr"><a href="http://www.ridgerun.com" target="_blank">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">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_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">saurabh9bora@gmail.com</a></div><div> <a href="mailto:saurabh9bora@outlook.com" target="_blank">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">gstreamer-devel@lists.freedesktop.org</a></span><br><span><a href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel" target="_blank">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">gstreamer-devel@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel" rel="noreferrer" target="_blank">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">saurabh9bora@gmail.com</a></div><div> <a href="mailto:saurabh9bora@outlook.com" target="_blank">saurabh9bora@outlook.com</a></div></div></div></div></div></div></div></div></div>