L4T Multimedia API Reference

28.2 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages


The Video CUDA Encoder sample demonstrates how to capture use libv4l2 API to encode H.264/H.265 video streams. Before beginning encoding, it uses the NVIDIA® CUDA® API to render a black rectangle on the input YUV image data; the same buffer is used and shared by the decode component.

Building and Running


To build:

  • Enter:
     $ cd $HOME/tegra_multimedia_api/samples/03_video_cuda_enc
     $ make

To run

  • Enter:
     $ ./video_cuda_enc <in-file> <in-width> <in-height> <encoder-type> <out-file> [OPTIONS]


   $ ./video_cuda_enc ../../data/Video/sample_outdoor_car_1080p_10fps.yuv 1920 1080 H264 test.h264
The video_cuda_enc sample consumes a YUV file. If you do not already have a YUV file, you can use the video_dec_cuda sample to generate one. For example:
 $ cd $HOME/tegra_multimedia_api/samples/02_video_dec_cuda/
 $ ./video_dec_cuda ../../data/Video/sample_outdoor_car_1080p_10fps.h264 H264 --disable-rendering -o ../../data/Video/sample_outdoor_car_1080p_10fps.yuv -f 2


The following diagram shows the flow through this sample.

The NvEGLImageFromFd NV-defined API returns an EGLImage pointer from the file descriptor buffer that is allocated by Tegra. CUDA then uses EGLImage buffer to render the rectangle. The buffer then performs the video encode operation and encodes it to either an H.264 or H.265 video stream.

Key Structure and Classes

The struct context_t global structure manages all of the resources in the application.

NvVideoConverter Description
class NvVideoEncoder This class contains all video encode related elements and functions.
EGLImageHKR Specifies the EGLImage used for CUDA processing.

The NvVideoConverter class packages all video converting related elements and functions. The sample uses the following key members:

NvVideoConverter Description
output_plane Specifies the output plane.
capture_plane Specifies the capture plane.
createVideoEncoder Static function to create video encode object.
setExtControls Sets external control to V4l2 device.
setOutputPlaneFormat Sets the output plane format.
setCapturePlaneFormat Checks if under error state.

Class NvVideoEncoder contains two key elements: output_plane and capture_plane. These objects are derived from class type NvV4l2ElementPlane. The sample uses the following key members:

NvV4l2ElementPlane Description
setupPlane Sets up the plane of V4l2 element.
deinitPlane Destroys the plane of V4l2 element.
setStreamStatus Starts/stops the stream.
setDQThreadCallback Sets the callback function of dqueue buffer thread.
startDQThread Starts the thread of dqueue buffer.
stopDQThread Stops the thread of dqueue buffer.
qBuffer Queues the V4l2 buffer.
dqBuffer Dqueues V4l2 buffer.
getNumBuffers Gets the number of V4l2 buffers.
getNthBuffer Gets the nth V4l2 buffer.

Two global functions are used to create and destroy EGLImage from the dmabuf file descriptor:

Global Function Description
NvEGLImageFromFd Creates an EGLImage from dmabuf fd.
NvDestroyEGLImage Destroys the EGLImage.