vp9_svc_layercontext.c 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  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>

Dmitry Kovalev's avatar
Dmitry Kovalev committed
13
#include "vp9/encoder/vp9_encoder.h"
14
#include "vp9/encoder/vp9_svc_layercontext.h"
15
#include "vp9/encoder/vp9_extend.h"
16 17

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
24 25
  svc->spatial_layer_id = 0;
  svc->temporal_layer_id = 0;
26

27
  if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
28
    layer_end = svc->number_temporal_layers;
29
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
30
    layer_end = svc->number_spatial_layers;
31 32 33
  }

  for (layer = 0; layer < layer_end; ++layer) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
34
    LAYER_CONTEXT *const lc = &svc->layer_context[layer];
35
    RATE_CONTROL *const lrc = &lc->rc;
36
    int i;
37
    lc->current_video_frame_in_layer = 0;
38
    lc->layer_size = 0;
39
    lc->frames_from_key_frame = 0;
40
    lc->last_frame_type = FRAME_TYPES;
41
    lrc->ni_av_qi = oxcf->worst_allowed_q;
42 43 44 45 46 47 48 49
    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;
50 51 52 53

    for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
      lrc->rate_correction_factors[i] = 1.0;
    }
54

55
    if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
56
      lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
57
      lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
58
      lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
59
    } else {
60
      lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
61 62
      lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
      lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
63 64 65 66 67 68 69 70
      lrc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
                                          oxcf->best_allowed_q) / 2;
      lrc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
                                            oxcf->best_allowed_q) / 2;
      if (oxcf->ss_play_alternate[layer])
        lc->alt_ref_idx = alt_ref_idx++;
      else
        lc->alt_ref_idx = -1;
71
      lc->gold_ref_idx = -1;
72 73
    }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
74 75
    lrc->buffer_level = oxcf->starting_buffer_level_ms *
                            lc->target_bandwidth / 1000;
76 77
    lrc->bits_off_target = lrc->buffer_level;
  }
78 79

  // Still have extra buffer for base layer golden frame
80 81
  if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
      && alt_ref_idx < REF_FRAMES)
82
    svc->layer_context[0].gold_ref_idx = alt_ref_idx;
83 84 85 86 87
}

// 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
88
  SVC *const svc = &cpi->svc;
89
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
90
  const RATE_CONTROL *const rc = &cpi->rc;
91
  int layer;
92
  int layer_end;
93
  float bitrate_alloc = 1.0;
94

95
  if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
96
    layer_end = svc->number_temporal_layers;
97
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
98
    layer_end = svc->number_spatial_layers;
99 100 101
  }

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

105
    if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
106
      lc->target_bandwidth = oxcf->ts_target_bitrate[layer];
107
    } else {
108
      lc->target_bandwidth = oxcf->ss_target_bitrate[layer];
109
    }
110
    bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
111
    // Update buffer-related quantities.
112 113 114 115 116 117
    lrc->starting_buffer_level =
        (int64_t)(rc->starting_buffer_level * bitrate_alloc);
    lrc->optimal_buffer_level =
        (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
    lrc->maximum_buffer_size =
        (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
118 119
    lrc->bits_off_target = MIN(lrc->bits_off_target, lrc->maximum_buffer_size);
    lrc->buffer_level = MIN(lrc->buffer_level, lrc->maximum_buffer_size);
120
    // Update framerate-related quantities.
121
    if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
122
      lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
123
    } else {
124
      lc->framerate = cpi->framerate;
125
    }
126
    lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
127 128 129 130 131 132 133
    lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
    // Update qp-related quantities.
    lrc->worst_quality = rc->worst_quality;
    lrc->best_quality = rc->best_quality;
  }
}

134 135 136 137
static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
  return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
         &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
         &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
138 139
}

140
void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
141
  SVC *const svc = &cpi->svc;
142
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
143
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
144
  RATE_CONTROL *const lrc = &lc->rc;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
145
  const int layer = svc->temporal_layer_id;
146

147
  lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
148
  lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
149 150
  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
  // Update the average layer frame size (non-cumulative per-frame-bw).
151
  if (layer == 0) {
152
    lc->avg_frame_size = lrc->avg_frame_bandwidth;
153
  } else {
154
    const double prev_layer_framerate =
155
        cpi->framerate / oxcf->ts_rate_decimator[layer - 1];
156
    const int prev_layer_target_bandwidth = oxcf->ts_target_bitrate[layer - 1];
157 158 159 160 161 162
    lc->avg_frame_size =
        (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
              (lc->framerate - prev_layer_framerate));
  }
}

163
void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
164
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
165
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
166 167 168
  RATE_CONTROL *const lrc = &lc->rc;

  lc->framerate = framerate;
169 170
  lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
  lrc->min_frame_bandwidth = (int)(lrc->avg_frame_bandwidth *
171
                                   oxcf->two_pass_vbrmin_section / 100);
172
  lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
173
                                   oxcf->two_pass_vbrmax_section) / 100);
174
  vp9_rc_set_gf_max_interval(cpi, lrc);
175 176
}

177
void vp9_restore_layer_context(VP9_COMP *const cpi) {
178
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
179 180 181
  const int old_frame_since_key = cpi->rc.frames_since_key;
  const int old_frame_to_key = cpi->rc.frames_to_key;

182
  cpi->rc = lc->rc;
183
  cpi->twopass = lc->twopass;
184
  cpi->oxcf.target_bandwidth = lc->target_bandwidth;
185
  cpi->alt_ref_source = lc->alt_ref_source;
186 187
  // 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).
188 189 190 191
  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;
  }
192 193 194
}

void vp9_save_layer_context(VP9_COMP *const cpi) {
195
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
196
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
197

198
  lc->rc = cpi->rc;
199
  lc->twopass = cpi->twopass;
200
  lc->target_bandwidth = (int)oxcf->target_bandwidth;
201
  lc->alt_ref_source = cpi->alt_ref_source;
202
}
203 204

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

Dmitry Kovalev's avatar
Dmitry Kovalev committed
208
  for (i = 0; i < svc->number_spatial_layers; ++i) {
Paul Wilkins's avatar
Paul Wilkins committed
209
    TWO_PASS *const twopass = &svc->layer_context[i].twopass;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
210 211

    svc->spatial_layer_id = i;
212 213 214 215 216
    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
217
  svc->spatial_layer_id = 0;
218
}
219

220 221 222 223 224
void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
  LAYER_CONTEXT *const lc =
      (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ?
      &cpi->svc.layer_context[cpi->svc.temporal_layer_id] :
      &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
225
  ++lc->current_video_frame_in_layer;
226
  ++lc->frames_from_key_frame;
227
}
228

229
int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
230
  return is_two_pass_svc(cpi) &&
231 232
         cpi->svc.spatial_layer_id > 0 &&
         cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame;
233
}
234

235
#if CONFIG_SPATIAL_SVC
236 237 238 239
static void get_layer_resolution(const int width_org, const int height_org,
                                 const int num, const int den,
                                 int *width_out, int *height_out) {
  int w, h;
240

241 242
  if (width_out == NULL || height_out == NULL || den == 0)
    return;
243

244 245
  w = width_org * num / den;
  h = height_org * num / den;
246

247 248 249
  // make height and width even to make chrome player happy
  w += w % 2;
  h += h % 2;
250

251 252
  *width_out = w;
  *height_out = h;
253 254
}

255 256
int vp9_svc_start_frame(VP9_COMP *const cpi) {
  int width = 0, height = 0;
257
  LAYER_CONTEXT *lc;
258
  int count = 1 << (cpi->svc.number_temporal_layers - 1);
259

260
  cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
261
  lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
262

263 264 265 266 267 268
  cpi->svc.temporal_layer_id = 0;
  while ((lc->current_video_frame_in_layer % count) != 0) {
    ++cpi->svc.temporal_layer_id;
    count >>= 1;
  }

269 270
  cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;

271 272 273 274 275 276 277
  cpi->lst_fb_idx = cpi->svc.spatial_layer_id;

  if (cpi->svc.spatial_layer_id == 0)
    cpi->gld_fb_idx = (lc->gold_ref_idx >= 0) ?
                      lc->gold_ref_idx : cpi->lst_fb_idx;
  else
    cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
278 279

  if (lc->current_video_frame_in_layer == 0) {
280
    if (cpi->svc.spatial_layer_id >= 2) {
281
      cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
282
    } else {
283
      cpi->alt_fb_idx = cpi->lst_fb_idx;
284 285
      cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
    }
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  } else {
    if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id]) {
      cpi->alt_fb_idx = lc->alt_ref_idx;
      if (!lc->has_alt_frame)
        cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
    } else {
      // Find a proper alt_fb_idx for layers that don't have alt ref frame
      if (cpi->svc.spatial_layer_id == 0) {
        cpi->alt_fb_idx = cpi->lst_fb_idx;
      } else {
        LAYER_CONTEXT *lc_lower =
            &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];

        if (cpi->oxcf.ss_play_alternate[cpi->svc.spatial_layer_id - 1] &&
            lc_lower->alt_ref_source != NULL)
          cpi->alt_fb_idx = lc_lower->alt_ref_idx;
        else if (cpi->svc.spatial_layer_id >= 2)
303
          cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
304 305 306 307 308
        else
          cpi->alt_fb_idx = cpi->lst_fb_idx;
      }
    }
  }
309

310 311 312 313
  get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
                       lc->scaling_factor_num, lc->scaling_factor_den,
                       &width, &height);
  if (vp9_set_size_literal(cpi, width, height) != 0)
314 315
    return VPX_CODEC_INVALID_PARAM;

316 317
  cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
  cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
318 319 320 321 322

  vp9_change_config(cpi, &cpi->oxcf);

  vp9_set_high_precision_mv(cpi, 1);

323
  cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
324 325 326 327 328 329 330 331 332 333

  return 0;
}

struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
                                              struct lookahead_ctx *ctx,
                                              int drain) {
  struct lookahead_entry *buf = NULL;

  if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
334
    buf = vp9_lookahead_peek(ctx, 0);
335
    if (buf != NULL) {
336
      // Only remove the buffer when pop the highest layer.
337 338 339 340 341 342 343 344
      if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
        vp9_lookahead_pop(ctx, drain);
      }
    }
  }

  return buf;
}
345
#endif