Jetson Linux API Reference

39.2 Release
decoder_unit_sample (video decode unit sample)

Overview

The decode_sample application is a unit level implementation that demonstrates how to use the buffer allocated by the libv4l2 component to decode H.264 video streams.

The application reads an H.264 elementary video file, decodes it, and writes the raw YUV buffer into a file without any extra memory copy.

CUDA Integration

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

  • CUDA Memory Types: Supports CUDA Device 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/decoder_unit_sample
     $ make
    

To run

  • Enter:
     $ ./decode_sample <in-file> <out-file>
    

Example

  $ ./decode_sample ../../data/Video/sample_outdoor_car_1080p_10fps.h264 out_sample_outdoor_car_1080p_10fps.yuv


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 decoder device.
  • The decoder device node is opened (/dev/v4l2-nvdec for Orin, /dev/nvidia0 & /dev/nvidia1 for OpenRM).
  • Decoder capabilities are queried using VIDIOC_QUERYCAP.
  • The Output Plane format is set first (required to be the first call before any control IDs for GPU).
  • External controls are set.
  • V4L2_EVENT_RESOLUTION_CHANGE event is subscribed to handle resolution changes.
  • The Output Plane receives input in bitstream format. The buffers are requested, queried, exported, mapped (MMAP memory type) and delivered to decoder for decoding.
  • Streaming is started on the Output Plane (VIDIOC_STREAMON).
  • A capture thread is created to handle the Capture Plane operations.
  • The Capture Plane transfers decoded frames to the application in YUV format.
  • The main thread reads encoded bitstream chunks and queues them to the Output Plane.
  • After reaching end-of-stream (EOS), all queued buffers are dequeued.
  • Cleanup: streams are stopped, buffers are unmapped/destroyed, and the device is closed.
  • For the Output Plane the application supports MMAP memory type. For the Capture Plane it supports DMABUF memory type.
  • The application takes decoded buffers from Capture Plane and dumps into a 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
decode_process Function loop to DQ and EnQ buffers on output plane till eos is signalled.
capture_thread A pointer to the thread handler for the decoding capture loop.
dq_event Function to dequeue an event.
dq_buffer Function to dequeue a buffer from the plane.
q_buffer Function to queue a buffer on the plane.
req_buffers_on_capture_plane Function to request for buffers on the decoder capture plane.
req_buffers_on_output_plane Function to request for buffers on the decoder output plane.
set_capture_plane_format Function to set the format on the decoder capture plane.
set_output_plane_format Function to set the format on the decoder output plane.
set_ext_controls Function to set the value of controls.
subscribe_event Function to subscribes to a V4L2 event.