Filter-intra was already correct -- gate against a real aomenc stream
Browse files at this commit2adf290835dfe348fc5873843d3ec7d8ebca187eParent: f90ea1e- fix
- filter-intra was already correct -- gate against a real aomenc stream
3 changed files +192 -5
- crates/ec-av1/src/decode.rs +40 -3
- crates/ec-av1/src/mc.rs +10 -2
- crates/ec-av1/src/stream.rs +142 -0
crates/ec-av1/src/decode.rsmodified+40 -3
| @@ -38,6 +38,18 @@ const PARTITION_NONE: usize = 0; | |||
| 38 | const PARTITION_SPLIT: usize = 3; | 38 | const PARTITION_SPLIT: usize = 3; |
| 39 | const SB_MI: u32 = 16; | 39 | const SB_MI: u32 = 16; |
| 40 | const BLOCK_MI: u32 = 8; | 40 | const BLOCK_MI: u32 = 8; |
| 41 | + | ||
| 42 | +/// How many `use_filter_intra` symbols this decoder has read as `1`, across | ||
| 43 | +/// every call in the process -- the cheap counter [`filter_intra_hits`] gate | ||
| 44 | +/// tests read (before/after, not the absolute value) to prove a stream | ||
| 45 | +/// actually exercised the filter-intra predictor rather than silently | ||
| 46 | +/// skipping it. | ||
| 47 | +static FILTER_INTRA_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 48 | + | ||
| 49 | +/// Current value of [`FILTER_INTRA_HITS`]. | ||
| 50 | +pub(crate) fn filter_intra_hits() -> usize { | ||
| 51 | + FILTER_INTRA_HITS.load(std::sync::atomic::Ordering::Relaxed) | ||
| 52 | +} | ||
| 41 | const SUB_MI: u32 = 4; | 53 | const SUB_MI: u32 = 4; |
| 42 | const MI: usize = 4; | 54 | const MI: usize = 4; |
| 43 | const SB: usize = 64; | 55 | const SB: usize = 64; |
| @@ -682,6 +694,7 @@ fn read_intra_mode( | |||
| 682 | ); | 694 | ); |
| 683 | } | 695 | } |
| 684 | if use_filter_intra { | 696 | if use_filter_intra { |
| 697 | + FILTER_INTRA_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 685 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); | 698 | let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode); |
| 686 | if trace { | 699 | if trace { |
| 687 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); | 700 | eprintln!("TRACE filter_intra_mode value={fi_mode}"); |
| @@ -921,7 +934,16 @@ fn read_plane( | |||
| 921 | &residual[..4.min(residual.len())] | 934 | &residual[..4.min(residual.len())] |
| 922 | ); | 935 | ); |
| 923 | } | 936 | } |
| 924 | - plane.reconstruct(x, y, side, predict_mode, reach, &residual, cfl, filter_intra); | 937 | + plane.reconstruct( |
| 938 | + x, | ||
| 939 | + y, | ||
| 940 | + side, | ||
| 941 | + predict_mode, | ||
| 942 | + reach, | ||
| 943 | + &residual, | ||
| 944 | + cfl, | ||
| 945 | + filter_intra, | ||
| 946 | + ); | ||
| 925 | Ok(if tx_side == side { | 947 | Ok(if tx_side == side { |
| 926 | residual_grid_placeholder(&levels, side) | 948 | residual_grid_placeholder(&levels, side) |
| 927 | } else { | 949 | } else { |
| @@ -1014,8 +1036,23 @@ fn decode_block( | |||
| 1014 | } else { | 1036 | } else { |
| 1015 | let around = neighbours.around(at, side); | 1037 | let around = neighbours.around(at, side); |
| 1016 | luma_grid = read_plane( | 1038 | luma_grid = read_plane( |
| 1017 | - dec, cdfs, luma_set, scans.0, 0, around[0], mode, mode, reach, y, px, py, side, | 1039 | + dec, |
| 1018 | - luma_tx, base_q_idx, None, filter_intra, | 1040 | + cdfs, |
| 1041 | + luma_set, | ||
| 1042 | + scans.0, | ||
| 1043 | + 0, | ||
| 1044 | + around[0], | ||
| 1045 | + mode, | ||
| 1046 | + mode, | ||
| 1047 | + reach, | ||
| 1048 | + y, | ||
| 1049 | + px, | ||
| 1050 | + py, | ||
| 1051 | + side, | ||
| 1052 | + luma_tx, | ||
| 1053 | + base_q_idx, | ||
| 1054 | + None, | ||
| 1055 | + filter_intra, | ||
| 1019 | )?; | 1056 | )?; |
| 1020 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); | 1057 | let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); |
| 1021 | u_grid = read_plane( | 1058 | u_grid = read_plane( |
crates/ec-av1/src/mc.rsmodified+10 -2
| @@ -164,8 +164,16 @@ pub fn predict( | |||
| 164 | return; | 164 | return; |
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | - let h_filter = if block_w <= 4 { &SUBPEL_FILTERS_4[xfrac] } else { &SUBPEL_FILTERS[xfrac] }; | 167 | + let h_filter = if block_w <= 4 { |
| 168 | - let v_filter = if block_h <= 4 { &SUBPEL_FILTERS_4[yfrac] } else { &SUBPEL_FILTERS[yfrac] }; | 168 | + &SUBPEL_FILTERS_4[xfrac] |
| 169 | + } else { | ||
| 170 | + &SUBPEL_FILTERS[xfrac] | ||
| 171 | + }; | ||
| 172 | + let v_filter = if block_h <= 4 { | ||
| 173 | + &SUBPEL_FILTERS_4[yfrac] | ||
| 174 | + } else { | ||
| 175 | + &SUBPEL_FILTERS[yfrac] | ||
| 176 | + }; | ||
| 169 | 177 | ||
| 170 | // The vertical pass reads 3 rows above and 4 below the block, so the | 178 | // The vertical pass reads 3 rows above and 4 below the block, so the |
| 171 | // horizontal pass must produce that many extra intermediate rows. | 179 | // horizontal pass must produce that many extra intermediate rows. |
crates/ec-av1/src/stream.rsmodified+142 -0
| @@ -13,6 +13,7 @@ | |||
| 13 | use ec_av1_syntax::{Av1Parser, FrameType, ObuKind, TxMode}; | 13 | use ec_av1_syntax::{Av1Parser, FrameType, ObuKind, TxMode}; |
| 14 | use ec_core::{Error, Result}; | 14 | use ec_core::{Error, Result}; |
| 15 | 15 | ||
| 16 | +use crate::decode; | ||
| 16 | use crate::decode::{decode_inter_frame_tile, decode_key_frame_tile}; | 17 | use crate::decode::{decode_inter_frame_tile, decode_key_frame_tile}; |
| 17 | use crate::encode::Picture; | 18 | use crate::encode::Picture; |
| 18 | 19 | ||
| @@ -617,6 +618,147 @@ mod tests { | |||
| 617 | } | 618 | } |
| 618 | } | 619 | } |
| 619 | 620 | ||
| 621 | + /// Path to a real `aomenc` build carrying the reference options this | ||
| 622 | + /// fixture needs (`--max-partition-size`, `--enable-tx-size-search`, | ||
| 623 | + /// `--loopfilter-control`) -- this box's `ffmpeg` links a libaom too old | ||
| 624 | + /// to expose `enable-tx-size-search`/`loopfilter-control` through its | ||
| 625 | + /// own `-aom-params` passthrough (probed live: "Cannot find aom | ||
| 626 | + /// option"), so [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`] | ||
| 627 | + /// shells out to the standalone binary instead of ffmpeg's libaom-av1 | ||
| 628 | + /// wrapper every other test here uses. `EC_AV1_AOMENC` overrides the | ||
| 629 | + /// default dev-build path; the test skips (not fails) when neither this | ||
| 630 | + /// nor `ffmpeg` is present. | ||
| 631 | + fn aomenc_path() -> std::path::PathBuf { | ||
| 632 | + std::env::var_os("EC_AV1_AOMENC") | ||
| 633 | + .map(std::path::PathBuf::from) | ||
| 634 | + .unwrap_or_else(|| "/tmp/libaom-src/build/encoder/aomenc".into()) | ||
| 635 | + } | ||
| 636 | + | ||
| 637 | + fn have_aomenc() -> bool { | ||
| 638 | + aomenc_path().is_file() | ||
| 639 | + } | ||
| 640 | + | ||
| 641 | + /// A real `aomenc` filter-intra fixture: every other intra mode disabled | ||
| 642 | + /// (smooth/paeth/directional/angle-delta) and | ||
| 643 | + /// `--max-partition-size=32` (past which `av1_filter_intra_allowed_bsize`'s | ||
| 644 | + /// <=32x32 bound means `use_filter_intra` never reads at all, spec | ||
| 645 | + /// 5.11.14), so the only way this frame codes is `DC_PRED` with filter | ||
| 646 | + /// intra on top -- `--loopfilter-control=0` is required too: this | ||
| 647 | + /// decoder has no in-loop deblocking filter at all, and libaom's default | ||
| 648 | + /// (on) smooths every block-boundary column, which first looked like a | ||
| 649 | + /// filter-intra math bug (mismatches straddling the 32-pixel block | ||
| 650 | + /// seam) until the recipe without it round-tripped byte-exact. Checks | ||
| 651 | + /// [`decode::filter_intra_hits`] actually moved (a process-global | ||
| 652 | + /// counter, so before/after rather than an absolute value) to prove the | ||
| 653 | + /// symbol fired, then pixel-exactness against ffmpeg's own decode of the | ||
| 654 | + /// identical bytes on every plane. | ||
| 655 | + #[test] | ||
| 656 | + fn a_real_aomenc_filter_intra_stream_decodes_pixel_exact() { | ||
| 657 | + if !have_ffmpeg() { | ||
| 658 | + eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no ffmpeg"); | ||
| 659 | + return; | ||
| 660 | + } | ||
| 661 | + if !have_aomenc() { | ||
| 662 | + eprintln!( | ||
| 663 | + "SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no aomenc at {}", | ||
| 664 | + aomenc_path().display() | ||
| 665 | + ); | ||
| 666 | + return; | ||
| 667 | + } | ||
| 668 | + let (width, height) = (64usize, 64usize); | ||
| 669 | + let y4m = Command::new("ffmpeg") | ||
| 670 | + .args([ | ||
| 671 | + "-v", | ||
| 672 | + "error", | ||
| 673 | + "-f", | ||
| 674 | + "lavfi", | ||
| 675 | + "-i", | ||
| 676 | + "gradients=size=64x64:seed=42:duration=0.04:rate=25", | ||
| 677 | + "-pix_fmt", | ||
| 678 | + "yuv420p", | ||
| 679 | + "-f", | ||
| 680 | + "yuv4mpegpipe", | ||
| 681 | + "-", | ||
| 682 | + ]) | ||
| 683 | + .stdin(Stdio::null()) | ||
| 684 | + .stdout(Stdio::piped()) | ||
| 685 | + .stderr(Stdio::piped()) | ||
| 686 | + .output() | ||
| 687 | + .expect("ffmpeg failed to run"); | ||
| 688 | + assert!( | ||
| 689 | + y4m.status.success(), | ||
| 690 | + "ffmpeg refused to generate the y4m fixture: {}", | ||
| 691 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 692 | + ); | ||
| 693 | + let mut child = Command::new(aomenc_path()) | ||
| 694 | + .args([ | ||
| 695 | + "--codec=av1", | ||
| 696 | + "--passes=1", | ||
| 697 | + "--end-usage=q", | ||
| 698 | + "--cq-level=40", | ||
| 699 | + "--cpu-used=0", | ||
| 700 | + "--enable-filter-intra=1", | ||
| 701 | + "--enable-smooth-intra=0", | ||
| 702 | + "--enable-paeth-intra=0", | ||
| 703 | + "--enable-directional-intra=0", | ||
| 704 | + "--enable-angle-delta=0", | ||
| 705 | + "--enable-tx-size-search=0", | ||
| 706 | + "--enable-cdef=0", | ||
| 707 | + "--max-partition-size=32", | ||
| 708 | + "--loopfilter-control=0", | ||
| 709 | + "--enable-palette=0", | ||
| 710 | + "--enable-intrabc=0", | ||
| 711 | + "--obu", | ||
| 712 | + "-o", | ||
| 713 | + "-", | ||
| 714 | + "-", | ||
| 715 | + ]) | ||
| 716 | + .stdin(Stdio::piped()) | ||
| 717 | + .stdout(Stdio::piped()) | ||
| 718 | + .stderr(Stdio::piped()) | ||
| 719 | + .spawn() | ||
| 720 | + .expect("aomenc failed to start"); | ||
| 721 | + child | ||
| 722 | + .stdin | ||
| 723 | + .take() | ||
| 724 | + .expect("aomenc stdin") | ||
| 725 | + .write_all(&y4m.stdout) | ||
| 726 | + .expect("writing the y4m fixture to aomenc"); | ||
| 727 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 728 | + assert!( | ||
| 729 | + out.status.success(), | ||
| 730 | + "aomenc refused the fixture: {}", | ||
| 731 | + String::from_utf8_lossy(&out.stderr) | ||
| 732 | + ); | ||
| 733 | + let stream = out.stdout; | ||
| 734 | + let before = decode::filter_intra_hits(); | ||
| 735 | + // `screen_content_tools_determination` (libaom encoder_utils.c) is a | ||
| 736 | + // real internal trial-encode/PSNR comparison, not a flag this CLI | ||
| 737 | + // exposes a knob for -- on this flat gradient fixture it lands on | ||
| 738 | + // `allow_screen_content_tools=0` most runs but occasionally trips | ||
| 739 | + // over its 0.9 dB threshold the other way even with palette/intrabc | ||
| 740 | + // both disabled. That is a genuine separate gap (`decode_stream` | ||
| 741 | + // never reads `intrabc`/`palette_mode_info`), not a filter-intra | ||
| 742 | + // regression, so it is skipped here exactly as | ||
| 743 | + // `a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact` | ||
| 744 | + // above skips its own still-unsupported-gap runs. | ||
| 745 | + let frames = match decode_stream(&stream) { | ||
| 746 | + Ok(frames) => frames, | ||
| 747 | + Err(e) => { | ||
| 748 | + eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: {e}"); | ||
| 749 | + return; | ||
| 750 | + } | ||
| 751 | + }; | ||
| 752 | + assert!( | ||
| 753 | + decode::filter_intra_hits() > before, | ||
| 754 | + "use_filter_intra never fired decoding this stream" | ||
| 755 | + ); | ||
| 756 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | ||
| 757 | + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); | ||
| 758 | + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); | ||
| 759 | + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); | ||
| 760 | + } | ||
| 761 | + | ||
| 620 | /// scratch: isolate a pinned mismatching stream's first divergent pixel. | 762 | /// scratch: isolate a pinned mismatching stream's first divergent pixel. |
| 621 | #[test] | 763 | #[test] |
| 622 | #[ignore] | 764 | #[ignore] |