diff --git a/.github/workflows/h264-encoder.yml b/.github/workflows/h264-encoder.yml index ee1057c3e..c10da5a67 100644 --- a/.github/workflows/h264-encoder.yml +++ b/.github/workflows/h264-encoder.yml @@ -11,6 +11,7 @@ on: - crates/coreav-transcode/** - crates/coreav-frame/** - scripts/native_h264_cli.py + - scripts/track_boxes.py - crates/coreav-kernels-h264/** - crates/coreav-h264/** - scripts/h264_g1* @@ -38,6 +39,7 @@ on: - crates/coreav-transcode/** - crates/coreav-frame/** - scripts/native_h264_cli.py + - scripts/track_boxes.py - crates/coreav-kernels-h264/** - crates/coreav-h264/** - scripts/h264_g1* @@ -94,6 +96,8 @@ jobs: cp target/release/coreav "$RUNNER_TEMP/default-base" cargo build --locked --release -p coreav-cli --features native-h264 cp target/release/coreav "$RUNNER_TEMP/external-base" + cargo build --locked --release -p coreav-cli --features native-h264-encoder,native-h264 + cp target/release/coreav "$RUNNER_TEMP/native-base" - name: Keep native encoding out of default and release builds run: | set -euo pipefail @@ -132,6 +136,7 @@ jobs: --default-candidate "$RUNNER_TEMP/default-candidate" \ --baseline-default "$RUNNER_TEMP/default-base" \ --baseline-external "$RUNNER_TEMP/external-base" \ + --baseline-native "$RUNNER_TEMP/native-base" \ --driver target/release/examples/p \ --ffmpeg "$RUNNER_TEMP/cli-oracles/ffmpeg" \ --output "$RUNNER_TEMP/native-cli" diff --git a/crates/coreav-cli/src/ffmpeg/native_h264.rs b/crates/coreav-cli/src/ffmpeg/native_h264.rs index 2f4d668b5..c9af6fa33 100644 --- a/crates/coreav-cli/src/ffmpeg/native_h264.rs +++ b/crates/coreav-cli/src/ffmpeg/native_h264.rs @@ -61,9 +61,7 @@ pub(super) fn source(report: &Report, index: usize) -> bool { && matches!(field("pix_fmt"), Some(Value::Str(s)) if s == "yuv420p") && dimension("width", 1920) && dimension("height", 1080) - && stream - .r_frame_rate() - .is_some_and(|(n, d)| d.checked_mul(30) == Some(n)) + && stream.r_frame_rate().is_some() } /// Tries the opt-in encoder without changing the command used on fallback. @@ -160,8 +158,6 @@ mod tests { ("field_order", Value::Na), ("pix_fmt", Value::Str("yuv420p10le".into())), ("pix_fmt", Value::Str("yuv444p".into())), - ("r_frame_rate", Value::Str("30000/1001".into())), - ("r_frame_rate", Value::Str("3002/100".into())), ("r_frame_rate", Value::Str("0/0".into())), ] { let mut changed = report.clone(); @@ -174,6 +170,17 @@ mod tests { } assert!(!source(&changed, 0), "{name}: {value:?}"); } + for rate in ["25/1", "24000/1001", "30000/1001", "1000/33"] { + let mut changed = report.clone(); + if let Some(stream) = changed.streams.first_mut() { + for (name, field) in &mut stream.entries { + if *name == "r_frame_rate" { + *field = Value::Str(rate.into()); + } + } + } + assert!(source(&changed, 0), "{rate}"); + } } fn args(extra: &str) -> Vec { diff --git a/crates/coreav-h264-encoder/examples/p.rs b/crates/coreav-h264-encoder/examples/p.rs index 2e4468063..e5519accd 100644 --- a/crates/coreav-h264-encoder/examples/p.rs +++ b/crates/coreav-h264-encoder/examples/p.rs @@ -21,10 +21,11 @@ fn run() -> Result<(), Box> { decoded, deblocking, interval, + metadata @ .., ] = args.as_slice() else { return Err( - "usage: p WIDTH HEIGHT QP_OR_crf:N FRAMES INPUT OUTPUT RECON DECODED DEBLOCKING INTERVAL".into(), + "usage: p WIDTH HEIGHT QP_OR_crf:N FRAMES INPUT OUTPUT RECON DECODED DEBLOCKING INTERVAL [FPS_NUM FPS_DEN SAR_NUM SAR_DEN]".into(), ); }; let deblocking = if deblocking.parse::()? { @@ -35,9 +36,21 @@ fn run() -> Result<(), Box> { let width = width.parse::()?; let height = height.parse::()?; let frames = frames.parse::()?; + let (frame_rate, sample_aspect_ratio) = match metadata { + [] => (Default::default(), Default::default()), + [num, den, sar_num, sar_den] => ( + coreav_h264_encoder::FrameRate::new(num.parse()?, den.parse()?)?, + if sar_num == "0" && sar_den == "1" { + Default::default() + } else { + coreav_h264_encoder::SampleAspectRatio::new(sar_num.parse()?, sar_den.parse()?)? + }, + ), + _ => return Err("expected four optional timing and aspect terms".into()), + }; let settings = Settings { - frame_rate: Default::default(), - sample_aspect_ratio: Default::default(), + frame_rate, + sample_aspect_ratio, width, height, rate_mode: match qp.strip_prefix("crf:") { diff --git a/crates/coreav-transcode/src/native_h264.rs b/crates/coreav-transcode/src/native_h264.rs index 78ebe6208..db637db1d 100644 --- a/crates/coreav-transcode/src/native_h264.rs +++ b/crates/coreav-transcode/src/native_h264.rs @@ -1,7 +1,7 @@ //! Bounded native H.264 access units for the existing transcode spool and muxers. use coreav_frame::{ChromaLocation, ColorInfo, PixelFormat, Pixels}; -use coreav_h264_encoder::{Colour, Deblocking, Gop, Limits, Plane, Settings}; +use coreav_h264_encoder::{Colour, Deblocking, Gop, Limits, Plane, SampleAspectRatio, Settings}; use crate::Error; use crate::encoder::{EncoderSettings, Sink}; @@ -25,6 +25,24 @@ pub(crate) fn color(color: ColorInfo) -> ColorInfo { } } +pub(crate) fn aspect(value: Option<(u64, u64)>) -> Result { + let Some((mut n, mut d)) = value.filter(|(n, d)| *n > 0 && *d > 0 && n != d) else { + return Ok(SampleAspectRatio::default()); + }; + let (mut a, mut b) = (n, d); + while b != 0 { + (a, b) = (b, a % b); + } + n /= a; + d /= a; + let invalid = || Error::NativeIneligible("the sample aspect does not fit native H.264"); + SampleAspectRatio::new( + u32::try_from(n).map_err(|_| invalid())?, + u32::try_from(d).map_err(|_| invalid())?, + ) + .map_err(ineligible) +} + pub(crate) struct Encoder { encoder: coreav_h264_encoder::Encoder, sink: S, @@ -39,17 +57,23 @@ impl Encoder { max_bytes: u64, sink: S, ) -> Result { - if settings.format != PixelFormat::YUV420P - || settings.sample_aspect.is_some_and(|(n, d)| n != d) - { + if settings.format != PixelFormat::YUV420P { return Err(Error::NativeIneligible( - "native H.264 needs square 8-bit 4:2:0 pixels", + "native H.264 needs 8-bit 4:2:0 pixels", )); } let c = settings.color; let settings_native = Settings { - frame_rate: Default::default(), - sample_aspect_ratio: Default::default(), + frame_rate: coreav_h264_encoder::FrameRate::new( + settings.frame_rate.num, + settings.frame_rate.den, + ) + .map_err(ineligible)?, + sample_aspect_ratio: aspect( + settings + .sample_aspect + .map(|(n, d)| (u64::from(n), u64::from(d))), + )?, width: settings.width as usize, height: settings.height as usize, rate_mode: request.rate, @@ -234,9 +258,87 @@ mod tests { sample_aspect: Some((4, 3)), ..settings(1) }; + assert!(Encoder::new(&settings, request, 1_000_000, Bytes::default()).is_ok()); + let settings = EncoderSettings { + sample_aspect: Some((65536, 65535)), + ..settings + }; assert!(matches!( Encoder::new(&settings, request, 1_000_000, Bytes::default()), Err(Error::NativeIneligible(_)) )); } + + #[test] + fn sample_aspect_is_exact_and_preserves_unspecified_and_square_bytes() { + for value in [ + None, + Some((0, 1)), + Some((1, 0)), + Some((1, 1)), + Some((u64::MAX, u64::MAX)), + ] { + assert_eq!(aspect(value).unwrap(), SampleAspectRatio::default()); + } + assert_eq!( + aspect(Some((16 * 1_000_000_000, 15 * 1_000_000_000))).unwrap(), + SampleAspectRatio::new(16, 15).unwrap() + ); + assert!(aspect(Some((u64::MAX, u64::MAX - 1))).is_err()); + assert!(aspect(Some((65536, 65535))).is_err()); + let mut previous = None; + for sample_aspect in [None, Some((0, 1)), Some((1, 1)), Some((16, 16))] { + let settings = EncoderSettings { + sample_aspect, + ..settings(1) + }; + let mut encoder = Encoder::new( + &settings, + NativeH264 { + rate: RateMode::Crf(23), + }, + 1_000_000, + Bytes::default(), + ) + .unwrap(); + encoder.send_frame(Pixels::Cpu(picture())).unwrap(); + let bytes = encoder.finish().0; + if let Some(previous) = &previous { + assert_eq!(&bytes, previous); + } + previous = Some(bytes); + } + } + + #[test] + fn fractional_rate_and_declared_aspect_are_thread_independent() { + let mut previous = None; + for threads in [0, 1, 2, 8] { + let settings = EncoderSettings { + frame_rate: FrameRate { + num: 30000, + den: 1001, + }, + sample_aspect: Some((16, 15)), + ..settings(threads) + }; + let mut encoder = Encoder::new( + &settings, + NativeH264 { + rate: RateMode::FixedQp(28), + }, + 1_000_000, + Bytes::default(), + ) + .unwrap(); + for _ in 0..3 { + encoder.send_frame(Pixels::Cpu(picture())).unwrap(); + } + let bytes = encoder.finish().0; + if let Some(previous) = &previous { + assert_eq!(&bytes, previous); + } + previous = Some(bytes); + } + } } diff --git a/crates/coreav-transcode/src/pipeline.rs b/crates/coreav-transcode/src/pipeline.rs index b7db60ece..f872d7275 100644 --- a/crates/coreav-transcode/src/pipeline.rs +++ b/crates/coreav-transcode/src/pipeline.rs @@ -550,15 +550,19 @@ fn execute( .ok_or_else(|| unsupported(missing)) }; #[cfg(feature = "h264-native")] - let program = match options.native_h264 { + let (program, frame_rate) = match options.native_h264 { Some(native) => { if options.matroska_output.is_some() { return Err(Error::NativeIneligible("native H.264 requires MP4 output")); } - native_timing(&presented, &video)?; - H264Program::Native(native) + let source_rate = options + .frame_rates + .iter() + .find_map(|(index, rate)| (*index == video.stream_index).then_some(*rate)); + let frame_rate = native_timing(&presented, &video, source_rate)?; + (H264Program::Native(native), frame_rate) } - None => external()?, + None => (external()?, frame_rate), }; #[cfg(not(feature = "h264-native"))] let program = external()?; @@ -1570,7 +1574,11 @@ impl Run<'_> { .chroma_location .is_some_and(|c| c != coreav_frame::ChromaLocation::Left) || self.sent_pts.last().is_some_and(|last| { - !native_interval(i128::from(time) - i128::from(*last), self.video.time_base) + !native_interval( + i128::from(time) - i128::from(*last), + self.video.time_base, + self.frame_rate, + ) }) { return Err(Error::NativeIneligible( @@ -1731,6 +1739,10 @@ impl Run<'_> { } else { scaled_to.unwrap_or((width, height)) }; + #[cfg(feature = "h264-native")] + if self.options.native_h264.is_some() { + crate::native_h264::aspect(self.video.pixel_aspect)?; + } let sample_aspect = sample_aspect(self.video.pixel_aspect, (width, height), scaled_to); let color = output_color(self.video.stream.color.or(frame.color)); #[cfg(feature = "h264-native")] @@ -1759,6 +1771,12 @@ impl Run<'_> { .map(min_keyint), ..EncoderSettings::new(out_w, out_h, frame.format(), self.frame_rate) }; + #[cfg(feature = "h264-native")] + if self.options.native_h264.is_some() { + presentation_size(&settings).map_err(|_| { + Error::NativeIneligible("the sample aspect does not fit the MP4 display size") + })?; + } settings.validate().map_err(|e| match e.kind { ErrorKind::Unsupported | ErrorKind::Limit => Error::Unsupported(e.message), _ => Error::Encode(e), @@ -1790,33 +1808,112 @@ fn native_size(width: u32, height: u32) -> bool { } #[cfg(feature = "h264-native")] -fn native_interval(ticks: i128, time_base: Rational) -> bool { +fn native_interval(ticks: i128, time_base: Rational, rate: FrameRate) -> bool { ticks > 0 && ticks .checked_mul(time_base.num) - .and_then(|n| n.checked_mul(30)) - == Some(i128::from(time_base.den)) + .and_then(|n| n.checked_mul(i128::from(rate.num))) + == i128::from(time_base.den).checked_mul(i128::from(rate.den)) } #[cfg(feature = "h264-native")] -fn native_timing(presented: &Presented, video: &VideoStream) -> Result<(), Error> { - let size = video.stream.width.zip(video.stream.height); - if !size.is_some_and(|(w, h)| native_size(w, h)) - || presented.times.len() != video.packets.iter().filter(|p| p.presented).count() - || presented.times.windows(2).any(|w| match w { - [a, b] => !native_interval(i128::from(*b) - i128::from(*a), video.time_base), +fn native_rate( + times: &[i64], + duration: Option, + time_base: Rational, +) -> Result { + let invalid = + || Error::NativeIneligible("native H.264 needs an exactly constant supported frame rate"); + let period = match times { + [a, b, ..] => u64::try_from(i128::from(*b) - i128::from(*a)).map_err(|_| invalid())?, + [_] => duration.ok_or_else(invalid)?, + _ => return Err(invalid()), + }; + if period == 0 + || duration.is_some_and(|d| d != period) + || times.windows(2).any(|w| match w { + [a, b] => i128::from(*b) - i128::from(*a) != i128::from(period), _ => true, }) - || !native_interval( + { + return Err(invalid()); + } + let num = u128::from(time_base.den); + let den = u128::try_from(time_base.num) + .ok() + .filter(|n| *n > 0) + .and_then(|n| n.checked_mul(u128::from(period))) + .ok_or_else(invalid)?; + let divisor = gcd(num, den); + if divisor == 0 { + return Err(invalid()); + } + let rate = coreav_h264_encoder::FrameRate::new( + u32::try_from(num / divisor).map_err(|_| invalid())?, + u32::try_from(den / divisor).map_err(|_| invalid())?, + ) + .map_err(|_| invalid())?; + let (num, den) = rate.as_ratio(); + if u32::try_from(period).is_err() { + return Err(Error::NativeIneligible( + "the constant interval does not fit the MP4 sample clock", + )); + } + Ok(FrameRate { num, den }) +} + +#[cfg(feature = "h264-native")] +fn legacy_native_rate( + presented: &Presented, + time_base: Rational, + aspect: Option<(u64, u64)>, + source_rate: Option, +) -> Option { + let rate = FrameRate { num: 30, den: 1 }; + (aspect.is_none_or(|(n, d)| n == 0 || d == 0 || n == d) + && source_rate.is_none_or(|r| r.den > 0 && r.den.checked_mul(30) == Some(r.num)) + && presented.times.windows(2).all(|w| match w { + [a, b] => native_interval(i128::from(*b) - i128::from(*a), time_base, rate), + _ => false, + }) + && native_interval( i128::from(presented.last_duration(&presented.times, Some(presented.last))), - video.time_base, - ) + time_base, + rate, + )) + .then_some(rate) +} + +#[cfg(feature = "h264-native")] +fn native_timing( + presented: &Presented, + video: &VideoStream, + source_rate: Option, +) -> Result { + let size = video.stream.width.zip(video.stream.height); + if !size.is_some_and(|(w, h)| native_size(w, h)) + || presented.times.len() != video.packets.iter().filter(|p| p.presented).count() + { + return Err(Error::NativeIneligible( + "native H.264 needs fixed-size constant-rate input", + )); + } + if let Some(rate) = + legacy_native_rate(presented, video.time_base, video.pixel_aspect, source_rate) { + return Ok(rate); + } + if presented.times.len() != presented.distinct.len() { return Err(Error::NativeIneligible( - "native H.264 needs fixed-size constant 30 fps input", + "native H.264 needs distinct presentation timestamps", )); } - Ok(()) + let duration = video + .packets + .iter() + .find(|p| p.presented && p.pts == presented.last) + .and_then(|p| p.duration); + native_rate(&presented.times, duration, video.time_base) } /// The colour description the encoder signals: the frames', without the @@ -2087,14 +2184,95 @@ mod tests { assert!(!native_size(w, h)); } let time_base = Rational::new(1, 15360).unwrap(); - assert!(native_interval(512, time_base)); + let rate = FrameRate { num: 30, den: 1 }; + assert!(native_interval(512, time_base, rate)); for ticks in [-1, 0, 511, 513, i128::MAX] { - assert!(!native_interval(ticks, time_base)); + assert!(!native_interval(ticks, time_base, rate)); } - assert!(!native_interval(1, Rational::new(1001, 30000).unwrap())); + assert!(!native_interval( + 1, + Rational::new(1001, 30000).unwrap(), + rate + )); } use super::*; + #[cfg(feature = "h264-native")] + #[test] + fn native_clock_uses_exact_positive_rationals_and_last_duration() { + for (num, den) in [(25, 1), (24000, 1001), (30000, 1001), (1000, 33)] { + let time_base = Rational::new(i128::from(den), u64::from(num)).unwrap(); + for times in [ + &[0, 1, 2][..], + &[90, 91, 92][..], + &[-3, -2, -1][..], + &[17][..], + ] { + let found = native_rate(times, Some(1), time_base).unwrap(); + assert_eq!((found.num, found.den), (num, den)); + assert!(native_interval(1, time_base, found)); + } + assert!(native_rate(&[0, 1, 2], None, time_base).is_ok()); + } + let time_base = Rational::new(1, 30000).unwrap(); + for (times, duration) in [ + (&[][..], None), + (&[0][..], None), + (&[0][..], Some(0)), + (&[0, 0, 1][..], Some(1)), + (&[0, 1001, 2003][..], Some(1001)), + (&[0, 1001, 2002][..], Some(1002)), + (&[0, 1001][..], Some(0)), + (&[i64::MIN, i64::MAX][..], None), + ] { + assert!(matches!( + native_rate(times, duration, time_base), + Err(Error::NativeIneligible(_)) + )); + } + assert!(native_rate(&[0, 33, 67, 100], Some(33), Rational::new(1, 1000).unwrap()).is_err()); + assert!(native_rate(&[0, 1], Some(1), Rational::new(-1, 1000).unwrap()).is_err()); + assert!(native_rate(&[0, 1], Some(1), Rational::new(1, u64::MAX).unwrap()).is_err()); + assert!( + native_rate( + &[0, 1], + Some(1), + Rational::new(1, u64::from(u32::MAX)).unwrap() + ) + .is_err() + ); + let wire_limit = native_rate( + &[0, 2], + Some(2), + Rational::new(1, u64::from(u32::MAX)).unwrap(), + ) + .unwrap(); + assert_eq!((wire_limit.num, wire_limit.den), (u32::MAX, 2)); + assert!( + native_rate( + &[i64::MIN, i64::MAX], + Some(u64::MAX), + Rational::new(i128::MAX, u64::MAX).unwrap() + ) + .is_err() + ); + let slow = native_rate( + &[0, 3_000_000_000], + Some(3_000_000_000), + Rational::new(1, 1_000_000_000).unwrap(), + ) + .unwrap(); + assert_eq!((slow.num, slow.den), (1, 3)); + assert!( + native_rate( + &[0, 5_000_000_000], + Some(5_000_000_000), + Rational::new(1, 1_000_000_000).unwrap() + ) + .is_err() + ); + } + #[test] fn invalid_crop_options_refuse_before_source_access() { struct Unread; @@ -2341,6 +2519,57 @@ mod tests { } } + #[cfg(feature = "h264-native")] + #[test] + fn legacy_native_clock_keeps_only_preexisting_timing_defaults() { + let time_base = Rational::new(1, 30).unwrap(); + for times in [&[0][..], &[0, 1, 2][..]] { + let mut clock = presented(times, 1); + clock.durations.clear(); + for aspect in [None, Some((1, 1)), Some((0, 1)), Some((1, 0))] { + for hint in [ + None, + Some(FrameRate { num: 30, den: 1 }), + Some(FrameRate { num: 60, den: 2 }), + ] { + assert_eq!( + legacy_native_rate(&clock, time_base, aspect, hint).map(|r| (r.num, r.den)), + Some((30, 1)) + ); + } + } + for hint in [ + FrameRate { num: 25, den: 1 }, + FrameRate { num: 0, den: 0 }, + FrameRate { + num: 1, + den: u32::MAX, + }, + ] { + assert!(legacy_native_rate(&clock, time_base, None, Some(hint)).is_none()); + } + assert!(legacy_native_rate(&clock, time_base, Some((4, 3)), None).is_none()); + assert!( + legacy_native_rate(&clock, Rational::new(1, 25).unwrap(), None, None).is_none() + ); + } + assert!(legacy_native_rate(&presented(&[0, 1, 2], 2), time_base, None, None).is_none()); + assert!(legacy_native_rate(&presented(&[0], 2), time_base, None, None).is_none()); + let strict = native_rate(&[0], Some(2), time_base).unwrap(); + assert_eq!((strict.num, strict.den), (15, 1)); + let strict = native_rate(&[0], Some(1), Rational::new(1, 25).unwrap()).unwrap(); + assert_eq!((strict.num, strict.den), (25, 1)); + let mut repeated = presented(&[0, 1, 2], 1); + repeated.pts = [0].into_iter().collect(); + repeated.distinct = vec![0]; + repeated.last = 0; + repeated.durations = [(0, 1)].into_iter().collect(); + assert_eq!( + legacy_native_rate(&repeated, time_base, None, None).map(|r| (r.num, r.den)), + Some((30, 1)) + ); + } + #[test] fn repeated_times_move_one_tick() { // WebM blocks whose millisecond times repeat: kept, each one tick diff --git a/docs/agents/transcode.md b/docs/agents/transcode.md index 873b511dd..73ee315f9 100644 --- a/docs/agents/transcode.md +++ b/docs/agents/transcode.md @@ -44,3 +44,6 @@ Encode path, sandboxed workers, and audio-encode selection. | Native FFV1 refuses declared chroma siting that the Matroska writer cannot retain | `colour` owns admission for both stream and frame metadata; packet-admission tests reject all five non-left locations before encoder creation or packet publication. The required CLI gate verifies a real centered-chroma source and its specific refusal, while negative tests reject wrong source signalling, false success and unrelated errors. Frame types alone cannot express which metadata a selected container writer preserves | | The `coreav ffmpeg` video encode (H.264/AV1), audio encode and FFV1 jobs run under one command limit, never a library default such as the 60-second decoder timeout; the AVI MP3 encode keeps its own 120-second deadline | `ffmpeg::ENCODE_LIMIT` is the single owner that the encode request, the audio deadline and the FFV1 job options read. `the_encode_has_the_same_command_limit_as_other_encodes` failed on the FFV1 job's former 60-second default. A type cannot make a job builder read the constant, so the test covers the job that once missed it | | Native H.264 refusal retries inherit the original command deadline, never a fresh encode limit | `EncodeAttempt` carries the same absolute deadline through both CLI attempts. `ffmpeg_native_retry_uses_only_the_original_deadline` checks reduced and expired budgets; `expired_external_retry_keeps_destination_and_removes_staging` executes the retry boundary and requires timeout without replacing the destination or leaving staging. Expiry is a runtime property, not a type invariant | +| A native H.264 rate must fit both VUI and the unchanged MP4 source-clock durations after exact reduction | `native_rate` owns the library rate validation and positive 32-bit stored-interval guard. `native_clock_uses_exact_positive_rationals_and_last_duration` accepts a reducible three-billion-tick interval and rejects five billion ticks, explicit zero or unequal final durations and nonuniform clocks. The rate type cannot establish whether the original clock fits the separate muxer's duration field; the native boundary must retain that check | +| The native CLI conformance reader rejects zero explicit SPS aspect terms with a named diagnostic | `sps_metadata` validates both terms before constructing a fraction. `test_explicit_sps_sample_aspect_rejects_zero_terms_with_a_label` covers zero width, zero height and both zero, with a valid extended-aspect control. A general fraction type permits zero numerators and raises an unlabelled exception for zero denominators, so the conformance boundary owns the positive-term check | +| Widening native H.264 metadata admission retains every previously eligible 30 fps command's timing defaults | `legacy_native_rate` owns the former normalized-clock predicate before strict new admission. `legacy_native_clock_keeps_only_preexisting_timing_defaults` checks inference, normalized repeats and exclusion of new rate/aspect hints. Actual-base single/terminal-zero comparisons in `native_h264_cli.py` require native success without external programs and identical complete MP4 bytes; paired non-30/non-square controls require unchanged fallback. A stricter positive-duration predicate alone can reject commands the former ledger accepted | diff --git a/docs/design/cli-native-h264-metadata.md b/docs/design/cli-native-h264-metadata.md new file mode 100644 index 000000000..f83a1d206 --- /dev/null +++ b/docs/design/cli-native-h264-metadata.md @@ -0,0 +1,31 @@ +# Native H.264 constant frame rates and sample aspects + +## Scope + +Extend the explicit MP4 encoder preference from coreav-dev/coreav#509 using the checked timing and sample-aspect types from coreav-dev/coreav#511. Admit supported positive constant rational frame rates and declared non-square sample aspects. Keep the default build, release feature sets, unflagged commands, original fallback arguments, command deadline, geometry, pixel-format and color exclusions unchanged. Matroska output remains separate work. + +## Exact presentation clock + +Previously eligible 30 fps square-pixel commands retain their former timing predicate over the shared presentation ledger, including its existing terminal-duration inference and single-picture one-tick default. The selected source-rate hint must be absent or exactly 30 fps, and normalized intervals and the ledger's final duration must still match 30 fps exactly. This preserves old native commands without applying their defaults to new rates or non-square aspects. + +For widened admission, the native path derives its rate from the presented packet clock, not from the external encoder path's snapped average or a nominal metadata rate. Require positive identical presentation intervals and the same final duration, then reduce the exact rational clock with checked arithmetic and validate it with `FrameRate::new`. A one-picture source needs an explicit positive duration. Missing final duration on a longer stream may use the preceding constant interval, but an explicitly zero or different final duration is not admitted by this new branch. + +This admits every supported rational cadence represented exactly by the source clock, including NTSC ratios. Nonuniform intervals, including rounded alternating timestamp steps, retain the existing path. A tolerance cannot distinguish those observable timestamps from genuine VFR. No floating-point admission or timestamp rewrite is added. The decoded-frame boundary rechecks the admitted exact interval. + +Only the native branch uses the derived rate. Other encoders retain their existing rate selection and snapping. MP4 sample timestamps and the final duration continue to come from the existing presentation ledger. + +The exact source-clock interval must also fit the positive 32-bit MP4 sample-duration field. A reducible rate does not make an oversized stored interval representable. Such inputs retain the existing path rather than rewriting the source clock or replacing the final duration with a guessed tick. + +## Declared sample aspect + +Validate the original source ratio before the shared aspect helper can narrow its terms. Reduce positive ratios exactly and use `SampleAspectRatio::new` to enforce the library's represented bounds. Absent, zero-term and square ratios retain the default, preserving previously eligible native output. Shared normalization already treats declarations such as `(0, 1)` or `(1, 0)` as unstated. Unsupported ratios or an unrepresentable MP4 display size cause native ineligibility and the unchanged fallback. + +The reduced declared ratio feeds the encoder settings and the existing MP4 `pasp` and display-size paths, so container and VUI signalling agree. The encoder library and muxer APIs do not change. + +## Verification + +- Unit cases cover integer, NTSC and unusual rational clocks, nonzero origins, one-picture durations, nonuniform or duplicate timestamps, incorrect final durations and overflow. +- Actual-base native comparisons retain single-picture zero duration and multi-picture zero terminal duration at 30 fps. Non-30 and non-square controls cannot inherit those defaults; positive-duration non-square single-picture input exercises widened admission. +- Generated MP4 cases use pinned FFmpeg 9.0.2 to require exact reported `r_frame_rate`, `avg_frame_rate`, SAR and DAR, with independent SPS and `pasp` checks. +- Fresh same-head reconstruction verifies native pictures. Complete output bytes remain identical across thread choices at fractional rates and non-square aspects. +- VFR, rounded timestamp cadence and unsupported aspects match the flag-off fallback. Previously eligible absent, unspecified and square aspects retain their native bytes. Default feature graphs and unflagged output comparisons remain unchanged. diff --git a/docs/design/cli-native-h264.md b/docs/design/cli-native-h264.md index 68e175e48..5e9685c18 100644 --- a/docs/design/cli-native-h264.md +++ b/docs/design/cli-native-h264.md @@ -12,7 +12,7 @@ The existing argument parser and transcode planner remain authoritative. Native Verification narrowed the initial MP4 and Matroska design to MP4. The existing Matroska cadence calculation omits `DefaultDuration` when rounded nanosecond intervals differ. Pinned ffprobe then reports 60 fps for the native stream instead of 30. Matroska therefore keeps the existing external path; correcting its shared cadence calculation is separate work. -Decoded pictures must be progressive planar 8-bit 4:2:0, with even dimensions from 16 through 1920 horizontally and 16 through 1080 vertically. The source must also fit those dimensions. CLI admission remains exactly 30/1 with square pixels, so other clocks, variable frame rates, and non-square pixels keep the external path. The adapter uses the library's default 30/1 timing and absent sample-aspect signalling. Unsupported color descriptions also keep the external path. The adapter maps absent color code points to unspecified, without converting samples. +Decoded pictures must be progressive planar 8-bit 4:2:0, with even dimensions from 16 through 1920 horizontally and 16 through 1080 vertically. The source must also fit those dimensions. The [metadata design](cli-native-h264-metadata.md) admits supported positive constant rational frame rates from the exact presentation clock and retains declared non-square sample aspects in both VUI and MP4. Variable or rounded alternating clocks and unrepresentable ratios keep the external path. Absent, unspecified and square aspects preserve the former native signalling. Unsupported color descriptions also keep the external path. The adapter maps absent color code points to unspecified, without converting samples. Native output uses Constrained Baseline, enabled deblocking, and an IDR interval of 250. The native encoder receives the requested thread count, but its output cannot depend on that count. Existing decoding policy, staging, spool limits, timestamps, audio handling, and muxing are reused. Strict-safe selection retains today's path and policy rather than changing decoder or process authorization. @@ -28,4 +28,4 @@ The native path checks its deadline before and after each bounded picture. It do - Compare complete native MP4 bytes across thread counts, retain exact external Matroska output, and run the existing CLI limit and timeout tests. - Check default and release feature graphs, generated command documentation, public text, DCO, CI, and exact-head similarity. -Speed and quality comparisons are outside this change. Broader CLI frame-rate and sample-aspect admission remains separate work even when the library supports it. +Speed and quality comparisons are outside this change. Matroska output admission remains separate work. diff --git a/docs/design/transcode.md b/docs/design/transcode.md index 6672547a8..bdd214ef9 100644 --- a/docs/design/transcode.md +++ b/docs/design/transcode.md @@ -1152,7 +1152,7 @@ that AAC source decoding is byte-identical to the oracle. ## Through `coreav ffmpeg` -The optional `native-h264-encoder` CLI feature and outer `--native-h264-encoder` flag prefer CoreAV's native H.264 encoder for eligible commands. The [native CLI design](cli-native-h264.md) defines its integer CRF and fixed-QP mapping, input limits, transactional retry, and unchanged default and release feature sets. The external encoder rules below remain the default and the fallback. +The optional `native-h264-encoder` CLI feature and outer `--native-h264-encoder` flag prefer CoreAV's native H.264 encoder for eligible commands. The [native CLI design](cli-native-h264.md) defines its integer CRF and fixed-QP mapping, exact constant rational clocks, declared sample aspects, input limits, transactional retry, and unchanged default and release feature sets. The external encoder rules below remain the default and the fallback. `coreav ffmpeg` ([`compat-cli.md`](compat-cli.md)) runs the common `ffmpeg` command lines of this job through this pipeline: one input, the diff --git a/scripts/native_h264_cli.py b/scripts/native_h264_cli.py index a6c75d6af..1b4df53a6 100644 --- a/scripts/native_h264_cli.py +++ b/scripts/native_h264_cli.py @@ -6,6 +6,7 @@ import os from pathlib import Path import re +import struct import subprocess import sys @@ -13,6 +14,7 @@ import h264_crf_conformance as crf import h264_p_conformance as syntax import test_oracle +import track_boxes def run(argv, *, env=None, success=True): @@ -34,6 +36,90 @@ def annexb_units(data): return [part.rstrip(b'\0') for part in parts] +def entry_aspect(data): + parent = (0, len(data)) + for kind in (b'moov', b'trak', b'mdia', b'minf', b'stbl', b'stsd', b'avc1'): + parent = track_boxes.child(data, parent, kind) + if parent is None: + raise ValueError('missing generated video sample entry') + if kind == b'stsd': + parent = (parent[0] + 8, parent[1]) + elif kind == b'avc1': + parent = (parent[0] + 78, parent[1]) + return track_boxes.child(data, parent, b'pasp') + + +def terminal_duration(data, ticks): + parents = [] + parent = (0, len(data)) + for kind in (b'moov', b'trak', b'mdia', b'minf', b'stbl'): + parent = track_boxes.child(data, parent, kind) + if parent is None: + raise ValueError('missing generated sample table parent') + start, end = parent + same(struct.unpack_from('>I', data, start - 8)[0], end - start + 8, 'generated 32-bit parent') + parents.append(parent) + same(parents[0][1], len(data), 'generated movie follows media') + table = track_boxes.child(data, parent, b'stts') + if table is None or table[1] - table[0] != 16: + raise ValueError('expected one generated duration run') + start, end = table + same(struct.unpack_from('>I', data, start - 8)[0], end - start + 8, 'generated 32-bit duration box') + version, entries, count, delta = struct.unpack_from('>IIII', data, start) + same((version, entries), (0, 1), 'generated duration header') + if not count or not delta: + raise ValueError('empty generated duration run') + changed = bytearray(data) + if count == 1: + struct.pack_into('>I', changed, start + 12, ticks) + else: + changed[start:end] = struct.pack('>IIIIII', 0, 2, count - 1, delta, 1, ticks) + struct.pack_into('>I', changed, start - 8, end - start + 16) + for parent_start, parent_end in parents: + struct.pack_into('>I', changed, parent_start - 8, parent_end - parent_start + 16) + return bytes(changed) + + +def sps_metadata(nal): + r = syntax.rbsp(nal) + same(r.u(24), 0x42c033, 'native SPS profile constraints and level') + same([r.ue() for _ in range(4)], [0, 0, 2, 1], 'native SPS fixed fields') + same(r.u(1), 0, 'frame number gaps') + r.ue() + r.ue() + same((r.u(1), r.u(1)), (1, 1), 'progressive SPS') + if r.u(1): + for _ in range(4): + r.ue() + same(r.u(1), 1, 'VUI presence') + aspect = None + if r.u(1): + code = r.u(8) + ratios = ((1, 1), (12, 11), (10, 11), (16, 11), (40, 33), (24, 11), + (20, 11), (32, 11), (80, 33), (18, 11), (15, 11), (64, 33), + (160, 99), (4, 3), (3, 2), (2, 1)) + if code == 255: + width, height = r.u(16), r.u(16) + if not width or not height: + raise ValueError('zero SPS sample aspect') + aspect = Fraction(width, height) + elif 1 <= code <= len(ratios): + aspect = Fraction(*ratios[code - 1]) + else: + raise ValueError('unsupported SPS aspect code') + same((r.u(1), r.u(1), r.u(3), r.u(1), r.u(1)), (0, 1, 5, 0, 1), 'video signal') + same([r.u(8) for _ in range(3)], [1, 1, 1], 'BT.709 colour') + same((r.u(1), r.ue(), r.ue()), (1, 0, 0), 'chroma location') + same(r.u(1), 1, 'VUI timing presence') + tick, scale = r.u(32), r.u(32) + same(r.u(1), 1, 'fixed frame rate') + same([r.u(1) for _ in range(4)], [0] * 4, 'HRD and restrictions') + r.finish() + if not tick or not scale: + raise ValueError('zero VUI clock') + return Fraction(scale, 2 * tick), aspect + + def verify(args): tools = test_oracle.require(args.ffmpeg, args.ffmpeg.with_name('ffprobe')) root = args.output.resolve() @@ -112,6 +198,92 @@ def source(name, width=18, height=34, rate='30', extra=(), frames=4, raw=None): syntax.inspect(repeated, 18, 34, quality, 4, True, 120, picture_qps=qps) rows.append(dict(mode=mode, quality=quality, container=container, frames=4)) + metadata_rows = [] + for index, (rate, sar, frames) in enumerate([ + ('24/1', '1/1', 4), ('25/1', '4/3', 4), ('24000/1001', '12/11', 4), + ('30000/1001', '16/15', 4), ('60000/1001', '17/13', 4), + ('25/2', '65535/65534', 4), ('1000/33', '40/33', 4), ('30/1', '4/3', 4), + ('30000/1001', '16/15', 1), + ('25/1', '17/13', 1), + ]): + rate, sar = Fraction(rate), Fraction(sar) + path = source(f'metadata-{index}', rate=str(rate), frames=frames, extra=('-vf', f'setsar={sar}:max=65535')) + decoded_source = root / f'metadata-source-{index}.yuv' + run([tools.ffmpeg, '-v', 'error', '-i', path, '-an', '-f', 'rawvideo', decoded_source]) + for mode, quality in [('crf', 23), ('qp', 28)]: + prefix = root / f'metadata-{index}-{mode}' + reference = prefix.with_suffix('.reference.h264') + reconstruction = prefix.with_suffix('.reference.yuv') + run([args.driver, 18, 34, f'crf:{quality}' if mode == 'crf' else quality, frames, + decoded_source, reference, reconstruction, prefix.with_suffix('.reference-own.yuv'), + 'true', 250, rate.numerator, rate.denominator, 0 if sar == 1 else sar.numerator, sar.denominator]) + identity = None + for threads in ('1', '2', '8', '0', 'auto'): + output = root / f'metadata-{index}-{mode}-{threads}.mp4' + run([args.candidate, '--native-h264-encoder', 'ffmpeg', '-i', path, '-an', + '-c:v', 'libx264', f'-{mode}', quality, '-threads', threads, output], env=native_only) + if identity is not None: + same(output.read_bytes(), identity, 'metadata thread-independent complete output') + continue + identity = output.read_bytes() + info = json.loads(run([tools.ffprobe, '-v', 'error', '-show_streams', '-show_packets', + '-of', 'json', output]).stdout) + stream, = info['streams'] + for field in ('r_frame_rate', 'avg_frame_rate'): + same(Fraction(stream[field]), rate, field) + same(Fraction(stream['sample_aspect_ratio'].replace(':', '/')), sar, 'declared SAR') + same(Fraction(stream['display_aspect_ratio'].replace(':', '/')), sar * Fraction(18, 34), 'DAR') + same([Fraction(p['pts']) * Fraction(stream['time_base']) for p in info['packets']], + [Fraction(i) / rate for i in range(frames)], 'exact presentation clock') + same(Fraction(info['packets'][-1]['duration']) * Fraction(stream['time_base']), 1 / rate, 'last duration') + pasp = entry_aspect(identity) + if pasp is None or pasp[1] - pasp[0] != 8: + raise ValueError('missing or malformed declared pasp') + same(Fraction(*struct.unpack_from('>II', identity, pasp[0])), sar, 'independent MP4 pasp') + elementary = output.with_suffix('.h264') + run([tools.ffmpeg, '-v', 'error', '-i', output, '-c:v', 'copy', '-an', + '-bsf:v', 'h264_mp4toannexb', '-f', 'h264', elementary]) + units = annexb_units(elementary.read_bytes()) + same(sps_metadata(next(n for n in units if n[0] & 31 == 7)), + (rate, None if sar == 1 else sar), 'independent SPS metadata') + reference_units = annexb_units(reference.read_bytes()) + same([n for n in units if n[0] & 31 in (7, 8)], + [n for n in reference_units if n[0] & 31 in (7, 8)][:2], 'fresh same-head parameter sets') + same([n for n in units if n[0] & 31 in (1, 5)], + [n for n in reference_units if n[0] & 31 in (1, 5)], 'fresh same-head coded pictures') + for executable, name in ((tools.ffmpeg, 'oracle'), (args.candidate, 'coreav')): + decoded = output.with_suffix(f'.{name}.yuv') + command = [executable, '-v', 'error', '-xerror', '-i', output, '-an', '-f', 'rawvideo', decoded] if name == 'oracle' else [executable, 'ffmpeg', '-i', output, '-an', '-c:v', 'rawvideo', '-pix_fmt', 'yuv420p', '-f', 'rawvideo', decoded] + run(command, env=native_only) + same(decoded.read_bytes(), reconstruction.read_bytes(), f'{name} metadata reconstruction') + metadata_rows.append(dict(rate=str(rate), sar=str(sar), mode=mode, frames=frames)) + + square = source('square-declaration', extra=('-vf', 'setsar=1')) + square_bytes = square.read_bytes() + pasp = entry_aspect(square_bytes) + if pasp is None: + raise ValueError('missing square source declaration') + legacy_sources = [('absent', source_path), ('square', square)] + unspecified = root / 'unspecified-aspect.mp4' + data = bytearray(square_bytes) + struct.pack_into('>II', data, pasp[0], 0, 1) + unspecified.write_bytes(data) + legacy_sources.append(('unspecified', unspecified)) + one_clock = source('legacy-one-clock', frames=1, extra=('-video_track_timescale', '30')) + one_zero = root / 'legacy-one-zero.mp4' + one_zero.write_bytes(terminal_duration(one_clock.read_bytes(), 0)) + final_zero = root / 'legacy-final-zero.mp4' + final_zero.write_bytes(terminal_duration(source_path.read_bytes(), 0)) + legacy_sources.extend([('one-zero-duration', one_zero), ('final-zero-duration', final_zero)]) + for label, path in legacy_sources: + previous = [] + for index, executable in enumerate((args.baseline_native, args.candidate)): + output = root / f'legacy-{label}-{index}.mp4' + run([executable, '--native-h264-encoder', 'ffmpeg', '-i', path, '-an', '-c:v', 'libx264', output], env=native_only) + previous.append(output.read_bytes()) + same(*previous, f'unchanged previously eligible {label} bytes') + print('native base comparisons: ' + ', '.join(label for label, _ in legacy_sources)) + flat = root / 'gop.yuv' flat.write_bytes(bytes([128]) * (384 * 251)) gop_source = source('gop', width=16, height=16, frames=251, raw=flat) @@ -155,8 +327,6 @@ def compare(source_path, extra, label, container='mp4'): ('wide', dict(width=1922, height=16, frames=1)), ('tall', dict(width=16, height=1082, frames=1)), ('4k', dict(width=3840, height=2160, frames=1)), - ('rate24', dict(rate='24')), ('rate2997', dict(rate='30000/1001')), - ('sar', dict(extra=('-vf', 'setsar=4/3'))), ('depth10', dict(extra=('-pix_fmt', 'yuv420p10le'))), ('chroma444', dict(extra=('-pix_fmt', 'yuv444p'))), ('hdr', dict(extra=('-x264-params', 'colorprim=bt709:transfer=smpte2084:colormatrix=bt709'))), @@ -166,6 +336,32 @@ def compare(source_path, extra, label, container='mp4'): variable = source('variable-rate', extra=('-vf', 'setpts=if(gte(N\\,2)\\,N+1\\,N)/30/TB', '-fps_mode', 'passthrough')) compare(variable, ('-fps_mode', 'passthrough'), 'variable-rate') + compare(variable, (), 'variable-native-clock') + rounded = source('rounded-clock', rate='30000/1001', extra=('-video_track_timescale', '1000')) + compare(rounded, (), 'rounded-clock') + for label, parameters in [ + ('zero-duration-new-rate', dict(rate='25', frames=1, extra=('-video_track_timescale', '25'))), + ('zero-duration-new-aspect', dict(frames=1, extra=('-video_track_timescale', '30', '-vf', 'setsar=4/3'))), + ]: + original = source(label + '-source', **parameters) + path = root / f'{label}.mp4' + path.write_bytes(terminal_duration(original.read_bytes(), 0)) + output = root / f'{label}-native-only.mp4' + rejected = run([args.candidate, '--native-h264-encoder', 'ffmpeg', '-i', path, + '-an', '-c:v', 'libx264', output], env=native_only, success=False) + same(rejected.returncode, 4, 'new metadata cannot inherit the legacy zero-duration default') + if output.exists() or b'no encoder program' not in rejected.stderr: + raise ValueError('zero-duration control failed outside the existing encoder fallback') + compare(path, (), label) + last_unequal = root / 'unequal-final-duration.mp4' + last_unequal.write_bytes(terminal_duration(source_path.read_bytes(), 1024)) + compare(last_unequal, (), 'unequal-final-duration') + for label, sar in [('unsupported-aspect', (65536, 65535)), ('display-overflow', (65535, 1))]: + data = bytearray(square_bytes) + struct.pack_into('>II', data, pasp[0], *sar) + path = root / f'{label}.mp4' + path.write_bytes(data) + compare(path, (), label) state = 17 dense = bytearray() @@ -214,17 +410,17 @@ def compare(source_path, extra, label, container='mp4'): run([args.default_candidate, 'ffmpeg', '-i', source_path, *command, output], env=environment) same(output.read_bytes(), outputs[0], 'default-build bytes against base') - (root / 'summary.json').write_text(json.dumps(dict(native=rows, gop_frames=251, fallback_cases=fallback_cases, + (root / 'summary.json').write_text(json.dumps(dict(native=rows, metadata=metadata_rows, legacy_native_cases=len(legacy_sources), legacy_native_shapes=[label for label, _ in legacy_sources], gop_frames=251, fallback_cases=fallback_cases, default_bytes_unchanged=True), indent=2) + '\n') - print(f'native CLI: {len(rows)} conformance cases, thread identity, fallback and default byte checks passed') + print(f'native CLI: {len(rows)} conformance cases, {len(metadata_rows)} exact rate/SAR cases, thread identity, fallback and default byte checks passed') def main(): parser = argparse.ArgumentParser(description=__doc__) - for name in ('candidate', 'default-candidate', 'baseline-default', 'baseline-external', 'driver', 'ffmpeg', 'output'): + for name in ('candidate', 'default-candidate', 'baseline-default', 'baseline-external', 'baseline-native', 'driver', 'ffmpeg', 'output'): parser.add_argument('--' + name, type=Path, required=True) args = parser.parse_args() - for name in ('candidate', 'default_candidate', 'baseline_default', 'baseline_external', 'driver', 'ffmpeg'): + for name in ('candidate', 'default_candidate', 'baseline_default', 'baseline_external', 'baseline_native', 'driver', 'ffmpeg'): setattr(args, name, getattr(args, name).resolve()) verify(args) diff --git a/scripts/tests/test_native_h264_cli.py b/scripts/tests/test_native_h264_cli.py index fd8c5a300..a72d18a74 100644 --- a/scripts/tests/test_native_h264_cli.py +++ b/scripts/tests/test_native_h264_cli.py @@ -1,4 +1,6 @@ import pathlib +from fractions import Fraction +import struct import sys import unittest @@ -8,6 +10,56 @@ class NativeCli(unittest.TestCase): + def test_terminal_duration_changes_only_the_last_generated_sample(self): + def box(kind, body): + return struct.pack('>I4s', len(body) + 8, kind) + body + + def movie(payload): + for kind in (b'stbl', b'minf', b'mdia', b'trak', b'moov'): + payload = box(kind, payload) + return box(b'mdat', b'unchanged encoded pictures') + payload + + for count in (1, 4): + original = movie(box(b'stts', struct.pack('>IIII', 0, 1, count, 512))) + for duration in (0, 1024): + pairs = (1, count, duration) if count == 1 else (2, count - 1, 512, 1, duration) + expected = movie(box(b'stts', struct.pack('>' + 'I' * (len(pairs) + 1), 0, *pairs))) + self.assertEqual(cli.terminal_duration(original, duration), expected) + + def test_explicit_sps_sample_aspect_rejects_zero_terms_with_a_label(self): + def sps(width, height): + bits = (f'{0x42c033:024b}' + '110110100111101111111111' + + f'{width:016b}{height:016b}' + '0110101' + + f'{0x010101:024b}' + '1111' + f'{1:032b}{60:032b}' + '100001') + bits += '0' * (-len(bits) % 8) + escaped = bytearray() + for value in int(bits, 2).to_bytes(len(bits) // 8, 'big'): + if escaped[-2:] == b'\0\0' and value <= 3: + escaped.append(3) + escaped.append(value) + return b'\x67' + escaped + + self.assertEqual(cli.sps_metadata(sps(17, 13)), (Fraction(30), Fraction(17, 13))) + for width, height in ((0, 1), (1, 0), (0, 0)): + with self.subTest(width=width, height=height): + with self.assertRaisesRegex(ValueError, 'zero SPS sample aspect'): + cli.sps_metadata(sps(width, height)) + + def test_sample_aspect_reader_uses_the_visual_entry_structure(self): + def box(kind, body): + return struct.pack('>I4s', len(body) + 8, kind) + body + + for ratio in (None, (16, 15), (65535, 65534)): + body = bytes(78) + (box(b'pasp', struct.pack('>II', *ratio)) if ratio else b'') + body = box(b'stsd', bytes(4) + struct.pack('>I', 1) + box(b'avc1', body)) + for kind in (b'stbl', b'minf', b'mdia', b'trak', b'moov'): + body = box(kind, body) + data = box(b'mdat', box(b'pasp', struct.pack('>II', 7, 9))) + body + actual = cli.entry_aspect(data) + self.assertEqual(struct.unpack_from('>II', data, actual[0]) if actual else None, ratio) + with self.assertRaisesRegex(ValueError, 'missing generated video sample entry'): + cli.entry_aspect(box(b'mdat', box(b'pasp', struct.pack('>II', 7, 9)))) + def test_oracle_annex_b_accepts_both_standard_delimiter_lengths(self): self.assertEqual(cli.annexb_units(b'\0\0\0\1\x67\x80\0\0\1\x68\x80\0\0\0\1\x65\x80'), [b'\x67\x80', b'\x68\x80', b'\x65\x80']) @@ -31,9 +83,15 @@ def test_workflow_builds_actual_base_and_candidate_modes(self): '--test docs_site --test strict_safe_limit --test ffprobe_limits', "for features in '' release-default release-full"): self.assertIn(required, job) + base_step = job.split('- name: Preserve base-build byte references', 1)[1].split('- name:', 1)[0] + self.assertIn('cd ../baseline', base_step) + self.assertIn('cargo build --locked --release -p coreav-cli --features native-h264-encoder,native-h264\n' + ' cp target/release/coreav "$RUNNER_TEMP/native-base"', base_step) + self.assertIn('--baseline-native "$RUNNER_TEMP/native-base"', job) for event in ('push:', 'pull_request:'): selected = workflow.split(' ' + event, 1)[1].split(' workflow_dispatch:', 1)[0] - for path in ('crates/coreav-cli/**', 'crates/coreav-transcode/**', 'scripts/native_h264_cli.py'): + for path in ('crates/coreav-cli/**', 'crates/coreav-transcode/**', 'scripts/native_h264_cli.py', + 'scripts/track_boxes.py'): self.assertIn(path, selected) def test_cli_oracles_install_nasm_before_x264(self):