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