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