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