L4T Multimedia API Reference

27.1 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Video CUDA Encoder Sample


The Video CUDA Encoder sample demonstrates how to capture use libv4l2 API to encode H.264/H.265 video streams. It starts using CUDA API before encoding to render a black rectangle on the input YUV image data; the same buffer is used and shared by the decode component.

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.


Before running the sample, you must have the following:

  • README that provides details on the environment requirements to build and run the sample

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 Sets the capture plane format.
isInError 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.