From de9bde5d9b1006f045aaf9ae4c2bae7ac79fc236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 16 Sep 2026 16:12:13 +0300 Subject: [PATCH] vp9kf: VP9 tile size prefixes are BIG-endian decode.rs read the per-tile 4-byte size prefix as little-endian, so any frame with more than one tile - ffmpeg's default at 1080p, cols_log2 = 2 - starved the walk and refused a VALID stream as `Corrupt: truncated tile data`: 00 00 0D DC little-endian is 3691839488 against a 71110-byte tail. libvpx `get_tile_buffer` reads it with `mem_get_be32` (vp9_decodeframe.c:1688/1690; vpx_ports/mem_ops.h:88). One token changed, plus the comment and the diagnostic probe's accounting print. Evidence: an 8-tile 2560x1440 IVF now walks exactly (prefixes 120646 + last tile 11383 == 132029 tail; frame 1 120851 + 11355 == 132206); a 4-tile stream decodes 2/2 byte-exact against ffmpeg; keyframe_exact 4/4 (a single-tile frame takes the whole tail, so that path cannot change); 5 lib warnings unchanged. Reviewed independently: SAFE TO COMMIT. Not this: the residual real-content divergence is tile-independent - the same source re-encoded with -tile-columns 0 -tile-rows 0 still differs, so the tile walk was never the entropy problem. --- crates/ec-vp9/src/decode.rs | 5 +++-- crates/ec-vp9/tests/scratch_tiledbg.rs | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/crates/ec-vp9/src/decode.rs b/crates/ec-vp9/src/decode.rs index c792dd3e..bdf3ae32 100644 --- a/crates/ec-vp9/src/decode.rs +++ b/crates/ec-vp9/src/decode.rs @@ -209,7 +209,8 @@ impl Decoder { let tile_rows = 1usize << hdr.tile_info.rows_log2; // Tile data: every tile except the overall last carries a - // 32-bit LE size prefix (spec 6.4). + // 32-bit BIG-endian size prefix (spec 6.4; libvpx `get_tile_buffer` + // reads it with `mem_get_be32`, vp9_decodeframe.c:1688/1690). let mut data: &[u8] = tail; let mut tiles: Vec<&[u8]> = Vec::with_capacity(tile_cols * tile_rows); for tr in 0..tile_rows { @@ -219,7 +220,7 @@ impl Decoder { tiles.push(data); } else { ensure(data.len() >= 4, "truncated tile size")?; - let sz = u32::from_le_bytes(data[..4].try_into().unwrap()) as usize; + let sz = u32::from_be_bytes(data[..4].try_into().unwrap()) as usize; data = &data[4..]; ensure(data.len() >= sz, "truncated tile data")?; tiles.push(&data[..sz]); diff --git a/crates/ec-vp9/tests/scratch_tiledbg.rs b/crates/ec-vp9/tests/scratch_tiledbg.rs index 8d3abadc..19047c37 100644 --- a/crates/ec-vp9/tests/scratch_tiledbg.rs +++ b/crates/ec-vp9/tests/scratch_tiledbg.rs @@ -37,11 +37,29 @@ fn tile_dbg() { tail.len(), &tail[..16.min(tail.len())] ); + // Tile size prefixes are BIG-endian (libvpx `mem_get_be32`, + // vp9_decodeframe.c:1686) -- a LE read turns a 4-column + // 1080p prefix into ~5e8 and starves the walk. if tail.len() >= 4 { - let sz = u32::from_le_bytes(tail[..4].try_into().unwrap()) as usize; + let be = u32::from_be_bytes(tail[..4].try_into().unwrap()) as usize; + let le = u32::from_le_bytes(tail[..4].try_into().unwrap()) as usize; + println!(" first tile size prefix be={be} le={le} (tail-4 = {})", tail.len() - 4); + // Walk the prefixes: each must fit, and the LAST tile is the + // remainder (`get_tile_buffer` size = data_end - data). + let n = 1usize << (hdr.tile_info.cols_log2 + hdr.tile_info.rows_log2); + let (mut pos, mut ok) = (0usize, true); + for _ in 0..n.saturating_sub(1) { + if pos + 4 > tail.len() { + ok = false; + break; + } + let sz = u32::from_be_bytes(tail[pos..pos + 4].try_into().unwrap()) as usize; + pos += 4 + sz; + ok &= pos <= tail.len(); + } println!( - " first tile size prefix {sz} (tail-4 = {})", - tail.len() - 4 + " walk: {n} tiles, prefixes consume {pos} ok={ok}, last tile {}", + tail.len().saturating_sub(pos) ); } }