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