Hardware image encoding error in deepstream probe callback function

Please provide complete information as applicable to your setup.
• Hardware Platform (Jetson / GPU):jetson orin nano
• DeepStream Version:6.2

my pipeline is nvurisrcbin-->nvstreammux-->nvinfer-->fakesink
GstPadProbeReturn 
sink_sink_pad_buffer_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
{
    GstBuffer *buf = (GstBuffer *)info->data;

    NvDsMetaList *l_frame = NULL;
    NvDsMetaList *l_obj = NULL;

    NvBufSurface *nvbuf_surface = NULL;
    GstMapInfo in_map_info;

    if (!gst_buffer_map(buf, &in_map_info, GST_MAP_READ))
    {
        return GST_PAD_PROBE_DROP;
    }

    NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta(buf);

    nvbuf_surface = (NvBufSurface *)in_map_info.data;

    for (l_frame = batch_meta->frame_meta_list; l_frame != NULL; l_frame = l_frame->next)
    {
        NvDsFrameMeta *frame_meta = (NvDsFrameMeta *)(l_frame->data);
        int frame_index = frame_meta->batch_id;
        auto &s = nvbuf_surface->surfaceList[frame_index];

        g_printerr("Surface check: index=%d colorFormat=%d layout=%d memType=%d\n",
                   frame_index,
                   s.colorFormat,
                   s.layout,
                   nvbuf_surface->memType);

        safe_encode_to_jpeg(nvbuf_surface, frame_meta->batch_id, "/home/nvidia/code/tgsd_code/test2/frame.jpg")
    }
    gst_buffer_unmap(buf, &in_map_info);

    return GST_PAD_PROBE_OK;
}

In the above probe callback function

colorFormat=NVBUF_COLOR_FORMAT_NV12
layout=NVBUF_LAYOUT_PITCH 
memType=NVBUF_MEM_SURFACE_ARRAY

When I add the custom function safe_encode_to_jpeg for encoding and saving images (nvbuf_Surface, frame_ceta ->batch_id, “/home/Nvidia/code/tgsd_comde/test2/frame. jpg”)

Specific Function Implementation:

static std::unique_ptr<NvJPEGEncoder> jpegEnc = nullptr;

bool 
safe_encode_to_jpeg(NvBufSurface *surface, int frame_index, const std::string &filename)
{
    if (!surface)
    {
        std::cerr << "❌ surface is null." << std::endl;
        return false;
    }

    NvBufSurface *converted = nullptr;
    NvBufSurfaceCreateParams params;
    memset(&params, 0, sizeof(params));
    params.gpuId = surface->gpuId;
    params.width = surface->surfaceList[frame_index].width;
    params.height = surface->surfaceList[frame_index].height;
    params.layout = NVBUF_LAYOUT_PITCH;
    params.colorFormat = NVBUF_COLOR_FORMAT_NV12;
    params.memType = NVBUF_MEM_SURFACE_ARRAY;

    if (NvBufSurfaceCreate(&converted, 1, &params) != 0)
    {
        std::cerr << "❌ Failed to create converted surface." << std::endl;
        return false;
    }

    NvBufSurfTransformParams trans_params;
    memset(&trans_params, 0, sizeof(trans_params));
    trans_params.transform_flag = NVBUFSURF_TRANSFORM_FILTER;
    trans_params.transform_filter = NvBufSurfTransformInter_Default;

    NvBufSurfTransformConfigParams config_params;
    memset(&config_params, 0, sizeof(config_params));
    config_params.compute_mode = NvBufSurfTransformCompute_Default;
    config_params.gpu_id = surface->gpuId;
    NvBufSurfTransformSetSessionParams(&config_params);

    if (NvBufSurfTransform(surface, converted, &trans_params) != 0)
    {
        std::cerr << "❌ Transform failed." << std::endl;
        NvBufSurfaceDestroy(converted);
        return false;
    }

    cudaDeviceSynchronize();

    if (!jpegEnc)
    {
        jpegEnc.reset(NvJPEGEncoder::createJPEGEncoder("jpegenc"));
        if (!jpegEnc)
        {
            std::cerr << "❌ Failed to create NvJPEGEncoder." << std::endl;
            NvBufSurfaceDestroy(converted);
            return false;
        }
    }

    unsigned char *jpeg_buf = nullptr;
    unsigned long jpeg_size = 0;
    int fd = converted->surfaceList[0].bufferDesc;

    if (jpegEnc->encodeFromFd(fd, JCS_YCbCr, &jpeg_buf, jpeg_size, 80) != 0 || !jpeg_buf)
    {
        std::cerr << "❌ JPEG encode failed." << std::endl;
        NvBufSurfaceDestroy(converted);
        return false;
    }

    std::ofstream ofs(filename, std::ios::binary);
    ofs.write(reinterpret_cast<const char *>(jpeg_buf), jpeg_size);
    ofs.close();

    std::cout << "✅ JPEG saved: " << filename << std::endl;
    NvBufSurfaceDestroy(converted);
    return true;

}

But in jpegEnc ->encodeFromFd (fd, JCS-YCbCr,&jpegobuf, jpeg_stize, 80)= 0 core crashes occur

I don’t know where the problem lies

We don’t recommend you use it this way if you want to save pictures in DeepStream. Please use our nvds_obj_enc_process API by referring to our deepstream-image-meta-test.

There is no update from you for a period, assuming this is not an issue anymore. Hence we are closing this topic. If need further support, please open a new one. Thanks