Gate the V_DCT/H_DCT class-1 path on real aomenc bytes
Browse files at this commit514765196a3d6f7e584453fe8e9cb9075b06a97eParent: ea55462- feat
- gate the V_DCT/H_DCT class-1 path on real aomenc bytes
2 changed files +196 -2
- crates/ec-av1/src/decode.rs +40 -2
- crates/ec-av1/src/stream.rs +156 -0
crates/ec-av1/src/decode.rsmodified+40 -2
| @@ -74,6 +74,19 @@ static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::Atomic | |||
| 74 | pub(crate) fn tx_depth_hits() -> usize { | 74 | pub(crate) fn tx_depth_hits() -> usize { |
| 75 | TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed) | 75 | TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed) |
| 76 | } | 76 | } |
| 77 | + | ||
| 78 | +/// How many [`read_coeffs`] reads resolved a `tx_type` outside `TX_CLASS_2D` | ||
| 79 | +/// (`V_DCT`/`H_DCT`, spec 5.11.39's `TxClass::Horiz`/`Vert`), across every | ||
| 80 | +/// call in the process -- the same before/after counter pattern as | ||
| 81 | +/// [`TX_DEPTH_HITS`], proving a stream actually exercised the class-split | ||
| 82 | +/// `eob_pt`/1D-neighbour context path rather than every block coincidentally | ||
| 83 | +/// landing on `DCT_DCT`. | ||
| 84 | +static TX_CLASS1_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 85 | + | ||
| 86 | +/// Current value of [`TX_CLASS1_HITS`]. | ||
| 87 | +pub(crate) fn tx_class1_hits() -> usize { | ||
| 88 | + TX_CLASS1_HITS.load(std::sync::atomic::Ordering::Relaxed) | ||
| 89 | +} | ||
| 77 | const SUB_MI: u32 = 4; | 90 | const SUB_MI: u32 = 4; |
| 78 | const MI: usize = 4; | 91 | const MI: usize = 4; |
| 79 | const SB: usize = 64; | 92 | const SB: usize = 64; |
| @@ -225,7 +238,15 @@ fn nz_map_ctx_offset_1d(i: usize) -> usize { | |||
| 225 | } | 238 | } |
| 226 | 239 | ||
| 227 | fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { | 240 | fn base_ctx(grid: &[i32], side: usize, row: usize, col: usize, class: TxClass) -> usize { |
| 228 | - if row == 0 && col == 0 { | 241 | + // libaom's `get_nz_map_ctx_from_stats` only special-cases the DC |
| 242 | + // coefficient to ctx 0 for `TX_CLASS_2D` (`(tx_class | coeff_idx) == 0`, | ||
| 243 | + // txb_common.h) -- `V_DCT`/`H_DCT` (class `Horiz`/`Vert`) still run the DC | ||
| 244 | + // position through the full 1D neighbour sum and offset table. Skipping | ||
| 245 | + // that for every class desynced the last (DC) coefficient of a | ||
| 246 | + // `TX_CLASS_HORIZ` block (lane-av1tx4 final round: real ctx 28 read as | ||
| 247 | + // 0, which then read one extra `BR` symbol at the max and forced a | ||
| 248 | + // spurious third Golomb call). | ||
| 249 | + if class == TxClass::TwoD && row == 0 && col == 0 { | ||
| 229 | return 0; | 250 | return 0; |
| 230 | } | 251 | } |
| 231 | let offsets: [(usize, usize); 5] = match class { | 252 | let offsets: [(usize, usize); 5] = match class { |
| @@ -383,6 +404,9 @@ fn read_coeffs( | |||
| 383 | } | 404 | } |
| 384 | 405 | ||
| 385 | let class = TxClass::of(tx_type); | 406 | let class = TxClass::of(tx_type); |
| 407 | + if class != TxClass::TwoD { | ||
| 408 | + TX_CLASS1_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 409 | + } | ||
| 386 | let eob = read_eob(dec, coding, class); | 410 | let eob = read_eob(dec, coding, class); |
| 387 | if trace { | 411 | if trace { |
| 388 | eprintln!("TRACE eob value={eob}"); | 412 | eprintln!("TRACE eob value={eob}"); |
| @@ -2814,7 +2838,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 2814 | let part32 = if has_cols32 && has_rows32 { | 2838 | let part32 = if has_cols32 && has_rows32 { |
| 2815 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); | 2839 | let p = dec.symbol(&mut cdfs.partition_w32[ctx32]); |
| 2816 | if std::env::var_os("EC_AV1_TRACE").is_some() { | 2840 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 2817 | - eprintln!("TRACE partition_w32 ctx={ctx32} value={p}"); | 2841 | + eprintln!( |
| 2842 | + "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p}", | ||
| 2843 | + r32 * BLOCK_MI, | ||
| 2844 | + c32 * BLOCK_MI | ||
| 2845 | + ); | ||
| 2818 | } | 2846 | } |
| 2819 | p | 2847 | p |
| 2820 | } else { | 2848 | } else { |
| @@ -2934,6 +2962,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 2934 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); | 2962 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 2935 | let part8 = | 2963 | let part8 = |
| 2936 | dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); | 2964 | dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 2965 | + if std::env::var_os("EC_AV1_TRACE").is_some() { | ||
| 2966 | + eprintln!( | ||
| 2967 | + "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" | ||
| 2968 | + ); | ||
| 2969 | + } | ||
| 2937 | if part8 != PARTITION_NONE { | 2970 | if part8 != PARTITION_NONE { |
| 2938 | return Err(unsupported( | 2971 | return Err(unsupported( |
| 2939 | "a partition below 8x8 (this encoder never writes one)", | 2972 | "a partition below 8x8 (this encoder never writes one)", |
| @@ -2998,6 +3031,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | |||
| 2998 | let leaf_mi = (mr as usize, mc as usize); | 3031 | let leaf_mi = (mr as usize, mc as usize); |
| 2999 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); | 3032 | let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8); |
| 3000 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); | 3033 | let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]); |
| 3034 | + if std::env::var_os("EC_AV1_TRACE").is_some() { | ||
| 3035 | + eprintln!( | ||
| 3036 | + "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}" | ||
| 3037 | + ); | ||
| 3038 | + } | ||
| 3001 | if part8 != PARTITION_NONE { | 3039 | if part8 != PARTITION_NONE { |
| 3002 | return Err(unsupported( | 3040 | return Err(unsupported( |
| 3003 | "a partition below 8x8 (this encoder never writes one)", | 3041 | "a partition below 8x8 (this encoder never writes one)", |
crates/ec-av1/src/stream.rsmodified+156 -0
| @@ -1551,4 +1551,160 @@ mod tests { | |||
| 1551 | } | 1551 | } |
| 1552 | } | 1552 | } |
| 1553 | } | 1553 | } |
| 1554 | + | ||
| 1555 | + /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8 | ||
| 1556 | + /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec | ||
| 1557 | + /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame | ||
| 1558 | + /// never triggers), so a real `aomenc` stream from this recipe can only | ||
| 1559 | + /// legitimately read `PARTITION_NONE` at every `bsize=3` node -- any | ||
| 1560 | + /// other value this decoder sees there is a genuine desync, not a | ||
| 1561 | + /// "partition below 8x8" the encoder actually wrote (traced live against | ||
| 1562 | + /// a debug `aomdec` 2026-08-27: the true bitstream's own 8x8 partition | ||
| 1563 | + /// reads are all `NONE`; the mismatch was `base_ctx`'s DC-position | ||
| 1564 | + /// short-circuit firing for `TX_CLASS_HORIZ`/`VERT` too, off by one | ||
| 1565 | + /// `BR` read into a spurious extra Golomb call). The sinusoidal-stripe | ||
| 1566 | + /// `geq` source (a smooth luma ramp, not the sharp two-tone stripes that | ||
| 1567 | + /// tripped `aomenc`'s screen-content-tools auto-detect) is what actually | ||
| 1568 | + /// resolves `V_DCT`/`H_DCT` (`TxClass::of`) on some 8x8 TUs at this | ||
| 1569 | + /// content's frequency; the attempt loop moves on from named refusals | ||
| 1570 | + /// (`allow_screen_content_tools`, angle-delta) same as every other | ||
| 1571 | + /// multi-seed recipe in this file, and only counts a run as a genuine | ||
| 1572 | + /// firing when [`decode::tx_class1_hits`] actually moved. | ||
| 1573 | + #[test] | ||
| 1574 | + fn a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact() { | ||
| 1575 | + if !have_ffmpeg() { | ||
| 1576 | + eprintln!( | ||
| 1577 | + "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no ffmpeg" | ||
| 1578 | + ); | ||
| 1579 | + return; | ||
| 1580 | + } | ||
| 1581 | + if !have_aomenc() { | ||
| 1582 | + eprintln!( | ||
| 1583 | + "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no aomenc at {}", | ||
| 1584 | + aomenc_path().display() | ||
| 1585 | + ); | ||
| 1586 | + return; | ||
| 1587 | + } | ||
| 1588 | + let (width, height) = (64usize, 64usize); | ||
| 1589 | + let mut refusals = Vec::new(); | ||
| 1590 | + let mut fired_runs = 0u32; | ||
| 1591 | + for attempt in 0..60u32 { | ||
| 1592 | + let cq = 20 + (attempt % 5) * 10; | ||
| 1593 | + let period = [4u32, 6, 8, 12, 16][(attempt as usize / 5) % 5]; | ||
| 1594 | + let y4m = Command::new("ffmpeg") | ||
| 1595 | + .args([ | ||
| 1596 | + "-v", | ||
| 1597 | + "error", | ||
| 1598 | + "-f", | ||
| 1599 | + "lavfi", | ||
| 1600 | + "-i", | ||
| 1601 | + "testsrc2=size=64x64:rate=25", | ||
| 1602 | + "-vf", | ||
| 1603 | + &format!( | ||
| 1604 | + "geq=lum='128+80*sin(2*PI*Y/{period})':cb=128:cr=128,noise=alls=6:allf=t" | ||
| 1605 | + ), | ||
| 1606 | + "-pix_fmt", | ||
| 1607 | + "yuv420p", | ||
| 1608 | + "-t", | ||
| 1609 | + "0.04", | ||
| 1610 | + "-f", | ||
| 1611 | + "yuv4mpegpipe", | ||
| 1612 | + "-", | ||
| 1613 | + ]) | ||
| 1614 | + .stdin(Stdio::null()) | ||
| 1615 | + .stdout(Stdio::piped()) | ||
| 1616 | + .stderr(Stdio::piped()) | ||
| 1617 | + .output() | ||
| 1618 | + .expect("ffmpeg failed to run"); | ||
| 1619 | + assert!( | ||
| 1620 | + y4m.status.success(), | ||
| 1621 | + "ffmpeg fixture: {}", | ||
| 1622 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 1623 | + ); | ||
| 1624 | + let mut child = Command::new(aomenc_path()) | ||
| 1625 | + .args([ | ||
| 1626 | + "--codec=av1", | ||
| 1627 | + "--passes=1", | ||
| 1628 | + "--end-usage=q", | ||
| 1629 | + &format!("--cq-level={cq}"), | ||
| 1630 | + "--cpu-used=0", | ||
| 1631 | + "--enable-directional-intra=0", | ||
| 1632 | + "--enable-smooth-intra=0", | ||
| 1633 | + "--enable-paeth-intra=0", | ||
| 1634 | + "--enable-rect-partitions=0", | ||
| 1635 | + "--enable-angle-delta=0", | ||
| 1636 | + "--enable-palette=0", | ||
| 1637 | + "--reduced-tx-type-set=0", | ||
| 1638 | + "--enable-restoration=0", | ||
| 1639 | + "--enable-cdef=0", | ||
| 1640 | + "--loopfilter-control=0", | ||
| 1641 | + "--min-partition-size=8", | ||
| 1642 | + "--max-partition-size=8", | ||
| 1643 | + "--enable-ab-partitions=0", | ||
| 1644 | + "--enable-1to4-partitions=0", | ||
| 1645 | + "--enable-filter-intra=0", | ||
| 1646 | + "--enable-intrabc=0", | ||
| 1647 | + "--enable-cfl-intra=0", | ||
| 1648 | + "--tune-content=default", | ||
| 1649 | + "--obu", | ||
| 1650 | + "-o", | ||
| 1651 | + "-", | ||
| 1652 | + "-", | ||
| 1653 | + ]) | ||
| 1654 | + .stdin(Stdio::piped()) | ||
| 1655 | + .stdout(Stdio::piped()) | ||
| 1656 | + .stderr(Stdio::piped()) | ||
| 1657 | + .spawn() | ||
| 1658 | + .expect("aomenc failed to start"); | ||
| 1659 | + child | ||
| 1660 | + .stdin | ||
| 1661 | + .take() | ||
| 1662 | + .expect("aomenc stdin") | ||
| 1663 | + .write_all(&y4m.stdout) | ||
| 1664 | + .expect("writing y4m to aomenc"); | ||
| 1665 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 1666 | + if !out.status.success() { | ||
| 1667 | + refusals.push(format!( | ||
| 1668 | + "cq={cq} period={period}: aomenc itself refused the fixture" | ||
| 1669 | + )); | ||
| 1670 | + continue; | ||
| 1671 | + } | ||
| 1672 | + let stream = out.stdout; | ||
| 1673 | + let before = decode::tx_class1_hits(); | ||
| 1674 | + let frames = match decode_stream(&stream) { | ||
| 1675 | + Ok(frames) => frames, | ||
| 1676 | + Err(e) => { | ||
| 1677 | + refusals.push(format!("cq={cq} period={period}: {e}")); | ||
| 1678 | + continue; | ||
| 1679 | + } | ||
| 1680 | + }; | ||
| 1681 | + if decode::tx_class1_hits() == before { | ||
| 1682 | + refusals.push(format!( | ||
| 1683 | + "cq={cq} period={period}: decoded, but no block read a V_DCT/H_DCT tx_type" | ||
| 1684 | + )); | ||
| 1685 | + continue; | ||
| 1686 | + } | ||
| 1687 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | ||
| 1688 | + assert_eq!( | ||
| 1689 | + frames[0].y, ffmpeg_frames[0].y, | ||
| 1690 | + "luma vs ffmpeg (cq={cq} period={period})" | ||
| 1691 | + ); | ||
| 1692 | + assert_eq!( | ||
| 1693 | + frames[0].u, ffmpeg_frames[0].u, | ||
| 1694 | + "U vs ffmpeg (cq={cq} period={period})" | ||
| 1695 | + ); | ||
| 1696 | + assert_eq!( | ||
| 1697 | + frames[0].v, ffmpeg_frames[0].v, | ||
| 1698 | + "V vs ffmpeg (cq={cq} period={period})" | ||
| 1699 | + ); | ||
| 1700 | + fired_runs += 1; | ||
| 1701 | + if fired_runs >= 4 { | ||
| 1702 | + return; | ||
| 1703 | + } | ||
| 1704 | + } | ||
| 1705 | + panic!( | ||
| 1706 | + "fewer than 4 firing+pixel-exact runs out of 60 attempts:\n{}", | ||
| 1707 | + refusals.join("\n") | ||
| 1708 | + ); | ||
| 1709 | + } | ||
| 1554 | } | 1710 | } |