forge
Explore
Log in

Don't you dare go Hollow

Palette colours were read at 8 bits on every stream -- the raw literal widths, the delta range and the clamp are all sized by the sequence bit_depth

Browse files at this commit

769ba672ceba09f4b03a19e965af507b5fb47f7bAhmet Kaan GümüşParent: 87836ae
  • fix
    • palette colours were read at 8 bits on every stream -- the raw literal widths, the delta range and the clamp are all sized by the sequence bit_depth
Show full message
fix(av1): palette colours were read at 8 bits on every stream -- the raw literal widths, the delta range and the clamp are all sized by the sequence bit_depth

read_palette_colors_y/_uv (decodemv.c:478/:509) size every raw literal by
bit_depth: the first colour is bit_depth bits, min_bits is bit_depth-3 (Y/U)
and bit_depth-4 (V), range starts at 1<<bit_depth and the clamp/wrap bound is
(1<<bit_depth)-1. Ours hardcoded 8, so a 10-bit palette block read two bits
too few per transmitted colour and decoded the whole rest of the tile from
the wrong bit position -- with no refusal.

It was silent because raw literals are EQUIPROBABLE and at the ranges this
content reaches an equiprobable read maps the msac range onto itself
(62728 -> 31364 -> renormalised 62728), so a cross-decoder RANGE ladder stays
bit-identical straight through the miscount and only the VALUE moves
(compare-range-not-tell's blind spot). Pinned on a 256x192 10-bit cq59
stream whose first block is a 32x16 palette: aomdec's EC_PAL ladder and ours
agreed on every range and disagreed on the first colour index, 0 vs 1. After
the fix all 1024 EC_PAL lines of that block match aomdec line for line and
the block grid matches aomdec's for the rest of the frame.

Gate: a_real_aomenc_palette_stream_decodes_pixel_exact_at_8_and_10_bit --
the existing 8-bit palette recipe with the depth as the only variable, both
arms hard-asserting palette_hits() moved and comparing all three planes
against ffmpeg. Reverting the fix to a literal 8 makes the 10-bit arm fail
on luma; the 8-bit arm is unaffected.
crates/ec-av1/src/decode.rsmodified+29 -16
@@ -5220,7 +5220,15 @@ fn merge_colors(transmitted: &[u16], cached: &[u16]) -> Vec<u16> {
5220 out5220 out
5221 }5221 }
5222 5222
5223-/// `read_palette_colors_y` (decodemv.c:478), bit_depth fixed at 8 (this5223+/// `read_palette_colors_y` (decodemv.c:478), at the sequence's real
5224+/// `bit_depth` -- lane-kf900 r3: every raw literal here (the first colour,
5225+/// each delta's width, the `range` bound and the clamp) is sized by
5226+/// `bit_depth`, and hardcoding 8 read 2 bits too few per colour on every
5227+/// 10-bit palette block. Raw literals are EQUIPROBABLE, and at the ranges
5228+/// this stream reaches (e.g. 62728) an equiprobable read is a FIXED POINT of
5229+/// the msac range, so a cross-decoder range ladder stays bit-identical
5230+/// through the whole miscount -- only the decoded VALUE differs (class
5231+/// `compare-range-not-tell`'s blind spot). (this
5224 /// decoder is 8-bit only): accepts up to `n` colours straight from `cache`5232 /// decoder is 8-bit only): accepts up to `n` colours straight from `cache`
5225 /// (one flag bit each, in cache order), then delta-codes the remainder5233 /// (one flag bit each, in cache order), then delta-codes the remainder
5226 /// (first value raw, each following delta `>= 1` off a shrinking bit width),5234 /// (first value raw, each following delta `>= 1` off a shrinking bit width),
@@ -5235,20 +5243,22 @@ fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> [u
5235 cached.push(c);5243 cached.push(c);
5236 }5244 }
5237 }5245 }
5246+ let bd = u32::from(bit_depth());
5247+ let max_val = (1i32 << bd) - 1;
5238 let mut colors = [0u16; 8];5248 let mut colors = [0u16; 8];
5239 let n_cached = cached.len();5249 let n_cached = cached.len();
5240 if n_cached < n {5250 if n_cached < n {
5241 let mut transmitted = Vec::with_capacity(n - n_cached);5251 let mut transmitted = Vec::with_capacity(n - n_cached);
5242- let first = dec.literal(8) as u16;5252+ let first = dec.literal(bd) as u16;
5243 transmitted.push(first);5253 transmitted.push(first);
5244 if n_cached + 1 < n {5254 if n_cached + 1 < n {
5245- let min_bits = 8u32 - 3;5255+ let min_bits = bd - 3;
5246 let mut bits = min_bits + dec.literal(2);5256 let mut bits = min_bits + dec.literal(2);
5247- let mut range = 255i32 - i32::from(first) - 1;5257+ let mut range = (1i32 << bd) - i32::from(first) - 1;
5248 for _ in (n_cached + 1)..n {5258 for _ in (n_cached + 1)..n {
5249 let delta = dec.literal(bits) as i32 + 1;5259 let delta = dec.literal(bits) as i32 + 1;
5250 let prev = *transmitted.last().unwrap();5260 let prev = *transmitted.last().unwrap();
5251- let val = (i32::from(prev) + delta).clamp(0, 255) as u16;5261+ let val = (i32::from(prev) + delta).clamp(0, max_val) as u16;
5252 range -= i32::from(val) - i32::from(prev);5262 range -= i32::from(val) - i32::from(prev);
5253 bits = bits.min(ceil_log2(range.max(0) as u32));5263 bits = bits.min(ceil_log2(range.max(0) as u32));
5254 transmitted.push(val);5264 transmitted.push(val);
@@ -5262,7 +5272,8 @@ fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> [u
5262 colors5272 colors
5263 }5273 }
5264 5274
5265-/// `read_palette_colors_uv` (decodemv.c:509), bit_depth fixed at 8: U reads5275+/// `read_palette_colors_uv` (decodemv.c:509), at the sequence's real
5276+/// `bit_depth` (see [`read_palette_colors_y`]): U reads
5266 /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only5277 /// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only
5267 /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta`5278 /// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta`
5268 /// unbiased by `+1` -- both differences from Y's own reader, matching the C5279 /// unbiased by `+1` -- both differences from Y's own reader, matching the C
@@ -5279,20 +5290,22 @@ fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> (
5279 cached.push(c);5290 cached.push(c);
5280 }5291 }
5281 }5292 }
5293+ let bd = u32::from(bit_depth());
5294+ let max_val = 1i32 << bd;
5282 let mut u_colors = [0u16; 8];5295 let mut u_colors = [0u16; 8];
5283 let n_cached = cached.len();5296 let n_cached = cached.len();
5284 if n_cached < n {5297 if n_cached < n {
5285 let mut transmitted = Vec::with_capacity(n - n_cached);5298 let mut transmitted = Vec::with_capacity(n - n_cached);
5286- let first = dec.literal(8) as u16;5299+ let first = dec.literal(bd) as u16;
5287 transmitted.push(first);5300 transmitted.push(first);
5288 if n_cached + 1 < n {5301 if n_cached + 1 < n {
5289- let min_bits = 8u32 - 3;5302+ let min_bits = bd - 3;
5290 let mut bits = min_bits + dec.literal(2);5303 let mut bits = min_bits + dec.literal(2);
5291- let mut range = 255i32 - i32::from(first);5304+ let mut range = max_val - i32::from(first);
5292 for _ in (n_cached + 1)..n {5305 for _ in (n_cached + 1)..n {
5293 let delta = dec.literal(bits) as i32;5306 let delta = dec.literal(bits) as i32;
5294 let prev = *transmitted.last().unwrap();5307 let prev = *transmitted.last().unwrap();
5295- let val = (i32::from(prev) + delta).clamp(0, 255) as u16;5308+ let val = (i32::from(prev) + delta).clamp(0, max_val - 1) as u16;
5296 range -= i32::from(val) - i32::from(prev);5309 range -= i32::from(val) - i32::from(prev);
5297 bits = bits.min(ceil_log2(range.max(0) as u32));5310 bits = bits.min(ceil_log2(range.max(0) as u32));
5298 transmitted.push(val);5311 transmitted.push(val);
@@ -5305,9 +5318,9 @@ fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> (
5305 }5318 }
5306 let mut v_colors = [0u16; 8];5319 let mut v_colors = [0u16; 8];
5307 if dec.literal(1) == 1 {5320 if dec.literal(1) == 1 {
5308- let min_bits_v = 8u32 - 4;5321+ let min_bits_v = bd - 4;
5309 let bits = min_bits_v + dec.literal(2);5322 let bits = min_bits_v + dec.literal(2);
5310- v_colors[0] = dec.literal(8) as u16;5323+ v_colors[0] = dec.literal(bd) as u16;
5311 for i in 1..n {5324 for i in 1..n {
5312 let mut delta = dec.literal(bits) as i32;5325 let mut delta = dec.literal(bits) as i32;
5313 if delta != 0 && dec.literal(1) == 1 {5326 if delta != 0 && dec.literal(1) == 1 {
@@ -5315,16 +5328,16 @@ fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> (
5315 }5328 }
5316 let mut val = i32::from(v_colors[i - 1]) + delta;5329 let mut val = i32::from(v_colors[i - 1]) + delta;
5317 if val < 0 {5330 if val < 0 {
5318- val += 256;5331+ val += max_val;
5319 }5332 }
5320- if val >= 256 {5333+ if val >= max_val {
5321- val -= 256;5334+ val -= max_val;
5322 }5335 }
5323 v_colors[i] = val as u16;5336 v_colors[i] = val as u16;
5324 }5337 }
5325 } else {5338 } else {
5326 for i in 0..n {5339 for i in 0..n {
5327- v_colors[i] = dec.literal(8) as u16;5340+ v_colors[i] = dec.literal(bd) as u16;
5328 }5341 }
5329 }5342 }
5330 (u_colors, v_colors)5343 (u_colors, v_colors)
crates/ec-av1/src/stream.rsmodified+141 -0
@@ -2572,6 +2572,147 @@ mod tests {
2572 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");2572 assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
2573 }2573 }
2574 2574
2575+ /// lane-kf900 r3: the palette colour reader at BOTH bit depths.
2576+ ///
2577+ /// `read_palette_colors_y`/`_uv` (decode.rs, mirroring `decodemv.c:478`
2578+ /// and `:509`) size every raw literal they read by the sequence's
2579+ /// `bit_depth`: the first colour is `bit_depth` bits, `min_bits` is
2580+ /// `bit_depth - 3` (Y/U) or `bit_depth - 4` (V), `range` starts at
2581+ /// `1 << bit_depth`, and the clamp/wrap bound is `(1 << bit_depth) - 1`.
2582+ /// Ours hardcoded 8, so every 10-bit palette block read TWO BITS TOO FEW
2583+ /// per transmitted colour and silently decoded the rest of the tile from
2584+ /// the wrong bit position.
2585+ ///
2586+ /// It was silent because raw literals are EQUIPROBABLE: at the ranges
2587+ /// this content reaches an equiprobable read maps the msac range onto
2588+ /// itself (62728 -> 31364 -> renormalised 62728), so a cross-decoder
2589+ /// RANGE ladder stays bit-identical straight through the miscount and
2590+ /// only the decoded VALUE moves. Pinned on a 256x192 10-bit
2591+ /// `--cq-level=59` stream whose first block is a 32x16 palette: aomdec's
2592+ /// `EC_PAL` ladder and ours agreed on every range and disagreed on the
2593+ /// very first colour index (0 vs 1); after the fix all 1024 `EC_PAL`
2594+ /// lines of that block match aomdec line for line.
2595+ ///
2596+ /// The recipe is the 8-bit `..._with_palette_y_...` gate's, with the
2597+ /// depth as the only variable, so a failure here is the depth and
2598+ /// nothing else. HARD-asserts [`decode::palette_hits`] moved on each arm
2599+ /// (a stream with no real palette block would prove nothing) and
2600+ /// compares all three planes against ffmpeg's decode of the same bytes.
2601+ #[test]
2602+ fn a_real_aomenc_palette_stream_decodes_pixel_exact_at_8_and_10_bit() {
2603+ const NAME: &str = "a_real_aomenc_palette_stream_decodes_pixel_exact_at_8_and_10_bit";
2604+ if !have_ffmpeg() {
2605+ eprintln!("SKIP {NAME}: no ffmpeg");
2606+ return;
2607+ }
2608+ if !have_aomenc() {
2609+ eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display());
2610+ return;
2611+ }
2612+ let (width, height) = (64usize, 64usize);
2613+ for depth in [8usize, 10] {
2614+ let pix_fmt = if depth == 10 { "yuv420p10le" } else { "yuv420p" };
2615+ let render = || {
2616+ Command::new("ffmpeg")
2617+ .args([
2618+ "-v", "error",
2619+ "-f", "lavfi",
2620+ "-i", "smptebars=size=64x64:rate=25",
2621+ "-vf", "hue=s=0",
2622+ "-pix_fmt", pix_fmt,
2623+ "-strict", "-1",
2624+ "-t", "0.04",
2625+ "-f", "yuv4mpegpipe",
2626+ "-",
2627+ ])
2628+ .stdin(Stdio::null())
2629+ .stdout(Stdio::piped())
2630+ .stderr(Stdio::piped())
2631+ .output()
2632+ .expect("ffmpeg failed to run")
2633+ .stdout
2634+ };
2635+ let y4m_a = render();
2636+ let y4m_b = render();
2637+ assert_eq!(
2638+ y4m_a, y4m_b,
2639+ "{NAME}: smptebars must render byte-identical across two runs at {depth}-bit"
2640+ );
2641+ let y4m = y4m_a;
2642+ let depth_arg = format!("--bit-depth={depth}");
2643+ let input_depth_arg = format!("--input-bit-depth={depth}");
2644+ let mut child = Command::new(aomenc_path())
2645+ .args([
2646+ "--codec=av1",
2647+ "--passes=1",
2648+ "--end-usage=q",
2649+ "--cq-level=30",
2650+ "--cpu-used=0",
2651+ "--threads=1",
2652+ "--row-mt=0",
2653+ "--sb-size=64",
2654+ "--tune-content=screen",
2655+ "--enable-palette=1",
2656+ "--enable-intrabc=0",
2657+ "--enable-rect-partitions=0",
2658+ "--enable-ab-partitions=0",
2659+ "--enable-1to4-partitions=0",
2660+ "--min-partition-size=32",
2661+ "--max-partition-size=32",
2662+ "--enable-filter-intra=0",
2663+ "--enable-smooth-intra=0",
2664+ "--enable-paeth-intra=0",
2665+ "--enable-directional-intra=0",
2666+ "--enable-angle-delta=0",
2667+ "--enable-cfl-intra=0",
2668+ "--enable-cdef=0",
2669+ "--enable-restoration=0",
2670+ "--enable-tx-size-search=0",
2671+ "--loopfilter-control=0",
2672+ "--limit=1",
2673+ ])
2674+ // Per-arm overrides go AFTER the base recipe: aomenc keeps the
2675+ // LAST occurrence of a repeated flag.
2676+ .args([&depth_arg, &input_depth_arg])
2677+ .args(["--obu", "-o", "-", "-"])
2678+ .stdin(Stdio::piped())
2679+ .stdout(Stdio::piped())
2680+ .stderr(Stdio::piped())
2681+ .spawn()
2682+ .expect("aomenc failed to start");
2683+ child
2684+ .stdin
2685+ .take()
2686+ .expect("aomenc stdin")
2687+ .write_all(&y4m)
2688+ .expect("writing y4m to aomenc");
2689+ let out = child.wait_with_output().expect("aomenc failed to run");
2690+ assert!(
2691+ out.status.success(),
2692+ "{NAME}: aomenc refused the {depth}-bit fixture: {}",
2693+ String::from_utf8_lossy(&out.stderr)
2694+ );
2695+ let stream = out.stdout;
2696+ let before = decode::palette_hits();
2697+ let frames = match decode_stream(&stream) {
2698+ Ok(frames) => frames,
2699+ Err(e) => panic!("{NAME}: decode_stream refused the {depth}-bit stream: {e}"),
2700+ };
2701+ assert!(
2702+ decode::palette_hits() > before,
2703+ "{NAME}: no real palette block at {depth}-bit -- gate is vacuous"
2704+ );
2705+ let ffmpeg_frames = if depth == 10 {
2706+ ffmpeg_decode_sequence_10bit(&stream, width, height, 1)
2707+ } else {
2708+ ffmpeg_decode_sequence(&stream, width, height, 1)
2709+ };
2710+ assert_eq!(frames[0].y, ffmpeg_frames[0].y, "{NAME}: luma at {depth}-bit");
2711+ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "{NAME}: U at {depth}-bit");
2712+ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "{NAME}: V at {depth}-bit");
2713+ }
2714+ }
2715+
2575 /// lane-hbd r5: a real `aomenc --bit-depth=10` stream, decoded through2716 /// lane-hbd r5: a real `aomenc --bit-depth=10` stream, decoded through
2576 /// this crate's own 10-bit planes (`Picture` widened to `u16` in an2717 /// this crate's own 10-bit planes (`Picture` widened to `u16` in an
2577 /// earlier round) and checked pixel-exact against ffmpeg's own decode of2718 /// earlier round) and checked pixel-exact against ffmpeg's own decode of