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_interinter(
52 const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *cur_mv,
53 const uint8_t *mask, int mask_stride, int *rate_mv, int ref_idx);
54
55 int av1_compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
56 BLOCK_SIZE bsize, MV *this_mv,
57 const uint8_t *second_pred,
58 const uint8_t *mask, int mask_stride,
59 int *rate_mv, int ref_idx);
60
61 // Performs a motion search in SIMPLE_TRANSLATION mode using reference frame
62 // ref and calculates the sse and var of the residue. Note that this sets the
63 // offset of mbmi, so we will need to reset it after calling this function.
64 int_mv av1_simple_motion_search_sse_var(struct AV1_COMP *cpi, MACROBLOCK *x,
65 int mi_row, int mi_col,
66 BLOCK_SIZE bsize, int ref,
67 const FULLPEL_MV start_mv,
68 int num_planes, int use_subpixel,
69 unsigned int *sse, unsigned int *var);
70
av1_get_search_site_config(const AV1_COMP * cpi,MACROBLOCK * x,SEARCH_METHODS search_method)71 static AOM_INLINE const search_site_config *av1_get_search_site_config(
72 const AV1_COMP *cpi, MACROBLOCK *x, SEARCH_METHODS search_method) {
73 const int ref_stride = x->e_mbd.plane[0].pre[0].stride;
74
75 // AV1_COMP::mv_search_params.search_site_config is a compressor level cache
76 // that's shared by multiple threads. In most cases where all frames have the
77 // same resolution, the cache contains the search site config that we need.
78 const MotionVectorSearchParams *mv_search_params = &cpi->mv_search_params;
79 if (ref_stride == mv_search_params->search_site_cfg[SS_CFG_SRC]->stride) {
80 return mv_search_params->search_site_cfg[SS_CFG_SRC];
81 } else if (ref_stride ==
82 mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD]->stride) {
83 return mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD];
84 }
85
86 // If the cache does not contain the correct stride, then we will need to rely
87 // on the thread level config MACROBLOCK::search_site_cfg_buf. If even the
88 // thread level config doesn't match, then we need to update it.
89 search_method = search_method_lookup[search_method];
90 assert(search_method_lookup[search_method] == search_method &&
91 "The search_method_lookup table should be idempotent.");
92 if (ref_stride != x->search_site_cfg_buf[search_method].stride) {
93 av1_refresh_search_site_config(x->search_site_cfg_buf, search_method,
94 ref_stride);
95 }
96
97 return x->search_site_cfg_buf;
98 }
99
100 static AOM_INLINE SEARCH_METHODS
av1_get_faster_search_method(SEARCH_METHODS search_method)101 av1_get_faster_search_method(SEARCH_METHODS search_method) {
102 // Note on search method's accuracy:
103 // 1. NSTEP
104 // 2. DIAMOND
105 // 3. BIGDIA \approx SQUARE
106 // 4. HEX.
107 // 5. FAST_HEX \approx FAST_DIAMOND
108 switch (search_method) {
109 case NSTEP: return DIAMOND;
110 case NSTEP_8PT: return DIAMOND;
111 case DIAMOND: return BIGDIA;
112 case CLAMPED_DIAMOND: return BIGDIA;
113 case BIGDIA: return HEX;
114 case SQUARE: return HEX;
115 case HEX: return FAST_HEX;
116 case FAST_HEX: return FAST_HEX;
117 case FAST_DIAMOND: return VFAST_DIAMOND;
118 case FAST_BIGDIA: return FAST_BIGDIA;
119 case VFAST_DIAMOND: return VFAST_DIAMOND;
120 default: assert(0 && "Invalid search method!"); return DIAMOND;
121 }
122 }
123
av1_get_default_mv_search_method(const MACROBLOCK * x,const MV_SPEED_FEATURES * mv_sf,BLOCK_SIZE bsize)124 static AOM_INLINE SEARCH_METHODS av1_get_default_mv_search_method(
125 const MACROBLOCK *x, const MV_SPEED_FEATURES *mv_sf, BLOCK_SIZE bsize) {
126 SEARCH_METHODS search_method = mv_sf->search_method;
127 const int sf_blk_search_method = mv_sf->use_bsize_dependent_search_method;
128 const int min_dim = AOMMIN(block_size_wide[bsize], block_size_high[bsize]);
129 const int qband = x->qindex >> (QINDEX_BITS - 2);
130 const bool use_faster_search_method =
131 (sf_blk_search_method == 1 && min_dim >= 32) ||
132 (sf_blk_search_method >= 2 && min_dim >= 16 &&
133 x->content_state_sb.source_sad_nonrd <= kMedSad && qband < 3);
134
135 if (use_faster_search_method) {
136 search_method = av1_get_faster_search_method(search_method);
137 }
138 return search_method;
139 }
140
141 #ifdef __cplusplus
142 } // extern "C"
143 #endif
144
145 #endif // AOM_AV1_ENCODER_MOTION_SEARCH_H_
146