]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_svc_layercontext.c
subpel variance neon: add mixed sizes
[libvpx] / vp9 / encoder / vp9_svc_layercontext.c
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
22 void vp9_init_layer_context(VP9_COMP *const cpi) {
23   SVC *const svc = &cpi->svc;
24   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
25   int mi_rows = cpi->common.mi_rows;
26   int mi_cols = cpi->common.mi_cols;
27   int sl, tl, i;
28   int alt_ref_idx = svc->number_spatial_layers;
29
30   svc->spatial_layer_id = 0;
31   svc->temporal_layer_id = 0;
32   svc->first_spatial_layer_to_encode = 0;
33   svc->rc_drop_superframe = 0;
34   svc->force_zero_mode_spatial_ref = 0;
35   svc->use_base_mv = 0;
36   svc->scaled_temp_is_alloc = 0;
37   svc->scaled_one_half = 0;
38   svc->current_superframe = 0;
39   for (i = 0; i < REF_FRAMES; ++i) svc->ref_frame_index[i] = -1;
40   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
41     cpi->svc.ext_frame_flags[sl] = 0;
42     cpi->svc.ext_lst_fb_idx[sl] = 0;
43     cpi->svc.ext_gld_fb_idx[sl] = 1;
44     cpi->svc.ext_alt_fb_idx[sl] = 2;
45     cpi->svc.filtertype_downsample_source[sl] = 0;
46   }
47
48   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) {
49     if (vpx_realloc_frame_buffer(&cpi->svc.empty_frame.img, SMALL_FRAME_WIDTH,
50                                  SMALL_FRAME_HEIGHT, cpi->common.subsampling_x,
51                                  cpi->common.subsampling_y,
52 #if CONFIG_VP9_HIGHBITDEPTH
53                                  cpi->common.use_highbitdepth,
54 #endif
55                                  VP9_ENC_BORDER_IN_PIXELS,
56                                  cpi->common.byte_alignment, NULL, NULL, NULL))
57       vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
58                          "Failed to allocate empty frame for multiple frame "
59                          "contexts");
60
61     memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80,
62            cpi->svc.empty_frame.img.buffer_alloc_sz);
63   }
64
65   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
66     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
67       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
68       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
69       RATE_CONTROL *const lrc = &lc->rc;
70       int i;
71       lc->current_video_frame_in_layer = 0;
72       lc->layer_size = 0;
73       lc->frames_from_key_frame = 0;
74       lc->last_frame_type = FRAME_TYPES;
75       lrc->ni_av_qi = oxcf->worst_allowed_q;
76       lrc->total_actual_bits = 0;
77       lrc->total_target_vs_actual = 0;
78       lrc->ni_tot_qi = 0;
79       lrc->tot_q = 0.0;
80       lrc->avg_q = 0.0;
81       lrc->ni_frames = 0;
82       lrc->decimation_count = 0;
83       lrc->decimation_factor = 0;
84
85       for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
86         lrc->rate_correction_factors[i] = 1.0;
87       }
88
89       if (cpi->oxcf.rc_mode == VPX_CBR) {
90         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
91         lrc->last_q[INTER_FRAME] = oxcf->worst_allowed_q;
92         lrc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
93         lrc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
94       } else {
95         lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
96         lrc->last_q[KEY_FRAME] = oxcf->best_allowed_q;
97         lrc->last_q[INTER_FRAME] = oxcf->best_allowed_q;
98         lrc->avg_frame_qindex[KEY_FRAME] =
99             (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
100         lrc->avg_frame_qindex[INTER_FRAME] =
101             (oxcf->worst_allowed_q + oxcf->best_allowed_q) / 2;
102         if (oxcf->ss_enable_auto_arf[sl])
103           lc->alt_ref_idx = alt_ref_idx++;
104         else
105           lc->alt_ref_idx = INVALID_IDX;
106         lc->gold_ref_idx = INVALID_IDX;
107       }
108
109       lrc->buffer_level =
110           oxcf->starting_buffer_level_ms * lc->target_bandwidth / 1000;
111       lrc->bits_off_target = lrc->buffer_level;
112
113       // Initialize the cyclic refresh parameters. If spatial layers are used
114       // (i.e., ss_number_layers > 1), these need to be updated per spatial
115       // layer.
116       // Cyclic refresh is only applied on base temporal layer.
117       if (oxcf->ss_number_layers > 1 && tl == 0) {
118         size_t last_coded_q_map_size;
119         size_t consec_zero_mv_size;
120         VP9_COMMON *const cm = &cpi->common;
121         lc->sb_index = 0;
122         CHECK_MEM_ERROR(cm, lc->map,
123                         vpx_malloc(mi_rows * mi_cols * sizeof(*lc->map)));
124         memset(lc->map, 0, mi_rows * mi_cols);
125         last_coded_q_map_size =
126             mi_rows * mi_cols * sizeof(*lc->last_coded_q_map);
127         CHECK_MEM_ERROR(cm, lc->last_coded_q_map,
128                         vpx_malloc(last_coded_q_map_size));
129         assert(MAXQ <= 255);
130         memset(lc->last_coded_q_map, MAXQ, last_coded_q_map_size);
131         consec_zero_mv_size = mi_rows * mi_cols * sizeof(*lc->consec_zero_mv);
132         CHECK_MEM_ERROR(cm, lc->consec_zero_mv,
133                         vpx_malloc(consec_zero_mv_size));
134         memset(lc->consec_zero_mv, 0, consec_zero_mv_size);
135       }
136     }
137   }
138
139   // Still have extra buffer for base layer golden frame
140   if (!(svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) &&
141       alt_ref_idx < REF_FRAMES)
142     svc->layer_context[0].gold_ref_idx = alt_ref_idx;
143 }
144
145 // Update the layer context from a change_config() call.
146 void vp9_update_layer_context_change_config(VP9_COMP *const cpi,
147                                             const int target_bandwidth) {
148   SVC *const svc = &cpi->svc;
149   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
150   const RATE_CONTROL *const rc = &cpi->rc;
151   int sl, tl, layer = 0, spatial_layer_target;
152   float bitrate_alloc = 1.0;
153
154   if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
155     for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
156       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
157         layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
158         svc->layer_context[layer].target_bandwidth =
159             oxcf->layer_target_bitrate[layer];
160       }
161
162       layer = LAYER_IDS_TO_IDX(
163           sl,
164           ((oxcf->ts_number_layers - 1) < 0 ? 0 : (oxcf->ts_number_layers - 1)),
165           oxcf->ts_number_layers);
166       spatial_layer_target = svc->layer_context[layer].target_bandwidth =
167           oxcf->layer_target_bitrate[layer];
168
169       for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
170         LAYER_CONTEXT *const lc =
171             &svc->layer_context[sl * oxcf->ts_number_layers + tl];
172         RATE_CONTROL *const lrc = &lc->rc;
173
174         lc->spatial_layer_target_bandwidth = spatial_layer_target;
175         bitrate_alloc = (float)lc->target_bandwidth / spatial_layer_target;
176         lrc->starting_buffer_level =
177             (int64_t)(rc->starting_buffer_level * bitrate_alloc);
178         lrc->optimal_buffer_level =
179             (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
180         lrc->maximum_buffer_size =
181             (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
182         lrc->bits_off_target =
183             VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
184         lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
185         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
186         lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
187         lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
188         lrc->worst_quality = rc->worst_quality;
189         lrc->best_quality = rc->best_quality;
190       }
191     }
192   } else {
193     int layer_end;
194
195     if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
196       layer_end = svc->number_temporal_layers;
197     } else {
198       layer_end = svc->number_spatial_layers;
199     }
200
201     for (layer = 0; layer < layer_end; ++layer) {
202       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
203       RATE_CONTROL *const lrc = &lc->rc;
204
205       lc->target_bandwidth = oxcf->layer_target_bitrate[layer];
206
207       bitrate_alloc = (float)lc->target_bandwidth / target_bandwidth;
208       // Update buffer-related quantities.
209       lrc->starting_buffer_level =
210           (int64_t)(rc->starting_buffer_level * bitrate_alloc);
211       lrc->optimal_buffer_level =
212           (int64_t)(rc->optimal_buffer_level * bitrate_alloc);
213       lrc->maximum_buffer_size =
214           (int64_t)(rc->maximum_buffer_size * bitrate_alloc);
215       lrc->bits_off_target =
216           VPXMIN(lrc->bits_off_target, lrc->maximum_buffer_size);
217       lrc->buffer_level = VPXMIN(lrc->buffer_level, lrc->maximum_buffer_size);
218       // Update framerate-related quantities.
219       if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) {
220         lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[layer];
221       } else {
222         lc->framerate = cpi->framerate;
223       }
224       lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
225       lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
226       // Update qp-related quantities.
227       lrc->worst_quality = rc->worst_quality;
228       lrc->best_quality = rc->best_quality;
229     }
230   }
231 }
232
233 static LAYER_CONTEXT *get_layer_context(VP9_COMP *const cpi) {
234   if (is_one_pass_cbr_svc(cpi))
235     return &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
236                                        cpi->svc.number_temporal_layers +
237                                    cpi->svc.temporal_layer_id];
238   else
239     return (cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR)
240                ? &cpi->svc.layer_context[cpi->svc.temporal_layer_id]
241                : &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
242 }
243
244 void vp9_update_temporal_layer_framerate(VP9_COMP *const cpi) {
245   SVC *const svc = &cpi->svc;
246   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
247   LAYER_CONTEXT *const lc = get_layer_context(cpi);
248   RATE_CONTROL *const lrc = &lc->rc;
249   // Index into spatial+temporal arrays.
250   const int st_idx = svc->spatial_layer_id * svc->number_temporal_layers +
251                      svc->temporal_layer_id;
252   const int tl = svc->temporal_layer_id;
253
254   lc->framerate = cpi->framerate / oxcf->ts_rate_decimator[tl];
255   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
256   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
257   // Update the average layer frame size (non-cumulative per-frame-bw).
258   if (tl == 0) {
259     lc->avg_frame_size = lrc->avg_frame_bandwidth;
260   } else {
261     const double prev_layer_framerate =
262         cpi->framerate / oxcf->ts_rate_decimator[tl - 1];
263     const int prev_layer_target_bandwidth =
264         oxcf->layer_target_bitrate[st_idx - 1];
265     lc->avg_frame_size =
266         (int)((lc->target_bandwidth - prev_layer_target_bandwidth) /
267               (lc->framerate - prev_layer_framerate));
268   }
269 }
270
271 void vp9_update_spatial_layer_framerate(VP9_COMP *const cpi, double framerate) {
272   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
273   LAYER_CONTEXT *const lc = get_layer_context(cpi);
274   RATE_CONTROL *const lrc = &lc->rc;
275
276   lc->framerate = framerate;
277   lrc->avg_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
278   lrc->min_frame_bandwidth =
279       (int)(lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100);
280   lrc->max_frame_bandwidth = (int)(((int64_t)lrc->avg_frame_bandwidth *
281                                     oxcf->two_pass_vbrmax_section) /
282                                    100);
283   vp9_rc_set_gf_interval_range(cpi, lrc);
284 }
285
286 void vp9_restore_layer_context(VP9_COMP *const cpi) {
287   LAYER_CONTEXT *const lc = get_layer_context(cpi);
288   const int old_frame_since_key = cpi->rc.frames_since_key;
289   const int old_frame_to_key = cpi->rc.frames_to_key;
290
291   cpi->rc = lc->rc;
292   cpi->twopass = lc->twopass;
293   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
294   cpi->alt_ref_source = lc->alt_ref_source;
295   // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
296   // does not use speed = 0).
297   if (is_one_pass_cbr_svc(cpi) && lc->speed > 0) {
298     cpi->oxcf.speed = lc->speed;
299   }
300   // Reset the frames_since_key and frames_to_key counters to their values
301   // before the layer restore. Keep these defined for the stream (not layer).
302   if (cpi->svc.number_temporal_layers > 1 ||
303       (cpi->svc.number_spatial_layers > 1 && !is_two_pass_svc(cpi))) {
304     cpi->rc.frames_since_key = old_frame_since_key;
305     cpi->rc.frames_to_key = old_frame_to_key;
306   }
307
308   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
309   // for the base temporal layer.
310   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
311       cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
312     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
313     signed char *temp = cr->map;
314     uint8_t *temp2 = cr->last_coded_q_map;
315     uint8_t *temp3 = cpi->consec_zero_mv;
316     cr->map = lc->map;
317     lc->map = temp;
318     cr->last_coded_q_map = lc->last_coded_q_map;
319     lc->last_coded_q_map = temp2;
320     cpi->consec_zero_mv = lc->consec_zero_mv;
321     lc->consec_zero_mv = temp3;
322     cr->sb_index = lc->sb_index;
323   }
324 }
325
326 void vp9_save_layer_context(VP9_COMP *const cpi) {
327   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
328   LAYER_CONTEXT *const lc = get_layer_context(cpi);
329
330   lc->rc = cpi->rc;
331   lc->twopass = cpi->twopass;
332   lc->target_bandwidth = (int)oxcf->target_bandwidth;
333   lc->alt_ref_source = cpi->alt_ref_source;
334
335   // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
336   // for the base temporal layer.
337   if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
338       cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
339     CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
340     signed char *temp = lc->map;
341     uint8_t *temp2 = lc->last_coded_q_map;
342     uint8_t *temp3 = lc->consec_zero_mv;
343     lc->map = cr->map;
344     cr->map = temp;
345     lc->last_coded_q_map = cr->last_coded_q_map;
346     cr->last_coded_q_map = temp2;
347     lc->consec_zero_mv = cpi->consec_zero_mv;
348     cpi->consec_zero_mv = temp3;
349     lc->sb_index = cr->sb_index;
350   }
351 }
352
353 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
354   SVC *const svc = &cpi->svc;
355   int i;
356
357   for (i = 0; i < svc->number_spatial_layers; ++i) {
358     TWO_PASS *const twopass = &svc->layer_context[i].twopass;
359
360     svc->spatial_layer_id = i;
361     vp9_init_second_pass(cpi);
362
363     twopass->total_stats.spatial_layer_id = i;
364     twopass->total_left_stats.spatial_layer_id = i;
365   }
366   svc->spatial_layer_id = 0;
367 }
368
369 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
370   LAYER_CONTEXT *const lc =
371       &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
372                               cpi->svc.number_temporal_layers];
373   ++lc->current_video_frame_in_layer;
374   ++lc->frames_from_key_frame;
375   if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
376     ++cpi->svc.current_superframe;
377 }
378
379 int vp9_is_upper_layer_key_frame(const VP9_COMP *const cpi) {
380   return is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0 &&
381          cpi->svc
382              .layer_context[cpi->svc.spatial_layer_id *
383                                 cpi->svc.number_temporal_layers +
384                             cpi->svc.temporal_layer_id]
385              .is_key_frame;
386 }
387
388 static void get_layer_resolution(const int width_org, const int height_org,
389                                  const int num, const int den, int *width_out,
390                                  int *height_out) {
391   int w, h;
392
393   if (width_out == NULL || height_out == NULL || den == 0) return;
394
395   w = width_org * num / den;
396   h = height_org * num / den;
397
398   // make height and width even to make chrome player happy
399   w += w % 2;
400   h += h % 2;
401
402   *width_out = w;
403   *height_out = h;
404 }
405
406 // The function sets proper ref_frame_flags, buffer indices, and buffer update
407 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
408 // scheme.
409 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
410   int frame_num_within_temporal_struct = 0;
411   int spatial_id, temporal_id;
412   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
413   frame_num_within_temporal_struct =
414       cpi->svc
415           .layer_context[cpi->svc.spatial_layer_id *
416                          cpi->svc.number_temporal_layers]
417           .current_video_frame_in_layer %
418       4;
419   temporal_id = cpi->svc.temporal_layer_id =
420       (frame_num_within_temporal_struct & 1)
421           ? 2
422           : (frame_num_within_temporal_struct >> 1);
423   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
424       cpi->ext_refresh_alt_ref_frame = 0;
425   if (!temporal_id) {
426     cpi->ext_refresh_frame_flags_pending = 1;
427     cpi->ext_refresh_last_frame = 1;
428     if (!spatial_id) {
429       cpi->ref_frame_flags = VP9_LAST_FLAG;
430     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
431       // base layer is a key frame.
432       cpi->ref_frame_flags = VP9_LAST_FLAG;
433       cpi->ext_refresh_last_frame = 0;
434       cpi->ext_refresh_golden_frame = 1;
435     } else {
436       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
437     }
438   } else if (temporal_id == 1) {
439     cpi->ext_refresh_frame_flags_pending = 1;
440     cpi->ext_refresh_alt_ref_frame = 1;
441     if (!spatial_id) {
442       cpi->ref_frame_flags = VP9_LAST_FLAG;
443     } else {
444       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
445     }
446   } else {
447     if (frame_num_within_temporal_struct == 1) {
448       // the first tl2 picture
449       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
450         cpi->ext_refresh_frame_flags_pending = 1;
451         if (!spatial_id)
452           cpi->ref_frame_flags = VP9_LAST_FLAG;
453         else
454           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
455       } else if (!spatial_id) {
456         cpi->ext_refresh_frame_flags_pending = 1;
457         cpi->ext_refresh_alt_ref_frame = 1;
458         cpi->ref_frame_flags = VP9_LAST_FLAG;
459       } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
460         cpi->ext_refresh_frame_flags_pending = 1;
461         cpi->ext_refresh_alt_ref_frame = 1;
462         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
463       }
464     } else {
465       //  The second tl2 picture
466       if (spatial_id == cpi->svc.number_spatial_layers - 1) {  // top layer
467         cpi->ext_refresh_frame_flags_pending = 1;
468         if (!spatial_id)
469           cpi->ref_frame_flags = VP9_LAST_FLAG;
470         else
471           cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
472       } else if (!spatial_id) {
473         cpi->ext_refresh_frame_flags_pending = 1;
474         cpi->ref_frame_flags = VP9_LAST_FLAG;
475         cpi->ext_refresh_alt_ref_frame = 1;
476       } else {  // top layer
477         cpi->ext_refresh_frame_flags_pending = 1;
478         cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
479         cpi->ext_refresh_alt_ref_frame = 1;
480       }
481     }
482   }
483   if (temporal_id == 0) {
484     cpi->lst_fb_idx = spatial_id;
485     if (spatial_id) {
486       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
487         cpi->lst_fb_idx = spatial_id - 1;
488         cpi->gld_fb_idx = spatial_id;
489       } else {
490         cpi->gld_fb_idx = spatial_id - 1;
491       }
492     } else {
493       cpi->gld_fb_idx = 0;
494     }
495     cpi->alt_fb_idx = 0;
496   } else if (temporal_id == 1) {
497     cpi->lst_fb_idx = spatial_id;
498     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
499     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
500   } else if (frame_num_within_temporal_struct == 1) {
501     cpi->lst_fb_idx = spatial_id;
502     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
503     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
504   } else {
505     cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
506     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
507     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
508   }
509 }
510
511 // The function sets proper ref_frame_flags, buffer indices, and buffer update
512 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
513 // scheme.
514 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
515   int spatial_id, temporal_id;
516   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
517   temporal_id = cpi->svc.temporal_layer_id =
518       cpi->svc
519           .layer_context[cpi->svc.spatial_layer_id *
520                          cpi->svc.number_temporal_layers]
521           .current_video_frame_in_layer &
522       1;
523   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
524       cpi->ext_refresh_alt_ref_frame = 0;
525   if (!temporal_id) {
526     cpi->ext_refresh_frame_flags_pending = 1;
527     cpi->ext_refresh_last_frame = 1;
528     if (!spatial_id) {
529       cpi->ref_frame_flags = VP9_LAST_FLAG;
530     } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
531       // base layer is a key frame.
532       cpi->ref_frame_flags = VP9_LAST_FLAG;
533       cpi->ext_refresh_last_frame = 0;
534       cpi->ext_refresh_golden_frame = 1;
535     } else {
536       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
537     }
538   } else if (temporal_id == 1) {
539     cpi->ext_refresh_frame_flags_pending = 1;
540     cpi->ext_refresh_alt_ref_frame = 1;
541     if (!spatial_id) {
542       cpi->ref_frame_flags = VP9_LAST_FLAG;
543     } else {
544       cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
545     }
546   }
547
548   if (temporal_id == 0) {
549     cpi->lst_fb_idx = spatial_id;
550     if (spatial_id) {
551       if (cpi->svc.layer_context[temporal_id].is_key_frame) {
552         cpi->lst_fb_idx = spatial_id - 1;
553         cpi->gld_fb_idx = spatial_id;
554       } else {
555         cpi->gld_fb_idx = spatial_id - 1;
556       }
557     } else {
558       cpi->gld_fb_idx = 0;
559     }
560     cpi->alt_fb_idx = 0;
561   } else if (temporal_id == 1) {
562     cpi->lst_fb_idx = spatial_id;
563     cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
564     cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
565   }
566 }
567
568 // The function sets proper ref_frame_flags, buffer indices, and buffer update
569 // variables for temporal layering mode 0 - that has no temporal layering.
570 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
571     VP9_COMP *const cpi) {
572   int spatial_id;
573   spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
574   cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
575       cpi->ext_refresh_alt_ref_frame = 0;
576   cpi->ext_refresh_frame_flags_pending = 1;
577   cpi->ext_refresh_last_frame = 1;
578   if (!spatial_id) {
579     cpi->ref_frame_flags = VP9_LAST_FLAG;
580   } else if (cpi->svc.layer_context[0].is_key_frame) {
581     cpi->ref_frame_flags = VP9_LAST_FLAG;
582     cpi->ext_refresh_last_frame = 0;
583     cpi->ext_refresh_golden_frame = 1;
584   } else {
585     cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
586   }
587   cpi->lst_fb_idx = spatial_id;
588   if (spatial_id) {
589     if (cpi->svc.layer_context[0].is_key_frame) {
590       cpi->lst_fb_idx = spatial_id - 1;
591       cpi->gld_fb_idx = spatial_id;
592     } else {
593       cpi->gld_fb_idx = spatial_id - 1;
594     }
595   } else {
596     cpi->gld_fb_idx = 0;
597   }
598 }
599
600 int vp9_one_pass_cbr_svc_start_layer(VP9_COMP *const cpi) {
601   int width = 0, height = 0;
602   LAYER_CONTEXT *lc = NULL;
603   if (cpi->svc.number_spatial_layers > 1) cpi->svc.use_base_mv = 1;
604   cpi->svc.force_zero_mode_spatial_ref = 1;
605
606   if (cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
607     set_flags_and_fb_idx_for_temporal_mode3(cpi);
608   } else if (cpi->svc.temporal_layering_mode ==
609              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
610     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
611   } else if (cpi->svc.temporal_layering_mode ==
612              VP9E_TEMPORAL_LAYERING_MODE_0101) {
613     set_flags_and_fb_idx_for_temporal_mode2(cpi);
614   } else if (cpi->svc.temporal_layering_mode ==
615              VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
616     // In the BYPASS/flexible mode, the encoder is relying on the application
617     // to specify, for each spatial layer, the flags and buffer indices for the
618     // layering.
619     // Note that the check (cpi->ext_refresh_frame_flags_pending == 0) is
620     // needed to support the case where the frame flags may be passed in via
621     // vpx_codec_encode(), which can be used for the temporal-only svc case.
622     // TODO(marpan): Consider adding an enc_config parameter to better handle
623     // this case.
624     if (cpi->ext_refresh_frame_flags_pending == 0) {
625       int sl;
626       cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
627       sl = cpi->svc.spatial_layer_id;
628       vp9_apply_encoding_flags(cpi, cpi->svc.ext_frame_flags[sl]);
629       cpi->lst_fb_idx = cpi->svc.ext_lst_fb_idx[sl];
630       cpi->gld_fb_idx = cpi->svc.ext_gld_fb_idx[sl];
631       cpi->alt_fb_idx = cpi->svc.ext_alt_fb_idx[sl];
632     }
633   }
634
635   if (cpi->svc.spatial_layer_id == cpi->svc.first_spatial_layer_to_encode)
636     cpi->svc.rc_drop_superframe = 0;
637
638   lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
639                                    cpi->svc.number_temporal_layers +
640                                cpi->svc.temporal_layer_id];
641
642   // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
643   // only for non-BYPASS mode for now.
644   if (cpi->svc.temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
645     RATE_CONTROL *const lrc = &lc->rc;
646     lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
647     lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
648   }
649
650   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
651                        lc->scaling_factor_num, lc->scaling_factor_den, &width,
652                        &height);
653
654   // The usage of use_base_mv assumes down-scale of 2x2. For now, turn off use
655   // of base motion vectors if spatial scale factors for any layers are not 2,
656   // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
657   // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
658   // Same condition applies to use of non-zero phase_scaler.
659   if (cpi->svc.number_spatial_layers > 1) {
660     int sl;
661     for (sl = 0; sl < cpi->svc.number_spatial_layers - 1; ++sl) {
662       lc = &cpi->svc.layer_context[sl * cpi->svc.number_temporal_layers +
663                                    cpi->svc.temporal_layer_id];
664       if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
665           !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
666             cpi->svc.number_spatial_layers == 3)) {
667         int sl2;
668         cpi->svc.use_base_mv = 0;
669         for (sl2 = 0; sl2 < cpi->svc.number_spatial_layers - 1; ++sl2)
670           cpi->svc.filtertype_downsample_source[sl2] = 0;
671         break;
672       }
673     }
674   }
675
676   if (vp9_set_size_literal(cpi, width, height) != 0)
677     return VPX_CODEC_INVALID_PARAM;
678
679   return 0;
680 }
681
682 #if CONFIG_SPATIAL_SVC
683 #define SMALL_FRAME_FB_IDX 7
684
685 int vp9_svc_start_frame(VP9_COMP *const cpi) {
686   int width = 0, height = 0;
687   LAYER_CONTEXT *lc;
688   struct lookahead_entry *buf;
689   int count = 1 << (cpi->svc.number_temporal_layers - 1);
690
691   cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
692   lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id];
693
694   cpi->svc.temporal_layer_id = 0;
695   while ((lc->current_video_frame_in_layer % count) != 0) {
696     ++cpi->svc.temporal_layer_id;
697     count >>= 1;
698   }
699
700   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
701
702   cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
703
704   if (cpi->svc.spatial_layer_id == 0)
705     cpi->gld_fb_idx =
706         (lc->gold_ref_idx >= 0) ? lc->gold_ref_idx : cpi->lst_fb_idx;
707   else
708     cpi->gld_fb_idx = cpi->svc.spatial_layer_id - 1;
709
710   if (lc->current_video_frame_in_layer == 0) {
711     if (cpi->svc.spatial_layer_id >= 2) {
712       cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
713     } else {
714       cpi->alt_fb_idx = cpi->lst_fb_idx;
715       cpi->ref_frame_flags &= (~VP9_LAST_FLAG & ~VP9_ALT_FLAG);
716     }
717   } else {
718     if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id]) {
719       cpi->alt_fb_idx = lc->alt_ref_idx;
720       if (!lc->has_alt_frame) cpi->ref_frame_flags &= (~VP9_ALT_FLAG);
721     } else {
722       // Find a proper alt_fb_idx for layers that don't have alt ref frame
723       if (cpi->svc.spatial_layer_id == 0) {
724         cpi->alt_fb_idx = cpi->lst_fb_idx;
725       } else {
726         LAYER_CONTEXT *lc_lower =
727             &cpi->svc.layer_context[cpi->svc.spatial_layer_id - 1];
728
729         if (cpi->oxcf.ss_enable_auto_arf[cpi->svc.spatial_layer_id - 1] &&
730             lc_lower->alt_ref_source != NULL)
731           cpi->alt_fb_idx = lc_lower->alt_ref_idx;
732         else if (cpi->svc.spatial_layer_id >= 2)
733           cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2;
734         else
735           cpi->alt_fb_idx = cpi->lst_fb_idx;
736       }
737     }
738   }
739
740   get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height,
741                        lc->scaling_factor_num, lc->scaling_factor_den, &width,
742                        &height);
743
744   // Workaround for multiple frame contexts. In some frames we can't use prev_mi
745   // since its previous frame could be changed during decoding time. The idea is
746   // we put a empty invisible frame in front of them, then we will not use
747   // prev_mi when encoding these frames.
748
749   buf = vp9_lookahead_peek(cpi->lookahead, 0);
750   if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 &&
751       cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE &&
752       lc->rc.frames_to_key != 0 &&
753       !(buf != NULL && (buf->flags & VPX_EFLAG_FORCE_KF))) {
754     if ((cpi->svc.number_temporal_layers > 1 &&
755          cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) ||
756         (cpi->svc.number_spatial_layers > 1 &&
757          cpi->svc.spatial_layer_id == 0)) {
758       struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0);
759
760       if (buf != NULL) {
761         cpi->svc.empty_frame.ts_start = buf->ts_start;
762         cpi->svc.empty_frame.ts_end = buf->ts_end;
763         cpi->svc.encode_empty_frame_state = ENCODING;
764         cpi->common.show_frame = 0;
765         cpi->ref_frame_flags = 0;
766         cpi->common.frame_type = INTER_FRAME;
767         cpi->lst_fb_idx = cpi->gld_fb_idx = cpi->alt_fb_idx =
768             SMALL_FRAME_FB_IDX;
769
770         if (cpi->svc.encode_intra_empty_frame != 0) cpi->common.intra_only = 1;
771
772         width = SMALL_FRAME_WIDTH;
773         height = SMALL_FRAME_HEIGHT;
774       }
775     }
776   }
777
778   cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q);
779   cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q);
780
781   vp9_change_config(cpi, &cpi->oxcf);
782
783   if (vp9_set_size_literal(cpi, width, height) != 0)
784     return VPX_CODEC_INVALID_PARAM;
785
786   vp9_set_high_precision_mv(cpi, 1);
787
788   cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source;
789
790   return 0;
791 }
792
793 #undef SMALL_FRAME_FB_IDX
794 #endif  // CONFIG_SPATIAL_SVC
795
796 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
797                                               struct lookahead_ctx *ctx,
798                                               int drain) {
799   struct lookahead_entry *buf = NULL;
800   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
801     buf = vp9_lookahead_peek(ctx, 0);
802     if (buf != NULL) {
803       // Only remove the buffer when pop the highest layer.
804       if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
805         vp9_lookahead_pop(ctx, drain);
806       }
807     }
808   }
809   return buf;
810 }
811
812 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
813   int sl, tl;
814   SVC *const svc = &cpi->svc;
815   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
816   for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
817     for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
818       int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
819       LAYER_CONTEXT *const lc = &svc->layer_context[layer];
820       if (lc->map) vpx_free(lc->map);
821       if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
822       if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
823     }
824   }
825 }
826
827 // Reset on key frame: reset counters, references and buffer updates.
828 void vp9_svc_reset_key_frame(VP9_COMP *const cpi) {
829   int sl, tl;
830   SVC *const svc = &cpi->svc;
831   LAYER_CONTEXT *lc = NULL;
832   for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
833     for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
834       lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
835       lc->current_video_frame_in_layer = 0;
836       lc->frames_from_key_frame = 0;
837     }
838   }
839   if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
840     set_flags_and_fb_idx_for_temporal_mode3(cpi);
841   } else if (svc->temporal_layering_mode ==
842              VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
843     set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
844   } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
845     set_flags_and_fb_idx_for_temporal_mode2(cpi);
846   }
847   vp9_update_temporal_layer_framerate(cpi);
848   vp9_restore_layer_context(cpi);
849 }