Commit 4864ab21 authored by Marco Paniconi's avatar Marco Paniconi

Layer based rate control for CBR mode.

This patch adds a buffer-based rate control for temporal layers,
under CBR mode.

Added vpx_temporal_scalable_patters.c encoder for testing temporal
layers, for both vp9 and vp8 (replaces the old vp8_scalable_patterns).

Updated datarate unittest with tests for temporal layer rate-targeting.

Change-Id: I8900a854288b9354d9c697cfeb0243a9fd6790b1
parent 9602ed88
...@@ -54,9 +54,6 @@ vpxenc.SRCS += third_party/libmkv/EbmlWriter.h ...@@ -54,9 +54,6 @@ vpxenc.SRCS += third_party/libmkv/EbmlWriter.h
vpxenc.SRCS += $(LIBYUV_SRCS) vpxenc.SRCS += $(LIBYUV_SRCS)
vpxenc.GUID = 548DEC74-7A15-4B2B-AFC3-AA102E7C25C1 vpxenc.GUID = 548DEC74-7A15-4B2B-AFC3-AA102E7C25C1
vpxenc.DESCRIPTION = Full featured encoder vpxenc.DESCRIPTION = Full featured encoder
UTILS-$(CONFIG_VP8_ENCODER) += vp8_scalable_patterns.c
vp8_scalable_patterns.GUID = 0D6A210B-F482-4D6F-8570-4A9C01ACC88C
vp8_scalable_patterns.DESCRIPTION = Temporal Scalability Encoder
UTILS-$(CONFIG_VP9_ENCODER) += vp9_spatial_scalable_encoder.c UTILS-$(CONFIG_VP9_ENCODER) += vp9_spatial_scalable_encoder.c
vp9_spatial_scalable_encoder.SRCS += args.c args.h vp9_spatial_scalable_encoder.SRCS += args.c args.h
vp9_spatial_scalable_encoder.SRCS += ivfenc.c ivfenc.h vp9_spatial_scalable_encoder.SRCS += ivfenc.c ivfenc.h
...@@ -73,6 +70,11 @@ endif ...@@ -73,6 +70,11 @@ endif
#example_xma.GUID = A955FC4A-73F1-44F7-135E-30D84D32F022 #example_xma.GUID = A955FC4A-73F1-44F7-135E-30D84D32F022
#example_xma.DESCRIPTION = External Memory Allocation mode usage #example_xma.DESCRIPTION = External Memory Allocation mode usage
EXAMPLES-$(CONFIG_ENCODERS) += vpx_temporal_scalable_patterns.c
vpx_temporal_scalable_patterns.SRCS += ivfenc.c ivfenc.h
vpx_temporal_scalable_patterns.SRCS += tools_common.c tools_common.h
vpx_temporal_scalable_patterns.GUID = B18C08F2-A439-4502-A78E-849BE3D60947
vpx_temporal_scalable_patterns.DESCRIPTION = Temporal Scalability Encoder
EXAMPLES-$(CONFIG_VP8_DECODER) += simple_decoder.c EXAMPLES-$(CONFIG_VP8_DECODER) += simple_decoder.c
simple_decoder.GUID = D3BBF1E9-2427-450D-BBFF-B2843C1D44CC simple_decoder.GUID = D3BBF1E9-2427-450D-BBFF-B2843C1D44CC
simple_decoder.SRCS += ivfdec.h ivfdec.c simple_decoder.SRCS += ivfdec.h ivfdec.c
......
This diff is collapsed.
This diff is collapsed.
...@@ -123,6 +123,11 @@ class Encoder { ...@@ -123,6 +123,11 @@ class Encoder {
ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError(); ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
} }
void Control(int ctrl_id, struct vpx_svc_layer_id *arg) {
const vpx_codec_err_t res = vpx_codec_control_(&encoder_, ctrl_id, arg);
ASSERT_EQ(VPX_CODEC_OK, res) << EncoderError();
}
void set_deadline(unsigned long deadline) { void set_deadline(unsigned long deadline) {
deadline_ = deadline; deadline_ = deadline;
} }
......
This diff is collapsed.
...@@ -147,8 +147,12 @@ extern "C" { ...@@ -147,8 +147,12 @@ extern "C" {
// END DATARATE CONTROL OPTIONS // END DATARATE CONTROL OPTIONS
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// Spatial scalability // Spatial and temporal scalability.
int ss_number_layers; int ss_number_layers; // Number of spatial layers.
int ts_number_layers; // Number of temporal layers.
// Bitrate allocation (CBR mode) and framerate factor, for temporal layers.
int ts_target_bitrate[VPX_TS_MAX_LAYERS];
int ts_rate_decimator[VPX_TS_MAX_LAYERS];
// these parameters aren't to be used in final build don't use!!! // these parameters aren't to be used in final build don't use!!!
int play_alternate; int play_alternate;
......
...@@ -1144,6 +1144,109 @@ static int64_t rescale(int val, int64_t num, int denom) { ...@@ -1144,6 +1144,109 @@ static int64_t rescale(int val, int64_t num, int denom) {
return (llval * llnum / llden); return (llval * llnum / llden);
} }
// Initialize layer context data from init_config().
static void init_layer_context(VP9_COMP *const cpi) {
const VP9_CONFIG *const oxcf = &cpi->oxcf;
int temporal_layer = 0;
cpi->svc.spatial_layer_id = 0;
cpi->svc.temporal_layer_id = 0;
for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
++temporal_layer) {
LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
RATE_CONTROL *const lrc = &lc->rc;
lrc->active_worst_quality = q_trans[oxcf->worst_allowed_q];
lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
lrc->ni_av_qi = lrc->active_worst_quality;
lrc->total_actual_bits = 0;
lrc->total_target_vs_actual = 0;
lrc->ni_tot_qi = 0;
lrc->tot_q = 0.0;
lrc->ni_frames = 0;
lrc->rate_correction_factor = 1.0;
lrc->key_frame_rate_correction_factor = 1.0;
lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] *
1000;
lrc->buffer_level = rescale((int)(oxcf->starting_buffer_level),
lc->target_bandwidth, 1000);
lrc->bits_off_target = lrc->buffer_level;
}
}
// Update the layer context from a change_config() call.
static void update_layer_context_change_config(VP9_COMP *const cpi,
const int target_bandwidth) {
const VP9_CONFIG *const oxcf = &cpi->oxcf;
const RATE_CONTROL *const rc = &cpi->rc;
int temporal_layer = 0;
float bitrate_alloc = 1.0;
for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
++temporal_layer) {
LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
RATE_CONTROL *const lrc = &lc->rc;
lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] * 1000;
bitrate_alloc = (float)lc->target_bandwidth / (float)target_bandwidth;
// Update buffer-related quantities.
lc->starting_buffer_level = oxcf->starting_buffer_level * bitrate_alloc;
lc->optimal_buffer_level = oxcf->optimal_buffer_level * bitrate_alloc;
lc->maximum_buffer_size = 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);
// Update framerate-related quantities.
lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
// Update qp-related quantities.
lrc->worst_quality = rc->worst_quality;
lrc->best_quality = rc->best_quality;
lrc->active_worst_quality = rc->active_worst_quality;
}
}
// Prior to encoding the frame, update framerate-related quantities
// for the current layer.
static void update_layer_framerate(VP9_COMP *const cpi) {
int temporal_layer = cpi->svc.temporal_layer_id;
LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
RATE_CONTROL *const lrc = &lc->rc;
lc->framerate = cpi->oxcf.framerate /
cpi->oxcf.ts_rate_decimator[temporal_layer];
lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth /
lc->framerate);
lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
}
// Prior to encoding the frame, set the layer context, for the current layer
// to be encoded, to the cpi struct.
static void restore_layer_context(VP9_COMP *const cpi) {
int temporal_layer = cpi->svc.temporal_layer_id;
LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
int frame_since_key = cpi->rc.frames_since_key;
int frame_to_key = cpi->rc.frames_to_key;
cpi->rc = lc->rc;
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;
cpi->output_framerate = lc->framerate;
// 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).
cpi->rc.frames_since_key = frame_since_key;
cpi->rc.frames_to_key = frame_to_key;
}
// Save the layer context after encoding the frame.
static void save_layer_context(VP9_COMP *const cpi) {
int temporal_layer = cpi->svc.temporal_layer_id;
LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
lc->rc = cpi->rc;
lc->target_bandwidth = cpi->oxcf.target_bandwidth;
lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
lc->framerate = cpi->output_framerate;
}
static void set_tile_limits(VP9_COMP *cpi) { static void set_tile_limits(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common; VP9_COMMON *const cm = &cpi->common;
...@@ -1170,6 +1273,16 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1170,6 +1273,16 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
cm->subsampling_y = 0; cm->subsampling_y = 0;
vp9_alloc_compressor_data(cpi); vp9_alloc_compressor_data(cpi);
// Spatial scalability.
cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
// Temporal scalability.
cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
if (cpi->svc.number_temporal_layers > 1 &&
cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
init_layer_context(cpi);
}
// change includes all joint functionality // change includes all joint functionality
vp9_change_config(ptr, oxcf); vp9_change_config(ptr, oxcf);
...@@ -1210,9 +1323,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1210,9 +1323,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
cpi->gld_fb_idx = 1; cpi->gld_fb_idx = 1;
cpi->alt_fb_idx = 2; cpi->alt_fb_idx = 2;
cpi->current_layer = 0;
cpi->use_svc = 0;
set_tile_limits(cpi); set_tile_limits(cpi);
cpi->fixed_divide[0] = 0; cpi->fixed_divide[0] = 0;
...@@ -1220,7 +1330,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1220,7 +1330,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
cpi->fixed_divide[i] = 0x80000 / i; cpi->fixed_divide[i] = 0x80000 / i;
} }
void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
VP9_COMP *cpi = (VP9_COMP *)(ptr); VP9_COMP *cpi = (VP9_COMP *)(ptr);
VP9_COMMON *const cm = &cpi->common; VP9_COMMON *const cm = &cpi->common;
...@@ -1312,10 +1421,10 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1312,10 +1421,10 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
cpi->oxcf.target_bandwidth, 1000); cpi->oxcf.target_bandwidth, 1000);
// Under a configuration change, where maximum_buffer_size may change, // Under a configuration change, where maximum_buffer_size may change,
// keep buffer level clipped to the maximum allowed buffer size. // keep buffer level clipped to the maximum allowed buffer size.
if (cpi->rc.bits_off_target > cpi->oxcf.maximum_buffer_size) { cpi->rc.bits_off_target = MIN(cpi->rc.bits_off_target,
cpi->rc.bits_off_target = cpi->oxcf.maximum_buffer_size; cpi->oxcf.maximum_buffer_size);
cpi->rc.buffer_level = cpi->rc.bits_off_target; cpi->rc.buffer_level = MIN(cpi->rc.buffer_level,
} cpi->oxcf.maximum_buffer_size);
// Set up frame rate and related parameters rate control values. // Set up frame rate and related parameters rate control values.
vp9_new_framerate(cpi, cpi->oxcf.framerate); vp9_new_framerate(cpi, cpi->oxcf.framerate);
...@@ -1350,6 +1459,11 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1350,6 +1459,11 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
} }
update_frame_size(cpi); update_frame_size(cpi);
if (cpi->svc.number_temporal_layers > 1 &&
cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
update_layer_context_change_config(cpi, cpi->oxcf.target_bandwidth);
}
cpi->speed = cpi->oxcf.cpu_used; cpi->speed = cpi->oxcf.cpu_used;
if (cpi->oxcf.lag_in_frames == 0) { if (cpi->oxcf.lag_in_frames == 0) {
...@@ -1573,6 +1687,8 @@ VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { ...@@ -1573,6 +1687,8 @@ VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
vp9_create_common(cm); vp9_create_common(cm);
cpi->use_svc = 0;
init_config((VP9_PTR)cpi, oxcf); init_config((VP9_PTR)cpi, oxcf);
init_pick_mode_context(cpi); init_pick_mode_context(cpi);
...@@ -1588,9 +1704,6 @@ VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { ...@@ -1588,9 +1704,6 @@ VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
cpi->alt_is_last = 0; cpi->alt_is_last = 0;
cpi->gold_is_alt = 0; cpi->gold_is_alt = 0;
// Spatial scalability
cpi->number_spatial_layers = oxcf->ss_number_layers;
// Create the encoder segmentation map and set all entries to 0 // Create the encoder segmentation map and set all entries to 0
CHECK_MEM_ERROR(cm, cpi->segmentation_map, CHECK_MEM_ERROR(cm, cpi->segmentation_map,
vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
...@@ -3504,6 +3617,12 @@ int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, ...@@ -3504,6 +3617,12 @@ int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
adjust_frame_rate(cpi); adjust_frame_rate(cpi);
} }
if (cpi->svc.number_temporal_layers > 1 &&
cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
update_layer_framerate(cpi);
restore_layer_context(cpi);
}
// start with a 0 size frame // start with a 0 size frame
*size = 0; *size = 0;
...@@ -3579,6 +3698,12 @@ int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, ...@@ -3579,6 +3698,12 @@ int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
cpi->droppable = !frame_is_reference(cpi); cpi->droppable = !frame_is_reference(cpi);
} }
// Save layer specific state.
if (cpi->svc.number_temporal_layers > 1 &&
cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
save_layer_context(cpi);
}
vpx_usec_timer_mark(&cmptimer); vpx_usec_timer_mark(&cmptimer);
cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
......
...@@ -407,6 +407,15 @@ typedef struct { ...@@ -407,6 +407,15 @@ typedef struct {
int super_fast_rtc; int super_fast_rtc;
} SPEED_FEATURES; } SPEED_FEATURES;
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;
} LAYER_CONTEXT;
typedef struct VP9_COMP { typedef struct VP9_COMP {
DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]); DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]); DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
...@@ -451,9 +460,6 @@ typedef struct VP9_COMP { ...@@ -451,9 +460,6 @@ typedef struct VP9_COMP {
int gld_fb_idx; int gld_fb_idx;
int alt_fb_idx; int alt_fb_idx;
int current_layer;
int use_svc;
#if CONFIG_MULTIPLE_ARF #if CONFIG_MULTIPLE_ARF
int alt_ref_fb_idx[REF_FRAMES - 3]; int alt_ref_fb_idx[REF_FRAMES - 3];
#endif #endif
...@@ -669,7 +675,18 @@ typedef struct VP9_COMP { ...@@ -669,7 +675,18 @@ typedef struct VP9_COMP {
int initial_width; int initial_width;
int initial_height; int initial_height;
int number_spatial_layers; int use_svc;
struct svc {
int spatial_layer_id;
int temporal_layer_id;
int number_spatial_layers;
int number_temporal_layers;
// Layer context used for rate control in CBR mode, only defined for
// temporal layers for now.
LAYER_CONTEXT layer_context[VPX_TS_MAX_LAYERS];
} svc;
int enable_encode_breakout; // Default value is 1. From first pass stats, int enable_encode_breakout; // Default value is 1. From first pass stats,
// encode_breakout may be disabled. // encode_breakout may be disabled.
......
...@@ -241,6 +241,26 @@ int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { ...@@ -241,6 +241,26 @@ int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
return target; return target;
} }
// Update the buffer level for higher layers, given the encoded current layer.
static void update_layer_buffer_level(VP9_COMP *const cpi,
int encoded_frame_size) {
int temporal_layer = 0;
int current_temporal_layer = cpi->svc.temporal_layer_id;
for (temporal_layer = current_temporal_layer + 1;
temporal_layer < cpi->svc.number_temporal_layers; ++temporal_layer) {
LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
RATE_CONTROL *lrc = &lc->rc;
int bits_off_for_this_layer = (int)(lc->target_bandwidth / lc->framerate -
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->buffer_level = lrc->bits_off_target;
}
}
// Update the buffer level: leaky bucket model. // Update the buffer level: leaky bucket model.
static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
const VP9_COMMON *const cm = &cpi->common; const VP9_COMMON *const cm = &cpi->common;
...@@ -255,14 +275,18 @@ static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) { ...@@ -255,14 +275,18 @@ static void update_buffer_level(VP9_COMP *cpi, int encoded_frame_size) {
} }
// Clip the buffer level to the maximum specified buffer size. // Clip the buffer level to the maximum specified buffer size.
rc->buffer_level = MIN(rc->bits_off_target, oxcf->maximum_buffer_size); rc->bits_off_target = MIN(rc->bits_off_target, oxcf->maximum_buffer_size);
rc->buffer_level = rc->bits_off_target;
if (cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
update_layer_buffer_level(cpi, encoded_frame_size);
}
} }
int vp9_rc_drop_frame(VP9_COMP *cpi) { int vp9_rc_drop_frame(VP9_COMP *cpi) {
const VP9_CONFIG *oxcf = &cpi->oxcf; const VP9_CONFIG *oxcf = &cpi->oxcf;
RATE_CONTROL *const rc = &cpi->rc; RATE_CONTROL *const rc = &cpi->rc;
if (!oxcf->drop_frames_water_mark) { if (!oxcf->drop_frames_water_mark) {
return 0; return 0;
} else { } else {
...@@ -273,7 +297,7 @@ int vp9_rc_drop_frame(VP9_COMP *cpi) { ...@@ -273,7 +297,7 @@ int vp9_rc_drop_frame(VP9_COMP *cpi) {
// If buffer is below drop_mark, for now just drop every other frame // 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. // (starting with the next frame) until it increases back over drop_mark.
int drop_mark = (int)(oxcf->drop_frames_water_mark * int drop_mark = (int)(oxcf->drop_frames_water_mark *
oxcf->optimal_buffer_level / 100); oxcf->optimal_buffer_level / 100);
if ((rc->buffer_level > drop_mark) && if ((rc->buffer_level > drop_mark) &&
(rc->decimation_factor > 0)) { (rc->decimation_factor > 0)) {
--rc->decimation_factor; --rc->decimation_factor;
...@@ -301,7 +325,8 @@ static double get_rate_correction_factor(const VP9_COMP *cpi) { ...@@ -301,7 +325,8 @@ static double get_rate_correction_factor(const VP9_COMP *cpi) {
if (cpi->common.frame_type == KEY_FRAME) { if (cpi->common.frame_type == KEY_FRAME) {
return cpi->rc.key_frame_rate_correction_factor; return cpi->rc.key_frame_rate_correction_factor;
} else { } else {
if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
!(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
return cpi->rc.gf_rate_correction_factor; return cpi->rc.gf_rate_correction_factor;
else else
return cpi->rc.rate_correction_factor; return cpi->rc.rate_correction_factor;
...@@ -312,7 +337,8 @@ static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { ...@@ -312,7 +337,8 @@ static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
if (cpi->common.frame_type == KEY_FRAME) { if (cpi->common.frame_type == KEY_FRAME) {
cpi->rc.key_frame_rate_correction_factor = factor; cpi->rc.key_frame_rate_correction_factor = factor;
} else { } else {
if (cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
!(cpi->use_svc && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
cpi->rc.gf_rate_correction_factor = factor; cpi->rc.gf_rate_correction_factor = factor;
else else
cpi->rc.rate_correction_factor = factor; cpi->rc.rate_correction_factor = factor;
...@@ -538,7 +564,12 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -538,7 +564,12 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
active_best_quality = cpi->cq_target_quality; active_best_quality = cpi->cq_target_quality;
} else { } else {
active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; // Use the lower of active_worst_quality and recent/average Q.
if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
else
active_best_quality = inter_minq[active_worst_quality];
//
// For the constrained quality mode we don't want // For the constrained quality mode we don't want
// q to fall below the cq level. // q to fall below the cq level.
if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) &&
...@@ -574,7 +605,6 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -574,7 +605,6 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
*top_index = (active_worst_quality + active_best_quality) / 2; *top_index = (active_worst_quality + active_best_quality) / 2;
} }
#endif #endif
if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) { if (oxcf->end_usage == USAGE_CONSTANT_QUALITY) {
q = active_best_quality; q = active_best_quality;
// Special case code to try and match quality with forced key frames // Special case code to try and match quality with forced key frames
...@@ -1002,21 +1032,6 @@ void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { ...@@ -1002,21 +1032,6 @@ void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
cpi->rc.frames_to_key--; cpi->rc.frames_to_key--;
} }
void vp9_rc_get_svc_params(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common;
if ((cm->current_video_frame == 0) ||
(cm->frame_flags & FRAMEFLAGS_KEY) ||
(cpi->oxcf.auto_key && (cpi->rc.frames_since_key %
cpi->key_frame_frequency == 0))) {
cm->frame_type = KEY_FRAME;
cpi->rc.source_alt_ref_active = 0;
} else {
cm->frame_type = INTER_FRAME;
}
cpi->rc.frames_till_gf_update_due = INT_MAX;
cpi->rc.baseline_gf_interval = INT_MAX;
}
static int test_for_kf_one_pass(VP9_COMP *cpi) { static int test_for_kf_one_pass(VP9_COMP *cpi) {
// Placeholder function for auto key frame // Placeholder function for auto key frame
return 0; return 0;
...@@ -1171,6 +1186,32 @@ static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { ...@@ -1171,6 +1186,32 @@ static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
return target; return target;
} }
void vp9_rc_get_svc_params(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common;
int target = cpi->rc.av_per_frame_bandwidth;
if ((cm->current_video_frame == 0) ||
(cm->frame_flags & FRAMEFLAGS_KEY) ||
(cpi->oxcf.auto_key && (cpi->rc.frames_since_key %
cpi->key_frame_frequency == 0))) {
cm->frame_type = KEY_FRAME;
cpi->rc.source_alt_ref_active = 0;
if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
target = calc_iframe_target_size_one_pass_cbr(cpi);
cpi->rc.active_worst_quality = cpi->rc.worst_quality;
}
} else {
cm->frame_type = INTER_FRAME;
if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
target = calc_pframe_target_size_one_pass_cbr(cpi);
cpi->rc.active_worst_quality =
calc_active_worst_quality_one_pass_cbr(cpi);
}
}
vp9_rc_set_frame_target(cpi, target);
cpi->rc.frames_till_gf_update_due = INT_MAX;
cpi->rc.baseline_gf_interval = INT_MAX;
}
void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common; VP9_COMMON *const cm = &cpi->common;
int target; int target;
......
...@@ -175,6 +175,23 @@ static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx, ...@@ -175,6 +175,23 @@ static vpx_codec_err_t validate_config(vpx_codec_alg_priv_t *ctx,
RANGE_CHECK(cfg, ss_number_layers, 1, RANGE_CHECK(cfg, ss_number_layers, 1,
VPX_SS_MAX_LAYERS); /*Spatial layers max */ VPX_SS_MAX_LAYERS); /*Spatial layers max */
RANGE_CHECK(cfg, ts_number_layers, 1, VPX_TS_MAX_LAYERS);
if (cfg->ts_number_layers > 1) {
int i;
for (i = 1; i < cfg->ts_number_layers; ++i) {
if (cfg->ts_target_bitrate[i] < cfg->ts_target_bitrate[i-1]) {
ERROR("ts_target_bitrate entries are not increasing");
}
}
RANGE_CHECK(cfg, ts_rate_decimator[cfg->ts_number_layers-1], 1, 1);
for (i = cfg->ts_number_layers-2; i > 0; --i) {
if (cfg->ts_rate_decimator[i-1] != 2*cfg->ts_rate_decimator[i]) {
ERROR("ts_rate_decimator factors are not powers of 2");
}
}
}
/* VP8 does not support a lower bound on the keyframe interval in /* VP8 does not support a lower bound on the keyframe interval in
* automatic keyframe placement mode. * automatic keyframe placement mode.
*/ */
...@@ -342,6 +359,19 @@ static vpx_codec_err_t set_vp9e_config(VP9_CONFIG *oxcf, ...@@ -342,6 +359,19 @@ static vpx_codec_err_t set_vp9e_config(VP9_CONFIG *oxcf,
oxcf->aq_mode = vp8_cfg.aq_mode; oxcf->aq_mode = vp8_cfg.aq_mode;
oxcf->ss_number_layers = cfg.ss_number_layers; oxcf->ss_number_layers = cfg.ss_number_layers;
oxcf->ts_number_layers = cfg.ts_number_layers;