<div dir="ltr"><div>I got it working, and I just wanted to close the loop on this in cse someone is searching around for it in the future. The only mistake I had was how I got the data pointer. After changing that to GST_VIDEO_FRAME_PLANE_DATA, it all worked!</div><div><br> cv::cuda::GpuMat gpu_out(<br> cv::Size(<br> GST_VIDEO_FRAME_WIDTH(&out_frame),<br> GST_VIDEO_FRAME_HEIGHT(&out_frame)<br> ),<br> CV_8UC3,<br> GST_VIDEO_FRAME_PLANE_DATA(&out_frame, 0),<br> GST_VIDEO_FRAME_PLANE_STRIDE(&out_frame, 0));</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 8, 2023 at 5:57 PM Richard Pickler <<a href="mailto:rpickler@gmail.com">rpickler@gmail.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="ltr"><div>Thanks for the speedy reply. I've replaced the standard buffer_map with the following <br></div><div><br></div><div><br> GstVideoFrame out_frame;<br> if (!gst_video_frame_map (&out_frame, &debayer->info, outbuf,<br> (GstMapFlags)(GST_MAP_WRITE | GST_MAP_CUDA))) {<br> //gst_video_frame_unmap (&in_frame);<br> GST_ERROR_OBJECT (debayer, "Failed to map output buffer");<br> std::cout << "Failed to map output buffer" << std::endl;<br> return GST_FLOW_ERROR;<br> }</div><div><br></div><div>And the gpuMat initialization:<br></div><div><span><br> cv::cuda::GpuMat gpu_out(<br> cv::Size(<br></span> GST_VIDEO_FRAME_WIDTH(&out_frame),<br> GST_VIDEO_FRAME_HEIGHT(&out_frame)),<br> CV_8UC3,<br> (char*) out_frame.data,<br> GST_VIDEO_FRAME_PLANE_STRIDE(&out_frame, 0));</div><div><br></div><div>But no such luck. Now I am the proud recipient of black frames. </div><div><br></div><div>You're
correct that the stride is different. 5760 vs 6144 on my 1920 wide
image. That seems to cause the demosaic function to overrun and cause a
memory exception in cuda. I then changed the gpumat initialization to:</div><div><br></div><div><span><br> cv::cuda::GpuMat gpu_out(<br> cv::Size(<br></span> out_frame.map[0].size / GST_VIDEO_FRAME_HEIGHT(&out_frame) / 3,<br> GST_VIDEO_FRAME_HEIGHT(&out_frame)),<br> CV_8UC3,<br> (char*) out_frame.data,<br> GST_VIDEO_FRAME_PLANE_STRIDE(&out_frame, 0));</div><div><br></div><div>Which doesn't crash, but also doesn't produce an image.</div><div><br></div><div>(sorry for duplicate. I forgot to reply-all)<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 8, 2023 at 2:22 PM Seungha Yang <<a href="mailto:seungha@centricular.com" target="_blank">seungha@centricular.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><div lang="KO"><div><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US">Hi,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US">GstCudaMemory’s stride is likely different from default one. <u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US">You will need to use gst_video_frame_map() and GST_VIDEO_FRAME_PLANE_STRIDE(frame, 0), or GstCudaMemory.info.stride[0]<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US">Regards,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US">Seungha<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10pt;font-family:"\00b9d1\00c740 \00ace0\00b515"" lang="EN-US"><u></u> <u></u></span></p><div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm"><p class="MsoNormal"><b><span style="font-size:11pt;font-family:"Calibri",sans-serif" lang="EN-US">From:</span></b><span style="font-size:11pt;font-family:"Calibri",sans-serif" lang="EN-US"> gstreamer-devel <<a href="mailto:gstreamer-devel-bounces@lists.freedesktop.org" target="_blank">gstreamer-devel-bounces@lists.freedesktop.org</a>> <b>On Behalf Of </b>Richard Pickler via gstreamer-devel<br><b>Sent:</b> Thursday, June 8, 2023 10:05 PM<br><b>To:</b> <a href="mailto:gstreamer-devel@lists.freedesktop.org" target="_blank">gstreamer-devel@lists.freedesktop.org</a><br><b>Cc:</b> Richard Pickler <<a href="mailto:rpickler@gmail.com" target="_blank">rpickler@gmail.com</a>><br><b>Subject:</b> CUDA GstBuffer output buffer with OpenCV GpuMat<u></u><u></u></span></p></div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><div><div><p class="MsoNormal"><span lang="EN-US">I'm struggling to use the recent cuda improvements in gstreamer, using an OpenCV GpuMat to write to a CUDAMemory outbuf. I'm was successful in getting an appropriate buffer through decide/propose_allocation, but simply cannot seem to write to a buffer properly. In my transform function below, I'm able to map the buffer and have the demosaic function run properly, but the output stream comes out garbage. I placed a simple cv::imwrite in the code, and sure enough, the data comes through the function properly, it's just not in a structure that gstreamer is happy with.<u></u><u></u></span></p></div><div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p></div><div><p class="MsoNormal"><span lang="EN-US">Any ideas? I've been pounding my head against this wall for a couple days now.<u></u><u></u></span></p></div><div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p></div><div><p class="MsoNormal" style="margin-bottom:12pt"><span lang="EN-US">static GstFlowReturn<br>debayer_transform(GstBaseTransform *base, <br> GstBuffer *inbuf, GstBuffer *outbuf)<br>{<br> Debayer *debayer = DEBAYER(base);<br> GstCudaBaseTransform *btrans = GST_CUDA_BASE_TRANSFORM (base);<br><br> if (gst_buffer_n_memory (outbuf) != 1) {<br> GST_ERROR_OBJECT (debayer, "Invalid output buffer");<br> return GST_FLOW_ERROR;<br> }<br><br> GstMemory *mem;<br> mem = gst_buffer_peek_memory (outbuf, 0);<br> if (!gst_is_cuda_memory (mem)) {<br> GST_ERROR_OBJECT (debayer, "Input buffer is not CUDA");<br> std::cout << "Input buffer is not cuda" << std::endl;<br> return GST_FLOW_ERROR;<br> }<br><br> GstMapInfo map;<br> if(!gst_buffer_map(inbuf, &map, GST_MAP_READ))<br> {<br> std::cout << "Can't map" << std::endl;<br> return GST_FLOW_ERROR;<br> }<br> <br> GstMapInfo outmap;<br> if(!gst_buffer_map(outbuf, &outmap, (GstMapFlags)(GST_MAP_WRITE | GST_MAP_CUDA)))<br> {<br> std::cout << "Can't map" << std::endl;<br> return GST_FLOW_ERROR;<br> }<br><br> cv::Mat in(<br> cv::Size(debayer->info.width, debayer->info.height), CV_8UC1,<br> (char*) map.data);<br><br> cv::cuda::GpuMat gpu_in;<br><br> cv::cuda::GpuMat gpu_out(<br> cv::Size(debayer->info.width, debayer->info.height),<br> CV_8UC3,<br> (char*) outmap.data, debayer->info.stride[0]);<br><br> gpu_in.upload(in);<br> cv::cuda::demosaicing(gpu_in, gpu_out, cv::COLOR_BayerRG2BGR);<br> cv::Mat out(gpu_out);<br> cv::imwrite("foo.jpg", out);<br><br> gst_buffer_unmap(inbuf, &map);<br> gst_buffer_unmap(outbuf, &outmap);<br> return GST_FLOW_OK;<br>}<br><br><u></u><u></u></span></p></div><div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p></div><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p></div></div></div></div></blockquote></div>
</blockquote></div>