1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2020, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/reconinter.h"
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/interp_search.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/mcomp.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/motion_search_facade.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/partition_strategy.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/reconinter_enc.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tpl_model.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/tx_search.h"
23*77c1e3ccSAndroid Build Coastguard Worker
24*77c1e3ccSAndroid Build Coastguard Worker #define RIGHT_SHIFT_MV(x) (((x) + 3 + ((x) >= 0)) >> 3)
25*77c1e3ccSAndroid Build Coastguard Worker
26*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
27*77c1e3ccSAndroid Build Coastguard Worker int_mv fmv;
28*77c1e3ccSAndroid Build Coastguard Worker int weight;
29*77c1e3ccSAndroid Build Coastguard Worker } cand_mv_t;
30*77c1e3ccSAndroid Build Coastguard Worker
compare_weight(const void * a,const void * b)31*77c1e3ccSAndroid Build Coastguard Worker static int compare_weight(const void *a, const void *b) {
32*77c1e3ccSAndroid Build Coastguard Worker const int diff = ((cand_mv_t *)a)->weight - ((cand_mv_t *)b)->weight;
33*77c1e3ccSAndroid Build Coastguard Worker if (diff < 0)
34*77c1e3ccSAndroid Build Coastguard Worker return 1;
35*77c1e3ccSAndroid Build Coastguard Worker else if (diff > 0)
36*77c1e3ccSAndroid Build Coastguard Worker return -1;
37*77c1e3ccSAndroid Build Coastguard Worker return 0;
38*77c1e3ccSAndroid Build Coastguard Worker }
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker // Allow more mesh searches for screen content type on the ARF.
use_fine_search_interval(const AV1_COMP * const cpi)41*77c1e3ccSAndroid Build Coastguard Worker static int use_fine_search_interval(const AV1_COMP *const cpi) {
42*77c1e3ccSAndroid Build Coastguard Worker return cpi->is_screen_content_type &&
43*77c1e3ccSAndroid Build Coastguard Worker cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == ARF_UPDATE &&
44*77c1e3ccSAndroid Build Coastguard Worker cpi->oxcf.speed <= 2;
45*77c1e3ccSAndroid Build Coastguard Worker }
46*77c1e3ccSAndroid Build Coastguard Worker
47*77c1e3ccSAndroid Build Coastguard Worker // Iterate through the tpl and collect the mvs to be used as candidates
get_mv_candidate_from_tpl(const AV1_COMP * const cpi,const MACROBLOCK * x,BLOCK_SIZE bsize,int ref,cand_mv_t * cand,int * cand_count,int * total_cand_weight)48*77c1e3ccSAndroid Build Coastguard Worker static inline void get_mv_candidate_from_tpl(const AV1_COMP *const cpi,
49*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCK *x,
50*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, int ref,
51*77c1e3ccSAndroid Build Coastguard Worker cand_mv_t *cand, int *cand_count,
52*77c1e3ccSAndroid Build Coastguard Worker int *total_cand_weight) {
53*77c1e3ccSAndroid Build Coastguard Worker const SuperBlockEnc *sb_enc = &x->sb_enc;
54*77c1e3ccSAndroid Build Coastguard Worker if (!sb_enc->tpl_data_count) {
55*77c1e3ccSAndroid Build Coastguard Worker return;
56*77c1e3ccSAndroid Build Coastguard Worker }
57*77c1e3ccSAndroid Build Coastguard Worker
58*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *cm = &cpi->common;
59*77c1e3ccSAndroid Build Coastguard Worker const MACROBLOCKD *xd = &x->e_mbd;
60*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
61*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
62*77c1e3ccSAndroid Build Coastguard Worker
63*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE tpl_bsize =
64*77c1e3ccSAndroid Build Coastguard Worker convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
65*77c1e3ccSAndroid Build Coastguard Worker const int tplw = mi_size_wide[tpl_bsize];
66*77c1e3ccSAndroid Build Coastguard Worker const int tplh = mi_size_high[tpl_bsize];
67*77c1e3ccSAndroid Build Coastguard Worker const int nw = mi_size_wide[bsize] / tplw;
68*77c1e3ccSAndroid Build Coastguard Worker const int nh = mi_size_high[bsize] / tplh;
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker if (nw >= 1 && nh >= 1) {
71*77c1e3ccSAndroid Build Coastguard Worker const int of_h = mi_row % mi_size_high[cm->seq_params->sb_size];
72*77c1e3ccSAndroid Build Coastguard Worker const int of_w = mi_col % mi_size_wide[cm->seq_params->sb_size];
73*77c1e3ccSAndroid Build Coastguard Worker const int start = of_h / tplh * sb_enc->tpl_stride + of_w / tplw;
74*77c1e3ccSAndroid Build Coastguard Worker int valid = 1;
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker // Assign large weight to start_mv, so it is always tested.
77*77c1e3ccSAndroid Build Coastguard Worker cand[0].weight = nw * nh;
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker for (int k = 0; k < nh; k++) {
80*77c1e3ccSAndroid Build Coastguard Worker for (int l = 0; l < nw; l++) {
81*77c1e3ccSAndroid Build Coastguard Worker const int_mv mv =
82*77c1e3ccSAndroid Build Coastguard Worker sb_enc
83*77c1e3ccSAndroid Build Coastguard Worker ->tpl_mv[start + k * sb_enc->tpl_stride + l][ref - LAST_FRAME];
84*77c1e3ccSAndroid Build Coastguard Worker if (mv.as_int == INVALID_MV) {
85*77c1e3ccSAndroid Build Coastguard Worker valid = 0;
86*77c1e3ccSAndroid Build Coastguard Worker break;
87*77c1e3ccSAndroid Build Coastguard Worker }
88*77c1e3ccSAndroid Build Coastguard Worker
89*77c1e3ccSAndroid Build Coastguard Worker const FULLPEL_MV fmv = { GET_MV_RAWPEL(mv.as_mv.row),
90*77c1e3ccSAndroid Build Coastguard Worker GET_MV_RAWPEL(mv.as_mv.col) };
91*77c1e3ccSAndroid Build Coastguard Worker int unique = 1;
92*77c1e3ccSAndroid Build Coastguard Worker for (int m = 0; m < *cand_count; m++) {
93*77c1e3ccSAndroid Build Coastguard Worker if (RIGHT_SHIFT_MV(fmv.row) ==
94*77c1e3ccSAndroid Build Coastguard Worker RIGHT_SHIFT_MV(cand[m].fmv.as_fullmv.row) &&
95*77c1e3ccSAndroid Build Coastguard Worker RIGHT_SHIFT_MV(fmv.col) ==
96*77c1e3ccSAndroid Build Coastguard Worker RIGHT_SHIFT_MV(cand[m].fmv.as_fullmv.col)) {
97*77c1e3ccSAndroid Build Coastguard Worker unique = 0;
98*77c1e3ccSAndroid Build Coastguard Worker cand[m].weight++;
99*77c1e3ccSAndroid Build Coastguard Worker break;
100*77c1e3ccSAndroid Build Coastguard Worker }
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker
103*77c1e3ccSAndroid Build Coastguard Worker if (unique) {
104*77c1e3ccSAndroid Build Coastguard Worker cand[*cand_count].fmv.as_fullmv = fmv;
105*77c1e3ccSAndroid Build Coastguard Worker cand[*cand_count].weight = 1;
106*77c1e3ccSAndroid Build Coastguard Worker (*cand_count)++;
107*77c1e3ccSAndroid Build Coastguard Worker }
108*77c1e3ccSAndroid Build Coastguard Worker }
109*77c1e3ccSAndroid Build Coastguard Worker if (!valid) break;
110*77c1e3ccSAndroid Build Coastguard Worker }
111*77c1e3ccSAndroid Build Coastguard Worker
112*77c1e3ccSAndroid Build Coastguard Worker if (valid) {
113*77c1e3ccSAndroid Build Coastguard Worker *total_cand_weight = 2 * nh * nw;
114*77c1e3ccSAndroid Build Coastguard Worker if (*cand_count > 2)
115*77c1e3ccSAndroid Build Coastguard Worker qsort(cand, *cand_count, sizeof(cand[0]), &compare_weight);
116*77c1e3ccSAndroid Build Coastguard Worker }
117*77c1e3ccSAndroid Build Coastguard Worker }
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker
av1_single_motion_search(const AV1_COMP * const cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int ref_idx,int * rate_mv,int search_range,inter_mode_info * mode_info,int_mv * best_mv,struct HandleInterModeArgs * const args)120*77c1e3ccSAndroid Build Coastguard Worker void av1_single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x,
121*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, int ref_idx, int *rate_mv,
122*77c1e3ccSAndroid Build Coastguard Worker int search_range, inter_mode_info *mode_info,
123*77c1e3ccSAndroid Build Coastguard Worker int_mv *best_mv,
124*77c1e3ccSAndroid Build Coastguard Worker struct HandleInterModeArgs *const args) {
125*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
126*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *cm = &cpi->common;
127*77c1e3ccSAndroid Build Coastguard Worker const MotionVectorSearchParams *mv_search_params = &cpi->mv_search_params;
128*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
129*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
130*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
131*77c1e3ccSAndroid Build Coastguard Worker int bestsme = INT_MAX;
132*77c1e3ccSAndroid Build Coastguard Worker const int ref = mbmi->ref_frame[ref_idx];
133*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *scaled_ref_frame =
134*77c1e3ccSAndroid Build Coastguard Worker av1_get_scaled_ref_frame(cpi, ref);
135*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
136*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
137*77c1e3ccSAndroid Build Coastguard Worker const MvCosts *mv_costs = x->mv_costs;
138*77c1e3ccSAndroid Build Coastguard Worker
139*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
140*77c1e3ccSAndroid Build Coastguard Worker // Swap out the reference frame for a version that's been scaled to
141*77c1e3ccSAndroid Build Coastguard Worker // match the resolution of the current frame, allowing the existing
142*77c1e3ccSAndroid Build Coastguard Worker // full-pixel motion search code to be used without additional
143*77c1e3ccSAndroid Build Coastguard Worker // modifications.
144*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_planes; i++) {
145*77c1e3ccSAndroid Build Coastguard Worker backup_yv12[i] = xd->plane[i].pre[ref_idx];
146*77c1e3ccSAndroid Build Coastguard Worker }
147*77c1e3ccSAndroid Build Coastguard Worker av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
148*77c1e3ccSAndroid Build Coastguard Worker num_planes);
149*77c1e3ccSAndroid Build Coastguard Worker }
150*77c1e3ccSAndroid Build Coastguard Worker
151*77c1e3ccSAndroid Build Coastguard Worker // Work out the size of the first step in the mv step search.
152*77c1e3ccSAndroid Build Coastguard Worker // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc.
153*77c1e3ccSAndroid Build Coastguard Worker int step_param;
154*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.mv_sf.auto_mv_step_size && cm->show_frame) {
155*77c1e3ccSAndroid Build Coastguard Worker // Take the weighted average of the step_params based on the last frame's
156*77c1e3ccSAndroid Build Coastguard Worker // max mv magnitude and that based on the best ref mvs of the current
157*77c1e3ccSAndroid Build Coastguard Worker // block for the given reference.
158*77c1e3ccSAndroid Build Coastguard Worker step_param = (av1_init_search_range(x->max_mv_context[ref]) +
159*77c1e3ccSAndroid Build Coastguard Worker mv_search_params->mv_step_param) /
160*77c1e3ccSAndroid Build Coastguard Worker 2;
161*77c1e3ccSAndroid Build Coastguard Worker } else {
162*77c1e3ccSAndroid Build Coastguard Worker step_param = mv_search_params->mv_step_param;
163*77c1e3ccSAndroid Build Coastguard Worker }
164*77c1e3ccSAndroid Build Coastguard Worker
165*77c1e3ccSAndroid Build Coastguard Worker const MV ref_mv = av1_get_ref_mv(x, ref_idx).as_mv;
166*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV start_mv;
167*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->motion_mode != SIMPLE_TRANSLATION)
168*77c1e3ccSAndroid Build Coastguard Worker start_mv = get_fullmv_from_mv(&mbmi->mv[0].as_mv);
169*77c1e3ccSAndroid Build Coastguard Worker else
170*77c1e3ccSAndroid Build Coastguard Worker start_mv = get_fullmv_from_mv(&ref_mv);
171*77c1e3ccSAndroid Build Coastguard Worker
172*77c1e3ccSAndroid Build Coastguard Worker // cand stores start_mv and all possible MVs in a SB.
173*77c1e3ccSAndroid Build Coastguard Worker cand_mv_t cand[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB + 1];
174*77c1e3ccSAndroid Build Coastguard Worker av1_zero(cand);
175*77c1e3ccSAndroid Build Coastguard Worker cand[0].fmv.as_fullmv = start_mv;
176*77c1e3ccSAndroid Build Coastguard Worker int cnt = 1;
177*77c1e3ccSAndroid Build Coastguard Worker int total_weight = 0;
178*77c1e3ccSAndroid Build Coastguard Worker
179*77c1e3ccSAndroid Build Coastguard Worker if (!cpi->sf.mv_sf.full_pixel_search_level &&
180*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode == SIMPLE_TRANSLATION) {
181*77c1e3ccSAndroid Build Coastguard Worker get_mv_candidate_from_tpl(cpi, x, bsize, ref, cand, &cnt, &total_weight);
182*77c1e3ccSAndroid Build Coastguard Worker }
183*77c1e3ccSAndroid Build Coastguard Worker
184*77c1e3ccSAndroid Build Coastguard Worker const int cand_cnt = AOMMIN(2, cnt);
185*77c1e3ccSAndroid Build Coastguard Worker // TODO(any): Test the speed feature for OBMC_CAUSAL mode.
186*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.mv_sf.skip_fullpel_search_using_startmv &&
187*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode == SIMPLE_TRANSLATION) {
188*77c1e3ccSAndroid Build Coastguard Worker const int stack_size = args->start_mv_cnt;
189*77c1e3ccSAndroid Build Coastguard Worker for (int cand_idx = 0; cand_idx < cand_cnt; cand_idx++) {
190*77c1e3ccSAndroid Build Coastguard Worker int_mv *fmv_cand = &cand[cand_idx].fmv;
191*77c1e3ccSAndroid Build Coastguard Worker int skip_cand_mv = 0;
192*77c1e3ccSAndroid Build Coastguard Worker
193*77c1e3ccSAndroid Build Coastguard Worker // Check difference between mvs in the stack and candidate mv.
194*77c1e3ccSAndroid Build Coastguard Worker for (int stack_idx = 0; stack_idx < stack_size; stack_idx++) {
195*77c1e3ccSAndroid Build Coastguard Worker const uint8_t this_ref_mv_idx = args->ref_mv_idx_stack[stack_idx];
196*77c1e3ccSAndroid Build Coastguard Worker const FULLPEL_MV *fmv_stack = &args->start_mv_stack[stack_idx];
197*77c1e3ccSAndroid Build Coastguard Worker const int this_newmv_valid =
198*77c1e3ccSAndroid Build Coastguard Worker args->single_newmv_valid[this_ref_mv_idx][ref];
199*77c1e3ccSAndroid Build Coastguard Worker const int row_diff = abs(fmv_stack->row - fmv_cand->as_fullmv.row);
200*77c1e3ccSAndroid Build Coastguard Worker const int col_diff = abs(fmv_stack->col - fmv_cand->as_fullmv.col);
201*77c1e3ccSAndroid Build Coastguard Worker
202*77c1e3ccSAndroid Build Coastguard Worker if (!this_newmv_valid) continue;
203*77c1e3ccSAndroid Build Coastguard Worker
204*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.mv_sf.skip_fullpel_search_using_startmv >= 2) {
205*77c1e3ccSAndroid Build Coastguard Worker // Prunes the current start_mv candidate, if the absolute mv
206*77c1e3ccSAndroid Build Coastguard Worker // difference of both row and column are <= 1.
207*77c1e3ccSAndroid Build Coastguard Worker if (row_diff <= 1 && col_diff <= 1) {
208*77c1e3ccSAndroid Build Coastguard Worker skip_cand_mv = 1;
209*77c1e3ccSAndroid Build Coastguard Worker break;
210*77c1e3ccSAndroid Build Coastguard Worker }
211*77c1e3ccSAndroid Build Coastguard Worker } else if (cpi->sf.mv_sf.skip_fullpel_search_using_startmv >= 1) {
212*77c1e3ccSAndroid Build Coastguard Worker // Prunes the current start_mv candidate, if the sum of the absolute
213*77c1e3ccSAndroid Build Coastguard Worker // mv difference of row and column is <= 1.
214*77c1e3ccSAndroid Build Coastguard Worker if (row_diff + col_diff <= 1) {
215*77c1e3ccSAndroid Build Coastguard Worker skip_cand_mv = 1;
216*77c1e3ccSAndroid Build Coastguard Worker break;
217*77c1e3ccSAndroid Build Coastguard Worker }
218*77c1e3ccSAndroid Build Coastguard Worker }
219*77c1e3ccSAndroid Build Coastguard Worker }
220*77c1e3ccSAndroid Build Coastguard Worker if (skip_cand_mv) {
221*77c1e3ccSAndroid Build Coastguard Worker // Ensure atleast one full-pel motion search is not pruned.
222*77c1e3ccSAndroid Build Coastguard Worker assert(mbmi->ref_mv_idx != 0);
223*77c1e3ccSAndroid Build Coastguard Worker // Mark the candidate mv as invalid so that motion search gets skipped.
224*77c1e3ccSAndroid Build Coastguard Worker cand[cand_idx].fmv.as_int = INVALID_MV;
225*77c1e3ccSAndroid Build Coastguard Worker } else {
226*77c1e3ccSAndroid Build Coastguard Worker // Store start_mv candidate and corresponding ref_mv_idx of full-pel
227*77c1e3ccSAndroid Build Coastguard Worker // search in the mv stack (except last ref_mv_idx).
228*77c1e3ccSAndroid Build Coastguard Worker if (mbmi->ref_mv_idx != MAX_REF_MV_SEARCH - 1) {
229*77c1e3ccSAndroid Build Coastguard Worker assert(args->start_mv_cnt < (MAX_REF_MV_SEARCH - 1) * 2);
230*77c1e3ccSAndroid Build Coastguard Worker args->start_mv_stack[args->start_mv_cnt] = fmv_cand->as_fullmv;
231*77c1e3ccSAndroid Build Coastguard Worker args->ref_mv_idx_stack[args->start_mv_cnt] = mbmi->ref_mv_idx;
232*77c1e3ccSAndroid Build Coastguard Worker args->start_mv_cnt++;
233*77c1e3ccSAndroid Build Coastguard Worker }
234*77c1e3ccSAndroid Build Coastguard Worker }
235*77c1e3ccSAndroid Build Coastguard Worker }
236*77c1e3ccSAndroid Build Coastguard Worker }
237*77c1e3ccSAndroid Build Coastguard Worker
238*77c1e3ccSAndroid Build Coastguard Worker // Hot fix for asan complaints when resize mode is on. When resize mode is on,
239*77c1e3ccSAndroid Build Coastguard Worker // the stride of the reference frame can be different from indicated by
240*77c1e3ccSAndroid Build Coastguard Worker // MotionVectorSearchParams::search_site_cfg. When this happens, we need to
241*77c1e3ccSAndroid Build Coastguard Worker // readjust the stride.
242*77c1e3ccSAndroid Build Coastguard Worker const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
243*77c1e3ccSAndroid Build Coastguard Worker const SEARCH_METHODS search_method =
244*77c1e3ccSAndroid Build Coastguard Worker av1_get_default_mv_search_method(x, mv_sf, bsize);
245*77c1e3ccSAndroid Build Coastguard Worker const search_site_config *src_search_site_cfg =
246*77c1e3ccSAndroid Build Coastguard Worker av1_get_search_site_config(cpi, x, search_method);
247*77c1e3ccSAndroid Build Coastguard Worker
248*77c1e3ccSAndroid Build Coastguard Worker // Further reduce the search range.
249*77c1e3ccSAndroid Build Coastguard Worker if (search_range < INT_MAX) {
250*77c1e3ccSAndroid Build Coastguard Worker const search_site_config *search_site_cfg =
251*77c1e3ccSAndroid Build Coastguard Worker &src_search_site_cfg[search_method_lookup[search_method]];
252*77c1e3ccSAndroid Build Coastguard Worker // Max step_param is search_site_cfg->num_search_steps.
253*77c1e3ccSAndroid Build Coastguard Worker if (search_range < 1) {
254*77c1e3ccSAndroid Build Coastguard Worker step_param = search_site_cfg->num_search_steps;
255*77c1e3ccSAndroid Build Coastguard Worker } else {
256*77c1e3ccSAndroid Build Coastguard Worker while (search_site_cfg->radius[search_site_cfg->num_search_steps -
257*77c1e3ccSAndroid Build Coastguard Worker step_param - 1] > (search_range << 1) &&
258*77c1e3ccSAndroid Build Coastguard Worker search_site_cfg->num_search_steps - step_param - 1 > 0)
259*77c1e3ccSAndroid Build Coastguard Worker step_param++;
260*77c1e3ccSAndroid Build Coastguard Worker }
261*77c1e3ccSAndroid Build Coastguard Worker }
262*77c1e3ccSAndroid Build Coastguard Worker
263*77c1e3ccSAndroid Build Coastguard Worker int cost_list[5];
264*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV_STATS best_mv_stats;
265*77c1e3ccSAndroid Build Coastguard Worker int_mv second_best_mv;
266*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_int = second_best_mv.as_int = INVALID_MV;
267*77c1e3ccSAndroid Build Coastguard Worker
268*77c1e3ccSAndroid Build Coastguard Worker // Allow more mesh searches for screen content type on the ARF.
269*77c1e3ccSAndroid Build Coastguard Worker const int fine_search_interval = use_fine_search_interval(cpi);
270*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
271*77c1e3ccSAndroid Build Coastguard Worker
272*77c1e3ccSAndroid Build Coastguard Worker switch (mbmi->motion_mode) {
273*77c1e3ccSAndroid Build Coastguard Worker case SIMPLE_TRANSLATION: {
274*77c1e3ccSAndroid Build Coastguard Worker // Perform a search with the top 2 candidates
275*77c1e3ccSAndroid Build Coastguard Worker int sum_weight = 0;
276*77c1e3ccSAndroid Build Coastguard Worker for (int m = 0; m < cand_cnt; m++) {
277*77c1e3ccSAndroid Build Coastguard Worker int_mv smv = cand[m].fmv;
278*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV this_best_mv, this_second_best_mv;
279*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV_STATS this_mv_stats;
280*77c1e3ccSAndroid Build Coastguard Worker
281*77c1e3ccSAndroid Build Coastguard Worker if (smv.as_int == INVALID_MV) continue;
282*77c1e3ccSAndroid Build Coastguard Worker
283*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_fullpel_ms_params(
284*77c1e3ccSAndroid Build Coastguard Worker &full_ms_params, cpi, x, bsize, &ref_mv, smv.as_fullmv,
285*77c1e3ccSAndroid Build Coastguard Worker src_search_site_cfg, search_method, fine_search_interval);
286*77c1e3ccSAndroid Build Coastguard Worker
287*77c1e3ccSAndroid Build Coastguard Worker const int thissme =
288*77c1e3ccSAndroid Build Coastguard Worker av1_full_pixel_search(smv.as_fullmv, &full_ms_params, step_param,
289*77c1e3ccSAndroid Build Coastguard Worker cond_cost_list(cpi, cost_list), &this_best_mv,
290*77c1e3ccSAndroid Build Coastguard Worker &this_mv_stats, &this_second_best_mv);
291*77c1e3ccSAndroid Build Coastguard Worker
292*77c1e3ccSAndroid Build Coastguard Worker if (thissme < bestsme) {
293*77c1e3ccSAndroid Build Coastguard Worker bestsme = thissme;
294*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_fullmv = this_best_mv;
295*77c1e3ccSAndroid Build Coastguard Worker best_mv_stats = this_mv_stats;
296*77c1e3ccSAndroid Build Coastguard Worker second_best_mv.as_fullmv = this_second_best_mv;
297*77c1e3ccSAndroid Build Coastguard Worker }
298*77c1e3ccSAndroid Build Coastguard Worker
299*77c1e3ccSAndroid Build Coastguard Worker sum_weight += cand[m].weight;
300*77c1e3ccSAndroid Build Coastguard Worker if (4 * sum_weight > 3 * total_weight) break;
301*77c1e3ccSAndroid Build Coastguard Worker }
302*77c1e3ccSAndroid Build Coastguard Worker } break;
303*77c1e3ccSAndroid Build Coastguard Worker case OBMC_CAUSAL:
304*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
305*77c1e3ccSAndroid Build Coastguard Worker &ref_mv, start_mv, src_search_site_cfg,
306*77c1e3ccSAndroid Build Coastguard Worker search_method, fine_search_interval);
307*77c1e3ccSAndroid Build Coastguard Worker
308*77c1e3ccSAndroid Build Coastguard Worker bestsme = av1_obmc_full_pixel_search(start_mv, &full_ms_params,
309*77c1e3ccSAndroid Build Coastguard Worker step_param, &best_mv->as_fullmv);
310*77c1e3ccSAndroid Build Coastguard Worker break;
311*77c1e3ccSAndroid Build Coastguard Worker default: assert(0 && "Invalid motion mode!\n");
312*77c1e3ccSAndroid Build Coastguard Worker }
313*77c1e3ccSAndroid Build Coastguard Worker if (best_mv->as_int == INVALID_MV) return;
314*77c1e3ccSAndroid Build Coastguard Worker
315*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
316*77c1e3ccSAndroid Build Coastguard Worker // Swap back the original buffers for subpel motion search.
317*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_planes; i++) {
318*77c1e3ccSAndroid Build Coastguard Worker xd->plane[i].pre[ref_idx] = backup_yv12[i];
319*77c1e3ccSAndroid Build Coastguard Worker }
320*77c1e3ccSAndroid Build Coastguard Worker }
321*77c1e3ccSAndroid Build Coastguard Worker
322*77c1e3ccSAndroid Build Coastguard Worker // Terminate search with the current ref_idx based on fullpel mv, rate cost,
323*77c1e3ccSAndroid Build Coastguard Worker // and other know cost.
324*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.inter_sf.skip_newmv_in_drl >= 2 &&
325*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode == SIMPLE_TRANSLATION &&
326*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_int != INVALID_MV) {
327*77c1e3ccSAndroid Build Coastguard Worker int_mv this_mv;
328*77c1e3ccSAndroid Build Coastguard Worker this_mv.as_mv = get_mv_from_fullmv(&best_mv->as_fullmv);
329*77c1e3ccSAndroid Build Coastguard Worker const int ref_mv_idx = mbmi->ref_mv_idx;
330*77c1e3ccSAndroid Build Coastguard Worker const int this_mv_rate =
331*77c1e3ccSAndroid Build Coastguard Worker av1_mv_bit_cost(&this_mv.as_mv, &ref_mv, mv_costs->nmv_joint_cost,
332*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
333*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].full_search_mv.as_int = this_mv.as_int;
334*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].full_mv_rate = this_mv_rate;
335*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].full_mv_bestsme = bestsme;
336*77c1e3ccSAndroid Build Coastguard Worker
337*77c1e3ccSAndroid Build Coastguard Worker for (int prev_ref_idx = 0; prev_ref_idx < ref_mv_idx; ++prev_ref_idx) {
338*77c1e3ccSAndroid Build Coastguard Worker // Check if the motion search result same as previous results
339*77c1e3ccSAndroid Build Coastguard Worker if (this_mv.as_int == mode_info[prev_ref_idx].full_search_mv.as_int) {
340*77c1e3ccSAndroid Build Coastguard Worker // Compare the rate cost
341*77c1e3ccSAndroid Build Coastguard Worker const int prev_rate_cost = mode_info[prev_ref_idx].full_mv_rate +
342*77c1e3ccSAndroid Build Coastguard Worker mode_info[prev_ref_idx].drl_cost;
343*77c1e3ccSAndroid Build Coastguard Worker const int this_rate_cost =
344*77c1e3ccSAndroid Build Coastguard Worker this_mv_rate + mode_info[ref_mv_idx].drl_cost;
345*77c1e3ccSAndroid Build Coastguard Worker
346*77c1e3ccSAndroid Build Coastguard Worker if (prev_rate_cost <= this_rate_cost) {
347*77c1e3ccSAndroid Build Coastguard Worker // If the current rate_cost is worse than the previous rate_cost, then
348*77c1e3ccSAndroid Build Coastguard Worker // we terminate the search. Since av1_single_motion_search is only
349*77c1e3ccSAndroid Build Coastguard Worker // called by handle_new_mv in SIMPLE_TRANSLATION mode, we set the
350*77c1e3ccSAndroid Build Coastguard Worker // best_mv to INVALID mv to signal that we wish to terminate search
351*77c1e3ccSAndroid Build Coastguard Worker // for the current mode.
352*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_int = INVALID_MV;
353*77c1e3ccSAndroid Build Coastguard Worker return;
354*77c1e3ccSAndroid Build Coastguard Worker }
355*77c1e3ccSAndroid Build Coastguard Worker }
356*77c1e3ccSAndroid Build Coastguard Worker
357*77c1e3ccSAndroid Build Coastguard Worker // Terminate the evaluation of current ref_mv_idx based on bestsme and
358*77c1e3ccSAndroid Build Coastguard Worker // drl_cost.
359*77c1e3ccSAndroid Build Coastguard Worker const int psme = mode_info[prev_ref_idx].full_mv_bestsme;
360*77c1e3ccSAndroid Build Coastguard Worker if (psme == INT_MAX) continue;
361*77c1e3ccSAndroid Build Coastguard Worker const int thr =
362*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.inter_sf.skip_newmv_in_drl == 3 ? (psme + (psme >> 2)) : psme;
363*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.inter_sf.skip_newmv_in_drl >= 3 &&
364*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].full_mv_bestsme > thr &&
365*77c1e3ccSAndroid Build Coastguard Worker mode_info[prev_ref_idx].drl_cost < mode_info[ref_mv_idx].drl_cost) {
366*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_int = INVALID_MV;
367*77c1e3ccSAndroid Build Coastguard Worker return;
368*77c1e3ccSAndroid Build Coastguard Worker }
369*77c1e3ccSAndroid Build Coastguard Worker }
370*77c1e3ccSAndroid Build Coastguard Worker }
371*77c1e3ccSAndroid Build Coastguard Worker
372*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.features.cur_frame_force_integer_mv) {
373*77c1e3ccSAndroid Build Coastguard Worker convert_fullmv_to_mv(best_mv);
374*77c1e3ccSAndroid Build Coastguard Worker }
375*77c1e3ccSAndroid Build Coastguard Worker
376*77c1e3ccSAndroid Build Coastguard Worker const int use_fractional_mv =
377*77c1e3ccSAndroid Build Coastguard Worker bestsme < INT_MAX && cpi->common.features.cur_frame_force_integer_mv == 0;
378*77c1e3ccSAndroid Build Coastguard Worker int best_mv_rate = 0;
379*77c1e3ccSAndroid Build Coastguard Worker int mv_rate_calculated = 0;
380*77c1e3ccSAndroid Build Coastguard Worker if (use_fractional_mv) {
381*77c1e3ccSAndroid Build Coastguard Worker int_mv fractional_ms_list[3];
382*77c1e3ccSAndroid Build Coastguard Worker av1_set_fractional_mv(fractional_ms_list);
383*77c1e3ccSAndroid Build Coastguard Worker int dis; /* TODO: use dis in distortion calculation later. */
384*77c1e3ccSAndroid Build Coastguard Worker
385*77c1e3ccSAndroid Build Coastguard Worker SUBPEL_MOTION_SEARCH_PARAMS ms_params;
386*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
387*77c1e3ccSAndroid Build Coastguard Worker cost_list);
388*77c1e3ccSAndroid Build Coastguard Worker MV subpel_start_mv = get_mv_from_fullmv(&best_mv->as_fullmv);
389*77c1e3ccSAndroid Build Coastguard Worker assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv));
390*77c1e3ccSAndroid Build Coastguard Worker
391*77c1e3ccSAndroid Build Coastguard Worker switch (mbmi->motion_mode) {
392*77c1e3ccSAndroid Build Coastguard Worker case SIMPLE_TRANSLATION:
393*77c1e3ccSAndroid Build Coastguard Worker if (mv_sf->use_accurate_subpel_search) {
394*77c1e3ccSAndroid Build Coastguard Worker const int try_second = second_best_mv.as_int != INVALID_MV &&
395*77c1e3ccSAndroid Build Coastguard Worker second_best_mv.as_int != best_mv->as_int &&
396*77c1e3ccSAndroid Build Coastguard Worker (mv_sf->disable_second_mv <= 1);
397*77c1e3ccSAndroid Build Coastguard Worker const int best_mv_var = mv_search_params->find_fractional_mv_step(
398*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, &best_mv_stats,
399*77c1e3ccSAndroid Build Coastguard Worker &best_mv->as_mv, &dis, &x->pred_sse[ref], fractional_ms_list);
400*77c1e3ccSAndroid Build Coastguard Worker
401*77c1e3ccSAndroid Build Coastguard Worker if (try_second) {
402*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *p = xd->plane;
403*77c1e3ccSAndroid Build Coastguard Worker const BUFFER_SET orig_dst = {
404*77c1e3ccSAndroid Build Coastguard Worker { p[0].dst.buf, p[1].dst.buf, p[2].dst.buf },
405*77c1e3ccSAndroid Build Coastguard Worker { p[0].dst.stride, p[1].dst.stride, p[2].dst.stride },
406*77c1e3ccSAndroid Build Coastguard Worker };
407*77c1e3ccSAndroid Build Coastguard Worker int64_t rd = INT64_MAX;
408*77c1e3ccSAndroid Build Coastguard Worker if (!mv_sf->disable_second_mv) {
409*77c1e3ccSAndroid Build Coastguard Worker // Calculate actual rd cost.
410*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[0].as_mv = best_mv->as_mv;
411*77c1e3ccSAndroid Build Coastguard Worker av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
412*77c1e3ccSAndroid Build Coastguard Worker bsize, 0, 0);
413*77c1e3ccSAndroid Build Coastguard Worker av1_subtract_plane(x, bsize, 0);
414*77c1e3ccSAndroid Build Coastguard Worker RD_STATS this_rd_stats;
415*77c1e3ccSAndroid Build Coastguard Worker av1_init_rd_stats(&this_rd_stats);
416*77c1e3ccSAndroid Build Coastguard Worker av1_estimate_txfm_yrd(cpi, x, &this_rd_stats, INT64_MAX, bsize,
417*77c1e3ccSAndroid Build Coastguard Worker max_txsize_rect_lookup[bsize]);
418*77c1e3ccSAndroid Build Coastguard Worker int this_mv_rate = av1_mv_bit_cost(
419*77c1e3ccSAndroid Build Coastguard Worker &best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
420*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
421*77c1e3ccSAndroid Build Coastguard Worker rd = RDCOST(x->rdmult, this_mv_rate + this_rd_stats.rate,
422*77c1e3ccSAndroid Build Coastguard Worker this_rd_stats.dist);
423*77c1e3ccSAndroid Build Coastguard Worker }
424*77c1e3ccSAndroid Build Coastguard Worker
425*77c1e3ccSAndroid Build Coastguard Worker MV this_best_mv;
426*77c1e3ccSAndroid Build Coastguard Worker subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
427*77c1e3ccSAndroid Build Coastguard Worker if (av1_is_subpelmv_in_range(&ms_params.mv_limits,
428*77c1e3ccSAndroid Build Coastguard Worker subpel_start_mv)) {
429*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
430*77c1e3ccSAndroid Build Coastguard Worker const int this_var = mv_search_params->find_fractional_mv_step(
431*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, NULL, &this_best_mv,
432*77c1e3ccSAndroid Build Coastguard Worker &dis, &sse, fractional_ms_list);
433*77c1e3ccSAndroid Build Coastguard Worker
434*77c1e3ccSAndroid Build Coastguard Worker if (!mv_sf->disable_second_mv) {
435*77c1e3ccSAndroid Build Coastguard Worker // If cpi->sf.mv_sf.disable_second_mv is 0, use actual rd cost
436*77c1e3ccSAndroid Build Coastguard Worker // to choose the better MV.
437*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[0].as_mv = this_best_mv;
438*77c1e3ccSAndroid Build Coastguard Worker av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
439*77c1e3ccSAndroid Build Coastguard Worker bsize, 0, 0);
440*77c1e3ccSAndroid Build Coastguard Worker av1_subtract_plane(x, bsize, 0);
441*77c1e3ccSAndroid Build Coastguard Worker RD_STATS tmp_rd_stats;
442*77c1e3ccSAndroid Build Coastguard Worker av1_init_rd_stats(&tmp_rd_stats);
443*77c1e3ccSAndroid Build Coastguard Worker av1_estimate_txfm_yrd(cpi, x, &tmp_rd_stats, INT64_MAX, bsize,
444*77c1e3ccSAndroid Build Coastguard Worker max_txsize_rect_lookup[bsize]);
445*77c1e3ccSAndroid Build Coastguard Worker int tmp_mv_rate = av1_mv_bit_cost(
446*77c1e3ccSAndroid Build Coastguard Worker &this_best_mv, &ref_mv, mv_costs->nmv_joint_cost,
447*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
448*77c1e3ccSAndroid Build Coastguard Worker int64_t tmp_rd =
449*77c1e3ccSAndroid Build Coastguard Worker RDCOST(x->rdmult, tmp_rd_stats.rate + tmp_mv_rate,
450*77c1e3ccSAndroid Build Coastguard Worker tmp_rd_stats.dist);
451*77c1e3ccSAndroid Build Coastguard Worker if (tmp_rd < rd) {
452*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_mv = this_best_mv;
453*77c1e3ccSAndroid Build Coastguard Worker x->pred_sse[ref] = sse;
454*77c1e3ccSAndroid Build Coastguard Worker }
455*77c1e3ccSAndroid Build Coastguard Worker } else {
456*77c1e3ccSAndroid Build Coastguard Worker // If cpi->sf.mv_sf.disable_second_mv = 1, use var to decide the
457*77c1e3ccSAndroid Build Coastguard Worker // best MV.
458*77c1e3ccSAndroid Build Coastguard Worker if (this_var < best_mv_var) {
459*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_mv = this_best_mv;
460*77c1e3ccSAndroid Build Coastguard Worker x->pred_sse[ref] = sse;
461*77c1e3ccSAndroid Build Coastguard Worker }
462*77c1e3ccSAndroid Build Coastguard Worker }
463*77c1e3ccSAndroid Build Coastguard Worker }
464*77c1e3ccSAndroid Build Coastguard Worker }
465*77c1e3ccSAndroid Build Coastguard Worker } else {
466*77c1e3ccSAndroid Build Coastguard Worker mv_search_params->find_fractional_mv_step(
467*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, &best_mv_stats,
468*77c1e3ccSAndroid Build Coastguard Worker &best_mv->as_mv, &dis, &x->pred_sse[ref], NULL);
469*77c1e3ccSAndroid Build Coastguard Worker }
470*77c1e3ccSAndroid Build Coastguard Worker break;
471*77c1e3ccSAndroid Build Coastguard Worker case OBMC_CAUSAL:
472*77c1e3ccSAndroid Build Coastguard Worker av1_find_best_obmc_sub_pixel_tree_up(
473*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, NULL, &best_mv->as_mv, &dis,
474*77c1e3ccSAndroid Build Coastguard Worker &x->pred_sse[ref], NULL);
475*77c1e3ccSAndroid Build Coastguard Worker break;
476*77c1e3ccSAndroid Build Coastguard Worker default: assert(0 && "Invalid motion mode!\n");
477*77c1e3ccSAndroid Build Coastguard Worker }
478*77c1e3ccSAndroid Build Coastguard Worker
479*77c1e3ccSAndroid Build Coastguard Worker // Terminate search with the current ref_idx based on subpel mv and rate
480*77c1e3ccSAndroid Build Coastguard Worker // cost.
481*77c1e3ccSAndroid Build Coastguard Worker if (cpi->sf.inter_sf.skip_newmv_in_drl >= 1 && args != NULL &&
482*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode == SIMPLE_TRANSLATION &&
483*77c1e3ccSAndroid Build Coastguard Worker best_mv->as_int != INVALID_MV) {
484*77c1e3ccSAndroid Build Coastguard Worker const int ref_mv_idx = mbmi->ref_mv_idx;
485*77c1e3ccSAndroid Build Coastguard Worker best_mv_rate =
486*77c1e3ccSAndroid Build Coastguard Worker av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
487*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
488*77c1e3ccSAndroid Build Coastguard Worker mv_rate_calculated = 1;
489*77c1e3ccSAndroid Build Coastguard Worker
490*77c1e3ccSAndroid Build Coastguard Worker for (int prev_ref_idx = 0; prev_ref_idx < ref_mv_idx; ++prev_ref_idx) {
491*77c1e3ccSAndroid Build Coastguard Worker if (!args->single_newmv_valid[prev_ref_idx][ref]) continue;
492*77c1e3ccSAndroid Build Coastguard Worker // Check if the motion vectors are the same.
493*77c1e3ccSAndroid Build Coastguard Worker if (best_mv->as_int == args->single_newmv[prev_ref_idx][ref].as_int) {
494*77c1e3ccSAndroid Build Coastguard Worker // Skip this evaluation if the previous one is skipped.
495*77c1e3ccSAndroid Build Coastguard Worker if (mode_info[prev_ref_idx].skip) {
496*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].skip = 1;
497*77c1e3ccSAndroid Build Coastguard Worker break;
498*77c1e3ccSAndroid Build Coastguard Worker }
499*77c1e3ccSAndroid Build Coastguard Worker // Compare the rate cost that we current know.
500*77c1e3ccSAndroid Build Coastguard Worker const int prev_rate_cost =
501*77c1e3ccSAndroid Build Coastguard Worker args->single_newmv_rate[prev_ref_idx][ref] +
502*77c1e3ccSAndroid Build Coastguard Worker mode_info[prev_ref_idx].drl_cost;
503*77c1e3ccSAndroid Build Coastguard Worker const int this_rate_cost =
504*77c1e3ccSAndroid Build Coastguard Worker best_mv_rate + mode_info[ref_mv_idx].drl_cost;
505*77c1e3ccSAndroid Build Coastguard Worker
506*77c1e3ccSAndroid Build Coastguard Worker if (prev_rate_cost <= this_rate_cost) {
507*77c1e3ccSAndroid Build Coastguard Worker // If the current rate_cost is worse than the previous rate_cost,
508*77c1e3ccSAndroid Build Coastguard Worker // then we terminate the search for this ref_mv_idx.
509*77c1e3ccSAndroid Build Coastguard Worker mode_info[ref_mv_idx].skip = 1;
510*77c1e3ccSAndroid Build Coastguard Worker break;
511*77c1e3ccSAndroid Build Coastguard Worker }
512*77c1e3ccSAndroid Build Coastguard Worker }
513*77c1e3ccSAndroid Build Coastguard Worker }
514*77c1e3ccSAndroid Build Coastguard Worker }
515*77c1e3ccSAndroid Build Coastguard Worker }
516*77c1e3ccSAndroid Build Coastguard Worker
517*77c1e3ccSAndroid Build Coastguard Worker if (mv_rate_calculated) {
518*77c1e3ccSAndroid Build Coastguard Worker *rate_mv = best_mv_rate;
519*77c1e3ccSAndroid Build Coastguard Worker } else {
520*77c1e3ccSAndroid Build Coastguard Worker *rate_mv =
521*77c1e3ccSAndroid Build Coastguard Worker av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
522*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
523*77c1e3ccSAndroid Build Coastguard Worker }
524*77c1e3ccSAndroid Build Coastguard Worker }
525*77c1e3ccSAndroid Build Coastguard Worker
av1_joint_motion_search(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int_mv * cur_mv,const uint8_t * mask,int mask_stride,int * rate_mv,int allow_second_mv,int joint_me_num_refine_iter)526*77c1e3ccSAndroid Build Coastguard Worker int av1_joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
527*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, int_mv *cur_mv,
528*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *mask, int mask_stride, int *rate_mv,
529*77c1e3ccSAndroid Build Coastguard Worker int allow_second_mv, int joint_me_num_refine_iter) {
530*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
531*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
532*77c1e3ccSAndroid Build Coastguard Worker const int pw = block_size_wide[bsize];
533*77c1e3ccSAndroid Build Coastguard Worker const int ph = block_size_high[bsize];
534*77c1e3ccSAndroid Build Coastguard Worker const int plane = 0;
535*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
536*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
537*77c1e3ccSAndroid Build Coastguard Worker // This function should only ever be called for compound modes
538*77c1e3ccSAndroid Build Coastguard Worker assert(has_second_ref(mbmi));
539*77c1e3ccSAndroid Build Coastguard Worker const int_mv init_mv[2] = { cur_mv[0], cur_mv[1] };
540*77c1e3ccSAndroid Build Coastguard Worker const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
541*77c1e3ccSAndroid Build Coastguard Worker const MvCosts *mv_costs = x->mv_costs;
542*77c1e3ccSAndroid Build Coastguard Worker int_mv ref_mv[2];
543*77c1e3ccSAndroid Build Coastguard Worker int ite, ref;
544*77c1e3ccSAndroid Build Coastguard Worker
545*77c1e3ccSAndroid Build Coastguard Worker // Get the prediction block from the 'other' reference frame.
546*77c1e3ccSAndroid Build Coastguard Worker const int_interpfilters interp_filters =
547*77c1e3ccSAndroid Build Coastguard Worker av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
548*77c1e3ccSAndroid Build Coastguard Worker
549*77c1e3ccSAndroid Build Coastguard Worker InterPredParams inter_pred_params;
550*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
551*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
552*77c1e3ccSAndroid Build Coastguard Worker
553*77c1e3ccSAndroid Build Coastguard Worker // Do joint motion search in compound mode to get more accurate mv.
554*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d backup_yv12[2][MAX_MB_PLANE];
555*77c1e3ccSAndroid Build Coastguard Worker int last_besterr[2] = { INT_MAX, INT_MAX };
556*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
557*77c1e3ccSAndroid Build Coastguard Worker av1_get_scaled_ref_frame(cpi, refs[0]),
558*77c1e3ccSAndroid Build Coastguard Worker av1_get_scaled_ref_frame(cpi, refs[1])
559*77c1e3ccSAndroid Build Coastguard Worker };
560*77c1e3ccSAndroid Build Coastguard Worker
561*77c1e3ccSAndroid Build Coastguard Worker // Prediction buffer from second frame.
562*77c1e3ccSAndroid Build Coastguard Worker DECLARE_ALIGNED(16, uint8_t, second_pred16[MAX_SB_SQUARE * sizeof(uint16_t)]);
563*77c1e3ccSAndroid Build Coastguard Worker uint8_t *second_pred = get_buf_by_bd(xd, second_pred16);
564*77c1e3ccSAndroid Build Coastguard Worker
565*77c1e3ccSAndroid Build Coastguard Worker int_mv best_mv, second_best_mv;
566*77c1e3ccSAndroid Build Coastguard Worker
567*77c1e3ccSAndroid Build Coastguard Worker // Allow joint search multiple times iteratively for each reference frame
568*77c1e3ccSAndroid Build Coastguard Worker // and break out of the search loop if it couldn't find a better mv.
569*77c1e3ccSAndroid Build Coastguard Worker for (ite = 0; ite < (2 * joint_me_num_refine_iter); ite++) {
570*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d ref_yv12[2];
571*77c1e3ccSAndroid Build Coastguard Worker int bestsme = INT_MAX;
572*77c1e3ccSAndroid Build Coastguard Worker int id = ite % 2; // Even iterations search in the first reference frame,
573*77c1e3ccSAndroid Build Coastguard Worker // odd iterations search in the second. The predictor
574*77c1e3ccSAndroid Build Coastguard Worker // found for the 'other' reference frame is factored in.
575*77c1e3ccSAndroid Build Coastguard Worker if (ite >= 2 && cur_mv[!id].as_int == init_mv[!id].as_int) {
576*77c1e3ccSAndroid Build Coastguard Worker if (cur_mv[id].as_int == init_mv[id].as_int) {
577*77c1e3ccSAndroid Build Coastguard Worker break;
578*77c1e3ccSAndroid Build Coastguard Worker } else {
579*77c1e3ccSAndroid Build Coastguard Worker int_mv cur_int_mv, init_int_mv;
580*77c1e3ccSAndroid Build Coastguard Worker cur_int_mv.as_mv.col = cur_mv[id].as_mv.col >> 3;
581*77c1e3ccSAndroid Build Coastguard Worker cur_int_mv.as_mv.row = cur_mv[id].as_mv.row >> 3;
582*77c1e3ccSAndroid Build Coastguard Worker init_int_mv.as_mv.row = init_mv[id].as_mv.row >> 3;
583*77c1e3ccSAndroid Build Coastguard Worker init_int_mv.as_mv.col = init_mv[id].as_mv.col >> 3;
584*77c1e3ccSAndroid Build Coastguard Worker if (cur_int_mv.as_int == init_int_mv.as_int) {
585*77c1e3ccSAndroid Build Coastguard Worker break;
586*77c1e3ccSAndroid Build Coastguard Worker }
587*77c1e3ccSAndroid Build Coastguard Worker }
588*77c1e3ccSAndroid Build Coastguard Worker }
589*77c1e3ccSAndroid Build Coastguard Worker for (ref = 0; ref < 2; ++ref) {
590*77c1e3ccSAndroid Build Coastguard Worker ref_mv[ref] = av1_get_ref_mv(x, ref);
591*77c1e3ccSAndroid Build Coastguard Worker // Swap out the reference frame for a version that's been scaled to
592*77c1e3ccSAndroid Build Coastguard Worker // match the resolution of the current frame, allowing the existing
593*77c1e3ccSAndroid Build Coastguard Worker // motion search code to be used without additional modifications.
594*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame[ref]) {
595*77c1e3ccSAndroid Build Coastguard Worker int i;
596*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < num_planes; i++)
597*77c1e3ccSAndroid Build Coastguard Worker backup_yv12[ref][i] = xd->plane[i].pre[ref];
598*77c1e3ccSAndroid Build Coastguard Worker av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
599*77c1e3ccSAndroid Build Coastguard Worker NULL, num_planes);
600*77c1e3ccSAndroid Build Coastguard Worker }
601*77c1e3ccSAndroid Build Coastguard Worker }
602*77c1e3ccSAndroid Build Coastguard Worker
603*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(scaled_ref_frame[0] != NULL,
604*77c1e3ccSAndroid Build Coastguard Worker cm->width == scaled_ref_frame[0]->y_crop_width &&
605*77c1e3ccSAndroid Build Coastguard Worker cm->height == scaled_ref_frame[0]->y_crop_height));
606*77c1e3ccSAndroid Build Coastguard Worker assert(IMPLIES(scaled_ref_frame[1] != NULL,
607*77c1e3ccSAndroid Build Coastguard Worker cm->width == scaled_ref_frame[1]->y_crop_width &&
608*77c1e3ccSAndroid Build Coastguard Worker cm->height == scaled_ref_frame[1]->y_crop_height));
609*77c1e3ccSAndroid Build Coastguard Worker
610*77c1e3ccSAndroid Build Coastguard Worker // Initialize based on (possibly scaled) prediction buffers.
611*77c1e3ccSAndroid Build Coastguard Worker ref_yv12[0] = xd->plane[plane].pre[0];
612*77c1e3ccSAndroid Build Coastguard Worker ref_yv12[1] = xd->plane[plane].pre[1];
613*77c1e3ccSAndroid Build Coastguard Worker
614*77c1e3ccSAndroid Build Coastguard Worker av1_init_inter_params(&inter_pred_params, pw, ph, mi_row * MI_SIZE,
615*77c1e3ccSAndroid Build Coastguard Worker mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
616*77c1e3ccSAndroid Build Coastguard Worker &cm->sf_identity, &ref_yv12[!id], interp_filters);
617*77c1e3ccSAndroid Build Coastguard Worker inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
618*77c1e3ccSAndroid Build Coastguard Worker
619*77c1e3ccSAndroid Build Coastguard Worker // Since we have scaled the reference frames to match the size of the
620*77c1e3ccSAndroid Build Coastguard Worker // current frame we must use a unit scaling factor during mode selection.
621*77c1e3ccSAndroid Build Coastguard Worker av1_enc_build_one_inter_predictor(second_pred, pw, &cur_mv[!id].as_mv,
622*77c1e3ccSAndroid Build Coastguard Worker &inter_pred_params);
623*77c1e3ccSAndroid Build Coastguard Worker
624*77c1e3ccSAndroid Build Coastguard Worker // Do full-pixel compound motion search on the current reference frame.
625*77c1e3ccSAndroid Build Coastguard Worker if (id) xd->plane[plane].pre[0] = ref_yv12[id];
626*77c1e3ccSAndroid Build Coastguard Worker
627*77c1e3ccSAndroid Build Coastguard Worker // Make motion search params
628*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
629*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV_STATS best_mv_stats;
630*77c1e3ccSAndroid Build Coastguard Worker const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
631*77c1e3ccSAndroid Build Coastguard Worker const SEARCH_METHODS search_method =
632*77c1e3ccSAndroid Build Coastguard Worker av1_get_default_mv_search_method(x, mv_sf, bsize);
633*77c1e3ccSAndroid Build Coastguard Worker const search_site_config *src_search_sites =
634*77c1e3ccSAndroid Build Coastguard Worker av1_get_search_site_config(cpi, x, search_method);
635*77c1e3ccSAndroid Build Coastguard Worker // Use the mv result from the single mode as mv predictor.
636*77c1e3ccSAndroid Build Coastguard Worker const FULLPEL_MV start_fullmv = get_fullmv_from_mv(&cur_mv[id].as_mv);
637*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
638*77c1e3ccSAndroid Build Coastguard Worker &ref_mv[id].as_mv, start_fullmv,
639*77c1e3ccSAndroid Build Coastguard Worker src_search_sites, search_method,
640*77c1e3ccSAndroid Build Coastguard Worker /*fine_search_interval=*/0);
641*77c1e3ccSAndroid Build Coastguard Worker
642*77c1e3ccSAndroid Build Coastguard Worker av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
643*77c1e3ccSAndroid Build Coastguard Worker mask_stride, id);
644*77c1e3ccSAndroid Build Coastguard Worker
645*77c1e3ccSAndroid Build Coastguard Worker // Small-range full-pixel motion search.
646*77c1e3ccSAndroid Build Coastguard Worker if (!mv_sf->disable_extensive_joint_motion_search &&
647*77c1e3ccSAndroid Build Coastguard Worker mbmi->interinter_comp.type != COMPOUND_WEDGE) {
648*77c1e3ccSAndroid Build Coastguard Worker bestsme = av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
649*77c1e3ccSAndroid Build Coastguard Worker &best_mv.as_fullmv, &best_mv_stats,
650*77c1e3ccSAndroid Build Coastguard Worker &second_best_mv.as_fullmv);
651*77c1e3ccSAndroid Build Coastguard Worker } else {
652*77c1e3ccSAndroid Build Coastguard Worker bestsme = av1_refining_search_8p_c(&full_ms_params, start_fullmv,
653*77c1e3ccSAndroid Build Coastguard Worker &best_mv.as_fullmv);
654*77c1e3ccSAndroid Build Coastguard Worker second_best_mv = best_mv;
655*77c1e3ccSAndroid Build Coastguard Worker }
656*77c1e3ccSAndroid Build Coastguard Worker
657*77c1e3ccSAndroid Build Coastguard Worker const int try_second = second_best_mv.as_int != INVALID_MV &&
658*77c1e3ccSAndroid Build Coastguard Worker second_best_mv.as_int != best_mv.as_int &&
659*77c1e3ccSAndroid Build Coastguard Worker allow_second_mv;
660*77c1e3ccSAndroid Build Coastguard Worker
661*77c1e3ccSAndroid Build Coastguard Worker // Restore the pointer to the first (possibly scaled) prediction buffer.
662*77c1e3ccSAndroid Build Coastguard Worker if (id) xd->plane[plane].pre[0] = ref_yv12[0];
663*77c1e3ccSAndroid Build Coastguard Worker
664*77c1e3ccSAndroid Build Coastguard Worker for (ref = 0; ref < 2; ++ref) {
665*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame[ref]) {
666*77c1e3ccSAndroid Build Coastguard Worker // Swap back the original buffers for subpel motion search.
667*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_planes; i++) {
668*77c1e3ccSAndroid Build Coastguard Worker xd->plane[i].pre[ref] = backup_yv12[ref][i];
669*77c1e3ccSAndroid Build Coastguard Worker }
670*77c1e3ccSAndroid Build Coastguard Worker // Re-initialize based on unscaled prediction buffers.
671*77c1e3ccSAndroid Build Coastguard Worker ref_yv12[ref] = xd->plane[plane].pre[ref];
672*77c1e3ccSAndroid Build Coastguard Worker }
673*77c1e3ccSAndroid Build Coastguard Worker }
674*77c1e3ccSAndroid Build Coastguard Worker
675*77c1e3ccSAndroid Build Coastguard Worker // Do sub-pixel compound motion search on the current reference frame.
676*77c1e3ccSAndroid Build Coastguard Worker if (id) xd->plane[plane].pre[0] = ref_yv12[id];
677*77c1e3ccSAndroid Build Coastguard Worker
678*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.features.cur_frame_force_integer_mv) {
679*77c1e3ccSAndroid Build Coastguard Worker convert_fullmv_to_mv(&best_mv);
680*77c1e3ccSAndroid Build Coastguard Worker }
681*77c1e3ccSAndroid Build Coastguard Worker if (bestsme < INT_MAX &&
682*77c1e3ccSAndroid Build Coastguard Worker cpi->common.features.cur_frame_force_integer_mv == 0) {
683*77c1e3ccSAndroid Build Coastguard Worker int dis; /* TODO: use dis in distortion calculation later. */
684*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
685*77c1e3ccSAndroid Build Coastguard Worker SUBPEL_MOTION_SEARCH_PARAMS ms_params;
686*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize,
687*77c1e3ccSAndroid Build Coastguard Worker &ref_mv[id].as_mv, NULL);
688*77c1e3ccSAndroid Build Coastguard Worker av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
689*77c1e3ccSAndroid Build Coastguard Worker mask, mask_stride, id);
690*77c1e3ccSAndroid Build Coastguard Worker ms_params.forced_stop = EIGHTH_PEL;
691*77c1e3ccSAndroid Build Coastguard Worker MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
692*77c1e3ccSAndroid Build Coastguard Worker assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
693*77c1e3ccSAndroid Build Coastguard Worker bestsme = cpi->mv_search_params.find_fractional_mv_step(
694*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, start_mv, NULL, &best_mv.as_mv, &dis, &sse, NULL);
695*77c1e3ccSAndroid Build Coastguard Worker
696*77c1e3ccSAndroid Build Coastguard Worker if (try_second) {
697*77c1e3ccSAndroid Build Coastguard Worker MV this_best_mv;
698*77c1e3ccSAndroid Build Coastguard Worker MV subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
699*77c1e3ccSAndroid Build Coastguard Worker if (av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv)) {
700*77c1e3ccSAndroid Build Coastguard Worker const int thissme = cpi->mv_search_params.find_fractional_mv_step(
701*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, NULL, &this_best_mv, &dis,
702*77c1e3ccSAndroid Build Coastguard Worker &sse, NULL);
703*77c1e3ccSAndroid Build Coastguard Worker if (thissme < bestsme) {
704*77c1e3ccSAndroid Build Coastguard Worker best_mv.as_mv = this_best_mv;
705*77c1e3ccSAndroid Build Coastguard Worker bestsme = thissme;
706*77c1e3ccSAndroid Build Coastguard Worker }
707*77c1e3ccSAndroid Build Coastguard Worker }
708*77c1e3ccSAndroid Build Coastguard Worker }
709*77c1e3ccSAndroid Build Coastguard Worker }
710*77c1e3ccSAndroid Build Coastguard Worker
711*77c1e3ccSAndroid Build Coastguard Worker // Restore the pointer to the first prediction buffer.
712*77c1e3ccSAndroid Build Coastguard Worker if (id) xd->plane[plane].pre[0] = ref_yv12[0];
713*77c1e3ccSAndroid Build Coastguard Worker if (bestsme < last_besterr[id]) {
714*77c1e3ccSAndroid Build Coastguard Worker cur_mv[id] = best_mv;
715*77c1e3ccSAndroid Build Coastguard Worker last_besterr[id] = bestsme;
716*77c1e3ccSAndroid Build Coastguard Worker } else {
717*77c1e3ccSAndroid Build Coastguard Worker break;
718*77c1e3ccSAndroid Build Coastguard Worker }
719*77c1e3ccSAndroid Build Coastguard Worker }
720*77c1e3ccSAndroid Build Coastguard Worker
721*77c1e3ccSAndroid Build Coastguard Worker *rate_mv = 0;
722*77c1e3ccSAndroid Build Coastguard Worker
723*77c1e3ccSAndroid Build Coastguard Worker for (ref = 0; ref < 2; ++ref) {
724*77c1e3ccSAndroid Build Coastguard Worker const int_mv curr_ref_mv = av1_get_ref_mv(x, ref);
725*77c1e3ccSAndroid Build Coastguard Worker *rate_mv += av1_mv_bit_cost(&cur_mv[ref].as_mv, &curr_ref_mv.as_mv,
726*77c1e3ccSAndroid Build Coastguard Worker mv_costs->nmv_joint_cost,
727*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
728*77c1e3ccSAndroid Build Coastguard Worker }
729*77c1e3ccSAndroid Build Coastguard Worker
730*77c1e3ccSAndroid Build Coastguard Worker return AOMMIN(last_besterr[0], last_besterr[1]);
731*77c1e3ccSAndroid Build Coastguard Worker }
732*77c1e3ccSAndroid Build Coastguard Worker
733*77c1e3ccSAndroid Build Coastguard Worker // Search for the best mv for one component of a compound,
734*77c1e3ccSAndroid Build Coastguard Worker // given that the other component is fixed.
av1_compound_single_motion_search(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,MV * this_mv,const uint8_t * second_pred,const uint8_t * mask,int mask_stride,int * rate_mv,int ref_idx)735*77c1e3ccSAndroid Build Coastguard Worker int av1_compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
736*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, MV *this_mv,
737*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *second_pred,
738*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *mask, int mask_stride,
739*77c1e3ccSAndroid Build Coastguard Worker int *rate_mv, int ref_idx) {
740*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
741*77c1e3ccSAndroid Build Coastguard Worker const int num_planes = av1_num_planes(cm);
742*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
743*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
744*77c1e3ccSAndroid Build Coastguard Worker const int ref = mbmi->ref_frame[ref_idx];
745*77c1e3ccSAndroid Build Coastguard Worker const int_mv ref_mv = av1_get_ref_mv(x, ref_idx);
746*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[0];
747*77c1e3ccSAndroid Build Coastguard Worker const MvCosts *mv_costs = x->mv_costs;
748*77c1e3ccSAndroid Build Coastguard Worker
749*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d backup_yv12[MAX_MB_PLANE];
750*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *const scaled_ref_frame =
751*77c1e3ccSAndroid Build Coastguard Worker av1_get_scaled_ref_frame(cpi, ref);
752*77c1e3ccSAndroid Build Coastguard Worker
753*77c1e3ccSAndroid Build Coastguard Worker // Check that this is either an interinter or an interintra block
754*77c1e3ccSAndroid Build Coastguard Worker assert(has_second_ref(mbmi) || (ref_idx == 0 && is_interintra_mode(mbmi)));
755*77c1e3ccSAndroid Build Coastguard Worker
756*77c1e3ccSAndroid Build Coastguard Worker // Store the first prediction buffer.
757*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d orig_yv12;
758*77c1e3ccSAndroid Build Coastguard Worker if (ref_idx) {
759*77c1e3ccSAndroid Build Coastguard Worker orig_yv12 = pd->pre[0];
760*77c1e3ccSAndroid Build Coastguard Worker pd->pre[0] = pd->pre[ref_idx];
761*77c1e3ccSAndroid Build Coastguard Worker }
762*77c1e3ccSAndroid Build Coastguard Worker
763*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
764*77c1e3ccSAndroid Build Coastguard Worker // Swap out the reference frame for a version that's been scaled to
765*77c1e3ccSAndroid Build Coastguard Worker // match the resolution of the current frame, allowing the existing
766*77c1e3ccSAndroid Build Coastguard Worker // full-pixel motion search code to be used without additional
767*77c1e3ccSAndroid Build Coastguard Worker // modifications.
768*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_planes; i++) {
769*77c1e3ccSAndroid Build Coastguard Worker backup_yv12[i] = xd->plane[i].pre[ref_idx];
770*77c1e3ccSAndroid Build Coastguard Worker }
771*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
772*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
773*77c1e3ccSAndroid Build Coastguard Worker // The index below needs to be 0 instead of ref_idx since we assume the
774*77c1e3ccSAndroid Build Coastguard Worker // 0th slot to be used for subsequent searches. Note that the ref_idx
775*77c1e3ccSAndroid Build Coastguard Worker // reference buffer has been copied to the 0th slot in the code above.
776*77c1e3ccSAndroid Build Coastguard Worker // Now we need to swap the reference frame for the 0th slot.
777*77c1e3ccSAndroid Build Coastguard Worker av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
778*77c1e3ccSAndroid Build Coastguard Worker num_planes);
779*77c1e3ccSAndroid Build Coastguard Worker }
780*77c1e3ccSAndroid Build Coastguard Worker
781*77c1e3ccSAndroid Build Coastguard Worker int bestsme = INT_MAX;
782*77c1e3ccSAndroid Build Coastguard Worker int_mv best_mv;
783*77c1e3ccSAndroid Build Coastguard Worker
784*77c1e3ccSAndroid Build Coastguard Worker // Make motion search params
785*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
786*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV_STATS best_mv_stats;
787*77c1e3ccSAndroid Build Coastguard Worker const SEARCH_METHODS search_method =
788*77c1e3ccSAndroid Build Coastguard Worker av1_get_default_mv_search_method(x, &cpi->sf.mv_sf, bsize);
789*77c1e3ccSAndroid Build Coastguard Worker const search_site_config *src_search_sites =
790*77c1e3ccSAndroid Build Coastguard Worker av1_get_search_site_config(cpi, x, search_method);
791*77c1e3ccSAndroid Build Coastguard Worker // Use the mv result from the single mode as mv predictor.
792*77c1e3ccSAndroid Build Coastguard Worker const FULLPEL_MV start_fullmv = get_fullmv_from_mv(this_mv);
793*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
794*77c1e3ccSAndroid Build Coastguard Worker &ref_mv.as_mv, start_fullmv,
795*77c1e3ccSAndroid Build Coastguard Worker src_search_sites, search_method,
796*77c1e3ccSAndroid Build Coastguard Worker /*fine_search_interval=*/0);
797*77c1e3ccSAndroid Build Coastguard Worker
798*77c1e3ccSAndroid Build Coastguard Worker av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
799*77c1e3ccSAndroid Build Coastguard Worker mask_stride, ref_idx);
800*77c1e3ccSAndroid Build Coastguard Worker
801*77c1e3ccSAndroid Build Coastguard Worker // Small-range full-pixel motion search.
802*77c1e3ccSAndroid Build Coastguard Worker bestsme = av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
803*77c1e3ccSAndroid Build Coastguard Worker &best_mv.as_fullmv, &best_mv_stats, NULL);
804*77c1e3ccSAndroid Build Coastguard Worker
805*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
806*77c1e3ccSAndroid Build Coastguard Worker // Swap back the original buffers for subpel motion search for the 0th slot.
807*77c1e3ccSAndroid Build Coastguard Worker for (int i = 0; i < num_planes; i++) {
808*77c1e3ccSAndroid Build Coastguard Worker xd->plane[i].pre[0] = backup_yv12[i];
809*77c1e3ccSAndroid Build Coastguard Worker }
810*77c1e3ccSAndroid Build Coastguard Worker }
811*77c1e3ccSAndroid Build Coastguard Worker
812*77c1e3ccSAndroid Build Coastguard Worker if (cpi->common.features.cur_frame_force_integer_mv) {
813*77c1e3ccSAndroid Build Coastguard Worker convert_fullmv_to_mv(&best_mv);
814*77c1e3ccSAndroid Build Coastguard Worker }
815*77c1e3ccSAndroid Build Coastguard Worker const int use_fractional_mv =
816*77c1e3ccSAndroid Build Coastguard Worker bestsme < INT_MAX && cpi->common.features.cur_frame_force_integer_mv == 0;
817*77c1e3ccSAndroid Build Coastguard Worker if (use_fractional_mv) {
818*77c1e3ccSAndroid Build Coastguard Worker int dis; /* TODO: use dis in distortion calculation later. */
819*77c1e3ccSAndroid Build Coastguard Worker unsigned int sse;
820*77c1e3ccSAndroid Build Coastguard Worker SUBPEL_MOTION_SEARCH_PARAMS ms_params;
821*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv.as_mv,
822*77c1e3ccSAndroid Build Coastguard Worker NULL);
823*77c1e3ccSAndroid Build Coastguard Worker av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
824*77c1e3ccSAndroid Build Coastguard Worker mask, mask_stride, ref_idx);
825*77c1e3ccSAndroid Build Coastguard Worker ms_params.forced_stop = EIGHTH_PEL;
826*77c1e3ccSAndroid Build Coastguard Worker MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
827*77c1e3ccSAndroid Build Coastguard Worker assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
828*77c1e3ccSAndroid Build Coastguard Worker bestsme = cpi->mv_search_params.find_fractional_mv_step(
829*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, start_mv, &best_mv_stats, &best_mv.as_mv, &dis,
830*77c1e3ccSAndroid Build Coastguard Worker &sse, NULL);
831*77c1e3ccSAndroid Build Coastguard Worker }
832*77c1e3ccSAndroid Build Coastguard Worker
833*77c1e3ccSAndroid Build Coastguard Worker // Restore the pointer to the first unscaled prediction buffer.
834*77c1e3ccSAndroid Build Coastguard Worker if (ref_idx) pd->pre[0] = orig_yv12;
835*77c1e3ccSAndroid Build Coastguard Worker
836*77c1e3ccSAndroid Build Coastguard Worker if (bestsme < INT_MAX) *this_mv = best_mv.as_mv;
837*77c1e3ccSAndroid Build Coastguard Worker
838*77c1e3ccSAndroid Build Coastguard Worker *rate_mv = 0;
839*77c1e3ccSAndroid Build Coastguard Worker
840*77c1e3ccSAndroid Build Coastguard Worker *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, mv_costs->nmv_joint_cost,
841*77c1e3ccSAndroid Build Coastguard Worker mv_costs->mv_cost_stack, MV_COST_WEIGHT);
842*77c1e3ccSAndroid Build Coastguard Worker return bestsme;
843*77c1e3ccSAndroid Build Coastguard Worker }
844*77c1e3ccSAndroid Build Coastguard Worker
build_second_inter_pred(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,const MV * other_mv,int ref_idx,uint8_t * second_pred)845*77c1e3ccSAndroid Build Coastguard Worker static inline void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x,
846*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, const MV *other_mv,
847*77c1e3ccSAndroid Build Coastguard Worker int ref_idx, uint8_t *second_pred) {
848*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMMON *const cm = &cpi->common;
849*77c1e3ccSAndroid Build Coastguard Worker const int pw = block_size_wide[bsize];
850*77c1e3ccSAndroid Build Coastguard Worker const int ph = block_size_high[bsize];
851*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
852*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
853*77c1e3ccSAndroid Build Coastguard Worker struct macroblockd_plane *const pd = &xd->plane[0];
854*77c1e3ccSAndroid Build Coastguard Worker const int mi_row = xd->mi_row;
855*77c1e3ccSAndroid Build Coastguard Worker const int mi_col = xd->mi_col;
856*77c1e3ccSAndroid Build Coastguard Worker const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x);
857*77c1e3ccSAndroid Build Coastguard Worker const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y);
858*77c1e3ccSAndroid Build Coastguard Worker
859*77c1e3ccSAndroid Build Coastguard Worker // This function should only ever be called for compound modes
860*77c1e3ccSAndroid Build Coastguard Worker assert(has_second_ref(mbmi));
861*77c1e3ccSAndroid Build Coastguard Worker
862*77c1e3ccSAndroid Build Coastguard Worker const int plane = 0;
863*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d ref_yv12 = xd->plane[plane].pre[!ref_idx];
864*77c1e3ccSAndroid Build Coastguard Worker
865*77c1e3ccSAndroid Build Coastguard Worker struct scale_factors sf;
866*77c1e3ccSAndroid Build Coastguard Worker av1_setup_scale_factors_for_frame(&sf, ref_yv12.width, ref_yv12.height,
867*77c1e3ccSAndroid Build Coastguard Worker cm->width, cm->height);
868*77c1e3ccSAndroid Build Coastguard Worker
869*77c1e3ccSAndroid Build Coastguard Worker InterPredParams inter_pred_params;
870*77c1e3ccSAndroid Build Coastguard Worker
871*77c1e3ccSAndroid Build Coastguard Worker av1_init_inter_params(&inter_pred_params, pw, ph, p_row, p_col,
872*77c1e3ccSAndroid Build Coastguard Worker pd->subsampling_x, pd->subsampling_y, xd->bd,
873*77c1e3ccSAndroid Build Coastguard Worker is_cur_buf_hbd(xd), 0, &sf, &ref_yv12,
874*77c1e3ccSAndroid Build Coastguard Worker mbmi->interp_filters);
875*77c1e3ccSAndroid Build Coastguard Worker inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
876*77c1e3ccSAndroid Build Coastguard Worker
877*77c1e3ccSAndroid Build Coastguard Worker // Get the prediction block from the 'other' reference frame.
878*77c1e3ccSAndroid Build Coastguard Worker av1_enc_build_one_inter_predictor(second_pred, pw, other_mv,
879*77c1e3ccSAndroid Build Coastguard Worker &inter_pred_params);
880*77c1e3ccSAndroid Build Coastguard Worker }
881*77c1e3ccSAndroid Build Coastguard Worker
882*77c1e3ccSAndroid Build Coastguard Worker // Wrapper for av1_compound_single_motion_search, for the common case
883*77c1e3ccSAndroid Build Coastguard Worker // where the second prediction is also an inter mode.
compound_single_motion_search_interinter(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int_mv * cur_mv,const uint8_t * mask,int mask_stride,int * rate_mv,int ref_idx)884*77c1e3ccSAndroid Build Coastguard Worker static int compound_single_motion_search_interinter(
885*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *cur_mv,
886*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *mask, int mask_stride, int *rate_mv, int ref_idx) {
887*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
888*77c1e3ccSAndroid Build Coastguard Worker // This function should only ever be called for compound modes
889*77c1e3ccSAndroid Build Coastguard Worker assert(has_second_ref(xd->mi[0]));
890*77c1e3ccSAndroid Build Coastguard Worker
891*77c1e3ccSAndroid Build Coastguard Worker // Prediction buffer from second frame.
892*77c1e3ccSAndroid Build Coastguard Worker DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]);
893*77c1e3ccSAndroid Build Coastguard Worker uint8_t *second_pred;
894*77c1e3ccSAndroid Build Coastguard Worker if (is_cur_buf_hbd(xd))
895*77c1e3ccSAndroid Build Coastguard Worker second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
896*77c1e3ccSAndroid Build Coastguard Worker else
897*77c1e3ccSAndroid Build Coastguard Worker second_pred = (uint8_t *)second_pred_alloc_16;
898*77c1e3ccSAndroid Build Coastguard Worker
899*77c1e3ccSAndroid Build Coastguard Worker MV *this_mv = &cur_mv[ref_idx].as_mv;
900*77c1e3ccSAndroid Build Coastguard Worker const MV *other_mv = &cur_mv[!ref_idx].as_mv;
901*77c1e3ccSAndroid Build Coastguard Worker build_second_inter_pred(cpi, x, bsize, other_mv, ref_idx, second_pred);
902*77c1e3ccSAndroid Build Coastguard Worker return av1_compound_single_motion_search(cpi, x, bsize, this_mv, second_pred,
903*77c1e3ccSAndroid Build Coastguard Worker mask, mask_stride, rate_mv, ref_idx);
904*77c1e3ccSAndroid Build Coastguard Worker }
905*77c1e3ccSAndroid Build Coastguard Worker
do_masked_motion_search_indexed(const AV1_COMP * const cpi,MACROBLOCK * x,const int_mv * const cur_mv,const INTERINTER_COMPOUND_DATA * const comp_data,BLOCK_SIZE bsize,int_mv * tmp_mv,int * rate_mv,int which)906*77c1e3ccSAndroid Build Coastguard Worker static inline void do_masked_motion_search_indexed(
907*77c1e3ccSAndroid Build Coastguard Worker const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
908*77c1e3ccSAndroid Build Coastguard Worker const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize,
909*77c1e3ccSAndroid Build Coastguard Worker int_mv *tmp_mv, int *rate_mv, int which) {
910*77c1e3ccSAndroid Build Coastguard Worker // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both
911*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
912*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
913*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE sb_type = mbmi->bsize;
914*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *mask;
915*77c1e3ccSAndroid Build Coastguard Worker const int mask_stride = block_size_wide[bsize];
916*77c1e3ccSAndroid Build Coastguard Worker
917*77c1e3ccSAndroid Build Coastguard Worker mask = av1_get_compound_type_mask(comp_data, sb_type);
918*77c1e3ccSAndroid Build Coastguard Worker
919*77c1e3ccSAndroid Build Coastguard Worker tmp_mv[0].as_int = cur_mv[0].as_int;
920*77c1e3ccSAndroid Build Coastguard Worker tmp_mv[1].as_int = cur_mv[1].as_int;
921*77c1e3ccSAndroid Build Coastguard Worker if (which == 0 || which == 1) {
922*77c1e3ccSAndroid Build Coastguard Worker compound_single_motion_search_interinter(cpi, x, bsize, tmp_mv, mask,
923*77c1e3ccSAndroid Build Coastguard Worker mask_stride, rate_mv, which);
924*77c1e3ccSAndroid Build Coastguard Worker } else if (which == 2) {
925*77c1e3ccSAndroid Build Coastguard Worker const int joint_me_num_refine_iter =
926*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.inter_sf.enable_fast_compound_mode_search == 2
927*77c1e3ccSAndroid Build Coastguard Worker ? REDUCED_JOINT_ME_REFINE_ITER
928*77c1e3ccSAndroid Build Coastguard Worker : NUM_JOINT_ME_REFINE_ITER;
929*77c1e3ccSAndroid Build Coastguard Worker av1_joint_motion_search(cpi, x, bsize, tmp_mv, mask, mask_stride, rate_mv,
930*77c1e3ccSAndroid Build Coastguard Worker !cpi->sf.mv_sf.disable_second_mv,
931*77c1e3ccSAndroid Build Coastguard Worker joint_me_num_refine_iter);
932*77c1e3ccSAndroid Build Coastguard Worker }
933*77c1e3ccSAndroid Build Coastguard Worker }
934*77c1e3ccSAndroid Build Coastguard Worker
av1_interinter_compound_motion_search(const AV1_COMP * const cpi,MACROBLOCK * x,const int_mv * const cur_mv,const BLOCK_SIZE bsize,const PREDICTION_MODE this_mode)935*77c1e3ccSAndroid Build Coastguard Worker int av1_interinter_compound_motion_search(const AV1_COMP *const cpi,
936*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCK *x,
937*77c1e3ccSAndroid Build Coastguard Worker const int_mv *const cur_mv,
938*77c1e3ccSAndroid Build Coastguard Worker const BLOCK_SIZE bsize,
939*77c1e3ccSAndroid Build Coastguard Worker const PREDICTION_MODE this_mode) {
940*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *const xd = &x->e_mbd;
941*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *const mbmi = xd->mi[0];
942*77c1e3ccSAndroid Build Coastguard Worker int_mv tmp_mv[2];
943*77c1e3ccSAndroid Build Coastguard Worker int tmp_rate_mv = 0;
944*77c1e3ccSAndroid Build Coastguard Worker // TODO(jingning): The average compound mode has proper SAD and variance
945*77c1e3ccSAndroid Build Coastguard Worker // functions implemented, and is triggerd by setting the mask pointer as
946*77c1e3ccSAndroid Build Coastguard Worker // Null. Need to further implement those for frame distance weighted mode.
947*77c1e3ccSAndroid Build Coastguard Worker mbmi->interinter_comp.seg_mask =
948*77c1e3ccSAndroid Build Coastguard Worker mbmi->interinter_comp.type == COMPOUND_AVERAGE ? NULL : xd->seg_mask;
949*77c1e3ccSAndroid Build Coastguard Worker const INTERINTER_COMPOUND_DATA *compound_data = &mbmi->interinter_comp;
950*77c1e3ccSAndroid Build Coastguard Worker
951*77c1e3ccSAndroid Build Coastguard Worker if (this_mode == NEW_NEWMV) {
952*77c1e3ccSAndroid Build Coastguard Worker do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
953*77c1e3ccSAndroid Build Coastguard Worker tmp_mv, &tmp_rate_mv, 2);
954*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[0].as_int = tmp_mv[0].as_int;
955*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[1].as_int = tmp_mv[1].as_int;
956*77c1e3ccSAndroid Build Coastguard Worker } else if (this_mode >= NEAREST_NEWMV && this_mode <= NEW_NEARMV) {
957*77c1e3ccSAndroid Build Coastguard Worker // which = 1 if this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV
958*77c1e3ccSAndroid Build Coastguard Worker // which = 0 if this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV
959*77c1e3ccSAndroid Build Coastguard Worker int which = (NEWMV == compound_ref1_mode(this_mode));
960*77c1e3ccSAndroid Build Coastguard Worker do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
961*77c1e3ccSAndroid Build Coastguard Worker tmp_mv, &tmp_rate_mv, which);
962*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[which].as_int = tmp_mv[which].as_int;
963*77c1e3ccSAndroid Build Coastguard Worker }
964*77c1e3ccSAndroid Build Coastguard Worker return tmp_rate_mv;
965*77c1e3ccSAndroid Build Coastguard Worker }
966*77c1e3ccSAndroid Build Coastguard Worker
av1_simple_motion_search_sse_var(AV1_COMP * const cpi,MACROBLOCK * x,int mi_row,int mi_col,BLOCK_SIZE bsize,int ref,FULLPEL_MV start_mv,int num_planes,int use_subpixel,unsigned int * sse,unsigned int * var)967*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_simple_motion_search_sse_var(AV1_COMP *const cpi, MACROBLOCK *x,
968*77c1e3ccSAndroid Build Coastguard Worker int mi_row, int mi_col,
969*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE bsize, int ref,
970*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV start_mv, int num_planes,
971*77c1e3ccSAndroid Build Coastguard Worker int use_subpixel, unsigned int *sse,
972*77c1e3ccSAndroid Build Coastguard Worker unsigned int *var) {
973*77c1e3ccSAndroid Build Coastguard Worker assert(num_planes == 1 &&
974*77c1e3ccSAndroid Build Coastguard Worker "Currently simple_motion_search only supports luma plane");
975*77c1e3ccSAndroid Build Coastguard Worker assert(!frame_is_intra_only(&cpi->common) &&
976*77c1e3ccSAndroid Build Coastguard Worker "Simple motion search only enabled for non-key frames");
977*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *const cm = &cpi->common;
978*77c1e3ccSAndroid Build Coastguard Worker MACROBLOCKD *xd = &x->e_mbd;
979*77c1e3ccSAndroid Build Coastguard Worker
980*77c1e3ccSAndroid Build Coastguard Worker set_offsets_for_motion_search(cpi, x, mi_row, mi_col, bsize);
981*77c1e3ccSAndroid Build Coastguard Worker
982*77c1e3ccSAndroid Build Coastguard Worker MB_MODE_INFO *mbmi = xd->mi[0];
983*77c1e3ccSAndroid Build Coastguard Worker mbmi->bsize = bsize;
984*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[0] = ref;
985*77c1e3ccSAndroid Build Coastguard Worker mbmi->ref_frame[1] = NONE_FRAME;
986*77c1e3ccSAndroid Build Coastguard Worker mbmi->motion_mode = SIMPLE_TRANSLATION;
987*77c1e3ccSAndroid Build Coastguard Worker mbmi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
988*77c1e3ccSAndroid Build Coastguard Worker
989*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref);
990*77c1e3ccSAndroid Build Coastguard Worker const YV12_BUFFER_CONFIG *scaled_ref_frame =
991*77c1e3ccSAndroid Build Coastguard Worker av1_get_scaled_ref_frame(cpi, ref);
992*77c1e3ccSAndroid Build Coastguard Worker struct buf_2d backup_yv12;
993*77c1e3ccSAndroid Build Coastguard Worker // ref_mv is used to calculate the cost of the motion vector
994*77c1e3ccSAndroid Build Coastguard Worker const MV ref_mv = kZeroMv;
995*77c1e3ccSAndroid Build Coastguard Worker const int step_param =
996*77c1e3ccSAndroid Build Coastguard Worker AOMMIN(cpi->mv_search_params.mv_step_param +
997*77c1e3ccSAndroid Build Coastguard Worker cpi->sf.part_sf.simple_motion_search_reduce_search_steps,
998*77c1e3ccSAndroid Build Coastguard Worker MAX_MVSEARCH_STEPS - 2);
999*77c1e3ccSAndroid Build Coastguard Worker int cost_list[5];
1000*77c1e3ccSAndroid Build Coastguard Worker const int ref_idx = 0;
1001*77c1e3ccSAndroid Build Coastguard Worker int bestsme;
1002*77c1e3ccSAndroid Build Coastguard Worker int_mv best_mv;
1003*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MV_STATS best_mv_stats;
1004*77c1e3ccSAndroid Build Coastguard Worker
1005*77c1e3ccSAndroid Build Coastguard Worker av1_setup_pre_planes(xd, ref_idx, yv12, mi_row, mi_col,
1006*77c1e3ccSAndroid Build Coastguard Worker get_ref_scale_factors(cm, ref), num_planes);
1007*77c1e3ccSAndroid Build Coastguard Worker set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1008*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
1009*77c1e3ccSAndroid Build Coastguard Worker backup_yv12 = xd->plane[AOM_PLANE_Y].pre[ref_idx];
1010*77c1e3ccSAndroid Build Coastguard Worker av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
1011*77c1e3ccSAndroid Build Coastguard Worker num_planes);
1012*77c1e3ccSAndroid Build Coastguard Worker }
1013*77c1e3ccSAndroid Build Coastguard Worker
1014*77c1e3ccSAndroid Build Coastguard Worker // Allow more mesh searches for screen content type on the ARF.
1015*77c1e3ccSAndroid Build Coastguard Worker const int fine_search_interval = use_fine_search_interval(cpi);
1016*77c1e3ccSAndroid Build Coastguard Worker FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
1017*77c1e3ccSAndroid Build Coastguard Worker const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
1018*77c1e3ccSAndroid Build Coastguard Worker const SEARCH_METHODS search_method =
1019*77c1e3ccSAndroid Build Coastguard Worker av1_get_default_mv_search_method(x, mv_sf, bsize);
1020*77c1e3ccSAndroid Build Coastguard Worker const search_site_config *src_search_sites =
1021*77c1e3ccSAndroid Build Coastguard Worker av1_get_search_site_config(cpi, x, search_method);
1022*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &ref_mv,
1023*77c1e3ccSAndroid Build Coastguard Worker start_mv, src_search_sites, search_method,
1024*77c1e3ccSAndroid Build Coastguard Worker fine_search_interval);
1025*77c1e3ccSAndroid Build Coastguard Worker
1026*77c1e3ccSAndroid Build Coastguard Worker bestsme = av1_full_pixel_search(start_mv, &full_ms_params, step_param,
1027*77c1e3ccSAndroid Build Coastguard Worker cond_cost_list(cpi, cost_list),
1028*77c1e3ccSAndroid Build Coastguard Worker &best_mv.as_fullmv, &best_mv_stats, NULL);
1029*77c1e3ccSAndroid Build Coastguard Worker
1030*77c1e3ccSAndroid Build Coastguard Worker const int use_subpel_search =
1031*77c1e3ccSAndroid Build Coastguard Worker bestsme < INT_MAX && !cpi->common.features.cur_frame_force_integer_mv &&
1032*77c1e3ccSAndroid Build Coastguard Worker use_subpixel &&
1033*77c1e3ccSAndroid Build Coastguard Worker (cpi->sf.mv_sf.simple_motion_subpel_force_stop != FULL_PEL);
1034*77c1e3ccSAndroid Build Coastguard Worker if (scaled_ref_frame) {
1035*77c1e3ccSAndroid Build Coastguard Worker xd->plane[AOM_PLANE_Y].pre[ref_idx] = backup_yv12;
1036*77c1e3ccSAndroid Build Coastguard Worker }
1037*77c1e3ccSAndroid Build Coastguard Worker if (use_subpel_search) {
1038*77c1e3ccSAndroid Build Coastguard Worker int not_used = 0;
1039*77c1e3ccSAndroid Build Coastguard Worker
1040*77c1e3ccSAndroid Build Coastguard Worker SUBPEL_MOTION_SEARCH_PARAMS ms_params;
1041*77c1e3ccSAndroid Build Coastguard Worker av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
1042*77c1e3ccSAndroid Build Coastguard Worker cost_list);
1043*77c1e3ccSAndroid Build Coastguard Worker // TODO(yunqing): integrate this into av1_make_default_subpel_ms_params().
1044*77c1e3ccSAndroid Build Coastguard Worker ms_params.forced_stop = mv_sf->simple_motion_subpel_force_stop;
1045*77c1e3ccSAndroid Build Coastguard Worker
1046*77c1e3ccSAndroid Build Coastguard Worker MV subpel_start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
1047*77c1e3ccSAndroid Build Coastguard Worker assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv));
1048*77c1e3ccSAndroid Build Coastguard Worker
1049*77c1e3ccSAndroid Build Coastguard Worker cpi->mv_search_params.find_fractional_mv_step(
1050*77c1e3ccSAndroid Build Coastguard Worker xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &best_mv.as_mv,
1051*77c1e3ccSAndroid Build Coastguard Worker ¬_used, &x->pred_sse[ref], NULL);
1052*77c1e3ccSAndroid Build Coastguard Worker
1053*77c1e3ccSAndroid Build Coastguard Worker mbmi->mv[0] = best_mv;
1054*77c1e3ccSAndroid Build Coastguard Worker
1055*77c1e3ccSAndroid Build Coastguard Worker // Get a copy of the prediction output
1056*77c1e3ccSAndroid Build Coastguard Worker av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1057*77c1e3ccSAndroid Build Coastguard Worker AOM_PLANE_Y, AOM_PLANE_Y);
1058*77c1e3ccSAndroid Build Coastguard Worker *var = cpi->ppi->fn_ptr[bsize].vf(
1059*77c1e3ccSAndroid Build Coastguard Worker x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].dst.buf,
1060*77c1e3ccSAndroid Build Coastguard Worker xd->plane[0].dst.stride, sse);
1061*77c1e3ccSAndroid Build Coastguard Worker } else {
1062*77c1e3ccSAndroid Build Coastguard Worker // Manually convert from units of pixel to 1/8-pixels if we are not doing
1063*77c1e3ccSAndroid Build Coastguard Worker // subpel search
1064*77c1e3ccSAndroid Build Coastguard Worker convert_fullmv_to_mv(&best_mv);
1065*77c1e3ccSAndroid Build Coastguard Worker *var = best_mv_stats.distortion;
1066*77c1e3ccSAndroid Build Coastguard Worker *sse = best_mv_stats.sse;
1067*77c1e3ccSAndroid Build Coastguard Worker }
1068*77c1e3ccSAndroid Build Coastguard Worker
1069*77c1e3ccSAndroid Build Coastguard Worker return best_mv;
1070*77c1e3ccSAndroid Build Coastguard Worker }
1071