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