/* * 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.
*/
staticvoid auto_tile_size_balancing(AV1_COMMON *const cm, int num_sbs, int num_tiles_lg, int tile_col_row) {
CommonTileParams *const tiles = &cm->tiles; int i, start_sb; int size_sb = num_sbs >> num_tiles_lg; int res_sbs = num_sbs - (size_sb << num_tiles_lg); int num_tiles = 1 << num_tiles_lg; int inc_index = num_tiles - res_sbs;
tiles->uniform_spacing = 0;
for (i = 0, start_sb = 0; start_sb < num_sbs && i < MAX_TILE_COLS; ++i) { if (i == inc_index) ++size_sb; if (tile_col_row)
tiles->col_start_sb[i] = start_sb; else
tiles->row_start_sb[i] = start_sb;
// Setup mi_params here in case we need more mi's.
CommonModeInfoParams *const mi_params = &cm->mi_params;
mi_params->set_mb_mi(mi_params, cm->width, cm->height,
cpi->sf.part_sf.default_min_partition_size);
av1_init_macroblockd(cm, xd);
if (!cpi->ppi->seq_params_locked)
set_sb_size(cm->seq_params,
av1_select_sb_size(&cpi->oxcf, cm->width, cm->height,
cpi->ppi->number_spatial_layers));
staticvoid set_bitstream_level_tier(AV1_PRIMARY *const ppi, int width, int height, double init_framerate) {
SequenceHeader *const seq_params = &ppi->seq_params; const AV1LevelParams *const level_params = &ppi->level_params; // TODO(any): This is a placeholder function that only addresses dimensions // and max display sample rates. // Need to add checks for max bit rate, max decoded luma sample rate, header // rate, etc. that are not covered by this function.
AV1_LEVEL level = SEQ_LEVEL_MAX; if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) {
level = SEQ_LEVEL_2_0;
} elseif (does_level_match(width, height, init_framerate, 704, 396, 30.0,
4)) {
level = SEQ_LEVEL_2_1;
} elseif (does_level_match(width, height, init_framerate, 1088, 612, 30.0,
4)) {
level = SEQ_LEVEL_3_0;
} elseif (does_level_match(width, height, init_framerate, 1376, 774, 30.0,
4)) {
level = SEQ_LEVEL_3_1;
} elseif (does_level_match(width, height, init_framerate, 2048, 1152, 30.0,
3)) {
level = SEQ_LEVEL_4_0;
} elseif (does_level_match(width, height, init_framerate, 2048, 1152, 60.0,
3)) {
level = SEQ_LEVEL_4_1;
} elseif (does_level_match(width, height, init_framerate, 4096, 2176, 30.0,
2)) {
level = SEQ_LEVEL_5_0;
} elseif (does_level_match(width, height, init_framerate, 4096, 2176, 60.0,
2)) {
level = SEQ_LEVEL_5_1;
} elseif (does_level_match(width, height, init_framerate, 4096, 2176, 120.0,
2)) {
level = SEQ_LEVEL_5_2;
} elseif (does_level_match(width, height, init_framerate, 8192, 4352, 30.0,
2)) {
level = SEQ_LEVEL_6_0;
} elseif (does_level_match(width, height, init_framerate, 8192, 4352, 60.0,
2)) {
level = SEQ_LEVEL_6_1;
} elseif (does_level_match(width, height, init_framerate, 8192, 4352, 120.0,
2)) {
level = SEQ_LEVEL_6_2;
} #if CONFIG_CWG_C013 // TODO(bohanli): currently target level is only working for the 0th operating // point, so scalable coding is not supported. elseif (level_params->target_seq_level_idx[0] >= SEQ_LEVEL_7_0 &&
level_params->target_seq_level_idx[0] <= SEQ_LEVEL_8_3) { // Only use level 7.x to 8.x when explicitly asked to. if (does_level_match(width, height, init_framerate, 16384, 8704, 30.0, 2)) {
level = SEQ_LEVEL_7_0;
} elseif (does_level_match(width, height, init_framerate, 16384, 8704,
60.0, 2)) {
level = SEQ_LEVEL_7_1;
} elseif (does_level_match(width, height, init_framerate, 16384, 8704,
120.0, 2)) {
level = SEQ_LEVEL_7_2;
} elseif (does_level_match(width, height, init_framerate, 32768, 17408,
30.0, 2)) {
level = SEQ_LEVEL_8_0;
} elseif (does_level_match(width, height, init_framerate, 32768, 17408,
60.0, 2)) {
level = SEQ_LEVEL_8_1;
} elseif (does_level_match(width, height, init_framerate, 32768, 17408,
120.0, 2)) {
level = SEQ_LEVEL_8_2;
}
} #endif
for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
assert(is_valid_seq_level_idx(level_params->target_seq_level_idx[i]) ||
level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS); // If a higher target level is specified, it is then used rather than the // inferred one from resolution and framerate.
seq_params->seq_level_idx[i] =
level_params->target_seq_level_idx[i] < SEQ_LEVELS &&
level_params->target_seq_level_idx[i] > level
? level_params->target_seq_level_idx[i]
: level; // Set the maximum parameters for bitrate and buffer size for this profile, // level, and tier
seq_params->op_params[i].bitrate = av1_max_level_bitrate(
seq_params->profile, seq_params->seq_level_idx[i], seq_params->tier[i]); // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the // check if (seq_params->op_params[i].bitrate == 0)
aom_internal_error(
&ppi->error, AOM_CODEC_UNSUP_BITSTREAM, "AV1 does not support this combination of profile, level, and tier."); // Buffer size in bits/s is bitrate in bits/s * 1 s
seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate;
}
}
int sb_size = seq_params->sb_size; // Superblock size should not be updated after the first key frame. if (!ppi->seq_params_locked) {
set_sb_size(seq_params, av1_select_sb_size(oxcf, frm_dim_cfg->width,
frm_dim_cfg->height,
ppi->number_spatial_layers)); for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
} if (is_sb_size_changed != NULL && sb_size != seq_params->sb_size)
*is_sb_size_changed = true;
// Init sequence level coding tools // This should not be called after the first key frame. if (!ppi->seq_params_locked) {
seq_params->operating_points_cnt_minus_1 =
(ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1)
? ppi->number_spatial_layers * ppi->number_temporal_layers - 1
: 0;
init_seq_coding_tools(ppi, oxcf,
ppi->use_svc || ppi->rtc_ref.set_ref_frame_config);
}
seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr;
// in case of LAP, lag in frames is set according to number of lap buffers // calculated at init time. This stores and restores LAP's lag in frames to // prevent override by new cfg. int lap_lag_in_frames = -1; if (cpi->ppi->lap_enabled && cpi->compressor_stage == LAP_STAGE) {
lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames;
}
// When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure // that any analysis (e.g. TPL) happening outside the main encoding loop still // happens at full resolution. // This value will later be set appropriately just before main encoding loop.
cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO
? AOM_SUPERRES_NONE
: oxcf->superres_cfg.superres_mode; // default
x->e_mbd.bd = (int)seq_params->bit_depth;
x->e_mbd.global_motion = cm->global_motion;
memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx, sizeof(level_params->target_seq_level_idx));
level_params->keep_level_stats = 0; for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) { if (level_params->target_seq_level_idx[i] < SEQ_LEVELS ||
level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS) {
level_params->keep_level_stats |= 1u << i; if (!level_params->level_info[i]) {
CHECK_MEM_ERROR(cm, level_params->level_info[i],
aom_calloc(1, sizeof(*level_params->level_info[i])));
}
}
}
// TODO(huisu@): level targeting currently only works for the 0th operating // point, so scalable coding is not supported yet. if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) { // Adjust encoder config in order to meet target level.
config_target_level(cpi, level_params->target_seq_level_idx[0],
seq_params->tier[0]);
}
if (has_no_stats_stage(cpi) && (rc_cfg->mode == AOM_Q)) {
p_rc->baseline_gf_interval = FIXED_GF_INTERVAL;
} elseif (!is_one_pass_rt_params(cpi) ||
cm->current_frame.frame_number == 0) { // For rtc mode: logic for setting the baseline_gf_interval is done // in av1_get_one_pass_rt_params(), and it should not be reset here in // change_config(), unless after init_config (first frame).
p_rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
}
if (x->palette_buffer == NULL) {
CHECK_MEM_ERROR(cm, x->palette_buffer,
aom_memalign(16, sizeof(*x->palette_buffer)));
}
if (x->tmp_conv_dst == NULL) {
CHECK_MEM_ERROR(
cm, x->tmp_conv_dst,
aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
} // The buffers 'tmp_pred_bufs[]' and 'comp_rd_buffer' are used in inter frames // to store intermediate inter mode prediction results and are not required // for allintra encoding mode. Hence, the memory allocations for these buffers // are avoided for allintra encoding mode. if (cpi->oxcf.kf_cfg.key_freq_max != 0) { if (x->comp_rd_buffer.pred0 == NULL)
alloc_compound_type_rd_buffers(cm->error, &x->comp_rd_buffer);
for (int i = 0; i < 2; ++i) { if (x->tmp_pred_bufs[i] == NULL) {
CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i],
aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE * sizeof(*x->tmp_pred_bufs[i])));
x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i];
}
}
}
av1_reset_segment_features(cm);
av1_set_high_precision_mv(cpi, 1, 0);
// Under a configuration change, where maximum_buffer_size may change, // keep buffer level clipped to the maximum allowed buffer size.
p_rc->bits_off_target =
AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size);
p_rc->buffer_level = AOMMIN(p_rc->buffer_level, p_rc->maximum_buffer_size);
// Set up frame rate and related parameters rate control values.
av1_new_framerate(cpi, cpi->framerate);
// Set absolute upper and lower quality limits
rc->worst_quality = rc_cfg->worst_allowed_q;
rc->best_quality = rc_cfg->best_allowed_q;
// If lossless has been requested make sure average Q accumulators are reset. if (is_lossless_requested(&cpi->oxcf.rc_cfg)) { int i; for (i = 0; i < FRAME_TYPES; ++i) {
p_rc->avg_frame_qindex[i] = 0;
}
}
AV1_PRIMARY *av1_create_primary_compressor( struct aom_codec_pkt_list *pkt_list_head, int num_lap_buffers, const AV1EncoderConfig *oxcf) {
AV1_PRIMARY *volatileconst ppi = aom_memalign(32, sizeof(AV1_PRIMARY)); if (!ppi) return NULL;
av1_zero(*ppi);
// The jmp_buf is valid only for the duration of the function that calls // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 // before it returns. if (setjmp(ppi->error.jmp)) {
ppi->error.setjmp = 0;
av1_remove_primary_compressor(ppi); return 0;
}
ppi->error.setjmp = 1;
{ // As cm->mi_params is a part of the frame level context (cpi), it is // unavailable at this point. mi_params is created as a local temporary // variable, to be passed into the functions used for allocating tpl // buffers. The values in this variable are populated according to initial // width and height of the frame.
CommonModeInfoParams mi_params;
enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
BLOCK_4X4);
const BLOCK_SIZE bsize = BLOCK_16X16; constint w = mi_size_wide[bsize]; constint h = mi_size_high[bsize]; constint num_cols = (mi_params.mi_cols + w - 1) / w; constint num_rows = (mi_params.mi_rows + h - 1) / h;
AOM_CHECK_MEM_ERROR(
&ppi->error, ppi->tpl_sb_rdmult_scaling_factors,
aom_calloc(num_rows * num_cols, sizeof(*ppi->tpl_sb_rdmult_scaling_factors)));
// The jmp_buf is valid only for the duration of the function that calls // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 // before it returns. if (setjmp(cm->error->jmp)) {
cm->error->setjmp = 0;
av1_remove_compressor(cpi); return NULL;
}
if (!cpi->ppi->lap_enabled) { /*Re-initialize to stats buffer, populated by application in the case of
* two pass*/
cpi->ppi->twopass.stats_buf_ctx->stats_in_start =
oxcf->twopass_stats_in.buf;
cpi->twopass_frame.stats_in =
cpi->ppi->twopass.stats_buf_ctx->stats_in_start;
cpi->ppi->twopass.stats_buf_ctx->stats_in_end =
&cpi->ppi->twopass.stats_buf_ctx->stats_in_start[packets - 1];
// The buffer size is packets - 1 because the last packet is total_stats.
av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info,
oxcf->twopass_stats_in.buf, packets - 1);
av1_init_second_pass(cpi);
} else {
av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info, NULL, 0);
av1_init_single_pass_lap(cpi);
}
} #endif
// The buffer "obmc_buffer" is used in inter frames for fast obmc search. // Hence, the memory allocation for the same is avoided for allintra encoding // mode. if (cpi->oxcf.kf_cfg.key_freq_max != 0)
alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm->error);
for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++)
CHECK_MEM_ERROR(
cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
(uint32_t *)aom_malloc(
AOM_BUFFER_SIZE_FOR_BLOCK_HASH * sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0])));
#if CONFIG_TUNE_VMAF
{ const BLOCK_SIZE bsize = BLOCK_64X64; constint w = mi_size_wide[bsize]; constint h = mi_size_high[bsize]; constint num_cols = (mi_params->mi_cols + w - 1) / w; constint num_rows = (mi_params->mi_rows + h - 1) / h;
CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors,
aom_calloc(num_rows * num_cols, sizeof(*cpi->vmaf_info.rdmult_scaling_factors))); for (int i = 0; i < MAX_ARF_LAYERS; i++) {
cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0;
cpi->vmaf_info.last_frame_ysse[i] = -1.0;
cpi->vmaf_info.last_frame_vmaf[i] = -1.0;
}
cpi->vmaf_info.original_qindex = -1;
cpi->vmaf_info.vmaf_model = NULL;
} #endif
#if CONFIG_TUNE_BUTTERAUGLI
{ constint w = mi_size_wide[butteraugli_rdo_bsize]; constint h = mi_size_high[butteraugli_rdo_bsize]; constint num_cols = (mi_params->mi_cols + w - 1) / w; constint num_rows = (mi_params->mi_rows + h - 1) / h;
CHECK_MEM_ERROR(
cm, cpi->butteraugli_info.rdmult_scaling_factors,
aom_malloc(num_rows * num_cols * sizeof(*cpi->butteraugli_info.rdmult_scaling_factors)));
memset(&cpi->butteraugli_info.source, 0, sizeof(cpi->butteraugli_info.source));
memset(&cpi->butteraugli_info.resized_source, 0, sizeof(cpi->butteraugli_info.resized_source));
cpi->butteraugli_info.recon_set = false;
} #endif
#if CONFIG_SALIENCY_MAP
{
CHECK_MEM_ERROR(cm, cpi->saliency_map,
(uint8_t *)aom_calloc(cm->height * cm->width, sizeof(*cpi->saliency_map))); // Buffer initialization based on MIN_MIB_SIZE_LOG2 to ensure that // cpi->sm_scaling_factor buffer is allocated big enough, since we have no // idea of the actual superblock size we are going to use yet. constint min_mi_w_sb = (1 << MIN_MIB_SIZE_LOG2); constint min_mi_h_sb = (1 << MIN_MIB_SIZE_LOG2); constint max_sb_cols =
(cm->mi_params.mi_cols + min_mi_w_sb - 1) / min_mi_w_sb; constint max_sb_rows =
(cm->mi_params.mi_rows + min_mi_h_sb - 1) / min_mi_h_sb;
CHECK_MEM_ERROR(cm, cpi->sm_scaling_factor,
(double *)aom_calloc(max_sb_rows * max_sb_cols, sizeof(*cpi->sm_scaling_factor)));
} #endif
// Initialize the members of DeltaQuantParams with INT_MAX to ensure that // the quantizer tables are correctly initialized using the default deltaq // parameters when av1_init_quantizer is called for the first time.
DeltaQuantParams *const prev_deltaq_params =
&cpi->enc_quant_dequant_params.prev_deltaq_params;
prev_deltaq_params->y_dc_delta_q = INT_MAX;
prev_deltaq_params->u_dc_delta_q = INT_MAX;
prev_deltaq_params->v_dc_delta_q = INT_MAX;
prev_deltaq_params->u_ac_delta_q = INT_MAX;
prev_deltaq_params->v_ac_delta_q = INT_MAX;
// Default based on max resolution.
mv_search_params->mv_step_param = av1_init_search_range(max_mv_def);
if (cpi->sf.mv_sf.auto_mv_step_size) { if (frame_is_intra_only(cm)) { // Initialize max_mv_magnitude for use in the first INTER frame // after a key/intra-only frame.
mv_search_params->max_mv_magnitude = max_mv_def;
} else { // Use adaptive mv steps based on previous frame stats for show frames and // internal arfs.
FRAME_UPDATE_TYPE cur_update_type =
cpi->ppi->gf_group.update_type[cpi->gf_frame_index]; int use_auto_mv_step =
(cm->show_frame || cur_update_type == INTNL_ARF_UPDATE) &&
mv_search_params->max_mv_magnitude != -1 &&
cpi->sf.mv_sf.auto_mv_step_size >= 2; if (use_auto_mv_step) { // Allow mv_steps to correspond to twice the max mv magnitude found // in the previous frame, capped by the default max_mv_magnitude based // on resolution.
mv_search_params->mv_step_param = av1_init_search_range(
AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude));
} // Reset max_mv_magnitude based on update flag. if (cpi->do_frame_data_update) mv_search_params->max_mv_magnitude = -1;
}
}
}
// Screen content tools are not evaluated in non-RD encoding mode unless // content type is not set explicitly, i.e., when // cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN, use_nonrd_pick_mode = 1 // and hybrid_intra_pickmode = 0. Hence, screen content detection is // disabled. if (cpi->sf.rt_sf.use_nonrd_pick_mode &&
!cpi->sf.rt_sf.hybrid_intra_pickmode) {
features->allow_screen_content_tools = features->allow_intrabc = 0; return;
}
// Estimate if the source frame is screen content, based on the portion of // blocks that have few luma colors. const uint8_t *src = cpi->unfiltered_source->y_buffer;
assert(src != NULL); constint use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH; constint stride = cpi->unfiltered_source->y_stride; constint width = cpi->unfiltered_source->y_width; constint height = cpi->unfiltered_source->y_height; const int64_t area = (int64_t)width * height; constint bd = cm->seq_params->bit_depth; constint blk_w = 16; constint blk_h = 16; // These threshold values are selected experimentally. constint color_thresh = 4; constunsignedint var_thresh = 0; // Counts of blocks with no more than color_thresh colors.
int64_t counts_1 = 0; // Counts of blocks with no more than color_thresh colors and variance larger // than var_thresh.
int64_t counts_2 = 0;
for (int r = 0; r + blk_h <= height; r += blk_h) { for (int c = 0; c + blk_w <= width; c += blk_w) { int count_buf[1 << 8]; // Maximum (1 << 8) bins for hbd path. const uint8_t *const this_src = src + r * stride + c; int n_colors; if (use_hbd)
av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL,
count_buf, &n_colors, NULL); else
av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors); if (n_colors > 1 && n_colors <= color_thresh) {
++counts_1; struct buf_2d buf;
buf.stride = stride;
buf.buf = (uint8_t *)this_src; constunsignedint var = av1_get_perpixel_variance(
cpi, xd, &buf, BLOCK_16X16, AOM_PLANE_Y, use_hbd); if (var > var_thresh) ++counts_2;
}
}
}
// The threshold values are selected experimentally.
features->allow_screen_content_tools = counts_1 * blk_h * blk_w * 10 > area; // IntraBC would force loop filters off, so we use more strict rules that also // requires that the block has high variance.
features->allow_intrabc = features->allow_screen_content_tools &&
counts_2 * blk_h * blk_w * 12 > area;
cpi->use_screen_content_tools = features->allow_screen_content_tools;
cpi->is_screen_content_type =
features->allow_intrabc || (counts_1 * blk_h * blk_w * 10 > area * 4 &&
counts_2 * blk_h * blk_w * 30 > area);
}
// Update if search_site_cfg is uninitialized or the current frame has a new // stride constint should_update =
!mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride ||
!mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride ||
(y_stride !=
mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride);
if (!should_update) { return;
}
// Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS. for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) { constint level = ((i == NSTEP_8PT) || (i == CLAMPED_DIAMOND)) ? 1 : 0;
av1_init_motion_compensation[i](
&mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride, level);
av1_init_motion_compensation[i](
&mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src,
level);
}
// First pass search site config initialization.
av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND],
fpf_y_stride); for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) {
memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i],
&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND], sizeof(search_site_config));
}
}
staticvoid init_ref_frame_bufs(AV1_COMP *cpi) {
AV1_COMMON *const cm = &cpi->common; int i; if (cm->cur_frame) {
cm->cur_frame->ref_count--;
cm->cur_frame = NULL;
} for (i = 0; i < REF_FRAMES; ++i) { if (cm->ref_frame_map[i]) {
cm->ref_frame_map[i]->ref_count--;
cm->ref_frame_map[i] = NULL;
}
} #ifndef NDEBUG
BufferPool *const pool = cm->buffer_pool; for (i = 0; i < pool->num_frame_bufs; ++i) {
assert(pool->frame_bufs[i].ref_count == 0);
} #endif
}
// TODO(chengchen): consider renaming this function as it is necessary // for the encoder to setup critical parameters, and it does not // deal with initial width any longer.
aom_codec_err_t av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth, int subsampling_x, int subsampling_y) {
AV1_COMMON *const cm = &cpi->common;
SequenceHeader *const seq_params = cm->seq_params;
void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
AV1_COMMON *const cm = &cpi->common; const SequenceHeader *const seq_params = cm->seq_params; constint num_planes = av1_num_planes(cm);
MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; int ref_frame;
if (width != cm->width || height != cm->height) { // There has been a change in the encoded frame size
set_size_literal(cpi, width, height); // Recalculate 'all_lossless' in case super-resolution was (un)selected.
cm->features.all_lossless =
cm->features.coded_lossless && !av1_superres_scaled(cm);
av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height); #if CONFIG_AV1_TEMPORAL_DENOISING // Reset the denoiser on the resized frame. if (cpi->oxcf.noise_sensitivity > 0) {
av1_denoiser_free(&(cpi->denoiser));
setup_denoiser_buffer(cpi);
} #endif
} if (is_stat_consumption_stage(cpi)) {
av1_set_target_rate(cpi, cm->width, cm->height);
}
staticinlineint extend_borders_mt(const AV1_COMP *cpi,
MULTI_THREADED_MODULES stage, int plane) { const AV1_COMMON *const cm = &cpi->common; if (cpi->mt_info.num_mod_workers[stage] < 2) return 0; switch (stage) { // TODO(deepa.kg@ittiam.com): When cdef and loop-restoration are disabled, // multi-thread frame border extension along with loop filter frame. // As loop-filtering of a superblock row modifies the pixels of the // above superblock row, border extension requires that loop filtering // of the current and above superblock row is complete. case MOD_LPF: return 0; case MOD_CDEF: return is_cdef_used(cm) && !cpi->ppi->rtc_ref.non_reference_frame &&
!is_restoration_used(cm) && !av1_superres_scaled(cm); case MOD_LR: return is_restoration_used(cm) &&
(cm->rst_info[plane].frame_restoration_type != RESTORE_NONE); default: assert(0);
} return 0;
}
/*!\brief Select and apply cdef filters and switchable restoration filters * * \ingroup high_level_algo
*/ staticvoid cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
MACROBLOCKD *xd, int use_restoration, int use_cdef, unsignedint skip_apply_postproc_filters) { #if !CONFIG_REALTIME_ONLY if (use_restoration)
av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0); #else
(void)use_restoration; #endif
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, loop_filter_time); #endif if (use_loopfilter) {
av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick); struct loopfilter *lf = &cm->lf; if ((lf->filter_level[0] || lf->filter_level[1]) &&
(skip_apply_postproc_filters & SKIP_APPLY_LOOPFILTER) == 0) {
assert(!cpi->ppi->rtc_ref.non_reference_frame); // lpf_opt_level = 1 : Enables dual/quad loop-filtering. // lpf_opt_level is set to 1 if transform size search depth in inter // blocks is limited to one as quad loop filtering assumes that all the // transform blocks within a 16x8/8x16/16x16 prediction block are of the // same size. lpf_opt_level = 2 : Filters both chroma planes together, in // addition to enabling dual/quad loop-filtering. This is enabled when lpf // pick method is LPF_PICK_FROM_Q as u and v plane filter levels are // equal. int lpf_opt_level = get_lpf_opt_level(&cpi->sf);
av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
mt_info->workers, num_workers,
&mt_info->lf_row_sync, lpf_opt_level);
}
}
if (cpi->sf.rt_sf.use_temporal_noise_estimate) {
av1_update_noise_estimate(cpi);
}
#if CONFIG_AV1_TEMPORAL_DENOISING if (cpi->oxcf.noise_sensitivity > 0 && cpi->ppi->use_svc)
av1_denoiser_reset_on_first_frame(cpi); #endif
// For 1 spatial layer encoding: if the (non-LAST) reference has different // resolution from the source then disable that reference. This is to avoid // significant increase in encode time from scaling the references in // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger) // resized frame and ALTREF will be refreshed ~4 frames later, so both // references become available again after few frames. // For superres: don't disable golden reference. if (svc->number_spatial_layers == 1) { if (!cpi->oxcf.superres_cfg.enable_superres) { if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) { const YV12_BUFFER_CONFIG *const ref =
get_ref_frame_yv12_buf(cm, GOLDEN_FRAME); if (ref == NULL || ref->y_crop_width != cm->width ||
ref->y_crop_height != cm->height) {
cpi->ref_frame_flags ^= AOM_GOLD_FLAG;
}
}
} if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) { const YV12_BUFFER_CONFIG *const ref =
get_ref_frame_yv12_buf(cm, ALTREF_FRAME); if (ref == NULL || ref->y_crop_width != cm->width ||
ref->y_crop_height != cm->height) {
cpi->ref_frame_flags ^= AOM_ALT_FLAG;
}
}
}
av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq,
cpi->oxcf.mode == ALLINTRA, cpi->oxcf.tune_cfg.tuning);
av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
cm->seq_params->bit_depth);
av1_set_variance_partition_thresholds(cpi, q, 0);
av1_setup_frame(cpi);
// Check if this high_source_sad (scene/slide change) frame should be // encoded at high/max QP, and if so, set the q and adjust some rate // control parameters. if (cpi->sf.rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ &&
cpi->rc.high_source_sad) { if (av1_encodedframe_overshoot_cbr(cpi, &q)) {
av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q,
q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq,
cpi->oxcf.mode == ALLINTRA, cpi->oxcf.tune_cfg.tuning);
av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed);
av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params,
cm->seq_params->bit_depth);
av1_set_variance_partition_thresholds(cpi, q, 0); if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
cm->features.primary_ref_frame == PRIMARY_REF_NONE)
av1_setup_frame(cpi);
}
}
av1_apply_active_map(cpi); if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) av1_cyclic_refresh_setup(cpi); if (cm->seg.enabled) { if (!cm->seg.update_data && cm->prev_frame) {
segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
cm->seg.enabled = cm->prev_frame->seg.enabled;
} else {
av1_calculate_segdata(&cm->seg);
}
} else {
memset(&cm->seg, 0, sizeof(cm->seg));
}
segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
cm->cur_frame->seg.enabled = cm->seg.enabled;
// This is for rtc temporal filtering case. if (is_psnr_calc_enabled(cpi) && cpi->sf.rt_sf.use_rtc_tf) { const SequenceHeader *seq_params = cm->seq_params;
if (cpi->orig_source.buffer_alloc_sz == 0 ||
cpi->rc.prev_coded_width != cpi->oxcf.frm_dim_cfg.width ||
cpi->rc.prev_coded_height != cpi->oxcf.frm_dim_cfg.height) { // Allocate a source buffer to store the true source for psnr calculation. if (aom_alloc_frame_buffer(
&cpi->orig_source, cpi->oxcf.frm_dim_cfg.width,
cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x,
seq_params->subsampling_y, seq_params->use_highbitdepth,
cpi->oxcf.border_in_pixels, cm->features.byte_alignment, false,
0))
aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, "Failed to allocate scaled buffer");
}
// Set the motion vector precision based on mv stats from the last coded // frame. if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q);
if (!cpi->rc.rtc_external_ratectrl && !frame_is_intra_only(cm))
update_motion_stat(cpi);
// Adjust the refresh of the golden (longer-term) reference based on QP // selected for this frame. This is for CBR real-time mode, and only // for single layer without usage of the set_ref_frame_config (so // reference structure for 1 layer is set internally). if (!frame_is_intra_only(cm) && cpi->oxcf.rc_cfg.mode == AOM_CBR &&
cpi->oxcf.mode == REALTIME && svc->number_spatial_layers == 1 &&
svc->number_temporal_layers == 1 && !cpi->rc.rtc_external_ratectrl &&
!cpi->ppi->rtc_ref.set_ref_frame_config &&
sf->rt_sf.gf_refresh_based_on_qp)
av1_adjust_gf_refresh_qp_one_pass_rt(cpi);
/*!\brief Recode loop for encoding one frame. the purpose of encoding one frame * for multiple times can be approaching a target bitrate or adjusting the usage * of global motions. * * \ingroup high_level_algo * * \param[in] cpi Top-level encoder structure * \param[in] size Bitstream size * \param[out] dest Bitstream output buffer * \param[in] dest_size Bitstream output buffer size * * \return Returns a value to indicate if the encoding is done successfully. * \retval #AOM_CODEC_OK * \retval -1 * \retval #AOM_CODEC_ERROR
*/ staticint encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest,
size_t dest_size) {
AV1_COMMON *const cm = &cpi->common;
RATE_CONTROL *const rc = &cpi->rc;
GlobalMotionInfo *const gm_info = &cpi->gm_info; const AV1EncoderConfig *const oxcf = &cpi->oxcf; const QuantizationCfg *const q_cfg = &oxcf->q_cfg; constint allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE); // Must allow recode if minimum compression ratio is set.
assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode));
set_size_independent_vars(cpi); if (is_stat_consumption_stage_twopass(cpi) &&
cpi->sf.interp_sf.adaptive_interp_filter_search)
cpi->interp_search_flags.interp_filter_search_mask =
av1_setup_interp_filter_search_mask(cpi);
av1_setup_frame_size(cpi);
if (av1_superres_in_recode_allowed(cpi) &&
cpi->superres_mode != AOM_SUPERRES_NONE &&
cm->superres_scale_denominator == SCALE_NUMERATOR) { // Superres mode is currently enabled, but the denominator selected will // disable superres. So no need to continue, as we will go through another // recode loop for full-resolution after this anyway. return -1;
}
if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION)
variance_partition_alloc(cpi);
if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi);
#if CONFIG_COLLECT_COMPONENT_TIMING
printf("\n Encoding a frame: \n"); #endif
#if !CONFIG_RD_COMMAND // Determine whether to use screen content tools using two fast encoding. if (!cpi->sf.hl_sf.disable_extra_sc_testing && !cpi->use_ducky_encode)
av1_determine_sc_tools_with_encoding(cpi, q); #endif// !CONFIG_RD_COMMAND
#if CONFIG_TUNE_BUTTERAUGLI
cpi->butteraugli_info.recon_set = false; int original_q = 0; #endif
cpi->num_frame_recode = 0;
// Loop variables int loop = 0; int loop_count = 0; int overshoot_seen = 0; int undershoot_seen = 0; int low_cr_seen = 0; int last_loop_allow_hp = 0;
do {
loop = 0; int do_mv_stats_collection = 1;
// if frame was scaled calculate global_motion_search again if already // done if (loop_count > 0 && cpi->source && gm_info->search_done) { if (cpi->source->y_crop_width != cm->width ||
cpi->source->y_crop_height != cm->height) {
gm_info->search_done = 0;
}
}
cpi->source = av1_realloc_and_scale_if_required(
cm, cpi->unscaled_source, &cpi->scaled_source, EIGHTTAP_REGULAR, 0, false, false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
#if CONFIG_TUNE_BUTTERAUGLI if (oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) { if (loop_count == 0) {
original_q = q; // TODO(sdeng): different q here does not make big difference. Use a // faster pass instead.
q = 96;
av1_setup_butteraugli_source(cpi);
} else {
q = original_q;
}
} #endif
if (cpi->unscaled_last_source != NULL) {
cpi->last_source = av1_realloc_and_scale_if_required(
cm, cpi->unscaled_last_source, &cpi->scaled_last_source,
EIGHTTAP_REGULAR, 0, false, false, cpi->oxcf.border_in_pixels,
cpi->alloc_pyramid);
}
int scale_references = 0; #if CONFIG_FPMT_TEST
scale_references =
cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0; #endif// CONFIG_FPMT_TEST if (scale_references ||
cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) { if (!frame_is_intra_only(cm)) { if (loop_count > 0) {
release_scaled_references(cpi);
}
av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0);
}
}
if (loop_count == 0) {
av1_setup_frame(cpi);
} elseif (get_primary_ref_frame_buf(cm) == NULL) { // Base q-index may have changed, so we need to assign proper default coef // probs before every iteration.
av1_default_coef_probs(cm);
av1_setup_frame_contexts(cm);
}
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, av1_encode_frame_time); #endif // Set the motion vector precision based on mv stats from the last coded // frame. if (!frame_is_intra_only(cm)) {
av1_pick_and_set_high_precision_mv(cpi, q);
// If the precision has changed during different iteration of the loop, // then we need to reset the global motion vectors if (loop_count > 0 &&
cm->features.allow_high_precision_mv != last_loop_allow_hp) {
gm_info->search_done = 0;
}
last_loop_allow_hp = cm->features.allow_high_precision_mv;
}
// Disable mv_stats collection for parallel frames based on update flag. if (!cpi->do_frame_data_update) do_mv_stats_collection = 0;
// Reset the mv_stats in case we are interrupted by an intraframe or an // overlay frame. if (cpi->mv_stats.valid && do_mv_stats_collection) av1_zero(cpi->mv_stats);
// Gather the mv_stats for the next frame if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA &&
av1_frame_allows_smart_mv(cpi) && do_mv_stats_collection) {
av1_collect_mv_stats(cpi, q);
}
#if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND constint do_dummy_pack = 1; #else// CONFIG_BITRATE_ACCURACY // Dummy pack of the bitstream using up to date stats to get an // accurate estimate of output frame size to determine if we need // to recode. constint do_dummy_pack =
(cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF &&
oxcf->rc_cfg.mode != AOM_Q) ||
oxcf->rc_cfg.min_cr > 0; #endif// CONFIG_BITRATE_ACCURACY if (do_dummy_pack) {
av1_finalize_encoded_frame(cpi); int largest_tile_id = 0; // Output from bitstream: unused here
rc->coefficient_size = 0; if (av1_pack_bitstream(cpi, dest, dest_size, size, &largest_tile_id) !=
AOM_CODEC_OK) { return AOM_CODEC_ERROR;
}
// bits used for this frame
rc->projected_frame_size = (int)(*size) << 3; #if CONFIG_RD_COMMAND
PSNR_STATS psnr;
aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
printf("q %d rdmult %d rate %d dist %" PRIu64 "\n", q, cpi->rd.RDMULT,
rc->projected_frame_size, psnr.sse[0]);
++rd_command->frame_index; if (rd_command->frame_index == rd_command->frame_count) { return AOM_CODEC_ERROR;
} #endif// CONFIG_RD_COMMAND
if (cpi->use_ducky_encode) { // Ducky encode currently does not support recode loop.
loop = 0;
} #if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
loop = 0; // turn off recode loop when CONFIG_BITRATE_ACCURACY is on #endif// CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND
// TODO(jingning, paulwilkins): Set up high grain level to test // hardware decoders. Need to adapt the actual noise variance // according to the difference between reconstructed frame and the // source signal. staticvoid set_grain_syn_params(AV1_COMMON *cm) {
aom_film_grain_t *film_grain_params = &cm->film_grain_params;
film_grain_params->apply_grain = 1;
film_grain_params->update_parameters = 1;
film_grain_params->random_seed = rand() & 0xffff;
/*!\brief Recode loop or a single loop for encoding one frame, followed by * in-loop deblocking filters, CDEF filters, and restoration filters. * * \ingroup high_level_algo * \callgraph * \callergraph * * \param[in] cpi Top-level encoder structure * \param[in] size Bitstream size * \param[out] dest Bitstream output buffer * \param[in] dest_size Bitstream output buffer size * \param[in] sse Total distortion of the frame * \param[in] rate Total rate of the frame * \param[in] largest_tile_id Tile id of the last tile * * \return Returns a value to indicate if the encoding is done successfully. * \retval #AOM_CODEC_OK * \retval #AOM_CODEC_ERROR
*/ staticint encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
uint8_t *dest, size_t dest_size,
int64_t *sse, int64_t *rate, int *largest_tile_id) { #if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, encode_with_or_without_recode_time); #endif for (int i = 0; i < NUM_RECODES_PER_FRAME; i++) {
cpi->do_update_frame_probs_txtype[i] = 0;
cpi->do_update_frame_probs_obmc[i] = 0;
cpi->do_update_frame_probs_warp[i] = 0;
cpi->do_update_frame_probs_interpfilter[i] = 0;
}
cpi->do_update_vbr_bits_off_target_fast = 0; int err; #if CONFIG_REALTIME_ONLY
err = encode_without_recode(cpi); #else if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE)
err = encode_without_recode(cpi); else
err = encode_with_recode_loop(cpi, size, dest, dest_size); #endif #if CONFIG_COLLECT_COMPONENT_TIMING
end_timing(cpi, encode_with_or_without_recode_time); #endif if (err != AOM_CODEC_OK) { if (err == -1) { // special case as described in encode_with_recode_loop(). // Encoding was skipped.
err = AOM_CODEC_OK; if (sse != NULL) *sse = INT64_MAX; if (rate != NULL) *rate = INT64_MAX;
*largest_tile_id = 0;
} return err;
}
AV1_COMMON *const cm = &cpi->common;
SequenceHeader *const seq_params = cm->seq_params;
// Special case code to reduce pulsing when key frames are forced at a // fixed interval. Note the reconstruction error if it is the frame before // the force key frame if (cpi->ppi->p_rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) { #if CONFIG_AV1_HIGHBITDEPTH if (seq_params->use_highbitdepth) {
cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
} else {
cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
} #else
cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf); #endif
}
// Note: Both use common rdmult based on base qindex of fullres. const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
bit_depth, update_type, cm->quant_params.base_qindex,
cpi->oxcf.tune_cfg.tuning);
// Find the best rdcost among all superres denoms. int best_denom = -1; for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR;
++denom) { constint this_index = denom - (SCALE_NUMERATOR + 1); const int64_t this_sse = superres_sses[this_index]; const int64_t this_rate = superres_rates[this_index]; constint this_largest_tile_id = superres_largest_tile_ids[this_index]; constdouble this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST(
rdmult, this_rate, this_sse, bit_depth); if (this_rdcost < proj_rdcost1) {
sse1 = this_sse;
rate1 = this_rate;
largest_tile_id1 = this_largest_tile_id;
proj_rdcost1 = this_rdcost;
best_denom = denom;
}
} constdouble proj_rdcost2 =
RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth); // Re-encode with superres if it's better. if (proj_rdcost1 < proj_rdcost2) {
restore_all_coding_context(cpi); // TODO(urvang): We should avoid rerunning the recode loop by saving // previous output+state, or running encode only for the selected 'q' in // previous step. // Again, temporarily force the best denom.
superres_cfg->superres_scale_denominator = best_denom;
superres_cfg->superres_kf_scale_denominator = best_denom;
int64_t sse3 = INT64_MAX;
int64_t rate3 = INT64_MAX;
cpi->superres_mode =
AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size,
&sse3, &rate3, largest_tile_id);
cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
assert(sse1 == sse3);
assert(rate1 == rate3);
assert(largest_tile_id1 == *largest_tile_id); // Reset.
superres_cfg->superres_scale_denominator = SCALE_NUMERATOR;
superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR;
} else {
*largest_tile_id = largest_tile_id2;
}
} else {
assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL);
cpi->superres_mode =
AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, &sse1,
&rate1, &largest_tile_id1);
cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res). if (err != AOM_CODEC_OK) return err;
restore_all_coding_context(cpi); // Encode without superres.
assert(cpi->superres_mode == AOM_SUPERRES_NONE);
err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size, &sse2,
&rate2, &largest_tile_id2); if (err != AOM_CODEC_OK) return err;
// Note: Both use common rdmult based on base qindex of fullres. const int64_t rdmult = av1_compute_rd_mult_based_on_qindex(
bit_depth, update_type, cm->quant_params.base_qindex,
cpi->oxcf.tune_cfg.tuning);
proj_rdcost1 =
RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1, bit_depth); constdouble proj_rdcost2 =
RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth); // Re-encode with superres if it's better. if (proj_rdcost1 < proj_rdcost2) {
restore_all_coding_context(cpi); // TODO(urvang): We should avoid rerunning the recode loop by saving // previous output+state, or running encode only for the selected 'q' in // previous step.
int64_t sse3 = INT64_MAX;
int64_t rate3 = INT64_MAX;
cpi->superres_mode =
AOM_SUPERRES_AUTO; // Super-res on for this recode loop.
err = encode_with_recode_loop_and_filter(cpi, size, dest, dest_size,
&sse3, &rate3, largest_tile_id);
cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res).
assert(sse1 == sse3);
assert(rate1 == rate3);
assert(largest_tile_id1 == *largest_tile_id);
} else {
*largest_tile_id = largest_tile_id2;
}
}
return err;
}
// Conditions to disable cdf_update mode in selective mode for real-time. // Handle case for layers, scene change, and resizing. staticinlineint selective_disable_cdf_rtc(const AV1_COMP *cpi) { const AV1_COMMON *const cm = &cpi->common; const RATE_CONTROL *const rc = &cpi->rc; // For single layer. if (cpi->svc.number_spatial_layers == 1 &&
cpi->svc.number_temporal_layers == 1) { // Don't disable on intra_only, scene change (high_source_sad = 1), // or resized frame. To avoid quality loss force enable at // for ~30 frames after key or scene/slide change, and // after 8 frames since last update if frame_source_sad > 0. if (frame_is_intra_only(cm) || is_frame_resize_pending(cpi) ||
rc->high_source_sad || rc->frames_since_key < 30 ||
(cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
cpi->cyclic_refresh->counter_encode_maxq_scene_change < 30) ||
(cpi->frames_since_last_update > 8 && cpi->rc.frame_source_sad > 0)) return 0; else return 1;
} elseif (cpi->svc.number_temporal_layers > 1) { // Disable only on top temporal enhancement layer for now. return cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1;
} return 1;
}
features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm); // features->allow_ref_frame_mvs needs to be written into the frame header // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case // is separated from frame_might_allow_ref_frame_mvs().
features->allow_ref_frame_mvs &= !cm->tiles.large_scale;
if (frame_is_intra_only(cm)) {
cpi->frames_since_last_update = 0;
}
if (frame_is_sframe(cm)) {
GF_GROUP *gf_group = &cpi->ppi->gf_group; // S frame will wipe out any previously encoded altref so we cannot place // an overlay frame
gf_group->update_type[gf_group->size] = GF_UPDATE;
}
if (encode_show_existing_frame(cm)) { #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY // TODO(angiebird): Move this into a function. if (oxcf->pass == AOM_RC_THIRD_PASS) { int frame_coding_idx =
av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index);
rc_log_frame_encode_param(
&cpi->rc_log, frame_coding_idx, 1, 255,
cpi->ppi->gf_group.update_type[cpi->gf_frame_index]);
} #endif
av1_finalize_encoded_frame(cpi); // Build the bitstream int largest_tile_id = 0; // Output from bitstream: unused here
cpi->rc.coefficient_size = 0; if (av1_pack_bitstream(cpi, dest, dest_size, size, &largest_tile_id) !=
AOM_CODEC_OK) return AOM_CODEC_ERROR;
if (seq_params->frame_id_numbers_present_flag &&
current_frame->frame_type == KEY_FRAME) { // Displaying a forward key-frame, so reset the ref buffer IDs int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show]; for (int i = 0; i < REF_FRAMES; i++)
cm->ref_frame_id[i] = display_frame_id;
}
#if DUMP_RECON_FRAMES == 1 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
av1_dump_filtered_recon_frames(cpi); #endif// DUMP_RECON_FRAMES
// NOTE: Save the new show frame buffer index for --test-code=warn, i.e., // for the purpose to verify no mismatch between encoder and decoder. if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
// Since we allocate a spot for the OVERLAY frame in the gf group, we need // to do post-encoding update accordingly.
av1_set_target_rate(cpi, cm->width, cm->height);
if (is_psnr_calc_enabled(cpi)) {
cpi->source =
realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width,
cm->cur_frame->buf.y_crop_height);
}
// Work out whether to force_integer_mv this frame if (!is_stat_generation_stage(cpi) &&
cpi->common.features.allow_screen_content_tools &&
!frame_is_intra_only(cm) && !cpi->sf.rt_sf.use_nonrd_pick_mode) { if (cpi->common.seq_params->force_integer_mv == 2) { // Adaptive mode: see what previous frame encoded did if (cpi->unscaled_last_source != NULL) {
features->cur_frame_force_integer_mv = av1_is_integer_mv(
cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info);
} else {
cpi->common.features.cur_frame_force_integer_mv = 0;
}
} else {
cpi->common.features.cur_frame_force_integer_mv =
cpi->common.seq_params->force_integer_mv;
}
} else {
cpi->common.features.cur_frame_force_integer_mv = 0;
}
// This is used by av1_pack_bitstream. So this needs to be set in case of // row-mt where the encoding code will use a temporary structure.
cpi->td.mb.e_mbd.cur_frame_force_integer_mv =
cpi->common.features.cur_frame_force_integer_mv;
// Set default state for segment based loop filter update flags.
cm->lf.mode_ref_delta_update = 0;
// Set various flags etc to special state if it is a key frame. if (frame_is_intra_only(cm) || frame_is_sframe(cm)) { // Reset the loop filter deltas and segmentation map.
av1_reset_segment_features(cm);
// If segmentation is enabled force a map update for key frames. if (seg->enabled) {
seg->update_map = 1;
seg->update_data = 1;
}
} if (tile_cfg->mtu == 0) {
cpi->num_tg = tile_cfg->num_tile_groups;
} else { // Use a default value for the purposes of weighting costs in probability // updates
cpi->num_tg = DEFAULT_MAX_NUM_TG;
}
// For 1 pass CBR mode: check if we are dropping this frame. if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR) { // Always drop for spatial enhancement layer if layer bandwidth is 0. // Otherwise check for frame-dropping based on buffer level in // av1_rc_drop_frame(). if ((cpi->svc.spatial_layer_id > 0 &&
cpi->oxcf.rc_cfg.target_bandwidth == 0) ||
av1_rc_drop_frame(cpi)) {
cpi->is_dropped_frame = true;
} if (cpi->is_dropped_frame) {
av1_setup_frame_size(cpi);
av1_set_mv_search_params(cpi);
av1_rc_postencode_update_drop_frame(cpi);
release_scaled_references(cpi);
cpi->ppi->gf_group.is_frame_dropped[cpi->gf_frame_index] = true; // A dropped frame might not be shown but it always takes a slot in the gf // group. Therefore, even when it is not shown, we still need to update // the relevant frame counters. if (cm->show_frame) {
update_counters_for_show_frame(cpi);
} return AOM_CODEC_OK;
}
}
if (seq_params->frame_id_numbers_present_flag) { /* Non-normative definition of current_frame_id ("frame counter" with
* wraparound) */ if (cm->current_frame_id == -1) { int lsb, msb; /* quasi-random initialization of current_frame_id for a key frame */ if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
} else {
lsb = cpi->source->y_buffer[0] & 0xff;
msb = cpi->source->y_buffer[1] & 0xff;
}
cm->current_frame_id =
((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
// S_frame is meant for stitching different streams of different // resolutions together, so current_frame_id must be the // same across different streams of the same content current_frame_id // should be the same and not random. 0x37 is a chosen number as start // point if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37;
} else {
cm->current_frame_id =
(cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
(1 << seq_params->frame_id_length);
}
}
switch (oxcf->algo_cfg.cdf_update_mode) { case 0: // No CDF update for any frames(4~6% compression loss).
features->disable_cdf_update = 1; break; case 1: // Enable CDF update for all frames. if (cpi->sf.rt_sf.disable_cdf_update_non_reference_frame &&
cpi->ppi->rtc_ref.non_reference_frame && cpi->rc.frames_since_key > 2)
features->disable_cdf_update = 1; elseif (cpi->sf.rt_sf.selective_cdf_update)
features->disable_cdf_update = selective_disable_cdf_rtc(cpi); else
features->disable_cdf_update = 0; break; case 2: // Strategically determine at which frames to do CDF update. // Currently only enable CDF update for all-intra and no-show frames(1.5% // compression loss) for good qualiy or allintra mode. if (oxcf->mode == GOOD || oxcf->mode == ALLINTRA) {
features->disable_cdf_update =
(frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
} else {
features->disable_cdf_update = selective_disable_cdf_rtc(cpi);
} break;
}
// Disable cdf update for the INTNL_ARF_UPDATE frame with // frame_parallel_level 1. if (!cpi->do_frame_data_update &&
cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
assert(cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1);
features->disable_cdf_update = 1;
}
#if !CONFIG_REALTIME_ONLY if (cpi->oxcf.tool_cfg.enable_global_motion && !frame_is_intra_only(cm)) { // Flush any stale global motion information, which may be left over // from a previous frame
aom_invalidate_pyramid(cpi->source->y_pyramid);
av1_invalidate_corner_list(cpi->source->corners);
} #endif// !CONFIG_REALTIME_ONLY
// Update reference frame ids for reference frames this frame will overwrite if (seq_params->frame_id_numbers_present_flag) { for (int i = 0; i < REF_FRAMES; i++) { if ((current_frame->refresh_frame_flags >> i) & 1) {
cm->ref_frame_id[i] = cm->current_frame_id;
}
}
}
if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
cpi->svc.num_encoded_top_layer++;
#if DUMP_RECON_FRAMES == 1 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
av1_dump_filtered_recon_frames(cpi); #endif// DUMP_RECON_FRAMES
if (cm->seg.enabled) { if (cm->seg.update_map == 0 && cm->last_frame_seg_map) {
memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
cm->cur_frame->mi_cols * cm->cur_frame->mi_rows * sizeof(*cm->cur_frame->seg_map));
}
}
// NOTE: Save the new show frame buffer index for --test-code=warn, i.e., // for the purpose to verify no mismatch between encoder and decoder. if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
*cm->fc = cpi->tile_data[largest_tile_id].tctx;
av1_reset_cdf_symbol_counters(cm->fc);
} if (!cm->tiles.large_scale) {
cm->cur_frame->frame_context = *cm->fc;
}
if (tile_cfg->enable_ext_tile_debug) { // (yunqing) This test ensures the correctness of large scale tile coding. if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) { char fn[20] = "./fc";
fn[4] = current_frame->frame_number / 100 + '0';
fn[5] = (current_frame->frame_number % 100) / 10 + '0';
fn[6] = (current_frame->frame_number % 10) + '0';
fn[7] = '\0';
av1_print_frame_contexts(cm->fc, fn);
}
}
#if CONFIG_DENOISE // even if denoise_noise_level is > 0, we don't need need to denoise on pass // 1 of 2 if enable_dnl_denoising is disabled since the 2nd pass will be // encoding the original (non-denoised) frame if (cpi->oxcf.noise_level > 0 && !(cpi->oxcf.pass == AOM_RC_FIRST_PASS &&
!cpi->oxcf.enable_dnl_denoising)) { #if !CONFIG_REALTIME_ONLY // Choose a synthetic noise level for still images for enhanced perceptual // quality based on an estimated noise level in the source, but only if // the noise level is set on the command line to > 0. if (cpi->oxcf.mode == ALLINTRA) { // No noise synthesis if source is very clean. // Uses a low edge threshold to focus on smooth areas. // Increase output noise setting a little compared to measured value. double y_noise_level = 0.0;
av1_estimate_noise_level(sd, &y_noise_level, AOM_PLANE_Y, AOM_PLANE_Y,
cm->seq_params->bit_depth, 16);
cpi->oxcf.noise_level = (float)(y_noise_level - 0.1);
cpi->oxcf.noise_level = (float)AOMMAX(0.0, cpi->oxcf.noise_level); if (cpi->oxcf.noise_level > 0.0) {
cpi->oxcf.noise_level += (float)0.5;
}
cpi->oxcf.noise_level = (float)AOMMIN(5.0, cpi->oxcf.noise_level);
}
if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
cpi->oxcf.noise_level, time_stamp, end_time) < 0)
res = -1; #endif// !CONFIG_REALTIME_ONLY
} #endif// CONFIG_DENOISE
// Note: Regarding profile setting, the following checks are added to help // choose a proper profile for the input video. The criterion is that all // bitstreams must be designated as the lowest profile that match its content. // E.G. A bitstream that contains 4:4:4 video must be designated as High // Profile in the seq header, and likewise a bitstream that contains 4:2:2 // bitstream must be designated as Professional Profile in the sequence // header. if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
(subsampling_x != 1 || subsampling_y != 1)) {
aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, "Non-4:2:0 color format requires profile 1 or 2");
res = -1;
} if ((seq_params->profile == PROFILE_1) &&
!(subsampling_x == 0 && subsampling_y == 0)) {
aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, "Profile 1 requires 4:4:4 color format");
res = -1;
} if ((seq_params->profile == PROFILE_2) &&
(seq_params->bit_depth <= AOM_BITS_10) &&
!(subsampling_x == 1 && subsampling_y == 0)) {
aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, "Profile 2 bit-depth <= 10 requires 4:2:2 color format");
res = -1;
}
return res;
}
#if CONFIG_ENTROPY_STATS void print_entropy_stats(AV1_PRIMARY *const ppi) { if (!ppi->cpi) return;
staticinlinevoid update_keyframe_counters(AV1_COMP *cpi) { if (cpi->common.show_frame && cpi->rc.frames_to_key) { #if !CONFIG_REALTIME_ONLY
FIRSTPASS_INFO *firstpass_info = &cpi->ppi->twopass.firstpass_info; if (firstpass_info->past_stats_count > FIRSTPASS_INFO_STATS_PAST_MIN) {
av1_firstpass_info_move_cur_index_and_pop(firstpass_info);
} else { // When there is not enough past stats, we move the current // index without popping the past stats
av1_firstpass_info_move_cur_index(firstpass_info);
} #endif if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
cpi->rc.frames_since_key++;
cpi->rc.frames_to_key--;
cpi->rc.frames_to_fwd_kf--;
cpi->rc.frames_since_scene_change++;
}
}
}
staticinlinevoid update_frames_till_gf_update(AV1_COMP *cpi) { // TODO(weitinglin): Updating this counter for is_frame_droppable // is a work-around to handle the condition when a frame is drop. // We should fix the cpi->common.show_frame flag // instead of checking the other condition to update the counter properly. if (cpi->common.show_frame ||
is_frame_droppable(&cpi->ppi->rtc_ref, &cpi->ext_flags.refresh_frame)) { // Decrement count down till next gf if (cpi->rc.frames_till_gf_update_due > 0)
cpi->rc.frames_till_gf_update_due--;
}
}
staticinlinevoid update_gf_group_index(AV1_COMP *cpi) { // Increment the gf group index ready for the next frame. if (is_one_pass_rt_params(cpi) &&
cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
++cpi->gf_frame_index; // Reset gf_frame_index in case it reaches MAX_STATIC_GF_GROUP_LENGTH // for real time encoding. if (cpi->gf_frame_index == MAX_STATIC_GF_GROUP_LENGTH)
cpi->gf_frame_index = 0;
} else {
++cpi->gf_frame_index;
}
}
staticvoid update_fb_of_context_type(const AV1_COMP *const cpi, int *const fb_of_context_type) { const AV1_COMMON *const cm = &cpi->common; constint current_frame_ref_type = get_current_frame_ref_type(cpi);
if (frame_is_intra_only(cm) || cm->features.error_resilient_mode ||
cpi->ext_flags.use_primary_ref_none) { for (int i = 0; i < REF_FRAMES; i++) {
fb_of_context_type[i] = -1;
}
fb_of_context_type[current_frame_ref_type] =
cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME)
: get_ref_frame_map_idx(cm, ALTREF_FRAME);
}
if (!encode_show_existing_frame(cm)) { // Refresh fb_of_context_type[]: see encoder.h for explanation if (cm->current_frame.frame_type == KEY_FRAME) { // All ref frames are refreshed, pick one that will live long enough
fb_of_context_type[current_frame_ref_type] = 0;
} else { // If more than one frame is refreshed, it doesn't matter which one we // pick so pick the first. LST sometimes doesn't refresh any: this is ok
for (int i = 0; i < REF_FRAMES; i++) { if (cm->current_frame.refresh_frame_flags & (1 << i)) {
fb_of_context_type[current_frame_ref_type] = i; break;
}
}
}
}
}
staticvoid update_end_of_frame_stats(AV1_COMP *cpi) { if (cpi->do_frame_data_update) { // Store current frame loopfilter levels in ppi, if update flag is set. if (!cpi->common.show_existing_frame) {
AV1_COMMON *const cm = &cpi->common; struct loopfilter *const lf = &cm->lf;
cpi->ppi->filter_level[0] = lf->filter_level[0];
cpi->ppi->filter_level[1] = lf->filter_level[1];
cpi->ppi->filter_level_u = lf->filter_level_u;
cpi->ppi->filter_level_v = lf->filter_level_v;
}
} // Store frame level mv_stats from cpi to ppi.
cpi->ppi->mv_stats = cpi->mv_stats;
}
// Updates frame level stats related to global motion staticinlinevoid update_gm_stats(AV1_COMP *cpi) {
FRAME_UPDATE_TYPE update_type =
cpi->ppi->gf_group.update_type[cpi->gf_frame_index]; int i, is_gm_present = 0;
// Check if the current frame has any valid global motion model across its // reference frames for (i = 0; i < REF_FRAMES; i++) { if (cpi->common.global_motion[i].wmtype != IDENTITY) {
is_gm_present = 1; break;
}
} int update_actual_stats = 1; #if CONFIG_FPMT_TEST
update_actual_stats =
(cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 0 : 1; if (!update_actual_stats) { if (cpi->ppi->temp_valid_gm_model_found[update_type] == INT32_MAX) {
cpi->ppi->temp_valid_gm_model_found[update_type] = is_gm_present;
} else {
cpi->ppi->temp_valid_gm_model_found[update_type] |= is_gm_present;
} int show_existing_between_parallel_frames =
(cpi->ppi->gf_group.update_type[cpi->gf_frame_index] ==
INTNL_OVERLAY_UPDATE &&
cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); if (cpi->do_frame_data_update == 1 &&
!show_existing_between_parallel_frames) { for (i = 0; i < FRAME_UPDATE_TYPES; i++) {
cpi->ppi->valid_gm_model_found[i] =
cpi->ppi->temp_valid_gm_model_found[i];
}
}
} #endif if (update_actual_stats) { if (cpi->ppi->valid_gm_model_found[update_type] == INT32_MAX) {
cpi->ppi->valid_gm_model_found[update_type] = is_gm_present;
} else {
cpi->ppi->valid_gm_model_found[update_type] |= is_gm_present;
}
}
}
void av1_post_encode_updates(AV1_COMP *const cpi, const AV1_COMP_DATA *const cpi_data) {
AV1_PRIMARY *const ppi = cpi->ppi;
AV1_COMMON *const cm = &cpi->common;
update_gm_stats(cpi);
#if !CONFIG_REALTIME_ONLY // Update the total stats remaining structure. if (cpi->twopass_frame.this_frame != NULL &&
ppi->twopass.stats_buf_ctx->total_left_stats) {
subtract_stats(ppi->twopass.stats_buf_ctx->total_left_stats,
cpi->twopass_frame.this_frame);
} #endif
if (!is_stat_generation_stage(cpi) && !cpi->is_dropped_frame) { // Before calling refresh_reference_frames(), copy ppi->ref_frame_map_copy // to cm->ref_frame_map for frame_parallel_level 2 frame in a parallel // encode set of lower layer frames. // TODO(Remya): Move ref_frame_map from AV1_COMMON to AV1_PRIMARY to avoid // copy. if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 2 &&
ppi->gf_group.frame_parallel_level[cpi->gf_frame_index - 1] == 1 &&
ppi->gf_group.update_type[cpi->gf_frame_index - 1] ==
INTNL_ARF_UPDATE) {
memcpy(cm->ref_frame_map, ppi->ref_frame_map_copy, sizeof(cm->ref_frame_map));
}
refresh_reference_frames(cpi); // For frame_parallel_level 1 frame in a parallel encode set of lower layer // frames, store the updated cm->ref_frame_map in ppi->ref_frame_map_copy. if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1 &&
ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
memcpy(ppi->ref_frame_map_copy, cm->ref_frame_map, sizeof(cm->ref_frame_map));
}
av1_rc_postencode_update(cpi, cpi_data->frame_size);
}
if (cpi_data->pop_lookahead == 1) {
av1_lookahead_pop(cpi->ppi->lookahead, cpi_data->flush,
cpi->compressor_stage);
} if (cpi->common.show_frame) {
cpi->ppi->ts_start_last_show_frame = cpi_data->ts_frame_start;
cpi->ppi->ts_end_last_show_frame = cpi_data->ts_frame_end;
} if (ppi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) { // Initialize level info. at the beginning of each sequence. if (cm->current_frame.frame_type == KEY_FRAME &&
ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) {
av1_init_level_info(cpi);
}
av1_update_level_info(cpi, cpi_data->frame_size, cpi_data->ts_frame_start,
cpi_data->ts_frame_end);
}
if (!is_stat_generation_stage(cpi)) { #if !CONFIG_REALTIME_ONLY if (!has_no_stats_stage(cpi)) av1_twopass_postencode_update(cpi); #endif
update_fb_of_context_type(cpi, ppi->fb_of_context_type);
update_rc_counts(cpi);
update_end_of_frame_stats(cpi);
}
if (ppi->rtc_ref.set_ref_frame_config && !cpi->is_dropped_frame) {
av1_svc_update_buffer_slot_refreshed(cpi);
av1_svc_set_reference_was_previous(cpi);
}
if (ppi->use_svc) av1_save_layer_context(cpi);
// Note *size = 0 indicates a dropped frame for which psnr is not calculated if (ppi->b_calculate_psnr && cpi_data->frame_size > 0) { if (cm->show_existing_frame ||
(!is_stat_generation_stage(cpi) && cm->show_frame)) {
generate_psnr_packet(cpi);
}
}
#if CONFIG_INTERNAL_STATS if (!is_stat_generation_stage(cpi)) {
compute_internal_stats(cpi, (int)cpi_data->frame_size);
} #endif// CONFIG_INTERNAL_STATS
#if CONFIG_THREE_PASS // Write frame info. Subtract 1 from frame index since if was incremented in // update_rc_counts.
av1_write_second_pass_per_frame_info(cpi, cpi->gf_frame_index - 1); #endif
}
int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) { const AV1EncoderConfig *const oxcf = &cpi->oxcf;
AV1_COMMON *const cm = &cpi->common;
// The jmp_buf is valid only for the duration of the function that calls // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 // before it returns. if (setjmp(cm->error->jmp)) {
cm->error->setjmp = 0; return cm->error->error_code;
}
cm->error->setjmp = 1;
#if CONFIG_BITSTREAM_DEBUG
assert(cpi->oxcf.max_threads <= 1 && "bitstream debug tool does not support multithreading");
bitstream_queue_record_write();
if (cm->seq_params->order_hint_info.enable_order_hint) {
aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 +
cm->show_frame);
} else { // This is currently used in RTC encoding. cm->show_frame is always 1.
aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number);
} #endif if (cpi->ppi->use_svc) {
av1_one_pass_cbr_svc_start_layer(cpi);
}
// Normal defaults
cm->features.refresh_frame_context =
oxcf->tool_cfg.frame_parallel_decoding_mode
? REFRESH_FRAME_CONTEXT_DISABLED
: REFRESH_FRAME_CONTEXT_BACKWARD; if (oxcf->tile_cfg.enable_large_scale_tile)
cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
if (assign_cur_frame_new_fb(cm) == NULL) {
aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, "Failed to allocate new cur_frame");
}
#if CONFIG_COLLECT_COMPONENT_TIMING // Accumulate 2nd pass time in 2-pass case or 1 pass time in 1-pass case. if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0)
start_timing(cpi, av1_encode_strategy_time); #endif
// Populates cpi->scaled_ref_buf corresponding to frames in a parallel encode // set. Also sets the bitmask 'ref_buffers_used_map'. staticvoid scale_references_fpmt(AV1_COMP *cpi, int *ref_buffers_used_map) {
AV1_COMMON *cm = &cpi->common;
MV_REFERENCE_FRAME ref_frame;
for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1). if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) { const YV12_BUFFER_CONFIG *const ref =
get_ref_frame_yv12_buf(cm, ref_frame);
// FPMT does not support scaling yet.
assert(ref->y_crop_width == cm->width &&
ref->y_crop_height == cm->height);
RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
cpi->scaled_ref_buf[ref_frame - 1] = buf; for (int i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) { if (&cm->buffer_pool->frame_bufs[i] == buf) {
*ref_buffers_used_map |= (1 << i);
}
}
} else { if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
}
}
}
// Increments the ref_count of frame buffers referenced by cpi->scaled_ref_buf // corresponding to frames in a parallel encode set. staticvoid increment_scaled_ref_counts_fpmt(BufferPool *buffer_pool, int ref_buffers_used_map) { for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) { if (ref_buffers_used_map & (1 << i)) {
++buffer_pool->frame_bufs[i].ref_count;
}
}
}
// Releases cpi->scaled_ref_buf corresponding to frames in a parallel encode // set. void av1_release_scaled_references_fpmt(AV1_COMP *cpi) { // TODO(isbs): only refresh the necessary frames, rather than all of them for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
RefCntBuffer *const buf = cpi->scaled_ref_buf[i]; if (buf != NULL) {
cpi->scaled_ref_buf[i] = NULL;
}
}
}
// Decrements the ref_count of frame buffers referenced by cpi->scaled_ref_buf // corresponding to frames in a parallel encode set. void av1_decrement_ref_counts_fpmt(BufferPool *buffer_pool, int ref_buffers_used_map) { for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) { if (ref_buffers_used_map & (1 << i)) {
--buffer_pool->frame_bufs[i].ref_count;
}
}
}
// Loop over parallel_cpi to find the cpi that processed the current // gf_frame_index ahead of time. for (int i = 1; i < ppi->num_fp_contexts; i++) { if (ppi->cpi->gf_frame_index == ppi->parallel_cpi[i]->gf_frame_index) {
cpi_idx = i; break;
}
}
// Initialises frames belonging to a parallel encode set. int av1_init_parallel_frame_context(const AV1_COMP_DATA *const first_cpi_data,
AV1_PRIMARY *const ppi, int *ref_buffers_used_map) {
AV1_COMP *const first_cpi = ppi->cpi;
GF_GROUP *const gf_group = &ppi->gf_group; int gf_index_start = first_cpi->gf_frame_index;
assert(gf_group->frame_parallel_level[gf_index_start] == 1); int parallel_frame_count = 0; int cur_frame_num = first_cpi->common.current_frame.frame_number; int show_frame_count = first_cpi->frame_index_set.show_frame_count; int frames_since_key = first_cpi->rc.frames_since_key; int frames_to_key = first_cpi->rc.frames_to_key; int frames_to_fwd_kf = first_cpi->rc.frames_to_fwd_kf; int cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[gf_index_start]; const FIRSTPASS_STATS *stats_in = first_cpi->twopass_frame.stats_in;
assert(*ref_buffers_used_map == 0);
// Release the previously used frame-buffer by a frame_parallel_level 1 frame. if (first_cpi->common.cur_frame != NULL) {
--first_cpi->common.cur_frame->ref_count;
first_cpi->common.cur_frame = NULL;
}
// Store the reference refresh index of frame_parallel_level 1 frame in a // parallel encode set of lower layer frames. if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) {
first_cpi->ref_refresh_index = av1_calc_refresh_idx_for_intnl_arf(
first_cpi, ref_frame_map_pairs, gf_index_start);
assert(first_cpi->ref_refresh_index != INVALID_IDX &&
first_cpi->ref_refresh_index < REF_FRAMES);
first_cpi->refresh_idx_available = true; // Update ref_frame_map_pairs.
ref_frame_map_pairs[first_cpi->ref_refresh_index].disp_order =
gf_group->display_idx[gf_index_start];
ref_frame_map_pairs[first_cpi->ref_refresh_index].pyr_level =
gf_group->layer_depth[gf_index_start];
}
// Set do_frame_data_update flag as false for frame_parallel_level 1 frame.
first_cpi->do_frame_data_update = false; if (gf_group->arf_src_offset[gf_index_start] == 0) {
first_cpi->time_stamps.prev_ts_start = ppi->ts_start_last_show_frame;
first_cpi->time_stamps.prev_ts_end = ppi->ts_end_last_show_frame;
}
// Iterate through the GF_GROUP to find the remaining frame_parallel_level 2 // frames which are part of the current parallel encode set and initialize the // required cpi elements. for (int i = gf_index_start + 1; i < gf_group->size; i++) { // Update frame counters if previous frame was show frame or show existing // frame. if (gf_group->arf_src_offset[i - 1] == 0) {
cur_frame_num++;
show_frame_count++; if (frames_to_fwd_kf <= 0)
frames_to_fwd_kf = first_cpi->oxcf.kf_cfg.fwd_kf_dist; if (frames_to_key) {
frames_since_key++;
frames_to_key--;
frames_to_fwd_kf--;
}
stats_in++;
}
cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[i]; if (gf_group->frame_parallel_level[i] == 2) {
AV1_COMP *cur_cpi = ppi->parallel_cpi[parallel_frame_count];
AV1_COMP_DATA *cur_cpi_data =
&ppi->parallel_frames_data[parallel_frame_count - 1];
cur_cpi->gf_frame_index = i;
cur_cpi->framerate = first_cpi->framerate;
cur_cpi->common.current_frame.frame_number = cur_frame_num;
cur_cpi->common.current_frame.frame_type = gf_group->frame_type[i];
cur_cpi->frame_index_set.show_frame_count = show_frame_count;
cur_cpi->rc.frames_since_key = frames_since_key;
cur_cpi->rc.frames_to_key = frames_to_key;
cur_cpi->rc.frames_to_fwd_kf = frames_to_fwd_kf;
cur_cpi->rc.active_worst_quality = first_cpi->rc.active_worst_quality;
cur_cpi->rc.avg_frame_bandwidth = first_cpi->rc.avg_frame_bandwidth;
cur_cpi->rc.max_frame_bandwidth = first_cpi->rc.max_frame_bandwidth;
cur_cpi->rc.min_frame_bandwidth = first_cpi->rc.min_frame_bandwidth;
cur_cpi->rc.intervals_till_gf_calculate_due =
first_cpi->rc.intervals_till_gf_calculate_due;
cur_cpi->mv_search_params.max_mv_magnitude =
first_cpi->mv_search_params.max_mv_magnitude; if (gf_group->update_type[cur_cpi->gf_frame_index] == INTNL_ARF_UPDATE) {
cur_cpi->common.lf.mode_ref_delta_enabled = 1;
}
cur_cpi->do_frame_data_update = false; // Initialize prev_ts_start and prev_ts_end for show frame(s) and show // existing frame(s). if (gf_group->arf_src_offset[i] == 0) { // Choose source of prev frame. int src_index = gf_group->src_offset[i]; struct lookahead_entry *prev_source = av1_lookahead_peek(
ppi->lookahead, src_index - 1, cur_cpi->compressor_stage); // Save timestamps of prev frame.
cur_cpi->time_stamps.prev_ts_start = prev_source->ts_start;
cur_cpi->time_stamps.prev_ts_end = prev_source->ts_end;
}
cur_cpi->time_stamps.first_ts_start =
first_cpi->time_stamps.first_ts_start;
memcpy(cur_cpi->common.ref_frame_map, first_cpi->common.ref_frame_map, sizeof(first_cpi->common.ref_frame_map));
cur_cpi_data->lib_flags = 0;
cur_cpi_data->timestamp_ratio = first_cpi_data->timestamp_ratio;
cur_cpi_data->flush = first_cpi_data->flush;
cur_cpi_data->frame_size = 0; if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) { // If the first frame in a parallel encode set is INTNL_ARF_UPDATE // frame, initialize lib_flags of frame_parallel_level 2 frame in the // set with that of frame_parallel_level 1 frame.
cur_cpi_data->lib_flags = first_cpi_data->lib_flags; // Store the reference refresh index of frame_parallel_level 2 frame in // a parallel encode set of lower layer frames.
cur_cpi->ref_refresh_index =
av1_calc_refresh_idx_for_intnl_arf(cur_cpi, ref_frame_map_pairs, i);
cur_cpi->refresh_idx_available = true; // Skip the reference frame which will be refreshed by // frame_parallel_level 1 frame in a parallel encode set of lower layer // frames.
cur_cpi->ref_idx_to_skip = first_cpi->ref_refresh_index;
} else {
cur_cpi->ref_idx_to_skip = INVALID_IDX;
cur_cpi->ref_refresh_index = INVALID_IDX;
cur_cpi->refresh_idx_available = false;
}
cur_cpi->twopass_frame.stats_in = stats_in;
av1_get_ref_frames(first_ref_frame_map_pairs, cur_frame_disp, cur_cpi, i,
1, cur_cpi->common.remapped_ref_idx);
scale_references_fpmt(cur_cpi, ref_buffers_used_map);
parallel_frame_count++;
}
// Set do_frame_data_update to true for the last frame_parallel_level 2 // frame in the current parallel encode set. if (i == (gf_group->size - 1) ||
(gf_group->frame_parallel_level[i + 1] == 0 &&
(gf_group->update_type[i + 1] == ARF_UPDATE ||
gf_group->update_type[i + 1] == INTNL_ARF_UPDATE)) ||
gf_group->frame_parallel_level[i + 1] == 1) {
ppi->parallel_cpi[parallel_frame_count - 1]->do_frame_data_update = true; break;
}
}
// go through each OBUs while (remaining_size > 0) {
uint8_t saved_obu_header[2];
uint64_t obu_payload_size;
size_t length_of_payload_size;
size_t length_of_obu_size; const uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
size_t obu_bytes_read = obu_header_size; // bytes read for current obu
// save the obu header (1 or 2 bytes)
memcpy(saved_obu_header, buff_ptr, obu_header_size); // clear the obu_has_size_field
saved_obu_header[0] &= ~0x2;
// get the payload_size and length of payload_size if (aom_uleb_decode(buff_ptr + obu_header_size,
remaining_size - obu_header_size, &obu_payload_size,
&length_of_payload_size) != 0) { return AOM_CODEC_ERROR;
}
obu_bytes_read += length_of_payload_size;
// calculate the length of size of the obu header plus payload const uint64_t obu_size = obu_header_size + obu_payload_size;
length_of_obu_size = aom_uleb_size_in_bytes(obu_size);
if (length_of_obu_size + obu_header_size >
buffer_size - output_size - (remaining_size - obu_bytes_read)) { return AOM_CODEC_ERROR;
} // move the rest of data to new location
memmove(buff_ptr + length_of_obu_size + obu_header_size,
buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
obu_bytes_read += (size_t)obu_payload_size;
// write the new obu size
size_t coded_obu_size; if (aom_uleb_encode(obu_size, length_of_obu_size, buff_ptr,
&coded_obu_size) != 0 ||
coded_obu_size != length_of_obu_size) { return AOM_CODEC_ERROR;
}
// write the saved (modified) obu_header following obu size
memcpy(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
staticint rtc_set_references_external_ref_frame_config(AV1_COMP *cpi) { // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3), // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6). int ref = AOM_REFFRAME_ALL; for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { if (!cpi->ppi->rtc_ref.reference[i]) ref ^= (1 << i);
} return ref;
}
void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) { // TODO(yunqingwang): For what references to use, external encoding flags // should be consistent with internal reference frame selection. Need to // ensure that there is not conflict between the two. In AV1 encoder, the // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3, // GOLDEN, BWDREF, ALTREF2.
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.