diff --git a/crates/ec-dnxhd/src/tables.rs b/crates/ec-dnxhd/src/tables.rs index 4fa14b67..5e6a403d 100644 --- a/crates/ec-dnxhd/src/tables.rs +++ b/crates/ec-dnxhd/src/tables.rs @@ -2,17 +2,13 @@ pub(crate) const FLAG_INTERLACED: u16 = 1; pub(crate) const FLAG_MBAFF: u16 = 2; -#[allow(dead_code)] -pub(crate) const FLAG_444: u16 = 4; /// One compression id (CID): frame geometry and the code tables it uses. -/// `width`, `height` and `coding_unit_size` are 0 for the DNxHR ids (variable); +/// `width` and `coding_unit_size` are 0 for the DNxHR ids (variable); /// `bit_depth` is 0 when the frame header states it. pub(crate) struct CidEntry { pub cid: u32, pub width: u32, - #[allow(dead_code)] - pub height: u32, pub coding_unit_size: u32, pub flags: u16, pub bit_depth: u32, diff --git a/crates/ec-dnxhd/src/tables_gen.rs b/crates/ec-dnxhd/src/tables_gen.rs index 3f064cbe..bf035d4b 100644 --- a/crates/ec-dnxhd/src/tables_gen.rs +++ b/crates/ec-dnxhd/src/tables_gen.rs @@ -632,64 +632,64 @@ static T_1250_RUN: [u8; 62] = [ ]; pub(crate) static CID_TABLE: [CidEntry; 20] = [ - CidEntry { cid: 1235, width: 1920, height: 1080, coding_unit_size: 917504, flags: 0, bit_depth: 10, eob_index: 4, + CidEntry { cid: 1235, width: 1920, coding_unit_size: 917504, flags: 0, bit_depth: 10, eob_index: 4, luma_weight: &T_1235_LUMA_WEIGHT, chroma_weight: &T_1235_CHROMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1235_AC_CODES, ac_bits: &T_1235_AC_BITS, ac_info: &T_1235_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1235_RUN }, - CidEntry { cid: 1237, width: 1920, height: 1080, coding_unit_size: 606208, flags: 0, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1237, width: 1920, coding_unit_size: 606208, flags: 0, bit_depth: 8, eob_index: 3, luma_weight: &T_1237_LUMA_WEIGHT, chroma_weight: &T_1237_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1238, width: 1920, height: 1080, coding_unit_size: 917504, flags: 0, bit_depth: 8, eob_index: 4, + CidEntry { cid: 1238, width: 1920, coding_unit_size: 917504, flags: 0, bit_depth: 8, eob_index: 4, luma_weight: &T_1238_LUMA_WEIGHT, chroma_weight: &T_1238_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1238_AC_CODES, ac_bits: &T_1238_AC_BITS, ac_info: &T_1238_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1238_RUN }, - CidEntry { cid: 1241, width: 1920, height: 1080, coding_unit_size: 458752, flags: FLAG_INTERLACED, bit_depth: 10, eob_index: 4, + CidEntry { cid: 1241, width: 1920, coding_unit_size: 458752, flags: FLAG_INTERLACED, bit_depth: 10, eob_index: 4, luma_weight: &T_1241_LUMA_WEIGHT, chroma_weight: &T_1241_CHROMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1235_AC_CODES, ac_bits: &T_1235_AC_BITS, ac_info: &T_1235_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1235_RUN }, - CidEntry { cid: 1242, width: 1920, height: 1080, coding_unit_size: 303104, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1242, width: 1920, coding_unit_size: 303104, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 3, luma_weight: &T_1242_LUMA_WEIGHT, chroma_weight: &T_1242_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1243, width: 1920, height: 1080, coding_unit_size: 458752, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 4, + CidEntry { cid: 1243, width: 1920, coding_unit_size: 458752, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 4, luma_weight: &T_1243_LUMA_WEIGHT, chroma_weight: &T_1243_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1238_AC_CODES, ac_bits: &T_1238_AC_BITS, ac_info: &T_1238_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1238_RUN }, - CidEntry { cid: 1244, width: 1440, height: 1080, coding_unit_size: 303104, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1244, width: 1440, coding_unit_size: 303104, flags: FLAG_INTERLACED, bit_depth: 8, eob_index: 3, luma_weight: &T_1260_LUMA_WEIGHT, chroma_weight: &T_1260_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1250, width: 1280, height: 720, coding_unit_size: 458752, flags: 0, bit_depth: 10, eob_index: 4, + CidEntry { cid: 1250, width: 1280, coding_unit_size: 458752, flags: 0, bit_depth: 10, eob_index: 4, luma_weight: &T_1250_LUMA_WEIGHT, chroma_weight: &T_1250_CHROMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1250_AC_CODES, ac_bits: &T_1250_AC_BITS, ac_info: &T_1250_AC_INFO, run_codes: &T_1250_RUN_CODES, run_bits: &T_1250_RUN_BITS, run: &T_1250_RUN }, - CidEntry { cid: 1251, width: 1280, height: 720, coding_unit_size: 458752, flags: 0, bit_depth: 8, eob_index: 4, + CidEntry { cid: 1251, width: 1280, coding_unit_size: 458752, flags: 0, bit_depth: 8, eob_index: 4, luma_weight: &T_1251_LUMA_WEIGHT, chroma_weight: &T_1251_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1251_AC_CODES, ac_bits: &T_1251_AC_BITS, ac_info: &T_1251_AC_INFO, run_codes: &T_1250_RUN_CODES, run_bits: &T_1250_RUN_BITS, run: &T_1250_RUN }, - CidEntry { cid: 1252, width: 1280, height: 720, coding_unit_size: 303104, flags: 0, bit_depth: 8, eob_index: 5, + CidEntry { cid: 1252, width: 1280, coding_unit_size: 303104, flags: 0, bit_depth: 8, eob_index: 5, luma_weight: &T_1252_LUMA_WEIGHT, chroma_weight: &T_1252_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1252_AC_CODES, ac_bits: &T_1252_AC_BITS, ac_info: &T_1252_AC_INFO, run_codes: &T_1250_RUN_CODES, run_bits: &T_1250_RUN_BITS, run: &T_1250_RUN }, - CidEntry { cid: 1253, width: 1920, height: 1080, coding_unit_size: 188416, flags: 0, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1253, width: 1920, coding_unit_size: 188416, flags: 0, bit_depth: 8, eob_index: 3, luma_weight: &T_1237_LUMA_WEIGHT, chroma_weight: &T_1237_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1256, width: 1920, height: 1080, coding_unit_size: 1835008, flags: FLAG_444, bit_depth: 10, eob_index: 4, + CidEntry { cid: 1256, width: 1920, coding_unit_size: 1835008, flags: 0, bit_depth: 10, eob_index: 4, luma_weight: &T_1235_LUMA_WEIGHT, chroma_weight: &T_1235_LUMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1235_AC_CODES, ac_bits: &T_1235_AC_BITS, ac_info: &T_1235_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1235_RUN }, - CidEntry { cid: 1258, width: 960, height: 720, coding_unit_size: 212992, flags: 0, bit_depth: 8, eob_index: 5, + CidEntry { cid: 1258, width: 960, coding_unit_size: 212992, flags: 0, bit_depth: 8, eob_index: 5, luma_weight: &T_1252_LUMA_WEIGHT, chroma_weight: &T_1252_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1252_AC_CODES, ac_bits: &T_1252_AC_BITS, ac_info: &T_1252_AC_INFO, run_codes: &T_1250_RUN_CODES, run_bits: &T_1250_RUN_BITS, run: &T_1250_RUN }, - CidEntry { cid: 1259, width: 1440, height: 1080, coding_unit_size: 417792, flags: 0, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1259, width: 1440, coding_unit_size: 417792, flags: 0, bit_depth: 8, eob_index: 3, luma_weight: &T_1237_LUMA_WEIGHT, chroma_weight: &T_1237_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1260, width: 1440, height: 1080, coding_unit_size: 417792, flags: FLAG_INTERLACED | FLAG_MBAFF, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1260, width: 1440, coding_unit_size: 417792, flags: FLAG_INTERLACED | FLAG_MBAFF, bit_depth: 8, eob_index: 3, luma_weight: &T_1260_LUMA_WEIGHT, chroma_weight: &T_1260_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1270, width: 0, height: 0, coding_unit_size: 0, flags: FLAG_444, bit_depth: 0, eob_index: 4, + CidEntry { cid: 1270, width: 0, coding_unit_size: 0, flags: 0, bit_depth: 0, eob_index: 4, luma_weight: &T_1235_LUMA_WEIGHT, chroma_weight: &T_1235_LUMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1235_AC_CODES, ac_bits: &T_1235_AC_BITS, ac_info: &T_1235_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1235_RUN }, - CidEntry { cid: 1271, width: 0, height: 0, coding_unit_size: 0, flags: 0, bit_depth: 0, eob_index: 4, + CidEntry { cid: 1271, width: 0, coding_unit_size: 0, flags: 0, bit_depth: 0, eob_index: 4, luma_weight: &T_1241_LUMA_WEIGHT, chroma_weight: &T_1241_CHROMA_WEIGHT, dc_codes: &T_1235_DC_CODES, dc_bits: &T_1235_DC_BITS, ac_codes: &T_1235_AC_CODES, ac_bits: &T_1235_AC_BITS, ac_info: &T_1235_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1235_RUN }, - CidEntry { cid: 1272, width: 0, height: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 4, + CidEntry { cid: 1272, width: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 4, luma_weight: &T_1238_LUMA_WEIGHT, chroma_weight: &T_1238_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1238_AC_CODES, ac_bits: &T_1238_AC_BITS, ac_info: &T_1238_AC_INFO, run_codes: &T_1235_RUN_CODES, run_bits: &T_1235_RUN_BITS, run: &T_1238_RUN }, - CidEntry { cid: 1273, width: 0, height: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1273, width: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 3, luma_weight: &T_1237_LUMA_WEIGHT, chroma_weight: &T_1237_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, - CidEntry { cid: 1274, width: 0, height: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 3, + CidEntry { cid: 1274, width: 0, coding_unit_size: 0, flags: 0, bit_depth: 8, eob_index: 3, luma_weight: &T_1237_LUMA_WEIGHT, chroma_weight: &T_1237_CHROMA_WEIGHT, dc_codes: &T_1237_DC_CODES, dc_bits: &T_1237_DC_BITS, ac_codes: &T_1237_AC_CODES, ac_bits: &T_1237_AC_BITS, ac_info: &T_1237_AC_INFO, run_codes: &T_1237_RUN_CODES, run_bits: &T_1237_RUN_BITS, run: &T_1237_RUN }, ]; diff --git a/crates/ec-dnxhd/tests/ffmpeg_parity.rs b/crates/ec-dnxhd/tests/ffmpeg_parity.rs index 58379824..e848386e 100644 --- a/crates/ec-dnxhd/tests/ffmpeg_parity.rs +++ b/crates/ec-dnxhd/tests/ffmpeg_parity.rs @@ -7,6 +7,7 @@ mod common; use common::*; use ec_dnxhd::{Chroma, DnxhdDecoder, Picture}; use ec_core::registry::{CodecId, CodecParameters}; +use ec_core::error::Error; struct Case { tag: &'static str, @@ -384,3 +385,126 @@ fn matroska_carries_dnxhd_too() { assert_eq!(avi_frames(&avi), mov_frames(&mov)); let _ = std::fs::remove_dir_all(&dir); } + +fn decoder() -> DnxhdDecoder { + DnxhdDecoder::new(CodecParameters::new(CodecId::Dnxhd)).unwrap() +} + +fn encode_one(tag: &str, pix: &str, enc: &[&str]) -> Vec { + let dir = scratch(tag); + let out = dir.join("a.mov"); + let lavfi = format!("testsrc2=size=320x176:rate=25,format={pix}"); + let mut args = vec!["-f", "lavfi", "-i", &lavfi, "-frames:v", "1", "-c:v", "dnxhd"]; + args.extend_from_slice(enc); + args.push(out.to_str().unwrap()); + assert!(ffmpeg(&args)); + let f = mov_frames(&out).remove(0); + let _ = std::fs::remove_dir_all(&dir); + f +} + +fn refused(r: Result, what: &str) { + match r { + Err(Error::Unsupported { what: w, .. }) => { + assert!(w.contains(what), "refusal names {w:?}, wanted {what:?}") + } + other => panic!("expected an Unsupported({what}), got {:?}", other.map(|f| f.format)), + } +} + +#[test] +fn alpha_rgb_and_act_variants_are_refused_by_name() { + let sq = encode_one("ref-sq", "yuv422p", &["-profile:v", "dnxhr_sq"]); + assert!(decoder().decode_bytes(&sq).is_ok()); + + let mut alpha = sq.clone(); + alpha[7] |= 1; + refused(decoder().decode_bytes(&alpha), "alpha"); + + let mut act = sq.clone(); + act[0x2c] |= 1; + refused(decoder().decode_bytes(&act), "adaptive colour transform"); + + // GBR (RGB) 4:4:4: ffmpeg writes it with the ACT flag clear. + let rgb = encode_one("ref-rgb", "gbrp10le", &["-profile:v", "dnxhr_444"]); + refused(decoder().decode_bytes(&rgb), "RGB"); + // The same encoder's YCbCr 4:4:4 is accepted. + let yuv = encode_one("ref-444", "yuv444p10le", &["-profile:v", "dnxhr_444"]); + assert!(decoder().decode_bytes(&yuv).is_ok()); +} + +#[test] +fn unknown_cids_and_bad_headers_are_errors() { + let sq = encode_one("ref-bad", "yuv422p", &["-profile:v", "dnxhr_sq"]); + let mut bad = sq.clone(); + bad[0x28..0x2c].copy_from_slice(&1239u32.to_be_bytes()); + assert!(matches!(decoder().decode_bytes(&bad), Err(Error::Unsupported { .. }))); + let mut bad = sq.clone(); + bad[2] = 0x55; + assert!(decoder().decode_bytes(&bad).is_err()); + assert!(decoder().decode_bytes(&sq[..100]).is_err()); + assert!(decoder().decode_bytes(&[]).is_err()); + // Depth indicator that contradicts the CID. + let mut bad = sq.clone(); + bad[0x28..0x2c].copy_from_slice(&1235u32.to_be_bytes()); + assert!(decoder().decode_bytes(&bad).is_err()); +} + +#[test] +fn damaged_frames_never_panic() { + let hq = encode_one("ref-dmg", "yuv422p10le", &["-profile:v", "dnxhr_hqx"]); + let mut dec = decoder(); + let mut s = 0x9E37_79B9_7F4A_7C15u64; + let mut next = move || { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + s + }; + for round in 0..400 { + let mut f = hq.clone(); + match round % 4 { + 0 => { + for _ in 0..1 + next() % 8 { + let i = (next() as usize) % f.len(); + f[i] ^= 1 << (next() % 8); + } + } + 1 => { + // Headers only. + for _ in 0..1 + next() % 4 { + let i = (next() as usize) % 0x300.min(f.len()); + f[i] = next() as u8; + } + } + 2 => f.truncate((next() as usize) % f.len()), + _ => { + let i = (next() as usize) % f.len(); + for b in &mut f[i..] { + *b = next() as u8; + } + } + } + let _ = dec.decode_bytes(&f); + let _ = dec.decode_native(&f); + } + // The decoder is still usable afterwards. + assert!(dec.decode_bytes(&hq).is_ok()); +} + +#[test] +fn registry_decoder_path_returns_a_frame() { + use ec_core::registry::Decoder; + let hq = encode_one("ref-reg", "yuv422p", &["-profile:v", "dnxhr_hq"]); + let mut d = decoder(); + let pkt = ec_core::packet::Packet::new(0, ec_core::timebase::TimeBase::new(1, 25), hq); + d.send_packet(&pkt).unwrap(); + let frame = d.receive_frame().unwrap(); + assert!(matches!(frame, ec_core::frame::Frame::Video(_))); + assert!(d.receive_frame().unwrap_err().is_need_more()); + d.flush().unwrap(); + assert!(d.receive_frame().unwrap_err().is_eof()); + let params = d.codec_parameters(); + assert_eq!(params.video().unwrap().width, 320); + assert!(DnxhdDecoder::new(CodecParameters::new(CodecId::H264)).is_err()); +} diff --git a/crates/ec-dnxhd/tests/refusals.rs b/crates/ec-dnxhd/tests/refusals.rs deleted file mode 100644 index 78826dc3..00000000 --- a/crates/ec-dnxhd/tests/refusals.rs +++ /dev/null @@ -1,131 +0,0 @@ -#![allow(clippy::too_many_arguments, clippy::needless_range_loop, clippy::len_zero)] -//! Streams the decoder refuses by name, and damaged input that must never panic. - -mod common; -use common::*; -use ec_core::error::Error; -use ec_core::registry::{CodecId, CodecParameters}; -use ec_dnxhd::DnxhdDecoder; - -fn decoder() -> DnxhdDecoder { - DnxhdDecoder::new(CodecParameters::new(CodecId::Dnxhd)).unwrap() -} - -fn encode_one(tag: &str, pix: &str, enc: &[&str]) -> Vec { - let dir = scratch(tag); - let out = dir.join("a.mov"); - let lavfi = format!("testsrc2=size=320x176:rate=25,format={pix}"); - let mut args = vec!["-f", "lavfi", "-i", &lavfi, "-frames:v", "1", "-c:v", "dnxhd"]; - args.extend_from_slice(enc); - args.push(out.to_str().unwrap()); - assert!(ffmpeg(&args)); - let f = mov_frames(&out).remove(0); - let _ = std::fs::remove_dir_all(&dir); - f -} - -fn refused(r: Result, what: &str) { - match r { - Err(Error::Unsupported { what: w, .. }) => { - assert!(w.contains(what), "refusal names {w:?}, wanted {what:?}") - } - other => panic!("expected an Unsupported({what}), got {:?}", other.map(|f| f.format)), - } -} - -#[test] -fn alpha_rgb_and_act_variants_are_refused_by_name() { - let sq = encode_one("ref-sq", "yuv422p", &["-profile:v", "dnxhr_sq"]); - assert!(decoder().decode_bytes(&sq).is_ok()); - - let mut alpha = sq.clone(); - alpha[7] |= 1; - refused(decoder().decode_bytes(&alpha), "alpha"); - - let mut act = sq.clone(); - act[0x2c] |= 1; - refused(decoder().decode_bytes(&act), "adaptive colour transform"); - - // GBR (RGB) 4:4:4: ffmpeg writes it with the ACT flag clear. - let rgb = encode_one("ref-rgb", "gbrp10le", &["-profile:v", "dnxhr_444"]); - refused(decoder().decode_bytes(&rgb), "RGB"); - // The same encoder's YCbCr 4:4:4 is accepted. - let yuv = encode_one("ref-444", "yuv444p10le", &["-profile:v", "dnxhr_444"]); - assert!(decoder().decode_bytes(&yuv).is_ok()); -} - -#[test] -fn unknown_cids_and_bad_headers_are_errors() { - let sq = encode_one("ref-bad", "yuv422p", &["-profile:v", "dnxhr_sq"]); - let mut bad = sq.clone(); - bad[0x28..0x2c].copy_from_slice(&1239u32.to_be_bytes()); - assert!(matches!(decoder().decode_bytes(&bad), Err(Error::Unsupported { .. }))); - let mut bad = sq.clone(); - bad[2] = 0x55; - assert!(decoder().decode_bytes(&bad).is_err()); - assert!(decoder().decode_bytes(&sq[..100]).is_err()); - assert!(decoder().decode_bytes(&[]).is_err()); - // Depth indicator that contradicts the CID. - let mut bad = sq.clone(); - bad[0x28..0x2c].copy_from_slice(&1235u32.to_be_bytes()); - assert!(decoder().decode_bytes(&bad).is_err()); -} - -#[test] -fn damaged_frames_never_panic() { - let hq = encode_one("ref-dmg", "yuv422p10le", &["-profile:v", "dnxhr_hqx"]); - let mut dec = decoder(); - let mut s = 0x9E37_79B9_7F4A_7C15u64; - let mut next = move || { - s ^= s << 13; - s ^= s >> 7; - s ^= s << 17; - s - }; - for round in 0..400 { - let mut f = hq.clone(); - match round % 4 { - 0 => { - for _ in 0..1 + next() % 8 { - let i = (next() as usize) % f.len(); - f[i] ^= 1 << (next() % 8); - } - } - 1 => { - // Headers only. - for _ in 0..1 + next() % 4 { - let i = (next() as usize) % 0x300.min(f.len()); - f[i] = next() as u8; - } - } - 2 => f.truncate((next() as usize) % f.len()), - _ => { - let i = (next() as usize) % f.len(); - for b in &mut f[i..] { - *b = next() as u8; - } - } - } - let _ = dec.decode_bytes(&f); - let _ = dec.decode_native(&f); - } - // The decoder is still usable afterwards. - assert!(dec.decode_bytes(&hq).is_ok()); -} - -#[test] -fn registry_decoder_path_returns_a_frame() { - use ec_core::registry::Decoder; - let hq = encode_one("ref-reg", "yuv422p", &["-profile:v", "dnxhr_hq"]); - let mut d = decoder(); - let pkt = ec_core::packet::Packet::new(0, ec_core::timebase::TimeBase::new(1, 25), hq); - d.send_packet(&pkt).unwrap(); - let frame = d.receive_frame().unwrap(); - assert!(matches!(frame, ec_core::frame::Frame::Video(_))); - assert!(d.receive_frame().unwrap_err().is_need_more()); - d.flush().unwrap(); - assert!(d.receive_frame().unwrap_err().is_eof()); - let params = d.codec_parameters(); - assert_eq!(params.video().unwrap().width, 320); - assert!(DnxhdDecoder::new(CodecParameters::new(CodecId::H264)).is_err()); -} diff --git a/crates/ec-dv/src/lib.rs b/crates/ec-dv/src/lib.rs index ec68dde9..22323a1d 100644 --- a/crates/ec-dv/src/lib.rs +++ b/crates/ec-dv/src/lib.rs @@ -6,11 +6,11 @@ //! blocks, macroblocks and video segments, so planes are byte-identical to FFmpeg's native //! `yuv411p` / `yuv420p` / `yuv422p` output. //! -//! * [`DvVideoDecoder`] / [`DvPicture`] -- frame in, native planes out (4:1:1 stays 4:1:1). +//! * [`DvVideoDecoder`] / `DvPicture` -- frame in, native planes out (4:1:1 stays 4:1:1). //! * [`DvDecoder`] -- the `ec_core` [`Decoder`](ec_core::registry::Decoder). It widens 4:1:1 to //! `I422` by exact chroma duplication (ec-core has no `I411`); 4:2:0 -> `I420`; 4:2:2 -> `I422`. //! * [`extract_audio`] -- the PCM in the audio DIF blocks (16-bit linear and 12-bit non-linear, -//! both returned as 16-bit linear) as [`DvAudio`]. No audio `CodecId` is involved: audio leaves +//! both returned as 16-bit linear) as `DvAudio`. No audio `CodecId` is involved: audio leaves //! this crate as PCM, and [`DvDemuxer`] publishes it as `PcmS16Le` streams. //! * [`DvDemuxer`], [`is_dv`] -- raw `.dv` / `.dif` files. #![forbid(unsafe_code)] @@ -24,8 +24,8 @@ mod profile; mod tables; mod video; -pub use audio::{DvAudio, extract_audio}; -pub use decoder::{DvDecoder, output_format, to_video_frame}; -pub use demux::{DvDemuxer, dv_probe_score, is_dv}; -pub use profile::{Chroma, PROFILES, PROFILE_BYTES, Profile, frame_profile}; -pub use video::{DvPicture, DvVideoDecoder, is_dv_header}; +pub use audio::extract_audio; +pub use decoder::{DvDecoder, output_format}; +pub use demux::{DvDemuxer, is_dv}; +pub use profile::{Chroma, PROFILES, Profile, frame_profile}; +pub use video::DvVideoDecoder; diff --git a/crates/ec-dv/src/video.rs b/crates/ec-dv/src/video.rs index 662d2539..a908cdac 100644 --- a/crates/ec-dv/src/video.rs +++ b/crates/ec-dv/src/video.rs @@ -6,7 +6,7 @@ use ec_core::error::{Error, Result}; use ec_core::frame::Interlace; use crate::idct::{idct248_tile, idct_tile}; -use crate::profile::{Chroma, PROFILE_BYTES, Profile, WorkChunk, frame_profile, work_chunks}; +use crate::profile::{Chroma, Profile, WorkChunk, frame_profile, work_chunks}; use crate::tables::*; const DV_VIDEO_CONTROL: u8 = 0x61; @@ -669,11 +669,6 @@ fn last_row_field_luma(out: &mut Out, plane: usize, off: usize, stride: usize, b put_block_8x4(&blocks[3][32..], out, plane, off + 24 + stride, s2); } -/// `true` when `buf` starts a plausible DV frame header (DIF header block of a DV file). -pub fn is_dv_header(buf: &[u8]) -> bool { - buf.len() >= PROFILE_BYTES && (buf[0] == 0x1f) && buf[1] & 0x07 == 0x07 && buf[2] == 0 && (buf[3] & 0x7f) == 0x3f -} - #[cfg(test)] mod tests { use super::ZIGZAG; diff --git a/crates/ec-dv/tests/perf.rs b/crates/ec-dv/tests/perf.rs deleted file mode 100644 index 784488b0..00000000 --- a/crates/ec-dv/tests/perf.rs +++ /dev/null @@ -1,60 +0,0 @@ -//! Throughput floor: SD >= 300 fps, HD >= 60 fps (release builds only). -mod common; - -use std::time::Instant; - -use common::*; -use ec_dv::DvVideoDecoder; - -fn fps(f: &Flavour) -> Option { - let path = make(f, "perf", "dv", true, 10)?; - let data = std::fs::read(path).unwrap(); - let fs = ec_dv::frame_profile(None, &data).unwrap().frame_size; - let frames: Vec<&[u8]> = data.chunks_exact(fs).collect(); - let mut d = DvVideoDecoder::new(); - d.decode(frames[0]).unwrap(); - let start = Instant::now(); - let mut n = 0u32; - while start.elapsed().as_secs_f64() < 1.0 || n < 20 { - for fr in &frames { - std::hint::black_box(d.decode(fr).unwrap()); - n += 1; - } - } - Some(f64::from(n) / start.elapsed().as_secs_f64()) -} - -/// A shared machine under load cannot show the floor: the check is skipped (and says so) when the -/// 1-minute load average exceeds half the cores, which is how the lane servers look while other -/// jobs run. An idle machine enforces the floor. -fn machine_is_busy() -> bool { - let load = std::fs::read_to_string("/proc/loadavg") - .ok() - .and_then(|s| s.split_whitespace().next().and_then(|v| v.parse::().ok())); - let cores = std::thread::available_parallelism().map_or(1, |n| n.get()) as f64; - load.is_some_and(|l| l > cores * 0.5) -} - -#[test] -fn decode_speed() { - let mut bad = Vec::new(); - for f in FLAVOURS { - let hd = f.size.starts_with("1440") || f.size.starts_with("1280") || f.size.starts_with("960"); - let mut r = 0f64; - for _ in 0..3 { - let Some(x) = fps(f) else { panic!("fixture {}", f.name) }; - r = r.max(x); - if r >= if hd { 60.0 } else { 300.0 } { - break; - } - } - eprintln!("{}: {r:.0} fps", f.name); - if !cfg!(debug_assertions) && !machine_is_busy() { - let floor = if hd { 60.0 } else { 300.0 }; - if r < floor { - bad.push(format!("{}: {r:.0} fps < {floor}", f.name)); - } - } - } - assert!(bad.is_empty(), "{bad:?}"); -} diff --git a/crates/ec-dv/tests/robust.rs b/crates/ec-dv/tests/robust.rs deleted file mode 100644 index 8a57ce01..00000000 --- a/crates/ec-dv/tests/robust.rs +++ /dev/null @@ -1,91 +0,0 @@ -//! Damaged and hostile input: nothing may panic, and decoding is deterministic. -use ec_dv::{DvVideoDecoder, PROFILES, extract_audio}; - -struct Rng(u64); -impl Rng { - fn next(&mut self) -> u64 { - self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); - self.0 >> 33 - } -} - -fn frame_for(p: &ec_dv::Profile, rng: &mut Rng, header_bytes: bool) -> Vec { - let mut f: Vec = (0..p.frame_size).map(|_| rng.next() as u8).collect(); - if header_bytes { - f[3] = (f[3] & 0x7f) | (p.dsf << 7); - f[80 * 5 + 48 + 3] = p.video_stype; - f[4] &= 0xf8; - for b in 0..p.frame_size / 80 { - // plausible DIF headers keep the segment status nibbles equal, as real streams do - f[b * 80 + 3] = (f[b * 80 + 3] & 0x0f) | 0xf0; - } - f[3] = (f[3] & 0x7f) | (p.dsf << 7); - f[80 * 5 + 48 + 3] = p.video_stype; - } - f -} - -#[test] -fn random_frames_of_every_profile_do_not_panic() { - let mut rng = Rng(7); - for (i, p) in PROFILES.iter().enumerate() { - if i == 2 || i == 9 { - continue; // PAL 4:1:1 / IEC 61883-5 share their header bytes with other entries - } - for round in 0..6 { - let f = frame_for(p, &mut rng, round % 2 == 0); - let mut d = DvVideoDecoder::new(); - if round % 2 == 1 { - let _ = d.decode(&f); // random header: an error is fine, a panic is not - let _ = extract_audio(&f); - continue; - } - let a = d.decode(&f).unwrap_or_else(|e| panic!("profile {i} round {round}: {e:?}")); - let b = DvVideoDecoder::new().decode(&f).unwrap(); - assert_eq!(a.planes, b.planes, "decode must be deterministic"); - assert_eq!((a.width, a.height), (p.width, p.height)); - let _ = extract_audio(&f); - } - } -} - -#[test] -fn bit_flips_and_truncations_do_not_panic() { - let mut rng = Rng(99); - let p = &PROFILES[0]; - let base = frame_for(p, &mut rng, true); - let mut d = DvVideoDecoder::new(); - for n in 0..200 { - let mut f = base.clone(); - for _ in 0..(1 + n % 20) { - let at = rng.next() as usize % f.len(); - f[at] ^= 1 << (rng.next() % 8); - } - let _ = d.decode(&f); - let cut = rng.next() as usize % f.len(); - let _ = d.decode(&f[..cut]); - let _ = extract_audio(&f[..cut]); - let _ = extract_audio(&f); - } -} - -#[test] -fn all_headers_with_hostile_audio_packs() { - let mut rng = Rng(5); - for p in PROFILES.iter() { - for _ in 0..20 { - let mut f = frame_for(p, &mut rng, true); - for c in 0..10usize { - let offs = if c & 1 == 1 { 80 * 6 + 3 + c * 12000 } else { 80 * 6 + 80 * 16 * 3 + 3 + c * 12000 }; - if offs + 5 < f.len() { - f[offs] = 0x50; - f[offs + 1] = rng.next() as u8; - f[offs + 3] = rng.next() as u8; - f[offs + 4] = rng.next() as u8; - } - } - let _ = extract_audio(&f); - let _ = DvVideoDecoder::new().decode(&f); - } - } -} diff --git a/crates/ec-prores/src/idct.rs b/crates/ec-prores/src/idct.rs index 4890216e..4fa38a9b 100644 --- a/crates/ec-prores/src/idct.rs +++ b/crates/ec-prores/src/idct.rs @@ -13,8 +13,6 @@ use wide::{i16x8, i32x4, i32x8}; /// Scalar implementation in raster layout, a line-for-line port of ffmpeg's. pub mod reference { - #![allow(dead_code)] - pub struct K { /// `a` (even part) coefficient rows for r2, r4, r6. pub a2: [i32; 4], @@ -119,6 +117,20 @@ pub mod reference { out } + pub fn put_dc(dst: &mut [u8], stride: usize, dc: i16) { + let k = if B12 { &K12 } else { &K10 }; + let (lo, hi): (i16, i16) = if B12 { (4, 4091) } else { (4, 1019) }; + let t = (i32::from(dc).wrapping_add(1) >> 1) as i16; + let c0 = t.wrapping_add(8192) as i32; + let bias = (1i32 << (k.col_shift - 1)) / k.w4; + let v = ((k.w4.wrapping_mul(c0.wrapping_add(bias)) >> k.col_shift) as i16).clamp(lo, hi) as u16; + for r in 0..8 { + for c in dst[r * stride * 2..r * stride * 2 + 16].as_chunks_mut::<2>().0.iter_mut() { + *c = v.to_le_bytes(); + } + } + } + /// Run the IDCT on an already dequantised `block` and store the clipped 8x8 result at /// `dst[0]`, row pitch `stride`. With `dc_only` only `block[0]` is read (the block's other /// coefficients are zero), which is exact: the shortcut rows and columns of the full @@ -128,15 +140,7 @@ pub mod reference { let k = if B12 { &K12 } else { &K10 }; let (lo, hi): (i16, i16) = if B12 { (4, 4091) } else { (4, 1019) }; if dc_only { - let t = (i32::from(block[0]).wrapping_add(1) >> 1) as i16; - let c0 = t.wrapping_add(8192) as i32; - let bias = (1i32 << (k.col_shift - 1)) / k.w4; - let v = ((k.w4.wrapping_mul(c0.wrapping_add(bias)) >> k.col_shift) as i16).clamp(lo, hi) as u16; - for r in 0..8 { - for c in dst[r * stride * 2..r * stride * 2 + 16].as_chunks_mut::<2>().0.iter_mut() { - *c = v.to_le_bytes(); - } - } + put_dc::(dst, stride, block[0]); return; } for r in 0..8 { @@ -153,7 +157,7 @@ pub mod reference { } } -use reference::{K10, K12}; +use reference::K10; use archmage::{SimdToken, X64V1Token}; use reference::K; @@ -369,18 +373,8 @@ fn idct_put_10(dst: &mut [u8], stride: usize, block: &[i16; 64]) { #[inline(always)] pub fn idct_put(dst: &mut [u8], stride: usize, block: &mut [i16; 64]) { let dc_only = block[1..].iter().fold(0, |a, &x| a | x) == 0; - let k = if B12 { &K12 } else { &K10 }; - let (lo, hi): (i16, i16) = if B12 { (4, 4091) } else { (4, 1019) }; if dc_only { - let t = (i32::from(block[0]).wrapping_add(1) >> 1) as i16; - let c0 = t.wrapping_add(8192) as i32; - let bias = (1i32 << (k.col_shift - 1)) / k.w4; - let v = ((k.w4.wrapping_mul(c0.wrapping_add(bias)) >> k.col_shift) as i16).clamp(lo, hi) as u16; - for r in 0..8 { - for c in dst[r * stride * 2..r * stride * 2 + 16].as_chunks_mut::<2>().0.iter_mut() { - *c = v.to_le_bytes(); - } - } + reference::put_dc::(dst, stride, block[0]); return; } if B12 { diff --git a/crates/ec-prores/tests/perf.rs b/crates/ec-prores/tests/perf.rs deleted file mode 100644 index e6f6515b..00000000 --- a/crates/ec-prores/tests/perf.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! Throughput of 1080p 4:2:2 HQ (ffmpeg-encoded, light noise so the entropy coder has work). -//! Prints fps for 1 and 4 threads; set `EC_PRORES_MIN_FPS` to assert the single-thread rate. - -mod common; -use common::*; - -#[test] -fn hd_hq_throughput() { - let dir = scratch("perf"); - let mov = dir.join("hd.mov"); - assert!(run(&[ - "-f", "lavfi", "-i", "testsrc2=size=1920x1080:rate=25,noise=alls=12:allf=t", - "-frames:v", "24", "-c:v", "prores_ks", "-profile", "3", "-pix_fmt", "yuv422p10le", - mov.to_str().unwrap(), - ])); - let bytes = std::fs::read(&mov).unwrap(); - let pk = packets(&bytes); - let mut single = 0.0; - for threads in [1usize, 4] { - let mut dec = decoder(threads); - // Warm up, then best of 10 passes. - for p in &pk { - dec.decode(p).unwrap(); - } - let mut best = f64::MAX; - for _ in 0..10 { - let t0 = std::time::Instant::now(); - for p in &pk { - std::hint::black_box(dec.decode(p).unwrap()); - } - best = best.min(t0.elapsed().as_secs_f64()); - } - let fps = pk.len() as f64 / best; - eprintln!("1080p 422 HQ, {} bytes/frame, {threads} thread(s): {fps:.1} fps", bytes.len() / pk.len()); - if threads == 1 { - single = fps; - } - } - if let Some(min) = std::env::var("EC_PRORES_MIN_FPS").ok().and_then(|v| v.parse::().ok()) { - assert!(single >= min, "single-thread {single:.1} fps below {min}"); - } - let _ = std::fs::remove_dir_all(dir); -} diff --git a/crates/ec-prores/tests/robust.rs b/crates/ec-prores/tests/robust.rs deleted file mode 100644 index 09c91129..00000000 --- a/crates/ec-prores/tests/robust.rs +++ /dev/null @@ -1,135 +0,0 @@ -//! Robustness: truncated, corrupted and hostile frames must never panic, and refusals must name -//! what they refused. - -mod common; -use common::*; -use ec_core::Error; - -struct Rng(u64); -impl Rng { - fn next(&mut self) -> u64 { - self.0 ^= self.0 << 13; - self.0 ^= self.0 >> 7; - self.0 ^= self.0 << 17; - self.0 - } - fn below(&mut self, n: usize) -> usize { - (self.next() % n.max(1) as u64) as usize - } -} - -/// Small frames: progressive 422, interlaced 422, 4444 with alpha. -fn frames() -> Vec> { - static CACHE: std::sync::OnceLock>> = std::sync::OnceLock::new(); - CACHE.get_or_init(make_frames).clone() -} - -fn make_frames() -> Vec> { - let dir = scratch("robust"); - let mut out = Vec::new(); - for (i, (prof, fmt, extra)) in [ - ("3", "yuv422p10le", ""), - ("1", "yuv422p10le", "+ildct"), - ("4", "yuva444p10le", ""), - ] - .into_iter() - .enumerate() - { - let mov = dir.join(format!("r{i}.mov")); - let mut a = vec![ - "-f", "lavfi", "-i", "testsrc2=size=100x70:rate=25,noise=alls=20:allf=t", "-frames:v", "1", - "-c:v", "prores_ks", "-profile", prof, "-pix_fmt", fmt, - ]; - if !extra.is_empty() { - a.extend(["-flags", extra]); - } - a.push(mov.to_str().unwrap()); - assert!(run(&a)); - out.push(packets(&std::fs::read(&mov).unwrap()).remove(0)); - } - let _ = std::fs::remove_dir_all(dir); - out -} - -#[test] -fn truncations_never_panic() { - let fs = frames(); - for f in &fs { - let mut d = decoder(2); - assert!(d.decode(f).is_ok()); - for n in (0..f.len()).step_by(7).chain(0..40) { - let _ = d.decode(&f[..n.min(f.len())]); - } - } -} - -#[test] -fn corruptions_never_panic() { - let fs = frames(); - let mut rng = Rng(0x9E3779B97F4A7C15); - for f in &fs { - let mut d = decoder(3); - for round in 0..400 { - let mut g = f.clone(); - let flips = 1 + rng.below(8); - for _ in 0..flips { - // Half the hits land in the headers, where the structure lives. - let at = if round % 2 == 0 { rng.below(g.len().min(200)) } else { rng.below(g.len()) }; - g[at] ^= 1 << rng.below(8); - } - let _ = d.decode(&g); - } - } -} - -#[test] -fn garbage_is_refused_by_name() { - let mut d = decoder(1); - let mut rng = Rng(7); - for len in [0usize, 1, 7, 8, 27, 28, 200, 5000] { - let g: Vec = (0..len).map(|_| rng.next() as u8).collect(); - assert!(d.decode(&g).is_err()); - } - // Right magic, nonsense after it. - let mut g = vec![0u8; 300]; - g[..4].copy_from_slice(&300u32.to_be_bytes()); - g[4..8].copy_from_slice(b"icpf"); - match d.decode(&g) { - Err(Error::Unsupported { .. } | Error::Corrupt { .. }) => {} - other => panic!("expected a named refusal, got {:?}", other.map(|f| f.width)), - } -} - -#[test] -fn hostile_dimensions_are_bounded() { - let fs = frames(); - let mut g = fs[0].clone(); - // width/height at frame-header bytes 8..12 (after size+icpf: offset 8+8). - g[16..20].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); - let t = std::time::Instant::now(); - assert!(decoder(1).decode(&g).is_err()); - assert!(t.elapsed().as_secs() < 5); - g[16..20].copy_from_slice(&[0, 0, 0, 0]); - assert!(decoder(1).decode(&g).is_err()); -} - -#[test] -fn wrong_codec_is_unsupported() { - use ec_core::registry::{CodecId, CodecParameters}; - match ec_prores::ProResDecoder::new(CodecParameters::new(CodecId::H264)) { - Err(Error::Unsupported { .. }) => {} - _ => panic!("expected Unsupported"), - } -} - -/// `EC_SEED_DIR=crates/ec-prores/fuzz/corpus/decode_prores cargo test -p ec-prores --test robust -- --ignored dump_seeds` -#[test] -#[ignore] -fn dump_seeds() { - let dir = std::env::var("EC_SEED_DIR").expect("EC_SEED_DIR"); - std::fs::create_dir_all(&dir).unwrap(); - let fs = frames(); - for (i, f) in fs.iter().enumerate() { - std::fs::write(format!("{dir}/seed{i}.bin"), f).unwrap(); - } -}