Watching an official client's stream failed partly because the app decoded too little: no AV1 (the official client prefers it on modern GPUs), and H.264 only with Cisco's OpenH264 downloaded. FFmpeg, already loaded at runtime for the encoders, now decodes too, never linked or shipped: the platform's hardware decoders through FFmpeg's hwaccel API (VA-API and NVDEC on Linux, D3D11VA and DXVA2 on Windows, VideoToolbox on macOS) for AV1, HEVC, VP9, H.264 and VP8, then FFmpeg's software ones (dav1d, libaom, its own HEVC, VP9, H.264, VP8). Hardware decoders get AVCodecContext.hw_device_ctx, located next to hw_frames_ctx from the AVOption offsets of its neighbours and checked in a new context like the encoders' fields; FFmpeg's default get_format then picks the device's surfaces. Pictures are copied out of the GPU into a frame of our own and into the caller's VideoFrame through the new VideoDecoder::decode_into, both reused, so a running decoder allocates nothing per picture on our side. Slice threads only and dav1d with a frame delay of one keep the latency at zero. A self-test per decoder, run once per process in parallel with the encoders' probe, decodes a three-frame clip of each codec made by our own encoders (src/ffmpeg/clips): each picture at once, at its size, above 30 dB against the test picture, and on the GPU for a hardware decoder, since FFmpeg silently decodes in software when the GPU lacks the codec (VP8 on AMD). Codecs now orders a ladder per codec (decoders_for): hardware, FFmpeg's software decoders, then the built-in libvpx, OpenH264 and dav1d, under a DecoderPreference like the encoders'; decoders() lists every codec one of them decodes, best first (AV1, HEVC, VP9, H.264, VP8). The report lists the decoders too. Tested here (RX 7900 GRE, FFmpeg 9.0.1): VA-API decodes AV1, HEVC, VP9 and H.264 on the GPU, and every encoder of the machine decodes with every decoder of its codec above 36 dB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
2.9 KiB
Rust
88 lines
2.9 KiB
Rust
//! Encoder and decoder factories found at runtime.
|
|
//!
|
|
//! - FFmpeg (feature `ffmpeg`, desktop): VA-API, NVENC, Quick Sync, AMF,
|
|
//! Media Foundation and VideoToolbox encoders, plus software ones FFmpeg
|
|
//! wraps (x264, OpenH264, SVT-AV1, rav1e, libaom); one factory per backend
|
|
//! that passed its self-test ([`crate::ffmpeg::probe`]). Decoders likewise:
|
|
//! VA-API, NVDEC, D3D11VA, DXVA2 and VideoToolbox, then dav1d and FFmpeg's
|
|
//! own ([`crate::ffmpeg::decoder::probe`]).
|
|
//! - Android: MediaCodec ([`super::mediacodec`]).
|
|
|
|
use crate::Result;
|
|
use crate::codec::{
|
|
Codec, DecoderBackend, EncoderBackend, EncoderConfig, VideoDecoder, VideoEncoder,
|
|
};
|
|
|
|
/// Creates encoders of one backend.
|
|
pub trait EncoderFactory: Send + Sync {
|
|
/// Short name for logs and settings (`"h264_vaapi"`, `"mediacodec"`).
|
|
fn name(&self) -> &'static str;
|
|
|
|
/// Codecs this backend encodes, best first.
|
|
fn codecs(&self) -> Vec<Codec>;
|
|
|
|
fn create(&self, codec: Codec, config: &EncoderConfig) -> Result<Box<dyn VideoEncoder>>;
|
|
|
|
/// A GPU / OS encoder (the default), as opposed to a software encoder
|
|
/// reached through the same API (FFmpeg's libx264, ...).
|
|
fn is_hardware(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
/// Whether the automatic choice may use it (not for encoders with a long
|
|
/// delay; those are used only when named in the settings).
|
|
fn is_automatic(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
/// The backend its encoders report.
|
|
fn backend(&self) -> EncoderBackend {
|
|
EncoderBackend::Hardware(self.name())
|
|
}
|
|
}
|
|
|
|
/// Encoder factories usable on this machine: FFmpeg's backends that passed
|
|
/// their self-test, then MediaCodec (Android).
|
|
pub fn probe() -> Vec<Box<dyn EncoderFactory>> {
|
|
#[allow(unused_mut)]
|
|
let mut found: Vec<Box<dyn EncoderFactory>> = Vec::new();
|
|
#[cfg(feature = "ffmpeg")]
|
|
for factory in crate::ffmpeg::FfmpegFactory::available() {
|
|
found.push(Box::new(factory));
|
|
}
|
|
#[cfg(target_os = "android")]
|
|
if let Some(f) = super::mediacodec::probe() {
|
|
found.push(Box::new(f));
|
|
}
|
|
found
|
|
}
|
|
|
|
/// Creates decoders of one backend for one codec: FFmpeg's, or one an
|
|
/// application adds ([`Codecs::with_decoder`](super::Codecs::with_decoder)).
|
|
pub trait DecoderFactory: Send + Sync {
|
|
/// The backend its decoders are (for the ladder, logs and settings).
|
|
fn backend(&self) -> DecoderBackend;
|
|
|
|
fn codec(&self) -> Codec;
|
|
|
|
/// The API or library behind it (`VA-API`, `dav1d`, ...), for the UI.
|
|
fn api(&self) -> &'static str;
|
|
|
|
/// A GPU decoder: left out when hardware decoding is off.
|
|
fn is_hardware(&self) -> bool;
|
|
|
|
fn create(&self) -> Result<Box<dyn VideoDecoder>>;
|
|
}
|
|
|
|
/// Decoder factories usable on this machine: FFmpeg's decoders that passed
|
|
/// their self-test, hardware first.
|
|
pub fn probe_decoders() -> Vec<Box<dyn DecoderFactory>> {
|
|
#[allow(unused_mut)]
|
|
let mut found: Vec<Box<dyn DecoderFactory>> = Vec::new();
|
|
#[cfg(feature = "ffmpeg")]
|
|
for factory in crate::ffmpeg::decoder::FfmpegDecoderFactory::available() {
|
|
found.push(Box::new(factory));
|
|
}
|
|
found
|
|
}
|