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