Coverage Report

Created: 2026-05-24 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/encoder/svc_layercontext.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
#include <math.h>
14
15
#include "av1/encoder/encoder.h"
16
#include "av1/encoder/encoder_alloc.h"
17
18
0
static void swap_ptr(void *a, void *b) {
19
0
  void **a_p = (void **)a;
20
0
  void **b_p = (void **)b;
21
0
  void *c = *a_p;
22
0
  *a_p = *b_p;
23
0
  *b_p = c;
24
0
}
25
26
0
void av1_init_layer_context(AV1_COMP *const cpi) {
27
0
  AV1_COMMON *const cm = &cpi->common;
28
0
  const AV1EncoderConfig *const oxcf = &cpi->oxcf;
29
0
  SVC *const svc = &cpi->svc;
30
0
  int mi_rows = cpi->common.mi_params.mi_rows;
31
0
  int mi_cols = cpi->common.mi_params.mi_cols;
32
0
  svc->base_framerate = 30.0;
33
0
  svc->current_superframe = 0;
34
0
  svc->force_zero_mode_spatial_ref = 1;
35
0
  svc->num_encoded_top_layer = 0;
36
0
  svc->use_flexible_mode = 0;
37
0
  svc->has_lower_quality_layer = 0;
38
39
0
  for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
40
0
    for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
41
0
      int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
42
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
43
0
      RATE_CONTROL *const lrc = &lc->rc;
44
0
      PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
45
0
      lrc->ni_av_qi = oxcf->rc_cfg.worst_allowed_q;
46
0
      lp_rc->total_actual_bits = 0;
47
0
      lrc->ni_tot_qi = 0;
48
0
      lp_rc->tot_q = 0.0;
49
0
      lp_rc->avg_q = 0.0;
50
0
      lp_rc->ni_frames = 0;
51
0
      lrc->decimation_count = 0;
52
0
      lrc->decimation_factor = 0;
53
0
      lrc->worst_quality = av1_quantizer_to_qindex(lc->max_q);
54
0
      lrc->best_quality = av1_quantizer_to_qindex(lc->min_q);
55
0
      lrc->rtc_external_ratectrl = 0;
56
0
      for (int i = 0; i < RATE_FACTOR_LEVELS; ++i) {
57
0
        lp_rc->rate_correction_factors[i] = 1.0;
58
0
      }
59
0
      lc->target_bandwidth = lc->layer_target_bitrate;
60
0
      lp_rc->last_q[INTER_FRAME] = lrc->worst_quality;
61
0
      lp_rc->avg_frame_qindex[INTER_FRAME] = lrc->worst_quality;
62
0
      lp_rc->avg_frame_qindex[KEY_FRAME] = lrc->worst_quality;
63
0
      lp_rc->buffer_level =
64
0
          oxcf->rc_cfg.starting_buffer_level_ms * lc->target_bandwidth / 1000;
65
0
      lp_rc->bits_off_target = lp_rc->buffer_level;
66
      // Initialize the cyclic refresh parameters. If spatial layers are used
67
      // (i.e., ss_number_layers > 1), these need to be updated per spatial
68
      // layer. Cyclic refresh is only applied on base temporal layer.
69
0
      if (svc->number_spatial_layers > 1 && tl == 0 &&
70
0
          cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ) {
71
0
        lc->sb_index = 0;
72
0
        lc->actual_num_seg1_blocks = 0;
73
0
        lc->actual_num_seg2_blocks = 0;
74
0
        lc->counter_encode_maxq_scene_change = 0;
75
0
        aom_free(lc->map);
76
0
        CHECK_MEM_ERROR(cm, lc->map,
77
0
                        aom_calloc(mi_rows * mi_cols, sizeof(*lc->map)));
78
0
      }
79
0
    }
80
0
    svc->downsample_filter_type[sl] = BILINEAR;
81
0
    svc->downsample_filter_phase[sl] = 8;
82
0
    svc->last_layer_dropped[sl] = false;
83
0
    svc->drop_spatial_layer[sl] = false;
84
0
  }
85
0
  if (svc->number_spatial_layers == 3) {
86
0
    svc->downsample_filter_type[0] = EIGHTTAP_SMOOTH;
87
0
  }
88
0
}
89
90
0
bool av1_alloc_layer_context(AV1_COMP *cpi, int num_layers) {
91
0
  SVC *const svc = &cpi->svc;
92
0
  if (svc->layer_context == NULL || svc->num_allocated_layers < num_layers) {
93
0
    av1_free_svc_cyclic_refresh(cpi);
94
0
    assert(num_layers > 1);
95
0
    aom_free(svc->layer_context);
96
0
    svc->num_allocated_layers = 0;
97
0
    svc->layer_context =
98
0
        (LAYER_CONTEXT *)aom_calloc(num_layers, sizeof(*svc->layer_context));
99
0
    if (svc->layer_context == NULL) return false;
100
0
    svc->num_allocated_layers = num_layers;
101
0
  }
102
0
  return true;
103
0
}
104
105
// Update the layer context from a change_config() call.
106
void av1_update_layer_context_change_config(AV1_COMP *const cpi,
107
0
                                            const int64_t target_bandwidth) {
108
0
  const RATE_CONTROL *const rc = &cpi->rc;
109
0
  const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
110
0
  AV1_COMMON *const cm = &cpi->common;
111
0
  SVC *const svc = &cpi->svc;
112
0
  int layer = 0;
113
0
  int64_t spatial_layer_target = 0;
114
0
  float bitrate_alloc = 1.0;
115
0
  const int mi_rows = cm->mi_params.mi_rows;
116
0
  const int mi_cols = cm->mi_params.mi_cols;
117
0
  for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
118
0
    for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
119
0
      layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
120
0
      LAYER_CONTEXT *const lc = &svc->layer_context[layer];
121
0
      svc->layer_context[layer].target_bandwidth = lc->layer_target_bitrate;
122
0
    }
123
0
    spatial_layer_target = svc->layer_context[layer].target_bandwidth;
124
0
    for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
125
0
      LAYER_CONTEXT *const lc =
126
0
          &svc->layer_context[sl * svc->number_temporal_layers + tl];
127
0
      RATE_CONTROL *const lrc = &lc->rc;
128
0
      PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
129
0
      lc->spatial_layer_target_bandwidth = spatial_layer_target;
130
0
      if (target_bandwidth != 0) {
131
0
        bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
132
0
      }
133
0
      lp_rc->starting_buffer_level =
134
0
          (int64_t)(p_rc->starting_buffer_level * bitrate_alloc);
135
0
      lp_rc->optimal_buffer_level =
136
0
          (int64_t)(p_rc->optimal_buffer_level * bitrate_alloc);
137
0
      lp_rc->maximum_buffer_size =
138
0
          (int64_t)(p_rc->maximum_buffer_size * bitrate_alloc);
139
0
      lp_rc->bits_off_target =
140
0
          AOMMIN(lp_rc->bits_off_target, lp_rc->maximum_buffer_size);
141
0
      lp_rc->buffer_level =
142
0
          AOMMIN(lp_rc->buffer_level, lp_rc->maximum_buffer_size);
143
0
      lc->framerate = cpi->framerate / lc->framerate_factor;
144
0
      lrc->avg_frame_bandwidth =
145
0
          (int)round(lc->target_bandwidth / lc->framerate);
146
0
      lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
147
0
      lrc->rtc_external_ratectrl = rc->rtc_external_ratectrl;
148
0
      lrc->worst_quality = av1_quantizer_to_qindex(lc->max_q);
149
0
      lrc->best_quality = av1_quantizer_to_qindex(lc->min_q);
150
0
      if (rc->use_external_qp_one_pass) {
151
0
        lrc->worst_quality = rc->worst_quality;
152
0
        lrc->best_quality = rc->best_quality;
153
0
      }
154
      // Reset the cyclic refresh parameters, if needed (map is NULL),
155
      // or number of spatial layers has changed.
156
      // Cyclic refresh is only applied on base temporal layer.
157
0
      if (svc->number_spatial_layers > 1 && tl == 0 &&
158
0
          cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
159
0
          (lc->map == NULL ||
160
0
           svc->prev_number_spatial_layers != svc->number_spatial_layers)) {
161
0
        lc->sb_index = 0;
162
0
        lc->actual_num_seg1_blocks = 0;
163
0
        lc->actual_num_seg2_blocks = 0;
164
0
        lc->counter_encode_maxq_scene_change = 0;
165
0
        aom_free(lc->map);
166
0
        CHECK_MEM_ERROR(cm, lc->map,
167
0
                        aom_calloc(mi_rows * mi_cols, sizeof(*lc->map)));
168
0
      }
169
0
    }
170
0
  }
171
0
}
172
173
/*!\brief Return layer context for current layer.
174
 *
175
 * \ingroup rate_control
176
 * \param[in]       cpi   Top level encoder structure
177
 *
178
 * \return LAYER_CONTEXT for current layer.
179
 */
180
0
static LAYER_CONTEXT *get_layer_context(AV1_COMP *const cpi) {
181
0
  return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
182
0
                                     cpi->svc.number_temporal_layers +
183
0
                                 cpi->svc.temporal_layer_id];
184
0
}
185
186
0
void av1_update_temporal_layer_framerate(AV1_COMP *const cpi) {
187
0
  SVC *const svc = &cpi->svc;
188
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
189
0
  RATE_CONTROL *const lrc = &lc->rc;
190
0
  const int tl = svc->temporal_layer_id;
191
0
  lc->framerate = cpi->framerate / lc->framerate_factor;
192
0
  lrc->avg_frame_bandwidth =
193
0
      saturate_cast_double_to_int(round(lc->target_bandwidth / lc->framerate));
194
0
  lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
195
  // Update the average layer frame size (non-cumulative per-frame-bw).
196
0
  if (tl == 0) {
197
0
    lc->avg_frame_size = lrc->avg_frame_bandwidth;
198
0
  } else {
199
0
    int prev_layer = svc->spatial_layer_id * svc->number_temporal_layers +
200
0
                     svc->temporal_layer_id - 1;
201
0
    LAYER_CONTEXT *const lcprev = &svc->layer_context[prev_layer];
202
0
    const double prev_layer_framerate =
203
0
        cpi->framerate / lcprev->framerate_factor;
204
0
    const int64_t prev_layer_target_bandwidth = lcprev->layer_target_bitrate;
205
0
    if (lc->framerate > prev_layer_framerate) {
206
0
      lc->avg_frame_size =
207
0
          (int)round((lc->target_bandwidth - prev_layer_target_bandwidth) /
208
0
                     (lc->framerate - prev_layer_framerate));
209
0
    } else {
210
0
      lc->avg_frame_size = (int)round(lc->target_bandwidth / lc->framerate);
211
0
    }
212
0
  }
213
0
}
214
215
bool av1_check_ref_is_low_spatial_res_super_frame(AV1_COMP *const cpi,
216
0
                                                  int ref_frame) {
217
0
  SVC *svc = &cpi->svc;
218
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
219
0
  int ref_frame_idx = rtc_ref->ref_idx[ref_frame - 1];
220
0
  return rtc_ref->buffer_time_index[ref_frame_idx] == svc->current_superframe &&
221
0
         rtc_ref->buffer_spatial_layer[ref_frame_idx] <=
222
0
             svc->spatial_layer_id - 1;
223
0
}
224
225
0
void av1_restore_layer_context(AV1_COMP *const cpi) {
226
0
  SVC *const svc = &cpi->svc;
227
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
228
0
  const AV1_COMMON *const cm = &cpi->common;
229
0
  LAYER_CONTEXT *const lc = get_layer_context(cpi);
230
0
  const int old_frame_since_key = cpi->rc.frames_since_key;
231
0
  const int old_frame_to_key = cpi->rc.frames_to_key;
232
0
  const int frames_since_scene_change = cpi->rc.frames_since_scene_change;
233
0
  const int last_encoded_size_keyframe = cpi->rc.last_encoded_size_keyframe;
234
0
  const int last_target_size_keyframe = cpi->rc.last_target_size_keyframe;
235
0
  const int max_consec_drop = cpi->rc.max_consec_drop;
236
0
  const int postencode_drop = cpi->rc.postencode_drop;
237
0
  const int static_since_last_scene_change =
238
0
      cpi->rc.static_since_last_scene_change;
239
  // Restore layer rate control.
240
0
  cpi->rc = lc->rc;
241
0
  cpi->ppi->p_rc = lc->p_rc;
242
0
  cpi->oxcf.rc_cfg.target_bandwidth = lc->target_bandwidth;
243
0
  cpi->gf_frame_index = 0;
244
0
  cpi->mv_search_params.max_mv_magnitude = lc->max_mv_magnitude;
245
0
  if (cpi->mv_search_params.max_mv_magnitude == 0)
246
0
    cpi->mv_search_params.max_mv_magnitude = AOMMAX(cm->width, cm->height);
247
  // Reset the following parameters to their values before
248
  // the layer restore. Keep these defined for the stream (not layer).
249
0
  cpi->rc.frames_since_key = old_frame_since_key;
250
0
  cpi->rc.frames_to_key = old_frame_to_key;
251
0
  cpi->rc.frames_since_scene_change = frames_since_scene_change;
252
0
  cpi->rc.last_encoded_size_keyframe = last_encoded_size_keyframe;
253
0
  cpi->rc.last_target_size_keyframe = last_target_size_keyframe;
254
0
  cpi->rc.max_consec_drop = max_consec_drop;
255
0
  cpi->rc.postencode_drop = postencode_drop;
256
0
  cpi->rc.static_since_last_scene_change = static_since_last_scene_change;
257
  // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
258
  // for the base temporal layer.
259
0
  if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
260
0
      svc->number_spatial_layers > 1 && svc->temporal_layer_id == 0) {
261
0
    CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
262
0
    swap_ptr(&cr->map, &lc->map);
263
0
    cr->sb_index = lc->sb_index;
264
0
    cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
265
0
    cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
266
0
    cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
267
0
  }
268
0
  svc->skip_mvsearch_last = 0;
269
0
  svc->skip_mvsearch_gf = 0;
270
0
  svc->skip_mvsearch_altref = 0;
271
  // For each reference (LAST/GOLDEN) set the skip_mvsearch_last/gf frame flags.
272
  // This is to skip searching mv for that reference if it was last
273
  // refreshed (i.e., buffer slot holding that reference was refreshed) on the
274
  // previous spatial layer(s) at the same time (current_superframe).
275
0
  if (rtc_ref->set_ref_frame_config && svc->force_zero_mode_spatial_ref &&
276
0
      cpi->sf.rt_sf.use_nonrd_pick_mode) {
277
0
    if (av1_check_ref_is_low_spatial_res_super_frame(cpi, LAST_FRAME)) {
278
0
      svc->skip_mvsearch_last = 1;
279
0
    }
280
0
    if (av1_check_ref_is_low_spatial_res_super_frame(cpi, GOLDEN_FRAME)) {
281
0
      svc->skip_mvsearch_gf = 1;
282
0
    }
283
0
    if (av1_check_ref_is_low_spatial_res_super_frame(cpi, ALTREF_FRAME)) {
284
0
      svc->skip_mvsearch_altref = 1;
285
0
    }
286
0
  }
287
0
}
288
289
0
void av1_svc_update_buffer_slot_refreshed(AV1_COMP *const cpi) {
290
0
  SVC *const svc = &cpi->svc;
291
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
292
0
  const unsigned int current_frame =
293
0
      cpi->ppi->use_svc ? svc->current_superframe
294
0
                        : cpi->common.current_frame.frame_number;
295
  // For any buffer slot that is refreshed, update it with
296
  // the spatial_layer_id and the current_superframe.
297
0
  if (cpi->common.current_frame.frame_type == KEY_FRAME) {
298
    // All slots are refreshed on KEY.
299
0
    for (unsigned int i = 0; i < REF_FRAMES; i++) {
300
0
      rtc_ref->buffer_time_index[i] = current_frame;
301
0
      rtc_ref->buffer_spatial_layer[i] = svc->spatial_layer_id;
302
0
    }
303
0
  } else if (rtc_ref->set_ref_frame_config) {
304
0
    for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
305
0
      const int ref_frame_map_idx = rtc_ref->ref_idx[i];
306
0
      if (rtc_ref->refresh[ref_frame_map_idx]) {
307
0
        rtc_ref->buffer_time_index[ref_frame_map_idx] = current_frame;
308
0
        rtc_ref->buffer_spatial_layer[ref_frame_map_idx] =
309
0
            svc->spatial_layer_id;
310
0
      }
311
0
    }
312
0
  }
313
0
}
314
315
0
void av1_save_layer_context(AV1_COMP *const cpi) {
316
0
  SVC *const svc = &cpi->svc;
317
0
  const AV1_COMMON *const cm = &cpi->common;
318
0
  LAYER_CONTEXT *lc = get_layer_context(cpi);
319
0
  lc->rc = cpi->rc;
320
0
  lc->p_rc = cpi->ppi->p_rc;
321
0
  lc->target_bandwidth = (int)cpi->oxcf.rc_cfg.target_bandwidth;
322
0
  lc->group_index = cpi->gf_frame_index;
323
0
  lc->max_mv_magnitude = cpi->mv_search_params.max_mv_magnitude;
324
0
  if (svc->spatial_layer_id == 0) svc->base_framerate = cpi->framerate;
325
  // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
326
  // for the base temporal layer.
327
0
  if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
328
0
      cpi->svc.number_spatial_layers > 1 && svc->temporal_layer_id == 0) {
329
0
    CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
330
0
    signed char *temp = lc->map;
331
0
    lc->map = cr->map;
332
0
    cr->map = temp;
333
0
    lc->sb_index = cr->sb_index;
334
0
    lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
335
0
    lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
336
0
    lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
337
0
  }
338
0
  if (!cpi->is_dropped_frame) {
339
0
    av1_svc_update_buffer_slot_refreshed(cpi);
340
0
    for (unsigned int i = 0; i < REF_FRAMES; i++) {
341
0
      if (frame_is_intra_only(cm) ||
342
0
          cm->current_frame.refresh_frame_flags & (1 << i)) {
343
0
        svc->spatial_layer_fb[i] = svc->spatial_layer_id;
344
0
        svc->temporal_layer_fb[i] = svc->temporal_layer_id;
345
0
      }
346
0
    }
347
0
  }
348
0
  if (svc->spatial_layer_id == svc->number_spatial_layers - 1) {
349
0
    svc->current_superframe++;
350
    // Reset drop flag to false for next superframe.
351
0
    for (int sl = 0; sl < svc->number_spatial_layers; sl++)
352
0
      svc->drop_spatial_layer[sl] = false;
353
0
  }
354
0
}
355
356
0
int av1_svc_primary_ref_frame(const AV1_COMP *const cpi) {
357
0
  const SVC *const svc = &cpi->svc;
358
0
  const AV1_COMMON *const cm = &cpi->common;
359
0
  int fb_idx = -1;
360
0
  int primary_ref_frame = PRIMARY_REF_NONE;
361
0
  if (cpi->svc.number_spatial_layers > 1 ||
362
0
      cpi->svc.number_temporal_layers > 1) {
363
    // Set the primary_ref_frame to LAST_FRAME if that buffer slot for LAST
364
    // was last updated on a lower temporal layer (or base TL0) and for the
365
    // same spatial layer. For RTC patterns this allows for continued decoding
366
    // when set of enhancement layers are dropped (continued decoding starting
367
    // at next base TL0), so error_resilience can be off/0 for all layers.
368
0
    fb_idx = get_ref_frame_map_idx(cm, LAST_FRAME);
369
0
    if (cpi->ppi->rtc_ref.reference[0] == 1 &&
370
0
        svc->spatial_layer_fb[fb_idx] == svc->spatial_layer_id &&
371
0
        (svc->temporal_layer_fb[fb_idx] < svc->temporal_layer_id ||
372
0
         svc->temporal_layer_fb[fb_idx] == 0)) {
373
0
      primary_ref_frame = 0;  // LAST_FRAME: ref_frame - LAST_FRAME
374
0
    }
375
0
  } else if (cpi->ppi->rtc_ref.set_ref_frame_config) {
376
0
    const ExternalFlags *const ext_flags = &cpi->ext_flags;
377
0
    int flags = ext_flags->ref_frame_flags;
378
0
    if (flags & AOM_LAST_FLAG) {
379
0
      primary_ref_frame = 0;  // LAST_FRAME: ref_frame - LAST_FRAME
380
0
    } else if (flags & AOM_GOLD_FLAG) {
381
0
      primary_ref_frame = GOLDEN_FRAME - LAST_FRAME;
382
0
    } else if (flags & AOM_ALT_FLAG) {
383
0
      primary_ref_frame = ALTREF_FRAME - LAST_FRAME;
384
0
    }
385
0
  }
386
0
  return primary_ref_frame;
387
0
}
388
389
0
void av1_free_svc_cyclic_refresh(AV1_COMP *const cpi) {
390
0
  SVC *const svc = &cpi->svc;
391
0
  if (svc->layer_context == NULL) return;
392
0
  for (int i = 0; i < svc->num_allocated_layers; ++i) {
393
0
    LAYER_CONTEXT *const lc = &svc->layer_context[i];
394
0
    if (lc->map) {
395
0
      aom_free(lc->map);
396
0
      lc->map = NULL;
397
0
    }
398
0
  }
399
0
}
400
401
0
void av1_svc_reset_temporal_layers(AV1_COMP *const cpi, int is_key) {
402
0
  SVC *const svc = &cpi->svc;
403
0
  LAYER_CONTEXT *lc = NULL;
404
0
  for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
405
0
    for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
406
0
      lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
407
0
      if (is_key) lc->frames_from_key_frame = 0;
408
0
    }
409
0
  }
410
0
  av1_update_temporal_layer_framerate(cpi);
411
0
  av1_restore_layer_context(cpi);
412
0
}
413
414
void av1_get_layer_resolution(const int width_org, const int height_org,
415
                              const int num, const int den, int *width_out,
416
0
                              int *height_out) {
417
0
  int w, h;
418
0
  if (width_out == NULL || height_out == NULL || den == 0) return;
419
0
  if (den == 1 && num == 1) {
420
0
    *width_out = width_org;
421
0
    *height_out = height_org;
422
0
    return;
423
0
  }
424
0
  w = width_org * num / den;
425
0
  h = height_org * num / den;
426
  // Make height and width even.
427
0
  w += w % 2;
428
0
  h += h % 2;
429
0
  *width_out = w;
430
0
  *height_out = h;
431
0
}
432
433
0
void av1_one_pass_cbr_svc_start_layer(AV1_COMP *const cpi) {
434
0
  SVC *const svc = &cpi->svc;
435
0
  AV1_COMMON *const cm = &cpi->common;
436
0
  LAYER_CONTEXT *lc = NULL;
437
0
  int width = 0, height = 0;
438
0
  lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
439
0
                           svc->temporal_layer_id];
440
  // Set the lower quality layer flag.
441
0
  svc->has_lower_quality_layer = 0;
442
0
  if (cpi->svc.spatial_layer_id > 0) {
443
0
    const LAYER_CONTEXT *lc_prev =
444
0
        &svc->layer_context[(svc->spatial_layer_id - 1) *
445
0
                                svc->number_temporal_layers +
446
0
                            svc->temporal_layer_id];
447
0
    if (lc_prev->scaling_factor_den == 1 && lc_prev->scaling_factor_num == 1)
448
0
      svc->has_lower_quality_layer = 1;
449
0
  }
450
0
  av1_get_layer_resolution(cpi->oxcf.frm_dim_cfg.width,
451
0
                           cpi->oxcf.frm_dim_cfg.height, lc->scaling_factor_num,
452
0
                           lc->scaling_factor_den, &width, &height);
453
  // Use Eightap_smooth for low resolutions.
454
0
  if (width * height <= 320 * 240)
455
0
    svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
456
457
0
  cm->width = width;
458
0
  cm->height = height;
459
0
  alloc_mb_mode_info_buffers(cpi);
460
0
  av1_update_frame_size(cpi);
461
0
}
462
463
enum {
464
  SVC_LAST_FRAME = 0,
465
  SVC_LAST2_FRAME,
466
  SVC_LAST3_FRAME,
467
  SVC_GOLDEN_FRAME,
468
  SVC_BWDREF_FRAME,
469
  SVC_ALTREF2_FRAME,
470
  SVC_ALTREF_FRAME
471
};
472
473
// For fixed svc mode: fixed pattern is set based on the number of
474
// spatial and temporal layers, and the ksvc_fixed_mode.
475
0
void av1_set_svc_fixed_mode(AV1_COMP *const cpi) {
476
0
  SVC *const svc = &cpi->svc;
477
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
478
0
  int i;
479
0
  assert(svc->use_flexible_mode == 0);
480
0
  assert(svc->number_spatial_layers >= 1 && svc->number_temporal_layers >= 1);
481
  // Fixed SVC mode only supports at most 3 spatial or temporal layers.
482
0
  if (svc->number_spatial_layers > 3 || svc->number_temporal_layers > 3) {
483
0
    aom_internal_error(&cpi->ppi->error, AOM_CODEC_INVALID_PARAM,
484
0
                       "Invalid number of spatial/temporal layers for fixed "
485
0
                       "SVC mode (max: 3)");
486
0
  }
487
0
  rtc_ref->set_ref_frame_config = 1;
488
0
  int superframe_cnt = svc->current_superframe;
489
  // Set the reference map buffer idx for the 7 references:
490
  // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
491
  // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
492
0
  for (i = 0; i < INTER_REFS_PER_FRAME; i++) {
493
0
    rtc_ref->reference[i] = 0;
494
0
    rtc_ref->ref_idx[i] = i;
495
0
  }
496
0
  for (i = 0; i < REF_FRAMES; i++) rtc_ref->refresh[i] = 0;
497
  // Always reference LAST, and reference GOLDEN on SL > 0.
498
  // For KSVC: GOLDEN reference will be removed on INTER_FRAMES later
499
  // when frame_type is set.
500
0
  rtc_ref->reference[SVC_LAST_FRAME] = 1;
501
0
  if (svc->spatial_layer_id > 0) rtc_ref->reference[SVC_GOLDEN_FRAME] = 1;
502
0
  if (svc->temporal_layer_id == 0) {
503
    // Base temporal layer.
504
0
    if (svc->spatial_layer_id == 0) {
505
      // Set all buffer_idx to 0. Update slot 0 (LAST).
506
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
507
0
      rtc_ref->refresh[0] = 1;
508
0
    } else if (svc->spatial_layer_id == 1) {
509
      // Set buffer_idx for LAST to slot 1, GOLDEN (and all other refs) to
510
      // slot 0. Update slot 1 (LAST).
511
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
512
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 1;
513
0
      rtc_ref->refresh[1] = 1;
514
0
    } else if (svc->spatial_layer_id == 2) {
515
      // Set buffer_idx for LAST to slot 2, GOLDEN (and all other refs) to
516
      // slot 1. Update slot 2 (LAST).
517
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 1;
518
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 2;
519
0
      rtc_ref->refresh[2] = 1;
520
0
    }
521
0
  } else if (svc->temporal_layer_id == 2 && (superframe_cnt - 1) % 4 == 0) {
522
    // First top temporal enhancement layer.
523
0
    if (svc->spatial_layer_id == 0) {
524
      // Reference LAST (slot 0).
525
      // Set GOLDEN to slot 3 and update slot 3.
526
      // Set all other buffer_idx to slot 0.
527
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
528
0
      if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
529
0
        rtc_ref->ref_idx[SVC_GOLDEN_FRAME] = 3;
530
0
        rtc_ref->refresh[3] = 1;
531
0
      }
532
0
    } else if (svc->spatial_layer_id == 1) {
533
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
534
      // GOLDEN (and all other refs) to slot 3.
535
      // Set LAST2 to slot 4 and Update slot 4.
536
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 3;
537
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 1;
538
0
      if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
539
0
        rtc_ref->ref_idx[SVC_LAST2_FRAME] = 4;
540
0
        rtc_ref->refresh[4] = 1;
541
0
      }
542
0
    } else if (svc->spatial_layer_id == 2) {
543
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
544
      // GOLDEN (and all other refs) to slot 4.
545
      // No update.
546
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 4;
547
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 2;
548
0
    }
549
0
  } else if (svc->temporal_layer_id == 1) {
550
    // Middle temporal enhancement layer.
551
0
    if (svc->spatial_layer_id == 0) {
552
      // Reference LAST.
553
      // Set all buffer_idx to 0.
554
      // Set GOLDEN to slot 5 and update slot 5.
555
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
556
0
      if (svc->temporal_layer_id < svc->number_temporal_layers - 1 ||
557
0
          svc->spatial_layer_id < svc->number_spatial_layers - 1) {
558
0
        rtc_ref->ref_idx[SVC_GOLDEN_FRAME] = 5;
559
0
        rtc_ref->refresh[5] = 1;
560
0
      }
561
0
    } else if (svc->spatial_layer_id == 1) {
562
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
563
      // GOLDEN (and all other refs) to slot 5.
564
      // Set LAST3 to slot 6 and update slot 6.
565
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 5;
566
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 1;
567
0
      if (svc->temporal_layer_id < svc->number_temporal_layers - 1 ||
568
0
          svc->spatial_layer_id < svc->number_spatial_layers - 1) {
569
0
        rtc_ref->ref_idx[SVC_LAST3_FRAME] = 6;
570
0
        rtc_ref->refresh[6] = 1;
571
0
      }
572
0
    } else if (svc->spatial_layer_id == 2) {
573
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
574
      // GOLDEN (and all other refs) to slot 6.
575
      // Set LAST3 to slot 7 and update slot 7.
576
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 6;
577
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 2;
578
0
      if (svc->temporal_layer_id < svc->number_temporal_layers - 1) {
579
0
        rtc_ref->ref_idx[SVC_LAST3_FRAME] = 7;
580
0
        rtc_ref->refresh[7] = 1;
581
0
      }
582
0
    }
583
0
  } else if (svc->temporal_layer_id == 2 && (superframe_cnt - 3) % 4 == 0) {
584
    // Second top temporal enhancement layer.
585
0
    if (svc->spatial_layer_id == 0) {
586
      // Set LAST to slot 5 and reference LAST.
587
      // Set GOLDEN to slot 3 and update slot 3.
588
      // Set all other buffer_idx to 0.
589
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
590
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 5;
591
0
      if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
592
0
        rtc_ref->ref_idx[SVC_GOLDEN_FRAME] = 3;
593
0
        rtc_ref->refresh[3] = 1;
594
0
      }
595
0
    } else if (svc->spatial_layer_id == 1) {
596
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
597
      // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
598
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
599
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 6;
600
0
      rtc_ref->ref_idx[SVC_GOLDEN_FRAME] = 3;
601
0
      if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
602
0
        rtc_ref->ref_idx[SVC_LAST2_FRAME] = 4;
603
0
        rtc_ref->refresh[4] = 1;
604
0
      }
605
0
    } else if (svc->spatial_layer_id == 2) {
606
      // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
607
      // GOLDEN to slot 4. No update.
608
0
      for (i = 0; i < INTER_REFS_PER_FRAME; i++) rtc_ref->ref_idx[i] = 0;
609
0
      rtc_ref->ref_idx[SVC_LAST_FRAME] = 7;
610
0
      rtc_ref->ref_idx[SVC_GOLDEN_FRAME] = 4;
611
0
    }
612
0
  }
613
0
}
614
615
0
void av1_svc_check_reset_layer_rc_flag(AV1_COMP *const cpi) {
616
0
  SVC *const svc = &cpi->svc;
617
0
  for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
618
    // Check for reset based on avg_frame_bandwidth for spatial layer sl.
619
    // If avg_frame_bandwidth for top temporal layer is not set
620
    // (because enhancement layer was inactive), use the base TL0
621
0
    int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
622
0
                                 svc->number_temporal_layers);
623
0
    LAYER_CONTEXT *lc = &svc->layer_context[layer];
624
0
    RATE_CONTROL *lrc = &lc->rc;
625
0
    int avg_frame_bandwidth = lrc->avg_frame_bandwidth;
626
0
    int prev_avg_frame_bandwidth = lrc->prev_avg_frame_bandwidth;
627
0
    if (avg_frame_bandwidth == 0 || prev_avg_frame_bandwidth == 0) {
628
      // Use base TL0.
629
0
      layer = LAYER_IDS_TO_IDX(sl, 0, svc->number_temporal_layers);
630
0
      lc = &svc->layer_context[layer];
631
0
      lrc = &lc->rc;
632
0
      avg_frame_bandwidth = lrc->avg_frame_bandwidth;
633
0
      prev_avg_frame_bandwidth = lrc->prev_avg_frame_bandwidth;
634
0
    }
635
0
    if (avg_frame_bandwidth / 3 > (prev_avg_frame_bandwidth >> 1) ||
636
0
        avg_frame_bandwidth < (prev_avg_frame_bandwidth >> 1)) {
637
      // Reset for all temporal layers with spatial layer sl.
638
0
      for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
639
0
        int layer2 = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
640
0
        LAYER_CONTEXT *lc2 = &svc->layer_context[layer2];
641
0
        RATE_CONTROL *lrc2 = &lc2->rc;
642
0
        PRIMARY_RATE_CONTROL *lp_rc2 = &lc2->p_rc;
643
0
        PRIMARY_RATE_CONTROL *const lp_rc = &lc2->p_rc;
644
0
        lrc2->rc_1_frame = 0;
645
0
        lrc2->rc_2_frame = 0;
646
0
        lp_rc2->bits_off_target = lp_rc->optimal_buffer_level;
647
0
        lp_rc2->buffer_level = lp_rc->optimal_buffer_level;
648
0
      }
649
0
    }
650
0
  }
651
0
}
652
653
void av1_svc_set_last_source(AV1_COMP *const cpi, EncodeFrameInput *frame_input,
654
0
                             YV12_BUFFER_CONFIG *prev_source) {
655
0
  frame_input->last_source = prev_source != NULL ? prev_source : NULL;
656
0
  if (cpi->svc.source_last_TL0.buffer_alloc_sz == 0) return;
657
0
  if (!cpi->ppi->use_svc && cpi->rc.prev_frame_is_dropped &&
658
0
      cpi->rc.frame_number_encoded > 0) {
659
0
    frame_input->last_source = &cpi->svc.source_last_TL0;
660
0
  } else {
661
0
    RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
662
0
    if (cpi->svc.spatial_layer_id == 0) {
663
      // For base spatial layer: if the LAST reference (index 0) is not
664
      // the previous (super)frame set the last_source to the source
665
      // corresponding to the last TL0, otherwise keep it at prev_source.
666
      // Always use source_last_TL0 if previous base TL0 was dropped.
667
0
      if (cpi->svc.current_superframe > 0) {
668
0
        const int buffslot_last = rtc_ref->ref_idx[0];
669
        // Check if previous frame was dropped on base TL0 layer.
670
0
        const int layer =
671
0
            LAYER_IDS_TO_IDX(0, 0, cpi->svc.number_temporal_layers);
672
0
        LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
673
0
        RATE_CONTROL *lrc = &lc->rc;
674
0
        if (lrc->prev_frame_is_dropped ||
675
0
            rtc_ref->buffer_time_index[buffslot_last] <
676
0
                cpi->svc.current_superframe - 1) {
677
0
          frame_input->last_source = &cpi->svc.source_last_TL0;
678
0
        }
679
0
      }
680
0
    } else if (cpi->svc.spatial_layer_id > 0) {
681
      // For spatial enhancement layers: the previous source (prev_source)
682
      // corresponds to the lower spatial layer (which is the same source so
683
      // we can't use that), so always set the last_source to the source of the
684
      // last TL0.
685
0
      if (cpi->svc.current_superframe > 0)
686
0
        frame_input->last_source = &cpi->svc.source_last_TL0;
687
0
      else
688
0
        frame_input->last_source = NULL;
689
0
    }
690
0
  }
691
0
}
692
693
0
int av1_svc_get_min_ref_dist(const AV1_COMP *cpi) {
694
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
695
0
  int min_dist = INT_MAX;
696
0
  const unsigned int current_frame_num =
697
0
      cpi->ppi->use_svc ? cpi->svc.current_superframe
698
0
                        : cpi->common.current_frame.frame_number;
699
0
  for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
700
0
    if (rtc_ref->reference[i]) {
701
0
      const int ref_frame_map_idx = rtc_ref->ref_idx[i];
702
0
      const int dist = (1 + current_frame_num) -
703
0
                       rtc_ref->buffer_time_index[ref_frame_map_idx];
704
0
      if (dist < min_dist) min_dist = dist;
705
0
    }
706
0
  }
707
0
  return min_dist;
708
0
}
709
710
0
void av1_svc_set_reference_was_previous(AV1_COMP *cpi) {
711
0
  RTC_REF *const rtc_ref = &cpi->ppi->rtc_ref;
712
  // Check if the encoded frame had some reference that was the
713
  // previous frame.
714
0
  const unsigned int current_frame =
715
0
      cpi->ppi->use_svc ? cpi->svc.current_superframe
716
0
                        : cpi->common.current_frame.frame_number;
717
0
  rtc_ref->reference_was_previous_frame = true;
718
0
  if (current_frame > 0) {
719
0
    rtc_ref->reference_was_previous_frame = false;
720
0
    for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
721
0
      if (rtc_ref->reference[i]) {
722
0
        const int ref_frame_map_idx = rtc_ref->ref_idx[i];
723
0
        if (rtc_ref->buffer_time_index[ref_frame_map_idx] == current_frame - 1)
724
0
          rtc_ref->reference_was_previous_frame = true;
725
0
      }
726
0
    }
727
0
  }
728
0
}