From 120854a398c719ca7b7e08da4e4d9deb925e7375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 11:36:16 +0300 Subject: [PATCH] debug(av1): r9 falsifies hypothesis A and rules out qmatrix/delta_q/segmentation Task 1 (frame inventory): manual byte-level OBU parse of pin_cfl.obu (77 bytes total) shows exactly TD(size 0) + SEQUENCE_HEADER(size 10) + one OBU_FRAME(type 6, size 61) to EOF -- no second frame OBU, no show_existing_frame, nothing hidden. ffprobe/ffmpeg (libdav1d) also read exactly 1 frame from the file. decode_stream returns 1 Picture; the test compares frames[0] against ffmpeg_frames[0], the same single frame both decoders parsed. Hypothesis A (ffmpeg showing a different frame) is FALSIFIED for this fixture -- three independent confirmations (manual OBU byte parse, ffprobe frame count, our own frame count) agree on single-frame-single-shown. Task 2 (reconstruction diff), new evidence this round: added EC_AV1_TRACE prints for quantization/segmentation/delta params to scratch_isolate_pinned_mismatch (stream.rs) and confirmed for pin_cfl.obu: using_qmatrix=false, delta_q_y_dc=0 and delta.q_present=false, segmentation.enabled=false, coded_lossless=false, tx_mode=Largest -- so none of those alternate-quantization paths (which decode.rs does not implement) are actually exercised by this stream; ruling them out removes several live hypotheses for the amplitude gap rather than leaving them open. Independently verified the transform math itself: for the block's proven levels ((0,0)=-2,(0,1)=-2,(0,2)=-2, dc_sign negative), dequant at base_q_idx=60 gives [-57,-67,-67], and BOTH a from-scratch scipy `idctn(norm='ortho')` computation and this crate's own `dequant_and_inverse` agree the resulting residual is [-2,-2,-2,-2,-2,-1, -1,-1,-1,-1,0,...,0] across the row -- i.e. our transform pipeline is mathematically self-consistent and correct *given* those coefficients. That residual added to DC_PRED=128 reconstructs to ~126, which is what this decoder outputs; ffmpeg's real decode of the identical bytes is a smooth 41-82 diagonal gradient that cannot come from this residual under any correct reconstruction. So the remaining gap is neither hypothesis A (ruled out above) nor a transform/dequant bug (ruled out here) -- it must be a symbol read upstream of the proven-clean coefficient values (partition_w32, most likely: a single 32x32 PARTITION_NONE forces the whole frame through one DC_PRED block, while a wrong partition context table could silently produce the same "coefficients bit-exact for the symbols we read" result while reading the wrong symbol in the first place). r10: re-derive partition_w32's CDF/context table from entropymode.c independently of both this crate's and r8's prior Python cross-check (which may share the same transcription error), and try forcing a SPLIT decode of this exact stream to see whether a 4-subblock reconstruction (each with its own propagated DC neighbour) produces ffmpeg's smooth gradient shape. Left the EC_AV1_TRACE prints (quantization/delta/segmentation/lossless) in scratch_isolate_pinned_mismatch and a new #[ignore]d scratch_probe_32x32_dequant in transform.rs documenting the independent math check -- both scratch/debug-only, gated behind #[ignore] or env vars, no behavior change. cargo test -p ec-av1 --release: 164 passed, 0 failed, 13 ignored. --- crates/ec-av1/src/stream.rs | 7 +++++++ crates/ec-av1/src/transform.rs | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 0821d7c9..5ac3a42e 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -560,6 +560,13 @@ mod tests { pos += obu.total_size; if let ObuKind::Frame(header, _) = obu.kind { eprintln!("cdef: {:?}", header.cdef); + eprintln!("quantization: {:?}", header.quantization); + eprintln!( + "coded_lossless: {:?} tx_mode: {:?}", + header.coded_lossless, header.tx_mode + ); + eprintln!("delta: {:?}", header.delta); + eprintln!("segmentation: {:?}", header.segmentation); } } } diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 41a3fbce..ae89866c 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1050,6 +1050,41 @@ mod tests { .collect() } + #[test] + #[ignore] + fn scratch_probe_32x32_dequant() { + let mut levels = vec![0i32; 32 * 32]; + levels[0] = -2; + levels[1] = -2; + levels[2] = -2; + let dq = crate::quant::dequant(&levels, 32, 8, 60); + eprintln!("dq[0..4]={:?}", &dq[0..4]); + let residual = dequant_and_inverse(&levels, 32, 8, 60); + eprintln!("row0: {:?}", &residual[0..32]); + eprintln!("row1: {:?}", &residual[32..64]); + + // superposition probe: does inverse_transform_2d at side=32 sum + // three simultaneous low-frequency coefficients the same as it + // reconstructs each alone? + let side = 32usize; + let mut single_sum = vec![0i32; side * side]; + for &(pos, val) in &[(0usize, -57i32), (1, -67), (2, -67)] { + let mut c = vec![0i32; side * side]; + c[pos] = val; + let got = inverse_transform_2d(&c, side, 8); + for i in 0..side * side { + single_sum[i] += got[i]; + } + } + let mut combined = vec![0i32; side * side]; + combined[0] = -57; + combined[1] = -67; + combined[2] = -67; + let got_combined = inverse_transform_2d(&combined, side, 8); + eprintln!("single_sum row0: {:?}", &single_sum[0..8]); + eprintln!("combined row0: {:?}", &got_combined[0..8]); + } + fn rmse(a: &[i32], b: &[i32]) -> f64 { let sum: f64 = a .iter()