WIP(av1): temporal MV wiring + gate — r2 capped at final compile check
Browse files at this commit253fdd8df9bb716fe10d02b112d50d4f8a88cfeeParent: 916263c2 changed files +331 -10
- crates/ec-av1/src/decode.rs +103 -7
- crates/ec-av1/src/stream.rs +228 -3
crates/ec-av1/src/decode.rsmodified+103 -7
| @@ -148,6 +148,59 @@ pub(crate) fn tmv_hits() -> usize { | |||
| 148 | crate::mvstack::tmv_hits() | 148 | crate::mvstack::tmv_hits() |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | +/// Everything [`decode_inter_block`] needs to fold temporal MV candidates | ||
| 152 | +/// (spec 7.10.2.8), threaded down from [`decode_inter_frame_tile_with_cdfs`]: | ||
| 153 | +/// the current frame's own projected field ([`crate::motion_field::setup_motion_field`]'s | ||
| 154 | +/// output, built by the caller when `header.use_ref_frame_mvs` is set) plus | ||
| 155 | +/// the order-hint bookkeeping [`crate::motion_field::get_relative_dist`] | ||
| 156 | +/// needs to turn a query block's own resolved `ref_frame` into | ||
| 157 | +/// `TplArgs::cur_offset_0` (spec 7.9.3). | ||
| 158 | +pub(crate) struct TplFrameArgs<'a> { | ||
| 159 | + pub field: &'a crate::motion_field::TplField, | ||
| 160 | + pub order_hint_bits: u32, | ||
| 161 | + pub order_hint: u32, | ||
| 162 | + /// `ref_order_hints[ref_frame - LAST_FRAME]` for `ref_frame` in | ||
| 163 | + /// `LAST_FRAME..=ALTREF_FRAME` — the OrderHint of the picture this | ||
| 164 | + /// frame's own `ref_frame_idx` names for each single reference. | ||
| 165 | + pub ref_order_hints: [u32; 7], | ||
| 166 | +} | ||
| 167 | + | ||
| 168 | +/// Builds this frame's own saved motion field (libaom `av1_copy_frame_mvs`, | ||
| 169 | +/// spec 7.9's per-frame `MotionFieldMvs` storage step) from its fully | ||
| 170 | +/// decoded `grid`: every 4x4 unit an inter block covers reads the same | ||
| 171 | +/// `MiInfo` ([`decode_inter_block`]/`decode_inter_block8` write it | ||
| 172 | +/// block-uniform), so sampling any one 4x4 unit inside each 8x8 cell is | ||
| 173 | +/// exact — every block this crate ever codes is at least 8x8, so no 8x8 | ||
| 174 | +/// cell straddles two different blocks' values. | ||
| 175 | +pub(crate) fn build_motion_field( | ||
| 176 | + grid: &MiGrid, | ||
| 177 | + mi_cols: usize, | ||
| 178 | + mi_rows: usize, | ||
| 179 | + order_hint: u32, | ||
| 180 | + ref_order_hints: [u32; 7], | ||
| 181 | +) -> crate::motion_field::MotionField { | ||
| 182 | + let mut field = | ||
| 183 | + crate::motion_field::MotionField::new(mi_cols, mi_rows, order_hint, ref_order_hints); | ||
| 184 | + for row in 0..mi_rows { | ||
| 185 | + for col in 0..mi_cols { | ||
| 186 | + if let Some(info) = grid.get(row, col) | ||
| 187 | + && info.is_inter | ||
| 188 | + && info.ref_frame > 0 | ||
| 189 | + { | ||
| 190 | + field.set( | ||
| 191 | + row, | ||
| 192 | + col, | ||
| 193 | + crate::motion_field::SavedMv { | ||
| 194 | + mv: info.mv, | ||
| 195 | + ref_frame: info.ref_frame, | ||
| 196 | + }, | ||
| 197 | + ); | ||
| 198 | + } | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + field | ||
| 202 | +} | ||
| 203 | + | ||
| 151 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; | 204 | /// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; |
| 152 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). | 205 | /// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). |
| 153 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { | 206 | pub(crate) fn ref_hits(ref_frame: i8) -> usize { |
| @@ -3775,6 +3828,7 @@ fn decode_inter_block( | |||
| 3775 | allow_high_precision_mv: bool, | 3828 | allow_high_precision_mv: bool, |
| 3776 | interp_fixed: Option<mc::InterpFilterKind>, | 3829 | interp_fixed: Option<mc::InterpFilterKind>, |
| 3777 | enable_dual_filter: bool, | 3830 | enable_dual_filter: bool, |
| 3831 | + tpl_frame: Option<&TplFrameArgs>, | ||
| 3778 | ) -> Result<()> { | 3832 | ) -> Result<()> { |
| 3779 | let (r, c) = at; | 3833 | let (r, c) = at; |
| 3780 | let (px, py) = (c * SUB, r * SUB); | 3834 | let (px, py) = (c * SUB, r * SUB); |
| @@ -3821,6 +3875,20 @@ fn decode_inter_block( | |||
| 3821 | 3875 | ||
| 3822 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | 3876 | let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); |
| 3823 | let bw4 = side / 4; | 3877 | let bw4 = side / 4; |
| 3878 | + // spec 7.10.2.8: only reached when the frame header set | ||
| 3879 | + // `use_ref_frame_mvs` (`tpl_frame` is `Some` then, `None` | ||
| 3880 | + // otherwise) -- `cur_offset_0` is this query block's own resolved | ||
| 3881 | + // `ref_frame`'s distance from the current frame (spec 7.9.3), | ||
| 3882 | + // recomputed per block since different blocks pick different refs. | ||
| 3883 | + let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs { | ||
| 3884 | + field: t.field, | ||
| 3885 | + cur_offset_0: crate::motion_field::get_relative_dist( | ||
| 3886 | + t.order_hint_bits, | ||
| 3887 | + t.order_hint, | ||
| 3888 | + t.ref_order_hints[(ref_frame - LAST_FRAME) as usize], | ||
| 3889 | + ), | ||
| 3890 | + allow_high_precision_mv, | ||
| 3891 | + }); | ||
| 3824 | let stack = find_mv_stack_with_sign_bias( | 3892 | let stack = find_mv_stack_with_sign_bias( |
| 3825 | grid, | 3893 | grid, |
| 3826 | mi_row, | 3894 | mi_row, |
| @@ -3831,11 +3899,7 @@ fn decode_inter_block( | |||
| 3831 | mi_cols as usize, | 3899 | mi_cols as usize, |
| 3832 | mi_rows as usize, | 3900 | mi_rows as usize, |
| 3833 | sign_bias_table, | 3901 | sign_bias_table, |
| 3834 | - // TODO(lane-av1tmvp HANDOFF): thread a real `TplArgs` here once | 3902 | + tpl, |
| 3835 | - // `decode_inter_frame_tile_with_cdfs` carries a projected | ||
| 3836 | - // `TplField` (see that function's own doc) -- `None` keeps | ||
| 3837 | - // today's `use_ref_frame_mvs = 0`-only behaviour exact. | ||
| 3838 | - None, | ||
| 3839 | ); | 3903 | ); |
| 3840 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; | 3904 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 3841 | let mut is_globalmv = false; | 3905 | let mut is_globalmv = false; |
| @@ -4678,8 +4742,12 @@ pub fn decode_inter_frame_tile( | |||
| 4678 | NO_SIGN_BIAS, | 4742 | NO_SIGN_BIAS, |
| 4679 | interp_fixed, | 4743 | interp_fixed, |
| 4680 | enable_dual_filter, | 4744 | enable_dual_filter, |
| 4745 | + 0, | ||
| 4746 | + 0, | ||
| 4747 | + [0; 7], | ||
| 4748 | + None, | ||
| 4681 | ) | 4749 | ) |
| 4682 | - .map(|(picture, _)| picture) | 4750 | + .map(|(picture, _, _)| picture) |
| 4683 | } | 4751 | } |
| 4684 | 4752 | ||
| 4685 | /// [`decode_inter_frame_tile`], threading a cross-frame CDF forward -- see | 4753 | /// [`decode_inter_frame_tile`], threading a cross-frame CDF forward -- see |
| @@ -4703,7 +4771,22 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 4703 | sign_bias_table: SignBiasTable, | 4771 | sign_bias_table: SignBiasTable, |
| 4704 | interp_fixed: Option<mc::InterpFilterKind>, | 4772 | interp_fixed: Option<mc::InterpFilterKind>, |
| 4705 | enable_dual_filter: bool, | 4773 | enable_dual_filter: bool, |
| 4706 | -) -> Result<(Picture, Cdfs)> { | 4774 | + // lane-av1tmvp: `order_hint`/`order_hint_bits`/`ref_order_hints` are |
| 4775 | + // always needed (this frame's own saved [`crate::motion_field::MotionField`] | ||
| 4776 | + // carries them for a *later* frame's projection, spec 7.9); `tpl_field` | ||
| 4777 | + // is `Some` only when this frame's own header set `use_ref_frame_mvs` | ||
| 4778 | + // and the caller ([`crate::stream::decode_stream`]) projected one. | ||
| 4779 | + order_hint_bits: u32, | ||
| 4780 | + order_hint: u32, | ||
| 4781 | + ref_order_hints: [u32; 7], | ||
| 4782 | + tpl_field: Option<&crate::motion_field::TplField>, | ||
| 4783 | +) -> Result<(Picture, Cdfs, crate::motion_field::MotionField)> { | ||
| 4784 | + let tpl_frame = tpl_field.map(|field| TplFrameArgs { | ||
| 4785 | + field, | ||
| 4786 | + order_hint_bits, | ||
| 4787 | + order_hint, | ||
| 4788 | + ref_order_hints, | ||
| 4789 | + }); | ||
| 4707 | if mi_cols == 0 || mi_rows == 0 { | 4790 | if mi_cols == 0 || mi_rows == 0 { |
| 4708 | return Err(unsupported("a frame with no mode-info grid")); | 4791 | return Err(unsupported("a frame with no mode-info grid")); |
| 4709 | } | 4792 | } |
| @@ -4892,6 +4975,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 4892 | allow_high_precision_mv, | 4975 | allow_high_precision_mv, |
| 4893 | interp_fixed, | 4976 | interp_fixed, |
| 4894 | enable_dual_filter, | 4977 | enable_dual_filter, |
| 4978 | + tpl_frame.as_ref(), | ||
| 4895 | )?; | 4979 | )?; |
| 4896 | } | 4980 | } |
| 4897 | PARTITION_SPLIT => { | 4981 | PARTITION_SPLIT => { |
| @@ -4949,6 +5033,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 4949 | allow_high_precision_mv, | 5033 | allow_high_precision_mv, |
| 4950 | interp_fixed, | 5034 | interp_fixed, |
| 4951 | enable_dual_filter, | 5035 | enable_dual_filter, |
| 5036 | + tpl_frame.as_ref(), | ||
| 4952 | )?; | 5037 | )?; |
| 4953 | } else { | 5038 | } else { |
| 4954 | let ctx16 = neighbours.partition_ctx(at16, SUB); | 5039 | let ctx16 = neighbours.partition_ctx(at16, SUB); |
| @@ -5036,6 +5121,14 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 5036 | apply_deblock(&mut y, &mut u, &mut v, loop_filter, &neighbours); | 5121 | apply_deblock(&mut y, &mut u, &mut v, loop_filter, &neighbours); |
| 5037 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); | 5122 | apply_cdef(&mut y, &mut u, &mut v, cdef, &neighbours); |
| 5038 | 5123 | ||
| 5124 | + let motion_field = build_motion_field( | ||
| 5125 | + &grid, | ||
| 5126 | + mi_cols as usize, | ||
| 5127 | + mi_rows as usize, | ||
| 5128 | + order_hint, | ||
| 5129 | + ref_order_hints, | ||
| 5130 | + ); | ||
| 5131 | + | ||
| 5039 | let (fw, fh) = (frame_width as usize, frame_height as usize); | 5132 | let (fw, fh) = (frame_width as usize, frame_height as usize); |
| 5040 | if fw == width && fh == height { | 5133 | if fw == width && fh == height { |
| 5041 | return Ok(( | 5134 | return Ok(( |
| @@ -5047,6 +5140,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 5047 | v: v.data, | 5140 | v: v.data, |
| 5048 | }, | 5141 | }, |
| 5049 | cdfs, | 5142 | cdfs, |
| 5143 | + motion_field, | ||
| 5050 | )); | 5144 | )); |
| 5051 | } | 5145 | } |
| 5052 | let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> { | 5146 | let crop = |plane: &PlaneBuf, w: usize, h: usize| -> Vec<u8> { |
| @@ -5065,6 +5159,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 5065 | v: crop(&v, fw / 2, fh / 2), | 5159 | v: crop(&v, fw / 2, fh / 2), |
| 5066 | }, | 5160 | }, |
| 5067 | cdfs, | 5161 | cdfs, |
| 5162 | + motion_field, | ||
| 5068 | )) | 5163 | )) |
| 5069 | } | 5164 | } |
| 5070 | 5165 | ||
| @@ -5687,6 +5782,7 @@ mod tests { | |||
| 5687 | false, | 5782 | false, |
| 5688 | Some(mc::InterpFilterKind::Regular), | 5783 | Some(mc::InterpFilterKind::Regular), |
| 5689 | false, | 5784 | false, |
| 5785 | + None, | ||
| 5690 | ) | 5786 | ) |
| 5691 | .unwrap(); | 5787 | .unwrap(); |
| 5692 | 5788 | ||
crates/ec-av1/src/stream.rsmodified+228 -3
| @@ -59,6 +59,14 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 59 | // and small-magnitude (the two pictures mostly agree), which is exactly | 59 | // and small-magnitude (the two pictures mostly agree), which is exactly |
| 60 | // what made it look like a rounding bug rather than a wrong reference. | 60 | // what made it look like a rounding bug rather than a wrong reference. |
| 61 | let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); | 61 | let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); |
| 62 | + // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion | ||
| 63 | + // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom | ||
| 64 | + // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was | ||
| 65 | + // decoded with -- `Picture` itself carries neither, so both ride | ||
| 66 | + // alongside `ref_slots` on the exact same `refresh_frame_flags` update. | ||
| 67 | + let mut motion_field_slots: [Option<crate::motion_field::MotionField>; NUM_REF_FRAMES] = | ||
| 68 | + std::array::from_fn(|_| None); | ||
| 69 | + let mut order_hint_slots: [u32; NUM_REF_FRAMES] = [0; NUM_REF_FRAMES]; | ||
| 62 | // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a | 70 | // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a |
| 63 | // previous frame's `refresh_frame_flags` stored into it -- the frame's | 71 | // previous frame's `refresh_frame_flags` stored into it -- the frame's |
| 64 | // own end-of-tile adapted table, or (when the frame set | 72 | // own end-of-tile adapted table, or (when the frame set |
| @@ -232,8 +240,19 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 232 | }; | 240 | }; |
| 233 | let started_from = initial_cdfs.clone(); | 241 | let started_from = initial_cdfs.clone(); |
| 234 | 242 | ||
| 235 | - let (picture, end_cdfs) = if header.frame_type == FrameType::Key { | 243 | + let order_hint_bits = parser |
| 236 | - decode_key_frame_tile_with_cdfs( | 244 | + .sequence_header() |
| 245 | + .map_or(0, |seq| seq.order_hint_bits); | ||
| 246 | + // spec 7.9/7.20: this frame's own `ref_order_hints` -- the OrderHint | ||
| 247 | + // of the picture sitting in each of its 7 single references' DPB | ||
| 248 | + // slots, for this frame's *own* saved [`crate::motion_field::MotionField`] | ||
| 249 | + // (a later frame projects from it) whether or not this frame itself | ||
| 250 | + // reads temporal candidates. | ||
| 251 | + let ref_order_hints: [u32; 7] = | ||
| 252 | + std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]); | ||
| 253 | + | ||
| 254 | + let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { | ||
| 255 | + let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( | ||
| 237 | tile_bytes, | 256 | tile_bytes, |
| 238 | header.mi_cols, | 257 | header.mi_cols, |
| 239 | header.mi_rows, | 258 | header.mi_rows, |
| @@ -247,7 +266,19 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 247 | initial_cdfs, | 266 | initial_cdfs, |
| 248 | header.tx_mode == TxMode::Select, | 267 | header.tx_mode == TxMode::Select, |
| 249 | header.reduced_tx_set, | 268 | header.reduced_tx_set, |
| 250 | - )? | 269 | + )?; |
| 270 | + // A key frame codes no inter blocks -- its own saved motion | ||
| 271 | + // field has no cells set, matching libaom's own "intra frame | ||
| 272 | + // contributes nothing to a later projection" behaviour, but | ||
| 273 | + // still carries `order_hint`/`ref_order_hints` for the distance | ||
| 274 | + // arithmetic a later frame's projection needs. | ||
| 275 | + let motion_field = crate::motion_field::MotionField::new( | ||
| 276 | + header.mi_cols as usize, | ||
| 277 | + header.mi_rows as usize, | ||
| 278 | + header.order_hint, | ||
| 279 | + ref_order_hints, | ||
| 280 | + ); | ||
| 281 | + (picture, end_cdfs, motion_field) | ||
| 251 | } else { | 282 | } else { |
| 252 | let last_slot = header.ref_frame_idx[0] as usize; | 283 | let last_slot = header.ref_frame_idx[0] as usize; |
| 253 | let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { | 284 | let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { |
| @@ -272,6 +303,20 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 272 | .and_then(Option::as_ref) | 303 | .and_then(Option::as_ref) |
| 273 | } | 304 | } |
| 274 | }); | 305 | }); |
| 306 | + // spec 7.9's own driver, `av1_setup_motion_field`: only run when | ||
| 307 | + // this frame's header actually asks for temporal candidates -- | ||
| 308 | + // `find_mv_stack_with_sign_bias` reproduces its old always-`None` | ||
| 309 | + // behaviour bit for bit when this is `None` (see its own doc). | ||
| 310 | + let tpl_field = header.use_ref_frame_mvs.then(|| { | ||
| 311 | + crate::motion_field::setup_motion_field( | ||
| 312 | + &motion_field_slots, | ||
| 313 | + header.ref_frame_idx, | ||
| 314 | + header.order_hint, | ||
| 315 | + order_hint_bits, | ||
| 316 | + header.mi_rows as usize, | ||
| 317 | + header.mi_cols as usize, | ||
| 318 | + ) | ||
| 319 | + }); | ||
| 275 | decode_inter_frame_tile_with_cdfs( | 320 | decode_inter_frame_tile_with_cdfs( |
| 276 | tile_bytes, | 321 | tile_bytes, |
| 277 | header.mi_cols, | 322 | header.mi_cols, |
| @@ -288,6 +333,10 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 288 | header.ref_frame_sign_bias, | 333 | header.ref_frame_sign_bias, |
| 289 | interp_fixed, | 334 | interp_fixed, |
| 290 | enable_dual_filter, | 335 | enable_dual_filter, |
| 336 | + order_hint_bits, | ||
| 337 | + header.order_hint, | ||
| 338 | + ref_order_hints, | ||
| 339 | + tpl_field.as_ref(), | ||
| 291 | )? | 340 | )? |
| 292 | }; | 341 | }; |
| 293 | // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's | 342 | // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's |
| @@ -329,6 +378,8 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 329 | // pre-grain decode, even though the frame pushed onto the | 378 | // pre-grain decode, even though the frame pushed onto the |
| 330 | // caller's output below carries the grained picture. | 379 | // caller's output below carries the grained picture. |
| 331 | ref_slots[i] = Some(picture.clone()); | 380 | ref_slots[i] = Some(picture.clone()); |
| 381 | + motion_field_slots[i] = Some(motion_field.clone()); | ||
| 382 | + order_hint_slots[i] = header.order_hint; | ||
| 332 | } | 383 | } |
| 333 | } | 384 | } |
| 334 | let output = if header.film_grain.apply_grain { | 385 | let output = if header.film_grain.apply_grain { |
| @@ -2444,6 +2495,180 @@ mod tests { | |||
| 2444 | ); | 2495 | ); |
| 2445 | } | 2496 | } |
| 2446 | 2497 | ||
| 2498 | + /// lane-av1tmvp: the temporal-MV gate -- unlike every `_single_ref_gate` | ||
| 2499 | + /// attempt above, this one leaves `--enable-ref-frame-mvs` at aomenc's | ||
| 2500 | + /// own default (on) instead of pinning it off, so `use_ref_frame_mvs` | ||
| 2501 | + /// actually fires and `decode::tmv_hits()` (this gate's own capability | ||
| 2502 | + /// proof, not just `non_last_ref_hits`) has to advance for the run to | ||
| 2503 | + /// count as a real pass rather than a `never_fired` skip. | ||
| 2504 | + fn a_real_aomenc_temporal_mv_gate(seed_base: u32, attempts: u32) { | ||
| 2505 | + let gate_name = "a_real_aomenc_temporal_mv_gate"; | ||
| 2506 | + if !have_ffmpeg() { | ||
| 2507 | + eprintln!("SKIP {gate_name}: no ffmpeg"); | ||
| 2508 | + return; | ||
| 2509 | + } | ||
| 2510 | + if !have_aomenc() { | ||
| 2511 | + eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); | ||
| 2512 | + return; | ||
| 2513 | + } | ||
| 2514 | + let (width, height, frame_count) = (64usize, 64usize, 16usize); | ||
| 2515 | + let mut never_fired = 0u32; | ||
| 2516 | + let mut refusals = Vec::new(); | ||
| 2517 | + for attempt in 0..attempts { | ||
| 2518 | + let seed = seed_base + attempt; | ||
| 2519 | + let duration = frame_count as f64 / 25.0; | ||
| 2520 | + let source = | ||
| 2521 | + format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); | ||
| 2522 | + let y4m = Command::new("ffmpeg") | ||
| 2523 | + .args([ | ||
| 2524 | + "-v", | ||
| 2525 | + "error", | ||
| 2526 | + "-f", | ||
| 2527 | + "lavfi", | ||
| 2528 | + "-i", | ||
| 2529 | + &source, | ||
| 2530 | + "-pix_fmt", | ||
| 2531 | + "yuv420p", | ||
| 2532 | + "-f", | ||
| 2533 | + "yuv4mpegpipe", | ||
| 2534 | + "-", | ||
| 2535 | + ]) | ||
| 2536 | + .stdin(Stdio::null()) | ||
| 2537 | + .stdout(Stdio::piped()) | ||
| 2538 | + .stderr(Stdio::piped()) | ||
| 2539 | + .output() | ||
| 2540 | + .expect("ffmpeg failed to run"); | ||
| 2541 | + assert!( | ||
| 2542 | + y4m.status.success(), | ||
| 2543 | + "ffmpeg fixture: {}", | ||
| 2544 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 2545 | + ); | ||
| 2546 | + // Same envelope as `a_real_aomenc_single_ref_gate`, minus its | ||
| 2547 | + // `--enable-ref-frame-mvs=0` pin -- everything else this decoder | ||
| 2548 | + // still cannot code stays refused the same way. | ||
| 2549 | + let args: Vec<&str> = vec![ | ||
| 2550 | + "--codec=av1", | ||
| 2551 | + "--passes=1", | ||
| 2552 | + "--end-usage=q", | ||
| 2553 | + "--cq-level=55", | ||
| 2554 | + "--cpu-used=0", | ||
| 2555 | + "--kf-max-dist=1000", | ||
| 2556 | + "--threads=1", | ||
| 2557 | + "--row-mt=0", | ||
| 2558 | + "--auto-alt-ref=1", | ||
| 2559 | + "--lag-in-frames=16", | ||
| 2560 | + "--enable-fwd-kf=0", | ||
| 2561 | + "--enable-warped-motion=0", | ||
| 2562 | + "--enable-obmc=0", | ||
| 2563 | + "--enable-masked-comp=0", | ||
| 2564 | + "--enable-interintra-comp=0", | ||
| 2565 | + "--enable-dist-wtd-comp=0", | ||
| 2566 | + "--enable-diff-wtd-comp=0", | ||
| 2567 | + "--enable-onesided-comp=0", | ||
| 2568 | + "--enable-interintra-wedge=0", | ||
| 2569 | + "--enable-smooth-interintra=0", | ||
| 2570 | + "--enable-rect-partitions=0", | ||
| 2571 | + "--enable-ab-partitions=0", | ||
| 2572 | + "--enable-1to4-partitions=0", | ||
| 2573 | + "--enable-filter-intra=0", | ||
| 2574 | + "--enable-smooth-intra=0", | ||
| 2575 | + "--enable-paeth-intra=0", | ||
| 2576 | + "--enable-directional-intra=0", | ||
| 2577 | + "--enable-angle-delta=0", | ||
| 2578 | + "--enable-tx-size-search=0", | ||
| 2579 | + "--enable-cdef=0", | ||
| 2580 | + "--enable-restoration=0", | ||
| 2581 | + "--max-partition-size=32", | ||
| 2582 | + "--min-partition-size=32", | ||
| 2583 | + "--enable-palette=0", | ||
| 2584 | + "--enable-intrabc=0", | ||
| 2585 | + "--enable-cfl-intra=0", | ||
| 2586 | + "--obu", | ||
| 2587 | + "-o", | ||
| 2588 | + "-", | ||
| 2589 | + "-", | ||
| 2590 | + ]; | ||
| 2591 | + let mut child = Command::new(aomenc_path()) | ||
| 2592 | + .args(&args) | ||
| 2593 | + .stdin(Stdio::piped()) | ||
| 2594 | + .stdout(Stdio::piped()) | ||
| 2595 | + .stderr(Stdio::piped()) | ||
| 2596 | + .spawn() | ||
| 2597 | + .expect("aomenc failed to start"); | ||
| 2598 | + child | ||
| 2599 | + .stdin | ||
| 2600 | + .take() | ||
| 2601 | + .expect("aomenc stdin") | ||
| 2602 | + .write_all(&y4m.stdout) | ||
| 2603 | + .expect("writing y4m to aomenc"); | ||
| 2604 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 2605 | + assert!( | ||
| 2606 | + out.status.success(), | ||
| 2607 | + "aomenc refused the fixture: {}", | ||
| 2608 | + String::from_utf8_lossy(&out.stderr) | ||
| 2609 | + ); | ||
| 2610 | + let stream = out.stdout; | ||
| 2611 | + let before = decode::tmv_hits(); | ||
| 2612 | + let frames = match decode_stream(&stream) { | ||
| 2613 | + Err(e) => { | ||
| 2614 | + let msg = e.to_string(); | ||
| 2615 | + assert!( | ||
| 2616 | + msg.contains("unsupported"), | ||
| 2617 | + "{gate_name} failed outright (seed {seed}): {msg}" | ||
| 2618 | + ); | ||
| 2619 | + refusals.push(format!("seed {seed}: {msg}")); | ||
| 2620 | + continue; | ||
| 2621 | + } | ||
| 2622 | + Ok(frames) => frames, | ||
| 2623 | + }; | ||
| 2624 | + if decode::tmv_hits() == before { | ||
| 2625 | + never_fired += 1; | ||
| 2626 | + continue; | ||
| 2627 | + } | ||
| 2628 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | ||
| 2629 | + assert_eq!(frames.len(), frame_count); | ||
| 2630 | + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 2631 | + let mismatched = frames | ||
| 2632 | + .iter() | ||
| 2633 | + .zip(&ffmpeg_frames) | ||
| 2634 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | ||
| 2635 | + if mismatched { | ||
| 2636 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 2637 | + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); | ||
| 2638 | + } | ||
| 2639 | + } | ||
| 2640 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 2641 | + assert_eq!( | ||
| 2642 | + got.y, want.y, | ||
| 2643 | + "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" | ||
| 2644 | + ); | ||
| 2645 | + assert_eq!( | ||
| 2646 | + got.u, want.u, | ||
| 2647 | + "{gate_name} frame {i} U vs ffmpeg (seed {seed})" | ||
| 2648 | + ); | ||
| 2649 | + assert_eq!( | ||
| 2650 | + got.v, want.v, | ||
| 2651 | + "{gate_name} frame {i} V vs ffmpeg (seed {seed})" | ||
| 2652 | + ); | ||
| 2653 | + } | ||
| 2654 | + eprintln!( | ||
| 2655 | + "{gate_name} FIRING seed {seed}: tmv_hits advanced by {}", | ||
| 2656 | + decode::tmv_hits() - before | ||
| 2657 | + ); | ||
| 2658 | + return; | ||
| 2659 | + } | ||
| 2660 | + eprintln!( | ||
| 2661 | + "SKIP {gate_name}: {never_fired} attempts decoded but never fired a temporal MV \ | ||
| 2662 | + candidate, and every other attempt hit a named refusal:\n{}", | ||
| 2663 | + refusals.join("\n") | ||
| 2664 | + ); | ||
| 2665 | + } | ||
| 2666 | + | ||
| 2667 | + #[test] | ||
| 2668 | + fn a_real_aomenc_stream_with_temporal_mvs_decodes_pixel_exact() { | ||
| 2669 | + a_real_aomenc_temporal_mv_gate(42, 40); | ||
| 2670 | + } | ||
| 2671 | + | ||
| 2447 | /// scratch: decode a pinned stream end to end and report where/why it | 2672 | /// scratch: decode a pinned stream end to end and report where/why it |
| 2448 | /// refuses, plus `non_last_ref_hits` progression. | 2673 | /// refuses, plus `non_last_ref_hits` progression. |
| 2449 | #[test] | 2674 | #[test] |