1 /*
2 * Copyright (c) 2016, 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_RDOPT_H_
13 #define AOM_AV1_ENCODER_RDOPT_H_
14
15 #include <stdbool.h>
16
17 #include "av1/common/blockd.h"
18 #include "av1/common/txb_common.h"
19
20 #include "av1/encoder/block.h"
21 #include "av1/encoder/context_tree.h"
22 #include "av1/encoder/encoder.h"
23 #include "av1/encoder/encodetxb.h"
24 #include "av1/encoder/rdopt_utils.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define COMP_TYPE_RD_THRESH_SCALE 11
31 #define COMP_TYPE_RD_THRESH_SHIFT 4
32 #define MAX_WINNER_MOTION_MODES 10
33
34 struct TileInfo;
35 struct macroblock;
36 struct RD_STATS;
37
38 /*!\brief AV1 intra mode selection for intra frames.
39 *
40 * \ingroup intra_mode_search
41 * \callgraph
42 * Top level function for rd-based intra mode selection during intra frame
43 * encoding. This function will first search for the best luma prediction by
44 * calling av1_rd_pick_intra_sby_mode, then it searches for chroma prediction
45 * with av1_rd_pick_intra_sbuv_mode. If applicable, this function ends the
46 * search with an evaluation for intrabc.
47 *
48 * \param[in] cpi Top-level encoder structure.
49 * \param[in] x Pointer to structure holding all the data for
50 the current macroblock.
51 * \param[in] rd_cost Struct to keep track of the RD information.
52 * \param[in] bsize Current block size.
53 * \param[in] ctx Structure to hold snapshot of coding context
54 during the mode picking process.
55 * \param[in] best_rd Best RD seen for this block so far.
56 *
57 * \return Nothing is returned. Instead, the MB_MODE_INFO struct inside x
58 * is modified to store information about the best mode computed
59 * in this function. The rd_cost struct is also updated with the RD stats
60 * corresponding to the best mode found.
61 */
62 void av1_rd_pick_intra_mode_sb(const struct AV1_COMP *cpi, struct macroblock *x,
63 struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
64 PICK_MODE_CONTEXT *ctx, int64_t best_rd);
65
66 /*!\brief AV1 inter mode selection.
67 *
68 * \ingroup inter_mode_search
69 * \callgraph
70 * Top level function for inter mode selection. This function will loop over
71 * all possible inter modes and select the best one for the current block by
72 * computing the RD cost. The mode search and RD are computed in
73 * handle_inter_mode(), which is called from this function within the main
74 * loop.
75 *
76 * \param[in] cpi Top-level encoder structure
77 * \param[in] tile_data Pointer to struct holding adaptive
78 data/contexts/models for the tile during
79 encoding
80 * \param[in] x Pointer to structure holding all the data for
81 the current macroblock
82 * \param[in] rd_cost Struct to keep track of the RD information
83 * \param[in] bsize Current block size
84 * \param[in] ctx Structure to hold snapshot of coding context
85 during the mode picking process
86 * \param[in] best_rd_so_far Best RD seen for this block so far
87 *
88 * \return Nothing is returned. Instead, the MB_MODE_INFO struct inside x
89 * is modified to store information about the best mode computed
90 * in this function. The rd_cost struct is also updated with the RD stats
91 * corresponding to the best mode found.
92 */
93 void av1_rd_pick_inter_mode(struct AV1_COMP *cpi, struct TileDataEnc *tile_data,
94 struct macroblock *x, struct RD_STATS *rd_cost,
95 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx,
96 int64_t best_rd_so_far);
97
98 /*!\brief AV1 intra mode selection based on Non-RD optimized model.
99 *
100 * \ingroup nonrd_mode_search
101 * \callgraph
102 * \callergraph
103 * Top level function for Non-RD optimized intra mode selection.
104 * This finction will loop over subset of intra modes and select the best one
105 * based on calculated modelled RD cost. Only 4 intra modes are checked as
106 * specified in \c intra_mode_list. When calculating RD cost Hadamard transform
107 * of residual is used to calculate rate. Estmation of RD cost is performed
108 * in \c estimate_block_intra which is called from this function
109 *
110 * \param[in] cpi Top-level encoder structure
111 * \param[in] x Pointer to structure holding all the data for
112 the current macroblock
113 * \param[in] rd_cost Struct to keep track of the RD information
114 * \param[in] bsize Current block size
115 * \param[in] ctx Structure to hold snapshot of coding context
116 during the mode picking process
117 *
118 * \return Nothing is returned. Instead, the MB_MODE_INFO struct inside x
119 * is modified to store information about the best mode computed
120 * in this function. The rd_cost struct is also updated with the RD stats
121 * corresponding to the best mode found.
122 */
123 void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
124 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
125
126 /*!\brief AV1 inter mode selection based on Non-RD optimized model.
127 *
128 * \ingroup nonrd_mode_search
129 * \callgraph
130 * Top level function for Non-RD optimized inter mode selection.
131 * This finction will loop over subset of inter modes and select the best one
132 * based on calculated modelled RD cost. While making decisions which modes to
133 * check, this function applies heuristics based on previously checked modes,
134 * block residual variance, block size, and other factors to prune certain
135 * modes and reference frames. Currently only single reference frame modes
136 * are checked. Additional heuristics are applied to decide if intra modes
137 * need to be checked.
138 * *
139 * \param[in] cpi Top-level encoder structure
140 * \param[in] tile_data Pointer to struct holding adaptive
141 data/contexts/models for the tile during
142 encoding
143 * \param[in] x Pointer to structure holding all the data for
144 the current macroblock
145 * \param[in] rd_cost Struct to keep track of the RD information
146 * \param[in] bsize Current block size
147 * \param[in] ctx Structure to hold snapshot of coding context
148 during the mode picking process
149 *
150 * \return Nothing is returned. Instead, the MB_MODE_INFO struct inside x
151 * is modified to store information about the best mode computed
152 * in this function. The rd_cost struct is also updated with the RD stats
153 * corresponding to the best mode found.
154 */
155 void av1_nonrd_pick_inter_mode_sb(struct AV1_COMP *cpi,
156 struct TileDataEnc *tile_data,
157 struct macroblock *x,
158 struct RD_STATS *rd_cost, BLOCK_SIZE bsize,
159 PICK_MODE_CONTEXT *ctx);
160
161 void av1_rd_pick_inter_mode_sb_seg_skip(
162 const struct AV1_COMP *cpi, struct TileDataEnc *tile_data,
163 struct macroblock *x, int mi_row, int mi_col, struct RD_STATS *rd_cost,
164 BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far);
165
166 // TODO(any): The defs below could potentially be moved to rdopt_utils.h instead
167 // because they are not the main rdopt functions.
168 /*!\cond */
169 // The best edge strength seen in the block, as well as the best x and y
170 // components of edge strength seen.
171 typedef struct {
172 uint16_t magnitude;
173 uint16_t x;
174 uint16_t y;
175 } EdgeInfo;
176 /*!\endcond */
177
178 /** Returns an integer indicating the strength of the edge.
179 * 0 means no edge found, 556 is the strength of a solid black/white edge,
180 * and the number may range higher if the signal is even stronger (e.g., on a
181 * corner). high_bd is a bool indicating the source should be treated
182 * as a 16-bit array. bd is the bit depth.
183 */
184 EdgeInfo av1_edge_exists(const uint8_t *src, int src_stride, int w, int h,
185 bool high_bd, int bd);
186
187 /** Applies a Gaussian blur with sigma = 1.3. Used by av1_edge_exists and
188 * tests.
189 */
190 void av1_gaussian_blur(const uint8_t *src, int src_stride, int w, int h,
191 uint8_t *dst, bool high_bd, int bd);
192
193 /*!\cond */
194 /* Applies standard 3x3 Sobel matrix. */
195 typedef struct {
196 int16_t x;
197 int16_t y;
198 } sobel_xy;
199 /*!\endcond */
200
201 sobel_xy av1_sobel(const uint8_t *input, int stride, int i, int j,
202 bool high_bd);
203
204 void av1_inter_mode_data_init(struct TileDataEnc *tile_data);
205 void av1_inter_mode_data_fit(TileDataEnc *tile_data, int rdmult);
206
coded_to_superres_mi(int mi_col,int denom)207 static INLINE int coded_to_superres_mi(int mi_col, int denom) {
208 return (mi_col * denom + SCALE_NUMERATOR / 2) / SCALE_NUMERATOR;
209 }
210
av1_encoder_get_relative_dist(int a,int b)211 static INLINE int av1_encoder_get_relative_dist(int a, int b) {
212 assert(a >= 0 && b >= 0);
213 return (a - b);
214 }
215
216 // This function will return number of mi's in a superblock.
av1_get_sb_mi_size(const AV1_COMMON * const cm)217 static INLINE int av1_get_sb_mi_size(const AV1_COMMON *const cm) {
218 const int mi_alloc_size_1d = mi_size_wide[cm->mi_params.mi_alloc_bsize];
219 int sb_mi_rows =
220 (mi_size_wide[cm->seq_params->sb_size] + mi_alloc_size_1d - 1) /
221 mi_alloc_size_1d;
222 assert(mi_size_wide[cm->seq_params->sb_size] ==
223 mi_size_high[cm->seq_params->sb_size]);
224 int sb_mi_size = sb_mi_rows * sb_mi_rows;
225
226 return sb_mi_size;
227 }
228
229 // This function will copy usable ref_mv_stack[ref_frame][4] and
230 // weight[ref_frame][4] information from ref_mv_stack[ref_frame][8] and
231 // weight[ref_frame][8].
av1_copy_usable_ref_mv_stack_and_weight(const MACROBLOCKD * xd,MB_MODE_INFO_EXT * const mbmi_ext,MV_REFERENCE_FRAME ref_frame)232 static INLINE void av1_copy_usable_ref_mv_stack_and_weight(
233 const MACROBLOCKD *xd, MB_MODE_INFO_EXT *const mbmi_ext,
234 MV_REFERENCE_FRAME ref_frame) {
235 memcpy(mbmi_ext->weight[ref_frame], xd->weight[ref_frame],
236 USABLE_REF_MV_STACK_SIZE * sizeof(xd->weight[0][0]));
237 memcpy(mbmi_ext->ref_mv_stack[ref_frame], xd->ref_mv_stack[ref_frame],
238 USABLE_REF_MV_STACK_SIZE * sizeof(xd->ref_mv_stack[0][0]));
239 }
240
241 // This function prunes the mode if either of the reference frame falls in the
242 // pruning list
prune_ref(const MV_REFERENCE_FRAME * const ref_frame,const unsigned int * const ref_display_order_hint,const unsigned int frame_display_order_hint,const int * ref_frame_list)243 static INLINE int prune_ref(const MV_REFERENCE_FRAME *const ref_frame,
244 const unsigned int *const ref_display_order_hint,
245 const unsigned int frame_display_order_hint,
246 const int *ref_frame_list) {
247 for (int i = 0; i < 2; i++) {
248 if (ref_frame_list[i] == NONE_FRAME) continue;
249
250 if (ref_frame[0] == ref_frame_list[i] ||
251 ref_frame[1] == ref_frame_list[i]) {
252 if (av1_encoder_get_relative_dist(
253 ref_display_order_hint[ref_frame_list[i] - LAST_FRAME],
254 frame_display_order_hint) < 0)
255 return 1;
256 }
257 }
258 return 0;
259 }
260
prune_ref_by_selective_ref_frame(const AV1_COMP * const cpi,const MACROBLOCK * const x,const MV_REFERENCE_FRAME * const ref_frame,const unsigned int * const ref_display_order_hint)261 static INLINE int prune_ref_by_selective_ref_frame(
262 const AV1_COMP *const cpi, const MACROBLOCK *const x,
263 const MV_REFERENCE_FRAME *const ref_frame,
264 const unsigned int *const ref_display_order_hint) {
265 const SPEED_FEATURES *const sf = &cpi->sf;
266 if (!sf->inter_sf.selective_ref_frame) return 0;
267
268 const int comp_pred = ref_frame[1] > INTRA_FRAME;
269
270 if (sf->inter_sf.selective_ref_frame >= 2 ||
271 (sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
272 int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
273
274 if (x != NULL) {
275 // Disable pruning if either tpl suggests that we keep the frame or
276 // the pred_mv gives us the best sad
277 if (x->tpl_keep_ref_frame[LAST3_FRAME] ||
278 x->pred_mv_sad[LAST3_FRAME] == x->best_pred_mv_sad) {
279 ref_frame_list[0] = NONE_FRAME;
280 }
281 if (x->tpl_keep_ref_frame[LAST2_FRAME] ||
282 x->pred_mv_sad[LAST2_FRAME] == x->best_pred_mv_sad) {
283 ref_frame_list[1] = NONE_FRAME;
284 }
285 }
286
287 if (prune_ref(ref_frame, ref_display_order_hint,
288 ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
289 ref_frame_list))
290 return 1;
291 }
292
293 if (sf->inter_sf.selective_ref_frame >= 3) {
294 int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
295
296 if (x != NULL) {
297 // Disable pruning if either tpl suggests that we keep the frame or
298 // the pred_mv gives us the best sad
299 if (x->tpl_keep_ref_frame[ALTREF2_FRAME] ||
300 x->pred_mv_sad[ALTREF2_FRAME] == x->best_pred_mv_sad) {
301 ref_frame_list[0] = NONE_FRAME;
302 }
303 if (x->tpl_keep_ref_frame[BWDREF_FRAME] ||
304 x->pred_mv_sad[BWDREF_FRAME] == x->best_pred_mv_sad) {
305 ref_frame_list[1] = NONE_FRAME;
306 }
307 }
308
309 if (prune_ref(ref_frame, ref_display_order_hint,
310 ref_display_order_hint[LAST_FRAME - LAST_FRAME],
311 ref_frame_list))
312 return 1;
313 }
314
315 return 0;
316 }
317
318 // This function will copy the best reference mode information from
319 // MB_MODE_INFO_EXT to MB_MODE_INFO_EXT_FRAME.
av1_copy_mbmi_ext_to_mbmi_ext_frame(MB_MODE_INFO_EXT_FRAME * mbmi_ext_best,const MB_MODE_INFO_EXT * const mbmi_ext,uint8_t ref_frame_type)320 static INLINE void av1_copy_mbmi_ext_to_mbmi_ext_frame(
321 MB_MODE_INFO_EXT_FRAME *mbmi_ext_best,
322 const MB_MODE_INFO_EXT *const mbmi_ext, uint8_t ref_frame_type) {
323 memcpy(mbmi_ext_best->ref_mv_stack, mbmi_ext->ref_mv_stack[ref_frame_type],
324 sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
325 memcpy(mbmi_ext_best->weight, mbmi_ext->weight[ref_frame_type],
326 sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
327 mbmi_ext_best->mode_context = mbmi_ext->mode_context[ref_frame_type];
328 mbmi_ext_best->ref_mv_count = mbmi_ext->ref_mv_count[ref_frame_type];
329 memcpy(mbmi_ext_best->global_mvs, mbmi_ext->global_mvs,
330 sizeof(mbmi_ext->global_mvs));
331 }
332
333 #ifdef __cplusplus
334 } // extern "C"
335 #endif
336
337 #endif // AOM_AV1_ENCODER_RDOPT_H_
338