1 /*
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include <math.h>
12
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"
18
19 #define SMALL_FRAME_WIDTH 32
20 #define SMALL_FRAME_HEIGHT 16
21
swap_ptr(void * a,void * b)22 static void swap_ptr(void *a, void *b) {
23 void **a_p = (void **)a;
24 void **b_p = (void **)b;
25 void *c = *a_p;
26 *a_p = *b_p;
27 *b_p = c;
28 }
29
vp9_init_layer_context(VP9_COMP * const cpi)30 void vp9_init_layer_context(VP9_COMP *const cpi) {
31 SVC *const svc = &cpi->svc;
32 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
33 int mi_rows = cpi->common.mi_rows;
34 int mi_cols = cpi->common.mi_cols;
35 int sl, tl, i;
36 int alt_ref_idx = svc->number_spatial_layers;
37
38 svc->spatial_layer_id = 0;
39 svc->temporal_layer_id = 0;
40 svc->force_zero_mode_spatial_ref = 0;
41 svc->use_base_mv = 0;
42 svc->use_partition_reuse = 0;
43 svc->use_gf_temporal_ref = 1;
44 svc->use_gf_temporal_ref_current_layer = 0;
45 svc->scaled_temp_is_alloc = 0;
46 svc->scaled_one_half = 0;
47 svc->current_superframe = 0;
48 svc->non_reference_frame = 0;
49 svc->skip_enhancement_layer = 0;
50 svc->disable_inter_layer_pred = INTER_LAYER_PRED_ON;
51 svc->framedrop_mode = CONSTRAINED_LAYER_DROP;
52 svc->set_intra_only_frame = 0;
53 svc->previous_frame_is_intra_only = 0;
54 svc->superframe_has_layer_sync = 0;
55 svc->use_set_ref_frame_config = 0;
56 svc->num_encoded_top_layer = 0;
57 svc->simulcast_mode = 0;
58
59 for (i = 0; i < REF_FRAMES; ++i) {
60 svc->fb_idx_spatial_layer_id[i] = 0xff;
61 svc->fb_idx_temporal_layer_id[i] = 0xff;
62 svc->fb_idx_base[i] = 0;
63 }
64 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
65 svc->last_layer_dropped[sl] = 0;
66 svc->drop_spatial_layer[sl] = 0;
67 svc->ext_frame_flags[sl] = 0;
68 svc->lst_fb_idx[sl] = 0;
69 svc->gld_fb_idx[sl] = 1;
70 svc->alt_fb_idx[sl] = 2;
71 svc->downsample_filter_type[sl] = BILINEAR;
72 svc->downsample_filter_phase[sl] = 8; // Set to 8 for averaging filter.
73 svc->framedrop_thresh[sl] = oxcf->drop_frames_water_mark;
74 svc->fb_idx_upd_tl0[sl] = -1;
75 svc->drop_count[sl] = 0;
76 svc->spatial_layer_sync[sl] = 0;
77 svc->force_drop_constrained_from_above[sl] = 0;
78 }
79 svc->max_consec_drop = INT_MAX;
80
81 svc->buffer_gf_temporal_ref[1].idx = 7;
82 svc->buffer_gf_temporal_ref[0].idx = 6;
83 svc->buffer_gf_temporal_ref[1].is_used = 0;
84 svc->buffer_gf_temporal_ref[0].is_used = 0;
85
86 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
87 if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
88 SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
89 cpi->common.subsampling_y,
90 #if CONFIG_VP9_HIGHBITDEPTH
91 cpi->common.use_highbitdepth,
92 #endif
93 VP9_ENC_BORDER_IN_PIXELS,
94 cpi->common.byte_alignment, NULL, NULL, NULL))
95 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
96 "Failed to allocate empty frame for multiple frame "
97 "contexts");
98
99 memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
100 cpi->svc.empty_frame.img.buffer_alloc_sz);
101 }
102
103 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
104 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
105 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
106 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
107 RATE_CONTROL *const lrc = &lc->rc;
108 int i;
109 lc->current_video_frame_in_layer = 0;
110 lc->layer_size = 0;
111 lc->frames_from_key_frame = 0;
112 lc->last_frame_type = FRAME_TYPES;
113 lrc->ni_av_qi = oxcf->worst_allowed_q;
114 lrc->total_actual_bits = 0;
115 lrc->total_target_vs_actual = 0;
116 lrc->ni_tot_qi = 0;
117 lrc->tot_q = 0.0;
118 lrc->avg_q = 0.0;
119 lrc->ni_frames = 0;
120 lrc->decimation_count = 0;
121 lrc->decimation_factor = 0;
122 lrc->worst_quality = oxcf->worst_allowed_q;
123 lrc->best_quality = oxcf->best_allowed_q;
124
125 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
126 lrc->rate_correction_factors[i] = 1.0;
127 }
128
129 if (cpi->oxcf.rc_mode == VPX_CBR) {
130 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
131 lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
132 lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
133 lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
134 } else {
135 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
136 lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
137 lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
138 lrc->avg_frame_qindex[KEY_FRAME] =
139 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
140 lrc->avg_frame_qindex[INTER_FRAME] =
141 (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
142 if (oxcf->ss_enable_auto_arf[sl])
143 lc->alt_ref_idx = alt_ref_idx++;
144 else
145 lc->alt_ref_idx = INVALID_IDX;
146 lc->gold_ref_idx = INVALID_IDX;
147 }
148
149 lrc->buffer_level =
150 oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
151 lrc->bits_off_target = lrc->buffer_level;
152
153 // Initialize the cyclic refresh parameters. If spatial layers are used
154 // (i.e., ss_number_layers > 1), these need to be updated per spatial
155 // layer.
156 // Cyclic refresh is only applied on base temporal layer.
157 if (oxcf->ss_number_layers > 1 && tl == 0) {
158 size_t last_coded_q_map_size;
159 size_t consec_zero_mv_size;
160 VP9_COMMON *const cm = &cpi->common;
161 lc->sb_index = 0;
162 lc->actual_num_seg1_blocks = 0;
163 lc->actual_num_seg2_blocks = 0;
164 lc->counter_encode_maxq_scene_change = 0;
165 CHECK_MEM_ERROR(cm, lc->map,
166 vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
167 memset(lc->map, 0, mi_rows * mi_cols);
168 last_coded_q_map_size =
169 mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
170 CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
171 vpx_malloc(last_coded_q_map_size));
172 assert(MAXQ <= 255);
173 memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
174 consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
175 CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
176 vpx_malloc(consec_zero_mv_size));
177 memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
178 }
179 }
180 }
181
182 // Still have extra buffer for base layer golden frame
183 if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
184 alt_ref_idx < REF_FRAMES)
185 svc->layer_context[0].gold_ref_idx = alt_ref_idx;
186 }
187
188 // Update the layer context from a change_config() call.
vp9_update_layer_context_change_config(VP9_COMP * const cpi,const int target_bandwidth)189 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
190 const int target_bandwidth) {
191 SVC *const svc = &cpi->svc;
192 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
193 const RATE_CONTROL *const rc = &cpi->rc;
194 int sl, tl, layer = 0, spatial_layer_target;
195 float bitrate_alloc = 1.0;
196
197 cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
198
199 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
200 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
201 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
202 layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
203 svc->layer_context[layer].target_bandwidth =
204 oxcf->layer_target_bitrate[layer];
205 }
206
207 layer = LAYER_IDS_TO_IDX(
208 sl,
209 ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
210 oxcf->ts_number_layers);
211 spatial_layer_target = svc->layer_context[layer].target_bandwidth =
212 oxcf->layer_target_bitrate[layer];
213
214 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
215 LAYER_CONTEXT *const lc =
216 &svc->layer_context[sl * oxcf->ts_number_layers + tl];
217 RATE_CONTROL *const lrc = &lc->rc;
218
219 lc->spatial_layer_target_bandwidth = spatial_layer_target;
220 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
221 lrc->starting_buffer_level =
222 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
223 lrc->optimal_buffer_level =
224 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
225 lrc->maximum_buffer_size =
226 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
227 lrc->bits_off_target =
228 VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
229 lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
230 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
231 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
232 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
233 lrc->worst_quality = rc->worst_quality;
234 lrc->best_quality = rc->best_quality;
235 }
236 }
237 } else {
238 int layer_end;
239
240 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
241 layer_end = svc->number_temporal_layers;
242 } else {
243 layer_end = svc->number_spatial_layers;
244 }
245
246 for (layer = 0; layer < layer_end; ++layer) {
247 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
248 RATE_CONTROL *const lrc = &lc->rc;
249
250 lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
251
252 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
253 // Update buffer-related quantities.
254 lrc->starting_buffer_level =
255 (int64_t)(rc->starting_buffer_level * bitrate_alloc);
256 lrc->optimal_buffer_level =
257 (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
258 lrc->maximum_buffer_size =
259 (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
260 lrc->bits_off_target =
261 VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
262 lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
263 // Update framerate-related quantities.
264 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
265 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
266 } else {
267 lc->framerate = cpi->framerate;
268 }
269 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
270 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
271 // Update qp-related quantities.
272 lrc->worst_quality = rc->worst_quality;
273 lrc->best_quality = rc->best_quality;
274 }
275 }
276 }
277
get_layer_context(VP9_COMP * const cpi)278 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
279 if (is_one_pass_cbr_svc(cpi))
280 return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
281 cpi->svc.number_temporal_layers +
282 cpi->svc.temporal_layer_id];
283 else
284 return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
285 ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
286 : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
287 }
288
vp9_update_temporal_layer_framerate(VP9_COMP * const cpi)289 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
290 SVC *const svc = &cpi->svc;
291 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
292 LAYER_CONTEXT *const lc = get_layer_context(cpi);
293 RATE_CONTROL *const lrc = &lc->rc;
294 // Index into spatial+temporal arrays.
295 const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
296 svc->temporal_layer_id;
297 const int tl = svc->temporal_layer_id;
298
299 lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
300 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
301 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
302 // Update the average layer frame size (non-cumulative per-frame-bw).
303 if (tl == 0) {
304 lc->avg_frame_size = lrc->avg_frame_bandwidth;
305 } else {
306 const double prev_layer_framerate =
307 cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
308 const int prev_layer_target_bandwidth =
309 oxcf->layer_target_bitrate[st_idx - 1];
310 lc->avg_frame_size =
311 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
312 (lc->framerate - prev_layer_framerate));
313 }
314 }
315
vp9_update_spatial_layer_framerate(VP9_COMP * const cpi,double framerate)316 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
317 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
318 LAYER_CONTEXT *const lc = get_layer_context(cpi);
319 RATE_CONTROL *const lrc = &lc->rc;
320
321 lc->framerate = framerate;
322 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
323 lrc->min_frame_bandwidth =
324 (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
325 lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
326 oxcf->two_pass_vbrmax_section) /
327 100);
328 vp9_rc_set_gf_interval_range(cpi, lrc);
329 }
330
vp9_restore_layer_context(VP9_COMP * const cpi)331 void vp9_restore_layer_context(VP9_COMP *const cpi) {
332 LAYER_CONTEXT *const lc = get_layer_context(cpi);
333 const int old_frame_since_key = cpi->rc.frames_since_key;
334 const int old_frame_to_key = cpi->rc.frames_to_key;
335 const int old_ext_use_post_encode_drop = cpi->rc.ext_use_post_encode_drop;
336
337 cpi->rc = lc->rc;
338 cpi->twopass = lc->twopass;
339 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
340 cpi->alt_ref_source = lc->alt_ref_source;
341 // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
342 // does not use speed = 0).
343 if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
344 cpi->oxcf.speed = lc->speed;
345 }
346 // Reset the frames_since_key and frames_to_key counters to their values
347 // before the layer restore. Keep these defined for the stream (not layer).
348 if (cpi->svc.number_temporal_layers > 1 ||
349 cpi->svc.number_spatial_layers > 1) {
350 cpi->rc.frames_since_key = old_frame_since_key;
351 cpi->rc.frames_to_key = old_frame_to_key;
352 }
353 cpi->rc.ext_use_post_encode_drop = old_ext_use_post_encode_drop;
354 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
355 // for the base temporal layer.
356 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
357 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
358 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
359 swap_ptr(&cr->map, &lc->map);
360 swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
361 swap_ptr(&cpi->consec_zero_mv, &lc->consec_zero_mv);
362 cr->sb_index = lc->sb_index;
363 cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
364 cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
365 cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
366 }
367 }
368
vp9_save_layer_context(VP9_COMP * const cpi)369 void vp9_save_layer_context(VP9_COMP *const cpi) {
370 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
371 LAYER_CONTEXT *const lc = get_layer_context(cpi);
372
373 lc->rc = cpi->rc;
374 lc->twopass = cpi->twopass;
375 lc->target_bandwidth = (int)oxcf->target_bandwidth;
376 lc->alt_ref_source = cpi->alt_ref_source;
377
378 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
379 // for the base temporal layer.
380 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
381 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
382 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
383 signed char *temp = lc->map;
384 uint8_t *temp2 = lc->last_coded_q_map;
385 uint8_t *temp3 = lc->consec_zero_mv;
386 lc->map = cr->map;
387 cr->map = temp;
388 lc->last_coded_q_map = cr->last_coded_q_map;
389 cr->last_coded_q_map = temp2;
390 lc->consec_zero_mv = cpi->consec_zero_mv;
391 cpi->consec_zero_mv = temp3;
392 lc->sb_index = cr->sb_index;
393 lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
394 lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
395 lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
396 }
397 }
398
399 #if !CONFIG_REALTIME_ONLY
vp9_init_second_pass_spatial_svc(VP9_COMP * cpi)400 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
401 SVC *const svc = &cpi->svc;
402 int i;
403
404 for (i = 0; i < svc->number_spatial_layers; ++i) {
405 TWO_PASS *const twopass = &svc->layer_context[i].twopass;
406
407 svc->spatial_layer_id = i;
408 vp9_init_second_pass(cpi);
409
410 twopass->total_stats.spatial_layer_id = i;
411 twopass->total_left_stats.spatial_layer_id = i;
412 }
413 svc->spatial_layer_id = 0;
414 }
415 #endif // !CONFIG_REALTIME_ONLY
416
vp9_inc_frame_in_layer(VP9_COMP * const cpi)417 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
418 LAYER_CONTEXT *const lc =
419 &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
420 cpi->svc.number_temporal_layers];
421 ++lc->current_video_frame_in_layer;
422 ++lc->frames_from_key_frame;
423 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
424 ++cpi->svc.current_superframe;
425 }
426
get_layer_resolution(const int width_org,const int height_org,const int num,const int den,int * width_out,int * height_out)427 void get_layer_resolution(const int width_org, const int height_org,
428 const int num, const int den, int *width_out,
429 int *height_out) {
430 int w, h;
431
432 if (width_out == NULL || height_out == NULL || den == 0) return;
433
434 w = width_org * num / den;
435 h = height_org * num / den;
436
437 // make height and width even to make chrome player happy
438 w += w % 2;
439 h += h % 2;
440
441 *width_out = w;
442 *height_out = h;
443 }
444
reset_fb_idx_unused(VP9_COMP * const cpi)445 static void reset_fb_idx_unused(VP9_COMP *const cpi) {
446 // If a reference frame is not referenced or refreshed, then set the
447 // fb_idx for that reference to the first one used/referenced.
448 // This is to avoid setting fb_idx for a reference to a slot that is not
449 // used/needed (i.e., since that reference is not referenced or refreshed).
450 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
451 VP9_ALT_FLAG };
452 MV_REFERENCE_FRAME ref_frame;
453 MV_REFERENCE_FRAME first_ref = 0;
454 int first_fb_idx = 0;
455 int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
456 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
457 if (cpi->ref_frame_flags & flag_list[ref_frame]) {
458 first_ref = ref_frame;
459 first_fb_idx = fb_idx[ref_frame - 1];
460 break;
461 }
462 }
463 if (first_ref > 0) {
464 if (first_ref != LAST_FRAME &&
465 !(cpi->ref_frame_flags & flag_list[LAST_FRAME]) &&
466 !cpi->ext_refresh_last_frame)
467 cpi->lst_fb_idx = first_fb_idx;
468 else if (first_ref != GOLDEN_FRAME &&
469 !(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) &&
470 !cpi->ext_refresh_golden_frame)
471 cpi->gld_fb_idx = first_fb_idx;
472 else if (first_ref != ALTREF_FRAME &&
473 !(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]) &&
474 !cpi->ext_refresh_alt_ref_frame)
475 cpi->alt_fb_idx = first_fb_idx;
476 }
477 }
478
479 // Never refresh any reference frame buffers on top temporal layers in
480 // simulcast mode, which has interlayer prediction disabled.
non_reference_frame_simulcast(VP9_COMP * const cpi)481 static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
482 if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
483 cpi->svc.temporal_layer_id > 0) {
484 cpi->ext_refresh_last_frame = 0;
485 cpi->ext_refresh_golden_frame = 0;
486 cpi->ext_refresh_alt_ref_frame = 0;
487 }
488 }
489
490 // The function sets proper ref_frame_flags, buffer indices, and buffer update
491 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
492 // scheme.
set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP * const cpi)493 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
494 int frame_num_within_temporal_struct = 0;
495 int spatial_id, temporal_id;
496 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
497 frame_num_within_temporal_struct =
498 cpi->svc
499 .layer_context[cpi->svc.spatial_layer_id *
500 cpi->svc.number_temporal_layers]
501 .current_video_frame_in_layer %
502 4;
503 temporal_id = cpi->svc.temporal_layer_id =
504 (frame_num_within_temporal_struct & 1)
505 ? 2
506 : (frame_num_within_temporal_struct >> 1);
507 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
508 cpi->ext_refresh_alt_ref_frame = 0;
509 if (!temporal_id) {
510 cpi->ext_refresh_frame_flags_pending = 1;
511 cpi->ext_refresh_last_frame = 1;
512 if (!spatial_id) {
513 cpi->ref_frame_flags = VP9_LAST_FLAG;
514 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
515 // base layer is a key frame.
516 cpi->ref_frame_flags = VP9_LAST_FLAG;
517 cpi->ext_refresh_last_frame = 0;
518 cpi->ext_refresh_golden_frame = 1;
519 } else {
520 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
521 }
522 } else if (temporal_id == 1) {
523 cpi->ext_refresh_frame_flags_pending = 1;
524 cpi->ext_refresh_alt_ref_frame = 1;
525 if (!spatial_id) {
526 cpi->ref_frame_flags = VP9_LAST_FLAG;
527 } else {
528 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
529 }
530 } else {
531 if (frame_num_within_temporal_struct == 1) {
532 // the first tl2 picture
533 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
534 cpi->ext_refresh_frame_flags_pending = 1;
535 if (!spatial_id)
536 cpi->ref_frame_flags = VP9_LAST_FLAG;
537 else
538 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
539 } else if (!spatial_id) {
540 cpi->ext_refresh_frame_flags_pending = 1;
541 cpi->ext_refresh_alt_ref_frame = 1;
542 cpi->ref_frame_flags = VP9_LAST_FLAG;
543 } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
544 cpi->ext_refresh_frame_flags_pending = 1;
545 cpi->ext_refresh_alt_ref_frame = 1;
546 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
547 }
548 } else {
549 // The second tl2 picture
550 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
551 cpi->ext_refresh_frame_flags_pending = 1;
552 if (!spatial_id)
553 cpi->ref_frame_flags = VP9_LAST_FLAG;
554 else
555 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
556 } else if (!spatial_id) {
557 cpi->ext_refresh_frame_flags_pending = 1;
558 cpi->ref_frame_flags = VP9_LAST_FLAG;
559 cpi->ext_refresh_alt_ref_frame = 1;
560 } else { // top layer
561 cpi->ext_refresh_frame_flags_pending = 1;
562 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
563 cpi->ext_refresh_alt_ref_frame = 1;
564 }
565 }
566 }
567 if (temporal_id == 0) {
568 cpi->lst_fb_idx = spatial_id;
569 if (spatial_id) {
570 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
571 cpi->lst_fb_idx = spatial_id - 1;
572 cpi->gld_fb_idx = spatial_id;
573 } else {
574 cpi->gld_fb_idx = spatial_id - 1;
575 }
576 } else {
577 cpi->gld_fb_idx = 0;
578 }
579 cpi->alt_fb_idx = 0;
580 } else if (temporal_id == 1) {
581 cpi->lst_fb_idx = spatial_id;
582 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
583 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
584 } else if (frame_num_within_temporal_struct == 1) {
585 cpi->lst_fb_idx = spatial_id;
586 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
587 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
588 } else {
589 cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
590 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
591 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
592 }
593
594 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
595
596 reset_fb_idx_unused(cpi);
597 }
598
599 // The function sets proper ref_frame_flags, buffer indices, and buffer update
600 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
601 // scheme.
set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP * const cpi)602 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
603 int spatial_id, temporal_id;
604 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
605 temporal_id = cpi->svc.temporal_layer_id =
606 cpi->svc
607 .layer_context[cpi->svc.spatial_layer_id *
608 cpi->svc.number_temporal_layers]
609 .current_video_frame_in_layer &
610 1;
611 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
612 cpi->ext_refresh_alt_ref_frame = 0;
613 if (!temporal_id) {
614 cpi->ext_refresh_frame_flags_pending = 1;
615 cpi->ext_refresh_last_frame = 1;
616 if (!spatial_id) {
617 cpi->ref_frame_flags = VP9_LAST_FLAG;
618 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
619 // base layer is a key frame.
620 cpi->ref_frame_flags = VP9_LAST_FLAG;
621 cpi->ext_refresh_last_frame = 0;
622 cpi->ext_refresh_golden_frame = 1;
623 } else {
624 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
625 }
626 } else if (temporal_id == 1) {
627 cpi->ext_refresh_frame_flags_pending = 1;
628 cpi->ext_refresh_alt_ref_frame = 1;
629 if (!spatial_id) {
630 cpi->ref_frame_flags = VP9_LAST_FLAG;
631 } else {
632 if (spatial_id == cpi->svc.number_spatial_layers - 1)
633 cpi->ext_refresh_alt_ref_frame = 0;
634 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
635 }
636 }
637
638 if (temporal_id == 0) {
639 cpi->lst_fb_idx = spatial_id;
640 if (spatial_id) {
641 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
642 cpi->lst_fb_idx = spatial_id - 1;
643 cpi->gld_fb_idx = spatial_id;
644 } else {
645 cpi->gld_fb_idx = spatial_id - 1;
646 }
647 } else {
648 cpi->gld_fb_idx = 0;
649 }
650 cpi->alt_fb_idx = 0;
651 } else if (temporal_id == 1) {
652 cpi->lst_fb_idx = spatial_id;
653 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
654 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
655 }
656
657 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
658
659 reset_fb_idx_unused(cpi);
660 }
661
662 // The function sets proper ref_frame_flags, buffer indices, and buffer update
663 // variables for temporal layering mode 0 - that has no temporal layering.
set_flags_and_fb_idx_for_temporal_mode_noLayering(VP9_COMP * const cpi)664 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
665 VP9_COMP *const cpi) {
666 int spatial_id;
667 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
668 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
669 cpi->ext_refresh_alt_ref_frame = 0;
670 cpi->ext_refresh_frame_flags_pending = 1;
671 cpi->ext_refresh_last_frame = 1;
672 if (!spatial_id) {
673 cpi->ref_frame_flags = VP9_LAST_FLAG;
674 } else if (cpi->svc.layer_context[0].is_key_frame) {
675 cpi->ref_frame_flags = VP9_LAST_FLAG;
676 cpi->ext_refresh_last_frame = 0;
677 cpi->ext_refresh_golden_frame = 1;
678 } else {
679 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
680 }
681 cpi->lst_fb_idx = spatial_id;
682 if (spatial_id) {
683 if (cpi->svc.layer_context[0].is_key_frame) {
684 cpi->lst_fb_idx = spatial_id - 1;
685 cpi->gld_fb_idx = spatial_id;
686 } else {
687 cpi->gld_fb_idx = spatial_id - 1;
688 }
689 } else {
690 cpi->gld_fb_idx = 0;
691 }
692
693 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
694
695 reset_fb_idx_unused(cpi);
696 }
697
set_flags_and_fb_idx_bypass_via_set_ref_frame_config(VP9_COMP * const cpi)698 static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
699 VP9_COMP *const cpi) {
700 SVC *const svc = &cpi->svc;
701 int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
702 cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
703 cpi->ext_refresh_frame_flags_pending = 1;
704 cpi->lst_fb_idx = svc->lst_fb_idx[sl];
705 cpi->gld_fb_idx = svc->gld_fb_idx[sl];
706 cpi->alt_fb_idx = svc->alt_fb_idx[sl];
707 cpi->ext_refresh_last_frame = 0;
708 cpi->ext_refresh_golden_frame = 0;
709 cpi->ext_refresh_alt_ref_frame = 0;
710 cpi->ref_frame_flags = 0;
711 if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
712 if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
713 if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
714 }
715
vp9_copy_flags_ref_update_idx(VP9_COMP * const cpi)716 void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
717 SVC *const svc = &cpi->svc;
718 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
719 VP9_ALT_FLAG };
720 int sl = svc->spatial_layer_id;
721 svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
722 svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
723 svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
724 // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
725 // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
726 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
727 int ref;
728 for (ref = 0; ref < REF_FRAMES; ++ref) {
729 svc->update_buffer_slot[sl] &= ~(1 << ref);
730 if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
731 (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
732 (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
733 svc->update_buffer_slot[sl] |= (1 << ref);
734 }
735 }
736
737 // TODO(jianj): Remove these 3, deprecated.
738 svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
739 svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
740 svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
741
742 svc->reference_last[sl] =
743 (uint8_t)(cpi->ref_frame_flags & flag_list[LAST_FRAME]);
744 svc->reference_golden[sl] =
745 (uint8_t)(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]);
746 svc->reference_altref[sl] =
747 (uint8_t)(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]);
748 }
749
vp9_one_pass_cbr_svc_start_layer(VP9_COMP * const cpi)750 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
751 int width = 0, height = 0;
752 SVC *const svc = &cpi->svc;
753 LAYER_CONTEXT *lc = NULL;
754 svc->skip_enhancement_layer = 0;
755
756 if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
757 svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
758 svc->number_temporal_layers <= 3 &&
759 !(svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
760 svc->use_set_ref_frame_config))
761 svc->simulcast_mode = 1;
762 else
763 svc->simulcast_mode = 0;
764
765 if (svc->number_spatial_layers > 1) {
766 svc->use_base_mv = 1;
767 svc->use_partition_reuse = 1;
768 }
769 svc->force_zero_mode_spatial_ref = 1;
770 svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
771 svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
772 svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
773
774 // For constrained_from_above drop mode: before encoding superframe (i.e.,
775 // at SL0 frame) check all spatial layers (starting from top) for possible
776 // drop, and if so, set a flag to force drop of that layer and all its lower
777 // layers.
778 if (svc->spatial_layer_to_encode == svc->first_spatial_layer_to_encode) {
779 int sl;
780 for (sl = 0; sl < svc->number_spatial_layers; sl++)
781 svc->force_drop_constrained_from_above[sl] = 0;
782 if (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP) {
783 for (sl = svc->number_spatial_layers - 1;
784 sl >= svc->first_spatial_layer_to_encode; sl--) {
785 int layer = sl * svc->number_temporal_layers + svc->temporal_layer_id;
786 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
787 cpi->rc = lc->rc;
788 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
789 if (vp9_test_drop(cpi)) {
790 int sl2;
791 // Set flag to force drop in encoding for this mode.
792 for (sl2 = sl; sl2 >= svc->first_spatial_layer_to_encode; sl2--)
793 svc->force_drop_constrained_from_above[sl2] = 1;
794 break;
795 }
796 }
797 }
798 }
799
800 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
801 set_flags_and_fb_idx_for_temporal_mode3(cpi);
802 } else if (svc->temporal_layering_mode ==
803 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
804 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
805 } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
806 set_flags_and_fb_idx_for_temporal_mode2(cpi);
807 } else if (svc->temporal_layering_mode ==
808 VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
809 svc->use_set_ref_frame_config) {
810 set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
811 }
812
813 if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
814 cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
815 cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
816 svc->buffer_gf_temporal_ref[0].is_used = 1;
817 if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
818 cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
819 cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
820 svc->buffer_gf_temporal_ref[1].is_used = 1;
821
822 // For the fixed (non-flexible/bypass) SVC mode:
823 // If long term temporal reference is enabled at the sequence level
824 // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
825 // we can use golden as a second temporal reference
826 // (since the spatial/inter-layer reference is disabled).
827 // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
828 // unused (slot 7 and 6 should be available for 3-3 layer system).
829 // For now usage of this second temporal reference will only be used for
830 // highest and next to highest spatial layer (i.e., top and middle layer for
831 // 3 spatial layers).
832 svc->use_gf_temporal_ref_current_layer = 0;
833 if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
834 !svc->buffer_gf_temporal_ref[1].is_used &&
835 svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
836 svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
837 svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
838 svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
839 // Enable the second (long-term) temporal reference at the frame-level.
840 svc->use_gf_temporal_ref_current_layer = 1;
841 }
842
843 // Check if current superframe has any layer sync, only check once on
844 // base layer.
845 if (svc->spatial_layer_id == 0) {
846 int sl = 0;
847 // Default is no sync.
848 svc->superframe_has_layer_sync = 0;
849 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
850 if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
851 }
852 }
853
854 // Reset the drop flags for all spatial layers, on the base layer.
855 if (svc->spatial_layer_id == 0) {
856 vp9_zero(svc->drop_spatial_layer);
857 // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
858 // causes an issue with frame dropping and temporal layers, when the frame
859 // flags are passed via the encode call (bypass mode). Issue is that we're
860 // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
861 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
862 memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
863 memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
864 memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
865 // These are set by API before the superframe is encoded and they are
866 // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
867 // mode.
868 vp9_zero(svc->update_buffer_slot);
869 vp9_zero(svc->reference_last);
870 vp9_zero(svc->reference_golden);
871 vp9_zero(svc->reference_altref);
872 // TODO(jianj): Remove these 3, deprecated.
873 vp9_zero(svc->update_last);
874 vp9_zero(svc->update_golden);
875 vp9_zero(svc->update_altref);
876 }
877 }
878
879 lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
880 svc->temporal_layer_id];
881
882 // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
883 // only for non-BYPASS mode for now.
884 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
885 svc->use_set_ref_frame_config) {
886 RATE_CONTROL *const lrc = &lc->rc;
887 lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
888 lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
889 }
890
891 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
892 lc->scaling_factor_num, lc->scaling_factor_den, &width,
893 &height);
894
895 // Use Eightap_smooth for low resolutions.
896 if (width * height <= 320 * 240)
897 svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
898 // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
899 // to source pixel).
900 lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
901 svc->temporal_layer_id];
902 if (lc->scaling_factor_num > (3 * lc->scaling_factor_den) >> 2)
903 svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
904
905 // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
906 // For now, turn off use of base motion vectors and partition reuse if the
907 // spatial scale factors for any layers are not 2,
908 // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
909 // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
910 if (svc->number_spatial_layers > 1) {
911 int sl;
912 for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
913 lc = &svc->layer_context[sl * svc->number_temporal_layers +
914 svc->temporal_layer_id];
915 if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
916 !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
917 svc->number_spatial_layers == 3)) {
918 svc->use_base_mv = 0;
919 svc->use_partition_reuse = 0;
920 break;
921 }
922 }
923 // For non-zero spatial layers: if the previous spatial layer was dropped
924 // disable the base_mv and partition_reuse features.
925 if (svc->spatial_layer_id > 0 &&
926 svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
927 svc->use_base_mv = 0;
928 svc->use_partition_reuse = 0;
929 }
930 }
931
932 svc->non_reference_frame = 0;
933 if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
934 !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
935 svc->non_reference_frame = 1;
936 // For non-flexible mode, where update_buffer_slot is used, need to check if
937 // all buffer slots are not refreshed.
938 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
939 if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
940 svc->non_reference_frame = 0;
941 }
942
943 if (svc->spatial_layer_id == 0) {
944 svc->high_source_sad_superframe = 0;
945 svc->high_num_blocks_with_motion = 0;
946 }
947
948 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
949 svc->last_layer_dropped[svc->spatial_layer_id] &&
950 svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
951 !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
952 // For fixed/non-flexible mode, if the previous frame (same spatial layer
953 // from previous superframe) was dropped, make sure the lst_fb_idx
954 // for this frame corresponds to the buffer index updated on (last) encoded
955 // TL0 frame (with same spatial layer).
956 cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
957 }
958
959 if (vp9_set_size_literal(cpi, width, height) != 0)
960 return VPX_CODEC_INVALID_PARAM;
961
962 return 0;
963 }
964
vp9_svc_lookahead_pop(VP9_COMP * const cpi,struct lookahead_ctx * ctx,int drain)965 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
966 struct lookahead_ctx *ctx,
967 int drain) {
968 struct lookahead_entry *buf = NULL;
969 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
970 buf = vp9_lookahead_peek(ctx, 0);
971 if (buf != NULL) {
972 // Only remove the buffer when pop the highest layer.
973 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
974 vp9_lookahead_pop(ctx, drain);
975 }
976 }
977 }
978 return buf;
979 }
980
vp9_free_svc_cyclic_refresh(VP9_COMP * const cpi)981 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
982 int sl, tl;
983 SVC *const svc = &cpi->svc;
984 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
985 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
986 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
987 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
988 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
989 if (lc->map) vpx_free(lc->map);
990 if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
991 if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
992 }
993 }
994 }
995
996 // Reset on key frame: reset counters, references and buffer updates.
vp9_svc_reset_temporal_layers(VP9_COMP * const cpi,int is_key)997 void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
998 int sl, tl;
999 SVC *const svc = &cpi->svc;
1000 LAYER_CONTEXT *lc = NULL;
1001 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1002 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1003 lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
1004 lc->current_video_frame_in_layer = 0;
1005 if (is_key) lc->frames_from_key_frame = 0;
1006 }
1007 }
1008 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
1009 set_flags_and_fb_idx_for_temporal_mode3(cpi);
1010 } else if (svc->temporal_layering_mode ==
1011 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
1012 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
1013 } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
1014 set_flags_and_fb_idx_for_temporal_mode2(cpi);
1015 }
1016 vp9_update_temporal_layer_framerate(cpi);
1017 vp9_restore_layer_context(cpi);
1018 }
1019
vp9_svc_check_reset_layer_rc_flag(VP9_COMP * const cpi)1020 void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
1021 SVC *svc = &cpi->svc;
1022 int sl, tl;
1023 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1024 // Check for reset based on avg_frame_bandwidth for spatial layer sl.
1025 int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
1026 svc->number_temporal_layers);
1027 LAYER_CONTEXT *lc = &svc->layer_context[layer];
1028 RATE_CONTROL *lrc = &lc->rc;
1029 if (lrc->avg_frame_bandwidth > (3 * lrc->last_avg_frame_bandwidth >> 1) ||
1030 lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
1031 // Reset for all temporal layers with spatial layer sl.
1032 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1033 int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
1034 LAYER_CONTEXT *lc = &svc->layer_context[layer];
1035 RATE_CONTROL *lrc = &lc->rc;
1036 lrc->rc_1_frame = 0;
1037 lrc->rc_2_frame = 0;
1038 lrc->bits_off_target = lrc->optimal_buffer_level;
1039 lrc->buffer_level = lrc->optimal_buffer_level;
1040 }
1041 }
1042 }
1043 }
1044
vp9_svc_constrain_inter_layer_pred(VP9_COMP * const cpi)1045 void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
1046 VP9_COMMON *const cm = &cpi->common;
1047 SVC *const svc = &cpi->svc;
1048 const int sl = svc->spatial_layer_id;
1049 // Check for disabling inter-layer (spatial) prediction, if
1050 // svc.disable_inter_layer_pred is set. If the previous spatial layer was
1051 // dropped then disable the prediction from this (scaled) reference.
1052 // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
1053 // on key frames or if any spatial layer is a sync layer.
1054 if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
1055 !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1056 !svc->superframe_has_layer_sync) ||
1057 svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
1058 svc->drop_spatial_layer[sl - 1]) {
1059 MV_REFERENCE_FRAME ref_frame;
1060 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1061 VP9_ALT_FLAG };
1062 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1063 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1064 if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
1065 const struct scale_factors *const scale_fac =
1066 &cm->frame_refs[ref_frame - 1].sf;
1067 if (vp9_is_scaled(scale_fac)) {
1068 cpi->ref_frame_flags &= (~flag_list[ref_frame]);
1069 // Point golden/altref frame buffer index to last.
1070 if (!svc->simulcast_mode) {
1071 if (ref_frame == GOLDEN_FRAME)
1072 cpi->gld_fb_idx = cpi->lst_fb_idx;
1073 else if (ref_frame == ALTREF_FRAME)
1074 cpi->alt_fb_idx = cpi->lst_fb_idx;
1075 }
1076 }
1077 }
1078 }
1079 }
1080 // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
1081 // If the reference for inter-layer prediction (the reference that is scaled)
1082 // is not the previous spatial layer from the same superframe, then we disable
1083 // inter-layer prediction. Only need to check when inter_layer prediction is
1084 // not set to OFF mode.
1085 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1086 svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
1087 // We only use LAST and GOLDEN for prediction in real-time mode, so we
1088 // check both here.
1089 MV_REFERENCE_FRAME ref_frame;
1090 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
1091 struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
1092 if (vp9_is_scaled(scale_fac)) {
1093 // If this reference was updated on the previous spatial layer of the
1094 // current superframe, then we keep this reference (don't disable).
1095 // Otherwise we disable the inter-layer prediction.
1096 // This condition is verified by checking if the current frame buffer
1097 // index is equal to any of the slots for the previous spatial layer,
1098 // and if so, check if that slot was updated/refreshed. If that is the
1099 // case, then this reference is valid for inter-layer prediction under
1100 // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
1101 int fb_idx =
1102 ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
1103 int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
1104 int disable = 1;
1105 if (fb_idx < 0) continue;
1106 if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
1107 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1108 (fb_idx == svc->gld_fb_idx[sl - 1] &&
1109 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1110 (fb_idx == svc->alt_fb_idx[sl - 1] &&
1111 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
1112 disable = 0;
1113 if (disable) cpi->ref_frame_flags &= (~ref_flag);
1114 }
1115 }
1116 }
1117 }
1118
vp9_svc_assert_constraints_pattern(VP9_COMP * const cpi)1119 void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
1120 SVC *const svc = &cpi->svc;
1121 // For fixed/non-flexible mode, the following constraint are expected,
1122 // when inter-layer prediciton is on (default).
1123 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1124 svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
1125 svc->framedrop_mode != LAYER_DROP) {
1126 if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1127 // On non-key frames: LAST is always temporal reference, GOLDEN is
1128 // spatial reference.
1129 if (svc->temporal_layer_id == 0)
1130 // Base temporal only predicts from base temporal.
1131 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
1132 else
1133 // Non-base temporal only predicts from lower temporal layer.
1134 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
1135 svc->temporal_layer_id);
1136 if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
1137 svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1138 // Non-base spatial only predicts from lower spatial layer with same
1139 // temporal_id.
1140 assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1141 svc->spatial_layer_id - 1);
1142 assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1143 svc->temporal_layer_id);
1144 }
1145 } else if (svc->spatial_layer_id > 0 &&
1146 svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1147 // Only 1 reference for frame whose base is key; reference may be LAST
1148 // or GOLDEN, so we check both.
1149 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1150 assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
1151 svc->spatial_layer_id - 1);
1152 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
1153 svc->temporal_layer_id);
1154 } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1155 assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1156 svc->spatial_layer_id - 1);
1157 assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1158 svc->temporal_layer_id);
1159 }
1160 }
1161 } else if (svc->use_gf_temporal_ref_current_layer &&
1162 !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1163 // For the usage of golden as second long term reference: the
1164 // temporal_layer_id of that reference must be base temporal layer 0, and
1165 // spatial_layer_id of that reference must be same as current
1166 // spatial_layer_id. If not, disable feature.
1167 // TODO(marpan): Investigate when this can happen, and maybe put this check
1168 // and reset in a different place.
1169 if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
1170 svc->spatial_layer_id ||
1171 svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
1172 svc->use_gf_temporal_ref_current_layer = 0;
1173 }
1174 }
1175
1176 #if CONFIG_VP9_TEMPORAL_DENOISING
vp9_denoise_svc_non_key(VP9_COMP * const cpi)1177 int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
1178 int layer =
1179 LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1180 cpi->svc.number_temporal_layers);
1181 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1182 return denoise_svc(cpi) && !lc->is_key_frame;
1183 }
1184 #endif
1185
vp9_svc_check_spatial_layer_sync(VP9_COMP * const cpi)1186 void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
1187 SVC *const svc = &cpi->svc;
1188 // Only for superframes whose base is not key, as those are
1189 // already sync frames.
1190 if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1191 if (svc->spatial_layer_id == 0) {
1192 // On base spatial layer: if the current superframe has a layer sync then
1193 // reset the pattern counters and reset to base temporal layer.
1194 if (svc->superframe_has_layer_sync)
1195 vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
1196 }
1197 // If the layer sync is set for this current spatial layer then
1198 // disable the temporal reference.
1199 if (svc->spatial_layer_id > 0 &&
1200 svc->spatial_layer_sync[svc->spatial_layer_id]) {
1201 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1202 if (svc->use_gf_temporal_ref_current_layer) {
1203 int index = svc->spatial_layer_id;
1204 // If golden is used as second reference: need to remove it from
1205 // prediction, reset refresh period to 0, and update the reference.
1206 svc->use_gf_temporal_ref_current_layer = 0;
1207 cpi->rc.baseline_gf_interval = 0;
1208 cpi->rc.frames_till_gf_update_due = 0;
1209 // On layer sync frame we must update the buffer index used for long
1210 // term reference. Use the alt_ref since it is not used or updated on
1211 // sync frames.
1212 if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
1213 assert(index >= 0);
1214 cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
1215 cpi->ext_refresh_alt_ref_frame = 1;
1216 }
1217 }
1218 }
1219 }
1220
vp9_svc_update_ref_frame_buffer_idx(VP9_COMP * const cpi)1221 void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
1222 SVC *const svc = &cpi->svc;
1223 // Update the usage of frame buffer index for base spatial layers.
1224 if (svc->spatial_layer_id == 0) {
1225 if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
1226 svc->fb_idx_base[cpi->lst_fb_idx] = 1;
1227 if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
1228 svc->fb_idx_base[cpi->gld_fb_idx] = 1;
1229 if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
1230 svc->fb_idx_base[cpi->alt_fb_idx] = 1;
1231 }
1232 }
1233
vp9_svc_update_ref_frame_bypass_mode(VP9_COMP * const cpi)1234 static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
1235 // For non-flexible/bypass SVC mode: check for refreshing other buffer
1236 // slots.
1237 SVC *const svc = &cpi->svc;
1238 VP9_COMMON *const cm = &cpi->common;
1239 BufferPool *const pool = cm->buffer_pool;
1240 int i;
1241 for (i = 0; i < REF_FRAMES; i++) {
1242 if (cm->frame_type == KEY_FRAME ||
1243 svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
1244 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1245 svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1246 svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1247 }
1248 }
1249 }
1250
vp9_svc_update_ref_frame(VP9_COMP * const cpi)1251 void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
1252 VP9_COMMON *const cm = &cpi->common;
1253 SVC *const svc = &cpi->svc;
1254 BufferPool *const pool = cm->buffer_pool;
1255
1256 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1257 svc->use_set_ref_frame_config) {
1258 vp9_svc_update_ref_frame_bypass_mode(cpi);
1259 } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
1260 // Keep track of frame index for each reference frame.
1261 int i;
1262 // On key frame update all reference frame slots.
1263 for (i = 0; i < REF_FRAMES; i++) {
1264 svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1265 svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1266 // LAST/GOLDEN/ALTREF is already updated above.
1267 if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
1268 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1269 }
1270 } else {
1271 if (cpi->refresh_last_frame) {
1272 svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
1273 svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
1274 }
1275 if (cpi->refresh_golden_frame) {
1276 svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
1277 svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
1278 }
1279 if (cpi->refresh_alt_ref_frame) {
1280 svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
1281 svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
1282 }
1283 }
1284 // Copy flags from encoder to SVC struct.
1285 vp9_copy_flags_ref_update_idx(cpi);
1286 vp9_svc_update_ref_frame_buffer_idx(cpi);
1287 }
1288
vp9_svc_adjust_frame_rate(VP9_COMP * const cpi)1289 void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
1290 int64_t this_duration =
1291 cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
1292 vp9_new_framerate(cpi, 10000000.0 / this_duration);
1293 }
1294
vp9_svc_adjust_avg_frame_qindex(VP9_COMP * const cpi)1295 void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
1296 VP9_COMMON *const cm = &cpi->common;
1297 SVC *const svc = &cpi->svc;
1298 RATE_CONTROL *const rc = &cpi->rc;
1299 // On key frames in CBR mode: reset the avg_frame_index for base layer
1300 // (to level closer to worst_quality) if the overshoot is significant.
1301 // Reset it for all temporal layers on base spatial layer.
1302 if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
1303 !svc->simulcast_mode &&
1304 rc->projected_frame_size > 3 * rc->avg_frame_bandwidth) {
1305 int tl;
1306 rc->avg_frame_qindex[INTER_FRAME] =
1307 VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
1308 (cm->base_qindex + rc->worst_quality) >> 1);
1309 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1310 const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
1311 LAYER_CONTEXT *lc = &svc->layer_context[layer];
1312 RATE_CONTROL *lrc = &lc->rc;
1313 lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
1314 }
1315 }
1316 }
1317