Commit 1cdc2389 authored by Dmitry Kovalev's avatar Dmitry Kovalev
Browse files

Adding buffer levels to RATE_CONTROL struct.

Change-Id: Ib35ff854378764dc3c6745844c67a33dee545663
parent 7106f709
......@@ -627,33 +627,30 @@ void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
// local file playback mode == really big buffer
if (cpi->oxcf.rc_mode == RC_MODE_VBR) {
cpi->oxcf.starting_buffer_level = 60000;
cpi->oxcf.optimal_buffer_level = 60000;
cpi->oxcf.maximum_buffer_size = 240000;
cpi->oxcf.starting_buffer_level_ms = 60000;
cpi->oxcf.optimal_buffer_level_ms = 60000;
cpi->oxcf.maximum_buffer_size_ms = 240000;
}
cpi->oxcf.starting_buffer_level =
vp9_rescale(cpi->oxcf.starting_buffer_level,
cpi->oxcf.target_bandwidth, 1000);
rc->starting_buffer_level = vp9_rescale(cpi->oxcf.starting_buffer_level_ms,
cpi->oxcf.target_bandwidth, 1000);
// Set or reset optimal and maximum buffer levels.
if (cpi->oxcf.optimal_buffer_level == 0)
cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
if (cpi->oxcf.optimal_buffer_level_ms == 0)
rc->optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
else
cpi->oxcf.optimal_buffer_level =
vp9_rescale(cpi->oxcf.optimal_buffer_level,
cpi->oxcf.target_bandwidth, 1000);
rc->optimal_buffer_level = vp9_rescale(cpi->oxcf.optimal_buffer_level_ms,
cpi->oxcf.target_bandwidth, 1000);
if (cpi->oxcf.maximum_buffer_size == 0)
cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
if (cpi->oxcf.maximum_buffer_size_ms == 0)
rc->maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
else
cpi->oxcf.maximum_buffer_size =
vp9_rescale(cpi->oxcf.maximum_buffer_size,
cpi->oxcf.target_bandwidth, 1000);
rc->maximum_buffer_size = vp9_rescale(cpi->oxcf.maximum_buffer_size_ms,
cpi->oxcf.target_bandwidth, 1000);
// Under a configuration change, where maximum_buffer_size may change,
// keep buffer level clipped to the maximum allowed buffer size.
rc->bits_off_target = MIN(rc->bits_off_target, cpi->oxcf.maximum_buffer_size);
rc->buffer_level = MIN(rc->buffer_level, cpi->oxcf.maximum_buffer_size);
rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
// Set up frame rate and related parameters rate control values.
vp9_new_framerate(cpi, cpi->oxcf.framerate);
......
......@@ -216,9 +216,9 @@ typedef struct VP9EncoderConfig {
int over_shoot_pct;
// buffering parameters
int64_t starting_buffer_level; // in seconds
int64_t optimal_buffer_level;
int64_t maximum_buffer_size;
int64_t starting_buffer_level_ms;
int64_t optimal_buffer_level_ms;
int64_t maximum_buffer_size_ms;
// Frame drop threshold.
int drop_frames_water_mark;
......
......@@ -159,7 +159,7 @@ static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
lrc->bits_off_target += bits_off_for_this_layer;
// Clip buffer level to maximum buffer size for the layer.
lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
lrc->buffer_level = lrc->bits_off_target;
}
}
......@@ -167,7 +167,6 @@ static void update_layer_buffer_level(SVC *svc, int encoded_frame_size) {
// Update the buffer level: leaky bucket model.
static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
const VP9_COMMON *const cm = &cpi->common;
const VP9EncoderConfig *oxcf = &cpi->oxcf;
RATE_CONTROL *const rc = &cpi->rc;
// Non-viewable frames are a special case and are treated as pure overhead.
......@@ -178,7 +177,7 @@ static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
}
// Clip the buffer level to the maximum specified buffer size.
rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size);
rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
rc->buffer_level = rc->bits_off_target;
if (cpi->use_svc && cpi->oxcf.rc_mode == RC_MODE_CBR) {
......@@ -203,8 +202,8 @@ void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
rc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
rc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
rc->buffer_level = oxcf->starting_buffer_level;
rc->bits_off_target = oxcf->starting_buffer_level;
rc->buffer_level = rc->starting_buffer_level;
rc->bits_off_target = rc->starting_buffer_level;
rc->rolling_target_bits = rc->avg_frame_bandwidth;
rc->rolling_actual_bits = rc->avg_frame_bandwidth;
......@@ -250,7 +249,7 @@ int vp9_rc_drop_frame(VP9_COMP *cpi) {
// If buffer is below drop_mark, for now just drop every other frame
// (starting with the next frame) until it increases back over drop_mark.
int drop_mark = (int)(oxcf->drop_frames_water_mark *
oxcf->optimal_buffer_level / 100);
rc->optimal_buffer_level / 100);
if ((rc->buffer_level > drop_mark) &&
(rc->decimation_factor > 0)) {
--rc->decimation_factor;
......@@ -444,10 +443,9 @@ static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
// ambient Q (at buffer = optimal level) to worst_quality level
// (at buffer = critical level).
const VP9_COMMON *const cm = &cpi->common;
const VP9EncoderConfig *oxcf = &cpi->oxcf;
const RATE_CONTROL *rc = &cpi->rc;
// Buffer level below which we push active_worst to worst_quality.
int64_t critical_level = oxcf->optimal_buffer_level >> 2;
int64_t critical_level = rc->optimal_buffer_level >> 2;
int64_t buff_lvl_step = 0;
int adjustment = 0;
int active_worst_quality;
......@@ -459,26 +457,26 @@ static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
else
active_worst_quality = MIN(rc->worst_quality,
rc->avg_frame_qindex[KEY_FRAME] * 3 / 2);
if (rc->buffer_level > oxcf->optimal_buffer_level) {
if (rc->buffer_level > rc->optimal_buffer_level) {
// Adjust down.
// Maximum limit for down adjustment, ~30%.
int max_adjustment_down = active_worst_quality / 3;
if (max_adjustment_down) {
buff_lvl_step = ((oxcf->maximum_buffer_size -
oxcf->optimal_buffer_level) / max_adjustment_down);
buff_lvl_step = ((rc->maximum_buffer_size -
rc->optimal_buffer_level) / max_adjustment_down);
if (buff_lvl_step)
adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) /
adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
buff_lvl_step);
active_worst_quality -= adjustment;
}
} else if (rc->buffer_level > critical_level) {
// Adjust up from ambient Q.
if (critical_level) {
buff_lvl_step = (oxcf->optimal_buffer_level - critical_level);
buff_lvl_step = (rc->optimal_buffer_level - critical_level);
if (buff_lvl_step) {
adjustment =
(int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) *
(oxcf->optimal_buffer_level - rc->buffer_level) /
(rc->optimal_buffer_level - rc->buffer_level) /
buff_lvl_step);
}
active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment;
......@@ -1227,8 +1225,8 @@ static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
const VP9EncoderConfig *oxcf = &cpi->oxcf;
const RATE_CONTROL *rc = &cpi->rc;
const SVC *const svc = &cpi->svc;
const int64_t diff = oxcf->optimal_buffer_level - rc->buffer_level;
const int64_t one_pct_bits = 1 + oxcf->optimal_buffer_level / 100;
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 = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
int target = rc->avg_frame_bandwidth;
if (svc->number_temporal_layers > 1 &&
......@@ -1259,8 +1257,8 @@ static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
const SVC *const svc = &cpi->svc;
int target;
if (cpi->common.current_video_frame == 0) {
target = ((cpi->oxcf.starting_buffer_level / 2) > INT_MAX)
? INT_MAX : (int)(cpi->oxcf.starting_buffer_level / 2);
target = ((rc->starting_buffer_level / 2) > INT_MAX)
? INT_MAX : (int)(rc->starting_buffer_level / 2);
} else {
int kf_boost = 32;
double framerate = oxcf->framerate;
......
......@@ -84,6 +84,10 @@ typedef struct {
int worst_quality;
int best_quality;
int64_t starting_buffer_level;
int64_t optimal_buffer_level;
int64_t maximum_buffer_size;
// int active_best_quality;
} RATE_CONTROL;
......
......@@ -54,7 +54,7 @@ void vp9_init_layer_context(VP9_COMP *const cpi) {
lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
}
lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level),
lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level_ms),
lc->target_bandwidth, 1000);
lrc->bits_off_target = lrc->buffer_level;
}
......@@ -87,14 +87,14 @@ void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
}
bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
// Update buffer-related quantities.
lc->starting_buffer_level =
(int64_t)(oxcf->starting_buffer_level * bitrate_alloc);
lc->optimal_buffer_level =
(int64_t)(oxcf->optimal_buffer_level * bitrate_alloc);
lc->maximum_buffer_size =
(int64_t)(oxcf->maximum_buffer_size * bitrate_alloc);
lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
lrc->starting_buffer_level =
(int64_t)(rc->starting_buffer_level * bitrate_alloc);
lrc->optimal_buffer_level =
(int64_t)(rc->optimal_buffer_level * bitrate_alloc);
lrc->maximum_buffer_size =
(int64_t)(rc->maximum_buffer_size * bitrate_alloc);
lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size);
// Update framerate-related quantities.
if (svc->number_temporal_layers > 1) {
lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer];
......@@ -160,9 +160,6 @@ void vp9_restore_layer_context(VP9_COMP *const cpi) {
cpi->rc = lc->rc;
cpi->twopass = lc->twopass;
cpi->oxcf.target_bandwidth = lc->target_bandwidth;
cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
// Reset the frames_since_key and frames_to_key counters to their values
// before the layer restore. Keep these defined for the stream (not layer).
if (cpi->svc.number_temporal_layers > 1) {
......@@ -178,9 +175,6 @@ void vp9_save_layer_context(VP9_COMP *const cpi) {
lc->rc = cpi->rc;
lc->twopass = cpi->twopass;
lc->target_bandwidth = (int)oxcf->target_bandwidth;
lc->starting_buffer_level = oxcf->starting_buffer_level;
lc->optimal_buffer_level = oxcf->optimal_buffer_level;
lc->maximum_buffer_size = oxcf->maximum_buffer_size;
}
void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
......
......@@ -22,9 +22,6 @@ extern "C" {
typedef struct {
RATE_CONTROL rc;
int target_bandwidth;
int64_t starting_buffer_level;
int64_t optimal_buffer_level;
int64_t maximum_buffer_size;
double framerate;
int avg_frame_size;
TWO_PASS twopass;
......
......@@ -343,9 +343,9 @@ static vpx_codec_err_t set_encoder_config(
oxcf->scaled_frame_width = cfg->rc_scaled_width;
oxcf->scaled_frame_height = cfg->rc_scaled_height;
oxcf->maximum_buffer_size = cfg->rc_buf_sz;
oxcf->starting_buffer_level = cfg->rc_buf_initial_sz;
oxcf->optimal_buffer_level = cfg->rc_buf_optimal_sz;
oxcf->maximum_buffer_size_ms = cfg->rc_buf_sz;
oxcf->starting_buffer_level_ms = cfg->rc_buf_initial_sz;
oxcf->optimal_buffer_level_ms = cfg->rc_buf_optimal_sz;
oxcf->drop_frames_water_mark = cfg->rc_dropframe_thresh;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment