Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <limits.h> |
| 13 | #include <math.h> |
| 14 | #include <stdio.h> |
| 15 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 16 | #include "config/aom_config.h" |
Tom Finegan | 44702c8 | 2018-05-22 13:00:39 -0700 | [diff] [blame] | 17 | #include "config/aom_dsp_rtcd.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 18 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 19 | #include "aom_dsp/aom_dsp_common.h" |
| 20 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | #include "aom_ports/mem.h" |
| 22 | |
Wan-Teh Chang | f2d15ee | 2020-03-10 09:24:43 -0700 | [diff] [blame] | 23 | #include "av1/common/av1_common_int.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | #include "av1/common/common.h" |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 25 | #include "av1/common/filter.h" |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 26 | #include "av1/common/mvref_common.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 27 | #include "av1/common/reconinter.h" |
| 28 | |
| 29 | #include "av1/encoder/encoder.h" |
Angie Chiang | 0904092 | 2018-06-14 15:37:50 -0700 | [diff] [blame] | 30 | #include "av1/encoder/encodemv.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 31 | #include "av1/encoder/mcomp.h" |
| 32 | #include "av1/encoder/rdopt.h" |
Urvang Joshi | 7500f80 | 2018-07-31 11:47:01 -0700 | [diff] [blame] | 33 | #include "av1/encoder/reconinter_enc.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 34 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 35 | static INLINE void init_mv_cost_params(MV_COST_PARAMS *mv_cost_params, |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 36 | const MvCosts *mv_costs, |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 37 | const MV *ref_mv) { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 38 | mv_cost_params->ref_mv = ref_mv; |
| 39 | mv_cost_params->full_ref_mv = get_fullmv_from_mv(ref_mv); |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 40 | mv_cost_params->mv_cost_type = MV_COST_ENTROPY; |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 41 | mv_cost_params->error_per_bit = mv_costs->errorperbit; |
| 42 | mv_cost_params->sad_per_bit = mv_costs->sadperbit; |
| 43 | mv_cost_params->mvjcost = mv_costs->nmv_joint_cost; |
| 44 | mv_cost_params->mvcost[0] = mv_costs->mv_cost_stack[0]; |
| 45 | mv_cost_params->mvcost[1] = mv_costs->mv_cost_stack[1]; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static INLINE void init_ms_buffers(MSBuffers *ms_buffers, const MACROBLOCK *x) { |
| 49 | ms_buffers->ref = &x->e_mbd.plane[0].pre[0]; |
| 50 | ms_buffers->src = &x->plane[0].src; |
| 51 | |
chiyotsai | 4d1b7d9 | 2020-04-07 13:56:22 -0700 | [diff] [blame] | 52 | av1_set_ms_compound_refs(ms_buffers, NULL, NULL, 0, 0); |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 53 | |
chiyotsai | d2b1221 | 2020-04-28 20:57:19 -0700 | [diff] [blame] | 54 | ms_buffers->wsrc = x->obmc_buffer.wsrc; |
| 55 | ms_buffers->obmc_mask = x->obmc_buffer.mask; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 56 | } |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 57 | |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 58 | static AOM_INLINE SEARCH_METHODS |
| 59 | get_faster_search_method(SEARCH_METHODS search_method) { |
| 60 | // Note on search method's accuracy: |
| 61 | // 1. NSTEP |
| 62 | // 2. DIAMOND |
| 63 | // 3. BIGDIA \approx SQUARE |
| 64 | // 4. HEX. |
| 65 | // 5. FAST_HEX \approx FAST_DIAMOND |
| 66 | switch (search_method) { |
| 67 | case NSTEP: return DIAMOND; |
| 68 | case DIAMOND: return BIGDIA; |
| 69 | case BIGDIA: return HEX; |
| 70 | case SQUARE: return HEX; |
| 71 | case HEX: return FAST_HEX; |
| 72 | case FAST_HEX: return FAST_HEX; |
| 73 | case FAST_DIAMOND: return FAST_DIAMOND; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 74 | case FAST_BIGDIA: return FAST_BIGDIA; |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 75 | default: assert(0 && "Invalid search method!"); return DIAMOND; |
| 76 | } |
| 77 | } |
| 78 | |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 79 | void av1_make_default_fullpel_ms_params( |
| 80 | FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const struct AV1_COMP *cpi, |
| 81 | const MACROBLOCK *x, BLOCK_SIZE bsize, const MV *ref_mv, |
| 82 | const search_site_config search_sites[NUM_SEARCH_METHODS], |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 83 | int fine_search_interval) { |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 84 | const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf; |
| 85 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 86 | // High level params |
| 87 | ms_params->bsize = bsize; |
| 88 | ms_params->vfp = &cpi->fn_ptr[bsize]; |
| 89 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 90 | init_ms_buffers(&ms_params->ms_buffers, x); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 91 | |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 92 | SEARCH_METHODS search_method = mv_sf->search_method; |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 93 | if (mv_sf->use_bsize_dependent_search_method) { |
| 94 | const int min_dim = AOMMIN(block_size_wide[bsize], block_size_high[bsize]); |
| 95 | if (min_dim >= 32) { |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 96 | search_method = get_faster_search_method(search_method); |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
chiyotsai | 7430da6 | 2020-07-27 14:06:26 -0700 | [diff] [blame] | 99 | |
| 100 | av1_set_mv_search_method(ms_params, search_sites, search_method); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 101 | |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 102 | const int use_downsampled_sad = |
| 103 | mv_sf->use_downsampled_sad && block_size_high[bsize] >= 16; |
| 104 | if (use_downsampled_sad) { |
| 105 | ms_params->sdf = ms_params->vfp->sdsf; |
| 106 | ms_params->sdx4df = ms_params->vfp->sdsx4df; |
| 107 | } else { |
| 108 | ms_params->sdf = ms_params->vfp->sdf; |
| 109 | ms_params->sdx4df = ms_params->vfp->sdx4df; |
| 110 | } |
| 111 | |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 112 | ms_params->mesh_patterns[0] = mv_sf->mesh_patterns; |
| 113 | ms_params->mesh_patterns[1] = mv_sf->intrabc_mesh_patterns; |
| 114 | ms_params->force_mesh_thresh = mv_sf->exhaustive_searches_thresh; |
| 115 | ms_params->prune_mesh_search = mv_sf->prune_mesh_search; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 116 | ms_params->run_mesh_search = 0; |
Cheng Chen | cf16b02 | 2020-04-15 11:19:23 -0700 | [diff] [blame] | 117 | ms_params->fine_search_interval = fine_search_interval; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 118 | |
| 119 | ms_params->is_intra_mode = 0; |
| 120 | |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 121 | ms_params->fast_obmc_search = mv_sf->obmc_full_pixel_search_level; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 122 | |
| 123 | ms_params->mv_limits = x->mv_limits; |
| 124 | av1_set_mv_search_range(&ms_params->mv_limits, ref_mv); |
| 125 | |
| 126 | // Mvcost params |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 127 | init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, ref_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 128 | } |
| 129 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 130 | void av1_make_default_subpel_ms_params(SUBPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 131 | const struct AV1_COMP *cpi, |
| 132 | const MACROBLOCK *x, BLOCK_SIZE bsize, |
chiyotsai | 4d1b7d9 | 2020-04-07 13:56:22 -0700 | [diff] [blame] | 133 | const MV *ref_mv, const int *cost_list) { |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 134 | const AV1_COMMON *cm = &cpi->common; |
| 135 | // High level params |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 136 | ms_params->allow_hp = cm->features.allow_high_precision_mv; |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 137 | ms_params->forced_stop = cpi->sf.mv_sf.subpel_force_stop; |
| 138 | ms_params->iters_per_step = cpi->sf.mv_sf.subpel_iters_per_step; |
| 139 | ms_params->cost_list = cond_cost_list_const(cpi, cost_list); |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 140 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 141 | av1_set_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits, ref_mv); |
| 142 | |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 143 | // Mvcost params |
chiyotsai | 9a06d18 | 2020-05-01 17:12:12 -0700 | [diff] [blame] | 144 | init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, ref_mv); |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 145 | |
| 146 | // Subpel variance params |
| 147 | ms_params->var_params.vfp = &cpi->fn_ptr[bsize]; |
| 148 | ms_params->var_params.subpel_search_type = |
| 149 | cpi->sf.mv_sf.use_accurate_subpel_search; |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 150 | ms_params->var_params.w = block_size_wide[bsize]; |
| 151 | ms_params->var_params.h = block_size_high[bsize]; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 152 | |
| 153 | // Ref and src buffers |
| 154 | MSBuffers *ms_buffers = &ms_params->var_params.ms_buffers; |
| 155 | init_ms_buffers(ms_buffers, x); |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 156 | } |
| 157 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 158 | static INLINE int get_offset_from_fullmv(const FULLPEL_MV *mv, int stride) { |
chiyotsai | 87bb805 | 2020-02-12 16:56:33 -0800 | [diff] [blame] | 159 | return mv->row * stride + mv->col; |
| 160 | } |
| 161 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 162 | static INLINE const uint8_t *get_buf_from_fullmv(const struct buf_2d *buf, |
| 163 | const FULLPEL_MV *mv) { |
| 164 | return &buf->buf[get_offset_from_fullmv(mv, buf->stride)]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
chiyotsai | 2ad959b | 2020-02-12 14:29:32 -0800 | [diff] [blame] | 167 | void av1_set_mv_search_range(FullMvLimits *mv_limits, const MV *mv) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 168 | int col_min = |
| 169 | GET_MV_RAWPEL(mv->col) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0); |
| 170 | int row_min = |
| 171 | GET_MV_RAWPEL(mv->row) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0); |
| 172 | int col_max = GET_MV_RAWPEL(mv->col) + MAX_FULL_PEL_VAL; |
| 173 | int row_max = GET_MV_RAWPEL(mv->row) + MAX_FULL_PEL_VAL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 174 | |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 175 | col_min = AOMMAX(col_min, GET_MV_RAWPEL(MV_LOW) + 1); |
| 176 | row_min = AOMMAX(row_min, GET_MV_RAWPEL(MV_LOW) + 1); |
| 177 | col_max = AOMMIN(col_max, GET_MV_RAWPEL(MV_UPP) - 1); |
| 178 | row_max = AOMMIN(row_max, GET_MV_RAWPEL(MV_UPP) - 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 179 | |
| 180 | // Get intersection of UMV window and valid MV window to reduce # of checks |
| 181 | // in diamond search. |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 182 | if (mv_limits->col_min < col_min) mv_limits->col_min = col_min; |
| 183 | if (mv_limits->col_max > col_max) mv_limits->col_max = col_max; |
| 184 | if (mv_limits->row_min < row_min) mv_limits->row_min = row_min; |
| 185 | if (mv_limits->row_max > row_max) mv_limits->row_max = row_max; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 188 | int av1_init_search_range(int size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | int sr = 0; |
| 190 | // Minimum search size no matter what the passed in value. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 191 | size = AOMMAX(16, size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 192 | |
| 193 | while ((size << sr) < MAX_FULL_PEL_VAL) sr++; |
| 194 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 195 | sr = AOMMIN(sr, MAX_MVSEARCH_STEPS - 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 196 | return sr; |
| 197 | } |
| 198 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 199 | // ============================================================================ |
| 200 | // Cost of motion vectors |
| 201 | // ============================================================================ |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 202 | // TODO(any): Adaptively adjust the regularization strength based on image size |
| 203 | // and motion activity instead of using hard-coded values. It seems like we |
| 204 | // roughly half the lambda for each increase in resolution |
| 205 | // These are multiplier used to perform regularization in motion compensation |
| 206 | // when x->mv_cost_type is set to MV_COST_L1. |
| 207 | // LOWRES |
| 208 | #define SSE_LAMBDA_LOWRES 2 // Used by mv_cost_err_fn |
| 209 | #define SAD_LAMBDA_LOWRES 32 // Used by mvsad_err_cost during full pixel search |
| 210 | // MIDRES |
| 211 | #define SSE_LAMBDA_MIDRES 0 // Used by mv_cost_err_fn |
| 212 | #define SAD_LAMBDA_MIDRES 15 // Used by mvsad_err_cost during full pixel search |
| 213 | // HDRES |
| 214 | #define SSE_LAMBDA_HDRES 1 // Used by mv_cost_err_fn |
| 215 | #define SAD_LAMBDA_HDRES 8 // Used by mvsad_err_cost during full pixel search |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 216 | |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 217 | // Returns the rate of encoding the current motion vector based on the |
chiyotsai | fcffdc6 | 2020-02-04 15:17:05 -0800 | [diff] [blame] | 218 | // joint_cost and comp_cost. joint_costs covers the cost of transmitting |
| 219 | // JOINT_MV, and comp_cost covers the cost of transmitting the actual motion |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 220 | // vector. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 221 | static INLINE int mv_cost(const MV *mv, const int *joint_cost, |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 222 | const int *const comp_cost[2]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 223 | return joint_cost[av1_get_mv_joint(mv)] + comp_cost[0][mv->row] + |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 224 | comp_cost[1][mv->col]; |
| 225 | } |
| 226 | |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 227 | #define CONVERT_TO_CONST_MVCOST(ptr) ((const int *const *)(ptr)) |
chiyotsai | fcffdc6 | 2020-02-04 15:17:05 -0800 | [diff] [blame] | 228 | // Returns the cost of encoding the motion vector diff := *mv - *ref. The cost |
| 229 | // is defined as the rate required to encode diff * weight, rounded to the |
| 230 | // nearest 2 ** 7. |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 231 | // This is NOT used during motion compensation. |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 232 | int av1_mv_bit_cost(const MV *mv, const MV *ref_mv, const int *mvjcost, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 233 | int *mvcost[2], int weight) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 234 | const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col }; |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 235 | return ROUND_POWER_OF_TWO( |
| 236 | mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) * weight, 7); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 237 | } |
| 238 | |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 239 | // Returns the cost of using the current mv during the motion search. This is |
| 240 | // used when var is used as the error metric. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 241 | #define PIXEL_TRANSFORM_ERROR_SCALE 4 |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 242 | static INLINE int mv_err_cost(const MV *mv, const MV *ref_mv, |
| 243 | const int *mvjcost, const int *const mvcost[2], |
| 244 | int error_per_bit, MV_COST_TYPE mv_cost_type) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 245 | const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col }; |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 246 | const MV abs_diff = { abs(diff.row), abs(diff.col) }; |
| 247 | |
| 248 | switch (mv_cost_type) { |
| 249 | case MV_COST_ENTROPY: |
| 250 | if (mvcost) { |
| 251 | return (int)ROUND_POWER_OF_TWO_64( |
| 252 | (int64_t)mv_cost(&diff, mvjcost, mvcost) * error_per_bit, |
| 253 | RDDIV_BITS + AV1_PROB_COST_SHIFT - RD_EPB_SHIFT + |
| 254 | PIXEL_TRANSFORM_ERROR_SCALE); |
| 255 | } |
| 256 | return 0; |
| 257 | case MV_COST_L1_LOWRES: |
| 258 | return (SSE_LAMBDA_LOWRES * (abs_diff.row + abs_diff.col)) >> 3; |
| 259 | case MV_COST_L1_MIDRES: |
| 260 | return (SSE_LAMBDA_MIDRES * (abs_diff.row + abs_diff.col)) >> 3; |
| 261 | case MV_COST_L1_HDRES: |
| 262 | return (SSE_LAMBDA_HDRES * (abs_diff.row + abs_diff.col)) >> 3; |
| 263 | case MV_COST_NONE: return 0; |
| 264 | default: assert(0 && "Invalid rd_cost_type"); return 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 265 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 266 | } |
| 267 | |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 268 | static INLINE int mv_err_cost_(const MV *mv, |
| 269 | const MV_COST_PARAMS *mv_cost_params) { |
| 270 | return mv_err_cost(mv, mv_cost_params->ref_mv, mv_cost_params->mvjcost, |
| 271 | mv_cost_params->mvcost, mv_cost_params->error_per_bit, |
| 272 | mv_cost_params->mv_cost_type); |
| 273 | } |
| 274 | |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 275 | // Returns the cost of using the current mv during the motion search. This is |
| 276 | // only used during full pixel motion search when sad is used as the error |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 277 | // metric |
| 278 | static INLINE int mvsad_err_cost(const FULLPEL_MV *mv, const FULLPEL_MV *ref_mv, |
| 279 | const int *mvjcost, const int *const mvcost[2], |
| 280 | int sad_per_bit, MV_COST_TYPE mv_cost_type) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 281 | const MV diff = { GET_MV_SUBPEL(mv->row - ref_mv->row), |
| 282 | GET_MV_SUBPEL(mv->col - ref_mv->col) }; |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 283 | |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 284 | switch (mv_cost_type) { |
| 285 | case MV_COST_ENTROPY: |
| 286 | return ROUND_POWER_OF_TWO( |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 287 | (unsigned)mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) * |
chiyotsai | 9365bff | 2020-01-31 14:40:05 -0800 | [diff] [blame] | 288 | sad_per_bit, |
| 289 | AV1_PROB_COST_SHIFT); |
| 290 | case MV_COST_L1_LOWRES: |
| 291 | return (SAD_LAMBDA_LOWRES * (abs(diff.row) + abs(diff.col))) >> 3; |
| 292 | case MV_COST_L1_MIDRES: |
| 293 | return (SAD_LAMBDA_MIDRES * (abs(diff.row) + abs(diff.col))) >> 3; |
| 294 | case MV_COST_L1_HDRES: |
| 295 | return (SAD_LAMBDA_HDRES * (abs(diff.row) + abs(diff.col))) >> 3; |
| 296 | case MV_COST_NONE: return 0; |
| 297 | default: assert(0 && "Invalid rd_cost_type"); return 0; |
| 298 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 299 | } |
| 300 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 301 | static INLINE int mvsad_err_cost_(const FULLPEL_MV *mv, |
| 302 | const MV_COST_PARAMS *mv_cost_params) { |
| 303 | return mvsad_err_cost(mv, &mv_cost_params->full_ref_mv, |
| 304 | mv_cost_params->mvjcost, mv_cost_params->mvcost, |
| 305 | mv_cost_params->sad_per_bit, |
| 306 | mv_cost_params->mv_cost_type); |
| 307 | } |
| 308 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 309 | // ============================================================================= |
| 310 | // Fullpixel Motion Search: Translational |
| 311 | // ============================================================================= |
| 312 | #define MAX_PATTERN_SCALES 11 |
| 313 | #define MAX_PATTERN_CANDIDATES 8 // max number of candidates per scale |
| 314 | #define PATTERN_CANDIDATES_REF 3 // number of refinement candidates |
| 315 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 316 | void av1_init_dsmotion_compensation(search_site_config *cfg, int stride) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 317 | int num_search_steps = 0; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 318 | int stage_index = MAX_MVSEARCH_STEPS - 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 319 | |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 320 | cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0; |
| 321 | cfg->site[stage_index][0].offset = 0; |
chiyotsai | 836b69b | 2019-04-09 13:41:24 -0700 | [diff] [blame] | 322 | cfg->stride = stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 323 | |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 324 | for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) { |
| 325 | int num_search_pts = 8; |
| 326 | |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 327 | const FULLPEL_MV search_site_mvs[13] = { |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 328 | { 0, 0 }, { -radius, 0 }, { radius, 0 }, |
| 329 | { 0, -radius }, { 0, radius }, { -radius, -radius }, |
| 330 | { radius, radius }, { -radius, radius }, { radius, -radius }, |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 331 | }; |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 332 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 333 | int i; |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 334 | for (i = 0; i <= num_search_pts; ++i) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 335 | search_site *const site = &cfg->site[stage_index][i]; |
| 336 | site->mv = search_site_mvs[i]; |
| 337 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 338 | } |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 339 | cfg->searches_per_step[stage_index] = num_search_pts; |
| 340 | cfg->radius[stage_index] = radius; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 341 | --stage_index; |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 342 | ++num_search_steps; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 343 | } |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 344 | cfg->num_search_steps = num_search_steps; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 347 | void av1_init_motion_fpf(search_site_config *cfg, int stride) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 348 | int num_search_steps = 0; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 349 | int stage_index = MAX_MVSEARCH_STEPS - 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 350 | |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 351 | cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0; |
| 352 | cfg->site[stage_index][0].offset = 0; |
chiyotsai | 836b69b | 2019-04-09 13:41:24 -0700 | [diff] [blame] | 353 | cfg->stride = stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 354 | |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 355 | for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 356 | // Generate offsets for 8 search sites per step. |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 357 | int tan_radius = AOMMAX((int)(0.41 * radius), 1); |
| 358 | int num_search_pts = 12; |
| 359 | if (radius == 1) num_search_pts = 8; |
| 360 | |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 361 | const FULLPEL_MV search_site_mvs[13] = { |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 362 | { 0, 0 }, |
| 363 | { -radius, 0 }, |
| 364 | { radius, 0 }, |
| 365 | { 0, -radius }, |
| 366 | { 0, radius }, |
| 367 | { -radius, -tan_radius }, |
| 368 | { radius, tan_radius }, |
| 369 | { -tan_radius, radius }, |
| 370 | { tan_radius, -radius }, |
| 371 | { -radius, tan_radius }, |
| 372 | { radius, -tan_radius }, |
| 373 | { tan_radius, radius }, |
| 374 | { -tan_radius, -radius }, |
| 375 | }; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 376 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 377 | int i; |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 378 | for (i = 0; i <= num_search_pts; ++i) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 379 | search_site *const site = &cfg->site[stage_index][i]; |
| 380 | site->mv = search_site_mvs[i]; |
| 381 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 382 | } |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 383 | cfg->searches_per_step[stage_index] = num_search_pts; |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 384 | cfg->radius[stage_index] = radius; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 385 | --stage_index; |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 386 | ++num_search_steps; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 387 | } |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 388 | cfg->num_search_steps = num_search_steps; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 389 | } |
| 390 | |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 391 | // Search site initialization for NSTEP search method. |
| 392 | void av1_init_motion_compensation_nstep(search_site_config *cfg, int stride) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 393 | int num_search_steps = 0; |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 394 | int stage_index = 0; |
| 395 | cfg->stride = stride; |
| 396 | int radius = 1; |
Jingning Han | a1e7dfe | 2020-01-03 15:36:27 -0800 | [diff] [blame] | 397 | for (stage_index = 0; stage_index < 15; ++stage_index) { |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 398 | int tan_radius = AOMMAX((int)(0.41 * radius), 1); |
| 399 | int num_search_pts = 12; |
Jingning Han | a73154a | 2020-01-30 17:01:33 -0800 | [diff] [blame] | 400 | if (radius <= 5) { |
| 401 | tan_radius = radius; |
| 402 | num_search_pts = 8; |
| 403 | } |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 404 | const FULLPEL_MV search_site_mvs[13] = { |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 405 | { 0, 0 }, |
| 406 | { -radius, 0 }, |
| 407 | { radius, 0 }, |
| 408 | { 0, -radius }, |
| 409 | { 0, radius }, |
| 410 | { -radius, -tan_radius }, |
| 411 | { radius, tan_radius }, |
| 412 | { -tan_radius, radius }, |
| 413 | { tan_radius, -radius }, |
| 414 | { -radius, tan_radius }, |
| 415 | { radius, -tan_radius }, |
| 416 | { tan_radius, radius }, |
| 417 | { -tan_radius, -radius }, |
| 418 | }; |
| 419 | |
| 420 | for (int i = 0; i <= num_search_pts; ++i) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 421 | search_site *const site = &cfg->site[stage_index][i]; |
| 422 | site->mv = search_site_mvs[i]; |
| 423 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 424 | } |
| 425 | cfg->searches_per_step[stage_index] = num_search_pts; |
| 426 | cfg->radius[stage_index] = radius; |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 427 | ++num_search_steps; |
Jingning Han | a1e7dfe | 2020-01-03 15:36:27 -0800 | [diff] [blame] | 428 | if (stage_index < 12) |
| 429 | radius = (int)AOMMAX((radius * 1.5 + 0.5), radius + 1); |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 430 | } |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 431 | cfg->num_search_steps = num_search_steps; |
Jingning Han | 969a8d4 | 2019-12-16 19:40:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 434 | // Search site initialization for BIGDIA / FAST_BIGDIA / FAST_DIAMOND |
| 435 | // search methods. |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 436 | void av1_init_motion_compensation_bigdia(search_site_config *cfg, int stride) { |
| 437 | cfg->stride = stride; |
| 438 | // First scale has 4-closest points, the rest have 8 points in diamond |
| 439 | // shape at increasing scales |
| 440 | static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = { |
| 441 | 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 442 | }; |
| 443 | |
| 444 | // BIGDIA search method candidates. |
| 445 | // Note that the largest candidate step at each scale is 2^scale |
| 446 | /* clang-format off */ |
| 447 | static const FULLPEL_MV |
| 448 | site_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = { |
| 449 | { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, 0 }, |
| 450 | { 0, 0 }, { 0, 0 } }, |
| 451 | { { -1, -1 }, { 0, -2 }, { 1, -1 }, { 2, 0 }, { 1, 1 }, { 0, 2 }, |
| 452 | { -1, 1 }, { -2, 0 } }, |
| 453 | { { -2, -2 }, { 0, -4 }, { 2, -2 }, { 4, 0 }, { 2, 2 }, { 0, 4 }, |
| 454 | { -2, 2 }, { -4, 0 } }, |
| 455 | { { -4, -4 }, { 0, -8 }, { 4, -4 }, { 8, 0 }, { 4, 4 }, { 0, 8 }, |
| 456 | { -4, 4 }, { -8, 0 } }, |
| 457 | { { -8, -8 }, { 0, -16 }, { 8, -8 }, { 16, 0 }, { 8, 8 }, { 0, 16 }, |
| 458 | { -8, 8 }, { -16, 0 } }, |
| 459 | { { -16, -16 }, { 0, -32 }, { 16, -16 }, { 32, 0 }, { 16, 16 }, |
| 460 | { 0, 32 }, { -16, 16 }, { -32, 0 } }, |
| 461 | { { -32, -32 }, { 0, -64 }, { 32, -32 }, { 64, 0 }, { 32, 32 }, |
| 462 | { 0, 64 }, { -32, 32 }, { -64, 0 } }, |
| 463 | { { -64, -64 }, { 0, -128 }, { 64, -64 }, { 128, 0 }, { 64, 64 }, |
| 464 | { 0, 128 }, { -64, 64 }, { -128, 0 } }, |
| 465 | { { -128, -128 }, { 0, -256 }, { 128, -128 }, { 256, 0 }, |
| 466 | { 128, 128 }, { 0, 256 }, { -128, 128 }, { -256, 0 } }, |
| 467 | { { -256, -256 }, { 0, -512 }, { 256, -256 }, { 512, 0 }, |
| 468 | { 256, 256 }, { 0, 512 }, { -256, 256 }, { -512, 0 } }, |
| 469 | { { -512, -512 }, { 0, -1024 }, { 512, -512 }, { 1024, 0 }, |
| 470 | { 512, 512 }, { 0, 1024 }, { -512, 512 }, { -1024, 0 } }, |
| 471 | }; |
| 472 | |
| 473 | /* clang-format on */ |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 474 | int radius = 1; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 475 | for (int i = 0; i < MAX_PATTERN_SCALES; ++i) { |
| 476 | cfg->searches_per_step[i] = bigdia_num_candidates[i]; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 477 | cfg->radius[i] = radius; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 478 | for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) { |
| 479 | search_site *const site = &cfg->site[i][j]; |
| 480 | site->mv = site_candidates[i][j]; |
| 481 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
| 482 | } |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 483 | radius *= 2; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 484 | } |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 485 | cfg->num_search_steps = MAX_PATTERN_SCALES; |
| 486 | } |
| 487 | |
| 488 | // Search site initialization for SQUARE search method. |
| 489 | void av1_init_motion_compensation_square(search_site_config *cfg, int stride) { |
| 490 | cfg->stride = stride; |
| 491 | // All scales have 8 closest points in square shape. |
| 492 | static const int square_num_candidates[MAX_PATTERN_SCALES] = { |
| 493 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 494 | }; |
| 495 | |
| 496 | // Square search method candidates. |
| 497 | // Note that the largest candidate step at each scale is 2^scale. |
| 498 | /* clang-format off */ |
| 499 | static const FULLPEL_MV |
| 500 | square_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = { |
| 501 | { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, |
| 502 | { -1, 1 }, { -1, 0 } }, |
| 503 | { { -2, -2 }, { 0, -2 }, { 2, -2 }, { 2, 0 }, { 2, 2 }, { 0, 2 }, |
| 504 | { -2, 2 }, { -2, 0 } }, |
| 505 | { { -4, -4 }, { 0, -4 }, { 4, -4 }, { 4, 0 }, { 4, 4 }, { 0, 4 }, |
| 506 | { -4, 4 }, { -4, 0 } }, |
| 507 | { { -8, -8 }, { 0, -8 }, { 8, -8 }, { 8, 0 }, { 8, 8 }, { 0, 8 }, |
| 508 | { -8, 8 }, { -8, 0 } }, |
| 509 | { { -16, -16 }, { 0, -16 }, { 16, -16 }, { 16, 0 }, { 16, 16 }, |
| 510 | { 0, 16 }, { -16, 16 }, { -16, 0 } }, |
| 511 | { { -32, -32 }, { 0, -32 }, { 32, -32 }, { 32, 0 }, { 32, 32 }, |
| 512 | { 0, 32 }, { -32, 32 }, { -32, 0 } }, |
| 513 | { { -64, -64 }, { 0, -64 }, { 64, -64 }, { 64, 0 }, { 64, 64 }, |
| 514 | { 0, 64 }, { -64, 64 }, { -64, 0 } }, |
| 515 | { { -128, -128 }, { 0, -128 }, { 128, -128 }, { 128, 0 }, |
| 516 | { 128, 128 }, { 0, 128 }, { -128, 128 }, { -128, 0 } }, |
| 517 | { { -256, -256 }, { 0, -256 }, { 256, -256 }, { 256, 0 }, |
| 518 | { 256, 256 }, { 0, 256 }, { -256, 256 }, { -256, 0 } }, |
| 519 | { { -512, -512 }, { 0, -512 }, { 512, -512 }, { 512, 0 }, |
| 520 | { 512, 512 }, { 0, 512 }, { -512, 512 }, { -512, 0 } }, |
| 521 | { { -1024, -1024 }, { 0, -1024 }, { 1024, -1024 }, { 1024, 0 }, |
| 522 | { 1024, 1024 }, { 0, 1024 }, { -1024, 1024 }, { -1024, 0 } }, |
| 523 | }; |
| 524 | |
| 525 | /* clang-format on */ |
| 526 | int radius = 1; |
| 527 | for (int i = 0; i < MAX_PATTERN_SCALES; ++i) { |
| 528 | cfg->searches_per_step[i] = square_num_candidates[i]; |
| 529 | cfg->radius[i] = radius; |
| 530 | for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) { |
| 531 | search_site *const site = &cfg->site[i][j]; |
| 532 | site->mv = square_candidates[i][j]; |
| 533 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
| 534 | } |
| 535 | radius *= 2; |
| 536 | } |
| 537 | cfg->num_search_steps = MAX_PATTERN_SCALES; |
| 538 | } |
| 539 | |
| 540 | // Search site initialization for HEX / FAST_HEX search methods. |
| 541 | void av1_init_motion_compensation_hex(search_site_config *cfg, int stride) { |
| 542 | cfg->stride = stride; |
| 543 | // First scale has 8-closest points, the rest have 6 points in hex shape |
| 544 | // at increasing scales. |
| 545 | static const int hex_num_candidates[MAX_PATTERN_SCALES] = { 8, 6, 6, 6, 6, 6, |
| 546 | 6, 6, 6, 6, 6 }; |
| 547 | // Note that the largest candidate step at each scale is 2^scale. |
| 548 | /* clang-format off */ |
| 549 | static const FULLPEL_MV |
| 550 | hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = { |
| 551 | { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, |
| 552 | { -1, 1 }, { -1, 0 } }, |
| 553 | { { -1, -2 }, { 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 } }, |
| 554 | { { -2, -4 }, { 2, -4 }, { 4, 0 }, { 2, 4 }, { -2, 4 }, { -4, 0 } }, |
| 555 | { { -4, -8 }, { 4, -8 }, { 8, 0 }, { 4, 8 }, { -4, 8 }, { -8, 0 } }, |
| 556 | { { -8, -16 }, { 8, -16 }, { 16, 0 }, { 8, 16 }, |
| 557 | { -8, 16 }, { -16, 0 } }, |
| 558 | { { -16, -32 }, { 16, -32 }, { 32, 0 }, { 16, 32 }, { -16, 32 }, |
| 559 | { -32, 0 } }, |
| 560 | { { -32, -64 }, { 32, -64 }, { 64, 0 }, { 32, 64 }, { -32, 64 }, |
| 561 | { -64, 0 } }, |
| 562 | { { -64, -128 }, { 64, -128 }, { 128, 0 }, { 64, 128 }, |
| 563 | { -64, 128 }, { -128, 0 } }, |
| 564 | { { -128, -256 }, { 128, -256 }, { 256, 0 }, { 128, 256 }, |
| 565 | { -128, 256 }, { -256, 0 } }, |
| 566 | { { -256, -512 }, { 256, -512 }, { 512, 0 }, { 256, 512 }, |
| 567 | { -256, 512 }, { -512, 0 } }, |
| 568 | { { -512, -1024 }, { 512, -1024 }, { 1024, 0 }, { 512, 1024 }, |
| 569 | { -512, 1024 }, { -1024, 0 } }, |
| 570 | }; |
| 571 | |
| 572 | /* clang-format on */ |
| 573 | int radius = 1; |
| 574 | for (int i = 0; i < MAX_PATTERN_SCALES; ++i) { |
| 575 | cfg->searches_per_step[i] = hex_num_candidates[i]; |
| 576 | cfg->radius[i] = radius; |
| 577 | for (int j = 0; j < hex_num_candidates[i]; ++j) { |
| 578 | search_site *const site = &cfg->site[i][j]; |
| 579 | site->mv = hex_candidates[i][j]; |
| 580 | site->offset = get_offset_from_fullmv(&site->mv, stride); |
| 581 | } |
| 582 | radius *= 2; |
| 583 | } |
| 584 | cfg->num_search_steps = MAX_PATTERN_SCALES; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 585 | } |
| 586 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 587 | // Checks whether the mv is within range of the mv_limits |
chiyotsai | 2ad959b | 2020-02-12 14:29:32 -0800 | [diff] [blame] | 588 | static INLINE int check_bounds(const FullMvLimits *mv_limits, int row, int col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 589 | int range) { |
Alex Converse | 0fa0f42 | 2017-04-24 12:51:14 -0700 | [diff] [blame] | 590 | return ((row - range) >= mv_limits->row_min) & |
| 591 | ((row + range) <= mv_limits->row_max) & |
| 592 | ((col - range) >= mv_limits->col_min) & |
| 593 | ((col + range) <= mv_limits->col_max); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 594 | } |
| 595 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 596 | static INLINE int get_mvpred_var_cost( |
| 597 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) { |
| 598 | const aom_variance_fn_ptr_t *vfp = ms_params->vfp; |
| 599 | const MV sub_this_mv = get_mv_from_fullmv(this_mv); |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 600 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 601 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 602 | const uint8_t *src_buf = src->buf; |
| 603 | const int src_stride = src->stride; |
| 604 | const int ref_stride = ref->stride; |
| 605 | |
| 606 | unsigned unused; |
| 607 | int bestsme; |
| 608 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 609 | bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv), |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 610 | ref_stride, &unused); |
| 611 | |
| 612 | bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params); |
| 613 | |
| 614 | return bestsme; |
| 615 | } |
| 616 | |
| 617 | static INLINE int get_mvpred_sad(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 618 | const struct buf_2d *const src, |
| 619 | const uint8_t *const ref_address, |
| 620 | const int ref_stride) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 621 | const uint8_t *src_buf = src->buf; |
| 622 | const int src_stride = src->stride; |
| 623 | |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 624 | return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 625 | } |
| 626 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 627 | static INLINE int get_mvpred_compound_var_cost( |
| 628 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) { |
| 629 | const aom_variance_fn_ptr_t *vfp = ms_params->vfp; |
| 630 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 631 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
| 632 | const uint8_t *src_buf = src->buf; |
| 633 | const int src_stride = src->stride; |
| 634 | const int ref_stride = ref->stride; |
| 635 | |
| 636 | const uint8_t *mask = ms_params->ms_buffers.mask; |
| 637 | const uint8_t *second_pred = ms_params->ms_buffers.second_pred; |
| 638 | const int mask_stride = ms_params->ms_buffers.mask_stride; |
| 639 | const int invert_mask = ms_params->ms_buffers.inv_mask; |
| 640 | unsigned unused; |
| 641 | int bestsme; |
| 642 | |
| 643 | if (mask) { |
Jingning Han | 142ade2 | 2020-10-10 00:32:34 -0700 | [diff] [blame] | 644 | bestsme = vfp->msvf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0, |
| 645 | src_buf, src_stride, second_pred, mask, mask_stride, |
| 646 | invert_mask, &unused); |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 647 | } else if (second_pred) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 648 | bestsme = vfp->svaf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0, |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 649 | src_buf, src_stride, &unused, second_pred); |
| 650 | } else { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 651 | bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv), |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 652 | ref_stride, &unused); |
| 653 | } |
| 654 | |
| 655 | const MV sub_this_mv = get_mv_from_fullmv(this_mv); |
| 656 | bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params); |
| 657 | |
| 658 | return bestsme; |
| 659 | } |
| 660 | |
| 661 | static INLINE int get_mvpred_compound_sad( |
| 662 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 663 | const struct buf_2d *const src, const uint8_t *const ref_address, |
| 664 | const int ref_stride) { |
| 665 | const aom_variance_fn_ptr_t *vfp = ms_params->vfp; |
| 666 | const uint8_t *src_buf = src->buf; |
| 667 | const int src_stride = src->stride; |
| 668 | |
| 669 | const uint8_t *mask = ms_params->ms_buffers.mask; |
| 670 | const uint8_t *second_pred = ms_params->ms_buffers.second_pred; |
| 671 | const int mask_stride = ms_params->ms_buffers.mask_stride; |
| 672 | const int invert_mask = ms_params->ms_buffers.inv_mask; |
| 673 | |
| 674 | if (mask) { |
| 675 | return vfp->msdf(src_buf, src_stride, ref_address, ref_stride, second_pred, |
| 676 | mask, mask_stride, invert_mask); |
| 677 | } else if (second_pred) { |
| 678 | return vfp->sdaf(src_buf, src_stride, ref_address, ref_stride, second_pred); |
| 679 | } else { |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 680 | return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride); |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 684 | // Calculates and returns a sad+mvcost list around an integer best pel during |
| 685 | // fullpixel motion search. The resulting list can be used to speed up subpel |
| 686 | // motion search later. |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 687 | #define USE_SAD_COSTLIST 1 |
| 688 | |
| 689 | // calc_int_cost_list uses var to populate the costlist, which is more accurate |
| 690 | // than sad but slightly slower. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 691 | static AOM_FORCE_INLINE void calc_int_cost_list( |
| 692 | const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 693 | int *cost_list) { |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 694 | static const FULLPEL_MV neighbors[4] = { |
| 695 | { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } |
| 696 | }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 697 | const int br = best_mv.row; |
| 698 | const int bc = best_mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 699 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 700 | cost_list[0] = get_mvpred_var_cost(ms_params, &best_mv); |
| 701 | |
| 702 | if (check_bounds(&ms_params->mv_limits, br, bc, 1)) { |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 703 | for (int i = 0; i < 4; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 704 | const FULLPEL_MV neighbor_mv = { br + neighbors[i].row, |
| 705 | bc + neighbors[i].col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 706 | cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 707 | } |
| 708 | } else { |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 709 | for (int i = 0; i < 4; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 710 | const FULLPEL_MV neighbor_mv = { br + neighbors[i].row, |
| 711 | bc + neighbors[i].col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 712 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, neighbor_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 713 | cost_list[i + 1] = INT_MAX; |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 714 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 715 | cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv); |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 716 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 721 | // calc_int_sad_list uses sad to populate the costlist, which is less accurate |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 722 | // than var but faster. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 723 | static AOM_FORCE_INLINE void calc_int_sad_list( |
| 724 | const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 725 | int *cost_list, int costlist_has_sad) { |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 726 | static const FULLPEL_MV neighbors[4] = { |
| 727 | { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } |
| 728 | }; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 729 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 730 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 731 | const int ref_stride = ref->stride; |
| 732 | const int br = best_mv.row; |
| 733 | const int bc = best_mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 734 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 735 | assert(av1_is_fullmv_in_range(&ms_params->mv_limits, best_mv)); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 736 | |
| 737 | // Refresh the costlist it does not contain valid sad |
| 738 | if (!costlist_has_sad) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 739 | cost_list[0] = get_mvpred_sad( |
| 740 | ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride); |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 741 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 742 | if (check_bounds(&ms_params->mv_limits, br, bc, 1)) { |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 743 | for (int i = 0; i < 4; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 744 | const FULLPEL_MV this_mv = { br + neighbors[i].row, |
| 745 | bc + neighbors[i].col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 746 | cost_list[i + 1] = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 747 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 748 | } |
| 749 | } else { |
chiyotsai | 4e29a45 | 2020-01-27 14:55:52 -0800 | [diff] [blame] | 750 | for (int i = 0; i < 4; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 751 | const FULLPEL_MV this_mv = { br + neighbors[i].row, |
| 752 | bc + neighbors[i].col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 753 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 754 | cost_list[i + 1] = INT_MAX; |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 755 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 756 | cost_list[i + 1] = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 757 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 758 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 761 | } |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 762 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 763 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 764 | cost_list[0] += mvsad_err_cost_(&best_mv, mv_cost_params); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 765 | |
| 766 | for (int idx = 0; idx < 4; idx++) { |
| 767 | if (cost_list[idx + 1] != INT_MAX) { |
| 768 | const FULLPEL_MV this_mv = { br + neighbors[idx].row, |
| 769 | bc + neighbors[idx].col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 770 | cost_list[idx + 1] += mvsad_err_cost_(&this_mv, mv_cost_params); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 775 | // Computes motion vector cost and adds to the sad cost. |
| 776 | // Then updates the best sad and motion vectors. |
| 777 | // Inputs: |
| 778 | // this_sad: the sad to be evaluated. |
| 779 | // mv: the current motion vector. |
| 780 | // mv_cost_params: a structure containing information to compute mv cost. |
| 781 | // best_sad: the current best sad. |
| 782 | // raw_best_sad (optional): the current best sad without calculating mv cost. |
| 783 | // best_mv: the current best motion vector. |
| 784 | // second_best_mv (optional): the second best motion vector up to now. |
| 785 | // Modifies: |
| 786 | // best_sad, raw_best_sad, best_mv, second_best_mv |
| 787 | // If the current sad is lower than the current best sad. |
| 788 | // Returns: |
| 789 | // Whether the input sad (mv) is better than the current best. |
| 790 | static int update_mvs_and_sad(const unsigned int this_sad, const FULLPEL_MV *mv, |
| 791 | const MV_COST_PARAMS *mv_cost_params, |
| 792 | unsigned int *best_sad, |
| 793 | unsigned int *raw_best_sad, FULLPEL_MV *best_mv, |
| 794 | FULLPEL_MV *second_best_mv) { |
| 795 | if (this_sad >= *best_sad) return 0; |
| 796 | |
| 797 | // Add the motion vector cost. |
| 798 | const unsigned int sad = this_sad + mvsad_err_cost_(mv, mv_cost_params); |
| 799 | if (sad < *best_sad) { |
| 800 | if (raw_best_sad) *raw_best_sad = this_sad; |
| 801 | *best_sad = sad; |
| 802 | if (second_best_mv) *second_best_mv = *best_mv; |
| 803 | *best_mv = *mv; |
| 804 | return 1; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 805 | } |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 806 | return 0; |
| 807 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 808 | |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 809 | // Calculate sad4 and update the bestmv information |
| 810 | // in FAST_DIAMOND search method. |
| 811 | static void calc_sad4_update_bestmv( |
| 812 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 813 | const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv, |
| 814 | FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad, |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 815 | int search_step, int *best_site, int cand_start) { |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 816 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 817 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
| 818 | const search_site *site = ms_params->search_sites->site[search_step]; |
| 819 | |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 820 | unsigned char const *block_offset[4]; |
| 821 | unsigned int sads[4]; |
| 822 | const uint8_t *best_address; |
| 823 | const uint8_t *src_buf = src->buf; |
| 824 | const int src_stride = src->stride; |
| 825 | best_address = get_buf_from_fullmv(ref, temp_best_mv); |
| 826 | // Loop over number of candidates. |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 827 | for (int j = 0; j < 4; j++) |
| 828 | block_offset[j] = site[cand_start + j].offset + best_address; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 829 | |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 830 | // 4-point sad calculation. |
| 831 | ms_params->sdx4df(src_buf, src_stride, block_offset, ref->stride, sads); |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 832 | |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 833 | for (int j = 0; j < 4; j++) { |
| 834 | const FULLPEL_MV this_mv = { |
| 835 | temp_best_mv->row + site[cand_start + j].mv.row, |
| 836 | temp_best_mv->col + site[cand_start + j].mv.col |
| 837 | }; |
| 838 | const int found_better_mv = update_mvs_and_sad( |
| 839 | sads[j], &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv, |
| 840 | /*second_best_mv=*/NULL); |
| 841 | if (found_better_mv) *best_site = cand_start + j; |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | // Calculate sad and update the bestmv information |
| 846 | // in FAST_DIAMOND search method. |
| 847 | static void calc_sad_update_bestmv( |
| 848 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 849 | const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv, |
| 850 | FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad, |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 851 | int search_step, int *best_site, const int num_candidates, int cand_start) { |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 852 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 853 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
| 854 | const search_site *site = ms_params->search_sites->site[search_step]; |
| 855 | // Loop over number of candidates. |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 856 | for (int i = cand_start; i < num_candidates; i++) { |
venkat sanampudi | dc5371c | 2020-07-10 09:36:01 +0530 | [diff] [blame] | 857 | const FULLPEL_MV this_mv = { temp_best_mv->row + site[i].mv.row, |
| 858 | temp_best_mv->col + site[i].mv.col }; |
| 859 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) continue; |
| 860 | int thissad = get_mvpred_sad( |
| 861 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref->stride); |
| 862 | const int found_better_mv = update_mvs_and_sad( |
| 863 | thissad, &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv, |
| 864 | /*second_best_mv=*/NULL); |
| 865 | if (found_better_mv) *best_site = i; |
| 866 | } |
| 867 | } |
| 868 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 869 | // Generic pattern search function that searches over multiple scales. |
| 870 | // Each scale can have a different number of candidates and shape of |
| 871 | // candidates as indicated in the num_candidates and candidates arrays |
| 872 | // passed into this function |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 873 | static int pattern_search(FULLPEL_MV start_mv, |
| 874 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 875 | int search_step, const int do_init_search, |
| 876 | int *cost_list, FULLPEL_MV *best_mv) { |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 877 | static const int search_steps[MAX_MVSEARCH_STEPS] = { |
Yunqing Wang | e25fdbb | 2018-03-16 16:36:32 -0700 | [diff] [blame] | 878 | 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 879 | }; |
| 880 | int i, s, t; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 881 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 882 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 883 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 884 | const search_site_config *search_sites = ms_params->search_sites; |
| 885 | const int *num_candidates = search_sites->searches_per_step; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 886 | const int ref_stride = ref->stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 887 | const int last_is_4 = num_candidates[0] == 4; |
| 888 | int br, bc; |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 889 | unsigned int bestsad = UINT_MAX, raw_bestsad = UINT_MAX; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 890 | int thissad; |
| 891 | int k = -1; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 892 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
chiyotsai | e4020f7 | 2020-07-13 15:46:47 -0700 | [diff] [blame] | 893 | search_step = AOMMIN(search_step, MAX_MVSEARCH_STEPS - 1); |
| 894 | assert(search_step >= 0); |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 895 | int best_init_s = search_steps[search_step]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 896 | // adjust ref_mv to make sure it is within MV range |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 897 | clamp_fullmv(&start_mv, &ms_params->mv_limits); |
chiyotsai | 74ee04a | 2020-02-21 16:58:02 -0800 | [diff] [blame] | 898 | br = start_mv.row; |
| 899 | bc = start_mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 900 | if (cost_list != NULL) { |
| 901 | cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = |
| 902 | INT_MAX; |
| 903 | } |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 904 | int costlist_has_sad = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 905 | |
| 906 | // Work out the start point for the search |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 907 | raw_bestsad = get_mvpred_sad(ms_params, src, |
| 908 | get_buf_from_fullmv(ref, &start_mv), ref_stride); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 909 | bestsad = raw_bestsad + mvsad_err_cost_(&start_mv, mv_cost_params); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 910 | |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 911 | // Search all possible scales up to the search param around the center point |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 912 | // pick the scale of the point that is best as the starting scale of |
| 913 | // further steps around it. |
| 914 | if (do_init_search) { |
| 915 | s = best_init_s; |
| 916 | best_init_s = -1; |
| 917 | for (t = 0; t <= s; ++t) { |
| 918 | int best_site = -1; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 919 | FULLPEL_MV temp_best_mv; |
| 920 | temp_best_mv.row = br; |
| 921 | temp_best_mv.col = bc; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 922 | if (check_bounds(&ms_params->mv_limits, br, bc, 1 << t)) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 923 | // Call 4-point sad for multiples of 4 candidates. |
| 924 | const int no_of_4_cand_loops = num_candidates[t] >> 2; |
| 925 | for (i = 0; i < no_of_4_cand_loops; i++) { |
| 926 | calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 927 | &temp_best_mv, &bestsad, &raw_bestsad, t, |
| 928 | &best_site, i * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 929 | } |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 930 | // Rest of the candidates |
| 931 | const int remaining_cand = num_candidates[t] % 4; |
| 932 | calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 933 | &temp_best_mv, &bestsad, &raw_bestsad, t, |
| 934 | &best_site, remaining_cand, |
| 935 | no_of_4_cand_loops * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 936 | } else { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 937 | calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 938 | &temp_best_mv, &bestsad, &raw_bestsad, t, |
| 939 | &best_site, num_candidates[t], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 940 | } |
| 941 | if (best_site == -1) { |
| 942 | continue; |
| 943 | } else { |
| 944 | best_init_s = t; |
| 945 | k = best_site; |
| 946 | } |
| 947 | } |
| 948 | if (best_init_s != -1) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 949 | br += search_sites->site[best_init_s][k].mv.row; |
| 950 | bc += search_sites->site[best_init_s][k].mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
| 954 | // If the center point is still the best, just skip this and move to |
| 955 | // the refinement step. |
| 956 | if (best_init_s != -1) { |
| 957 | const int last_s = (last_is_4 && cost_list != NULL); |
| 958 | int best_site = -1; |
| 959 | s = best_init_s; |
| 960 | |
| 961 | for (; s >= last_s; s--) { |
| 962 | // No need to search all points the 1st time if initial search was used |
| 963 | if (!do_init_search || s != best_init_s) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 964 | FULLPEL_MV temp_best_mv; |
| 965 | temp_best_mv.row = br; |
| 966 | temp_best_mv.col = bc; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 967 | if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 968 | // Call 4-point sad for multiples of 4 candidates. |
| 969 | const int no_of_4_cand_loops = num_candidates[s] >> 2; |
| 970 | for (i = 0; i < no_of_4_cand_loops; i++) { |
| 971 | calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 972 | &temp_best_mv, &bestsad, &raw_bestsad, s, |
| 973 | &best_site, i * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 974 | } |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 975 | // Rest of the candidates |
| 976 | const int remaining_cand = num_candidates[s] % 4; |
| 977 | calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 978 | &temp_best_mv, &bestsad, &raw_bestsad, s, |
| 979 | &best_site, remaining_cand, |
| 980 | no_of_4_cand_loops * 4); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 981 | } else { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 982 | calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv, |
| 983 | &temp_best_mv, &bestsad, &raw_bestsad, s, |
| 984 | &best_site, num_candidates[s], 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | if (best_site == -1) { |
| 988 | continue; |
| 989 | } else { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 990 | br += search_sites->site[s][best_site].mv.row; |
| 991 | bc += search_sites->site[s][best_site].mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 992 | k = best_site; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | do { |
| 997 | int next_chkpts_indices[PATTERN_CANDIDATES_REF]; |
| 998 | best_site = -1; |
| 999 | next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1; |
| 1000 | next_chkpts_indices[1] = k; |
| 1001 | next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1; |
| 1002 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1003 | if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1004 | for (i = 0; i < PATTERN_CANDIDATES_REF; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1005 | const FULLPEL_MV this_mv = { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1006 | br + search_sites->site[s][next_chkpts_indices[i]].mv.row, |
| 1007 | bc + search_sites->site[s][next_chkpts_indices[i]].mv.col |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1008 | }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1009 | thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1010 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1011 | const int found_better_mv = |
| 1012 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1013 | &raw_bestsad, best_mv, |
| 1014 | /*second_best_mv=*/NULL); |
| 1015 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } else { |
| 1018 | for (i = 0; i < PATTERN_CANDIDATES_REF; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1019 | const FULLPEL_MV this_mv = { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1020 | br + search_sites->site[s][next_chkpts_indices[i]].mv.row, |
| 1021 | bc + search_sites->site[s][next_chkpts_indices[i]].mv.col |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1022 | }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1023 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) |
| 1024 | continue; |
| 1025 | thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1026 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1027 | const int found_better_mv = |
| 1028 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1029 | &raw_bestsad, best_mv, |
| 1030 | /*second_best_mv=*/NULL); |
| 1031 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if (best_site != -1) { |
| 1036 | k = next_chkpts_indices[best_site]; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1037 | br += search_sites->site[s][k].mv.row; |
| 1038 | bc += search_sites->site[s][k].mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } while (best_site != -1); |
| 1041 | } |
| 1042 | |
| 1043 | // Note: If we enter the if below, then cost_list must be non-NULL. |
| 1044 | if (s == 0) { |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 1045 | cost_list[0] = raw_bestsad; |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1046 | costlist_has_sad = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1047 | if (!do_init_search || s != best_init_s) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1048 | if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1049 | for (i = 0; i < num_candidates[s]; i++) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1050 | const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row, |
| 1051 | bc + search_sites->site[s][i].mv.col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1052 | cost_list[i + 1] = thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1053 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1054 | const int found_better_mv = |
| 1055 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1056 | &raw_bestsad, best_mv, |
| 1057 | /*second_best_mv=*/NULL); |
| 1058 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1059 | } |
| 1060 | } else { |
| 1061 | for (i = 0; i < num_candidates[s]; i++) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1062 | const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row, |
| 1063 | bc + search_sites->site[s][i].mv.col }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1064 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) |
| 1065 | continue; |
| 1066 | cost_list[i + 1] = thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1067 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1068 | const int found_better_mv = |
| 1069 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1070 | &raw_bestsad, best_mv, |
| 1071 | /*second_best_mv=*/NULL); |
| 1072 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | if (best_site != -1) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1077 | br += search_sites->site[s][best_site].mv.row; |
| 1078 | bc += search_sites->site[s][best_site].mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1079 | k = best_site; |
| 1080 | } |
| 1081 | } |
| 1082 | while (best_site != -1) { |
| 1083 | int next_chkpts_indices[PATTERN_CANDIDATES_REF]; |
| 1084 | best_site = -1; |
| 1085 | next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1; |
| 1086 | next_chkpts_indices[1] = k; |
| 1087 | next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1; |
| 1088 | cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX; |
| 1089 | cost_list[((k + 2) % 4) + 1] = cost_list[0]; |
chiyotsai | 6edca11 | 2020-02-25 14:31:18 -0800 | [diff] [blame] | 1090 | cost_list[0] = raw_bestsad; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1091 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1092 | if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1093 | for (i = 0; i < PATTERN_CANDIDATES_REF; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1094 | const FULLPEL_MV this_mv = { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1095 | br + search_sites->site[s][next_chkpts_indices[i]].mv.row, |
| 1096 | bc + search_sites->site[s][next_chkpts_indices[i]].mv.col |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1097 | }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1098 | cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1099 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1100 | const int found_better_mv = |
| 1101 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1102 | &raw_bestsad, best_mv, |
| 1103 | /*second_best_mv=*/NULL); |
| 1104 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } else { |
| 1107 | for (i = 0; i < PATTERN_CANDIDATES_REF; i++) { |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1108 | const FULLPEL_MV this_mv = { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1109 | br + search_sites->site[s][next_chkpts_indices[i]].mv.row, |
| 1110 | bc + search_sites->site[s][next_chkpts_indices[i]].mv.col |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1111 | }; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1112 | if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1113 | cost_list[next_chkpts_indices[i] + 1] = INT_MAX; |
| 1114 | continue; |
| 1115 | } |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1116 | cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1117 | ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1118 | const int found_better_mv = |
| 1119 | update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad, |
| 1120 | &raw_bestsad, best_mv, |
| 1121 | /*second_best_mv=*/NULL); |
| 1122 | if (found_better_mv) best_site = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | if (best_site != -1) { |
| 1127 | k = next_chkpts_indices[best_site]; |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1128 | br += search_sites->site[s][k].mv.row; |
| 1129 | bc += search_sites->site[s][k].mv.col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1135 | best_mv->row = br; |
| 1136 | best_mv->col = bc; |
| 1137 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1138 | // Returns the one-away integer pel cost/sad around the best as follows: |
| 1139 | // cost_list[0]: cost/sad at the best integer pel |
| 1140 | // cost_list[1]: cost/sad at delta {0, -1} (left) from the best integer pel |
| 1141 | // cost_list[2]: cost/sad at delta { 1, 0} (bottom) from the best integer pel |
| 1142 | // cost_list[3]: cost/sad at delta { 0, 1} (right) from the best integer pel |
| 1143 | // cost_list[4]: cost/sad at delta {-1, 0} (top) from the best integer pel |
| 1144 | if (cost_list) { |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1145 | if (USE_SAD_COSTLIST) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1146 | calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1147 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1148 | calc_int_cost_list(*best_mv, ms_params, cost_list); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1151 | best_mv->row = br; |
| 1152 | best_mv->col = bc; |
chiyotsai | 2cbd640 | 2020-02-26 14:49:00 -0800 | [diff] [blame] | 1153 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1154 | const int var_cost = get_mvpred_var_cost(ms_params, best_mv); |
chiyotsai | 2cbd640 | 2020-02-26 14:49:00 -0800 | [diff] [blame] | 1155 | return var_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1156 | } |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 1157 | |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1158 | // For the following foo_search, the input arguments are: |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1159 | // start_mv: where we are starting our motion search |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1160 | // ms_params: a collection of motion search parameters |
| 1161 | // search_step: how many steps to skip in our motion search. For example, |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1162 | // a value 3 suggests that 3 search steps have already taken place prior to |
| 1163 | // this function call, so we jump directly to step 4 of the search process |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1164 | // do_init_search: if on, do an initial search of all possible scales around the |
| 1165 | // start_mv, and then pick the best scale. |
| 1166 | // cond_list: used to hold the cost around the best full mv so we can use it to |
| 1167 | // speed up subpel search later. |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1168 | // best_mv: the best mv found in the motion search |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1169 | static int hex_search(const FULLPEL_MV start_mv, |
| 1170 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1171 | const int search_step, const int do_init_search, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1172 | int *cost_list, FULLPEL_MV *best_mv) { |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1173 | return pattern_search(start_mv, ms_params, search_step, do_init_search, |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1174 | cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1177 | static int bigdia_search(const FULLPEL_MV start_mv, |
| 1178 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1179 | const int search_step, const int do_init_search, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1180 | int *cost_list, FULLPEL_MV *best_mv) { |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1181 | return pattern_search(start_mv, ms_params, search_step, do_init_search, |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1182 | cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1185 | static int square_search(const FULLPEL_MV start_mv, |
| 1186 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1187 | const int search_step, const int do_init_search, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1188 | int *cost_list, FULLPEL_MV *best_mv) { |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1189 | return pattern_search(start_mv, ms_params, search_step, do_init_search, |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1190 | cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1193 | static int fast_hex_search(const FULLPEL_MV start_mv, |
| 1194 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1195 | const int search_step, const int do_init_search, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1196 | int *cost_list, FULLPEL_MV *best_mv) { |
| 1197 | return hex_search(start_mv, ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1198 | AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step), do_init_search, |
| 1199 | cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1202 | static int fast_dia_search(const FULLPEL_MV start_mv, |
| 1203 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1204 | const int search_step, const int do_init_search, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1205 | int *cost_list, FULLPEL_MV *best_mv) { |
venkat sanampudi | aeada12 | 2020-07-17 13:14:42 +0530 | [diff] [blame] | 1206 | return bigdia_search(start_mv, ms_params, |
| 1207 | AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step), |
| 1208 | do_init_search, cost_list, best_mv); |
| 1209 | } |
| 1210 | |
| 1211 | static int fast_bigdia_search(const FULLPEL_MV start_mv, |
| 1212 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1213 | const int search_step, const int do_init_search, |
| 1214 | int *cost_list, FULLPEL_MV *best_mv) { |
venkat sanampudi | 14affd0 | 2020-07-23 07:41:05 +0530 | [diff] [blame] | 1215 | return bigdia_search(start_mv, ms_params, |
| 1216 | AOMMAX(MAX_MVSEARCH_STEPS - 3, search_step), |
| 1217 | do_init_search, cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1220 | static int diamond_search_sad(FULLPEL_MV start_mv, |
| 1221 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1222 | const int search_step, int *num00, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1223 | FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 1224 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 1225 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1226 | |
| 1227 | const int ref_stride = ref->stride; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1228 | const uint8_t *best_address; |
| 1229 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 1230 | const uint8_t *mask = ms_params->ms_buffers.mask; |
| 1231 | const uint8_t *second_pred = ms_params->ms_buffers.second_pred; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1232 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 1233 | |
| 1234 | const search_site_config *cfg = ms_params->search_sites; |
| 1235 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1236 | unsigned int bestsad = INT_MAX; |
| 1237 | int best_site = 0; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1238 | int is_off_center = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1239 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1240 | clamp_fullmv(&start_mv, &ms_params->mv_limits); |
chiyotsai | 74ee04a | 2020-02-21 16:58:02 -0800 | [diff] [blame] | 1241 | |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 1242 | // search_step determines the length of the initial step and hence the number |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1243 | // of iterations. |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1244 | const int tot_steps = cfg->num_search_steps - search_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1245 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1246 | *num00 = 0; |
chiyotsai | 74ee04a | 2020-02-21 16:58:02 -0800 | [diff] [blame] | 1247 | *best_mv = start_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1248 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1249 | // Check the starting position |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1250 | best_address = get_buf_from_fullmv(ref, &start_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1251 | bestsad = get_mvpred_compound_sad(ms_params, src, best_address, ref_stride); |
| 1252 | bestsad += mvsad_err_cost_(best_mv, &ms_params->mv_cost_params); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1253 | |
Jingning Han | a1e7dfe | 2020-01-03 15:36:27 -0800 | [diff] [blame] | 1254 | int next_step_size = tot_steps > 2 ? cfg->radius[tot_steps - 2] : 1; |
Jingning Han | 8c36803 | 2019-12-03 14:56:47 -0800 | [diff] [blame] | 1255 | for (int step = tot_steps - 1; step >= 0; --step) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1256 | const search_site *site = cfg->site[step]; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1257 | best_site = 0; |
Jingning Han | a1e7dfe | 2020-01-03 15:36:27 -0800 | [diff] [blame] | 1258 | if (step > 0) next_step_size = cfg->radius[step - 1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1259 | |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1260 | int all_in = 1, j; |
| 1261 | // Trap illegal vectors |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1262 | all_in &= best_mv->row + site[1].mv.row >= ms_params->mv_limits.row_min; |
| 1263 | all_in &= best_mv->row + site[2].mv.row <= ms_params->mv_limits.row_max; |
| 1264 | all_in &= best_mv->col + site[3].mv.col >= ms_params->mv_limits.col_min; |
| 1265 | all_in &= best_mv->col + site[4].mv.col <= ms_params->mv_limits.col_max; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1266 | |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1267 | // TODO(anyone): Implement 4 points search for msdf&sdaf |
| 1268 | if (all_in && !mask && !second_pred) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1269 | const uint8_t *src_buf = src->buf; |
| 1270 | const int src_stride = src->stride; |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1271 | for (int idx = 1; idx <= cfg->searches_per_step[step]; idx += 4) { |
| 1272 | unsigned char const *block_offset[4]; |
| 1273 | unsigned int sads[4]; |
Jingning Han | aaa3467 | 2019-11-25 14:29:26 -0800 | [diff] [blame] | 1274 | |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1275 | for (j = 0; j < 4; j++) |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1276 | block_offset[j] = site[idx + j].offset + best_address; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1277 | |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 1278 | ms_params->sdx4df(src_buf, src_stride, block_offset, ref_stride, sads); |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1279 | for (j = 0; j < 4; j++) { |
| 1280 | if (sads[j] < bestsad) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1281 | const FULLPEL_MV this_mv = { best_mv->row + site[idx + j].mv.row, |
| 1282 | best_mv->col + site[idx + j].mv.col }; |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1283 | unsigned int thissad = |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1284 | sads[j] + mvsad_err_cost_(&this_mv, mv_cost_params); |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1285 | if (thissad < bestsad) { |
| 1286 | bestsad = thissad; |
| 1287 | best_site = idx + j; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } else { |
| 1293 | for (int idx = 1; idx <= cfg->searches_per_step[step]; idx++) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1294 | const FULLPEL_MV this_mv = { best_mv->row + site[idx].mv.row, |
| 1295 | best_mv->col + site[idx].mv.col }; |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1296 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1297 | if (av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1298 | const uint8_t *const check_here = site[idx].offset + best_address; |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1299 | unsigned int thissad; |
| 1300 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1301 | thissad = |
| 1302 | get_mvpred_compound_sad(ms_params, src, check_here, ref_stride); |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1303 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1304 | if (thissad < bestsad) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1305 | thissad += mvsad_err_cost_(&this_mv, mv_cost_params); |
Ruiling Song | ddafbe2 | 2019-12-09 09:00:07 +0800 | [diff] [blame] | 1306 | if (thissad < bestsad) { |
| 1307 | bestsad = thissad; |
| 1308 | best_site = idx; |
| 1309 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1312 | } |
| 1313 | } |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1314 | |
| 1315 | if (best_site != 0) { |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1316 | if (second_best_mv) { |
| 1317 | *second_best_mv = *best_mv; |
| 1318 | } |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1319 | best_mv->row += site[best_site].mv.row; |
| 1320 | best_mv->col += site[best_site].mv.col; |
| 1321 | best_address += site[best_site].offset; |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1322 | is_off_center = 1; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1323 | } |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1324 | |
Jingning Han | b7dad71 | 2019-11-22 15:11:56 -0800 | [diff] [blame] | 1325 | if (is_off_center == 0) (*num00)++; |
Jingning Han | a1e7dfe | 2020-01-03 15:36:27 -0800 | [diff] [blame] | 1326 | |
| 1327 | if (best_site == 0) { |
| 1328 | while (next_step_size == cfg->radius[step] && step > 2) { |
| 1329 | ++(*num00); |
| 1330 | --step; |
| 1331 | next_step_size = cfg->radius[step - 1]; |
| 1332 | } |
| 1333 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1334 | } |
Jingning Han | 4d780b0 | 2019-11-22 09:48:14 -0800 | [diff] [blame] | 1335 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1336 | return bestsad; |
| 1337 | } |
| 1338 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1339 | /* do_refine: If last step (1-away) of n-step search doesn't pick the center |
| 1340 | point as the best match, we will do a final 1-away diamond |
| 1341 | refining search */ |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1342 | static int full_pixel_diamond(const FULLPEL_MV start_mv, |
| 1343 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1344 | const int step_param, int *cost_list, |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1345 | FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1346 | const search_site_config *cfg = ms_params->search_sites; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1347 | int thissme, n, num00 = 0; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1348 | int bestsme = diamond_search_sad(start_mv, ms_params, step_param, &n, best_mv, |
| 1349 | second_best_mv); |
Jingning Han | 5e95d28 | 2019-12-19 12:04:17 -0800 | [diff] [blame] | 1350 | |
| 1351 | if (bestsme < INT_MAX) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1352 | bestsme = get_mvpred_compound_var_cost(ms_params, best_mv); |
Jingning Han | 5e95d28 | 2019-12-19 12:04:17 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1355 | // If there won't be more n-step search, check to see if refining search is |
| 1356 | // needed. |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 1357 | const int further_steps = cfg->num_search_steps - 1 - step_param; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1358 | while (n < further_steps) { |
| 1359 | ++n; |
| 1360 | |
| 1361 | if (num00) { |
| 1362 | num00--; |
| 1363 | } else { |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1364 | // TODO(chiyotsai@google.com): There is another bug here where the second |
| 1365 | // best mv gets incorrectly overwritten. Fix it later. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1366 | FULLPEL_MV tmp_best_mv; |
| 1367 | thissme = diamond_search_sad(start_mv, ms_params, step_param + n, &num00, |
| 1368 | &tmp_best_mv, second_best_mv); |
Jingning Han | 5e95d28 | 2019-12-19 12:04:17 -0800 | [diff] [blame] | 1369 | |
| 1370 | if (thissme < INT_MAX) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1371 | thissme = get_mvpred_compound_var_cost(ms_params, &tmp_best_mv); |
Jingning Han | 5e95d28 | 2019-12-19 12:04:17 -0800 | [diff] [blame] | 1372 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1373 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1374 | if (thissme < bestsme) { |
| 1375 | bestsme = thissme; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1376 | *best_mv = tmp_best_mv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1381 | // Return cost list. |
| 1382 | if (cost_list) { |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1383 | if (USE_SAD_COSTLIST) { |
| 1384 | const int costlist_has_sad = 0; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1385 | calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1386 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1387 | calc_int_cost_list(*best_mv, ms_params, cost_list); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1388 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1389 | } |
| 1390 | return bestsme; |
| 1391 | } |
| 1392 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1393 | // Exhaustive motion search around a given centre position with a given |
| 1394 | // step size. |
| 1395 | static int exhaustive_mesh_search(FULLPEL_MV start_mv, |
| 1396 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1397 | const int range, const int step, |
| 1398 | FULLPEL_MV *best_mv, |
| 1399 | FULLPEL_MV *second_best_mv) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1400 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 1401 | const struct buf_2d *const src = ms_params->ms_buffers.src; |
| 1402 | const struct buf_2d *const ref = ms_params->ms_buffers.ref; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1403 | const int ref_stride = ref->stride; |
| 1404 | unsigned int best_sad = INT_MAX; |
| 1405 | int r, c, i; |
| 1406 | int start_col, end_col, start_row, end_row; |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1407 | const int col_step = (step > 1) ? step : 4; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1408 | |
| 1409 | assert(step >= 1); |
| 1410 | |
| 1411 | clamp_fullmv(&start_mv, &ms_params->mv_limits); |
| 1412 | *best_mv = start_mv; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1413 | best_sad = get_mvpred_sad(ms_params, src, get_buf_from_fullmv(ref, &start_mv), |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1414 | ref_stride); |
| 1415 | best_sad += mvsad_err_cost_(&start_mv, mv_cost_params); |
| 1416 | start_row = AOMMAX(-range, ms_params->mv_limits.row_min - start_mv.row); |
| 1417 | start_col = AOMMAX(-range, ms_params->mv_limits.col_min - start_mv.col); |
| 1418 | end_row = AOMMIN(range, ms_params->mv_limits.row_max - start_mv.row); |
| 1419 | end_col = AOMMIN(range, ms_params->mv_limits.col_max - start_mv.col); |
| 1420 | |
| 1421 | for (r = start_row; r <= end_row; r += step) { |
| 1422 | for (c = start_col; c <= end_col; c += col_step) { |
| 1423 | // Step > 1 means we are not checking every location in this pass. |
| 1424 | if (step > 1) { |
| 1425 | const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c }; |
| 1426 | unsigned int sad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1427 | ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1428 | update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad, |
| 1429 | /*raw_best_sad=*/NULL, best_mv, second_best_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1430 | } else { |
| 1431 | // 4 sads in a single call if we are checking every location |
| 1432 | if (c + 3 <= end_col) { |
| 1433 | unsigned int sads[4]; |
| 1434 | const uint8_t *addrs[4]; |
| 1435 | for (i = 0; i < 4; ++i) { |
| 1436 | const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i }; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1437 | addrs[i] = get_buf_from_fullmv(ref, &mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1438 | } |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 1439 | |
| 1440 | ms_params->sdx4df(src->buf, src->stride, addrs, ref_stride, sads); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1441 | |
| 1442 | for (i = 0; i < 4; ++i) { |
| 1443 | if (sads[i] < best_sad) { |
| 1444 | const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i }; |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1445 | update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad, |
| 1446 | /*raw_best_sad=*/NULL, best_mv, |
| 1447 | second_best_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | } else { |
| 1451 | for (i = 0; i < end_col - c; ++i) { |
| 1452 | const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i }; |
| 1453 | unsigned int sad = get_mvpred_sad( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1454 | ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride); |
Cheng Chen | 5483f60 | 2020-04-16 15:29:56 -0700 | [diff] [blame] | 1455 | update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad, |
| 1456 | /*raw_best_sad=*/NULL, best_mv, second_best_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | return best_sad; |
| 1464 | } |
| 1465 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1466 | // Runs an limited range exhaustive mesh search using a pattern set |
| 1467 | // according to the encode speed profile. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1468 | static int full_pixel_exhaustive(const FULLPEL_MV start_mv, |
| 1469 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1470 | const struct MESH_PATTERN *const mesh_patterns, |
| 1471 | int *cost_list, FULLPEL_MV *best_mv, |
| 1472 | FULLPEL_MV *second_best_mv) { |
| 1473 | const int kMinRange = 7; |
| 1474 | const int kMaxRange = 256; |
| 1475 | const int kMinInterval = 1; |
| 1476 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1477 | int bestsme; |
| 1478 | int i; |
Ranjit Kumar Tulabandu | 5e1f701 | 2019-10-18 19:01:46 +0530 | [diff] [blame] | 1479 | int interval = mesh_patterns[0].interval; |
| 1480 | int range = mesh_patterns[0].range; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1481 | int baseline_interval_divisor; |
| 1482 | |
chiyotsai | 74ee04a | 2020-02-21 16:58:02 -0800 | [diff] [blame] | 1483 | *best_mv = start_mv; |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1484 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1485 | // Trap illegal values for interval and range for this function. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1486 | if ((range < kMinRange) || (range > kMaxRange) || (interval < kMinInterval) || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1487 | (interval > range)) |
| 1488 | return INT_MAX; |
| 1489 | |
| 1490 | baseline_interval_divisor = range / interval; |
| 1491 | |
| 1492 | // Check size of proposed first range against magnitude of the centre |
| 1493 | // value used as a starting point. |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1494 | range = AOMMAX(range, (5 * AOMMAX(abs(best_mv->row), abs(best_mv->col))) / 4); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1495 | range = AOMMIN(range, kMaxRange); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1496 | interval = AOMMAX(interval, range / baseline_interval_divisor); |
Cheng Chen | cf16b02 | 2020-04-15 11:19:23 -0700 | [diff] [blame] | 1497 | // Use a small search step/interval for certain kind of clips. |
| 1498 | // For example, screen content clips with a lot of texts. |
| 1499 | // Large interval could lead to a false matching position, and it can't find |
| 1500 | // the best global candidate in following iterations due to reduced search |
| 1501 | // range. The solution here is to use a small search iterval in the beginning |
| 1502 | // and thus reduces the chance of missing the best candidate. |
| 1503 | if (ms_params->fine_search_interval) { |
| 1504 | interval = AOMMIN(interval, 4); |
| 1505 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1506 | |
| 1507 | // initial search |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1508 | bestsme = exhaustive_mesh_search(*best_mv, ms_params, range, interval, |
| 1509 | best_mv, second_best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1510 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1511 | if ((interval > kMinInterval) && (range > kMinRange)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1512 | // Progressive searches with range and step size decreasing each time |
| 1513 | // till we reach a step size of 1. Then break out. |
| 1514 | for (i = 1; i < MAX_MESH_STEP; ++i) { |
| 1515 | // First pass with coarser step and longer range |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1516 | bestsme = exhaustive_mesh_search( |
| 1517 | *best_mv, ms_params, mesh_patterns[i].range, |
| 1518 | mesh_patterns[i].interval, best_mv, second_best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1519 | |
Ranjit Kumar Tulabandu | 5e1f701 | 2019-10-18 19:01:46 +0530 | [diff] [blame] | 1520 | if (mesh_patterns[i].interval == 1) break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1524 | if (bestsme < INT_MAX) { |
| 1525 | bestsme = get_mvpred_var_cost(ms_params, best_mv); |
| 1526 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1527 | |
| 1528 | // Return cost list. |
| 1529 | if (cost_list) { |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1530 | if (USE_SAD_COSTLIST) { |
| 1531 | const int costlist_has_sad = 0; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1532 | calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1533 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1534 | calc_int_cost_list(*best_mv, ms_params, cost_list); |
chiyotsai | 85eecf9 | 2020-02-25 17:56:59 -0800 | [diff] [blame] | 1535 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1536 | } |
| 1537 | return bestsme; |
| 1538 | } |
| 1539 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1540 | // This function is called when we do joint motion search in comp_inter_inter |
David Barker | f19f35f | 2017-05-22 16:33:22 +0100 | [diff] [blame] | 1541 | // mode, or when searching for one component of an ext-inter compound mode. |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1542 | int av1_refining_search_8p_c(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1543 | const FULLPEL_MV start_mv, FULLPEL_MV *best_mv) { |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1544 | static const search_neighbors neighbors[8] = { |
| 1545 | { { -1, 0 }, -1 * SEARCH_GRID_STRIDE_8P + 0 }, |
| 1546 | { { 0, -1 }, 0 * SEARCH_GRID_STRIDE_8P - 1 }, |
| 1547 | { { 0, 1 }, 0 * SEARCH_GRID_STRIDE_8P + 1 }, |
| 1548 | { { 1, 0 }, 1 * SEARCH_GRID_STRIDE_8P + 0 }, |
| 1549 | { { -1, -1 }, -1 * SEARCH_GRID_STRIDE_8P - 1 }, |
| 1550 | { { 1, -1 }, 1 * SEARCH_GRID_STRIDE_8P - 1 }, |
| 1551 | { { -1, 1 }, -1 * SEARCH_GRID_STRIDE_8P + 1 }, |
| 1552 | { { 1, 1 }, 1 * SEARCH_GRID_STRIDE_8P + 1 } |
| 1553 | }; |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1554 | |
Hien Ho | 830b897 | 2019-04-04 15:51:14 -0700 | [diff] [blame] | 1555 | uint8_t do_refine_search_grid[SEARCH_GRID_STRIDE_8P * |
| 1556 | SEARCH_GRID_STRIDE_8P] = { 0 }; |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1557 | int grid_center = SEARCH_GRID_CENTER_8P; |
| 1558 | int grid_coord = grid_center; |
Yunqing Wang | 37f86a3 | 2017-04-20 15:53:00 -0700 | [diff] [blame] | 1559 | |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1560 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 1561 | const FullMvLimits *mv_limits = &ms_params->mv_limits; |
| 1562 | const MSBuffers *ms_buffers = &ms_params->ms_buffers; |
| 1563 | const struct buf_2d *src = ms_buffers->src; |
| 1564 | const struct buf_2d *ref = ms_buffers->ref; |
| 1565 | const int ref_stride = ref->stride; |
| 1566 | |
| 1567 | *best_mv = start_mv; |
| 1568 | clamp_fullmv(best_mv, mv_limits); |
| 1569 | |
| 1570 | unsigned int best_sad = get_mvpred_compound_sad( |
| 1571 | ms_params, src, get_buf_from_fullmv(ref, best_mv), ref_stride); |
| 1572 | best_sad += mvsad_err_cost_(best_mv, mv_cost_params); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1573 | |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1574 | do_refine_search_grid[grid_coord] = 1; |
| 1575 | |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1576 | for (int i = 0; i < SEARCH_RANGE_8P; ++i) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1577 | int best_site = -1; |
| 1578 | |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1579 | for (int j = 0; j < 8; ++j) { |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1580 | grid_coord = grid_center + neighbors[j].coord_offset; |
| 1581 | if (do_refine_search_grid[grid_coord] == 1) { |
| 1582 | continue; |
| 1583 | } |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1584 | const FULLPEL_MV mv = { best_mv->row + neighbors[j].coord.row, |
| 1585 | best_mv->col + neighbors[j].coord.col }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1586 | |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1587 | do_refine_search_grid[grid_coord] = 1; |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1588 | if (av1_is_fullmv_in_range(mv_limits, mv)) { |
David Barker | c155e01 | 2017-05-11 13:54:54 +0100 | [diff] [blame] | 1589 | unsigned int sad; |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1590 | sad = get_mvpred_compound_sad( |
| 1591 | ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1592 | if (sad < best_sad) { |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 1593 | sad += mvsad_err_cost_(&mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1594 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1595 | if (sad < best_sad) { |
| 1596 | best_sad = sad; |
| 1597 | best_site = j; |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | if (best_site == -1) { |
| 1604 | break; |
| 1605 | } else { |
Deepa K G | d4febfb | 2018-08-14 11:33:51 +0530 | [diff] [blame] | 1606 | best_mv->row += neighbors[best_site].coord.row; |
| 1607 | best_mv->col += neighbors[best_site].coord.col; |
| 1608 | grid_center += neighbors[best_site].coord_offset; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | return best_sad; |
| 1612 | } |
| 1613 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1614 | int av1_full_pixel_search(const FULLPEL_MV start_mv, |
| 1615 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1616 | const int step_param, int *cost_list, |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1617 | FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1618 | const BLOCK_SIZE bsize = ms_params->bsize; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1619 | const SEARCH_METHODS search_method = ms_params->search_method; |
| 1620 | |
| 1621 | const int is_intra_mode = ms_params->is_intra_mode; |
| 1622 | int run_mesh_search = ms_params->run_mesh_search; |
| 1623 | |
| 1624 | int var = 0; |
chiyotsai | e4d0d85 | 2020-04-07 15:21:09 -0700 | [diff] [blame] | 1625 | MARK_MV_INVALID(best_mv); |
| 1626 | if (second_best_mv) { |
| 1627 | MARK_MV_INVALID(second_best_mv); |
| 1628 | } |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1629 | |
Debargha Mukherjee | 57fc130 | 2020-10-31 03:42:09 +0000 | [diff] [blame] | 1630 | assert(ms_params->ms_buffers.second_pred == NULL && |
| 1631 | ms_params->ms_buffers.mask == NULL && |
| 1632 | "av1_full_pixel_search does not support compound pred"); |
| 1633 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1634 | if (cost_list) { |
| 1635 | cost_list[0] = INT_MAX; |
| 1636 | cost_list[1] = INT_MAX; |
| 1637 | cost_list[2] = INT_MAX; |
| 1638 | cost_list[3] = INT_MAX; |
| 1639 | cost_list[4] = INT_MAX; |
| 1640 | } |
| 1641 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1642 | switch (search_method) { |
venkat sanampudi | aeada12 | 2020-07-17 13:14:42 +0530 | [diff] [blame] | 1643 | case FAST_BIGDIA: |
| 1644 | var = fast_bigdia_search(start_mv, ms_params, step_param, 0, cost_list, |
| 1645 | best_mv); |
| 1646 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1647 | case FAST_DIAMOND: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1648 | var = fast_dia_search(start_mv, ms_params, step_param, 0, cost_list, |
| 1649 | best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1650 | break; |
| 1651 | case FAST_HEX: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1652 | var = fast_hex_search(start_mv, ms_params, step_param, 0, cost_list, |
| 1653 | best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1654 | break; |
| 1655 | case HEX: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1656 | var = hex_search(start_mv, ms_params, step_param, 1, cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1657 | break; |
| 1658 | case SQUARE: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1659 | var = |
| 1660 | square_search(start_mv, ms_params, step_param, 1, cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1661 | break; |
| 1662 | case BIGDIA: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1663 | var = |
| 1664 | bigdia_search(start_mv, ms_params, step_param, 1, cost_list, best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1665 | break; |
| 1666 | case NSTEP: |
Jingning Han | 183b2a8 | 2019-12-18 16:03:41 -0800 | [diff] [blame] | 1667 | case DIAMOND: |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1668 | var = full_pixel_diamond(start_mv, ms_params, step_param, cost_list, |
| 1669 | best_mv, second_best_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1670 | break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1671 | default: assert(0 && "Invalid search method."); |
| 1672 | } |
| 1673 | |
Hui Su | 410ca3b | 2018-08-07 11:57:57 -0700 | [diff] [blame] | 1674 | // Should we allow a follow on exhaustive search? |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1675 | if (!run_mesh_search && search_method == NSTEP) { |
Cheng Chen | cf16b02 | 2020-04-15 11:19:23 -0700 | [diff] [blame] | 1676 | int exhaustive_thr = ms_params->force_mesh_thresh; |
| 1677 | exhaustive_thr >>= |
Hui Su | 31e2617 | 2019-10-10 09:59:25 -0700 | [diff] [blame] | 1678 | 10 - (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]); |
| 1679 | // Threshold variance for an exhaustive full search. |
Cheng Chen | cf16b02 | 2020-04-15 11:19:23 -0700 | [diff] [blame] | 1680 | if (var > exhaustive_thr) run_mesh_search = 1; |
Hui Su | 410ca3b | 2018-08-07 11:57:57 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Yunqing Wang | efc5ecf | 2020-02-07 18:04:53 -0800 | [diff] [blame] | 1683 | // TODO(yunqing): the following is used to reduce mesh search in temporal |
| 1684 | // filtering. Can extend it to intrabc. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1685 | if (!is_intra_mode && ms_params->prune_mesh_search) { |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1686 | const int full_pel_mv_diff = AOMMAX(abs(start_mv.row - best_mv->row), |
| 1687 | abs(start_mv.col - best_mv->col)); |
Yunqing Wang | efc5ecf | 2020-02-07 18:04:53 -0800 | [diff] [blame] | 1688 | if (full_pel_mv_diff <= 4) { |
| 1689 | run_mesh_search = 0; |
| 1690 | } |
| 1691 | } |
| 1692 | |
chiyotsai | c814afb | 2020-08-04 13:12:35 -0700 | [diff] [blame] | 1693 | if (ms_params->sdf != ms_params->vfp->sdf) { |
| 1694 | // If we are skipping rows when we perform the motion search, we need to |
| 1695 | // check the quality of skipping. If it's bad, then we run mesh search with |
| 1696 | // skip row features off. |
| 1697 | // TODO(chiyotsai@google.com): Handle the case where we have a vertical |
| 1698 | // offset of 1 before we hit this statement to avoid having to redo |
| 1699 | // motion search. |
| 1700 | const struct buf_2d *src = ms_params->ms_buffers.src; |
| 1701 | const struct buf_2d *ref = ms_params->ms_buffers.ref; |
| 1702 | const int src_stride = src->stride; |
| 1703 | const int ref_stride = ref->stride; |
| 1704 | |
| 1705 | const uint8_t *src_address = src->buf; |
| 1706 | const uint8_t *best_address = get_buf_from_fullmv(ref, best_mv); |
| 1707 | const int sad = |
| 1708 | ms_params->vfp->sdf(src_address, src_stride, best_address, ref_stride); |
| 1709 | const int skip_sad = |
| 1710 | ms_params->vfp->sdsf(src_address, src_stride, best_address, ref_stride); |
| 1711 | // We will keep the result of skipping rows if it's good enough. Here, good |
| 1712 | // enough means the error is less than 1 per pixel. |
| 1713 | const int kSADThresh = |
| 1714 | 1 << (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]); |
| 1715 | if (sad > kSADThresh && abs(skip_sad - sad) * 10 >= AOMMAX(sad, 1) * 9) { |
| 1716 | // There is a large discrepancy between skipping and not skipping, so we |
| 1717 | // need to redo the motion search. |
| 1718 | FULLPEL_MOTION_SEARCH_PARAMS new_ms_params = *ms_params; |
| 1719 | new_ms_params.sdf = new_ms_params.vfp->sdf; |
| 1720 | new_ms_params.sdx4df = new_ms_params.vfp->sdx4df; |
| 1721 | |
| 1722 | return av1_full_pixel_search(start_mv, &new_ms_params, step_param, |
| 1723 | cost_list, best_mv, second_best_mv); |
| 1724 | } |
| 1725 | } |
| 1726 | |
Hui Su | 410ca3b | 2018-08-07 11:57:57 -0700 | [diff] [blame] | 1727 | if (run_mesh_search) { |
| 1728 | int var_ex; |
chiyotsai | e46cff7 | 2020-02-05 15:03:34 -0800 | [diff] [blame] | 1729 | FULLPEL_MV tmp_mv_ex; |
Ranjit Kumar Tulabandu | 5e1f701 | 2019-10-18 19:01:46 +0530 | [diff] [blame] | 1730 | // Pick the mesh pattern for exhaustive search based on the toolset (intraBC |
| 1731 | // or non-intraBC) |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1732 | // TODO(chiyotsai@google.com): There is a bug here where the second best mv |
| 1733 | // gets overwritten without actually comparing the rdcost. |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1734 | const MESH_PATTERN *const mesh_patterns = |
| 1735 | ms_params->mesh_patterns[is_intra_mode]; |
| 1736 | // TODO(chiyotsai@google.com): the second best mv is not set correctly by |
| 1737 | // full_pixel_exhaustive, which can incorrectly override it. |
| 1738 | var_ex = full_pixel_exhaustive(*best_mv, ms_params, mesh_patterns, |
| 1739 | cost_list, &tmp_mv_ex, second_best_mv); |
| 1740 | if (var_ex < var) { |
| 1741 | var = var_ex; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1742 | *best_mv = tmp_mv_ex; |
Hui Su | 410ca3b | 2018-08-07 11:57:57 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 1746 | return var; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1747 | } |
Hui Su | 2d5fd74 | 2018-02-21 18:10:37 -0800 | [diff] [blame] | 1748 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1749 | int av1_intrabc_hash_search(const AV1_COMP *cpi, const MACROBLOCKD *xd, |
| 1750 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 1751 | IntraBCHashInfo *intrabc_hash_info, |
| 1752 | FULLPEL_MV *best_mv) { |
| 1753 | if (!av1_use_hash_me(cpi)) return INT_MAX; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 1754 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1755 | const BLOCK_SIZE bsize = ms_params->bsize; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1756 | const int block_width = block_size_wide[bsize]; |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1757 | const int block_height = block_size_high[bsize]; |
| 1758 | |
| 1759 | if (block_width != block_height) return INT_MAX; |
| 1760 | |
| 1761 | const FullMvLimits *mv_limits = &ms_params->mv_limits; |
| 1762 | const MSBuffers *ms_buffer = &ms_params->ms_buffers; |
| 1763 | |
| 1764 | const uint8_t *src = ms_buffer->src->buf; |
| 1765 | const int src_stride = ms_buffer->src->stride; |
| 1766 | |
| 1767 | const int mi_row = xd->mi_row; |
| 1768 | const int mi_col = xd->mi_col; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1769 | const int x_pos = mi_col * MI_SIZE; |
| 1770 | const int y_pos = mi_row * MI_SIZE; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 1771 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1772 | uint32_t hash_value1, hash_value2; |
| 1773 | int best_hash_cost = INT_MAX; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 1774 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1775 | // for the hashMap |
| 1776 | hash_table *ref_frame_hash = &intrabc_hash_info->intrabc_hash_table; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 1777 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1778 | av1_get_block_hash_value(intrabc_hash_info, src, src_stride, block_width, |
| 1779 | &hash_value1, &hash_value2, is_cur_buf_hbd(xd)); |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1780 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1781 | const int count = av1_hash_table_count(ref_frame_hash, hash_value1); |
| 1782 | if (count <= 1) { |
| 1783 | return INT_MAX; |
| 1784 | } |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1785 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1786 | Iterator iterator = av1_hash_get_first_iterator(ref_frame_hash, hash_value1); |
| 1787 | for (int i = 0; i < count; i++, aom_iterator_increment(&iterator)) { |
| 1788 | block_hash ref_block_hash = *(block_hash *)(aom_iterator_get(&iterator)); |
| 1789 | if (hash_value2 == ref_block_hash.hash_value2) { |
| 1790 | // Make sure the prediction is from valid area. |
| 1791 | const MV dv = { GET_MV_SUBPEL(ref_block_hash.y - y_pos), |
| 1792 | GET_MV_SUBPEL(ref_block_hash.x - x_pos) }; |
| 1793 | if (!av1_is_dv_valid(dv, &cpi->common, xd, mi_row, mi_col, bsize, |
| 1794 | cpi->common.seq_params.mib_size_log2)) |
| 1795 | continue; |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1796 | |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1797 | FULLPEL_MV hash_mv; |
| 1798 | hash_mv.col = ref_block_hash.x - x_pos; |
| 1799 | hash_mv.row = ref_block_hash.y - y_pos; |
| 1800 | if (!av1_is_fullmv_in_range(mv_limits, hash_mv)) continue; |
| 1801 | const int refCost = get_mvpred_var_cost(ms_params, &hash_mv); |
| 1802 | if (refCost < best_hash_cost) { |
| 1803 | best_hash_cost = refCost; |
| 1804 | *best_mv = hash_mv; |
RogerZhou | cc5d35d | 2017-08-07 22:20:15 -0700 | [diff] [blame] | 1805 | } |
| 1806 | } |
chiyotsai | 8bbdd41 | 2020-03-03 14:57:23 -0800 | [diff] [blame] | 1807 | } |
chiyotsai | 82f36c9 | 2020-04-09 16:18:02 -0700 | [diff] [blame] | 1808 | |
| 1809 | return best_hash_cost; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 1812 | static int vector_match(int16_t *ref, int16_t *src, int bwl) { |
| 1813 | int best_sad = INT_MAX; |
| 1814 | int this_sad; |
| 1815 | int d; |
| 1816 | int center, offset = 0; |
| 1817 | int bw = 4 << bwl; // redundant variable, to be changed in the experiments. |
| 1818 | for (d = 0; d <= bw; d += 16) { |
| 1819 | this_sad = aom_vector_var(&ref[d], src, bwl); |
| 1820 | if (this_sad < best_sad) { |
| 1821 | best_sad = this_sad; |
| 1822 | offset = d; |
| 1823 | } |
| 1824 | } |
| 1825 | center = offset; |
| 1826 | |
| 1827 | for (d = -8; d <= 8; d += 16) { |
| 1828 | int this_pos = offset + d; |
| 1829 | // check limit |
| 1830 | if (this_pos < 0 || this_pos > bw) continue; |
| 1831 | this_sad = aom_vector_var(&ref[this_pos], src, bwl); |
| 1832 | if (this_sad < best_sad) { |
| 1833 | best_sad = this_sad; |
| 1834 | center = this_pos; |
| 1835 | } |
| 1836 | } |
| 1837 | offset = center; |
| 1838 | |
| 1839 | for (d = -4; d <= 4; d += 8) { |
| 1840 | int this_pos = offset + d; |
| 1841 | // check limit |
| 1842 | if (this_pos < 0 || this_pos > bw) continue; |
| 1843 | this_sad = aom_vector_var(&ref[this_pos], src, bwl); |
| 1844 | if (this_sad < best_sad) { |
| 1845 | best_sad = this_sad; |
| 1846 | center = this_pos; |
| 1847 | } |
| 1848 | } |
| 1849 | offset = center; |
| 1850 | |
| 1851 | for (d = -2; d <= 2; d += 4) { |
| 1852 | int this_pos = offset + d; |
| 1853 | // check limit |
| 1854 | if (this_pos < 0 || this_pos > bw) continue; |
| 1855 | this_sad = aom_vector_var(&ref[this_pos], src, bwl); |
| 1856 | if (this_sad < best_sad) { |
| 1857 | best_sad = this_sad; |
| 1858 | center = this_pos; |
| 1859 | } |
| 1860 | } |
| 1861 | offset = center; |
| 1862 | |
| 1863 | for (d = -1; d <= 1; d += 2) { |
| 1864 | int this_pos = offset + d; |
| 1865 | // check limit |
| 1866 | if (this_pos < 0 || this_pos > bw) continue; |
| 1867 | this_sad = aom_vector_var(&ref[this_pos], src, bwl); |
| 1868 | if (this_sad < best_sad) { |
| 1869 | best_sad = this_sad; |
| 1870 | center = this_pos; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | return (center - (bw >> 1)); |
| 1875 | } |
| 1876 | |
| 1877 | // A special fast version of motion search used in rt mode |
| 1878 | unsigned int av1_int_pro_motion_estimation(const AV1_COMP *cpi, MACROBLOCK *x, |
| 1879 | BLOCK_SIZE bsize, int mi_row, |
| 1880 | int mi_col, const MV *ref_mv) { |
| 1881 | MACROBLOCKD *xd = &x->e_mbd; |
| 1882 | MB_MODE_INFO *mi = xd->mi[0]; |
| 1883 | struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } }; |
| 1884 | DECLARE_ALIGNED(16, int16_t, hbuf[256]); |
| 1885 | DECLARE_ALIGNED(16, int16_t, vbuf[256]); |
| 1886 | DECLARE_ALIGNED(16, int16_t, src_hbuf[128]); |
| 1887 | DECLARE_ALIGNED(16, int16_t, src_vbuf[128]); |
| 1888 | int idx; |
| 1889 | const int bw = 4 << mi_size_wide_log2[bsize]; |
| 1890 | const int bh = 4 << mi_size_high_log2[bsize]; |
| 1891 | const int search_width = bw << 1; |
| 1892 | const int search_height = bh << 1; |
| 1893 | const int src_stride = x->plane[0].src.stride; |
| 1894 | const int ref_stride = xd->plane[0].pre[0].stride; |
| 1895 | uint8_t const *ref_buf, *src_buf; |
| 1896 | int_mv *best_int_mv = &xd->mi[0]->mv[0]; |
| 1897 | unsigned int best_sad, tmp_sad, this_sad[4]; |
| 1898 | const int norm_factor = 3 + (bw >> 5); |
| 1899 | const YV12_BUFFER_CONFIG *scaled_ref_frame = |
| 1900 | av1_get_scaled_ref_frame(cpi, mi->ref_frame[0]); |
| 1901 | static const MV search_pos[4] = { |
| 1902 | { -1, 0 }, |
| 1903 | { 0, -1 }, |
| 1904 | { 0, 1 }, |
| 1905 | { 1, 0 }, |
| 1906 | }; |
| 1907 | |
| 1908 | if (scaled_ref_frame) { |
| 1909 | int i; |
| 1910 | // Swap out the reference frame for a version that's been scaled to |
| 1911 | // match the resolution of the current frame, allowing the existing |
| 1912 | // motion search code to be used without additional modifications. |
| 1913 | for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0]; |
| 1914 | av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL, |
| 1915 | MAX_MB_PLANE); |
| 1916 | } |
| 1917 | |
| 1918 | if (xd->bd != 8) { |
| 1919 | unsigned int sad; |
| 1920 | best_int_mv->as_fullmv = kZeroFullMv; |
| 1921 | sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride, |
| 1922 | xd->plane[0].pre[0].buf, ref_stride); |
| 1923 | |
| 1924 | if (scaled_ref_frame) { |
| 1925 | int i; |
| 1926 | for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i]; |
| 1927 | } |
| 1928 | return sad; |
| 1929 | } |
| 1930 | |
| 1931 | // Set up prediction 1-D reference set |
| 1932 | ref_buf = xd->plane[0].pre[0].buf - (bw >> 1); |
| 1933 | for (idx = 0; idx < search_width; idx += 16) { |
| 1934 | aom_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh); |
| 1935 | ref_buf += 16; |
| 1936 | } |
| 1937 | |
| 1938 | ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride; |
| 1939 | for (idx = 0; idx < search_height; ++idx) { |
| 1940 | vbuf[idx] = aom_int_pro_col(ref_buf, bw) >> norm_factor; |
| 1941 | ref_buf += ref_stride; |
| 1942 | } |
| 1943 | |
| 1944 | // Set up src 1-D reference set |
| 1945 | for (idx = 0; idx < bw; idx += 16) { |
| 1946 | src_buf = x->plane[0].src.buf + idx; |
| 1947 | aom_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh); |
| 1948 | } |
| 1949 | |
| 1950 | src_buf = x->plane[0].src.buf; |
| 1951 | for (idx = 0; idx < bh; ++idx) { |
| 1952 | src_vbuf[idx] = aom_int_pro_col(src_buf, bw) >> norm_factor; |
| 1953 | src_buf += src_stride; |
| 1954 | } |
| 1955 | |
| 1956 | // Find the best match per 1-D search |
| 1957 | best_int_mv->as_fullmv.col = |
| 1958 | vector_match(hbuf, src_hbuf, mi_size_wide_log2[bsize]); |
| 1959 | best_int_mv->as_fullmv.row = |
| 1960 | vector_match(vbuf, src_vbuf, mi_size_high_log2[bsize]); |
| 1961 | |
| 1962 | FULLPEL_MV this_mv = best_int_mv->as_fullmv; |
| 1963 | src_buf = x->plane[0].src.buf; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1964 | ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 1965 | best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride); |
| 1966 | |
| 1967 | { |
| 1968 | const uint8_t *const pos[4] = { |
| 1969 | ref_buf - ref_stride, |
| 1970 | ref_buf - 1, |
| 1971 | ref_buf + 1, |
| 1972 | ref_buf + ref_stride, |
| 1973 | }; |
| 1974 | |
| 1975 | cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad); |
| 1976 | } |
| 1977 | |
| 1978 | for (idx = 0; idx < 4; ++idx) { |
| 1979 | if (this_sad[idx] < best_sad) { |
| 1980 | best_sad = this_sad[idx]; |
| 1981 | best_int_mv->as_fullmv.row = search_pos[idx].row + this_mv.row; |
| 1982 | best_int_mv->as_fullmv.col = search_pos[idx].col + this_mv.col; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | if (this_sad[0] < this_sad[3]) |
| 1987 | this_mv.row -= 1; |
| 1988 | else |
| 1989 | this_mv.row += 1; |
| 1990 | |
| 1991 | if (this_sad[1] < this_sad[2]) |
| 1992 | this_mv.col -= 1; |
| 1993 | else |
| 1994 | this_mv.col += 1; |
| 1995 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 1996 | ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 1997 | |
| 1998 | tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride); |
| 1999 | if (best_sad > tmp_sad) { |
| 2000 | best_int_mv->as_fullmv = this_mv; |
| 2001 | best_sad = tmp_sad; |
| 2002 | } |
| 2003 | |
| 2004 | convert_fullmv_to_mv(best_int_mv); |
| 2005 | |
| 2006 | SubpelMvLimits subpel_mv_limits; |
| 2007 | av1_set_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits, ref_mv); |
| 2008 | clamp_mv(&best_int_mv->as_mv, &subpel_mv_limits); |
| 2009 | |
| 2010 | if (scaled_ref_frame) { |
| 2011 | int i; |
| 2012 | for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i]; |
| 2013 | } |
| 2014 | |
| 2015 | return best_sad; |
| 2016 | } |
| 2017 | |
| 2018 | // ============================================================================= |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2019 | // Fullpixel Motion Search: OBMC |
| 2020 | // ============================================================================= |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2021 | static INLINE int get_obmc_mvpred_var( |
| 2022 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) { |
| 2023 | const aom_variance_fn_ptr_t *vfp = ms_params->vfp; |
| 2024 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2025 | const MSBuffers *ms_buffers = &ms_params->ms_buffers; |
| 2026 | const int32_t *wsrc = ms_buffers->wsrc; |
| 2027 | const int32_t *mask = ms_buffers->obmc_mask; |
| 2028 | const struct buf_2d *ref_buf = ms_buffers->ref; |
| 2029 | |
| 2030 | const MV mv = get_mv_from_fullmv(this_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2031 | unsigned int unused; |
| 2032 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2033 | return vfp->ovf(get_buf_from_fullmv(ref_buf, this_mv), ref_buf->stride, wsrc, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2034 | mask, &unused) + |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2035 | mv_err_cost_(&mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2038 | static int obmc_refining_search_sad( |
| 2039 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV *best_mv) { |
| 2040 | const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp; |
| 2041 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2042 | const MSBuffers *ms_buffers = &ms_params->ms_buffers; |
| 2043 | const int32_t *wsrc = ms_buffers->wsrc; |
| 2044 | const int32_t *mask = ms_buffers->obmc_mask; |
| 2045 | const struct buf_2d *ref_buf = ms_buffers->ref; |
| 2046 | const FULLPEL_MV neighbors[4] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } }; |
| 2047 | const int kSearchRange = 8; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2048 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2049 | unsigned int best_sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, best_mv), |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2050 | ref_buf->stride, wsrc, mask) + |
| 2051 | mvsad_err_cost_(best_mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2052 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2053 | for (int i = 0; i < kSearchRange; i++) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2054 | int best_site = -1; |
| 2055 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2056 | for (int j = 0; j < 4; j++) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2057 | const FULLPEL_MV mv = { best_mv->row + neighbors[j].row, |
| 2058 | best_mv->col + neighbors[j].col }; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2059 | if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2060 | unsigned int sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, &mv), |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2061 | ref_buf->stride, wsrc, mask); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2062 | if (sad < best_sad) { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2063 | sad += mvsad_err_cost_(&mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2064 | |
| 2065 | if (sad < best_sad) { |
| 2066 | best_sad = sad; |
| 2067 | best_site = j; |
| 2068 | } |
| 2069 | } |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | if (best_site == -1) { |
| 2074 | break; |
| 2075 | } else { |
| 2076 | best_mv->row += neighbors[best_site].row; |
| 2077 | best_mv->col += neighbors[best_site].col; |
| 2078 | } |
| 2079 | } |
| 2080 | return best_sad; |
| 2081 | } |
| 2082 | |
| 2083 | static int obmc_diamond_search_sad( |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2084 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV start_mv, |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 2085 | FULLPEL_MV *best_mv, int search_step, int *num00) { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2086 | const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp; |
| 2087 | const search_site_config *cfg = ms_params->search_sites; |
| 2088 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2089 | const MSBuffers *ms_buffers = &ms_params->ms_buffers; |
| 2090 | const int32_t *wsrc = ms_buffers->wsrc; |
| 2091 | const int32_t *mask = ms_buffers->obmc_mask; |
| 2092 | const struct buf_2d *const ref_buf = ms_buffers->ref; |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 2093 | // search_step determines the length of the initial step and hence the number |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2094 | // of iterations |
| 2095 | // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 = |
| 2096 | // (MAX_FIRST_STEP/4) pel... etc. |
| 2097 | |
Cheng Chen | bb983ba | 2020-04-21 12:17:53 -0700 | [diff] [blame] | 2098 | const int tot_steps = MAX_MVSEARCH_STEPS - 1 - search_step; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2099 | const uint8_t *best_address, *init_ref; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2100 | int best_sad = INT_MAX; |
| 2101 | int best_site = 0; |
| 2102 | int step; |
| 2103 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2104 | clamp_fullmv(&start_mv, &ms_params->mv_limits); |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2105 | best_address = init_ref = get_buf_from_fullmv(ref_buf, &start_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2106 | *num00 = 0; |
| 2107 | *best_mv = start_mv; |
| 2108 | |
| 2109 | // Check the starting position |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2110 | best_sad = fn_ptr->osdf(best_address, ref_buf->stride, wsrc, mask) + |
| 2111 | mvsad_err_cost_(best_mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2112 | |
| 2113 | for (step = tot_steps; step >= 0; --step) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 2114 | const search_site *const site = cfg->site[step]; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2115 | best_site = 0; |
| 2116 | for (int idx = 1; idx <= cfg->searches_per_step[step]; ++idx) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 2117 | const FULLPEL_MV mv = { best_mv->row + site[idx].mv.row, |
| 2118 | best_mv->col + site[idx].mv.col }; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2119 | if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 2120 | int sad = fn_ptr->osdf(best_address + site[idx].offset, ref_buf->stride, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2121 | wsrc, mask); |
| 2122 | if (sad < best_sad) { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2123 | sad += mvsad_err_cost_(&mv, mv_cost_params); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2124 | |
| 2125 | if (sad < best_sad) { |
| 2126 | best_sad = sad; |
| 2127 | best_site = idx; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | if (best_site != 0) { |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 2134 | best_mv->row += site[best_site].mv.row; |
| 2135 | best_mv->col += site[best_site].mv.col; |
| 2136 | best_address += site[best_site].offset; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2137 | } else if (best_address == init_ref) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2138 | (*num00)++; |
| 2139 | } |
| 2140 | } |
| 2141 | return best_sad; |
| 2142 | } |
| 2143 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2144 | static int obmc_full_pixel_diamond( |
| 2145 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV start_mv, |
| 2146 | int step_param, int do_refine, FULLPEL_MV *best_mv) { |
| 2147 | const search_site_config *cfg = ms_params->search_sites; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2148 | FULLPEL_MV tmp_mv; |
| 2149 | int thissme, n, num00 = 0; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2150 | int bestsme = |
| 2151 | obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv, step_param, &n); |
| 2152 | if (bestsme < INT_MAX) bestsme = get_obmc_mvpred_var(ms_params, &tmp_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2153 | *best_mv = tmp_mv; |
| 2154 | |
| 2155 | // If there won't be more n-step search, check to see if refining search is |
| 2156 | // needed. |
Cheng Chen | 66190d9 | 2020-04-21 12:58:43 -0700 | [diff] [blame] | 2157 | const int further_steps = cfg->num_search_steps - 1 - step_param; |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2158 | if (n > further_steps) do_refine = 0; |
| 2159 | |
| 2160 | while (n < further_steps) { |
| 2161 | ++n; |
| 2162 | |
| 2163 | if (num00) { |
| 2164 | num00--; |
| 2165 | } else { |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2166 | thissme = obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv, |
| 2167 | step_param + n, &num00); |
| 2168 | if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2169 | |
| 2170 | // check to see if refining search is needed. |
| 2171 | if (num00 > further_steps - n) do_refine = 0; |
| 2172 | |
| 2173 | if (thissme < bestsme) { |
| 2174 | bestsme = thissme; |
| 2175 | *best_mv = tmp_mv; |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | // final 1-away diamond refining search |
| 2181 | if (do_refine) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2182 | tmp_mv = *best_mv; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2183 | thissme = obmc_refining_search_sad(ms_params, &tmp_mv); |
| 2184 | if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2185 | if (thissme < bestsme) { |
| 2186 | bestsme = thissme; |
| 2187 | *best_mv = tmp_mv; |
| 2188 | } |
| 2189 | } |
| 2190 | return bestsme; |
| 2191 | } |
| 2192 | |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2193 | int av1_obmc_full_pixel_search(const FULLPEL_MV start_mv, |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2194 | const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 2195 | const int step_param, FULLPEL_MV *best_mv) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2196 | if (!ms_params->fast_obmc_search) { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2197 | const int do_refine = 1; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2198 | const int bestsme = obmc_full_pixel_diamond(ms_params, start_mv, step_param, |
| 2199 | do_refine, best_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2200 | return bestsme; |
| 2201 | } else { |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2202 | *best_mv = start_mv; |
chiyotsai | 94f4aca | 2020-03-20 12:54:47 -0700 | [diff] [blame] | 2203 | clamp_fullmv(best_mv, &ms_params->mv_limits); |
| 2204 | int thissme = obmc_refining_search_sad(ms_params, best_mv); |
| 2205 | if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, best_mv); |
chiyotsai | 2e42a66 | 2020-02-26 17:39:03 -0800 | [diff] [blame] | 2206 | return thissme; |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | // ============================================================================= |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2211 | // Subpixel Motion Search: Translational |
| 2212 | // ============================================================================= |
| 2213 | #define INIT_SUBPEL_STEP_SIZE (4) |
| 2214 | /* |
| 2215 | * To avoid the penalty for crossing cache-line read, preload the reference |
| 2216 | * area in a small buffer, which is aligned to make sure there won't be crossing |
| 2217 | * cache-line read while reading from this buffer. This reduced the cpu |
| 2218 | * cycles spent on reading ref data in sub-pixel filter functions. |
| 2219 | * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x |
| 2220 | * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we |
| 2221 | * could reduce the area. |
| 2222 | */ |
| 2223 | |
| 2224 | // Returns the subpel offset used by various subpel variance functions [m]sv[a]f |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2225 | static INLINE int get_subpel_part(int x) { return x & 7; } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2226 | |
| 2227 | // Gets the address of the ref buffer at subpel location (r, c), rounded to the |
| 2228 | // nearest fullpel precision toward - \infty |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2229 | |
| 2230 | static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf, |
| 2231 | const MV mv) { |
| 2232 | const int offset = (mv.row >> 3) * buf->stride + (mv.col >> 3); |
| 2233 | return &buf->buf[offset]; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | // Estimates the variance of prediction residue using bilinear filter for fast |
| 2237 | // search. |
| 2238 | static INLINE int estimated_pref_error( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2239 | const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2240 | unsigned int *sse) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2241 | const aom_variance_fn_ptr_t *vfp = var_params->vfp; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2242 | |
| 2243 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 2244 | const uint8_t *src = ms_buffers->src->buf; |
| 2245 | const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv); |
| 2246 | const int src_stride = ms_buffers->src->stride; |
| 2247 | const int ref_stride = ms_buffers->ref->stride; |
| 2248 | const uint8_t *second_pred = ms_buffers->second_pred; |
| 2249 | const uint8_t *mask = ms_buffers->mask; |
| 2250 | const int mask_stride = ms_buffers->mask_stride; |
| 2251 | const int invert_mask = ms_buffers->inv_mask; |
| 2252 | |
| 2253 | const int subpel_x_q3 = get_subpel_part(this_mv->col); |
| 2254 | const int subpel_y_q3 = get_subpel_part(this_mv->row); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2255 | |
| 2256 | if (second_pred == NULL) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2257 | return vfp->svf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride, |
| 2258 | sse); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2259 | } else if (mask) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2260 | return vfp->msvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride, |
| 2261 | second_pred, mask, mask_stride, invert_mask, sse); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2262 | } else { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2263 | return vfp->svaf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride, |
| 2264 | sse, second_pred); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | // Calculates the variance of prediction residue. |
| 2269 | static int upsampled_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2270 | const MV *this_mv, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2271 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2272 | unsigned int *sse) { |
| 2273 | const aom_variance_fn_ptr_t *vfp = var_params->vfp; |
| 2274 | const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2275 | |
| 2276 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 2277 | const uint8_t *src = ms_buffers->src->buf; |
| 2278 | const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv); |
| 2279 | const int src_stride = ms_buffers->src->stride; |
| 2280 | const int ref_stride = ms_buffers->ref->stride; |
| 2281 | const uint8_t *second_pred = ms_buffers->second_pred; |
| 2282 | const uint8_t *mask = ms_buffers->mask; |
| 2283 | const int mask_stride = ms_buffers->mask_stride; |
| 2284 | const int invert_mask = ms_buffers->inv_mask; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2285 | const int w = var_params->w; |
| 2286 | const int h = var_params->h; |
| 2287 | |
| 2288 | const int mi_row = xd->mi_row; |
| 2289 | const int mi_col = xd->mi_col; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2290 | const int subpel_x_q3 = get_subpel_part(this_mv->col); |
| 2291 | const int subpel_y_q3 = get_subpel_part(this_mv->row); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 2292 | |
| 2293 | unsigned int besterr; |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 2294 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2295 | if (is_cur_buf_hbd(xd)) { |
| 2296 | DECLARE_ALIGNED(16, uint16_t, pred16[MAX_SB_SQUARE]); |
| 2297 | uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred16); |
| 2298 | if (second_pred != NULL) { |
| 2299 | if (mask) { |
| 2300 | aom_highbd_comp_mask_upsampled_pred( |
| 2301 | xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h, |
| 2302 | subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride, |
| 2303 | invert_mask, xd->bd, subpel_search_type); |
| 2304 | } else { |
| 2305 | aom_highbd_comp_avg_upsampled_pred( |
| 2306 | xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h, |
| 2307 | subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd, |
| 2308 | subpel_search_type); |
| 2309 | } |
| 2310 | } else { |
| 2311 | aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h, |
| 2312 | subpel_x_q3, subpel_y_q3, ref, ref_stride, |
| 2313 | xd->bd, subpel_search_type); |
| 2314 | } |
| 2315 | besterr = vfp->vf(pred8, w, src, src_stride, sse); |
| 2316 | } else { |
| 2317 | DECLARE_ALIGNED(16, uint8_t, pred[MAX_SB_SQUARE]); |
| 2318 | if (second_pred != NULL) { |
| 2319 | if (mask) { |
| 2320 | aom_comp_mask_upsampled_pred( |
| 2321 | xd, cm, mi_row, mi_col, this_mv, pred, second_pred, w, h, |
| 2322 | subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride, |
| 2323 | invert_mask, subpel_search_type); |
| 2324 | } else { |
| 2325 | aom_comp_avg_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, |
| 2326 | second_pred, w, h, subpel_x_q3, subpel_y_q3, |
| 2327 | ref, ref_stride, subpel_search_type); |
| 2328 | } |
| 2329 | } else { |
| 2330 | aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, |
| 2331 | subpel_x_q3, subpel_y_q3, ref, ref_stride, |
| 2332 | subpel_search_type); |
| 2333 | } |
| 2334 | |
| 2335 | besterr = vfp->vf(pred, w, src, src_stride, sse); |
| 2336 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2337 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2338 | return besterr; |
| 2339 | } |
| 2340 | |
| 2341 | // Estimates whether this_mv is better than best_mv. This function incorporates |
| 2342 | // both prediction error and residue into account. It is suffixed "fast" because |
| 2343 | // it uses bilinear filter to estimate the prediction. |
| 2344 | static INLINE unsigned int check_better_fast( |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2345 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv, |
| 2346 | const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2347 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2348 | unsigned int *sse1, int *distortion, int *has_better_mv, int is_scaled) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2349 | unsigned int cost; |
| 2350 | if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) { |
| 2351 | unsigned int sse; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2352 | int thismse; |
| 2353 | if (is_scaled) { |
| 2354 | thismse = upsampled_pref_error(xd, cm, this_mv, var_params, &sse); |
| 2355 | } else { |
| 2356 | thismse = estimated_pref_error(this_mv, var_params, &sse); |
| 2357 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2358 | cost = mv_err_cost_(this_mv, mv_cost_params); |
| 2359 | cost += thismse; |
| 2360 | |
| 2361 | if (cost < *besterr) { |
| 2362 | *besterr = cost; |
| 2363 | *best_mv = *this_mv; |
| 2364 | *distortion = thismse; |
| 2365 | *sse1 = sse; |
| 2366 | *has_better_mv |= 1; |
| 2367 | } |
| 2368 | } else { |
| 2369 | cost = INT_MAX; |
| 2370 | } |
| 2371 | return cost; |
| 2372 | } |
| 2373 | |
| 2374 | // Checks whether this_mv is better than best_mv. This function incorporates |
| 2375 | // both prediction error and residue into account. |
| 2376 | static AOM_FORCE_INLINE unsigned int check_better( |
| 2377 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2378 | const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2379 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 2380 | unsigned int *sse1, int *distortion, int *is_better) { |
| 2381 | unsigned int cost; |
| 2382 | if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) { |
| 2383 | unsigned int sse; |
| 2384 | int thismse; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2385 | thismse = upsampled_pref_error(xd, cm, this_mv, var_params, &sse); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2386 | cost = mv_err_cost_(this_mv, mv_cost_params); |
| 2387 | cost += thismse; |
| 2388 | if (cost < *besterr) { |
| 2389 | *besterr = cost; |
| 2390 | *best_mv = *this_mv; |
| 2391 | *distortion = thismse; |
| 2392 | *sse1 = sse; |
| 2393 | *is_better |= 1; |
| 2394 | } |
| 2395 | } else { |
| 2396 | cost = INT_MAX; |
| 2397 | } |
| 2398 | return cost; |
| 2399 | } |
| 2400 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2401 | static INLINE MV get_best_diag_step(int step_size, unsigned int left_cost, |
| 2402 | unsigned int right_cost, |
| 2403 | unsigned int up_cost, |
| 2404 | unsigned int down_cost) { |
| 2405 | const MV diag_step = { up_cost <= down_cost ? -step_size : step_size, |
| 2406 | left_cost <= right_cost ? -step_size : step_size }; |
| 2407 | |
| 2408 | return diag_step; |
| 2409 | } |
| 2410 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2411 | // Searches the four cardinal direction for a better mv, then follows up with a |
| 2412 | // search in the best quadrant. This uses bilinear filter to speed up the |
| 2413 | // calculation. |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2414 | static AOM_FORCE_INLINE MV first_level_check_fast( |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2415 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, MV *best_mv, |
| 2416 | int hstep, const SubpelMvLimits *mv_limits, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2417 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2418 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2419 | unsigned int *sse1, int *distortion, int is_scaled) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2420 | // Check the four cardinal directions |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2421 | const MV left_mv = { this_mv.row, this_mv.col - hstep }; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2422 | int dummy = 0; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2423 | const unsigned int left = check_better_fast( |
| 2424 | xd, cm, &left_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, |
| 2425 | sse1, distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2426 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2427 | const MV right_mv = { this_mv.row, this_mv.col + hstep }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2428 | const unsigned int right = check_better_fast( |
| 2429 | xd, cm, &right_mv, best_mv, mv_limits, var_params, mv_cost_params, |
| 2430 | besterr, sse1, distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2431 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2432 | const MV top_mv = { this_mv.row - hstep, this_mv.col }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2433 | const unsigned int up = check_better_fast( |
| 2434 | xd, cm, &top_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, |
| 2435 | sse1, distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2436 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2437 | const MV bottom_mv = { this_mv.row + hstep, this_mv.col }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2438 | const unsigned int down = check_better_fast( |
| 2439 | xd, cm, &bottom_mv, best_mv, mv_limits, var_params, mv_cost_params, |
| 2440 | besterr, sse1, distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2441 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2442 | const MV diag_step = get_best_diag_step(hstep, left, right, up, down); |
| 2443 | const MV diag_mv = { this_mv.row + diag_step.row, |
| 2444 | this_mv.col + diag_step.col }; |
| 2445 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2446 | // Check the diagonal direction with the best mv |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2447 | check_better_fast(xd, cm, &diag_mv, best_mv, mv_limits, var_params, |
| 2448 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2449 | is_scaled); |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2450 | |
| 2451 | return diag_step; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | // Performs a following up search after first_level_check_fast is called. This |
| 2455 | // performs two extra chess pattern searches in the best quadrant. |
| 2456 | static AOM_FORCE_INLINE void second_level_check_fast( |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2457 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, const MV diag_step, |
| 2458 | MV *best_mv, int hstep, const SubpelMvLimits *mv_limits, |
| 2459 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2460 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2461 | unsigned int *sse1, int *distortion, int is_scaled) { |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2462 | assert(diag_step.row == hstep || diag_step.row == -hstep); |
| 2463 | assert(diag_step.col == hstep || diag_step.col == -hstep); |
| 2464 | const int tr = this_mv.row; |
| 2465 | const int tc = this_mv.col; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2466 | const int br = best_mv->row; |
| 2467 | const int bc = best_mv->col; |
| 2468 | int dummy = 0; |
| 2469 | if (tr != br && tc != bc) { |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2470 | assert(diag_step.col == bc - tc); |
| 2471 | assert(diag_step.row == br - tr); |
| 2472 | const MV chess_mv_1 = { br, bc + diag_step.col }; |
| 2473 | const MV chess_mv_2 = { br + diag_step.row, bc }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2474 | check_better_fast(xd, cm, &chess_mv_1, best_mv, mv_limits, var_params, |
| 2475 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2476 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2477 | |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2478 | check_better_fast(xd, cm, &chess_mv_2, best_mv, mv_limits, var_params, |
| 2479 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2480 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2481 | } else if (tr == br && tc != bc) { |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2482 | assert(diag_step.col == bc - tc); |
| 2483 | // Continue searching in the best direction |
| 2484 | const MV bottom_long_mv = { br + hstep, bc + diag_step.col }; |
| 2485 | const MV top_long_mv = { br - hstep, bc + diag_step.col }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2486 | check_better_fast(xd, cm, &bottom_long_mv, best_mv, mv_limits, var_params, |
| 2487 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2488 | is_scaled); |
| 2489 | check_better_fast(xd, cm, &top_long_mv, best_mv, mv_limits, var_params, |
| 2490 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2491 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2492 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2493 | // Search in the direction opposite of the best quadrant |
| 2494 | const MV rev_mv = { br - diag_step.row, bc }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2495 | check_better_fast(xd, cm, &rev_mv, best_mv, mv_limits, var_params, |
| 2496 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2497 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2498 | } else if (tr != br && tc == bc) { |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2499 | assert(diag_step.row == br - tr); |
| 2500 | // Continue searching in the best direction |
| 2501 | const MV right_long_mv = { br + diag_step.row, bc + hstep }; |
| 2502 | const MV left_long_mv = { br + diag_step.row, bc - hstep }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2503 | check_better_fast(xd, cm, &right_long_mv, best_mv, mv_limits, var_params, |
| 2504 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2505 | is_scaled); |
| 2506 | check_better_fast(xd, cm, &left_long_mv, best_mv, mv_limits, var_params, |
| 2507 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2508 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2509 | |
chiyotsai | c1df6a0 | 2020-03-19 13:21:39 -0700 | [diff] [blame] | 2510 | // Search in the direction opposite of the best quadrant |
| 2511 | const MV rev_mv = { br, bc - diag_step.col }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2512 | check_better_fast(xd, cm, &rev_mv, best_mv, mv_limits, var_params, |
| 2513 | mv_cost_params, besterr, sse1, distortion, &dummy, |
| 2514 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | // Combines first level check and second level check when applicable. This first |
| 2519 | // searches the four cardinal directions, and perform several |
| 2520 | // diagonal/chess-pattern searches in the best quadrant. |
| 2521 | static AOM_FORCE_INLINE void two_level_checks_fast( |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2522 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, MV *best_mv, |
| 2523 | int hstep, const SubpelMvLimits *mv_limits, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2524 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2525 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2526 | unsigned int *sse1, int *distortion, int iters, int is_scaled) { |
| 2527 | const MV diag_step = first_level_check_fast( |
| 2528 | xd, cm, this_mv, best_mv, hstep, mv_limits, var_params, mv_cost_params, |
| 2529 | besterr, sse1, distortion, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2530 | if (iters > 1) { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2531 | second_level_check_fast(xd, cm, this_mv, diag_step, best_mv, hstep, |
| 2532 | mv_limits, var_params, mv_cost_params, besterr, |
| 2533 | sse1, distortion, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2537 | static AOM_FORCE_INLINE MV |
| 2538 | first_level_check(MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, |
| 2539 | MV *best_mv, const int hstep, const SubpelMvLimits *mv_limits, |
| 2540 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2541 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 2542 | unsigned int *sse1, int *distortion) { |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2543 | int dummy = 0; |
| 2544 | const MV left_mv = { this_mv.row, this_mv.col - hstep }; |
| 2545 | const MV right_mv = { this_mv.row, this_mv.col + hstep }; |
| 2546 | const MV top_mv = { this_mv.row - hstep, this_mv.col }; |
| 2547 | const MV bottom_mv = { this_mv.row + hstep, this_mv.col }; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2548 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2549 | const unsigned int left = |
| 2550 | check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params, |
| 2551 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 2552 | const unsigned int right = |
| 2553 | check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params, |
| 2554 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 2555 | const unsigned int up = |
| 2556 | check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params, |
| 2557 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 2558 | const unsigned int down = |
| 2559 | check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params, |
| 2560 | mv_cost_params, besterr, sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2561 | |
| 2562 | const MV diag_step = get_best_diag_step(hstep, left, right, up, down); |
| 2563 | const MV diag_mv = { this_mv.row + diag_step.row, |
| 2564 | this_mv.col + diag_step.col }; |
| 2565 | |
| 2566 | // Check the diagonal direction with the best mv |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2567 | check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params, mv_cost_params, |
| 2568 | besterr, sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2569 | |
| 2570 | return diag_step; |
| 2571 | } |
| 2572 | |
| 2573 | // A newer version of second level check that gives better quality. |
| 2574 | // TODO(chiyotsai@google.com): evaluate this on subpel_search_types different |
| 2575 | // from av1_find_best_sub_pixel_tree |
| 2576 | static AOM_FORCE_INLINE void second_level_check_v2( |
| 2577 | MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2578 | MV *best_mv, const SubpelMvLimits *mv_limits, |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2579 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2580 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2581 | unsigned int *sse1, int *distortion, int is_scaled) { |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2582 | assert(best_mv->row == this_mv.row + diag_step.row || |
| 2583 | best_mv->col == this_mv.col + diag_step.col); |
| 2584 | if (CHECK_MV_EQUAL(this_mv, *best_mv)) { |
| 2585 | return; |
| 2586 | } else if (this_mv.row == best_mv->row) { |
| 2587 | // Search away from diagonal step since diagonal search did not provide any |
| 2588 | // improvement |
| 2589 | diag_step.row *= -1; |
| 2590 | } else if (this_mv.col == best_mv->col) { |
| 2591 | diag_step.col *= -1; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 2594 | const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col }; |
| 2595 | const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col }; |
| 2596 | const MV diag_bias_mv = { best_mv->row + diag_step.row, |
| 2597 | best_mv->col + diag_step.col }; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2598 | int has_better_mv = 0; |
| 2599 | |
| 2600 | if (var_params->subpel_search_type != USE_2_TAPS_ORIG) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2601 | check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params, |
| 2602 | mv_cost_params, besterr, sse1, distortion, &has_better_mv); |
| 2603 | check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params, |
| 2604 | mv_cost_params, besterr, sse1, distortion, &has_better_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2605 | |
| 2606 | // Do an additional search if the second iteration gives a better mv |
| 2607 | if (has_better_mv) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2608 | check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params, |
| 2609 | mv_cost_params, besterr, sse1, distortion, &has_better_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2610 | } |
| 2611 | } else { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2612 | check_better_fast(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params, |
| 2613 | mv_cost_params, besterr, sse1, distortion, &has_better_mv, |
| 2614 | is_scaled); |
| 2615 | check_better_fast(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params, |
| 2616 | mv_cost_params, besterr, sse1, distortion, &has_better_mv, |
| 2617 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2618 | |
| 2619 | // Do an additional search if the second iteration gives a better mv |
| 2620 | if (has_better_mv) { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2621 | check_better_fast(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2622 | mv_cost_params, besterr, sse1, distortion, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2623 | &has_better_mv, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | // Gets the error at the beginning when the mv has fullpel precision |
| 2629 | static unsigned int setup_center_error( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2630 | const MACROBLOCKD *xd, const MV *bestmv, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2631 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2632 | const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) { |
| 2633 | const aom_variance_fn_ptr_t *vfp = var_params->vfp; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2634 | const int w = var_params->w; |
| 2635 | const int h = var_params->h; |
| 2636 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2637 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 2638 | const uint8_t *src = ms_buffers->src->buf; |
| 2639 | const uint8_t *y = get_buf_from_mv(ms_buffers->ref, *bestmv); |
| 2640 | const int src_stride = ms_buffers->src->stride; |
| 2641 | const int y_stride = ms_buffers->ref->stride; |
| 2642 | const uint8_t *second_pred = ms_buffers->second_pred; |
| 2643 | const uint8_t *mask = ms_buffers->mask; |
| 2644 | const int mask_stride = ms_buffers->mask_stride; |
| 2645 | const int invert_mask = ms_buffers->inv_mask; |
| 2646 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2647 | unsigned int besterr; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2648 | |
| 2649 | if (second_pred != NULL) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2650 | if (is_cur_buf_hbd(xd)) { |
| 2651 | DECLARE_ALIGNED(16, uint16_t, comp_pred16[MAX_SB_SQUARE]); |
| 2652 | uint8_t *comp_pred = CONVERT_TO_BYTEPTR(comp_pred16); |
| 2653 | if (mask) { |
| 2654 | aom_highbd_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride, |
| 2655 | mask, mask_stride, invert_mask); |
| 2656 | } else { |
| 2657 | aom_highbd_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride); |
| 2658 | } |
| 2659 | besterr = vfp->vf(comp_pred, w, src, src_stride, sse1); |
| 2660 | } else { |
| 2661 | DECLARE_ALIGNED(16, uint8_t, comp_pred[MAX_SB_SQUARE]); |
| 2662 | if (mask) { |
| 2663 | aom_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride, mask, |
| 2664 | mask_stride, invert_mask); |
| 2665 | } else { |
| 2666 | aom_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride); |
| 2667 | } |
| 2668 | besterr = vfp->vf(comp_pred, w, src, src_stride, sse1); |
| 2669 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2670 | } else { |
| 2671 | besterr = vfp->vf(y, y_stride, src, src_stride, sse1); |
| 2672 | } |
| 2673 | *distortion = besterr; |
| 2674 | besterr += mv_err_cost_(bestmv, mv_cost_params); |
| 2675 | return besterr; |
| 2676 | } |
| 2677 | |
| 2678 | // Gets the error at the beginning when the mv has fullpel precision |
| 2679 | static unsigned int upsampled_setup_center_error( |
| 2680 | MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *bestmv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2681 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2682 | const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2683 | unsigned int besterr = upsampled_pref_error(xd, cm, bestmv, var_params, sse1); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2684 | *distortion = besterr; |
| 2685 | besterr += mv_err_cost_(bestmv, mv_cost_params); |
| 2686 | return besterr; |
| 2687 | } |
| 2688 | |
| 2689 | static INLINE int divide_and_round(int n, int d) { |
| 2690 | return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d); |
| 2691 | } |
| 2692 | |
| 2693 | static INLINE int is_cost_list_wellbehaved(const int *cost_list) { |
| 2694 | return cost_list[0] < cost_list[1] && cost_list[0] < cost_list[2] && |
| 2695 | cost_list[0] < cost_list[3] && cost_list[0] < cost_list[4]; |
| 2696 | } |
| 2697 | |
| 2698 | // Returns surface minima estimate at given precision in 1/2^n bits. |
| 2699 | // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C |
| 2700 | // For a given set of costs S0, S1, S2, S3, S4 at points |
| 2701 | // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively, |
| 2702 | // the solution for the location of the minima (x0, y0) is given by: |
| 2703 | // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0), |
| 2704 | // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0). |
| 2705 | // The code below is an integerized version of that. |
| 2706 | static AOM_INLINE void get_cost_surf_min(const int *cost_list, int *ir, int *ic, |
| 2707 | int bits) { |
| 2708 | *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)), |
| 2709 | (cost_list[1] - 2 * cost_list[0] + cost_list[3])); |
| 2710 | *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)), |
| 2711 | (cost_list[4] - 2 * cost_list[0] + cost_list[2])); |
| 2712 | } |
| 2713 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2714 | // Checks the list of mvs searched in the last iteration and see if we are |
| 2715 | // repeating it. If so, return 1. Otherwise we update the last_mv_search_list |
| 2716 | // with current_mv and return 0. |
| 2717 | static INLINE int check_repeated_mv_and_update(int_mv *last_mv_search_list, |
| 2718 | const MV current_mv, int iter) { |
| 2719 | if (last_mv_search_list) { |
| 2720 | if (CHECK_MV_EQUAL(last_mv_search_list[iter].as_mv, current_mv)) { |
| 2721 | return 1; |
| 2722 | } |
| 2723 | |
| 2724 | last_mv_search_list[iter].as_mv = current_mv; |
| 2725 | } |
| 2726 | return 0; |
| 2727 | } |
| 2728 | |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2729 | static AOM_INLINE int setup_center_error_facade( |
| 2730 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *bestmv, |
| 2731 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 2732 | const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion, |
| 2733 | int is_scaled) { |
| 2734 | if (is_scaled) { |
| 2735 | return upsampled_setup_center_error(xd, cm, bestmv, var_params, |
| 2736 | mv_cost_params, sse1, distortion); |
| 2737 | } else { |
| 2738 | return setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1, |
| 2739 | distortion); |
| 2740 | } |
| 2741 | } |
| 2742 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2743 | int av1_find_best_sub_pixel_tree_pruned_evenmore( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2744 | MACROBLOCKD *xd, const AV1_COMMON *const cm, |
| 2745 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv, |
| 2746 | int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) { |
| 2747 | (void)cm; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2748 | const int allow_hp = ms_params->allow_hp; |
| 2749 | const int forced_stop = ms_params->forced_stop; |
| 2750 | const int iters_per_step = ms_params->iters_per_step; |
| 2751 | const int *cost_list = ms_params->cost_list; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2752 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2753 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2754 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
| 2755 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2756 | // The iteration we are current searching for. Iter 0 corresponds to fullpel |
| 2757 | // mv, iter 1 to half pel, and so on |
| 2758 | int iter = 0; |
| 2759 | int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2760 | unsigned int besterr = INT_MAX; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2761 | *bestmv = start_mv; |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2762 | #if CONFIG_SDP |
| 2763 | const struct scale_factors *const sf = |
| 2764 | is_intrabc_block(xd->mi[0], xd->tree_type) |
| 2765 | #else |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2766 | const struct scale_factors *const sf = is_intrabc_block(xd->mi[0]) |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2767 | #endif |
| 2768 | ? &cm->sf_identity |
| 2769 | : xd->block_ref_scale_factors[0]; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2770 | const int is_scaled = av1_is_scaled(sf); |
| 2771 | besterr = setup_center_error_facade( |
| 2772 | xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled); |
| 2773 | |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 2774 | // If forced_stop is FULL_PEL, return. |
| 2775 | if (forced_stop == FULL_PEL) return besterr; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2776 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2777 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2778 | return INT_MAX; |
| 2779 | } |
| 2780 | iter++; |
| 2781 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2782 | if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX && |
| 2783 | cost_list[2] != INT_MAX && cost_list[3] != INT_MAX && |
| 2784 | cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) { |
| 2785 | int ir, ic; |
| 2786 | int dummy = 0; |
| 2787 | get_cost_surf_min(cost_list, &ir, &ic, 2); |
| 2788 | if (ir != 0 || ic != 0) { |
| 2789 | const MV this_mv = { start_mv.row + 2 * ir, start_mv.col + 2 * ic }; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2790 | check_better_fast(xd, cm, &this_mv, bestmv, mv_limits, var_params, |
| 2791 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2792 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2793 | } |
| 2794 | } else { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2795 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2796 | var_params, mv_cost_params, &besterr, sse1, |
| 2797 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2798 | |
| 2799 | // Each subsequent iteration checks at least one point in common with |
| 2800 | // the last iteration could be 2 ( if diag selected) 1/4 pel |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 2801 | if (forced_stop < HALF_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2802 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2803 | return INT_MAX; |
| 2804 | } |
| 2805 | iter++; |
| 2806 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2807 | hstep >>= 1; |
| 2808 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2809 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2810 | var_params, mv_cost_params, &besterr, sse1, |
| 2811 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | if (allow_hp && forced_stop == EIGHTH_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2816 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2817 | return INT_MAX; |
| 2818 | } |
| 2819 | iter++; |
| 2820 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2821 | hstep >>= 1; |
| 2822 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2823 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2824 | var_params, mv_cost_params, &besterr, sse1, |
| 2825 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | return besterr; |
| 2829 | } |
| 2830 | |
| 2831 | int av1_find_best_sub_pixel_tree_pruned_more( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2832 | MACROBLOCKD *xd, const AV1_COMMON *const cm, |
| 2833 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv, |
| 2834 | int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) { |
| 2835 | (void)cm; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2836 | const int allow_hp = ms_params->allow_hp; |
| 2837 | const int forced_stop = ms_params->forced_stop; |
| 2838 | const int iters_per_step = ms_params->iters_per_step; |
| 2839 | const int *cost_list = ms_params->cost_list; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2840 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2841 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2842 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
| 2843 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2844 | // The iteration we are current searching for. Iter 0 corresponds to fullpel |
| 2845 | // mv, iter 1 to half pel, and so on |
| 2846 | int iter = 0; |
| 2847 | int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2848 | unsigned int besterr = INT_MAX; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2849 | *bestmv = start_mv; |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2850 | #if CONFIG_SDP |
| 2851 | const struct scale_factors *const sf = |
| 2852 | is_intrabc_block(xd->mi[0], xd->tree_type) |
| 2853 | #else |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2854 | const struct scale_factors *const sf = is_intrabc_block(xd->mi[0]) |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2855 | #endif |
| 2856 | ? &cm->sf_identity |
| 2857 | : xd->block_ref_scale_factors[0]; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2858 | const int is_scaled = av1_is_scaled(sf); |
| 2859 | besterr = setup_center_error_facade( |
| 2860 | xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled); |
| 2861 | |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 2862 | // If forced_stop is FULL_PEL, return. |
| 2863 | if (forced_stop == FULL_PEL) return besterr; |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2864 | |
| 2865 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2866 | return INT_MAX; |
| 2867 | } |
| 2868 | iter++; |
| 2869 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2870 | if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX && |
| 2871 | cost_list[2] != INT_MAX && cost_list[3] != INT_MAX && |
| 2872 | cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) { |
| 2873 | int ir, ic; |
| 2874 | get_cost_surf_min(cost_list, &ir, &ic, 1); |
| 2875 | if (ir != 0 || ic != 0) { |
| 2876 | const MV this_mv = { start_mv.row + ir * hstep, |
| 2877 | start_mv.col + ic * hstep }; |
| 2878 | int dummy = 0; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2879 | check_better_fast(xd, cm, &this_mv, bestmv, mv_limits, var_params, |
| 2880 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2881 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2882 | } |
| 2883 | } else { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2884 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2885 | var_params, mv_cost_params, &besterr, sse1, |
| 2886 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | // Each subsequent iteration checks at least one point in common with |
| 2890 | // the last iteration could be 2 ( if diag selected) 1/4 pel |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 2891 | if (forced_stop < HALF_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2892 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2893 | return INT_MAX; |
| 2894 | } |
| 2895 | iter++; |
| 2896 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2897 | hstep >>= 1; |
| 2898 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2899 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2900 | var_params, mv_cost_params, &besterr, sse1, |
| 2901 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | if (allow_hp && forced_stop == EIGHTH_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2905 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2906 | return INT_MAX; |
| 2907 | } |
| 2908 | iter++; |
| 2909 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2910 | hstep >>= 1; |
| 2911 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2912 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 2913 | var_params, mv_cost_params, &besterr, sse1, |
| 2914 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | return besterr; |
| 2918 | } |
| 2919 | |
| 2920 | int av1_find_best_sub_pixel_tree_pruned( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2921 | MACROBLOCKD *xd, const AV1_COMMON *const cm, |
| 2922 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv, |
| 2923 | int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) { |
| 2924 | (void)cm; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2925 | const int allow_hp = ms_params->allow_hp; |
| 2926 | const int forced_stop = ms_params->forced_stop; |
| 2927 | const int iters_per_step = ms_params->iters_per_step; |
| 2928 | const int *cost_list = ms_params->cost_list; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2929 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2930 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 2931 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
| 2932 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2933 | // The iteration we are current searching for. Iter 0 corresponds to fullpel |
| 2934 | // mv, iter 1 to half pel, and so on |
| 2935 | int iter = 0; |
| 2936 | int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2937 | unsigned int besterr = INT_MAX; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 2938 | *bestmv = start_mv; |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2939 | #if CONFIG_SDP |
| 2940 | const struct scale_factors *const sf = |
| 2941 | is_intrabc_block(xd->mi[0], xd->tree_type) |
| 2942 | #else |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2943 | const struct scale_factors *const sf = is_intrabc_block(xd->mi[0]) |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 2944 | #endif |
| 2945 | ? &cm->sf_identity |
| 2946 | : xd->block_ref_scale_factors[0]; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2947 | const int is_scaled = av1_is_scaled(sf); |
| 2948 | besterr = setup_center_error_facade( |
| 2949 | xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled); |
| 2950 | |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 2951 | // If forced_stop is FULL_PEL, return. |
| 2952 | if (forced_stop == FULL_PEL) return besterr; |
| 2953 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 2954 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 2955 | return INT_MAX; |
| 2956 | } |
| 2957 | iter++; |
| 2958 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2959 | if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX && |
| 2960 | cost_list[2] != INT_MAX && cost_list[3] != INT_MAX && |
| 2961 | cost_list[4] != INT_MAX) { |
| 2962 | const unsigned int whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) + |
| 2963 | (cost_list[2] < cost_list[4] ? 0 : 2); |
| 2964 | |
| 2965 | const MV left_mv = { start_mv.row, start_mv.col - hstep }; |
| 2966 | const MV right_mv = { start_mv.row, start_mv.col + hstep }; |
| 2967 | const MV bottom_mv = { start_mv.row + hstep, start_mv.col }; |
| 2968 | const MV top_mv = { start_mv.row - hstep, start_mv.col }; |
| 2969 | |
| 2970 | const MV bottom_left_mv = { start_mv.row + hstep, start_mv.col - hstep }; |
| 2971 | const MV bottom_right_mv = { start_mv.row + hstep, start_mv.col + hstep }; |
| 2972 | const MV top_left_mv = { start_mv.row - hstep, start_mv.col - hstep }; |
| 2973 | const MV top_right_mv = { start_mv.row - hstep, start_mv.col + hstep }; |
| 2974 | |
| 2975 | int dummy = 0; |
| 2976 | |
| 2977 | switch (whichdir) { |
| 2978 | case 0: // bottom left quadrant |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2979 | check_better_fast(xd, cm, &left_mv, bestmv, mv_limits, var_params, |
| 2980 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2981 | is_scaled); |
| 2982 | check_better_fast(xd, cm, &bottom_mv, bestmv, mv_limits, var_params, |
| 2983 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2984 | is_scaled); |
| 2985 | check_better_fast(xd, cm, &bottom_left_mv, bestmv, mv_limits, |
| 2986 | var_params, mv_cost_params, &besterr, sse1, |
| 2987 | distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2988 | break; |
| 2989 | case 1: // bottom right quadrant |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 2990 | check_better_fast(xd, cm, &right_mv, bestmv, mv_limits, var_params, |
| 2991 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2992 | is_scaled); |
| 2993 | check_better_fast(xd, cm, &bottom_mv, bestmv, mv_limits, var_params, |
| 2994 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 2995 | is_scaled); |
| 2996 | check_better_fast(xd, cm, &bottom_right_mv, bestmv, mv_limits, |
| 2997 | var_params, mv_cost_params, &besterr, sse1, |
| 2998 | distortion, &dummy, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 2999 | break; |
| 3000 | case 2: // top left quadrant |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3001 | check_better_fast(xd, cm, &left_mv, bestmv, mv_limits, var_params, |
| 3002 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3003 | is_scaled); |
| 3004 | check_better_fast(xd, cm, &top_mv, bestmv, mv_limits, var_params, |
| 3005 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3006 | is_scaled); |
| 3007 | check_better_fast(xd, cm, &top_left_mv, bestmv, mv_limits, var_params, |
| 3008 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3009 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3010 | break; |
| 3011 | case 3: // top right quadrant |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3012 | check_better_fast(xd, cm, &right_mv, bestmv, mv_limits, var_params, |
| 3013 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3014 | is_scaled); |
| 3015 | check_better_fast(xd, cm, &top_mv, bestmv, mv_limits, var_params, |
| 3016 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3017 | is_scaled); |
| 3018 | check_better_fast(xd, cm, &top_right_mv, bestmv, mv_limits, var_params, |
| 3019 | mv_cost_params, &besterr, sse1, distortion, &dummy, |
| 3020 | is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3021 | break; |
| 3022 | } |
| 3023 | } else { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3024 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 3025 | var_params, mv_cost_params, &besterr, sse1, |
| 3026 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | // Each subsequent iteration checks at least one point in common with |
| 3030 | // the last iteration could be 2 ( if diag selected) 1/4 pel |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 3031 | if (forced_stop < HALF_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 3032 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 3033 | return INT_MAX; |
| 3034 | } |
| 3035 | iter++; |
| 3036 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3037 | hstep >>= 1; |
| 3038 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3039 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 3040 | var_params, mv_cost_params, &besterr, sse1, |
| 3041 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | if (allow_hp && forced_stop == EIGHTH_PEL) { |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 3045 | if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) { |
| 3046 | return INT_MAX; |
| 3047 | } |
| 3048 | iter++; |
| 3049 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3050 | hstep >>= 1; |
| 3051 | start_mv = *bestmv; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3052 | two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits, |
| 3053 | var_params, mv_cost_params, &besterr, sse1, |
| 3054 | distortion, iters_per_step, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | return besterr; |
| 3058 | } |
| 3059 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3060 | int av1_find_best_sub_pixel_tree(MACROBLOCKD *xd, const AV1_COMMON *const cm, |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3061 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, |
Yaowu Xu | f8b0e9b | 2020-03-31 13:14:38 -0700 | [diff] [blame] | 3062 | MV start_mv, MV *bestmv, int *distortion, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3063 | unsigned int *sse1, |
| 3064 | int_mv *last_mv_search_list) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3065 | const int allow_hp = ms_params->allow_hp; |
| 3066 | const int forced_stop = ms_params->forced_stop; |
| 3067 | const int iters_per_step = ms_params->iters_per_step; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3068 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 3069 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3070 | const SUBPEL_SEARCH_TYPE subpel_search_type = |
| 3071 | ms_params->var_params.subpel_search_type; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3072 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3073 | |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 3074 | // How many steps to take. A round of 0 means fullpel search only, 1 means |
| 3075 | // half-pel, and so on. |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3076 | const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp); |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 3077 | int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3078 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3079 | unsigned int besterr = INT_MAX; |
| 3080 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3081 | *bestmv = start_mv; |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 3082 | #if CONFIG_SDP |
| 3083 | const struct scale_factors *const sf = |
| 3084 | is_intrabc_block(xd->mi[0], xd->tree_type) |
| 3085 | #else |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3086 | const struct scale_factors *const sf = is_intrabc_block(xd->mi[0]) |
leolzhao | 3ab5984 | 2021-05-11 10:07:48 -0700 | [diff] [blame] | 3087 | #endif |
| 3088 | ? &cm->sf_identity |
| 3089 | : xd->block_ref_scale_factors[0]; |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3090 | const int is_scaled = av1_is_scaled(sf); |
| 3091 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3092 | if (subpel_search_type != USE_2_TAPS_ORIG) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3093 | besterr = upsampled_setup_center_error(xd, cm, bestmv, var_params, |
| 3094 | mv_cost_params, sse1, distortion); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3095 | } else { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3096 | besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1, |
| 3097 | distortion); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3098 | } |
| 3099 | |
Yunqing Wang | 8e94c58 | 2020-04-15 17:24:47 -0700 | [diff] [blame] | 3100 | // If forced_stop is FULL_PEL, return. |
| 3101 | if (!round) return besterr; |
| 3102 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3103 | for (int iter = 0; iter < round; ++iter) { |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3104 | MV iter_center_mv = *bestmv; |
chiyotsai | 083db97 | 2020-03-30 11:43:52 -0700 | [diff] [blame] | 3105 | if (check_repeated_mv_and_update(last_mv_search_list, iter_center_mv, |
| 3106 | iter)) { |
| 3107 | return INT_MAX; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3108 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3109 | |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3110 | MV diag_step; |
| 3111 | if (subpel_search_type != USE_2_TAPS_ORIG) { |
| 3112 | diag_step = first_level_check(xd, cm, iter_center_mv, bestmv, hstep, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3113 | mv_limits, var_params, mv_cost_params, |
| 3114 | &besterr, sse1, distortion); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3115 | } else { |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3116 | diag_step = first_level_check_fast(xd, cm, iter_center_mv, bestmv, hstep, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3117 | mv_limits, var_params, mv_cost_params, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3118 | &besterr, sse1, distortion, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | // Check diagonal sub-pixel position |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3122 | if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) { |
| 3123 | second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3124 | mv_limits, var_params, mv_cost_params, &besterr, |
Mufaddal Chakera | 25bb7d0 | 2020-06-18 12:53:03 +0530 | [diff] [blame] | 3125 | sse1, distortion, is_scaled); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3126 | } |
| 3127 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3128 | hstep >>= 1; |
| 3129 | } |
| 3130 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3131 | return besterr; |
| 3132 | } |
| 3133 | |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 3134 | // Note(yunqingwang): The following 2 functions are only used in the motion |
| 3135 | // vector unit test, which return extreme motion vectors allowed by the MV |
| 3136 | // limits. |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3137 | // Returns the maximum MV. |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3138 | int av1_return_max_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm, |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 3139 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, |
Yaowu Xu | f8b0e9b | 2020-03-31 13:14:38 -0700 | [diff] [blame] | 3140 | MV start_mv, MV *bestmv, int *distortion, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3141 | unsigned int *sse1, |
| 3142 | int_mv *last_mv_search_list) { |
| 3143 | (void)xd; |
Urvang Joshi | 52b6299 | 2018-02-02 14:43:07 -0800 | [diff] [blame] | 3144 | (void)cm; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3145 | (void)start_mv; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3146 | (void)sse1; |
| 3147 | (void)distortion; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3148 | (void)last_mv_search_list; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3149 | |
| 3150 | const int allow_hp = ms_params->allow_hp; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3151 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3152 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3153 | bestmv->row = mv_limits->row_max; |
| 3154 | bestmv->col = mv_limits->col_max; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3155 | |
| 3156 | unsigned int besterr = 0; |
| 3157 | |
Yaowu Xu | 45295c3 | 2018-03-29 12:06:10 -0700 | [diff] [blame] | 3158 | // In the sub-pel motion search, if hp is not used, then the last bit of mv |
| 3159 | // has to be 0. |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 3160 | lower_mv_precision(bestmv, allow_hp, 0); |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 3161 | return besterr; |
| 3162 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3163 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3164 | // Returns the minimum MV. |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3165 | int av1_return_min_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm, |
chiyotsai | 2aac300 | 2020-02-13 17:02:01 -0800 | [diff] [blame] | 3166 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, |
Yaowu Xu | f8b0e9b | 2020-03-31 13:14:38 -0700 | [diff] [blame] | 3167 | MV start_mv, MV *bestmv, int *distortion, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3168 | unsigned int *sse1, |
| 3169 | int_mv *last_mv_search_list) { |
| 3170 | (void)xd; |
Urvang Joshi | 52b6299 | 2018-02-02 14:43:07 -0800 | [diff] [blame] | 3171 | (void)cm; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3172 | (void)start_mv; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3173 | (void)sse1; |
| 3174 | (void)distortion; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3175 | (void)last_mv_search_list; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3176 | |
| 3177 | const int allow_hp = ms_params->allow_hp; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3178 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3179 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3180 | bestmv->row = mv_limits->row_min; |
| 3181 | bestmv->col = mv_limits->col_min; |
chiyotsai | a742e7d | 2020-02-14 17:27:58 -0800 | [diff] [blame] | 3182 | |
| 3183 | unsigned int besterr = 0; |
Yaowu Xu | 45295c3 | 2018-03-29 12:06:10 -0700 | [diff] [blame] | 3184 | // In the sub-pel motion search, if hp is not used, then the last bit of mv |
| 3185 | // has to be 0. |
RogerZhou | 3b63524 | 2017-09-19 10:06:46 -0700 | [diff] [blame] | 3186 | lower_mv_precision(bestmv, allow_hp, 0); |
Yunqing Wang | ff4fa06 | 2017-04-21 10:56:08 -0700 | [diff] [blame] | 3187 | return besterr; |
| 3188 | } |
chiyotsai | 19a58ee | 2019-03-18 18:01:05 -0700 | [diff] [blame] | 3189 | |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3190 | // Computes the cost of the current predictor by going through the whole |
| 3191 | // av1_enc_build_inter_predictor pipeline. This is mainly used by warped mv |
| 3192 | // during motion_mode_rd. We are going through the whole |
| 3193 | // av1_enc_build_inter_predictor because we might have changed the interpolation |
| 3194 | // filter, etc before motion_mode_rd is called. |
| 3195 | static INLINE unsigned int compute_motion_cost( |
| 3196 | MACROBLOCKD *xd, const AV1_COMMON *const cm, |
| 3197 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, BLOCK_SIZE bsize, |
| 3198 | const MV *this_mv) { |
| 3199 | unsigned int mse; |
| 3200 | unsigned int sse; |
| 3201 | const int mi_row = xd->mi_row; |
| 3202 | const int mi_col = xd->mi_col; |
| 3203 | |
| 3204 | av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize, |
| 3205 | AOM_PLANE_Y, AOM_PLANE_Y); |
| 3206 | |
| 3207 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
| 3208 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 3209 | |
| 3210 | const uint8_t *const src = ms_buffers->src->buf; |
| 3211 | const int src_stride = ms_buffers->src->stride; |
| 3212 | const uint8_t *const dst = xd->plane[0].dst.buf; |
| 3213 | const int dst_stride = xd->plane[0].dst.stride; |
| 3214 | const aom_variance_fn_ptr_t *vfp = ms_params->var_params.vfp; |
| 3215 | |
| 3216 | mse = vfp->vf(dst, dst_stride, src, src_stride, &sse); |
| 3217 | mse += mv_err_cost_(this_mv, &ms_params->mv_cost_params); |
| 3218 | return mse; |
| 3219 | } |
| 3220 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3221 | // Refines MV in a small range |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3222 | unsigned int av1_refine_warped_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm, |
| 3223 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, |
| 3224 | BLOCK_SIZE bsize, const int *pts0, |
| 3225 | const int *pts_inref0, int total_samples) { |
chiyotsai | 19a58ee | 2019-03-18 18:01:05 -0700 | [diff] [blame] | 3226 | MB_MODE_INFO *mbmi = xd->mi[0]; |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3227 | static const MV neighbors[8] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, |
| 3228 | { 0, -2 }, { 2, 0 }, { 0, 2 }, { -2, 0 } }; |
| 3229 | MV *best_mv = &mbmi->mv[0].as_mv; |
| 3230 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3231 | WarpedMotionParams best_wm_params = mbmi->wm_params; |
| 3232 | int best_num_proj_ref = mbmi->num_proj_ref; |
| 3233 | unsigned int bestmse; |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3234 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 19a58ee | 2019-03-18 18:01:05 -0700 | [diff] [blame] | 3235 | |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3236 | const int start = ms_params->allow_hp ? 0 : 4; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3237 | |
| 3238 | // Calculate the center position's error |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3239 | assert(av1_is_subpelmv_in_range(mv_limits, *best_mv)); |
| 3240 | bestmse = compute_motion_cost(xd, cm, ms_params, bsize, best_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3241 | |
| 3242 | // MV search |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3243 | int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE]; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3244 | const int mi_row = xd->mi_row; |
| 3245 | const int mi_col = xd->mi_col; |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3246 | for (int ite = 0; ite < 2; ++ite) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3247 | int best_idx = -1; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3248 | |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3249 | for (int idx = start; idx < start + 4; ++idx) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3250 | unsigned int thismse; |
| 3251 | |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3252 | MV this_mv = { best_mv->row + neighbors[idx].row, |
| 3253 | best_mv->col + neighbors[idx].col }; |
| 3254 | if (av1_is_subpelmv_in_range(mv_limits, this_mv)) { |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3255 | memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0)); |
| 3256 | memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0)); |
| 3257 | if (total_samples > 1) |
| 3258 | mbmi->num_proj_ref = |
| 3259 | av1_selectSamples(&this_mv, pts, pts_inref, total_samples, bsize); |
| 3260 | |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3261 | if (!av1_find_projection(mbmi->num_proj_ref, pts, pts_inref, bsize, |
| 3262 | this_mv.row, this_mv.col, &mbmi->wm_params, |
| 3263 | mi_row, mi_col)) { |
| 3264 | thismse = compute_motion_cost(xd, cm, ms_params, bsize, &this_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3265 | |
| 3266 | if (thismse < bestmse) { |
| 3267 | best_idx = idx; |
| 3268 | best_wm_params = mbmi->wm_params; |
| 3269 | best_num_proj_ref = mbmi->num_proj_ref; |
| 3270 | bestmse = thismse; |
| 3271 | } |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | if (best_idx == -1) break; |
| 3277 | |
| 3278 | if (best_idx >= 0) { |
chiyotsai | 74da72b | 2020-04-07 13:45:40 -0700 | [diff] [blame] | 3279 | best_mv->row += neighbors[best_idx].row; |
| 3280 | best_mv->col += neighbors[best_idx].col; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3281 | } |
chiyotsai | 19a58ee | 2019-03-18 18:01:05 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3284 | mbmi->wm_params = best_wm_params; |
| 3285 | mbmi->num_proj_ref = best_num_proj_ref; |
| 3286 | return bestmse; |
chiyotsai | 19a58ee | 2019-03-18 18:01:05 -0700 | [diff] [blame] | 3287 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3288 | // ============================================================================= |
| 3289 | // Subpixel Motion Search: OBMC |
| 3290 | // ============================================================================= |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3291 | // Estimates the variance of prediction residue |
| 3292 | static INLINE int estimate_obmc_pref_error( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3293 | const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 3294 | unsigned int *sse) { |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3295 | const aom_variance_fn_ptr_t *vfp = var_params->vfp; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3296 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3297 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 3298 | const int32_t *src = ms_buffers->wsrc; |
| 3299 | const int32_t *mask = ms_buffers->obmc_mask; |
| 3300 | const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv); |
| 3301 | const int ref_stride = ms_buffers->ref->stride; |
| 3302 | |
| 3303 | const int subpel_x_q3 = get_subpel_part(this_mv->col); |
| 3304 | const int subpel_y_q3 = get_subpel_part(this_mv->row); |
| 3305 | |
| 3306 | return vfp->osvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, mask, sse); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3307 | } |
| 3308 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3309 | // Calculates the variance of prediction residue |
| 3310 | static int upsampled_obmc_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3311 | const MV *this_mv, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3312 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 3313 | unsigned int *sse) { |
| 3314 | const aom_variance_fn_ptr_t *vfp = var_params->vfp; |
| 3315 | const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type; |
| 3316 | const int w = var_params->w; |
| 3317 | const int h = var_params->h; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3318 | |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3319 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 3320 | const int32_t *wsrc = ms_buffers->wsrc; |
| 3321 | const int32_t *mask = ms_buffers->obmc_mask; |
| 3322 | const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv); |
| 3323 | const int ref_stride = ms_buffers->ref->stride; |
| 3324 | |
| 3325 | const int subpel_x_q3 = get_subpel_part(this_mv->col); |
| 3326 | const int subpel_y_q3 = get_subpel_part(this_mv->row); |
| 3327 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3328 | const int mi_row = xd->mi_row; |
| 3329 | const int mi_col = xd->mi_col; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3330 | |
| 3331 | unsigned int besterr; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3332 | DECLARE_ALIGNED(16, uint8_t, pred[2 * MAX_SB_SQUARE]); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3333 | if (is_cur_buf_hbd(xd)) { |
| 3334 | uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3335 | aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h, |
| 3336 | subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd, |
| 3337 | subpel_search_type); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3338 | besterr = vfp->ovf(pred8, w, wsrc, mask, sse); |
| 3339 | } else { |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3340 | aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, subpel_x_q3, |
| 3341 | subpel_y_q3, ref, ref_stride, subpel_search_type); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3342 | |
| 3343 | besterr = vfp->ovf(pred, w, wsrc, mask, sse); |
| 3344 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3345 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3346 | return besterr; |
| 3347 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3348 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3349 | static unsigned int setup_obmc_center_error( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3350 | const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3351 | const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3352 | // TODO(chiyotsai@google.com): There might be a bug here where we didn't use |
| 3353 | // get_buf_from_mv(ref, *this_mv). |
| 3354 | const MSBuffers *ms_buffers = &var_params->ms_buffers; |
| 3355 | const int32_t *wsrc = ms_buffers->wsrc; |
| 3356 | const int32_t *mask = ms_buffers->obmc_mask; |
| 3357 | const uint8_t *ref = ms_buffers->ref->buf; |
| 3358 | const int ref_stride = ms_buffers->ref->stride; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3359 | unsigned int besterr = |
| 3360 | var_params->vfp->ovf(ref, ref_stride, wsrc, mask, sse1); |
| 3361 | *distortion = besterr; |
| 3362 | besterr += mv_err_cost_(this_mv, mv_cost_params); |
| 3363 | return besterr; |
| 3364 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3365 | |
| 3366 | static unsigned int upsampled_setup_obmc_center_error( |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3367 | MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *this_mv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3368 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3369 | const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3370 | unsigned int besterr = |
| 3371 | upsampled_obmc_pref_error(xd, cm, this_mv, var_params, sse1); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3372 | *distortion = besterr; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3373 | besterr += mv_err_cost_(this_mv, mv_cost_params); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3374 | return besterr; |
| 3375 | } |
| 3376 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3377 | // Estimates the variance of prediction residue |
| 3378 | // TODO(chiyotsai@google.com): the cost does does not match the cost in |
| 3379 | // mv_cost_. Investigate this later. |
| 3380 | static INLINE int estimate_obmc_mvcost(const MV *this_mv, |
| 3381 | const MV_COST_PARAMS *mv_cost_params) { |
| 3382 | const MV *ref_mv = mv_cost_params->ref_mv; |
| 3383 | const int *mvjcost = mv_cost_params->mvjcost; |
| 3384 | const int *const *mvcost = mv_cost_params->mvcost; |
| 3385 | const int error_per_bit = mv_cost_params->error_per_bit; |
| 3386 | const MV_COST_TYPE mv_cost_type = mv_cost_params->mv_cost_type; |
| 3387 | const MV diff_mv = { GET_MV_SUBPEL(this_mv->row - ref_mv->row), |
| 3388 | GET_MV_SUBPEL(this_mv->col - ref_mv->col) }; |
| 3389 | |
| 3390 | switch (mv_cost_type) { |
| 3391 | case MV_COST_ENTROPY: |
| 3392 | return (unsigned)((mv_cost(&diff_mv, mvjcost, |
| 3393 | CONVERT_TO_CONST_MVCOST(mvcost)) * |
| 3394 | error_per_bit + |
| 3395 | 4096) >> |
| 3396 | 13); |
| 3397 | case MV_COST_NONE: return 0; |
| 3398 | default: |
| 3399 | assert(0 && "L1 norm is not tuned for estimated obmc mvcost"); |
| 3400 | return 0; |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | // Estimates whether this_mv is better than best_mv. This function incorporates |
| 3405 | // both prediction error and residue into account. |
| 3406 | static INLINE unsigned int obmc_check_better_fast( |
| 3407 | const MV *this_mv, MV *best_mv, const SubpelMvLimits *mv_limits, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3408 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3409 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 3410 | unsigned int *sse1, int *distortion, int *has_better_mv) { |
| 3411 | unsigned int cost; |
| 3412 | if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) { |
| 3413 | unsigned int sse; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3414 | const int thismse = estimate_obmc_pref_error(this_mv, var_params, &sse); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3415 | |
| 3416 | cost = estimate_obmc_mvcost(this_mv, mv_cost_params); |
| 3417 | cost += thismse; |
| 3418 | |
| 3419 | if (cost < *besterr) { |
| 3420 | *besterr = cost; |
| 3421 | *best_mv = *this_mv; |
| 3422 | *distortion = thismse; |
| 3423 | *sse1 = sse; |
| 3424 | *has_better_mv |= 1; |
| 3425 | } |
| 3426 | } else { |
| 3427 | cost = INT_MAX; |
| 3428 | } |
| 3429 | return cost; |
| 3430 | } |
| 3431 | |
| 3432 | // Estimates whether this_mv is better than best_mv. This function incorporates |
| 3433 | // both prediction error and residue into account. |
| 3434 | static INLINE unsigned int obmc_check_better( |
| 3435 | MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3436 | const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3437 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 3438 | unsigned int *sse1, int *distortion, int *has_better_mv) { |
| 3439 | unsigned int cost; |
| 3440 | if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) { |
| 3441 | unsigned int sse; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3442 | const int thismse = |
| 3443 | upsampled_obmc_pref_error(xd, cm, this_mv, var_params, &sse); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3444 | cost = mv_err_cost_(this_mv, mv_cost_params); |
| 3445 | |
| 3446 | cost += thismse; |
| 3447 | |
| 3448 | if (cost < *besterr) { |
| 3449 | *besterr = cost; |
| 3450 | *best_mv = *this_mv; |
| 3451 | *distortion = thismse; |
| 3452 | *sse1 = sse; |
| 3453 | *has_better_mv |= 1; |
| 3454 | } |
| 3455 | } else { |
| 3456 | cost = INT_MAX; |
| 3457 | } |
| 3458 | return cost; |
| 3459 | } |
| 3460 | |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3461 | static AOM_FORCE_INLINE MV obmc_first_level_check( |
| 3462 | MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV *best_mv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3463 | const int hstep, const SubpelMvLimits *mv_limits, |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3464 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 3465 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 3466 | unsigned int *sse1, int *distortion) { |
| 3467 | int dummy = 0; |
| 3468 | const MV left_mv = { this_mv.row, this_mv.col - hstep }; |
| 3469 | const MV right_mv = { this_mv.row, this_mv.col + hstep }; |
| 3470 | const MV top_mv = { this_mv.row - hstep, this_mv.col }; |
| 3471 | const MV bottom_mv = { this_mv.row + hstep, this_mv.col }; |
| 3472 | |
| 3473 | if (var_params->subpel_search_type != USE_2_TAPS_ORIG) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3474 | const unsigned int left = |
| 3475 | obmc_check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params, |
| 3476 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 3477 | const unsigned int right = |
| 3478 | obmc_check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params, |
| 3479 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 3480 | const unsigned int up = |
| 3481 | obmc_check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params, |
| 3482 | mv_cost_params, besterr, sse1, distortion, &dummy); |
| 3483 | const unsigned int down = |
| 3484 | obmc_check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params, |
| 3485 | mv_cost_params, besterr, sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3486 | |
| 3487 | const MV diag_step = get_best_diag_step(hstep, left, right, up, down); |
| 3488 | const MV diag_mv = { this_mv.row + diag_step.row, |
| 3489 | this_mv.col + diag_step.col }; |
| 3490 | |
| 3491 | // Check the diagonal direction with the best mv |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3492 | obmc_check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params, |
| 3493 | mv_cost_params, besterr, sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3494 | |
| 3495 | return diag_step; |
| 3496 | } else { |
| 3497 | const unsigned int left = obmc_check_better_fast( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3498 | &left_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1, |
| 3499 | distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3500 | const unsigned int right = obmc_check_better_fast( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3501 | &right_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, |
| 3502 | sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3503 | |
| 3504 | const unsigned int up = obmc_check_better_fast( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3505 | &top_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1, |
| 3506 | distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3507 | |
| 3508 | const unsigned int down = obmc_check_better_fast( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3509 | &bottom_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, |
| 3510 | sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3511 | |
| 3512 | const MV diag_step = get_best_diag_step(hstep, left, right, up, down); |
| 3513 | const MV diag_mv = { this_mv.row + diag_step.row, |
| 3514 | this_mv.col + diag_step.col }; |
| 3515 | |
| 3516 | // Check the diagonal direction with the best mv |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3517 | obmc_check_better_fast(&diag_mv, best_mv, mv_limits, var_params, |
| 3518 | mv_cost_params, besterr, sse1, distortion, &dummy); |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3519 | |
| 3520 | return diag_step; |
| 3521 | } |
| 3522 | } |
| 3523 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3524 | // A newer version of second level check for obmc that gives better quality. |
| 3525 | static AOM_FORCE_INLINE void obmc_second_level_check_v2( |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3526 | MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3527 | MV *best_mv, const SubpelMvLimits *mv_limits, |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3528 | const SUBPEL_SEARCH_VAR_PARAMS *var_params, |
| 3529 | const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr, |
| 3530 | unsigned int *sse1, int *distortion) { |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3531 | assert(best_mv->row == this_mv.row + diag_step.row || |
| 3532 | best_mv->col == this_mv.col + diag_step.col); |
| 3533 | if (CHECK_MV_EQUAL(this_mv, *best_mv)) { |
| 3534 | return; |
| 3535 | } else if (this_mv.row == best_mv->row) { |
| 3536 | // Search away from diagonal step since diagonal search did not provide any |
| 3537 | // improvement |
| 3538 | diag_step.row *= -1; |
| 3539 | } else if (this_mv.col == best_mv->col) { |
| 3540 | diag_step.col *= -1; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3541 | } |
| 3542 | |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3543 | const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col }; |
| 3544 | const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col }; |
| 3545 | const MV diag_bias_mv = { best_mv->row + diag_step.row, |
| 3546 | best_mv->col + diag_step.col }; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3547 | int has_better_mv = 0; |
| 3548 | |
| 3549 | if (var_params->subpel_search_type != USE_2_TAPS_ORIG) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3550 | obmc_check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params, |
| 3551 | mv_cost_params, besterr, sse1, distortion, |
| 3552 | &has_better_mv); |
| 3553 | obmc_check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params, |
| 3554 | mv_cost_params, besterr, sse1, distortion, |
| 3555 | &has_better_mv); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3556 | |
| 3557 | // Do an additional search if the second iteration gives a better mv |
| 3558 | if (has_better_mv) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3559 | obmc_check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params, |
| 3560 | mv_cost_params, besterr, sse1, distortion, |
| 3561 | &has_better_mv); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3562 | } |
| 3563 | } else { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3564 | obmc_check_better_fast(&row_bias_mv, best_mv, mv_limits, var_params, |
| 3565 | mv_cost_params, besterr, sse1, distortion, |
| 3566 | &has_better_mv); |
| 3567 | obmc_check_better_fast(&col_bias_mv, best_mv, mv_limits, var_params, |
| 3568 | mv_cost_params, besterr, sse1, distortion, |
| 3569 | &has_better_mv); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3570 | |
| 3571 | // Do an additional search if the second iteration gives a better mv |
| 3572 | if (has_better_mv) { |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3573 | obmc_check_better_fast(&diag_bias_mv, best_mv, mv_limits, var_params, |
| 3574 | mv_cost_params, besterr, sse1, distortion, |
| 3575 | &has_better_mv); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | } |
| 3579 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3580 | int av1_find_best_obmc_sub_pixel_tree_up( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3581 | MACROBLOCKD *xd, const AV1_COMMON *const cm, |
Yaowu Xu | f8b0e9b | 2020-03-31 13:14:38 -0700 | [diff] [blame] | 3582 | const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3583 | int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) { |
| 3584 | (void)last_mv_search_list; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3585 | const int allow_hp = ms_params->allow_hp; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3586 | const int forced_stop = ms_params->forced_stop; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3587 | const int iters_per_step = ms_params->iters_per_step; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3588 | const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params; |
| 3589 | const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3590 | const SUBPEL_SEARCH_TYPE subpel_search_type = |
| 3591 | ms_params->var_params.subpel_search_type; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3592 | const SubpelMvLimits *mv_limits = &ms_params->mv_limits; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3593 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3594 | int hstep = INIT_SUBPEL_STEP_SIZE; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3595 | const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3596 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3597 | unsigned int besterr = INT_MAX; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3598 | *bestmv = start_mv; |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3599 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3600 | if (subpel_search_type != USE_2_TAPS_ORIG) |
| 3601 | besterr = upsampled_setup_obmc_center_error( |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3602 | xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3603 | else |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3604 | besterr = setup_obmc_center_error(bestmv, var_params, mv_cost_params, sse1, |
| 3605 | distortion); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3606 | |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3607 | for (int iter = 0; iter < round; ++iter) { |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3608 | MV iter_center_mv = *bestmv; |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3609 | MV diag_step = obmc_first_level_check(xd, cm, iter_center_mv, bestmv, hstep, |
| 3610 | mv_limits, var_params, mv_cost_params, |
| 3611 | &besterr, sse1, distortion); |
chiyotsai | a7e2cf8 | 2020-02-19 16:13:29 -0800 | [diff] [blame] | 3612 | |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3613 | if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) { |
| 3614 | obmc_second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv, |
chiyotsai | 5aa7075 | 2020-03-26 10:13:10 -0700 | [diff] [blame] | 3615 | mv_limits, var_params, mv_cost_params, |
chiyotsai | 981eb5f | 2020-03-19 14:26:17 -0700 | [diff] [blame] | 3616 | &besterr, sse1, distortion); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3617 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3618 | hstep >>= 1; |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3619 | } |
| 3620 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3621 | return besterr; |
| 3622 | } |
| 3623 | |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3624 | // ============================================================================= |
| 3625 | // Public cost function: mv_cost + pred error |
| 3626 | // ============================================================================= |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 3627 | int av1_get_mvpred_sse(const MV_COST_PARAMS *mv_cost_params, |
| 3628 | const FULLPEL_MV best_mv, |
| 3629 | const aom_variance_fn_ptr_t *vfp, |
| 3630 | const struct buf_2d *src, const struct buf_2d *pre) { |
| 3631 | const MV mv = get_mv_from_fullmv(&best_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3632 | unsigned int sse, var; |
| 3633 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 3634 | var = vfp->vf(src->buf, src->stride, get_buf_from_fullmv(pre, &best_mv), |
| 3635 | pre->stride, &sse); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3636 | (void)var; |
| 3637 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 3638 | return sse + mv_err_cost_(&mv, mv_cost_params); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3639 | } |
| 3640 | |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3641 | static INLINE int get_mvpred_av_var(const MV_COST_PARAMS *mv_cost_params, |
| 3642 | const FULLPEL_MV best_mv, |
| 3643 | const uint8_t *second_pred, |
| 3644 | const aom_variance_fn_ptr_t *vfp, |
| 3645 | const struct buf_2d *src, |
| 3646 | const struct buf_2d *pre) { |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3647 | const MV mv = get_mv_from_fullmv(&best_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3648 | unsigned int unused; |
| 3649 | |
chiyotsai | c95e364 | 2020-04-10 13:17:06 -0700 | [diff] [blame] | 3650 | return vfp->svaf(get_buf_from_fullmv(pre, &best_mv), pre->stride, 0, 0, |
| 3651 | src->buf, src->stride, &unused, second_pred) + |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3652 | mv_err_cost_(&mv, mv_cost_params); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3653 | } |
| 3654 | |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3655 | static INLINE int get_mvpred_mask_var( |
| 3656 | const MV_COST_PARAMS *mv_cost_params, const FULLPEL_MV best_mv, |
| 3657 | const uint8_t *second_pred, const uint8_t *mask, int mask_stride, |
| 3658 | int invert_mask, const aom_variance_fn_ptr_t *vfp, const struct buf_2d *src, |
| 3659 | const struct buf_2d *pre) { |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3660 | const MV mv = get_mv_from_fullmv(&best_mv); |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3661 | unsigned int unused; |
| 3662 | |
Jingning Han | 142ade2 | 2020-10-10 00:32:34 -0700 | [diff] [blame] | 3663 | return vfp->msvf(get_buf_from_fullmv(pre, &best_mv), pre->stride, 0, 0, |
| 3664 | src->buf, src->stride, second_pred, mask, mask_stride, |
| 3665 | invert_mask, &unused) + |
chiyotsai | 30a5bdf | 2020-04-06 14:28:59 -0700 | [diff] [blame] | 3666 | mv_err_cost_(&mv, mv_cost_params); |
| 3667 | } |
| 3668 | |
| 3669 | int av1_get_mvpred_compound_var(const MV_COST_PARAMS *mv_cost_params, |
| 3670 | const FULLPEL_MV best_mv, |
| 3671 | const uint8_t *second_pred, const uint8_t *mask, |
| 3672 | int mask_stride, int invert_mask, |
| 3673 | const aom_variance_fn_ptr_t *vfp, |
| 3674 | const struct buf_2d *src, |
| 3675 | const struct buf_2d *pre) { |
| 3676 | if (mask) { |
| 3677 | return get_mvpred_mask_var(mv_cost_params, best_mv, second_pred, mask, |
| 3678 | mask_stride, invert_mask, vfp, src, pre); |
| 3679 | } else { |
| 3680 | return get_mvpred_av_var(mv_cost_params, best_mv, second_pred, vfp, src, |
| 3681 | pre); |
| 3682 | } |
chiyotsai | 9f664e3 | 2020-02-19 14:17:31 -0800 | [diff] [blame] | 3683 | } |