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