vp9_svc_layercontext.c 8.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <math.h>

#include "vp9/encoder/vp9_onyx_int.h"
#include "vp9/encoder/vp9_svc_layercontext.h"

void vp9_init_layer_context(VP9_COMP *const cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
17
  SVC *const svc = &cpi->svc;
18
  const VP9_CONFIG *const oxcf = &cpi->oxcf;
19
  int layer;
20
  int layer_end;
21

Dmitry Kovalev's avatar
Dmitry Kovalev committed
22
23
  svc->spatial_layer_id = 0;
  svc->temporal_layer_id = 0;
24

Dmitry Kovalev's avatar
Dmitry Kovalev committed
25
26
  if (svc->number_temporal_layers > 1) {
    layer_end = svc->number_temporal_layers;
27
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
28
    layer_end = svc->number_spatial_layers;
29
30
31
  }

  for (layer = 0; layer < layer_end; ++layer) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
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
36
    lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
    lrc->ni_av_qi = oxcf->worst_allowed_q;
37
38
39
40
41
42
43
44
45
46
    lrc->total_actual_bits = 0;
    lrc->total_target_vs_actual = 0;
    lrc->ni_tot_qi = 0;
    lrc->tot_q = 0.0;
    lrc->avg_q = 0.0;
    lrc->ni_frames = 0;
    lrc->decimation_count = 0;
    lrc->decimation_factor = 0;
    lrc->rate_correction_factor = 1.0;
    lrc->key_frame_rate_correction_factor = 1.0;
47

Dmitry Kovalev's avatar
Dmitry Kovalev committed
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
52
    } else {
      lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000;
53
54
55
      lrc->last_q[0] = oxcf->best_allowed_q;
      lrc->last_q[1] = oxcf->best_allowed_q;
      lrc->last_q[2] = oxcf->best_allowed_q;
56
57
    }

58
59
    lrc->buffer_level = vp9_rescale((int)(oxcf->starting_buffer_level),
                                    lc->target_bandwidth, 1000);
60
61
62
63
64
65
66
    lrc->bits_off_target = lrc->buffer_level;
  }
}

// Update the layer context from a change_config() call.
void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
                                            const int target_bandwidth) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
67
  SVC *const svc = &cpi->svc;
68
69
  const VP9_CONFIG *const oxcf = &cpi->oxcf;
  const RATE_CONTROL *const rc = &cpi->rc;
70
  int layer;
71
  int layer_end;
72
  float bitrate_alloc = 1.0;
73

Dmitry Kovalev's avatar
Dmitry Kovalev committed
74
75
  if (svc->number_temporal_layers > 1) {
    layer_end = svc->number_temporal_layers;
76
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
77
    layer_end = svc->number_spatial_layers;
78
79
80
  }

  for (layer = 0; layer < layer_end; ++layer) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
81
    LAYER_CONTEXT *const lc = &svc->layer_context[layer];
82
    RATE_CONTROL *const lrc = &lc->rc;
83

Dmitry Kovalev's avatar
Dmitry Kovalev committed
84
    if (svc->number_temporal_layers > 1) {
85
86
87
88
      lc->target_bandwidth = oxcf->ts_target_bitrate[layer] * 1000;
    } else {
      lc->target_bandwidth = oxcf->ss_target_bitrate[layer] * 1000;
    }
89
    bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
90
91
92
93
94
95
96
97
98
99
    // Update buffer-related quantities.
    lc->starting_buffer_level =
        (int64_t)(oxcf->starting_buffer_level * bitrate_alloc);
    lc->optimal_buffer_level =
        (int64_t)(oxcf->optimal_buffer_level * bitrate_alloc);
    lc->maximum_buffer_size =
        (int64_t)(oxcf->maximum_buffer_size * bitrate_alloc);
    lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
    lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
    // Update framerate-related quantities.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
100
    if (svc->number_temporal_layers > 1) {
101
102
103
104
      lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer];
    } else {
      lc->framerate = oxcf->framerate;
    }
105
106
107
108
109
110
111
112
    lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
    lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
    // Update qp-related quantities.
    lrc->worst_quality = rc->worst_quality;
    lrc->best_quality = rc->best_quality;
  }
}

113
114
115
116
static LAYER_CONTEXT *get_layer_context(SVC *svc) {
  return svc->number_temporal_layers > 1 ?
         &svc->layer_context[svc->temporal_layer_id] :
         &svc->layer_context[svc->spatial_layer_id];
117
118
}

119
void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
120
  SVC *const svc = &cpi->svc;
121
  const VP9_CONFIG *const oxcf = &cpi->oxcf;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
122
  LAYER_CONTEXT *const lc = get_layer_context(svc);
123
  RATE_CONTROL *const lrc = &lc->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
124
  const int layer = svc->temporal_layer_id;
125
126

  lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[layer];
127
128
129
  lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
  // Update the average layer frame size (non-cumulative per-frame-bw).
130
  if (layer == 0) {
131
132
    lc->avg_frame_size = lrc->av_per_frame_bandwidth;
  } else {
133
134
135
136
    const double prev_layer_framerate =
        oxcf->framerate / oxcf->ts_rate_decimator[layer - 1];
    const int prev_layer_target_bandwidth =
        oxcf->ts_target_bitrate[layer - 1] * 1000;
137
138
139
140
141
142
    lc->avg_frame_size =
        (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
              (lc->framerate - prev_layer_framerate));
  }
}

143
144
void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
  const VP9_CONFIG *const oxcf = &cpi->oxcf;
145
  LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
146
147
148
149
150
151
152
153
154
  RATE_CONTROL *const lrc = &lc->rc;

  lc->framerate = framerate;
  lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  lrc->min_frame_bandwidth = (int)(lrc->av_per_frame_bandwidth *
                                   oxcf->two_pass_vbrmin_section / 100);
  lrc->max_frame_bandwidth = (int)(((int64_t)lrc->av_per_frame_bandwidth *
                                   oxcf->two_pass_vbrmax_section) / 100);
  lrc->max_gf_interval = 16;
155
156
157
158
159
160
161
162
163
164
165
166
167

  lrc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;

  if (oxcf->play_alternate && oxcf->lag_in_frames) {
    if (lrc->max_gf_interval > oxcf->lag_in_frames - 1)
      lrc->max_gf_interval = oxcf->lag_in_frames - 1;

    if (lrc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
      lrc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
  }

  if (lrc->max_gf_interval > lrc->static_scene_max_gf_interval)
    lrc->max_gf_interval = lrc->static_scene_max_gf_interval;
168
169
}

170
void vp9_restore_layer_context(VP9_COMP *const cpi) {
171
  LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
172
173
174
  const int old_frame_since_key = cpi->rc.frames_since_key;
  const int old_frame_to_key = cpi->rc.frames_to_key;

175
  cpi->rc = lc->rc;
176
  cpi->twopass = lc->twopass;
177
178
179
180
181
182
  cpi->oxcf.target_bandwidth = lc->target_bandwidth;
  cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
  cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
  cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
  // Reset the frames_since_key and frames_to_key counters to their values
  // before the layer restore. Keep these defined for the stream (not layer).
183
184
185
186
  if (cpi->svc.number_temporal_layers > 1) {
    cpi->rc.frames_since_key = old_frame_since_key;
    cpi->rc.frames_to_key = old_frame_to_key;
  }
187
188
189
}

void vp9_save_layer_context(VP9_COMP *const cpi) {
190
  const VP9_CONFIG *const oxcf = &cpi->oxcf;
191
  LAYER_CONTEXT *const lc = get_layer_context(&cpi->svc);
192

193
  lc->rc = cpi->rc;
194
  lc->twopass = cpi->twopass;
195
196
197
198
  lc->target_bandwidth = (int)oxcf->target_bandwidth;
  lc->starting_buffer_level = oxcf->starting_buffer_level;
  lc->optimal_buffer_level = oxcf->optimal_buffer_level;
  lc->maximum_buffer_size = oxcf->maximum_buffer_size;
199
}
200
201

void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
202
  SVC *const svc = &cpi->svc;
203
204
  int i;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
205
206
207
208
  for (i = 0; i < svc->number_spatial_layers; ++i) {
    struct twopass_rc *const twopass = &svc->layer_context[i].twopass;

    svc->spatial_layer_id = i;
209
210
211
212
213
    vp9_init_second_pass(cpi);

    twopass->total_stats.spatial_layer_id = i;
    twopass->total_left_stats.spatial_layer_id = i;
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
214
  svc->spatial_layer_id = 0;
215
}
216
217
218
219
220
221
222

void vp9_inc_frame_in_layer(SVC *svc) {
  LAYER_CONTEXT *const lc = (svc->number_temporal_layers > 1)
      ? &svc->layer_context[svc->temporal_layer_id]
      : &svc->layer_context[svc->spatial_layer_id];
  ++lc->current_video_frame_in_layer;
}