wip(av1): warp candidate snapshot at the agent cap (may not compile)
Browse files at this commitfbf67c0cffd5987da7fecf523bf5a878c64079e3Parent: cb813703 changed files +347 -116
- crates/ec-av1/src/decode.rs +9 -3
- crates/ec-av1/src/encode.rs +250 -95
- crates/ec-av1/src/tile.rs +88 -18
crates/ec-av1/src/decode.rsmodified+9 -3
| @@ -552,7 +552,7 @@ pub(crate) fn warp_plane_suppress_hits() -> usize { | |||
| 552 | /// block dimensions decide it -- never the square `side`/`chroma_side`, which | 552 | /// block dimensions decide it -- never the square `side`/`chroma_side`, which |
| 553 | /// for a rectangular luma block (8x16, 16x8) or a PARTITION_HORZ/VERT strip is | 553 | /// for a rectangular luma block (8x16, 16x8) or a PARTITION_HORZ/VERT strip is |
| 554 | /// bigger than the real block in one axis. | 554 | /// bigger than the real block in one axis. |
| 555 | -fn warp_plane_allowed(w: usize, h: usize) -> bool { | 555 | +pub(crate) fn warp_plane_allowed(w: usize, h: usize) -> bool { |
| 556 | if w >= 8 && h >= 8 { | 556 | if w >= 8 && h >= 8 { |
| 557 | return true; | 557 | return true; |
| 558 | } | 558 | } |
| @@ -24220,7 +24220,7 @@ fn has_top_right(mi_row: usize, mi_col: usize, bs: usize, fctx: &crate::decode:: | |||
| 24220 | /// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula) | 24220 | /// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula) |
| 24221 | /// come from the *neighbour* cell's own coded size, not this block's. | 24221 | /// come from the *neighbour* cell's own coded size, not this block's. |
| 24222 | #[allow(clippy::too_many_arguments)] | 24222 | #[allow(clippy::too_many_arguments)] |
| 24223 | -fn find_samples( | 24223 | +pub(crate) fn find_samples( |
| 24224 | grid: &MiGrid, | 24224 | grid: &MiGrid, |
| 24225 | mi_row: usize, | 24225 | mi_row: usize, |
| 24226 | mi_col: usize, | 24226 | mi_col: usize, |
| @@ -24378,7 +24378,7 @@ fn find_samples( | |||
| 24378 | /// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over | 24378 | /// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over |
| 24379 | /// the 2-symbol `obmc_cdf` (spec 5.11.24). | 24379 | /// the 2-symbol `obmc_cdf` (spec 5.11.24). |
| 24380 | #[allow(clippy::too_many_arguments)] | 24380 | #[allow(clippy::too_many_arguments)] |
| 24381 | -fn num_proj_ref( | 24381 | +pub(crate) fn num_proj_ref( |
| 24382 | grid: &MiGrid, | 24382 | grid: &MiGrid, |
| 24383 | mi_row: usize, | 24383 | mi_row: usize, |
| 24384 | mi_col: usize, | 24384 | mi_col: usize, |
| @@ -32053,6 +32053,12 @@ fn decode_inter_block8( | |||
| 32053 | mc::scale_factor(ref_width, frame_width) != mc::REF_NO_SCALE; | 32053 | mc::scale_factor(ref_width, frame_width) != mc::REF_NO_SCALE; |
| 32054 | let warp_eligible = | 32054 | let warp_eligible = |
| 32055 | allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok; | 32055 | allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok; |
| 32056 | + if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { | ||
| 32057 | + eprintln!( | ||
| 32058 | + "EC_MM8 mi_row={mi_row} mi_col={mi_col} proj={} warp_elig={} awm={}", | ||
| 32059 | + proj_ok as u8, warp_eligible as u8, allow_warped_motion as u8 | ||
| 32060 | + ); | ||
| 32061 | + } | ||
| 32056 | if allow_warped_motion && proj_ok && ref_is_scaled { | 32062 | if allow_warped_motion && proj_ok && ref_is_scaled { |
| 32057 | hit!(SCALED_WARP_SUPPRESSED_HITS); | 32063 | hit!(SCALED_WARP_SUPPRESSED_HITS); |
| 32058 | } | 32064 | } |
crates/ec-av1/src/encode.rsmodified+250 -95
| @@ -1028,7 +1028,10 @@ pub(crate) fn key_frame_headers_colour( | |||
| 1028 | additional_frame_id_length: 0, | 1028 | additional_frame_id_length: 0, |
| 1029 | enable_interintra_compound: false, | 1029 | enable_interintra_compound: false, |
| 1030 | enable_masked_compound: false, | 1030 | enable_masked_compound: false, |
| 1031 | - enable_warped_motion: false, | 1031 | + // lane-av1obmc2: the sequence bit `allow_warped_motion` is gated on |
| 1032 | + // (spec 5.5.2). Set only under the warp knob, so the DEFAULT sequence | ||
| 1033 | + // header is byte-identical to the streams before this lane. | ||
| 1034 | + enable_warped_motion: crate::envflags::env_flag!("EC_AV1_WARP"), | ||
| 1032 | enable_dual_filter: false, | 1035 | enable_dual_filter: false, |
| 1033 | enable_jnt_comp: false, | 1036 | enable_jnt_comp: false, |
| 1034 | enable_ref_frame_mvs: false, | 1037 | enable_ref_frame_mvs: false, |
| @@ -1166,9 +1169,14 @@ pub fn inter_frame_headers_slots( | |||
| 1166 | // to the OBMC knob rather than always on: with every block choosing | 1169 | // to the OBMC knob rather than always on: with every block choosing |
| 1167 | // SIMPLE_TRANSLATION the symbol is pure cost (+0.07% / +0.02% on the | 1170 | // SIMPLE_TRANSLATION the symbol is pure cost (+0.07% / +0.02% on the |
| 1168 | // gate clip), so the DEFAULT path writes no motion_mode syntax at all | 1171 | // gate clip), so the DEFAULT path writes no motion_mode syntax at all |
| 1169 | - // and stays byte-identical to the streams before this lane. A future | 1172 | + // and stays byte-identical to the streams before this lane. |
| 1170 | - // warp knob joins this disjunction. | 1173 | + // lane-av1obmc2: the warp knob joins that disjunction, and turns on |
| 1171 | - is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC"), | 1174 | + // `allow_warped_motion` besides -- which is what makes an eligible |
| 1175 | + // block read the 3-symbol `motion_mode_cdf` alphabet instead of the | ||
| 1176 | + // 2-symbol `obmc_cdf` one (libaom `motion_mode_allowed`). | ||
| 1177 | + is_motion_mode_switchable: crate::envflags::env_flag!("EC_AV1_OBMC") | ||
| 1178 | + || crate::envflags::env_flag!("EC_AV1_WARP"), | ||
| 1179 | + allow_warped_motion: crate::envflags::env_flag!("EC_AV1_WARP"), | ||
| 1172 | use_ref_frame_mvs: false, | 1180 | use_ref_frame_mvs: false, |
| 1173 | // Forces `get_tx_set` (spec 5.11.48) to the two-symbol | 1181 | // Forces `get_tx_set` (spec 5.11.48) to the two-symbol |
| 1174 | // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only | 1182 | // `TX_SET_INTER_3` for every inter transform below 32x32 -- the only |
| @@ -2281,11 +2289,113 @@ fn obmc_min_side() -> usize { | |||
| 2281 | /// this frame's writer really starts from (`tile::obmc_symbol_bits`) -- the | 2289 | /// this frame's writer really starts from (`tile::obmc_symbol_bits`) -- the |
| 2282 | /// same frame-CDF price the coefficients of a non-screen frame are estimated | 2290 | /// same frame-CDF price the coefficients of a non-screen frame are estimated |
| 2283 | /// with, not the static default table. | 2291 | /// with, not the static default table. |
| 2284 | -fn obmc_bits(write_w: usize, write_h: usize, obmc: bool) -> f64 { | 2292 | +/// The same price for whichever alphabet the writer will really use for this |
| 2293 | +/// block: `warp_alphabet` is `motion_mode_allowed`'s 3-symbol choice | ||
| 2294 | +/// (`allow_warped_motion` and at least one warp sample), otherwise the | ||
| 2295 | +/// 2-symbol `obmc` one. Both sides of every motion-mode comparison in the | ||
| 2296 | +/// search pay it, so the alphabet cancels out of a comparison and only its | ||
| 2297 | +/// per-value spread decides. | ||
| 2298 | +fn motion_mode_bits(write_w: usize, write_h: usize, motion: u8, warp_alphabet: bool) -> f64 { | ||
| 2285 | let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else { | 2299 | let Some(row) = crate::decode::motion_mode_cdf_row(write_w, write_h) else { |
| 2286 | return 0.0; | 2300 | return 0.0; |
| 2287 | }; | 2301 | }; |
| 2288 | - crate::tile::obmc_symbol_bits(row, obmc) | 2302 | + match warp_alphabet { |
| 2303 | + true => crate::tile::motion_mode_symbol_bits(row, motion), | ||
| 2304 | + false => crate::tile::obmc_symbol_bits(row, motion == 1), | ||
| 2305 | + } | ||
| 2306 | +} | ||
| 2307 | + | ||
| 2308 | +/// The WARPED_CAUSAL prediction of one square single-reference block: the | ||
| 2309 | +/// decoder's own warp-sample walk (`decode::find_samples` + | ||
| 2310 | +/// `warp::select_samples`), least-squares model (`warp::find_projection`) and | ||
| 2311 | +/// affine predictor (`warp::warp_affine`), driven off the encoder's own mi | ||
| 2312 | +/// grid -- never a second implementation of any of the three. `None` when the | ||
| 2313 | +/// block would code no motion_mode symbol at all, when it has no warp sample | ||
| 2314 | +/// (the writer then codes the 2-symbol alphabet, which has no WARPED value), | ||
| 2315 | +/// or when the projection is not filter-representable (the decoder would then | ||
| 2316 | +/// predict translationally, so the mode is pure cost). | ||
| 2317 | +#[allow(clippy::too_many_arguments)] | ||
| 2318 | +fn warp_prediction( | ||
| 2319 | + grid: &MiGrid, | ||
| 2320 | + (mi_row, mi_col): (usize, usize), | ||
| 2321 | + (mi_rows, mi_cols): (usize, usize), | ||
| 2322 | + (x, y): (usize, usize), | ||
| 2323 | + side: usize, | ||
| 2324 | + mv: (i32, i32), | ||
| 2325 | + ref_frame: i8, | ||
| 2326 | + ref_planes: [(&[u16], usize, usize, usize); 3], | ||
| 2327 | + fctx: &crate::decode::FrameCtx, | ||
| 2328 | +) -> Option<[Vec<u8>; 3]> { | ||
| 2329 | + let n4 = side / 4; | ||
| 2330 | + if !crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, n4, n4, mi_cols, mi_rows) { | ||
| 2331 | + return None; | ||
| 2332 | + } | ||
| 2333 | + let mut samples = crate::decode::find_samples( | ||
| 2334 | + grid, mi_row, mi_col, n4, n4, mi_cols, mi_rows, ref_frame, fctx, | ||
| 2335 | + ); | ||
| 2336 | + if samples.is_empty() { | ||
| 2337 | + return None; | ||
| 2338 | + } | ||
| 2339 | + if samples.len() > 1 { | ||
| 2340 | + crate::warp::select_samples(mv, &mut samples, side as i32, side as i32); | ||
| 2341 | + } | ||
| 2342 | + let params = crate::warp::find_projection( | ||
| 2343 | + &samples, | ||
| 2344 | + side as i32, | ||
| 2345 | + side as i32, | ||
| 2346 | + mv.1, | ||
| 2347 | + mv.0, | ||
| 2348 | + mi_row as i32, | ||
| 2349 | + mi_col as i32, | ||
| 2350 | + )?; | ||
| 2351 | + let mut pred = [ | ||
| 2352 | + vec![0u16; side * side], | ||
| 2353 | + vec![0u16; side * side / 4], | ||
| 2354 | + vec![0u16; side * side / 4], | ||
| 2355 | + ]; | ||
| 2356 | + // The translational prediction first, exactly as the decoder builds it, | ||
| 2357 | + // then the affine one over the planes whose OWN block is at least 8x8 | ||
| 2358 | + // (libaom `av1_init_warp_params` bails below that, so an 8x8 luma block's | ||
| 2359 | + // 4x4 chroma stays translational). | ||
| 2360 | + for (i, r) in ref_planes.into_iter().enumerate() { | ||
| 2361 | + let luma_plane = i == 0; | ||
| 2362 | + let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) }; | ||
| 2363 | + let s = if luma_plane { side } else { side / 2 }; | ||
| 2364 | + mc::predict( | ||
| 2365 | + r.0, | ||
| 2366 | + r.1, | ||
| 2367 | + r.2, | ||
| 2368 | + r.3, | ||
| 2369 | + mv_to_q4(px, mv.1, luma_plane), | ||
| 2370 | + mv_to_q4(py, mv.0, luma_plane), | ||
| 2371 | + s, | ||
| 2372 | + s, | ||
| 2373 | + &mut pred[i], | ||
| 2374 | + fctx, | ||
| 2375 | + ); | ||
| 2376 | + if crate::decode::warp_plane_allowed(s, s) { | ||
| 2377 | + let (sub, cside) = (usize::from(!luma_plane) as i32, s as i32); | ||
| 2378 | + crate::warp::warp_affine( | ||
| 2379 | + ¶ms, | ||
| 2380 | + r.0, | ||
| 2381 | + r.2 as i32, | ||
| 2382 | + r.3 as i32, | ||
| 2383 | + r.1 as i32, | ||
| 2384 | + &mut pred[i], | ||
| 2385 | + px as i32, | ||
| 2386 | + py as i32, | ||
| 2387 | + cside, | ||
| 2388 | + cside, | ||
| 2389 | + cside, | ||
| 2390 | + sub, | ||
| 2391 | + sub, | ||
| 2392 | + fctx, | ||
| 2393 | + ); | ||
| 2394 | + } | ||
| 2395 | + } | ||
| 2396 | + let [py_buf, pu_buf, pv_buf] = pred; | ||
| 2397 | + let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>(); | ||
| 2398 | + Some([u8s(&py_buf), u8s(&pu_buf), u8s(&pv_buf)]) | ||
| 2289 | } | 2399 | } |
| 2290 | 2400 | ||
| 2291 | pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { | 2401 | pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { |
| @@ -3989,15 +4099,14 @@ fn code_square_inter( | |||
| 3989 | // reconstruction priced here is the one a decoder rebuilds. lane-av1obmc | 4099 | // reconstruction priced here is the one a decoder rebuilds. lane-av1obmc |
| 3990 | // offered this only at 32x32, which is not where libaom cashes its OBMC | 4100 | // offered this only at 32x32, which is not where libaom cashes its OBMC |
| 3991 | // gain. | 4101 | // gain. |
| 3992 | - let mut obmc_won = false; | 4102 | + let mut motion_won = 0u8; |
| 3993 | let single = match &best.1 { | 4103 | let single = match &best.1 { |
| 3994 | None => Some(nearest_info), | 4104 | None => Some(nearest_info), |
| 3995 | Some((_, _, _, _, info)) if info.ref1.is_none() => Some(*info), | 4105 | Some((_, _, _, _, info)) if info.ref1.is_none() => Some(*info), |
| 3996 | Some(_) => None, | 4106 | Some(_) => None, |
| 3997 | }; | 4107 | }; |
| 3998 | if let Some(info) = single.filter(|i| { | 4108 | if let Some(info) = single.filter(|i| { |
| 3999 | - crate::envflags::env_flag!("EC_AV1_OBMC") | 4109 | + (crate::envflags::env_flag!("EC_AV1_OBMC") || crate::envflags::env_flag!("EC_AV1_WARP")) |
| 4000 | - && side >= obmc_min_side() | ||
| 4001 | && i.ref_frame == crate::mvstack::LAST_FRAME | 4110 | && i.ref_frame == crate::mvstack::LAST_FRAME |
| 4002 | }) { | 4111 | }) { |
| 4003 | let planes = [ | 4112 | let planes = [ |
| @@ -4005,6 +4114,26 @@ fn code_square_inter( | |||
| 4005 | (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | 4114 | (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), |
| 4006 | (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | 4115 | (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), |
| 4007 | ]; | 4116 | ]; |
| 4117 | + // Which alphabet this block's writer will code the symbol against | ||
| 4118 | + // (`tile::write_motion_mode`, libaom `motion_mode_allowed`): the | ||
| 4119 | + // 3-symbol one only under `allow_warped_motion` and at least one warp | ||
| 4120 | + // sample. Mirrored here so the candidates are priced against the | ||
| 4121 | + // table the tile really narrows. | ||
| 4122 | + let warp_alphabet = crate::envflags::env_flag!("EC_AV1_WARP") | ||
| 4123 | + && crate::decode::num_proj_ref( | ||
| 4124 | + grid, | ||
| 4125 | + mi_row, | ||
| 4126 | + mi_col, | ||
| 4127 | + side / 4, | ||
| 4128 | + side / 4, | ||
| 4129 | + mi_cols, | ||
| 4130 | + mi_rows, | ||
| 4131 | + info.ref_frame, | ||
| 4132 | + fctx, | ||
| 4133 | + ) >= 1; | ||
| 4134 | + let mm = |m: u8| motion_mode_bits(side, side, m, warp_alphabet); | ||
| 4135 | + let mut candidates: Vec<(u8, [Vec<u8>; 3])> = Vec::new(); | ||
| 4136 | + if crate::envflags::env_flag!("EC_AV1_OBMC") && side >= obmc_min_side() { | ||
| 4008 | if let Some(pred) = obmc_prediction( | 4137 | if let Some(pred) = obmc_prediction( |
| 4009 | grid, | 4138 | grid, |
| 4010 | (mi_row, mi_col), | 4139 | (mi_row, mi_col), |
| @@ -4017,6 +4146,25 @@ fn code_square_inter( | |||
| 4017 | reference.width, | 4146 | reference.width, |
| 4018 | fctx, | 4147 | fctx, |
| 4019 | ) { | 4148 | ) { |
| 4149 | + candidates.push((1, pred)); | ||
| 4150 | + } | ||
| 4151 | + } | ||
| 4152 | + if warp_alphabet { | ||
| 4153 | + if let Some(pred) = warp_prediction( | ||
| 4154 | + grid, | ||
| 4155 | + (mi_row, mi_col), | ||
| 4156 | + (mi_rows, mi_cols), | ||
| 4157 | + (x, y), | ||
| 4158 | + side, | ||
| 4159 | + info.mv, | ||
| 4160 | + info.ref_frame, | ||
| 4161 | + planes, | ||
| 4162 | + fctx, | ||
| 4163 | + ) { | ||
| 4164 | + candidates.push((2, pred)); | ||
| 4165 | + } | ||
| 4166 | + } | ||
| 4167 | + for (motion, pred) in candidates { | ||
| 4020 | let luma_o = luma.code_from_prediction( | 4168 | let luma_o = luma.code_from_prediction( |
| 4021 | x, y, side, &pred[0], false, search.base_q_idx, search.deadzone, luma_set, | 4169 | x, y, side, &pred[0], false, search.base_q_idx, search.deadzone, luma_set, |
| 4022 | ); | 4170 | ); |
| @@ -4037,12 +4185,12 @@ fn code_square_inter( | |||
| 4037 | + search.lambda | 4185 | + search.lambda |
| 4038 | * (skip_bits(skip_o) | 4186 | * (skip_bits(skip_o) |
| 4039 | + single_syntax | 4187 | + single_syntax |
| 4040 | - + obmc_bits(side, side, true) | 4188 | + + mm(motion) |
| 4041 | + if skip_o { 0.0 } else { luma_o.bits + u_o.bits + v_o.bits }); | 4189 | + if skip_o { 0.0 } else { luma_o.bits + u_o.bits + v_o.bits }); |
| 4042 | - // The incumbent pays the SIMPLE_TRANSLATION symbol it was priced | 4190 | + // The incumbent pays the motion_mode symbol IT would code, so |
| 4043 | - // without, so both sides carry the motion_mode syntax. | 4191 | + // both sides of the comparison carry that syntax. |
| 4044 | - if cost_o < best.0 + search.lambda * obmc_bits(side, side, false) { | 4192 | + if cost_o < best.0 + search.lambda * mm(motion_won) { |
| 4045 | - obmc_won = true; | 4193 | + motion_won = motion; |
| 4046 | best = (cost_o, Some((luma_o, u_o, v_o, skip_o, info))); | 4194 | best = (cost_o, Some((luma_o, u_o, v_o, skip_o, info))); |
| 4047 | } | 4195 | } |
| 4048 | } | 4196 | } |
| @@ -4073,7 +4221,7 @@ fn code_square_inter( | |||
| 4073 | // An OBMC winner commits the flat trial too: `commit_inter_luma` | 4221 | // An OBMC winner commits the flat trial too: `commit_inter_luma` |
| 4074 | // re-predicts each var-tx unit by plain translation, which is not | 4222 | // re-predicts each var-tx unit by plain translation, which is not |
| 4075 | // the prediction this candidate was priced from. | 4223 | // the prediction this candidate was priced from. |
| 4076 | - let (levels, tx_depth, dcost) = if info.ref1.is_some() || obmc_won { | 4224 | + let (levels, tx_depth, dcost) = if info.ref1.is_some() || motion_won != 0 { |
| 4077 | luma.commit(x, y, side, &luma_new); | 4225 | luma.commit(x, y, side, &luma_new); |
| 4078 | (luma_new.levels.clone(), 0, 0.0) | 4226 | (luma_new.levels.clone(), 0, 0.0) |
| 4079 | } else { | 4227 | } else { |
| @@ -4099,7 +4247,7 @@ fn code_square_inter( | |||
| 4099 | palette_uv: None, | 4247 | palette_uv: None, |
| 4100 | tx_depth, | 4248 | tx_depth, |
| 4101 | inter: Some(info), | 4249 | inter: Some(info), |
| 4102 | - obmc: obmc_won, | 4250 | + motion_mode: motion_won, |
| 4103 | }, | 4251 | }, |
| 4104 | cost + dcost, | 4252 | cost + dcost, |
| 4105 | ); | 4253 | ); |
| @@ -4131,7 +4279,7 @@ fn code_square_inter( | |||
| 4131 | palette: None, | 4279 | palette: None, |
| 4132 | palette_uv: None, | 4280 | palette_uv: None, |
| 4133 | tx_depth, | 4281 | tx_depth, |
| 4134 | - obmc: false, | 4282 | + motion_mode: 0, |
| 4135 | inter: Some(InterInfo { | 4283 | inter: Some(InterInfo { |
| 4136 | ref1: None, | 4284 | ref1: None, |
| 4137 | mv1: (0, 0), | 4285 | mv1: (0, 0), |
| @@ -6526,91 +6674,92 @@ fn search_inter_block( | |||
| 6526 | // that is where libaom cashes most of its OBMC gain), and the symbol/ | 6674 | // that is where libaom cashes most of its OBMC gain), and the symbol/ |
| 6527 | // residual price here is the static-CDF estimate. | 6675 | // residual price here is the static-CDF estimate. |
| 6528 | let mut best = best; | 6676 | let mut best = best; |
| 6529 | - let mut obmc_won = false; | 6677 | + let mut motion_won = 0u8; |
| 6530 | - if let Some(info) = best.inter.filter(|i| i.ref1.is_none()) { | 6678 | + if let Some(info) = best.inter.filter(|i| { |
| 6531 | - if crate::envflags::env_flag!("EC_AV1_OBMC") | 6679 | + i.ref1.is_none() |
| 6532 | - && crate::decode::has_overlappable_neighbour(grid, mi_row, mi_col, 8, 8, mi_cols, mi_rows) | 6680 | + && (crate::envflags::env_flag!("EC_AV1_OBMC") |
| 6533 | - { | 6681 | + || crate::envflags::env_flag!("EC_AV1_WARP")) |
| 6682 | + }) { | ||
| 6534 | let mut refs: [Option<&Picture>; 8] = [None; 8]; | 6683 | let mut refs: [Option<&Picture>; 8] = [None; 8]; |
| 6535 | refs[LAST_FRAME as usize] = Some(reference); | 6684 | refs[LAST_FRAME as usize] = Some(reference); |
| 6536 | for &(r, p, _) in extra { | 6685 | for &(r, p, _) in extra { |
| 6537 | refs[r as usize] = Some(p); | 6686 | refs[r as usize] = Some(p); |
| 6538 | } | 6687 | } |
| 6539 | - let refpix = crate::decode::RefPix::ready(refs); | 6688 | + // This block's OWN reference planes: the single-reference winner may |
| 6540 | - let plan = crate::decode::obmc_plan( | 6689 | + // be one of the extra references, not LAST. |
| 6690 | + let planes: [(&[u16], usize, usize, usize); 3] = | ||
| 6691 | + match extra.iter().find(|(r, _, _)| *r == info.ref_frame) { | ||
| 6692 | + Some(&(_, g, _)) => [ | ||
| 6693 | + (g.y.as_slice(), g.width, luma.true_width, luma.true_height), | ||
| 6694 | + (g.u.as_slice(), g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 6695 | + (g.v.as_slice(), g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 6696 | + ], | ||
| 6697 | + None => [ | ||
| 6698 | + (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), | ||
| 6699 | + (ref_u.0.as_slice(), ref_u.1, ref_u.2, ref_u.3), | ||
| 6700 | + (ref_v.0.as_slice(), ref_v.1, ref_v.2, ref_v.3), | ||
| 6701 | + ], | ||
| 6702 | + }; | ||
| 6703 | + // The alphabet `tile::write_motion_mode` will code this block's | ||
| 6704 | + // symbol against, mirrored term for term (libaom | ||
| 6705 | + // `motion_mode_allowed`). | ||
| 6706 | + let warp_alphabet = crate::envflags::env_flag!("EC_AV1_WARP") | ||
| 6707 | + && crate::decode::num_proj_ref( | ||
| 6541 | grid, | 6708 | grid, |
| 6542 | - &[], | ||
| 6543 | - &[], | ||
| 6544 | mi_row, | 6709 | mi_row, |
| 6545 | mi_col, | 6710 | mi_col, |
| 6546 | 8, | 6711 | 8, |
| 6547 | 8, | 6712 | 8, |
| 6548 | - mi_rows, | ||
| 6549 | mi_cols, | 6713 | mi_cols, |
| 6714 | + mi_rows, | ||
| 6715 | + info.ref_frame, | ||
| 6716 | + fctx, | ||
| 6717 | + ) >= 1; | ||
| 6718 | + let mm = |m: u8| motion_mode_bits(BLOCK, BLOCK, m, warp_alphabet); | ||
| 6719 | + let mut candidates: Vec<(u8, [Vec<u8>; 3])> = Vec::new(); | ||
| 6720 | + if crate::envflags::env_flag!("EC_AV1_OBMC") { | ||
| 6721 | + if let Some(pred) = obmc_prediction( | ||
| 6722 | + grid, | ||
| 6723 | + (mi_row, mi_col), | ||
| 6724 | + (mi_rows, mi_cols), | ||
| 6725 | + (x, y), | ||
| 6550 | BLOCK, | 6726 | BLOCK, |
| 6551 | - BLOCK, | 6727 | + info.mv, |
| 6552 | - BLOCK, | 6728 | + &refs, |
| 6553 | - BLOCK / 2, | 6729 | + planes, |
| 6554 | - x, | ||
| 6555 | - y, | ||
| 6556 | - x / 2, | ||
| 6557 | - y / 2, | ||
| 6558 | - &refpix, | ||
| 6559 | - Some(mc::InterpFilterKind::Regular), | ||
| 6560 | reference.width, | 6730 | reference.width, |
| 6561 | - ); | ||
| 6562 | - if let Ok(plan) = plan { | ||
| 6563 | - let (ref_luma, ref_u, ref_v) = match extra | ||
| 6564 | - .iter() | ||
| 6565 | - .find(|(r, _, _)| *r == info.ref_frame) | ||
| 6566 | - { | ||
| 6567 | - Some(&(_, g, _)) => ( | ||
| 6568 | - (&g.y, g.width, luma.true_width, luma.true_height), | ||
| 6569 | - (&g.u, g.width / 2, chroma[0].true_width, chroma[0].true_height), | ||
| 6570 | - (&g.v, g.width / 2, chroma[1].true_width, chroma[1].true_height), | ||
| 6571 | - ), | ||
| 6572 | - None => (ref_luma, ref_u, ref_v), | ||
| 6573 | - }; | ||
| 6574 | - let mv = info.mv; | ||
| 6575 | - let mut pred = [ | ||
| 6576 | - vec![0u16; BLOCK * BLOCK], | ||
| 6577 | - vec![0u16; BLOCK * BLOCK / 4], | ||
| 6578 | - vec![0u16; BLOCK * BLOCK / 4], | ||
| 6579 | - ]; | ||
| 6580 | - for (i, (r, luma_plane)) in | ||
| 6581 | - [(ref_luma, true), (ref_u, false), (ref_v, false)].into_iter().enumerate() | ||
| 6582 | - { | ||
| 6583 | - let (px, py) = if luma_plane { (x, y) } else { (x / 2, y / 2) }; | ||
| 6584 | - let side = if luma_plane { BLOCK } else { BLOCK / 2 }; | ||
| 6585 | - mc::predict( | ||
| 6586 | - r.0, | ||
| 6587 | - r.1, | ||
| 6588 | - r.2, | ||
| 6589 | - r.3, | ||
| 6590 | - mv_to_q4(px, mv.1, luma_plane), | ||
| 6591 | - mv_to_q4(py, mv.0, luma_plane), | ||
| 6592 | - side, | ||
| 6593 | - side, | ||
| 6594 | - &mut pred[i], | ||
| 6595 | fctx, | 6731 | fctx, |
| 6596 | - ); | 6732 | + ) { |
| 6733 | + candidates.push((1, pred)); | ||
| 6597 | } | 6734 | } |
| 6598 | - let [mut py_buf, mut pu_buf, mut pv_buf] = pred; | 6735 | + } |
| 6599 | - crate::decode::obmc_run( | 6736 | + if warp_alphabet { |
| 6600 | - &plan, &refpix, &mut py_buf, &mut pu_buf, &mut pv_buf, fctx, | 6737 | + if let Some(pred) = warp_prediction( |
| 6601 | - ); | 6738 | + grid, |
| 6602 | - let u8s = |b: &[u16]| b.iter().map(|&v| v as u8).collect::<Vec<u8>>(); | 6739 | + (mi_row, mi_col), |
| 6740 | + (mi_rows, mi_cols), | ||
| 6741 | + (x, y), | ||
| 6742 | + BLOCK, | ||
| 6743 | + info.mv, | ||
| 6744 | + info.ref_frame, | ||
| 6745 | + planes, | ||
| 6746 | + fctx, | ||
| 6747 | + ) { | ||
| 6748 | + candidates.push((2, pred)); | ||
| 6749 | + } | ||
| 6750 | + } | ||
| 6751 | + for (motion, pred) in candidates { | ||
| 6603 | let luma_trial = luma.code_from_prediction( | 6752 | let luma_trial = luma.code_from_prediction( |
| 6604 | - x, y, BLOCK, &u8s(&py_buf), false, search.base_q_idx, search.deadzone, | 6753 | + x, y, BLOCK, &pred[0], false, search.base_q_idx, search.deadzone, |
| 6605 | TxbSet::Luma32Inter, | 6754 | TxbSet::Luma32Inter, |
| 6606 | ); | 6755 | ); |
| 6607 | let u = chroma[0].code_from_prediction( | 6756 | let u = chroma[0].code_from_prediction( |
| 6608 | - x / 2, y / 2, BLOCK / 2, &u8s(&pu_buf), false, search.base_q_idx, | 6757 | + x / 2, y / 2, BLOCK / 2, &pred[1], false, search.base_q_idx, search.deadzone, |
| 6609 | - search.deadzone, chroma_set, | 6758 | + chroma_set, |
| 6610 | ); | 6759 | ); |
| 6611 | let v = chroma[1].code_from_prediction( | 6760 | let v = chroma[1].code_from_prediction( |
| 6612 | - x / 2, y / 2, BLOCK / 2, &u8s(&pv_buf), false, search.base_q_idx, | 6761 | + x / 2, y / 2, BLOCK / 2, &pred[2], false, search.base_q_idx, search.deadzone, |
| 6613 | - search.deadzone, chroma_set, | 6762 | + chroma_set, |
| 6614 | ); | 6763 | ); |
| 6615 | let skip = luma_trial.levels.iter().all(|&l| l == 0) | 6764 | let skip = luma_trial.levels.iter().all(|&l| l == 0) |
| 6616 | && u.levels.iter().all(|&l| l == 0) | 6765 | && u.levels.iter().all(|&l| l == 0) |
| @@ -6622,10 +6771,10 @@ fn search_inter_block( | |||
| 6622 | * (skip_bits(skip) | 6771 | * (skip_bits(skip) |
| 6623 | + intra_inter_bits(true) | 6772 | + intra_inter_bits(true) |
| 6624 | + best.mode_bits | 6773 | + best.mode_bits |
| 6625 | - + obmc_bits(BLOCK, BLOCK, true) | 6774 | + + mm(motion) |
| 6626 | + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); | 6775 | + if skip { 0.0 } else { luma_trial.bits + u.bits + v.bits }); |
| 6627 | - if cost < best.cost + search.lambda * obmc_bits(BLOCK, BLOCK, false) { | 6776 | + if cost < best.cost + search.lambda * mm(motion_won) { |
| 6628 | - obmc_won = true; | 6777 | + motion_won = motion; |
| 6629 | best = Candidate { | 6778 | best = Candidate { |
| 6630 | cost, | 6779 | cost, |
| 6631 | luma: luma_trial, | 6780 | luma: luma_trial, |
| @@ -6639,7 +6788,6 @@ fn search_inter_block( | |||
| 6639 | } | 6788 | } |
| 6640 | } | 6789 | } |
| 6641 | } | 6790 | } |
| 6642 | - } | ||
| 6643 | // lane-av1txbits BUILT the transform-depth search this path lacks -- a | 6791 | // lane-av1txbits BUILT the transform-depth search this path lacks -- a |
| 6644 | // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 | 6792 | // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 |
| 6645 | // units against its flat trial, exactly as a 16x16 winner is -- and | 6793 | // units against its flat trial, exactly as a 16x16 winner is -- and |
| @@ -6661,7 +6809,7 @@ fn search_inter_block( | |||
| 6661 | uv_mode: DC_PRED, | 6809 | uv_mode: DC_PRED, |
| 6662 | skip: best.skip, | 6810 | skip: best.skip, |
| 6663 | inter: best.inter, | 6811 | inter: best.inter, |
| 6664 | - obmc: obmc_won, | 6812 | + motion_mode: motion_won, |
| 6665 | eight: None, | 6813 | eight: None, |
| 6666 | dv: None, | 6814 | dv: None, |
| 6667 | palette: None, | 6815 | palette: None, |
| @@ -7411,6 +7559,7 @@ pub(crate) fn encode_inter_frame( | |||
| 7411 | let start_cdfs = CdfSnapshot(cdfs); | 7559 | let start_cdfs = CdfSnapshot(cdfs); |
| 7412 | let reference_select_bit = header.reference_select; | 7560 | let reference_select_bit = header.reference_select; |
| 7413 | let switchable_motion_mode = header.is_motion_mode_switchable; | 7561 | let switchable_motion_mode = header.is_motion_mode_switchable; |
| 7562 | + let warped_motion = header.allow_warped_motion; | ||
| 7414 | let order_hint_bits = seq.order_hint_bits; | 7563 | let order_hint_bits = seq.order_hint_bits; |
| 7415 | let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; | 7564 | let lr_horz = crate::restoration::count_units(header.frame_width, 64) as u32; |
| 7416 | let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; | 7565 | let lr_vert = crate::restoration::count_units(header.frame_height, 64) as u32; |
| @@ -7433,6 +7582,7 @@ pub(crate) fn encode_inter_frame( | |||
| 7433 | // Thread-locals: every tile job arms its own worker. | 7582 | // Thread-locals: every tile job arms its own worker. |
| 7434 | crate::tile::arm_reference_select(reference_select_bit); | 7583 | crate::tile::arm_reference_select(reference_select_bit); |
| 7435 | crate::tile::arm_motion_mode(switchable_motion_mode); | 7584 | crate::tile::arm_motion_mode(switchable_motion_mode); |
| 7585 | + crate::tile::arm_warped_motion(warped_motion); | ||
| 7436 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); | 7586 | crate::tile::arm_order_hints(order_hint_bits, order_hint, order_hints); |
| 7437 | crate::tile::arm_screen(screen); | 7587 | crate::tile::arm_screen(screen); |
| 7438 | let mut cdfs = start_cdfs.0.clone(); | 7588 | let mut cdfs = start_cdfs.0.clone(); |
| @@ -8363,7 +8513,7 @@ mod tests { | |||
| 8363 | mv: (0, 0), | 8513 | mv: (0, 0), |
| 8364 | ref_mv_idx: 0, | 8514 | ref_mv_idx: 0, |
| 8365 | }), | 8515 | }), |
| 8366 | - obmc: false, | 8516 | + motion_mode: 0, |
| 8367 | }; | 8517 | }; |
| 8368 | let skipped_block = BlockCoeffs { | 8518 | let skipped_block = BlockCoeffs { |
| 8369 | skip: true, | 8519 | skip: true, |
| @@ -8387,6 +8537,7 @@ mod tests { | |||
| 8387 | // `is_motion_mode_switchable`, so the writer must be armed with it | 8537 | // `is_motion_mode_switchable`, so the writer must be armed with it |
| 8388 | // too or the decoder reads a `motion_mode` symbol nobody wrote. | 8538 | // too or the decoder reads a `motion_mode` symbol nobody wrote. |
| 8389 | crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable); | 8539 | crate::tile::arm_motion_mode(inter_header.is_motion_mode_switchable); |
| 8540 | + crate::tile::arm_warped_motion(inter_header.allow_warped_motion); | ||
| 8390 | let tile = | 8541 | let tile = |
| 8391 | crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) | 8542 | crate::tile::sb_coeff_inter_frame_tile(inter_header.mi_cols, inter_header.mi_rows, 100, &blocks) |
| 8392 | .unwrap(); | 8543 | .unwrap(); |
| @@ -11475,10 +11626,12 @@ mod tests { | |||
| 11475 | // `gate-blind-to-feature`). | 11626 | // `gate-blind-to-feature`). |
| 11476 | let mm = crate::tile::take_motion_mode_hits(); | 11627 | let mm = crate::tile::take_motion_mode_hits(); |
| 11477 | eprintln!( | 11628 | eprintln!( |
| 11478 | - "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \ | 11629 | + "{name}: motion_mode 32x32 SIMPLE={} OBMC={} WARP={} | 16x16 SIMPLE={} OBMC={} \ |
| 11479 | - 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)", | 11630 | + WARP={} | 8x8 SIMPLE={} OBMC={} WARP={} ({:.1}% OBMC / {:.1}% WARP of {} \ |
| 11480 | - mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], | 11631 | + eligible)", |
| 11481 | - 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | 11632 | + mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], mm[6], mm[7], mm[8], |
| 11633 | + 100.0 * (mm[1] + mm[4] + mm[7]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | ||
| 11634 | + 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | ||
| 11482 | mm.iter().sum::<usize>(), | 11635 | mm.iter().sum::<usize>(), |
| 11483 | ); | 11636 | ); |
| 11484 | let (aom, aom_wall) = | 11637 | let (aom, aom_wall) = |
| @@ -11695,10 +11848,12 @@ mod tests { | |||
| 11695 | // say whether the tool fired at all. | 11848 | // say whether the tool fired at all. |
| 11696 | let mm = crate::tile::take_motion_mode_hits(); | 11849 | let mm = crate::tile::take_motion_mode_hits(); |
| 11697 | eprintln!( | 11850 | eprintln!( |
| 11698 | - "{name}: motion_mode 32x32 SIMPLE={} OBMC={} | 16x16 SIMPLE={} OBMC={} | \ | 11851 | + "{name}: motion_mode 32x32 SIMPLE={} OBMC={} WARP={} | 16x16 SIMPLE={} OBMC={} \ |
| 11699 | - 8x8 SIMPLE={} OBMC={} ({:.1}% OBMC of {} eligible)", | 11852 | + WARP={} | 8x8 SIMPLE={} OBMC={} WARP={} ({:.1}% OBMC / {:.1}% WARP of {} \ |
| 11700 | - mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], | 11853 | + eligible)", |
| 11701 | - 100.0 * (mm[1] + mm[3] + mm[5]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | 11854 | + mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], mm[6], mm[7], mm[8], |
| 11855 | + 100.0 * (mm[1] + mm[4] + mm[7]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | ||
| 11856 | + 100.0 * (mm[2] + mm[5] + mm[8]) as f64 / mm.iter().sum::<usize>().max(1) as f64, | ||
| 11702 | mm.iter().sum::<usize>(), | 11857 | mm.iter().sum::<usize>(), |
| 11703 | ); | 11858 | ); |
| 11704 | let palette = crate::tile::take_palette_hits(); | 11859 | let palette = crate::tile::take_palette_hits(); |
crates/ec-av1/src/tile.rsmodified+88 -18
| @@ -390,6 +390,28 @@ thread_local! { | |||
| 390 | static MOTION_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | 390 | static MOTION_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; |
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | +thread_local! { | ||
| 394 | + /// This frame header's own `allow_warped_motion` bit (spec 5.9.2): with | ||
| 395 | + /// it set, every block libaom's `motion_mode_allowed` accepts AND that | ||
| 396 | + /// has at least one warp sample (`num_proj_ref >= 1`) reads the 3-symbol | ||
| 397 | + /// `motion_mode_cdf` alphabet instead of the 2-symbol `obmc_cdf` one. | ||
| 398 | + /// lane-av1obmc2. | ||
| 399 | + static WARPED_MOTION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; | ||
| 400 | + /// The frame geometry the warp-sample walk needs (`num_proj_ref` -> | ||
| 401 | + /// `has_top_right`), which is only ever this writer's own superblock | ||
| 402 | + /// size. Every stream this crate writes uses a 64 superblock, which is | ||
| 403 | + /// `FrameCtx::new`'s default. | ||
| 404 | + /// corner-cut: ceiling is a 128-superblock WRITER -- it must arm the real | ||
| 405 | + /// frame context here (or take one as a parameter) or the warp-sample | ||
| 406 | + /// walk reads the wrong `has_top_right` geometry. | ||
| 407 | + static WRITER_FCTX: crate::decode::FrameCtx = crate::decode::FrameCtx::new(); | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +/// Arms [`WARPED_MOTION`] for the next tile written on this thread. | ||
| 411 | +pub(crate) fn arm_warped_motion(on: bool) { | ||
| 412 | + WARPED_MOTION.with(|c| c.set(on)); | ||
| 413 | +} | ||
| 414 | + | ||
| 393 | /// Arms the frame's `is_motion_mode_switchable` bit for the next tile written | 415 | /// Arms the frame's `is_motion_mode_switchable` bit for the next tile written |
| 394 | /// on this thread; cleared by [`CdefIdxGuard`] when that writer returns. | 416 | /// on this thread; cleared by [`CdefIdxGuard`] when that writer returns. |
| 395 | pub(crate) fn arm_motion_mode(on: bool) { | 417 | pub(crate) fn arm_motion_mode(on: bool) { |
| @@ -401,16 +423,27 @@ pub(crate) fn arm_motion_mode(on: bool) { | |||
| 401 | /// -- the histogram a gate prints so that "OBMC is on" is a measurement, not | 423 | /// -- the histogram a gate prints so that "OBMC is on" is a measurement, not |
| 402 | /// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for | 424 | /// a claim (class `gate-blind-to-feature`). Index: `0 + 2 * bucket` for |
| 403 | /// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8. | 425 | /// SIMPLE, `1 + 2 * bucket` for OBMC, bucket 0/1/2 = 32x32/16x16/8x8. |
| 404 | -static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 6] = | 426 | +static MOTION_MODE_HITS: [std::sync::atomic::AtomicUsize; 9] = |
| 405 | - [const { std::sync::atomic::AtomicUsize::new(0) }; 6]; | 427 | + [const { std::sync::atomic::AtomicUsize::new(0) }; 9]; |
| 406 | 428 | ||
| 407 | /// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the | 429 | /// Reads [`MOTION_MODE_HITS`] and zeroes it, so a gate can attribute the |
| 408 | /// counts to its own encode. | 430 | /// counts to its own encode. |
| 409 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 431 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 410 | -pub(crate) fn take_motion_mode_hits() -> [usize; 6] { | 432 | +pub(crate) fn take_motion_mode_hits() -> [usize; 9] { |
| 411 | std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) | 433 | std::array::from_fn(|i| MOTION_MODE_HITS[i].swap(0, std::sync::atomic::Ordering::Relaxed)) |
| 412 | } | 434 | } |
| 413 | 435 | ||
| 436 | +/// What the 3-symbol `motion_mode` symbol costs, in bits, against the same | ||
| 437 | +/// frame tables [`obmc_symbol_bits`] prices the 2-symbol alphabet off -- | ||
| 438 | +/// which alphabet a block reads is `motion_mode_allowed`'s own choice | ||
| 439 | +/// ([`write_motion_mode`]). | ||
| 440 | +pub(crate) fn motion_mode_symbol_bits(row: usize, motion: u8) -> f64 { | ||
| 441 | + PRICING_BASE.with_borrow(|slot| match slot.as_deref() { | ||
| 442 | + Some(s) => crate::encode::symbol_bits(&s.0.motion_mode[row], usize::from(motion)), | ||
| 443 | + None => crate::encode::symbol_bits(&crate::cdf::MOTION_MODE[row], usize::from(motion)), | ||
| 444 | + }) | ||
| 445 | +} | ||
| 446 | + | ||
| 414 | /// What the `motion_mode`/`obmc` symbol costs, in bits, against the tables | 447 | /// What the `motion_mode`/`obmc` symbol costs, in bits, against the tables |
| 415 | /// this frame's writer really starts from ([`arm_pricing_cdfs`]) -- the same | 448 | /// this frame's writer really starts from ([`arm_pricing_cdfs`]) -- the same |
| 416 | /// tables the coefficients are priced off since lane-av1price2, rather than | 449 | /// tables the coefficients are priced off since lane-av1price2, rather than |
| @@ -445,7 +478,12 @@ fn write_motion_mode( | |||
| 445 | (bw4, bh4): (usize, usize), | 478 | (bw4, bh4): (usize, usize), |
| 446 | (write_w, write_h): (usize, usize), | 479 | (write_w, write_h): (usize, usize), |
| 447 | (mi_cols, mi_rows): (usize, usize), | 480 | (mi_cols, mi_rows): (usize, usize), |
| 448 | - obmc: bool, | 481 | + // This block's own reference, for the warp-sample walk that picks the |
| 482 | + // alphabet (`num_proj_ref`). | ||
| 483 | + ref_frame: i8, | ||
| 484 | + // 0 SIMPLE_TRANSLATION, 1 OBMC_CAUSAL, 2 WARPED_CAUSAL -- what the | ||
| 485 | + // encoder's search committed for this block. | ||
| 486 | + motion: u8, | ||
| 449 | ) -> Result<()> { | 487 | ) -> Result<()> { |
| 450 | let eligible = MOTION_MODE.with(std::cell::Cell::get) | 488 | let eligible = MOTION_MODE.with(std::cell::Cell::get) |
| 451 | && write_w.min(write_h) >= 8 | 489 | && write_w.min(write_h) >= 8 |
| @@ -453,9 +491,15 @@ fn write_motion_mode( | |||
| 453 | grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, | 491 | grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, |
| 454 | ); | 492 | ); |
| 455 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { | 493 | if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") { |
| 494 | + let proj = WRITER_FCTX.with(|fctx| { | ||
| 495 | + crate::decode::num_proj_ref( | ||
| 496 | + grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame, fctx, | ||
| 497 | + ) | ||
| 498 | + }); | ||
| 456 | eprintln!( | 499 | eprintln!( |
| 457 | - "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={}", | 500 | + "EC_MMW mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={} proj={proj} warp_armed={} motion={motion}", |
| 458 | - eligible as u8 | 501 | + eligible as u8, |
| 502 | + WARPED_MOTION.with(std::cell::Cell::get) as u8, | ||
| 459 | ); | 503 | ); |
| 460 | } | 504 | } |
| 461 | if !eligible { | 505 | if !eligible { |
| @@ -463,7 +507,7 @@ fn write_motion_mode( | |||
| 463 | // codes as SIMPLE_TRANSLATION: its reconstruction is not the | 507 | // codes as SIMPLE_TRANSLATION: its reconstruction is not the |
| 464 | // decoder's (class encoder-grid-drift), which is exactly what the | 508 | // decoder's (class encoder-grid-drift), which is exactly what the |
| 465 | // `EC_COMP_MISMATCH` rung exists to catch. | 509 | // `EC_COMP_MISMATCH` rung exists to catch. |
| 466 | - if obmc && crate::envflags::env_flag!("EC_COMP_MISMATCH") { | 510 | + if motion != 0 && crate::envflags::env_flag!("EC_COMP_MISMATCH") { |
| 467 | eprintln!( | 511 | eprintln!( |
| 468 | "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})" | 512 | "EC_COMP_MISMATCH motion_mode enc=OBMC wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})" |
| 469 | ); | 513 | ); |
| @@ -476,13 +520,37 @@ fn write_motion_mode( | |||
| 476 | format!("a {write_w}x{write_h} block has no motion_mode CDF row"), | 520 | format!("a {write_w}x{write_h} block has no motion_mode CDF row"), |
| 477 | ) | 521 | ) |
| 478 | })?; | 522 | })?; |
| 479 | - enc.symbol(usize::from(obmc), &mut cdfs.obmc[row]); | 523 | + // libaom `motion_mode_allowed`: the 3-symbol `motion_mode_cdf` alphabet |
| 524 | + // exactly when `allow_warped_motion` is on and the block has at least one | ||
| 525 | + // warp sample. The other two vetoes libaom applies there cannot fire for | ||
| 526 | + // a stream this encoder writes -- it codes no scaled reference and no | ||
| 527 | + // inter frame with `force_integer_mv` -- and the decoder checks all three | ||
| 528 | + // (`decode.rs`'s `warp_eligible`), so the two sides agree term for term. | ||
| 529 | + let warp_alphabet = WARPED_MOTION.with(std::cell::Cell::get) | ||
| 530 | + && WRITER_FCTX.with(|fctx| { | ||
| 531 | + crate::decode::num_proj_ref( | ||
| 532 | + grid, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows, ref_frame, fctx, | ||
| 533 | + ) | ||
| 534 | + }) >= 1; | ||
| 535 | + if warp_alphabet { | ||
| 536 | + enc.symbol(usize::from(motion), &mut cdfs.motion_mode[row]); | ||
| 537 | + } else { | ||
| 538 | + // A warp winner whose block the writer resolves to the 2-symbol | ||
| 539 | + // alphabet would reconstruct as SIMPLE at the decoder: the same | ||
| 540 | + // encoder-grid-drift rung the OBMC case above prints. | ||
| 541 | + if motion == 2 && crate::envflags::env_flag!("EC_COMP_MISMATCH") { | ||
| 542 | + eprintln!( | ||
| 543 | + "EC_COMP_MISMATCH motion_mode enc=WARP wrote=SIMPLE mi=({mi_row},{mi_col}) wh=({write_w}x{write_h})" | ||
| 544 | + ); | ||
| 545 | + } | ||
| 546 | + enc.symbol(usize::from(motion == 1), &mut cdfs.obmc[row]); | ||
| 547 | + } | ||
| 480 | let bucket = match write_w.min(write_h) { | 548 | let bucket = match write_w.min(write_h) { |
| 481 | 32.. => 0, | 549 | 32.. => 0, |
| 482 | 16 => 1, | 550 | 16 => 1, |
| 483 | _ => 2, | 551 | _ => 2, |
| 484 | }; | 552 | }; |
| 485 | - MOTION_MODE_HITS[usize::from(obmc) + 2 * bucket] | 553 | + MOTION_MODE_HITS[usize::from(motion.min(2)) + 3 * bucket] |
| 486 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 554 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 487 | Ok(()) | 555 | Ok(()) |
| 488 | } | 556 | } |
| @@ -1347,12 +1415,11 @@ pub struct BlockCoeffs { | |||
| 1347 | /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key | 1415 | /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key |
| 1348 | /// frame writers never read this field. | 1416 | /// frame writers never read this field. |
| 1349 | pub inter: Option<InterInfo>, | 1417 | pub inter: Option<InterInfo>, |
| 1350 | - /// Whether this (single-reference inter) block's prediction was built | 1418 | + /// This (single-reference inter) block's spec `motion_mode`, written by |
| 1351 | - /// with OBMC -- spec `motion_mode == OBMC_CAUSAL`, written by | 1419 | + /// [`write_motion_mode`]: 0 `SIMPLE_TRANSLATION` (the [`Default`], and |
| 1352 | - /// [`write_motion_mode`]. `false` (the [`Default`]) is | 1420 | + /// the only mode this writer coded before lane-av1obmc), 1 `OBMC_CAUSAL`, |
| 1353 | - /// `SIMPLE_TRANSLATION`, the only motion mode this writer coded before | 1421 | + /// 2 `WARPED_CAUSAL` (lane-av1obmc2). |
| 1354 | - /// lane-av1obmc. | 1422 | + pub motion_mode: u8, |
| 1355 | - pub obmc: bool, | ||
| 1356 | /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), | 1423 | /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), |
| 1357 | /// each with its own 8x8 luma transform and 4x4 chroma transforms, in | 1424 | /// each with its own 8x8 luma transform and 4x4 chroma transforms, in |
| 1358 | /// raster order among the leaves that are inside the true frame. `Some` | 1425 | /// raster order among the leaves that are inside the true frame. `Some` |
| @@ -5560,7 +5627,8 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( | |||
| 5560 | (8, 8), | 5627 | (8, 8), |
| 5561 | (BLOCK, BLOCK), | 5628 | (BLOCK, BLOCK), |
| 5562 | (mi_cols as usize, mi_rows as usize), | 5629 | (mi_cols as usize, mi_rows as usize), |
| 5563 | - block.obmc, | 5630 | + info.ref_frame, |
| 5631 | + block.motion_mode, | ||
| 5564 | )?; | 5632 | )?; |
| 5565 | mode_for_tx = 0; | 5633 | mode_for_tx = 0; |
| 5566 | } | 5634 | } |
| @@ -5794,7 +5862,8 @@ fn write_inter_frame_leaf( | |||
| 5794 | (SUB_MI as usize, SUB_MI as usize), | 5862 | (SUB_MI as usize, SUB_MI as usize), |
| 5795 | (SUB, SUB), | 5863 | (SUB, SUB), |
| 5796 | (mi_cols as usize, mi_rows as usize), | 5864 | (mi_cols as usize, mi_rows as usize), |
| 5797 | - block.obmc, | 5865 | + info.ref_frame, |
| 5866 | + block.motion_mode, | ||
| 5798 | )?; | 5867 | )?; |
| 5799 | mode_for_tx = 0; | 5868 | mode_for_tx = 0; |
| 5800 | } | 5869 | } |
| @@ -6034,7 +6103,8 @@ fn write_inter_frame_leaf8( | |||
| 6034 | (2, 2), | 6103 | (2, 2), |
| 6035 | (8, 8), | 6104 | (8, 8), |
| 6036 | (mi_cols as usize, mi_rows as usize), | 6105 | (mi_cols as usize, mi_rows as usize), |
| 6037 | - block.obmc, | 6106 | + info.ref_frame, |
| 6107 | + block.motion_mode, | ||
| 6038 | )?; | 6108 | )?; |
| 6039 | mode_for_tx = 0; | 6109 | mode_for_tx = 0; |
| 6040 | } | 6110 | } |