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_aq_cyclicrefresh.h"
14 #include "vp9/encoder/vp9_encoder.h"
15 #include "vp9/encoder/vp9_svc_layercontext.h"
16 #include "vp9/encoder/vp9_extend.h"
17 #include "vpx_dsp/vpx_dsp_common.h"
19 #define SMALL_FRAME_WIDTH 32
20 #define SMALL_FRAME_HEIGHT 16
22 void vp9_init_layer_context(VP9_COMP *const cpi) {
23 SVC *const svc = &cpi->svc;
24 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
25 int mi_rows = cpi->common.mi_rows;
26 int mi_cols = cpi->common.mi_cols;
28 int alt_ref_idx = svc->number_spatial_layers;
30 svc->spatial_layer_id = 0;
31 svc->temporal_layer_id = 0;
32 svc->first_spatial_layer_to_encode = 0;
33 svc->rc_drop_superframe = 0;
34 svc->force_zero_mode_spatial_ref = 0;
36 svc->scaled_temp_is_alloc = 0;
37 svc->scaled_one_half = 0;
38 svc->current_superframe = 0;
39 svc->non_reference_frame = 0;
40 for (i = 0; i < REF_FRAMES; ++i) svc->ref_frame_index[i] = -1;
41 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
42 svc->ext_frame_flags[sl] = 0;
43 svc->ext_lst_fb_idx[sl] = 0;
44 svc->ext_gld_fb_idx[sl] = 1;
45 svc->ext_alt_fb_idx[sl] = 2;
46 svc->downsample_filter_type[sl] = EIGHTTAP;
47 svc->downsample_filter_phase[sl] = 0; // Set to 8 for averaging filter.
50 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
51 if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
52 SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
53 cpi->common.subsampling_y,
54 #if CONFIG_VP9_HIGHBITDEPTH
55 cpi->common.use_highbitdepth,
57 VP9_ENC_BORDER_IN_PIXELS,
58 cpi->common.byte_alignment, NULL, NULL, NULL))
59 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
60 "Failed to allocate empty frame for multiple frame "
63 memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
64 cpi->svc.empty_frame.img.buffer_alloc_sz);
67 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
68 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
69 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
70 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
71 RATE_CONTROL *const lrc = &lc->rc;
73 lc->current_video_frame_in_layer = 0;
75 lc->frames_from_key_frame = 0;
76 lc->last_frame_type = FRAME_TYPES;
77 lrc->ni_av_qi = oxcf->worst_allowed_q;
78 lrc->total_actual_bits = 0;
79 lrc->total_target_vs_actual = 0;
84 lrc->decimation_count = 0;
85 lrc->decimation_factor = 0;
87 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
88 lrc->rate_correction_factors[i] = 1.0;
91 if (cpi->oxcf.rc_mode == VPX_CBR) {
92 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
93 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
94 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
95 lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
97 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
98 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
99 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
100 lrc->avg_frame_qindex[KEY_FRAME] =
101 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
102 lrc->avg_frame_qindex[INTER_FRAME] =
103 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
104 if (oxcf->ss_enable_auto_arf[sl])
105 lc->alt_ref_idx = alt_ref_idx++;
107 lc->alt_ref_idx = INVALID_IDX;
108 lc->gold_ref_idx = INVALID_IDX;
112 oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
113 lrc->bits_off_target = lrc->buffer_level;
115 // Initialize the cyclic refresh parameters. If spatial layers are used
116 // (i.e., ss_number_layers > 1), these need to be updated per spatial
118 // Cyclic refresh is only applied on base temporal layer.
119 if (oxcf->ss_number_layers > 1 && tl == 0) {
120 size_t last_coded_q_map_size;
121 size_t consec_zero_mv_size;
122 VP9_COMMON *const cm = &cpi->common;
124 CHECK_MEM_ERROR(cm, lc->map,
125 vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
126 memset(lc->map, 0, mi_rows * mi_cols);
127 last_coded_q_map_size =
128 mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
129 CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
130 vpx_malloc(last_coded_q_map_size));
132 memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
133 consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
134 CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
135 vpx_malloc(consec_zero_mv_size));
136 memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
141 // Still have extra buffer for base layer golden frame
142 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
143 alt_ref_idx < REF_FRAMES)
144 svc->layer_context[0].gold_ref_idx = alt_ref_idx;
147 // Update the layer context from a change_config() call.
148 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
149 const int target_bandwidth) {
150 SVC *const svc = &cpi->svc;
151 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
152 const RATE_CONTROL *const rc = &cpi->rc;
153 int sl, tl, layer = 0, spatial_layer_target;
154 float bitrate_alloc = 1.0;
156 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
157 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
158 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
159 layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
160 svc->layer_context[layer].target_bandwidth =
161 oxcf->layer_target_bitrate[layer];
164 layer = LAYER_IDS_TO_IDX(
166 ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
167 oxcf->ts_number_layers);
168 spatial_layer_target = svc->layer_context[layer].target_bandwidth =
169 oxcf->layer_target_bitrate[layer];
171 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
172 LAYER_CONTEXT *const lc =
173 &svc->layer_context[sl * oxcf->ts_number_layers + tl];
174 RATE_CONTROL *const lrc = &lc->rc;
176 lc->spatial_layer_target_bandwidth = spatial_layer_target;
177 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
178 lrc->starting_buffer_level =
179 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
180 lrc->optimal_buffer_level =
181 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
182 lrc->maximum_buffer_size =
183 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
184 lrc->bits_off_target =
185 VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
186 lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
187 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
188 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
189 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
190 lrc->worst_quality = rc->worst_quality;
191 lrc->best_quality = rc->best_quality;
197 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
198 layer_end = svc->number_temporal_layers;
200 layer_end = svc->number_spatial_layers;
203 for (layer = 0; layer < layer_end; ++layer) {
204 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
205 RATE_CONTROL *const lrc = &lc->rc;
207 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
209 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
210 // Update buffer-related quantities.
211 lrc->starting_buffer_level =
212 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
213 lrc->optimal_buffer_level =
214 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
215 lrc->maximum_buffer_size =
216 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
217 lrc->bits_off_target =
218 VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
219 lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
220 // Update framerate-related quantities.
221 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
222 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
224 lc->framerate = cpi->framerate;
226 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
227 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
228 // Update qp-related quantities.
229 lrc->worst_quality = rc->worst_quality;
230 lrc->best_quality = rc->best_quality;
235 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
236 if (is_one_pass_cbr_svc(cpi))
237 return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
238 cpi->svc.number_temporal_layers +
239 cpi->svc.temporal_layer_id];
241 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
242 ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
243 : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
246 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
247 SVC *const svc = &cpi->svc;
248 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
249 LAYER_CONTEXT *const lc = get_layer_context(cpi);
250 RATE_CONTROL *const lrc = &lc->rc;
251 // Index into spatial+temporal arrays.
252 const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
253 svc->temporal_layer_id;
254 const int tl = svc->temporal_layer_id;
256 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
257 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
258 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
259 // Update the average layer frame size (non-cumulative per-frame-bw).
261 lc->avg_frame_size = lrc->avg_frame_bandwidth;
263 const double prev_layer_framerate =
264 cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
265 const int prev_layer_target_bandwidth =
266 oxcf->layer_target_bitrate[st_idx - 1];
268 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
269 (lc->framerate - prev_layer_framerate));
273 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
274 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
275 LAYER_CONTEXT *const lc = get_layer_context(cpi);
276 RATE_CONTROL *const lrc = &lc->rc;
278 lc->framerate = framerate;
279 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
280 lrc->min_frame_bandwidth =
281 (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
282 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
283 oxcf->two_pass_vbrmax_section) /
285 vp9_rc_set_gf_interval_range(cpi, lrc);
288 void vp9_restore_layer_context(VP9_COMP *const cpi) {
289 LAYER_CONTEXT *const lc = get_layer_context(cpi);
290 const int old_frame_since_key = cpi->rc.frames_since_key;
291 const int old_frame_to_key = cpi->rc.frames_to_key;
294 cpi->twopass = lc->twopass;
295 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
296 cpi->alt_ref_source = lc->alt_ref_source;
297 // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
298 // does not use speed = 0).
299 if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
300 cpi->oxcf.speed = lc->speed;
302 // Reset the frames_since_key and frames_to_key counters to their values
303 // before the layer restore. Keep these defined for the stream (not layer).
304 if (cpi->svc.number_temporal_layers > 1 ||
305 (cpi->svc.number_spatial_layers > 1 && !is_two_pass_svc(cpi))) {
306 cpi->rc.frames_since_key = old_frame_since_key;
307 cpi->rc.frames_to_key = old_frame_to_key;
310 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
311 // for the base temporal layer.
312 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
313 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
314 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
315 signed char *temp = cr->map;
316 uint8_t *temp2 = cr->last_coded_q_map;
317 uint8_t *temp3 = cpi->consec_zero_mv;
320 cr->last_coded_q_map = lc->last_coded_q_map;
321 lc->last_coded_q_map = temp2;
322 cpi->consec_zero_mv = lc->consec_zero_mv;
323 lc->consec_zero_mv = temp3;
324 cr->sb_index = lc->sb_index;
328 void vp9_save_layer_context(VP9_COMP *const cpi) {
329 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
330 LAYER_CONTEXT *const lc = get_layer_context(cpi);
333 lc->twopass = cpi->twopass;
334 lc->target_bandwidth = (int)oxcf->target_bandwidth;
335 lc->alt_ref_source = cpi->alt_ref_source;
337 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
338 // for the base temporal layer.
339 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
340 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
341 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
342 signed char *temp = lc->map;
343 uint8_t *temp2 = lc->last_coded_q_map;
344 uint8_t *temp3 = lc->consec_zero_mv;
347 lc->last_coded_q_map = cr->last_coded_q_map;
348 cr->last_coded_q_map = temp2;
349 lc->consec_zero_mv = cpi->consec_zero_mv;
350 cpi->consec_zero_mv = temp3;
351 lc->sb_index = cr->sb_index;
355 #if !CONFIG_REALTIME_ONLY
356 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
357 SVC *const svc = &cpi->svc;
360 for (i = 0; i < svc->number_spatial_layers; ++i) {
361 TWO_PASS *const twopass = &svc->layer_context[i].twopass;
363 svc->spatial_layer_id = i;
364 vp9_init_second_pass(cpi);
366 twopass->total_stats.spatial_layer_id = i;
367 twopass->total_left_stats.spatial_layer_id = i;
369 svc->spatial_layer_id = 0;
371 #endif // !CONFIG_REALTIME_ONLY
373 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
374 LAYER_CONTEXT *const lc =
375 &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
376 cpi->svc.number_temporal_layers];
377 ++lc->current_video_frame_in_layer;
378 ++lc->frames_from_key_frame;
379 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
380 ++cpi->svc.current_superframe;
383 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
384 return is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0 &&
386 .layer_context[cpi->svc.spatial_layer_id *
387 cpi->svc.number_temporal_layers +
388 cpi->svc.temporal_layer_id]
392 static void get_layer_resolution(const int width_org, const int height_org,
393 const int num, const int den, int *width_out,
397 if (width_out == NULL || height_out == NULL || den == 0) return;
399 w = width_org * num / den;
400 h = height_org * num / den;
402 // make height and width even to make chrome player happy
410 // The function sets proper ref_frame_flags, buffer indices, and buffer update
411 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
413 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
414 int frame_num_within_temporal_struct = 0;
415 int spatial_id, temporal_id;
416 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
417 frame_num_within_temporal_struct =
419 .layer_context[cpi->svc.spatial_layer_id *
420 cpi->svc.number_temporal_layers]
421 .current_video_frame_in_layer %
423 temporal_id = cpi->svc.temporal_layer_id =
424 (frame_num_within_temporal_struct & 1)
426 : (frame_num_within_temporal_struct >> 1);
427 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
428 cpi->ext_refresh_alt_ref_frame = 0;
430 cpi->ext_refresh_frame_flags_pending = 1;
431 cpi->ext_refresh_last_frame = 1;
433 cpi->ref_frame_flags = VP9_LAST_FLAG;
434 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
435 // base layer is a key frame.
436 cpi->ref_frame_flags = VP9_LAST_FLAG;
437 cpi->ext_refresh_last_frame = 0;
438 cpi->ext_refresh_golden_frame = 1;
440 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
442 } else if (temporal_id == 1) {
443 cpi->ext_refresh_frame_flags_pending = 1;
444 cpi->ext_refresh_alt_ref_frame = 1;
446 cpi->ref_frame_flags = VP9_LAST_FLAG;
448 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
451 if (frame_num_within_temporal_struct == 1) {
452 // the first tl2 picture
453 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
454 cpi->ext_refresh_frame_flags_pending = 1;
456 cpi->ref_frame_flags = VP9_LAST_FLAG;
458 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
459 } else if (!spatial_id) {
460 cpi->ext_refresh_frame_flags_pending = 1;
461 cpi->ext_refresh_alt_ref_frame = 1;
462 cpi->ref_frame_flags = VP9_LAST_FLAG;
463 } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
464 cpi->ext_refresh_frame_flags_pending = 1;
465 cpi->ext_refresh_alt_ref_frame = 1;
466 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
469 // The second tl2 picture
470 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
471 cpi->ext_refresh_frame_flags_pending = 1;
473 cpi->ref_frame_flags = VP9_LAST_FLAG;
475 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
476 } else if (!spatial_id) {
477 cpi->ext_refresh_frame_flags_pending = 1;
478 cpi->ref_frame_flags = VP9_LAST_FLAG;
479 cpi->ext_refresh_alt_ref_frame = 1;
480 } else { // top layer
481 cpi->ext_refresh_frame_flags_pending = 1;
482 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
483 cpi->ext_refresh_alt_ref_frame = 1;
487 if (temporal_id == 0) {
488 cpi->lst_fb_idx = spatial_id;
490 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
491 cpi->lst_fb_idx = spatial_id - 1;
492 cpi->gld_fb_idx = spatial_id;
494 cpi->gld_fb_idx = spatial_id - 1;
500 } else if (temporal_id == 1) {
501 cpi->lst_fb_idx = spatial_id;
502 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
503 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
504 } else if (frame_num_within_temporal_struct == 1) {
505 cpi->lst_fb_idx = spatial_id;
506 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
507 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
509 cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
510 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
511 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
515 // The function sets proper ref_frame_flags, buffer indices, and buffer update
516 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
518 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
519 int spatial_id, temporal_id;
520 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
521 temporal_id = cpi->svc.temporal_layer_id =
523 .layer_context[cpi->svc.spatial_layer_id *
524 cpi->svc.number_temporal_layers]
525 .current_video_frame_in_layer &
527 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
528 cpi->ext_refresh_alt_ref_frame = 0;
530 cpi->ext_refresh_frame_flags_pending = 1;
531 cpi->ext_refresh_last_frame = 1;
533 cpi->ref_frame_flags = VP9_LAST_FLAG;
534 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
535 // base layer is a key frame.
536 cpi->ref_frame_flags = VP9_LAST_FLAG;
537 cpi->ext_refresh_last_frame = 0;
538 cpi->ext_refresh_golden_frame = 1;
540 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
542 } else if (temporal_id == 1) {
543 cpi->ext_refresh_frame_flags_pending = 1;
544 cpi->ext_refresh_alt_ref_frame = 1;
546 cpi->ref_frame_flags = VP9_LAST_FLAG;
548 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
552 if (temporal_id == 0) {
553 cpi->lst_fb_idx = spatial_id;
555 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
556 cpi->lst_fb_idx = spatial_id - 1;
557 cpi->gld_fb_idx = spatial_id;
559 cpi->gld_fb_idx = spatial_id - 1;
565 } else if (temporal_id == 1) {
566 cpi->lst_fb_idx = spatial_id;
567 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
568 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
572 // The function sets proper ref_frame_flags, buffer indices, and buffer update
573 // variables for temporal layering mode 0 - that has no temporal layering.
574 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
575 VP9_COMP *const cpi) {
577 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
578 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
579 cpi->ext_refresh_alt_ref_frame = 0;
580 cpi->ext_refresh_frame_flags_pending = 1;
581 cpi->ext_refresh_last_frame = 1;
583 cpi->ref_frame_flags = VP9_LAST_FLAG;
584 } else if (cpi->svc.layer_context[0].is_key_frame) {
585 cpi->ref_frame_flags = VP9_LAST_FLAG;
586 cpi->ext_refresh_last_frame = 0;
587 cpi->ext_refresh_golden_frame = 1;
589 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
591 cpi->lst_fb_idx = spatial_id;
593 if (cpi->svc.layer_context[0].is_key_frame) {
594 cpi->lst_fb_idx = spatial_id - 1;
595 cpi->gld_fb_idx = spatial_id;
597 cpi->gld_fb_idx = spatial_id - 1;
604 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
605 int width = 0, height = 0;
606 LAYER_CONTEXT *lc = NULL;
607 if (cpi->svc.number_spatial_layers > 1) cpi->svc.use_base_mv = 1;
608 cpi->svc.force_zero_mode_spatial_ref = 1;
609 cpi->svc.mi_stride[cpi->svc.spatial_layer_id] = cpi->common.mi_stride;
611 if (cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
612 set_flags_and_fb_idx_for_temporal_mode3(cpi);
613 } else if (cpi->svc.temporal_layering_mode ==
614 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
615 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
616 } else if (cpi->svc.temporal_layering_mode ==
617 VP9E_TEMPORAL_LAYERING_MODE_0101) {
618 set_flags_and_fb_idx_for_temporal_mode2(cpi);
619 } else if (cpi->svc.temporal_layering_mode ==
620 VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
621 // In the BYPASS/flexible mode, the encoder is relying on the application
622 // to specify, for each spatial layer, the flags and buffer indices for the
624 // Note that the check (cpi->ext_refresh_frame_flags_pending == 0) is
625 // needed to support the case where the frame flags may be passed in via
626 // vpx_codec_encode(), which can be used for the temporal-only svc case.
627 // TODO(marpan): Consider adding an enc_config parameter to better handle
629 if (cpi->ext_refresh_frame_flags_pending == 0) {
631 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
632 sl = cpi->svc.spatial_layer_id;
633 vp9_apply_encoding_flags(cpi, cpi->svc.ext_frame_flags[sl]);
634 cpi->lst_fb_idx = cpi->svc.ext_lst_fb_idx[sl];
635 cpi->gld_fb_idx = cpi->svc.ext_gld_fb_idx[sl];
636 cpi->alt_fb_idx = cpi->svc.ext_alt_fb_idx[sl];
640 if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode)
641 cpi->svc.rc_drop_superframe = 0;
643 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
644 cpi->svc.number_temporal_layers +
645 cpi->svc.temporal_layer_id];
647 // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
648 // only for non-BYPASS mode for now.
649 if (cpi->svc.temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
650 RATE_CONTROL *const lrc = &lc->rc;
651 lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
652 lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
655 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
656 lc->scaling_factor_num, lc->scaling_factor_den, &width,
659 // For resolutions <= QVGA: set phase of the filter = 8 (for symmetric
660 // averaging filter), use bilinear for now.
661 if (width * height <= 320 * 240) {
662 cpi->svc.downsample_filter_type[cpi->svc.spatial_layer_id] = BILINEAR;
663 cpi->svc.downsample_filter_phase[cpi->svc.spatial_layer_id] = 8;
666 // The usage of use_base_mv assumes down-scale of 2x2. For now, turn off use
667 // of base motion vectors if spatial scale factors for any layers are not 2,
668 // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
669 // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
670 if (cpi->svc.number_spatial_layers > 1) {
672 for (sl = 0; sl < cpi->svc.number_spatial_layers - 1; ++sl) {
673 lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers +
674 cpi->svc.temporal_layer_id];
675 if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
676 !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
677 cpi->svc.number_spatial_layers == 3)) {
678 cpi->svc.use_base_mv = 0;
684 cpi->svc.non_reference_frame = 0;
685 if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
686 !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame) {
687 cpi->svc.non_reference_frame = 1;
690 if (vp9_set_size_literal(cpi, width, height) != 0)
691 return VPX_CODEC_INVALID_PARAM;
696 #if CONFIG_SPATIAL_SVC
697 #define SMALL_FRAME_FB_IDX 7
699 int vp9_svc_start_frame(VP9_COMP *const cpi) {
700 int width = 0, height = 0;
702 struct lookahead_entry *buf;
703 int count = 1 << (cpi->svc.number_temporal_layers - 1);
705 cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
706 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
708 cpi->svc.temporal_layer_id = 0;
709 while ((lc->current_video_frame_in_layer % count) != 0) {
710 ++cpi->svc.temporal_layer_id;
714 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
716 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
718 if (cpi->svc.spatial_layer_id == 0)
720 (lc->gold_ref_idx >= 0) ? lc->gold_ref_idx : cpi->lst_fb_idx;
722 cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
724 if (lc->current_video_frame_in_layer == 0) {
725 if (cpi->svc.spatial_layer_id >= 2) {
726 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
728 cpi->alt_fb_idx = cpi->lst_fb_idx;
729 cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
732 if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
733 cpi->alt_fb_idx = lc->alt_ref_idx;
734 if (!lc->has_alt_frame) cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
736 // Find a proper alt_fb_idx for layers that don't have alt ref frame
737 if (cpi->svc.spatial_layer_id == 0) {
738 cpi->alt_fb_idx = cpi->lst_fb_idx;
740 LAYER_CONTEXT *lc_lower =
741 &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
743 if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
744 lc_lower->alt_ref_source != NULL)
745 cpi->alt_fb_idx = lc_lower->alt_ref_idx;
746 else if (cpi->svc.spatial_layer_id >= 2)
747 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
749 cpi->alt_fb_idx = cpi->lst_fb_idx;
754 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
755 lc->scaling_factor_num, lc->scaling_factor_den, &width,
758 // Workaround for multiple frame contexts. In some frames we can't use prev_mi
759 // since its previous frame could be changed during decoding time. The idea is
760 // we put a empty invisible frame in front of them, then we will not use
761 // prev_mi when encoding these frames.
763 buf = vp9_lookahead_peek(cpi->lookahead, 0);
764 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
765 cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
766 lc->rc.frames_to_key != 0 &&
767 !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
768 if ((cpi->svc.number_temporal_layers > 1 &&
769 cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
770 (cpi->svc.number_spatial_layers > 1 &&
771 cpi->svc.spatial_layer_id == 0)) {
772 struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
775 cpi->svc.empty_frame.ts_start = buf->ts_start;
776 cpi->svc.empty_frame.ts_end = buf->ts_end;
777 cpi->svc.encode_empty_frame_state = ENCODING;
778 cpi->common.show_frame = 0;
779 cpi->ref_frame_flags = 0;
780 cpi->common.frame_type = INTER_FRAME;
781 cpi->lst_fb_idx = cpi->gld_fb_idx = cpi->alt_fb_idx =
784 if (cpi->svc.encode_intra_empty_frame != 0) cpi->common.intra_only = 1;
786 width = SMALL_FRAME_WIDTH;
787 height = SMALL_FRAME_HEIGHT;
792 cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
793 cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
795 vp9_change_config(cpi, &cpi->oxcf);
797 if (vp9_set_size_literal(cpi, width, height) != 0)
798 return VPX_CODEC_INVALID_PARAM;
800 vp9_set_high_precision_mv(cpi, 1);
802 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
807 #undef SMALL_FRAME_FB_IDX
808 #endif // CONFIG_SPATIAL_SVC
810 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
811 struct lookahead_ctx *ctx,
813 struct lookahead_entry *buf = NULL;
814 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
815 buf = vp9_lookahead_peek(ctx, 0);
817 // Only remove the buffer when pop the highest layer.
818 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
819 vp9_lookahead_pop(ctx, drain);
826 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
828 SVC *const svc = &cpi->svc;
829 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
830 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
831 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
832 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
833 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
834 if (lc->map) vpx_free(lc->map);
835 if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
836 if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
841 // Reset on key frame: reset counters, references and buffer updates.
842 void vp9_svc_reset_key_frame(VP9_COMP *const cpi) {
844 SVC *const svc = &cpi->svc;
845 LAYER_CONTEXT *lc = NULL;
846 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
847 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
848 lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
849 lc->current_video_frame_in_layer = 0;
850 lc->frames_from_key_frame = 0;
853 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
854 set_flags_and_fb_idx_for_temporal_mode3(cpi);
855 } else if (svc->temporal_layering_mode ==
856 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
857 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
858 } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
859 set_flags_and_fb_idx_for_temporal_mode2(cpi);
861 vp9_update_temporal_layer_framerate(cpi);
862 vp9_restore_layer_context(cpi);