#include "av1/common/common_data.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconintra.h"
#include "av1/encoder/encoder.h"
#include "av1/encoder/encodeframe_utils.h"
#include "av1/encoder/encoder_utils.h"
#include "av1/encoder/rdopt.h"
void av1_set_ssim_rdmult(const AV1_COMP *const cpi, int *errorperbit,
const BLOCK_SIZE bsize, const int mi_row,
const int mi_col, int *const rdmult) { … }
#if CONFIG_SALIENCY_MAP
void av1_set_saliency_map_vmaf_rdmult(const AV1_COMP *const cpi,
int *errorperbit, const BLOCK_SIZE bsize,
const int mi_row, const int mi_col,
int *const rdmult) {
const AV1_COMMON *const cm = &cpi->common;
const int num_mi_w = mi_size_wide[bsize];
const int num_mi_h = mi_size_high[bsize];
const int num_cols = (cm->mi_params.mi_cols + num_mi_w - 1) / num_mi_w;
*rdmult =
(int)(*rdmult * cpi->sm_scaling_factor[(mi_row / num_mi_h) * num_cols +
(mi_col / num_mi_w)]);
*rdmult = AOMMAX(*rdmult, 0);
av1_set_error_per_bit(errorperbit, *rdmult);
}
#endif
#if !CONFIG_REALTIME_ONLY
int av1_get_cb_rdmult(const AV1_COMP *const cpi, MACROBLOCK *const x,
const BLOCK_SIZE bsize, const int mi_row,
const int mi_col) {
const AV1_COMMON *const cm = &cpi->common;
assert(IMPLIES(cpi->ppi->gf_group.size > 0,
cpi->gf_frame_index < cpi->ppi->gf_group.size));
const int tpl_idx = cpi->gf_frame_index;
int deltaq_rdmult = set_rdmult(cpi, x, -1);
if (!av1_tpl_stats_ready(&cpi->ppi->tpl_data, tpl_idx)) return deltaq_rdmult;
if (cm->superres_scale_denominator != SCALE_NUMERATOR) return deltaq_rdmult;
if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return deltaq_rdmult;
if (x->rb == 0) return deltaq_rdmult;
TplParams *const tpl_data = &cpi->ppi->tpl_data;
TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
const int mi_wide = mi_size_wide[bsize];
const int mi_high = mi_size_high[bsize];
int tpl_stride = tpl_frame->stride;
double intra_cost_base = 0;
double mc_dep_cost_base = 0;
double cbcmp_base = 0;
const int step = 1 << tpl_data->tpl_stats_block_mis_log2;
for (int row = mi_row; row < mi_row + mi_high; row += step) {
for (int col = mi_col; col < mi_col + mi_wide; col += step) {
if (row >= cm->mi_params.mi_rows || col >= cm->mi_params.mi_cols)
continue;
TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
double cbcmp = (double)this_stats->srcrf_dist;
int64_t mc_dep_delta =
RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
this_stats->mc_dep_dist);
double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
intra_cost_base += log(dist_scaled) * cbcmp;
mc_dep_cost_base += log(3 * dist_scaled + mc_dep_delta) * cbcmp;
cbcmp_base += cbcmp;
}
}
if (cbcmp_base == 0) return deltaq_rdmult;
double rk = exp((intra_cost_base - mc_dep_cost_base) / cbcmp_base);
deltaq_rdmult = (int)(deltaq_rdmult * (rk / x->rb));
return AOMMAX(deltaq_rdmult, 1);
}
#endif
static inline void update_filter_type_count(FRAME_COUNTS *counts,
const MACROBLOCKD *xd,
const MB_MODE_INFO *mbmi) { … }
static inline void copy_mbmi_ext_frame_to_mbmi_ext(
MB_MODE_INFO_EXT *mbmi_ext,
const MB_MODE_INFO_EXT_FRAME *const mbmi_ext_best, uint8_t ref_frame_type) { … }
void av1_update_state(const AV1_COMP *const cpi, ThreadData *td,
const PICK_MODE_CONTEXT *const ctx, int mi_row,
int mi_col, BLOCK_SIZE bsize, RUN_TYPE dry_run) { … }
void av1_update_inter_mode_stats(FRAME_CONTEXT *fc, FRAME_COUNTS *counts,
PREDICTION_MODE mode, int16_t mode_context) { … }
static void update_palette_cdf(MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
FRAME_COUNTS *counts) { … }
void av1_sum_intra_stats(const AV1_COMMON *const cm, FRAME_COUNTS *counts,
MACROBLOCKD *xd, const MB_MODE_INFO *const mbmi,
const MB_MODE_INFO *above_mi,
const MB_MODE_INFO *left_mi, const int intraonly) { … }
void av1_restore_context(MACROBLOCK *x, const RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
int mi_row, int mi_col, BLOCK_SIZE bsize,
const int num_planes) { … }
void av1_save_context(const MACROBLOCK *x, RD_SEARCH_MACROBLOCK_CONTEXT *ctx,
int mi_row, int mi_col, BLOCK_SIZE bsize,
const int num_planes) { … }
static void set_partial_sb_partition(const AV1_COMMON *const cm,
MB_MODE_INFO *mi, int bh_in, int bw_in,
int mi_rows_remaining,
int mi_cols_remaining, BLOCK_SIZE bsize,
MB_MODE_INFO **mib) { … }
void av1_set_fixed_partitioning(AV1_COMP *cpi, const TileInfo *const tile,
MB_MODE_INFO **mib, int mi_row, int mi_col,
BLOCK_SIZE bsize) { … }
int av1_is_leaf_split_partition(AV1_COMMON *cm, int mi_row, int mi_col,
BLOCK_SIZE bsize) { … }
#if !CONFIG_REALTIME_ONLY
int av1_get_rdmult_delta(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
int mi_col, int orig_rdmult) {
AV1_COMMON *const cm = &cpi->common;
const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
assert(IMPLIES(cpi->ppi->gf_group.size > 0,
cpi->gf_frame_index < cpi->ppi->gf_group.size));
const int tpl_idx = cpi->gf_frame_index;
TplParams *const tpl_data = &cpi->ppi->tpl_data;
const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
int64_t intra_cost = 0;
int64_t mc_dep_cost = 0;
const int mi_wide = mi_size_wide[bsize];
const int mi_high = mi_size_high[bsize];
TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
int tpl_stride = tpl_frame->stride;
if (!av1_tpl_stats_ready(&cpi->ppi->tpl_data, cpi->gf_frame_index)) {
return orig_rdmult;
}
if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) {
return orig_rdmult;
}
#ifndef NDEBUG
int mi_count = 0;
#endif
const int mi_col_sr =
coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
const int mi_col_end_sr =
coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
const int step = 1 << block_mis_log2;
const int row_step = step;
const int col_step_sr =
coded_to_superres_mi(step, cm->superres_scale_denominator);
for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
TplDepStats *this_stats =
&tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
int64_t mc_dep_delta =
RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
this_stats->mc_dep_dist);
intra_cost += this_stats->recrf_dist << RDDIV_BITS;
mc_dep_cost += (this_stats->recrf_dist << RDDIV_BITS) + mc_dep_delta;
#ifndef NDEBUG
mi_count++;
#endif
}
}
assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
double beta = 1.0;
if (mc_dep_cost > 0 && intra_cost > 0) {
const double r0 = cpi->rd.r0;
const double rk = (double)intra_cost / mc_dep_cost;
beta = (r0 / rk);
}
int rdmult = av1_get_adaptive_rdmult(cpi, beta);
rdmult = AOMMIN(rdmult, orig_rdmult * 3 / 2);
rdmult = AOMMAX(rdmult, orig_rdmult * 1 / 2);
rdmult = AOMMAX(1, rdmult);
return rdmult;
}
int av1_active_h_edge(const AV1_COMP *cpi, int mi_row, int mi_step) {
int top_edge = 0;
int bottom_edge = cpi->common.mi_params.mi_rows;
int is_active_h_edge = 0;
if (is_stat_consumption_stage_twopass(cpi)) {
const AV1_COMMON *const cm = &cpi->common;
const FIRSTPASS_STATS *const this_frame_stats = read_one_frame_stats(
&cpi->ppi->twopass, cm->current_frame.display_order_hint);
if (this_frame_stats == NULL) return AOM_CODEC_ERROR;
top_edge += (int)(this_frame_stats->inactive_zone_rows * 4);
bottom_edge -= (int)(this_frame_stats->inactive_zone_rows * 4);
bottom_edge = AOMMAX(top_edge, bottom_edge);
}
if (((top_edge >= mi_row) && (top_edge < (mi_row + mi_step))) ||
((bottom_edge >= mi_row) && (bottom_edge < (mi_row + mi_step)))) {
is_active_h_edge = 1;
}
return is_active_h_edge;
}
int av1_active_v_edge(const AV1_COMP *cpi, int mi_col, int mi_step) {
int left_edge = 0;
int right_edge = cpi->common.mi_params.mi_cols;
int is_active_v_edge = 0;
if (is_stat_consumption_stage_twopass(cpi)) {
const AV1_COMMON *const cm = &cpi->common;
const FIRSTPASS_STATS *const this_frame_stats = read_one_frame_stats(
&cpi->ppi->twopass, cm->current_frame.display_order_hint);
if (this_frame_stats == NULL) return AOM_CODEC_ERROR;
left_edge += (int)(this_frame_stats->inactive_zone_cols * 4);
right_edge -= (int)(this_frame_stats->inactive_zone_cols * 4);
right_edge = AOMMAX(left_edge, right_edge);
}
if (((left_edge >= mi_col) && (left_edge < (mi_col + mi_step))) ||
((right_edge >= mi_col) && (right_edge < (mi_col + mi_step)))) {
is_active_v_edge = 1;
}
return is_active_v_edge;
}
void av1_get_tpl_stats_sb(AV1_COMP *cpi, BLOCK_SIZE bsize, int mi_row,
int mi_col, SuperBlockEnc *sb_enc) {
sb_enc->tpl_data_count = 0;
if (!cpi->oxcf.algo_cfg.enable_tpl_model) return;
if (cpi->common.current_frame.frame_type == KEY_FRAME) return;
const FRAME_UPDATE_TYPE update_type =
get_frame_update_type(&cpi->ppi->gf_group, cpi->gf_frame_index);
if (update_type == INTNL_OVERLAY_UPDATE || update_type == OVERLAY_UPDATE)
return;
assert(IMPLIES(cpi->ppi->gf_group.size > 0,
cpi->gf_frame_index < cpi->ppi->gf_group.size));
AV1_COMMON *const cm = &cpi->common;
const int gf_group_index = cpi->gf_frame_index;
TplParams *const tpl_data = &cpi->ppi->tpl_data;
if (!av1_tpl_stats_ready(tpl_data, gf_group_index)) return;
const int mi_wide = mi_size_wide[bsize];
const int mi_high = mi_size_high[bsize];
TplDepFrame *tpl_frame = &tpl_data->tpl_frame[gf_group_index];
TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
int tpl_stride = tpl_frame->stride;
int mi_count = 0;
int count = 0;
const int mi_col_sr =
coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
const int mi_col_end_sr =
coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
const BLOCK_SIZE tpl_bsize = convert_length_to_bsize(tpl_data->tpl_bsize_1d);
assert(mi_size_wide[tpl_bsize] == mi_size_high[tpl_bsize]);
const int row_step = mi_size_high[tpl_bsize];
const int col_step_sr = coded_to_superres_mi(mi_size_wide[tpl_bsize],
cm->superres_scale_denominator);
sb_enc->tpl_stride = (mi_col_end_sr - mi_col_sr) / col_step_sr;
for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
assert(count < MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) {
sb_enc->tpl_inter_cost[count] = INT64_MAX;
sb_enc->tpl_intra_cost[count] = INT64_MAX;
for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
sb_enc->tpl_mv[count][i].as_int = INVALID_MV;
}
count++;
continue;
}
TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos(
row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)];
sb_enc->tpl_inter_cost[count] = this_stats->inter_cost
<< TPL_DEP_COST_SCALE_LOG2;
sb_enc->tpl_intra_cost[count] = this_stats->intra_cost
<< TPL_DEP_COST_SCALE_LOG2;
memcpy(sb_enc->tpl_mv[count], this_stats->mv, sizeof(this_stats->mv));
mi_count++;
count++;
}
}
assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
sb_enc->tpl_data_count = mi_count;
}
int av1_get_q_for_deltaq_objective(AV1_COMP *const cpi, ThreadData *td,
int64_t *delta_dist, BLOCK_SIZE bsize,
int mi_row, int mi_col) {
AV1_COMMON *const cm = &cpi->common;
assert(IMPLIES(cpi->ppi->gf_group.size > 0,
cpi->gf_frame_index < cpi->ppi->gf_group.size));
const int tpl_idx = cpi->gf_frame_index;
TplParams *const tpl_data = &cpi->ppi->tpl_data;
const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
double intra_cost = 0;
double mc_dep_reg = 0;
double mc_dep_cost = 0;
double cbcmp_base = 1;
double srcrf_dist = 0;
double srcrf_sse = 0;
double srcrf_rate = 0;
const int mi_wide = mi_size_wide[bsize];
const int mi_high = mi_size_high[bsize];
const int base_qindex = cm->quant_params.base_qindex;
if (tpl_idx >= MAX_TPL_FRAME_IDX) return base_qindex;
TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
int tpl_stride = tpl_frame->stride;
if (!tpl_frame->is_valid) return base_qindex;
#ifndef NDEBUG
int mi_count = 0;
#endif
const int mi_col_sr =
coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
const int mi_col_end_sr =
coded_to_superres_mi(mi_col + mi_wide, cm->superres_scale_denominator);
const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
const int step = 1 << block_mis_log2;
const int row_step = step;
const int col_step_sr =
coded_to_superres_mi(step, cm->superres_scale_denominator);
for (int row = mi_row; row < mi_row + mi_high; row += row_step) {
for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
if (row >= cm->mi_params.mi_rows || col >= mi_cols_sr) continue;
TplDepStats *this_stats =
&tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
double cbcmp = (double)this_stats->srcrf_dist;
int64_t mc_dep_delta =
RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate,
this_stats->mc_dep_dist);
double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS);
intra_cost += log(dist_scaled) * cbcmp;
mc_dep_cost += log(dist_scaled + mc_dep_delta) * cbcmp;
mc_dep_reg += log(3 * dist_scaled + mc_dep_delta) * cbcmp;
srcrf_dist += (double)(this_stats->srcrf_dist << RDDIV_BITS);
srcrf_sse += (double)(this_stats->srcrf_sse << RDDIV_BITS);
srcrf_rate += (double)(this_stats->srcrf_rate << TPL_DEP_COST_SCALE_LOG2);
#ifndef NDEBUG
mi_count++;
#endif
cbcmp_base += cbcmp;
}
}
assert(mi_count <= MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB);
int offset = 0;
double beta = 1.0;
double rk;
if (mc_dep_cost > 0 && intra_cost > 0) {
const double r0 = cpi->rd.r0;
rk = exp((intra_cost - mc_dep_cost) / cbcmp_base);
td->mb.rb = exp((intra_cost - mc_dep_reg) / cbcmp_base);
beta = (r0 / rk);
assert(beta > 0.0);
} else {
return base_qindex;
}
offset = av1_get_deltaq_offset(cm->seq_params->bit_depth, base_qindex, beta);
const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
int qindex = cm->quant_params.base_qindex + offset;
qindex = AOMMIN(qindex, MAXQ);
qindex = AOMMAX(qindex, MINQ);
int frm_qstep = av1_dc_quant_QTX(base_qindex, 0, cm->seq_params->bit_depth);
int sbs_qstep =
av1_dc_quant_QTX(base_qindex, offset, cm->seq_params->bit_depth);
if (delta_dist) {
double sbs_dist = srcrf_dist * pow((double)sbs_qstep / frm_qstep, 2.0);
double sbs_rate = srcrf_rate * ((double)frm_qstep / sbs_qstep);
sbs_dist = AOMMIN(sbs_dist, srcrf_sse);
*delta_dist = (int64_t)((sbs_dist - srcrf_dist) / rk);
*delta_dist += RDCOST(tpl_frame->base_rdmult, 4 * 256, 0);
*delta_dist += RDCOST(tpl_frame->base_rdmult, sbs_rate - srcrf_rate, 0);
}
return qindex;
}
#if !DISABLE_HDR_LUMA_DELTAQ
static const int hdr_thres[HDR_QP_LEVELS + 1] = { 0, 301, 367, 434, 501, 567,
634, 701, 767, 834, 1024 };
static const int hdr10_qp_offset[HDR_QP_LEVELS] = { 3, 2, 1, 0, -1,
-2, -3, -4, -5, -6 };
#endif
int av1_get_q_for_hdr(AV1_COMP *const cpi, MACROBLOCK *const x,
BLOCK_SIZE bsize, int mi_row, int mi_col) {
AV1_COMMON *const cm = &cpi->common;
assert(cm->seq_params->bit_depth == AOM_BITS_10);
#if DISABLE_HDR_LUMA_DELTAQ
(void)x;
(void)bsize;
(void)mi_row;
(void)mi_col;
return cm->quant_params.base_qindex;
#else
const int block_luma_avg = av1_log_block_avg(cpi, x, bsize, mi_row, mi_col);
int offset = 0;
for (int i = 0; i < HDR_QP_LEVELS; i++) {
if (block_luma_avg >= hdr_thres[i] && block_luma_avg < hdr_thres[i + 1]) {
offset = (int)(hdr10_qp_offset[i] * QP_SCALE_FACTOR);
break;
}
}
const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
offset = AOMMIN(offset, delta_q_info->delta_q_res * 9 - 1);
offset = AOMMAX(offset, -delta_q_info->delta_q_res * 9 + 1);
int qindex = cm->quant_params.base_qindex + offset;
qindex = AOMMIN(qindex, MAXQ);
qindex = AOMMAX(qindex, MINQ);
return qindex;
#endif
}
#endif
void av1_reset_simple_motion_tree_partition(SIMPLE_MOTION_DATA_TREE *sms_tree,
BLOCK_SIZE bsize) { … }
void av1_update_picked_ref_frames_mask(MACROBLOCK *const x, int ref_type,
BLOCK_SIZE bsize, int mib_size,
int mi_row, int mi_col) { … }
static void avg_cdf_symbol(aom_cdf_prob *cdf_ptr_left, aom_cdf_prob *cdf_ptr_tr,
int num_cdfs, int cdf_stride, int nsymbs,
int wt_left, int wt_tr) { … }
#define AVERAGE_CDF(cname_left, cname_tr, nsymbs) …
#define AVG_CDF_STRIDE(cname_left, cname_tr, nsymbs, cdf_stride) …
static void avg_nmv(nmv_context *nmv_left, nmv_context *nmv_tr, int wt_left,
int wt_tr) { … }
void av1_avg_cdf_symbols(FRAME_CONTEXT *ctx_left, FRAME_CONTEXT *ctx_tr,
int wt_left, int wt_tr) { … }
static int check_neighbor_blocks(MB_MODE_INFO **mi, int mi_stride,
const TileInfo *const tile_info, int mi_row,
int mi_col) { … }
static int fast_detect_non_zero_motion(AV1_COMP *cpi, const uint8_t *src_y,
int src_ystride,
const uint8_t *last_src_y,
int last_src_ystride, int mi_row,
int mi_col) { … }
void av1_source_content_sb(AV1_COMP *cpi, MACROBLOCK *x, TileDataEnc *tile_data,
int mi_row, int mi_col) { … }
void av1_reset_mbmi(CommonModeInfoParams *const mi_params, BLOCK_SIZE sb_size,
int mi_row, int mi_col) { … }
void av1_backup_sb_state(SB_FIRST_PASS_STATS *sb_fp_stats, const AV1_COMP *cpi,
ThreadData *td, const TileDataEnc *tile_data,
int mi_row, int mi_col) { … }
void av1_restore_sb_state(const SB_FIRST_PASS_STATS *sb_fp_stats, AV1_COMP *cpi,
ThreadData *td, TileDataEnc *tile_data, int mi_row,
int mi_col) { … }
static int skip_cost_update(const SequenceHeader *seq_params,
const TileInfo *const tile_info, const int mi_row,
const int mi_col,
INTERNAL_COST_UPDATE_TYPE upd_level) { … }
static int skip_mv_cost_update(AV1_COMP *cpi, const TileInfo *const tile_info,
const int mi_row, const int mi_col) { … }
static int skip_dv_cost_update(AV1_COMP *cpi, const TileInfo *const tile_info,
const int mi_row, const int mi_col) { … }
void av1_set_cost_upd_freq(AV1_COMP *cpi, ThreadData *td,
const TileInfo *const tile_info, const int mi_row,
const int mi_col) { … }
void av1_dealloc_src_diff_buf(struct macroblock *mb, int num_planes) { … }
void av1_alloc_src_diff_buf(const struct AV1Common *cm, struct macroblock *mb) { … }