Help,dump image from nvv4l2decoder problem

Hi Guys,

My device is Jetson NX, and deepstream 5.0;

I‘m trying to dump the h264 decoded data from nvv4l2decoder element。
I add a probe to the src of nvv4l2decoder,my code is like this:

After run the code, i got the output:
batch:1 numfilled:1 memtype:4 surfacearrary:4
size is : 3538944 w:1920 h:1080 pitch:1920 format:6 nv12is:6 layout:1
numer plans:2
plan: h: 1080 w:1920 bytes:1 pitch:1920 offset: 0 psize:2228224
plan: h: 540 w:960 bytes:2 pitch:1920 offset: 2228224 psize:1310720

The width, height, pitch is right, the memtype is surfacearrary, frame_format is NV12,but the image i saved is wrong, i don’t know why. And I have already set “enable-full-frame” true.
Please give me some help, thanks.

int detect_num = 0;
GstPadProbeReturn nvdecode_probe_callback(GstPad *pad, GstPadProbeInfo *pProbeInfo, gpointer u_data)
{
    int *sub_dev_id = (int *)u_data;
    GstBuffer *buf = (GstBuffer *)pProbeInfo->data;
    GstMapInfo map_info;

    if (buf == NULL || !gst_buffer_map(buf, &map_info, GST_MAP_READ))
    {
        printf("gst_buffer_map() error!");
        return GST_PAD_PROBE_OK;
    }

    if (detect_num == 0)
    {
        detect_num++;
        NvBufSurface *surface = (NvBufSurface *)map_info.data;

        printf("batch:%d numfilled:%d memtype:%d surfacearrary:%d \n", surface->batchSize, surface->numFilled, surface->memType, NVBUF_MEM_SURFACE_ARRAY);

        int index = 0;
        int size = surface->surfaceList[index].dataSize;

        gint frame_width = (gint)surface->surfaceList[index].width;
        gint frame_height = (gint)surface->surfaceList[index].height;
        gint frame_step = (gint)surface->surfaceList[index].pitch;
        gint frame_format = (gint)surface->surfaceList[index].colorFormat;
        gint frame_layout = (gint)surface->surfaceList[index].layout;
        printf("size is : %d w:%d h:%d pitch:%d format:%d nv12is:%d layout:%d \n", size, frame_width, frame_height, frame_step, frame_format, NVBUF_COLOR_FORMAT_NV12, frame_layout);

        NvBufSurfacePlaneParams *pParams = &surface->surfaceList[index].planeParams;
        printf("numer plans:%d \n", pParams->num_planes);

        for (int i = 0; i < pParams->num_planes; i++)
        {
            printf("plan: h: %d  w:%d bytes:%d pitch:%d offset: %d psize:%d \n", pParams->height[i], pParams->width[i], pParams->bytesPerPix[i], pParams->pitch[i],
            pParams->offset[i], pParams->psize[i]);
        }

        NvBufSurfaceMap(surface, -1, -1, NVBUF_MAP_READ);

        char *src_data = (char *)malloc(surface->surfaceList[0].dataSize);
        unsigned int offset = 0;
        for (unsigned int num_planes = 0; num_planes < pParams->num_planes; num_planes++)
        {
            if (num_planes > 0){
                offset += pParams->height[num_planes - 1] * (pParams->bytesPerPix[num_planes - 1] * pParams->width[num_planes - 1]);
            }
                
            for (unsigned int h = 0; h < pParams->height[num_planes]; h++)
            {
                memcpy((void *)(src_data + offset + h * pParams->bytesPerPix[num_planes] * pParams->width[num_planes]),
                       (void *)((char *)surface->surfaceList[index].mappedAddr.addr[num_planes] + h * pParams->pitch[num_planes]),
                       pParams->bytesPerPix[num_planes] * pParams->width[num_planes]);
            }
        }

        NvBufSurfaceUnMap (surface, -1, -1);

        cv::Mat frame = cv::Mat(1080 * 1.5, 1920, CV_8UC1, src_data, 1920);
        cv::Mat out_mat = cv::Mat(cv::Size(1920, 1080), CV_8UC3);
        cv::cvtColor(frame, out_mat, CV_YUV2BGR_NV12);
        cv::imwrite("test.jpg", out_mat);

        free(src_data);
    }

    gst_buffer_unmap(buf, &map_info);
    return GST_PAD_PROBE_OK;
}

Before you dump data from NvBufSurface, you need to check memType, format and layout first. Different memType, format and layout, the dump method is different. DeepStream SDK FAQ - Intelligent Video Analytics / DeepStream SDK - NVIDIA Developer Forums

Thanks, i will make a try.
AS my code above, memtype is NVBUF_MEM_SURFACE_ARRAY, FrameForamt is NVBUF_COLOR_FORMAT_NV12,Layout is NVBUF_LAYOUT_BLOCK_LINEAR, what’s the meaning of NVBUF_LAYOUT_BLOCK_LINEAR?

What is the difference between NvBufferLayout_Pitch NvBufferLayout_BlockLinear in nvbuf_utils.h? - Jetson & Embedded Systems / Jetson AGX Xavier - NVIDIA Developer Forums

Hi fiona,may i ask a question,
“printf(" layout = %d\n", sur->layout);”
what’s the output value in your patch,NVBUF_LAYOUT_BLOCK_LINEAR or NVBUF_LAYOUT_BLOCK_PITCH