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
18 #define SMALL_FRAME_WIDTH 16
19 #define SMALL_FRAME_HEIGHT 16
21 void vp9_init_layer_context(VP9_COMP *const cpi) {
22 SVC *const svc = &cpi->svc;
23 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
26 int alt_ref_idx = svc->number_spatial_layers;
28 svc->spatial_layer_id = 0;
29 svc->temporal_layer_id = 0;
31 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
32 layer_end = svc->number_temporal_layers;
34 layer_end = svc->number_spatial_layers;
36 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
37 if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img,
38 SMALL_FRAME_WIDTH, SMALL_FRAME_HEIGHT,
39 cpi->common.subsampling_x,
40 cpi->common.subsampling_y,
41 #if CONFIG_VP9_HIGHBITDEPTH
42 cpi->common.use_highbitdepth,
44 VP9_ENC_BORDER_IN_PIXELS,
45 cpi->common.byte_alignment,
47 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
48 "Failed to allocate empty frame for multiple frame "
51 memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
52 cpi->svc.empty_frame.img.buffer_alloc_sz);
56 for (layer = 0; layer < layer_end; ++layer) {
57 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
58 RATE_CONTROL *const lrc = &lc->rc;
60 lc->current_video_frame_in_layer = 0;
62 lc->frames_from_key_frame = 0;
63 lc->last_frame_type = FRAME_TYPES;
64 lrc->ni_av_qi = oxcf->worst_allowed_q;
65 lrc->total_actual_bits = 0;
66 lrc->total_target_vs_actual = 0;
71 lrc->decimation_count = 0;
72 lrc->decimation_factor = 0;
74 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
75 lrc->rate_correction_factors[i] = 1.0;
78 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
79 lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
80 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
81 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
82 lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
84 lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
85 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
86 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
87 lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
88 oxcf->best_allowed_q) / 2;
89 lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
90 oxcf->best_allowed_q) / 2;
91 if (oxcf->ss_enable_auto_arf[layer])
92 lc->alt_ref_idx = alt_ref_idx++;
94 lc->alt_ref_idx = INVALID_IDX;
95 lc->gold_ref_idx = INVALID_IDX;
98 lrc->buffer_level = oxcf->starting_buffer_level_ms *
99 lc->target_bandwidth / 1000;
100 lrc->bits_off_target = lrc->buffer_level;
103 // Still have extra buffer for base layer golden frame
104 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
105 && alt_ref_idx < REF_FRAMES)
106 svc->layer_context[0].gold_ref_idx = alt_ref_idx;
109 // Update the layer context from a change_config() call.
110 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
111 const int target_bandwidth) {
112 SVC *const svc = &cpi->svc;
113 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
114 const RATE_CONTROL *const rc = &cpi->rc;
117 float bitrate_alloc = 1.0;
119 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
120 layer_end = svc->number_temporal_layers;
122 layer_end = svc->number_spatial_layers;
125 for (layer = 0; layer < layer_end; ++layer) {
126 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
127 RATE_CONTROL *const lrc = &lc->rc;
129 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
130 lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
132 lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
134 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
135 // Update buffer-related quantities.
136 lrc->starting_buffer_level =
137 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
138 lrc->optimal_buffer_level =
139 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
140 lrc->maximum_buffer_size =
141 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
142 lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
143 lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size);
144 // Update framerate-related quantities.
145 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
146 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
148 lc->framerate = cpi->framerate;
150 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
151 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
152 // Update qp-related quantities.
153 lrc->worst_quality = rc->worst_quality;
154 lrc->best_quality = rc->best_quality;
158 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
159 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
160 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
161 &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
164 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
165 SVC *const svc = &cpi->svc;
166 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
167 LAYER_CONTEXT *const lc = get_layer_context(cpi);
168 RATE_CONTROL *const lrc = &lc->rc;
169 const int layer = svc->temporal_layer_id;
171 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
172 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
173 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
174 // Update the average layer frame size (non-cumulative per-frame-bw).
176 lc->avg_frame_size = lrc->avg_frame_bandwidth;
178 const double prev_layer_framerate =
179 cpi->framerate / oxcf->ts_rate_decimator[layer - 1];
180 const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1];
182 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
183 (lc->framerate - prev_layer_framerate));
187 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
188 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
189 LAYER_CONTEXT *const lc = get_layer_context(cpi);
190 RATE_CONTROL *const lrc = &lc->rc;
192 lc->framerate = framerate;
193 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
194 lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth *
195 oxcf->two_pass_vbrmin_section / 100);
196 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
197 oxcf->two_pass_vbrmax_section) / 100);
198 vp9_rc_set_gf_interval_range(cpi, lrc);
201 void vp9_restore_layer_context(VP9_COMP *const cpi) {
202 LAYER_CONTEXT *const lc = get_layer_context(cpi);
203 const int old_frame_since_key = cpi->rc.frames_since_key;
204 const int old_frame_to_key = cpi->rc.frames_to_key;
207 cpi->twopass = lc->twopass;
208 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
209 cpi->alt_ref_source = lc->alt_ref_source;
210 // Reset the frames_since_key and frames_to_key counters to their values
211 // before the layer restore. Keep these defined for the stream (not layer).
212 if (cpi->svc.number_temporal_layers > 1) {
213 cpi->rc.frames_since_key = old_frame_since_key;
214 cpi->rc.frames_to_key = old_frame_to_key;
218 void vp9_save_layer_context(VP9_COMP *const cpi) {
219 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
220 LAYER_CONTEXT *const lc = get_layer_context(cpi);
223 lc->twopass = cpi->twopass;
224 lc->target_bandwidth = (int)oxcf->target_bandwidth;
225 lc->alt_ref_source = cpi->alt_ref_source;
228 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
229 SVC *const svc = &cpi->svc;
232 for (i = 0; i < svc->number_spatial_layers; ++i) {
233 TWO_PASS *const twopass = &svc->layer_context[i].twopass;
235 svc->spatial_layer_id = i;
236 vp9_init_second_pass(cpi);
238 twopass->total_stats.spatial_layer_id = i;
239 twopass->total_left_stats.spatial_layer_id = i;
241 svc->spatial_layer_id = 0;
244 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
245 LAYER_CONTEXT *const lc =
246 (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
247 &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
248 &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
249 ++lc->current_video_frame_in_layer;
250 ++lc->frames_from_key_frame;
253 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
254 return is_two_pass_svc(cpi) &&
255 cpi->svc.spatial_layer_id > 0 &&
256 cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame;
259 #if CONFIG_SPATIAL_SVC
260 static void get_layer_resolution(const int width_org, const int height_org,
261 const int num, const int den,
262 int *width_out, int *height_out) {
265 if (width_out == NULL || height_out == NULL || den == 0)
268 w = width_org * num / den;
269 h = height_org * num / den;
271 // make height and width even to make chrome player happy
279 int vp9_svc_start_frame(VP9_COMP *const cpi) {
280 int width = 0, height = 0;
282 struct lookahead_entry *buf;
283 int count = 1 << (cpi->svc.number_temporal_layers - 1);
285 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
286 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
288 cpi->svc.temporal_layer_id = 0;
289 while ((lc->current_video_frame_in_layer % count) != 0) {
290 ++cpi->svc.temporal_layer_id;
294 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
296 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
298 if (cpi->svc.spatial_layer_id == 0)
299 cpi->gld_fb_idx = (lc->gold_ref_idx >= 0) ?
300 lc->gold_ref_idx : cpi->lst_fb_idx;
302 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
304 if (lc->current_video_frame_in_layer == 0) {
305 if (cpi->svc.spatial_layer_id >= 2) {
306 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
308 cpi->alt_fb_idx = cpi->lst_fb_idx;
309 cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
312 if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
313 cpi->alt_fb_idx = lc->alt_ref_idx;
314 if (!lc->has_alt_frame)
315 cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
317 // Find a proper alt_fb_idx for layers that don't have alt ref frame
318 if (cpi->svc.spatial_layer_id == 0) {
319 cpi->alt_fb_idx = cpi->lst_fb_idx;
321 LAYER_CONTEXT *lc_lower =
322 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
324 if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
325 lc_lower->alt_ref_source != NULL)
326 cpi->alt_fb_idx = lc_lower->alt_ref_idx;
327 else if (cpi->svc.spatial_layer_id >= 2)
328 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
330 cpi->alt_fb_idx = cpi->lst_fb_idx;
335 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
336 lc->scaling_factor_num, lc->scaling_factor_den,
339 // Workaround for multiple frame contexts. In some frames we can't use prev_mi
340 // since its previous frame could be changed during decoding time. The idea is
341 // we put a empty invisible frame in front of them, then we will not use
342 // prev_mi when encoding these frames.
344 buf = vp9_lookahead_peek(cpi->lookahead, 0);
345 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
346 cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
347 lc->rc.frames_to_key != 0 &&
348 !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
349 if ((cpi->svc.number_temporal_layers > 1 &&
350 cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
351 (cpi->svc.number_spatial_layers > 1 &&
352 cpi->svc.spatial_layer_id == 0)) {
353 struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
356 cpi->svc.empty_frame.ts_start = buf->ts_start;
357 cpi->svc.empty_frame.ts_end = buf->ts_end;
358 cpi->svc.encode_empty_frame_state = ENCODING;
359 cpi->common.show_frame = 0;
360 cpi->ref_frame_flags = 0;
361 cpi->common.frame_type = INTER_FRAME;
363 cpi->gld_fb_idx = cpi->alt_fb_idx = SMALL_FRAME_FB_IDX;
365 if (cpi->svc.encode_intra_empty_frame != 0)
366 cpi->common.intra_only = 1;
368 width = SMALL_FRAME_WIDTH;
369 height = SMALL_FRAME_HEIGHT;
374 cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
375 cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
377 vp9_change_config(cpi, &cpi->oxcf);
379 if (vp9_set_size_literal(cpi, width, height) != 0)
380 return VPX_CODEC_INVALID_PARAM;
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);