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