Merge one and two pass paths
This enables full pyramid for one-pass Q mode. A followup patch will
be submitted to enable full pyramid for VBR and CBR once the
bit allocations are finalized.
STATS_CHANGED
Q Mode: 11% gain on lowres speed 1
VBR Mode: 3% gain on lowres speed 1
CBR Mode: 6.7% gain on lowres speed 1
Change-Id: I5c730b76e32570f89e169081e3e077860a880adf
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 64ee5ce..de2e671 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -102,7 +102,7 @@
case INTNL_ARF_UPDATE:
frame_params->refresh_last_frame = 0;
frame_params->refresh_golden_frame = 0;
- if (cpi->oxcf.pass == 2) {
+ if (cpi->oxcf.pass != 1) {
frame_params->refresh_bwd_ref_frame = 1;
frame_params->refresh_alt2_ref_frame = 0;
} else {
@@ -172,7 +172,7 @@
}
#if !CONFIG_REALTIME_ONLY
-static INLINE void update_twopass_gf_group_index(AV1_COMP *cpi) {
+static INLINE void update_gf_group_index(AV1_COMP *cpi) {
// Increment the gf group index ready for the next frame. If this is
// a show_existing_frame with a source other than altref, or if it is not
// a displayed forward keyframe, the index was incremented when it was
@@ -188,7 +188,7 @@
update_keyframe_counters(cpi);
update_frames_till_gf_update(cpi);
#if !CONFIG_REALTIME_ONLY
- if (cpi->oxcf.pass == 2) update_twopass_gf_group_index(cpi);
+ update_gf_group_index(cpi);
#endif
}
@@ -499,7 +499,7 @@
static int get_arf_src_index(AV1_COMP *cpi) {
RATE_CONTROL *const rc = &cpi->rc;
int arf_src_index = 0;
- if (cpi->oxcf.pass == 2) {
+ if (cpi->oxcf.pass != 1) {
const GF_GROUP *const gf_group = &cpi->gf_group;
if (get_frame_update_type(cpi) == ARF_UPDATE) {
assert(is_altref_enabled(cpi));
@@ -515,7 +515,7 @@
// as the internal arf midpoint. Otherwise, returns 0.
static int get_internal_arf_src_index(AV1_COMP *cpi) {
int internal_arf_src_index = 0;
- if (cpi->oxcf.pass == 2) {
+ if (cpi->oxcf.pass != 1) {
const GF_GROUP *const gf_group = &cpi->gf_group;
if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
assert(is_altref_enabled(cpi) && cpi->internal_altref_allowed);
@@ -971,7 +971,7 @@
refresh_mask |= 1 << get_ref_frame_map_idx(cm, LAST3_FRAME);
break;
case INTNL_ARF_UPDATE:
- if (cpi->oxcf.pass == 2) {
+ if (cpi->oxcf.pass != 1) {
// Push the new ARF2 onto the bwdref stack. We refresh EXTREF which is
// at the bottom of the stack then move it to the top.
refresh_mask |= 1 << get_ref_frame_map_idx(cm, EXTREF_FRAME);
@@ -1000,6 +1000,10 @@
memset(&frame_params, 0, sizeof(frame_params));
memset(&frame_results, 0, sizeof(frame_results));
+ // TODO(sarahparker) finish bit allocation for one pass pyramid
+ if (oxcf->pass == 0 && oxcf->rc_mode != AOM_Q)
+ cpi->oxcf.gf_max_pyr_height = USE_ALTREF_FOR_ONE_PASS;
+
if (oxcf->pass == 0 || oxcf->pass == 2) {
check_show_existing_frame(cpi, &frame_params);
frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags);
@@ -1019,8 +1023,8 @@
&frame_update_type, &frame_params);
}
- // In pass 2 we get the frame_update_type from gf_group
- if (oxcf->pass == 2) {
+ // In pass 0 and 2, we get the frame_update_type from gf_group
+ if (oxcf->pass != 1) {
frame_update_type = get_frame_update_type(cpi);
}
@@ -1076,7 +1080,7 @@
}
#if !CONFIG_REALTIME_ONLY
- if (oxcf->pass == 2 && (!frame_params.show_existing_frame || is_overlay)) {
+ if (oxcf->pass != 1 && (!frame_params.show_existing_frame || is_overlay)) {
// GF_GROUP needs updating for arf overlays as well as non-show-existing
av1_get_second_pass_params(cpi, &frame_params, *frame_flags);
frame_update_type = get_frame_update_type(cpi);
@@ -1101,14 +1105,8 @@
frame_params.speed = oxcf->speed;
// Work out some encoding parameters specific to the pass:
- if (oxcf->pass == 0) {
- if (cpi->oxcf.rc_mode == AOM_CBR) {
- av1_rc_get_one_pass_cbr_params(cpi, &frame_update_type, &frame_params,
- *frame_flags);
- } else {
- av1_rc_get_one_pass_vbr_params(cpi, &frame_update_type, &frame_params,
- *frame_flags);
- }
+ if (cpi->oxcf.pass == 0 && cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
+ av1_cyclic_refresh_update_parameters(cpi);
} else if (oxcf->pass == 1) {
cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&cpi->oxcf);
const int kf_requested = (cm->current_frame.frame_number == 0 ||
@@ -1190,7 +1188,7 @@
AOM_CODEC_OK) {
return AOM_CODEC_ERROR;
}
- if (oxcf->pass == 2) cpi->num_gf_group_show_frames += frame_params.show_frame;
+ if (oxcf->pass != 1) cpi->num_gf_group_show_frames += frame_params.show_frame;
if (oxcf->pass == 0 || oxcf->pass == 2) {
// First pass doesn't modify reference buffer assignment or produce frame
@@ -1200,7 +1198,7 @@
}
#if !CONFIG_REALTIME_ONLY
- if (oxcf->pass == 2) {
+ if (oxcf->pass != 1) {
#if TXCOEFF_COST_TIMER
cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
fprintf(stderr,
@@ -1213,7 +1211,7 @@
}
#endif // !CONFIG_REALTIME_ONLY
- if (oxcf->pass == 0 || oxcf->pass == 2) {
+ if (oxcf->pass != 1) {
update_fb_of_context_type(cpi, &frame_params, cpi->fb_of_context_type);
set_additional_frame_flags(cm, frame_flags);
update_rc_counts(cpi);
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 9919cb3..32ad9a5 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -27,6 +27,9 @@
#include "av1/encoder/ratectrl.h"
#include "av1/encoder/use_flat_gop_model_params.h"
+#define DEFAULT_KF_BOOST 2000
+#define DEFAULT_GF_BOOST 2000
+
// Calculate an active area of the image that discounts formatting
// bars and partially discounts other 0 energy areas.
#define MIN_ACTIVE_AREA 0.5
@@ -773,6 +776,148 @@
rc->avg_frame_qindex[INTER_FRAME] = avg_frame_qindex;
}
+static int calc_pframe_target_size_one_pass_vbr(
+ const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
+ static const int af_ratio = 10;
+ const RATE_CONTROL *const rc = &cpi->rc;
+ int target;
+#if USE_ALTREF_FOR_ONE_PASS
+ if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
+ frame_update_type == ARF_UPDATE) {
+ target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
+ (rc->baseline_gf_interval + af_ratio - 1);
+ } else {
+ target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
+ (rc->baseline_gf_interval + af_ratio - 1);
+ }
+#else
+ target = rc->avg_frame_bandwidth;
+#endif
+ return av1_rc_clamp_pframe_target_size(cpi, target, frame_update_type);
+}
+
+static int calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
+ static const int kf_ratio = 25;
+ const RATE_CONTROL *rc = &cpi->rc;
+ const int target = rc->avg_frame_bandwidth * kf_ratio;
+ return av1_rc_clamp_iframe_target_size(cpi, target);
+}
+
+#define FRAME_OVERHEAD_BITS 200
+
+static int calc_pframe_target_size_one_pass_cbr(
+ const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
+ const AV1EncoderConfig *oxcf = &cpi->oxcf;
+ const RATE_CONTROL *rc = &cpi->rc;
+ const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
+ const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
+ int min_frame_target =
+ AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
+ int target;
+
+ if (oxcf->gf_cbr_boost_pct) {
+ const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
+ if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
+ target =
+ (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
+ (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
+ } else {
+ target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
+ (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
+ }
+ } else {
+ target = rc->avg_frame_bandwidth;
+ }
+
+ if (diff > 0) {
+ // Lower the target bandwidth for this frame.
+ const int pct_low = (int)AOMMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
+ target -= (target * pct_low) / 200;
+ } else if (diff < 0) {
+ // Increase the target bandwidth for this frame.
+ const int pct_high =
+ (int)AOMMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
+ target += (target * pct_high) / 200;
+ }
+ if (oxcf->rc_max_inter_bitrate_pct) {
+ const int max_rate =
+ rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
+ target = AOMMIN(target, max_rate);
+ }
+ return AOMMAX(min_frame_target, target);
+}
+
+static int calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
+ const RATE_CONTROL *rc = &cpi->rc;
+ int target;
+ if (cpi->common.current_frame.frame_number == 0) {
+ target = ((rc->starting_buffer_level / 2) > INT_MAX)
+ ? INT_MAX
+ : (int)(rc->starting_buffer_level / 2);
+ } else {
+ int kf_boost = 32;
+ double framerate = cpi->framerate;
+
+ kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
+ if (rc->frames_since_key < framerate / 2) {
+ kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
+ }
+ target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
+ }
+ return av1_rc_clamp_iframe_target_size(cpi, target);
+}
+
+static void define_gf_group_pass0(AV1_COMP *cpi,
+ const EncodeFrameParams *const frame_params) {
+ RATE_CONTROL *const rc = &cpi->rc;
+ GF_GROUP *const gf_group = &cpi->gf_group;
+ int target;
+
+ if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
+ av1_cyclic_refresh_set_golden_update(cpi);
+ else
+ rc->baseline_gf_interval = MAX_GF_INTERVAL;
+
+ if (rc->baseline_gf_interval > rc->frames_to_key)
+ rc->baseline_gf_interval = rc->frames_to_key;
+
+ rc->gfu_boost = DEFAULT_GF_BOOST;
+ rc->constrained_gf_group =
+ (rc->baseline_gf_interval >= rc->frames_to_key) ? 1 : 0;
+ const int use_alt_ref =
+ is_altref_enabled(cpi) &&
+ (rc->baseline_gf_interval < cpi->oxcf.lag_in_frames) &&
+ !(cpi->lookahead->sz < rc->baseline_gf_interval - 1) &&
+ (cpi->oxcf.gf_max_pyr_height > MIN_PYRAMID_LVL);
+ rc->source_alt_ref_pending = use_alt_ref;
+ cpi->preserve_arf_as_gld = use_alt_ref;
+
+ // Set up the structure of this Group-Of-Pictures (same as GF_GROUP)
+ av1_gop_setup_structure(cpi, frame_params);
+
+ if (cpi->oxcf.rc_mode == AOM_Q) av1_assign_q_and_bounds_q_mode(cpi);
+
+ // Allocate bits to each of the frames in the GF group.
+ // TODO(sarahparker) Extend this to work with pyramid structure.
+ for (int cur_index = 0; cur_index < gf_group->size; ++cur_index) {
+ const FRAME_UPDATE_TYPE cur_update_type = gf_group->update_type[cur_index];
+ if (cpi->oxcf.rc_mode == AOM_CBR) {
+ if (cur_update_type == KEY_FRAME) {
+ target = calc_iframe_target_size_one_pass_cbr(cpi);
+ } else {
+ target = calc_pframe_target_size_one_pass_cbr(cpi, cur_update_type);
+ }
+ } else {
+ if (cur_update_type == KEY_FRAME) {
+ target = calc_iframe_target_size_one_pass_vbr(cpi);
+ } else {
+ target = calc_pframe_target_size_one_pass_vbr(cpi, cur_update_type);
+ }
+ }
+ gf_group->bit_allocation[cur_index] = target;
+ }
+}
+
// Analyse and define a gf/arf group.
static void define_gf_group(AV1_COMP *cpi, FIRSTPASS_STATS *this_frame,
const EncodeFrameParams *const frame_params) {
@@ -828,6 +973,11 @@
aom_clear_system_state();
av1_zero(next_frame);
+ if (oxcf->pass == 0) {
+ define_gf_group_pass0(cpi, frame_params);
+ return;
+ }
+
// Load stats for the current frame.
mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame);
@@ -1333,15 +1483,42 @@
#define MIN_STATIC_KF_BOOST 5400 // Minimum boost for static KF interval
static void find_next_key_frame(AV1_COMP *cpi, FIRSTPASS_STATS *this_frame) {
- int i, j;
RATE_CONTROL *const rc = &cpi->rc;
TWO_PASS *const twopass = &cpi->twopass;
GF_GROUP *const gf_group = &cpi->gf_group;
+ AV1_COMMON *const cm = &cpi->common;
+ CurrentFrame *const current_frame = &cm->current_frame;
const AV1EncoderConfig *const oxcf = &cpi->oxcf;
const FIRSTPASS_STATS first_frame = *this_frame;
- const FIRSTPASS_STATS *const start_position = twopass->stats_in;
FIRSTPASS_STATS next_frame;
FIRSTPASS_STATS last_frame;
+ av1_zero(next_frame);
+
+ rc->frames_since_key = 0;
+
+ // Reset the GF group data structures.
+ av1_zero(*gf_group);
+
+ // Clear the alt ref active flag and last group multi arf flags as they
+ // can never be set for a key frame.
+ rc->source_alt_ref_active = 0;
+
+ // KF is always a GF so clear frames till next gf counter.
+ rc->frames_till_gf_update_due = 0;
+
+ rc->frames_to_key = 1;
+
+ if (cpi->oxcf.pass == 0) {
+ rc->this_key_frame_forced =
+ current_frame->frame_number != 0 && rc->frames_to_key == 0;
+ rc->frames_to_key = cpi->oxcf.key_freq;
+ rc->kf_boost = DEFAULT_KF_BOOST;
+ rc->source_alt_ref_active = 0;
+ gf_group->update_type[0] = KF_UPDATE;
+ return;
+ }
+ int i, j;
+ const FIRSTPASS_STATS *const start_position = twopass->stats_in;
int kf_bits = 0;
int loop_decay_counter = 0;
double decay_accumulator = 1.0;
@@ -1352,25 +1529,9 @@
double kf_group_err = 0.0;
double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
- av1_zero(next_frame);
-
- rc->frames_since_key = 0;
-
- // Reset the GF group data structures.
- av1_zero(*gf_group);
-
// Is this a forced key frame by interval.
rc->this_key_frame_forced = rc->next_key_frame_forced;
- // Clear the alt ref active flag and last group multi arf flags as they
- // can never be set for a key frame.
- rc->source_alt_ref_active = 0;
-
- // KF is always a GF so clear frames till next gf counter.
- rc->frames_till_gf_update_due = 0;
-
- rc->frames_to_key = 1;
-
twopass->kf_group_bits = 0; // Total bits available to kf group
twopass->kf_group_error_left = 0; // Group modified error score.
@@ -1582,6 +1743,7 @@
}
static int is_skippable_frame(const AV1_COMP *cpi) {
+ if (cpi->oxcf.pass == 0) return 0;
// If the current frame does not have non-zero motion vector detected in the
// first pass, and so do its previous and forward frames, then this frame
// can be skipped for partition check, and the partition size is assigned
@@ -1606,57 +1768,17 @@
#endif
#define DEFAULT_GRP_WEIGHT 1.0
-void av1_get_second_pass_params(AV1_COMP *cpi,
- EncodeFrameParams *const frame_params,
- unsigned int frame_flags) {
+static void process_first_pass_stats(AV1_COMP *cpi,
+ FIRSTPASS_STATS *this_frame) {
AV1_COMMON *const cm = &cpi->common;
CurrentFrame *const current_frame = &cm->current_frame;
RATE_CONTROL *const rc = &cpi->rc;
TWO_PASS *const twopass = &cpi->twopass;
- GF_GROUP *const gf_group = &cpi->gf_group;
- int frames_left;
- FIRSTPASS_STATS this_frame;
- int target_rate;
+ if (cpi->oxcf.rc_mode != AOM_Q && current_frame->frame_number == 0) {
+ const int frames_left =
+ (int)(twopass->total_stats.count - current_frame->frame_number);
- frames_left = (int)(twopass->total_stats.count - current_frame->frame_number);
-
- if (!twopass->stats_in) return;
-
- if (rc->frames_till_gf_update_due > 0) {
- assert(gf_group->index < gf_group->size);
- const int update_type = gf_group->update_type[gf_group->index];
-
- // If this is an arf frame then we dont want to read the stats file or
- // advance the input pointer as we already have what we need.
- if (update_type == ARF_UPDATE || update_type == INTNL_ARF_UPDATE) {
- target_rate = gf_group->bit_allocation[gf_group->index];
- target_rate =
- av1_rc_clamp_pframe_target_size(cpi, target_rate, update_type);
- rc->base_frame_target = target_rate;
-
- if (cpi->no_show_kf) {
- assert(update_type == ARF_UPDATE);
- frame_params->frame_type = KEY_FRAME;
- } else {
- frame_params->frame_type = INTER_FRAME;
- }
-
- // Do the firstpass stats indicate that this frame is skippable for the
- // partition search?
- if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2) {
- cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
- }
-
- return;
- }
- }
-
- aom_clear_system_state();
-
- if (cpi->oxcf.rc_mode == AOM_Q) {
- rc->active_worst_quality = cpi->oxcf.cq_level;
- } else if (current_frame->frame_number == 0) {
// Special case code for first frame.
const int section_target_bandwidth =
(int)(twopass->bits_left / frames_left);
@@ -1681,14 +1803,96 @@
rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
}
- av1_zero(this_frame);
- if (EOF == input_stats(twopass, &this_frame)) return;
+ if (EOF == input_stats(twopass, this_frame)) return;
+
+ {
+ const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
+ ? cpi->initial_mbs
+ : cpi->common.MBs;
+ // The multiplication by 256 reverses a scaling factor of (>> 8)
+ // applied when combining MB error values for the frame.
+ twopass->mb_av_energy = log((this_frame->intra_error / num_mbs) + 1.0);
+ twopass->frame_avg_haar_energy =
+ log((this_frame->frame_avg_wavelet_energy / num_mbs) + 1.0);
+ }
+
+ // Update the total stats remaining structure.
+ subtract_stats(&twopass->total_left_stats, this_frame);
// Set the frame content type flag.
- if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
+ if (this_frame->intra_skip_pct >= FC_ANIMATION_THRESH)
twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
else
twopass->fr_content_type = FC_NORMAL;
+}
+
+static void setup_target_rate(AV1_COMP *cpi, FRAME_TYPE frame_type) {
+ RATE_CONTROL *const rc = &cpi->rc;
+ GF_GROUP *const gf_group = &cpi->gf_group;
+
+ int target_rate = gf_group->bit_allocation[gf_group->index];
+
+ if (cpi->oxcf.pass == 0) {
+ av1_rc_set_frame_target(cpi, target_rate, cpi->common.width,
+ cpi->common.height);
+ } else {
+ if (frame_type == KEY_FRAME) {
+ target_rate = av1_rc_clamp_iframe_target_size(cpi, target_rate);
+ } else {
+ target_rate = av1_rc_clamp_pframe_target_size(
+ cpi, target_rate, gf_group->update_type[gf_group->index]);
+ }
+ }
+
+ rc->base_frame_target = target_rate;
+}
+
+void av1_get_second_pass_params(AV1_COMP *cpi,
+ EncodeFrameParams *const frame_params,
+ unsigned int frame_flags) {
+ RATE_CONTROL *const rc = &cpi->rc;
+ TWO_PASS *const twopass = &cpi->twopass;
+ GF_GROUP *const gf_group = &cpi->gf_group;
+
+ if (cpi->oxcf.pass == 2 && !twopass->stats_in) return;
+
+ if (rc->frames_till_gf_update_due > 0) {
+ assert(gf_group->index < gf_group->size);
+ const int update_type = gf_group->update_type[gf_group->index];
+
+ setup_target_rate(cpi, frame_params->frame_type);
+
+ // If this is an arf frame then we dont want to read the stats file or
+ // advance the input pointer as we already have what we need.
+ if (update_type == ARF_UPDATE || update_type == INTNL_ARF_UPDATE) {
+ if (cpi->no_show_kf) {
+ assert(update_type == ARF_UPDATE);
+ frame_params->frame_type = KEY_FRAME;
+ } else {
+ frame_params->frame_type = INTER_FRAME;
+ }
+
+ // Do the firstpass stats indicate that this frame is skippable for the
+ // partition search?
+ if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2) {
+ cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
+ }
+
+ return;
+ }
+ }
+
+ aom_clear_system_state();
+
+ if (cpi->oxcf.rc_mode == AOM_Q) rc->active_worst_quality = cpi->oxcf.cq_level;
+ FIRSTPASS_STATS this_frame;
+ av1_zero(this_frame);
+ // call above fn
+ if (cpi->oxcf.pass == 2) {
+ process_first_pass_stats(cpi, &this_frame);
+ } else {
+ rc->active_worst_quality = cpi->oxcf.cq_level;
+ }
// Keyframe and section processing.
if (rc->frames_to_key == 0 || (frame_flags & FRAMEFLAGS_KEY)) {
@@ -1704,7 +1908,7 @@
// Define a new GF/ARF group. (Should always enter here for key frames).
if (rc->frames_till_gf_update_due == 0) {
- assert(current_frame->frame_number == 0 ||
+ assert(cpi->common.current_frame.frame_number == 0 ||
gf_group->index == gf_group->size);
define_gf_group(cpi, &this_frame, frame_params);
rc->frames_till_gf_update_due = rc->baseline_gf_interval;
@@ -1732,30 +1936,7 @@
cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
}
- target_rate = gf_group->bit_allocation[gf_group->index];
-
- if (frame_params->frame_type == KEY_FRAME) {
- target_rate = av1_rc_clamp_iframe_target_size(cpi, target_rate);
- } else {
- target_rate = av1_rc_clamp_pframe_target_size(
- cpi, target_rate, gf_group->update_type[gf_group->index]);
- }
-
- rc->base_frame_target = target_rate;
-
- {
- const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
- ? cpi->initial_mbs
- : cpi->common.MBs;
- // The multiplication by 256 reverses a scaling factor of (>> 8)
- // applied when combining MB error values for the frame.
- twopass->mb_av_energy = log((this_frame.intra_error / num_mbs) + 1.0);
- twopass->frame_avg_haar_energy =
- log((this_frame.frame_avg_wavelet_energy / num_mbs) + 1.0);
- }
-
- // Update the total stats remaining structure.
- subtract_stats(&twopass->total_left_stats, &this_frame);
+ setup_target_rate(cpi, frame_params->frame_type);
}
void av1_init_second_pass(AV1_COMP *cpi) {
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index cfeb6aa..ac66e15 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -41,9 +41,6 @@
#define MAX_MB_RATE 250
#define MAXRATE_1080P 2025000
-#define DEFAULT_KF_BOOST 2000
-#define DEFAULT_GF_BOOST 2000
-
#define MIN_BPB_FACTOR 0.005
#define MAX_BPB_FACTOR 50
@@ -1425,7 +1422,13 @@
int av1_rc_pick_q_and_bounds(AV1_COMP *cpi, int width, int height,
int *bottom_index, int *top_index) {
int q;
- if (cpi->oxcf.pass == 0) {
+ // TODO(sarahparker) merge onepass vbr, keyframe and altref q computation
+ // with two pass
+ GF_GROUP *gf_group = &cpi->gf_group;
+ if ((cpi->oxcf.rc_mode != AOM_Q ||
+ gf_group->update_type[gf_group->index] == KF_UPDATE ||
+ gf_group->update_type[gf_group->index] == ARF_UPDATE) &&
+ cpi->oxcf.pass == 0) {
if (cpi->oxcf.rc_mode == AOM_CBR)
q = rc_pick_q_and_bounds_one_pass_cbr(cpi, width, height, bottom_index,
top_index);
@@ -1438,18 +1441,12 @@
q = rc_pick_q_and_bounds_one_pass_vbr(cpi, width, height, bottom_index,
top_index);
} else {
- assert(cpi->oxcf.pass == 2 && "invalid encode pass");
-
- GF_GROUP *gf_group = &cpi->gf_group;
int arf_q = -1; // Initialize to invalid value, for sanity check later.
q = rc_pick_q_and_bounds_two_pass(cpi, width, height, bottom_index,
top_index, &arf_q);
-
- if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
- cpi->rc.arf_q = arf_q;
- }
}
+ if (gf_group->update_type[gf_group->index] == ARF_UPDATE) cpi->rc.arf_q = q;
return q;
}
@@ -1470,8 +1467,7 @@
}
}
-static void rc_set_frame_target(AV1_COMP *cpi, int target, int width,
- int height) {
+void av1_rc_set_frame_target(AV1_COMP *cpi, int target, int width, int height) {
const AV1_COMMON *const cm = &cpi->common;
RATE_CONTROL *const rc = &cpi->rc;
@@ -1641,227 +1637,6 @@
cpi->rc.rc_1_frame = 0;
}
-// Use this macro to turn on/off use of alt-refs in one-pass mode.
-#define USE_ALTREF_FOR_ONE_PASS 1
-
-static int calc_pframe_target_size_one_pass_vbr(
- const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
- static const int af_ratio = 10;
- const RATE_CONTROL *const rc = &cpi->rc;
- int target;
-#if USE_ALTREF_FOR_ONE_PASS
- if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
- frame_update_type == ARF_UPDATE) {
- target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
- (rc->baseline_gf_interval + af_ratio - 1);
- } else {
- target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
- (rc->baseline_gf_interval + af_ratio - 1);
- }
-#else
- target = rc->avg_frame_bandwidth;
-#endif
- return av1_rc_clamp_pframe_target_size(cpi, target, frame_update_type);
-}
-
-static int calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
- static const int kf_ratio = 25;
- const RATE_CONTROL *rc = &cpi->rc;
- const int target = rc->avg_frame_bandwidth * kf_ratio;
- return av1_rc_clamp_iframe_target_size(cpi, target);
-}
-
-void av1_rc_get_one_pass_vbr_params(AV1_COMP *cpi,
- FRAME_UPDATE_TYPE *const frame_update_type,
- EncodeFrameParams *const frame_params,
- unsigned int frame_flags) {
- AV1_COMMON *const cm = &cpi->common;
- RATE_CONTROL *const rc = &cpi->rc;
- CurrentFrame *const current_frame = &cm->current_frame;
- int target;
- int altref_enabled = is_altref_enabled(cpi);
- int sframe_dist = cpi->oxcf.sframe_dist;
- int sframe_mode = cpi->oxcf.sframe_mode;
- int sframe_enabled = cpi->oxcf.sframe_enabled;
- // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
- if (*frame_update_type != ARF_UPDATE &&
- (current_frame->frame_number == 0 || (frame_flags & FRAMEFLAGS_KEY) ||
- rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
- frame_params->frame_type = KEY_FRAME;
- rc->this_key_frame_forced =
- current_frame->frame_number != 0 && rc->frames_to_key == 0;
- rc->frames_to_key = cpi->oxcf.key_freq;
- rc->kf_boost = DEFAULT_KF_BOOST;
- rc->source_alt_ref_active = 0;
- } else {
- frame_params->frame_type = INTER_FRAME;
- if (sframe_enabled) {
- if (altref_enabled) {
- if (sframe_mode == 1) {
- // sframe_mode == 1: insert sframe if it matches altref frame.
-
- if (current_frame->frame_number % sframe_dist == 0 &&
- current_frame->frame_number != 0 &&
- *frame_update_type == ARF_UPDATE) {
- frame_params->frame_type = S_FRAME;
- }
- } else {
- // sframe_mode != 1: if sframe will be inserted at the next available
- // altref frame
-
- if (current_frame->frame_number % sframe_dist == 0 &&
- current_frame->frame_number != 0) {
- rc->sframe_due = 1;
- }
-
- if (rc->sframe_due && *frame_update_type == ARF_UPDATE) {
- frame_params->frame_type = S_FRAME;
- rc->sframe_due = 0;
- }
- }
- } else {
- if (current_frame->frame_number % sframe_dist == 0 &&
- current_frame->frame_number != 0) {
- frame_params->frame_type = S_FRAME;
- }
- }
- }
- }
- if (rc->frames_till_gf_update_due == 0) {
- rc->baseline_gf_interval = (rc->min_gf_interval + rc->max_gf_interval) / 2;
- rc->frames_till_gf_update_due = rc->baseline_gf_interval;
- // NOTE: frames_till_gf_update_due must be <= frames_to_key.
- if (rc->frames_till_gf_update_due > rc->frames_to_key) {
- rc->frames_till_gf_update_due = rc->frames_to_key;
- rc->constrained_gf_group = 1;
- } else {
- rc->constrained_gf_group = 0;
- }
- if (*frame_update_type == LF_UPDATE) *frame_update_type = GF_UPDATE;
- rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
- rc->gfu_boost = DEFAULT_GF_BOOST;
- }
-
- if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
- av1_cyclic_refresh_update_parameters(cpi);
-
- if (frame_params->frame_type == KEY_FRAME)
- target = calc_iframe_target_size_one_pass_vbr(cpi);
- else
- target = calc_pframe_target_size_one_pass_vbr(cpi, *frame_update_type);
- rc_set_frame_target(cpi, target, cm->width, cm->height);
-}
-
-static int calc_pframe_target_size_one_pass_cbr(
- const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
- const AV1EncoderConfig *oxcf = &cpi->oxcf;
- const RATE_CONTROL *rc = &cpi->rc;
- const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
- const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
- int min_frame_target =
- AOMMAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
- int target;
-
- if (oxcf->gf_cbr_boost_pct) {
- const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
- if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
- target =
- (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
- (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
- } else {
- target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
- (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
- }
- } else {
- target = rc->avg_frame_bandwidth;
- }
-
- if (diff > 0) {
- // Lower the target bandwidth for this frame.
- const int pct_low = (int)AOMMIN(diff / one_pct_bits, oxcf->under_shoot_pct);
- target -= (target * pct_low) / 200;
- } else if (diff < 0) {
- // Increase the target bandwidth for this frame.
- const int pct_high =
- (int)AOMMIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
- target += (target * pct_high) / 200;
- }
- if (oxcf->rc_max_inter_bitrate_pct) {
- const int max_rate =
- rc->avg_frame_bandwidth * oxcf->rc_max_inter_bitrate_pct / 100;
- target = AOMMIN(target, max_rate);
- }
- return AOMMAX(min_frame_target, target);
-}
-
-static int calc_iframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
- const RATE_CONTROL *rc = &cpi->rc;
- int target;
- if (cpi->common.current_frame.frame_number == 0) {
- target = ((rc->starting_buffer_level / 2) > INT_MAX)
- ? INT_MAX
- : (int)(rc->starting_buffer_level / 2);
- } else {
- int kf_boost = 32;
- double framerate = cpi->framerate;
-
- kf_boost = AOMMAX(kf_boost, (int)(2 * framerate - 16));
- if (rc->frames_since_key < framerate / 2) {
- kf_boost = (int)(kf_boost * rc->frames_since_key / (framerate / 2));
- }
- target = ((16 + kf_boost) * rc->avg_frame_bandwidth) >> 4;
- }
- return av1_rc_clamp_iframe_target_size(cpi, target);
-}
-
-void av1_rc_get_one_pass_cbr_params(AV1_COMP *cpi,
- FRAME_UPDATE_TYPE *const frame_update_type,
- EncodeFrameParams *const frame_params,
- unsigned int frame_flags) {
- AV1_COMMON *const cm = &cpi->common;
- RATE_CONTROL *const rc = &cpi->rc;
- CurrentFrame *const current_frame = &cm->current_frame;
- int target;
- // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
- if ((current_frame->frame_number == 0 || (frame_flags & FRAMEFLAGS_KEY) ||
- rc->frames_to_key == 0 || (cpi->oxcf.auto_key && 0))) {
- frame_params->frame_type = KEY_FRAME;
- rc->this_key_frame_forced =
- current_frame->frame_number != 0 && rc->frames_to_key == 0;
- rc->frames_to_key = cpi->oxcf.key_freq;
- rc->kf_boost = DEFAULT_KF_BOOST;
- rc->source_alt_ref_active = 0;
- } else {
- frame_params->frame_type = INTER_FRAME;
- }
- if (rc->frames_till_gf_update_due == 0) {
- if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
- av1_cyclic_refresh_set_golden_update(cpi);
- else
- rc->baseline_gf_interval =
- (rc->min_gf_interval + rc->max_gf_interval) / 2;
- rc->frames_till_gf_update_due = rc->baseline_gf_interval;
- // NOTE: frames_till_gf_update_due must be <= frames_to_key.
- if (rc->frames_till_gf_update_due > rc->frames_to_key)
- rc->frames_till_gf_update_due = rc->frames_to_key;
- if (*frame_update_type == LF_UPDATE) *frame_update_type = GF_UPDATE;
- rc->gfu_boost = DEFAULT_GF_BOOST;
- }
-
- // Any update/change of global cyclic refresh parameters (amount/delta-qp)
- // should be done here, before the frame qp is selected.
- if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ)
- av1_cyclic_refresh_update_parameters(cpi);
-
- if (frame_params->frame_type == KEY_FRAME)
- target = calc_iframe_target_size_one_pass_cbr(cpi);
- else
- target = calc_pframe_target_size_one_pass_cbr(cpi, *frame_update_type);
-
- rc_set_frame_target(cpi, target, cm->width, cm->height);
- // TODO(afergs): Decide whether to scale up, down, or not at all
-}
-
int av1_find_qindex(double desired_q, aom_bit_depth_t bit_depth,
int best_qindex, int worst_qindex) {
assert(best_qindex <= worst_qindex);
@@ -2046,5 +1821,5 @@
// Correction to rate target based on prior over or under shoot.
if (cpi->oxcf.rc_mode == AOM_VBR || cpi->oxcf.rc_mode == AOM_CQ)
vbr_rate_correction(cpi, &target_rate);
- rc_set_frame_target(cpi, target_rate, width, height);
+ av1_rc_set_frame_target(cpi, target_rate, width, height);
}
diff --git a/av1/encoder/ratectrl.h b/av1/encoder/ratectrl.h
index 3b3a8cd..cbe925d 100644
--- a/av1/encoder/ratectrl.h
+++ b/av1/encoder/ratectrl.h
@@ -27,6 +27,9 @@
// Bits Per MB at different Q (Multiplied by 512)
#define BPER_MB_NORMBITS 9
+// Use this macro to turn on/off use of alt-refs in one-pass mode.
+#define USE_ALTREF_FOR_ONE_PASS 1
+
// Threshold used to define if a KF group is static (e.g. a slide show).
// Essentially, this means that no frame in the group has more than 1% of MBs
// that are not marked as coded with 0,0 motion in the first pass.
@@ -167,8 +170,6 @@
// Generally at the high level, the following flow is expected
// to be enforced for rate control:
// First call per frame, one of:
-// av1_rc_get_one_pass_vbr_params()
-// av1_rc_get_one_pass_cbr_params()
// av1_rc_get_first_pass_params()
// av1_rc_get_second_pass_params()
// depending on the usage to set the rate control encode parameters desired.
@@ -188,12 +189,6 @@
// Functions to set parameters for encoding before the actual
// encode_frame_to_data_rate() function.
struct EncodeFrameParams;
-void av1_rc_get_one_pass_vbr_params(
- struct AV1_COMP *cpi, uint8_t *const frame_update_type,
- struct EncodeFrameParams *const frame_params, unsigned int frame_flags);
-void av1_rc_get_one_pass_cbr_params(
- struct AV1_COMP *cpi, uint8_t *const frame_update_type,
- struct EncodeFrameParams *const frame_params, unsigned int frame_flags);
// Post encode update of the rate control parameters based
// on bytes used
@@ -264,6 +259,9 @@
int av1_resize_one_pass_cbr(struct AV1_COMP *cpi);
+void av1_rc_set_frame_target(struct AV1_COMP *cpi, int target, int width,
+ int height);
+
int av1_estimate_q_constant_quality_two_pass(const struct AV1_COMP *cpi,
int width, int height, int *arf_q,
int gf_index);