rdopt.h
/*
* Copyright (c) 2016, Alliance for Open Media. All rights reserved.
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#ifndef AOM_AV1_ENCODER_RDOPT_H_
#define AOM_AV1_ENCODER_RDOPT_H_
#include <stdbool.h>
#include "av1/common/blockd.h"
#include "av1/common/txb_common.h"
#include "av1/encoder/block.h"
#include "av1/encoder/context_tree.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/encodetxb.h"
#include "av1/encoder/rdopt_utils.h"
#ifdef __cplusplus
extern "C" {
#endif
#define COMP_TYPE_RD_THRESH_SCALE 11
#define COMP_TYPE_RD_THRESH_SHIFT 4
#define MAX_WINNER_MOTION_MODES 10
struct TileInfo;
struct macroblock;
struct RD_STATS;
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);
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);
void av1_nonrd_pick_intra_mode(AV1_COMP *cpi, MACROBLOCK *x, RD_STATS *rd_cost,
BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx);
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);
void av1_rd_pick_inter_mode_sb_seg_skip(
const struct AV1_COMP *cpi, struct TileDataEnc *tile_data,
struct macroblock *x, int mi_row, int mi_col, struct RD_STATS *rd_cost,
BLOCK_SIZE bsize, PICK_MODE_CONTEXT *ctx, int64_t best_rd_so_far);
void av1_inter_mode_data_init(struct TileDataEnc *tile_data);
void av1_inter_mode_data_fit(TileDataEnc *tile_data, int rdmult);
static inline int coded_to_superres_mi(int mi_col, int denom) {
return (mi_col * denom + SCALE_NUMERATOR / 2) / SCALE_NUMERATOR;
}
static inline int av1_encoder_get_relative_dist(int a, int b) {
assert(a >= 0 && b >= 0);
return (a - b);
}
// This function will return number of mi's in a superblock.
static inline int av1_get_sb_mi_size(const AV1_COMMON *const cm) {
const int mi_alloc_size_1d = mi_size_wide[cm->mi_params.mi_alloc_bsize];
int sb_mi_rows =
(mi_size_wide[cm->seq_params->sb_size] + mi_alloc_size_1d - 1) /
mi_alloc_size_1d;
assert(mi_size_wide[cm->seq_params->sb_size] ==
mi_size_high[cm->seq_params->sb_size]);
int sb_mi_size = sb_mi_rows * sb_mi_rows;
return sb_mi_size;
}
// This function prunes the mode if either of the reference frame falls in the
// pruning list
static inline int 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) {
for (int i = 0; i < 2; i++) {
if (ref_frame_list[i] == NONE_FRAME) continue;
if (ref_frame[0] == ref_frame_list[i] ||
ref_frame[1] == ref_frame_list[i]) {
if (av1_encoder_get_relative_dist(
ref_display_order_hint[ref_frame_list[i] - LAST_FRAME],
frame_display_order_hint) < 0)
return 1;
}
}
return 0;
}
static inline int has_closest_ref_frames(const MV_REFERENCE_FRAME *ref_frame,
int8_t closest_past_ref,
int8_t closest_future_ref) {
int has_closest_past_ref =
(ref_frame[0] == closest_past_ref) || (ref_frame[1] == closest_past_ref);
int has_closest_future_ref = (ref_frame[0] == closest_future_ref) ||
(ref_frame[1] == closest_future_ref);
return (has_closest_past_ref && has_closest_future_ref);
}
static inline int has_best_pred_mv_sad(const MV_REFERENCE_FRAME *ref_frame,
const MACROBLOCK *const x) {
int has_best_past_pred_mv_sad = 0;
int has_best_future_pred_mv_sad = 0;
if (x->best_pred_mv_sad[0] < INT_MAX && x->best_pred_mv_sad[1] < INT_MAX) {
has_best_past_pred_mv_sad =
(x->pred_mv_sad[ref_frame[0]] == x->best_pred_mv_sad[0]) ||
(x->pred_mv_sad[ref_frame[1]] == x->best_pred_mv_sad[0]);
has_best_future_pred_mv_sad =
(x->pred_mv_sad[ref_frame[0]] == x->best_pred_mv_sad[1]) ||
(x->pred_mv_sad[ref_frame[1]] == x->best_pred_mv_sad[1]);
}
return (has_best_past_pred_mv_sad && has_best_future_pred_mv_sad);
}
static inline int 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) {
const SPEED_FEATURES *const sf = &cpi->sf;
if (!sf->inter_sf.selective_ref_frame) return 0;
const int comp_pred = ref_frame[1] > INTRA_FRAME;
if (sf->inter_sf.selective_ref_frame >= 2 ||
(sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
if (x != NULL) {
// Disable pruning if either tpl suggests that we keep the frame or
// the pred_mv gives us the best sad
if (x->tpl_keep_ref_frame[LAST3_FRAME] ||
x->pred_mv_sad[LAST3_FRAME] == x->best_pred_mv_sad[0]) {
ref_frame_list[0] = NONE_FRAME;
}
if (x->tpl_keep_ref_frame[LAST2_FRAME] ||
x->pred_mv_sad[LAST2_FRAME] == x->best_pred_mv_sad[0]) {
ref_frame_list[1] = NONE_FRAME;
}
}
if (prune_ref(ref_frame, ref_display_order_hint,
ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
ref_frame_list))
return 1;
}
if (sf->inter_sf.selective_ref_frame >= 3) {
int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
if (x != NULL) {
// Disable pruning if either tpl suggests that we keep the frame or
// the pred_mv gives us the best sad
if (x->tpl_keep_ref_frame[ALTREF2_FRAME] ||
x->pred_mv_sad[ALTREF2_FRAME] == x->best_pred_mv_sad[0]) {
ref_frame_list[0] = NONE_FRAME;
}
if (x->tpl_keep_ref_frame[BWDREF_FRAME] ||
x->pred_mv_sad[BWDREF_FRAME] == x->best_pred_mv_sad[0]) {
ref_frame_list[1] = NONE_FRAME;
}
}
if (prune_ref(ref_frame, ref_display_order_hint,
ref_display_order_hint[LAST_FRAME - LAST_FRAME],
ref_frame_list))
return 1;
}
if (x != NULL && sf->inter_sf.prune_comp_ref_frames && comp_pred) {
int closest_ref_frames = has_closest_ref_frames(
ref_frame, cpi->ref_frame_dist_info.nearest_past_ref,
cpi->ref_frame_dist_info.nearest_future_ref);
const int ref_idx0 = ref_frame[0] - LAST_FRAME;
const int ref_idx1 = ref_frame[1] - LAST_FRAME;
const int keep_comp_ref_pair_mask =
(cpi->keep_comp_ref_frame_mask & (1 << ref_idx0)) &&
(cpi->keep_comp_ref_frame_mask & (1 << ref_idx1));
// Don't prune references frame pairs which are important or closest.
if (!(keep_comp_ref_pair_mask || closest_ref_frames)) {
// Prune reference frames which are not the closest to the current frame.
if (sf->inter_sf.prune_comp_ref_frames >= 3) {
return 1;
} else if (sf->inter_sf.prune_comp_ref_frames >= 1) {
// Prune reference frames with non minimum pred_mv_sad.
if (has_best_pred_mv_sad(ref_frame, x) == 0) return 1;
}
}
}
return 0;
}
// This function will copy the best reference mode information from
// MB_MODE_INFO_EXT to MB_MODE_INFO_EXT_FRAME.
static inline void 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) {
memcpy(mbmi_ext_best->ref_mv_stack, mbmi_ext->ref_mv_stack[ref_frame_type],
sizeof(mbmi_ext->ref_mv_stack[USABLE_REF_MV_STACK_SIZE]));
memcpy(mbmi_ext_best->weight, mbmi_ext->weight[ref_frame_type],
sizeof(mbmi_ext->weight[USABLE_REF_MV_STACK_SIZE]));
mbmi_ext_best->mode_context = mbmi_ext->mode_context[ref_frame_type];
mbmi_ext_best->ref_mv_count = mbmi_ext->ref_mv_count[ref_frame_type];
memcpy(mbmi_ext_best->global_mvs, mbmi_ext->global_mvs,
sizeof(mbmi_ext->global_mvs));
}
#ifdef __cplusplus
} // extern "C"
#endif
#endif // AOM_AV1_ENCODER_RDOPT_H_
block.h
MACROBLOCK
struct macroblock MACROBLOCK
Encoder's parameters related to the current coding block.
encoder.h
Declares top-level encoder structures and functions.
av1_rd_pick_inter_mode
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:6100
av1_rd_pick_intra_mode_sb
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:3636
av1_nonrd_pick_inter_mode_sb
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:3278
av1_nonrd_pick_intra_mode
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:1582
AV1Common::seq_params
SequenceHeader * seq_params
Definition av1_common_int.h:992
AV1Common::mi_params
CommonModeInfoParams mi_params
Definition av1_common_int.h:926
AV1_COMP
Top level encoder structure.
Definition encoder.h:2897
AV1_COMP::keep_comp_ref_frame_mask
int keep_comp_ref_frame_mask
Definition encoder.h:3343
AV1_COMP::sf
SPEED_FEATURES sf
Definition encoder.h:3124
AV1_COMP::ref_frame_dist_info
RefFrameDistanceInfo ref_frame_dist_info
Definition encoder.h:3426
CommonModeInfoParams::mi_alloc_bsize
BLOCK_SIZE mi_alloc_bsize
Definition av1_common_int.h:560
MB_MODE_INFO_EXT_FRAME
Stores best extended mode information at frame level.
Definition block.h:245
MB_MODE_INFO_EXT_FRAME::ref_mv_count
uint8_t ref_mv_count
Number of ref mvs in the drl.
Definition block.h:251
MB_MODE_INFO_EXT_FRAME::weight
uint16_t weight[USABLE_REF_MV_STACK_SIZE]
The weights used to compute the ref mvs.
Definition block.h:249
MB_MODE_INFO_EXT_FRAME::ref_mv_stack
CANDIDATE_MV ref_mv_stack[USABLE_REF_MV_STACK_SIZE]
The reference mv list for the current block.
Definition block.h:247
MB_MODE_INFO_EXT_FRAME::mode_context
int16_t mode_context
Context used to encode the current mode.
Definition block.h:256
MB_MODE_INFO_EXT_FRAME::global_mvs
int_mv global_mvs[REF_FRAMES]
Global mvs.
Definition block.h:254
MB_MODE_INFO_EXT
Extended mode info derived from mbmi.
Definition block.h:225
MB_MODE_INFO_EXT::global_mvs
int_mv global_mvs[REF_FRAMES]
Global mvs.
Definition block.h:234
MB_MODE_INFO_EXT::ref_mv_stack
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:228
MB_MODE_INFO_EXT::weight
uint16_t weight[MODE_CTX_REF_FRAMES][USABLE_REF_MV_STACK_SIZE]
The weights used to compute the ref mvs.
Definition block.h:230
MB_MODE_INFO_EXT::mode_context
int16_t mode_context[MODE_CTX_REF_FRAMES]
Context used to encode the current mode.
Definition block.h:236
MB_MODE_INFO_EXT::ref_mv_count
uint8_t ref_mv_count[MODE_CTX_REF_FRAMES]
Number of ref mvs in the drl.
Definition block.h:232
RefFrameDistanceInfo::nearest_past_ref
int8_t nearest_past_ref
Definition encoder.h:2199
RefFrameDistanceInfo::nearest_future_ref
int8_t nearest_future_ref
Definition encoder.h:2203
SPEED_FEATURES
Top level speed vs quality trade off data struture.
Definition speed_features.h:2087
SPEED_FEATURES::inter_sf
INTER_MODE_SPEED_FEATURES inter_sf
Definition speed_features.h:2121
macroblock
Encoder's parameters related to the current coding block.
Definition block.h:889
macroblock::best_pred_mv_sad
int best_pred_mv_sad[2]
The minimum of pred_mv_sad.
Definition block.h:1127
macroblock::pred_mv_sad
int pred_mv_sad[REF_FRAMES]
Sum absolute distortion of the predicted mv for each ref frame.
Definition block.h:1121
macroblock::tpl_keep_ref_frame
uint8_t tpl_keep_ref_frame[REF_FRAMES]
Disables certain ref frame pruning based on tpl.
Definition block.h:1139