forge
Explore
Log in

Don't you dare go Hollow

vp9kf: VP9 tile size prefixes are BIG-endian

Browse files at this commit

de9bde5d9b1006f045aaf9ae4c2bae7ac79fc236Ahmet Kaan GümüşParent: 82ba265
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.rsmodified+3 -2
@@ -209,7 +209,8 @@ impl Decoder {
209209 let tile_rows = 1usize << hdr.tile_info.rows_log2;
210210
211211 // Tile data: every tile except the overall last carries a
212- // 32-bit LE size prefix (spec 6.4).
212+ // 32-bit BIG-endian size prefix (spec 6.4; libvpx `get_tile_buffer`
213+ // reads it with `mem_get_be32`, vp9_decodeframe.c:1688/1690).
213214 let mut data: &[u8] = tail;
214215 let mut tiles: Vec<&[u8]> = Vec::with_capacity(tile_cols * tile_rows);
215216 for tr in 0..tile_rows {
@@ -219,7 +220,7 @@ impl Decoder {
219220 tiles.push(data);
220221 } else {
221222 ensure(data.len() >= 4, "truncated tile size")?;
222- let sz = u32::from_le_bytes(data[..4].try_into().unwrap()) as usize;
223+ let sz = u32::from_be_bytes(data[..4].try_into().unwrap()) as usize;
223224 data = &data[4..];
224225 ensure(data.len() >= sz, "truncated tile data")?;
225226 tiles.push(&data[..sz]);
crates/ec-vp9/tests/scratch_tiledbg.rsmodified+21 -3
@@ -37,11 +37,29 @@ fn tile_dbg() {
3737 tail.len(),
3838 &tail[..16.min(tail.len())]
3939 );
40+ // Tile size prefixes are BIG-endian (libvpx `mem_get_be32`,
41+ // vp9_decodeframe.c:1686) -- a LE read turns a 4-column
42+ // 1080p prefix into ~5e8 and starves the walk.
4043 if tail.len() >= 4 {
41- let sz = u32::from_le_bytes(tail[..4].try_into().unwrap()) as usize;
44+ let be = u32::from_be_bytes(tail[..4].try_into().unwrap()) as usize;
45+ let le = u32::from_le_bytes(tail[..4].try_into().unwrap()) as usize;
46+ println!(" first tile size prefix be={be} le={le} (tail-4 = {})", tail.len() - 4);
47+ // Walk the prefixes: each must fit, and the LAST tile is the
48+ // remainder (`get_tile_buffer` size = data_end - data).
49+ let n = 1usize << (hdr.tile_info.cols_log2 + hdr.tile_info.rows_log2);
50+ let (mut pos, mut ok) = (0usize, true);
51+ for _ in 0..n.saturating_sub(1) {
52+ if pos + 4 > tail.len() {
53+ ok = false;
54+ break;
55+ }
56+ let sz = u32::from_be_bytes(tail[pos..pos + 4].try_into().unwrap()) as usize;
57+ pos += 4 + sz;
58+ ok &= pos <= tail.len();
59+ }
4260 println!(
43- " first tile size prefix {sz} (tail-4 = {})",
44- tail.len() - 4
61+ " walk: {n} tiles, prefixes consume {pos} ok={ok}, last tile {}",
62+ tail.len().saturating_sub(pos)
4563 );
4664 }
4765 }