CUDA GstBuffer output buffer with OpenCV GpuMat

Richard Pickler rpickler at
Thu Jun 8 13:05:21 UTC 2023

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.

Any ideas?  I've been pounding my head against this wall for a couple days

static GstFlowReturn
debayer_transform(GstBaseTransform *base,
                  GstBuffer *inbuf, GstBuffer *outbuf)
    Debayer *debayer = DEBAYER(base);
    GstCudaBaseTransform *btrans = GST_CUDA_BASE_TRANSFORM (base);

    if (gst_buffer_n_memory (outbuf) != 1) {
        GST_ERROR_OBJECT (debayer, "Invalid output buffer");
        return GST_FLOW_ERROR;

    GstMemory *mem;
    mem = gst_buffer_peek_memory (outbuf, 0);
    if (!gst_is_cuda_memory (mem)) {
        GST_ERROR_OBJECT (debayer, "Input buffer is not CUDA");
        std::cout << "Input buffer is not cuda" << std::endl;
        return GST_FLOW_ERROR;

    GstMapInfo map;
    if(!gst_buffer_map(inbuf, &map, GST_MAP_READ))
        std::cout << "Can't map" << std::endl;
        return GST_FLOW_ERROR;

    GstMapInfo outmap;
    if(!gst_buffer_map(outbuf, &outmap, (GstMapFlags)(GST_MAP_WRITE |
        std::cout << "Can't map" << std::endl;
        return GST_FLOW_ERROR;

    cv::Mat in(
        cv::Size(debayer->info.width, debayer->info.height), CV_8UC1,

    cv::cuda::GpuMat gpu_in;

    cv::cuda::GpuMat gpu_out(
        cv::Size(debayer->info.width, debayer->info.height),
        (char*), debayer->info.stride[0]);

    cv::cuda::demosaicing(gpu_in, gpu_out, cv::COLOR_BayerRG2BGR);
    cv::Mat out(gpu_out);
    cv::imwrite("foo.jpg", out);

    gst_buffer_unmap(inbuf, &map);
    gst_buffer_unmap(outbuf, &outmap);
    return GST_FLOW_OK;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the gstreamer-devel mailing list