Home/docs/kernel/Hardware decoding

Hardware decoding

Decoding

All decoding goes through FFmpeg's avcodec interface, and DecoderBackend selects a software or hardware backend. When the hardware path is unavailable, the kernel falls back to software decoding and reports the switch to the host through the VideoDecoderChanged event.

Decoding backends#

BackendPlatformDescription
Cuda / NVDECLinux / NVIDIAUses a CUDA video decoding device.
VaapiLinux / Intel, AMDUses a VA-API render device.
SoftwareAllFFmpeg software decoding; the fallback target on every platform.
VideoToolboxmacOS / iOS / tvOSApple hardware decoding.
D3d11vaWindowsDirect3D 11 video acceleration.
MediaCodecAndroidSurface mode is the primary mode; falls back to ByteBuffer mode on failure.
AvCodecHarmonyOSAVCodec hardware decoding, covering H.264 / HEVC.

VideoDecodePreference determines the default backend for each platform: VideoToolbox on Apple platforms, D3D11VA on Windows, MediaCodec on Android, AVCodec on HarmonyOS, and on Linux CUDA / NVDEC first, then VA-API. All other platforms use software decoding.

Hardware path#

Hardware backend selection happens in open_raw. It picks the codec for the backend, queries the hardware configuration with avcodec_get_hw_config, and then creates the device context with av_hwdevice_ctx_create. If device creation fails, decoding falls back along the fallback chain to software decoding.

MediaCodec is the most unusual of these paths. In surface mode, the decoder binds AVMediaCodecDeviceContext.native_window and sets three options: ndk_codec=1, delay_flush=1, and erika_nonblocking=1. If creating or binding the surface fails, the playback thread rebuilds the decoder in ByteBuffer mode and reopens the stream. To the host, the whole process appears as a single VideoDecoderChanged event.

Fallback is explicit. Every backend switch publishes a VideoDecoderChanged event. The host can use it to update a diagnostics UI or just write a log entry. The statistics fields hardware_video_frames and software_video_frames count the frames decoded on each path.

Frame queues#

Decoded output goes into bounded queues, which decouple the playback thread from the rendering side. You can adjust the queue depths in PlayerConfig:

QueueDefault capacityConfig field
Video frames3video_frame_queue_capacity
Audio frames192audio_frame_queue_capacity
Subtitle frames16subtitle_frame_queue_capacity

When a queue is full, new frames are dropped; the statistics field video_frame_backpressure_drops counts the dropped video frames. The audio frame queue is drained by the audio clock, and its capacity is set by PlayerConfig.audio_frame_queue_capacity (AudioSyncConfig holds only synchronization parameters, not capacity).

Related statistics#

Decoding-related fields in ErikaPresenterStats:

FieldDescription
decoded_video_framesTotal number of decoded video frames.
hardware_video_framesNumber of hardware-decoded frames.
software_video_framesNumber of software-decoded frames.
zero_copy_video_framesNumber of frames imported into the renderer with zero copy.
cpu_video_frame_fallbacksNumber of frames that fell back to CPU upload after zero-copy import failed.
import_failuresNumber of times importing decoder output into the GPU failed.
video_frame_backpressure_dropsNumber of frames dropped because a queue was full.

For the details of zero-copy import, see Zero-copy import.

Software decoding fallback#

Version 0.2.1 completed software decoding for WMV / WMA and added 8-bit and 10-bit AV1 support through dav1d. When hardware decoding is unavailable or the device does not support the video format, software-decoded frames enter the same rendering pipeline. Windows also supports uploading software frames.

Software decoding threads · 0.3.0#

Software video decoding uses frame/slice multithreading by default, and FFmpeg chooses the thread count based on the codec and the runtime environment. Set ERIKA_VIDEO_DECODE_THREADS=1 to restore single-threaded decoding. When you test the pure software path, also set ERIKA_HWDEC=software.

When a hardware decoding backend internally falls back to a software pixel format, diagnostics and VideoDecoderChanged report the actual decoding path, the fallback reason, and the counts. You cannot tell whether hardware decoding is actually in use from the requested backend alone.

liberika v0.3.8 Section Kernel License MPL-2.0