/* * Copyright (c) 2019, 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.
*/
void av1_configure_buffer_updates(AV1_COMP *const cpi,
RefreshFrameInfo *const refresh_frame, const FRAME_UPDATE_TYPE type, const REFBUF_STATE refbuf_state, int force_refresh_all) { // NOTE(weitinglin): Should we define another function to take care of // cpi->rc.is_$Source_Type to make this function as it is in the comment? const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
&cpi->ext_flags.refresh_frame;
cpi->rc.is_src_frame_alt_ref = 0;
switch (type) { case KF_UPDATE:
set_refresh_frame_flags(refresh_frame, true, true, true); break;
case LF_UPDATE:
set_refresh_frame_flags(refresh_frame, false, false, false); break;
case GF_UPDATE:
set_refresh_frame_flags(refresh_frame, true, false, false); break;
case OVERLAY_UPDATE: if (refbuf_state == REFBUF_RESET)
set_refresh_frame_flags(refresh_frame, true, true, true); else
set_refresh_frame_flags(refresh_frame, true, false, false);
cpi->rc.is_src_frame_alt_ref = 1; break;
case ARF_UPDATE: // NOTE: BWDREF does not get updated along with ALTREF_FRAME. if (refbuf_state == REFBUF_RESET)
set_refresh_frame_flags(refresh_frame, true, true, true); else
set_refresh_frame_flags(refresh_frame, false, false, true);
break;
case INTNL_OVERLAY_UPDATE:
set_refresh_frame_flags(refresh_frame, false, false, false);
cpi->rc.is_src_frame_alt_ref = 1; break;
case INTNL_ARF_UPDATE:
set_refresh_frame_flags(refresh_frame, false, true, false); break;
default: assert(0); break;
}
if (ext_refresh_frame_flags->update_pending &&
(!is_stat_generation_stage(cpi))) {
set_refresh_frame_flags(refresh_frame,
ext_refresh_frame_flags->golden_frame,
ext_refresh_frame_flags->bwd_ref_frame,
ext_refresh_frame_flags->alt_ref_frame);
GF_GROUP *gf_group = &cpi->ppi->gf_group; if (ext_refresh_frame_flags->golden_frame)
gf_group->update_type[cpi->gf_frame_index] = GF_UPDATE; if (ext_refresh_frame_flags->alt_ref_frame)
gf_group->update_type[cpi->gf_frame_index] = ARF_UPDATE; if (ext_refresh_frame_flags->bwd_ref_frame)
gf_group->update_type[cpi->gf_frame_index] = INTNL_ARF_UPDATE;
}
if (force_refresh_all)
set_refresh_frame_flags(refresh_frame, true, true, true);
}
staticvoid set_additional_frame_flags(const AV1_COMMON *const cm, unsignedint *const frame_flags) { if (frame_is_intra_only(cm)) {
*frame_flags |= FRAMEFLAGS_INTRAONLY;
} if (frame_is_sframe(cm)) {
*frame_flags |= FRAMEFLAGS_SWITCH;
} if (cm->features.error_resilient_mode) {
*frame_flags |= FRAMEFLAGS_ERROR_RESILIENT;
}
}
staticvoid set_ext_overrides(AV1_COMMON *const cm,
EncodeFrameParams *const frame_params,
ExternalFlags *const ext_flags) { // Overrides the defaults with the externally supplied values with // av1_update_reference() and av1_update_entropy() calls // Note: The overrides are valid only for the next frame passed // to av1_encode_lowlevel()
if (ext_flags->use_s_frame) {
frame_params->frame_type = S_FRAME;
}
frame_params->error_resilient_mode = ext_flags->use_error_resilient; // A keyframe is already error resilient and keyframes with // error_resilient_mode interferes with the use of show_existing_frame // when forward reference keyframes are enabled.
frame_params->error_resilient_mode &= frame_params->frame_type != KEY_FRAME; // For bitstream conformance, s-frames must be error-resilient
frame_params->error_resilient_mode |= frame_params->frame_type == S_FRAME;
}
// In large scale case, always use Last frame's frame contexts. // Note(yunqing): In other cases, primary_ref_frame is chosen based on // cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index], which also controls // frame bit allocation. if (cm->tiles.large_scale) return (LAST_FRAME - LAST_FRAME);
if (cpi->ppi->use_svc || cpi->ppi->rtc_ref.set_ref_frame_config) return av1_svc_primary_ref_frame(cpi);
// Find the most recent reference frame with the same reference type as the // current frame constint current_ref_type = get_current_frame_ref_type(cpi); int wanted_fb = cpi->ppi->fb_of_context_type[current_ref_type]; #if CONFIG_FPMT_TEST if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) {
GF_GROUP *const gf_group = &cpi->ppi->gf_group; if (gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) { int frame_level = gf_group->frame_parallel_level[cpi->gf_frame_index]; // Book keep wanted_fb of frame_parallel_level 1 frame in an FP2 set. if (frame_level == 1) {
cpi->wanted_fb = wanted_fb;
} // Use the wanted_fb of level 1 frame in an FP2 for a level 2 frame in the // set. if (frame_level == 2 &&
gf_group->update_type[cpi->gf_frame_index - 1] == INTNL_ARF_UPDATE) {
assert(gf_group->frame_parallel_level[cpi->gf_frame_index - 1] == 1);
wanted_fb = cpi->wanted_fb;
}
}
} #endif// CONFIG_FPMT_TEST int primary_ref_frame = PRIMARY_REF_NONE; for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) { if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) {
primary_ref_frame = ref_frame - LAST_FRAME;
}
}
if (cpi->ppi->use_svc && cpi->ppi->rtc_ref.set_ref_frame_config &&
cpi->svc.number_spatial_layers > 1) { // ts_start is the timestamp for the current frame and ts_end is the // expected next timestamp given the duration passed into codec_encode(). // See the setting in encoder_encode() in av1_cx_iface.c: // ts_start = timebase_units_to_ticks(cpi_data.timestamp_ratio, ptsvol), // ts_end = timebase_units_to_ticks(cpi_data.timestamp_ratio, ptsvol + // duration). So the difference ts_end - ts_start is the duration passed // in by the user. For spatial layers SVC set the framerate based directly // on the duration, and bypass the adjustments below.
this_duration = ts_end - ts_start; if (this_duration > 0) {
cpi->new_framerate = 10000000.0 / this_duration;
av1_new_framerate(cpi, cpi->new_framerate);
time_stamps->prev_ts_start = ts_start;
time_stamps->prev_ts_end = ts_end; return;
}
}
// Determine whether there is a forced keyframe pending in the lookahead buffer int is_forced_keyframe_pending(struct lookahead_ctx *lookahead, constint up_to_index, const COMPRESSOR_STAGE compressor_stage) { for (int i = 0; i <= up_to_index; i++) { conststruct lookahead_entry *e =
av1_lookahead_peek(lookahead, i, compressor_stage); if (e == NULL) { // We have reached the end of the lookahead buffer and not early-returned // so there isn't a forced key-frame pending. return -1;
} elseif (e->flags == AOM_EFLAG_FORCE_KF) { return i;
} else { continue;
}
} return -1; // Never reached
}
// Check if we should encode an ARF or internal ARF. If not, try a LAST // Do some setup associated with the chosen source // temporal_filtered, flush, and frame_update_type are outputs. // Return the frame source, or NULL if we couldn't find one staticstruct lookahead_entry *choose_frame_source(
AV1_COMP *const cpi, int *const flush, int *pop_lookahead, struct lookahead_entry **last_source, int *const show_frame) {
AV1_COMMON *const cm = &cpi->common; const GF_GROUP *const gf_group = &cpi->ppi->gf_group; struct lookahead_entry *source = NULL;
// Source index in lookahead buffer. int src_index = gf_group->arf_src_offset[cpi->gf_frame_index];
// TODO(Aasaipriya): Forced key frames need to be fixed when rc_mode != AOM_Q if (src_index &&
(is_forced_keyframe_pending(cpi->ppi->lookahead, src_index,
cpi->compressor_stage) != -1) &&
cpi->oxcf.rc_cfg.mode != AOM_Q && !is_stat_generation_stage(cpi)) {
src_index = 0;
*flush = 1;
}
// If the current frame is arf, then we should not pop from the lookahead // buffer. If the current frame is not arf, then pop it. This assumes the // first frame in the GF group is not arf. May need to change if it is not // true.
*pop_lookahead = (src_index == 0); // If this is a key frame and keyframe filtering is enabled with overlay, // then do not pop. if (*pop_lookahead && cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1 &&
gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE &&
!is_stat_generation_stage(cpi) && cpi->ppi->lookahead) { if (cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz &&
(*flush ||
cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].sz ==
cpi->ppi->lookahead->read_ctxs[cpi->compressor_stage].pop_sz)) {
*pop_lookahead = 0;
}
}
// LAP stage does not have ARFs or forward key-frames, // hence, always pop_lookahead here. if (is_stat_generation_stage(cpi)) {
*pop_lookahead = 1;
src_index = 0;
}
*show_frame = *pop_lookahead;
#if CONFIG_FPMT_TEST if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_ENCODE) { #else
{ #endif// CONFIG_FPMT_TEST // Future frame in parallel encode set if (gf_group->src_offset[cpi->gf_frame_index] != 0 &&
!is_stat_generation_stage(cpi))
src_index = gf_group->src_offset[cpi->gf_frame_index];
} if (*show_frame) { // show frame, pop from buffer // Get last frame source. if (cm->current_frame.frame_number > 0) {
*last_source = av1_lookahead_peek(cpi->ppi->lookahead, src_index - 1,
cpi->compressor_stage);
} // Read in the source frame.
source = av1_lookahead_peek(cpi->ppi->lookahead, src_index,
cpi->compressor_stage);
} else { // no show frames are arf frames
source = av1_lookahead_peek(cpi->ppi->lookahead, src_index,
cpi->compressor_stage); if (source != NULL) {
cm->showable_frame = 1;
}
} return source;
}
// Don't allow a show_existing_frame to coincide with an error resilient or // S-Frame. An exception can be made in the case of a keyframe, since it does // not depend on any previous frames. staticint allow_show_existing(const AV1_COMP *const cpi, unsignedint frame_flags) { if (cpi->common.current_frame.frame_number == 0) return 0;
staticint get_refresh_idx(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], int update_arf, GF_GROUP *gf_group, int gf_index, int enable_refresh_skip, int cur_frame_disp) { int arf_count = 0; int oldest_arf_order = INT32_MAX; int oldest_arf_idx = -1;
int oldest_frame_order = INT32_MAX; int oldest_idx = -1;
for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) {
RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx]; if (ref_pair.disp_order == -1) continue; constint frame_order = ref_pair.disp_order; constint reference_frame_level = ref_pair.pyr_level; // Keep future frames and three closest previous frames in output order. if (frame_order > cur_frame_disp - 3) continue;
if (enable_refresh_skip) { int skip_frame = 0; // Prevent refreshing a frame in gf_group->skip_frame_refresh. for (int i = 0; i < REF_FRAMES; i++) { int frame_to_skip = gf_group->skip_frame_refresh[gf_index][i]; if (frame_to_skip == INVALID_IDX) break; if (frame_order == frame_to_skip) {
skip_frame = 1; break;
}
} if (skip_frame) continue;
}
// Keep track of the oldest level 1 frame if the current frame is also level // 1. if (reference_frame_level == 1) { // If there are more than 2 level 1 frames in the reference list, // discard the oldest. if (frame_order < oldest_arf_order) {
oldest_arf_order = frame_order;
oldest_arf_idx = map_idx;
}
arf_count++; continue;
}
// Update the overall oldest reference frame. if (frame_order < oldest_frame_order) {
oldest_frame_order = frame_order;
oldest_idx = map_idx;
}
} if (update_arf && arf_count > 2) return oldest_arf_idx; if (oldest_idx >= 0) return oldest_idx; if (oldest_arf_idx >= 0) return oldest_arf_idx; if (oldest_idx == -1) {
assert(arf_count > 2 && enable_refresh_skip); return oldest_arf_idx;
}
assert(0 && "No valid refresh index found"); return -1;
}
// Computes the reference refresh index for INTNL_ARF_UPDATE frame. int av1_calc_refresh_idx_for_intnl_arf(
AV1_COMP *cpi, RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], int gf_index) {
GF_GROUP *const gf_group = &cpi->ppi->gf_group;
// Search for the open slot to store the current frame. int free_fb_index = get_free_ref_map_index(ref_frame_map_pairs);
// Use a free slot if available. if (free_fb_index != INVALID_IDX) { return free_fb_index;
} else { int enable_refresh_skip = !is_one_pass_rt_params(cpi); int refresh_idx =
get_refresh_idx(ref_frame_map_pairs, 0, gf_group, gf_index,
enable_refresh_skip, gf_group->display_idx[gf_index]); return refresh_idx;
}
}
int av1_get_refresh_frame_flags( const AV1_COMP *const cpi, const EncodeFrameParams *const frame_params,
FRAME_UPDATE_TYPE frame_update_type, int gf_index, int cur_disp_order,
RefFrameMapPair ref_frame_map_pairs[REF_FRAMES]) { const AV1_COMMON *const cm = &cpi->common; const ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags =
&cpi->ext_flags.refresh_frame;
GF_GROUP *gf_group = &cpi->ppi->gf_group; if (gf_group->refbuf_state[gf_index] == REFBUF_RESET) return SELECT_ALL_BUF_SLOTS;
// TODO(jingning): Deprecate the following operations. // Switch frames and shown key-frames overwrite all reference slots if (frame_params->frame_type == S_FRAME) return SELECT_ALL_BUF_SLOTS;
// show_existing_frames don't actually send refresh_frame_flags so set the // flags to 0 to keep things consistent. if (frame_params->show_existing_frame) return 0;
#if !CONFIG_REALTIME_ONLY if (cpi->use_ducky_encode &&
cpi->ducky_encode_info.frame_info.gop_mode == DUCKY_ENCODE_GOP_MODE_RCL) { int new_fb_map_idx = cpi->ppi->gf_group.update_ref_idx[gf_index]; if (new_fb_map_idx == INVALID_IDX) return 0; return 1 << new_fb_map_idx;
} #endif// !CONFIG_REALTIME_ONLY
int refresh_mask = 0; if (ext_refresh_frame_flags->update_pending) { if (rtc_ref->set_ref_frame_config ||
use_rtc_reference_structure_one_layer(cpi)) { for (unsignedint i = 0; i < INTER_REFS_PER_FRAME; i++) { int ref_frame_map_idx = rtc_ref->ref_idx[i];
refresh_mask |= rtc_ref->refresh[ref_frame_map_idx]
<< ref_frame_map_idx;
} return refresh_mask;
} // Unfortunately the encoder interface reflects the old refresh_*_frame // flags so we have to replicate the old refresh_frame_flags logic here in // order to preserve the behaviour of the flag overrides. int ref_frame_map_idx = get_ref_frame_map_idx(cm, LAST_FRAME); if (ref_frame_map_idx != INVALID_IDX)
refresh_mask |= ext_refresh_frame_flags->last_frame << ref_frame_map_idx;
// Search for the open slot to store the current frame. int free_fb_index = get_free_ref_map_index(ref_frame_map_pairs);
// No refresh necessary for these frame types. if (frame_update_type == OVERLAY_UPDATE ||
frame_update_type == INTNL_OVERLAY_UPDATE) return refresh_mask;
// If there is an open slot, refresh that one instead of replacing a // reference. if (free_fb_index != INVALID_IDX) {
refresh_mask = 1 << free_fb_index; return refresh_mask;
} constint enable_refresh_skip = !is_one_pass_rt_params(cpi); constint update_arf = frame_update_type == ARF_UPDATE; constint refresh_idx =
get_refresh_idx(ref_frame_map_pairs, update_arf, &cpi->ppi->gf_group,
gf_index, enable_refresh_skip, cur_disp_order); return 1 << refresh_idx;
}
#if !CONFIG_REALTIME_ONLY // Apply temporal filtering to source frames and encode the filtered frame. // If the current frame does not require filtering, this function is identical // to av1_encode() except that tpl is not performed. staticint denoise_and_encode(AV1_COMP *const cpi, uint8_t *const dest,
size_t dest_size,
EncodeFrameInput *const frame_input, const EncodeFrameParams *const frame_params,
size_t *const frame_size) { #if CONFIG_COLLECT_COMPONENT_TIMING if (cpi->oxcf.pass == 2) start_timing(cpi, denoise_and_encode_time); #endif const AV1EncoderConfig *const oxcf = &cpi->oxcf;
AV1_COMMON *const cm = &cpi->common;
// Decide whether to apply temporal filtering to the source frame. int apply_filtering =
av1_is_temporal_filter_on(oxcf) && !is_stat_generation_stage(cpi); if (update_type != KF_UPDATE && update_type != ARF_UPDATE && !is_second_arf) {
apply_filtering = 0;
} if (apply_filtering) { if (frame_params->frame_type == KEY_FRAME) { // TODO(angiebird): Move the noise level check to av1_tf_info_filtering. // Decide whether it is allowed to perform key frame filtering int allow_kf_filtering = oxcf->kf_cfg.enable_keyframe_filtering &&
!frame_params->show_existing_frame &&
!is_lossless_requested(&oxcf->rc_cfg); if (allow_kf_filtering) { double y_noise_level = 0.0;
av1_estimate_noise_level(
frame_input->source, &y_noise_level, AOM_PLANE_Y, AOM_PLANE_Y,
cm->seq_params->bit_depth, NOISE_ESTIMATION_EDGE_THRESHOLD);
apply_filtering = y_noise_level > 0;
} else {
apply_filtering = 0;
} // If we are doing kf filtering, set up a few things. if (apply_filtering) {
av1_setup_past_independence(cm);
}
} elseif (is_second_arf) {
apply_filtering = cpi->sf.hl_sf.second_alt_ref_filtering;
}
}
#if CONFIG_COLLECT_COMPONENT_TIMING if (cpi->oxcf.pass == 2) start_timing(cpi, apply_filtering_time); #endif // Save the pointer to the original source image.
YV12_BUFFER_CONFIG *source_buffer = frame_input->source; // apply filtering to frame if (apply_filtering) { int show_existing_alt_ref = 0;
FRAME_DIFF frame_diff; int top_index = 0; int bottom_index = 0; constint q_index = av1_rc_pick_q_and_bounds(
cpi, cpi->oxcf.frm_dim_cfg.width, cpi->oxcf.frm_dim_cfg.height,
cpi->gf_frame_index, &bottom_index, &top_index);
// TODO(bohanli): figure out why we need frame_type in cm here.
cm->current_frame.frame_type = frame_params->frame_type; if (update_type == KF_UPDATE || update_type == ARF_UPDATE) {
YV12_BUFFER_CONFIG *tf_buf = av1_tf_info_get_filtered_buf(
&cpi->ppi->tf_info, cpi->gf_frame_index, &frame_diff); if (tf_buf != NULL) {
frame_input->source = tf_buf;
show_existing_alt_ref = av1_check_show_filtered_frame(
tf_buf, &frame_diff, q_index, cm->seq_params->bit_depth); if (show_existing_alt_ref) {
cpi->common.showable_frame |= 1;
} else {
cpi->common.showable_frame = 0;
}
} if (gf_group->frame_type[cpi->gf_frame_index] != KEY_FRAME) {
cpi->ppi->show_existing_alt_ref = show_existing_alt_ref;
}
}
if (is_second_arf) { // Allocate the memory for tf_buf_second_arf buffer, only when it is // required. int ret = aom_realloc_frame_buffer(
&cpi->ppi->tf_info.tf_buf_second_arf, oxcf->frm_dim_cfg.width,
oxcf->frm_dim_cfg.height, cm->seq_params->subsampling_x,
cm->seq_params->subsampling_y, cm->seq_params->use_highbitdepth,
cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
NULL, cpi->alloc_pyramid, 0); if (ret)
aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, "Failed to allocate tf_buf_second_arf");
YV12_BUFFER_CONFIG *tf_buf_second_arf =
&cpi->ppi->tf_info.tf_buf_second_arf; // We didn't apply temporal filtering for second arf ahead in // av1_tf_info_filtering(). constint arf_src_index = gf_group->arf_src_offset[cpi->gf_frame_index]; // Right now, we are still using tf_buf_second_arf due to // implementation complexity. // TODO(angiebird): Reuse tf_info->tf_buf here.
av1_temporal_filter(cpi, arf_src_index, cpi->gf_frame_index, &frame_diff,
tf_buf_second_arf);
show_existing_alt_ref = av1_check_show_filtered_frame(
tf_buf_second_arf, &frame_diff, q_index, cm->seq_params->bit_depth); if (show_existing_alt_ref) {
aom_extend_frame_borders(tf_buf_second_arf, av1_num_planes(cm));
frame_input->source = tf_buf_second_arf;
} // Currently INTNL_ARF_UPDATE only do show_existing.
cpi->common.showable_frame |= 1;
}
// Copy source metadata to the temporal filtered frame if (source_buffer->metadata &&
aom_copy_metadata_to_frame_buffer(frame_input->source,
source_buffer->metadata)) {
aom_internal_error(
cm->error, AOM_CODEC_MEM_ERROR, "Failed to copy source metadata to the temporal filtered frame");
}
} #if CONFIG_COLLECT_COMPONENT_TIMING if (cpi->oxcf.pass == 2) end_timing(cpi, apply_filtering_time); #endif
int set_mv_params = frame_params->frame_type == KEY_FRAME ||
update_type == ARF_UPDATE || update_type == GF_UPDATE;
cm->show_frame = frame_params->show_frame;
cm->current_frame.frame_type = frame_params->frame_type; // TODO(bohanli): Why is this? what part of it is necessary?
av1_set_frame_size(cpi, cm->width, cm->height); if (set_mv_params) av1_set_mv_search_params(cpi);
#if CONFIG_RD_COMMAND if (frame_params->frame_type == KEY_FRAME) { char filepath[] = "rd_command.txt";
av1_read_rd_command(filepath, &cpi->rd_command);
} #endif// CONFIG_RD_COMMAND if (cpi->gf_frame_index == 0 && !is_stat_generation_stage(cpi)) { // perform tpl after filtering int allow_tpl =
oxcf->gf_cfg.lag_in_frames > 1 && oxcf->algo_cfg.enable_tpl_model; if (gf_group->size > MAX_LENGTH_TPL_FRAME_STATS) {
allow_tpl = 0;
} if (frame_params->frame_type != KEY_FRAME) { // In rare case, it's possible to have non ARF/GF update_type here. // We should set allow_tpl to zero in the situation
allow_tpl =
allow_tpl && (update_type == ARF_UPDATE || update_type == GF_UPDATE ||
(cpi->use_ducky_encode &&
cpi->ducky_encode_info.frame_info.gop_mode ==
DUCKY_ENCODE_GOP_MODE_RCL));
}
// Set frame_input source to true source for psnr calculation. if (apply_filtering && is_psnr_calc_enabled(cpi)) {
cpi->source = av1_realloc_and_scale_if_required(
cm, source_buffer, &cpi->scaled_source, cm->features.interp_filter, 0, false, true, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid);
cpi->unscaled_source = source_buffer;
} #if CONFIG_COLLECT_COMPONENT_TIMING if (cpi->oxcf.pass == 2) end_timing(cpi, denoise_and_encode_time); #endif return AOM_CODEC_OK;
} #endif// !CONFIG_REALTIME_ONLY
/*!\cond */ // Struct to keep track of relevant reference frame data. typedefstruct { int map_idx; int disp_order; int pyr_level; int used;
} RefBufMapData; /*!\endcond */
// Checks to see if a particular reference frame is already in the reference // frame map. staticint is_in_ref_map(RefBufMapData *map, int disp_order, int n_frames) { for (int i = 0; i < n_frames; i++) { if (disp_order == map[i].disp_order) return 1;
} return 0;
}
// Add a reference buffer index to a named reference slot. staticvoid add_ref_to_slot(RefBufMapData *ref, int *const remapped_ref_idx, int frame) {
remapped_ref_idx[frame - LAST_FRAME] = ref->map_idx;
ref->used = 1;
}
// Threshold dictating when we are allowed to start considering // leaving lowest level frames unmapped. #define LOW_LEVEL_FRAMES_TR 5
// Find which reference buffer should be left out of the named mapping. // This is because there are 8 reference buffers and only 7 named slots. staticvoid set_unmapped_ref(RefBufMapData *buffer_map, int n_bufs, int n_min_level_refs, int min_level, int cur_frame_disp) { int max_dist = 0; int unmapped_idx = -1; if (n_bufs <= ALTREF_FRAME) return; for (int i = 0; i < n_bufs; i++) { if (buffer_map[i].used) continue; if (buffer_map[i].pyr_level != min_level ||
n_min_level_refs >= LOW_LEVEL_FRAMES_TR) { int dist = abs(cur_frame_disp - buffer_map[i].disp_order); if (dist > max_dist) {
max_dist = dist;
unmapped_idx = i;
}
}
}
assert(unmapped_idx >= 0 && "Unmapped reference not found");
buffer_map[unmapped_idx].used = 1;
}
void av1_get_ref_frames(RefFrameMapPair ref_frame_map_pairs[REF_FRAMES], int cur_frame_disp, const AV1_COMP *cpi, int gf_index, int is_parallel_encode, int remapped_ref_idx[REF_FRAMES]) { int buf_map_idx = 0;
// Initialize reference frame mappings. for (int i = 0; i < REF_FRAMES; ++i) remapped_ref_idx[i] = INVALID_IDX;
int valid_rf_idx = 0; staticconstint ref_frame_type_order[REF_FRAMES - LAST_FRAME] = {
GOLDEN_FRAME, ALTREF_FRAME, LAST_FRAME, BWDREF_FRAME,
ALTREF2_FRAME, LAST2_FRAME, LAST3_FRAME
}; for (int i = 0; i < REF_FRAMES - LAST_FRAME; i++) { int rf = ref_frame_type_order[i]; if (remapped_ref_idx[rf - LAST_FRAME] != INVALID_IDX) {
valid_rf_idx = remapped_ref_idx[rf - LAST_FRAME]; break;
}
}
for (int i = 0; i < REF_FRAMES; ++i) { if (remapped_ref_idx[i] == INVALID_IDX) {
remapped_ref_idx[i] = valid_rf_idx;
}
}
return;
} #endif// !CONFIG_REALTIME_ONLY
RefBufMapData buffer_map[REF_FRAMES]; int n_bufs = 0;
memset(buffer_map, 0, REF_FRAMES * sizeof(buffer_map[0])); int min_level = MAX_ARF_LAYERS; int max_level = 0;
GF_GROUP *gf_group = &cpi->ppi->gf_group; int skip_ref_unmapping = 0; int is_one_pass_rt = is_one_pass_rt_params(cpi);
// Go through current reference buffers and store display order, pyr level, // and map index. for (int map_idx = 0; map_idx < REF_FRAMES; map_idx++) { // Get reference frame buffer.
RefFrameMapPair ref_pair = ref_frame_map_pairs[map_idx]; if (ref_pair.disp_order == -1) continue; constint frame_order = ref_pair.disp_order; // Avoid duplicates. if (is_in_ref_map(buffer_map, frame_order, n_bufs)) continue; constint reference_frame_level = ref_pair.pyr_level;
// Keep track of the lowest and highest levels that currently exist. if (reference_frame_level < min_level) min_level = reference_frame_level; if (reference_frame_level > max_level) max_level = reference_frame_level;
int n_min_level_refs = 0; int closest_past_ref = -1; int golden_idx = -1; int altref_idx = -1;
// Find the GOLDEN_FRAME and BWDREF_FRAME. // Also collect various stats about the reference frames for the remaining // mappings. for (int i = n_bufs - 1; i >= 0; i--) { if (buffer_map[i].pyr_level == min_level) { // Keep track of the number of lowest level frames.
n_min_level_refs++; if (buffer_map[i].disp_order < cur_frame_disp && golden_idx == -1 &&
remapped_ref_idx[GOLDEN_FRAME - LAST_FRAME] == INVALID_IDX) { // Save index for GOLDEN.
golden_idx = i;
} elseif (buffer_map[i].disp_order > cur_frame_disp &&
altref_idx == -1 &&
remapped_ref_idx[ALTREF_FRAME - LAST_FRAME] == INVALID_IDX) { // Save index for ALTREF.
altref_idx = i;
}
} elseif (buffer_map[i].disp_order == cur_frame_disp) { // Map the BWDREF_FRAME if this is the show_existing_frame.
add_ref_to_slot(&buffer_map[i], remapped_ref_idx, BWDREF_FRAME);
}
// During parallel encodes of lower layer frames, exclude the first frame // (frame_parallel_level 1) from being used for the reference assignment of // the second frame (frame_parallel_level 2). if (!is_one_pass_rt && gf_group->frame_parallel_level[gf_index] == 2 &&
gf_group->frame_parallel_level[gf_index - 1] == 1 &&
gf_group->update_type[gf_index - 1] == INTNL_ARF_UPDATE) {
assert(gf_group->update_type[gf_index] == INTNL_ARF_UPDATE); #if CONFIG_FPMT_TEST
is_parallel_encode = (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_ENCODE)
? is_parallel_encode
: 0; #endif// CONFIG_FPMT_TEST // If parallel cpis are active, use ref_idx_to_skip, else, use display // index.
assert(IMPLIES(is_parallel_encode, cpi->ref_idx_to_skip != INVALID_IDX));
assert(IMPLIES(!is_parallel_encode,
gf_group->skip_frame_as_ref[gf_index] != INVALID_IDX));
buffer_map[i].used = is_parallel_encode
? (buffer_map[i].map_idx == cpi->ref_idx_to_skip)
: (buffer_map[i].disp_order ==
gf_group->skip_frame_as_ref[gf_index]); // In case a ref frame is excluded from being used during assignment, // skip the call to set_unmapped_ref(). Applicable in steady state. if (buffer_map[i].used) skip_ref_unmapping = 1;
}
// Keep track of where the frames change from being past frames to future // frames. if (buffer_map[i].disp_order < cur_frame_disp && closest_past_ref < 0)
closest_past_ref = i;
}
// Do not map GOLDEN and ALTREF based on their pyramid level if all reference // frames have the same level. if (n_min_level_refs <= n_bufs) { // Map the GOLDEN_FRAME. if (golden_idx > -1)
add_ref_to_slot(&buffer_map[golden_idx], remapped_ref_idx, GOLDEN_FRAME); // Map the ALTREF_FRAME. if (altref_idx > -1)
add_ref_to_slot(&buffer_map[altref_idx], remapped_ref_idx, ALTREF_FRAME);
}
// Find the buffer to be excluded from the mapping. if (!skip_ref_unmapping)
set_unmapped_ref(buffer_map, n_bufs, n_min_level_refs, min_level,
cur_frame_disp);
// Place past frames in LAST_FRAME, LAST2_FRAME, and LAST3_FRAME. for (int frame = LAST_FRAME; frame < GOLDEN_FRAME; frame++) { // Continue if the current ref slot is already full. if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; // Find the next unmapped reference buffer // in decreasing ouptut order relative to current picture. int next_buf_max = 0; int next_disp_order = INT_MIN; for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) { if (!buffer_map[buf_map_idx].used &&
buffer_map[buf_map_idx].disp_order < cur_frame_disp &&
buffer_map[buf_map_idx].disp_order > next_disp_order) {
next_disp_order = buffer_map[buf_map_idx].disp_order;
next_buf_max = buf_map_idx;
}
}
buf_map_idx = next_buf_max; if (buf_map_idx < 0) break; if (buffer_map[buf_map_idx].used) break;
add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
}
// Place future frames (if there are any) in BWDREF_FRAME and ALTREF2_FRAME. for (int frame = BWDREF_FRAME; frame < REF_FRAMES; frame++) { // Continue if the current ref slot is already full. if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; // Find the next unmapped reference buffer // in increasing ouptut order relative to current picture. int next_buf_max = 0; int next_disp_order = INT_MAX; for (buf_map_idx = n_bufs - 1; buf_map_idx >= 0; buf_map_idx--) { if (!buffer_map[buf_map_idx].used &&
buffer_map[buf_map_idx].disp_order > cur_frame_disp &&
buffer_map[buf_map_idx].disp_order < next_disp_order) {
next_disp_order = buffer_map[buf_map_idx].disp_order;
next_buf_max = buf_map_idx;
}
}
buf_map_idx = next_buf_max; if (buf_map_idx < 0) break; if (buffer_map[buf_map_idx].used) break;
add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
}
// Place remaining past frames.
buf_map_idx = closest_past_ref; for (int frame = LAST_FRAME; frame < REF_FRAMES; frame++) { // Continue if the current ref slot is already full. if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; // Find the next unmapped reference buffer. for (; buf_map_idx >= 0; buf_map_idx--) { if (!buffer_map[buf_map_idx].used) break;
} if (buf_map_idx < 0) break; if (buffer_map[buf_map_idx].used) break;
add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
}
// Place remaining future frames.
buf_map_idx = n_bufs - 1; for (int frame = ALTREF_FRAME; frame >= LAST_FRAME; frame--) { // Continue if the current ref slot is already full. if (remapped_ref_idx[frame - LAST_FRAME] != INVALID_IDX) continue; // Find the next unmapped reference buffer. for (; buf_map_idx > closest_past_ref; buf_map_idx--) { if (!buffer_map[buf_map_idx].used) break;
} if (buf_map_idx < 0) break; if (buffer_map[buf_map_idx].used) break;
add_ref_to_slot(&buffer_map[buf_map_idx], remapped_ref_idx, frame);
}
// Fill any slots that are empty (should only happen for the first 7 frames). for (int i = 0; i < REF_FRAMES; ++i) if (remapped_ref_idx[i] == INVALID_IDX) remapped_ref_idx[i] = 0;
}
// Check if we need to stuff more src frames if (flush == 0) { int srcbuf_size =
av1_lookahead_depth(cpi->ppi->lookahead, cpi->compressor_stage); int pop_size =
av1_lookahead_pop_sz(cpi->ppi->lookahead, cpi->compressor_stage);
// Special handling to reset 'show_existing_frame' in case of dropped // frames. if (oxcf->rc_cfg.drop_frames_water_mark &&
(gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE ||
gf_group->update_type[cpi->gf_frame_index] == INTNL_OVERLAY_UPDATE)) { // During the encode of an OVERLAY_UPDATE/INTNL_OVERLAY_UPDATE frame, loop // over the gf group to check if the corresponding // ARF_UPDATE/INTNL_ARF_UPDATE frame was dropped. int cur_disp_idx = gf_group->display_idx[cpi->gf_frame_index]; for (int idx = 0; idx < cpi->gf_frame_index; idx++) { if (cur_disp_idx == gf_group->display_idx[idx]) {
assert(IMPLIES(
gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE,
gf_group->update_type[idx] == ARF_UPDATE));
assert(IMPLIES(gf_group->update_type[cpi->gf_frame_index] ==
INTNL_OVERLAY_UPDATE,
gf_group->update_type[idx] == INTNL_ARF_UPDATE)); // Reset show_existing_frame and set cpi->is_dropped_frame to true if // the frame was dropped during its first encode. if (gf_group->is_frame_dropped[idx]) {
frame_params.show_existing_frame = 0;
assert(!cpi->is_dropped_frame);
cpi->is_dropped_frame = true;
} break;
}
}
}
// Reset show_existing_alt_ref decision to 0 after it is used. if (gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE) {
cpi->ppi->show_existing_alt_ref = 0;
}
} else {
frame_params.show_existing_frame = 0;
}
if (source == NULL) { // If no source was found, we can't encode a frame. #if !CONFIG_REALTIME_ONLY if (flush && oxcf->pass == AOM_RC_FIRST_PASS &&
!cpi->ppi->twopass.first_pass_done) {
av1_end_first_pass(cpi); /* get last stats packet */
cpi->ppi->twopass.first_pass_done = 1;
} #endif return -1;
}
// reset src_offset to allow actual encode call for this frame to get its // source.
gf_group->src_offset[cpi->gf_frame_index] = 0;
// Source may be changed if temporal filtered later.
frame_input.source = &source->img; if ((cpi->ppi->use_svc || cpi->rc.prev_frame_is_dropped) &&
last_source != NULL)
av1_svc_set_last_source(cpi, &frame_input, &last_source->img); else
frame_input.last_source = last_source != NULL ? &last_source->img : NULL;
frame_input.ts_duration = source->ts_end - source->ts_start; // Save unfiltered source. It is used in av1_get_second_pass_params().
cpi->unfiltered_source = frame_input.source;
#if CONFIG_FPMT_TEST if (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) { if (cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) {
cpi->framerate = cpi->temp_framerate;
}
} #endif// CONFIG_FPMT_TEST
// Shown frames and arf-overlay frames need frame-rate considering if (frame_params.show_frame)
adjust_frame_rate(cpi, source->ts_start, source->ts_end);
if (!frame_params.show_existing_frame) { #if !CONFIG_REALTIME_ONLY if (cpi->film_grain_table) {
cm->cur_frame->film_grain_params_present = aom_film_grain_table_lookup(
cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
&cm->film_grain_params);
} else {
cm->cur_frame->film_grain_params_present =
cm->seq_params->film_grain_params_present;
} #endif // only one operating point supported now const int64_t pts64 = ticks_to_timebase_units(timestamp_ratio, *time_stamp); if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
if (frame_params.show_existing_frame &&
frame_params.frame_type != KEY_FRAME) { // Force show-existing frames to be INTER, except forward keyframes
frame_params.frame_type = INTER_FRAME;
}
// Per-frame encode speed. In theory this can vary, but things may have // been written assuming speed-level will not change within a sequence, so // this parameter should be used with caution.
frame_params.speed = oxcf->speed;
#if !CONFIG_REALTIME_ONLY // Set forced key frames when necessary. For two-pass encoding / lap mode, // this is already handled by av1_get_second_pass_params. However when no // stats are available, we still need to check if the new frame is a keyframe. // For one pass rt, this is already checked in av1_get_one_pass_rt_params. if (!use_one_pass_rt_params &&
(is_stat_generation_stage(cpi) || has_no_stats_stage(cpi))) { // Current frame is coded as a key-frame for any of the following cases: // 1) First frame of a video // 2) For all-intra frame encoding // 3) When a key-frame is forced constint kf_requested =
(cm->current_frame.frame_number == 0 ||
oxcf->kf_cfg.key_freq_max == 0 || (*frame_flags & FRAMEFLAGS_KEY)); if (kf_requested && frame_update_type != OVERLAY_UPDATE &&
frame_update_type != INTNL_OVERLAY_UPDATE) {
frame_params.frame_type = KEY_FRAME;
} elseif (is_stat_generation_stage(cpi)) { // For stats generation, set the frame type to inter here.
frame_params.frame_type = INTER_FRAME;
}
} #endif
// Work out some encoding parameters specific to the pass: if (has_no_stats_stage(cpi) && oxcf->q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
av1_cyclic_refresh_update_parameters(cpi);
} elseif (is_stat_generation_stage(cpi)) {
cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&oxcf->rc_cfg);
} elseif (is_stat_consumption_stage(cpi)) { #if CONFIG_MISMATCH_DEBUG
mismatch_move_frame_idx_w(); #endif #if TXCOEFF_COST_TIMER
cm->txcoeff_cost_timer = 0;
cm->txcoeff_cost_count = 0; #endif
}
if (!is_stat_generation_stage(cpi))
set_ext_overrides(cm, &frame_params, ext_flags);
// Shown keyframes and S frames refresh all reference buffers constint force_refresh_all =
((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) ||
frame_params.frame_type == S_FRAME) &&
!frame_params.show_existing_frame;
int get_ref_frames = 0; #if CONFIG_FPMT_TEST
get_ref_frames =
(cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 1 : 0; #endif// CONFIG_FPMT_TEST if (get_ref_frames ||
gf_group->frame_parallel_level[cpi->gf_frame_index] == 0) { if (!ext_flags->refresh_frame.update_pending) {
av1_get_ref_frames(ref_frame_map_pairs, cur_frame_disp, cpi,
cpi->gf_frame_index, 1, cm->remapped_ref_idx);
} elseif (cpi->ppi->rtc_ref.set_ref_frame_config ||
use_rtc_reference_structure_one_layer(cpi)) { for (unsignedint i = 0; i < INTER_REFS_PER_FRAME; i++)
cm->remapped_ref_idx[i] = cpi->ppi->rtc_ref.ref_idx[i];
}
}
// Get the reference frames bool has_ref_frames = false; for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { const RefCntBuffer *ref_frame =
get_ref_frame_buf(cm, ref_frame_priority_order[i]);
ref_frame_buf[i] = ref_frame != NULL ? &ref_frame->buf : NULL; if (ref_frame != NULL) has_ref_frames = true;
} if (!has_ref_frames && (frame_params.frame_type == INTER_FRAME ||
frame_params.frame_type == S_FRAME)) { return AOM_CODEC_ERROR;
}
// Work out which reference frame slots may be used.
frame_params.ref_frame_flags =
get_ref_frame_flags(&cpi->sf, is_one_pass_rt_params(cpi), ref_frame_buf,
ext_flags->ref_frame_flags);
// Set primary_ref_frame of non-reference frames as PRIMARY_REF_NONE. if (cpi->ppi->gf_group.is_frame_non_ref[cpi->gf_frame_index]) {
frame_params.primary_ref_frame = PRIMARY_REF_NONE;
} else {
frame_params.primary_ref_frame =
choose_primary_ref_frame(cpi, &frame_params);
}
// Call av1_get_refresh_frame_flags() if refresh index not available. if (!cpi->refresh_idx_available) {
frame_params.refresh_frame_flags = av1_get_refresh_frame_flags(
cpi, &frame_params, frame_update_type, cpi->gf_frame_index,
cur_frame_disp, ref_frame_map_pairs);
} else {
assert(cpi->ref_refresh_index != INVALID_IDX);
frame_params.refresh_frame_flags = (1 << cpi->ref_refresh_index);
}
// Make the frames marked as is_frame_non_ref to non-reference frames. if (gf_group->is_frame_non_ref[cpi->gf_frame_index])
frame_params.refresh_frame_flags = 0;
frame_params.existing_fb_idx_to_show = INVALID_IDX; // Find the frame buffer to show based on display order. if (frame_params.show_existing_frame) { for (int frame = 0; frame < REF_FRAMES; frame++) { const RefCntBuffer *const buf = cm->ref_frame_map[frame]; if (buf == NULL) continue; constint frame_order = (int)buf->display_order_hint; if (frame_order == cur_frame_disp)
frame_params.existing_fb_idx_to_show = frame;
}
}
}
// The way frame_params->remapped_ref_idx is setup is a placeholder. // Currently, reference buffer assignment is done by update_ref_frame_map() // which is called by high-level strategy AFTER encoding a frame. It // modifies cm->remapped_ref_idx. If you want to use an alternative method // to determine reference buffer assignment, just put your assignments into // frame_params->remapped_ref_idx here and they will be used when encoding // this frame. If frame_params->remapped_ref_idx is setup independently of // cm->remapped_ref_idx then update_ref_frame_map() will have no effect.
memcpy(frame_params.remapped_ref_idx, cm->remapped_ref_idx,
REF_FRAMES * sizeof(*cm->remapped_ref_idx));
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.