The signalling computes profile-level-id from the stream's size, frame rate and bitrate, but nothing told the encoder, so every wrapper picked its own level. Measured on this GPU: h264_amf writes level 4.2 into the SPS of a 720p30 stream whose offer says 3.1, and 5.0 for a 1440p60 stream offered as 5.1. A viewer that sizes its decoder from our SDP can refuse a stream that declares more than the offer did, which is what breaks official clients. Compute the level from ITU-T Table A-1 and set it on both the codec context and the wrapper's private option. A new test parses the SPS of every usable H.264 backend at two sizes and both profiles and asserts profile_idc against the offer and level_idc never above it; with the level set, all three backends now emit exactly the offered level. The level table is a second copy of the one in voelin-stream, because neither crate can depend on the other, so a unit test pins them together over a spread of sizes, rates, bitrates and profiles. Remaining difference, only in the constraint-flag byte and left alone: the offer claims Constrained High (0c) and Constrained Baseline (e0) as WebRTC implementations conventionally do; the VA-API and AMF encoders emit 0c for the former, libx264 emits 00, and all three emit 40 or c0 for the latter. The bits do not change what a decoder can decode. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1530 lines
49 KiB
Rust
1530 lines
49 KiB
Rust
//! Video encoders through FFmpeg: the backends, their realtime settings, the
|
|
//! encoder session and the startup self-test.
|
|
//!
|
|
//! Every backend is configured for live streaming: no B-frames, no
|
|
//! lookahead, keyframes only at the start and on request (an "infinite"
|
|
//! GOP, forced IDR), constant bitrate. The bitrate changes on the running
|
|
//! encoder where FFmpeg's wrapper applies it (x264, NVENC, Quick Sync);
|
|
//! elsewhere at the next keyframe, or at once when it drops below half
|
|
//! (congestion). Frame and packet buffers are allocated once per session
|
|
//! and reused; VA-API surfaces come from a pool.
|
|
#![allow(unsafe_code)]
|
|
|
|
use std::collections::VecDeque;
|
|
use std::ffi::c_int;
|
|
use std::sync::OnceLock;
|
|
use std::time::{Duration, Instant};
|
|
|
|
use super::layout::{self, HwFramesFields};
|
|
use super::sys::{self, FrameHead, PacketHead, Ptr, Rational, cstr};
|
|
use super::{Ffmpeg, take_log};
|
|
use crate::capture::{DmaBufRef, drm_fourcc};
|
|
use crate::codec::hw::EncoderFactory;
|
|
use crate::codec::{
|
|
Codec, ContentHint, EncodedChunk, EncodedFrame, EncoderBackend, EncoderConfig, H264Profile,
|
|
VideoEncoder,
|
|
};
|
|
use crate::convert;
|
|
use crate::frame::{FrameData, VideoFrame};
|
|
use crate::{Error, Result};
|
|
|
|
/// How a backend takes frames.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Input {
|
|
/// Planar YUV 4:2:0 in system memory.
|
|
Yuv420p,
|
|
/// NV12 in system memory.
|
|
Nv12,
|
|
/// NV12 uploaded into a VA-API surface pool (or DMA-BUFs mapped to
|
|
/// surfaces).
|
|
Vaapi,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum BackendKind {
|
|
/// A GPU or OS encoder.
|
|
Hardware,
|
|
/// A software encoder that FFmpeg wraps (x264, SVT-AV1, ...).
|
|
Software,
|
|
}
|
|
|
|
/// One FFmpeg encoder this crate knows how to drive.
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub struct BackendSpec {
|
|
/// FFmpeg's encoder name, also the name in settings
|
|
/// (`stream.encoder_backend`).
|
|
pub name: &'static str,
|
|
pub codec: Codec,
|
|
/// The API or library behind it, for the UI.
|
|
pub api: &'static str,
|
|
pub kind: BackendKind,
|
|
pub input: Input,
|
|
/// FFmpeg applies bitrate changes to the running encoder.
|
|
pub dynamic_bitrate: bool,
|
|
/// Keyframe interval that means "only on request" for this wrapper.
|
|
pub infinite_gop: i64,
|
|
}
|
|
|
|
const INFINITE: i64 = 1 << 30;
|
|
|
|
macro_rules! backend {
|
|
($name:literal, $codec:ident, $api:literal, $kind:ident, $input:ident, $dynamic:literal, $gop:expr) => {
|
|
BackendSpec {
|
|
name: $name,
|
|
codec: Codec::$codec,
|
|
api: $api,
|
|
kind: BackendKind::$kind,
|
|
input: Input::$input,
|
|
dynamic_bitrate: $dynamic,
|
|
infinite_gop: $gop,
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Every backend, in order of preference within its codec and kind. The
|
|
/// ones FFmpeg or the platform lacks fail the probe and are skipped.
|
|
pub static BACKENDS: &[BackendSpec] = &[
|
|
// H.264
|
|
backend!("h264_nvenc", H264, "NVENC", Hardware, Yuv420p, true, INFINITE),
|
|
backend!("h264_amf", H264, "AMF", Hardware, Nv12, false, 0),
|
|
backend!("h264_vaapi", H264, "VA-API", Hardware, Vaapi, false, INFINITE),
|
|
backend!("h264_qsv", H264, "Quick Sync", Hardware, Nv12, true, 65535),
|
|
backend!("h264_videotoolbox", H264, "VideoToolbox", Hardware, Nv12, false, 0),
|
|
backend!("h264_mf", H264, "Media Foundation", Hardware, Nv12, false, INFINITE),
|
|
backend!("libx264", H264, "x264", Software, Yuv420p, true, INFINITE),
|
|
backend!("libopenh264", H264, "OpenH264", Software, Yuv420p, false, INFINITE),
|
|
// HEVC (offered last: only used when a viewer takes nothing else)
|
|
backend!("hevc_nvenc", H265, "NVENC", Hardware, Yuv420p, true, INFINITE),
|
|
backend!("hevc_amf", H265, "AMF", Hardware, Nv12, false, 0),
|
|
backend!("hevc_vaapi", H265, "VA-API", Hardware, Vaapi, false, INFINITE),
|
|
backend!("hevc_qsv", H265, "Quick Sync", Hardware, Nv12, true, 65535),
|
|
backend!("hevc_videotoolbox", H265, "VideoToolbox", Hardware, Nv12, false, 0),
|
|
backend!("hevc_mf", H265, "Media Foundation", Hardware, Nv12, false, INFINITE),
|
|
// AV1
|
|
backend!("av1_nvenc", Av1, "NVENC", Hardware, Yuv420p, true, INFINITE),
|
|
backend!("av1_amf", Av1, "AMF", Hardware, Nv12, false, 0),
|
|
backend!("av1_vaapi", Av1, "VA-API", Hardware, Vaapi, false, INFINITE),
|
|
backend!("av1_qsv", Av1, "Quick Sync", Hardware, Nv12, true, 65535),
|
|
backend!("libsvtav1", Av1, "SVT-AV1", Software, Yuv420p, false, INFINITE),
|
|
// rav1e refuses a keyframe interval of 2^30.
|
|
backend!("librav1e", Av1, "rav1e", Software, Yuv420p, false, 1 << 29),
|
|
backend!("libaom-av1", Av1, "libaom", Software, Yuv420p, false, INFINITE),
|
|
// VP9 and VP8 (software VP8 / VP9 is libvpx, used directly)
|
|
backend!("vp9_vaapi", Vp9, "VA-API", Hardware, Vaapi, false, INFINITE),
|
|
backend!("vp9_qsv", Vp9, "Quick Sync", Hardware, Nv12, true, 65535),
|
|
backend!("vp8_vaapi", Vp8, "VA-API", Hardware, Vaapi, false, INFINITE),
|
|
];
|
|
|
|
impl BackendSpec {
|
|
pub fn by_name(name: &str) -> Option<&'static BackendSpec> {
|
|
BACKENDS.iter().find(|b| b.name == name)
|
|
}
|
|
|
|
pub fn is_hardware(&self) -> bool {
|
|
self.kind == BackendKind::Hardware
|
|
}
|
|
|
|
/// Whether the automatic choice may use it: rav1e holds about 20 frames
|
|
/// before its first packet even in its low-latency mode (measured with
|
|
/// rav1e 0.7), so it is only used when named in the settings.
|
|
pub fn is_automatic(&self) -> bool {
|
|
self.name != "librav1e"
|
|
}
|
|
|
|
fn is_vaapi(&self) -> bool {
|
|
self.input == Input::Vaapi
|
|
}
|
|
|
|
fn family(&self) -> &'static str {
|
|
self.name.rsplit('_').next().unwrap_or(self.name)
|
|
}
|
|
}
|
|
|
|
/// Where an option is set.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
enum Target {
|
|
/// `AVCodecContext` (generic options).
|
|
Codec,
|
|
/// The encoder's private options (`priv_data`).
|
|
Private,
|
|
}
|
|
|
|
/// One option with alternative values (tried in order: older releases
|
|
/// know other names), optional unless `required`.
|
|
struct Setting {
|
|
target: Target,
|
|
name: &'static str,
|
|
values: Vec<String>,
|
|
required: bool,
|
|
}
|
|
|
|
fn opt(name: &'static str, values: &[&str]) -> Setting {
|
|
Setting {
|
|
target: Target::Private,
|
|
name,
|
|
values: values.iter().map(|v| (*v).to_owned()).collect(),
|
|
required: false,
|
|
}
|
|
}
|
|
|
|
fn generic(name: &'static str, value: impl ToString) -> Setting {
|
|
Setting { target: Target::Codec, name, values: vec![value.to_string()], required: false }
|
|
}
|
|
|
|
/// libx264 presets, fastest first (`EncoderConfig::speed` indexes them).
|
|
const X264_PRESETS: [&str; 10] = [
|
|
"ultrafast",
|
|
"superfast",
|
|
"veryfast",
|
|
"faster",
|
|
"fast",
|
|
"medium",
|
|
"slow",
|
|
"slower",
|
|
"veryslow",
|
|
"placebo",
|
|
];
|
|
|
|
/// `FF_PROFILE_H264_HIGH` and `FF_PROFILE_H264_CONSTRAINED_BASELINE`.
|
|
const PROFILE_H264_HIGH: i64 = 100;
|
|
const PROFILE_H264_CONSTRAINED_BASELINE: i64 = 66 | 1 << 9;
|
|
|
|
/// H.264 level limits (ITU-T H.264 Table A-1): `level_idc`, macroblocks per
|
|
/// second, macroblocks per frame, bitrate in kbit/s for Baseline (High
|
|
/// allows 1.25 times that).
|
|
const H264_LEVELS: [(i64, u64, u64, u64); 19] = [
|
|
(10, 1_485, 99, 64),
|
|
(11, 3_000, 396, 192),
|
|
(12, 6_000, 396, 384),
|
|
(13, 11_880, 396, 768),
|
|
(20, 11_880, 396, 2_000),
|
|
(21, 19_800, 792, 4_000),
|
|
(22, 20_250, 1_620, 4_000),
|
|
(30, 40_500, 1_620, 10_000),
|
|
(31, 108_000, 3_600, 14_000),
|
|
(32, 216_000, 5_120, 20_000),
|
|
(40, 245_760, 8_192, 20_000),
|
|
(41, 245_760, 8_192, 50_000),
|
|
(42, 522_240, 8_704, 50_000),
|
|
(50, 589_824, 22_080, 135_000),
|
|
(51, 983_040, 36_864, 240_000),
|
|
(52, 2_073_600, 36_864, 240_000),
|
|
(60, 4_177_920, 139_264, 240_000),
|
|
(61, 8_355_840, 139_264, 480_000),
|
|
(62, 16_711_680, 139_264, 800_000),
|
|
];
|
|
|
|
/// The lowest `level_idc` whose limits hold this stream, or the highest
|
|
/// level defined if none does.
|
|
///
|
|
/// The signalling offers the same level to the viewer
|
|
/// (`voelin_stream::h264::level_idc`, which this mirrors — a test pins the
|
|
/// two together). It has to be told to the encoder, because wrappers that
|
|
/// pick a level themselves pick a generous one: AMF writes level 4.2 into
|
|
/// the SPS of a 720p30 stream whose offer says 3.1, and a decoder set up
|
|
/// from that offer may refuse it.
|
|
fn h264_level(profile: H264Profile, width: u32, height: u32, fps: u32, bitrate: u32) -> i64 {
|
|
let (mbs_w, mbs_h) = (u64::from(width.div_ceil(16)), u64::from(height.div_ceil(16)));
|
|
let frame = mbs_w * mbs_h;
|
|
let rate = frame * u64::from(fps.max(1));
|
|
let factor = match profile {
|
|
H264Profile::ConstrainedHigh => 1250,
|
|
H264Profile::ConstrainedBaseline => 1000,
|
|
};
|
|
H264_LEVELS
|
|
.iter()
|
|
.find(|&&(_, max_rate, max_frame, max_kbps)| {
|
|
rate <= max_rate
|
|
&& frame <= max_frame
|
|
// Neither side may exceed sqrt(8 * MaxFS) macroblocks.
|
|
&& mbs_w * mbs_w <= 8 * max_frame
|
|
&& mbs_h * mbs_h <= 8 * max_frame
|
|
&& u64::from(bitrate) <= max_kbps * factor
|
|
})
|
|
.map_or(H264_LEVELS[H264_LEVELS.len() - 1].0, |l| l.0)
|
|
}
|
|
|
|
/// The backend's realtime settings (besides size, format, time base and
|
|
/// rate, which every backend gets).
|
|
fn settings(spec: &BackendSpec, config: &EncoderConfig, low_power: bool) -> Vec<Setting> {
|
|
let screen = config.content == ContentHint::Screen;
|
|
let baseline = config.h264_profile == H264Profile::ConstrainedBaseline;
|
|
let h264 = spec.codec == Codec::H264;
|
|
let generic_profile =
|
|
if baseline { PROFILE_H264_CONSTRAINED_BASELINE } else { PROFILE_H264_HIGH };
|
|
let mut list = Vec::new();
|
|
match spec.family() {
|
|
"nvenc" => {
|
|
let preset = config.speed.map(|s| format!("p{}", s.clamp(1, 7)));
|
|
let mut presets = preset.into_iter().collect::<Vec<_>>();
|
|
presets.extend(["p2".into(), "llhp".into()]);
|
|
list.push(Setting { values: presets, ..opt("preset", &[]) });
|
|
list.push(opt("tune", &["ull", "ll"]));
|
|
list.push(opt("rc", &["cbr"]));
|
|
list.push(opt("zerolatency", &["1"]));
|
|
list.push(opt("delay", &["0"]));
|
|
list.push(opt("rc-lookahead", &["0"]));
|
|
list.push(opt("forced-idr", &["1"]));
|
|
list.push(opt("no-scenecut", &["1"]));
|
|
list.push(opt("b_ref_mode", &["disabled"]));
|
|
if h264 {
|
|
list.push(opt("profile", if baseline { &["baseline"] } else { &["high"] }));
|
|
}
|
|
}
|
|
"qsv" => {
|
|
list.push(opt("preset", &["veryfast"]));
|
|
list.push(opt("async_depth", &["1"]));
|
|
list.push(opt("look_ahead", &["0"]));
|
|
list.push(opt("look_ahead_depth", &["0"]));
|
|
list.push(opt("forced_idr", &["1"]));
|
|
list.push(opt("low_delay_brc", &["1"]));
|
|
list.push(opt(
|
|
"scenario",
|
|
if screen { &["displayremoting"] } else { &["videoconference"] },
|
|
));
|
|
if h264 {
|
|
list.push(opt("profile", if baseline { &["baseline"] } else { &["high"] }));
|
|
}
|
|
}
|
|
"amf" => {
|
|
list.push(opt("usage", &["ultralowlatency", "lowlatency"]));
|
|
list.push(opt("quality", &["speed"]));
|
|
list.push(opt("rc", &["cbr"]));
|
|
list.push(opt("header_insertion_mode", &["idr", "key", "gop"]));
|
|
list.push(opt("latency", &["1"]));
|
|
if h264 {
|
|
list.push(opt(
|
|
"profile",
|
|
if baseline {
|
|
&["constrained_baseline", "main"]
|
|
} else {
|
|
&["constrained_high", "high"]
|
|
},
|
|
));
|
|
}
|
|
}
|
|
"vaapi" => {
|
|
list.push(opt("rc_mode", &["CBR"]));
|
|
list.push(opt("async_depth", &["1"]));
|
|
if low_power {
|
|
list.push(Setting { required: true, ..opt("low_power", &["1"]) });
|
|
}
|
|
if h264 {
|
|
list.push(generic("profile", generic_profile));
|
|
}
|
|
}
|
|
"videotoolbox" => {
|
|
list.push(opt("realtime", &["1"]));
|
|
list.push(opt("prio_speed", &["1"]));
|
|
list.push(opt("allow_sw", &["0"]));
|
|
if h264 {
|
|
list.push(opt(
|
|
"profile",
|
|
if baseline {
|
|
&["constrained_baseline", "baseline"]
|
|
} else {
|
|
&["constrained_high", "high"]
|
|
},
|
|
));
|
|
}
|
|
}
|
|
"mf" => {
|
|
list.push(opt("hw_encoding", &["1"]));
|
|
list.push(opt("rate_control", &["cbr"]));
|
|
list.push(opt(
|
|
"scenario",
|
|
if screen { &["display_remoting"] } else { &["video_conference"] },
|
|
));
|
|
if h264 {
|
|
list.push(generic("profile", generic_profile));
|
|
}
|
|
}
|
|
_ => match spec.name {
|
|
"libx264" => {
|
|
let preset = config.speed.map_or("veryfast", |s| {
|
|
X264_PRESETS[s.clamp(0, X264_PRESETS.len() as i32 - 1) as usize]
|
|
});
|
|
list.push(Setting { required: true, ..opt("preset", &[preset]) });
|
|
list.push(opt("tune", &["zerolatency"]));
|
|
list.push(opt("profile", if baseline { &["baseline"] } else { &["high"] }));
|
|
list.push(opt("forced-idr", &["1"]));
|
|
list.push(opt("x264-params", &["scenecut=0"]));
|
|
}
|
|
"libopenh264" => {
|
|
list.push(generic("profile", generic_profile));
|
|
list.push(opt("allow_skip_frames", &["1"]));
|
|
list.push(opt("rc_mode", &["bitrate"]));
|
|
}
|
|
"libsvtav1" => {
|
|
let preset = config.speed.unwrap_or(10).to_string();
|
|
list.push(Setting { values: vec![preset], ..opt("preset", &[]) });
|
|
list.push(opt(
|
|
"svtav1-params",
|
|
&["rc=2:pred-struct=1:lookahead=0:scd=0", "rc=2:pred-struct=1"],
|
|
));
|
|
}
|
|
"librav1e" => {
|
|
let speed = config.speed.unwrap_or(10).to_string();
|
|
list.push(Setting { values: vec![speed], ..opt("speed", &[]) });
|
|
list.push(opt(
|
|
"rav1e-params",
|
|
&["low_latency=true:rdo_lookahead_frames=1:reservoir_frame_delay=12"],
|
|
));
|
|
}
|
|
"libaom-av1" => {
|
|
let cpu_used = config.speed.unwrap_or(8).to_string();
|
|
list.push(opt("usage", &["realtime"]));
|
|
list.push(Setting { values: vec![cpu_used], ..opt("cpu-used", &[]) });
|
|
list.push(opt("lag-in-frames", &["0"]));
|
|
list.push(opt("row-mt", &["1"]));
|
|
list.push(opt("end-usage", &["cbr"]));
|
|
list.push(opt("aq-mode", &["3"]));
|
|
list.push(opt("enable-tpl-model", &["0"]));
|
|
if screen {
|
|
list.push(opt("tune-content", &["screen"]));
|
|
}
|
|
}
|
|
_ => {}
|
|
},
|
|
}
|
|
list
|
|
}
|
|
|
|
/// The process's VA-API device: `VOELIN_VAAPI_DEVICE`, or the first DRM
|
|
/// render node.
|
|
struct Device(Ptr);
|
|
|
|
// SAFETY: an AVBufferRef of an AVHWDeviceContext, never freed; FFmpeg's
|
|
// VA-API device may be used from several threads (libva locks per display).
|
|
unsafe impl Send for Device {}
|
|
unsafe impl Sync for Device {}
|
|
|
|
/// The DRM render node for VA-API.
|
|
pub fn vaapi_device_path() -> Option<String> {
|
|
if let Some(path) = std::env::var_os("VOELIN_VAAPI_DEVICE") {
|
|
return Some(path.to_string_lossy().into_owned());
|
|
}
|
|
let mut nodes: Vec<String> = std::fs::read_dir("/dev/dri")
|
|
.ok()?
|
|
.flatten()
|
|
.map(|e| e.path().to_string_lossy().into_owned())
|
|
.filter(|p| p.rsplit('/').next().is_some_and(|n| n.starts_with("renderD")))
|
|
.collect();
|
|
nodes.sort();
|
|
nodes.into_iter().next()
|
|
}
|
|
|
|
fn vaapi_device(ffmpeg: &Ffmpeg) -> std::result::Result<Ptr, String> {
|
|
static DEVICE: OnceLock<std::result::Result<Device, String>> = OnceLock::new();
|
|
DEVICE
|
|
.get_or_init(|| {
|
|
let path = vaapi_device_path().ok_or("no DRM render node (/dev/dri/renderD*)")?;
|
|
let api = &ffmpeg.api;
|
|
let name = cstr("vaapi");
|
|
// SAFETY: a C string.
|
|
let kind = unsafe { (api.av_hwdevice_find_type_by_name)(name.as_ptr()) };
|
|
if kind <= 0 {
|
|
return Err("FFmpeg has no VA-API support".into());
|
|
}
|
|
let mut device = std::ptr::null_mut();
|
|
let c_path = cstr(&path);
|
|
// SAFETY: out pointer and C strings are valid; no options.
|
|
let ret = unsafe {
|
|
(api.av_hwdevice_ctx_create)(
|
|
&mut device,
|
|
kind,
|
|
c_path.as_ptr(),
|
|
std::ptr::null_mut(),
|
|
0,
|
|
)
|
|
};
|
|
if ret < 0 {
|
|
return Err(format!(
|
|
"VA-API device {path}: {}{}",
|
|
api.error_text(ret),
|
|
log_suffix("vaapi")
|
|
));
|
|
}
|
|
Ok(Device(device))
|
|
})
|
|
.as_ref()
|
|
.map(|d| d.0)
|
|
.map_err(Clone::clone)
|
|
}
|
|
|
|
/// FFmpeg's recent messages, appended to an error.
|
|
fn log_suffix(context: &str) -> String {
|
|
let log = take_log(context);
|
|
if log.is_empty() { String::new() } else { format!(" ({})", log.join("; ")) }
|
|
}
|
|
|
|
/// Maps output packets back to the frames' 90 kHz timestamps.
|
|
struct Timestamps {
|
|
pending: VecDeque<(i64, u64)>,
|
|
}
|
|
|
|
impl Timestamps {
|
|
fn new() -> Self {
|
|
Self { pending: VecDeque::with_capacity(64) }
|
|
}
|
|
|
|
fn push(&mut self, pts: i64, pts_90khz: u64) {
|
|
if self.pending.len() == self.pending.capacity() {
|
|
self.pending.pop_front();
|
|
}
|
|
self.pending.push_back((pts, pts_90khz));
|
|
}
|
|
|
|
fn take(&mut self, pts: i64) -> Option<u64> {
|
|
while let Some(&(p, t)) = self.pending.front() {
|
|
if p > pts {
|
|
break;
|
|
}
|
|
self.pending.pop_front();
|
|
if p == pts {
|
|
return Some(t);
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
/// An open encoder for one size and frame rate.
|
|
struct Session {
|
|
ctx: Ptr,
|
|
width: u32,
|
|
height: u32,
|
|
fps: u32,
|
|
bitrate: u32,
|
|
/// Frame in system memory, filled per frame (reused).
|
|
sw: Ptr,
|
|
/// VA-API: the surface pool and the frame that takes a surface.
|
|
pool: Ptr,
|
|
hw: Ptr,
|
|
packet: Ptr,
|
|
last_pts: Option<i64>,
|
|
/// No packet came out yet: the first one starts the stream, so it is a
|
|
/// keyframe even where the wrapper does not flag it (rav1e).
|
|
first_packet: bool,
|
|
/// When it was opened (bitrate increases reopen at most every
|
|
/// [`REOPEN_FOR_BITRATE`]).
|
|
opened: Instant,
|
|
timestamps: Timestamps,
|
|
nv12: bool,
|
|
}
|
|
|
|
// SAFETY: the FFmpeg objects belong to this session alone and are used from
|
|
// one thread at a time (`&mut self`).
|
|
unsafe impl Send for Session {}
|
|
|
|
impl Drop for Session {
|
|
fn drop(&mut self) {
|
|
let api = &Ffmpeg::get().expect("a session exists only with FFmpeg").api;
|
|
// SAFETY: each pointer is NULL or owned by this session.
|
|
unsafe {
|
|
(api.av_frame_free)(&mut self.sw);
|
|
(api.av_frame_free)(&mut self.hw);
|
|
(api.av_packet_free)(&mut self.packet);
|
|
(api.avcodec_free_context)(&mut self.ctx);
|
|
if !self.pool.is_null() {
|
|
(api.av_buffer_unref)(&mut self.pool);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// How long a session runs before a bitrate increase of half or more
|
|
/// reopens it (a keyframe) on backends that cannot change the bitrate
|
|
/// running; smaller increases wait for the next keyframe.
|
|
const REOPEN_FOR_BITRATE: Duration = Duration::from_secs(5);
|
|
|
|
/// When to recreate the session.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
enum Reinit {
|
|
No,
|
|
/// With the next keyframe (bitrate changes the wrapper cannot apply).
|
|
AtKeyframe,
|
|
/// Before the next frame.
|
|
Now,
|
|
}
|
|
|
|
/// An encoder of one [`BackendSpec`].
|
|
pub struct FfmpegEncoder {
|
|
ffmpeg: &'static Ffmpeg,
|
|
spec: &'static BackendSpec,
|
|
config: EncoderConfig,
|
|
session: Option<Session>,
|
|
reinit: Reinit,
|
|
/// VA-API only offers the low-power entry point for this codec.
|
|
low_power: bool,
|
|
/// `AVFrame.hw_frames_ctx` was checked on a surface of this encoder.
|
|
dmabuf_checked: bool,
|
|
}
|
|
|
|
impl FfmpegEncoder {
|
|
/// An encoder of backend `name` (nothing is opened until the first
|
|
/// frame, whose size it takes).
|
|
pub fn new(name: &str, config: EncoderConfig) -> Result<Self> {
|
|
let spec = BackendSpec::by_name(name).ok_or_else(|| Error::CodecUnavailable {
|
|
codec: Codec::H264,
|
|
reason: format!("unknown FFmpeg encoder {name}"),
|
|
})?;
|
|
let ffmpeg = Ffmpeg::get()
|
|
.map_err(|e| Error::CodecUnavailable { codec: spec.codec, reason: e.to_owned() })?;
|
|
Ok(Self {
|
|
ffmpeg,
|
|
spec,
|
|
config,
|
|
session: None,
|
|
reinit: Reinit::No,
|
|
low_power: false,
|
|
dmabuf_checked: false,
|
|
})
|
|
}
|
|
|
|
pub fn spec(&self) -> &'static BackendSpec {
|
|
self.spec
|
|
}
|
|
|
|
fn error(&self, what: &str, code: c_int) -> Error {
|
|
Error::Encoder {
|
|
codec: self.spec.codec,
|
|
message: format!(
|
|
"{} {what}: {}{}",
|
|
self.spec.name,
|
|
self.ffmpeg.api.error_text(code),
|
|
log_suffix(self.spec.name)
|
|
),
|
|
}
|
|
}
|
|
|
|
fn unavailable(&self, reason: String) -> Error {
|
|
Error::CodecUnavailable { codec: self.spec.codec, reason }
|
|
}
|
|
|
|
fn set_option(&self, obj: Ptr, name: &str, value: &str) -> c_int {
|
|
let (name, value) = (cstr(name), cstr(value));
|
|
// SAFETY: an AVClass-enabled object and two C strings.
|
|
unsafe { (self.ffmpeg.api.av_opt_set)(obj, name.as_ptr(), value.as_ptr(), 0) }
|
|
}
|
|
|
|
fn set_int(&self, obj: Ptr, name: &str, value: i64) -> c_int {
|
|
let name = cstr(name);
|
|
// SAFETY: an AVClass-enabled object and a C string.
|
|
unsafe { (self.ffmpeg.api.av_opt_set_int)(obj, name.as_ptr(), value, 0) }
|
|
}
|
|
|
|
/// Apply one setting; an optional one that this release does not know
|
|
/// (or does not accept) is skipped.
|
|
fn apply(&self, ctx: Ptr, private: Ptr, setting: &Setting) -> Result<()> {
|
|
let obj = match setting.target {
|
|
Target::Codec => ctx,
|
|
Target::Private if private.is_null() => return Ok(()),
|
|
Target::Private => private,
|
|
};
|
|
let mut last = sys::OPTION_NOT_FOUND;
|
|
for value in &setting.values {
|
|
last = self.set_option(obj, setting.name, value);
|
|
if last >= 0 {
|
|
return Ok(());
|
|
}
|
|
}
|
|
if setting.required {
|
|
return Err(self.error(&format!("option {}", setting.name), last));
|
|
}
|
|
if last != sys::OPTION_NOT_FOUND {
|
|
tracing::debug!(
|
|
backend = self.spec.name,
|
|
option = setting.name,
|
|
"not accepted: {}",
|
|
self.ffmpeg.api.error_text(last)
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The encoder size: even (4:2:0 chroma), at least 2x2.
|
|
fn coded_size(width: u32, height: u32) -> (u32, u32) {
|
|
((width & !1).max(2), (height & !1).max(2))
|
|
}
|
|
|
|
/// Open a session for `width` x `height`.
|
|
fn open(&mut self, width: u32, height: u32) -> Result<Session> {
|
|
match self.open_with(width, height, self.low_power) {
|
|
Err(e) if self.spec.is_vaapi() && !self.low_power => {
|
|
// Some GPUs only have the low-power entry point for a codec.
|
|
match self.open_with(width, height, true) {
|
|
Ok(session) => {
|
|
self.low_power = true;
|
|
Ok(session)
|
|
}
|
|
Err(_) => Err(e),
|
|
}
|
|
}
|
|
result => result,
|
|
}
|
|
}
|
|
|
|
fn open_with(&self, width: u32, height: u32, low_power: bool) -> Result<Session> {
|
|
let api = &self.ffmpeg.api;
|
|
let pix = self.ffmpeg.pix;
|
|
let name = cstr(self.spec.name);
|
|
// SAFETY: a C string; returns a static codec or NULL.
|
|
let codec = unsafe { (api.avcodec_find_encoder_by_name)(name.as_ptr()) };
|
|
if codec.is_null() {
|
|
return Err(self.unavailable(format!("{} is not in this FFmpeg build", self.spec.name)));
|
|
}
|
|
// SAFETY: allocates a context with the codec's private options.
|
|
let ctx = unsafe { (api.avcodec_alloc_context3)(codec) };
|
|
if ctx.is_null() {
|
|
return Err(self.unavailable("avcodec_alloc_context3 failed".into()));
|
|
}
|
|
let fps = self.config.fps.max(1);
|
|
let bitrate = self.config.bitrate_bps.max(1);
|
|
let mut session = Session {
|
|
ctx,
|
|
width,
|
|
height,
|
|
fps,
|
|
bitrate,
|
|
sw: std::ptr::null_mut(),
|
|
pool: std::ptr::null_mut(),
|
|
hw: std::ptr::null_mut(),
|
|
packet: std::ptr::null_mut(),
|
|
last_pts: None,
|
|
first_packet: true,
|
|
opened: Instant::now(),
|
|
timestamps: Timestamps::new(),
|
|
nv12: self.spec.input != Input::Yuv420p,
|
|
};
|
|
// SAFETY: `ctx` is a live codec context; the first child is its
|
|
// private options object (NULL if the codec has none).
|
|
let private = unsafe { (api.av_opt_child_next)(ctx, std::ptr::null_mut()) };
|
|
let (w, h) = (width as c_int, height as c_int);
|
|
let sw_format = if session.nv12 { pix.nv12 } else { pix.yuv420p };
|
|
let format = match self.spec.input {
|
|
Input::Vaapi => {
|
|
pix.vaapi.ok_or_else(|| self.unavailable("no VA-API pixel format".into()))?
|
|
}
|
|
_ => sw_format,
|
|
};
|
|
let size = cstr("video_size");
|
|
let pixel_format = cstr("pixel_format");
|
|
let time_base = cstr("time_base");
|
|
// SAFETY: typed option setters on a live context with C strings.
|
|
let ret = unsafe {
|
|
let mut ret = (api.av_opt_set_image_size)(ctx, size.as_ptr(), w, h, 0);
|
|
if ret >= 0 {
|
|
ret = (api.av_opt_set_pixel_fmt)(ctx, pixel_format.as_ptr(), format, 0);
|
|
}
|
|
if ret >= 0 {
|
|
let tb = Rational { num: 1, den: fps as c_int };
|
|
ret = (api.av_opt_set_q)(ctx, time_base.as_ptr(), tb, 0);
|
|
}
|
|
ret
|
|
};
|
|
if ret < 0 {
|
|
return Err(self.error("size, format and time base", ret));
|
|
}
|
|
let gop = self.config.keyframe_interval.map_or(self.spec.infinite_gop, i64::from);
|
|
let mut list = vec![
|
|
generic("b", bitrate),
|
|
// SVT-AV1 takes CBR from its own `rc=2` and rejects a maximum
|
|
// that is not above the target.
|
|
generic("maxrate", if self.spec.name == "libsvtav1" { 0 } else { bitrate }),
|
|
generic("bufsize", bitrate),
|
|
generic("g", gop),
|
|
generic("bf", 0),
|
|
generic("colorspace", "smpte170m"),
|
|
generic("color_primaries", "smpte170m"),
|
|
generic("color_trc", "smpte170m"),
|
|
generic("color_range", "tv"),
|
|
];
|
|
if !self.spec.is_hardware() {
|
|
list.push(generic("threads", self.config.threads_for(width, height)));
|
|
}
|
|
if self.spec.codec == Codec::H264 {
|
|
// The level the signalling offers, so the SPS cannot claim a
|
|
// higher one than the viewer's decoder was set up for. Both
|
|
// targets: the wrappers that have a private `level` read that
|
|
// one, the rest `AVCodecContext.level`.
|
|
let level =
|
|
h264_level(self.config.h264_profile, width, height, fps, bitrate).to_string();
|
|
list.push(generic("level", &level));
|
|
list.push(Setting { values: vec![level], ..opt("level", &[]) });
|
|
}
|
|
list.extend(settings(self.spec, &self.config, low_power));
|
|
for setting in &list {
|
|
self.apply(ctx, private, setting)?;
|
|
}
|
|
if self.spec.is_vaapi() {
|
|
session.pool = self.vaapi_pool(width, height)?;
|
|
let offset = self.ffmpeg.codec_hw_frames.clone().map_err(|e| self.unavailable(e))?;
|
|
// SAFETY: `offset` is AVCodecContext.hw_frames_ctx (checked at
|
|
// load); the context takes the new reference and frees it.
|
|
unsafe {
|
|
let reference = (api.av_buffer_ref)(session.pool);
|
|
layout::write::<Ptr>(ctx, offset, reference);
|
|
}
|
|
}
|
|
// SAFETY: a configured context and its codec; no options dictionary.
|
|
let ret = unsafe { (api.avcodec_open2)(ctx, codec, std::ptr::null_mut()) };
|
|
if ret < 0 {
|
|
return Err(self.error("open", ret));
|
|
}
|
|
// Buffers for the whole session.
|
|
// SAFETY: allocations; the head fields are set before
|
|
// av_frame_get_buffer as documented.
|
|
unsafe {
|
|
session.packet = (api.av_packet_alloc)();
|
|
session.sw = (api.av_frame_alloc)();
|
|
if session.packet.is_null() || session.sw.is_null() {
|
|
return Err(self.unavailable("out of memory".into()));
|
|
}
|
|
let head = session.sw.cast::<FrameHead>();
|
|
(*head).width = w;
|
|
(*head).height = h;
|
|
(*head).format = sw_format;
|
|
let ret = (api.av_frame_get_buffer)(session.sw, 0);
|
|
if ret < 0 {
|
|
return Err(self.error("frame buffer", ret));
|
|
}
|
|
if self.spec.is_vaapi() {
|
|
session.hw = (api.av_frame_alloc)();
|
|
if session.hw.is_null() {
|
|
return Err(self.unavailable("out of memory".into()));
|
|
}
|
|
}
|
|
}
|
|
tracing::debug!(
|
|
backend = self.spec.name,
|
|
width,
|
|
height,
|
|
fps,
|
|
bitrate,
|
|
low_power,
|
|
"FFmpeg encoder opened"
|
|
);
|
|
Ok(session)
|
|
}
|
|
|
|
/// A VA-API surface pool (NV12) for `width` x `height`.
|
|
fn vaapi_pool(&self, width: u32, height: u32) -> Result<Ptr> {
|
|
let api = &self.ffmpeg.api;
|
|
let device = vaapi_device(self.ffmpeg).map_err(|e| self.unavailable(e))?;
|
|
let vaapi =
|
|
self.ffmpeg.pix.vaapi.ok_or_else(|| self.unavailable("no VA-API format".into()))?;
|
|
// SAFETY: `device` is a live device reference.
|
|
let mut pool = unsafe { (api.av_hwframe_ctx_alloc)(device) };
|
|
if pool.is_null() {
|
|
return Err(self.unavailable("av_hwframe_ctx_alloc failed".into()));
|
|
}
|
|
// SAFETY: `pool` was just allocated on `device`.
|
|
let fields: HwFramesFields = match unsafe { layout::hw_frames_fields(pool, device) } {
|
|
Ok(f) => f,
|
|
Err(e) => {
|
|
// SAFETY: allocated above.
|
|
unsafe { (api.av_buffer_unref)(&mut pool) };
|
|
return Err(self.unavailable(e));
|
|
}
|
|
};
|
|
// SAFETY: the fields were located and checked on this very context;
|
|
// setting them before av_hwframe_ctx_init is what FFmpeg expects.
|
|
let ret = unsafe {
|
|
let ctx = (*pool.cast::<sys::BufferRefHead>()).data.cast::<std::ffi::c_void>();
|
|
layout::write::<c_int>(ctx, fields.format, vaapi);
|
|
layout::write::<c_int>(ctx, fields.sw_format, self.ffmpeg.pix.nv12);
|
|
layout::write::<c_int>(ctx, fields.width, width as c_int);
|
|
layout::write::<c_int>(ctx, fields.height, height as c_int);
|
|
// Surfaces are allocated on demand and recycled by the pool.
|
|
layout::write::<c_int>(ctx, fields.initial_pool_size, 0);
|
|
(api.av_hwframe_ctx_init)(pool)
|
|
};
|
|
if ret < 0 {
|
|
// SAFETY: allocated above.
|
|
unsafe { (api.av_buffer_unref)(&mut pool) };
|
|
return Err(self.error("VA-API surface pool", ret));
|
|
}
|
|
Ok(pool)
|
|
}
|
|
|
|
/// Copy the I420 picture into the session's frame (cropped to the coded
|
|
/// size; NV12 interleaved where the backend wants it).
|
|
fn fill(&self, session: &Session, frame: &VideoFrame) -> Result<()> {
|
|
let FrameData::I420 { y, u, v } = &frame.data else {
|
|
unreachable!("converted to I420 before");
|
|
};
|
|
let api = &self.ffmpeg.api;
|
|
// SAFETY: the frame was allocated by av_frame_get_buffer for the
|
|
// session size and format; make_writable keeps that and only swaps
|
|
// buffers the encoder still references.
|
|
let ret = unsafe { (api.av_frame_make_writable)(session.sw) };
|
|
if ret < 0 {
|
|
return Err(self.error("frame buffer", ret));
|
|
}
|
|
let (w, h) = (session.width as usize, session.height as usize);
|
|
let (cw, ch) = (w / 2, h / 2);
|
|
// SAFETY: planes of the session's frame: `linesize` bytes per row,
|
|
// `h` (luma) or `h / 2` (chroma) rows, at least `w` (or the chroma
|
|
// width) bytes each, owned by the frame (writable, see above).
|
|
unsafe {
|
|
let head = &*session.sw.cast::<FrameHead>();
|
|
let plane = |i: usize, rows: usize| {
|
|
let stride = head.linesize[i] as usize;
|
|
(std::slice::from_raw_parts_mut(head.data[i], stride * rows), stride)
|
|
};
|
|
let (dst, stride) = plane(0, h);
|
|
for row in 0..h {
|
|
dst[row * stride..][..w].copy_from_slice(y.row(row, w));
|
|
}
|
|
if session.nv12 {
|
|
let (dst, stride) = plane(1, ch);
|
|
for row in 0..ch {
|
|
let out = &mut dst[row * stride..][..cw * 2];
|
|
let (u_row, v_row) = (u.row(row, cw), v.row(row, cw));
|
|
for ((pair, &u), &v) in out.chunks_exact_mut(2).zip(u_row).zip(v_row) {
|
|
pair[0] = u;
|
|
pair[1] = v;
|
|
}
|
|
}
|
|
} else {
|
|
for (i, src) in [(1, u), (2, v)] {
|
|
let (dst, stride) = plane(i, ch);
|
|
for row in 0..ch {
|
|
dst[row * stride..][..cw].copy_from_slice(src.row(row, cw));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Receive every packet that is ready.
|
|
fn drain(&self, session: &mut Session, out: &mut dyn FnMut(EncodedChunk<'_>)) -> Result<bool> {
|
|
let api = &self.ffmpeg.api;
|
|
let mut keyframe = false;
|
|
loop {
|
|
// SAFETY: a live context and packet.
|
|
let ret = unsafe { (api.avcodec_receive_packet)(session.ctx, session.packet) };
|
|
if ret == sys::EAGAIN || ret == sys::EOF {
|
|
return Ok(keyframe);
|
|
}
|
|
if ret < 0 {
|
|
return Err(self.error("receive", ret));
|
|
}
|
|
// SAFETY: a packet just filled by the encoder: `data` holds
|
|
// `size` bytes until av_packet_unref.
|
|
unsafe {
|
|
let head = &*session.packet.cast::<PacketHead>();
|
|
let key = head.flags & sys::PKT_FLAG_KEY != 0 || session.first_packet;
|
|
session.first_packet = false;
|
|
keyframe |= key;
|
|
let pts_90khz = session
|
|
.timestamps
|
|
.take(head.pts)
|
|
.unwrap_or_else(|| (head.pts.max(0) as u64) * 90_000 / u64::from(session.fps));
|
|
if !head.data.is_null() && head.size > 0 {
|
|
let data = std::slice::from_raw_parts(head.data, head.size as usize);
|
|
out(EncodedChunk { data, keyframe: key, pts_90khz });
|
|
}
|
|
(api.av_packet_unref)(session.packet);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Send one frame (or NULL to flush), draining packets as the encoder
|
|
/// asks.
|
|
fn send(
|
|
&self,
|
|
session: &mut Session,
|
|
frame: Ptr,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<bool> {
|
|
let api = &self.ffmpeg.api;
|
|
let mut keyframe = false;
|
|
loop {
|
|
// SAFETY: a live context; `frame` is NULL or a live frame.
|
|
let ret = unsafe { (api.avcodec_send_frame)(session.ctx, frame) };
|
|
if ret == sys::EAGAIN {
|
|
keyframe |= self.drain(session, out)?;
|
|
continue;
|
|
}
|
|
if ret < 0 && ret != sys::EOF {
|
|
return Err(self.error("send", ret));
|
|
}
|
|
break;
|
|
}
|
|
keyframe |= self.drain(session, out)?;
|
|
Ok(keyframe)
|
|
}
|
|
|
|
/// The session for a `width` x `height` frame, opened anew when the size
|
|
/// changed or a reinit is due; whether the frame must be a keyframe.
|
|
fn prepare(
|
|
&mut self,
|
|
width: u32,
|
|
height: u32,
|
|
force_keyframe: bool,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<(Session, bool)> {
|
|
let (w, h) = Self::coded_size(width, height);
|
|
let mut force = force_keyframe;
|
|
let reopen = match &self.session {
|
|
None => true,
|
|
Some(s) => {
|
|
(s.width, s.height) != (w, h)
|
|
|| self.reinit == Reinit::Now
|
|
|| (self.reinit == Reinit::AtKeyframe && force)
|
|
}
|
|
};
|
|
if reopen {
|
|
// Frames the old session still holds (encoders with a delay) come
|
|
// out first; then one session at a time (hardware encoders have
|
|
// few).
|
|
if let Some(mut old) = self.session.take()
|
|
&& let Err(e) = self.send(&mut old, std::ptr::null_mut(), out)
|
|
{
|
|
tracing::debug!(backend = self.spec.name, "flushing the old session: {e}");
|
|
}
|
|
self.session = Some(self.open(w, h)?);
|
|
self.reinit = Reinit::No;
|
|
force = true;
|
|
}
|
|
Ok((self.session.take().expect("opened above"), force))
|
|
}
|
|
|
|
fn encode_i420(
|
|
&mut self,
|
|
frame: &VideoFrame,
|
|
force_keyframe: bool,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<()> {
|
|
let (mut session, force) = self.prepare(frame.width, frame.height, force_keyframe, out)?;
|
|
let result = self.encode_in(&mut session, frame, force, out);
|
|
self.session = Some(session);
|
|
result
|
|
}
|
|
|
|
/// Encode a frame that is still in a DMA-BUF, without a copy: the buffer
|
|
/// becomes a VA-API surface (`av_hwframe_map` of a DRM PRIME frame) that
|
|
/// the encoder reads directly.
|
|
///
|
|
/// VA-API backends and NV12 buffers only: the RGB frames screen capture
|
|
/// delivers first need a colour conversion on the GPU (VA-API video
|
|
/// processing), which is not implemented. Anything else fails with
|
|
/// `Error::CodecUnavailable`; callers then map the buffer and use
|
|
/// [`encode_with`](VideoEncoder::encode_with). The buffer must stay
|
|
/// unchanged until this frame's packet came out (the encoder reads it
|
|
/// asynchronously, one frame deep).
|
|
pub fn encode_dmabuf(
|
|
&mut self,
|
|
frame: &DmaBufRef,
|
|
force_keyframe: bool,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<()> {
|
|
if !self.spec.is_vaapi() {
|
|
return Err(self.unavailable(format!("{} does not import DMA-BUFs", self.spec.name)));
|
|
}
|
|
if frame.fourcc != drm_fourcc(b"NV12") || frame.plane_count != 2 {
|
|
return Err(self.unavailable(
|
|
"only NV12 DMA-BUFs are imported (RGB needs GPU colour conversion)".into(),
|
|
));
|
|
}
|
|
if Self::coded_size(frame.width, frame.height) != (frame.width, frame.height) {
|
|
return Err(self.unavailable("odd frame size".into()));
|
|
}
|
|
let refs = self.ffmpeg.frame_refs.clone().map_err(|e| self.unavailable(e))?;
|
|
let drm_prime =
|
|
self.ffmpeg.pix.drm_prime.ok_or_else(|| self.unavailable("no DRM PRIME".into()))?;
|
|
let vaapi = self.ffmpeg.pix.vaapi.ok_or_else(|| self.unavailable("no VA-API".into()))?;
|
|
let (mut session, force) = self.prepare(frame.width, frame.height, force_keyframe, out)?;
|
|
let result = self.import_and_send(&mut session, frame, force, refs, drm_prime, vaapi, out);
|
|
self.session = Some(session);
|
|
result
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)] // the parts of one import
|
|
fn import_and_send(
|
|
&mut self,
|
|
session: &mut Session,
|
|
frame: &DmaBufRef,
|
|
force: bool,
|
|
refs: layout::FrameRefs,
|
|
drm_prime: c_int,
|
|
vaapi: c_int,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<()> {
|
|
let api = &self.ffmpeg.api;
|
|
if !self.dmabuf_checked {
|
|
// The table's AVFrame.hw_frames_ctx must be where FFmpeg puts
|
|
// the pool of a surface it allocates.
|
|
// SAFETY: `hw` is our frame, the pool the session's.
|
|
let ok = unsafe {
|
|
let ret = (api.av_hwframe_get_buffer)(session.pool, session.hw, 0);
|
|
let ok = ret >= 0 && layout::check_hw_frames_ctx(session.hw, session.pool, refs);
|
|
(api.av_frame_unref)(session.hw);
|
|
ok
|
|
};
|
|
if !ok {
|
|
return Err(self.unavailable("AVFrame.hw_frames_ctx check failed".into()));
|
|
}
|
|
self.dmabuf_checked = true;
|
|
}
|
|
let mut descriptor =
|
|
sys::DrmFrameDescriptor { nb_objects: 1, nb_layers: 1, ..Default::default() };
|
|
descriptor.objects[0] =
|
|
sys::DrmObject { fd: frame.fd, size: frame.size, format_modifier: frame.modifier };
|
|
descriptor.layers[0].format = frame.fourcc;
|
|
descriptor.layers[0].nb_planes = frame.plane_count as c_int;
|
|
for (plane, &(offset, pitch)) in
|
|
descriptor.layers[0].planes.iter_mut().zip(&frame.planes[..frame.plane_count])
|
|
{
|
|
*plane =
|
|
sys::DrmPlane { object_index: 0, offset: offset as isize, pitch: pitch as isize };
|
|
}
|
|
let pts =
|
|
self.stamp(session, frame.timestamp, (frame.timestamp.as_micros() * 9 / 100) as u64);
|
|
// SAFETY: the descriptor lives in a buffer the source frame owns
|
|
// (buf[0], checked offset), so FFmpeg can keep it while mapped; the
|
|
// destination frame gets a reference to the pool at the checked
|
|
// hw_frames_ctx offset; av_frame_free / av_frame_unref release both.
|
|
unsafe {
|
|
let mut buffer = (api.av_buffer_allocz)(std::mem::size_of::<sys::DrmFrameDescriptor>());
|
|
let mut src = (api.av_frame_alloc)();
|
|
if buffer.is_null() || src.is_null() {
|
|
(api.av_buffer_unref)(&mut buffer);
|
|
(api.av_frame_free)(&mut src);
|
|
return Err(self.unavailable("out of memory".into()));
|
|
}
|
|
let data = (*buffer.cast::<sys::BufferRefHead>()).data;
|
|
data.cast::<sys::DrmFrameDescriptor>().write(descriptor);
|
|
let head = src.cast::<FrameHead>();
|
|
(*head).format = drm_prime;
|
|
(*head).width = frame.width as c_int;
|
|
(*head).height = frame.height as c_int;
|
|
(*head).data[0] = data;
|
|
layout::write::<Ptr>(src, refs.buf, buffer);
|
|
let dst = session.hw;
|
|
(*dst.cast::<FrameHead>()).format = vaapi;
|
|
layout::write::<Ptr>(dst, refs.hw_frames_ctx, (api.av_buffer_ref)(session.pool));
|
|
let ret = (api.av_hwframe_map)(dst, src, sys::HWFRAME_MAP_READ);
|
|
(api.av_frame_free)(&mut src);
|
|
if ret < 0 {
|
|
(api.av_frame_unref)(dst);
|
|
return Err(self.error("DMA-BUF import", ret));
|
|
}
|
|
let pict_type = if force { sys::PICTURE_TYPE_I } else { sys::PICTURE_TYPE_NONE };
|
|
layout::write::<i64>(dst, self.ffmpeg.frame.pts, pts);
|
|
layout::write::<c_int>(dst, self.ffmpeg.frame.pict_type, pict_type);
|
|
let result = self.send(session, dst, out);
|
|
(api.av_frame_unref)(dst);
|
|
result.map(|_| ())
|
|
}
|
|
}
|
|
|
|
/// The pts of a frame taken at `timestamp`: frame numbers at the
|
|
/// session's rate (its time base), strictly increasing; remembered with
|
|
/// the 90 kHz time for the packets.
|
|
fn stamp(&self, session: &mut Session, timestamp: Duration, pts_90khz: u64) -> i64 {
|
|
let exact = timestamp.as_secs_f64() * f64::from(session.fps);
|
|
let mut pts = exact.round() as i64;
|
|
if let Some(last) = session.last_pts
|
|
&& pts <= last
|
|
{
|
|
pts = last + 1;
|
|
}
|
|
session.last_pts = Some(pts);
|
|
session.timestamps.push(pts, pts_90khz);
|
|
pts
|
|
}
|
|
|
|
fn encode_in(
|
|
&self,
|
|
session: &mut Session,
|
|
frame: &VideoFrame,
|
|
force: bool,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<()> {
|
|
let api = &self.ffmpeg.api;
|
|
self.fill(session, frame)?;
|
|
let pts = self.stamp(session, frame.timestamp, frame.pts_90khz());
|
|
let fields = self.ffmpeg.frame;
|
|
let pict_type = if force { sys::PICTURE_TYPE_I } else { sys::PICTURE_TYPE_NONE };
|
|
let input = if session.hw.is_null() {
|
|
session.sw
|
|
} else {
|
|
// SAFETY: `hw` is our frame (unreferenced after each send); the
|
|
// pool is the session's; transfer copies the planes into the
|
|
// surface.
|
|
unsafe {
|
|
let ret = (api.av_hwframe_get_buffer)(session.pool, session.hw, 0);
|
|
if ret < 0 {
|
|
return Err(self.error("VA-API surface", ret));
|
|
}
|
|
let ret = (api.av_hwframe_transfer_data)(session.hw, session.sw, 0);
|
|
if ret < 0 {
|
|
(api.av_frame_unref)(session.hw);
|
|
return Err(self.error("VA-API upload", ret));
|
|
}
|
|
}
|
|
session.hw
|
|
};
|
|
// SAFETY: offsets checked at load for this release's AVFrame.
|
|
unsafe {
|
|
layout::write::<i64>(input, fields.pts, pts);
|
|
layout::write::<c_int>(input, fields.pict_type, pict_type);
|
|
}
|
|
let result = self.send(session, input, out);
|
|
if !session.hw.is_null() {
|
|
// The encoder holds its own reference; the surface returns to the
|
|
// pool once it is done with it.
|
|
// SAFETY: our frame.
|
|
unsafe { (api.av_frame_unref)(session.hw) };
|
|
}
|
|
result.map(|_| ())
|
|
}
|
|
|
|
/// Encode one frame and flush the encoder (the self-test): the packets,
|
|
/// and whether one was a keyframe.
|
|
fn self_test(&mut self, width: u32, height: u32) -> Result<(usize, bool)> {
|
|
let frame = test_frame(width, height);
|
|
let mut packets = 0;
|
|
let mut keyframe = false;
|
|
self.encode_with(&frame, true, &mut |chunk| {
|
|
packets += 1;
|
|
keyframe |= chunk.keyframe;
|
|
})?;
|
|
let mut session = self.session.take().expect("opened by the frame");
|
|
let flushed = self.send(&mut session, std::ptr::null_mut(), &mut |chunk| {
|
|
packets += 1;
|
|
keyframe |= chunk.keyframe;
|
|
});
|
|
drop(session);
|
|
flushed?;
|
|
Ok((packets, keyframe))
|
|
}
|
|
}
|
|
|
|
impl Drop for FfmpegEncoder {
|
|
/// Ends the stream properly (SVT-AV1 complains otherwise); the frames
|
|
/// still inside are dropped.
|
|
fn drop(&mut self) {
|
|
if let Some(mut session) = self.session.take() {
|
|
let _ = self.send(&mut session, std::ptr::null_mut(), &mut |_| {});
|
|
}
|
|
}
|
|
}
|
|
|
|
impl VideoEncoder for FfmpegEncoder {
|
|
fn codec(&self) -> Codec {
|
|
self.spec.codec
|
|
}
|
|
|
|
fn backend(&self) -> EncoderBackend {
|
|
EncoderBackend::Ffmpeg(self.spec.name)
|
|
}
|
|
|
|
fn encode(&mut self, frame: &VideoFrame, force_keyframe: bool) -> Result<Vec<EncodedFrame>> {
|
|
let mut frames = Vec::new();
|
|
self.encode_with(frame, force_keyframe, &mut |f| {
|
|
frames.push(EncodedFrame {
|
|
data: f.data.to_vec(),
|
|
keyframe: f.keyframe,
|
|
pts_90khz: f.pts_90khz,
|
|
});
|
|
})?;
|
|
Ok(frames)
|
|
}
|
|
|
|
/// Copies the picture into the session's reused frame (or VA-API
|
|
/// surface) and hands out FFmpeg's packet buffer.
|
|
fn encode_with(
|
|
&mut self,
|
|
frame: &VideoFrame,
|
|
force_keyframe: bool,
|
|
out: &mut dyn FnMut(EncodedChunk<'_>),
|
|
) -> Result<()> {
|
|
let i420 = convert::to_i420(frame)?;
|
|
self.encode_i420(&i420, force_keyframe, out)
|
|
}
|
|
|
|
fn set_bitrate(&mut self, bps: u32) -> Result<()> {
|
|
let bps = bps.max(1);
|
|
self.config.bitrate_bps = bps;
|
|
let Some((ctx, running, opened)) =
|
|
self.session.as_ref().map(|s| (s.ctx, s.bitrate, s.opened))
|
|
else {
|
|
return Ok(());
|
|
};
|
|
if running == bps {
|
|
return Ok(());
|
|
}
|
|
if self.spec.dynamic_bitrate {
|
|
// The wrapper compares these with its running configuration
|
|
// before each frame and reconfigures the encoder.
|
|
for name in ["b", "maxrate", "bufsize"] {
|
|
let ret = self.set_int(ctx, name, i64::from(bps));
|
|
if ret < 0 {
|
|
return Err(self.error(name, ret));
|
|
}
|
|
}
|
|
if let Some(session) = &mut self.session {
|
|
session.bitrate = bps;
|
|
}
|
|
} else if u64::from(bps) * 2 < u64::from(running) {
|
|
// Congestion: waiting for a keyframe would keep overshooting.
|
|
self.reinit = Reinit::Now;
|
|
} else if u64::from(bps) * 2 > u64::from(running) * 3
|
|
&& opened.elapsed() >= REOPEN_FOR_BITRATE
|
|
{
|
|
// Much more room (the estimate ramping up after the start): a
|
|
// screen that never asks for a keyframe would otherwise keep
|
|
// the start bitrate.
|
|
self.reinit = Reinit::Now;
|
|
} else {
|
|
self.reinit = self.reinit.max(Reinit::AtKeyframe);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The time base is the frame rate, fixed per session: a new rate opens
|
|
/// a new session (a keyframe).
|
|
fn set_fps(&mut self, fps: u32) -> Result<()> {
|
|
let fps = fps.max(1);
|
|
self.config.fps = fps;
|
|
if self.session.as_ref().is_some_and(|s| s.fps != fps) {
|
|
self.reinit = Reinit::Now;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// A gradient test picture.
|
|
fn test_frame(width: u32, height: u32) -> VideoFrame {
|
|
let mut frame = VideoFrame::black_i420(width, height);
|
|
if let FrameData::I420 { y, .. } = &mut frame.data {
|
|
for (i, px) in y.data.iter_mut().enumerate() {
|
|
*px = 16 + ((i % width as usize) * 200 / width as usize) as u8;
|
|
}
|
|
}
|
|
frame.with_timestamp(Duration::ZERO)
|
|
}
|
|
|
|
/// A backend and the result of its self-test.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct BackendStatus {
|
|
pub spec: &'static BackendSpec,
|
|
/// `Ok` if it encoded the test frame, else why not.
|
|
pub available: std::result::Result<(), String>,
|
|
}
|
|
|
|
/// Size of the self-test frame.
|
|
const TEST_SIZE: (u32, u32) = (320, 240);
|
|
|
|
fn test_backend(spec: &'static BackendSpec) -> std::result::Result<(), String> {
|
|
let config = EncoderConfig { fps: 30, bitrate_bps: 500_000, ..EncoderConfig::default() };
|
|
let mut encoder = FfmpegEncoder::new(spec.name, config).map_err(|e| e.to_string())?;
|
|
match encoder.self_test(TEST_SIZE.0, TEST_SIZE.1) {
|
|
Ok((0, _)) => Err("no packet from the test frame".into()),
|
|
Ok((_, false)) => Err("the test frame did not come out as a keyframe".into()),
|
|
Ok(_) => Ok(()),
|
|
Err(Error::CodecUnavailable { reason, .. }) => Err(reason),
|
|
Err(Error::Encoder { message, .. }) => Err(message),
|
|
Err(e) => Err(e.to_string()),
|
|
}
|
|
}
|
|
|
|
/// Whether FFmpeg has `name` at all (cheap, before any self-test).
|
|
fn in_build(ffmpeg: &Ffmpeg, name: &str) -> bool {
|
|
let name = cstr(name);
|
|
// SAFETY: a C string; returns a static codec or NULL.
|
|
!unsafe { (ffmpeg.api.avcodec_find_encoder_by_name)(name.as_ptr()) }.is_null()
|
|
}
|
|
|
|
/// Every backend with its self-test result (run once per process, the
|
|
/// backends in parallel). Empty without FFmpeg.
|
|
pub fn probe() -> &'static [BackendStatus] {
|
|
static PROBE: OnceLock<Vec<BackendStatus>> = OnceLock::new();
|
|
PROBE.get_or_init(|| {
|
|
let Ok(ffmpeg) = Ffmpeg::get() else { return Vec::new() };
|
|
let statuses: Vec<BackendStatus> = std::thread::scope(|scope| {
|
|
let tests: Vec<_> = BACKENDS
|
|
.iter()
|
|
.map(|spec| {
|
|
let present = in_build(ffmpeg, spec.name);
|
|
let test = present.then(|| scope.spawn(move || test_backend(spec)));
|
|
(spec, test)
|
|
})
|
|
.collect();
|
|
tests
|
|
.into_iter()
|
|
.map(|(spec, test)| BackendStatus {
|
|
spec,
|
|
available: match test {
|
|
None => Err("not in this FFmpeg build".into()),
|
|
Some(handle) => {
|
|
handle.join().unwrap_or_else(|_| Err("the self-test panicked".into()))
|
|
}
|
|
},
|
|
})
|
|
.collect()
|
|
});
|
|
for status in &statuses {
|
|
match &status.available {
|
|
Ok(()) => tracing::info!(backend = status.spec.name, "FFmpeg encoder available"),
|
|
Err(e) => {
|
|
tracing::debug!(backend = status.spec.name, "FFmpeg encoder unusable: {e}")
|
|
}
|
|
}
|
|
}
|
|
statuses
|
|
})
|
|
}
|
|
|
|
/// Creates encoders of one backend that passed its self-test.
|
|
pub struct FfmpegFactory {
|
|
spec: &'static BackendSpec,
|
|
}
|
|
|
|
impl FfmpegFactory {
|
|
/// Factories of the available backends, in [`BACKENDS`] order.
|
|
pub fn available() -> Vec<FfmpegFactory> {
|
|
probe()
|
|
.iter()
|
|
.filter(|s| s.available.is_ok())
|
|
.map(|s| FfmpegFactory { spec: s.spec })
|
|
.collect()
|
|
}
|
|
|
|
pub fn spec(&self) -> &'static BackendSpec {
|
|
self.spec
|
|
}
|
|
}
|
|
|
|
impl EncoderFactory for FfmpegFactory {
|
|
fn name(&self) -> &'static str {
|
|
self.spec.name
|
|
}
|
|
|
|
fn codecs(&self) -> Vec<Codec> {
|
|
vec![self.spec.codec]
|
|
}
|
|
|
|
fn create(&self, codec: Codec, config: &EncoderConfig) -> Result<Box<dyn VideoEncoder>> {
|
|
if codec != self.spec.codec {
|
|
return Err(Error::CodecUnavailable {
|
|
codec,
|
|
reason: format!("{} encodes {}", self.spec.name, self.spec.codec),
|
|
});
|
|
}
|
|
Ok(Box::new(FfmpegEncoder::new(self.spec.name, config.clone())?))
|
|
}
|
|
|
|
fn is_hardware(&self) -> bool {
|
|
self.spec.is_hardware()
|
|
}
|
|
|
|
fn is_automatic(&self) -> bool {
|
|
self.spec.is_automatic()
|
|
}
|
|
|
|
fn backend(&self) -> EncoderBackend {
|
|
EncoderBackend::Ffmpeg(self.spec.name)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn backend_table() {
|
|
for (i, spec) in BACKENDS.iter().enumerate() {
|
|
assert!(BACKENDS[..i].iter().all(|b| b.name != spec.name), "{} twice", spec.name);
|
|
assert_eq!(BackendSpec::by_name(spec.name), Some(spec));
|
|
// VA-API needs the hardware frame pool; nothing else does.
|
|
assert_eq!(spec.is_vaapi(), spec.name.ends_with("_vaapi"), "{}", spec.name);
|
|
assert_eq!(spec.is_hardware(), !spec.name.starts_with("lib"), "{}", spec.name);
|
|
}
|
|
assert_eq!(BackendSpec::by_name("h264_vaapi").unwrap().family(), "vaapi");
|
|
assert_eq!(BackendSpec::by_name("libx264").unwrap().family(), "libx264");
|
|
}
|
|
|
|
#[test]
|
|
fn timestamps_follow_packets() {
|
|
let mut t = Timestamps::new();
|
|
t.push(0, 0);
|
|
t.push(1, 3000);
|
|
t.push(3, 9000);
|
|
assert_eq!(t.take(1), Some(3000), "an older entry is skipped");
|
|
assert_eq!(t.take(2), None);
|
|
assert_eq!(t.take(3), Some(9000));
|
|
for i in 0..100 {
|
|
t.push(i, i as u64);
|
|
}
|
|
assert_eq!(t.pending.len(), 64, "bounded");
|
|
}
|
|
|
|
/// [`h264_level`] must agree with the level the signalling offers
|
|
/// (`voelin_stream::h264::level_idc`) for every stream, or the SPS and
|
|
/// the SDP would disagree again. Two copies of ITU-T Table A-1, pinned
|
|
/// to each other: this crate cannot depend on voelin-stream (it is the
|
|
/// lower one), and voelin-stream must not pull in the codecs.
|
|
#[test]
|
|
fn levels_agree_with_the_signalling() {
|
|
for (w, h) in [
|
|
(320u32, 240u32),
|
|
(640, 360),
|
|
(1280, 720),
|
|
(1920, 1080),
|
|
(2560, 1440),
|
|
(3840, 2160),
|
|
(4096, 64),
|
|
(16384, 16384),
|
|
] {
|
|
for fps in [1u32, 15, 30, 60, 120] {
|
|
for kbps in [200u32, 4_000, 8_000, 30_000] {
|
|
for (mine, theirs) in [
|
|
(H264Profile::ConstrainedHigh, voelin_stream::H264Profile::ConstrainedHigh),
|
|
(
|
|
H264Profile::ConstrainedBaseline,
|
|
voelin_stream::H264Profile::ConstrainedBaseline,
|
|
),
|
|
] {
|
|
let bitrate = kbps * 1000;
|
|
let ours = h264_level(mine, w, h, fps, bitrate);
|
|
let signalled =
|
|
voelin_stream::h264::level_idc(theirs, w, h, fps, u64::from(bitrate));
|
|
assert_eq!(
|
|
ours,
|
|
i64::from(signalled),
|
|
"{w}x{h}@{fps} {kbps}k {mine:?}: encoder level {ours}, offer \
|
|
{signalled}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn settings_per_backend() {
|
|
let config = EncoderConfig::default();
|
|
let names = |spec: &str, low_power| -> Vec<&'static str> {
|
|
settings(BackendSpec::by_name(spec).unwrap(), &config, low_power)
|
|
.iter()
|
|
.map(|s| s.name)
|
|
.collect()
|
|
};
|
|
assert!(names("libx264", false).contains(&"tune"));
|
|
assert!(names("h264_nvenc", false).contains(&"forced-idr"));
|
|
assert!(!names("h264_vaapi", false).contains(&"low_power"));
|
|
assert!(names("h264_vaapi", true).contains(&"low_power"));
|
|
assert!(names("libaom-av1", false).contains(&"lag-in-frames"));
|
|
let baseline = EncoderConfig { h264_profile: H264Profile::ConstrainedBaseline, ..config };
|
|
let x264 = settings(BackendSpec::by_name("libx264").unwrap(), &baseline, false);
|
|
let profile = x264.iter().find(|s| s.name == "profile").unwrap();
|
|
assert_eq!(profile.values, ["baseline"]);
|
|
}
|
|
}
|