AOMedia AV1 Codec
rdopt.h
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 
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 
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 
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 
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 void av1_inter_mode_data_init(struct TileDataEnc *tile_data);
167 void av1_inter_mode_data_fit(TileDataEnc *tile_data, int rdmult);
168 
169 static INLINE int coded_to_superres_mi(int mi_col, int denom) {
170  return (mi_col * denom + SCALE_NUMERATOR / 2) / SCALE_NUMERATOR;
171 }
172 
173 static INLINE int av1_encoder_get_relative_dist(int a, int b) {
174  assert(a >= 0 && b >= 0);
175  return (a - b);
176 }
177 
178 // This function will return number of mi's in a superblock.
179 static INLINE int av1_get_sb_mi_size(const AV1_COMMON *const cm) {
180  const int mi_alloc_size_1d = mi_size_wide[cm->mi_params.mi_alloc_bsize];
181  int sb_mi_rows =
182  (mi_size_wide[cm->seq_params->sb_size] + mi_alloc_size_1d - 1) /
183  mi_alloc_size_1d;
184  assert(mi_size_wide[cm->seq_params->sb_size] ==
185  mi_size_high[cm->seq_params->sb_size]);
186  int sb_mi_size = sb_mi_rows * sb_mi_rows;
187 
188  return sb_mi_size;
189 }
190 
191 // This function will copy usable ref_mv_stack[ref_frame][4] and
192 // weight[ref_frame][4] information from ref_mv_stack[ref_frame][8] and
193 // weight[ref_frame][8].
194 static INLINE void av1_copy_usable_ref_mv_stack_and_weight(
195  const MACROBLOCKD *xd, MB_MODE_INFO_EXT *const mbmi_ext,
196  MV_REFERENCE_FRAME ref_frame) {
197  memcpy(mbmi_ext->weight[ref_frame], xd->weight[ref_frame],
198  USABLE_REF_MV_STACK_SIZE * sizeof(xd->weight[0][0]));
199  memcpy(mbmi_ext->ref_mv_stack[ref_frame], xd->ref_mv_stack[ref_frame],
200  USABLE_REF_MV_STACK_SIZE * sizeof(xd->ref_mv_stack[0][0]));
201 }
202 
203 // This function prunes the mode if either of the reference frame falls in the
204 // pruning list
205 static INLINE int prune_ref(const MV_REFERENCE_FRAME *const ref_frame,
206  const unsigned int *const ref_display_order_hint,
207  const unsigned int frame_display_order_hint,
208  const int *ref_frame_list) {
209  for (int i = 0; i < 2; i++) {
210  if (ref_frame_list[i] == NONE_FRAME) continue;
211 
212  if (ref_frame[0] == ref_frame_list[i] ||
213  ref_frame[1] == ref_frame_list[i]) {
214  if (av1_encoder_get_relative_dist(
215  ref_display_order_hint[ref_frame_list[i] - LAST_FRAME],
216  frame_display_order_hint) < 0)
217  return 1;
218  }
219  }
220  return 0;
221 }
222 
223 static INLINE int prune_ref_by_selective_ref_frame(
224  const AV1_COMP *const cpi, const MACROBLOCK *const x,
225  const MV_REFERENCE_FRAME *const ref_frame,
226  const unsigned int *const ref_display_order_hint) {
227  const SPEED_FEATURES *const sf = &cpi->sf;
228  if (!sf->inter_sf.selective_ref_frame) return 0;
229 
230  const int comp_pred = ref_frame[1] > INTRA_FRAME;
231 
232  if (sf->inter_sf.selective_ref_frame >= 2 ||
233  (sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
234  int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
235 
236  if (x != NULL) {
237  // Disable pruning if either tpl suggests that we keep the frame or
238  // the pred_mv gives us the best sad
239  if (x->tpl_keep_ref_frame[LAST3_FRAME] ||
240  x->pred_mv_sad[LAST3_FRAME] == x->best_pred_mv_sad) {
241  ref_frame_list[0] = NONE_FRAME;
242  }
243  if (x->tpl_keep_ref_frame[LAST2_FRAME] ||
244  x->pred_mv_sad[LAST2_FRAME] == x->best_pred_mv_sad) {
245  ref_frame_list[1] = NONE_FRAME;
246  }
247  }
248 
249  if (prune_ref(ref_frame, ref_display_order_hint,
250  ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
251  ref_frame_list))
252  return 1;
253  }
254 
255  if (sf->inter_sf.selective_ref_frame >= 3) {
256  int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
257 
258  if (x != NULL) {
259  // Disable pruning if either tpl suggests that we keep the frame or
260  // the pred_mv gives us the best sad
261  if (x->tpl_keep_ref_frame[ALTREF2_FRAME] ||
262  x->pred_mv_sad[ALTREF2_FRAME] == x->best_pred_mv_sad) {
263  ref_frame_list[0] = NONE_FRAME;
264  }
265  if (x->tpl_keep_ref_frame[BWDREF_FRAME] ||
266  x->pred_mv_sad[BWDREF_FRAME] == x->best_pred_mv_sad) {
267  ref_frame_list[1] = NONE_FRAME;
268  }
269  }
270 
271  if (prune_ref(ref_frame, ref_display_order_hint,
272  ref_display_order_hint[LAST_FRAME - LAST_FRAME],
273  ref_frame_list))
274  return 1;
275  }
276 
277  return 0;
278 }
279 
280 // This function will copy the best reference mode information from
281 // MB_MODE_INFO_EXT to MB_MODE_INFO_EXT_FRAME.
282 static INLINE void av1_copy_mbmi_ext_to_mbmi_ext_frame(
283  MB_MODE_INFO_EXT_FRAME *mbmi_ext_best,
284  const MB_MODE_INFO_EXT *const mbmi_ext, uint8_t ref_frame_type) {
285  memcpy(mbmi_ext_best->ref_mv_stack, mbmi_ext->ref_mv_stack[ref_frame_type],
286  sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
287  memcpy(mbmi_ext_best->weight, mbmi_ext->weight[ref_frame_type],
288  sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
289  mbmi_ext_best->mode_context = mbmi_ext->mode_context[ref_frame_type];
290  mbmi_ext_best->ref_mv_count = mbmi_ext->ref_mv_count[ref_frame_type];
291  memcpy(mbmi_ext_best->global_mvs, mbmi_ext->global_mvs,
292  sizeof(mbmi_ext->global_mvs));
293 }
294 
295 #ifdef __cplusplus
296 } // extern "C"
297 #endif
298 
299 #endif // AOM_AV1_ENCODER_RDOPT_H_
Extended mode info derived from mbmi.
Definition: block.h:196
uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE]
The weights used to compute the ref mvs.
Definition: block.h:201
CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE]
The reference mv list for the current block.
Definition: block.h:218
void av1_rd_pick_inter_mode(struct AV1_COMP *cpi, struct TileDataEnc *tile_data, struct macroblock *x, struct RD_STATS *rd_cost, BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far)
AV1 inter mode selection.
Definition: rdopt.c:5623
CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE]
The reference mv list for the current block.
Definition: block.h:199
void av1_nonrd_pick_inter_mode_sb(struct AV1_COMP *cpi, struct TileDataEnc *tile_data, struct macroblock *x, struct RD_STATS *rd_cost, BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx)
AV1 inter mode selection based on Non-RD optimized model.
Definition: nonrd_pickmode.c:2242
uint8_t tpl_keep_ref_frame[REF_FRAMES]
Disables certain ref frame pruning based on tpl.
Definition: block.h:982
CommonModeInfoParams mi_params
Definition: av1_common_int.h:915
uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]
Number of ref mvs in the drl.
Definition: block.h:203
CANDIDATE_MV ref_mv_stack[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE]
Definition: blockd.h:783
SPEED_FEATURES sf
Definition: encoder.h:2833
int pred_mv_sad[REF_FRAMES]
Sum absolute distortion of the predicted mv for each ref frame.
Definition: block.h:968
Top level speed vs quality trade off data struture.
Definition: speed_features.h:1388
SequenceHeader * seq_params
Definition: av1_common_int.h:981
int16_t mode_context
Context used to encode the current mode.
Definition: block.h:227
INTER_MODE_SPEED_FEATURES inter_sf
Definition: speed_features.h:1422
int_mv global_mvs[REF_FRAMES]
Global mvs.
Definition: block.h:205
int best_pred_mv_sad
The minimum of pred_mv_sad.
Definition: block.h:970
uint8_t ref_mv_count
Number of ref mvs in the drl.
Definition: block.h:222
Variables related to current coding block.
Definition: blockd.h:577
void av1_rd_pick_intra_mode_sb(const struct AV1_COMP *cpi, struct macroblock *x, struct RD_STATS *rd_cost, BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd)
AV1 intra mode selection for intra frames.
Definition: rdopt.c:3282
Top level encoder structure.
Definition: encoder.h:2632
Declares top-level encoder structures and functions.
Top level common structure used by both encoder and decoder.
Definition: av1_common_int.h:755
int16_t mode_context[MODE_CTX_REF_FRAMES]
Context used to encode the current mode.
Definition: block.h:207
BLOCK_SIZE mi_alloc_bsize
Definition: av1_common_int.h:554
uint16_t weight[USABLE_REF_MV_STACK_SIZE]
The weights used to compute the ref mvs.
Definition: block.h:220
uint16_t weight[MODE_CTX_REF_FRAMES][MAX_REF_MV_STACK_SIZE]
Definition: blockd.h:788
Encoder&#39;s parameters related to the current coding block.
Definition: block.h:778
void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost, BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx)
AV1 intra mode selection based on Non-RD optimized model.
Definition: nonrd_pickmode.c:1713
int_mv global_mvs[REF_FRAMES]
Global mvs.
Definition: block.h:225
Stores best extended mode information at frame level.
Definition: block.h:216