• 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_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_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   MV_REFERENCE_FRAME ref_frame;
466   MV_REFERENCE_FRAME first_ref = 0;
467   int first_fb_idx = 0;
468   int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
469   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
470     if (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) {
471       first_ref = ref_frame;
472       first_fb_idx = fb_idx[ref_frame - 1];
473       break;
474     }
475   }
476   if (first_ref > 0) {
477     if (first_ref != LAST_FRAME && !(cpi->ref_frame_flags & VP9_LAST_FLAG) &&
478         !cpi->ext_refresh_last_frame)
479       cpi->lst_fb_idx = first_fb_idx;
480     else if (first_ref != GOLDEN_FRAME &&
481              !(cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
482              !cpi->ext_refresh_golden_frame)
483       cpi->gld_fb_idx = first_fb_idx;
484     else if (first_ref != ALTREF_FRAME &&
485              !(cpi->ref_frame_flags & VP9_ALT_FLAG) &&
486              !cpi->ext_refresh_alt_ref_frame)
487       cpi->alt_fb_idx = first_fb_idx;
488   }
489 }
490 
491 // Never refresh any reference frame buffers on top temporal layers in
492 // simulcast mode, which has interlayer prediction disabled.
non_reference_frame_simulcast(VP9_COMP * const cpi)493 static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
494   if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
495       cpi->svc.temporal_layer_id > 0) {
496     cpi->ext_refresh_last_frame = 0;
497     cpi->ext_refresh_golden_frame = 0;
498     cpi->ext_refresh_alt_ref_frame = 0;
499   }
500 }
501 
502 // The function sets proper ref_frame_flags, buffer indices, and buffer update
503 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
504 // scheme.
set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP * const cpi)505 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
506   int frame_num_within_temporal_struct = 0;
507   int spatial_id, temporal_id;
508   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
509   frame_num_within_temporal_struct =
510       cpi->svc
511           .layer_context[cpi->svc.spatial_layer_id *
512                          cpi->svc.number_temporal_layers]
513           .current_video_frame_in_layer %
514       4;
515   temporal_id = cpi->svc.temporal_layer_id =
516       (frame_num_within_temporal_struct & 1)
517           ? 2
518           : (frame_num_within_temporal_struct >> 1);
519   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
520       cpi->ext_refresh_alt_ref_frame = 0;
521   if (!temporal_id) {
522     cpi->ext_refresh_frame_flags_pending = 1;
523     cpi->ext_refresh_last_frame = 1;
524     if (!spatial_id) {
525       cpi->ref_frame_flags = VP9_LAST_FLAG;
526     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
527       // base layer is a key frame.
528       cpi->ref_frame_flags = VP9_LAST_FLAG;
529       cpi->ext_refresh_last_frame = 0;
530       cpi->ext_refresh_golden_frame = 1;
531     } else {
532       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
533     }
534   } else if (temporal_id == 1) {
535     cpi->ext_refresh_frame_flags_pending = 1;
536     cpi->ext_refresh_alt_ref_frame = 1;
537     if (!spatial_id) {
538       cpi->ref_frame_flags = VP9_LAST_FLAG;
539     } else {
540       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
541     }
542   } else {
543     if (frame_num_within_temporal_struct == 1) {
544       // the first tl2 picture
545       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
546         cpi->ext_refresh_frame_flags_pending = 1;
547         if (!spatial_id)
548           cpi->ref_frame_flags = VP9_LAST_FLAG;
549         else
550           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
551       } else if (!spatial_id) {
552         cpi->ext_refresh_frame_flags_pending = 1;
553         cpi->ext_refresh_alt_ref_frame = 1;
554         cpi->ref_frame_flags = VP9_LAST_FLAG;
555       } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
556         cpi->ext_refresh_frame_flags_pending = 1;
557         cpi->ext_refresh_alt_ref_frame = 1;
558         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
559       }
560     } else {
561       //  The second tl2 picture
562       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
563         cpi->ext_refresh_frame_flags_pending = 1;
564         if (!spatial_id)
565           cpi->ref_frame_flags = VP9_LAST_FLAG;
566         else
567           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
568       } else if (!spatial_id) {
569         cpi->ext_refresh_frame_flags_pending = 1;
570         cpi->ref_frame_flags = VP9_LAST_FLAG;
571         cpi->ext_refresh_alt_ref_frame = 1;
572       } else {  // top layer
573         cpi->ext_refresh_frame_flags_pending = 1;
574         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
575         cpi->ext_refresh_alt_ref_frame = 1;
576       }
577     }
578   }
579   if (temporal_id == 0) {
580     cpi->lst_fb_idx = spatial_id;
581     if (spatial_id) {
582       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
583         cpi->lst_fb_idx = spatial_id - 1;
584         cpi->gld_fb_idx = spatial_id;
585       } else {
586         cpi->gld_fb_idx = spatial_id - 1;
587       }
588     } else {
589       cpi->gld_fb_idx = 0;
590     }
591     cpi->alt_fb_idx = 0;
592   } else if (temporal_id == 1) {
593     cpi->lst_fb_idx = spatial_id;
594     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
595     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
596   } else if (frame_num_within_temporal_struct == 1) {
597     cpi->lst_fb_idx = spatial_id;
598     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
599     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
600   } else {
601     cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
602     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
603     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
604   }
605 
606   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
607 
608   reset_fb_idx_unused(cpi);
609 }
610 
611 // The function sets proper ref_frame_flags, buffer indices, and buffer update
612 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
613 // scheme.
set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP * const cpi)614 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
615   int spatial_id, temporal_id;
616   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
617   temporal_id = cpi->svc.temporal_layer_id =
618       cpi->svc
619           .layer_context[cpi->svc.spatial_layer_id *
620                          cpi->svc.number_temporal_layers]
621           .current_video_frame_in_layer &
622       1;
623   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
624       cpi->ext_refresh_alt_ref_frame = 0;
625   if (!temporal_id) {
626     cpi->ext_refresh_frame_flags_pending = 1;
627     cpi->ext_refresh_last_frame = 1;
628     if (!spatial_id) {
629       cpi->ref_frame_flags = VP9_LAST_FLAG;
630     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
631       // base layer is a key frame.
632       cpi->ref_frame_flags = VP9_LAST_FLAG;
633       cpi->ext_refresh_last_frame = 0;
634       cpi->ext_refresh_golden_frame = 1;
635     } else {
636       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
637     }
638   } else if (temporal_id == 1) {
639     cpi->ext_refresh_frame_flags_pending = 1;
640     cpi->ext_refresh_alt_ref_frame = 1;
641     if (!spatial_id) {
642       cpi->ref_frame_flags = VP9_LAST_FLAG;
643     } else {
644       if (spatial_id == cpi->svc.number_spatial_layers - 1)
645         cpi->ext_refresh_alt_ref_frame = 0;
646       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
647     }
648   }
649 
650   if (temporal_id == 0) {
651     cpi->lst_fb_idx = spatial_id;
652     if (spatial_id) {
653       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
654         cpi->lst_fb_idx = spatial_id - 1;
655         cpi->gld_fb_idx = spatial_id;
656       } else {
657         cpi->gld_fb_idx = spatial_id - 1;
658       }
659     } else {
660       cpi->gld_fb_idx = 0;
661     }
662     cpi->alt_fb_idx = 0;
663   } else if (temporal_id == 1) {
664     cpi->lst_fb_idx = spatial_id;
665     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
666     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
667   }
668 
669   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
670 
671   reset_fb_idx_unused(cpi);
672 }
673 
674 // The function sets proper ref_frame_flags, buffer indices, and buffer update
675 // variables for temporal layering mode 0 - that has no temporal layering.
set_flags_and_fb_idx_for_temporal_mode_noLayering(VP9_COMP * const cpi)676 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
677     VP9_COMP *const cpi) {
678   int spatial_id;
679   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
680   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
681       cpi->ext_refresh_alt_ref_frame = 0;
682   cpi->ext_refresh_frame_flags_pending = 1;
683   cpi->ext_refresh_last_frame = 1;
684   if (!spatial_id) {
685     cpi->ref_frame_flags = VP9_LAST_FLAG;
686   } else if (cpi->svc.layer_context[0].is_key_frame) {
687     cpi->ref_frame_flags = VP9_LAST_FLAG;
688     cpi->ext_refresh_last_frame = 0;
689     cpi->ext_refresh_golden_frame = 1;
690   } else {
691     cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
692   }
693   cpi->lst_fb_idx = spatial_id;
694   if (spatial_id) {
695     if (cpi->svc.layer_context[0].is_key_frame) {
696       cpi->lst_fb_idx = spatial_id - 1;
697       cpi->gld_fb_idx = spatial_id;
698     } else {
699       cpi->gld_fb_idx = spatial_id - 1;
700     }
701   } else {
702     cpi->gld_fb_idx = 0;
703   }
704 
705   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
706 
707   reset_fb_idx_unused(cpi);
708 }
709 
set_flags_and_fb_idx_bypass_via_set_ref_frame_config(VP9_COMP * const cpi)710 static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
711     VP9_COMP *const cpi) {
712   SVC *const svc = &cpi->svc;
713   int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
714   cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
715   cpi->ext_refresh_frame_flags_pending = 1;
716   cpi->lst_fb_idx = svc->lst_fb_idx[sl];
717   cpi->gld_fb_idx = svc->gld_fb_idx[sl];
718   cpi->alt_fb_idx = svc->alt_fb_idx[sl];
719   cpi->ext_refresh_last_frame = 0;
720   cpi->ext_refresh_golden_frame = 0;
721   cpi->ext_refresh_alt_ref_frame = 0;
722   cpi->ref_frame_flags = 0;
723   if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
724   if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
725   if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
726 }
727 
vp9_copy_flags_ref_update_idx(VP9_COMP * const cpi)728 void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
729   SVC *const svc = &cpi->svc;
730   int sl = svc->spatial_layer_id;
731   svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
732   svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
733   svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
734   // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
735   // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
736   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
737     int ref;
738     for (ref = 0; ref < REF_FRAMES; ++ref) {
739       svc->update_buffer_slot[sl] &= ~(1 << ref);
740       if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
741           (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
742           (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
743         svc->update_buffer_slot[sl] |= (1 << ref);
744     }
745   }
746 
747   // TODO(jianj): Remove these 3, deprecated.
748   svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
749   svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
750   svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
751 
752   svc->reference_last[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_LAST_FLAG);
753   svc->reference_golden[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_GOLD_FLAG);
754   svc->reference_altref[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_ALT_FLAG);
755 }
756 
vp9_one_pass_svc_start_layer(VP9_COMP * const cpi)757 int vp9_one_pass_svc_start_layer(VP9_COMP *const cpi) {
758   int width = 0, height = 0;
759   SVC *const svc = &cpi->svc;
760   LAYER_CONTEXT *lc = NULL;
761   int scaling_factor_num = 1;
762   int scaling_factor_den = 1;
763   svc->skip_enhancement_layer = 0;
764 
765   if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
766       svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
767       svc->number_temporal_layers <= 3)
768     svc->simulcast_mode = 1;
769   else
770     svc->simulcast_mode = 0;
771 
772   if (svc->number_spatial_layers > 1) {
773     svc->use_base_mv = 1;
774     svc->use_partition_reuse = 1;
775   }
776   svc->force_zero_mode_spatial_ref = 1;
777   svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
778   svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
779   svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
780 
781   // For constrained_from_above drop mode: before encoding superframe (i.e.,
782   // at SL0 frame) check all spatial layers (starting from top) for possible
783   // drop, and if so, set a flag to force drop of that layer and all its lower
784   // layers.
785   if (svc->spatial_layer_to_encode == svc->first_spatial_layer_to_encode) {
786     int sl;
787     for (sl = 0; sl < svc->number_spatial_layers; sl++)
788       svc->force_drop_constrained_from_above[sl] = 0;
789     if (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP) {
790       for (sl = svc->number_spatial_layers - 1;
791            sl >= svc->first_spatial_layer_to_encode; sl--) {
792         int layer = sl * svc->number_temporal_layers + svc->temporal_layer_id;
793         LAYER_CONTEXT *const lc = &svc->layer_context[layer];
794         cpi->rc = lc->rc;
795         cpi->oxcf.target_bandwidth = lc->target_bandwidth;
796         if (vp9_test_drop(cpi)) {
797           int sl2;
798           // Set flag to force drop in encoding for this mode.
799           for (sl2 = sl; sl2 >= svc->first_spatial_layer_to_encode; sl2--)
800             svc->force_drop_constrained_from_above[sl2] = 1;
801           break;
802         }
803       }
804     }
805   }
806 
807   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
808     set_flags_and_fb_idx_for_temporal_mode3(cpi);
809   } else if (svc->temporal_layering_mode ==
810              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
811     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
812   } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
813     set_flags_and_fb_idx_for_temporal_mode2(cpi);
814   } else if (svc->temporal_layering_mode ==
815                  VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
816              svc->use_set_ref_frame_config) {
817     set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
818   }
819 
820   if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
821       cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
822       cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
823     svc->buffer_gf_temporal_ref[0].is_used = 1;
824   if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
825       cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
826       cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
827     svc->buffer_gf_temporal_ref[1].is_used = 1;
828 
829   // For the fixed (non-flexible/bypass) SVC mode:
830   // If long term temporal reference is enabled at the sequence level
831   // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
832   // we can use golden as a second temporal reference
833   // (since the spatial/inter-layer reference is disabled).
834   // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
835   // unused (slot 7 and 6 should be available for 3-3 layer system).
836   // For now usage of this second temporal reference will only be used for
837   // highest and next to highest spatial layer (i.e., top and middle layer for
838   // 3 spatial layers).
839   svc->use_gf_temporal_ref_current_layer = 0;
840   if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
841       !svc->buffer_gf_temporal_ref[1].is_used &&
842       svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
843       svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
844       svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
845       svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
846     // Enable the second (long-term) temporal reference at the frame-level.
847     svc->use_gf_temporal_ref_current_layer = 1;
848   }
849 
850   // Check if current superframe has any layer sync, only check once on
851   // base layer.
852   if (svc->spatial_layer_id == 0) {
853     int sl = 0;
854     // Default is no sync.
855     svc->superframe_has_layer_sync = 0;
856     for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
857       if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
858     }
859   }
860 
861   // Reset the drop flags for all spatial layers, on the
862   // first_spatial_layer_to_encode.
863   if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
864     vp9_zero(svc->drop_spatial_layer);
865     // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
866     // causes an issue with frame dropping and temporal layers, when the frame
867     // flags are passed via the encode call (bypass mode). Issue is that we're
868     // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
869     if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
870       memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
871       memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
872       memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
873       // These are set by API before the superframe is encoded and they are
874       // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
875       // mode.
876       vp9_zero(svc->update_buffer_slot);
877       vp9_zero(svc->reference_last);
878       vp9_zero(svc->reference_golden);
879       vp9_zero(svc->reference_altref);
880       // TODO(jianj): Remove these 3, deprecated.
881       vp9_zero(svc->update_last);
882       vp9_zero(svc->update_golden);
883       vp9_zero(svc->update_altref);
884     }
885   }
886 
887   lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
888                            svc->temporal_layer_id];
889 
890   // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
891   // only for non-BYPASS mode for now.
892   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
893       svc->use_set_ref_frame_config) {
894     RATE_CONTROL *const lrc = &lc->rc;
895     lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
896     lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
897     if (cpi->fixed_qp_onepass) {
898       lrc->worst_quality = cpi->rc.worst_quality;
899       lrc->best_quality = cpi->rc.best_quality;
900     }
901   }
902 
903   if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 &&
904       svc->spatial_layer_id == svc->first_spatial_layer_to_encode &&
905       cpi->resize_state != ORIG) {
906     scaling_factor_num = lc->scaling_factor_num_resize;
907     scaling_factor_den = lc->scaling_factor_den_resize;
908   } else {
909     scaling_factor_num = lc->scaling_factor_num;
910     scaling_factor_den = lc->scaling_factor_den;
911   }
912 
913   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, scaling_factor_num,
914                        scaling_factor_den, &width, &height);
915 
916   // Use Eightap_smooth for low resolutions.
917   if (width * height <= 320 * 240)
918     svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
919   // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
920   // to source pixel).
921   if (scaling_factor_num > (3 * scaling_factor_den) >> 2)
922     svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
923 
924   // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
925   // For now, turn off use of base motion vectors and partition reuse if the
926   // spatial scale factors for any layers are not 2,
927   // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
928   // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
929   if (svc->number_spatial_layers > 1) {
930     int sl;
931     for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
932       lc = &svc->layer_context[sl * svc->number_temporal_layers +
933                                svc->temporal_layer_id];
934       if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
935           !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
936             svc->number_spatial_layers == 3)) {
937         svc->use_base_mv = 0;
938         svc->use_partition_reuse = 0;
939         break;
940       }
941     }
942     // For non-zero spatial layers: if the previous spatial layer was dropped
943     // disable the base_mv and partition_reuse features.
944     if (svc->spatial_layer_id > 0 &&
945         svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
946       svc->use_base_mv = 0;
947       svc->use_partition_reuse = 0;
948     }
949   }
950 
951   svc->non_reference_frame = 0;
952   if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
953       !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
954     svc->non_reference_frame = 1;
955   // For flexible mode, where update_buffer_slot is used, need to check if
956   // all buffer slots are not refreshed.
957   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
958     if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
959       svc->non_reference_frame = 0;
960   }
961 
962   if (svc->spatial_layer_id == 0) {
963     svc->high_source_sad_superframe = 0;
964     svc->high_num_blocks_with_motion = 0;
965   }
966 
967   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
968       svc->last_layer_dropped[svc->spatial_layer_id] &&
969       svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
970       !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
971     // For fixed/non-flexible mode, if the previous frame (same spatial layer
972     // from previous superframe) was dropped, make sure the lst_fb_idx
973     // for this frame corresponds to the buffer index updated on (last) encoded
974     // TL0 frame (with same spatial layer).
975     cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
976   }
977 
978   if (vp9_set_size_literal(cpi, width, height) != 0)
979     return VPX_CODEC_INVALID_PARAM;
980 
981   return 0;
982 }
983 
vp9_svc_lookahead_pop(VP9_COMP * const cpi,struct lookahead_ctx * ctx,int drain)984 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
985                                               struct lookahead_ctx *ctx,
986                                               int drain) {
987   struct lookahead_entry *buf = NULL;
988   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
989     buf = vp9_lookahead_peek(ctx, 0);
990     if (buf != NULL) {
991       // Only remove the buffer when pop the highest layer.
992       if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
993         vp9_lookahead_pop(ctx, drain);
994       }
995     }
996   }
997   return buf;
998 }
999 
vp9_free_svc_cyclic_refresh(VP9_COMP * const cpi)1000 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
1001   int sl, tl;
1002   SVC *const svc = &cpi->svc;
1003   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1004   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
1005     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
1006       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
1007       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1008       if (lc->map) vpx_free(lc->map);
1009       if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
1010       if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
1011     }
1012   }
1013 }
1014 
1015 // Reset on key frame: reset counters, references and buffer updates.
vp9_svc_reset_temporal_layers(VP9_COMP * const cpi,int is_key)1016 void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
1017   int sl, tl;
1018   SVC *const svc = &cpi->svc;
1019   LAYER_CONTEXT *lc = NULL;
1020   for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1021     for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1022       lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
1023       lc->current_video_frame_in_layer = 0;
1024       if (is_key) lc->frames_from_key_frame = 0;
1025     }
1026   }
1027   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
1028     set_flags_and_fb_idx_for_temporal_mode3(cpi);
1029   } else if (svc->temporal_layering_mode ==
1030              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
1031     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
1032   } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
1033     set_flags_and_fb_idx_for_temporal_mode2(cpi);
1034   }
1035   vp9_update_temporal_layer_framerate(cpi);
1036   vp9_restore_layer_context(cpi);
1037 }
1038 
vp9_svc_check_reset_layer_rc_flag(VP9_COMP * const cpi)1039 void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
1040   SVC *svc = &cpi->svc;
1041   int sl, tl;
1042   for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1043     // Check for reset based on avg_frame_bandwidth for spatial layer sl.
1044     int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
1045                                  svc->number_temporal_layers);
1046     LAYER_CONTEXT *lc = &svc->layer_context[layer];
1047     RATE_CONTROL *lrc = &lc->rc;
1048     if (lrc->avg_frame_bandwidth > (3 * lrc->last_avg_frame_bandwidth >> 1) ||
1049         lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
1050       // Reset for all temporal layers with spatial layer sl.
1051       for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1052         int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
1053         LAYER_CONTEXT *lc = &svc->layer_context[layer];
1054         RATE_CONTROL *lrc = &lc->rc;
1055         lrc->rc_1_frame = 0;
1056         lrc->rc_2_frame = 0;
1057         lrc->bits_off_target = lrc->optimal_buffer_level;
1058         lrc->buffer_level = lrc->optimal_buffer_level;
1059       }
1060     }
1061   }
1062 }
1063 
vp9_svc_constrain_inter_layer_pred(VP9_COMP * const cpi)1064 void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
1065   VP9_COMMON *const cm = &cpi->common;
1066   SVC *const svc = &cpi->svc;
1067   const int sl = svc->spatial_layer_id;
1068   // Check for disabling inter-layer (spatial) prediction, if
1069   // svc.disable_inter_layer_pred is set. If the previous spatial layer was
1070   // dropped then disable the prediction from this (scaled) reference.
1071   // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
1072   // on key frames or if any spatial layer is a sync layer.
1073   if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
1074        !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1075        !svc->superframe_has_layer_sync) ||
1076       svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
1077       svc->drop_spatial_layer[sl - 1]) {
1078     MV_REFERENCE_FRAME ref_frame;
1079     for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1080       const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1081       if (yv12 != NULL &&
1082           (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) {
1083         const struct scale_factors *const scale_fac =
1084             &cm->frame_refs[ref_frame - 1].sf;
1085         if (vp9_is_scaled(scale_fac)) {
1086           cpi->ref_frame_flags &= (~ref_frame_to_flag(ref_frame));
1087           // Point golden/altref frame buffer index to last.
1088           if (!svc->simulcast_mode) {
1089             if (ref_frame == GOLDEN_FRAME)
1090               cpi->gld_fb_idx = cpi->lst_fb_idx;
1091             else if (ref_frame == ALTREF_FRAME)
1092               cpi->alt_fb_idx = cpi->lst_fb_idx;
1093           }
1094         }
1095       }
1096     }
1097   }
1098   // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
1099   // If the reference for inter-layer prediction (the reference that is scaled)
1100   // is not the previous spatial layer from the same superframe, then we disable
1101   // inter-layer prediction. Only need to check when inter_layer prediction is
1102   // not set to OFF mode.
1103   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1104       svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
1105     // We only use LAST and GOLDEN for prediction in real-time mode, so we
1106     // check both here.
1107     MV_REFERENCE_FRAME ref_frame;
1108     for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
1109       struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
1110       if (vp9_is_scaled(scale_fac)) {
1111         // If this reference  was updated on the previous spatial layer of the
1112         // current superframe, then we keep this reference (don't disable).
1113         // Otherwise we disable the inter-layer prediction.
1114         // This condition is verified by checking if the current frame buffer
1115         // index is equal to any of the slots for the previous spatial layer,
1116         // and if so, check if that slot was updated/refreshed. If that is the
1117         // case, then this reference is valid for inter-layer prediction under
1118         // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
1119         int fb_idx =
1120             ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
1121         int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
1122         int disable = 1;
1123         if (fb_idx < 0) continue;
1124         if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
1125              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1126             (fb_idx == svc->gld_fb_idx[sl - 1] &&
1127              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1128             (fb_idx == svc->alt_fb_idx[sl - 1] &&
1129              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
1130           disable = 0;
1131         if (disable) cpi->ref_frame_flags &= (~ref_flag);
1132       }
1133     }
1134   }
1135 }
1136 
vp9_svc_assert_constraints_pattern(VP9_COMP * const cpi)1137 void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
1138   SVC *const svc = &cpi->svc;
1139   // For fixed/non-flexible mode, the following constraint are expected,
1140   // when inter-layer prediciton is on (default).
1141   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1142       svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
1143       svc->framedrop_mode != LAYER_DROP) {
1144     if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1145       // On non-key frames: LAST is always temporal reference, GOLDEN is
1146       // spatial reference.
1147       if (svc->temporal_layer_id == 0)
1148         // Base temporal only predicts from base temporal.
1149         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
1150       else
1151         // Non-base temporal only predicts from lower temporal layer.
1152         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
1153                svc->temporal_layer_id);
1154       if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
1155           svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1156         // Non-base spatial only predicts from lower spatial layer with same
1157         // temporal_id.
1158         assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1159                svc->spatial_layer_id - 1);
1160         assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1161                svc->temporal_layer_id);
1162       }
1163     } else if (svc->spatial_layer_id > 0 &&
1164                svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1165       // Only 1 reference for frame whose base is key; reference may be LAST
1166       // or GOLDEN, so we check both.
1167       if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1168         assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
1169                svc->spatial_layer_id - 1);
1170         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
1171                svc->temporal_layer_id);
1172       } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1173         assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1174                svc->spatial_layer_id - 1);
1175         assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1176                svc->temporal_layer_id);
1177       }
1178     }
1179   } else if (svc->use_gf_temporal_ref_current_layer &&
1180              !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1181     // For the usage of golden as second long term reference: the
1182     // temporal_layer_id of that reference must be base temporal layer 0, and
1183     // spatial_layer_id of that reference must be same as current
1184     // spatial_layer_id. If not, disable feature.
1185     // TODO(marpan): Investigate when this can happen, and maybe put this check
1186     // and reset in a different place.
1187     if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
1188             svc->spatial_layer_id ||
1189         svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
1190       svc->use_gf_temporal_ref_current_layer = 0;
1191   }
1192 }
1193 
1194 #if CONFIG_VP9_TEMPORAL_DENOISING
vp9_denoise_svc_non_key(VP9_COMP * const cpi)1195 int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
1196   int layer =
1197       LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1198                        cpi->svc.number_temporal_layers);
1199   LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1200   return denoise_svc(cpi) && !lc->is_key_frame;
1201 }
1202 #endif
1203 
vp9_svc_check_spatial_layer_sync(VP9_COMP * const cpi)1204 void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
1205   SVC *const svc = &cpi->svc;
1206   // Only for superframes whose base is not key, as those are
1207   // already sync frames.
1208   if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1209     if (svc->spatial_layer_id == 0) {
1210       // On base spatial layer: if the current superframe has a layer sync then
1211       // reset the pattern counters and reset to base temporal layer.
1212       if (svc->superframe_has_layer_sync)
1213         vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
1214     }
1215     // If the layer sync is set for this current spatial layer then
1216     // disable the temporal reference.
1217     if (svc->spatial_layer_id > 0 &&
1218         svc->spatial_layer_sync[svc->spatial_layer_id]) {
1219       cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1220       if (svc->use_gf_temporal_ref_current_layer) {
1221         int index = svc->spatial_layer_id;
1222         // If golden is used as second reference: need to remove it from
1223         // prediction, reset refresh period to 0, and update the reference.
1224         svc->use_gf_temporal_ref_current_layer = 0;
1225         cpi->rc.baseline_gf_interval = 0;
1226         cpi->rc.frames_till_gf_update_due = 0;
1227         // On layer sync frame we must update the buffer index used for long
1228         // term reference. Use the alt_ref since it is not used or updated on
1229         // sync frames.
1230         if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
1231         assert(index >= 0);
1232         cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
1233         cpi->ext_refresh_alt_ref_frame = 1;
1234       }
1235     }
1236   }
1237 }
1238 
vp9_svc_update_ref_frame_buffer_idx(VP9_COMP * const cpi)1239 void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
1240   SVC *const svc = &cpi->svc;
1241   int i = 0;
1242   // Update the usage of frame buffer index for base spatial layers.
1243   if (svc->spatial_layer_id == 0) {
1244     if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
1245       svc->fb_idx_base[cpi->lst_fb_idx] = 1;
1246     if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
1247       svc->fb_idx_base[cpi->gld_fb_idx] = 1;
1248     if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
1249       svc->fb_idx_base[cpi->alt_fb_idx] = 1;
1250     // For bypass/flexible mode: check for refresh slots.
1251     if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
1252       for (i = 0; i < REF_FRAMES; ++i)
1253         if (svc->update_buffer_slot[0] & (1 << i)) svc->fb_idx_base[i] = 1;
1254     }
1255   }
1256 }
1257 
vp9_svc_update_ref_frame_bypass_mode(VP9_COMP * const cpi)1258 static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
1259   // For non-flexible/bypass SVC mode: check for refreshing other buffer
1260   // slots.
1261   SVC *const svc = &cpi->svc;
1262   VP9_COMMON *const cm = &cpi->common;
1263   BufferPool *const pool = cm->buffer_pool;
1264   int i;
1265   for (i = 0; i < REF_FRAMES; i++) {
1266     if ((cm->frame_type == KEY_FRAME && !svc->simulcast_mode) ||
1267         svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
1268       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1269       svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1270       svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1271     }
1272   }
1273 }
1274 
vp9_svc_update_ref_frame(VP9_COMP * const cpi)1275 void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
1276   VP9_COMMON *const cm = &cpi->common;
1277   SVC *const svc = &cpi->svc;
1278   BufferPool *const pool = cm->buffer_pool;
1279 
1280   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1281       svc->use_set_ref_frame_config) {
1282     vp9_svc_update_ref_frame_bypass_mode(cpi);
1283   } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
1284     // Keep track of frame index for each reference frame.
1285     int i;
1286     // On key frame update all reference frame slots.
1287     for (i = 0; i < REF_FRAMES; i++) {
1288       svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1289       svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1290       // LAST/GOLDEN/ALTREF is already updated above.
1291       if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
1292         ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1293     }
1294   } else {
1295     if (cpi->refresh_last_frame) {
1296       svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
1297       svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
1298     }
1299     if (cpi->refresh_golden_frame) {
1300       svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
1301       svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
1302     }
1303     if (cpi->refresh_alt_ref_frame) {
1304       svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
1305       svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
1306     }
1307   }
1308   // Copy flags from encoder to SVC struct.
1309   vp9_copy_flags_ref_update_idx(cpi);
1310   vp9_svc_update_ref_frame_buffer_idx(cpi);
1311 }
1312 
vp9_svc_adjust_frame_rate(VP9_COMP * const cpi)1313 void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
1314   int64_t this_duration =
1315       cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
1316   vp9_new_framerate(cpi, 10000000.0 / this_duration);
1317 }
1318 
vp9_svc_adjust_avg_frame_qindex(VP9_COMP * const cpi)1319 void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
1320   VP9_COMMON *const cm = &cpi->common;
1321   SVC *const svc = &cpi->svc;
1322   RATE_CONTROL *const rc = &cpi->rc;
1323   // On key frames in CBR mode: reset the avg_frame_index for base layer
1324   // (to level closer to worst_quality) if the overshoot is significant.
1325   // Reset it for all temporal layers on base spatial layer.
1326   if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
1327       !svc->simulcast_mode &&
1328       rc->projected_frame_size > 3 * rc->avg_frame_bandwidth) {
1329     int tl;
1330     rc->avg_frame_qindex[INTER_FRAME] =
1331         VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
1332                (cm->base_qindex + rc->worst_quality) >> 1);
1333     for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1334       const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
1335       LAYER_CONTEXT *lc = &svc->layer_context[layer];
1336       RATE_CONTROL *lrc = &lc->rc;
1337       lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
1338     }
1339   }
1340 }
1341