xref: /aosp_15_r20/external/libaom/av1/encoder/motion_search_facade.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 #ifndef AOM_AV1_ENCODER_MOTION_SEARCH_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_MOTION_SEARCH_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
18*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
19*77c1e3ccSAndroid Build Coastguard Worker #endif
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker #define NUM_JOINT_ME_REFINE_ITER 2
22*77c1e3ccSAndroid Build Coastguard Worker #define REDUCED_JOINT_ME_REFINE_ITER 1
23*77c1e3ccSAndroid Build Coastguard Worker // TODO(any): rename this struct to something else. There is already another
24*77c1e3ccSAndroid Build Coastguard Worker // struct called inter_modes_info, which makes this terribly confusing.
25*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
26*77c1e3ccSAndroid Build Coastguard Worker   int drl_cost;
27*77c1e3ccSAndroid Build Coastguard Worker   int_mv full_search_mv;
28*77c1e3ccSAndroid Build Coastguard Worker   int full_mv_rate;
29*77c1e3ccSAndroid Build Coastguard Worker   int full_mv_bestsme;
30*77c1e3ccSAndroid Build Coastguard Worker   int skip;
31*77c1e3ccSAndroid Build Coastguard Worker } inter_mode_info;
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker struct HandleInterModeArgs;
34*77c1e3ccSAndroid Build Coastguard Worker void av1_single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x,
35*77c1e3ccSAndroid Build Coastguard Worker                               BLOCK_SIZE bsize, int ref_idx, int *rate_mv,
36*77c1e3ccSAndroid Build Coastguard Worker                               int search_range, inter_mode_info *mode_info,
37*77c1e3ccSAndroid Build Coastguard Worker                               int_mv *best_mv,
38*77c1e3ccSAndroid Build Coastguard Worker                               struct HandleInterModeArgs *const args);
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker int av1_joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
41*77c1e3ccSAndroid Build Coastguard Worker                             BLOCK_SIZE bsize, int_mv *cur_mv,
42*77c1e3ccSAndroid Build Coastguard Worker                             const uint8_t *mask, int mask_stride, int *rate_mv,
43*77c1e3ccSAndroid Build Coastguard Worker                             int allow_second_mv, int joint_me_num_refine_iter);
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker int av1_interinter_compound_motion_search(const AV1_COMP *const cpi,
46*77c1e3ccSAndroid Build Coastguard Worker                                           MACROBLOCK *x,
47*77c1e3ccSAndroid Build Coastguard Worker                                           const int_mv *const cur_mv,
48*77c1e3ccSAndroid Build Coastguard Worker                                           const BLOCK_SIZE bsize,
49*77c1e3ccSAndroid Build Coastguard Worker                                           const PREDICTION_MODE this_mode);
50*77c1e3ccSAndroid Build Coastguard Worker 
51*77c1e3ccSAndroid Build Coastguard Worker int av1_compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
52*77c1e3ccSAndroid Build Coastguard Worker                                       BLOCK_SIZE bsize, MV *this_mv,
53*77c1e3ccSAndroid Build Coastguard Worker                                       const uint8_t *second_pred,
54*77c1e3ccSAndroid Build Coastguard Worker                                       const uint8_t *mask, int mask_stride,
55*77c1e3ccSAndroid Build Coastguard Worker                                       int *rate_mv, int ref_idx);
56*77c1e3ccSAndroid Build Coastguard Worker 
57*77c1e3ccSAndroid Build Coastguard Worker // Performs a motion search in SIMPLE_TRANSLATION mode using reference frame
58*77c1e3ccSAndroid Build Coastguard Worker // ref and calculates the sse and var of the residue. Note that this sets the
59*77c1e3ccSAndroid Build Coastguard Worker // offset of mbmi, so we will need to reset it after calling this function.
60*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_simple_motion_search_sse_var(struct AV1_COMP *cpi, MACROBLOCK *x,
61*77c1e3ccSAndroid Build Coastguard Worker                                         int mi_row, int mi_col,
62*77c1e3ccSAndroid Build Coastguard Worker                                         BLOCK_SIZE bsize, int ref,
63*77c1e3ccSAndroid Build Coastguard Worker                                         const FULLPEL_MV start_mv,
64*77c1e3ccSAndroid Build Coastguard Worker                                         int num_planes, int use_subpixel,
65*77c1e3ccSAndroid Build Coastguard Worker                                         unsigned int *sse, unsigned int *var);
66*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_search_site_config(const AV1_COMP * cpi,MACROBLOCK * x,SEARCH_METHODS search_method)67*77c1e3ccSAndroid Build Coastguard Worker static inline const search_site_config *av1_get_search_site_config(
68*77c1e3ccSAndroid Build Coastguard Worker     const AV1_COMP *cpi, MACROBLOCK *x, SEARCH_METHODS search_method) {
69*77c1e3ccSAndroid Build Coastguard Worker   const int ref_stride = x->e_mbd.plane[0].pre[0].stride;
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker   // AV1_COMP::mv_search_params.search_site_config is a compressor level cache
72*77c1e3ccSAndroid Build Coastguard Worker   // that's shared by multiple threads. In most cases where all frames have the
73*77c1e3ccSAndroid Build Coastguard Worker   // same resolution, the cache contains the search site config that we need.
74*77c1e3ccSAndroid Build Coastguard Worker   const MotionVectorSearchParams *mv_search_params = &cpi->mv_search_params;
75*77c1e3ccSAndroid Build Coastguard Worker   if (ref_stride == mv_search_params->search_site_cfg[SS_CFG_SRC]->stride) {
76*77c1e3ccSAndroid Build Coastguard Worker     return mv_search_params->search_site_cfg[SS_CFG_SRC];
77*77c1e3ccSAndroid Build Coastguard Worker   } else if (ref_stride ==
78*77c1e3ccSAndroid Build Coastguard Worker              mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD]->stride) {
79*77c1e3ccSAndroid Build Coastguard Worker     return mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD];
80*77c1e3ccSAndroid Build Coastguard Worker   }
81*77c1e3ccSAndroid Build Coastguard Worker 
82*77c1e3ccSAndroid Build Coastguard Worker   // If the cache does not contain the correct stride, then we will need to rely
83*77c1e3ccSAndroid Build Coastguard Worker   // on the thread level config MACROBLOCK::search_site_cfg_buf. If even the
84*77c1e3ccSAndroid Build Coastguard Worker   // thread level config doesn't match, then we need to update it.
85*77c1e3ccSAndroid Build Coastguard Worker   search_method = search_method_lookup[search_method];
86*77c1e3ccSAndroid Build Coastguard Worker   assert(search_method_lookup[search_method] == search_method &&
87*77c1e3ccSAndroid Build Coastguard Worker          "The search_method_lookup table should be idempotent.");
88*77c1e3ccSAndroid Build Coastguard Worker   if (ref_stride != x->search_site_cfg_buf[search_method].stride) {
89*77c1e3ccSAndroid Build Coastguard Worker     av1_refresh_search_site_config(x->search_site_cfg_buf, search_method,
90*77c1e3ccSAndroid Build Coastguard Worker                                    ref_stride);
91*77c1e3ccSAndroid Build Coastguard Worker   }
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker   return x->search_site_cfg_buf;
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_faster_search_method(SEARCH_METHODS search_method)96*77c1e3ccSAndroid Build Coastguard Worker static inline SEARCH_METHODS av1_get_faster_search_method(
97*77c1e3ccSAndroid Build Coastguard Worker     SEARCH_METHODS search_method) {
98*77c1e3ccSAndroid Build Coastguard Worker   // Note on search method's accuracy:
99*77c1e3ccSAndroid Build Coastguard Worker   //  1. NSTEP
100*77c1e3ccSAndroid Build Coastguard Worker   //  2. DIAMOND
101*77c1e3ccSAndroid Build Coastguard Worker   //  3. BIGDIA \approx SQUARE
102*77c1e3ccSAndroid Build Coastguard Worker   //  4. HEX.
103*77c1e3ccSAndroid Build Coastguard Worker   //  5. FAST_HEX \approx FAST_DIAMOND
104*77c1e3ccSAndroid Build Coastguard Worker   switch (search_method) {
105*77c1e3ccSAndroid Build Coastguard Worker     case NSTEP: return DIAMOND;
106*77c1e3ccSAndroid Build Coastguard Worker     case NSTEP_8PT: return DIAMOND;
107*77c1e3ccSAndroid Build Coastguard Worker     case DIAMOND: return BIGDIA;
108*77c1e3ccSAndroid Build Coastguard Worker     case CLAMPED_DIAMOND: return BIGDIA;
109*77c1e3ccSAndroid Build Coastguard Worker     case BIGDIA: return HEX;
110*77c1e3ccSAndroid Build Coastguard Worker     case SQUARE: return HEX;
111*77c1e3ccSAndroid Build Coastguard Worker     case HEX: return FAST_HEX;
112*77c1e3ccSAndroid Build Coastguard Worker     case FAST_HEX: return FAST_HEX;
113*77c1e3ccSAndroid Build Coastguard Worker     case FAST_DIAMOND: return VFAST_DIAMOND;
114*77c1e3ccSAndroid Build Coastguard Worker     case FAST_BIGDIA: return FAST_BIGDIA;
115*77c1e3ccSAndroid Build Coastguard Worker     case VFAST_DIAMOND: return VFAST_DIAMOND;
116*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Invalid search method!"); return DIAMOND;
117*77c1e3ccSAndroid Build Coastguard Worker   }
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_default_mv_search_method(const MACROBLOCK * x,const MV_SPEED_FEATURES * mv_sf,BLOCK_SIZE bsize)120*77c1e3ccSAndroid Build Coastguard Worker static inline SEARCH_METHODS av1_get_default_mv_search_method(
121*77c1e3ccSAndroid Build Coastguard Worker     const MACROBLOCK *x, const MV_SPEED_FEATURES *mv_sf, BLOCK_SIZE bsize) {
122*77c1e3ccSAndroid Build Coastguard Worker   SEARCH_METHODS search_method = mv_sf->search_method;
123*77c1e3ccSAndroid Build Coastguard Worker   const int sf_blk_search_method = mv_sf->use_bsize_dependent_search_method;
124*77c1e3ccSAndroid Build Coastguard Worker   const int min_dim = AOMMIN(block_size_wide[bsize], block_size_high[bsize]);
125*77c1e3ccSAndroid Build Coastguard Worker   const int qband = x->qindex >> (QINDEX_BITS - 2);
126*77c1e3ccSAndroid Build Coastguard Worker   const bool use_faster_search_method =
127*77c1e3ccSAndroid Build Coastguard Worker       (sf_blk_search_method == 1 && min_dim >= 32) ||
128*77c1e3ccSAndroid Build Coastguard Worker       (sf_blk_search_method >= 2 && min_dim >= 16 &&
129*77c1e3ccSAndroid Build Coastguard Worker        x->content_state_sb.source_sad_nonrd <= kMedSad && qband < 3);
130*77c1e3ccSAndroid Build Coastguard Worker 
131*77c1e3ccSAndroid Build Coastguard Worker   if (use_faster_search_method) {
132*77c1e3ccSAndroid Build Coastguard Worker     search_method = av1_get_faster_search_method(search_method);
133*77c1e3ccSAndroid Build Coastguard Worker   }
134*77c1e3ccSAndroid Build Coastguard Worker   return search_method;
135*77c1e3ccSAndroid Build Coastguard Worker }
136*77c1e3ccSAndroid Build Coastguard Worker 
137*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
138*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
139*77c1e3ccSAndroid Build Coastguard Worker #endif
140*77c1e3ccSAndroid Build Coastguard Worker 
141*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AV1_ENCODER_MOTION_SEARCH_H_
142