Coverage Report

Created: 2026-05-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/encoder/vp9_svc_layercontext.c
Line
Count
Source
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_aq_cyclicrefresh.h"
14
#include "vp9/encoder/vp9_encoder.h"
15
#include "vp9/encoder/vp9_svc_layercontext.h"
16
#include "vp9/encoder/vp9_extend.h"
17
#include "vpx_dsp/vpx_dsp_common.h"
18
19
0
#define SMALL_FRAME_WIDTH 32
20
0
#define SMALL_FRAME_HEIGHT 16
21
22
0
static void swap_ptr(void *a, void *b) {
23
0
  void **a_p = (void **)a;
24
0
  void **b_p = (void **)b;
25
0
  void *c = *a_p;
26
0
  *a_p = *b_p;
27
0
  *b_p = c;
28
0
}
29
30
0
void vp9_init_layer_context(VP9_COMP *const cpi) {
31
0
  SVC *const svc = &cpi->svc;
32
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
33
0
  int mi_rows = cpi->common.mi_rows;
34
0
  int mi_cols = cpi->common.mi_cols;
35
0
  int sl, tl, i;
36
0
  int alt_ref_idx = svc->number_spatial_layers;
37
38
0
  svc->spatial_layer_id = 0;
39
0
  svc->temporal_layer_id = 0;
40
0
  svc->force_zero_mode_spatial_ref = 0;
41
0
  svc->use_base_mv = 0;
42
0
  svc->use_partition_reuse = 0;
43
0
  svc->use_gf_temporal_ref = 1;
44
0
  svc->use_gf_temporal_ref_current_layer = 0;
45
0
  svc->scaled_temp_is_alloc = 0;
46
0
  svc->scaled_one_half = 0;
47
0
  svc->current_superframe = 0;
48
0
  svc->non_reference_frame = 0;
49
0
  svc->skip_enhancement_layer = 0;
50
0
  svc->disable_inter_layer_pred = INTER_LAYER_PRED_ON;
51
0
  svc->framedrop_mode = CONSTRAINED_LAYER_DROP;
52
0
  svc->set_intra_only_frame = 0;
53
0
  svc->previous_frame_is_intra_only = 0;
54
0
  svc->superframe_has_layer_sync = 0;
55
0
  svc->use_set_ref_frame_config = 0;
56
0
  svc->num_encoded_top_layer = 0;
57
0
  svc->simulcast_mode = 0;
58
0
  svc->single_layer_svc = 0;
59
0
  svc->resize_set = 0;
60
61
0
  for (i = 0; i < REF_FRAMES; ++i) {
62
0
    svc->fb_idx_spatial_layer_id[i] = 0xff;
63
0
    svc->fb_idx_temporal_layer_id[i] = 0xff;
64
0
    svc->fb_idx_base[i] = 0;
65
0
  }
66
0
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
67
0
    svc->last_layer_dropped[sl] = 0;
68
0
    svc->drop_spatial_layer[sl] = 0;
69
0
    svc->ext_frame_flags[sl] = 0;
70
0
    svc->lst_fb_idx[sl] = 0;
71
0
    svc->gld_fb_idx[sl] = 1;
72
0
    svc->alt_fb_idx[sl] = 2;
73
0
    svc->downsample_filter_type[sl] = BILINEAR;
74
0
    svc->downsample_filter_phase[sl] = 8;  // Set to 8 for averaging filter.
75
0
    svc->framedrop_thresh[sl] = oxcf->drop_frames_water_mark;
76
0
    svc->fb_idx_upd_tl0[sl] = -1;
77
0
    svc->drop_count[sl] = 0;
78
0
    svc->spatial_layer_sync[sl] = 0;
79
0
    svc->force_drop_constrained_from_above[sl] = 0;
80
0
  }
81
0
  svc->max_consec_drop = INT_MAX;
82
83
0
  svc->buffer_gf_temporal_ref[1].idx = 7;
84
0
  svc->buffer_gf_temporal_ref[0].idx = 6;
85
0
  svc->buffer_gf_temporal_ref[1].is_used = 0;
86
0
  svc->buffer_gf_temporal_ref[0].is_used = 0;
87
88
0
  if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
89
0
    if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
90
0
                                 SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
91
0
                                 cpi->common.subsampling_y,
92
0
#if CONFIG_VP9_HIGHBITDEPTH
93
0
                                 cpi->common.use_highbitdepth,
94
0
#endif
95
0
                                 VP9_ENC_BORDER_IN_PIXELS,
96
0
                                 cpi->common.byte_alignment, NULL, NULL, NULL))
97
0
      vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
98
0
                         "Failed to allocate empty frame for multiple frame "
99
0
                         "contexts");
100
101
0
    memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
102
0
           cpi->svc.empty_frame.img.buffer_alloc_sz);
103
0
  }
104
105
0
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
106
0
    for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
107
0
      int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
108
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
109
0
      RATE_CONTROL *const lrc = &lc->rc;
110
0
      lc->current_video_frame_in_layer = 0;
111
0
      lc->layer_size = 0;
112
0
      lc->frames_from_key_frame = 0;
113
0
      lc->last_frame_type = FRAME_TYPES;
114
0
      lrc->ni_av_qi = oxcf->worst_allowed_q;
115
0
      lrc->total_actual_bits = 0;
116
0
      lrc->total_target_vs_actual = 0;
117
0
      lrc->ni_tot_qi = 0;
118
0
      lrc->tot_q = 0.0;
119
0
      lrc->avg_q = 0.0;
120
0
      lrc->ni_frames = 0;
121
0
      lrc->decimation_count = 0;
122
0
      lrc->decimation_factor = 0;
123
0
      lrc->worst_quality = oxcf->worst_allowed_q;
124
0
      lrc->best_quality = oxcf->best_allowed_q;
125
126
0
      for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
127
0
        lrc->rate_correction_factors[i] = 1.0;
128
0
      }
129
130
0
      if (cpi->oxcf.rc_mode == VPX_CBR) {
131
0
        lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
132
0
        lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
133
0
        lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
134
0
        lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
135
0
      } else {
136
0
        lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
137
0
        lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
138
0
        lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
139
0
        lrc->avg_frame_qindex[KEY_FRAME] =
140
0
            (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
141
0
        lrc->avg_frame_qindex[INTER_FRAME] =
142
0
            (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
143
0
        if (oxcf->ss_enable_auto_arf[sl])
144
0
          lc->alt_ref_idx = alt_ref_idx++;
145
0
        else
146
0
          lc->alt_ref_idx = INVALID_IDX;
147
0
        lc->gold_ref_idx = INVALID_IDX;
148
0
      }
149
150
0
      lrc->buffer_level =
151
0
          oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
152
0
      lrc->bits_off_target = lrc->buffer_level;
153
154
      // Initialize the cyclic refresh parameters. If spatial layers are used
155
      // (i.e., ss_number_layers > 1), these need to be updated per spatial
156
      // layer.
157
      // Cyclic refresh is only applied on base temporal layer.
158
0
      if (oxcf->ss_number_layers > 1 && tl == 0) {
159
0
        size_t last_coded_q_map_size;
160
0
        size_t consec_zero_mv_size;
161
0
        VP9_COMMON *const cm = &cpi->common;
162
0
        lc->sb_index = 0;
163
0
        lc->actual_num_seg1_blocks = 0;
164
0
        lc->actual_num_seg2_blocks = 0;
165
0
        lc->counter_encode_maxq_scene_change = 0;
166
0
        CHECK_MEM_ERROR(&cm->error, lc->map,
167
0
                        vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
168
0
        memset(lc->map, 0, mi_rows * mi_cols);
169
0
        last_coded_q_map_size =
170
0
            mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
171
0
        CHECK_MEM_ERROR(&cm->error, lc->last_coded_q_map,
172
0
                        vpx_malloc(last_coded_q_map_size));
173
0
        assert(MAXQ <= 255);
174
0
        memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
175
0
        consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
176
0
        CHECK_MEM_ERROR(&cm->error, lc->consec_zero_mv,
177
0
                        vpx_malloc(consec_zero_mv_size));
178
0
        memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
179
0
      }
180
0
    }
181
0
  }
182
183
  // Still have extra buffer for base layer golden frame
184
0
  if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
185
0
      alt_ref_idx < REF_FRAMES)
186
0
    svc->layer_context[0].gold_ref_idx = alt_ref_idx;
187
0
}
188
189
// Update the layer context from a change_config() call.
190
void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
191
0
                                            const int target_bandwidth) {
192
0
  SVC *const svc = &cpi->svc;
193
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
194
0
  const RATE_CONTROL *const rc = &cpi->rc;
195
0
  int sl, tl, layer = 0, spatial_layer_target;
196
0
  float bitrate_alloc = 1.0;
197
0
  int num_spatial_layers_nonzero_rate = 0;
198
199
0
  cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
200
201
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
202
0
    for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
203
0
      for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
204
0
        layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
205
0
        svc->layer_context[layer].target_bandwidth =
206
0
            oxcf->layer_target_bitrate[layer];
207
0
      }
208
209
0
      layer = LAYER_IDS_TO_IDX(
210
0
          sl,
211
0
          ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
212
0
          oxcf->ts_number_layers);
213
0
      spatial_layer_target = svc->layer_context[layer].target_bandwidth =
214
0
          oxcf->layer_target_bitrate[layer];
215
216
0
      for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
217
0
        LAYER_CONTEXT *const lc =
218
0
            &svc->layer_context[sl * oxcf->ts_number_layers + tl];
219
0
        RATE_CONTROL *const lrc = &lc->rc;
220
221
0
        lc->spatial_layer_target_bandwidth = spatial_layer_target;
222
0
        if (target_bandwidth != 0) {
223
0
          bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
224
0
        }
225
0
        lrc->starting_buffer_level =
226
0
            (int64_t)(rc->starting_buffer_level * bitrate_alloc + 0.5);
227
0
        lrc->optimal_buffer_level =
228
0
            (int64_t)(rc->optimal_buffer_level * bitrate_alloc + 0.5);
229
0
        lrc->maximum_buffer_size =
230
0
            (int64_t)(rc->maximum_buffer_size * bitrate_alloc + 0.5);
231
0
        lrc->bits_off_target =
232
0
            VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
233
0
        lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
234
0
        lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
235
0
        lrc->avg_frame_bandwidth = saturate_cast_double_to_int(
236
0
            round(lc->target_bandwidth / lc->framerate));
237
0
        lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
238
0
        lrc->worst_quality = rc->worst_quality;
239
0
        lrc->best_quality = rc->best_quality;
240
0
      }
241
0
    }
242
0
  } else {
243
0
    assert(svc->number_temporal_layers == 1);
244
0
    int layer_end = svc->number_spatial_layers;
245
0
    for (layer = 0; layer < layer_end; ++layer) {
246
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
247
0
      RATE_CONTROL *const lrc = &lc->rc;
248
249
0
      lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
250
251
0
      if (target_bandwidth != 0) {
252
0
        bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
253
0
      }
254
      // Update buffer-related quantities.
255
0
      lrc->starting_buffer_level =
256
0
          (int64_t)(rc->starting_buffer_level * bitrate_alloc);
257
0
      lrc->optimal_buffer_level =
258
0
          (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
259
0
      lrc->maximum_buffer_size =
260
0
          (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
261
0
      lrc->bits_off_target =
262
0
          VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
263
0
      lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
264
0
      lc->framerate = cpi->framerate;
265
0
      lrc->avg_frame_bandwidth = saturate_cast_double_to_int(
266
0
          round(lc->target_bandwidth / lc->framerate));
267
0
      lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
268
      // Update qp-related quantities.
269
0
      lrc->worst_quality = rc->worst_quality;
270
0
      lrc->best_quality = rc->best_quality;
271
0
    }
272
0
  }
273
0
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
274
    // Check bitrate of spatia layer.
275
0
    layer = LAYER_IDS_TO_IDX(sl, oxcf->ts_number_layers - 1,
276
0
                             oxcf->ts_number_layers);
277
0
    if (oxcf->layer_target_bitrate[layer] > 0)
278
0
      num_spatial_layers_nonzero_rate += 1;
279
0
  }
280
0
  if (num_spatial_layers_nonzero_rate == 1)
281
0
    svc->single_layer_svc = 1;
282
0
  else
283
0
    svc->single_layer_svc = 0;
284
0
}
285
286
0
static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
287
0
  if (is_one_pass_svc(cpi))
288
0
    return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
289
0
                                       cpi->svc.number_temporal_layers +
290
0
                                   cpi->svc.temporal_layer_id];
291
0
  else
292
0
    return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
293
0
               ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
294
0
               : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
295
0
}
296
297
0
void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
298
0
  SVC *const svc = &cpi->svc;
299
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
300
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
301
0
  RATE_CONTROL *const lrc = &lc->rc;
302
  // Index into spatial+temporal arrays.
303
0
  const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
304
0
                     svc->temporal_layer_id;
305
0
  const int tl = svc->temporal_layer_id;
306
307
0
  lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
308
0
  lrc->avg_frame_bandwidth =
309
0
      saturate_cast_double_to_int(round(lc->target_bandwidth / lc->framerate));
310
0
  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
311
  // Update the average layer frame size (non-cumulative per-frame-bw).
312
0
  if (tl == 0) {
313
0
    lc->avg_frame_size = lrc->avg_frame_bandwidth;
314
0
  } else {
315
0
    const double prev_layer_framerate =
316
0
        cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
317
0
    const int prev_layer_target_bandwidth =
318
0
        oxcf->layer_target_bitrate[st_idx - 1];
319
0
    lc->avg_frame_size =
320
0
        (int)round((lc->target_bandwidth - prev_layer_target_bandwidth) /
321
0
                   (lc->framerate - prev_layer_framerate));
322
0
  }
323
0
}
324
325
0
void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
326
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
327
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
328
0
  RATE_CONTROL *const lrc = &lc->rc;
329
330
0
  lc->framerate = framerate;
331
0
  lrc->avg_frame_bandwidth =
332
0
      saturate_cast_double_to_int(round(lc->target_bandwidth / lc->framerate));
333
0
  const int64_t vbr_min_bits =
334
0
      (int64_t)lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100;
335
0
  lrc->min_frame_bandwidth = (int)VPXMIN(vbr_min_bits, INT_MAX);
336
0
  const int64_t vbr_max_bits =
337
0
      (int64_t)lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section / 100;
338
0
  lrc->max_frame_bandwidth = (int)VPXMIN(vbr_max_bits, INT_MAX);
339
0
  vp9_rc_set_gf_interval_range(cpi, lrc);
340
0
}
341
342
0
void vp9_restore_layer_context(VP9_COMP *const cpi) {
343
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
344
0
  const int old_frame_since_key = cpi->rc.frames_since_key;
345
0
  const int old_frame_to_key = cpi->rc.frames_to_key;
346
0
  const int old_ext_use_post_encode_drop = cpi->rc.ext_use_post_encode_drop;
347
348
0
  cpi->rc = lc->rc;
349
0
  cpi->twopass = lc->twopass;
350
0
  cpi->oxcf.target_bandwidth = lc->target_bandwidth;
351
0
  cpi->alt_ref_source = lc->alt_ref_source;
352
  // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
353
  // does not use speed = 0).
354
0
  if (is_one_pass_svc(cpi) && lc->speed > 0) {
355
#if CONFIG_REALTIME_ONLY
356
    cpi->oxcf.speed = VPXMAX(lc->speed, 5);
357
#else
358
0
    cpi->oxcf.speed = lc->speed;
359
0
#endif
360
0
  }
361
0
  cpi->loopfilter_ctrl = lc->loopfilter_ctrl;
362
  // Reset the frames_since_key and frames_to_key counters to their values
363
  // before the layer restore. Keep these defined for the stream (not layer).
364
0
  if (cpi->svc.number_temporal_layers > 1 ||
365
0
      cpi->svc.number_spatial_layers > 1) {
366
0
    cpi->rc.frames_since_key = old_frame_since_key;
367
0
    cpi->rc.frames_to_key = old_frame_to_key;
368
0
  }
369
0
  cpi->rc.ext_use_post_encode_drop = old_ext_use_post_encode_drop;
370
  // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
371
  // for the base temporal layer.
372
0
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
373
0
      cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
374
0
    CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
375
0
    swap_ptr(&cr->map, &lc->map);
376
0
    swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
377
0
    swap_ptr(&cpi->consec_zero_mv, &lc->consec_zero_mv);
378
0
    cr->sb_index = lc->sb_index;
379
0
    cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
380
0
    cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
381
0
    cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
382
0
  }
383
0
}
384
385
0
void vp9_save_layer_context(VP9_COMP *const cpi) {
386
0
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
387
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
388
389
0
  lc->rc = cpi->rc;
390
0
  lc->twopass = cpi->twopass;
391
0
  lc->target_bandwidth = (int)oxcf->target_bandwidth;
392
0
  lc->alt_ref_source = cpi->alt_ref_source;
393
0
  lc->frame_qp = cpi->common.base_qindex;
394
0
  lc->MBs = cpi->common.MBs;
395
396
  // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
397
  // for the base temporal layer.
398
0
  if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
399
0
      cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
400
0
    CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
401
0
    signed char *temp = lc->map;
402
0
    uint8_t *temp2 = lc->last_coded_q_map;
403
0
    uint8_t *temp3 = lc->consec_zero_mv;
404
0
    lc->map = cr->map;
405
0
    cr->map = temp;
406
0
    lc->last_coded_q_map = cr->last_coded_q_map;
407
0
    cr->last_coded_q_map = temp2;
408
0
    lc->consec_zero_mv = cpi->consec_zero_mv;
409
0
    cpi->consec_zero_mv = temp3;
410
0
    lc->sb_index = cr->sb_index;
411
0
    lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
412
0
    lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
413
0
    lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
414
0
    lc->qindex_delta[0] = cr->qindex_delta[0];
415
0
    lc->qindex_delta[1] = cr->qindex_delta[1];
416
0
    lc->qindex_delta[2] = cr->qindex_delta[2];
417
0
  }
418
0
}
419
420
#if !CONFIG_REALTIME_ONLY
421
0
void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
422
0
  SVC *const svc = &cpi->svc;
423
0
  int i;
424
425
0
  for (i = 0; i < svc->number_spatial_layers; ++i) {
426
0
    TWO_PASS *const twopass = &svc->layer_context[i].twopass;
427
428
0
    svc->spatial_layer_id = i;
429
0
    vp9_init_second_pass(cpi);
430
431
0
    twopass->total_stats.spatial_layer_id = i;
432
0
    twopass->total_left_stats.spatial_layer_id = i;
433
0
  }
434
0
  svc->spatial_layer_id = 0;
435
0
}
436
#endif  // !CONFIG_REALTIME_ONLY
437
438
0
void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
439
0
  LAYER_CONTEXT *const lc =
440
0
      &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
441
0
                              cpi->svc.number_temporal_layers];
442
0
  ++lc->current_video_frame_in_layer;
443
0
  ++lc->frames_from_key_frame;
444
0
  if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
445
0
    ++cpi->svc.current_superframe;
446
0
}
447
448
void get_layer_resolution(const int width_org, const int height_org,
449
                          const int num, const int den, int *width_out,
450
0
                          int *height_out) {
451
0
  int w, h;
452
453
0
  if (width_out == NULL || height_out == NULL) return;
454
455
0
  if (den == 0 || num == 0) {
456
0
    *width_out = width_org;
457
0
    *height_out = height_org;
458
0
    return;
459
0
  }
460
461
0
  w = (int)((int64_t)width_org * num / den);
462
0
  h = (int)((int64_t)height_org * num / den);
463
464
  // make height and width even to make chrome player happy
465
0
  w += w % 2;
466
0
  h += h % 2;
467
468
0
  *width_out = w;
469
0
  *height_out = h;
470
0
}
471
472
0
static void reset_fb_idx_unused(VP9_COMP *const cpi) {
473
  // If a reference frame is not referenced or refreshed, then set the
474
  // fb_idx for that reference to the first one used/referenced.
475
  // This is to avoid setting fb_idx for a reference to a slot that is not
476
  // used/needed (i.e., since that reference is not referenced or refreshed).
477
0
  MV_REFERENCE_FRAME ref_frame;
478
0
  MV_REFERENCE_FRAME first_ref = 0;
479
0
  int first_fb_idx = 0;
480
0
  int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
481
0
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
482
0
    if (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) {
483
0
      first_ref = ref_frame;
484
0
      first_fb_idx = fb_idx[ref_frame - 1];
485
0
      break;
486
0
    }
487
0
  }
488
0
  if (first_ref > 0) {
489
0
    if (first_ref != LAST_FRAME && !(cpi->ref_frame_flags & VP9_LAST_FLAG) &&
490
0
        !cpi->ext_refresh_last_frame)
491
0
      cpi->lst_fb_idx = first_fb_idx;
492
0
    else if (first_ref != GOLDEN_FRAME &&
493
0
             !(cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
494
0
             !cpi->ext_refresh_golden_frame)
495
0
      cpi->gld_fb_idx = first_fb_idx;
496
0
    else if (first_ref != ALTREF_FRAME &&
497
0
             !(cpi->ref_frame_flags & VP9_ALT_FLAG) &&
498
0
             !cpi->ext_refresh_alt_ref_frame)
499
0
      cpi->alt_fb_idx = first_fb_idx;
500
0
  }
501
0
}
502
503
// Never refresh any reference frame buffers on top temporal layers in
504
// simulcast mode, which has interlayer prediction disabled.
505
0
static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
506
0
  if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
507
0
      cpi->svc.temporal_layer_id > 0) {
508
0
    cpi->ext_refresh_last_frame = 0;
509
0
    cpi->ext_refresh_golden_frame = 0;
510
0
    cpi->ext_refresh_alt_ref_frame = 0;
511
0
  }
512
0
}
513
514
// The function sets proper ref_frame_flags, buffer indices, and buffer update
515
// variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
516
// scheme.
517
0
static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
518
0
  int frame_num_within_temporal_struct = 0;
519
0
  int spatial_id, temporal_id;
520
0
  spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
521
0
  frame_num_within_temporal_struct =
522
0
      cpi->svc
523
0
          .layer_context[cpi->svc.spatial_layer_id *
524
0
                         cpi->svc.number_temporal_layers]
525
0
          .current_video_frame_in_layer %
526
0
      4;
527
0
  temporal_id = cpi->svc.temporal_layer_id =
528
0
      (frame_num_within_temporal_struct & 1)
529
0
          ? 2
530
0
          : (frame_num_within_temporal_struct >> 1);
531
0
  cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
532
0
      cpi->ext_refresh_alt_ref_frame = 0;
533
0
  if (!temporal_id) {
534
0
    cpi->ext_refresh_frame_flags_pending = 1;
535
0
    cpi->ext_refresh_last_frame = 1;
536
0
    if (!spatial_id) {
537
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
538
0
    } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
539
      // base layer is a key frame.
540
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
541
0
      cpi->ext_refresh_last_frame = 0;
542
0
      cpi->ext_refresh_golden_frame = 1;
543
0
    } else {
544
0
      cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
545
0
    }
546
0
  } else if (temporal_id == 1) {
547
0
    cpi->ext_refresh_frame_flags_pending = 1;
548
0
    cpi->ext_refresh_alt_ref_frame = 1;
549
0
    if (!spatial_id) {
550
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
551
0
    } else {
552
0
      cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
553
0
    }
554
0
  } else {
555
0
    if (frame_num_within_temporal_struct == 1) {
556
      // the first tl2 picture
557
0
      if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
558
0
        cpi->ext_refresh_frame_flags_pending = 1;
559
0
        if (!spatial_id)
560
0
          cpi->ref_frame_flags = VP9_LAST_FLAG;
561
0
        else
562
0
          cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
563
0
      } else if (!spatial_id) {
564
0
        cpi->ext_refresh_frame_flags_pending = 1;
565
0
        cpi->ext_refresh_alt_ref_frame = 1;
566
0
        cpi->ref_frame_flags = VP9_LAST_FLAG;
567
0
      } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
568
0
        cpi->ext_refresh_frame_flags_pending = 1;
569
0
        cpi->ext_refresh_alt_ref_frame = 1;
570
0
        cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
571
0
      }
572
0
    } else {
573
      //  The second tl2 picture
574
0
      if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
575
0
        cpi->ext_refresh_frame_flags_pending = 1;
576
0
        if (!spatial_id)
577
0
          cpi->ref_frame_flags = VP9_LAST_FLAG;
578
0
        else
579
0
          cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
580
0
      } else if (!spatial_id) {
581
0
        cpi->ext_refresh_frame_flags_pending = 1;
582
0
        cpi->ref_frame_flags = VP9_LAST_FLAG;
583
0
        cpi->ext_refresh_alt_ref_frame = 1;
584
0
      } else {  // top layer
585
0
        cpi->ext_refresh_frame_flags_pending = 1;
586
0
        cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
587
0
        cpi->ext_refresh_alt_ref_frame = 1;
588
0
      }
589
0
    }
590
0
  }
591
0
  if (temporal_id == 0) {
592
0
    cpi->lst_fb_idx = spatial_id;
593
0
    if (spatial_id) {
594
0
      if (cpi->svc.layer_context[temporal_id].is_key_frame) {
595
0
        cpi->lst_fb_idx = spatial_id - 1;
596
0
        cpi->gld_fb_idx = spatial_id;
597
0
      } else {
598
0
        cpi->gld_fb_idx = spatial_id - 1;
599
0
      }
600
0
    } else {
601
0
      cpi->gld_fb_idx = 0;
602
0
    }
603
0
    cpi->alt_fb_idx = 0;
604
0
  } else if (temporal_id == 1) {
605
0
    cpi->lst_fb_idx = spatial_id;
606
0
    cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
607
0
    cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
608
0
  } else if (frame_num_within_temporal_struct == 1) {
609
0
    cpi->lst_fb_idx = spatial_id;
610
0
    cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
611
0
    cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
612
0
  } else {
613
0
    cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
614
0
    cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
615
0
    cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
616
0
  }
617
618
0
  if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
619
620
0
  reset_fb_idx_unused(cpi);
621
0
}
622
623
// The function sets proper ref_frame_flags, buffer indices, and buffer update
624
// variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
625
// scheme.
626
0
static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
627
0
  int spatial_id, temporal_id;
628
0
  spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
629
0
  temporal_id = cpi->svc.temporal_layer_id =
630
0
      cpi->svc
631
0
          .layer_context[cpi->svc.spatial_layer_id *
632
0
                         cpi->svc.number_temporal_layers]
633
0
          .current_video_frame_in_layer &
634
0
      1;
635
0
  cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
636
0
      cpi->ext_refresh_alt_ref_frame = 0;
637
0
  if (!temporal_id) {
638
0
    cpi->ext_refresh_frame_flags_pending = 1;
639
0
    cpi->ext_refresh_last_frame = 1;
640
0
    if (!spatial_id) {
641
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
642
0
    } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
643
      // base layer is a key frame.
644
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
645
0
      cpi->ext_refresh_last_frame = 0;
646
0
      cpi->ext_refresh_golden_frame = 1;
647
0
    } else {
648
0
      cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
649
0
    }
650
0
  } else if (temporal_id == 1) {
651
0
    cpi->ext_refresh_frame_flags_pending = 1;
652
0
    cpi->ext_refresh_alt_ref_frame = 1;
653
0
    if (!spatial_id) {
654
0
      cpi->ref_frame_flags = VP9_LAST_FLAG;
655
0
    } else {
656
0
      if (spatial_id == cpi->svc.number_spatial_layers - 1)
657
0
        cpi->ext_refresh_alt_ref_frame = 0;
658
0
      cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
659
0
    }
660
0
  }
661
662
0
  if (temporal_id == 0) {
663
0
    cpi->lst_fb_idx = spatial_id;
664
0
    if (spatial_id) {
665
0
      if (cpi->svc.layer_context[temporal_id].is_key_frame) {
666
0
        cpi->lst_fb_idx = spatial_id - 1;
667
0
        cpi->gld_fb_idx = spatial_id;
668
0
      } else {
669
0
        cpi->gld_fb_idx = spatial_id - 1;
670
0
      }
671
0
    } else {
672
0
      cpi->gld_fb_idx = 0;
673
0
    }
674
0
    cpi->alt_fb_idx = 0;
675
0
  } else if (temporal_id == 1) {
676
0
    cpi->lst_fb_idx = spatial_id;
677
0
    cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
678
0
    cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
679
0
  }
680
681
0
  if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
682
683
0
  reset_fb_idx_unused(cpi);
684
0
}
685
686
// The function sets proper ref_frame_flags, buffer indices, and buffer update
687
// variables for temporal layering mode 0 - that has no temporal layering.
688
static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
689
0
    VP9_COMP *const cpi) {
690
0
  int spatial_id;
691
0
  spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
692
0
  cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
693
0
      cpi->ext_refresh_alt_ref_frame = 0;
694
0
  cpi->ext_refresh_frame_flags_pending = 1;
695
0
  cpi->ext_refresh_last_frame = 1;
696
0
  if (!spatial_id) {
697
0
    cpi->ref_frame_flags = VP9_LAST_FLAG;
698
0
  } else if (cpi->svc.layer_context[0].is_key_frame) {
699
0
    cpi->ref_frame_flags = VP9_LAST_FLAG;
700
0
    cpi->ext_refresh_last_frame = 0;
701
0
    cpi->ext_refresh_golden_frame = 1;
702
0
  } else {
703
0
    cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
704
0
  }
705
0
  cpi->lst_fb_idx = spatial_id;
706
0
  if (spatial_id) {
707
0
    if (cpi->svc.layer_context[0].is_key_frame) {
708
0
      cpi->lst_fb_idx = spatial_id - 1;
709
0
      cpi->gld_fb_idx = spatial_id;
710
0
    } else {
711
0
      cpi->gld_fb_idx = spatial_id - 1;
712
0
    }
713
0
  } else {
714
0
    cpi->gld_fb_idx = 0;
715
0
  }
716
717
0
  if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
718
719
0
  reset_fb_idx_unused(cpi);
720
0
}
721
722
static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
723
0
    VP9_COMP *const cpi) {
724
0
  SVC *const svc = &cpi->svc;
725
0
  int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
726
0
  cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
727
0
  cpi->ext_refresh_frame_flags_pending = 1;
728
0
  cpi->lst_fb_idx = svc->lst_fb_idx[sl];
729
0
  cpi->gld_fb_idx = svc->gld_fb_idx[sl];
730
0
  cpi->alt_fb_idx = svc->alt_fb_idx[sl];
731
0
  cpi->ext_refresh_last_frame = 0;
732
0
  cpi->ext_refresh_golden_frame = 0;
733
0
  cpi->ext_refresh_alt_ref_frame = 0;
734
0
  cpi->ref_frame_flags = 0;
735
0
  if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
736
0
  if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
737
0
  if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
738
0
}
739
740
0
void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
741
0
  SVC *const svc = &cpi->svc;
742
0
  int sl = svc->spatial_layer_id;
743
0
  svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
744
0
  svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
745
0
  svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
746
  // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
747
  // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
748
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
749
0
    int ref;
750
0
    for (ref = 0; ref < REF_FRAMES; ++ref) {
751
0
      svc->update_buffer_slot[sl] &= ~(1 << ref);
752
0
      if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
753
0
          (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
754
0
          (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
755
0
        svc->update_buffer_slot[sl] |= (1 << ref);
756
0
    }
757
0
  }
758
759
  // TODO(jianj): Remove these 3, deprecated.
760
0
  svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
761
0
  svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
762
0
  svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
763
764
0
  svc->reference_last[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_LAST_FLAG);
765
0
  svc->reference_golden[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_GOLD_FLAG);
766
0
  svc->reference_altref[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_ALT_FLAG);
767
0
}
768
769
0
int vp9_one_pass_svc_start_layer(VP9_COMP *const cpi) {
770
0
  int width = 0, height = 0;
771
0
  SVC *const svc = &cpi->svc;
772
0
  LAYER_CONTEXT *lc = NULL;
773
0
  int scaling_factor_num = 1;
774
0
  int scaling_factor_den = 1;
775
0
  svc->skip_enhancement_layer = 0;
776
777
0
  if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
778
0
      svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
779
0
      svc->number_temporal_layers <= 3)
780
0
    svc->simulcast_mode = 1;
781
0
  else
782
0
    svc->simulcast_mode = 0;
783
784
0
  if (svc->number_spatial_layers > 1) {
785
0
    svc->use_base_mv = 1;
786
0
    svc->use_partition_reuse = 1;
787
0
  }
788
0
  svc->force_zero_mode_spatial_ref = 1;
789
790
  // For constrained_from_above drop mode: before encoding superframe (i.e.,
791
  // at SL0 frame) check all spatial layers (starting from top) for possible
792
  // drop, and if so, set a flag to force drop of that layer and all its lower
793
  // layers.
794
0
  if (svc->spatial_layer_to_encode == svc->first_spatial_layer_to_encode) {
795
0
    int sl;
796
0
    for (sl = 0; sl < svc->number_spatial_layers; sl++)
797
0
      svc->force_drop_constrained_from_above[sl] = 0;
798
0
    if (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP) {
799
0
      for (sl = svc->number_spatial_layers - 1;
800
0
           sl >= svc->first_spatial_layer_to_encode; sl--) {
801
0
        int layer = sl * svc->number_temporal_layers + svc->temporal_layer_id;
802
0
        LAYER_CONTEXT *const sl_lc = &svc->layer_context[layer];
803
0
        cpi->rc = sl_lc->rc;
804
0
        cpi->oxcf.target_bandwidth = sl_lc->target_bandwidth;
805
0
        if (vp9_test_drop(cpi)) {
806
0
          int sl2;
807
          // Set flag to force drop in encoding for this mode.
808
0
          for (sl2 = sl; sl2 >= svc->first_spatial_layer_to_encode; sl2--)
809
0
            svc->force_drop_constrained_from_above[sl2] = 1;
810
0
          break;
811
0
        }
812
0
      }
813
0
    }
814
0
  }
815
816
0
  if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
817
0
    set_flags_and_fb_idx_for_temporal_mode3(cpi);
818
0
  } else if (svc->temporal_layering_mode ==
819
0
             VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
820
0
    set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
821
0
  } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
822
0
    set_flags_and_fb_idx_for_temporal_mode2(cpi);
823
0
  } else if (svc->temporal_layering_mode ==
824
0
                 VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
825
0
             svc->use_set_ref_frame_config) {
826
0
    set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
827
0
  }
828
829
0
  if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
830
0
      cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
831
0
      cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
832
0
    svc->buffer_gf_temporal_ref[0].is_used = 1;
833
0
  if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
834
0
      cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
835
0
      cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
836
0
    svc->buffer_gf_temporal_ref[1].is_used = 1;
837
838
  // For the fixed (non-flexible/bypass) SVC mode:
839
  // If long term temporal reference is enabled at the sequence level
840
  // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
841
  // we can use golden as a second temporal reference
842
  // (since the spatial/inter-layer reference is disabled).
843
  // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
844
  // unused (slot 7 and 6 should be available for 3-3 layer system).
845
  // For now usage of this second temporal reference will only be used for
846
  // highest and next to highest spatial layer (i.e., top and middle layer for
847
  // 3 spatial layers).
848
0
  svc->use_gf_temporal_ref_current_layer = 0;
849
0
  if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
850
0
      !svc->buffer_gf_temporal_ref[1].is_used &&
851
0
      svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
852
0
      svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
853
0
      svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
854
0
      svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
855
    // Enable the second (long-term) temporal reference at the frame-level.
856
0
    svc->use_gf_temporal_ref_current_layer = 1;
857
0
  }
858
859
  // Check if current superframe has any layer sync, only check once on
860
  // base layer.
861
0
  if (svc->spatial_layer_id == 0) {
862
0
    int sl = 0;
863
    // Default is no sync.
864
0
    svc->superframe_has_layer_sync = 0;
865
0
    for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
866
0
      if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
867
0
    }
868
0
  }
869
870
  // Reset the drop flags for all spatial layers, on the
871
  // first_spatial_layer_to_encode.
872
0
  if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
873
0
    vp9_zero(svc->drop_spatial_layer);
874
    // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
875
    // causes an issue with frame dropping and temporal layers, when the frame
876
    // flags are passed via the encode call (bypass mode). Issue is that we're
877
    // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
878
0
    if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
879
0
      memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
880
0
      memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
881
0
      memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
882
      // These are set by API before the superframe is encoded and they are
883
      // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
884
      // mode.
885
0
      vp9_zero(svc->update_buffer_slot);
886
0
      vp9_zero(svc->reference_last);
887
0
      vp9_zero(svc->reference_golden);
888
0
      vp9_zero(svc->reference_altref);
889
      // TODO(jianj): Remove these 3, deprecated.
890
0
      vp9_zero(svc->update_last);
891
0
      vp9_zero(svc->update_golden);
892
0
      vp9_zero(svc->update_altref);
893
0
    }
894
0
  }
895
896
0
  lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
897
0
                           svc->temporal_layer_id];
898
899
  // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
900
  // only for non-BYPASS mode for now.
901
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
902
0
      svc->use_set_ref_frame_config) {
903
0
    RATE_CONTROL *const lrc = &lc->rc;
904
0
    lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
905
0
    lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
906
0
    if (cpi->fixed_qp_onepass) {
907
0
      lrc->worst_quality = cpi->rc.worst_quality;
908
0
      lrc->best_quality = cpi->rc.best_quality;
909
0
    }
910
0
  }
911
912
0
  if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 &&
913
0
      svc->spatial_layer_id == svc->first_spatial_layer_to_encode &&
914
0
      cpi->resize_state != ORIG) {
915
0
    scaling_factor_num = lc->scaling_factor_num_resize;
916
0
    scaling_factor_den = lc->scaling_factor_den_resize;
917
0
  } else {
918
0
    scaling_factor_num = lc->scaling_factor_num;
919
0
    scaling_factor_den = lc->scaling_factor_den;
920
0
  }
921
922
0
  get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, scaling_factor_num,
923
0
                       scaling_factor_den, &width, &height);
924
925
  // Use Eightap_smooth for low resolutions.
926
0
  if (width * height <= 320 * 240)
927
0
    svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
928
  // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
929
  // to source pixel).
930
0
  if (scaling_factor_num > ((int64_t)3 * scaling_factor_den) >> 2)
931
0
    svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
932
933
  // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
934
  // For now, turn off use of base motion vectors and partition reuse if the
935
  // spatial scale factors for any layers are not 2,
936
  // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
937
  // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
938
0
  if (svc->number_spatial_layers > 1) {
939
0
    int sl;
940
0
    for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
941
0
      lc = &svc->layer_context[sl * svc->number_temporal_layers +
942
0
                               svc->temporal_layer_id];
943
0
      if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
944
0
          !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
945
0
            svc->number_spatial_layers == 3)) {
946
0
        svc->use_base_mv = 0;
947
0
        svc->use_partition_reuse = 0;
948
0
        break;
949
0
      }
950
0
    }
951
    // For non-zero spatial layers: if the previous spatial layer was dropped
952
    // disable the base_mv and partition_reuse features.
953
0
    if (svc->spatial_layer_id > 0 &&
954
0
        svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
955
0
      svc->use_base_mv = 0;
956
0
      svc->use_partition_reuse = 0;
957
0
    }
958
0
  }
959
960
0
  svc->non_reference_frame = 0;
961
0
  if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
962
0
      !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
963
0
    svc->non_reference_frame = 1;
964
  // For flexible mode, where update_buffer_slot is used, need to check if
965
  // all buffer slots are not refreshed.
966
0
  if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
967
0
    if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
968
0
      svc->non_reference_frame = 0;
969
0
  }
970
971
0
  if (svc->spatial_layer_id == 0) {
972
0
    svc->high_source_sad_superframe = 0;
973
0
    svc->high_num_blocks_with_motion = 0;
974
0
  }
975
976
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
977
0
      svc->last_layer_dropped[svc->spatial_layer_id] &&
978
0
      svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
979
0
      !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
980
    // For fixed/non-flexible mode, if the previous frame (same spatial layer
981
    // from previous superframe) was dropped, make sure the lst_fb_idx
982
    // for this frame corresponds to the buffer index updated on (last) encoded
983
    // TL0 frame (with same spatial layer).
984
0
    cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
985
0
  }
986
987
0
  if (vp9_set_size_literal(cpi, width, height) != 0)
988
0
    return VPX_CODEC_INVALID_PARAM;
989
990
0
  svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
991
0
  svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
992
0
  svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
993
0
  return 0;
994
0
}
995
996
struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
997
                                              struct lookahead_ctx *ctx,
998
0
                                              int drain) {
999
0
  struct lookahead_entry *buf = NULL;
1000
0
  if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
1001
0
    buf = vp9_lookahead_peek(ctx, 0);
1002
0
    if (buf != NULL) {
1003
      // Only remove the buffer when pop the highest layer.
1004
0
      if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
1005
0
        vp9_lookahead_pop(ctx, drain);
1006
0
      }
1007
0
    }
1008
0
  }
1009
0
  return buf;
1010
0
}
1011
1012
3.75k
void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
1013
3.75k
  int sl, tl;
1014
3.75k
  SVC *const svc = &cpi->svc;
1015
3.75k
  const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1016
7.51k
  for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
1017
7.51k
    for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
1018
3.75k
      int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
1019
3.75k
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1020
3.75k
      if (lc->map) vpx_free(lc->map);
1021
3.75k
      if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
1022
3.75k
      if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
1023
3.75k
    }
1024
3.75k
  }
1025
3.75k
}
1026
1027
// Reset on key frame: reset counters, references and buffer updates.
1028
0
void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
1029
0
  int sl, tl;
1030
0
  SVC *const svc = &cpi->svc;
1031
0
  LAYER_CONTEXT *lc = NULL;
1032
0
  for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1033
0
    for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1034
0
      lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
1035
0
      lc->current_video_frame_in_layer = 0;
1036
0
      if (is_key) lc->frames_from_key_frame = 0;
1037
0
    }
1038
0
  }
1039
0
  if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
1040
0
    set_flags_and_fb_idx_for_temporal_mode3(cpi);
1041
0
  } else if (svc->temporal_layering_mode ==
1042
0
             VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
1043
0
    set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
1044
0
  } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
1045
0
    set_flags_and_fb_idx_for_temporal_mode2(cpi);
1046
0
  }
1047
0
  vp9_update_temporal_layer_framerate(cpi);
1048
0
  vp9_restore_layer_context(cpi);
1049
0
}
1050
1051
0
void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
1052
0
  SVC *svc = &cpi->svc;
1053
0
  int sl, tl;
1054
0
  for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1055
    // Check for reset based on avg_frame_bandwidth for spatial layer sl.
1056
0
    const int spatial_layer_idx = LAYER_IDS_TO_IDX(
1057
0
        sl, svc->number_temporal_layers - 1, svc->number_temporal_layers);
1058
0
    LAYER_CONTEXT *lc = &svc->layer_context[spatial_layer_idx];
1059
0
    RATE_CONTROL *lrc = &lc->rc;
1060
0
    if (lrc->avg_frame_bandwidth / 3 > (lrc->last_avg_frame_bandwidth >> 1) ||
1061
0
        lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
1062
      // Reset for all temporal layers with spatial layer sl.
1063
0
      for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1064
0
        int temporal_layer_idx =
1065
0
            LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
1066
0
        lrc = &svc->layer_context[temporal_layer_idx].rc;
1067
0
        lrc->rc_1_frame = 0;
1068
0
        lrc->rc_2_frame = 0;
1069
0
        lrc->bits_off_target = lrc->optimal_buffer_level;
1070
0
        lrc->buffer_level = lrc->optimal_buffer_level;
1071
0
      }
1072
0
    }
1073
0
  }
1074
0
}
1075
1076
0
void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
1077
0
  VP9_COMMON *const cm = &cpi->common;
1078
0
  SVC *const svc = &cpi->svc;
1079
0
  const int sl = svc->spatial_layer_id;
1080
  // Check for disabling inter-layer (spatial) prediction, if
1081
  // svc.disable_inter_layer_pred is set. If the previous spatial layer was
1082
  // dropped then disable the prediction from this (scaled) reference.
1083
  // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
1084
  // on key frames or if any spatial layer is a sync layer.
1085
0
  if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
1086
0
       !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1087
0
       !svc->superframe_has_layer_sync) ||
1088
0
      svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
1089
0
      svc->drop_spatial_layer[sl - 1]) {
1090
0
    MV_REFERENCE_FRAME ref_frame;
1091
0
    for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1092
0
      const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1093
0
      if (yv12 != NULL &&
1094
0
          (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) {
1095
0
        const struct scale_factors *const scale_fac =
1096
0
            &cm->frame_refs[ref_frame - 1].sf;
1097
0
        if (vp9_is_scaled(scale_fac)) {
1098
0
          cpi->ref_frame_flags &= (~ref_frame_to_flag(ref_frame));
1099
          // Point golden/altref frame buffer index to last.
1100
0
          if (!svc->simulcast_mode) {
1101
0
            if (ref_frame == GOLDEN_FRAME)
1102
0
              cpi->gld_fb_idx = cpi->lst_fb_idx;
1103
0
            else if (ref_frame == ALTREF_FRAME)
1104
0
              cpi->alt_fb_idx = cpi->lst_fb_idx;
1105
0
          }
1106
0
        }
1107
0
      }
1108
0
    }
1109
0
  }
1110
  // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
1111
  // If the reference for inter-layer prediction (the reference that is scaled)
1112
  // is not the previous spatial layer from the same superframe, then we disable
1113
  // inter-layer prediction. Only need to check when inter_layer prediction is
1114
  // not set to OFF mode.
1115
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1116
0
      svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
1117
    // We only use LAST and GOLDEN for prediction in real-time mode, so we
1118
    // check both here.
1119
0
    MV_REFERENCE_FRAME ref_frame;
1120
0
    for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
1121
0
      struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
1122
0
      if (vp9_is_scaled(scale_fac)) {
1123
        // If this reference  was updated on the previous spatial layer of the
1124
        // current superframe, then we keep this reference (don't disable).
1125
        // Otherwise we disable the inter-layer prediction.
1126
        // This condition is verified by checking if the current frame buffer
1127
        // index is equal to any of the slots for the previous spatial layer,
1128
        // and if so, check if that slot was updated/refreshed. If that is the
1129
        // case, then this reference is valid for inter-layer prediction under
1130
        // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
1131
0
        int fb_idx =
1132
0
            ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
1133
0
        int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
1134
0
        int disable = 1;
1135
0
        if (fb_idx < 0) continue;
1136
0
        if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
1137
0
             (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1138
0
            (fb_idx == svc->gld_fb_idx[sl - 1] &&
1139
0
             (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1140
0
            (fb_idx == svc->alt_fb_idx[sl - 1] &&
1141
0
             (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
1142
0
          disable = 0;
1143
0
        if (disable) cpi->ref_frame_flags &= (~ref_flag);
1144
0
      }
1145
0
    }
1146
0
  }
1147
0
}
1148
1149
0
void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
1150
0
  SVC *const svc = &cpi->svc;
1151
  // For fixed/non-flexible mode, the following constraint are expected,
1152
  // when inter-layer prediction is on (default).
1153
0
  if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1154
0
      svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
1155
0
      svc->framedrop_mode != LAYER_DROP) {
1156
0
    if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1157
      // On non-key frames: LAST is always temporal reference, GOLDEN is
1158
      // spatial reference.
1159
0
      if (svc->temporal_layer_id == 0)
1160
        // Base temporal only predicts from base temporal.
1161
0
        assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
1162
0
      else
1163
        // Non-base temporal only predicts from lower temporal layer.
1164
0
        assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
1165
0
               svc->temporal_layer_id);
1166
0
      if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
1167
0
          svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1168
        // Non-base spatial only predicts from lower spatial layer with same
1169
        // temporal_id.
1170
0
        assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1171
0
               svc->spatial_layer_id - 1);
1172
0
        assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1173
0
               svc->temporal_layer_id);
1174
0
      }
1175
0
    } else if (svc->spatial_layer_id > 0 &&
1176
0
               svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1177
      // Only 1 reference for frame whose base is key; reference may be LAST
1178
      // or GOLDEN, so we check both.
1179
0
      if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1180
0
        assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
1181
0
               svc->spatial_layer_id - 1);
1182
0
        assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
1183
0
               svc->temporal_layer_id);
1184
0
      } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1185
0
        assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1186
0
               svc->spatial_layer_id - 1);
1187
0
        assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1188
0
               svc->temporal_layer_id);
1189
0
      }
1190
0
    }
1191
0
  } else if (svc->use_gf_temporal_ref_current_layer &&
1192
0
             !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1193
    // For the usage of golden as second long term reference: the
1194
    // temporal_layer_id of that reference must be base temporal layer 0, and
1195
    // spatial_layer_id of that reference must be same as current
1196
    // spatial_layer_id. If not, disable feature.
1197
    // TODO(marpan): Investigate when this can happen, and maybe put this check
1198
    // and reset in a different place.
1199
0
    if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
1200
0
            svc->spatial_layer_id ||
1201
0
        svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
1202
0
      svc->use_gf_temporal_ref_current_layer = 0;
1203
0
  }
1204
0
}
1205
1206
#if CONFIG_VP9_TEMPORAL_DENOISING
1207
int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
1208
  int layer =
1209
      LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1210
                       cpi->svc.number_temporal_layers);
1211
  LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1212
  return denoise_svc(cpi) && !lc->is_key_frame;
1213
}
1214
#endif
1215
1216
0
void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
1217
0
  SVC *const svc = &cpi->svc;
1218
  // Only for superframes whose base is not key, as those are
1219
  // already sync frames.
1220
0
  if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1221
0
    if (svc->spatial_layer_id == 0) {
1222
      // On base spatial layer: if the current superframe has a layer sync then
1223
      // reset the pattern counters and reset to base temporal layer.
1224
0
      if (svc->superframe_has_layer_sync)
1225
0
        vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
1226
0
    }
1227
    // If the layer sync is set for this current spatial layer then
1228
    // disable the temporal reference.
1229
0
    if (svc->spatial_layer_id > 0 &&
1230
0
        svc->spatial_layer_sync[svc->spatial_layer_id]) {
1231
0
      cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1232
0
      if (svc->use_gf_temporal_ref_current_layer) {
1233
0
        int index = svc->spatial_layer_id;
1234
        // If golden is used as second reference: need to remove it from
1235
        // prediction, reset refresh period to 0, and update the reference.
1236
0
        svc->use_gf_temporal_ref_current_layer = 0;
1237
0
        cpi->rc.baseline_gf_interval = 0;
1238
0
        cpi->rc.frames_till_gf_update_due = 0;
1239
        // On layer sync frame we must update the buffer index used for long
1240
        // term reference. Use the alt_ref since it is not used or updated on
1241
        // sync frames.
1242
0
        if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
1243
0
        assert(index >= 0);
1244
0
        cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
1245
0
        cpi->ext_refresh_alt_ref_frame = 1;
1246
0
      }
1247
0
    }
1248
0
  }
1249
0
}
1250
1251
0
void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
1252
0
  SVC *const svc = &cpi->svc;
1253
0
  int i = 0;
1254
  // Update the usage of frame buffer index for base spatial layers.
1255
0
  if (svc->spatial_layer_id == 0) {
1256
0
    if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
1257
0
      svc->fb_idx_base[cpi->lst_fb_idx] = 1;
1258
0
    if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
1259
0
      svc->fb_idx_base[cpi->gld_fb_idx] = 1;
1260
0
    if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
1261
0
      svc->fb_idx_base[cpi->alt_fb_idx] = 1;
1262
    // For bypass/flexible mode: check for refresh slots.
1263
0
    if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
1264
0
      for (i = 0; i < REF_FRAMES; ++i)
1265
0
        if (svc->update_buffer_slot[0] & (1 << i)) svc->fb_idx_base[i] = 1;
1266
0
    }
1267
0
  }
1268
0
}
1269
1270
0
static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
1271
  // For non-flexible/bypass SVC mode: check for refreshing other buffer
1272
  // slots.
1273
0
  SVC *const svc = &cpi->svc;
1274
0
  VP9_COMMON *const cm = &cpi->common;
1275
0
  BufferPool *const pool = cm->buffer_pool;
1276
0
  int i;
1277
0
  for (i = 0; i < REF_FRAMES; i++) {
1278
0
    if ((cm->frame_type == KEY_FRAME && !svc->simulcast_mode) ||
1279
0
        svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
1280
0
      ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1281
0
      svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1282
0
      svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1283
0
    }
1284
0
  }
1285
0
}
1286
1287
0
void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
1288
0
  VP9_COMMON *const cm = &cpi->common;
1289
0
  SVC *const svc = &cpi->svc;
1290
0
  BufferPool *const pool = cm->buffer_pool;
1291
1292
0
  if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1293
0
      svc->use_set_ref_frame_config) {
1294
0
    vp9_svc_update_ref_frame_bypass_mode(cpi);
1295
0
  } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
1296
    // Keep track of frame index for each reference frame.
1297
0
    int i;
1298
    // On key frame update all reference frame slots.
1299
0
    for (i = 0; i < REF_FRAMES; i++) {
1300
0
      svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1301
0
      svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1302
      // LAST/GOLDEN/ALTREF is already updated above.
1303
0
      if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
1304
0
        ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1305
0
    }
1306
0
  } else {
1307
0
    if (cpi->refresh_last_frame) {
1308
0
      svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
1309
0
      svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
1310
0
    }
1311
0
    if (cpi->refresh_golden_frame) {
1312
0
      svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
1313
0
      svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
1314
0
    }
1315
0
    if (cpi->refresh_alt_ref_frame) {
1316
0
      svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
1317
0
      svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
1318
0
    }
1319
0
  }
1320
  // Copy flags from encoder to SVC struct.
1321
0
  vp9_copy_flags_ref_update_idx(cpi);
1322
0
  vp9_svc_update_ref_frame_buffer_idx(cpi);
1323
0
}
1324
1325
0
void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
1326
0
  int64_t this_duration =
1327
0
      cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
1328
0
  vp9_new_framerate(cpi, 10000000.0 / this_duration);
1329
0
}
1330
1331
0
void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
1332
0
  VP9_COMMON *const cm = &cpi->common;
1333
0
  SVC *const svc = &cpi->svc;
1334
0
  RATE_CONTROL *const rc = &cpi->rc;
1335
  // On key frames in CBR mode: reset the avg_frame_qindex for base layer
1336
  // (to level closer to worst_quality) if the overshoot is significant.
1337
  // Reset it for all temporal layers on base spatial layer.
1338
0
  if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
1339
0
      !svc->simulcast_mode &&
1340
0
      rc->projected_frame_size / 3 > rc->avg_frame_bandwidth) {
1341
0
    int tl;
1342
0
    rc->avg_frame_qindex[INTER_FRAME] =
1343
0
        VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
1344
0
               (cm->base_qindex + rc->worst_quality) >> 1);
1345
0
    for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1346
0
      const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
1347
0
      LAYER_CONTEXT *lc = &svc->layer_context[layer];
1348
0
      RATE_CONTROL *lrc = &lc->rc;
1349
0
      lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
1350
0
    }
1351
0
  }
1352
0
}
1353
1354
// SVC: skip encoding of enhancement layer if the layer target bandwidth = 0.
1355
// No need to set svc.skip_enhancement_layer if whole superframe will be
1356
// dropped.
1357
72.9k
int vp9_svc_check_skip_enhancement_layer(VP9_COMP *const cpi) {
1358
72.9k
  if (cpi->use_svc && cpi->svc.spatial_layer_id > 0 &&
1359
0
      cpi->oxcf.target_bandwidth == 0 &&
1360
0
      !(cpi->svc.framedrop_mode != LAYER_DROP &&
1361
0
        (cpi->svc.framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP ||
1362
0
         cpi->svc
1363
0
             .force_drop_constrained_from_above[cpi->svc.number_spatial_layers -
1364
0
                                                1]) &&
1365
0
        cpi->svc.drop_spatial_layer[0])) {
1366
0
    cpi->svc.skip_enhancement_layer = 1;
1367
0
    vp9_rc_postencode_update_drop_frame(cpi);
1368
0
    cpi->ext_refresh_frame_flags_pending = 0;
1369
0
    cpi->last_frame_dropped = 1;
1370
0
    cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 1;
1371
0
    cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = 1;
1372
0
    vp9_inc_frame_in_layer(cpi);
1373
0
    return 1;
1374
0
  }
1375
72.9k
  return 0;
1376
72.9k
}