OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <math.h> | 11 #include <math.h> |
12 | 12 |
13 #include "vp9/encoder/vp9_encoder.h" | 13 #include "vp9/encoder/vp9_encoder.h" |
14 #include "vp9/encoder/vp9_svc_layercontext.h" | 14 #include "vp9/encoder/vp9_svc_layercontext.h" |
15 #include "vp9/encoder/vp9_extend.h" | 15 #include "vp9/encoder/vp9_extend.h" |
16 | 16 |
| 17 #define SMALL_FRAME_FB_IDX 7 |
| 18 |
17 void vp9_init_layer_context(VP9_COMP *const cpi) { | 19 void vp9_init_layer_context(VP9_COMP *const cpi) { |
18 SVC *const svc = &cpi->svc; | 20 SVC *const svc = &cpi->svc; |
19 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 21 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
20 int layer; | 22 int layer; |
21 int layer_end; | 23 int layer_end; |
22 int alt_ref_idx = svc->number_spatial_layers; | 24 int alt_ref_idx = svc->number_spatial_layers; |
23 | 25 |
24 svc->spatial_layer_id = 0; | 26 svc->spatial_layer_id = 0; |
25 svc->temporal_layer_id = 0; | 27 svc->temporal_layer_id = 0; |
26 | 28 |
27 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { | 29 if (svc->number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) { |
28 layer_end = svc->number_temporal_layers; | 30 layer_end = svc->number_temporal_layers; |
29 } else { | 31 } else { |
30 layer_end = svc->number_spatial_layers; | 32 layer_end = svc->number_spatial_layers; |
| 33 |
| 34 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2) { |
| 35 if (vp9_realloc_frame_buffer(&cpi->svc.empty_frame.img, |
| 36 cpi->common.width, cpi->common.height, |
| 37 cpi->common.subsampling_x, |
| 38 cpi->common.subsampling_y, |
| 39 #if CONFIG_VP9_HIGHBITDEPTH |
| 40 cpi->common.use_highbitdepth, |
| 41 #endif |
| 42 VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL)) |
| 43 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
| 44 "Failed to allocate empty frame for multiple frame " |
| 45 "contexts"); |
| 46 |
| 47 vpx_memset(cpi->svc.empty_frame.img.buffer_alloc, 0x80, |
| 48 cpi->svc.empty_frame.img.buffer_alloc_sz); |
| 49 cpi->svc.empty_frame_width = cpi->common.width; |
| 50 cpi->svc.empty_frame_height = cpi->common.height; |
| 51 } |
31 } | 52 } |
32 | 53 |
33 for (layer = 0; layer < layer_end; ++layer) { | 54 for (layer = 0; layer < layer_end; ++layer) { |
34 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; | 55 LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
35 RATE_CONTROL *const lrc = &lc->rc; | 56 RATE_CONTROL *const lrc = &lc->rc; |
36 int i; | 57 int i; |
37 lc->current_video_frame_in_layer = 0; | 58 lc->current_video_frame_in_layer = 0; |
38 lc->layer_size = 0; | 59 lc->layer_size = 0; |
39 lc->frames_from_key_frame = 0; | 60 lc->frames_from_key_frame = 0; |
40 lc->last_frame_type = FRAME_TYPES; | 61 lc->last_frame_type = FRAME_TYPES; |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; | 324 cpi->alt_fb_idx = cpi->svc.spatial_layer_id - 2; |
304 else | 325 else |
305 cpi->alt_fb_idx = cpi->lst_fb_idx; | 326 cpi->alt_fb_idx = cpi->lst_fb_idx; |
306 } | 327 } |
307 } | 328 } |
308 } | 329 } |
309 | 330 |
310 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, | 331 get_layer_resolution(cpi->oxcf.width, cpi->oxcf.height, |
311 lc->scaling_factor_num, lc->scaling_factor_den, | 332 lc->scaling_factor_num, lc->scaling_factor_den, |
312 &width, &height); | 333 &width, &height); |
| 334 |
| 335 // Workaround for multiple frame contexts. In some frames we can't use prev_mi |
| 336 // since its previous frame could be changed during decoding time. The idea is |
| 337 // we put a empty invisible frame in front of them, then we will not use |
| 338 // prev_mi when encoding these frames. |
| 339 if (cpi->oxcf.error_resilient_mode == 0 && cpi->oxcf.pass == 2 && |
| 340 cpi->svc.encode_empty_frame_state == NEED_TO_ENCODE) { |
| 341 if ((cpi->svc.number_temporal_layers > 1 && |
| 342 cpi->svc.temporal_layer_id < cpi->svc.number_temporal_layers - 1) || |
| 343 (cpi->svc.number_spatial_layers > 1 && |
| 344 cpi->svc.spatial_layer_id == 0)) { |
| 345 struct lookahead_entry *buf = vp9_lookahead_peek(cpi->lookahead, 0); |
| 346 |
| 347 if (buf != NULL) { |
| 348 cpi->svc.empty_frame.ts_start = buf->ts_start; |
| 349 cpi->svc.empty_frame.ts_end = buf->ts_end; |
| 350 cpi->svc.encode_empty_frame_state = ENCODING; |
| 351 cpi->common.show_frame = 0; |
| 352 cpi->ref_frame_flags = 0; |
| 353 cpi->common.frame_type = INTER_FRAME; |
| 354 cpi->lst_fb_idx = |
| 355 cpi->gld_fb_idx = cpi->alt_fb_idx = SMALL_FRAME_FB_IDX; |
| 356 |
| 357 // Gradually make the empty frame smaller to save bits. Make it half of |
| 358 // its previous size because of the scaling factor restriction. |
| 359 cpi->svc.empty_frame_width >>= 1; |
| 360 cpi->svc.empty_frame_width = (cpi->svc.empty_frame_width + 1) & ~1; |
| 361 if (cpi->svc.empty_frame_width < 16) |
| 362 cpi->svc.empty_frame_width = 16; |
| 363 |
| 364 cpi->svc.empty_frame_height >>= 1; |
| 365 cpi->svc.empty_frame_height = (cpi->svc.empty_frame_height + 1) & ~1; |
| 366 if (cpi->svc.empty_frame_height < 16) |
| 367 cpi->svc.empty_frame_height = 16; |
| 368 |
| 369 width = cpi->svc.empty_frame_width; |
| 370 height = cpi->svc.empty_frame_height; |
| 371 } |
| 372 } |
| 373 } |
| 374 |
313 if (vp9_set_size_literal(cpi, width, height) != 0) | 375 if (vp9_set_size_literal(cpi, width, height) != 0) |
314 return VPX_CODEC_INVALID_PARAM; | 376 return VPX_CODEC_INVALID_PARAM; |
315 | 377 |
316 cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q); | 378 cpi->oxcf.worst_allowed_q = vp9_quantizer_to_qindex(lc->max_q); |
317 cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q); | 379 cpi->oxcf.best_allowed_q = vp9_quantizer_to_qindex(lc->min_q); |
318 | 380 |
319 vp9_change_config(cpi, &cpi->oxcf); | 381 vp9_change_config(cpi, &cpi->oxcf); |
320 | |
321 vp9_set_high_precision_mv(cpi, 1); | 382 vp9_set_high_precision_mv(cpi, 1); |
322 | 383 |
323 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source; | 384 cpi->alt_ref_source = get_layer_context(cpi)->alt_ref_source; |
324 | 385 |
325 return 0; | 386 return 0; |
326 } | 387 } |
327 | 388 |
328 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi, | 389 struct lookahead_entry *vp9_svc_lookahead_pop(VP9_COMP *const cpi, |
329 struct lookahead_ctx *ctx, | 390 struct lookahead_ctx *ctx, |
330 int drain) { | 391 int drain) { |
331 struct lookahead_entry *buf = NULL; | 392 struct lookahead_entry *buf = NULL; |
332 | 393 |
333 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) { | 394 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) { |
334 buf = vp9_lookahead_peek(ctx, 0); | 395 buf = vp9_lookahead_peek(ctx, 0); |
335 if (buf != NULL) { | 396 if (buf != NULL) { |
336 // Only remove the buffer when pop the highest layer. | 397 // Only remove the buffer when pop the highest layer. |
337 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { | 398 if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
338 vp9_lookahead_pop(ctx, drain); | 399 vp9_lookahead_pop(ctx, drain); |
339 } | 400 } |
340 } | 401 } |
341 } | 402 } |
342 | 403 |
343 return buf; | 404 return buf; |
344 } | 405 } |
345 #endif | 406 #endif |
OLD | NEW |