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) ); } }