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_FB_IDX 7
     20 #define SMALL_FRAME_WIDTH  32
     21 #define SMALL_FRAME_HEIGHT 16
     22 
     23 void vp9_init_layer_context(VP9_COMP *const cpi) {
     24   SVC *const svc = &cpi->svc;
     25   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
     26   int mi_rows = cpi->common.mi_rows;
     27   int mi_cols = cpi->common.mi_cols;
     28   int sl, tl;
     29   int alt_ref_idx = svc->number_spatial_layers;
     30 
     31   svc->spatial_layer_id = 0;
     32   svc->temporal_layer_id = 0;
     33   svc->first_spatial_layer_to_encode = 0;
     34 
     35   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
     36     if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img,
     37                                  SMALL_FRAME_WIDTH, SMALL_FRAME_HEIGHT,
     38                                  cpi->common.subsampling_x,
     39                                  cpi->common.subsampling_y,
     40 #if CONFIG_VP9_HIGHBITDEPTH
     41                                  cpi->common.use_highbitdepth,
     42 #endif
     43                                  VP9_ENC_BORDER_IN_PIXELS,
     44                                  cpi->common.byte_alignment,
     45                                  NULL, NULL, NULL))
     46       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
     47                          "Failed to allocate empty frame for multiple frame "
     48                          "contexts");
     49 
     50     memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
     51            cpi->svc.empty_frame.img.buffer_alloc_sz);
     52   }
     53 
     54   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
     55     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
     56       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
     57       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
     58       RATE_CONTROL *const lrc = &lc->rc;
     59       int i;
     60       lc->current_video_frame_in_layer = 0;
     61       lc->layer_size = 0;
     62       lc->frames_from_key_frame = 0;
     63       lc->last_frame_type = FRAME_TYPES;
     64       lrc->ni_av_qi = oxcf->worst_allowed_q;
     65       lrc->total_actual_bits = 0;
     66       lrc->total_target_vs_actual = 0;
     67       lrc->ni_tot_qi = 0;
     68       lrc->tot_q = 0.0;
     69       lrc->avg_q = 0.0;
     70       lrc->ni_frames = 0;
     71       lrc->decimation_count = 0;
     72       lrc->decimation_factor = 0;
     73 
     74       for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
     75         lrc->rate_correction_factors[i] = 1.0;
     76       }
     77 
     78       if (cpi->oxcf.rc_mode == VPX_CBR) {
     79         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
     80         lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
     81         lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
     82         lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
     83       } else {
     84         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
     85         lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
     86         lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
     87         lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
     88                                             oxcf->best_allowed_q) / 2;
     89         lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
     90                                               oxcf->best_allowed_q) / 2;
     91         if (oxcf->ss_enable_auto_arf[sl])
     92           lc->alt_ref_idx = alt_ref_idx++;
     93         else
     94           lc->alt_ref_idx = INVALID_IDX;
     95         lc->gold_ref_idx = INVALID_IDX;
     96       }
     97 
     98       lrc->buffer_level = oxcf->starting_buffer_level_ms *
     99                               lc->target_bandwidth / 1000;
    100       lrc->bits_off_target = lrc->buffer_level;
    101 
    102       // Initialize the cyclic refresh parameters. If spatial layers are used
    103       // (i.e., ss_number_layers > 1), these need to be updated per spatial
    104       // layer.
    105       // Cyclic refresh is only applied on base temporal layer.
    106       if (oxcf->ss_number_layers > 1 &&
    107           tl == 0) {
    108         size_t last_coded_q_map_size;
    109         size_t consec_zero_mv_size;
    110         lc->sb_index = 0;
    111         lc->map = vpx_malloc(mi_rows * mi_cols * sizeof(signed char));
    112         memset(lc->map, 0, mi_rows * mi_cols);
    113         last_coded_q_map_size = mi_rows * mi_cols * sizeof(uint8_t);
    114         lc->last_coded_q_map = vpx_malloc(last_coded_q_map_size);
    115         assert(MAXQ <= 255);
    116         memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
    117         consec_zero_mv_size = mi_rows * mi_cols * sizeof(uint8_t);
    118         lc->consec_zero_mv = vpx_malloc(consec_zero_mv_size);
    119         memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
    120        }
    121     }
    122   }
    123 
    124   // Still have extra buffer for base layer golden frame
    125   if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
    126       && alt_ref_idx < REF_FRAMES)
    127     svc->layer_context[0].gold_ref_idx = alt_ref_idx;
    128 }
    129 
    130 // Update the layer context from a change_config() call.
    131 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
    132                                             const int target_bandwidth) {
    133   SVC *const svc = &cpi->svc;
    134   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    135   const RATE_CONTROL *const rc = &cpi->rc;
    136   int sl, tl, layer = 0, spatial_layer_target;
    137   float bitrate_alloc = 1.0;
    138 
    139   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
    140     for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
    141       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
    142         layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
    143         svc->layer_context[layer].target_bandwidth =
    144             oxcf->layer_target_bitrate[layer];
    145       }
    146 
    147       layer = LAYER_IDS_TO_IDX(sl, ((oxcf->ts_number_layers - 1) < 0 ?
    148           0 : (oxcf->ts_number_layers - 1)), oxcf->ts_number_layers);
    149       spatial_layer_target =
    150           svc->layer_context[layer].target_bandwidth =
    151               oxcf->layer_target_bitrate[layer];
    152 
    153       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
    154         LAYER_CONTEXT *const lc =
    155             &svc->layer_context[sl * oxcf->ts_number_layers + tl];
    156         RATE_CONTROL *const lrc = &lc->rc;
    157 
    158         lc->spatial_layer_target_bandwidth = spatial_layer_target;
    159         bitrate_alloc = (float)lc->target_bandwidth / spatial_layer_target;
    160         lrc->starting_buffer_level =
    161             (int64_t)(rc->starting_buffer_level * bitrate_alloc);
    162         lrc->optimal_buffer_level =
    163             (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
    164         lrc->maximum_buffer_size =
    165             (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
    166         lrc->bits_off_target =
    167             VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
    168         lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
    169         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
    170         lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    171         lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
    172         lrc->worst_quality = rc->worst_quality;
    173         lrc->best_quality = rc->best_quality;
    174       }
    175     }
    176   } else {
    177     int layer_end;
    178 
    179     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
    180       layer_end = svc->number_temporal_layers;
    181     } else {
    182       layer_end = svc->number_spatial_layers;
    183     }
    184 
    185     for (layer = 0; layer < layer_end; ++layer) {
    186       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
    187       RATE_CONTROL *const lrc = &lc->rc;
    188 
    189       lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
    190 
    191       bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
    192       // Update buffer-related quantities.
    193       lrc->starting_buffer_level =
    194           (int64_t)(rc->starting_buffer_level * bitrate_alloc);
    195       lrc->optimal_buffer_level =
    196           (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
    197       lrc->maximum_buffer_size =
    198           (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
    199       lrc->bits_off_target = VPXMIN(lrc->bits_off_target,
    200                                     lrc->maximum_buffer_size);
    201       lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
    202       // Update framerate-related quantities.
    203       if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
    204         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
    205       } else {
    206         lc->framerate = cpi->framerate;
    207       }
    208       lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    209       lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
    210       // Update qp-related quantities.
    211       lrc->worst_quality = rc->worst_quality;
    212       lrc->best_quality = rc->best_quality;
    213     }
    214   }
    215 }
    216 
    217 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
    218   if (is_one_pass_cbr_svc(cpi))
    219     return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    220         cpi->svc.number_temporal_layers + cpi->svc.temporal_layer_id];
    221   else
    222     return (cpi->svc.number_temporal_layers > 1 &&
    223             cpi->oxcf.rc_mode == VPX_CBR) ?
    224              &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
    225              &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
    226 }
    227 
    228 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
    229   SVC *const svc = &cpi->svc;
    230   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    231   LAYER_CONTEXT *const lc = get_layer_context(cpi);
    232   RATE_CONTROL *const lrc = &lc->rc;
    233   // Index into spatial+temporal arrays.
    234   const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
    235       svc->temporal_layer_id;
    236   const int tl = svc->temporal_layer_id;
    237 
    238   lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
    239   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    240   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
    241   // Update the average layer frame size (non-cumulative per-frame-bw).
    242   if (tl == 0) {
    243     lc->avg_frame_size = lrc->avg_frame_bandwidth;
    244   } else {
    245     const double prev_layer_framerate =
    246         cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
    247     const int prev_layer_target_bandwidth =
    248         oxcf->layer_target_bitrate[st_idx - 1];
    249     lc->avg_frame_size =
    250         (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
    251               (lc->framerate - prev_layer_framerate));
    252   }
    253 }
    254 
    255 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
    256   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    257   LAYER_CONTEXT *const lc = get_layer_context(cpi);
    258   RATE_CONTROL *const lrc = &lc->rc;
    259 
    260   lc->framerate = framerate;
    261   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    262   lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth *
    263                                    oxcf->two_pass_vbrmin_section / 100);
    264   lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
    265                                    oxcf->two_pass_vbrmax_section) / 100);
    266   vp9_rc_set_gf_interval_range(cpi, lrc);
    267 }
    268 
    269 void vp9_restore_layer_context(VP9_COMP *const cpi) {
    270   LAYER_CONTEXT *const lc = get_layer_context(cpi);
    271   const int old_frame_since_key = cpi->rc.frames_since_key;
    272   const int old_frame_to_key = cpi->rc.frames_to_key;
    273 
    274   cpi->rc = lc->rc;
    275   cpi->twopass = lc->twopass;
    276   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
    277   cpi->alt_ref_source = lc->alt_ref_source;
    278   // Reset the frames_since_key and frames_to_key counters to their values
    279   // before the layer restore. Keep these defined for the stream (not layer).
    280   if (cpi->svc.number_temporal_layers > 1) {
    281     cpi->rc.frames_since_key = old_frame_since_key;
    282     cpi->rc.frames_to_key = old_frame_to_key;
    283   }
    284 
    285   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
    286   // for the base temporal layer.
    287   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
    288       cpi->svc.number_spatial_layers > 1 &&
    289       cpi->svc.temporal_layer_id == 0) {
    290     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
    291     signed char *temp = cr->map;
    292     uint8_t *temp2 = cr->last_coded_q_map;
    293     uint8_t *temp3 = cr->consec_zero_mv;
    294     cr->map = lc->map;
    295     lc->map = temp;
    296     cr->last_coded_q_map = lc->last_coded_q_map;
    297     lc->last_coded_q_map = temp2;
    298     cr->consec_zero_mv = lc->consec_zero_mv;
    299     lc->consec_zero_mv = temp3;
    300     cr->sb_index = lc->sb_index;
    301   }
    302 }
    303 
    304 void vp9_save_layer_context(VP9_COMP *const cpi) {
    305   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    306   LAYER_CONTEXT *const lc = get_layer_context(cpi);
    307 
    308   lc->rc = cpi->rc;
    309   lc->twopass = cpi->twopass;
    310   lc->target_bandwidth = (int)oxcf->target_bandwidth;
    311   lc->alt_ref_source = cpi->alt_ref_source;
    312 
    313   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
    314   // for the base temporal layer.
    315   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
    316       cpi->svc.number_spatial_layers > 1 &&
    317       cpi->svc.temporal_layer_id == 0) {
    318     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
    319     signed char *temp = lc->map;
    320     uint8_t *temp2 = lc->last_coded_q_map;
    321     uint8_t *temp3 = lc->consec_zero_mv;
    322     lc->map = cr->map;
    323     cr->map = temp;
    324     lc->last_coded_q_map = cr->last_coded_q_map;
    325     cr->last_coded_q_map = temp2;
    326     lc->consec_zero_mv = cr->consec_zero_mv;
    327     cr->consec_zero_mv = temp3;
    328     lc->sb_index = cr->sb_index;
    329   }
    330 }
    331 
    332 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
    333   SVC *const svc = &cpi->svc;
    334   int i;
    335 
    336   for (i = 0; i < svc->number_spatial_layers; ++i) {
    337     TWO_PASS *const twopass = &svc->layer_context[i].twopass;
    338 
    339     svc->spatial_layer_id = i;
    340     vp9_init_second_pass(cpi);
    341 
    342     twopass->total_stats.spatial_layer_id = i;
    343     twopass->total_left_stats.spatial_layer_id = i;
    344   }
    345   svc->spatial_layer_id = 0;
    346 }
    347 
    348 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
    349   LAYER_CONTEXT *const lc =
    350       &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    351                               cpi->svc.number_temporal_layers];
    352   ++lc->current_video_frame_in_layer;
    353   ++lc->frames_from_key_frame;
    354 }
    355 
    356 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
    357   return is_two_pass_svc(cpi) &&
    358          cpi->svc.spatial_layer_id > 0 &&
    359          cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    360                                 cpi->svc.number_temporal_layers +
    361                                 cpi->svc.temporal_layer_id].is_key_frame;
    362 }
    363 
    364 static void get_layer_resolution(const int width_org, const int height_org,
    365                                  const int num, const int den,
    366                                  int *width_out, int *height_out) {
    367   int w, h;
    368 
    369   if (width_out == NULL || height_out == NULL || den == 0)
    370     return;
    371 
    372   w = width_org * num / den;
    373   h = height_org * num / den;
    374 
    375   // make height and width even to make chrome player happy
    376   w += w % 2;
    377   h += h % 2;
    378 
    379   *width_out = w;
    380   *height_out = h;
    381 }
    382 
    383 // The function sets proper ref_frame_flags, buffer indices, and buffer update
    384 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
    385 // scheme.
    386 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
    387   int frame_num_within_temporal_struct = 0;
    388   int spatial_id, temporal_id;
    389   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
    390   frame_num_within_temporal_struct =
    391       cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    392       cpi->svc.number_temporal_layers].current_video_frame_in_layer % 4;
    393   temporal_id = cpi->svc.temporal_layer_id =
    394       (frame_num_within_temporal_struct & 1) ? 2 :
    395       (frame_num_within_temporal_struct >> 1);
    396   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
    397       cpi->ext_refresh_alt_ref_frame = 0;
    398   if (!temporal_id) {
    399     cpi->ext_refresh_frame_flags_pending = 1;
    400     cpi->ext_refresh_last_frame = 1;
    401     if (!spatial_id) {
    402       cpi->ref_frame_flags = VP9_LAST_FLAG;
    403     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
    404       // base layer is a key frame.
    405       cpi->ref_frame_flags = VP9_GOLD_FLAG;
    406     } else {
    407       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    408     }
    409   } else if (temporal_id == 1) {
    410     cpi->ext_refresh_frame_flags_pending = 1;
    411     cpi->ext_refresh_alt_ref_frame = 1;
    412     if (!spatial_id) {
    413       cpi->ref_frame_flags = VP9_LAST_FLAG;
    414     } else {
    415       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    416     }
    417   } else {
    418     if (frame_num_within_temporal_struct == 1) {
    419       // the first tl2 picture
    420       if (!spatial_id) {
    421         cpi->ext_refresh_frame_flags_pending = 1;
    422         cpi->ext_refresh_alt_ref_frame = 1;
    423         cpi->ref_frame_flags = VP9_LAST_FLAG;
    424       } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
    425         cpi->ext_refresh_frame_flags_pending = 1;
    426         cpi->ext_refresh_alt_ref_frame = 1;
    427         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    428       } else {  // Top layer
    429         cpi->ext_refresh_frame_flags_pending = 0;
    430         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    431       }
    432     } else {
    433       //  The second tl2 picture
    434       if (!spatial_id) {
    435         cpi->ext_refresh_frame_flags_pending = 1;
    436         cpi->ref_frame_flags = VP9_LAST_FLAG;
    437         cpi->ext_refresh_last_frame = 1;
    438       } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
    439         cpi->ext_refresh_frame_flags_pending = 1;
    440         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    441         cpi->ext_refresh_last_frame = 1;
    442       } else {  // top layer
    443         cpi->ext_refresh_frame_flags_pending = 0;
    444         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    445       }
    446     }
    447   }
    448   if (temporal_id == 0) {
    449     cpi->lst_fb_idx = spatial_id;
    450     if (spatial_id)
    451       cpi->gld_fb_idx = spatial_id - 1;
    452     else
    453       cpi->gld_fb_idx = 0;
    454     cpi->alt_fb_idx = 0;
    455   } else if (temporal_id == 1) {
    456     cpi->lst_fb_idx = spatial_id;
    457     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
    458     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
    459   } else if (frame_num_within_temporal_struct == 1) {
    460     cpi->lst_fb_idx = spatial_id;
    461     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
    462     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
    463   } else {
    464     cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
    465     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
    466     cpi->alt_fb_idx = 0;
    467   }
    468 }
    469 
    470 // The function sets proper ref_frame_flags, buffer indices, and buffer update
    471 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
    472 // scheme.
    473 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
    474   int spatial_id, temporal_id;
    475   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
    476   temporal_id = cpi->svc.temporal_layer_id =
    477       cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    478       cpi->svc.number_temporal_layers].current_video_frame_in_layer & 1;
    479   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
    480                                 cpi->ext_refresh_alt_ref_frame = 0;
    481   if (!temporal_id) {
    482     cpi->ext_refresh_frame_flags_pending = 1;
    483     cpi->ext_refresh_last_frame = 1;
    484     if (!spatial_id) {
    485       cpi->ref_frame_flags = VP9_LAST_FLAG;
    486     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
    487       // base layer is a key frame.
    488       cpi->ref_frame_flags = VP9_GOLD_FLAG;
    489     } else {
    490       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    491     }
    492   } else if (temporal_id == 1) {
    493     cpi->ext_refresh_frame_flags_pending = 1;
    494     cpi->ext_refresh_alt_ref_frame = 1;
    495     if (!spatial_id) {
    496       cpi->ref_frame_flags = VP9_LAST_FLAG;
    497     } else {
    498       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    499     }
    500   }
    501 
    502   if (temporal_id == 0) {
    503     cpi->lst_fb_idx = spatial_id;
    504     if (spatial_id)
    505       cpi->gld_fb_idx = spatial_id - 1;
    506     else
    507       cpi->gld_fb_idx = 0;
    508     cpi->alt_fb_idx = 0;
    509   } else if (temporal_id == 1) {
    510     cpi->lst_fb_idx = spatial_id;
    511     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
    512     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
    513   }
    514 }
    515 
    516 // The function sets proper ref_frame_flags, buffer indices, and buffer update
    517 // variables for temporal layering mode 0 - that has no temporal layering.
    518 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
    519     VP9_COMP *const cpi) {
    520   int spatial_id;
    521   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
    522   cpi->ext_refresh_last_frame =
    523       cpi->ext_refresh_golden_frame = cpi->ext_refresh_alt_ref_frame = 0;
    524   cpi->ext_refresh_frame_flags_pending = 1;
    525   cpi->ext_refresh_last_frame = 1;
    526   if (!spatial_id) {
    527     cpi->ref_frame_flags = VP9_LAST_FLAG;
    528   } else if (cpi->svc.layer_context[0].is_key_frame) {
    529     cpi->ref_frame_flags = VP9_GOLD_FLAG;
    530   } else {
    531     cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
    532   }
    533   cpi->lst_fb_idx = spatial_id;
    534   if (spatial_id)
    535     cpi->gld_fb_idx = spatial_id - 1;
    536   else
    537     cpi->gld_fb_idx = 0;
    538 }
    539 
    540 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
    541   int width = 0, height = 0;
    542   LAYER_CONTEXT *lc = NULL;
    543 
    544   if (cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
    545     set_flags_and_fb_idx_for_temporal_mode3(cpi);
    546   } else if (cpi->svc.temporal_layering_mode ==
    547            VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
    548     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
    549   } else if (cpi->svc.temporal_layering_mode ==
    550            VP9E_TEMPORAL_LAYERING_MODE_0101) {
    551     set_flags_and_fb_idx_for_temporal_mode2(cpi);
    552   } else if (cpi->svc.temporal_layering_mode ==
    553       VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
    554     // In the BYPASS/flexible mode, the encoder is relying on the application
    555     // to specify, for each spatial layer, the flags and buffer indices for the
    556     // layering.
    557     // Note that the check (cpi->ext_refresh_frame_flags_pending == 0) is
    558     // needed to support the case where the frame flags may be passed in via
    559     // vpx_codec_encode(), which can be used for the temporal-only svc case.
    560     if (cpi->ext_refresh_frame_flags_pending == 0) {
    561       int sl;
    562       cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
    563       sl = cpi->svc.spatial_layer_id;
    564       vp9_apply_encoding_flags(cpi, cpi->svc.ext_frame_flags[sl]);
    565       cpi->lst_fb_idx = cpi->svc.ext_lst_fb_idx[sl];
    566       cpi->gld_fb_idx = cpi->svc.ext_gld_fb_idx[sl];
    567       cpi->alt_fb_idx = cpi->svc.ext_alt_fb_idx[sl];
    568     }
    569   }
    570 
    571   lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
    572                                cpi->svc.number_temporal_layers +
    573                                cpi->svc.temporal_layer_id];
    574 
    575   // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
    576   // only for non-BYPASS mode for now.
    577   if (cpi->svc.temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
    578     RATE_CONTROL *const lrc = &lc->rc;
    579     lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
    580     lrc->best_quality =  vp9_quantizer_to_qindex(lc->min_q);
    581   }
    582 
    583   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
    584                        lc->scaling_factor_num, lc->scaling_factor_den,
    585                        &width, &height);
    586 
    587   if (vp9_set_size_literal(cpi, width, height) != 0)
    588     return VPX_CODEC_INVALID_PARAM;
    589 
    590   return 0;
    591 }
    592 
    593 #if CONFIG_SPATIAL_SVC
    594 int vp9_svc_start_frame(VP9_COMP *const cpi) {
    595   int width = 0, height = 0;
    596   LAYER_CONTEXT *lc;
    597   struct lookahead_entry *buf;
    598   int count = 1 << (cpi->svc.number_temporal_layers - 1);
    599 
    600   cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
    601   lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
    602 
    603   cpi->svc.temporal_layer_id = 0;
    604   while ((lc->current_video_frame_in_layer % count) != 0) {
    605     ++cpi->svc.temporal_layer_id;
    606     count >>= 1;
    607   }
    608 
    609   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
    610 
    611   cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
    612 
    613   if (cpi->svc.spatial_layer_id == 0)
    614     cpi->gld_fb_idx = (lc->gold_ref_idx >= 0) ?
    615                       lc->gold_ref_idx : cpi->lst_fb_idx;
    616   else
    617     cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
    618 
    619   if (lc->current_video_frame_in_layer == 0) {
    620     if (cpi->svc.spatial_layer_id >= 2) {
    621       cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
    622     } else {
    623       cpi->alt_fb_idx = cpi->lst_fb_idx;
    624       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
    625     }
    626   } else {
    627     if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
    628       cpi->alt_fb_idx = lc->alt_ref_idx;
    629       if (!lc->has_alt_frame)
    630         cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
    631     } else {
    632       // Find a proper alt_fb_idx for layers that don't have alt ref frame
    633       if (cpi->svc.spatial_layer_id == 0) {
    634         cpi->alt_fb_idx = cpi->lst_fb_idx;
    635       } else {
    636         LAYER_CONTEXT *lc_lower =
    637             &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
    638 
    639         if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
    640             lc_lower->alt_ref_source != NULL)
    641           cpi->alt_fb_idx = lc_lower->alt_ref_idx;
    642         else if (cpi->svc.spatial_layer_id >= 2)
    643           cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
    644         else
    645           cpi->alt_fb_idx = cpi->lst_fb_idx;
    646       }
    647     }
    648   }
    649 
    650   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
    651                        lc->scaling_factor_num, lc->scaling_factor_den,
    652                        &width, &height);
    653 
    654   // Workaround for multiple frame contexts. In some frames we can't use prev_mi
    655   // since its previous frame could be changed during decoding time. The idea is
    656   // we put a empty invisible frame in front of them, then we will not use
    657   // prev_mi when encoding these frames.
    658 
    659   buf = vp9_lookahead_peek(cpi->lookahead, 0);
    660   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
    661       cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
    662       lc->rc.frames_to_key != 0 &&
    663       !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
    664     if ((cpi->svc.number_temporal_layers > 1 &&
    665          cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
    666         (cpi->svc.number_spatial_layers > 1 &&
    667          cpi->svc.spatial_layer_id == 0)) {
    668       struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
    669 
    670       if (buf != NULL) {
    671         cpi->svc.empty_frame.ts_start = buf->ts_start;
    672         cpi->svc.empty_frame.ts_end = buf->ts_end;
    673         cpi->svc.encode_empty_frame_state = ENCODING;
    674         cpi->common.show_frame = 0;
    675         cpi->ref_frame_flags = 0;
    676         cpi->common.frame_type = INTER_FRAME;
    677         cpi->lst_fb_idx =
    678             cpi->gld_fb_idx = cpi->alt_fb_idx = SMALL_FRAME_FB_IDX;
    679 
    680         if (cpi->svc.encode_intra_empty_frame != 0)
    681           cpi->common.intra_only = 1;
    682 
    683         width = SMALL_FRAME_WIDTH;
    684         height = SMALL_FRAME_HEIGHT;
    685       }
    686     }
    687   }
    688 
    689   cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
    690   cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
    691 
    692   vp9_change_config(cpi, &cpi->oxcf);
    693 
    694   if (vp9_set_size_literal(cpi, width, height) != 0)
    695     return VPX_CODEC_INVALID_PARAM;
    696 
    697   vp9_set_high_precision_mv(cpi, 1);
    698 
    699   cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
    700 
    701   return 0;
    702 }
    703 
    704 #endif
    705 
    706 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
    707                                               struct lookahead_ctx *ctx,
    708                                               int drain) {
    709   struct lookahead_entry *buf = NULL;
    710   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
    711     buf = vp9_lookahead_peek(ctx, 0);
    712     if (buf != NULL) {
    713       // Only remove the buffer when pop the highest layer.
    714       if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
    715         vp9_lookahead_pop(ctx, drain);
    716       }
    717     }
    718   }
    719   return buf;
    720 }
    721 
    722 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
    723   int sl, tl;
    724   SVC *const svc = &cpi->svc;
    725   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
    726   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
    727     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
    728       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
    729       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
    730         if (lc->map)
    731           vpx_free(lc->map);
    732         if (lc->last_coded_q_map)
    733           vpx_free(lc->last_coded_q_map);
    734         if (lc->consec_zero_mv)
    735           vpx_free(lc->consec_zero_mv);
    736     }
    737   }
    738 }
    739