Jetson Linux API Reference

39.2 Release
encoder_unit_sample (video encode unit samples)

Overview

The encode_sample application is a unit level implementation that demonstrates how to encode H.264 video streams.

The application reads YUV input buffers from a file, performs H.264 video encoding, and saves the encoded bitstream to an elementary .264 file.

The application runs on file source simulated input buffers, and so does not require a camera.

CUDA Integration

The application includes comprehensive CUDA support for GPU-accelerated encoding:

  • CUDA Memory Types: Supports CUDA Pinned memory allocation
  • Memory Management: Intelligent memory type selection based on GPU availability
  • Performance Optimization: GPU-accelerated processing paths for enhanced performance

Thor Platform Support

On Thor platforms, the application utilizes GPU-accelerated encoding for:

  • Enhanced GPU resource management and allocation
  • Optimized memory bandwidth utilization between CPU and GPU
  • Better integration with CUDA-based system-wide resource scheduling

Supported video formats are:

  • H.264


Building and Running

Prerequisites

To build

  • Enter:
     $ cd /usr/src/jetson_multimedia_api/samples/unittest_samples/encoder_unit_sample
     $ make
    

To run

  • Enter:
    $ ./encode_sample <in-file> <in-width> <in-height> <out-file>
    

Example

   $ ./encode_sample ../../data/Video/sample_outdoor_car_1080p_10fps.yuv 1920 1080 sample_outdoor_car_1080p_10fps.h264


Flow

The following steps show the flow through this sample.

  • If GPU is enabled, cuInit(0) is called to initialize the CUDA driver before opening the encoder device.
  • The encoder device node is opened (/dev/v4l2-nvenc for Orin, /dev/nvidia0 & /dev/nvidia1 for OpenRM).
  • Encoder capabilities are queried using VIDIOC_QUERYCAP.
  • Capture Plane format is set first (required - must be set before Output Plane format along with frame width and height).
  • Output Plane format is then set.
  • External controls and parameters are set.
  • Output Plane buffers are requested, queried, exported, and mapped (MMAP memory type).
  • Capture Plane buffers are requested, queried, exported, and mapped (MMAP memory type).
  • V4L2_EVENT_EOS event is subscribed to detect end of stream.
  • Streaming is started on both Output Plane and Capture Plane (VIDIOC_STREAMON).
  • A dequeue thread is created to handle the Capture Plane operations.
  • All empty Capture Plane buffers are initially enqueued.
  • The main thread reads raw YUV frames from the input file and queues them to the Output Plane.
  • After reaching end-of-stream (EOS), wait for the DQ thread to complete processing all buffers.
  • Cleanup: streams are stopped on both planes, buffers are unmapped, and the device is closed.
  • The application supports MMAP memory type for both Output Plane and Capture Plane.
  • Encoded H.264 bitstream is written to the specified output file.


Key Structure and Classes

The sample uses the following key class:

Element Description
Buffer Class modeled on v4l2_buffer structure.

The key structures and functions used in the sample are:

Function Description
encoder_process_blocking Function loop to DQ and EnQ buffers on output plane till eos is signalled.
dq_buffer Function to dequeue a buffer from the plane.
capture_plane_callback Callback function when enc_cap_thread is created.
q_buffer Function to queue a buffer on the plane.
req_buffers_on_capture_plane Function to request for buffers on the encoder capture plane.
req_buffers_on_output_plane Function to request for buffers on the encoder output plane.
set_capture_plane_format Function to set the format on the encoder capture plane.
set_output_plane_format Function to set the format on the encoder output plane.
set_ext_controls Function to set the value of controls.
subscribe_event Function to subscrive to a V4L2 event.