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デコーダ#
| Backend | Platform | Description |
|---|---|---|
| Cuda / NVDEC | Linux / NVIDIA | Uses a CUDA video decoding device. |
| Vaapi | Linux / Intel, AMD | Uses a VA-API render device. |
| Software | All | FFmpeg software decoding; the fallback target on every platform. |
| VideoToolbox | macOS / iOS / tvOS | Apple hardware decoding. |
| D3d11va | Windows | Direct3D 11 video acceleration. |
| MediaCodec | Android | Surface mode is the primary mode; falls back to ByteBuffer mode on failure. |
| AvCodec | HarmonyOS | AVCodec 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:
| Queue | Default capacity | Config field |
|---|---|---|
| Video frames | 3 | video_frame_queue_capacity |
| Audio frames | 192 | audio_frame_queue_capacity |
| Subtitle frames | 16 | subtitle_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:
| Field | Description |
|---|---|
| decoded_video_frames | Total number of decoded video frames. |
| hardware_video_frames | Number of hardware-decoded frames. |
| software_video_frames | Number of software-decoded frames. |
| zero_copy_video_frames | Number of frames imported into the renderer with zero copy. |
| cpu_video_frame_fallbacks | Number of frames that fell back to CPU upload after zero-copy import failed. |
| import_failures | Number of times importing decoder output into the GPU failed. |
| video_frame_backpressure_drops | Number 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.