diff --git a/Cargo.lock b/Cargo.lock index ac7fbf51..3cd5b8ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1144,6 +1144,14 @@ dependencies = [ "ec-aac", ] +[[package]] +name = "rusty_h264" +version = "0.9.1" +dependencies = [ + "ec-h264", + "ec-h264-syntax", +] + [[package]] name = "rusty_mp3" version = "0.6.1" diff --git a/Cargo.toml b/Cargo.toml index c88558ce..ee68cd99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ ec-av1-syntax = { path = "crates/ec-av1-syntax", version = "0.1.0" } ec-core = { path = "crates/ec-core", version = "0.1.0" } ec-dsp = { path = "crates/ec-dsp", version = "0.1.0" } ec-flac = { path = "crates/ec-flac", version = "0.1.0" } +ec-h264 = { path = "crates/ec-h264", version = "0.1.0" } ec-h264-syntax = { path = "crates/ec-h264-syntax", version = "0.1.0" } ec-h265-syntax = { path = "crates/ec-h265-syntax", version = "0.1.0" } ec-image = { path = "crates/ec-image", version = "0.1.0" } diff --git a/shims/rusty_h264/Cargo.toml b/shims/rusty_h264/Cargo.toml new file mode 100644 index 00000000..fb762503 --- /dev/null +++ b/shims/rusty_h264/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rusty_h264" +description = "Compatibility shim: the rusty_h264 0.9.1 surface edith consumes, served by ec-h264." +version = "0.9.1" +edition = "2024" +license = "MIT OR Apache-2.0" +authors = ["Ahmet Kaan Gümüş"] +publish = false + +[dependencies] +ec-h264 = { workspace = true } +ec-h264-syntax = { workspace = true } diff --git a/shims/rusty_h264/src/lib.rs b/shims/rusty_h264/src/lib.rs new file mode 100644 index 00000000..ca44a20a --- /dev/null +++ b/shims/rusty_h264/src/lib.rs @@ -0,0 +1,302 @@ +//! The `rusty_h264` 0.9.1 surface, served by `ec-h264`. +//! +//! Only what edith consumes is here, at the incumbent's signatures: +//! `Decoder::new` + `decode` over whole access units (decode.rs:1478-1500, +//! export.rs:4200), and `EncoderConfig` + `Encoder::new` + `try_encode` + +//! `try_flush` with `YuvFrame` and `Preset` (export.rs:3407-3422, 3640-3650, +//! 3727-3736). +//! +//! Two differences from the incumbent, both deliberate and both visible only +//! as *absences*: +//! +//! - No `#[global_allocator]`. The incumbent's `common` crate installed one +//! process-wide and had to be vendored out; there is nothing to vendor here. +//! - No lookahead buffering. `try_encode` returns the access unit of the +//! picture it was handed, every time, so `try_flush` has nothing left to +//! give and the caller's per-picture timing is exact whatever the rate +//! control mode. +//! +//! Pictures come out in decode order, which is what the incumbent did and what +//! edith's decode loop assumes. [`Decoder::in_display_order`] is the same +//! decoder with the reordering the incumbent never had. + +#![forbid(unsafe_code)] + +use ec_h264::{NalOutcome, OutputOrder, PictureView}; + +/// The incumbent's error type: one variant per refusal reason. +#[derive(Debug)] +pub enum Error { + NeedMore, + Invalid(String), + Unsupported(String), +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::NeedMore => write!(f, "need more data"), + Error::Invalid(m) => write!(f, "invalid: {m}"), + Error::Unsupported(m) => write!(f, "unsupported: {m}"), + } + } +} + +impl std::error::Error for Error {} + +impl From for Error { + fn from(e: ec_h264::Error) -> Error { + match e { + ec_h264::Error::NeedMore => Error::NeedMore, + ec_h264::Error::Unsupported { .. } => Error::Unsupported(e.to_string()), + other => Error::Invalid(other.to_string()), + } + } +} + +/// A decoded — or about to be encoded — I420 picture, planes owned. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct YuvFrame { + pub width: usize, + pub height: usize, + pub y: Vec, + pub u: Vec, + pub v: Vec, +} + +/// Speed/quality ladder. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Preset { + #[default] + Fast, + Balanced, +} + +impl From for ec_h264::Preset { + fn from(p: Preset) -> ec_h264::Preset { + match p { + Preset::Fast => ec_h264::Preset::Fast, + Preset::Balanced => ec_h264::Preset::Balanced, + } + } +} + +/// The H.264 software decoder. +pub struct Decoder { + inner: ec_h264::Decoder, +} + +impl Default for Decoder { + fn default() -> Decoder { + Decoder::new() + } +} + +impl Decoder { + /// A decoder that emits pictures in decode order. + pub fn new() -> Decoder { + let mut inner = ec_h264::Decoder::new(); + inner.set_output_order(OutputOrder::Decode); + Decoder { inner } + } + + /// A decoder that emits pictures in *display* order, which a stream with + /// B pictures needs and the incumbent could not do. Pictures then come out + /// with the stream's own reordering delay, so a caller that stops feeding + /// must call [`Decoder::flush`] to collect the tail. + pub fn in_display_order() -> Decoder { + let mut inner = ec_h264::Decoder::new(); + inner.set_output_order(OutputOrder::Display); + Decoder { inner } + } + + /// Decode one access unit (Annex B, one coded picture), returning the + /// picture it completed if any. + pub fn decode(&mut self, au: &[u8]) -> Result, Error> { + for nal in ec_h264_syntax::AnnexBIter::new(au) { + if self.inner.push_nal(nal)? == NalOutcome::PictureBoundary { + // Two pictures in one access unit: close the first one. + self.inner.end_picture()?; + self.inner.push_nal(nal)?; + } + } + if self.inner.picture_open() { + self.inner.end_picture()?; + } + Ok(self.take()) + } + + /// Collect a picture the reordering delay is still holding, at end of + /// stream. + pub fn flush(&mut self) -> Result, Error> { + self.inner.flush()?; + Ok(self.take()) + } + + fn take(&mut self) -> Option { + let frame = self.inner.next_frame()?; + let (w, h) = (frame.width as usize, frame.height as usize); + let plane = |i: usize, pw: usize, ph: usize| -> Vec { + let p = &frame.planes[i]; + let mut out = Vec::with_capacity(pw * ph); + for row in 0..ph { + out.extend_from_slice(&p.data[row * p.stride..row * p.stride + pw]); + } + out + }; + Some(YuvFrame { + width: w, + height: h, + y: plane(0, w, h), + u: plane(1, w / 2, h / 2), + v: plane(2, w / 2, h / 2), + }) + } +} + +/// Encoder settings, at the incumbent's field names. +#[derive(Debug, Clone)] +pub struct EncoderConfig { + pub width: usize, + pub height: usize, + pub framerate: f32, + /// Bits per second; zero codes at a constant quantiser instead. + pub bitrate: u32, + pub gop_size: u32, + /// Consecutive B pictures. Accepted at any value and coded as zero: this + /// encoder has no B pictures yet, and refusing the field would break a + /// caller that sets it. + pub bframes: u32, + pub preset: Preset, +} + +impl EncoderConfig { + /// The incumbent's constructor: geometry now, everything else by field. + pub fn new(width: usize, height: usize) -> EncoderConfig { + EncoderConfig { + width, + height, + framerate: 30.0, + bitrate: 0, + gop_size: 250, + bframes: 0, + preset: Preset::Fast, + } + } +} + +/// The H.264 software encoder. +pub struct Encoder { + inner: ec_h264::Encoder, + width: usize, + height: usize, +} + +impl Encoder { + pub fn new(cfg: EncoderConfig) -> Result { + let mut inner_cfg = ec_h264::EncoderConfig::new(cfg.width as u32, cfg.height as u32); + inner_cfg.framerate = cfg.framerate; + inner_cfg.bitrate = cfg.bitrate; + inner_cfg.gop_size = cfg.gop_size; + inner_cfg.bframes = cfg.bframes; + inner_cfg.preset = cfg.preset.into(); + let inner = ec_h264::Encoder::new(inner_cfg)?; + Ok(Encoder { + inner, + width: cfg.width, + height: cfg.height, + }) + } + + /// One picture in, its access unit out — always, with no lookahead delay. + pub fn try_encode(&mut self, frame: &YuvFrame) -> Result, Error> { + if frame.width != self.width || frame.height != self.height { + return Err(Error::Invalid(format!( + "frame is {}x{}, encoder is {}x{}", + frame.width, frame.height, self.width, self.height + ))); + } + let view = PictureView::i420( + self.width as u32, + self.height as u32, + &frame.y, + &frame.u, + &frame.v, + ); + Ok(self.inner.encode(&view)?.au) + } + + /// Nothing is ever held back, so this is always empty; it exists because + /// the caller's drain path calls it. + pub fn try_flush(&mut self) -> Result, Error> { + Ok(Vec::new()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn frame(w: usize, h: usize, t: usize) -> YuvFrame { + YuvFrame { + width: w, + height: h, + y: (0..w * h) + .map(|i| ((i / w + i % w + t * 4) % 256) as u8) + .collect(), + u: vec![110; w / 2 * h / 2], + v: vec![150; w / 2 * h / 2], + } + } + + /// The whole surface edith calls, in the order edith calls it: configure, + /// encode picture by picture, drain, then decode what came out. + #[test] + fn the_edith_call_sequence_round_trips() { + let (w, h) = (176usize, 144usize); + let mut cfg = EncoderConfig::new(w, h); + cfg.framerate = 25.0; + cfg.bitrate = 600_000; + cfg.gop_size = 5; + cfg.bframes = 0; + cfg.preset = Preset::Fast; + let mut enc = Encoder::new(cfg).expect("encoder"); + let mut units = Vec::new(); + for t in 0..6 { + let au = enc.try_encode(&frame(w, h, t)).expect("encode"); + assert!(!au.is_empty(), "picture {t} produced no access unit"); + units.push(au); + } + assert!(enc.try_flush().expect("flush").is_empty()); + + let mut dec = Decoder::new(); + for (t, au) in units.iter().enumerate() { + let got = dec.decode(au).expect("decode").expect("a picture"); + assert_eq!((got.width, got.height), (w, h), "picture {t} geometry"); + assert_eq!(got.y.len(), w * h); + assert_eq!(got.u.len(), w / 2 * h / 2); + // A coded picture is close to its source; this is the shim's own + // proof that the two halves are wired to each other. + let src = frame(w, h, t); + let mse: f64 = got + .y + .iter() + .zip(&src.y) + .map(|(&a, &b)| { + let d = f64::from(a) - f64::from(b); + d * d + }) + .sum::() + / (w * h) as f64; + assert!(mse < 25.0, "picture {t}: mean squared error {mse:.1}"); + } + } + + /// A frame whose geometry does not match the encoder is refused by name. + #[test] + fn geometry_mismatch_is_refused() { + let mut enc = Encoder::new(EncoderConfig::new(64, 64)).expect("encoder"); + let err = enc.try_encode(&frame(32, 32, 0)).unwrap_err(); + assert!(matches!(err, Error::Invalid(_)), "{err}"); + } +}