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_onyx_int.h"
     14 #include "vp9/encoder/vp9_svc_layercontext.h"
     15 
     16 void vp9_init_layer_context(VP9_COMP *const cpi) {
     17   const VP9_CONFIG *const oxcf = &cpi->oxcf;
     18   int layer;
     19   int layer_end;
     20 
     21   cpi->svc.spatial_layer_id = 0;
     22   cpi->svc.temporal_layer_id = 0;
     23 
     24   if (cpi->svc.number_temporal_layers > 1) {
     25     layer_end = cpi->svc.number_temporal_layers;
     26   } else {
     27     layer_end = cpi->svc.number_spatial_layers;
     28   }
     29 
     30   for (layer = 0; layer < layer_end; ++layer) {
     31     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer];
     32     RATE_CONTROL *const lrc = &lc->rc;
     33     lc->current_video_frame_in_layer = 0;
     34     lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
     35     lrc->ni_av_qi = q_trans[oxcf->worst_allowed_q];
     36     lrc->total_actual_bits = 0;
     37     lrc->total_target_vs_actual = 0;
     38     lrc->ni_tot_qi = 0;
     39     lrc->tot_q = 0.0;
     40     lrc->avg_q = 0.0;
     41     lrc->ni_frames = 0;
     42     lrc->decimation_count = 0;
     43     lrc->decimation_factor = 0;
     44     lrc->rate_correction_factor = 1.0;
     45     lrc->key_frame_rate_correction_factor = 1.0;
     46 
     47     if (cpi->svc.number_temporal_layers > 1) {
     48       lc->target_bandwidth = oxcf->ts_target_bitrate[layer] * 1000;
     49       lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
     50     } else {
     51       lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000;
     52       lrc->last_q[0] = q_trans[oxcf->best_allowed_q];
     53       lrc->last_q[1] = q_trans[oxcf->best_allowed_q];
     54       lrc->last_q[2] = q_trans[oxcf->best_allowed_q];
     55     }
     56 
     57     lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level),
     58                                     lc->target_bandwidth, 1000);
     59     lrc->bits_off_target = lrc->buffer_level;
     60   }
     61 }
     62 
     63 // Update the layer context from a change_config() call.
     64 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
     65                                             const int target_bandwidth) {
     66   const VP9_CONFIG *const oxcf = &cpi->oxcf;
     67   const RATE_CONTROL *const rc = &cpi->rc;
     68   int layer;
     69   int layer_end;
     70   float bitrate_alloc = 1.0;
     71 
     72   if (cpi->svc.number_temporal_layers > 1) {
     73     layer_end = cpi->svc.number_temporal_layers;
     74   } else {
     75     layer_end = cpi->svc.number_spatial_layers;
     76   }
     77 
     78   for (layer = 0; layer < layer_end; ++layer) {
     79     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer];
     80     RATE_CONTROL *const lrc = &lc->rc;
     81 
     82     if (cpi->svc.number_temporal_layers > 1) {
     83       lc->target_bandwidth = oxcf->ts_target_bitrate[layer] * 1000;
     84     } else {
     85       lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000;
     86     }
     87     bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
     88     // Update buffer-related quantities.
     89     lc->starting_buffer_level =
     90         (int64_t)(oxcf->starting_buffer_level * bitrate_alloc);
     91     lc->optimal_buffer_level =
     92         (int64_t)(oxcf->optimal_buffer_level * bitrate_alloc);
     93     lc->maximum_buffer_size =
     94         (int64_t)(oxcf->maximum_buffer_size * bitrate_alloc);
     95     lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
     96     lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
     97     // Update framerate-related quantities.
     98     if (cpi->svc.number_temporal_layers > 1) {
     99       lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer];
    100     } else {
    101       lc->framerate = oxcf->framerate;
    102     }
    103     lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    104     lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
    105     // Update qp-related quantities.
    106     lrc->worst_quality = rc->worst_quality;
    107     lrc->best_quality = rc->best_quality;
    108   }
    109 }
    110 
    111 static LAYER_CONTEXT *get_layer_context(SVC *svc) {
    112   return svc->number_temporal_layers > 1 ?
    113          &svc->layer_context[svc->temporal_layer_id] :
    114          &svc->layer_context[svc->spatial_layer_id];
    115 }
    116 
    117 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
    118   const int layer = cpi->svc.temporal_layer_id;
    119   const VP9_CONFIG *const oxcf = &cpi->oxcf;
    120   LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
    121   RATE_CONTROL *const lrc = &lc->rc;
    122 
    123   lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer];
    124   lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    125   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
    126   // Update the average layer frame size (non-cumulative per-frame-bw).
    127   if (layer == 0) {
    128     lc->avg_frame_size = lrc->av_per_frame_bandwidth;
    129   } else {
    130     const double prev_layer_framerate =
    131         oxcf->framerate / oxcf->ts_rate_decimator[layer - 1];
    132     const int prev_layer_target_bandwidth =
    133         oxcf->ts_target_bitrate[layer - 1] * 1000;
    134     lc->avg_frame_size =
    135         (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
    136               (lc->framerate - prev_layer_framerate));
    137   }
    138 }
    139 
    140 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
    141   const VP9_CONFIG *const oxcf = &cpi->oxcf;
    142   LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
    143   RATE_CONTROL *const lrc = &lc->rc;
    144 
    145   lc->framerate = framerate;
    146   lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    147   lrc->min_frame_bandwidth = (int)(lrc->av_per_frame_bandwidth *
    148                                    oxcf->two_pass_vbrmin_section / 100);
    149   lrc->max_frame_bandwidth = (int)(((int64_t)lrc->av_per_frame_bandwidth *
    150                                    oxcf->two_pass_vbrmax_section) / 100);
    151   lrc->max_gf_interval = 16;
    152 
    153   lrc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
    154 
    155   if (oxcf->play_alternate && oxcf->lag_in_frames) {
    156     if (lrc->max_gf_interval > oxcf->lag_in_frames - 1)
    157       lrc->max_gf_interval = oxcf->lag_in_frames - 1;
    158 
    159     if (lrc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
    160       lrc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
    161   }
    162 
    163   if (lrc->max_gf_interval > lrc->static_scene_max_gf_interval)
    164     lrc->max_gf_interval = lrc->static_scene_max_gf_interval;
    165 }
    166 
    167 void vp9_restore_layer_context(VP9_COMP *const cpi) {
    168   LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
    169   const int old_frame_since_key = cpi->rc.frames_since_key;
    170   const int old_frame_to_key = cpi->rc.frames_to_key;
    171 
    172   cpi->rc = lc->rc;
    173   cpi->twopass = lc->twopass;
    174   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
    175   cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
    176   cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
    177   cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
    178   cpi->output_framerate = lc->framerate;
    179   // Reset the frames_since_key and frames_to_key counters to their values
    180   // before the layer restore. Keep these defined for the stream (not layer).
    181   if (cpi->svc.number_temporal_layers > 1) {
    182     cpi->rc.frames_since_key = old_frame_since_key;
    183     cpi->rc.frames_to_key = old_frame_to_key;
    184   }
    185 }
    186 
    187 void vp9_save_layer_context(VP9_COMP *const cpi) {
    188   const VP9_CONFIG *const oxcf = &cpi->oxcf;
    189   LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
    190 
    191   lc->rc = cpi->rc;
    192   lc->twopass = cpi->twopass;
    193   lc->target_bandwidth = (int)oxcf->target_bandwidth;
    194   lc->starting_buffer_level = oxcf->starting_buffer_level;
    195   lc->optimal_buffer_level = oxcf->optimal_buffer_level;
    196   lc->maximum_buffer_size = oxcf->maximum_buffer_size;
    197   lc->framerate = cpi->output_framerate;
    198 }
    199 
    200 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
    201   int i;
    202   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
    203     struct twopass_rc *const twopass = &cpi->svc.layer_context[i].twopass;
    204 
    205     cpi->svc.spatial_layer_id = i;
    206     vp9_init_second_pass(cpi);
    207 
    208     twopass->total_stats.spatial_layer_id = i;
    209     twopass->total_left_stats.spatial_layer_id = i;
    210   }
    211   cpi->svc.spatial_layer_id = 0;
    212 }
    213