How to get the NvBuffer?

Hi Nvidia:
Frame data need to be read into NvBuffer by Function
int read_video_frame(std::ifstream * stream, NvBuffer &buffer) from H264 File,
but NvBuffer is created by Function
int NvBufferCreateEx(int * dmabuf_fd, NvBufferCreateParams * input_params),

how to get the parameter buffer I wanted in function read_video_frame by the output parameter dmabuf_fd of function NvBufferCreateEx?

Thanks a lot! Expected your reply as soon as possible!

Hi feng,
Please refer to the following function in 00_video_decode

static int
dump_dmabuf(int dmabuf_fd,
                unsigned int plane,
                std::ofstream * stream)

Hi DaneLLL:
Thank you for your reply!
Virtual address of NvBuffer’s each plane is MemMaped in function dump_dmabuf, and in main function of 00_video_decode function NvBufferMemSyncForCpu is used for hw memory cache sync for the CPU and then write data of each plane to the file stream.
if I MemMap Virtual address of NvBuffer’s each plane,it is still need reading data from file stream to Virtual address, is there any some interface to complete this operation? and my final operation is to use funtion like read_video_frame to read data from file stream to NvBuffer.
Thanks! Expected your reply!

hi feng,
you can refer to below code in 10_camera_recording:

            NvBufferParams par;
            NvBufferGetParams (fd, &par);
            void *ptr_y;
            uint8_t *ptr_cur;
            int i, j, a, b;
            NvBufferMemMap(fd, Y_INDEX, NvBufferMem_Write, &ptr_y);
            NvBufferMemSyncForCpu(fd, Y_INDEX, &ptr_y);
            ptr_cur = (uint8_t *)ptr_y + par.pitch[Y_INDEX]*START_POS + START_POS;

            // overwrite some pixels to put an 'N' on each Y plane
            // scan array_n to decide which pixel should be overwritten
            for (i=0; i < FONT_SIZE; i++) {
                for (j=0; j < FONT_SIZE; j++) {
                    a = i>>SHIFT_BITS;
                    b = j>>SHIFT_BITS;
                    if (array_n[a][b])
                        (*ptr_cur) = 0xff; // white color
                ptr_cur = (uint8_t *)ptr_y + par.pitch[Y_INDEX]*(START_POS + i)  + START_POS;
            NvBufferMemSyncForDevice (fd, Y_INDEX, &ptr_y);
            NvBufferMemUnMap(fd, Y_INDEX, &ptr_y);

It writes a few pixels in Y plane. For your usecase, you may need to write complete Y U V planes.

Hi DaneLLL:
Thanks for your reply!
If code as in 10_camera_recording you mentioned is used to realize my function,the data in file stream must be very clearly,which part of stream data is of Y Plane, which part of stream data is of U Plane, which part of stream data is of V Plane and its size must be known too. there is some difficulty for me.
function read_video_frame(std::ifstream * stream, NvBuffer & buffer) in file NvUtils.cpp realize the function to read frame data from file stream to NvBuffer,its code as below:
int read_video_frame(std::ifstream * stream, NvBuffer & buffer)
uint32_t i, j;
char *data;

for (i = 0; i < buffer.n_planes; i++)
    NvBuffer::NvBufferPlane &plane = buffer.planes[i];
    std::streamsize bytes_to_read =
        plane.fmt.bytesperpixel * plane.fmt.width;
    data = (char *);
    plane.bytesused = 0;
    for (j = 0; j < plane.fmt.height; j++)
        stream->read(data, bytes_to_read);
        if (stream->gcount() < bytes_to_read)
            return -1;
        data += plane.fmt.stride;
    plane.bytesused = plane.fmt.stride * plane.fmt.height;
return 0;

and in Function static int dump_dmabuf(int dmabuf_fd, unsigned int plane,std::ofstream * stream),NvBuffer Parameter can be got, combine these two function,is it possible to find some way to realize my function?

typedef struct _NvBufferParams
uint32_t dmabuf_fd;
void *nv_buffer;
uint32_t nv_buffer_size;
uint32_t pixel_format;
uint32_t num_planes;
uint32_t width[MAX_NUM_PLANES];
uint32_t height[MAX_NUM_PLANES];
uint32_t pitch[MAX_NUM_PLANES];
uint32_t offset[MAX_NUM_PLANES];
uint32_t psize[MAX_NUM_PLANES];
uint32_t layout[MAX_NUM_PLANES];
this struct data can be get by function int NvBufferGetParams(int dmabuf_fd, NvBufferParams * params). is there any to get the parameter in read_video_frame from NvBufferParams?(the key is to get the buffer.planes[i] in read_video_frame function).
Thanks! Is there any way? Expected your reply soon!

Hi Feng,

NvBufferMemMap(fd, Y_INDEX, NvBufferMem_Write, &ptr_y);

It gets mapped pointer to Y plane.

NvBufferParams par;
NvBufferGetParams (fd, &par);

It gets par.pitch[Y_PLANE], par.width[Y_PLANE], par.height[Y_PLANE]

With above information, you can copy your Y plane to the buffer pixel by pixel. it is same for U and V planes.