1 /*
2 * Copyright (c) 2019, Alliance for Open Media. 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 "av1/encoder/encoder.h"
14
swap_ptr(void * a,void * b)15 static void swap_ptr(void *a, void *b) {
16 void **a_p = (void **)a;
17 void **b_p = (void **)b;
18 void *c = *a_p;
19 *a_p = *b_p;
20 *b_p = c;
21 }
22
av1_init_layer_context(AV1_COMP * const cpi)23 void av1_init_layer_context(AV1_COMP *const cpi) {
24 AV1_COMMON *const cm = &cpi->common;
25 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
26 SVC *const svc = &cpi->svc;
27 int mi_rows = cpi->common.mi_params.mi_rows;
28 int mi_cols = cpi->common.mi_params.mi_cols;
29 svc->base_framerate = 30.0;
30 svc->current_superframe = 0;
31 svc->force_zero_mode_spatial_ref = 1;
32 svc->num_encoded_top_layer = 0;
33 svc->use_flexible_mode = 0;
34
35 for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
36 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
37 int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
38 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
39 RATE_CONTROL *const lrc = &lc->rc;
40 PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
41 lrc->ni_av_qi = oxcf->rc_cfg.worst_allowed_q;
42 lp_rc->total_actual_bits = 0;
43 lrc->ni_tot_qi = 0;
44 lp_rc->tot_q = 0.0;
45 lp_rc->avg_q = 0.0;
46 lp_rc->ni_frames = 0;
47 lrc->decimation_count = 0;
48 lrc->decimation_factor = 0;
49 lrc->worst_quality = av1_quantizer_to_qindex(lc->max_q);
50 lrc->best_quality = av1_quantizer_to_qindex(lc->min_q);
51 for (int i = 0; i < RATE_FACTOR_LEVELS; ++i) {
52 lp_rc->rate_correction_factors[i] = 1.0;
53 }
54 lc->target_bandwidth = lc->layer_target_bitrate;
55 lp_rc->last_q[INTER_FRAME] = lrc->worst_quality;
56 lp_rc->avg_frame_qindex[INTER_FRAME] = lrc->worst_quality;
57 lp_rc->avg_frame_qindex[KEY_FRAME] = lrc->worst_quality;
58 lp_rc->buffer_level =
59 oxcf->rc_cfg.starting_buffer_level_ms * lc->target_bandwidth / 1000;
60 lp_rc->bits_off_target = lp_rc->buffer_level;
61 // Initialize the cyclic refresh parameters. If spatial layers are used
62 // (i.e., ss_number_layers > 1), these need to be updated per spatial
63 // layer. Cyclic refresh is only applied on base temporal layer.
64 if (svc->number_spatial_layers > 1 && tl == 0) {
65 size_t last_coded_q_map_size;
66 lc->sb_index = 0;
67 lc->actual_num_seg1_blocks = 0;
68 lc->actual_num_seg2_blocks = 0;
69 lc->counter_encode_maxq_scene_change = 0;
70 if (lc->map) aom_free(lc->map);
71 CHECK_MEM_ERROR(cm, lc->map,
72 aom_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
73 memset(lc->map, 0, mi_rows * mi_cols);
74 last_coded_q_map_size =
75 mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
76 if (lc->last_coded_q_map) aom_free(lc->last_coded_q_map);
77 CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
78 aom_malloc(last_coded_q_map_size));
79 assert(MAXQ <= 255);
80 memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
81 }
82 }
83 svc->downsample_filter_type[sl] = BILINEAR;
84 svc->downsample_filter_phase[sl] = 8;
85 }
86 if (svc->number_spatial_layers == 3) {
87 svc->downsample_filter_type[0] = EIGHTTAP_SMOOTH;
88 }
89 svc->ref_frame_comp[0] = 0;
90 svc->ref_frame_comp[1] = 0;
91 svc->ref_frame_comp[2] = 0;
92 }
93
94 // Update the layer context from a change_config() call.
av1_update_layer_context_change_config(AV1_COMP * const cpi,const int64_t target_bandwidth)95 void av1_update_layer_context_change_config(AV1_COMP *const cpi,
96 const int64_t target_bandwidth) {
97 const RATE_CONTROL *const rc = &cpi->rc;
98 const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
99 SVC *const svc = &cpi->svc;
100 int layer = 0;
101 int64_t spatial_layer_target = 0;
102 float bitrate_alloc = 1.0;
103
104 for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
105 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
106 layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
107 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
108 svc->layer_context[layer].target_bandwidth = lc->layer_target_bitrate;
109 }
110 spatial_layer_target = svc->layer_context[layer].target_bandwidth;
111 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
112 LAYER_CONTEXT *const lc =
113 &svc->layer_context[sl * svc->number_temporal_layers + tl];
114 RATE_CONTROL *const lrc = &lc->rc;
115 PRIMARY_RATE_CONTROL *const lp_rc = &lc->p_rc;
116 lc->spatial_layer_target_bandwidth = spatial_layer_target;
117 bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
118 lp_rc->starting_buffer_level =
119 (int64_t)(p_rc->starting_buffer_level * bitrate_alloc);
120 lp_rc->optimal_buffer_level =
121 (int64_t)(p_rc->optimal_buffer_level * bitrate_alloc);
122 lp_rc->maximum_buffer_size =
123 (int64_t)(p_rc->maximum_buffer_size * bitrate_alloc);
124 lp_rc->bits_off_target =
125 AOMMIN(lp_rc->bits_off_target, lp_rc->maximum_buffer_size);
126 lp_rc->buffer_level =
127 AOMMIN(lp_rc->buffer_level, lp_rc->maximum_buffer_size);
128 lc->framerate = cpi->framerate / lc->framerate_factor;
129 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
130 lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
131 lrc->worst_quality = av1_quantizer_to_qindex(lc->max_q);
132 lrc->best_quality = av1_quantizer_to_qindex(lc->min_q);
133 }
134 }
135 }
136
137 /*!\brief Return layer context for current layer.
138 *
139 * \ingroup rate_control
140 * \param[in] cpi Top level encoder structure
141 *
142 * \return LAYER_CONTEXT for current layer.
143 */
get_layer_context(AV1_COMP * const cpi)144 static LAYER_CONTEXT *get_layer_context(AV1_COMP *const cpi) {
145 return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
146 cpi->svc.number_temporal_layers +
147 cpi->svc.temporal_layer_id];
148 }
149
av1_update_temporal_layer_framerate(AV1_COMP * const cpi)150 void av1_update_temporal_layer_framerate(AV1_COMP *const cpi) {
151 SVC *const svc = &cpi->svc;
152 LAYER_CONTEXT *const lc = get_layer_context(cpi);
153 RATE_CONTROL *const lrc = &lc->rc;
154 const int tl = svc->temporal_layer_id;
155 lc->framerate = cpi->framerate / lc->framerate_factor;
156 lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
157 lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
158 // Update the average layer frame size (non-cumulative per-frame-bw).
159 if (tl == 0) {
160 lc->avg_frame_size = lrc->avg_frame_bandwidth;
161 } else {
162 int prev_layer = svc->spatial_layer_id * svc->number_temporal_layers +
163 svc->temporal_layer_id - 1;
164 LAYER_CONTEXT *const lcprev = &svc->layer_context[prev_layer];
165 const double prev_layer_framerate =
166 cpi->framerate / lcprev->framerate_factor;
167 const int64_t prev_layer_target_bandwidth = lcprev->layer_target_bitrate;
168 lc->avg_frame_size =
169 (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
170 (lc->framerate - prev_layer_framerate));
171 }
172 }
173
av1_restore_layer_context(AV1_COMP * const cpi)174 void av1_restore_layer_context(AV1_COMP *const cpi) {
175 SVC *const svc = &cpi->svc;
176 const AV1_COMMON *const cm = &cpi->common;
177 LAYER_CONTEXT *const lc = get_layer_context(cpi);
178 const int old_frame_since_key = cpi->rc.frames_since_key;
179 const int old_frame_to_key = cpi->rc.frames_to_key;
180 // Restore layer rate control.
181 cpi->rc = lc->rc;
182 cpi->ppi->p_rc = lc->p_rc;
183 cpi->oxcf.rc_cfg.target_bandwidth = lc->target_bandwidth;
184 cpi->gf_frame_index = 0;
185 cpi->mv_search_params.max_mv_magnitude = lc->max_mv_magnitude;
186 if (cpi->mv_search_params.max_mv_magnitude == 0)
187 cpi->mv_search_params.max_mv_magnitude = AOMMAX(cm->width, cm->height);
188 // Reset the frames_since_key and frames_to_key counters to their values
189 // before the layer restore. Keep these defined for the stream (not layer).
190 cpi->rc.frames_since_key = old_frame_since_key;
191 cpi->rc.frames_to_key = old_frame_to_key;
192 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
193 // for the base temporal layer.
194 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
195 svc->number_spatial_layers > 1 && svc->temporal_layer_id == 0) {
196 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
197 swap_ptr(&cr->map, &lc->map);
198 swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
199 cr->sb_index = lc->sb_index;
200 cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
201 cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
202 }
203 svc->skip_mvsearch_last = 0;
204 svc->skip_mvsearch_gf = 0;
205 // For each reference (LAST/GOLDEN) set the skip_mvsearch_last/gf frame flags.
206 // This is to skip searching mv for that reference if it was last
207 // refreshed (i.e., buffer slot holding that reference was refreshed) on the
208 // previous spatial layer(s) at the same time (current_superframe).
209 if (svc->set_ref_frame_config && svc->force_zero_mode_spatial_ref) {
210 int ref_frame_idx = svc->ref_idx[LAST_FRAME - 1];
211 if (svc->buffer_time_index[ref_frame_idx] == svc->current_superframe &&
212 svc->buffer_spatial_layer[ref_frame_idx] <= svc->spatial_layer_id - 1)
213 svc->skip_mvsearch_last = 1;
214 ref_frame_idx = svc->ref_idx[GOLDEN_FRAME - 1];
215 if (svc->buffer_time_index[ref_frame_idx] == svc->current_superframe &&
216 svc->buffer_spatial_layer[ref_frame_idx] <= svc->spatial_layer_id - 1)
217 svc->skip_mvsearch_gf = 1;
218 }
219 }
220
av1_save_layer_context(AV1_COMP * const cpi)221 void av1_save_layer_context(AV1_COMP *const cpi) {
222 SVC *const svc = &cpi->svc;
223 const AV1_COMMON *const cm = &cpi->common;
224 LAYER_CONTEXT *lc = get_layer_context(cpi);
225 lc->rc = cpi->rc;
226 lc->p_rc = cpi->ppi->p_rc;
227 lc->target_bandwidth = (int)cpi->oxcf.rc_cfg.target_bandwidth;
228 lc->group_index = cpi->gf_frame_index;
229 lc->max_mv_magnitude = cpi->mv_search_params.max_mv_magnitude;
230 if (svc->spatial_layer_id == 0) svc->base_framerate = cpi->framerate;
231 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
232 // for the base temporal layer.
233 if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ &&
234 cpi->svc.number_spatial_layers > 1 && svc->temporal_layer_id == 0) {
235 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
236 signed char *temp = lc->map;
237 uint8_t *temp2 = lc->last_coded_q_map;
238 lc->map = cr->map;
239 cr->map = temp;
240 lc->last_coded_q_map = cr->last_coded_q_map;
241 cr->last_coded_q_map = temp2;
242 lc->sb_index = cr->sb_index;
243 lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
244 lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
245 }
246 // For any buffer slot that is refreshed, update it with
247 // the spatial_layer_id and the current_superframe.
248 if (cpi->common.current_frame.frame_type == KEY_FRAME) {
249 // All slots are refreshed on KEY.
250 for (unsigned int i = 0; i < REF_FRAMES; i++) {
251 svc->buffer_time_index[i] = svc->current_superframe;
252 svc->buffer_spatial_layer[i] = svc->spatial_layer_id;
253 }
254 } else if (cpi->svc.set_ref_frame_config) {
255 for (unsigned int i = 0; i < INTER_REFS_PER_FRAME; i++) {
256 int ref_frame_map_idx = svc->ref_idx[i];
257 if (cpi->svc.refresh[ref_frame_map_idx]) {
258 svc->buffer_time_index[ref_frame_map_idx] = svc->current_superframe;
259 svc->buffer_spatial_layer[ref_frame_map_idx] = svc->spatial_layer_id;
260 }
261 }
262 }
263 for (unsigned int i = 0; i < REF_FRAMES; i++) {
264 if (frame_is_intra_only(cm) ||
265 cm->current_frame.refresh_frame_flags & (1 << i)) {
266 svc->spatial_layer_fb[i] = svc->spatial_layer_id;
267 svc->temporal_layer_fb[i] = svc->temporal_layer_id;
268 }
269 }
270 if (svc->spatial_layer_id == svc->number_spatial_layers - 1)
271 svc->current_superframe++;
272 }
273
av1_svc_primary_ref_frame(const AV1_COMP * const cpi)274 int av1_svc_primary_ref_frame(const AV1_COMP *const cpi) {
275 const SVC *const svc = &cpi->svc;
276 const AV1_COMMON *const cm = &cpi->common;
277 int wanted_fb = -1;
278 int primary_ref_frame = PRIMARY_REF_NONE;
279 for (unsigned int i = 0; i < REF_FRAMES; i++) {
280 if (svc->spatial_layer_fb[i] == svc->spatial_layer_id &&
281 svc->temporal_layer_fb[i] == svc->temporal_layer_id) {
282 wanted_fb = i;
283 break;
284 }
285 }
286 if (wanted_fb != -1) {
287 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
288 if (get_ref_frame_map_idx(cm, ref_frame) == wanted_fb) {
289 primary_ref_frame = ref_frame - LAST_FRAME;
290 break;
291 }
292 }
293 }
294 return primary_ref_frame;
295 }
296
av1_free_svc_cyclic_refresh(AV1_COMP * const cpi)297 void av1_free_svc_cyclic_refresh(AV1_COMP *const cpi) {
298 SVC *const svc = &cpi->svc;
299 for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
300 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
301 int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
302 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
303 if (lc->map) aom_free(lc->map);
304 if (lc->last_coded_q_map) aom_free(lc->last_coded_q_map);
305 }
306 }
307 }
308
av1_svc_reset_temporal_layers(AV1_COMP * const cpi,int is_key)309 void av1_svc_reset_temporal_layers(AV1_COMP *const cpi, int is_key) {
310 SVC *const svc = &cpi->svc;
311 LAYER_CONTEXT *lc = NULL;
312 for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
313 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
314 lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
315 if (is_key) lc->frames_from_key_frame = 0;
316 }
317 }
318 av1_update_temporal_layer_framerate(cpi);
319 av1_restore_layer_context(cpi);
320 }
321
av1_get_layer_resolution(const int width_org,const int height_org,const int num,const int den,int * width_out,int * height_out)322 void av1_get_layer_resolution(const int width_org, const int height_org,
323 const int num, const int den, int *width_out,
324 int *height_out) {
325 int w, h;
326 if (width_out == NULL || height_out == NULL || den == 0) return;
327 w = width_org * num / den;
328 h = height_org * num / den;
329 // Make height and width even.
330 w += w % 2;
331 h += h % 2;
332 *width_out = w;
333 *height_out = h;
334 }
335
av1_one_pass_cbr_svc_start_layer(AV1_COMP * const cpi)336 void av1_one_pass_cbr_svc_start_layer(AV1_COMP *const cpi) {
337 SVC *const svc = &cpi->svc;
338 LAYER_CONTEXT *lc = NULL;
339 int width = 0, height = 0;
340 lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
341 svc->temporal_layer_id];
342 av1_get_layer_resolution(cpi->oxcf.frm_dim_cfg.width,
343 cpi->oxcf.frm_dim_cfg.height, lc->scaling_factor_num,
344 lc->scaling_factor_den, &width, &height);
345 // Use Eightap_smooth for low resolutions.
346 if (width * height <= 320 * 240)
347 svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
348
349 cpi->common.width = width;
350 cpi->common.height = height;
351 av1_update_frame_size(cpi);
352 }
353
354 enum {
355 SVC_LAST_FRAME = 0,
356 SVC_LAST2_FRAME,
357 SVC_LAST3_FRAME,
358 SVC_GOLDEN_FRAME,
359 SVC_BWDREF_FRAME,
360 SVC_ALTREF2_FRAME,
361 SVC_ALTREF_FRAME
362 };
363
364 // For fixed svc mode: fixed pattern is set based on the number of
365 // spatial and temporal layers, and the ksvc_fixed_mode.
av1_set_svc_fixed_mode(AV1_COMP * const cpi)366 void av1_set_svc_fixed_mode(AV1_COMP *const cpi) {
367 SVC *const svc = &cpi->svc;
368 int i;
369 assert(svc->use_flexible_mode == 0);
370 // Fixed SVC mode only supports at most 3 spatial or temporal layers.
371 assert(svc->number_spatial_layers >= 1 && svc->number_spatial_layers <= 3 &&
372 svc->number_temporal_layers >= 1 && svc->number_temporal_layers <= 3);
373 svc->set_ref_frame_config = 1;
374 int superframe_cnt = svc->current_superframe;
375 // Set the reference map buffer idx for the 7 references:
376 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
377 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
378 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = i;
379 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->reference[i] = 0;
380 for (i = 0; i < REF_FRAMES; i++) svc->refresh[i] = 0;
381 // Always reference LAST, and reference GOLDEN on SL > 0.
382 // For KSVC: GOLDEN reference will be removed on INTER_FRAMES later
383 // when frame_type is set.
384 svc->reference[SVC_LAST_FRAME] = 1;
385 if (svc->spatial_layer_id > 0) svc->reference[SVC_GOLDEN_FRAME] = 1;
386 if (svc->temporal_layer_id == 0) {
387 // Base temporal layer.
388 if (svc->spatial_layer_id == 0) {
389 // Set all buffer_idx to 0. Update slot 0 (LAST).
390 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
391 svc->refresh[0] = 1;
392 } else if (svc->spatial_layer_id == 1) {
393 // Set buffer_idx for LAST to slot 1, GOLDEN (and all other refs) to
394 // slot 0. Update slot 1 (LAST).
395 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
396 svc->ref_idx[SVC_LAST_FRAME] = 1;
397 svc->refresh[1] = 1;
398 } else if (svc->spatial_layer_id == 2) {
399 // Set buffer_idx for LAST to slot 2, GOLDEN (and all other refs) to
400 // slot 1. Update slot 2 (LAST).
401 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 1;
402 svc->ref_idx[SVC_LAST_FRAME] = 2;
403 svc->refresh[2] = 1;
404 }
405 } else if (svc->temporal_layer_id == 2 && (superframe_cnt - 1) % 4 == 0) {
406 // First top temporal enhancement layer.
407 if (svc->spatial_layer_id == 0) {
408 // Reference LAST (slot 0).
409 // Set GOLDEN to slot 3 and update slot 3.
410 // Set all other buffer_idx to slot 0.
411 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
412 if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
413 svc->ref_idx[SVC_GOLDEN_FRAME] = 3;
414 svc->refresh[3] = 1;
415 }
416 } else if (svc->spatial_layer_id == 1) {
417 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
418 // GOLDEN (and all other refs) to slot 3.
419 // Set LAST2 to slot 4 and Update slot 4.
420 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 3;
421 svc->ref_idx[SVC_LAST_FRAME] = 1;
422 if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
423 svc->ref_idx[SVC_LAST2_FRAME] = 4;
424 svc->refresh[4] = 1;
425 }
426 } else if (svc->spatial_layer_id == 2) {
427 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
428 // GOLDEN (and all other refs) to slot 4.
429 // No update.
430 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 4;
431 svc->ref_idx[SVC_LAST_FRAME] = 2;
432 }
433 } else if (svc->temporal_layer_id == 1) {
434 // Middle temporal enhancement layer.
435 if (svc->spatial_layer_id == 0) {
436 // Reference LAST.
437 // Set all buffer_idx to 0.
438 // Set GOLDEN to slot 5 and update slot 5.
439 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
440 if (svc->temporal_layer_id < svc->number_temporal_layers - 1) {
441 svc->ref_idx[SVC_GOLDEN_FRAME] = 5;
442 svc->refresh[5] = 1;
443 }
444 } else if (svc->spatial_layer_id == 1) {
445 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
446 // GOLDEN (and all other refs) to slot 5.
447 // Set LAST3 to slot 6 and update slot 6.
448 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 5;
449 svc->ref_idx[SVC_LAST_FRAME] = 1;
450 if (svc->temporal_layer_id < svc->number_temporal_layers - 1) {
451 svc->ref_idx[SVC_LAST3_FRAME] = 6;
452 svc->refresh[6] = 1;
453 }
454 } else if (svc->spatial_layer_id == 2) {
455 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
456 // GOLDEN (and all other refs) to slot 6.
457 // Set LAST3 to slot 7 and update slot 7.
458 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 6;
459 svc->ref_idx[SVC_LAST_FRAME] = 2;
460 if (svc->temporal_layer_id < svc->number_temporal_layers - 1) {
461 svc->ref_idx[SVC_LAST3_FRAME] = 7;
462 svc->refresh[7] = 1;
463 }
464 }
465 } else if (svc->temporal_layer_id == 2 && (superframe_cnt - 3) % 4 == 0) {
466 // Second top temporal enhancement layer.
467 if (svc->spatial_layer_id == 0) {
468 // Set LAST to slot 5 and reference LAST.
469 // Set GOLDEN to slot 3 and update slot 3.
470 // Set all other buffer_idx to 0.
471 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
472 svc->ref_idx[SVC_LAST_FRAME] = 5;
473 if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
474 svc->ref_idx[SVC_GOLDEN_FRAME] = 3;
475 svc->refresh[3] = 1;
476 }
477 } else if (svc->spatial_layer_id == 1) {
478 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
479 // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
480 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
481 svc->ref_idx[SVC_LAST_FRAME] = 6;
482 svc->ref_idx[SVC_GOLDEN_FRAME] = 3;
483 if (svc->spatial_layer_id < svc->number_spatial_layers - 1) {
484 svc->ref_idx[SVC_LAST2_FRAME] = 4;
485 svc->refresh[4] = 1;
486 }
487 } else if (svc->spatial_layer_id == 2) {
488 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
489 // GOLDEN to slot 4. No update.
490 for (i = 0; i < INTER_REFS_PER_FRAME; i++) svc->ref_idx[i] = 0;
491 svc->ref_idx[SVC_LAST_FRAME] = 7;
492 svc->ref_idx[SVC_GOLDEN_FRAME] = 4;
493 }
494 }
495 }
496
av1_svc_check_reset_layer_rc_flag(AV1_COMP * const cpi)497 void av1_svc_check_reset_layer_rc_flag(AV1_COMP *const cpi) {
498 SVC *const svc = &cpi->svc;
499 for (int sl = 0; sl < svc->number_spatial_layers; ++sl) {
500 // Check for reset based on avg_frame_bandwidth for spatial layer sl.
501 int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
502 svc->number_temporal_layers);
503 LAYER_CONTEXT *lc = &svc->layer_context[layer];
504 RATE_CONTROL *lrc = &lc->rc;
505 if (lrc->avg_frame_bandwidth > (3 * lrc->prev_avg_frame_bandwidth >> 1) ||
506 lrc->avg_frame_bandwidth < (lrc->prev_avg_frame_bandwidth >> 1)) {
507 // Reset for all temporal layers with spatial layer sl.
508 for (int tl = 0; tl < svc->number_temporal_layers; ++tl) {
509 int layer2 = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
510 LAYER_CONTEXT *lc2 = &svc->layer_context[layer2];
511 RATE_CONTROL *lrc2 = &lc2->rc;
512 PRIMARY_RATE_CONTROL *lp_rc2 = &lc2->p_rc;
513 PRIMARY_RATE_CONTROL *const lp_rc = &lc2->p_rc;
514 lrc2->rc_1_frame = 0;
515 lrc2->rc_2_frame = 0;
516 lp_rc2->bits_off_target = lp_rc->optimal_buffer_level;
517 lp_rc2->buffer_level = lp_rc->optimal_buffer_level;
518 }
519 }
520 }
521 }
522