Rect intra strips never read use_intrabc -- every screen-content key frame desynced at its first symbol
Browse files at this commit0bdf223f5e9a3e79992ec0f8fc137e3ae7b06403Parent: 6e5d3f7- fix
- rect intra strips never read use_intrabc -- every screen-content key frame desynced at its first symbol
4 changed files +241 -0
- crates/ec-av1/examples/decode_probe.rs +1 -0
- crates/ec-av1/src/decode.rs +45 -0
- crates/ec-av1/src/refusal_inventory.rs +1 -0
- crates/ec-av1/src/stream.rs +194 -0
crates/ec-av1/examples/decode_probe.rsmodified+1 -0
| @@ -33,6 +33,7 @@ fn main() { | |||
| 33 | println!("reelc_chroma: skip_cfl={sc} dir_1to4_pairs={dp}"); | 33 | println!("reelc_chroma: skip_cfl={sc} dir_1to4_pairs={dp}"); |
| 34 | let (h, v, c) = ec_av1::stream::rect4_32_counters(); | 34 | let (h, v, c) = ec_av1::stream::rect4_32_counters(); |
| 35 | println!("rect4_32: horz={h} vert={v} coded={c}"); | 35 | println!("rect4_32: horz={h} vert={v} coded={c}"); |
| 36 | + println!("rect_intrabc_reads: {}", ec_av1::stream::rect_intrabc_reads()); | ||
| 36 | let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); | 37 | let (rtu, rsplit, robmc) = ec_av1::stream::rect_inter_tu_counters(); |
| 37 | println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); | 38 | println!("rect_inter: tu={rtu} txsplit={rsplit} obmc_leaf={robmc}"); |
| 38 | // lane-kf900 r1: the counter the skipped-8x8-split-transform gate | 39 | // lane-kf900 r1: the counter the skipped-8x8-split-transform gate |
crates/ec-av1/src/decode.rsmodified+45 -0
| @@ -1571,6 +1571,30 @@ thread_local! { | |||
| 1571 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | 1571 | static INTRABC_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 1572 | } | 1572 | } |
| 1573 | 1573 | ||
| 1574 | +/// Whether this frame's header set `allow_intrabc` -- [`INTRABC_MI_GRID`] is | ||
| 1575 | +/// `Some` exactly then (set once per frame in `decode_frame`), so the flag | ||
| 1576 | +/// needs no separate plumbing to reach the rect-strip mode reader. | ||
| 1577 | +fn allow_intrabc_frame() -> bool { | ||
| 1578 | + INTRABC_MI_GRID.with(|g| g.borrow().is_some()) | ||
| 1579 | +} | ||
| 1580 | + | ||
| 1581 | +thread_local! { | ||
| 1582 | + /// How many rect intra strips READ the `use_intrabc` symbol (i.e. how many | ||
| 1583 | + /// times the fix below was exercised) -- the rect-strip gate's hit counter, | ||
| 1584 | + /// distinct from [`INTRABC_HITS`] which counts blocks that USE intrabc. | ||
| 1585 | + static RECT_INTRABC_READS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 1586 | +} | ||
| 1587 | + | ||
| 1588 | +/// Current value of [`RECT_INTRABC_READS`]. | ||
| 1589 | +pub(crate) fn rect_intrabc_reads() -> usize { | ||
| 1590 | + RECT_INTRABC_READS.with(|c| c.get()) | ||
| 1591 | +} | ||
| 1592 | + | ||
| 1593 | +/// Zeroes [`RECT_INTRABC_READS`] (gate tests call this before a decode). | ||
| 1594 | +pub(crate) fn reset_rect_intrabc_reads() { | ||
| 1595 | + RECT_INTRABC_READS.with(|c| c.set(0)); | ||
| 1596 | +} | ||
| 1597 | + | ||
| 1574 | /// Current value of [`INTRABC_HITS`]. | 1598 | /// Current value of [`INTRABC_HITS`]. |
| 1575 | pub(crate) fn intrabc_hits() -> usize { | 1599 | pub(crate) fn intrabc_hits() -> usize { |
| 1576 | INTRABC_HITS.with(|c| c.get()) | 1600 | INTRABC_HITS.with(|c| c.get()) |
| @@ -5580,6 +5604,27 @@ fn read_intra_mode_rect( | |||
| 5580 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); | 5604 | maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip); |
| 5581 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); | 5605 | maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip); |
| 5582 | istep!("dq", 0); | 5606 | istep!("dq", 0); |
| 5607 | + // `read_intrabc_info` (spec 5.11.13, libaom `decodemv.c:892`, right after | ||
| 5608 | + // `skip`/`segment_id`/`cdef`/`delta_q` and before `mbmi->mode`): the | ||
| 5609 | + // `use_intrabc` symbol is read for EVERY intra block of an `allow_intrabc` | ||
| 5610 | + // frame -- `av1_allow_intrabc` is a frame-level test with no shape term, so | ||
| 5611 | + // rect strips read it exactly like square leaves ([`read_intra_mode`], | ||
| 5612 | + // [`read_intra_mode_sub8`]). Missing it here desynced the arithmetic | ||
| 5613 | + // decoder at the FIRST symbol of the first rect block of every | ||
| 5614 | + // `--tune-content=screen` key frame. Reconstruction of a real intrabc | ||
| 5615 | + // block at this shape is out of scope, so the flag is consumed and | ||
| 5616 | + // refused by name when it fires. | ||
| 5617 | + if allow_intrabc_frame() { | ||
| 5618 | + let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0; | ||
| 5619 | + RECT_INTRABC_READS.with(|c| c.set(c.get() + 1)); | ||
| 5620 | + istep!("intrabc", use_intrabc as i32); | ||
| 5621 | + if use_intrabc { | ||
| 5622 | + INTRABC_HITS.with(|c| c.set(c.get() + 1)); | ||
| 5623 | + return Err(unsupported( | ||
| 5624 | + "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)", | ||
| 5625 | + )); | ||
| 5626 | + } | ||
| 5627 | + } | ||
| 5583 | let above_ctx = INTRA_MODE_CTX[above_mode]; | 5628 | let above_ctx = INTRA_MODE_CTX[above_mode]; |
| 5584 | let left_ctx = INTRA_MODE_CTX[left_mode]; | 5629 | let left_ctx = INTRA_MODE_CTX[left_mode]; |
| 5585 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); | 5630 | let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]); |
crates/ec-av1/src/refusal_inventory.rsmodified+1 -0
| @@ -47,6 +47,7 @@ const REFUSALS: &[&str] = &[ | |||
| 47 | "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", | 47 | "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", |
| 48 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", | 48 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 49 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", | 49 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| 50 | + "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)", | ||
| 50 | "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)", | 51 | "a sub-8x8 leaf that uses intrabc (this reader has no block-vector path; the 8x8-and-up reader reconstructs one)", |
| 51 | "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", | 52 | "an intrabc block under TxMode::Select (its transform size is coded by the inter var-tx partition tree, which this decoder never reads)", |
| 52 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", | 53 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", |
crates/ec-av1/src/stream.rsmodified+194 -0
| @@ -74,6 +74,11 @@ pub fn rect64_inter_tu_hits() -> usize { | |||
| 74 | crate::decode::rect64_inter_tu_hits() | 74 | crate::decode::rect64_inter_tu_hits() |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | +/// How many rect intra strips read the `use_intrabc` symbol on the last decode. | ||
| 78 | +pub fn rect_intrabc_reads() -> usize { | ||
| 79 | + crate::decode::rect_intrabc_reads() | ||
| 80 | +} | ||
| 81 | + | ||
| 77 | pub fn rect4_32_counters() -> (usize, usize, usize) { | 82 | pub fn rect4_32_counters() -> (usize, usize, usize) { |
| 78 | ( | 83 | ( |
| 79 | crate::decode::rect4_32_horz_hits(), | 84 | crate::decode::rect4_32_horz_hits(), |
| @@ -2713,6 +2718,195 @@ mod tests { | |||
| 2713 | } | 2718 | } |
| 2714 | } | 2719 | } |
| 2715 | 2720 | ||
| 2721 | + /// lane-kf900 r5, the gate for the rect-strip `use_intrabc` read. | ||
| 2722 | + /// | ||
| 2723 | + /// `av1_allow_intrabc` is a FRAME-level test (`decodemv.c:892`, | ||
| 2724 | + /// `read_intra_frame_mode_info`): on an intra frame whose header set | ||
| 2725 | + /// `allow_intrabc`, EVERY intra block reads a `use_intrabc` symbol right | ||
| 2726 | + /// after `skip`/`cdef`/`delta_q` and before `mbmi->mode`. There is no | ||
| 2727 | + /// block-shape term, so rect strips read it exactly like square leaves -- | ||
| 2728 | + /// `read_intra_mode_rect` never did, and the arithmetic decoder desynced | ||
| 2729 | + /// at the FIRST symbol of the first rect block of every | ||
| 2730 | + /// `--tune-content=screen` key frame (measured: `testsrc2` 256x192 cq59 | ||
| 2731 | + /// 8-bit decoded "OK" with all 49152 luma samples wrong). | ||
| 2732 | + /// | ||
| 2733 | + /// Arm 0 is that recipe at cq50 without palette: its rect strips really do | ||
| 2734 | + /// use intrabc, which this decoder cannot reconstruct, so it must stop on | ||
| 2735 | + /// the NAMED refusal instead of emitting a wrong frame -- and it must have | ||
| 2736 | + /// read the symbol to get there. | ||
| 2737 | + /// Arm 1 is the same recipe without palette and without tx-size search -- | ||
| 2738 | + /// it decodes the whole frame, reading the symbol dozens of times. | ||
| 2739 | + /// Arm 2 is a screen-tools key frame whose header does NOT set | ||
| 2740 | + /// `allow_intrabc` (cq63): it stays pixel-exact, proving the new read is | ||
| 2741 | + /// not taken when libaom does not write it. | ||
| 2742 | + /// | ||
| 2743 | + /// PIXEL RESIDUE (open, lane-kf900 r5): arm 1's frame is entropy-exact | ||
| 2744 | + /// against the instrumented aomdec for 429 mode-ladder elements and then | ||
| 2745 | + /// diverges inside the DV read of a SQUARE intrabc block at mi(32,44), so | ||
| 2746 | + /// its pixels are not compared here. That is a separate defect in | ||
| 2747 | + /// `read_intrabc_dv`, not in the read this gate covers. | ||
| 2748 | + #[test] | ||
| 2749 | + fn a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips() { | ||
| 2750 | + const NAME: &str = "a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips"; | ||
| 2751 | + if !have_ffmpeg() { | ||
| 2752 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 2753 | + return; | ||
| 2754 | + } | ||
| 2755 | + if !have_aomenc() { | ||
| 2756 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 2757 | + return; | ||
| 2758 | + } | ||
| 2759 | + let (width, height) = (256usize, 192usize); | ||
| 2760 | + // (arm, source, per-arm overrides AFTER the base recipe, expect_refusal, | ||
| 2761 | + // must_read_use_intrabc) | ||
| 2762 | + let arms: [(&str, &str, &[&str], bool, bool); 3] = [ | ||
| 2763 | + ( | ||
| 2764 | + "testsrc2-screen-nopalette-cq50", | ||
| 2765 | + "testsrc2=s=256x192:r=25", | ||
| 2766 | + &["--cq-level=50", "--enable-palette=0", "--enable-tx-size-search=0"], | ||
| 2767 | + true, | ||
| 2768 | + true, | ||
| 2769 | + ), | ||
| 2770 | + ( | ||
| 2771 | + "smptebars-screen-nopalette-cq45", | ||
| 2772 | + "smptebars=size=256x192:rate=25", | ||
| 2773 | + &["--cq-level=45", "--enable-palette=0", "--enable-tx-size-search=0"], | ||
| 2774 | + false, | ||
| 2775 | + true, | ||
| 2776 | + ), | ||
| 2777 | + ( | ||
| 2778 | + "testsrc2-screen-nopalette-cq63", | ||
| 2779 | + "testsrc2=s=256x192:r=25", | ||
| 2780 | + &["--cq-level=63", "--enable-palette=0", "--enable-tx-size-search=1"], | ||
| 2781 | + false, | ||
| 2782 | + false, | ||
| 2783 | + ), | ||
| 2784 | + ]; | ||
| 2785 | + let (mut reads_total, mut refused, mut frames_compared) = (0usize, 0u32, 0usize); | ||
| 2786 | + for (arm, src, extra, expect_refusal, must_read) in &arms { | ||
| 2787 | + let render = || { | ||
| 2788 | + let out = Command::new("ffmpeg") | ||
| 2789 | + .args([ | ||
| 2790 | + "-v", "error", "-f", "lavfi", "-i", *src, "-t", "0.2", "-pix_fmt", | ||
| 2791 | + "yuv420p", "-strict", "-1", "-f", "yuv4mpegpipe", "-", | ||
| 2792 | + ]) | ||
| 2793 | + .stdin(Stdio::null()) | ||
| 2794 | + .stdout(Stdio::piped()) | ||
| 2795 | + .stderr(Stdio::piped()) | ||
| 2796 | + .output() | ||
| 2797 | + .expect("ffmpeg failed to run"); | ||
| 2798 | + assert!(out.status.success(), "{NAME}: ffmpeg failed for {arm}"); | ||
| 2799 | + out.stdout | ||
| 2800 | + }; | ||
| 2801 | + let y4m_a = render(); | ||
| 2802 | + let y4m_b = render(); | ||
| 2803 | + assert_eq!(y4m_a, y4m_b, "{NAME}: {arm} must render byte-identical across two runs"); | ||
| 2804 | + let y4m = y4m_a; | ||
| 2805 | + let mut child = Command::new(aomenc_path()) | ||
| 2806 | + .args([ | ||
| 2807 | + "--codec=av1", | ||
| 2808 | + "--bit-depth=8", | ||
| 2809 | + "--input-bit-depth=8", | ||
| 2810 | + "--passes=1", | ||
| 2811 | + "--end-usage=q", | ||
| 2812 | + "--cpu-used=0", | ||
| 2813 | + "--lag-in-frames=0", | ||
| 2814 | + "--kf-max-dist=1", | ||
| 2815 | + "--limit=1", | ||
| 2816 | + "--threads=1", | ||
| 2817 | + "--tile-columns=0", | ||
| 2818 | + "--enable-rect-partitions=1", | ||
| 2819 | + "--enable-1to4-partitions=1", | ||
| 2820 | + "--min-partition-size=8", | ||
| 2821 | + "--max-partition-size=32", | ||
| 2822 | + "--sb-size=64", | ||
| 2823 | + // `allow_screen_content_tools` (and with it the frame | ||
| 2824 | + // header's `allow_intrabc` bit) comes from here. | ||
| 2825 | + "--tune-content=screen", | ||
| 2826 | + ]) | ||
| 2827 | + // aomenc keeps the LAST occurrence of a repeated flag. | ||
| 2828 | + .args(*extra) | ||
| 2829 | + .args(["--obu", "-o", "-", "-"]) | ||
| 2830 | + .stdin(Stdio::piped()) | ||
| 2831 | + .stdout(Stdio::piped()) | ||
| 2832 | + .stderr(Stdio::piped()) | ||
| 2833 | + .spawn() | ||
| 2834 | + .expect("aomenc failed to start"); | ||
| 2835 | + child | ||
| 2836 | + .stdin | ||
| 2837 | + .take() | ||
| 2838 | + .expect("aomenc stdin") | ||
| 2839 | + .write_all(&y4m) | ||
| 2840 | + .or_else(|e| match e.kind() { | ||
| 2841 | + std::io::ErrorKind::BrokenPipe => Ok(()), | ||
| 2842 | + _ => Err(e), | ||
| 2843 | + }) | ||
| 2844 | + .expect("writing y4m to aomenc"); | ||
| 2845 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 2846 | + assert!( | ||
| 2847 | + out.status.success(), | ||
| 2848 | + "{NAME}: aomenc refused {arm}: {}", | ||
| 2849 | + String::from_utf8_lossy(&out.stderr) | ||
| 2850 | + ); | ||
| 2851 | + let stream = out.stdout; | ||
| 2852 | + decode::reset_rect_intrabc_reads(); | ||
| 2853 | + let decoded = decode_stream(&stream); | ||
| 2854 | + let reads = decode::rect_intrabc_reads(); | ||
| 2855 | + reads_total += reads; | ||
| 2856 | + if *must_read { | ||
| 2857 | + assert!( | ||
| 2858 | + reads > 0, | ||
| 2859 | + "{NAME}: {arm} read no use_intrabc symbol on a rect strip -- the \ | ||
| 2860 | + arm no longer exercises the fix (allow_intrabc not set?)" | ||
| 2861 | + ); | ||
| 2862 | + } | ||
| 2863 | + match decoded { | ||
| 2864 | + Ok(frames) => { | ||
| 2865 | + assert!( | ||
| 2866 | + !*expect_refusal, | ||
| 2867 | + "{NAME}: {arm} decoded a frame whose rect strips use intrabc -- \ | ||
| 2868 | + reconstruction is not ported, it must refuse by name" | ||
| 2869 | + ); | ||
| 2870 | + assert!(!frames.is_empty(), "{NAME}: {arm} decoded no frame"); | ||
| 2871 | + if *must_read { | ||
| 2872 | + // Arm 1: entropy coverage only (see PIXEL RESIDUE above). | ||
| 2873 | + eprintln!("{NAME}: {arm} decoded {} frame(s), {reads} rect use_intrabc reads", frames.len()); | ||
| 2874 | + continue; | ||
| 2875 | + } | ||
| 2876 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frames.len()); | ||
| 2877 | + assert_eq!(frames.len(), ffmpeg_frames.len(), "{NAME}: {arm} frame count"); | ||
| 2878 | + for (i, (ours, theirs)) in frames.iter().zip(ffmpeg_frames.iter()).enumerate() { | ||
| 2879 | + assert_eq!(ours.y, theirs.y, "{NAME}: {arm} luma, frame {i}"); | ||
| 2880 | + assert_eq!(ours.u, theirs.u, "{NAME}: {arm} U, frame {i}"); | ||
| 2881 | + assert_eq!(ours.v, theirs.v, "{NAME}: {arm} V, frame {i}"); | ||
| 2882 | + frames_compared += 1; | ||
| 2883 | + } | ||
| 2884 | + } | ||
| 2885 | + Err(e) => { | ||
| 2886 | + let msg = e.to_string(); | ||
| 2887 | + assert!(msg.contains("unsupported"), "{NAME}: {arm} decode failed: {e}"); | ||
| 2888 | + assert!( | ||
| 2889 | + *expect_refusal, | ||
| 2890 | + "{NAME}: {arm} was expected to decode, it refused: {e}" | ||
| 2891 | + ); | ||
| 2892 | + assert!( | ||
| 2893 | + msg.contains("intra block copy") || msg.contains("intrabc"), | ||
| 2894 | + "{NAME}: {arm} refused for an unrelated reason: {e}" | ||
| 2895 | + ); | ||
| 2896 | + eprintln!("{NAME}: {arm} REFUSED (expected): {e}"); | ||
| 2897 | + refused += 1; | ||
| 2898 | + } | ||
| 2899 | + } | ||
| 2900 | + } | ||
| 2901 | + eprintln!( | ||
| 2902 | + "{NAME}: {reads_total} rect-strip use_intrabc symbol(s) read, \ | ||
| 2903 | + {refused} refused by name, {frames_compared} frames compared pixel-exact" | ||
| 2904 | + ); | ||
| 2905 | + assert!(reads_total > 0, "{NAME}: gate is vacuous -- no arm read the symbol"); | ||
| 2906 | + assert_eq!(refused, 1, "{NAME}: expected exactly one named intrabc refusal"); | ||
| 2907 | + assert!(frames_compared > 0, "{NAME}: no frame was compared pixel-exact"); | ||
| 2908 | + } | ||
| 2909 | + | ||
| 2716 | /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 | 2910 | /// lane-kf900 r4, the gate for the two lifted palette refusals ("a 1:4 |
| 2717 | /// rect strip that actually uses a palette (reconstruction is not ported | 2911 | /// rect strip that actually uses a palette (reconstruction is not ported |
| 2718 | /// at this shape)" and "a palette block on a HORZ/VERT intra strip below | 2912 | /// at this shape)" and "a palette block on a HORZ/VERT intra strip below |