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