Files
filmcraft/crates/dnx/tests/oracle_encode.rs
Brandon Thomas 226c139741 M9.6: DNxHD/DNxHR (SMPTE ST 2019-1 VC-3) decoder and DNxHR encoder
New L0 crate filmcraft-dnx, written from SMPTE ST 2019-1:2016 + Amd 1:2023
(tables extracted from the standard's text by tools/extract_tables.py).

Decoder: every compression ID (DNxHD 1235-1260 incl. field-coded and
macroblock-adaptive interlace, thin rasters, RGB 4:4:4; DNxHR 1270-1274
LB/SQ/HQ/HQX/444), 8/10/12-bit, 4:2:0/4:2:2/4:4:4, DCT and lossless alpha,
macroblock rows in parallel. Matches ffmpeg within IDCT precision (max 2 LSB,
<=50 per million beyond 1, 87-99% identical) on 22 ffmpeg fixtures; follows
ffmpeg where it departs from the standard's text (reconstruction offset,
CID 1271 weights, 4:4:4 signalling) - documented in the README.

Encoder: DNxHR LB/SQ/HQ/HQX(10/12-bit)/444, CBR at the eq. 7.1 frame sizes,
uniform-qsf binary search + per-macroblock refinement; ffmpeg decodes every
profile with -xerror (HQ luma PSNR 58.8-66.3 dB).

Wiring: MOV AVdn/AVdh import (codec label from the CID), export
Format::DnxHr (MOV AVdh, dnxProfile lb|sq|hq|hqx) in the export dialog and
file.exportMedia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 10:53:02 -04:00

77 lines
3.5 KiB
Rust

//! Encode DNxHR with our encoder, decode with ffmpeg (`-xerror`) and with our decoder.
mod common;
use common::*;
use filmcraft_dnx::{ChromaFormat, Encoder, EncoderConfig, Profile, decode_frame};
struct Case {
name: &'static str,
profile: Profile,
depth: u8,
lavfi: &'static str,
min_psnr: f64,
}
#[test]
fn encode_decodes_in_ffmpeg() {
let Some(ff) = ffmpeg() else { return };
let (w, h) = (1920u32, 1080u32);
let cases = [
Case { name: "enc_sq_mandel", profile: Profile::Sq, depth: 8, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 40.0 },
Case { name: "enc_hq_mandel", profile: Profile::Hq, depth: 8, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 45.0 },
Case { name: "enc_hq_testsrc", profile: Profile::Hq, depth: 8, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 },
Case { name: "enc_hqx10_mandel", profile: Profile::Hqx, depth: 10, lavfi: "mandelbrot=s=1920x1080:r=25", min_psnr: 45.0 },
Case { name: "enc_hqx12_testsrc", profile: Profile::Hqx, depth: 12, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 },
Case { name: "enc_lb_testsrc", profile: Profile::Lb, depth: 8, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 30.0 },
Case { name: "enc_444_testsrc", profile: Profile::R444, depth: 10, lavfi: "testsrc2=s=1920x1080:r=25", min_psnr: 45.0 },
];
for c in cases {
let mut cfg = EncoderConfig::new(c.profile, w, h);
cfg.bit_depth = c.depth;
let chroma = cfg.chroma();
let src = source_frames(&ff, c.name, c.lavfi, 2, w, h, chroma, c.depth);
let mut enc = Encoder::with_config(cfg).unwrap();
let t = std::time::Instant::now();
let coded: Vec<Vec<u8>> = src.iter().map(|f| enc.encode(f).unwrap()).collect();
let ms = t.elapsed().as_secs_f64() * 1000.0 / src.len() as f64;
for p in &coded {
assert_eq!(p.len() as u32, enc.frame_size(), "{}: CBR frame size", c.name);
}
let mov = fixture_dir().join(format!("{}.out.mov", c.name));
write_mov(&mov, b"AVdh", w as u16, h as u16, 24, &coded);
let pix = match (chroma, c.depth) {
(ChromaFormat::Yuv444, 10) => "yuv444p10le",
(_, 8) => "yuv422p",
(_, 10) => "yuv422p10le",
_ => "yuv422p12le",
};
// ffmpeg reads 4:4:4 Y'CbCr DNxHR as gbrp unless every macroblock is flagged
let ff_raw = ffmpeg_decode(&ff, &mov, pix);
let mut agree = Stats::default();
let mut psnr_min = f64::MAX;
let per = frame_samples(&src[0]);
assert_eq!(ff_raw.len(), per * coded.len(), "{}: ffmpeg frame count", c.name);
for (i, p) in coded.iter().enumerate() {
let f = decode_frame(p).unwrap();
let ours = planes(&f, pix);
agree.add(&ours, &ff_raw[i * per..(i + 1) * per]);
let peak = ((1u32 << c.depth) - 1) as f64;
let srcp = planes(&src[i], pix);
psnr_min = psnr_min.min(psnr(&srcp[..f.y.len()], &ours[..f.y.len()], peak));
}
println!(
"{:<18} {} {}-bit: {} bytes/frame, {:.1} ms/frame, Y PSNR {:.2} dB, vs ffmpeg max {} exact {:.2}%",
c.name,
c.profile.name(),
c.depth,
enc.frame_size(),
ms,
psnr_min,
agree.max,
agree.exact_pct()
);
assert!(agree.max <= 2, "{}: ffmpeg disagrees by {}", c.name, agree.max);
assert!(psnr_min >= c.min_psnr, "{}: PSNR {psnr_min:.2}", c.name);
}
}