2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #include "vp9/encoder/vp9_encoder.h"
14 #include "vp9/encoder/vp9_svc_layercontext.h"
15 #include "vp9/encoder/vp9_extend.h"
17 #define SMALL_FRAME_FB_IDX 7
19 void vp9_init_layer_context(VP9_COMP *const cpi) {
20 SVC *const svc = &cpi->svc;
21 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
24 int alt_ref_idx = svc->number_spatial_layers;
26 svc->spatial_layer_id = 0;
27 svc->temporal_layer_id = 0;
29 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
30 layer_end = svc->number_temporal_layers;
32 layer_end = svc->number_spatial_layers;
34 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
35 if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img,
36 cpi->common.width, cpi->common.height,
37 cpi->common.subsampling_x,
38 cpi->common.subsampling_y,
39 #if CONFIG_VP9_HIGHBITDEPTH
40 cpi->common.use_highbitdepth,
42 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
43 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
44 "Failed to allocate empty frame for multiple frame "
47 vpx_memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
48 cpi->svc.empty_frame.img.buffer_alloc_sz);
49 cpi->svc.empty_frame_width = cpi->common.width;
50 cpi->svc.empty_frame_height = cpi->common.height;
54 for (layer = 0; layer < layer_end; ++layer) {
55 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
56 RATE_CONTROL *const lrc = &lc->rc;
58 lc->current_video_frame_in_layer = 0;
60 lc->frames_from_key_frame = 0;
61 lc->last_frame_type = FRAME_TYPES;
62 lrc->ni_av_qi = oxcf->worst_allowed_q;
63 lrc->total_actual_bits = 0;
64 lrc->total_target_vs_actual = 0;
69 lrc->decimation_count = 0;
70 lrc->decimation_factor = 0;
72 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
73 lrc->rate_correction_factors[i] = 1.0;
76 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
77 lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
78 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
79 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
81 lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
82 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
83 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
84 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
85 oxcf->best_allowed_q) / 2;
86 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
87 oxcf->best_allowed_q) / 2;
88 if (oxcf->ss_play_alternate[layer])
89 lc->alt_ref_idx = alt_ref_idx++;
92 lc->gold_ref_idx = -1;
95 lrc->buffer_level = oxcf->starting_buffer_level_ms *
96 lc->target_bandwidth / 1000;
97 lrc->bits_off_target = lrc->buffer_level;
100 // Still have extra buffer for base layer golden frame
101 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
102 && alt_ref_idx < REF_FRAMES)
103 svc->layer_context[0].gold_ref_idx = alt_ref_idx;
106 // Update the layer context from a change_config() call.
107 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
108 const int target_bandwidth) {
109 SVC *const svc = &cpi->svc;
110 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
111 const RATE_CONTROL *const rc = &cpi->rc;
114 float bitrate_alloc = 1.0;
116 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
117 layer_end = svc->number_temporal_layers;
119 layer_end = svc->number_spatial_layers;
122 for (layer = 0; layer < layer_end; ++layer) {
123 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
124 RATE_CONTROL *const lrc = &lc->rc;
126 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
127 lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
129 lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
131 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
132 // Update buffer-related quantities.
133 lrc->starting_buffer_level =
134 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
135 lrc->optimal_buffer_level =
136 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
137 lrc->maximum_buffer_size =
138 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
139 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
140 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size);
141 // Update framerate-related quantities.
142 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
143 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
145 lc->framerate = cpi->framerate;
147 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
148 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
149 // Update qp-related quantities.
150 lrc->worst_quality = rc->worst_quality;
151 lrc->best_quality = rc->best_quality;
155 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
156 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
157 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
158 &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
161 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
162 SVC *const svc = &cpi->svc;
163 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
164 LAYER_CONTEXT *const lc = get_layer_context(cpi);
165 RATE_CONTROL *const lrc = &lc->rc;
166 const int layer = svc->temporal_layer_id;
168 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
169 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
170 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
171 // Update the average layer frame size (non-cumulative per-frame-bw).
173 lc->avg_frame_size = lrc->avg_frame_bandwidth;
175 const double prev_layer_framerate =
176 cpi->framerate / oxcf->ts_rate_decimator[layer - 1];
177 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1];
179 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
180 (lc->framerate - prev_layer_framerate));
184 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
185 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
186 LAYER_CONTEXT *const lc = get_layer_context(cpi);
187 RATE_CONTROL *const lrc = &lc->rc;
189 lc->framerate = framerate;
190 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
191 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth *
192 oxcf->two_pass_vbrmin_section / 100);
193 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
194 oxcf->two_pass_vbrmax_section) / 100);
195 vp9_rc_set_gf_max_interval(cpi, lrc);
198 void vp9_restore_layer_context(VP9_COMP *const cpi) {
199 LAYER_CONTEXT *const lc = get_layer_context(cpi);
200 const int old_frame_since_key = cpi->rc.frames_since_key;
201 const int old_frame_to_key = cpi->rc.frames_to_key;
204 cpi->twopass = lc->twopass;
205 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
206 cpi->alt_ref_source = lc->alt_ref_source;
207 // Reset the frames_since_key and frames_to_key counters to their values
208 // before the layer restore. Keep these defined for the stream (not layer).
209 if (cpi->svc.number_temporal_layers > 1) {
210 cpi->rc.frames_since_key = old_frame_since_key;
211 cpi->rc.frames_to_key = old_frame_to_key;
215 void vp9_save_layer_context(VP9_COMP *const cpi) {
216 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
217 LAYER_CONTEXT *const lc = get_layer_context(cpi);
220 lc->twopass = cpi->twopass;
221 lc->target_bandwidth = (int)oxcf->target_bandwidth;
222 lc->alt_ref_source = cpi->alt_ref_source;
225 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
226 SVC *const svc = &cpi->svc;
229 for (i = 0; i < svc->number_spatial_layers; ++i) {
230 TWO_PASS *const twopass = &svc->layer_context[i].twopass;
232 svc->spatial_layer_id = i;
233 vp9_init_second_pass(cpi);
235 twopass->total_stats.spatial_layer_id = i;
236 twopass->total_left_stats.spatial_layer_id = i;
238 svc->spatial_layer_id = 0;
241 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
242 LAYER_CONTEXT *const lc =
243 (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
244 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
245 &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
246 ++lc->current_video_frame_in_layer;
247 ++lc->frames_from_key_frame;
250 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
251 return is_two_pass_svc(cpi) &&
252 cpi->svc.spatial_layer_id > 0 &&
253 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame;
256 #if CONFIG_SPATIAL_SVC
257 static void get_layer_resolution(const int width_org, const int height_org,
258 const int num, const int den,
259 int *width_out, int *height_out) {
262 if (width_out == NULL || height_out == NULL || den == 0)
265 w = width_org * num / den;
266 h = height_org * num / den;
268 // make height and width even to make chrome player happy
276 int vp9_svc_start_frame(VP9_COMP *const cpi) {
277 int width = 0, height = 0;
279 int count = 1 << (cpi->svc.number_temporal_layers - 1);
281 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
282 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
284 cpi->svc.temporal_layer_id = 0;
285 while ((lc->current_video_frame_in_layer % count) != 0) {
286 ++cpi->svc.temporal_layer_id;
290 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
292 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
294 if (cpi->svc.spatial_layer_id == 0)
295 cpi->gld_fb_idx = (lc->gold_ref_idx >= 0) ?
296 lc->gold_ref_idx : cpi->lst_fb_idx;
298 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
300 if (lc->current_video_frame_in_layer == 0) {
301 if (cpi->svc.spatial_layer_id >= 2) {
302 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
304 cpi->alt_fb_idx = cpi->lst_fb_idx;
305 cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
308 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]) {
309 cpi->alt_fb_idx = lc->alt_ref_idx;
310 if (!lc->has_alt_frame)
311 cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
313 // Find a proper alt_fb_idx for layers that don't have alt ref frame
314 if (cpi->svc.spatial_layer_id == 0) {
315 cpi->alt_fb_idx = cpi->lst_fb_idx;
317 LAYER_CONTEXT *lc_lower =
318 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
320 if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id - 1] &&
321 lc_lower->alt_ref_source != NULL)
322 cpi->alt_fb_idx = lc_lower->alt_ref_idx;
323 else if (cpi->svc.spatial_layer_id >= 2)
324 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
326 cpi->alt_fb_idx = cpi->lst_fb_idx;
331 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
332 lc->scaling_factor_num, lc->scaling_factor_den,
335 // Workaround for multiple frame contexts. In some frames we can't use prev_mi
336 // since its previous frame could be changed during decoding time. The idea is
337 // we put a empty invisible frame in front of them, then we will not use
338 // prev_mi when encoding these frames.
339 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
340 cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE) {
341 if ((cpi->svc.number_temporal_layers > 1 &&
342 cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
343 (cpi->svc.number_spatial_layers > 1 &&
344 cpi->svc.spatial_layer_id == 0)) {
345 struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
348 cpi->svc.empty_frame.ts_start = buf->ts_start;
349 cpi->svc.empty_frame.ts_end = buf->ts_end;
350 cpi->svc.encode_empty_frame_state = ENCODING;
351 cpi->common.show_frame = 0;
352 cpi->ref_frame_flags = 0;
353 cpi->common.frame_type = INTER_FRAME;
355 cpi->gld_fb_idx = cpi->alt_fb_idx = SMALL_FRAME_FB_IDX;
357 // Gradually make the empty frame smaller to save bits. Make it half of
358 // its previous size because of the scaling factor restriction.
359 cpi->svc.empty_frame_width >>= 1;
360 cpi->svc.empty_frame_width = (cpi->svc.empty_frame_width + 1) & ~1;
361 if (cpi->svc.empty_frame_width < 16)
362 cpi->svc.empty_frame_width = 16;
364 cpi->svc.empty_frame_height >>= 1;
365 cpi->svc.empty_frame_height = (cpi->svc.empty_frame_height + 1) & ~1;
366 if (cpi->svc.empty_frame_height < 16)
367 cpi->svc.empty_frame_height = 16;
369 width = cpi->svc.empty_frame_width;
370 height = cpi->svc.empty_frame_height;
375 if (vp9_set_size_literal(cpi, width, height) != 0)
376 return VPX_CODEC_INVALID_PARAM;
378 cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
379 cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
381 vp9_change_config(cpi, &cpi->oxcf);
382 vp9_set_high_precision_mv(cpi, 1);
384 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
389 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
390 struct lookahead_ctx *ctx,
392 struct lookahead_entry *buf = NULL;
394 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
395 buf = vp9_lookahead_peek(ctx, 0);
397 // Only remove the buffer when pop the highest layer.
398 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
399 vp9_lookahead_pop(ctx, drain);