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