vp9_svc_layercontext.c revision a72801d7d92ababb50eecf27a36bd222d031d2fe
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
16void vp9_init_layer_context(VP9_COMP *const cpi) {
17  const VP9_CONFIG *const oxcf = &cpi->oxcf;
18  int temporal_layer = 0;
19  cpi->svc.spatial_layer_id = 0;
20  cpi->svc.temporal_layer_id = 0;
21  for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
22      ++temporal_layer) {
23    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
24    RATE_CONTROL *const lrc = &lc->rc;
25    lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
26    lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
27    lrc->ni_av_qi = q_trans[oxcf->worst_allowed_q];
28    lrc->total_actual_bits = 0;
29    lrc->total_target_vs_actual = 0;
30    lrc->ni_tot_qi = 0;
31    lrc->tot_q = 0.0;
32    lrc->avg_q = 0.0;
33    lrc->ni_frames = 0;
34    lrc->decimation_count = 0;
35    lrc->decimation_factor = 0;
36    lrc->rate_correction_factor = 1.0;
37    lrc->key_frame_rate_correction_factor = 1.0;
38    lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] *
39        1000;
40    lrc->buffer_level =
41        vp9_rescale((int)(oxcf->starting_buffer_level),
42                    lc->target_bandwidth, 1000);
43    lrc->bits_off_target = lrc->buffer_level;
44  }
45}
46
47// Update the layer context from a change_config() call.
48void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
49                                            const int target_bandwidth) {
50  const VP9_CONFIG *const oxcf = &cpi->oxcf;
51  const RATE_CONTROL *const rc = &cpi->rc;
52  int temporal_layer = 0;
53  float bitrate_alloc = 1.0;
54  for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
55      ++temporal_layer) {
56    LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
57    RATE_CONTROL *const lrc = &lc->rc;
58    lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] * 1000;
59    bitrate_alloc = (float)lc->target_bandwidth / (float)target_bandwidth;
60    // Update buffer-related quantities.
61    lc->starting_buffer_level =
62        (int64_t)(oxcf->starting_buffer_level * bitrate_alloc);
63    lc->optimal_buffer_level =
64        (int64_t)(oxcf->optimal_buffer_level * bitrate_alloc);
65    lc->maximum_buffer_size =
66        (int64_t)(oxcf->maximum_buffer_size * bitrate_alloc);
67    lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
68    lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
69    // Update framerate-related quantities.
70    lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
71    lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
72    lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
73    // Update qp-related quantities.
74    lrc->worst_quality = rc->worst_quality;
75    lrc->best_quality = rc->best_quality;
76  }
77}
78
79void vp9_update_layer_framerate(VP9_COMP *const cpi) {
80  int temporal_layer = cpi->svc.temporal_layer_id;
81  const VP9_CONFIG *const oxcf = &cpi->oxcf;
82  LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
83  RATE_CONTROL *const lrc = &lc->rc;
84  lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
85  lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
86  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
87  // Update the average layer frame size (non-cumulative per-frame-bw).
88  if (temporal_layer == 0) {
89    lc->avg_frame_size = lrc->av_per_frame_bandwidth;
90  } else {
91    double prev_layer_framerate = oxcf->framerate /
92        oxcf->ts_rate_decimator[temporal_layer - 1];
93    int prev_layer_target_bandwidth =
94        oxcf->ts_target_bitrate[temporal_layer - 1] * 1000;
95    lc->avg_frame_size =
96        (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
97              (lc->framerate - prev_layer_framerate));
98  }
99}
100
101void vp9_restore_layer_context(VP9_COMP *const cpi) {
102  int temporal_layer = cpi->svc.temporal_layer_id;
103  LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
104  int frame_since_key = cpi->rc.frames_since_key;
105  int frame_to_key = cpi->rc.frames_to_key;
106  cpi->rc = lc->rc;
107  cpi->oxcf.target_bandwidth = lc->target_bandwidth;
108  cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
109  cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
110  cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
111  cpi->output_framerate = lc->framerate;
112  // Reset the frames_since_key and frames_to_key counters to their values
113  // before the layer restore. Keep these defined for the stream (not layer).
114  cpi->rc.frames_since_key = frame_since_key;
115  cpi->rc.frames_to_key = frame_to_key;
116}
117
118void vp9_save_layer_context(VP9_COMP *const cpi) {
119  int temporal_layer = cpi->svc.temporal_layer_id;
120  LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
121  lc->rc = cpi->rc;
122  lc->target_bandwidth = (int)cpi->oxcf.target_bandwidth;
123  lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
124  lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
125  lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
126  lc->framerate = cpi->output_framerate;
127}
128