wip(av1): affine warp estimation + filter ported, wired into leaf16 -- COMPILING WIP
Browse files at this commitefe3e6564c40c6a6429e8d2c126d44d20c66228aParent: bd6939d3 changed files +811 -98
- crates/ec-av1/src/decode.rs +155 -98
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/warp.rs +655 -0
crates/ec-av1/src/decode.rsmodified+155 -98
| @@ -4150,18 +4150,17 @@ fn has_top_right(mi_row: usize, mi_col: usize, bs: usize) -> bool { | |||
| 4150 | has_tr | 4150 | has_tr |
| 4151 | } | 4151 | } |
| 4152 | 4152 | ||
| 4153 | -/// `av1_findSamples`' sample *count* only (`mvref_common.c`): the number of | 4153 | +/// `av1_findSamples` (`mvref_common.c`), full port: the up to |
| 4154 | -/// above/left/top-left/top-right neighbours (up to `LEAST_SQUARES_SAMPLES_MAX | 4154 | +/// `LEAST_SQUARES_SAMPLES_MAX` (8) above/left/top-left/top-right neighbour |
| 4155 | -/// = 8`) that share this block's own single reference frame and are | 4155 | +/// samples (`record_samples`) that share this block's own single reference |
| 4156 | -/// themselves single-ref -- exactly `mbmi->num_proj_ref`. The actual sample | 4156 | +/// frame and are themselves single-ref -- `mbmi->num_proj_ref` is |
| 4157 | -/// coordinates (`record_samples`, `pts`/`pts_inref`) feed the warp parameter | 4157 | +/// `.len()` of the returned vec. `bw4` is the block's own side in 4x4 units |
| 4158 | -/// *estimation* (`av1_find_projection`), not ported this round -- only the | 4158 | +/// (square, so also `bh4`; a real block's `xd->width`/`xd->height` in |
| 4159 | -/// count is needed to resolve `motion_mode_allowed`'s `WARPED_CAUSAL` vs | 4159 | +/// libaom are also mi units of that same square side here). Each sample's |
| 4160 | -/// `OBMC_CAUSAL` alphabet choice (spec 5.11.24), so that's all this port | 4160 | +/// own `bw`/`bh` (fed to [`crate::warp::record_sample`]'s offset formula) |
| 4161 | -/// keeps. `bw4` is the block's own side in 4x4 units (square, so also | 4161 | +/// come from the *neighbour* cell's own coded size, not this block's. |
| 4162 | -/// `bh4`; a real block's `xd->width`/`xd->height` in libaom are also mi | 4162 | +#[allow(clippy::too_many_arguments)] |
| 4163 | -/// units of that same square side here). | 4163 | +fn find_samples( |
| 4164 | -fn num_proj_ref( | ||
| 4165 | grid: &MiGrid, | 4164 | grid: &MiGrid, |
| 4166 | mi_row: usize, | 4165 | mi_row: usize, |
| 4167 | mi_col: usize, | 4166 | mi_col: usize, |
| @@ -4169,114 +4168,132 @@ fn num_proj_ref( | |||
| 4169 | mi_cols: usize, | 4168 | mi_cols: usize, |
| 4170 | mi_rows: usize, | 4169 | mi_rows: usize, |
| 4171 | ref_frame: i8, | 4170 | ref_frame: i8, |
| 4172 | -) -> u8 { | 4171 | +) -> Vec<crate::warp::Sample> { |
| 4173 | - const MAX: u8 = 8; | 4172 | + const MAX: usize = crate::warp::LEAST_SQUARES_SAMPLES_MAX; |
| 4174 | let up_available = mi_row > 0; | 4173 | let up_available = mi_row > 0; |
| 4175 | let left_available = mi_col > 0; | 4174 | let left_available = mi_col > 0; |
| 4176 | - let mut np = 0u8; | 4175 | + let mut samples = Vec::with_capacity(MAX); |
| 4177 | let single_ref_match = |info: &MiInfo| info.ref_frame == ref_frame && info.ref_frame1.is_none(); | 4176 | let single_ref_match = |info: &MiInfo| info.ref_frame == ref_frame && info.ref_frame1.is_none(); |
| 4178 | let mut do_tl = true; | 4177 | let mut do_tl = true; |
| 4179 | let mut do_tr = true; | 4178 | let mut do_tr = true; |
| 4179 | + let rec = |info: &MiInfo, row_offset: i32, sign_r: i32, col_offset: i32, sign_c: i32| { | ||
| 4180 | + let nb_bw = (info.size * 4) as i32; | ||
| 4181 | + crate::warp::record_sample(nb_bw, nb_bw, info.mv, row_offset, sign_r, col_offset, sign_c) | ||
| 4182 | + }; | ||
| 4180 | 4183 | ||
| 4181 | - if up_available { | 4184 | + 'outer: { |
| 4182 | - let above = grid.get(mi_row - 1, mi_col); | 4185 | + if up_available { |
| 4183 | - let superblock_width = above.map_or(1, |c| c.size).max(1); | 4186 | + let above = grid.get(mi_row - 1, mi_col); |
| 4184 | - if bw4 <= superblock_width { | 4187 | + let superblock_width = above.map_or(1, |c| c.size).max(1); |
| 4185 | - let col_offset = -((mi_col % superblock_width) as isize); | 4188 | + if bw4 <= superblock_width { |
| 4186 | - if col_offset < 0 { | 4189 | + let col_offset = -((mi_col % superblock_width) as isize); |
| 4187 | - do_tl = false; | 4190 | + if col_offset < 0 { |
| 4188 | - } | 4191 | + do_tl = false; |
| 4189 | - if col_offset + superblock_width as isize > bw4 as isize { | ||
| 4190 | - do_tr = false; | ||
| 4191 | - } | ||
| 4192 | - if let Some(info) = above { | ||
| 4193 | - if single_ref_match(info) { | ||
| 4194 | - np += 1; | ||
| 4195 | - if np >= MAX { | ||
| 4196 | - return MAX; | ||
| 4197 | - } | ||
| 4198 | } | 4192 | } |
| 4199 | - } | 4193 | + if col_offset + superblock_width as isize > bw4 as isize { |
| 4200 | - } else { | 4194 | + do_tr = false; |
| 4201 | - let mut i = 0usize; | 4195 | + } |
| 4202 | - let limit = bw4.min(mi_cols.saturating_sub(mi_col)); | 4196 | + if let Some(info) = above { |
| 4203 | - while i < limit { | ||
| 4204 | - let cell = grid.get(mi_row - 1, mi_col + i); | ||
| 4205 | - let sw = cell.map_or(1, |c| c.size).max(1); | ||
| 4206 | - if let Some(info) = cell { | ||
| 4207 | if single_ref_match(info) { | 4197 | if single_ref_match(info) { |
| 4208 | - np += 1; | 4198 | + samples.push(rec(info, 0, -1, col_offset as i32, 1)); |
| 4209 | - if np >= MAX { | 4199 | + if samples.len() >= MAX { |
| 4210 | - return MAX; | 4200 | + break 'outer; |
| 4201 | + } | ||
| 4202 | + } | ||
| 4203 | + } | ||
| 4204 | + } else { | ||
| 4205 | + let mut i = 0usize; | ||
| 4206 | + let limit = bw4.min(mi_cols.saturating_sub(mi_col)); | ||
| 4207 | + while i < limit { | ||
| 4208 | + let cell = grid.get(mi_row - 1, mi_col + i); | ||
| 4209 | + let sw = cell.map_or(1, |c| c.size).max(1); | ||
| 4210 | + if let Some(info) = cell { | ||
| 4211 | + if single_ref_match(info) { | ||
| 4212 | + samples.push(rec(info, 0, -1, i as i32, 1)); | ||
| 4213 | + if samples.len() >= MAX { | ||
| 4214 | + break 'outer; | ||
| 4215 | + } | ||
| 4211 | } | 4216 | } |
| 4212 | } | 4217 | } |
| 4218 | + i += sw; | ||
| 4213 | } | 4219 | } |
| 4214 | - i += sw; | ||
| 4215 | } | 4220 | } |
| 4216 | } | 4221 | } |
| 4217 | - } | ||
| 4218 | 4222 | ||
| 4219 | - if left_available { | 4223 | + if left_available { |
| 4220 | - let left = grid.get(mi_row, mi_col - 1); | 4224 | + let left = grid.get(mi_row, mi_col - 1); |
| 4221 | - let superblock_height = left.map_or(1, |c| c.size).max(1); | 4225 | + let superblock_height = left.map_or(1, |c| c.size).max(1); |
| 4222 | - if bw4 <= superblock_height { | 4226 | + if bw4 <= superblock_height { |
| 4223 | - let row_offset = -((mi_row % superblock_height) as isize); | 4227 | + let row_offset = -((mi_row % superblock_height) as isize); |
| 4224 | - if row_offset < 0 { | 4228 | + if row_offset < 0 { |
| 4225 | - do_tl = false; | 4229 | + do_tl = false; |
| 4226 | - } | ||
| 4227 | - if let Some(info) = left { | ||
| 4228 | - if single_ref_match(info) { | ||
| 4229 | - np += 1; | ||
| 4230 | - if np >= MAX { | ||
| 4231 | - return MAX; | ||
| 4232 | - } | ||
| 4233 | } | 4230 | } |
| 4234 | - } | 4231 | + if let Some(info) = left { |
| 4235 | - } else { | ||
| 4236 | - let mut i = 0usize; | ||
| 4237 | - let limit = bw4.min(mi_rows.saturating_sub(mi_row)); | ||
| 4238 | - while i < limit { | ||
| 4239 | - let cell = grid.get(mi_row + i, mi_col - 1); | ||
| 4240 | - let sh = cell.map_or(1, |c| c.size).max(1); | ||
| 4241 | - if let Some(info) = cell { | ||
| 4242 | if single_ref_match(info) { | 4232 | if single_ref_match(info) { |
| 4243 | - np += 1; | 4233 | + samples.push(rec(info, row_offset as i32, 1, 0, -1)); |
| 4244 | - if np >= MAX { | 4234 | + if samples.len() >= MAX { |
| 4245 | - return MAX; | 4235 | + break 'outer; |
| 4236 | + } | ||
| 4237 | + } | ||
| 4238 | + } | ||
| 4239 | + } else { | ||
| 4240 | + let mut i = 0usize; | ||
| 4241 | + let limit = bw4.min(mi_rows.saturating_sub(mi_row)); | ||
| 4242 | + while i < limit { | ||
| 4243 | + let cell = grid.get(mi_row + i, mi_col - 1); | ||
| 4244 | + let sh = cell.map_or(1, |c| c.size).max(1); | ||
| 4245 | + if let Some(info) = cell { | ||
| 4246 | + if single_ref_match(info) { | ||
| 4247 | + samples.push(rec(info, i as i32, 1, 0, -1)); | ||
| 4248 | + if samples.len() >= MAX { | ||
| 4249 | + break 'outer; | ||
| 4250 | + } | ||
| 4246 | } | 4251 | } |
| 4247 | } | 4252 | } |
| 4253 | + i += sh; | ||
| 4248 | } | 4254 | } |
| 4249 | - i += sh; | ||
| 4250 | } | 4255 | } |
| 4251 | } | 4256 | } |
| 4252 | - } | ||
| 4253 | 4257 | ||
| 4254 | - if do_tl && left_available && up_available { | 4258 | + if do_tl && left_available && up_available { |
| 4255 | - if let Some(info) = grid.get(mi_row - 1, mi_col - 1) { | 4259 | + if let Some(info) = grid.get(mi_row - 1, mi_col - 1) { |
| 4256 | - if single_ref_match(info) { | 4260 | + if single_ref_match(info) { |
| 4257 | - np += 1; | 4261 | + samples.push(rec(info, 0, -1, 0, -1)); |
| 4258 | - if np >= MAX { | 4262 | + if samples.len() >= MAX { |
| 4259 | - return MAX; | 4263 | + break 'outer; |
| 4264 | + } | ||
| 4260 | } | 4265 | } |
| 4261 | } | 4266 | } |
| 4262 | } | 4267 | } |
| 4263 | - } | ||
| 4264 | 4268 | ||
| 4265 | - if do_tr && has_top_right(mi_row, mi_col, bw4) { | 4269 | + if do_tr && has_top_right(mi_row, mi_col, bw4) { |
| 4266 | - let tr_col = mi_col + bw4; | 4270 | + let tr_col = mi_col + bw4; |
| 4267 | - if mi_row > 0 && tr_col < mi_cols { | 4271 | + if mi_row > 0 && tr_col < mi_cols { |
| 4268 | - if let Some(info) = grid.get(mi_row - 1, tr_col) { | 4272 | + if let Some(info) = grid.get(mi_row - 1, tr_col) { |
| 4269 | - if single_ref_match(info) { | 4273 | + if single_ref_match(info) { |
| 4270 | - np += 1; | 4274 | + samples.push(rec(info, 0, -1, bw4 as i32, 1)); |
| 4271 | - if np >= MAX { | ||
| 4272 | - return MAX; | ||
| 4273 | } | 4275 | } |
| 4274 | } | 4276 | } |
| 4275 | } | 4277 | } |
| 4276 | } | 4278 | } |
| 4277 | } | 4279 | } |
| 4280 | + samples | ||
| 4281 | +} | ||
| 4278 | 4282 | ||
| 4279 | - np | 4283 | +/// `mbmi->num_proj_ref`: [`find_samples`]'s own count, all that |
| 4284 | +/// `motion_mode_allowed` needs to pick the 3-symbol `motion_mode_cdf` over | ||
| 4285 | +/// the 2-symbol `obmc_cdf` (spec 5.11.24). | ||
| 4286 | +#[allow(clippy::too_many_arguments)] | ||
| 4287 | +fn num_proj_ref( | ||
| 4288 | + grid: &MiGrid, | ||
| 4289 | + mi_row: usize, | ||
| 4290 | + mi_col: usize, | ||
| 4291 | + bw4: usize, | ||
| 4292 | + mi_cols: usize, | ||
| 4293 | + mi_rows: usize, | ||
| 4294 | + ref_frame: i8, | ||
| 4295 | +) -> u8 { | ||
| 4296 | + find_samples(grid, mi_row, mi_col, bw4, mi_cols, mi_rows, ref_frame).len() as u8 | ||
| 4280 | } | 4297 | } |
| 4281 | 4298 | ||
| 4282 | /// `av1_get_obmc_mask` (`reconinter.c`): the per-position blend weight | 4299 | /// `av1_get_obmc_mask` (`reconinter.c`): the per-position blend weight |
| @@ -5645,14 +5662,20 @@ fn decode_inter_block( | |||
| 5645 | || !overlappable_left(grid, mi_row, mi_col, bw4, mi_rows as usize, 1) | 5662 | || !overlappable_left(grid, mi_row, mi_col, bw4, mi_rows as usize, 1) |
| 5646 | .is_empty()); | 5663 | .is_empty()); |
| 5647 | let mut obmc_selected = false; | 5664 | let mut obmc_selected = false; |
| 5665 | + // lane-warp round 2: `Some` when motion_mode == WARPED_CAUSAL | ||
| 5666 | + // *and* the local warp estimate is valid (`!wm_params.invalid`, | ||
| 5667 | + // `av1_find_projection`) -- an invalid estimate falls back to | ||
| 5668 | + // this block's own translational mv, same as any other block | ||
| 5669 | + // (spec `allow_warp`/`reconinter.c` -- global warp is never the | ||
| 5670 | + // fallback here since this decoder's global motion is always | ||
| 5671 | + // IDENTITY). | ||
| 5672 | + let mut warp_params: Option<crate::warp::WarpParams> = None; | ||
| 5648 | if motion_mode_eligible { | 5673 | if motion_mode_eligible { |
| 5649 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3. | 5674 | // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 -> 0/1/2/3. |
| 5650 | let bsize_idx = (side.trailing_zeros() - 3) as usize; | 5675 | let bsize_idx = (side.trailing_zeros() - 3) as usize; |
| 5651 | - // lane-warp round 1: `motion_mode_allowed` reads the 3-symbol | 5676 | + // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` |
| 5652 | - // `motion_mode_cdf` instead of the 2-symbol `obmc_cdf` exactly | 5677 | + // instead of the 2-symbol `obmc_cdf` exactly when |
| 5653 | - // when `num_proj_ref >= 1` under `allow_warped_motion` (see | 5678 | + // `num_proj_ref >= 1` under `allow_warped_motion`. |
| 5654 | - // `num_proj_ref`'s own doc -- only the *count* is ported, | ||
| 5655 | - // not the sample coordinates the warp estimation needs). | ||
| 5656 | let warp_eligible = allow_warped_motion | 5679 | let warp_eligible = allow_warped_motion |
| 5657 | && num_proj_ref(grid, mi_row, mi_col, bw4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1; | 5680 | && num_proj_ref(grid, mi_row, mi_col, bw4, mi_cols as usize, mi_rows as usize, ref_frame) >= 1; |
| 5658 | if warp_eligible { | 5681 | if warp_eligible { |
| @@ -5665,11 +5688,27 @@ fn decode_inter_block( | |||
| 5665 | } | 5688 | } |
| 5666 | _ => { | 5689 | _ => { |
| 5667 | WARP_SELECTED_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 5690 | WARP_SELECTED_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 5668 | - return Err(unsupported( | 5691 | + let mut samples = find_samples( |
| 5669 | - "a block that coded WARPED_CAUSAL (motion_mode == 2): \ | 5692 | + grid, |
| 5670 | - av1_find_projection/the affine warp filter are not \ | 5693 | + mi_row, |
| 5671 | - ported, only motion_mode_allowed's alphabet choice is", | 5694 | + mi_col, |
| 5672 | - )); | 5695 | + bw4, |
| 5696 | + mi_cols as usize, | ||
| 5697 | + mi_rows as usize, | ||
| 5698 | + ref_frame, | ||
| 5699 | + ); | ||
| 5700 | + if samples.len() > 1 { | ||
| 5701 | + crate::warp::select_samples(mv, &mut samples, side as i32, side as i32); | ||
| 5702 | + } | ||
| 5703 | + warp_params = crate::warp::find_projection( | ||
| 5704 | + &samples, | ||
| 5705 | + side as i32, | ||
| 5706 | + side as i32, | ||
| 5707 | + mv.1, | ||
| 5708 | + mv.0, | ||
| 5709 | + mi_row as i32, | ||
| 5710 | + mi_col as i32, | ||
| 5711 | + ); | ||
| 5673 | } | 5712 | } |
| 5674 | } | 5713 | } |
| 5675 | } else { | 5714 | } else { |
| @@ -5796,6 +5835,24 @@ fn decode_inter_block( | |||
| 5796 | &mut pred_v, | 5835 | &mut pred_v, |
| 5797 | ); | 5836 | ); |
| 5798 | 5837 | ||
| 5838 | + if let Some(params) = &warp_params { | ||
| 5839 | + crate::warp::warp_affine( | ||
| 5840 | + params, &py_ref.data, py_ref.true_width as i32, py_ref.true_height as i32, | ||
| 5841 | + py_ref.width as i32, &mut pred_y, px as i32, py as i32, side as i32, | ||
| 5842 | + side as i32, side as i32, 0, 0, | ||
| 5843 | + ); | ||
| 5844 | + crate::warp::warp_affine( | ||
| 5845 | + params, &pu_ref.data, pu_ref.true_width as i32, pu_ref.true_height as i32, | ||
| 5846 | + pu_ref.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32, | ||
| 5847 | + chroma_side as i32, chroma_side as i32, 1, 1, | ||
| 5848 | + ); | ||
| 5849 | + crate::warp::warp_affine( | ||
| 5850 | + params, &pv_ref.data, pv_ref.true_width as i32, pv_ref.true_height as i32, | ||
| 5851 | + pv_ref.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32, | ||
| 5852 | + chroma_side as i32, chroma_side as i32, 1, 1, | ||
| 5853 | + ); | ||
| 5854 | + } | ||
| 5855 | + | ||
| 5799 | if obmc_selected { | 5856 | if obmc_selected { |
| 5800 | obmc_blend( | 5857 | obmc_blend( |
| 5801 | grid, | 5858 | grid, |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -33,3 +33,4 @@ pub mod sequence; | |||
| 33 | pub mod stream; | 33 | pub mod stream; |
| 34 | pub mod tile; | 34 | pub mod tile; |
| 35 | pub mod transform; | 35 | pub mod transform; |
| 36 | +mod warp; | ||
crates/ec-av1/src/warp.rsadded+655 -0
| @@ -0,0 +1,655 @@ | |||
| 1 | +//! Local warp (`WARPED_CAUSAL`) motion prediction, spec 7.11.3.6-.8 / | ||
| 2 | +//! `av1/common/warped_motion.c`: estimate a 2x3 affine model from up to | ||
| 3 | +//! `LEAST_SQUARES_SAMPLES_MAX` neighbour-block motion samples | ||
| 4 | +//! ([`find_projection`]), then filter the reference through it with an 8x8 | ||
| 5 | +//! tiled, two-pass 8-tap warped filter ([`warp_affine`]). 8-bit, | ||
| 6 | +//! non-compound only (this decoder's warp arm only ever reaches a | ||
| 7 | +//! single-ref, non-skip-mode block, spec `is_motion_variation_allowed_bsize` | ||
| 8 | +//! -- `conv_params->is_compound` is always false on that path, so libaom's | ||
| 9 | +//! compound/highbd branches are dropped). | ||
| 10 | + | ||
| 11 | +/// `WARPEDMODEL_PREC_BITS` (`av1/common/mv.h`). | ||
| 12 | +const WARPEDMODEL_PREC_BITS: u32 = 16; | ||
| 13 | +/// `WARPEDMODEL_TRANS_CLAMP`. | ||
| 14 | +const WARPEDMODEL_TRANS_CLAMP: i64 = 128 << WARPEDMODEL_PREC_BITS; | ||
| 15 | +/// `WARPEDMODEL_NONDIAGAFFINE_CLAMP`. | ||
| 16 | +const WARPEDMODEL_NONDIAGAFFINE_CLAMP: i64 = 1 << (WARPEDMODEL_PREC_BITS - 3); | ||
| 17 | +/// `WARPEDPIXEL_PREC_BITS`. | ||
| 18 | +const WARPEDPIXEL_PREC_BITS: u32 = 6; | ||
| 19 | +/// `WARPEDPIXEL_PREC_SHIFTS`. | ||
| 20 | +const WARPEDPIXEL_PREC_SHIFTS: i32 = 1 << WARPEDPIXEL_PREC_BITS; | ||
| 21 | +/// `WARPEDDIFF_PREC_BITS`. | ||
| 22 | +const WARPEDDIFF_PREC_BITS: u32 = WARPEDMODEL_PREC_BITS - WARPEDPIXEL_PREC_BITS; | ||
| 23 | +/// `WARP_PARAM_REDUCE_BITS`. | ||
| 24 | +const WARP_PARAM_REDUCE_BITS: u32 = 6; | ||
| 25 | +/// `MI_SIZE` (4x4 luma pixels). | ||
| 26 | +const MI_SIZE: i32 = 4; | ||
| 27 | +/// `FILTER_BITS`. | ||
| 28 | +const FILTER_BITS: i32 = 7; | ||
| 29 | +/// `LEAST_SQUARES_SAMPLES_MAX`. | ||
| 30 | +pub const LEAST_SQUARES_SAMPLES_MAX: usize = 8; | ||
| 31 | +/// `LS_MV_MAX` (max mv in 1/8-pel, `find_affine_int`'s own sample gate). | ||
| 32 | +const LS_MV_MAX: i32 = 256; | ||
| 33 | +/// `LS_STEP`. | ||
| 34 | +const LS_STEP: i32 = 8; | ||
| 35 | + | ||
| 36 | +/// `Round2Signed` (spec 4.7): halves round away from zero's own sign. | ||
| 37 | +fn round2_signed(value: i64, n: u32) -> i64 { | ||
| 38 | + if value < 0 { | ||
| 39 | + -round2(-value, n) | ||
| 40 | + } else { | ||
| 41 | + round2(value, n) | ||
| 42 | + } | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +fn round2(value: i64, n: u32) -> i64 { | ||
| 46 | + (value + (1i64 << n >> 1)) >> n | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +/// `get_msb` (`aom_ports/bitops.h`): index of the highest set bit. | ||
| 50 | +fn get_msb64(n: u64) -> i32 { | ||
| 51 | + debug_assert!(n != 0); | ||
| 52 | + 63 - n.leading_zeros() as i32 | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +/// `resolve_divisor_64`: decomposes `d` such that `1/d = mult / 2^shift`. | ||
| 56 | +fn resolve_divisor_64(d: u64) -> (i64, i32) { | ||
| 57 | + const DIV_LUT_PREC_BITS: i32 = 14; | ||
| 58 | + const DIV_LUT_BITS: i32 = 8; | ||
| 59 | + let mut shift = get_msb64(d); | ||
| 60 | + let e = d as i64 - (1i64 << shift); | ||
| 61 | + let f = if shift > DIV_LUT_BITS { | ||
| 62 | + round2(e, (shift - DIV_LUT_BITS) as u32) | ||
| 63 | + } else { | ||
| 64 | + e << (DIV_LUT_BITS - shift) | ||
| 65 | + }; | ||
| 66 | + debug_assert!((0..=256).contains(&f)); | ||
| 67 | + shift += DIV_LUT_PREC_BITS; | ||
| 68 | + (DIV_LUT[f as usize] as i64, shift) | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +/// `resolve_divisor_32`, same decomposition for a 32-bit divisor (this port | ||
| 72 | +/// only ever needs the 64-bit form, `av1_get_shear_params`'s `mat[2]` is | ||
| 73 | +/// magnitude-bounded well under 32 bits too, so reuses [`resolve_divisor_64`] | ||
| 74 | +/// -- bit-identical for any value that fits both). | ||
| 75 | +fn resolve_divisor_32(d: u32) -> (i64, i32) { | ||
| 76 | + resolve_divisor_64(d as u64) | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +fn clamp(v: i64, lo: i64, hi: i64) -> i64 { | ||
| 80 | + v.clamp(lo, hi) | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +/// One recorded least-squares sample: `(pts1, pts2)`, each `(x, y)` in | ||
| 84 | +/// 1/8-pel, relative to the current block's own top-left (spec | ||
| 85 | +/// `record_samples`). | ||
| 86 | +#[derive(Clone, Copy)] | ||
| 87 | +pub struct Sample { | ||
| 88 | + pub pts1: (i32, i32), | ||
| 89 | + pub pts2: (i32, i32), | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +/// `record_samples` (`mvref_common.c`): one neighbour's own sample, `bw`/`bh` | ||
| 93 | +/// its block size in luma pixels, `(nb_mv_col, nb_mv_row)` its own motion | ||
| 94 | +/// vector (1/8-pel). | ||
| 95 | +#[allow(clippy::too_many_arguments)] | ||
| 96 | +pub fn record_sample( | ||
| 97 | + bw: i32, | ||
| 98 | + bh: i32, | ||
| 99 | + nb_mv: (i32, i32), | ||
| 100 | + row_offset: i32, | ||
| 101 | + sign_r: i32, | ||
| 102 | + col_offset: i32, | ||
| 103 | + sign_c: i32, | ||
| 104 | +) -> Sample { | ||
| 105 | + let x = col_offset * MI_SIZE + sign_c * bw / 2 - 1; | ||
| 106 | + let y = row_offset * MI_SIZE + sign_r * bh / 2 - 1; | ||
| 107 | + let pts1 = (x * 8, y * 8); | ||
| 108 | + let pts2 = (pts1.0 + nb_mv.1, pts1.1 + nb_mv.0); | ||
| 109 | + Sample { pts1, pts2 } | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +/// The resolved affine warp model, spec 7.11.3.6's `LocalWarpParams` -- | ||
| 113 | +/// `wmmat[2..6]` (the 2x2 linear part) plus the shear decomposition | ||
| 114 | +/// `av1_get_shear_params` derives from it. `wmmat[0]`/`[1]` are the | ||
| 115 | +/// translation terms. | ||
| 116 | +#[derive(Clone, Copy, Debug)] | ||
| 117 | +pub struct WarpParams { | ||
| 118 | + pub wmmat: [i32; 6], | ||
| 119 | + pub alpha: i16, | ||
| 120 | + pub beta: i16, | ||
| 121 | + pub gamma: i16, | ||
| 122 | + pub delta: i16, | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +/// `LS_SQUARE`/`LS_PRODUCT1`/`LS_PRODUCT2` (`warped_motion.c`): the | ||
| 126 | +/// downshifted quadratic terms `find_affine_int`'s normal-equation matrix | ||
| 127 | +/// accumulates, exploiting `LS_STEP == 8` (bottom 2 bits of every term are 0). | ||
| 128 | +fn ls_square(a: i64) -> i64 { | ||
| 129 | + (a * a * 4 + a * 4 * LS_STEP as i64 + (LS_STEP * LS_STEP * 2) as i64) >> 4 | ||
| 130 | +} | ||
| 131 | +fn ls_product1(a: i64, b: i64) -> i64 { | ||
| 132 | + (a * b * 4 + (a + b) * 2 * LS_STEP as i64 + (LS_STEP * LS_STEP) as i64) >> 4 | ||
| 133 | +} | ||
| 134 | +fn ls_product2(a: i64, b: i64) -> i64 { | ||
| 135 | + (a * b * 4 + (a + b) * 2 * LS_STEP as i64 + (LS_STEP * LS_STEP * 2) as i64) >> 4 | ||
| 136 | +} | ||
| 137 | + | ||
| 138 | +fn get_mult_shift_ndiag(px: i64, i_det: i64, shift: i32) -> i32 { | ||
| 139 | + let v = px * i_det; | ||
| 140 | + clamp( | ||
| 141 | + round2_signed(v, shift as u32), | ||
| 142 | + -WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1, | ||
| 143 | + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1, | ||
| 144 | + ) as i32 | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +fn get_mult_shift_diag(px: i64, i_det: i64, shift: i32) -> i32 { | ||
| 148 | + let v = px * i_det; | ||
| 149 | + clamp( | ||
| 150 | + round2_signed(v, shift as u32), | ||
| 151 | + (1i64 << WARPEDMODEL_PREC_BITS) - WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1, | ||
| 152 | + (1i64 << WARPEDMODEL_PREC_BITS) + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1, | ||
| 153 | + ) as i32 | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +/// `find_affine_int` (`warped_motion.c`): the least-squares affine fit over | ||
| 157 | +/// `samples`, `bw`/`bh` the current block's own size in luma pixels, `(mvx, | ||
| 158 | +/// mvy)` its own coded motion vector (1/8-pel, spec's own `mvx`/`mvy` order: | ||
| 159 | +/// `col`, `row`), `mi_row`/`mi_col` its own `mi` position. Returns `None` | ||
| 160 | +/// when the accumulated matrix is singular (`Det == 0`, spec's "found = 0" | ||
| 161 | +/// path -- caller still falls back to the block's own translational MV, | ||
| 162 | +/// matching libaom's own shear-validity fallback). | ||
| 163 | +fn find_affine_int( | ||
| 164 | + samples: &[Sample], | ||
| 165 | + bw: i32, | ||
| 166 | + bh: i32, | ||
| 167 | + mvx: i32, | ||
| 168 | + mvy: i32, | ||
| 169 | + mi_row: i32, | ||
| 170 | + mi_col: i32, | ||
| 171 | +) -> Option<[i32; 6]> { | ||
| 172 | + let mut a = [[0i64; 2]; 2]; | ||
| 173 | + let mut bx = [0i64; 2]; | ||
| 174 | + let mut by = [0i64; 2]; | ||
| 175 | + | ||
| 176 | + let rsuy = bh / 2 - 1; | ||
| 177 | + let rsux = bw / 2 - 1; | ||
| 178 | + let suy = rsuy * 8; | ||
| 179 | + let sux = rsux * 8; | ||
| 180 | + let duy = suy + mvy; | ||
| 181 | + let dux = sux + mvx; | ||
| 182 | + | ||
| 183 | + for s in samples { | ||
| 184 | + let dx = s.pts2.0 - dux; | ||
| 185 | + let dy = s.pts2.1 - duy; | ||
| 186 | + let sx = s.pts1.0 - sux; | ||
| 187 | + let sy = s.pts1.1 - suy; | ||
| 188 | + if (sx - dx).abs() < LS_MV_MAX && (sy - dy).abs() < LS_MV_MAX { | ||
| 189 | + let (sx, sy, dx, dy) = (sx as i64, sy as i64, dx as i64, dy as i64); | ||
| 190 | + a[0][0] += ls_square(sx); | ||
| 191 | + a[0][1] += ls_product1(sx, sy); | ||
| 192 | + a[1][1] += ls_square(sy); | ||
| 193 | + bx[0] += ls_product2(sx, dx); | ||
| 194 | + bx[1] += ls_product1(sy, dx); | ||
| 195 | + by[0] += ls_product1(sx, dy); | ||
| 196 | + by[1] += ls_product2(sy, dy); | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + let det = a[0][0] * a[1][1] - a[0][1] * a[0][1]; | ||
| 201 | + if det == 0 { | ||
| 202 | + return None; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + let (mut i_det, mut shift) = resolve_divisor_64(det.unsigned_abs()); | ||
| 206 | + if det < 0 { | ||
| 207 | + i_det = -i_det; | ||
| 208 | + } | ||
| 209 | + shift -= WARPEDMODEL_PREC_BITS as i32; | ||
| 210 | + if shift < 0 { | ||
| 211 | + i_det <<= -shift; | ||
| 212 | + shift = 0; | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + let px0 = a[1][1] * bx[0] - a[0][1] * bx[1]; | ||
| 216 | + let px1 = -a[0][1] * bx[0] + a[0][0] * bx[1]; | ||
| 217 | + let py0 = a[1][1] * by[0] - a[0][1] * by[1]; | ||
| 218 | + let py1 = -a[0][1] * by[0] + a[0][0] * by[1]; | ||
| 219 | + | ||
| 220 | + let mut wmmat = [0i32; 6]; | ||
| 221 | + wmmat[2] = get_mult_shift_diag(px0, i_det, shift); | ||
| 222 | + wmmat[3] = get_mult_shift_ndiag(px1, i_det, shift); | ||
| 223 | + wmmat[4] = get_mult_shift_ndiag(py0, i_det, shift); | ||
| 224 | + wmmat[5] = get_mult_shift_diag(py1, i_det, shift); | ||
| 225 | + | ||
| 226 | + let isuy = (mi_row * MI_SIZE + rsuy) as i64; | ||
| 227 | + let isux = (mi_col * MI_SIZE + rsux) as i64; | ||
| 228 | + let vx = mvx as i64 * (1i64 << (WARPEDMODEL_PREC_BITS - 3)) | ||
| 229 | + - (isux * (wmmat[2] as i64 - (1i64 << WARPEDMODEL_PREC_BITS)) + isuy * wmmat[3] as i64); | ||
| 230 | + let vy = mvy as i64 * (1i64 << (WARPEDMODEL_PREC_BITS - 3)) | ||
| 231 | + - (isux * wmmat[4] as i64 + isuy * (wmmat[5] as i64 - (1i64 << WARPEDMODEL_PREC_BITS))); | ||
| 232 | + wmmat[0] = clamp(vx, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1) as i32; | ||
| 233 | + wmmat[1] = clamp(vy, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1) as i32; | ||
| 234 | + Some(wmmat) | ||
| 235 | +} | ||
| 236 | + | ||
| 237 | +/// `av1_get_shear_params`: `None` on an invalid model (`mat[2] <= 0`, or the | ||
| 238 | +/// reduced shear terms fail `is_affine_shear_allowed` -- spec's own | ||
| 239 | +/// "warp filter cannot represent this" fallback). | ||
| 240 | +fn get_shear_params(wmmat: [i32; 6]) -> Option<(i16, i16, i16, i16)> { | ||
| 241 | + if wmmat[2] <= 0 { | ||
| 242 | + return None; | ||
| 243 | + } | ||
| 244 | + let mat = wmmat.map(|v| v as i64); | ||
| 245 | + let mut alpha = clamp(mat[2] - (1 << WARPEDMODEL_PREC_BITS), i16::MIN as i64, i16::MAX as i64); | ||
| 246 | + let beta = clamp(mat[3], i16::MIN as i64, i16::MAX as i64); | ||
| 247 | + let (y_mag, shift) = resolve_divisor_32(mat[2].unsigned_abs() as u32); | ||
| 248 | + let y = if mat[2] < 0 { -y_mag } else { y_mag }; | ||
| 249 | + let v = (mat[4] * (1 << WARPEDMODEL_PREC_BITS)) * y; | ||
| 250 | + let mut gamma = clamp(round2_signed(v, shift as u32), i16::MIN as i64, i16::MAX as i64); | ||
| 251 | + let v = (mat[3] * mat[4]) * y; | ||
| 252 | + let mut delta = clamp( | ||
| 253 | + mat[5] - round2_signed(v, shift as u32) - (1 << WARPEDMODEL_PREC_BITS), | ||
| 254 | + i16::MIN as i64, | ||
| 255 | + i16::MAX as i64, | ||
| 256 | + ); | ||
| 257 | + | ||
| 258 | + alpha = round2_signed(alpha, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS; | ||
| 259 | + let beta = round2_signed(beta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS; | ||
| 260 | + gamma = round2_signed(gamma, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS; | ||
| 261 | + delta = round2_signed(delta, WARP_PARAM_REDUCE_BITS) << WARP_PARAM_REDUCE_BITS; | ||
| 262 | + | ||
| 263 | + if (4 * alpha.abs() + 7 * beta.abs() >= (1 << WARPEDMODEL_PREC_BITS)) | ||
| 264 | + || (4 * gamma.abs() + 4 * delta.abs() >= (1 << WARPEDMODEL_PREC_BITS)) | ||
| 265 | + { | ||
| 266 | + return None; | ||
| 267 | + } | ||
| 268 | + Some((alpha as i16, beta as i16, gamma as i16, delta as i16)) | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +/// `av1_selectSamples` (`mvref_common.c`): keeps only the samples whose own | ||
| 272 | +/// MV agrees with the block's coded `mv` (`(row, col)`) within | ||
| 273 | +/// `clamp(max(bw, bh), 16, 112)` (1/8-pel), run only when more than one | ||
| 274 | +/// sample was found (`mbmi->num_proj_ref > 1` gate, `decodemv.c`). Always | ||
| 275 | +/// keeps at least one (the C code's own `AOMMAX(ret, 1)` floor -- this port | ||
| 276 | +/// mirrors it by never truncating below index 1 when the filter would | ||
| 277 | +/// otherwise empty the vec). | ||
| 278 | +pub fn select_samples(mv: (i32, i32), samples: &mut Vec<Sample>, bw: i32, bh: i32) { | ||
| 279 | + let thresh = bw.max(bh).clamp(16, 112); | ||
| 280 | + let mut kept: Vec<Sample> = samples | ||
| 281 | + .iter() | ||
| 282 | + .copied() | ||
| 283 | + .filter(|s| { | ||
| 284 | + let dx = s.pts2.0 - s.pts1.0 - mv.1; | ||
| 285 | + let dy = s.pts2.1 - s.pts1.1 - mv.0; | ||
| 286 | + dx.abs() + dy.abs() <= thresh | ||
| 287 | + }) | ||
| 288 | + .collect(); | ||
| 289 | + if kept.is_empty() { | ||
| 290 | + kept.push(samples[0]); | ||
| 291 | + } | ||
| 292 | + *samples = kept; | ||
| 293 | +} | ||
| 294 | + | ||
| 295 | +/// `av1_find_projection`: estimates the affine model, `None` when either the | ||
| 296 | +/// least-squares fit is singular or the resulting shear is not | ||
| 297 | +/// filter-representable -- both spec fallback cases collapse to the same | ||
| 298 | +/// "use the block's own translational MV instead" caller behaviour. | ||
| 299 | +pub fn find_projection( | ||
| 300 | + samples: &[Sample], | ||
| 301 | + bw: i32, | ||
| 302 | + bh: i32, | ||
| 303 | + mvx: i32, | ||
| 304 | + mvy: i32, | ||
| 305 | + mi_row: i32, | ||
| 306 | + mi_col: i32, | ||
| 307 | +) -> Option<WarpParams> { | ||
| 308 | + let wmmat = find_affine_int(samples, bw, bh, mvx, mvy, mi_row, mi_col)?; | ||
| 309 | + let (alpha, beta, gamma, delta) = get_shear_params(wmmat)?; | ||
| 310 | + Some(WarpParams { | ||
| 311 | + wmmat, | ||
| 312 | + alpha, | ||
| 313 | + beta, | ||
| 314 | + gamma, | ||
| 315 | + delta, | ||
| 316 | + }) | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +fn clip_pixel(v: i32) -> u8 { | ||
| 320 | + v.clamp(0, 255) as u8 | ||
| 321 | +} | ||
| 322 | + | ||
| 323 | +/// `av1_warp_affine_c` (`warped_motion.c`), 8-bit, non-compound only: | ||
| 324 | +/// filters `reference` (row-major, `stride`, true extent `width`x`height`) | ||
| 325 | +/// through `params`' affine model into `dst` (row-major, `p_stride`), an | ||
| 326 | +/// `p_width`x`p_height` block whose top-left sits at `(p_col, p_row)` in the | ||
| 327 | +/// (possibly chroma-subsampled) plane `dst` belongs to. | ||
| 328 | +#[allow(clippy::too_many_arguments)] | ||
| 329 | +pub fn warp_affine( | ||
| 330 | + params: &WarpParams, | ||
| 331 | + reference: &[u8], | ||
| 332 | + width: i32, | ||
| 333 | + height: i32, | ||
| 334 | + stride: i32, | ||
| 335 | + dst: &mut [u8], | ||
| 336 | + p_col: i32, | ||
| 337 | + p_row: i32, | ||
| 338 | + p_width: i32, | ||
| 339 | + p_height: i32, | ||
| 340 | + p_stride: i32, | ||
| 341 | + subsampling_x: i32, | ||
| 342 | + subsampling_y: i32, | ||
| 343 | +) { | ||
| 344 | + let mat = ¶ms.wmmat; | ||
| 345 | + let (alpha, beta, gamma, delta) = ( | ||
| 346 | + params.alpha as i64, | ||
| 347 | + params.beta as i64, | ||
| 348 | + params.gamma as i64, | ||
| 349 | + params.delta as i64, | ||
| 350 | + ); | ||
| 351 | + const BD: i32 = 8; | ||
| 352 | + let reduce_bits_horiz = 3; // conv_params->round_0, non-compound 8-bit (InterRound0) | ||
| 353 | + let reduce_bits_vert = 2 * FILTER_BITS - reduce_bits_horiz; // InterRound1 | ||
| 354 | + let offset_bits_horiz = BD + FILTER_BITS - 1; | ||
| 355 | + let offset_bits_vert = BD + 2 * FILTER_BITS - reduce_bits_horiz; | ||
| 356 | + | ||
| 357 | + let mut i = p_row; | ||
| 358 | + while i < p_row + p_height { | ||
| 359 | + let mut j = p_col; | ||
| 360 | + while j < p_col + p_width { | ||
| 361 | + let src_x = ((j + 4) << subsampling_x) as i64; | ||
| 362 | + let src_y = ((i + 4) << subsampling_y) as i64; | ||
| 363 | + let dst_x = mat[2] as i64 * src_x + mat[3] as i64 * src_y + mat[0] as i64; | ||
| 364 | + let dst_y = mat[4] as i64 * src_x + mat[5] as i64 * src_y + mat[1] as i64; | ||
| 365 | + let x4 = dst_x >> subsampling_x; | ||
| 366 | + let y4 = dst_y >> subsampling_y; | ||
| 367 | + | ||
| 368 | + let ix4 = (x4 >> WARPEDMODEL_PREC_BITS) as i32; | ||
| 369 | + let mut sx4 = (x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i64; | ||
| 370 | + let iy4 = (y4 >> WARPEDMODEL_PREC_BITS) as i32; | ||
| 371 | + let mut sy4 = (y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1)) as i64; | ||
| 372 | + | ||
| 373 | + sx4 += alpha * -4 + beta * -4; | ||
| 374 | + sy4 += gamma * -4 + delta * -4; | ||
| 375 | + sx4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1); | ||
| 376 | + sy4 &= !((1i64 << WARP_PARAM_REDUCE_BITS) - 1); | ||
| 377 | + | ||
| 378 | + let mut tmp = [[0i32; 8]; 15]; | ||
| 379 | + for k in -7..8i32 { | ||
| 380 | + let iy = (iy4 + k).clamp(0, height - 1); | ||
| 381 | + let mut sx = sx4 + beta * (k as i64 + 4); | ||
| 382 | + for l in -4..4i32 { | ||
| 383 | + let ix = ix4 + l - 3; | ||
| 384 | + let offs = | ||
| 385 | + (round2(sx, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS) as usize; | ||
| 386 | + let coeffs = &AV1_WARPED_FILTER[offs]; | ||
| 387 | + let mut sum: i32 = 1 << offset_bits_horiz; | ||
| 388 | + for (m, &c) in coeffs.iter().enumerate() { | ||
| 389 | + let sample_x = (ix + m as i32).clamp(0, width - 1); | ||
| 390 | + sum += i32::from(reference[(iy * stride + sample_x) as usize]) * c as i32; | ||
| 391 | + } | ||
| 392 | + sum = round2(sum as i64, reduce_bits_horiz as u32) as i32; | ||
| 393 | + tmp[(k + 7) as usize][(l + 4) as usize] = sum; | ||
| 394 | + sx += alpha; | ||
| 395 | + } | ||
| 396 | + } | ||
| 397 | + | ||
| 398 | + let k_hi = (4i32).min(p_row + p_height - i - 4); | ||
| 399 | + let mut k = -4i32; | ||
| 400 | + while k < k_hi { | ||
| 401 | + let mut sy = sy4 + delta * (k as i64 + 4); | ||
| 402 | + let l_hi = (4i32).min(p_col + p_width - j - 4); | ||
| 403 | + let mut l = -4i32; | ||
| 404 | + while l < l_hi { | ||
| 405 | + let offs = | ||
| 406 | + (round2(sy, WARPEDDIFF_PREC_BITS) as i32 + WARPEDPIXEL_PREC_SHIFTS) as usize; | ||
| 407 | + let coeffs = &AV1_WARPED_FILTER[offs]; | ||
| 408 | + let mut sum: i32 = 1 << offset_bits_vert; | ||
| 409 | + for m in 0..8i32 { | ||
| 410 | + sum += tmp[(k + m + 4) as usize][(l + 4) as usize] * coeffs[m as usize] as i32; | ||
| 411 | + } | ||
| 412 | + sum = round2(sum as i64, reduce_bits_vert as u32) as i32; | ||
| 413 | + let out_row = (i - p_row + k + 4) as usize; | ||
| 414 | + let out_col = (j - p_col + l + 4) as usize; | ||
| 415 | + dst[out_row * p_stride as usize + out_col] = | ||
| 416 | + clip_pixel(sum - (1 << (BD - 1)) - (1 << BD)); | ||
| 417 | + sy += gamma; | ||
| 418 | + l += 1; | ||
| 419 | + } | ||
| 420 | + k += 1; | ||
| 421 | + } | ||
| 422 | + j += 8; | ||
| 423 | + } | ||
| 424 | + i += 8; | ||
| 425 | + } | ||
| 426 | +} | ||
| 427 | + | ||
| 428 | +/// `av1_warped_filter` (`warped_motion.c`): 193 rows (`WARPEDPIXEL_PREC_SHIFTS | ||
| 429 | +/// * 3 + 1`), one per 1/64-pel fraction across `[-1, 2)`, each an 8-tap | ||
| 430 | +/// kernel (row 192 is a dummy replicate of row 191, transcribed verbatim). | ||
| 431 | +#[rustfmt::skip] | ||
| 432 | +const AV1_WARPED_FILTER: [[i16; 8]; 193] = [ | ||
| 433 | + [0, 0, 127, 1, 0, 0, 0, 0], | ||
| 434 | + [0, -1, 127, 2, 0, 0, 0, 0], | ||
| 435 | + [1, -3, 127, 4, -1, 0, 0, 0], | ||
| 436 | + [1, -4, 126, 6, -2, 1, 0, 0], | ||
| 437 | + [1, -5, 126, 8, -3, 1, 0, 0], | ||
| 438 | + [1, -6, 125, 11, -4, 1, 0, 0], | ||
| 439 | + [1, -7, 124, 13, -4, 1, 0, 0], | ||
| 440 | + [2, -8, 123, 15, -5, 1, 0, 0], | ||
| 441 | + [2, -9, 122, 18, -6, 1, 0, 0], | ||
| 442 | + [2, -10, 121, 20, -6, 1, 0, 0], | ||
| 443 | + [2, -11, 120, 22, -7, 2, 0, 0], | ||
| 444 | + [2, -12, 119, 25, -8, 2, 0, 0], | ||
| 445 | + [3, -13, 117, 27, -8, 2, 0, 0], | ||
| 446 | + [3, -13, 116, 29, -9, 2, 0, 0], | ||
| 447 | + [3, -14, 114, 32, -10, 3, 0, 0], | ||
| 448 | + [3, -15, 113, 35, -10, 2, 0, 0], | ||
| 449 | + [3, -15, 111, 37, -11, 3, 0, 0], | ||
| 450 | + [3, -16, 109, 40, -11, 3, 0, 0], | ||
| 451 | + [3, -16, 108, 42, -12, 3, 0, 0], | ||
| 452 | + [4, -17, 106, 45, -13, 3, 0, 0], | ||
| 453 | + [4, -17, 104, 47, -13, 3, 0, 0], | ||
| 454 | + [4, -17, 102, 50, -14, 3, 0, 0], | ||
| 455 | + [4, -17, 100, 52, -14, 3, 0, 0], | ||
| 456 | + [4, -18, 98, 55, -15, 4, 0, 0], | ||
| 457 | + [4, -18, 96, 58, -15, 3, 0, 0], | ||
| 458 | + [4, -18, 94, 60, -16, 4, 0, 0], | ||
| 459 | + [4, -18, 91, 63, -16, 4, 0, 0], | ||
| 460 | + [4, -18, 89, 65, -16, 4, 0, 0], | ||
| 461 | + [4, -18, 87, 68, -17, 4, 0, 0], | ||
| 462 | + [4, -18, 85, 70, -17, 4, 0, 0], | ||
| 463 | + [4, -18, 82, 73, -17, 4, 0, 0], | ||
| 464 | + [4, -18, 80, 75, -17, 4, 0, 0], | ||
| 465 | + [4, -18, 78, 78, -18, 4, 0, 0], | ||
| 466 | + [4, -17, 75, 80, -18, 4, 0, 0], | ||
| 467 | + [4, -17, 73, 82, -18, 4, 0, 0], | ||
| 468 | + [4, -17, 70, 85, -18, 4, 0, 0], | ||
| 469 | + [4, -17, 68, 87, -18, 4, 0, 0], | ||
| 470 | + [4, -16, 65, 89, -18, 4, 0, 0], | ||
| 471 | + [4, -16, 63, 91, -18, 4, 0, 0], | ||
| 472 | + [4, -16, 60, 94, -18, 4, 0, 0], | ||
| 473 | + [3, -15, 58, 96, -18, 4, 0, 0], | ||
| 474 | + [4, -15, 55, 98, -18, 4, 0, 0], | ||
| 475 | + [3, -14, 52, 100, -17, 4, 0, 0], | ||
| 476 | + [3, -14, 50, 102, -17, 4, 0, 0], | ||
| 477 | + [3, -13, 47, 104, -17, 4, 0, 0], | ||
| 478 | + [3, -13, 45, 106, -17, 4, 0, 0], | ||
| 479 | + [3, -12, 42, 108, -16, 3, 0, 0], | ||
| 480 | + [3, -11, 40, 109, -16, 3, 0, 0], | ||
| 481 | + [3, -11, 37, 111, -15, 3, 0, 0], | ||
| 482 | + [2, -10, 35, 113, -15, 3, 0, 0], | ||
| 483 | + [3, -10, 32, 114, -14, 3, 0, 0], | ||
| 484 | + [2, -9, 29, 116, -13, 3, 0, 0], | ||
| 485 | + [2, -8, 27, 117, -13, 3, 0, 0], | ||
| 486 | + [2, -8, 25, 119, -12, 2, 0, 0], | ||
| 487 | + [2, -7, 22, 120, -11, 2, 0, 0], | ||
| 488 | + [1, -6, 20, 121, -10, 2, 0, 0], | ||
| 489 | + [1, -6, 18, 122, -9, 2, 0, 0], | ||
| 490 | + [1, -5, 15, 123, -8, 2, 0, 0], | ||
| 491 | + [1, -4, 13, 124, -7, 1, 0, 0], | ||
| 492 | + [1, -4, 11, 125, -6, 1, 0, 0], | ||
| 493 | + [1, -3, 8, 126, -5, 1, 0, 0], | ||
| 494 | + [1, -2, 6, 126, -4, 1, 0, 0], | ||
| 495 | + [0, -1, 4, 127, -3, 1, 0, 0], | ||
| 496 | + [0, 0, 2, 127, -1, 0, 0, 0], | ||
| 497 | + [0, 0, 0, 127, 1, 0, 0, 0], | ||
| 498 | + [0, 0, -1, 127, 2, 0, 0, 0], | ||
| 499 | + [0, 1, -3, 127, 4, -2, 1, 0], | ||
| 500 | + [0, 1, -5, 127, 6, -2, 1, 0], | ||
| 501 | + [0, 2, -6, 126, 8, -3, 1, 0], | ||
| 502 | + [-1, 2, -7, 126, 11, -4, 2, -1], | ||
| 503 | + [-1, 3, -8, 125, 13, -5, 2, -1], | ||
| 504 | + [-1, 3, -10, 124, 16, -6, 3, -1], | ||
| 505 | + [-1, 4, -11, 123, 18, -7, 3, -1], | ||
| 506 | + [-1, 4, -12, 122, 20, -7, 3, -1], | ||
| 507 | + [-1, 4, -13, 121, 23, -8, 3, -1], | ||
| 508 | + [-2, 5, -14, 120, 25, -9, 4, -1], | ||
| 509 | + [-1, 5, -15, 119, 27, -10, 4, -1], | ||
| 510 | + [-1, 5, -16, 118, 30, -11, 4, -1], | ||
| 511 | + [-2, 6, -17, 116, 33, -12, 5, -1], | ||
| 512 | + [-2, 6, -17, 114, 35, -12, 5, -1], | ||
| 513 | + [-2, 6, -18, 113, 38, -13, 5, -1], | ||
| 514 | + [-2, 7, -19, 111, 41, -14, 6, -2], | ||
| 515 | + [-2, 7, -19, 110, 43, -15, 6, -2], | ||
| 516 | + [-2, 7, -20, 108, 46, -15, 6, -2], | ||
| 517 | + [-2, 7, -20, 106, 49, -16, 6, -2], | ||
| 518 | + [-2, 7, -21, 104, 51, -16, 7, -2], | ||
| 519 | + [-2, 7, -21, 102, 54, -17, 7, -2], | ||
| 520 | + [-2, 8, -21, 100, 56, -18, 7, -2], | ||
| 521 | + [-2, 8, -22, 98, 59, -18, 7, -2], | ||
| 522 | + [-2, 8, -22, 96, 62, -19, 7, -2], | ||
| 523 | + [-2, 8, -22, 94, 64, -19, 7, -2], | ||
| 524 | + [-2, 8, -22, 91, 67, -20, 8, -2], | ||
| 525 | + [-2, 8, -22, 89, 69, -20, 8, -2], | ||
| 526 | + [-2, 8, -22, 87, 72, -21, 8, -2], | ||
| 527 | + [-2, 8, -21, 84, 74, -21, 8, -2], | ||
| 528 | + [-2, 8, -22, 82, 77, -21, 8, -2], | ||
| 529 | + [-2, 8, -21, 79, 79, -21, 8, -2], | ||
| 530 | + [-2, 8, -21, 77, 82, -22, 8, -2], | ||
| 531 | + [-2, 8, -21, 74, 84, -21, 8, -2], | ||
| 532 | + [-2, 8, -21, 72, 87, -22, 8, -2], | ||
| 533 | + [-2, 8, -20, 69, 89, -22, 8, -2], | ||
| 534 | + [-2, 8, -20, 67, 91, -22, 8, -2], | ||
| 535 | + [-2, 7, -19, 64, 94, -22, 8, -2], | ||
| 536 | + [-2, 7, -19, 62, 96, -22, 8, -2], | ||
| 537 | + [-2, 7, -18, 59, 98, -22, 8, -2], | ||
| 538 | + [-2, 7, -18, 56, 100, -21, 8, -2], | ||
| 539 | + [-2, 7, -17, 54, 102, -21, 7, -2], | ||
| 540 | + [-2, 7, -16, 51, 104, -21, 7, -2], | ||
| 541 | + [-2, 6, -16, 49, 106, -20, 7, -2], | ||
| 542 | + [-2, 6, -15, 46, 108, -20, 7, -2], | ||
| 543 | + [-2, 6, -15, 43, 110, -19, 7, -2], | ||
| 544 | + [-2, 6, -14, 41, 111, -19, 7, -2], | ||
| 545 | + [-1, 5, -13, 38, 113, -18, 6, -2], | ||
| 546 | + [-1, 5, -12, 35, 114, -17, 6, -2], | ||
| 547 | + [-1, 5, -12, 33, 116, -17, 6, -2], | ||
| 548 | + [-1, 4, -11, 30, 118, -16, 5, -1], | ||
| 549 | + [-1, 4, -10, 27, 119, -15, 5, -1], | ||
| 550 | + [-1, 4, -9, 25, 120, -14, 5, -2], | ||
| 551 | + [-1, 3, -8, 23, 121, -13, 4, -1], | ||
| 552 | + [-1, 3, -7, 20, 122, -12, 4, -1], | ||
| 553 | + [-1, 3, -7, 18, 123, -11, 4, -1], | ||
| 554 | + [-1, 3, -6, 16, 124, -10, 3, -1], | ||
| 555 | + [-1, 2, -5, 13, 125, -8, 3, -1], | ||
| 556 | + [-1, 2, -4, 11, 126, -7, 2, -1], | ||
| 557 | + [0, 1, -3, 8, 126, -6, 2, 0], | ||
| 558 | + [0, 1, -2, 6, 127, -5, 1, 0], | ||
| 559 | + [0, 1, -2, 4, 127, -3, 1, 0], | ||
| 560 | + [0, 0, 0, 2, 127, -1, 0, 0], | ||
| 561 | + [0, 0, 0, 1, 127, 0, 0, 0], | ||
| 562 | + [0, 0, 0, -1, 127, 2, 0, 0], | ||
| 563 | + [0, 0, 1, -3, 127, 4, -1, 0], | ||
| 564 | + [0, 0, 1, -4, 126, 6, -2, 1], | ||
| 565 | + [0, 0, 1, -5, 126, 8, -3, 1], | ||
| 566 | + [0, 0, 1, -6, 125, 11, -4, 1], | ||
| 567 | + [0, 0, 1, -7, 124, 13, -4, 1], | ||
| 568 | + [0, 0, 2, -8, 123, 15, -5, 1], | ||
| 569 | + [0, 0, 2, -9, 122, 18, -6, 1], | ||
| 570 | + [0, 0, 2, -10, 121, 20, -6, 1], | ||
| 571 | + [0, 0, 2, -11, 120, 22, -7, 2], | ||
| 572 | + [0, 0, 2, -12, 119, 25, -8, 2], | ||
| 573 | + [0, 0, 3, -13, 117, 27, -8, 2], | ||
| 574 | + [0, 0, 3, -13, 116, 29, -9, 2], | ||
| 575 | + [0, 0, 3, -14, 114, 32, -10, 3], | ||
| 576 | + [0, 0, 3, -15, 113, 35, -10, 2], | ||
| 577 | + [0, 0, 3, -15, 111, 37, -11, 3], | ||
| 578 | + [0, 0, 3, -16, 109, 40, -11, 3], | ||
| 579 | + [0, 0, 3, -16, 108, 42, -12, 3], | ||
| 580 | + [0, 0, 4, -17, 106, 45, -13, 3], | ||
| 581 | + [0, 0, 4, -17, 104, 47, -13, 3], | ||
| 582 | + [0, 0, 4, -17, 102, 50, -14, 3], | ||
| 583 | + [0, 0, 4, -17, 100, 52, -14, 3], | ||
| 584 | + [0, 0, 4, -18, 98, 55, -15, 4], | ||
| 585 | + [0, 0, 4, -18, 96, 58, -15, 3], | ||
| 586 | + [0, 0, 4, -18, 94, 60, -16, 4], | ||
| 587 | + [0, 0, 4, -18, 91, 63, -16, 4], | ||
| 588 | + [0, 0, 4, -18, 89, 65, -16, 4], | ||
| 589 | + [0, 0, 4, -18, 87, 68, -17, 4], | ||
| 590 | + [0, 0, 4, -18, 85, 70, -17, 4], | ||
| 591 | + [0, 0, 4, -18, 82, 73, -17, 4], | ||
| 592 | + [0, 0, 4, -18, 80, 75, -17, 4], | ||
| 593 | + [0, 0, 4, -18, 78, 78, -18, 4], | ||
| 594 | + [0, 0, 4, -17, 75, 80, -18, 4], | ||
| 595 | + [0, 0, 4, -17, 73, 82, -18, 4], | ||
| 596 | + [0, 0, 4, -17, 70, 85, -18, 4], | ||
| 597 | + [0, 0, 4, -17, 68, 87, -18, 4], | ||
| 598 | + [0, 0, 4, -16, 65, 89, -18, 4], | ||
| 599 | + [0, 0, 4, -16, 63, 91, -18, 4], | ||
| 600 | + [0, 0, 4, -16, 60, 94, -18, 4], | ||
| 601 | + [0, 0, 3, -15, 58, 96, -18, 4], | ||
| 602 | + [0, 0, 4, -15, 55, 98, -18, 4], | ||
| 603 | + [0, 0, 3, -14, 52, 100, -17, 4], | ||
| 604 | + [0, 0, 3, -14, 50, 102, -17, 4], | ||
| 605 | + [0, 0, 3, -13, 47, 104, -17, 4], | ||
| 606 | + [0, 0, 3, -13, 45, 106, -17, 4], | ||
| 607 | + [0, 0, 3, -12, 42, 108, -16, 3], | ||
| 608 | + [0, 0, 3, -11, 40, 109, -16, 3], | ||
| 609 | + [0, 0, 3, -11, 37, 111, -15, 3], | ||
| 610 | + [0, 0, 2, -10, 35, 113, -15, 3], | ||
| 611 | + [0, 0, 3, -10, 32, 114, -14, 3], | ||
| 612 | + [0, 0, 2, -9, 29, 116, -13, 3], | ||
| 613 | + [0, 0, 2, -8, 27, 117, -13, 3], | ||
| 614 | + [0, 0, 2, -8, 25, 119, -12, 2], | ||
| 615 | + [0, 0, 2, -7, 22, 120, -11, 2], | ||
| 616 | + [0, 0, 1, -6, 20, 121, -10, 2], | ||
| 617 | + [0, 0, 1, -6, 18, 122, -9, 2], | ||
| 618 | + [0, 0, 1, -5, 15, 123, -8, 2], | ||
| 619 | + [0, 0, 1, -4, 13, 124, -7, 1], | ||
| 620 | + [0, 0, 1, -4, 11, 125, -6, 1], | ||
| 621 | + [0, 0, 1, -3, 8, 126, -5, 1], | ||
| 622 | + [0, 0, 1, -2, 6, 126, -4, 1], | ||
| 623 | + [0, 0, 0, -1, 4, 127, -3, 1], | ||
| 624 | + [0, 0, 0, 0, 2, 127, -1, 0], | ||
| 625 | + [0, 0, 0, 0, 2, 127, -1, 0], | ||
| 626 | +]; | ||
| 627 | + | ||
| 628 | +/// `div_lut` (`warped_motion.c`). | ||
| 629 | +#[rustfmt::skip] | ||
| 630 | +const DIV_LUT: [u16; 257] = [ | ||
| 631 | + 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768, | ||
| 632 | + 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142, | ||
| 633 | + 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564, | ||
| 634 | + 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028, | ||
| 635 | + 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530, | ||
| 636 | + 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066, | ||
| 637 | + 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633, | ||
| 638 | + 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228, | ||
| 639 | + 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848, | ||
| 640 | + 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491, | ||
| 641 | + 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155, | ||
| 642 | + 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838, | ||
| 643 | + 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538, | ||
| 644 | + 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255, | ||
| 645 | + 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986, | ||
| 646 | + 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732, | ||
| 647 | + 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489, | ||
| 648 | + 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259, | ||
| 649 | + 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039, | ||
| 650 | + 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830, | ||
| 651 | + 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630, | ||
| 652 | + 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439, | ||
| 653 | + 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257, | ||
| 654 | + 8240, 8224, 8208, 8192, | ||
| 655 | +]; | ||