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