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