/* * 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.
*/
// The baseline rd thresholds for breaking out of the rd loop for // certain modes are assumed to be based on 8x8 blocks. // This table is used to correct for block size. // The factors here are << 2 (2 = x0.5, 32 = x8 etc). staticconst uint8_t rd_thresh_block_size_factor[BLOCK_SIZES_ALL] = {
2, 3, 3, 4, 6, 6, 8, 12, 12, 16, 24, 24, 32, 48, 48, 64, 4, 4, 8, 8, 16, 16
};
void av1_fill_mode_rates(AV1_COMMON *const cm, ModeCosts *mode_costs,
FRAME_CONTEXT *fc) { int i, j;
for (i = 0; i < PARTITION_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->partition_cost[i],
fc->partition_cdf[i], NULL);
if (cm->current_frame.skip_mode_info.skip_mode_flag) { for (i = 0; i < SKIP_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->skip_mode_cost[i],
fc->skip_mode_cdfs[i], NULL);
}
}
for (i = 0; i < SKIP_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->skip_txfm_cost[i],
fc->skip_txfm_cdfs[i], NULL);
}
for (i = 0; i < KF_MODE_CONTEXTS; ++i) for (j = 0; j < KF_MODE_CONTEXTS; ++j)
av1_cost_tokens_from_cdf(mode_costs->y_mode_costs[i][j],
fc->kf_y_cdf[i][j], NULL);
for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
av1_cost_tokens_from_cdf(mode_costs->mbmode_cost[i], fc->y_mode_cdf[i],
NULL); for (i = 0; i < CFL_ALLOWED_TYPES; ++i) for (j = 0; j < INTRA_MODES; ++j)
av1_cost_tokens_from_cdf(mode_costs->intra_uv_mode_cost[i][j],
fc->uv_mode_cdf[i][j], NULL);
av1_cost_tokens_from_cdf(mode_costs->filter_intra_mode_cost,
fc->filter_intra_mode_cdf, NULL); for (i = 0; i < BLOCK_SIZES_ALL; ++i) { if (av1_filter_intra_allowed_bsize(cm, i))
av1_cost_tokens_from_cdf(mode_costs->filter_intra_cost[i],
fc->filter_intra_cdfs[i], NULL);
}
for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->switchable_interp_costs[i],
fc->switchable_interp_cdf[i], NULL);
for (i = 0; i < PALATTE_BSIZE_CTXS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->palette_y_size_cost[i],
fc->palette_y_size_cdf[i], NULL);
av1_cost_tokens_from_cdf(mode_costs->palette_uv_size_cost[i],
fc->palette_uv_size_cdf[i], NULL); for (j = 0; j < PALETTE_Y_MODE_CONTEXTS; ++j) {
av1_cost_tokens_from_cdf(mode_costs->palette_y_mode_cost[i][j],
fc->palette_y_mode_cdf[i][j], NULL);
}
}
for (i = 0; i < PALETTE_UV_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->palette_uv_mode_cost[i],
fc->palette_uv_mode_cdf[i], NULL);
}
for (i = 0; i < PALETTE_SIZES; ++i) { for (j = 0; j < PALETTE_COLOR_INDEX_CONTEXTS; ++j) {
av1_cost_tokens_from_cdf(mode_costs->palette_y_color_cost[i][j],
fc->palette_y_color_index_cdf[i][j], NULL);
av1_cost_tokens_from_cdf(mode_costs->palette_uv_color_cost[i][j],
fc->palette_uv_color_index_cdf[i][j], NULL);
}
}
int sign_cost[CFL_JOINT_SIGNS];
av1_cost_tokens_from_cdf(sign_cost, fc->cfl_sign_cdf, NULL); for (int joint_sign = 0; joint_sign < CFL_JOINT_SIGNS; joint_sign++) { int *cost_u = mode_costs->cfl_cost[joint_sign][CFL_PRED_U]; int *cost_v = mode_costs->cfl_cost[joint_sign][CFL_PRED_V]; if (CFL_SIGN_U(joint_sign) == CFL_SIGN_ZERO) {
memset(cost_u, 0, CFL_ALPHABET_SIZE * sizeof(*cost_u));
} else { const aom_cdf_prob *cdf_u = fc->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
av1_cost_tokens_from_cdf(cost_u, cdf_u, NULL);
} if (CFL_SIGN_V(joint_sign) == CFL_SIGN_ZERO) {
memset(cost_v, 0, CFL_ALPHABET_SIZE * sizeof(*cost_v));
} else { const aom_cdf_prob *cdf_v = fc->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
av1_cost_tokens_from_cdf(cost_v, cdf_v, NULL);
} for (int u = 0; u < CFL_ALPHABET_SIZE; u++)
cost_u[u] += sign_cost[joint_sign];
}
for (i = 0; i < MAX_TX_CATS; ++i) for (j = 0; j < TX_SIZE_CONTEXTS; ++j)
av1_cost_tokens_from_cdf(mode_costs->tx_size_cost[i][j],
fc->tx_size_cdf[i][j], NULL);
for (i = 0; i < TXFM_PARTITION_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->txfm_partition_cost[i],
fc->txfm_partition_cdf[i], NULL);
}
for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { int s; for (s = 1; s < EXT_TX_SETS_INTER; ++s) { if (use_inter_ext_tx_for_txsize[s][i]) {
av1_cost_tokens_from_cdf(
mode_costs->inter_tx_type_costs[s][i], fc->inter_ext_tx_cdf[s][i],
av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[1][s]]);
}
} for (s = 1; s < EXT_TX_SETS_INTRA; ++s) { if (use_intra_ext_tx_for_txsize[s][i]) { for (j = 0; j < INTRA_MODES; ++j) {
av1_cost_tokens_from_cdf(
mode_costs->intra_tx_type_costs[s][i][j],
fc->intra_ext_tx_cdf[s][i][j],
av1_ext_tx_inv[av1_ext_tx_set_idx_to_type[0][s]]);
}
}
}
} for (i = 0; i < DIRECTIONAL_MODES; ++i) {
av1_cost_tokens_from_cdf(mode_costs->angle_delta_cost[i],
fc->angle_delta_cdf[i], NULL);
}
av1_cost_tokens_from_cdf(mode_costs->intrabc_cost, fc->intrabc_cdf, NULL);
for (i = 0; i < SPATIAL_PREDICTION_PROBS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->spatial_pred_cost[i],
fc->seg.spatial_pred_seg_cdf[i], NULL);
}
for (i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->tmp_pred_cost[i], fc->seg.pred_cdf[i],
NULL);
}
if (!frame_is_intra_only(cm)) { for (i = 0; i < COMP_INTER_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->comp_inter_cost[i],
fc->comp_inter_cdf[i], NULL);
}
for (i = 0; i < REF_CONTEXTS; ++i) { for (j = 0; j < SINGLE_REFS - 1; ++j) {
av1_cost_tokens_from_cdf(mode_costs->single_ref_cost[i][j],
fc->single_ref_cdf[i][j], NULL);
}
}
for (i = 0; i < COMP_REF_TYPE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->comp_ref_type_cost[i],
fc->comp_ref_type_cdf[i], NULL);
}
for (i = 0; i < UNI_COMP_REF_CONTEXTS; ++i) { for (j = 0; j < UNIDIR_COMP_REFS - 1; ++j) {
av1_cost_tokens_from_cdf(mode_costs->uni_comp_ref_cost[i][j],
fc->uni_comp_ref_cdf[i][j], NULL);
}
}
for (i = 0; i < REF_CONTEXTS; ++i) { for (j = 0; j < FWD_REFS - 1; ++j) {
av1_cost_tokens_from_cdf(mode_costs->comp_ref_cost[i][j],
fc->comp_ref_cdf[i][j], NULL);
}
}
for (i = 0; i < REF_CONTEXTS; ++i) { for (j = 0; j < BWD_REFS - 1; ++j) {
av1_cost_tokens_from_cdf(mode_costs->comp_bwdref_cost[i][j],
fc->comp_bwdref_cdf[i][j], NULL);
}
}
for (i = 0; i < INTRA_INTER_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->intra_inter_cost[i],
fc->intra_inter_cdf[i], NULL);
}
for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->newmv_mode_cost[i], fc->newmv_cdf[i],
NULL);
}
for (i = 0; i < GLOBALMV_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->zeromv_mode_cost[i],
fc->zeromv_cdf[i], NULL);
}
for (i = 0; i < REFMV_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->refmv_mode_cost[i], fc->refmv_cdf[i],
NULL);
}
for (i = 0; i < DRL_MODE_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->drl_mode_cost0[i], fc->drl_cdf[i],
NULL);
} for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
av1_cost_tokens_from_cdf(mode_costs->inter_compound_mode_cost[i],
fc->inter_compound_mode_cdf[i], NULL); for (i = 0; i < BLOCK_SIZES_ALL; ++i)
av1_cost_tokens_from_cdf(mode_costs->compound_type_cost[i],
fc->compound_type_cdf[i], NULL); for (i = 0; i < BLOCK_SIZES_ALL; ++i) { if (av1_is_wedge_used(i)) {
av1_cost_tokens_from_cdf(mode_costs->wedge_idx_cost[i],
fc->wedge_idx_cdf[i], NULL);
}
} for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->interintra_cost[i],
fc->interintra_cdf[i], NULL);
av1_cost_tokens_from_cdf(mode_costs->interintra_mode_cost[i],
fc->interintra_mode_cdf[i], NULL);
} for (i = 0; i < BLOCK_SIZES_ALL; ++i) {
av1_cost_tokens_from_cdf(mode_costs->wedge_interintra_cost[i],
fc->wedge_interintra_cdf[i], NULL);
} for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
av1_cost_tokens_from_cdf(mode_costs->motion_mode_cost[i],
fc->motion_mode_cdf[i], NULL);
} for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
av1_cost_tokens_from_cdf(mode_costs->motion_mode_cost1[i],
fc->obmc_cdf[i], NULL);
} for (i = 0; i < COMP_INDEX_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->comp_idx_cost[i],
fc->compound_index_cdf[i], NULL);
} for (i = 0; i < COMP_GROUP_IDX_CONTEXTS; ++i) {
av1_cost_tokens_from_cdf(mode_costs->comp_group_idx_cost[i],
fc->comp_group_idx_cdf[i], NULL);
}
}
}
// Values are now correlated to quantizer. staticint sad_per_bit_lut_8[QINDEX_RANGE]; staticint sad_per_bit_lut_10[QINDEX_RANGE]; staticint sad_per_bit_lut_12[QINDEX_RANGE];
staticvoid init_me_luts_bd(int *bit16lut, int range,
aom_bit_depth_t bit_depth) { int i; // Initialize the sad lut tables using a formulaic calculation for now. // This is to make it easier to resolve the impact of experimental changes // to the quantizer tables. for (i = 0; i < range; i++) { constdouble q = av1_convert_qindex_to_q(i, bit_depth);
bit16lut[i] = (int)(0.0418 * q + 2.4107);
}
}
// Returns the default rd multiplier for inter frames for a given qindex. // The function here is a first pass estimate based on data from // a previous Vizer run staticdouble def_inter_rd_multiplier(int qindex) { return 3.2 + (0.0015 * (double)qindex);
}
// Returns the default rd multiplier for ARF/Golden Frames for a given qindex. // The function here is a first pass estimate based on data from // a previous Vizer run staticdouble def_arf_rd_multiplier(int qindex) { return 3.25 + (0.0015 * (double)qindex);
}
// Returns the default rd multiplier for key frames for a given qindex. // The function here is a first pass estimate based on data from // a previous Vizer run staticdouble def_kf_rd_multiplier(int qindex) { return 3.3 + (0.0015 * (double)qindex);
}
if (tuning == AOM_TUNE_SSIMULACRA2) { // Further multiply rdmult (by up to 200/128 = 1.5625) to improve image // quality. The most noticeable effect is a mild bias towards choosing // larger transform sizes (e.g. one 16x16 transform instead of 4 8x8 // transforms). // For very high qindexes, start progressively reducing the weight towards // unity (128/128), as transforms are large enough and making them even // larger actually harms subjective quality and SSIMULACRA 2 scores. // This weight part of the equation was determined by iteratively increasing // weight on CID22 and Daala's subset1, and observing its effects on visual // quality and SSIMULACRA 2 scores along the usable (0-100) range. // The ramp-down part of the equation was determined by choosing a fixed // initial qindex point [qindex 159 = (255 - 159) * 3 / 4] where SSIMULACRA // 2 scores for encodes with qindexes greater than 159 scored at or above // their equivalents with no rdmult adjustment. constint weight = clamp(((255 - qindex) * 3) / 4, 0, 72) + 128;
rdmult = (int64_t)((double)rdmult * weight / 128.0);
}
switch (bit_depth) { case AOM_BITS_8: break; case AOM_BITS_10: rdmult = ROUND_POWER_OF_TWO(rdmult, 4); break; case AOM_BITS_12: rdmult = ROUND_POWER_OF_TWO(rdmult, 8); break; default:
assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12"); return -1;
} return rdmult > 0 ? (int)AOMMIN(rdmult, INT_MAX) : 1;
}
for (bsize = 0; bsize < BLOCK_SIZES_ALL; ++bsize) { // Threshold here seems unnecessarily harsh but fine given actual // range of values used for cpi->sf.thresh_mult[]. constint t = q * rd_thresh_block_size_factor[bsize]; constint thresh_max = INT_MAX / t;
for (i = 0; i < num_modes_count; ++i) { constint mode_index = use_nonrd_pick_mode ? mode_indices[i] : i;
rd->threshes[segment_id][bsize][mode_index] =
rd->thresh_mult[mode_index] < thresh_max
? rd->thresh_mult[mode_index] * t / 4
: INT_MAX;
}
}
}
}
for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) { int br_rate[BR_CDF_SIZE]; int prev_cost = 0; int i, j;
av1_cost_tokens_from_cdf(
br_rate, fc->coeff_br_cdf[AOMMIN(tx_size, TX_32X32)][plane][ctx],
NULL); // printf("br_rate: "); // for(j = 0; j < BR_CDF_SIZE; j++) // printf("%4d ", br_rate[j]); // printf("\n"); for (i = 0; i < COEFF_BASE_RANGE; i += BR_CDF_SIZE - 1) { for (j = 0; j < BR_CDF_SIZE - 1; j++) {
pcost->lps_cost[ctx][i + j] = prev_cost + br_rate[j];
}
prev_cost += br_rate[j];
}
pcost->lps_cost[ctx][i] = prev_cost; // printf("lps_cost: %d %d %2d : ", tx_size, plane, ctx); // for (i = 0; i <= COEFF_BASE_RANGE; i++) // printf("%5d ", pcost->lps_cost[ctx][i]); // printf("\n");
} for (int ctx = 0; ctx < LEVEL_CONTEXTS; ++ctx) {
pcost->lps_cost[ctx][0 + COEFF_BASE_RANGE + 1] =
pcost->lps_cost[ctx][0]; for (int i = 1; i <= COEFF_BASE_RANGE; ++i) {
pcost->lps_cost[ctx][i + COEFF_BASE_RANGE + 1] =
pcost->lps_cost[ctx][i] - pcost->lps_cost[ctx][i - 1];
}
}
}
}
}
void av1_fill_mv_costs(const nmv_context *nmvc, int integer_mv, int usehp,
MvCosts *mv_costs) { // Avoid accessing 'mv_costs' when it is not allocated. if (mv_costs == NULL) return;
// Populates speed features based on codec control settings (of type // COST_UPDATE_TYPE) and expected speed feature settings (of type // INTERNAL_COST_UPDATE_TYPE) by considering the least frequent cost update. // The populated/updated speed features are used for cost updates in the // encoder. // WARNING: Population of unified cost update frequency needs to be taken care // accordingly, in case of any modifications/additions to the enum // COST_UPDATE_TYPE/INTERNAL_COST_UPDATE_TYPE. staticinlinevoid populate_unified_cost_update_freq( const CostUpdateFreq cost_upd_freq, SPEED_FEATURES *const sf) {
INTER_MODE_SPEED_FEATURES *const inter_sf = &sf->inter_sf; // Mapping of entropy cost update frequency from the encoder's codec control // settings of type COST_UPDATE_TYPE to speed features of type // INTERNAL_COST_UPDATE_TYPE. staticconst INTERNAL_COST_UPDATE_TYPE
map_cost_upd_to_internal_cost_upd[NUM_COST_UPDATE_TYPES] = {
INTERNAL_COST_UPD_SB, INTERNAL_COST_UPD_SBROW, INTERNAL_COST_UPD_TILE,
INTERNAL_COST_UPD_OFF
};
// Checks if entropy costs should be initialized/updated at frame level or not. staticinlineint is_frame_level_cost_upd_freq_set( const AV1_COMMON *const cm, const INTERNAL_COST_UPDATE_TYPE cost_upd_level, constint use_nonrd_pick_mode, constint frames_since_key) { constint fill_costs =
frame_is_intra_only(cm) ||
(use_nonrd_pick_mode ? frames_since_key < 2
: (cm->current_frame.frame_number & 0x07) == 1); return ((!use_nonrd_pick_mode && cost_upd_level != INTERNAL_COST_UPD_OFF) ||
cost_upd_level == INTERNAL_COST_UPD_TILE || fill_costs);
}
// Decide whether we want to update the mode entropy cost for the current frame. // The logit is currently inherited from selective_disable_cdf_rtc. staticinlineint should_force_mode_cost_update(const AV1_COMP *cpi) { const REAL_TIME_SPEED_FEATURES *const rt_sf = &cpi->sf.rt_sf; if (!rt_sf->frame_level_mode_cost_update) { returnfalse;
}
void av1_model_rd_from_var_lapndz(int64_t var, unsignedint n_log2, unsignedint qstep, int *rate,
int64_t *dist) { // This function models the rate and distortion for a Laplacian // source with given variance when quantized with a uniform quantizer // with given stepsize. The closed form expressions are in: // Hang and Chen, "Source Model for transform video coder and its // application - Part I: Fundamental Theory", IEEE Trans. Circ. // Sys. for Video Tech., April 1997. if (var == 0) {
*rate = 0;
*dist = 0;
} else { int d_q10, r_q10; staticconst uint32_t MAX_XSQ_Q10 = 245727; const uint64_t xsq_q10_64 =
(((uint64_t)qstep * qstep << (n_log2 + 10)) + (var >> 1)) / var; constint xsq_q10 = (int)AOMMIN(xsq_q10_64, MAX_XSQ_Q10);
model_rd_norm(xsq_q10, &r_q10, &d_q10);
*rate = ROUND_POWER_OF_TWO(r_q10 << n_log2, 10 - AV1_PROB_COST_SHIFT);
*dist = (var * (int64_t)d_q10 + 512) >> 10;
}
}
// Special clamping used in the encoder when calculating a prediction // // Logically, all pixel fetches used for prediction are clamped against the // edges of the frame. But doing this directly is slow, so instead we allocate // a finite border around the frame and fill it with copies of the outermost // pixels. // // Since this border is finite, we need to clamp the motion vector before // prediction in order to avoid out-of-bounds reads. At the same time, this // clamp must not change the prediction result. // // We can balance both of these concerns by calculating how far we would have // to go in each direction before the extended prediction region (the current // block + AOM_INTERP_EXTEND many pixels around the block) would be mapped // so that it touches the frame only at one row or column. This is a special // point because any more extreme MV will always lead to the same prediction. // So it is safe to clamp at that point. // // In the worst case, this requires a border of // max_block_width + 2*AOM_INTERP_EXTEND = 128 + 2*4 = 136 pixels // around the frame edges. staticinlinevoid enc_clamp_mv(const AV1_COMMON *cm, const MACROBLOCKD *xd,
MV *mv) { int bw = xd->width << MI_SIZE_LOG2; int bh = xd->height << MI_SIZE_LOG2;
int px_to_left_edge = xd->mi_col << MI_SIZE_LOG2; int px_to_right_edge = (cm->mi_params.mi_cols - xd->mi_col) << MI_SIZE_LOG2; int px_to_top_edge = xd->mi_row << MI_SIZE_LOG2; int px_to_bottom_edge = (cm->mi_params.mi_rows - xd->mi_row) << MI_SIZE_LOG2;
const uint8_t *const src_y_ptr = x->plane[0].src.buf; int zero_seen = 0; int best_sad = INT_MAX; int max_mv = 0; // Get the sad for each candidate reference mv. for (int i = 0; i < num_mv_refs; ++i) {
MV *this_mv = &pred_mv[i];
enc_clamp_mv(&cpi->common, &x->e_mbd, this_mv);
const uint8_t *const ref_y_ptr =
&ref_y_buffer[ref_y_stride * fp_row + fp_col]; // Find sad for current vector. constint this_sad = cpi->ppi->fn_ptr[block_size].sdf(
src_y_ptr, x->plane[0].src.stride, ref_y_ptr, ref_y_stride); // Note if it is the best so far. if (this_sad < best_sad) {
best_sad = this_sad;
} if (i == 0)
x->pred_mv0_sad[ref_frame] = this_sad; elseif (i == 1)
x->pred_mv1_sad[ref_frame] = this_sad;
}
// Note the index of the mv that worked best in the reference list.
x->max_mv_context[ref_frame] = max_mv;
x->pred_mv_sad[ref_frame] = best_sad;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.