FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
M9.15: MPEG-2 synthetic-stream oracle (field pictures, 16x8, dual prime, concealment), reference-decoder vector wrap and skip semantics
Brandon Thomasa218c22f8f9c
diff --git a/crates/mpeg2v/src/lib.rs b/crates/mpeg2v/src/lib.rsindex edf6c1520..7d3cc88f9 100644--- a/crates/mpeg2v/src/lib.rs+++ b/crates/mpeg2v/src/lib.rs@@ -407,6 +407,8 @@ pub struct Decoder {fwd: Option<Anchor>,bwd: Option<Anchor>,building: Option<Building>,+ /// Mid-gray stand-in for missing references.+ gray: Option<Arc<Frame>>,pending: Option<Pending>,out: Vec<Picture>,errors: usize,@@ -430,6 +432,7 @@ impl Decoder {fwd: None,bwd: None,building: None,+ gray: None,pending: None,out: Vec::new(),errors: 0,@@ -642,9 +645,19 @@ impl Decoder {};// a P second field may predict from the first field of its own framelet first_field = (is_second && ptype == PictureType::P).then(|| frame.clone());+ // A missing reference (decoding started at a P picture, or a broken link) predicts from+ // mid-gray.+ if self.gray.as_ref().is_none_or(|g| g.w != w || g.h != h || g.cw != cw) {+ let mut g = Frame::new(w, h, cw, ch);+ g.planes[0].fill(128);+ self.gray = Some(Arc::new(g));+ }+ let gray = self.gray.as_deref();+ let latest = self.bwd.as_ref().map(|a| &*a.frame);+ let older = self.fwd.as_ref().map(|a| &*a.frame);let (fwd, bwd) = match ptype {- PictureType::P => (self.bwd.as_ref().map(|a| &*a.frame), None),- PictureType::B => (self.fwd.as_ref().map(|a| &*a.frame), self.bwd.as_ref().map(|a| &*a.frame)),+ PictureType::P => (latest.or(gray), None),+ PictureType::B => (older.or(latest).or(gray), latest.or(gray)),_ => (None, None),};let p = PicParams {@@ -796,5 +809,7 @@ fn to_picture(f: &Frame, meta: &Meta) -> Picture {}}+#[cfg(test)]+mod synth_tests;#[cfg(test)]mod tests;diff --git a/crates/mpeg2v/src/slice.rs b/crates/mpeg2v/src/slice.rsindex e6c6f5549..5f3eb24eb 100644--- a/crates/mpeg2v/src/slice.rs+++ b/crates/mpeg2v/src/slice.rs@@ -8,7 +8,8 @@ use crate::mc::{Frame, predict};use crate::vlc::{DCT_EOB, DCT_ESCAPE, MB_BWD, MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT, MBA_ESCAPE, MBA_STUFFING, tables};/// Non-linear quantiser_scale (Table 7-6), indexed by quantiser_scale_code.-const NON_LINEAR_Q: [u8; 32] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 104, 112];+pub(crate) const NON_LINEAR_Q: [u8; 32] =+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80, 88, 96, 104, 112];/// Motion types (frame_motion_type / field_motion_type values).const MT_FIELD: u8 = 1;@@ -211,11 +212,12 @@ fn skipped_mb(p: &PicParams, st: &mut SliceState, addr: usize, buf: &mut MbBuf,if last.flags & MB_INTRA != 0 {return Err(SliceError("skipped macroblock after an intra macroblock"));}- // the previous macroblock's direction, one vector per direction (PMV[0][s])+ // the previous macroblock's direction, one vector per direction (PMV[0][s]); field+ // pictures predict from the field of the same parity (§7.6.6.4)let mut m = Motion { flags: last.flags & (MB_FWD | MB_BWD), mtype: if p.frame_picture() { MT_FRAME } else { MT_FIELD }, ..Default::default() };for s in 0..2 {m.mv[0][s] = st.pmv[0][s];- m.select[0][s] = last.select[0][s];+ m.select[0][s] = p.parity;}m}@@ -285,13 +287,7 @@ fn macroblock(p: &PicParams, st: &mut SliceState, b: &mut Bits, addr: usize, buflet delta = if f == 1 || code == 0 { code } else { (((code.abs() - 1) * f) + residual + 1) * code.signum() };let halve = p.mpeg2 && field_fmt && c == 1 && frame_pic;let pred = if halve { st.pmv[r][s][c] >> 1 } else { st.pmv[r][s][c] };- let (low, high, range) = (-16 * f, 16 * f - 1, 32 * f);- let mut v = pred + delta;- if v < low {- v += range;- } else if v > high {- v -= range;- }+ let v = wrap_vector(pred, delta, f);st.pmv[r][s][c] = if halve { v * 2 } else { v };m.mv[r][s][c] = v;}@@ -463,6 +459,23 @@ fn block(p: &PicParams, st: &mut SliceState, b: &mut Bits, cc: usize, intra: booOk(!ac && blk[1..].iter().all(|&v| v == 0))}+/// `prediction + delta`, brought back into [-16f, 16f-1] (§7.6.3.1). The range is restored on+/// the side the delta moves the vector, and a zero delta keeps the prediction, as the MPEG+/// Software Simulation Group reference decoder (ISO/IEC TR 13818-5) does. This only matters when+/// the prediction itself is out of range: a field vector's doubled vertical predictor before a+/// frame vector.+#[inline(always)]+pub(crate) fn wrap_vector(pred: i32, delta: i32, f: i32) -> i32 {+ let v = pred + delta;+ if delta > 0 && v > 16 * f - 1 {+ v - 32 * f+ } else if delta < 0 && v < -16 * f {+ v + 32 * f+ } else {+ v+ }+}+#[inline(always)]fn dequant(p: &PicParams, level: i32, qs: i32, w: i32, intra: bool) -> i32 {if p.mpeg2 {diff --git a/crates/mpeg2v/src/synth_tests.rs b/crates/mpeg2v/src/synth_tests.rsnew file mode 100644index 000000000..cc2758773--- /dev/null+++ b/crates/mpeg2v/src/synth_tests.rs@@ -0,0 +1,826 @@+//! Synthetic MPEG-2 streams for coding tools ffmpeg's encoder never writes: field pictures (the+//! second field predicted from the first), 16x8 motion compensation, dual-prime prediction in+//! field and frame pictures, concealment motion vectors in every picture type, skipped+//! macroblocks of field pictures, 4:2:2 field DCT... A random but valid stream is written here+//! (random macroblock types, vectors, field selects and coefficients) and decoded by both our+//! decoder and ffmpeg (the oracle); the frames must agree up to IDCT rounding.++use std::collections::HashMap;++use crate::tests::W;+use crate::vlc::*;+use crate::*;++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: u32) -> u32 {+ (self.next() % n.max(1) as u64) as u32+ }+ fn range(&mut self, lo: i32, hi: i32) -> i32 {+ lo + self.below((hi - lo + 1) as u32) as i32+ }+ fn chance(&mut self, pct: u32) -> bool {+ self.below(100) < pct+ }+}++fn code_of(table: &[(&'static str, i16)], v: i16) -> &'static str {+ table.iter().find(|e| e.1 == v).map(|e| e.0).unwrap_or_else(|| panic!("no code for {v}"))+}++#[derive(Clone, Copy)]+struct Cfg {+ name: &'static str,+ seed: u64,+ chroma: u8,+ w: usize,+ h: usize,+ /// Field pictures (else interlaced frame pictures with frame_pred_frame_dct = 0).+ field_pics: bool,+ /// The first field / top_field_first.+ tff: bool,+ b_frames: bool,+ dual: bool,+ alt_scan: bool,+ intra_vlc: bool,+ q_scale_type: bool,+ dc_prec: u8,+ concealment: bool,+ frames: usize,+}++#[derive(Clone, Copy, Default)]+struct M {+ flags: i16,+ mtype: u8,+ mv: [[[i32; 2]; 2]; 2],+ select: [[usize; 2]; 2],+ dmv: [i32; 2],+}++/// A prediction block: plane (0 luma, 1 chroma), field, position, half flags, size.+struct Req {+ chroma: bool,+ field: bool,+ x: i32,+ y: i32,+ hx: i32,+ hy: i32,+ w: i32,+ h: i32,+}++struct Enc {+ w: W,+ rng: Rng,+ cfg: Cfg,+ mbw: usize,+ mbh: usize,+ shy: u32,+ dct_codes: [HashMap<(u32, u32), &'static str>; 2],+ /// The active quantiser_scale.+ qs: i32,+}++/// Picture-level coding state.+struct Pic {+ ptype: PictureType,+ structure: u8,+ parity: usize,+ fpfd: bool,+ /// The P second field of the first frame: only the first field (opposite parity) exists.+ lone: bool,+}++struct St {+ pmv: [[[i32; 2]; 2]; 2],+ dc: [i32; 3],+ last: M,+}++const F_CODE: i32 = 2;++impl Enc {+ fn new(cfg: Cfg) -> Enc {+ let mut dct_codes = [HashMap::new(), HashMap::new()];+ for (k, t) in [dct_table(DCT_ZERO, DCT_ZERO_LONG), dct_table(DCT_ONE, &[])].into_iter().enumerate() {+ for (c, v) in t {+ if v >= 0 {+ dct_codes[k].insert(((v & 63) as u32, (v >> 6) as u32), c);+ }+ }+ }+ Enc { w: W::new(), rng: Rng(cfg.seed), cfg, mbw: cfg.w.div_ceil(16), mbh: 2 * cfg.h.div_ceil(32), shy: (cfg.chroma == 1) as u32, dct_codes, qs: 2 }+ }++ fn sequence(&mut self) {+ let c = self.cfg;+ let w = &mut self.w;+ w.start(0xB3);+ w.put(c.w as u32, 12);+ w.put(c.h as u32, 12);+ w.put(1, 4);+ w.put(3, 4);+ w.put(20000, 18);+ w.put(1, 1);+ w.put(112, 10);+ w.put(0, 1);+ w.put(0, 1);+ w.put(0, 1);+ w.start(0xB5);+ w.put(1, 4);+ w.put(if c.chroma == 2 { 0x85 } else { 0x48 }, 8);+ w.put(0, 1); // interlaced sequence+ w.put(c.chroma as u32, 2);+ w.put(0, 2);+ w.put(0, 2);+ w.put(0, 12);+ w.put(1, 1);+ w.put(0, 8);+ w.put(0, 1);+ w.put(0, 2);+ w.put(0, 5);+ w.start(0xB8);+ w.put(0, 12);+ w.put(1, 1); // marker_bit+ w.put(0, 12);+ w.put(1, 1); // closed GOP+ w.put(0, 1);+ }++ fn picture(&mut self, tref: u16, ptype: PictureType, structure: u8) {+ let c = self.cfg;+ let w = &mut self.w;+ w.start(0x00);+ w.put(tref as u32, 10);+ w.put(+ match ptype {+ PictureType::I => 1,+ PictureType::P => 2,+ _ => 3,+ },+ 3,+ );+ w.put(0xFFFF, 16);+ if ptype != PictureType::I {+ w.put(0, 1);+ w.put(7, 3);+ }+ if ptype == PictureType::B {+ w.put(0, 1);+ w.put(7, 3);+ }+ w.put(0, 1);+ w.start(0xB5);+ w.put(8, 4);+ let fwd = if ptype == PictureType::I && !c.concealment { 15 } else { F_CODE as u32 };+ let bwd = if ptype == PictureType::B { F_CODE as u32 } else { 15 };+ for v in [fwd, fwd, bwd, bwd] {+ w.put(v, 4);+ }+ w.put(c.dc_prec as u32, 2);+ w.put(structure as u32, 2);+ w.put((structure == 3 && c.tff) as u32, 1);+ w.put(0, 1); // frame_pred_frame_dct+ w.put(c.concealment as u32, 1);+ w.put(c.q_scale_type as u32, 1);+ w.put(c.intra_vlc as u32, 1);+ w.put(c.alt_scan as u32, 1);+ w.put(0, 1); // repeat_first_field+ w.put(0, 1); // chroma_420_type+ w.put(0, 1); // progressive_frame+ w.put(0, 1);+ let lone = tref == 0 && ptype == PictureType::P;+ let pic = Pic { ptype, structure, parity: (structure == 2) as usize, fpfd: false, lone };+ let rows = if structure == 3 { self.mbh } else { self.mbh / 2 };+ for r in 0..rows {+ self.slice(&pic, r);+ }+ }++ fn set_q(&mut self, code: i32) {+ self.qs = if self.cfg.q_scale_type { crate::slice::NON_LINEAR_Q[code as usize] as i32 } else { 2 * code };+ }++ fn dc_reset(&self) -> i32 {+ 1 << (7 + self.cfg.dc_prec)+ }++ fn slice(&mut self, p: &Pic, row: usize) {+ self.w.start(1 + row as u8);+ let q = self.rng.range(1, 31);+ self.set_q(q);+ self.w.put(q as u32, 5);+ self.w.put(0, 1);+ let mut st = St { pmv: [[[0; 2]; 2]; 2], dc: [self.dc_reset(); 3], last: M::default() };+ let mut skipped = 0;+ for mbx in 0..self.mbw {+ let can_skip = p.ptype != PictureType::I && !p.lone && mbx > 0 && mbx + 1 < self.mbw && self.rng.chance(15);+ if can_skip && self.try_skip(p, &mut st, mbx, row) {+ skipped += 1;+ continue;+ }+ self.address_increment(skipped + 1);+ skipped = 0;+ self.macroblock(p, &mut st, mbx, row);+ }+ }++ fn address_increment(&mut self, mut n: usize) {+ while n > 33 {+ self.w.code(code_of(MBA, MBA_ESCAPE));+ n -= 33;+ }+ self.w.code(code_of(MBA, n as i16));+ }++ /// Skip the macroblock when its implied prediction stays inside the picture.+ fn try_skip(&mut self, p: &Pic, st: &mut St, mbx: usize, mby: usize) -> bool {+ let mut m = M { mtype: if p.structure == 3 { 2 } else { 1 }, ..Default::default() };+ match p.ptype {+ PictureType::P => {+ m.flags = MB_FWD;+ m.select[0][0] = p.parity;+ }+ _ => {+ if st.last.flags & MB_INTRA != 0 || st.last.flags & (MB_FWD | MB_BWD) == 0 {+ return false;+ }+ m.flags = st.last.flags & (MB_FWD | MB_BWD);+ for s in 0..2 {+ m.mv[0][s] = st.pmv[0][s];+ m.select[0][s] = p.parity;+ }+ }+ }+ if !self.fits(p, &m, mbx, mby) {+ return false;+ }+ st.dc = [self.dc_reset(); 3];+ if p.ptype == PictureType::P {+ st.pmv = [[[0; 2]; 2]; 2];+ }+ true+ }++ /// Every prediction block of `m` lies inside its reference plane.+ fn fits(&self, p: &Pic, m: &M, mbx: usize, mby: usize) -> bool {+ let (lw, lh) = (self.mbw as i32 * 16, self.mbh as i32 * 16);+ let (cw, ch) = (lw / 2, lh >> self.shy);+ self.requests(p, m, mbx, mby).iter().all(|r| {+ let (pw, ph) = if r.chroma { (cw, ch) } else { (lw, lh) };+ let ph = if r.field { ph / 2 } else { ph };+ r.x >= 0 && r.y >= 0 && r.x + r.w + r.hx <= pw && r.y + r.h + r.hy <= ph+ })+ }++ /// The prediction blocks of `m` (mirrors the decoder's geometry).+ fn requests(&self, p: &Pic, m: &M, mbx: usize, mby: usize) -> Vec<Req> {+ let mut out = Vec::new();+ let cbh = 16 >> self.shy;+ let mut add = |field: bool, mv: [i32; 2], lx: i32, ly: i32, lh: i32, cy: i32, ch: i32| {+ out.push(Req { chroma: false, field, x: lx + (mv[0] >> 1), y: ly + (mv[1] >> 1), hx: mv[0] & 1, hy: mv[1] & 1, w: 16, h: lh });+ let cmv = [mv[0] / 2, if self.shy == 1 { mv[1] / 2 } else { mv[1] }];+ out.push(Req { chroma: true, field, x: lx / 2 + (cmv[0] >> 1), y: cy + (cmv[1] >> 1), hx: cmv[0] & 1, hy: cmv[1] & 1, w: 8, h: ch });+ };+ let x = (mbx * 16) as i32;+ for s in 0..2 {+ let dir = if s == 0 { MB_FWD } else { MB_BWD };+ if m.flags & dir == 0 {+ continue;+ }+ let mby = mby as i32;+ if p.structure == 3 {+ match m.mtype {+ 1 => {+ for r in 0..2 {+ add(true, m.mv[r][s], x, mby * 8, 8, mby * cbh / 2, cbh / 2);+ }+ }+ 3 => {+ for par in 0..2 {+ let (mm, e) = if par == 0 { (if self.cfg.tff { 1 } else { 3 }, -1) } else { (if self.cfg.tff { 3 } else { 1 }, 1) };+ add(true, m.mv[0][s], x, mby * 8, 8, mby * cbh / 2, cbh / 2);+ add(true, dual(m.mv[0][s], m.dmv, mm, e), x, mby * 8, 8, mby * cbh / 2, cbh / 2);+ }+ }+ _ => add(false, m.mv[0][s], x, mby * 16, 16, mby * cbh, cbh),+ }+ } else {+ match m.mtype {+ 2 => {+ for r in 0..2 {+ add(true, m.mv[r][s], x, mby * 16 + 8 * r as i32, 8, mby * cbh + r as i32 * cbh / 2, cbh / 2);+ }+ }+ 3 => {+ let e = if p.parity == 1 { 1 } else { -1 };+ add(true, m.mv[0][s], x, mby * 16, 16, mby * cbh, cbh);+ add(true, dual(m.mv[0][s], m.dmv, 1, e), x, mby * 16, 16, mby * cbh, cbh);+ }+ _ => add(true, m.mv[0][s], x, mby * 16, 16, mby * cbh, cbh),+ }+ }+ }+ out+ }++ fn macroblock(&mut self, p: &Pic, st: &mut St, mbx: usize, mby: usize) {+ let frame_pic = p.structure == 3;+ // macroblock_type+ let quant = self.rng.chance(25);+ let (table, mut flags): (&[(&str, i16)], i16) = match p.ptype {+ PictureType::I => (MBTYPE_I, MB_INTRA),+ PictureType::P => (+ MBTYPE_P,+ match self.rng.below(100) {+ 0..10 => MB_INTRA,+ 10..55 => MB_FWD | MB_PATTERN,+ 55..70 if !p.lone => MB_PATTERN,+ 55..70 => MB_FWD | MB_PATTERN,+ _ => MB_FWD,+ },+ ),+ _ => (+ MBTYPE_B,+ match self.rng.below(100) {+ 0..6 => MB_INTRA,+ 6..30 => MB_FWD | MB_BWD | MB_PATTERN,+ 30..45 => MB_FWD | MB_BWD,+ 45..60 => MB_FWD | MB_PATTERN,+ 60..70 => MB_FWD,+ 70..85 => MB_BWD | MB_PATTERN,+ _ => MB_BWD,+ },+ ),+ };+ if quant && (flags & (MB_INTRA | MB_PATTERN) != 0) {+ flags |= MB_QUANT;+ }+ let intra = flags & MB_INTRA != 0;+ let concealment = intra && self.cfg.concealment;+ // motion description: pick vectors until the prediction fits+ let mut m = M { flags, ..Default::default() };+ let moving = flags & (MB_FWD | MB_BWD) != 0;+ let mut pmv = st.pmv;+ if moving || concealment {+ let mut ok = false;+ for _ in 0..40 {+ let mut cand = M { flags, ..Default::default() };+ cand.mtype = if concealment {+ if frame_pic { 2 } else { 1 }+ } else {+ let dual_ok = self.cfg.dual && p.ptype == PictureType::P && !p.lone;+ match self.rng.below(if dual_ok { 3 } else { 2 }) {+ 0 => 1,+ 1 => 2,+ _ => 3,+ }+ };+ let spread = if self.rng.chance(20) { 0 } else { 31 };++ for r in 0..2 {+ for s in 0..2 {+ cand.select[r][s] = self.rng.below(2) as usize;+ cand.mv[r][s] = [self.rng.range(-spread - 1, spread), self.rng.range(-spread - 1, spread)];+ }+ }+ cand.dmv = [self.rng.range(-1, 1), self.rng.range(-1, 1)];+ if cand.mtype == 3 {+ cand.select = [[p.parity; 2]; 2];+ }+ if p.lone {+ cand.select = [[1 - p.parity; 2]; 2];+ }+ if (concealment || self.fits(p, &cand, mbx, mby)) && representable(p, &cand, concealment, pmv) {+ m = cand;+ ok = true;+ break;+ }+ }+ if !ok && concealment {+ // a vector the (possibly out-of-range) predictor reaches unambiguously+ let mut c = M { flags, mtype: if frame_pic { 2 } else { 1 }, ..Default::default() };+ for t in 0..2 {+ let pred = pmv[0][0][t];+ c.mv[0][0][t] = if pred < -32 {+ pred + 32+ } else if pred > 31 {+ pred - 32+ } else {+ 0+ };+ }+ m = c;+ } else if !ok {+ // zero vectors from the same parity always fit; field vectors (halved+ // predictors) are always representable+ let sel = if p.lone { 1 - p.parity } else { p.parity };+ m = if frame_pic {+ M { flags, mtype: 1, select: [[0, 0], [1, 1]], ..Default::default() }+ } else {+ M { flags, mtype: 1, select: [[sel; 2]; 2], ..Default::default() }+ };+ assert!(representable(p, &m, concealment, pmv));+ }+ }+ self.w.code(code_of(table, flags));+ if moving {+ self.w.put(m.mtype as u32, 2);+ }+ let dct_type = frame_pic && !p.fpfd && (intra || flags & MB_PATTERN != 0);+ if dct_type {+ self.w.put(self.rng.below(2), 1);+ }+ if flags & MB_QUANT != 0 {+ let q = self.rng.range(1, 31);+ self.set_q(q);+ self.w.put(q as u32, 5);+ }+ // vectors (differentially against the predictors, as the decoder forms them)+ let (count, field_fmt, dualp) = match (frame_pic, m.mtype) {+ (true, 1) => (2, true, false),+ (_, 3) => (1, true, true),+ (true, _) => (1, false, false),+ (false, 2) => (2, true, false),+ (false, _) => (1, true, false),+ };+ for s in 0..2 {+ let present = if s == 0 { flags & MB_FWD != 0 || concealment } else { flags & MB_BWD != 0 };+ if !present {+ continue;+ }+ for r in 0..count {+ if count == 2 || (field_fmt && !dualp) {+ self.w.put(m.select[r][s] as u32, 1);+ }+ for c in 0..2 {+ let halve = field_fmt && c == 1 && frame_pic;+ let pred = if halve { pmv[r][s][c] >> 1 } else { pmv[r][s][c] };+ let v = m.mv[r][s][c];+ self.motion_code(v - pred);+ if dualp {+ self.w.code(code_of(DMV, m.dmv[c] as i16));+ }+ pmv[r][s][c] = if halve { v * 2 } else { v };+ }+ }+ if count == 1 {+ pmv[1][s] = pmv[0][s];+ }+ }+ if concealment {+ self.w.put(1, 1);+ }+ // predictor resets+ if intra {+ if !concealment {+ pmv = [[[0; 2]; 2]; 2];+ }+ } else {+ st.dc = [self.dc_reset(); 3];+ if p.ptype == PictureType::P && flags & MB_FWD == 0 {+ pmv = [[[0; 2]; 2]; 2];+ m.select[0][0] = p.parity;+ }+ }+ st.pmv = pmv;+ // coded_block_pattern and blocks+ let nblocks = if self.cfg.chroma == 2 { 8 } else { 6 };+ let cbp: u32 = if flags & MB_PATTERN != 0 {+ // the coded_block_pattern code for 0 is only for 4:2:2 and 4:4:4 (Table B.9)+ let v = self.rng.range(1, (1 << nblocks) - 1) as u32;+ self.w.code(code_of(CBP, (v >> (nblocks - 6)) as i16));+ if nblocks == 8 {+ self.w.put(v & 3, 2);+ }+ v+ } else if intra {+ (1 << nblocks) - 1+ } else {+ 0+ };+ for i in 0..nblocks {+ if cbp & (1 << (nblocks - 1 - i)) != 0 {+ let cc = if i < 4 { 0 } else { 1 + (i & 1) };+ self.block(st, cc, intra);+ }+ }+ st.last = m;+ }++ fn motion_code(&mut self, delta: i32) {+ let f = 1 << (F_CODE - 1);+ let range = 32 * f;+ let mut d = delta;+ if d < -16 * f {+ d += range;+ } else if d > 16 * f - 1 {+ d -= range;+ }+ if d == 0 {+ self.w.code(code_of(MOTION, 0));+ return;+ }+ let a = d.abs() - 1;+ let code = (a >> (F_CODE - 1)) + 1;+ self.w.code(code_of(MOTION, (code * d.signum()) as i16));+ self.w.put((a & (f - 1)) as u32, (F_CODE - 1) as u32);+ }++ fn block(&mut self, st: &mut St, cc: usize, intra: bool) {+ let tab = (intra && self.cfg.intra_vlc) as usize;+ let mut first = !intra;+ if intra {+ let max = (1 << (8 + self.cfg.dc_prec)) - 1;+ let target = (st.dc[cc] + self.rng.range(-40, 40)).clamp(0, max);+ let diff = target - st.dc[cc];+ st.dc[cc] = target;+ let size = 32 - diff.unsigned_abs().leading_zeros();+ self.w.code(code_of(if cc == 0 { DC_LUMA } else { DC_CHROMA }, size as i16));+ if size > 0 {+ let v = if diff > 0 { diff } else { diff + (1 << size) - 1 };+ self.w.put(v as u32, size);+ }+ }+ let n = if intra { self.rng.below(6) } else { 1 + self.rng.below(6) };+ let mut idx: i32 = if intra { 0 } else { -1 };+ for _ in 0..n {+ let run = self.rng.range(0, 6);+ if idx + run + 1 > 63 {+ break;+ }+ idx += run + 1;+ // keep dequantised values within ±300 (W ≤ 83 for the default intra matrix): realistic+ // residuals (extreme ones overflow ffmpeg's integer IDCT, which then differs)+ let cap = if intra { 300 * 16 / (self.qs * 83) } else { (300 * 32 / (self.qs * 16) - 1) / 2 }.clamp(1, 2047);+ let mag = if self.rng.chance(8) { self.rng.range(cap.min(20), cap.min(400)) } else { self.rng.range(1, cap.min(4)) };+ let neg = self.rng.chance(50);+ if first && run == 0 && mag == 1 {+ self.w.code("1");+ self.w.put(neg as u32, 1);+ } else if let Some(c) = self.dct_codes[tab].get(&(run as u32, mag as u32)).filter(|_| !self.rng.chance(5)) {+ self.w.code(c);+ self.w.put(neg as u32, 1);+ } else {+ self.w.code("0000 01");+ self.w.put(run as u32, 6);+ let l = if neg { -mag } else { mag };+ self.w.put((l & 0xFFF) as u32, 12);+ }+ first = false;+ }+ if first {+ // a coded non-intra block needs a coefficient+ self.w.code("1");+ self.w.put(0, 1);+ }+ self.w.code(if tab == 1 { "0110" } else { "10" });+ }+}++/// Whether every vector of `m` decodes to itself under both the literal §7.6.3.1 wrap and the+/// reference decoder's (they differ only for out-of-range predictors).+fn representable(p: &Pic, m: &M, concealment: bool, mut pmv: [[[i32; 2]; 2]; 2]) -> bool {+ let frame_pic = p.structure == 3;+ let f = 1 << (F_CODE - 1);+ let (count, field_fmt, _) = match (frame_pic, m.mtype) {+ (true, 1) => (2, true, false),+ (_, 3) => (1, true, true),+ (true, _) => (1, false, false),+ (false, 2) => (2, true, false),+ (false, _) => (1, true, false),+ };+ for s in 0..2 {+ let present = if s == 0 { m.flags & MB_FWD != 0 || concealment } else { m.flags & MB_BWD != 0 };+ if !present {+ continue;+ }+ for r in 0..count {+ for c in 0..2 {+ let halve = field_fmt && c == 1 && frame_pic;+ let pred = if halve { pmv[r][s][c] >> 1 } else { pmv[r][s][c] };+ let v = m.mv[r][s][c];+ let mut d = v - pred;+ if d < -16 * f {+ d += 32 * f;+ } else if d > 16 * f - 1 {+ d -= 32 * f;+ }+ let literal = {+ let x = pred + d;+ if x < -16 * f {+ x + 32 * f+ } else if x > 16 * f - 1 {+ x - 32 * f+ } else {+ x+ }+ };+ if crate::slice::wrap_vector(pred, d, f) != v || literal != v {+ return false;+ }+ pmv[r][s][c] = if halve { v * 2 } else { v };+ }+ }+ if count == 1 {+ pmv[1][s] = pmv[0][s];+ }+ }+ true+}++fn dual(v: [i32; 2], dmv: [i32; 2], m: i32, e: i32) -> [i32; 2] {+ let s = |x: i32| (x * m + (x > 0) as i32) >> 1;+ [s(v[0]) + dmv[0], s(v[1]) + e + dmv[1]]+}++/// Write a stream: closed GOP, I/P/B frame order I0 P3 B1 B2 P6 B4 B5 … (or I0 P1 P2 … without+/// B pictures); field-coded frames are a pair of field pictures (I + P for the first frame).+fn stream(cfg: Cfg) -> Vec<u8> {+ let mut e = Enc::new(cfg);+ e.sequence();+ let mut order: Vec<(u16, PictureType)> = vec![(0, PictureType::I)];+ if cfg.b_frames {+ let mut a = 0u16;+ while (a as usize) + 3 < cfg.frames {+ order.push((a + 3, PictureType::P));+ order.push((a + 1, PictureType::B));+ order.push((a + 2, PictureType::B));+ a += 3;+ }+ } else {+ for k in 1..cfg.frames as u16 {+ order.push((k, PictureType::P));+ }+ }+ let (first, second) = if cfg.tff { (1, 2) } else { (2, 1) };+ for (tref, t) in order {+ if cfg.field_pics {+ e.picture(tref, t, first);+ e.picture(tref, if t == PictureType::I { PictureType::P } else { t }, second);+ } else {+ e.picture(tref, t, 3);+ }+ }+ e.w.start(0xB7);+ e.w.out+}++fn check(cfg: Cfg) {+ let es = stream(cfg);+ let (pics, errors) = {+ let mut d = Decoder::new();+ let mut out = Vec::new();+ for (i, r) in access_units(&es).into_iter().enumerate() {+ out.extend(d.decode(&es[r], i as i64).unwrap());+ }+ out.extend(d.flush());+ (out, d.errors())+ };+ assert_eq!(errors, 0, "{}: our decoder reports slice errors", cfg.name);+ let Some(ff) = filmcraft_testkit::ffmpeg_or_skip(cfg.name) else { return };+ let path = filmcraft_testkit::fixtures_dir("mpeg2v").join(format!("{}_{:x}.m2v", cfg.name, cfg.seed));+ std::fs::write(&path, &es).unwrap();+ let fmt = if cfg.chroma == 2 { "yuv422p" } else { "yuv420p" };+ let o = std::process::Command::new(ff)+ .args(["-hide_banner", "-loglevel", "error", "-xerror", "-i"])+ .arg(&path)+ .args(["-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", fmt, "-"])+ .output()+ .unwrap();+ assert!(o.status.success(), "{}: ffmpeg rejects the stream: {}", cfg.name, String::from_utf8_lossy(&o.stderr));+ let raw = o.stdout;+ let (w, h) = (cfg.w, cfg.h);+ let (cw, ch) = (w / 2, if cfg.chroma == 2 { h } else { h / 2 });+ let fs = w * h + 2 * cw * ch;+ assert_eq!(raw.len() / fs, pics.len(), "{}: frame count", cfg.name);+ let mut worst = 0u8;+ let (mut sse, mut n, mut differ) = (0f64, 0u64, 0u64);+ // (frame, type, macroblock x, y, line) of the first luma difference above 4+ let mut first_bad = None;+ for (i, p) in pics.iter().enumerate() {+ assert_eq!(p.temporal_reference as usize, i, "{}: display order", cfg.name);+ assert_eq!(p.top_field_first, cfg.tff);+ let f = &raw[i * fs..(i + 1) * fs];+ if first_bad.is_none()+ && let Some(k) = (0..w * h).find(|&k| p.y[k].abs_diff(f[k]) > 4)+ {+ first_bad = Some((i, p.picture_type, k % w / 16, k / w / 16, k / w % 16));+ }+ for (a, b) in p.y.iter().chain(&p.cb).chain(&p.cr).zip(f) {+ let d = a.abs_diff(*b);+ worst = worst.max(d);+ sse += (d as f64).powi(2);+ differ += (d != 0) as u64;+ n += 1;+ }+ }+ let psnr = 10.0 * (255f64 * 255.0 * n as f64 / sse.max(1e-9)).log10();+ println!("{}: {} frames, max diff {worst}, {:.3}% differ, PSNR {psnr:.2} dB", cfg.name, pics.len(), 100.0 * differ as f64 / n as f64);+ // the same criteria as the ffmpeg-encoded fixtures (tests/oracle.rs)+ assert!(worst <= 4 && psnr >= 58.0, "{} (seed {:#x}): max diff {worst}, PSNR {psnr:.2}, first at {first_bad:?}", cfg.name, cfg.seed);+}++const BASE: Cfg = Cfg {+ name: "",+ seed: 1,+ chroma: 1,+ w: 96,+ h: 64,+ field_pics: true,+ tff: true,+ b_frames: true,+ dual: false,+ alt_scan: false,+ intra_vlc: true,+ q_scale_type: false,+ dc_prec: 0,+ concealment: true,+ frames: 7,+};++/// Every configuration with more random streams.+#[test]+fn seed_sweep() {+ let cfgs = [+ Cfg { name: "sweep_field_ibp", ..BASE },+ Cfg { name: "sweep_field_dual_422", b_frames: false, dual: true, chroma: 2, tff: false, frames: 4, ..BASE },+ Cfg { name: "sweep_frame_dual", field_pics: false, b_frames: false, dual: true, frames: 4, dc_prec: 3, ..BASE },+ Cfg { name: "sweep_frame_ib_422", field_pics: false, chroma: 2, tff: false, q_scale_type: true, ..BASE },+ ];+ for seed in 1..=8u64 {+ for c in cfgs {+ check(Cfg { seed: (seed * 0x9E37_79B9_7F4A_7C15) ^ c.w as u64, ..c });+ }+ }+}++#[test]+fn field_pictures_with_b_and_16x8() {+ check(Cfg { name: "synth_field_ibp_tff", seed: 0x1234_5678_9abc, ..BASE });+ check(Cfg {+ name: "synth_field_ibp_bff_422",+ seed: 0xfeed_beef_1234,+ tff: false,+ chroma: 2,+ alt_scan: true,+ q_scale_type: true,+ dc_prec: 2,+ w: 112,+ h: 96,+ ..BASE+ });+}++#[test]+fn dual_prime_in_field_pictures() {+ check(Cfg { name: "synth_field_dual_tff", seed: 0x0bad_cafe_0042, b_frames: false, dual: true, frames: 5, ..BASE });+ check(Cfg {+ name: "synth_field_dual_bff_422",+ seed: 0x1357_9bdf_2468,+ b_frames: false,+ dual: true,+ tff: false,+ chroma: 2,+ concealment: false,+ frames: 5,+ ..BASE+ });+}++#[test]+fn frame_pictures_field_mc_and_dual_prime() {+ check(Cfg { name: "synth_frame_dual_tff", seed: 0x5555_aaaa_1111, field_pics: false, b_frames: false, dual: true, frames: 6, ..BASE });+ check(Cfg {+ name: "synth_frame_dual_bff_422",+ seed: 0x7777_3333_9999,+ field_pics: false,+ b_frames: false,+ dual: true,+ tff: false,+ chroma: 2,+ dc_prec: 1,+ frames: 6,+ ..BASE+ });+ check(Cfg {+ name: "synth_frame_ib_422",+ seed: 0x2468_ace0_1357,+ field_pics: false,+ chroma: 2,+ alt_scan: true,+ intra_vlc: false,+ q_scale_type: true,+ w: 128,+ h: 96,+ ..BASE+ });+}diff --git a/crates/mpeg2v/src/tests.rs b/crates/mpeg2v/src/tests.rsindex af6736b48..ca5f2e5e5 100644--- a/crates/mpeg2v/src/tests.rs+++ b/crates/mpeg2v/src/tests.rs@@ -4,17 +4,17 @@use super::*;/// A minimal bit writer for building streams.-struct W {- out: Vec<u8>,+pub(crate) struct W {+ pub(crate) out: Vec<u8>,acc: u64,n: u32,}impl W {- fn new() -> W {+ pub(crate) fn new() -> W {W { out: Vec::new(), acc: 0, n: 0 }}- fn put(&mut self, v: u32, bits: u32) {+ pub(crate) fn put(&mut self, v: u32, bits: u32) {for i in (0..bits).rev() {self.acc = (self.acc << 1) | ((v >> i) & 1) as u64;self.n += 1;@@ -25,17 +25,17 @@ impl W {}}}- fn code(&mut self, s: &str) {+ pub(crate) fn code(&mut self, s: &str) {for c in s.chars().filter(|c| *c != ' ') {self.put((c == '1') as u32, 1);}}- fn align(&mut self) {+ pub(crate) fn align(&mut self) {while self.n != 0 {self.put(0, 1);}}- fn start(&mut self, code: u8) {+ pub(crate) fn start(&mut self, code: u8) {self.align();self.out.extend_from_slice(&[0, 0, 1, code]);}diff --git a/crates/mpeg2v/src/vlc.rs b/crates/mpeg2v/src/vlc.rsindex 00f9552b4..438b9a2bd 100644--- a/crates/mpeg2v/src/vlc.rs+++ b/crates/mpeg2v/src/vlc.rs@@ -112,7 +112,7 @@ impl Vlc {/// macroblock_address_increment (B.1): 1..=33, [`MBA_ESCAPE`], [`MBA_STUFFING`].pub(crate) const MBA_ESCAPE: i16 = 34;pub(crate) const MBA_STUFFING: i16 = 35;-const MBA: &[(&str, i16)] = &[+pub(crate) const MBA: &[(&str, i16)] = &[("1", 1),("011", 2),("010", 3),@@ -158,9 +158,9 @@ pub(crate) const MB_PATTERN: i16 = 8;pub(crate) const MB_INTRA: i16 = 16;/// B.2: I pictures.-const MBTYPE_I: &[(&str, i16)] = &[("1", MB_INTRA), ("01", MB_INTRA | MB_QUANT)];+pub(crate) const MBTYPE_I: &[(&str, i16)] = &[("1", MB_INTRA), ("01", MB_INTRA | MB_QUANT)];/// B.3: P pictures.-const MBTYPE_P: &[(&str, i16)] = &[+pub(crate) const MBTYPE_P: &[(&str, i16)] = &[("1", MB_FWD | MB_PATTERN),("01", MB_PATTERN),("001", MB_FWD),@@ -170,7 +170,7 @@ const MBTYPE_P: &[(&str, i16)] = &[("0000 01", MB_QUANT | MB_INTRA),];/// B.4: B pictures.-const MBTYPE_B: &[(&str, i16)] = &[+pub(crate) const MBTYPE_B: &[(&str, i16)] = &[("10", MB_FWD | MB_BWD),("11", MB_FWD | MB_BWD | MB_PATTERN),("010", MB_BWD),@@ -184,10 +184,10 @@ const MBTYPE_B: &[(&str, i16)] = &[("0000 01", MB_QUANT | MB_INTRA),];/// ISO/IEC 11172-2 D pictures (DC intra-coded).-const MBTYPE_D: &[(&str, i16)] = &[("1", MB_INTRA)];+pub(crate) const MBTYPE_D: &[(&str, i16)] = &[("1", MB_INTRA)];/// B.9: coded_block_pattern (0 only in ISO/IEC 13818-2).-const CBP: &[(&str, i16)] = &[+pub(crate) const CBP: &[(&str, i16)] = &[("111", 60),("1101", 4),("1100", 8),@@ -255,7 +255,7 @@ const CBP: &[(&str, i16)] = &[];/// B.10: motion_code (-16..=16).-const MOTION: &[(&str, i16)] = &[+pub(crate) const MOTION: &[(&str, i16)] = &[("0000 0011 001", -16),("0000 0011 011", -15),("0000 0011 101", -14),@@ -292,10 +292,10 @@ const MOTION: &[(&str, i16)] = &[];/// B.11: dmvector.-const DMV: &[(&str, i16)] = &[("11", -1), ("0", 0), ("10", 1)];+pub(crate) const DMV: &[(&str, i16)] = &[("11", -1), ("0", 0), ("10", 1)];/// B.12: dct_dc_size_luminance.-const DC_LUMA: &[(&str, i16)] = &[+pub(crate) const DC_LUMA: &[(&str, i16)] = &[("100", 0),("00", 1),("01", 2),@@ -311,7 +311,7 @@ const DC_LUMA: &[(&str, i16)] = &[];/// B.13: dct_dc_size_chrominance.-const DC_CHROMA: &[(&str, i16)] = &[+pub(crate) const DC_CHROMA: &[(&str, i16)] = &[("00", 0),("01", 1),("10", 2),@@ -330,16 +330,16 @@ const DC_CHROMA: &[(&str, i16)] = &[pub(crate) const DCT_EOB: i16 = -1;pub(crate) const DCT_ESCAPE: i16 = -2;-const fn rl(run: i16, level: i16) -> i16 {+pub(crate) const fn rl(run: i16, level: i16) -> i16 {run | (level << 6)}/// Table zero's codes for levels 12-15 at run 0 (table one has shorter ones).-const DCT_ZERO_LONG: &[(&str, i16)] =+pub(crate) const DCT_ZERO_LONG: &[(&str, i16)] =&[("0000 0000 1101 0", rl(0, 12)), ("0000 0000 1100 1", rl(0, 13)), ("0000 0000 1100 0", rl(0, 14)), ("0000 0000 1011 1", rl(0, 15))];/// Codes from "0000 0000 1" on, common to tables zero and one (sign bit excluded).-const DCT_LONG: &[(&str, i16)] = &[+pub(crate) const DCT_LONG: &[(&str, i16)] = &[("0000 0000 1011 0", rl(1, 6)),("0000 0000 1010 1", rl(1, 7)),("0000 0000 1010 0", rl(2, 5)),@@ -403,7 +403,7 @@ const DCT_LONG: &[(&str, i16)] = &[];/// "0000 0001 xxxx" codes of both tables (table one replaces the six marked `*` with shorter ones).-const DCT_MID_SHARED: &[(&str, i16)] = &[+pub(crate) const DCT_MID_SHARED: &[(&str, i16)] = &[("0000 0001 1100", rl(3, 3)),("0000 0001 0010", rl(4, 3)),("0000 0001 1110", rl(6, 2)),@@ -417,7 +417,7 @@ const DCT_MID_SHARED: &[(&str, i16)] = &[];/// B.14: DCT coefficients table zero (the "1s" first-coefficient code is handled by the caller).-const DCT_ZERO: &[(&str, i16)] = &[+pub(crate) const DCT_ZERO: &[(&str, i16)] = &[("10", DCT_EOB),("11", rl(0, 1)),("011", rl(1, 1)),@@ -460,7 +460,7 @@ const DCT_ZERO: &[(&str, i16)] = &[];/// B.15: DCT coefficients table one (intra blocks with intra_vlc_format = 1).-const DCT_ONE: &[(&str, i16)] = &[+pub(crate) const DCT_ONE: &[(&str, i16)] = &[("0110", DCT_EOB),("10", rl(0, 1)),("010", rl(1, 1)),@@ -519,7 +519,7 @@ pub(crate) struct Tables {}/// A complete DCT coefficient table: its own short codes plus the shared long ones.-fn dct_table(short: &'static [(&'static str, i16)], extra: &'static [(&'static str, i16)]) -> Vec<(&'static str, i16)> {+pub(crate) fn dct_table(short: &'static [(&'static str, i16)], extra: &'static [(&'static str, i16)]) -> Vec<(&'static str, i16)> {let mut v = short.to_vec();v.extend_from_slice(extra);v.extend_from_slice(DCT_MID_SHARED);