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 = saturate_cast_double_to_int(
236 round(lc->target_bandwidth / lc->framerate));
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 = saturate_cast_double_to_int(
277 round(lc->target_bandwidth / lc->framerate));
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 saturate_cast_double_to_int(round(lc->target_bandwidth / lc->framerate));
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 saturate_cast_double_to_int(round(lc->target_bandwidth / lc->framerate));
344 const int64_t vbr_min_bits =
345 (int64_t)lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmin_section / 100;
346 lrc->min_frame_bandwidth = (int)VPXMIN(vbr_min_bits, INT_MAX);
347 const int64_t vbr_max_bits =
348 (int64_t)lrc->avg_frame_bandwidth * oxcf->two_pass_vbrmax_section / 100;
349 lrc->max_frame_bandwidth = (int)VPXMIN(vbr_max_bits, INT_MAX);
350 vp9_rc_set_gf_interval_range(cpi, lrc);
351 }
352
vp9_restore_layer_context(VP9_COMP * const cpi)353 void vp9_restore_layer_context(VP9_COMP *const cpi) {
354 LAYER_CONTEXT *const lc = get_layer_context(cpi);
355 const int old_frame_since_key = cpi->rc.frames_since_key;
356 const int old_frame_to_key = cpi->rc.frames_to_key;
357 const int old_ext_use_post_encode_drop = cpi->rc.ext_use_post_encode_drop;
358
359 cpi->rc = lc->rc;
360 cpi->twopass = lc->twopass;
361 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
362 cpi->alt_ref_source = lc->alt_ref_source;
363 // Check if it is one_pass_cbr_svc mode and lc->speed > 0 (real-time mode
364 // does not use speed = 0).
365 if (is_one_pass_svc(cpi) && lc->speed > 0) {
366 cpi->oxcf.speed = lc->speed;
367 }
368 cpi->loopfilter_ctrl = lc->loopfilter_ctrl;
369 // Reset the frames_since_key and frames_to_key counters to their values
370 // before the layer restore. Keep these defined for the stream (not layer).
371 if (cpi->svc.number_temporal_layers > 1 ||
372 cpi->svc.number_spatial_layers > 1) {
373 cpi->rc.frames_since_key = old_frame_since_key;
374 cpi->rc.frames_to_key = old_frame_to_key;
375 }
376 cpi->rc.ext_use_post_encode_drop = old_ext_use_post_encode_drop;
377 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
378 // for the base temporal layer.
379 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
380 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
381 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
382 swap_ptr(&cr->map, &lc->map);
383 swap_ptr(&cr->last_coded_q_map, &lc->last_coded_q_map);
384 swap_ptr(&cpi->consec_zero_mv, &lc->consec_zero_mv);
385 cr->sb_index = lc->sb_index;
386 cr->actual_num_seg1_blocks = lc->actual_num_seg1_blocks;
387 cr->actual_num_seg2_blocks = lc->actual_num_seg2_blocks;
388 cr->counter_encode_maxq_scene_change = lc->counter_encode_maxq_scene_change;
389 }
390 }
391
vp9_save_layer_context(VP9_COMP * const cpi)392 void vp9_save_layer_context(VP9_COMP *const cpi) {
393 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
394 LAYER_CONTEXT *const lc = get_layer_context(cpi);
395
396 lc->rc = cpi->rc;
397 lc->twopass = cpi->twopass;
398 lc->target_bandwidth = (int)oxcf->target_bandwidth;
399 lc->alt_ref_source = cpi->alt_ref_source;
400 lc->frame_qp = cpi->common.base_qindex;
401 lc->MBs = cpi->common.MBs;
402
403 // For spatial-svc, allow cyclic-refresh to be applied on the spatial layers,
404 // for the base temporal layer.
405 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
406 cpi->svc.number_spatial_layers > 1 && cpi->svc.temporal_layer_id == 0) {
407 CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
408 signed char *temp = lc->map;
409 uint8_t *temp2 = lc->last_coded_q_map;
410 uint8_t *temp3 = lc->consec_zero_mv;
411 lc->map = cr->map;
412 cr->map = temp;
413 lc->last_coded_q_map = cr->last_coded_q_map;
414 cr->last_coded_q_map = temp2;
415 lc->consec_zero_mv = cpi->consec_zero_mv;
416 cpi->consec_zero_mv = temp3;
417 lc->sb_index = cr->sb_index;
418 lc->actual_num_seg1_blocks = cr->actual_num_seg1_blocks;
419 lc->actual_num_seg2_blocks = cr->actual_num_seg2_blocks;
420 lc->counter_encode_maxq_scene_change = cr->counter_encode_maxq_scene_change;
421 lc->qindex_delta[0] = cr->qindex_delta[0];
422 lc->qindex_delta[1] = cr->qindex_delta[1];
423 lc->qindex_delta[2] = cr->qindex_delta[2];
424 }
425 }
426
427 #if !CONFIG_REALTIME_ONLY
vp9_init_second_pass_spatial_svc(VP9_COMP * cpi)428 void vp9_init_second_pass_spatial_svc(VP9_COMP *cpi) {
429 SVC *const svc = &cpi->svc;
430 int i;
431
432 for (i = 0; i < svc->number_spatial_layers; ++i) {
433 TWO_PASS *const twopass = &svc->layer_context[i].twopass;
434
435 svc->spatial_layer_id = i;
436 vp9_init_second_pass(cpi);
437
438 twopass->total_stats.spatial_layer_id = i;
439 twopass->total_left_stats.spatial_layer_id = i;
440 }
441 svc->spatial_layer_id = 0;
442 }
443 #endif // !CONFIG_REALTIME_ONLY
444
vp9_inc_frame_in_layer(VP9_COMP * const cpi)445 void vp9_inc_frame_in_layer(VP9_COMP *const cpi) {
446 LAYER_CONTEXT *const lc =
447 &cpi->svc.layer_context[cpi->svc.spatial_layer_id *
448 cpi->svc.number_temporal_layers];
449 ++lc->current_video_frame_in_layer;
450 ++lc->frames_from_key_frame;
451 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)
452 ++cpi->svc.current_superframe;
453 }
454
get_layer_resolution(const int width_org,const int height_org,const int num,const int den,int * width_out,int * height_out)455 void get_layer_resolution(const int width_org, const int height_org,
456 const int num, const int den, int *width_out,
457 int *height_out) {
458 int w, h;
459
460 if (width_out == NULL || height_out == NULL || den == 0) return;
461
462 w = width_org * num / den;
463 h = height_org * num / den;
464
465 // make height and width even to make chrome player happy
466 w += w % 2;
467 h += h % 2;
468
469 *width_out = w;
470 *height_out = h;
471 }
472
reset_fb_idx_unused(VP9_COMP * const cpi)473 static void reset_fb_idx_unused(VP9_COMP *const cpi) {
474 // If a reference frame is not referenced or refreshed, then set the
475 // fb_idx for that reference to the first one used/referenced.
476 // This is to avoid setting fb_idx for a reference to a slot that is not
477 // used/needed (i.e., since that reference is not referenced or refreshed).
478 MV_REFERENCE_FRAME ref_frame;
479 MV_REFERENCE_FRAME first_ref = 0;
480 int first_fb_idx = 0;
481 int fb_idx[3] = { cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx };
482 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
483 if (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame)) {
484 first_ref = ref_frame;
485 first_fb_idx = fb_idx[ref_frame - 1];
486 break;
487 }
488 }
489 if (first_ref > 0) {
490 if (first_ref != LAST_FRAME && !(cpi->ref_frame_flags & VP9_LAST_FLAG) &&
491 !cpi->ext_refresh_last_frame)
492 cpi->lst_fb_idx = first_fb_idx;
493 else if (first_ref != GOLDEN_FRAME &&
494 !(cpi->ref_frame_flags & VP9_GOLD_FLAG) &&
495 !cpi->ext_refresh_golden_frame)
496 cpi->gld_fb_idx = first_fb_idx;
497 else if (first_ref != ALTREF_FRAME &&
498 !(cpi->ref_frame_flags & VP9_ALT_FLAG) &&
499 !cpi->ext_refresh_alt_ref_frame)
500 cpi->alt_fb_idx = first_fb_idx;
501 }
502 }
503
504 // Never refresh any reference frame buffers on top temporal layers in
505 // simulcast mode, which has interlayer prediction disabled.
non_reference_frame_simulcast(VP9_COMP * const cpi)506 static void non_reference_frame_simulcast(VP9_COMP *const cpi) {
507 if (cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1 &&
508 cpi->svc.temporal_layer_id > 0) {
509 cpi->ext_refresh_last_frame = 0;
510 cpi->ext_refresh_golden_frame = 0;
511 cpi->ext_refresh_alt_ref_frame = 0;
512 }
513 }
514
515 // The function sets proper ref_frame_flags, buffer indices, and buffer update
516 // variables for temporal layering mode 3 - that does 0-2-1-2 temporal layering
517 // scheme.
set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP * const cpi)518 static void set_flags_and_fb_idx_for_temporal_mode3(VP9_COMP *const cpi) {
519 int frame_num_within_temporal_struct = 0;
520 int spatial_id, temporal_id;
521 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
522 frame_num_within_temporal_struct =
523 cpi->svc
524 .layer_context[cpi->svc.spatial_layer_id *
525 cpi->svc.number_temporal_layers]
526 .current_video_frame_in_layer %
527 4;
528 temporal_id = cpi->svc.temporal_layer_id =
529 (frame_num_within_temporal_struct & 1)
530 ? 2
531 : (frame_num_within_temporal_struct >> 1);
532 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
533 cpi->ext_refresh_alt_ref_frame = 0;
534 if (!temporal_id) {
535 cpi->ext_refresh_frame_flags_pending = 1;
536 cpi->ext_refresh_last_frame = 1;
537 if (!spatial_id) {
538 cpi->ref_frame_flags = VP9_LAST_FLAG;
539 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
540 // base layer is a key frame.
541 cpi->ref_frame_flags = VP9_LAST_FLAG;
542 cpi->ext_refresh_last_frame = 0;
543 cpi->ext_refresh_golden_frame = 1;
544 } else {
545 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
546 }
547 } else if (temporal_id == 1) {
548 cpi->ext_refresh_frame_flags_pending = 1;
549 cpi->ext_refresh_alt_ref_frame = 1;
550 if (!spatial_id) {
551 cpi->ref_frame_flags = VP9_LAST_FLAG;
552 } else {
553 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
554 }
555 } else {
556 if (frame_num_within_temporal_struct == 1) {
557 // the first tl2 picture
558 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
559 cpi->ext_refresh_frame_flags_pending = 1;
560 if (!spatial_id)
561 cpi->ref_frame_flags = VP9_LAST_FLAG;
562 else
563 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
564 } else if (!spatial_id) {
565 cpi->ext_refresh_frame_flags_pending = 1;
566 cpi->ext_refresh_alt_ref_frame = 1;
567 cpi->ref_frame_flags = VP9_LAST_FLAG;
568 } else if (spatial_id < cpi->svc.number_spatial_layers - 1) {
569 cpi->ext_refresh_frame_flags_pending = 1;
570 cpi->ext_refresh_alt_ref_frame = 1;
571 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
572 }
573 } else {
574 // The second tl2 picture
575 if (spatial_id == cpi->svc.number_spatial_layers - 1) { // top layer
576 cpi->ext_refresh_frame_flags_pending = 1;
577 if (!spatial_id)
578 cpi->ref_frame_flags = VP9_LAST_FLAG;
579 else
580 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
581 } else if (!spatial_id) {
582 cpi->ext_refresh_frame_flags_pending = 1;
583 cpi->ref_frame_flags = VP9_LAST_FLAG;
584 cpi->ext_refresh_alt_ref_frame = 1;
585 } else { // top layer
586 cpi->ext_refresh_frame_flags_pending = 1;
587 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
588 cpi->ext_refresh_alt_ref_frame = 1;
589 }
590 }
591 }
592 if (temporal_id == 0) {
593 cpi->lst_fb_idx = spatial_id;
594 if (spatial_id) {
595 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
596 cpi->lst_fb_idx = spatial_id - 1;
597 cpi->gld_fb_idx = spatial_id;
598 } else {
599 cpi->gld_fb_idx = spatial_id - 1;
600 }
601 } else {
602 cpi->gld_fb_idx = 0;
603 }
604 cpi->alt_fb_idx = 0;
605 } else if (temporal_id == 1) {
606 cpi->lst_fb_idx = spatial_id;
607 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
608 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
609 } else if (frame_num_within_temporal_struct == 1) {
610 cpi->lst_fb_idx = spatial_id;
611 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
612 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
613 } else {
614 cpi->lst_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
615 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
616 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
617 }
618
619 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
620
621 reset_fb_idx_unused(cpi);
622 }
623
624 // The function sets proper ref_frame_flags, buffer indices, and buffer update
625 // variables for temporal layering mode 2 - that does 0-1-0-1 temporal layering
626 // scheme.
set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP * const cpi)627 static void set_flags_and_fb_idx_for_temporal_mode2(VP9_COMP *const cpi) {
628 int spatial_id, temporal_id;
629 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
630 temporal_id = cpi->svc.temporal_layer_id =
631 cpi->svc
632 .layer_context[cpi->svc.spatial_layer_id *
633 cpi->svc.number_temporal_layers]
634 .current_video_frame_in_layer &
635 1;
636 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
637 cpi->ext_refresh_alt_ref_frame = 0;
638 if (!temporal_id) {
639 cpi->ext_refresh_frame_flags_pending = 1;
640 cpi->ext_refresh_last_frame = 1;
641 if (!spatial_id) {
642 cpi->ref_frame_flags = VP9_LAST_FLAG;
643 } else if (cpi->svc.layer_context[temporal_id].is_key_frame) {
644 // base layer is a key frame.
645 cpi->ref_frame_flags = VP9_LAST_FLAG;
646 cpi->ext_refresh_last_frame = 0;
647 cpi->ext_refresh_golden_frame = 1;
648 } else {
649 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
650 }
651 } else if (temporal_id == 1) {
652 cpi->ext_refresh_frame_flags_pending = 1;
653 cpi->ext_refresh_alt_ref_frame = 1;
654 if (!spatial_id) {
655 cpi->ref_frame_flags = VP9_LAST_FLAG;
656 } else {
657 if (spatial_id == cpi->svc.number_spatial_layers - 1)
658 cpi->ext_refresh_alt_ref_frame = 0;
659 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
660 }
661 }
662
663 if (temporal_id == 0) {
664 cpi->lst_fb_idx = spatial_id;
665 if (spatial_id) {
666 if (cpi->svc.layer_context[temporal_id].is_key_frame) {
667 cpi->lst_fb_idx = spatial_id - 1;
668 cpi->gld_fb_idx = spatial_id;
669 } else {
670 cpi->gld_fb_idx = spatial_id - 1;
671 }
672 } else {
673 cpi->gld_fb_idx = 0;
674 }
675 cpi->alt_fb_idx = 0;
676 } else if (temporal_id == 1) {
677 cpi->lst_fb_idx = spatial_id;
678 cpi->gld_fb_idx = cpi->svc.number_spatial_layers + spatial_id - 1;
679 cpi->alt_fb_idx = cpi->svc.number_spatial_layers + spatial_id;
680 }
681
682 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
683
684 reset_fb_idx_unused(cpi);
685 }
686
687 // The function sets proper ref_frame_flags, buffer indices, and buffer update
688 // variables for temporal layering mode 0 - that has no temporal layering.
set_flags_and_fb_idx_for_temporal_mode_noLayering(VP9_COMP * const cpi)689 static void set_flags_and_fb_idx_for_temporal_mode_noLayering(
690 VP9_COMP *const cpi) {
691 int spatial_id;
692 spatial_id = cpi->svc.spatial_layer_id = cpi->svc.spatial_layer_to_encode;
693 cpi->ext_refresh_last_frame = cpi->ext_refresh_golden_frame =
694 cpi->ext_refresh_alt_ref_frame = 0;
695 cpi->ext_refresh_frame_flags_pending = 1;
696 cpi->ext_refresh_last_frame = 1;
697 if (!spatial_id) {
698 cpi->ref_frame_flags = VP9_LAST_FLAG;
699 } else if (cpi->svc.layer_context[0].is_key_frame) {
700 cpi->ref_frame_flags = VP9_LAST_FLAG;
701 cpi->ext_refresh_last_frame = 0;
702 cpi->ext_refresh_golden_frame = 1;
703 } else {
704 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
705 }
706 cpi->lst_fb_idx = spatial_id;
707 if (spatial_id) {
708 if (cpi->svc.layer_context[0].is_key_frame) {
709 cpi->lst_fb_idx = spatial_id - 1;
710 cpi->gld_fb_idx = spatial_id;
711 } else {
712 cpi->gld_fb_idx = spatial_id - 1;
713 }
714 } else {
715 cpi->gld_fb_idx = 0;
716 }
717
718 if (cpi->svc.simulcast_mode) non_reference_frame_simulcast(cpi);
719
720 reset_fb_idx_unused(cpi);
721 }
722
set_flags_and_fb_idx_bypass_via_set_ref_frame_config(VP9_COMP * const cpi)723 static void set_flags_and_fb_idx_bypass_via_set_ref_frame_config(
724 VP9_COMP *const cpi) {
725 SVC *const svc = &cpi->svc;
726 int sl = svc->spatial_layer_id = svc->spatial_layer_to_encode;
727 cpi->svc.temporal_layer_id = cpi->svc.temporal_layer_id_per_spatial[sl];
728 cpi->ext_refresh_frame_flags_pending = 1;
729 cpi->lst_fb_idx = svc->lst_fb_idx[sl];
730 cpi->gld_fb_idx = svc->gld_fb_idx[sl];
731 cpi->alt_fb_idx = svc->alt_fb_idx[sl];
732 cpi->ext_refresh_last_frame = 0;
733 cpi->ext_refresh_golden_frame = 0;
734 cpi->ext_refresh_alt_ref_frame = 0;
735 cpi->ref_frame_flags = 0;
736 if (svc->reference_last[sl]) cpi->ref_frame_flags |= VP9_LAST_FLAG;
737 if (svc->reference_golden[sl]) cpi->ref_frame_flags |= VP9_GOLD_FLAG;
738 if (svc->reference_altref[sl]) cpi->ref_frame_flags |= VP9_ALT_FLAG;
739 }
740
vp9_copy_flags_ref_update_idx(VP9_COMP * const cpi)741 void vp9_copy_flags_ref_update_idx(VP9_COMP *const cpi) {
742 SVC *const svc = &cpi->svc;
743 int sl = svc->spatial_layer_id;
744 svc->lst_fb_idx[sl] = cpi->lst_fb_idx;
745 svc->gld_fb_idx[sl] = cpi->gld_fb_idx;
746 svc->alt_fb_idx[sl] = cpi->alt_fb_idx;
747 // For the fixed SVC mode: pass the refresh_lst/gld/alt_frame flags to the
748 // update_buffer_slot, this is needed for the GET_SVC_REF_FRAME_CONFIG api.
749 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
750 int ref;
751 for (ref = 0; ref < REF_FRAMES; ++ref) {
752 svc->update_buffer_slot[sl] &= ~(1 << ref);
753 if ((ref == svc->lst_fb_idx[sl] && cpi->refresh_last_frame) ||
754 (ref == svc->gld_fb_idx[sl] && cpi->refresh_golden_frame) ||
755 (ref == svc->alt_fb_idx[sl] && cpi->refresh_alt_ref_frame))
756 svc->update_buffer_slot[sl] |= (1 << ref);
757 }
758 }
759
760 // TODO(jianj): Remove these 3, deprecated.
761 svc->update_last[sl] = (uint8_t)cpi->refresh_last_frame;
762 svc->update_golden[sl] = (uint8_t)cpi->refresh_golden_frame;
763 svc->update_altref[sl] = (uint8_t)cpi->refresh_alt_ref_frame;
764
765 svc->reference_last[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_LAST_FLAG);
766 svc->reference_golden[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_GOLD_FLAG);
767 svc->reference_altref[sl] = (uint8_t)(cpi->ref_frame_flags & VP9_ALT_FLAG);
768 }
769
vp9_one_pass_svc_start_layer(VP9_COMP * const cpi)770 int vp9_one_pass_svc_start_layer(VP9_COMP *const cpi) {
771 int width = 0, height = 0;
772 SVC *const svc = &cpi->svc;
773 LAYER_CONTEXT *lc = NULL;
774 int scaling_factor_num = 1;
775 int scaling_factor_den = 1;
776 svc->skip_enhancement_layer = 0;
777
778 if (svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF &&
779 svc->number_spatial_layers > 1 && svc->number_spatial_layers <= 3 &&
780 svc->number_temporal_layers <= 3)
781 svc->simulcast_mode = 1;
782 else
783 svc->simulcast_mode = 0;
784
785 if (svc->number_spatial_layers > 1) {
786 svc->use_base_mv = 1;
787 svc->use_partition_reuse = 1;
788 }
789 svc->force_zero_mode_spatial_ref = 1;
790 svc->mi_stride[svc->spatial_layer_id] = cpi->common.mi_stride;
791 svc->mi_rows[svc->spatial_layer_id] = cpi->common.mi_rows;
792 svc->mi_cols[svc->spatial_layer_id] = cpi->common.mi_cols;
793
794 // For constrained_from_above drop mode: before encoding superframe (i.e.,
795 // at SL0 frame) check all spatial layers (starting from top) for possible
796 // drop, and if so, set a flag to force drop of that layer and all its lower
797 // layers.
798 if (svc->spatial_layer_to_encode == svc->first_spatial_layer_to_encode) {
799 int sl;
800 for (sl = 0; sl < svc->number_spatial_layers; sl++)
801 svc->force_drop_constrained_from_above[sl] = 0;
802 if (svc->framedrop_mode == CONSTRAINED_FROM_ABOVE_DROP) {
803 for (sl = svc->number_spatial_layers - 1;
804 sl >= svc->first_spatial_layer_to_encode; sl--) {
805 int layer = sl * svc->number_temporal_layers + svc->temporal_layer_id;
806 LAYER_CONTEXT *const sl_lc = &svc->layer_context[layer];
807 cpi->rc = sl_lc->rc;
808 cpi->oxcf.target_bandwidth = sl_lc->target_bandwidth;
809 if (vp9_test_drop(cpi)) {
810 int sl2;
811 // Set flag to force drop in encoding for this mode.
812 for (sl2 = sl; sl2 >= svc->first_spatial_layer_to_encode; sl2--)
813 svc->force_drop_constrained_from_above[sl2] = 1;
814 break;
815 }
816 }
817 }
818 }
819
820 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
821 set_flags_and_fb_idx_for_temporal_mode3(cpi);
822 } else if (svc->temporal_layering_mode ==
823 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
824 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
825 } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
826 set_flags_and_fb_idx_for_temporal_mode2(cpi);
827 } else if (svc->temporal_layering_mode ==
828 VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
829 svc->use_set_ref_frame_config) {
830 set_flags_and_fb_idx_bypass_via_set_ref_frame_config(cpi);
831 }
832
833 if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
834 cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[0].idx ||
835 cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[0].idx)
836 svc->buffer_gf_temporal_ref[0].is_used = 1;
837 if (cpi->lst_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
838 cpi->gld_fb_idx == svc->buffer_gf_temporal_ref[1].idx ||
839 cpi->alt_fb_idx == svc->buffer_gf_temporal_ref[1].idx)
840 svc->buffer_gf_temporal_ref[1].is_used = 1;
841
842 // For the fixed (non-flexible/bypass) SVC mode:
843 // If long term temporal reference is enabled at the sequence level
844 // (use_gf_temporal_ref == 1), and inter_layer is disabled (on inter-frames),
845 // we can use golden as a second temporal reference
846 // (since the spatial/inter-layer reference is disabled).
847 // We check that the fb_idx for this reference (buffer_gf_temporal_ref.idx) is
848 // unused (slot 7 and 6 should be available for 3-3 layer system).
849 // For now usage of this second temporal reference will only be used for
850 // highest and next to highest spatial layer (i.e., top and middle layer for
851 // 3 spatial layers).
852 svc->use_gf_temporal_ref_current_layer = 0;
853 if (svc->use_gf_temporal_ref && !svc->buffer_gf_temporal_ref[0].is_used &&
854 !svc->buffer_gf_temporal_ref[1].is_used &&
855 svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
856 svc->disable_inter_layer_pred != INTER_LAYER_PRED_ON &&
857 svc->number_spatial_layers <= 3 && svc->number_temporal_layers <= 3 &&
858 svc->spatial_layer_id >= svc->number_spatial_layers - 2) {
859 // Enable the second (long-term) temporal reference at the frame-level.
860 svc->use_gf_temporal_ref_current_layer = 1;
861 }
862
863 // Check if current superframe has any layer sync, only check once on
864 // base layer.
865 if (svc->spatial_layer_id == 0) {
866 int sl = 0;
867 // Default is no sync.
868 svc->superframe_has_layer_sync = 0;
869 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
870 if (cpi->svc.spatial_layer_sync[sl]) svc->superframe_has_layer_sync = 1;
871 }
872 }
873
874 // Reset the drop flags for all spatial layers, on the
875 // first_spatial_layer_to_encode.
876 if (svc->spatial_layer_id == svc->first_spatial_layer_to_encode) {
877 vp9_zero(svc->drop_spatial_layer);
878 // TODO(jianj/marpan): Investigate why setting svc->lst/gld/alt_fb_idx
879 // causes an issue with frame dropping and temporal layers, when the frame
880 // flags are passed via the encode call (bypass mode). Issue is that we're
881 // resetting ext_refresh_frame_flags_pending to 0 on frame drops.
882 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
883 memset(&svc->lst_fb_idx, -1, sizeof(svc->lst_fb_idx));
884 memset(&svc->gld_fb_idx, -1, sizeof(svc->lst_fb_idx));
885 memset(&svc->alt_fb_idx, -1, sizeof(svc->lst_fb_idx));
886 // These are set by API before the superframe is encoded and they are
887 // passed to encoder layer by layer. Don't reset them on layer 0 in bypass
888 // mode.
889 vp9_zero(svc->update_buffer_slot);
890 vp9_zero(svc->reference_last);
891 vp9_zero(svc->reference_golden);
892 vp9_zero(svc->reference_altref);
893 // TODO(jianj): Remove these 3, deprecated.
894 vp9_zero(svc->update_last);
895 vp9_zero(svc->update_golden);
896 vp9_zero(svc->update_altref);
897 }
898 }
899
900 lc = &svc->layer_context[svc->spatial_layer_id * svc->number_temporal_layers +
901 svc->temporal_layer_id];
902
903 // Setting the worst/best_quality via the encoder control: SET_SVC_PARAMETERS,
904 // only for non-BYPASS mode for now.
905 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS ||
906 svc->use_set_ref_frame_config) {
907 RATE_CONTROL *const lrc = &lc->rc;
908 lrc->worst_quality = vp9_quantizer_to_qindex(lc->max_q);
909 lrc->best_quality = vp9_quantizer_to_qindex(lc->min_q);
910 if (cpi->fixed_qp_onepass) {
911 lrc->worst_quality = cpi->rc.worst_quality;
912 lrc->best_quality = cpi->rc.best_quality;
913 }
914 }
915
916 if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && svc->single_layer_svc == 1 &&
917 svc->spatial_layer_id == svc->first_spatial_layer_to_encode &&
918 cpi->resize_state != ORIG) {
919 scaling_factor_num = lc->scaling_factor_num_resize;
920 scaling_factor_den = lc->scaling_factor_den_resize;
921 } else {
922 scaling_factor_num = lc->scaling_factor_num;
923 scaling_factor_den = lc->scaling_factor_den;
924 }
925
926 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, scaling_factor_num,
927 scaling_factor_den, &width, &height);
928
929 // Use Eightap_smooth for low resolutions.
930 if (width * height <= 320 * 240)
931 svc->downsample_filter_type[svc->spatial_layer_id] = EIGHTTAP_SMOOTH;
932 // For scale factors > 0.75, set the phase to 0 (aligns decimated pixel
933 // to source pixel).
934 if (scaling_factor_num > (3 * scaling_factor_den) >> 2)
935 svc->downsample_filter_phase[svc->spatial_layer_id] = 0;
936
937 // The usage of use_base_mv or partition_reuse assumes down-scale of 2x2.
938 // For now, turn off use of base motion vectors and partition reuse if the
939 // spatial scale factors for any layers are not 2,
940 // keep the case of 3 spatial layers with scale factor of 4x4 for base layer.
941 // TODO(marpan): Fix this to allow for use_base_mv for scale factors != 2.
942 if (svc->number_spatial_layers > 1) {
943 int sl;
944 for (sl = 0; sl < svc->number_spatial_layers - 1; ++sl) {
945 lc = &svc->layer_context[sl * svc->number_temporal_layers +
946 svc->temporal_layer_id];
947 if ((lc->scaling_factor_num != lc->scaling_factor_den >> 1) &&
948 !(lc->scaling_factor_num == lc->scaling_factor_den >> 2 && sl == 0 &&
949 svc->number_spatial_layers == 3)) {
950 svc->use_base_mv = 0;
951 svc->use_partition_reuse = 0;
952 break;
953 }
954 }
955 // For non-zero spatial layers: if the previous spatial layer was dropped
956 // disable the base_mv and partition_reuse features.
957 if (svc->spatial_layer_id > 0 &&
958 svc->drop_spatial_layer[svc->spatial_layer_id - 1]) {
959 svc->use_base_mv = 0;
960 svc->use_partition_reuse = 0;
961 }
962 }
963
964 svc->non_reference_frame = 0;
965 if (cpi->common.frame_type != KEY_FRAME && !cpi->ext_refresh_last_frame &&
966 !cpi->ext_refresh_golden_frame && !cpi->ext_refresh_alt_ref_frame)
967 svc->non_reference_frame = 1;
968 // For flexible mode, where update_buffer_slot is used, need to check if
969 // all buffer slots are not refreshed.
970 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
971 if (svc->update_buffer_slot[svc->spatial_layer_id] != 0)
972 svc->non_reference_frame = 0;
973 }
974
975 if (svc->spatial_layer_id == 0) {
976 svc->high_source_sad_superframe = 0;
977 svc->high_num_blocks_with_motion = 0;
978 }
979
980 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
981 svc->last_layer_dropped[svc->spatial_layer_id] &&
982 svc->fb_idx_upd_tl0[svc->spatial_layer_id] != -1 &&
983 !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
984 // For fixed/non-flexible mode, if the previous frame (same spatial layer
985 // from previous superframe) was dropped, make sure the lst_fb_idx
986 // for this frame corresponds to the buffer index updated on (last) encoded
987 // TL0 frame (with same spatial layer).
988 cpi->lst_fb_idx = svc->fb_idx_upd_tl0[svc->spatial_layer_id];
989 }
990
991 if (vp9_set_size_literal(cpi, width, height) != 0)
992 return VPX_CODEC_INVALID_PARAM;
993
994 return 0;
995 }
996
vp9_svc_lookahead_pop(VP9_COMP * const cpi,struct lookahead_ctx * ctx,int drain)997 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi,
998 struct lookahead_ctx *ctx,
999 int drain) {
1000 struct lookahead_entry *buf = NULL;
1001 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
1002 buf = vp9_lookahead_peek(ctx, 0);
1003 if (buf != NULL) {
1004 // Only remove the buffer when pop the highest layer.
1005 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) {
1006 vp9_lookahead_pop(ctx, drain);
1007 }
1008 }
1009 }
1010 return buf;
1011 }
1012
vp9_free_svc_cyclic_refresh(VP9_COMP * const cpi)1013 void vp9_free_svc_cyclic_refresh(VP9_COMP *const cpi) {
1014 int sl, tl;
1015 SVC *const svc = &cpi->svc;
1016 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1017 for (sl = 0; sl < oxcf->ss_number_layers; ++sl) {
1018 for (tl = 0; tl < oxcf->ts_number_layers; ++tl) {
1019 int layer = LAYER_IDS_TO_IDX(sl, tl, oxcf->ts_number_layers);
1020 LAYER_CONTEXT *const lc = &svc->layer_context[layer];
1021 if (lc->map) vpx_free(lc->map);
1022 if (lc->last_coded_q_map) vpx_free(lc->last_coded_q_map);
1023 if (lc->consec_zero_mv) vpx_free(lc->consec_zero_mv);
1024 }
1025 }
1026 }
1027
1028 // Reset on key frame: reset counters, references and buffer updates.
vp9_svc_reset_temporal_layers(VP9_COMP * const cpi,int is_key)1029 void vp9_svc_reset_temporal_layers(VP9_COMP *const cpi, int is_key) {
1030 int sl, tl;
1031 SVC *const svc = &cpi->svc;
1032 LAYER_CONTEXT *lc = NULL;
1033 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1034 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1035 lc = &cpi->svc.layer_context[sl * svc->number_temporal_layers + tl];
1036 lc->current_video_frame_in_layer = 0;
1037 if (is_key) lc->frames_from_key_frame = 0;
1038 }
1039 }
1040 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0212) {
1041 set_flags_and_fb_idx_for_temporal_mode3(cpi);
1042 } else if (svc->temporal_layering_mode ==
1043 VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING) {
1044 set_flags_and_fb_idx_for_temporal_mode_noLayering(cpi);
1045 } else if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_0101) {
1046 set_flags_and_fb_idx_for_temporal_mode2(cpi);
1047 }
1048 vp9_update_temporal_layer_framerate(cpi);
1049 vp9_restore_layer_context(cpi);
1050 }
1051
vp9_svc_check_reset_layer_rc_flag(VP9_COMP * const cpi)1052 void vp9_svc_check_reset_layer_rc_flag(VP9_COMP *const cpi) {
1053 SVC *svc = &cpi->svc;
1054 int sl, tl;
1055 for (sl = 0; sl < svc->number_spatial_layers; ++sl) {
1056 // Check for reset based on avg_frame_bandwidth for spatial layer sl.
1057 const int spatial_layer_idx = LAYER_IDS_TO_IDX(
1058 sl, svc->number_temporal_layers - 1, svc->number_temporal_layers);
1059 LAYER_CONTEXT *lc = &svc->layer_context[spatial_layer_idx];
1060 RATE_CONTROL *lrc = &lc->rc;
1061 if (lrc->avg_frame_bandwidth / 3 > (lrc->last_avg_frame_bandwidth >> 1) ||
1062 lrc->avg_frame_bandwidth < (lrc->last_avg_frame_bandwidth >> 1)) {
1063 // Reset for all temporal layers with spatial layer sl.
1064 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1065 int temporal_layer_idx =
1066 LAYER_IDS_TO_IDX(sl, tl, svc->number_temporal_layers);
1067 lrc = &svc->layer_context[temporal_layer_idx].rc;
1068 lrc->rc_1_frame = 0;
1069 lrc->rc_2_frame = 0;
1070 lrc->bits_off_target = lrc->optimal_buffer_level;
1071 lrc->buffer_level = lrc->optimal_buffer_level;
1072 }
1073 }
1074 }
1075 }
1076
vp9_svc_constrain_inter_layer_pred(VP9_COMP * const cpi)1077 void vp9_svc_constrain_inter_layer_pred(VP9_COMP *const cpi) {
1078 VP9_COMMON *const cm = &cpi->common;
1079 SVC *const svc = &cpi->svc;
1080 const int sl = svc->spatial_layer_id;
1081 // Check for disabling inter-layer (spatial) prediction, if
1082 // svc.disable_inter_layer_pred is set. If the previous spatial layer was
1083 // dropped then disable the prediction from this (scaled) reference.
1084 // For INTER_LAYER_PRED_OFF_NONKEY: inter-layer prediction is disabled
1085 // on key frames or if any spatial layer is a sync layer.
1086 if ((svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF_NONKEY &&
1087 !svc->layer_context[svc->temporal_layer_id].is_key_frame &&
1088 !svc->superframe_has_layer_sync) ||
1089 svc->disable_inter_layer_pred == INTER_LAYER_PRED_OFF ||
1090 svc->drop_spatial_layer[sl - 1]) {
1091 MV_REFERENCE_FRAME ref_frame;
1092 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1093 const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_buffer(cpi, ref_frame);
1094 if (yv12 != NULL &&
1095 (cpi->ref_frame_flags & ref_frame_to_flag(ref_frame))) {
1096 const struct scale_factors *const scale_fac =
1097 &cm->frame_refs[ref_frame - 1].sf;
1098 if (vp9_is_scaled(scale_fac)) {
1099 cpi->ref_frame_flags &= (~ref_frame_to_flag(ref_frame));
1100 // Point golden/altref frame buffer index to last.
1101 if (!svc->simulcast_mode) {
1102 if (ref_frame == GOLDEN_FRAME)
1103 cpi->gld_fb_idx = cpi->lst_fb_idx;
1104 else if (ref_frame == ALTREF_FRAME)
1105 cpi->alt_fb_idx = cpi->lst_fb_idx;
1106 }
1107 }
1108 }
1109 }
1110 }
1111 // For fixed/non-flexible SVC: check for disabling inter-layer prediction.
1112 // If the reference for inter-layer prediction (the reference that is scaled)
1113 // is not the previous spatial layer from the same superframe, then we disable
1114 // inter-layer prediction. Only need to check when inter_layer prediction is
1115 // not set to OFF mode.
1116 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1117 svc->disable_inter_layer_pred != INTER_LAYER_PRED_OFF) {
1118 // We only use LAST and GOLDEN for prediction in real-time mode, so we
1119 // check both here.
1120 MV_REFERENCE_FRAME ref_frame;
1121 for (ref_frame = LAST_FRAME; ref_frame <= GOLDEN_FRAME; ref_frame++) {
1122 struct scale_factors *scale_fac = &cm->frame_refs[ref_frame - 1].sf;
1123 if (vp9_is_scaled(scale_fac)) {
1124 // If this reference was updated on the previous spatial layer of the
1125 // current superframe, then we keep this reference (don't disable).
1126 // Otherwise we disable the inter-layer prediction.
1127 // This condition is verified by checking if the current frame buffer
1128 // index is equal to any of the slots for the previous spatial layer,
1129 // and if so, check if that slot was updated/refreshed. If that is the
1130 // case, then this reference is valid for inter-layer prediction under
1131 // the mode INTER_LAYER_PRED_ON_CONSTRAINED.
1132 int fb_idx =
1133 ref_frame == LAST_FRAME ? cpi->lst_fb_idx : cpi->gld_fb_idx;
1134 int ref_flag = ref_frame == LAST_FRAME ? VP9_LAST_FLAG : VP9_GOLD_FLAG;
1135 int disable = 1;
1136 if (fb_idx < 0) continue;
1137 if ((fb_idx == svc->lst_fb_idx[sl - 1] &&
1138 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1139 (fb_idx == svc->gld_fb_idx[sl - 1] &&
1140 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))) ||
1141 (fb_idx == svc->alt_fb_idx[sl - 1] &&
1142 (svc->update_buffer_slot[sl - 1] & (1 << fb_idx))))
1143 disable = 0;
1144 if (disable) cpi->ref_frame_flags &= (~ref_flag);
1145 }
1146 }
1147 }
1148 }
1149
vp9_svc_assert_constraints_pattern(VP9_COMP * const cpi)1150 void vp9_svc_assert_constraints_pattern(VP9_COMP *const cpi) {
1151 SVC *const svc = &cpi->svc;
1152 // For fixed/non-flexible mode, the following constraint are expected,
1153 // when inter-layer prediction is on (default).
1154 if (svc->temporal_layering_mode != VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1155 svc->disable_inter_layer_pred == INTER_LAYER_PRED_ON &&
1156 svc->framedrop_mode != LAYER_DROP) {
1157 if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1158 // On non-key frames: LAST is always temporal reference, GOLDEN is
1159 // spatial reference.
1160 if (svc->temporal_layer_id == 0)
1161 // Base temporal only predicts from base temporal.
1162 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] == 0);
1163 else
1164 // Non-base temporal only predicts from lower temporal layer.
1165 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] <
1166 svc->temporal_layer_id);
1167 if (svc->spatial_layer_id > 0 && cpi->ref_frame_flags & VP9_GOLD_FLAG &&
1168 svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1169 // Non-base spatial only predicts from lower spatial layer with same
1170 // temporal_id.
1171 assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1172 svc->spatial_layer_id - 1);
1173 assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1174 svc->temporal_layer_id);
1175 }
1176 } else if (svc->spatial_layer_id > 0 &&
1177 svc->spatial_layer_id > svc->first_spatial_layer_to_encode) {
1178 // Only 1 reference for frame whose base is key; reference may be LAST
1179 // or GOLDEN, so we check both.
1180 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
1181 assert(svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] ==
1182 svc->spatial_layer_id - 1);
1183 assert(svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] ==
1184 svc->temporal_layer_id);
1185 } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
1186 assert(svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] ==
1187 svc->spatial_layer_id - 1);
1188 assert(svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] ==
1189 svc->temporal_layer_id);
1190 }
1191 }
1192 } else if (svc->use_gf_temporal_ref_current_layer &&
1193 !svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1194 // For the usage of golden as second long term reference: the
1195 // temporal_layer_id of that reference must be base temporal layer 0, and
1196 // spatial_layer_id of that reference must be same as current
1197 // spatial_layer_id. If not, disable feature.
1198 // TODO(marpan): Investigate when this can happen, and maybe put this check
1199 // and reset in a different place.
1200 if (svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] !=
1201 svc->spatial_layer_id ||
1202 svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] != 0)
1203 svc->use_gf_temporal_ref_current_layer = 0;
1204 }
1205 }
1206
1207 #if CONFIG_VP9_TEMPORAL_DENOISING
vp9_denoise_svc_non_key(VP9_COMP * const cpi)1208 int vp9_denoise_svc_non_key(VP9_COMP *const cpi) {
1209 int layer =
1210 LAYER_IDS_TO_IDX(cpi->svc.spatial_layer_id, cpi->svc.temporal_layer_id,
1211 cpi->svc.number_temporal_layers);
1212 LAYER_CONTEXT *lc = &cpi->svc.layer_context[layer];
1213 return denoise_svc(cpi) && !lc->is_key_frame;
1214 }
1215 #endif
1216
vp9_svc_check_spatial_layer_sync(VP9_COMP * const cpi)1217 void vp9_svc_check_spatial_layer_sync(VP9_COMP *const cpi) {
1218 SVC *const svc = &cpi->svc;
1219 // Only for superframes whose base is not key, as those are
1220 // already sync frames.
1221 if (!svc->layer_context[svc->temporal_layer_id].is_key_frame) {
1222 if (svc->spatial_layer_id == 0) {
1223 // On base spatial layer: if the current superframe has a layer sync then
1224 // reset the pattern counters and reset to base temporal layer.
1225 if (svc->superframe_has_layer_sync)
1226 vp9_svc_reset_temporal_layers(cpi, cpi->common.frame_type == KEY_FRAME);
1227 }
1228 // If the layer sync is set for this current spatial layer then
1229 // disable the temporal reference.
1230 if (svc->spatial_layer_id > 0 &&
1231 svc->spatial_layer_sync[svc->spatial_layer_id]) {
1232 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
1233 if (svc->use_gf_temporal_ref_current_layer) {
1234 int index = svc->spatial_layer_id;
1235 // If golden is used as second reference: need to remove it from
1236 // prediction, reset refresh period to 0, and update the reference.
1237 svc->use_gf_temporal_ref_current_layer = 0;
1238 cpi->rc.baseline_gf_interval = 0;
1239 cpi->rc.frames_till_gf_update_due = 0;
1240 // On layer sync frame we must update the buffer index used for long
1241 // term reference. Use the alt_ref since it is not used or updated on
1242 // sync frames.
1243 if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
1244 assert(index >= 0);
1245 cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
1246 cpi->ext_refresh_alt_ref_frame = 1;
1247 }
1248 }
1249 }
1250 }
1251
vp9_svc_update_ref_frame_buffer_idx(VP9_COMP * const cpi)1252 void vp9_svc_update_ref_frame_buffer_idx(VP9_COMP *const cpi) {
1253 SVC *const svc = &cpi->svc;
1254 int i = 0;
1255 // Update the usage of frame buffer index for base spatial layers.
1256 if (svc->spatial_layer_id == 0) {
1257 if ((cpi->ref_frame_flags & VP9_LAST_FLAG) || cpi->refresh_last_frame)
1258 svc->fb_idx_base[cpi->lst_fb_idx] = 1;
1259 if ((cpi->ref_frame_flags & VP9_GOLD_FLAG) || cpi->refresh_golden_frame)
1260 svc->fb_idx_base[cpi->gld_fb_idx] = 1;
1261 if ((cpi->ref_frame_flags & VP9_ALT_FLAG) || cpi->refresh_alt_ref_frame)
1262 svc->fb_idx_base[cpi->alt_fb_idx] = 1;
1263 // For bypass/flexible mode: check for refresh slots.
1264 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
1265 for (i = 0; i < REF_FRAMES; ++i)
1266 if (svc->update_buffer_slot[0] & (1 << i)) svc->fb_idx_base[i] = 1;
1267 }
1268 }
1269 }
1270
vp9_svc_update_ref_frame_bypass_mode(VP9_COMP * const cpi)1271 static void vp9_svc_update_ref_frame_bypass_mode(VP9_COMP *const cpi) {
1272 // For non-flexible/bypass SVC mode: check for refreshing other buffer
1273 // slots.
1274 SVC *const svc = &cpi->svc;
1275 VP9_COMMON *const cm = &cpi->common;
1276 BufferPool *const pool = cm->buffer_pool;
1277 int i;
1278 for (i = 0; i < REF_FRAMES; i++) {
1279 if ((cm->frame_type == KEY_FRAME && !svc->simulcast_mode) ||
1280 svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
1281 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1282 svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1283 svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1284 }
1285 }
1286 }
1287
vp9_svc_update_ref_frame(VP9_COMP * const cpi)1288 void vp9_svc_update_ref_frame(VP9_COMP *const cpi) {
1289 VP9_COMMON *const cm = &cpi->common;
1290 SVC *const svc = &cpi->svc;
1291 BufferPool *const pool = cm->buffer_pool;
1292
1293 if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
1294 svc->use_set_ref_frame_config) {
1295 vp9_svc_update_ref_frame_bypass_mode(cpi);
1296 } else if (cm->frame_type == KEY_FRAME && !svc->simulcast_mode) {
1297 // Keep track of frame index for each reference frame.
1298 int i;
1299 // On key frame update all reference frame slots.
1300 for (i = 0; i < REF_FRAMES; i++) {
1301 svc->fb_idx_spatial_layer_id[i] = svc->spatial_layer_id;
1302 svc->fb_idx_temporal_layer_id[i] = svc->temporal_layer_id;
1303 // LAST/GOLDEN/ALTREF is already updated above.
1304 if (i != cpi->lst_fb_idx && i != cpi->gld_fb_idx && i != cpi->alt_fb_idx)
1305 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[i], cm->new_fb_idx);
1306 }
1307 } else {
1308 if (cpi->refresh_last_frame) {
1309 svc->fb_idx_spatial_layer_id[cpi->lst_fb_idx] = svc->spatial_layer_id;
1310 svc->fb_idx_temporal_layer_id[cpi->lst_fb_idx] = svc->temporal_layer_id;
1311 }
1312 if (cpi->refresh_golden_frame) {
1313 svc->fb_idx_spatial_layer_id[cpi->gld_fb_idx] = svc->spatial_layer_id;
1314 svc->fb_idx_temporal_layer_id[cpi->gld_fb_idx] = svc->temporal_layer_id;
1315 }
1316 if (cpi->refresh_alt_ref_frame) {
1317 svc->fb_idx_spatial_layer_id[cpi->alt_fb_idx] = svc->spatial_layer_id;
1318 svc->fb_idx_temporal_layer_id[cpi->alt_fb_idx] = svc->temporal_layer_id;
1319 }
1320 }
1321 // Copy flags from encoder to SVC struct.
1322 vp9_copy_flags_ref_update_idx(cpi);
1323 vp9_svc_update_ref_frame_buffer_idx(cpi);
1324 }
1325
vp9_svc_adjust_frame_rate(VP9_COMP * const cpi)1326 void vp9_svc_adjust_frame_rate(VP9_COMP *const cpi) {
1327 int64_t this_duration =
1328 cpi->svc.timebase_fac * cpi->svc.duration[cpi->svc.spatial_layer_id];
1329 vp9_new_framerate(cpi, 10000000.0 / this_duration);
1330 }
1331
vp9_svc_adjust_avg_frame_qindex(VP9_COMP * const cpi)1332 void vp9_svc_adjust_avg_frame_qindex(VP9_COMP *const cpi) {
1333 VP9_COMMON *const cm = &cpi->common;
1334 SVC *const svc = &cpi->svc;
1335 RATE_CONTROL *const rc = &cpi->rc;
1336 // On key frames in CBR mode: reset the avg_frame_qindex for base layer
1337 // (to level closer to worst_quality) if the overshoot is significant.
1338 // Reset it for all temporal layers on base spatial layer.
1339 if (cm->frame_type == KEY_FRAME && cpi->oxcf.rc_mode == VPX_CBR &&
1340 !svc->simulcast_mode &&
1341 rc->projected_frame_size / 3 > rc->avg_frame_bandwidth) {
1342 int tl;
1343 rc->avg_frame_qindex[INTER_FRAME] =
1344 VPXMAX(rc->avg_frame_qindex[INTER_FRAME],
1345 (cm->base_qindex + rc->worst_quality) >> 1);
1346 for (tl = 0; tl < svc->number_temporal_layers; ++tl) {
1347 const int layer = LAYER_IDS_TO_IDX(0, tl, svc->number_temporal_layers);
1348 LAYER_CONTEXT *lc = &svc->layer_context[layer];
1349 RATE_CONTROL *lrc = &lc->rc;
1350 lrc->avg_frame_qindex[INTER_FRAME] = rc->avg_frame_qindex[INTER_FRAME];
1351 }
1352 }
1353 }
1354
1355 // SVC: skip encoding of enhancement layer if the layer target bandwidth = 0.
1356 // No need to set svc.skip_enhancement_layer if whole superframe will be
1357 // dropped.
vp9_svc_check_skip_enhancement_layer(VP9_COMP * const cpi)1358 int vp9_svc_check_skip_enhancement_layer(VP9_COMP *const cpi) {
1359 if (cpi->use_svc && cpi->svc.spatial_layer_id > 0 &&
1360 cpi->oxcf.target_bandwidth == 0 &&
1361 !(cpi->svc.framedrop_mode != LAYER_DROP &&
1362 (cpi->svc.framedrop_mode != CONSTRAINED_FROM_ABOVE_DROP ||
1363 cpi->svc
1364 .force_drop_constrained_from_above[cpi->svc.number_spatial_layers -
1365 1]) &&
1366 cpi->svc.drop_spatial_layer[0])) {
1367 cpi->svc.skip_enhancement_layer = 1;
1368 vp9_rc_postencode_update_drop_frame(cpi);
1369 cpi->ext_refresh_frame_flags_pending = 0;
1370 cpi->last_frame_dropped = 1;
1371 cpi->svc.last_layer_dropped[cpi->svc.spatial_layer_id] = 1;
1372 cpi->svc.drop_spatial_layer[cpi->svc.spatial_layer_id] = 1;
1373 vp9_inc_frame_in_layer(cpi);
1374 return 1;
1375 }
1376 return 0;
1377 }
1378