Thread read_motion_mode/OBMC through decode_inter_block8's 8x8 leaves
Browse files at this commit1944bfcc673fef016d0af84f497536f7b8282750Parent: c0d4024- feat
- thread read_motion_mode/OBMC through decode_inter_block8's 8x8 leaves
2 changed files +254 -4
- crates/ec-av1/src/decode.rs +80 -4
- crates/ec-av1/src/stream.rs +174 -0
crates/ec-av1/src/decode.rsmodified+80 -4
| @@ -165,6 +165,17 @@ pub(crate) fn obmc_hits() -> usize { | ||
| 165 | 165 | OBMC_HITS.load(std::sync::atomic::Ordering::Relaxed) |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | +/// lane-motionmode round 3: same proof as [`OBMC_HITS`], narrowed to | |
| 169 | +/// `decode_inter_block8`'s own 8x8-leaf `read_motion_mode` -- a subset of | |
| 170 | +/// [`OBMC_HITS`] (both fire together on an 8x8 hit), lets the gate tell an | |
| 171 | +/// 8x8-leaf OBMC block apart from a 16x16+ one. | |
| 172 | +static OBMC_HITS_8: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 173 | + | |
| 174 | +/// Current value of [`OBMC_HITS_8`]. | |
| 175 | +pub(crate) fn obmc_hits_8() -> usize { | |
| 176 | + OBMC_HITS_8.load(std::sync::atomic::Ordering::Relaxed) | |
| 177 | +} | |
| 178 | + | |
| 168 | 179 | /// How many [`read_inter_compound_mode`] reads actually happened |
| 169 | 180 | /// (lane-av1comp) -- proves a `COMPOUND_REFERENCE` block reached its own |
| 170 | 181 | /// `compound_mode` symbol (past `comp_mode`/`comp_ref` and the compound |
| @@ -5761,6 +5772,16 @@ fn decode_inter_block8( | ||
| 5761 | 5772 | // lane-cdffwd2: this frame header's own `reduced_tx_set` bit -- see |
| 5762 | 5773 | // [`decode_inter_frame_tile_with_cdfs`]'s own doc. |
| 5763 | 5774 | reduced_tx_set: bool, |
| 5775 | + // lane-motionmode round 3: this frame header's own `is_motion_mode_switchable`/ | |
| 5776 | + // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`), plus | |
| 5777 | + // the fixed/switchable interp filter kind `obmc_blend`'s own neighbour | |
| 5778 | + // reads need (see [`decode_inter_block`]'s own doc) -- this leaf's own | |
| 5779 | + // prediction is always `Regular` (documented corner-cut, `mc::predict` | |
| 5780 | + // has no filter param), but an OBMC neighbour can be a real 16x16+ | |
| 5781 | + // block that DID resolve a switchable filter. | |
| 5782 | + interp_fixed: Option<mc::InterpFilterKind>, | |
| 5783 | + switchable_motion_mode: bool, | |
| 5784 | + allow_warped_motion: bool, | |
| 5764 | 5785 | // lane-av1comp: `Some((ref1, comp_group_idx, compound_idx))` only for a |
| 5765 | 5786 | // real `COMPOUND_REFERENCE` leaf, for the caller's own end-of-16x16 |
| 5766 | 5787 | // `record_compound_ctx` call -- mirrors [`decode_inter_block`]'s own |
| @@ -6208,6 +6229,33 @@ fn decode_inter_block8( | ||
| 6208 | 6229 | }; |
| 6209 | 6230 | (mv, false) |
| 6210 | 6231 | }; |
| 6232 | + // lane-motionmode round 3: `read_motion_mode` (spec 5.11.24) at the | |
| 6233 | + // 8x8 leaf too -- `motion_mode_allowed` still holds here (BLOCK_8X8 | |
| 6234 | + // is the spec's own minimum eligible size, single-ref-only branch, | |
| 6235 | + // no interintra reader, `IDENTITY`-only global motion). Mirrors the | |
| 6236 | + // 16x16 leaf's own `decode_inter_block` read (see its doc above) -- | |
| 6237 | + // same 2-symbol `obmc_cdf` alphabet, same `allow_warped_motion=1` | |
| 6238 | + // refusal (warp needs `av1_findSamples`, not ported at either leaf). | |
| 6239 | + let motion_mode_eligible = switchable_motion_mode | |
| 6240 | + && !skip_mode | |
| 6241 | + && (!overlappable_above(grid, mi_row, mi_col, 2, mi_cols as usize, 1).is_empty() | |
| 6242 | + || !overlappable_left(grid, mi_row, mi_col, 2, mi_rows as usize, 1).is_empty()); | |
| 6243 | + let mut obmc_selected = false; | |
| 6244 | + if motion_mode_eligible { | |
| 6245 | + if allow_warped_motion { | |
| 6246 | + return Err(unsupported( | |
| 6247 | + "a motion-variation-eligible 8x8 leaf under allow_warped_motion=1 \ | |
| 6248 | + (round 3 only reads the 2-symbol obmc_cdf; telling OBMC_CAUSAL \ | |
| 6249 | + from WARPED_CAUSAL needs av1_findSamples, not ported)", | |
| 6250 | + )); | |
| 6251 | + } | |
| 6252 | + // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3. | |
| 6253 | + obmc_selected = dec.symbol(&mut cdfs.obmc[0]) == 1; | |
| 6254 | + if obmc_selected { | |
| 6255 | + OBMC_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 6256 | + OBMC_HITS_8.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 6257 | + } | |
| 6258 | + } | |
| 6211 | 6259 | for dr in 0..2 { |
| 6212 | 6260 | for dc in 0..2 { |
| 6213 | 6261 | grid.set( |
| @@ -6264,6 +6312,32 @@ fn decode_inter_block8( | ||
| 6264 | 6312 | &mut pred_v, |
| 6265 | 6313 | ); |
| 6266 | 6314 | |
| 6315 | + if obmc_selected { | |
| 6316 | + obmc_blend( | |
| 6317 | + grid, | |
| 6318 | + neighbours, | |
| 6319 | + mi_row, | |
| 6320 | + mi_col, | |
| 6321 | + 2, | |
| 6322 | + mi_rows as usize, | |
| 6323 | + mi_cols as usize, | |
| 6324 | + SIDE, | |
| 6325 | + CHROMA_SIDE, | |
| 6326 | + px, | |
| 6327 | + py, | |
| 6328 | + cpx, | |
| 6329 | + cpy, | |
| 6330 | + ref_y, | |
| 6331 | + ref_u, | |
| 6332 | + ref_v, | |
| 6333 | + other_refs, | |
| 6334 | + interp_fixed, | |
| 6335 | + &mut pred_y, | |
| 6336 | + &mut pred_u, | |
| 6337 | + &mut pred_v, | |
| 6338 | + )?; | |
| 6339 | + } | |
| 6340 | + | |
| 6267 | 6341 | if skip { |
| 6268 | 6342 | y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE]); |
| 6269 | 6343 | u.reconstruct_mc( |
| @@ -6599,10 +6673,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 6599 | 6673 | // `Set1` coefficient tables at 16x16/8x8 rather than the reduced |
| 6600 | 6674 | // 2-symbol ones -- see [`TxbSet::Luma16InterSet1`]/[`TxbSet::Luma8InterSet1`]). |
| 6601 | 6675 | reduced_tx_set: bool, |
| 6602 | - // lane-motionmode round 1: this frame header's own `is_motion_mode_switchable`/ | |
| 6603 | - // `allow_warped_motion` bits, threaded to every `decode_inter_block` call | |
| 6604 | - // below (spec 5.11.24's `read_motion_mode`) -- `decode_inter_block8`'s 8x8 | |
| 6605 | - // leaves do not read this symbol yet (round 2). | |
| 6676 | + // lane-motionmode round 1/3: this frame header's own `is_motion_mode_switchable`/ | |
| 6677 | + // `allow_warped_motion` bits, threaded to every `decode_inter_block`/ | |
| 6678 | + // `decode_inter_block8` call below (spec 5.11.24's `read_motion_mode`). | |
| 6606 | 6679 | switchable_motion_mode: bool, |
| 6607 | 6680 | allow_warped_motion: bool, |
| 6608 | 6681 | ) -> Result<(Picture, Cdfs, crate::motion_field::MotionField)> { |
| @@ -6945,6 +7018,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 6945 | 7018 | skip_mode_present, |
| 6946 | 7019 | skip_mode_frame, |
| 6947 | 7020 | reduced_tx_set, |
| 7021 | + interp_fixed, | |
| 7022 | + switchable_motion_mode, | |
| 7023 | + allow_warped_motion, | |
| 6948 | 7024 | )?; |
| 6949 | 7025 | prev_leaf = Some((leaf_mi, skip, is_inter)); |
| 6950 | 7026 | last_skip_mode = skip_mode_leaf; |
crates/ec-av1/src/stream.rsmodified+174 -0
| @@ -3062,6 +3062,180 @@ mod tests { | ||
| 3062 | 3062 | ); |
| 3063 | 3063 | } |
| 3064 | 3064 | |
| 3065 | + /// lane-motionmode round 3: same shape as | |
| 3066 | + /// `a_real_aomenc_stream_with_obmc_decodes_pixel_exact`, but relaxed the | |
| 3067 | + /// `--min-partition-size=32 --max-partition-size=32` clamp (that other | |
| 3068 | + /// gate's own doc: locked to remove partition-depth choice from the | |
| 3069 | + /// search entirely) to `8`/`32`, to make an 8x8-leaf `OBMC_CAUSAL` pick | |
| 3070 | + /// possible at all. Gated on [`decode::obmc_hits_8`] specifically, not | |
| 3071 | + /// the aggregate [`decode::obmc_hits`]. Measured live (round 3, this | |
| 3072 | + /// box): 80 attempts, 0 ever called `decode_inter_block8` at all -- | |
| 3073 | + /// `cpu-used=0`'s RD search never once splits a 16x16 down to a clean | |
| 3074 | + /// 4-way 8x8 leaf for this smooth `gradients` fixture (large blocks | |
| 3075 | + /// always win the RD cost there); the handful of named refusals seen | |
| 3076 | + /// are `PARTITION_SPLIT` chosen *at* 8x8 (down to 4x4, already refused | |
| 3077 | + /// at `decode.rs`'s `part8 != PARTITION_NONE` check) or | |
| 3078 | + /// `allow_screen_content_tools`, neither reaching a leaf. Kept anyway | |
| 3079 | + /// (mirrors this file's own `never_fired`-skip convention) as the | |
| 3080 | + /// honest record of the attempt; a real 8x8 OBMC fixture needs | |
| 3081 | + /// higher-frequency content than this gate's fixtures use, out of | |
| 3082 | + /// round-3 budget. | |
| 3083 | + #[test] | |
| 3084 | + fn a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact() { | |
| 3085 | + const NAME: &str = "a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact"; | |
| 3086 | + let _gate_lock = lock_gate_counters(); | |
| 3087 | + if !have_ffmpeg() { | |
| 3088 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 3089 | + return; | |
| 3090 | + } | |
| 3091 | + if !have_aomenc() { | |
| 3092 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | |
| 3093 | + return; | |
| 3094 | + } | |
| 3095 | + let (width, height, frame_count) = (64usize, 64usize, 24usize); | |
| 3096 | + let mut refusals = Vec::new(); | |
| 3097 | + let mut never_fired = 0u32; | |
| 3098 | + let mut total_obmc8 = 0usize; | |
| 3099 | + for attempt in 0..80u32 { | |
| 3100 | + let seed = 42 + attempt % 40; | |
| 3101 | + let duration = frame_count as f64 / 25.0; | |
| 3102 | + let source = | |
| 3103 | + format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); | |
| 3104 | + let y4m = Command::new("ffmpeg") | |
| 3105 | + .args([ | |
| 3106 | + "-v", | |
| 3107 | + "error", | |
| 3108 | + "-f", | |
| 3109 | + "lavfi", | |
| 3110 | + "-i", | |
| 3111 | + &source, | |
| 3112 | + "-pix_fmt", | |
| 3113 | + "yuv420p", | |
| 3114 | + "-f", | |
| 3115 | + "yuv4mpegpipe", | |
| 3116 | + "-", | |
| 3117 | + ]) | |
| 3118 | + .stdin(Stdio::null()) | |
| 3119 | + .stdout(Stdio::piped()) | |
| 3120 | + .stderr(Stdio::piped()) | |
| 3121 | + .output() | |
| 3122 | + .expect("ffmpeg failed to run"); | |
| 3123 | + assert!( | |
| 3124 | + y4m.status.success(), | |
| 3125 | + "ffmpeg fixture: {}", | |
| 3126 | + String::from_utf8_lossy(&y4m.stderr) | |
| 3127 | + ); | |
| 3128 | + let args: Vec<&str> = vec![ | |
| 3129 | + "--codec=av1", | |
| 3130 | + "--passes=1", | |
| 3131 | + "--end-usage=q", | |
| 3132 | + "--cq-level=45", | |
| 3133 | + "--cpu-used=0", | |
| 3134 | + "--kf-max-dist=1000", | |
| 3135 | + "--threads=1", | |
| 3136 | + "--row-mt=0", | |
| 3137 | + "--auto-alt-ref=1", | |
| 3138 | + "--lag-in-frames=16", | |
| 3139 | + "--enable-fwd-kf=0", | |
| 3140 | + "--enable-order-hint=1", | |
| 3141 | + "--enable-warped-motion=0", | |
| 3142 | + "--enable-obmc=1", | |
| 3143 | + "--enable-masked-comp=0", | |
| 3144 | + "--enable-interintra-comp=0", | |
| 3145 | + "--enable-onesided-comp=0", | |
| 3146 | + "--enable-interintra-wedge=0", | |
| 3147 | + "--enable-smooth-interintra=0", | |
| 3148 | + "--enable-rect-partitions=0", | |
| 3149 | + "--enable-ab-partitions=0", | |
| 3150 | + "--enable-1to4-partitions=0", | |
| 3151 | + "--enable-filter-intra=0", | |
| 3152 | + "--enable-smooth-intra=0", | |
| 3153 | + "--enable-paeth-intra=0", | |
| 3154 | + "--enable-directional-intra=0", | |
| 3155 | + "--enable-angle-delta=0", | |
| 3156 | + "--enable-tx-size-search=0", | |
| 3157 | + "--enable-cdef=0", | |
| 3158 | + "--enable-restoration=0", | |
| 3159 | + "--min-partition-size=8", | |
| 3160 | + "--max-partition-size=32", | |
| 3161 | + "--enable-palette=0", | |
| 3162 | + "--enable-intrabc=0", | |
| 3163 | + "--enable-cfl-intra=0", | |
| 3164 | + "--enable-ref-frame-mvs=0", | |
| 3165 | + "--obu", | |
| 3166 | + "-o", | |
| 3167 | + "-", | |
| 3168 | + "-", | |
| 3169 | + ]; | |
| 3170 | + let mut child = Command::new(aomenc_path()) | |
| 3171 | + .args(&args) | |
| 3172 | + .stdin(Stdio::piped()) | |
| 3173 | + .stdout(Stdio::piped()) | |
| 3174 | + .stderr(Stdio::piped()) | |
| 3175 | + .spawn() | |
| 3176 | + .expect("aomenc failed to start"); | |
| 3177 | + child | |
| 3178 | + .stdin | |
| 3179 | + .take() | |
| 3180 | + .expect("aomenc stdin") | |
| 3181 | + .write_all(&y4m.stdout) | |
| 3182 | + .expect("writing y4m to aomenc"); | |
| 3183 | + let out = child.wait_with_output().expect("aomenc failed to run"); | |
| 3184 | + assert!( | |
| 3185 | + out.status.success(), | |
| 3186 | + "aomenc refused the fixture: {}", | |
| 3187 | + String::from_utf8_lossy(&out.stderr) | |
| 3188 | + ); | |
| 3189 | + let stream = out.stdout; | |
| 3190 | + let before8 = decode::obmc_hits_8(); | |
| 3191 | + let frames = match decode_stream(&stream) { | |
| 3192 | + Err(e) => { | |
| 3193 | + let msg = e.to_string(); | |
| 3194 | + assert!( | |
| 3195 | + msg.contains("unsupported"), | |
| 3196 | + "{NAME} failed outright (seed {seed}): {msg}" | |
| 3197 | + ); | |
| 3198 | + refusals.push(format!("seed {seed}: {msg}")); | |
| 3199 | + continue; | |
| 3200 | + } | |
| 3201 | + Ok(frames) => frames, | |
| 3202 | + }; | |
| 3203 | + let fired8 = decode::obmc_hits_8() - before8; | |
| 3204 | + total_obmc8 += fired8; | |
| 3205 | + if fired8 == 0 { | |
| 3206 | + never_fired += 1; | |
| 3207 | + continue; | |
| 3208 | + } | |
| 3209 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | |
| 3210 | + assert_eq!(frames.len(), frame_count); | |
| 3211 | + let mismatched = frames | |
| 3212 | + .iter() | |
| 3213 | + .zip(&ffmpeg_frames) | |
| 3214 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | |
| 3215 | + if mismatched { | |
| 3216 | + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | |
| 3217 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | |
| 3218 | + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); | |
| 3219 | + } | |
| 3220 | + } | |
| 3221 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | |
| 3222 | + assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); | |
| 3223 | + assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); | |
| 3224 | + assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); | |
| 3225 | + } | |
| 3226 | + eprintln!( | |
| 3227 | + "{NAME} FIRING seed {seed}: 8x8 obmc hits {fired8} (total across attempts so far {total_obmc8})" | |
| 3228 | + ); | |
| 3229 | + return; | |
| 3230 | + } | |
| 3231 | + eprintln!( | |
| 3232 | + "SKIP {NAME}: {never_fired} attempts decoded but never fired an 8x8 obmc block \ | |
| 3233 | + ({total_obmc8} total 8x8 obmc hits across all attempts), and every other attempt \ | |
| 3234 | + hit a named refusal:\n{}", | |
| 3235 | + refusals.join("\n") | |
| 3236 | + ); | |
| 3237 | + } | |
| 3238 | + | |
| 3065 | 3239 | /// lane-motionmode round 1: `--enable-warped-motion=1` must be a NAMED |
| 3066 | 3240 | /// refusal, never a silent mis-decode -- this decoder does not port |
| 3067 | 3241 | /// `av1_findSamples`/`num_proj_ref`, so it cannot tell `OBMC_CAUSAL` |