1 /*
2  *  Copyright (c) 2014 The WebM project authors. 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 "vp9/encoder/vp9_aq_cyclicrefresh.h"
14 #include "vp9/encoder/vp9_encoder.h"
15 #include "vp9/encoder/vp9_svc_layercontext.h"
16 #include "vp9/encoder/vp9_extend.h"
17 #include "vpx_dsp/vpx_dsp_common.h"
18 
19 #define SMALL_FRAME_WIDTH 32
20 #define SMALL_FRAME_HEIGHT 16
21 
swap_ptr(void * a,void * b)22 static void swap_ptr(void *a, void *b) {
23   void **a_p = (void **)a;
24   void **b_p = (void **)b;
25   void *c = *a_p;
26   *a_p = *b_p;
27   *b_p = c;
28 }
29 
vp9_init_layer_context(VP9_COMP * const cpi)30 void vp9_init_layer_context(VP9_COMP *const cpi) {
31   SVC *const svc = &cpi->svc;
32   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
33   int mi_rows = cpi->common.mi_rows;
34   int mi_cols = cpi->common.mi_cols;
35   int sl, tl, i;
36   int alt_ref_idx = svc->number_spatial_layers;
37 
38   svc->spatial_layer_id = 0;
39   svc->temporal_layer_id = 0;
40   svc->force_zero_mode_spatial_ref = 0;
41   svc->use_base_mv = 0;
42   svc->use_partition_reuse = 0;
43   svc->use_gf_temporal_ref = 1;
44   svc->use_gf_temporal_ref_current_layer = 0;
45   svc->scaled_temp_is_alloc = 0;
46   svc->scaled_one_half = 0;
47   svc->current_superframe = 0;
48   svc->non_reference_frame = 0;
49   svc->skip_enhancement_layer = 0;
50   svc->disable_inter_layer_pred = INTER_LAYER_PRED_ON;
51   svc->framedrop_mode = CONSTRAINED_LAYER_DROP;
52   svc->set_intra_only_frame = 0;
53   svc->previous_frame_is_intra_only = 0;
54   svc->superframe_has_layer_sync = 0;
55   svc->use_set_ref_frame_config = 0;
56   svc->num_encoded_top_layer = 0;
57   svc->simulcast_mode = 0;
58 
59   for (i = 0; i < REF_FRAMES; ++i) {
60     svc->fb_idx_spatial_layer_id[i] = -1;
61     svc->fb_idx_temporal_layer_id[i] = -1;
62     svc->fb_idx_base[i] = 0;
63   }
64   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
65     svc->last_layer_dropped[sl] = 0;
66     svc->drop_spatial_layer[sl] = 0;
67     svc->ext_frame_flags[sl] = 0;
68     svc->lst_fb_idx[sl] = 0;
69     svc->gld_fb_idx[sl] = 1;
70     svc->alt_fb_idx[sl] = 2;
71     svc->downsample_filter_type[sl] = BILINEAR;
72     svc->downsample_filter_phase[sl] = 8;  // Set to 8 for averaging filter.
73     svc->framedrop_thresh[sl] = oxcf->drop_frames_water_mark;
74     svc->fb_idx_upd_tl0[sl] = -1;
75     svc->drop_count[sl] = 0;
76     svc->spatial_layer_sync[sl] = 0;
77   }
78   svc->max_consec_drop = INT_MAX;
79 
80   svc->buffer_gf_temporal_ref[1].idx = 7;
81   svc->buffer_gf_temporal_ref[0].idx = 6;
82   svc->buffer_gf_temporal_ref[1].is_used = 0;
83   svc->buffer_gf_temporal_ref[0].is_used = 0;
84 
85   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
86     if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
87                                  SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
88                                  cpi->common.subsampling_y,
89 #if CONFIG_VP9_HIGHBITDEPTH
90                                  cpi->common.use_highbitdepth,
91 #endif
92                                  VP9_ENC_BORDER_IN_PIXELS,
93                                  cpi->common.byte_alignment, NULL, NULL, NULL))
94       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
95                          "Failed to allocate empty frame for multiple frame "
96                          "contexts");
97 
98     memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
99            cpi->svc.empty_frame.img.buffer_alloc_sz);
100   }
101 
102   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
103     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
104       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
105       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
106       RATE_CONTROL *const lrc = &lc->rc;
107       int i;
108       lc->current_video_frame_in_layer = 0;
109       lc->layer_size = 0;
110       lc->frames_from_key_frame = 0;
111       lc->last_frame_type = FRAME_TYPES;
112       lrc->ni_av_qi = oxcf->worst_allowed_q;
113       lrc->total_actual_bits = 0;
114       lrc->total_target_vs_actual = 0;
115       lrc->ni_tot_qi = 0;
116       lrc->tot_q = 0.0;
117       lrc->avg_q = 0.0;
118       lrc->ni_frames = 0;
119       lrc->decimation_count = 0;
120       lrc->decimation_factor = 0;
121       lrc->worst_quality = oxcf->worst_allowed_q;
122       lrc->best_quality = oxcf->best_allowed_q;
123 
124       for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
125         lrc->rate_correction_factors[i] = 1.0;
126       }
127 
128       if (cpi->oxcf.rc_mode == VPX_CBR) {
129         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
130         lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
131         lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
132         lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
133       } else {
134         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
135         lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
136         lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
137         lrc->avg_frame_qindex[KEY_FRAME] =
138             (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
139         lrc->avg_frame_qindex[INTER_FRAME] =
140             (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
141         if (oxcf->ss_enable_auto_arf[sl])
142           lc->alt_ref_idx = alt_ref_idx++;
143         else
144           lc->alt_ref_idx = INVALID_IDX;
145         lc->gold_ref_idx = INVALID_IDX;
146       }
147 
148       lrc->buffer_level =
149           oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
150       lrc->bits_off_target = lrc->buffer_level;
151 
152       // Initialize the cyclic refresh parameters. If spatial layers are used
153       // (i.e., ss_number_layers > 1), these need to be updated per spatial
154       // layer.
155       // Cyclic refresh is only applied on base temporal layer.
156       if (oxcf->ss_number_layers > 1 && tl == 0) {
157         size_t last_coded_q_map_size;
158         size_t consec_zero_mv_size;
159         VP9_COMMON *const cm = &cpi->common;
160         lc->sb_index = 0;
161         lc->actual_num_seg1_blocks = 0;
162         lc->actual_num_seg2_blocks = 0;
163         lc->counter_encode_maxq_scene_change = 0;
164         CHECK_MEM_ERROR(cm, lc->map,
165                         vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
166         memset(lc->map, 0, mi_rows * mi_cols);
167         last_coded_q_map_size =
168             mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
169         CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
170                         vpx_malloc(last_coded_q_map_size));
171         assert(MAXQ <= 255);
172         memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
173         consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
174         CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
175                         vpx_malloc(consec_zero_mv_size));
176         memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
177       }
178     }
179   }
180 
181   // Still have extra buffer for base layer golden frame
182   if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
183       alt_ref_idx < REF_FRAMES)
184     svc->layer_context[0].gold_ref_idx = alt_ref_idx;
185 }
186 
187 // Update the layer context from a change_config() call.
vp9_update_layer_context_change_config(VP9_COMP * const cpi,const int target_bandwidth)188 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
189                                             const int target_bandwidth) {
190   SVC *const svc = &cpi->svc;
191   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
192   const RATE_CONTROL *const rc = &cpi->rc;
193   int sl, tl, layer = 0, spatial_layer_target;
194   float bitrate_alloc = 1.0;
195 
196   cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
197 
198   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
199     for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
200       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
201         layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
202         svc->layer_context[layer].target_bandwidth =
203             oxcf->layer_target_bitrate[layer];
204       }
205 
206       layer = LAYER_IDS_TO_IDX(
207           sl,
208           ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
209           oxcf->ts_number_layers);
210       spatial_layer_target = svc->layer_context[layer].target_bandwidth =
211           oxcf->layer_target_bitrate[layer];
212 
213       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
214         LAYER_CONTEXT *const lc =
215             &svc->layer_context[sl * oxcf->ts_number_layers + tl];
216         RATE_CONTROL *const lrc = &lc->rc;
217 
218         lc->spatial_layer_target_bandwidth = spatial_layer_target;
219         bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
220         lrc->starting_buffer_level =
221             (int64_t)(rc->starting_buffer_level * bitrate_alloc);
222         lrc->optimal_buffer_level =
223             (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
224         lrc->maximum_buffer_size =
225             (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
226         lrc->bits_off_target =
227             VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
228         lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
229         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
230         lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
231         lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
232         lrc->worst_quality = rc->worst_quality;
233         lrc->best_quality = rc->best_quality;
234       }
235     }
236   } else {
237     int layer_end;
238 
239     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
240       layer_end = svc->number_temporal_layers;
241     } else {
242       layer_end = svc->number_spatial_layers;
243     }
244 
245     for (layer = 0; layer < layer_end; ++layer) {
246       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
247       RATE_CONTROL *const lrc = &lc->rc;
248 
249       lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
250 
251       bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
252       // Update buffer-related quantities.
253       lrc->starting_buffer_level =
254           (int64_t)(rc->starting_buffer_level * bitrate_alloc);
255       lrc->optimal_buffer_level =
256           (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
257       lrc->maximum_buffer_size =
258           (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
259       lrc->bits_off_target =
260           VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
261       lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
262       // Update framerate-related quantities.
263       if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
264         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
265       } else {
266         lc->framerate = cpi->framerate;
267       }
268       lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
269       lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
270       // Update qp-related quantities.
271       lrc->worst_quality = rc->worst_quality;
272       lrc->best_quality = rc->best_quality;
273     }
274   }
275 }
276 
get_layer_context(VP9_COMP * const cpi)277 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
278   if (is_one_pass_cbr_svc(cpi))
279     return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
280                                        cpi->svc.number_temporal_layers +
281                                    cpi->svc.temporal_layer_id];
282   else
283     return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
284                ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
285                : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
286 }
287 
vp9_update_temporal_layer_framerate(VP9_COMP * const cpi)288 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
289   SVC *const svc = &cpi->svc;
290   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
291   LAYER_CONTEXT *const lc = get_layer_context(cpi);
292   RATE_CONTROL *const lrc = &lc->rc;
293   // Index into spatial+temporal arrays.
294   const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
295                      svc->temporal_layer_id;
296   const int tl = svc->temporal_layer_id;
297 
298   lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
299   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
300   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
301   // Update the average layer frame size (non-cumulative per-frame-bw).
302   if (tl == 0) {
303     lc->avg_frame_size = lrc->avg_frame_bandwidth;
304   } else {
305     const double prev_layer_framerate =
306         cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
307     const int prev_layer_target_bandwidth =
308         oxcf->layer_target_bitrate[st_idx - 1];
309     lc->avg_frame_size =
310         (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
311               (lc->framerate - prev_layer_framerate));
312   }
313 }
314 
vp9_update_spatial_layer_framerate(VP9_COMP * const cpi,double framerate)315 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
316   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
317   LAYER_CONTEXT *const lc = get_layer_context(cpi);
318   RATE_CONTROL *const lrc = &lc->rc;
319 
320   lc->framerate = framerate;
321   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
322   lrc->min_frame_bandwidth =
323       (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
324   lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
325                                     oxcf->two_pass_vbrmax_section) /
326                                    100);
327   vp9_rc_set_gf_interval_range(cpi, lrc);
328 }
329 
vp9_restore_layer_context(VP9_COMP * const cpi)330 void vp9_restore_layer_context(VP9_COMP *const cpi) {
331   LAYER_CONTEXT *const lc = get_layer_context(cpi);
332   const int old_frame_since_key = cpi->rc.frames_since_key;
333   const int old_frame_to_key = cpi->rc.frames_to_key;
334   const int old_ext_use_post_encode_drop = cpi->rc.ext_use_post_encode_drop;
335 
336   cpi->rc = lc->rc;
337   cpi->twopass = lc->twopass;
338   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
339   cpi->alt_ref_source = lc->alt_ref_source;
340   // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
341   // does not use speed = 0).
342   if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
343     cpi->oxcf.speed = lc->speed;
344   }
345   // Reset the frames_since_key and frames_to_key counters to their values
346   // before the layer restore. Keep these defined for the stream (not layer).
347   if (cpi->svc.number_temporal_layers > 1 ||
348       cpi->svc.number_spatial_layers > 1) {
349     cpi->rc.frames_since_key = old_frame_since_key;
350     cpi->rc.frames_to_key = old_frame_to_key;
351   }
352   cpi->rc.ext_use_post_encode_drop = old_ext_use_post_encode_drop;
353   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
354   // for the base temporal layer.
355   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
356       cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
357     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
358     swap_ptr(&cr->map, &lc->map);
359     swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
360     swap_ptr(&cpi->consec_zero_mv, &lc->consec_zero_mv);
361     cr->sb_index = lc->sb_index;
362     cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
363     cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
364     cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
365   }
366 }
367 
vp9_save_layer_context(VP9_COMP * const cpi)368 void vp9_save_layer_context(VP9_COMP *const cpi) {
369   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
370   LAYER_CONTEXT *const lc = get_layer_context(cpi);
371 
372   lc->rc = cpi->rc;
373   lc->twopass = cpi->twopass;
374   lc->target_bandwidth = (int)oxcf->target_bandwidth;
375   lc->alt_ref_source = cpi->alt_ref_source;
376 
377   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
378   // for the base temporal layer.
379   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
380       cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
381     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
382     signed char *temp = lc->map;
383     uint8_t *temp2 = lc->last_coded_q_map;
384     uint8_t *temp3 = lc->consec_zero_mv;
385     lc->map = cr->map;
386     cr->map = temp;
387     lc->last_coded_q_map = cr->last_coded_q_map;
388     cr->last_coded_q_map = temp2;
389     lc->consec_zero_mv = cpi->consec_zero_mv;
390     cpi->consec_zero_mv = temp3;
391     lc->sb_index = cr->sb_index;
392     lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
393     lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
394     lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
395   }
396 }
397 
398 #if !CONFIG_REALTIME_ONLY
vp9_init_second_pass_spatial_svc(VP9_COMP * cpi)399 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
400   SVC *const svc = &cpi->svc;
401   int i;
402 
403   for (i = 0; i < svc->number_spatial_layers; ++i) {
404     TWO_PASS *const twopass = &svc->layer_context[i].twopass;
405 
406     svc->spatial_layer_id = i;
407     vp9_init_second_pass(cpi);
408 
409     twopass->total_stats.spatial_layer_id = i;
410     twopass->total_left_stats.spatial_layer_id = i;
411   }
412   svc->spatial_layer_id = 0;
413 }
414 #endif  // !CONFIG_REALTIME_ONLY
415 
vp9_inc_frame_in_layer(VP9_COMP * const cpi)416 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
417   LAYER_CONTEXT *const lc =
418       &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
419                               cpi->svc.number_temporal_layers];
420   ++lc->current_video_frame_in_layer;
421   ++lc->frames_from_key_frame;
422   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
423     ++cpi->svc.current_superframe;
424 }
425 
get_layer_resolution(const int width_org,const int height_org,const int num,const int den,int * width_out,int * height_out)426 void get_layer_resolution(const int width_org, const int height_org,
427                           const int num, const int den, int *width_out,
428                           int *height_out) {
429   int w, h;
430 
431   if (width_out == NULL || height_out == NULL || den == 0) return;
432 
433   w = width_org * num / den;
434   h = height_org * num / den;
435 
436   // make height and width even to make chrome player happy
437   w += w % 2;
438   h += h % 2;
439 
440   *width_out = w;
441   *height_out = h;
442 }
443 
reset_fb_idx_unused(VP9_COMP * const cpi)444 static void reset_fb_idx_unused(VP9_COMP *const cpi) {
445   // If a reference frame is not referenced or refreshed, then set the
446   // fb_idx for that reference to the first one used/referenced.
447   // This is to avoid setting fb_idx for a reference to a slot that is not
448   // used/needed (i.e., since that reference is not referenced or refreshed).
449   static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
450                                     VP9_ALT_FLAG };
451   MV_REFERENCE_FRAME ref_frame;
452   MV_REFERENCE_FRAME first_ref = 0;
453   int first_fb_idx = 0;
454   int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
455   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
456     if (cpi->ref_frame_flags & flag_list[ref_frame]) {
457       first_ref = ref_frame;
458       first_fb_idx = fb_idx[ref_frame - 1];
459       break;
460     }
461   }
462   if (first_ref > 0) {
463     if (first_ref != LAST_FRAME &&
464         !(cpi->ref_frame_flags & flag_list[LAST_FRAME]) &&
465         !cpi->ext_refresh_last_frame)
466       cpi->lst_fb_idx = first_fb_idx;
467     else if (first_ref != GOLDEN_FRAME &&
468              !(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]) &&
469              !cpi->ext_refresh_golden_frame)
470       cpi->gld_fb_idx = first_fb_idx;
471     else if (first_ref != ALTREF_FRAME &&
472              !(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]) &&
473              !cpi->ext_refresh_alt_ref_frame)
474       cpi->alt_fb_idx = first_fb_idx;
475   }
476 }
477 
478 // Never refresh any reference frame buffers on top temporal layers in
479 // simulcast mode, which has interlayer prediction disabled.
non_reference_frame_simulcast(VP9_COMP * const cpi)480 static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
481   if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
482       cpi->svc.temporal_layer_id > 0) {
483     cpi->ext_refresh_last_frame = 0;
484     cpi->ext_refresh_golden_frame = 0;
485     cpi->ext_refresh_alt_ref_frame = 0;
486   }
487 }
488 
489 // The function sets proper ref_frame_flags, buffer indices, and buffer update
490 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
491 // scheme.
set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP * const cpi)492 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
493   int frame_num_within_temporal_struct = 0;
494   int spatial_id, temporal_id;
495   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
496   frame_num_within_temporal_struct =
497       cpi->svc
498           .layer_context[cpi->svc.spatial_layer_id *
499                          cpi->svc.number_temporal_layers]
500           .current_video_frame_in_layer %
501       4;
502   temporal_id = cpi->svc.temporal_layer_id =
503       (frame_num_within_temporal_struct & 1)
504           ? 2
505           : (frame_num_within_temporal_struct >> 1);
506   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
507       cpi->ext_refresh_alt_ref_frame = 0;
508   if (!temporal_id) {
509     cpi->ext_refresh_frame_flags_pending = 1;
510     cpi->ext_refresh_last_frame = 1;
511     if (!spatial_id) {
512       cpi->ref_frame_flags = VP9_LAST_FLAG;
513     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
514       // base layer is a key frame.
515       cpi->ref_frame_flags = VP9_LAST_FLAG;
516       cpi->ext_refresh_last_frame = 0;
517       cpi->ext_refresh_golden_frame = 1;
518     } else {
519       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
520     }
521   } else if (temporal_id == 1) {
522     cpi->ext_refresh_frame_flags_pending = 1;
523     cpi->ext_refresh_alt_ref_frame = 1;
524     if (!spatial_id) {
525       cpi->ref_frame_flags = VP9_LAST_FLAG;
526     } else {
527       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
528     }
529   } else {
530     if (frame_num_within_temporal_struct == 1) {
531       // the first tl2 picture
532       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
533         cpi->ext_refresh_frame_flags_pending = 1;
534         if (!spatial_id)
535           cpi->ref_frame_flags = VP9_LAST_FLAG;
536         else
537           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
538       } else if (!spatial_id) {
539         cpi->ext_refresh_frame_flags_pending = 1;
540         cpi->ext_refresh_alt_ref_frame = 1;
541         cpi->ref_frame_flags = VP9_LAST_FLAG;
542       } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
543         cpi->ext_refresh_frame_flags_pending = 1;
544         cpi->ext_refresh_alt_ref_frame = 1;
545         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
546       }
547     } else {
548       //  The second tl2 picture
549       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
550         cpi->ext_refresh_frame_flags_pending = 1;
551         if (!spatial_id)
552           cpi->ref_frame_flags = VP9_LAST_FLAG;
553         else
554           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
555       } else if (!spatial_id) {
556         cpi->ext_refresh_frame_flags_pending = 1;
557         cpi->ref_frame_flags = VP9_LAST_FLAG;
558         cpi->ext_refresh_alt_ref_frame = 1;
559       } else {  // top layer
560         cpi->ext_refresh_frame_flags_pending = 1;
561         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
562         cpi->ext_refresh_alt_ref_frame = 1;
563       }
564     }
565   }
566   if (temporal_id == 0) {
567     cpi->lst_fb_idx = spatial_id;
568     if (spatial_id) {
569       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
570         cpi->lst_fb_idx = spatial_id - 1;
571         cpi->gld_fb_idx = spatial_id;
572       } else {
573         cpi->gld_fb_idx = spatial_id - 1;
574       }
575     } else {
576       cpi->gld_fb_idx = 0;
577     }
578     cpi->alt_fb_idx = 0;
579   } else if (temporal_id == 1) {
580     cpi->lst_fb_idx = spatial_id;
581     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
582     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
583   } else if (frame_num_within_temporal_struct == 1) {
584     cpi->lst_fb_idx = spatial_id;
585     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
586     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
587   } else {
588     cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
589     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
590     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
591   }
592 
593   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
594 
595   reset_fb_idx_unused(cpi);
596 }
597 
598 // The function sets proper ref_frame_flags, buffer indices, and buffer update
599 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
600 // scheme.
set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP * const cpi)601 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
602   int spatial_id, temporal_id;
603   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
604   temporal_id = cpi->svc.temporal_layer_id =
605       cpi->svc
606           .layer_context[cpi->svc.spatial_layer_id *
607                          cpi->svc.number_temporal_layers]
608           .current_video_frame_in_layer &
609       1;
610   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
611       cpi->ext_refresh_alt_ref_frame = 0;
612   if (!temporal_id) {
613     cpi->ext_refresh_frame_flags_pending = 1;
614     cpi->ext_refresh_last_frame = 1;
615     if (!spatial_id) {
616       cpi->ref_frame_flags = VP9_LAST_FLAG;
617     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
618       // base layer is a key frame.
619       cpi->ref_frame_flags = VP9_LAST_FLAG;
620       cpi->ext_refresh_last_frame = 0;
621       cpi->ext_refresh_golden_frame = 1;
622     } else {
623       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
624     }
625   } else if (temporal_id == 1) {
626     cpi->ext_refresh_frame_flags_pending = 1;
627     cpi->ext_refresh_alt_ref_frame = 1;
628     if (!spatial_id) {
629       cpi->ref_frame_flags = VP9_LAST_FLAG;
630     } else {
631       if (spatial_id == cpi->svc.number_spatial_layers - 1)
632         cpi->ext_refresh_alt_ref_frame = 0;
633       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
634     }
635   }
636 
637   if (temporal_id == 0) {
638     cpi->lst_fb_idx = spatial_id;
639     if (spatial_id) {
640       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
641         cpi->lst_fb_idx = spatial_id - 1;
642         cpi->gld_fb_idx = spatial_id;
643       } else {
644         cpi->gld_fb_idx = spatial_id - 1;
645       }
646     } else {
647       cpi->gld_fb_idx = 0;
648     }
649     cpi->alt_fb_idx = 0;
650   } else if (temporal_id == 1) {
651     cpi->lst_fb_idx = spatial_id;
652     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
653     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
654   }
655 
656   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
657 
658   reset_fb_idx_unused(cpi);
659 }
660 
661 // The function sets proper ref_frame_flags, buffer indices, and buffer update
662 // variables for temporal layering mode 0 - that has no temporal layering.
set_flags_and_fb_idx_for_temporal_mode_noLayering(VP9_COMP * const cpi)663 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
664     VP9_COMP *const cpi) {
665   int spatial_id;
666   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
667   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
668       cpi->ext_refresh_alt_ref_frame = 0;
669   cpi->ext_refresh_frame_flags_pending = 1;
670   cpi->ext_refresh_last_frame = 1;
671   if (!spatial_id) {
672     cpi->ref_frame_flags = VP9_LAST_FLAG;
673   } else if (cpi->svc.layer_context[0].is_key_frame) {
674     cpi->ref_frame_flags = VP9_LAST_FLAG;
675     cpi->ext_refresh_last_frame = 0;
676     cpi->ext_refresh_golden_frame = 1;
677   } else {
678     cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
679   }
680   cpi->lst_fb_idx = spatial_id;
681   if (spatial_id) {
682     if (cpi->svc.layer_context[0].is_key_frame) {
683       cpi->lst_fb_idx = spatial_id - 1;
684       cpi->gld_fb_idx = spatial_id;
685     } else {
686       cpi->gld_fb_idx = spatial_id - 1;
687     }
688   } else {
689     cpi->gld_fb_idx = 0;
690   }
691 
692   if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
693 
694   reset_fb_idx_unused(cpi);
695 }
696 
set_flags_and_fb_idx_bypass_via_set_ref_frame_config(VP9_COMP * const cpi)697 static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
698     VP9_COMP *const cpi) {
699   SVC *const svc = &cpi->svc;
700   int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
701   cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
702   cpi->ext_refresh_frame_flags_pending = 1;
703   cpi->lst_fb_idx = svc->lst_fb_idx[sl];
704   cpi->gld_fb_idx = svc->gld_fb_idx[sl];
705   cpi->alt_fb_idx = svc->alt_fb_idx[sl];
706   cpi->ext_refresh_last_frame = 0;
707   cpi->ext_refresh_golden_frame = 0;
708   cpi->ext_refresh_alt_ref_frame = 0;
709   cpi->ref_frame_flags = 0;
710   if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
711   if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
712   if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
713 }
714 
vp9_copy_flags_ref_update_idx(VP9_COMP * const cpi)715 void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
716   SVC *const svc = &cpi->svc;
717   static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
718                                     VP9_ALT_FLAG };
719   int sl = svc->spatial_layer_id;
720   svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
721   svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
722   svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
723   // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
724   // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
725   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
726     int ref;
727     for (ref = 0; ref < REF_FRAMES; ++ref) {
728       svc->update_buffer_slot[sl] &= ~(1 << ref);
729       if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
730           (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
731           (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
732         svc->update_buffer_slot[sl] |= (1 << ref);
733     }
734   }
735 
736   // TODO(jianj): Remove these 3, deprecated.
737   svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
738   svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
739   svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
740 
741   svc->reference_last[sl] =
742       (uint8_t)(cpi->ref_frame_flags & flag_list[LAST_FRAME]);
743   svc->reference_golden[sl] =
744       (uint8_t)(cpi->ref_frame_flags & flag_list[GOLDEN_FRAME]);
745   svc->reference_altref[sl] =
746       (uint8_t)(cpi->ref_frame_flags & flag_list[ALTREF_FRAME]);
747 }
748 
vp9_one_pass_cbr_svc_start_layer(VP9_COMP * const cpi)749 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
750   int width = 0, height = 0;
751   SVC *const svc = &cpi->svc;
752   LAYER_CONTEXT *lc = NULL;
753   svc->skip_enhancement_layer = 0;
754 
755   if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
756       svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
757       svc->number_temporal_layers <= 3 &&
758       !(svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
759         svc->use_set_ref_frame_config))
760     svc->simulcast_mode = 1;
761   else
762     svc->simulcast_mode = 0;
763 
764   if (svc->number_spatial_layers > 1) {
765     svc->use_base_mv = 1;
766     svc->use_partition_reuse = 1;
767   }
768   svc->force_zero_mode_spatial_ref = 1;
769   svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
770   svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
771   svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
772 
773   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
774     set_flags_and_fb_idx_for_temporal_mode3(cpi);
775   } else if (svc->temporal_layering_mode ==
776              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
777     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
778   } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
779     set_flags_and_fb_idx_for_temporal_mode2(cpi);
780   } else if (svc->temporal_layering_mode ==
781                  VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
782              svc->use_set_ref_frame_config) {
783     set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
784   }
785 
786   if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
787       cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
788       cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
789     svc->buffer_gf_temporal_ref[0].is_used = 1;
790   if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
791       cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
792       cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
793     svc->buffer_gf_temporal_ref[1].is_used = 1;
794 
795   // For the fixed (non-flexible/bypass) SVC mode:
796   // If long term temporal reference is enabled at the sequence level
797   // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
798   // we can use golden as a second temporal reference
799   // (since the spatial/inter-layer reference is disabled).
800   // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
801   // unused (slot 7 and 6 should be available for 3-3 layer system).
802   // For now usage of this second temporal reference will only be used for
803   // highest and next to highest spatial layer (i.e., top and middle layer for
804   // 3 spatial layers).
805   svc->use_gf_temporal_ref_current_layer = 0;
806   if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
807       !svc->buffer_gf_temporal_ref[1].is_used &&
808       svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
809       svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
810       svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
811       svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
812     // Enable the second (long-term) temporal reference at the frame-level.
813     svc->use_gf_temporal_ref_current_layer = 1;
814   }
815 
816   // Check if current superframe has any layer sync, only check once on
817   // base layer.
818   if (svc->spatial_layer_id == 0) {
819     int sl = 0;
820     // Default is no sync.
821     svc->superframe_has_layer_sync = 0;
822     for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
823       if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
824     }
825   }
826 
827   // Reset the drop flags for all spatial layers, on the base layer.
828   if (svc->spatial_layer_id == 0) {
829     vp9_zero(svc->drop_spatial_layer);
830     // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
831     // causes an issue with frame dropping and temporal layers, when the frame
832     // flags are passed via the encode call (bypass mode). Issue is that we're
833     // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
834     if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
835       memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
836       memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
837       memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
838       // These are set by API before the superframe is encoded and they are
839       // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
840       // mode.
841       vp9_zero(svc->update_buffer_slot);
842       vp9_zero(svc->reference_last);
843       vp9_zero(svc->reference_golden);
844       vp9_zero(svc->reference_altref);
845       // TODO(jianj): Remove these 3, deprecated.
846       vp9_zero(svc->update_last);
847       vp9_zero(svc->update_golden);
848       vp9_zero(svc->update_altref);
849     }
850   }
851 
852   lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
853                            svc->temporal_layer_id];
854 
855   // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
856   // only for non-BYPASS mode for now.
857   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
858       svc->use_set_ref_frame_config) {
859     RATE_CONTROL *const lrc = &lc->rc;
860     lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
861     lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
862   }
863 
864   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
865                        lc->scaling_factor_num, lc->scaling_factor_den, &width,
866                        &height);
867 
868   // Use Eightap_smooth for low resolutions.
869   if (width * height <= 320 * 240)
870     svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
871   // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
872   // to source pixel).
873   lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
874                            svc->temporal_layer_id];
875   if (lc->scaling_factor_num > (3 * lc->scaling_factor_den) >> 2)
876     svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
877 
878   // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
879   // For now, turn off use of base motion vectors and partition reuse if the
880   // spatial scale factors for any layers are not 2,
881   // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
882   // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
883   if (svc->number_spatial_layers > 1) {
884     int sl;
885     for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
886       lc = &svc->layer_context[sl * svc->number_temporal_layers +
887                                svc->temporal_layer_id];
888       if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
889           !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
890             svc->number_spatial_layers == 3)) {
891         svc->use_base_mv = 0;
892         svc->use_partition_reuse = 0;
893         break;
894       }
895     }
896     // For non-zero spatial layers: if the previous spatial layer was dropped
897     // disable the base_mv and partition_reuse features.
898     if (svc->spatial_layer_id > 0 &&
899         svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
900       svc->use_base_mv = 0;
901       svc->use_partition_reuse = 0;
902     }
903   }
904 
905   svc->non_reference_frame = 0;
906   if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
907       !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
908     svc->non_reference_frame = 1;
909   // For non-flexible mode, where update_buffer_slot is used, need to check if
910   // all buffer slots are not refreshed.
911   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
912     if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
913       svc->non_reference_frame = 0;
914   }
915 
916   if (svc->spatial_layer_id == 0) {
917     svc->high_source_sad_superframe = 0;
918     svc->high_num_blocks_with_motion = 0;
919   }
920 
921   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
922       svc->last_layer_dropped[svc->spatial_layer_id] &&
923       svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
924       !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
925     // For fixed/non-flexible mode, if the previous frame (same spatial layer
926     // from previous superframe) was dropped, make sure the lst_fb_idx
927     // for this frame corresponds to the buffer index updated on (last) encoded
928     // TL0 frame (with same spatial layer).
929     cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
930   }
931 
932   if (vp9_set_size_literal(cpi, width, height) != 0)
933     return VPX_CODEC_INVALID_PARAM;
934 
935   return 0;
936 }
937 
vp9_svc_lookahead_pop(VP9_COMP * const cpi,struct lookahead_ctx * ctx,int drain)938 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
939                                               struct lookahead_ctx *ctx,
940                                               int drain) {
941   struct lookahead_entry *buf = NULL;
942   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
943     buf = vp9_lookahead_peek(ctx, 0);
944     if (buf != NULL) {
945       // Only remove the buffer when pop the highest layer.
946       if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
947         vp9_lookahead_pop(ctx, drain);
948       }
949     }
950   }
951   return buf;
952 }
953 
vp9_free_svc_cyclic_refresh(VP9_COMP * const cpi)954 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
955   int sl, tl;
956   SVC *const svc = &cpi->svc;
957   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
958   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
959     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
960       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
961       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
962       if (lc->map) vpx_free(lc->map);
963       if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
964       if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
965     }
966   }
967 }
968 
969 // Reset on key frame: reset counters, references and buffer updates.
vp9_svc_reset_temporal_layers(VP9_COMP * const cpi,int is_key)970 void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
971   int sl, tl;
972   SVC *const svc = &cpi->svc;
973   LAYER_CONTEXT *lc = NULL;
974   for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
975     for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
976       lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
977       lc->current_video_frame_in_layer = 0;
978       if (is_key) lc->frames_from_key_frame = 0;
979     }
980   }
981   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
982     set_flags_and_fb_idx_for_temporal_mode3(cpi);
983   } else if (svc->temporal_layering_mode ==
984              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
985     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
986   } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
987     set_flags_and_fb_idx_for_temporal_mode2(cpi);
988   }
989   vp9_update_temporal_layer_framerate(cpi);
990   vp9_restore_layer_context(cpi);
991 }
992 
vp9_svc_check_reset_layer_rc_flag(VP9_COMP * const cpi)993 void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
994   SVC *svc = &cpi->svc;
995   int sl, tl;
996   for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
997     // Check for reset based on avg_frame_bandwidth for spatial layer sl.
998     int layer = LAYER_IDS_TO_IDX(sl, svc->number_temporal_layers - 1,
999                                  svc->number_temporal_layers);
1000     LAYER_CONTEXT *lc = &svc->layer_context[layer];
1001     RATE_CONTROL *lrc = &lc->rc;
1002     if (lrc->avg_frame_bandwidth > (3 * lrc->last_avg_frame_bandwidth >> 1) ||
1003         lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
1004       // Reset for all temporal layers with spatial layer sl.
1005       for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1006         int layer = LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
1007         LAYER_CONTEXT *lc = &svc->layer_context[layer];
1008         RATE_CONTROL *lrc = &lc->rc;
1009         lrc->rc_1_frame = 0;
1010         lrc->rc_2_frame = 0;
1011         lrc->bits_off_target = lrc->optimal_buffer_level;
1012         lrc->buffer_level = lrc->optimal_buffer_level;
1013       }
1014     }
1015   }
1016 }
1017 
vp9_svc_constrain_inter_layer_pred(VP9_COMP * const cpi)1018 void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
1019   VP9_COMMON *const cm = &cpi->common;
1020   SVC *const svc = &cpi->svc;
1021   const int sl = svc->spatial_layer_id;
1022   // Check for disabling inter-layer (spatial) prediction, if
1023   // svc.disable_inter_layer_pred is set. If the previous spatial layer was
1024   // dropped then disable the prediction from this (scaled) reference.
1025   // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
1026   // on key frames or if any spatial layer is a sync layer.
1027   if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
1028        !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1029        !svc->superframe_has_layer_sync) ||
1030       svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
1031       svc->drop_spatial_layer[sl - 1]) {
1032     MV_REFERENCE_FRAME ref_frame;
1033     static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG,
1034                                       VP9_ALT_FLAG };
1035     for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1036       const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1037       if (yv12 != NULL && (cpi->ref_frame_flags & flag_list[ref_frame])) {
1038         const struct scale_factors *const scale_fac =
1039             &cm->frame_refs[ref_frame - 1].sf;
1040         if (vp9_is_scaled(scale_fac)) {
1041           cpi->ref_frame_flags &= (~flag_list[ref_frame]);
1042           // Point golden/altref frame buffer index to last.
1043           if (!svc->simulcast_mode) {
1044             if (ref_frame == GOLDEN_FRAME)
1045               cpi->gld_fb_idx = cpi->lst_fb_idx;
1046             else if (ref_frame == ALTREF_FRAME)
1047               cpi->alt_fb_idx = cpi->lst_fb_idx;
1048           }
1049         }
1050       }
1051     }
1052   }
1053   // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
1054   // If the reference for inter-layer prediction (the reference that is scaled)
1055   // is not the previous spatial layer from the same superframe, then we disable
1056   // inter-layer prediction. Only need to check when inter_layer prediction is
1057   // not set to OFF mode.
1058   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1059       svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
1060     // We only use LAST and GOLDEN for prediction in real-time mode, so we
1061     // check both here.
1062     MV_REFERENCE_FRAME ref_frame;
1063     for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
1064       struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
1065       if (vp9_is_scaled(scale_fac)) {
1066         // If this reference  was updated on the previous spatial layer of the
1067         // current superframe, then we keep this reference (don't disable).
1068         // Otherwise we disable the inter-layer prediction.
1069         // This condition is verified by checking if the current frame buffer
1070         // index is equal to any of the slots for the previous spatial layer,
1071         // and if so, check if that slot was updated/refreshed. If that is the
1072         // case, then this reference is valid for inter-layer prediction under
1073         // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
1074         int fb_idx =
1075             ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
1076         int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
1077         int disable = 1;
1078         if (fb_idx < 0) continue;
1079         if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
1080              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1081             (fb_idx == svc->gld_fb_idx[sl - 1] &&
1082              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1083             (fb_idx == svc->alt_fb_idx[sl - 1] &&
1084              (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
1085           disable = 0;
1086         if (disable) cpi->ref_frame_flags &= (~ref_flag);
1087       }
1088     }
1089   }
1090 }
1091 
vp9_svc_assert_constraints_pattern(VP9_COMP * const cpi)1092 void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
1093   SVC *const svc = &cpi->svc;
1094   // For fixed/non-flexible mode, the following constraint are expected,
1095   // when inter-layer prediciton is on (default).
1096   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1097       svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
1098       svc->framedrop_mode != LAYER_DROP) {
1099     if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1100       // On non-key frames: LAST is always temporal reference, GOLDEN is
1101       // spatial reference.
1102       if (svc->temporal_layer_id == 0)
1103         // Base temporal only predicts from base temporal.
1104         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
1105       else
1106         // Non-base temporal only predicts from lower temporal layer.
1107         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
1108                svc->temporal_layer_id);
1109       if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
1110           svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1111         // Non-base spatial only predicts from lower spatial layer with same
1112         // temporal_id.
1113         assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1114                svc->spatial_layer_id - 1);
1115         assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1116                svc->temporal_layer_id);
1117       }
1118     } else if (svc->spatial_layer_id > 0 &&
1119                svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1120       // Only 1 reference for frame whose base is key; reference may be LAST
1121       // or GOLDEN, so we check both.
1122       if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1123         assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
1124                svc->spatial_layer_id - 1);
1125         assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
1126                svc->temporal_layer_id);
1127       } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1128         assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1129                svc->spatial_layer_id - 1);
1130         assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1131                svc->temporal_layer_id);
1132       }
1133     }
1134   } else if (svc->use_gf_temporal_ref_current_layer &&
1135              !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1136     // For the usage of golden as second long term reference: the
1137     // temporal_layer_id of that reference must be base temporal layer 0, and
1138     // spatial_layer_id of that reference must be same as current
1139     // spatial_layer_id. If not, disable feature.
1140     // TODO(marpan): Investigate when this can happen, and maybe put this check
1141     // and reset in a different place.
1142     if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
1143             svc->spatial_layer_id ||
1144         svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
1145       svc->use_gf_temporal_ref_current_layer = 0;
1146   }
1147 }
1148 
1149 #if CONFIG_VP9_TEMPORAL_DENOISING
vp9_denoise_svc_non_key(VP9_COMP * const cpi)1150 int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
1151   int layer =
1152       LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1153                        cpi->svc.number_temporal_layers);
1154   LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1155   return denoise_svc(cpi) && !lc->is_key_frame;
1156 }
1157 #endif
1158 
vp9_svc_check_spatial_layer_sync(VP9_COMP * const cpi)1159 void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
1160   SVC *const svc = &cpi->svc;
1161   // Only for superframes whose base is not key, as those are
1162   // already sync frames.
1163   if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1164     if (svc->spatial_layer_id == 0) {
1165       // On base spatial layer: if the current superframe has a layer sync then
1166       // reset the pattern counters and reset to base temporal layer.
1167       if (svc->superframe_has_layer_sync)
1168         vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
1169     }
1170     // If the layer sync is set for this current spatial layer then
1171     // disable the temporal reference.
1172     if (svc->spatial_layer_id > 0 &&
1173         svc->spatial_layer_sync[svc->spatial_layer_id]) {
1174       cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1175       if (svc->use_gf_temporal_ref_current_layer) {
1176         int index = svc->spatial_layer_id;
1177         // If golden is used as second reference: need to remove it from
1178         // prediction, reset refresh period to 0, and update the reference.
1179         svc->use_gf_temporal_ref_current_layer = 0;
1180         cpi->rc.baseline_gf_interval = 0;
1181         cpi->rc.frames_till_gf_update_due = 0;
1182         // On layer sync frame we must update the buffer index used for long
1183         // term reference. Use the alt_ref since it is not used or updated on
1184         // sync frames.
1185         if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
1186         assert(index >= 0);
1187         cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
1188         cpi->ext_refresh_alt_ref_frame = 1;
1189       }
1190     }
1191   }
1192 }
1193 
vp9_svc_update_ref_frame_buffer_idx(VP9_COMP * const cpi)1194 void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
1195   SVC *const svc = &cpi->svc;
1196   // Update the usage of frame buffer index for base spatial layers.
1197   if (svc->spatial_layer_id == 0) {
1198     if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
1199       svc->fb_idx_base[cpi->lst_fb_idx] = 1;
1200     if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
1201       svc->fb_idx_base[cpi->gld_fb_idx] = 1;
1202     if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
1203       svc->fb_idx_base[cpi->alt_fb_idx] = 1;
1204   }
1205 }
1206 
vp9_svc_update_ref_frame_bypass_mode(VP9_COMP * const cpi)1207 static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
1208   // For non-flexible/bypass SVC mode: check for refreshing other buffer
1209   // slots.
1210   SVC *const svc = &cpi->svc;
1211   VP9_COMMON *const cm = &cpi->common;
1212   BufferPool *const pool = cm->buffer_pool;
1213   int i;
1214   for (i = 0; i < REF_FRAMES; i++) {
1215     if (cm->frame_type == KEY_FRAME ||
1216         svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
1217       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1218       svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1219       svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1220     }
1221   }
1222 }
1223 
vp9_svc_update_ref_frame(VP9_COMP * const cpi)1224 void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
1225   VP9_COMMON *const cm = &cpi->common;
1226   SVC *const svc = &cpi->svc;
1227   BufferPool *const pool = cm->buffer_pool;
1228 
1229   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1230       svc->use_set_ref_frame_config) {
1231     vp9_svc_update_ref_frame_bypass_mode(cpi);
1232   } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
1233     // Keep track of frame index for each reference frame.
1234     int i;
1235     // On key frame update all reference frame slots.
1236     for (i = 0; i < REF_FRAMES; i++) {
1237       svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1238       svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1239       // LAST/GOLDEN/ALTREF is already updated above.
1240       if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
1241         ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1242     }
1243   } else {
1244     if (cpi->refresh_last_frame) {
1245       svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
1246       svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
1247     }
1248     if (cpi->refresh_golden_frame) {
1249       svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
1250       svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
1251     }
1252     if (cpi->refresh_alt_ref_frame) {
1253       svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
1254       svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
1255     }
1256   }
1257   // Copy flags from encoder to SVC struct.
1258   vp9_copy_flags_ref_update_idx(cpi);
1259   vp9_svc_update_ref_frame_buffer_idx(cpi);
1260 }
1261 
vp9_svc_adjust_frame_rate(VP9_COMP * const cpi)1262 void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
1263   int64_t this_duration =
1264       cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
1265   vp9_new_framerate(cpi, 10000000.0 / this_duration);
1266 }
1267 
vp9_svc_adjust_avg_frame_qindex(VP9_COMP * const cpi)1268 void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
1269   VP9_COMMON *const cm = &cpi->common;
1270   SVC *const svc = &cpi->svc;
1271   RATE_CONTROL *const rc = &cpi->rc;
1272   // On key frames in CBR mode: reset the avg_frame_index for base layer
1273   // (to level closer to worst_quality) if the overshoot is significant.
1274   // Reset it for all temporal layers on base spatial layer.
1275   if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
1276       !svc->simulcast_mode &&
1277       rc->projected_frame_size > 3 * rc->avg_frame_bandwidth) {
1278     int tl;
1279     rc->avg_frame_qindex[INTER_FRAME] =
1280         VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
1281                (cm->base_qindex + rc->worst_quality) >> 1);
1282     for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1283       const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
1284       LAYER_CONTEXT *lc = &svc->layer_context[layer];
1285       RATE_CONTROL *lrc = &lc->rc;
1286       lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
1287     }
1288   }
1289 }
1290