| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 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 #include <limits.h> | 12 #include <limits.h> |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 #include "vp9/common/vp9_systemdependent.h" |
| 14 #include "vp9/encoder/vp9_block.h" | 15 #include "vp9/encoder/vp9_block.h" |
| 16 #include "vp9/encoder/vp9_encodeframe.h" |
| 17 #include "vp9/encoder/vp9_encodemb.h" |
| 18 #include "vp9/encoder/vp9_extend.h" |
| 19 #include "vp9/encoder/vp9_firstpass.h" |
| 20 #include "vp9/encoder/vp9_mcomp.h" |
| 15 #include "vp9/encoder/vp9_onyx_int.h" | 21 #include "vp9/encoder/vp9_onyx_int.h" |
| 16 #include "vp9/encoder/vp9_variance.h" | 22 #include "vp9/encoder/vp9_variance.h" |
| 17 #include "vp9/encoder/vp9_encodeintra.h" | |
| 18 #include "vp9/encoder/vp9_mcomp.h" | |
| 19 #include "vp9/encoder/vp9_firstpass.h" | |
| 20 #include "vpx_scale/vpx_scale.h" | 23 #include "vpx_scale/vpx_scale.h" |
| 21 #include "vp9/encoder/vp9_encodeframe.h" | |
| 22 #include "vp9/encoder/vp9_encodemb.h" | |
| 23 #include "vp9/common/vp9_extend.h" | |
| 24 #include "vp9/common/vp9_systemdependent.h" | |
| 25 #include "vpx_mem/vpx_mem.h" | 24 #include "vpx_mem/vpx_mem.h" |
| 26 #include "vpx_scale/yv12config.h" | 25 #include "vpx_scale/yv12config.h" |
| 27 #include "vp9/encoder/vp9_quantize.h" | 26 #include "vp9/encoder/vp9_quantize.h" |
| 28 #include "vp9/encoder/vp9_rdopt.h" | 27 #include "vp9/encoder/vp9_rdopt.h" |
| 29 #include "vp9/encoder/vp9_ratectrl.h" | 28 #include "vp9/encoder/vp9_ratectrl.h" |
| 30 #include "vp9/common/vp9_quant_common.h" | 29 #include "vp9/common/vp9_quant_common.h" |
| 31 #include "vp9/common/vp9_entropymv.h" | 30 #include "vp9/common/vp9_entropymv.h" |
| 32 #include "vp9/encoder/vp9_encodemv.h" | 31 #include "vp9/encoder/vp9_encodemv.h" |
| 33 #include "vp9/encoder/vp9_vaq.h" | 32 #include "vp9/encoder/vp9_vaq.h" |
| 34 #include "./vpx_scale_rtcd.h" | 33 #include "./vpx_scale_rtcd.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 for (i = 0; i < QINDEX_RANGE; i++) { | 69 for (i = 0; i < QINDEX_RANGE; i++) { |
| 71 if (target_q <= vp9_convert_qindex_to_q(i)) { | 70 if (target_q <= vp9_convert_qindex_to_q(i)) { |
| 72 ret_val = i; | 71 ret_val = i; |
| 73 break; | 72 break; |
| 74 } | 73 } |
| 75 } | 74 } |
| 76 | 75 |
| 77 return ret_val; | 76 return ret_val; |
| 78 } | 77 } |
| 79 | 78 |
| 79 static int gfboost_qadjust(int qindex) { |
| 80 const double q = vp9_convert_qindex_to_q(qindex); |
| 81 return (int)((0.00000828 * q * q * q) + |
| 82 (-0.0055 * q * q) + |
| 83 (1.32 * q) + 79.3); |
| 84 } |
| 85 |
| 86 static int kfboost_qadjust(int qindex) { |
| 87 const double q = vp9_convert_qindex_to_q(qindex); |
| 88 return (int)((0.00000973 * q * q * q) + |
| 89 (-0.00613 * q * q) + |
| 90 (1.316 * q) + 121.2); |
| 91 } |
| 80 | 92 |
| 81 // Resets the first pass file to the given position using a relative seek from | 93 // Resets the first pass file to the given position using a relative seek from |
| 82 // the current position. | 94 // the current position. |
| 83 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) { | 95 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) { |
| 84 cpi->twopass.stats_in = position; | 96 cpi->twopass.stats_in = position; |
| 85 } | 97 } |
| 86 | 98 |
| 87 static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) { | 99 static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) { |
| 88 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) | 100 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) |
| 89 return EOF; | 101 return EOF; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 342 |
| 331 // This function returns the current per frame maximum bitrate target. | 343 // This function returns the current per frame maximum bitrate target. |
| 332 static int frame_max_bits(VP9_COMP *cpi) { | 344 static int frame_max_bits(VP9_COMP *cpi) { |
| 333 // Max allocation for a single frame based on the max section guidelines | 345 // Max allocation for a single frame based on the max section guidelines |
| 334 // passed in and how many bits are left. | 346 // passed in and how many bits are left. |
| 335 // For VBR base this on the bits and frames left plus the | 347 // For VBR base this on the bits and frames left plus the |
| 336 // two_pass_vbrmax_section rate passed in by the user. | 348 // two_pass_vbrmax_section rate passed in by the user. |
| 337 const double max_bits = (1.0 * cpi->twopass.bits_left / | 349 const double max_bits = (1.0 * cpi->twopass.bits_left / |
| 338 (cpi->twopass.total_stats.count - cpi->common.current_video_frame)) * | 350 (cpi->twopass.total_stats.count - cpi->common.current_video_frame)) * |
| 339 (cpi->oxcf.two_pass_vbrmax_section / 100.0); | 351 (cpi->oxcf.two_pass_vbrmax_section / 100.0); |
| 340 | 352 if (max_bits < 0) |
| 341 // Trap case where we are out of bits. | 353 return 0; |
| 342 return MAX((int)max_bits, 0); | 354 if (max_bits >= INT_MAX) |
| 355 return INT_MAX; |
| 356 return (int)max_bits; |
| 343 } | 357 } |
| 344 | 358 |
| 345 void vp9_init_first_pass(VP9_COMP *cpi) { | 359 void vp9_init_first_pass(VP9_COMP *cpi) { |
| 346 zero_stats(&cpi->twopass.total_stats); | 360 zero_stats(&cpi->twopass.total_stats); |
| 347 } | 361 } |
| 348 | 362 |
| 349 void vp9_end_first_pass(VP9_COMP *cpi) { | 363 void vp9_end_first_pass(VP9_COMP *cpi) { |
| 350 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); | 364 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); |
| 351 } | 365 } |
| 352 | 366 |
| 353 static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, | 367 static unsigned int zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
| 354 YV12_BUFFER_CONFIG *recon_buffer, | 368 YV12_BUFFER_CONFIG *recon_buffer, |
| 355 int *best_motion_err, int recon_yoffset) { | 369 int recon_yoffset) { |
| 356 MACROBLOCKD *const xd = &x->e_mbd; | 370 MACROBLOCKD *const xd = &x->e_mbd; |
| 371 const uint8_t *const src = x->plane[0].src.buf; |
| 372 const int src_stride = x->plane[0].src.stride; |
| 373 const uint8_t *const ref = xd->plane[0].pre[0].buf |
| 374 = recon_buffer->y_buffer + recon_yoffset; |
| 375 const int ref_stride = xd->plane[0].pre[0].stride; |
| 357 | 376 |
| 358 // Set up pointers for this macro block recon buffer | 377 unsigned int sse; |
| 359 xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset; | |
| 360 | |
| 361 switch (xd->mi_8x8[0]->mbmi.sb_type) { | 378 switch (xd->mi_8x8[0]->mbmi.sb_type) { |
| 362 case BLOCK_8X8: | 379 case BLOCK_8X8: |
| 363 vp9_mse8x8(x->plane[0].src.buf, x->plane[0].src.stride, | 380 vp9_mse8x8(src, src_stride, ref, ref_stride, &sse); |
| 364 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | |
| 365 (unsigned int *)(best_motion_err)); | |
| 366 break; | 381 break; |
| 367 case BLOCK_16X8: | 382 case BLOCK_16X8: |
| 368 vp9_mse16x8(x->plane[0].src.buf, x->plane[0].src.stride, | 383 vp9_mse16x8(src, src_stride, ref, ref_stride, &sse); |
| 369 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | |
| 370 (unsigned int *)(best_motion_err)); | |
| 371 break; | 384 break; |
| 372 case BLOCK_8X16: | 385 case BLOCK_8X16: |
| 373 vp9_mse8x16(x->plane[0].src.buf, x->plane[0].src.stride, | 386 vp9_mse8x16(src, src_stride, ref, ref_stride, &sse); |
| 374 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | |
| 375 (unsigned int *)(best_motion_err)); | |
| 376 break; | 387 break; |
| 377 default: | 388 default: |
| 378 vp9_mse16x16(x->plane[0].src.buf, x->plane[0].src.stride, | 389 vp9_mse16x16(src, src_stride, ref, ref_stride, &sse); |
| 379 xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride, | |
| 380 (unsigned int *)(best_motion_err)); | |
| 381 break; | 390 break; |
| 382 } | 391 } |
| 392 return sse; |
| 383 } | 393 } |
| 384 | 394 |
| 385 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, | 395 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
| 386 int_mv *ref_mv, MV *best_mv, | 396 int_mv *ref_mv, MV *best_mv, |
| 387 YV12_BUFFER_CONFIG *recon_buffer, | 397 YV12_BUFFER_CONFIG *recon_buffer, |
| 388 int *best_motion_err, int recon_yoffset) { | 398 int *best_motion_err, int recon_yoffset) { |
| 389 MACROBLOCKD *const xd = &x->e_mbd; | 399 MACROBLOCKD *const xd = &x->e_mbd; |
| 390 int num00; | 400 int num00; |
| 391 | 401 |
| 392 int_mv tmp_mv; | 402 int_mv tmp_mv; |
| 393 int_mv ref_mv_full; | 403 int_mv ref_mv_full; |
| 394 | 404 |
| 395 int tmp_err; | 405 int tmp_err; |
| 396 int step_param = 3; | 406 int step_param = 3; |
| 397 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; | 407 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
| 398 int n; | 408 int n; |
| 399 vp9_variance_fn_ptr_t v_fn_ptr = | 409 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[xd->mi_8x8[0]->mbmi.sb_type]; |
| 400 cpi->fn_ptr[xd->mi_8x8[0]->mbmi.sb_type]; | |
| 401 int new_mv_mode_penalty = 256; | 410 int new_mv_mode_penalty = 256; |
| 402 | 411 |
| 403 int sr = 0; | 412 int sr = 0; |
| 404 int quart_frm = MIN(cpi->common.width, cpi->common.height); | 413 int quart_frm = MIN(cpi->common.width, cpi->common.height); |
| 405 | 414 |
| 406 // refine the motion search range accroding to the frame dimension | 415 // refine the motion search range accroding to the frame dimension |
| 407 // for first pass test | 416 // for first pass test |
| 408 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) | 417 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) |
| 409 sr++; | 418 sr++; |
| 410 if (sr) | |
| 411 sr--; | |
| 412 | 419 |
| 413 step_param += sr; | 420 step_param += sr; |
| 414 further_steps -= sr; | 421 further_steps -= sr; |
| 415 | 422 |
| 416 // override the default variance function to use MSE | 423 // override the default variance function to use MSE |
| 417 switch (xd->mi_8x8[0]->mbmi.sb_type) { | 424 switch (xd->mi_8x8[0]->mbmi.sb_type) { |
| 418 case BLOCK_8X8: | 425 case BLOCK_8X8: |
| 419 v_fn_ptr.vf = vp9_mse8x8; | 426 v_fn_ptr.vf = vp9_mse8x8; |
| 420 break; | 427 break; |
| 421 case BLOCK_16X8: | 428 case BLOCK_16X8: |
| 422 v_fn_ptr.vf = vp9_mse16x8; | 429 v_fn_ptr.vf = vp9_mse16x8; |
| 423 break; | 430 break; |
| 424 case BLOCK_8X16: | 431 case BLOCK_8X16: |
| 425 v_fn_ptr.vf = vp9_mse8x16; | 432 v_fn_ptr.vf = vp9_mse8x16; |
| 426 break; | 433 break; |
| 427 default: | 434 default: |
| 428 v_fn_ptr.vf = vp9_mse16x16; | 435 v_fn_ptr.vf = vp9_mse16x16; |
| 429 break; | 436 break; |
| 430 } | 437 } |
| 431 | 438 |
| 432 // Set up pointers for this macro block recon buffer | 439 // Set up pointers for this macro block recon buffer |
| 433 xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset; | 440 xd->plane[0].pre[0].buf = recon_buffer->y_buffer + recon_yoffset; |
| 434 | 441 |
| 435 // Initial step/diamond search centred on best mv | 442 // Initial step/diamond search centred on best mv |
| 436 tmp_mv.as_int = 0; | 443 tmp_mv.as_int = 0; |
| 437 ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3; | 444 ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3; |
| 438 ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3; | 445 ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3; |
| 439 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, step_param, | 446 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full.as_mv, &tmp_mv.as_mv, |
| 447 step_param, |
| 440 x->sadperbit16, &num00, &v_fn_ptr, | 448 x->sadperbit16, &num00, &v_fn_ptr, |
| 441 x->nmvjointcost, | 449 x->nmvjointcost, |
| 442 x->mvcost, ref_mv); | 450 x->mvcost, &ref_mv->as_mv); |
| 443 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 451 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 444 tmp_err += new_mv_mode_penalty; | 452 tmp_err += new_mv_mode_penalty; |
| 445 | 453 |
| 446 if (tmp_err < *best_motion_err) { | 454 if (tmp_err < *best_motion_err) { |
| 447 *best_motion_err = tmp_err; | 455 *best_motion_err = tmp_err; |
| 448 best_mv->row = tmp_mv.as_mv.row; | 456 best_mv->row = tmp_mv.as_mv.row; |
| 449 best_mv->col = tmp_mv.as_mv.col; | 457 best_mv->col = tmp_mv.as_mv.col; |
| 450 } | 458 } |
| 451 | 459 |
| 452 // Further step/diamond searches as necessary | 460 // Further step/diamond searches as necessary |
| 453 n = num00; | 461 n = num00; |
| 454 num00 = 0; | 462 num00 = 0; |
| 455 | 463 |
| 456 while (n < further_steps) { | 464 while (n < further_steps) { |
| 457 n++; | 465 n++; |
| 458 | 466 |
| 459 if (num00) { | 467 if (num00) { |
| 460 num00--; | 468 num00--; |
| 461 } else { | 469 } else { |
| 462 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 470 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full.as_mv, &tmp_mv.as_mv, |
| 463 step_param + n, x->sadperbit16, | 471 step_param + n, x->sadperbit16, |
| 464 &num00, &v_fn_ptr, | 472 &num00, &v_fn_ptr, |
| 465 x->nmvjointcost, | 473 x->nmvjointcost, |
| 466 x->mvcost, ref_mv); | 474 x->mvcost, &ref_mv->as_mv); |
| 467 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 475 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 468 tmp_err += new_mv_mode_penalty; | 476 tmp_err += new_mv_mode_penalty; |
| 469 | 477 |
| 470 if (tmp_err < *best_motion_err) { | 478 if (tmp_err < *best_motion_err) { |
| 471 *best_motion_err = tmp_err; | 479 *best_motion_err = tmp_err; |
| 472 best_mv->row = tmp_mv.as_mv.row; | 480 best_mv->row = tmp_mv.as_mv.row; |
| 473 best_mv->col = tmp_mv.as_mv.col; | 481 best_mv->col = tmp_mv.as_mv.col; |
| 474 } | 482 } |
| 475 } | 483 } |
| 476 } | 484 } |
| 477 } | 485 } |
| 478 | 486 |
| 479 void vp9_first_pass(VP9_COMP *cpi) { | 487 void vp9_first_pass(VP9_COMP *cpi) { |
| 480 int mb_row, mb_col; | 488 int mb_row, mb_col; |
| 481 MACROBLOCK *const x = &cpi->mb; | 489 MACROBLOCK *const x = &cpi->mb; |
| 482 VP9_COMMON *const cm = &cpi->common; | 490 VP9_COMMON *const cm = &cpi->common; |
| 483 MACROBLOCKD *const xd = &x->e_mbd; | 491 MACROBLOCKD *const xd = &x->e_mbd; |
| 484 TileInfo tile; | 492 TileInfo tile; |
| 493 struct macroblock_plane *const p = x->plane; |
| 494 struct macroblockd_plane *const pd = xd->plane; |
| 495 PICK_MODE_CONTEXT *ctx = &x->sb64_context; |
| 496 int i; |
| 485 | 497 |
| 486 int recon_yoffset, recon_uvoffset; | 498 int recon_yoffset, recon_uvoffset; |
| 487 const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx]; | 499 const int lst_yv12_idx = cm->ref_frame_map[cpi->lst_fb_idx]; |
| 488 const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx]; | 500 const int gld_yv12_idx = cm->ref_frame_map[cpi->gld_fb_idx]; |
| 489 YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx]; | 501 YV12_BUFFER_CONFIG *const lst_yv12 = &cm->yv12_fb[lst_yv12_idx]; |
| 490 YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx]; | 502 YV12_BUFFER_CONFIG *const gld_yv12 = &cm->yv12_fb[gld_yv12_idx]; |
| 491 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); | 503 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
| 492 const int recon_y_stride = lst_yv12->y_stride; | 504 const int recon_y_stride = lst_yv12->y_stride; |
| 493 const int recon_uv_stride = lst_yv12->uv_stride; | 505 const int recon_uv_stride = lst_yv12->uv_stride; |
| 494 int64_t intra_error = 0; | 506 int64_t intra_error = 0; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 518 setup_dst_planes(xd, new_yv12, 0, 0); | 530 setup_dst_planes(xd, new_yv12, 0, 0); |
| 519 | 531 |
| 520 xd->mi_8x8 = cm->mi_grid_visible; | 532 xd->mi_8x8 = cm->mi_grid_visible; |
| 521 // required for vp9_frame_init_quantizer | 533 // required for vp9_frame_init_quantizer |
| 522 xd->mi_8x8[0] = cm->mi; | 534 xd->mi_8x8[0] = cm->mi; |
| 523 | 535 |
| 524 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); | 536 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
| 525 | 537 |
| 526 vp9_frame_init_quantizer(cpi); | 538 vp9_frame_init_quantizer(cpi); |
| 527 | 539 |
| 540 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 541 p[i].coeff = ctx->coeff_pbuf[i][1]; |
| 542 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; |
| 543 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; |
| 544 p[i].eobs = ctx->eobs_pbuf[i][1]; |
| 545 } |
| 546 x->skip_recode = 0; |
| 547 |
| 548 |
| 528 // Initialise the MV cost table to the defaults | 549 // Initialise the MV cost table to the defaults |
| 529 // if( cm->current_video_frame == 0) | 550 // if( cm->current_video_frame == 0) |
| 530 // if ( 0 ) | 551 // if ( 0 ) |
| 531 { | 552 { |
| 532 vp9_init_mv_probs(cm); | 553 vp9_init_mv_probs(cm); |
| 533 vp9_initialize_rd_consts(cpi); | 554 vp9_initialize_rd_consts(cpi); |
| 534 } | 555 } |
| 535 | 556 |
| 536 // tiling is ignored in the first pass | 557 // tiling is ignored in the first pass |
| 537 vp9_tile_init(&tile, cm, 0, 0); | 558 vp9_tile_init(&tile, cm, 0, 0); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 551 // outside the UMV borders | 572 // outside the UMV borders |
| 552 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); | 573 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
| 553 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) | 574 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
| 554 + BORDER_MV_PIXELS_B16; | 575 + BORDER_MV_PIXELS_B16; |
| 555 | 576 |
| 556 // for each macroblock col in image | 577 // for each macroblock col in image |
| 557 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { | 578 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { |
| 558 int this_error; | 579 int this_error; |
| 559 int gf_motion_error = INT_MAX; | 580 int gf_motion_error = INT_MAX; |
| 560 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 581 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 561 double error_weight; | 582 double error_weight = 1.0; |
| 562 | 583 |
| 563 vp9_clear_system_state(); // __asm emms; | 584 vp9_clear_system_state(); // __asm emms; |
| 564 error_weight = 1.0; // avoid uninitialized warnings | |
| 565 | 585 |
| 566 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 586 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 567 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 587 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 568 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 588 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 569 xd->left_available = (mb_col != 0); | 589 xd->left_available = (mb_col != 0); |
| 570 | 590 |
| 571 if (mb_col * 2 + 1 < cm->mi_cols) { | 591 if (mb_col * 2 + 1 < cm->mi_cols) { |
| 572 if (mb_row * 2 + 1 < cm->mi_rows) { | 592 if (mb_row * 2 + 1 < cm->mi_rows) { |
| 573 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X16; | 593 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X16; |
| 574 } else { | 594 } else { |
| 575 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X8; | 595 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_16X8; |
| 576 } | 596 } |
| 577 } else { | 597 } else { |
| 578 if (mb_row * 2 + 1 < cm->mi_rows) { | 598 if (mb_row * 2 + 1 < cm->mi_rows) { |
| 579 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X16; | 599 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X16; |
| 580 } else { | 600 } else { |
| 581 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X8; | 601 xd->mi_8x8[0]->mbmi.sb_type = BLOCK_8X8; |
| 582 } | 602 } |
| 583 } | 603 } |
| 584 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; | 604 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 585 set_mi_row_col(xd, &tile, | 605 set_mi_row_col(xd, &tile, |
| 586 mb_row << 1, | 606 mb_row << 1, |
| 587 1 << mi_height_log2(xd->mi_8x8[0]->mbmi.sb_type), | 607 num_8x8_blocks_high_lookup[xd->mi_8x8[0]->mbmi.sb_type], |
| 588 mb_col << 1, | 608 mb_col << 1, |
| 589 1 << mi_width_log2(xd->mi_8x8[0]->mbmi.sb_type), | 609 num_8x8_blocks_wide_lookup[xd->mi_8x8[0]->mbmi.sb_type], |
| 590 cm->mi_rows, cm->mi_cols); | 610 cm->mi_rows, cm->mi_cols); |
| 591 | 611 |
| 592 if (cpi->sf.variance_adaptive_quantization) { | 612 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 593 int energy = vp9_block_energy(cpi, x, xd->mi_8x8[0]->mbmi.sb_type); | 613 int energy = vp9_block_energy(cpi, x, xd->mi_8x8[0]->mbmi.sb_type); |
| 594 error_weight = vp9_vaq_inv_q_ratio(energy); | 614 error_weight = vp9_vaq_inv_q_ratio(energy); |
| 595 } | 615 } |
| 596 | 616 |
| 597 // do intra 16x16 prediction | 617 // do intra 16x16 prediction |
| 598 this_error = vp9_encode_intra(x, use_dc_pred); | 618 this_error = vp9_encode_intra(x, use_dc_pred); |
| 599 if (cpi->sf.variance_adaptive_quantization) { | 619 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 600 vp9_clear_system_state(); // __asm emms; | 620 vp9_clear_system_state(); // __asm emms; |
| 601 this_error *= error_weight; | 621 this_error *= error_weight; |
| 602 } | 622 } |
| 603 | 623 |
| 604 // intrapenalty below deals with situations where the intra and inter | 624 // intrapenalty below deals with situations where the intra and inter |
| 605 // error scores are very low (eg a plain black frame). | 625 // error scores are very low (eg a plain black frame). |
| 606 // We do not have special cases in first pass for 0,0 and nearest etc so | 626 // We do not have special cases in first pass for 0,0 and nearest etc so |
| 607 // all inter modes carry an overhead cost estimate for the mv. | 627 // all inter modes carry an overhead cost estimate for the mv. |
| 608 // When the error score is very low this causes us to pick all or lots of | 628 // When the error score is very low this causes us to pick all or lots of |
| 609 // INTRA modes and throw lots of key frames. | 629 // INTRA modes and throw lots of key frames. |
| 610 // This penalty adds a cost matching that of a 0,0 mv to the intra case. | 630 // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 611 this_error += intrapenalty; | 631 this_error += intrapenalty; |
| 612 | 632 |
| 613 // Cumulative intra error total | 633 // Cumulative intra error total |
| 614 intra_error += (int64_t)this_error; | 634 intra_error += (int64_t)this_error; |
| 615 | 635 |
| 616 // Set up limit values for motion vectors to prevent them extending | 636 // Set up limit values for motion vectors to prevent them extending |
| 617 // outside the UMV borders. | 637 // outside the UMV borders. |
| 618 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 638 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 619 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) | 639 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) |
| 620 + BORDER_MV_PIXELS_B16; | 640 + BORDER_MV_PIXELS_B16; |
| 621 | 641 |
| 622 // Other than for the first frame do a motion search | 642 // Other than for the first frame do a motion search |
| 623 if (cm->current_video_frame > 0) { | 643 if (cm->current_video_frame > 0) { |
| 624 int tmp_err; | 644 int tmp_err; |
| 625 int motion_error = INT_MAX; | 645 int motion_error = zz_motion_search(cpi, x, lst_yv12, recon_yoffset); |
| 626 int_mv mv, tmp_mv; | 646 int_mv mv, tmp_mv; |
| 627 | |
| 628 // Simple 0,0 motion with no mv overhead | 647 // Simple 0,0 motion with no mv overhead |
| 629 zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset); | |
| 630 mv.as_int = tmp_mv.as_int = 0; | 648 mv.as_int = tmp_mv.as_int = 0; |
| 631 | 649 |
| 632 // Test last reference frame using the previous best mv as the | 650 // Test last reference frame using the previous best mv as the |
| 633 // starting point (best reference) for the search | 651 // starting point (best reference) for the search |
| 634 first_pass_motion_search(cpi, x, &best_ref_mv, | 652 first_pass_motion_search(cpi, x, &best_ref_mv, |
| 635 &mv.as_mv, lst_yv12, | 653 &mv.as_mv, lst_yv12, |
| 636 &motion_error, recon_yoffset); | 654 &motion_error, recon_yoffset); |
| 637 if (cpi->sf.variance_adaptive_quantization) { | 655 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 638 vp9_clear_system_state(); // __asm emms; | 656 vp9_clear_system_state(); // __asm emms; |
| 639 motion_error *= error_weight; | 657 motion_error *= error_weight; |
| 640 } | 658 } |
| 641 | 659 |
| 642 // If the current best reference mv is not centered on 0,0 then do a 0,0 | 660 // If the current best reference mv is not centered on 0,0 then do a 0,0 |
| 643 // based search as well. | 661 // based search as well. |
| 644 if (best_ref_mv.as_int) { | 662 if (best_ref_mv.as_int) { |
| 645 tmp_err = INT_MAX; | 663 tmp_err = INT_MAX; |
| 646 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, | 664 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv, |
| 647 lst_yv12, &tmp_err, recon_yoffset); | 665 lst_yv12, &tmp_err, recon_yoffset); |
| 648 if (cpi->sf.variance_adaptive_quantization) { | 666 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 649 vp9_clear_system_state(); // __asm emms; | 667 vp9_clear_system_state(); // __asm emms; |
| 650 tmp_err *= error_weight; | 668 tmp_err *= error_weight; |
| 651 } | 669 } |
| 652 | 670 |
| 653 if (tmp_err < motion_error) { | 671 if (tmp_err < motion_error) { |
| 654 motion_error = tmp_err; | 672 motion_error = tmp_err; |
| 655 mv.as_int = tmp_mv.as_int; | 673 mv.as_int = tmp_mv.as_int; |
| 656 } | 674 } |
| 657 } | 675 } |
| 658 | 676 |
| 659 // Experimental search in an older reference frame | 677 // Experimental search in an older reference frame |
| 660 if (cm->current_video_frame > 1) { | 678 if (cm->current_video_frame > 1) { |
| 661 // Simple 0,0 motion with no mv overhead | 679 // Simple 0,0 motion with no mv overhead |
| 662 zz_motion_search(cpi, x, gld_yv12, | 680 gf_motion_error = zz_motion_search(cpi, x, gld_yv12, recon_yoffset); |
| 663 &gf_motion_error, recon_yoffset); | |
| 664 | 681 |
| 665 first_pass_motion_search(cpi, x, &zero_ref_mv, | 682 first_pass_motion_search(cpi, x, &zero_ref_mv, |
| 666 &tmp_mv.as_mv, gld_yv12, | 683 &tmp_mv.as_mv, gld_yv12, |
| 667 &gf_motion_error, recon_yoffset); | 684 &gf_motion_error, recon_yoffset); |
| 668 if (cpi->sf.variance_adaptive_quantization) { | 685 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 669 vp9_clear_system_state(); // __asm emms; | 686 vp9_clear_system_state(); // __asm emms; |
| 670 gf_motion_error *= error_weight; | 687 gf_motion_error *= error_weight; |
| 671 } | 688 } |
| 672 | 689 |
| 673 if ((gf_motion_error < motion_error) && | 690 if ((gf_motion_error < motion_error) && |
| 674 (gf_motion_error < this_error)) { | 691 (gf_motion_error < this_error)) { |
| 675 second_ref_count++; | 692 second_ref_count++; |
| 676 } | 693 } |
| 677 | 694 |
| 678 // Reset to last frame as reference buffer | 695 // Reset to last frame as reference buffer |
| (...skipping 13 matching lines...) Expand all Loading... |
| 692 sr_coded_error += motion_error; | 709 sr_coded_error += motion_error; |
| 693 } | 710 } |
| 694 /* Intra assumed best */ | 711 /* Intra assumed best */ |
| 695 best_ref_mv.as_int = 0; | 712 best_ref_mv.as_int = 0; |
| 696 | 713 |
| 697 if (motion_error <= this_error) { | 714 if (motion_error <= this_error) { |
| 698 // Keep a count of cases where the inter and intra were | 715 // Keep a count of cases where the inter and intra were |
| 699 // very close and very low. This helps with scene cut | 716 // very close and very low. This helps with scene cut |
| 700 // detection for example in cropped clips with black bars | 717 // detection for example in cropped clips with black bars |
| 701 // at the sides or top and bottom. | 718 // at the sides or top and bottom. |
| 702 if ((((this_error - intrapenalty) * 9) <= | 719 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
| 703 (motion_error * 10)) && | 720 this_error < 2 * intrapenalty) |
| 704 (this_error < (2 * intrapenalty))) { | |
| 705 neutral_count++; | 721 neutral_count++; |
| 706 } | |
| 707 | 722 |
| 708 mv.as_mv.row *= 8; | 723 mv.as_mv.row *= 8; |
| 709 mv.as_mv.col *= 8; | 724 mv.as_mv.col *= 8; |
| 710 this_error = motion_error; | 725 this_error = motion_error; |
| 711 vp9_set_mbmode_and_mvs(x, NEWMV, &mv); | 726 vp9_set_mbmode_and_mvs(x, NEWMV, &mv); |
| 712 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; | 727 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; |
| 713 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; | 728 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; |
| 714 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; | 729 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; |
| 715 vp9_build_inter_predictors_sby(xd, mb_row << 1, | 730 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, |
| 716 mb_col << 1, | |
| 717 xd->mi_8x8[0]->mbmi.sb_type); | 731 xd->mi_8x8[0]->mbmi.sb_type); |
| 718 vp9_encode_sby(x, xd->mi_8x8[0]->mbmi.sb_type); | 732 vp9_encode_sby(x, xd->mi_8x8[0]->mbmi.sb_type); |
| 719 sum_mvr += mv.as_mv.row; | 733 sum_mvr += mv.as_mv.row; |
| 720 sum_mvr_abs += abs(mv.as_mv.row); | 734 sum_mvr_abs += abs(mv.as_mv.row); |
| 721 sum_mvc += mv.as_mv.col; | 735 sum_mvc += mv.as_mv.col; |
| 722 sum_mvc_abs += abs(mv.as_mv.col); | 736 sum_mvc_abs += abs(mv.as_mv.col); |
| 723 sum_mvrs += mv.as_mv.row * mv.as_mv.row; | 737 sum_mvrs += mv.as_mv.row * mv.as_mv.row; |
| 724 sum_mvcs += mv.as_mv.col * mv.as_mv.col; | 738 sum_mvcs += mv.as_mv.col * mv.as_mv.col; |
| 725 intercount++; | 739 intercount++; |
| 726 | 740 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 | 923 |
| 910 double zz_cost; | 924 double zz_cost; |
| 911 double motion_cost; | 925 double motion_cost; |
| 912 double intra_cost; | 926 double intra_cost; |
| 913 | 927 |
| 914 zz_cost = bitcost(av_pct_inter - av_pct_motion); | 928 zz_cost = bitcost(av_pct_inter - av_pct_motion); |
| 915 motion_cost = bitcost(av_pct_motion); | 929 motion_cost = bitcost(av_pct_motion); |
| 916 intra_cost = bitcost(av_intra); | 930 intra_cost = bitcost(av_intra); |
| 917 | 931 |
| 918 // Estimate of extra bits per mv overhead for mbs | 932 // Estimate of extra bits per mv overhead for mbs |
| 919 // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb | 933 // << 9 is the normalization to the (bits * 512) used in vp9_rc_bits_per_mb |
| 920 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; | 934 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; |
| 921 | 935 |
| 922 // Crude estimate of overhead cost from modes | 936 // Crude estimate of overhead cost from modes |
| 923 // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb | 937 // << 9 is the normalization to (bits * 512) used in vp9_rc_bits_per_mb |
| 924 mode_cost = | 938 mode_cost = |
| 925 (int)((((av_pct_inter - av_pct_motion) * zz_cost) + | 939 (int)((((av_pct_inter - av_pct_motion) * zz_cost) + |
| 926 (av_pct_motion * motion_cost) + | 940 (av_pct_motion * motion_cost) + |
| 927 (av_intra * intra_cost)) * cpi->common.MBs) << 9; | 941 (av_intra * intra_cost)) * cpi->common.MBs) << 9; |
| 928 | 942 |
| 929 // return mv_cost + mode_cost; | 943 // return mv_cost + mode_cost; |
| 930 // TODO(paulwilkins): Fix overhead costs for extended Q range. | 944 // TODO(paulwilkins): Fix overhead costs for extended Q range. |
| 931 #endif | 945 #endif |
| 932 return 0; | 946 return 0; |
| 933 } | 947 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 949 | 963 |
| 950 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 964 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 951 } | 965 } |
| 952 | 966 |
| 953 // Given a current maxQ value sets a range for future values. | 967 // Given a current maxQ value sets a range for future values. |
| 954 // PGW TODO.. | 968 // PGW TODO.. |
| 955 // This code removes direct dependency on QIndex to determine the range | 969 // This code removes direct dependency on QIndex to determine the range |
| 956 // (now uses the actual quantizer) but has not been tuned. | 970 // (now uses the actual quantizer) but has not been tuned. |
| 957 static void adjust_maxq_qrange(VP9_COMP *cpi) { | 971 static void adjust_maxq_qrange(VP9_COMP *cpi) { |
| 958 int i; | 972 int i; |
| 959 // Set the max corresponding to cpi->avg_q * 2.0 | 973 // Set the max corresponding to cpi->rc.avg_q * 2.0 |
| 960 double q = cpi->avg_q * 2.0; | 974 double q = cpi->rc.avg_q * 2.0; |
| 961 cpi->twopass.maxq_max_limit = cpi->worst_quality; | 975 cpi->twopass.maxq_max_limit = cpi->rc.worst_quality; |
| 962 for (i = cpi->best_quality; i <= cpi->worst_quality; i++) { | 976 for (i = cpi->rc.best_quality; i <= cpi->rc.worst_quality; i++) { |
| 963 cpi->twopass.maxq_max_limit = i; | 977 cpi->twopass.maxq_max_limit = i; |
| 964 if (vp9_convert_qindex_to_q(i) >= q) | 978 if (vp9_convert_qindex_to_q(i) >= q) |
| 965 break; | 979 break; |
| 966 } | 980 } |
| 967 | 981 |
| 968 // Set the min corresponding to cpi->avg_q * 0.5 | 982 // Set the min corresponding to cpi->rc.avg_q * 0.5 |
| 969 q = cpi->avg_q * 0.5; | 983 q = cpi->rc.avg_q * 0.5; |
| 970 cpi->twopass.maxq_min_limit = cpi->best_quality; | 984 cpi->twopass.maxq_min_limit = cpi->rc.best_quality; |
| 971 for (i = cpi->worst_quality; i >= cpi->best_quality; i--) { | 985 for (i = cpi->rc.worst_quality; i >= cpi->rc.best_quality; i--) { |
| 972 cpi->twopass.maxq_min_limit = i; | 986 cpi->twopass.maxq_min_limit = i; |
| 973 if (vp9_convert_qindex_to_q(i) <= q) | 987 if (vp9_convert_qindex_to_q(i) <= q) |
| 974 break; | 988 break; |
| 975 } | 989 } |
| 976 } | 990 } |
| 977 | 991 |
| 978 static int estimate_max_q(VP9_COMP *cpi, | 992 static int estimate_max_q(VP9_COMP *cpi, |
| 979 FIRSTPASS_STATS *fpstats, | 993 FIRSTPASS_STATS *fpstats, |
| 980 int section_target_bandwitdh) { | 994 int section_target_bandwitdh) { |
| 981 int q; | 995 int q; |
| 982 int num_mbs = cpi->common.MBs; | 996 int num_mbs = cpi->common.MBs; |
| 983 int target_norm_bits_per_mb; | 997 int target_norm_bits_per_mb; |
| 984 | 998 |
| 985 double section_err = fpstats->coded_error / fpstats->count; | 999 double section_err = fpstats->coded_error / fpstats->count; |
| 986 double sr_correction; | |
| 987 double err_per_mb = section_err / num_mbs; | 1000 double err_per_mb = section_err / num_mbs; |
| 988 double err_correction_factor; | 1001 double err_correction_factor; |
| 989 double speed_correction = 1.0; | |
| 990 | 1002 |
| 991 if (section_target_bandwitdh <= 0) | 1003 if (section_target_bandwitdh <= 0) |
| 992 return cpi->twopass.maxq_max_limit; // Highest value allowed | 1004 return cpi->twopass.maxq_max_limit; // Highest value allowed |
| 993 | 1005 |
| 994 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) | 1006 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) |
| 995 ? (512 * section_target_bandwitdh) / num_mbs | 1007 ? (512 * section_target_bandwitdh) / num_mbs |
| 996 : 512 * (section_target_bandwitdh / num_mbs); | 1008 : 512 * (section_target_bandwitdh / num_mbs); |
| 997 | 1009 |
| 998 // Look at the drop in prediction quality between the last frame | |
| 999 // and the GF buffer (which contained an older frame). | |
| 1000 if (fpstats->sr_coded_error > fpstats->coded_error) { | |
| 1001 double sr_err_diff = (fpstats->sr_coded_error - fpstats->coded_error) / | |
| 1002 (fpstats->count * cpi->common.MBs); | |
| 1003 sr_correction = fclamp(pow(sr_err_diff / 32.0, 0.25), 0.75, 1.25); | |
| 1004 } else { | |
| 1005 sr_correction = 0.75; | |
| 1006 } | |
| 1007 | |
| 1008 // Calculate a corrective factor based on a rolling ratio of bits spent | |
| 1009 // vs target bits | |
| 1010 if (cpi->rolling_target_bits > 0 && | |
| 1011 cpi->active_worst_quality < cpi->worst_quality) { | |
| 1012 double rolling_ratio = (double)cpi->rolling_actual_bits / | |
| 1013 (double)cpi->rolling_target_bits; | |
| 1014 | |
| 1015 if (rolling_ratio < 0.95) | |
| 1016 cpi->twopass.est_max_qcorrection_factor -= 0.005; | |
| 1017 else if (rolling_ratio > 1.05) | |
| 1018 cpi->twopass.est_max_qcorrection_factor += 0.005; | |
| 1019 | |
| 1020 cpi->twopass.est_max_qcorrection_factor = fclamp( | |
| 1021 cpi->twopass.est_max_qcorrection_factor, 0.1, 10.0); | |
| 1022 } | |
| 1023 | |
| 1024 // Corrections for higher compression speed settings | |
| 1025 // (reduced compression expected) | |
| 1026 // FIXME(jimbankoski): Once we settle on vp9 speed features we need to | |
| 1027 // change this code. | |
| 1028 if (cpi->compressor_speed == 1) | |
| 1029 speed_correction = cpi->oxcf.cpu_used <= 5 ? | |
| 1030 1.04 + (/*cpi->oxcf.cpu_used*/0 * 0.04) : | |
| 1031 1.25; | |
| 1032 | 1010 |
| 1033 // Try and pick a max Q that will be high enough to encode the | 1011 // Try and pick a max Q that will be high enough to encode the |
| 1034 // content at the given rate. | 1012 // content at the given rate. |
| 1035 for (q = cpi->twopass.maxq_min_limit; q < cpi->twopass.maxq_max_limit; q++) { | 1013 for (q = cpi->twopass.maxq_min_limit; q < cpi->twopass.maxq_max_limit; q++) { |
| 1036 int bits_per_mb_at_this_q; | 1014 int bits_per_mb_at_this_q; |
| 1037 | 1015 |
| 1038 err_correction_factor = calc_correction_factor(err_per_mb, | 1016 err_correction_factor = calc_correction_factor(err_per_mb, |
| 1039 ERR_DIVISOR, 0.4, 0.90, q) * | 1017 ERR_DIVISOR, 0.4, 0.90, q); |
| 1040 sr_correction * speed_correction * | |
| 1041 cpi->twopass.est_max_qcorrection_factor; | |
| 1042 | 1018 |
| 1043 bits_per_mb_at_this_q = vp9_bits_per_mb(INTER_FRAME, q, | 1019 bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 1044 err_correction_factor); | 1020 err_correction_factor); |
| 1045 | 1021 |
| 1046 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | 1022 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 1047 break; | 1023 break; |
| 1048 } | 1024 } |
| 1049 | 1025 |
| 1050 // Restriction on active max q for constrained quality mode. | 1026 // Restriction on active max q for constrained quality mode. |
| 1051 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && | 1027 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && |
| 1052 q < cpi->cq_target_quality) | 1028 q < cpi->cq_target_quality) |
| 1053 q = cpi->cq_target_quality; | 1029 q = cpi->cq_target_quality; |
| 1054 | 1030 |
| 1055 // Adjust maxq_min_limit and maxq_max_limit limits based on | |
| 1056 // average q observed in clip for non kf/gf/arf frames | |
| 1057 // Give average a chance to settle though. | |
| 1058 // PGW TODO.. This code is broken for the extended Q range | |
| 1059 if (cpi->ni_frames > ((int)cpi->twopass.total_stats.count >> 8) && | |
| 1060 cpi->ni_frames > 25) | |
| 1061 adjust_maxq_qrange(cpi); | |
| 1062 | |
| 1063 return q; | 1031 return q; |
| 1064 } | 1032 } |
| 1065 | 1033 |
| 1066 // For cq mode estimate a cq level that matches the observed | 1034 // For cq mode estimate a cq level that matches the observed |
| 1067 // complexity and data rate. | 1035 // complexity and data rate. |
| 1068 static int estimate_cq(VP9_COMP *cpi, | 1036 static int estimate_cq(VP9_COMP *cpi, |
| 1069 FIRSTPASS_STATS *fpstats, | 1037 FIRSTPASS_STATS *fpstats, |
| 1070 int section_target_bandwitdh) { | 1038 int section_target_bandwitdh) { |
| 1071 int q; | 1039 int q; |
| 1072 int num_mbs = cpi->common.MBs; | 1040 int num_mbs = cpi->common.MBs; |
| 1073 int target_norm_bits_per_mb; | 1041 int target_norm_bits_per_mb; |
| 1074 | 1042 |
| 1075 double section_err = (fpstats->coded_error / fpstats->count); | 1043 double section_err = (fpstats->coded_error / fpstats->count); |
| 1076 double err_per_mb = section_err / num_mbs; | 1044 double err_per_mb = section_err / num_mbs; |
| 1077 double err_correction_factor; | 1045 double err_correction_factor; |
| 1078 double sr_err_diff; | |
| 1079 double sr_correction; | |
| 1080 double speed_correction = 1.0; | |
| 1081 double clip_iiratio; | 1046 double clip_iiratio; |
| 1082 double clip_iifactor; | 1047 double clip_iifactor; |
| 1083 | 1048 |
| 1084 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) | 1049 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) |
| 1085 ? (512 * section_target_bandwitdh) / num_mbs | 1050 ? (512 * section_target_bandwitdh) / num_mbs |
| 1086 : 512 * (section_target_bandwitdh / num_mbs); | 1051 : 512 * (section_target_bandwitdh / num_mbs); |
| 1087 | 1052 |
| 1088 | 1053 |
| 1089 // Corrections for higher compression speed settings | |
| 1090 // (reduced compression expected) | |
| 1091 if (cpi->compressor_speed == 1) { | |
| 1092 if (cpi->oxcf.cpu_used <= 5) | |
| 1093 speed_correction = 1.04 + (/*cpi->oxcf.cpu_used*/ 0 * 0.04); | |
| 1094 else | |
| 1095 speed_correction = 1.25; | |
| 1096 } | |
| 1097 | |
| 1098 // Look at the drop in prediction quality between the last frame | |
| 1099 // and the GF buffer (which contained an older frame). | |
| 1100 if (fpstats->sr_coded_error > fpstats->coded_error) { | |
| 1101 sr_err_diff = | |
| 1102 (fpstats->sr_coded_error - fpstats->coded_error) / | |
| 1103 (fpstats->count * cpi->common.MBs); | |
| 1104 sr_correction = (sr_err_diff / 32.0); | |
| 1105 sr_correction = pow(sr_correction, 0.25); | |
| 1106 if (sr_correction < 0.75) | |
| 1107 sr_correction = 0.75; | |
| 1108 else if (sr_correction > 1.25) | |
| 1109 sr_correction = 1.25; | |
| 1110 } else { | |
| 1111 sr_correction = 0.75; | |
| 1112 } | |
| 1113 | |
| 1114 // II ratio correction factor for clip as a whole | 1054 // II ratio correction factor for clip as a whole |
| 1115 clip_iiratio = cpi->twopass.total_stats.intra_error / | 1055 clip_iiratio = cpi->twopass.total_stats.intra_error / |
| 1116 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error); | 1056 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error); |
| 1117 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); | 1057 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); |
| 1118 if (clip_iifactor < 0.80) | 1058 if (clip_iifactor < 0.80) |
| 1119 clip_iifactor = 0.80; | 1059 clip_iifactor = 0.80; |
| 1120 | 1060 |
| 1121 // Try and pick a Q that can encode the content at the given rate. | 1061 // Try and pick a Q that can encode the content at the given rate. |
| 1122 for (q = 0; q < MAXQ; q++) { | 1062 for (q = 0; q < MAXQ; q++) { |
| 1123 int bits_per_mb_at_this_q; | 1063 int bits_per_mb_at_this_q; |
| 1124 | 1064 |
| 1125 // Error per MB based correction factor | 1065 // Error per MB based correction factor |
| 1126 err_correction_factor = | 1066 err_correction_factor = |
| 1127 calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, q) * | 1067 calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, q) * clip_iifactor; |
| 1128 sr_correction * speed_correction * clip_iifactor; | |
| 1129 | 1068 |
| 1130 bits_per_mb_at_this_q = | 1069 bits_per_mb_at_this_q = |
| 1131 vp9_bits_per_mb(INTER_FRAME, q, err_correction_factor); | 1070 vp9_rc_bits_per_mb(INTER_FRAME, q, err_correction_factor); |
| 1132 | 1071 |
| 1133 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | 1072 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 1134 break; | 1073 break; |
| 1135 } | 1074 } |
| 1136 | 1075 |
| 1137 // Clip value to range "best allowed to (worst allowed - 1)" | 1076 // Clip value to range "best allowed to (worst allowed - 1)" |
| 1138 q = select_cq_level(q); | 1077 q = select_cq_level(q); |
| 1139 if (q >= cpi->worst_quality) | 1078 if (q >= cpi->rc.worst_quality) |
| 1140 q = cpi->worst_quality - 1; | 1079 q = cpi->rc.worst_quality - 1; |
| 1141 if (q < cpi->best_quality) | 1080 if (q < cpi->rc.best_quality) |
| 1142 q = cpi->best_quality; | 1081 q = cpi->rc.best_quality; |
| 1143 | 1082 |
| 1144 return q; | 1083 return q; |
| 1145 } | 1084 } |
| 1146 | 1085 |
| 1147 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 1086 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); |
| 1148 | 1087 |
| 1149 void vp9_init_second_pass(VP9_COMP *cpi) { | 1088 void vp9_init_second_pass(VP9_COMP *cpi) { |
| 1150 FIRSTPASS_STATS this_frame; | 1089 FIRSTPASS_STATS this_frame; |
| 1151 FIRSTPASS_STATS *start_pos; | 1090 FIRSTPASS_STATS *start_pos; |
| 1152 | 1091 |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1582 cpi->new_frame_coding_order_period = 0; | 1521 cpi->new_frame_coding_order_period = 0; |
| 1583 cpi->next_frame_in_order = 0; | 1522 cpi->next_frame_in_order = 0; |
| 1584 cpi->arf_buffered = 0; | 1523 cpi->arf_buffered = 0; |
| 1585 vp9_zero(cpi->frame_coding_order); | 1524 vp9_zero(cpi->frame_coding_order); |
| 1586 vp9_zero(cpi->arf_buffer_idx); | 1525 vp9_zero(cpi->arf_buffer_idx); |
| 1587 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); | 1526 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); |
| 1588 | 1527 |
| 1589 if (cpi->twopass.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) { | 1528 if (cpi->twopass.frames_to_key <= (FIXED_ARF_GROUP_SIZE + 8)) { |
| 1590 // Setup a GF group close to the keyframe. | 1529 // Setup a GF group close to the keyframe. |
| 1591 cpi->source_alt_ref_pending = 0; | 1530 cpi->source_alt_ref_pending = 0; |
| 1592 cpi->baseline_gf_interval = cpi->twopass.frames_to_key; | 1531 cpi->rc.baseline_gf_interval = cpi->twopass.frames_to_key; |
| 1593 schedule_frames(cpi, 0, (cpi->baseline_gf_interval - 1), 2, 0, 0); | 1532 schedule_frames(cpi, 0, (cpi->rc.baseline_gf_interval - 1), 2, 0, 0); |
| 1594 } else { | 1533 } else { |
| 1595 // Setup a fixed period ARF group. | 1534 // Setup a fixed period ARF group. |
| 1596 cpi->source_alt_ref_pending = 1; | 1535 cpi->source_alt_ref_pending = 1; |
| 1597 cpi->baseline_gf_interval = FIXED_ARF_GROUP_SIZE; | 1536 cpi->rc.baseline_gf_interval = FIXED_ARF_GROUP_SIZE; |
| 1598 schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0); | 1537 schedule_frames(cpi, 0, -(cpi->rc.baseline_gf_interval - 1), 2, 1, 0); |
| 1599 } | 1538 } |
| 1600 | 1539 |
| 1601 // Replace level indicator of -1 with correct level. | 1540 // Replace level indicator of -1 with correct level. |
| 1602 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1541 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1603 if (cpi->arf_weight[i] > max_level) { | 1542 if (cpi->arf_weight[i] > max_level) { |
| 1604 max_level = cpi->arf_weight[i]; | 1543 max_level = cpi->arf_weight[i]; |
| 1605 } | 1544 } |
| 1606 } | 1545 } |
| 1607 ++max_level; | 1546 ++max_level; |
| 1608 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1547 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1685 // Motion breakout threshold for loop below depends on image size. | 1624 // Motion breakout threshold for loop below depends on image size. |
| 1686 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0; | 1625 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0; |
| 1687 | 1626 |
| 1688 // Work out a maximum interval for the GF. | 1627 // Work out a maximum interval for the GF. |
| 1689 // If the image appears completely static we can extend beyond this. | 1628 // If the image appears completely static we can extend beyond this. |
| 1690 // The value chosen depends on the active Q range. At low Q we have | 1629 // The value chosen depends on the active Q range. At low Q we have |
| 1691 // bits to spare and are better with a smaller interval and smaller boost. | 1630 // bits to spare and are better with a smaller interval and smaller boost. |
| 1692 // At high Q when there are few bits to spare we are better with a longer | 1631 // At high Q when there are few bits to spare we are better with a longer |
| 1693 // interval to spread the cost of the GF. | 1632 // interval to spread the cost of the GF. |
| 1694 active_max_gf_interval = | 1633 active_max_gf_interval = |
| 1695 12 + ((int)vp9_convert_qindex_to_q(cpi->active_worst_quality) >> 5); | 1634 12 + ((int)vp9_convert_qindex_to_q(cpi->rc.active_worst_quality) >> 5); |
| 1696 | 1635 |
| 1697 if (active_max_gf_interval > cpi->max_gf_interval) | 1636 if (active_max_gf_interval > cpi->rc.max_gf_interval) |
| 1698 active_max_gf_interval = cpi->max_gf_interval; | 1637 active_max_gf_interval = cpi->rc.max_gf_interval; |
| 1699 | 1638 |
| 1700 i = 0; | 1639 i = 0; |
| 1701 while (((i < cpi->twopass.static_scene_max_gf_interval) || | 1640 while (((i < cpi->twopass.static_scene_max_gf_interval) || |
| 1702 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) && | 1641 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) && |
| 1703 (i < cpi->twopass.frames_to_key)) { | 1642 (i < cpi->twopass.frames_to_key)) { |
| 1704 i++; // Increment the loop counter | 1643 i++; // Increment the loop counter |
| 1705 | 1644 |
| 1706 // Accumulate error score of frames in this gf group | 1645 // Accumulate error score of frames in this gf group |
| 1707 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1646 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1708 gf_group_err += mod_frame_err; | 1647 gf_group_err += mod_frame_err; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1782 break; | 1721 break; |
| 1783 | 1722 |
| 1784 if (i < cpi->twopass.frames_to_key) { | 1723 if (i < cpi->twopass.frames_to_key) { |
| 1785 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1724 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1786 gf_group_err += mod_frame_err; | 1725 gf_group_err += mod_frame_err; |
| 1787 } | 1726 } |
| 1788 } | 1727 } |
| 1789 } | 1728 } |
| 1790 | 1729 |
| 1791 // Set the interval until the next gf or arf. | 1730 // Set the interval until the next gf or arf. |
| 1792 cpi->baseline_gf_interval = i; | 1731 cpi->rc.baseline_gf_interval = i; |
| 1793 | 1732 |
| 1794 #if CONFIG_MULTIPLE_ARF | 1733 #if CONFIG_MULTIPLE_ARF |
| 1795 if (cpi->multi_arf_enabled) { | 1734 if (cpi->multi_arf_enabled) { |
| 1796 // Initialize frame coding order variables. | 1735 // Initialize frame coding order variables. |
| 1797 cpi->new_frame_coding_order_period = 0; | 1736 cpi->new_frame_coding_order_period = 0; |
| 1798 cpi->next_frame_in_order = 0; | 1737 cpi->next_frame_in_order = 0; |
| 1799 cpi->arf_buffered = 0; | 1738 cpi->arf_buffered = 0; |
| 1800 vp9_zero(cpi->frame_coding_order); | 1739 vp9_zero(cpi->frame_coding_order); |
| 1801 vp9_zero(cpi->arf_buffer_idx); | 1740 vp9_zero(cpi->arf_buffer_idx); |
| 1802 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); | 1741 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); |
| 1803 } | 1742 } |
| 1804 #endif | 1743 #endif |
| 1805 | 1744 |
| 1806 // Should we use the alternate reference frame | 1745 // Should we use the alternate reference frame |
| 1807 if (allow_alt_ref && | 1746 if (allow_alt_ref && |
| 1808 (i < cpi->oxcf.lag_in_frames) && | 1747 (i < cpi->oxcf.lag_in_frames) && |
| 1809 (i >= MIN_GF_INTERVAL) && | 1748 (i >= MIN_GF_INTERVAL) && |
| 1810 // dont use ARF very near next kf | 1749 // dont use ARF very near next kf |
| 1811 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && | 1750 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) && |
| 1812 ((next_frame.pcnt_inter > 0.75) || | 1751 ((next_frame.pcnt_inter > 0.75) || |
| 1813 (next_frame.pcnt_second_ref > 0.5)) && | 1752 (next_frame.pcnt_second_ref > 0.5)) && |
| 1814 ((mv_in_out_accumulator / (double)i > -0.2) || | 1753 ((mv_in_out_accumulator / (double)i > -0.2) || |
| 1815 (mv_in_out_accumulator > -2.0)) && | 1754 (mv_in_out_accumulator > -2.0)) && |
| 1816 (boost_score > 100)) { | 1755 (boost_score > 100)) { |
| 1817 // Alternative boost calculation for alt ref | 1756 // Alternative boost calculation for alt ref |
| 1818 cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, | 1757 cpi->rc.gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
| 1819 &b_boost); | 1758 &b_boost); |
| 1820 cpi->source_alt_ref_pending = 1; | 1759 cpi->source_alt_ref_pending = 1; |
| 1821 | 1760 |
| 1822 #if CONFIG_MULTIPLE_ARF | 1761 #if CONFIG_MULTIPLE_ARF |
| 1823 // Set the ARF schedule. | 1762 // Set the ARF schedule. |
| 1824 if (cpi->multi_arf_enabled) { | 1763 if (cpi->multi_arf_enabled) { |
| 1825 schedule_frames(cpi, 0, -(cpi->baseline_gf_interval - 1), 2, 1, 0); | 1764 schedule_frames(cpi, 0, -(cpi->rc.baseline_gf_interval - 1), 2, 1, 0); |
| 1826 } | 1765 } |
| 1827 #endif | 1766 #endif |
| 1828 } else { | 1767 } else { |
| 1829 cpi->gfu_boost = (int)boost_score; | 1768 cpi->rc.gfu_boost = (int)boost_score; |
| 1830 cpi->source_alt_ref_pending = 0; | 1769 cpi->source_alt_ref_pending = 0; |
| 1831 #if CONFIG_MULTIPLE_ARF | 1770 #if CONFIG_MULTIPLE_ARF |
| 1832 // Set the GF schedule. | 1771 // Set the GF schedule. |
| 1833 if (cpi->multi_arf_enabled) { | 1772 if (cpi->multi_arf_enabled) { |
| 1834 schedule_frames(cpi, 0, cpi->baseline_gf_interval - 1, 2, 0, 0); | 1773 schedule_frames(cpi, 0, cpi->rc.baseline_gf_interval - 1, 2, 0, 0); |
| 1835 assert(cpi->new_frame_coding_order_period == cpi->baseline_gf_interval); | 1774 assert(cpi->new_frame_coding_order_period == |
| 1775 cpi->rc.baseline_gf_interval); |
| 1836 } | 1776 } |
| 1837 #endif | 1777 #endif |
| 1838 } | 1778 } |
| 1839 | 1779 |
| 1840 #if CONFIG_MULTIPLE_ARF | 1780 #if CONFIG_MULTIPLE_ARF |
| 1841 if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) { | 1781 if (cpi->multi_arf_enabled && (cpi->common.frame_type != KEY_FRAME)) { |
| 1842 int max_level = INT_MIN; | 1782 int max_level = INT_MIN; |
| 1843 // Replace level indicator of -1 with correct level. | 1783 // Replace level indicator of -1 with correct level. |
| 1844 for (i = 0; i < cpi->frame_coding_order_period; ++i) { | 1784 for (i = 0; i < cpi->frame_coding_order_period; ++i) { |
| 1845 if (cpi->arf_weight[i] > max_level) { | 1785 if (cpi->arf_weight[i] > max_level) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1898 } | 1838 } |
| 1899 cpi->twopass.gf_group_bits = | 1839 cpi->twopass.gf_group_bits = |
| 1900 (cpi->twopass.gf_group_bits < 0) | 1840 (cpi->twopass.gf_group_bits < 0) |
| 1901 ? 0 | 1841 ? 0 |
| 1902 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) | 1842 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) |
| 1903 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; | 1843 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; |
| 1904 | 1844 |
| 1905 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | 1845 // Clip cpi->twopass.gf_group_bits based on user supplied data rate |
| 1906 // variability limit (cpi->oxcf.two_pass_vbrmax_section) | 1846 // variability limit (cpi->oxcf.two_pass_vbrmax_section) |
| 1907 if (cpi->twopass.gf_group_bits > | 1847 if (cpi->twopass.gf_group_bits > |
| 1908 (int64_t)max_bits * cpi->baseline_gf_interval) | 1848 (int64_t)max_bits * cpi->rc.baseline_gf_interval) |
| 1909 cpi->twopass.gf_group_bits = (int64_t)max_bits * cpi->baseline_gf_interval; | 1849 cpi->twopass.gf_group_bits = |
| 1850 (int64_t)max_bits * cpi->rc.baseline_gf_interval; |
| 1910 | 1851 |
| 1911 // Reset the file position | 1852 // Reset the file position |
| 1912 reset_fpf_position(cpi, start_pos); | 1853 reset_fpf_position(cpi, start_pos); |
| 1913 | 1854 |
| 1914 // Update the record of error used so far (only done once per gf group) | 1855 // Update the record of error used so far (only done once per gf group) |
| 1915 cpi->twopass.modified_error_used += gf_group_err; | 1856 cpi->twopass.modified_error_used += gf_group_err; |
| 1916 | 1857 |
| 1917 // Assign bits to the arf or gf. | 1858 // Assign bits to the arf or gf. |
| 1918 for (i = 0; | 1859 for (i = 0; |
| 1919 i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); | 1860 i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); |
| 1920 ++i) { | 1861 ++i) { |
| 1921 int allocation_chunks; | 1862 int allocation_chunks; |
| 1922 int q = cpi->oxcf.fixed_q < 0 ? cpi->last_q[INTER_FRAME] | 1863 int q = cpi->rc.last_q[INTER_FRAME]; |
| 1923 : cpi->oxcf.fixed_q; | |
| 1924 int gf_bits; | 1864 int gf_bits; |
| 1925 | 1865 |
| 1926 int boost = (cpi->gfu_boost * vp9_gfboost_qadjust(q)) / 100; | 1866 int boost = (cpi->rc.gfu_boost * gfboost_qadjust(q)) / 100; |
| 1927 | 1867 |
| 1928 // Set max and minimum boost and hence minimum allocation | 1868 // Set max and minimum boost and hence minimum allocation |
| 1929 boost = clamp(boost, 125, (cpi->baseline_gf_interval + 1) * 200); | 1869 boost = clamp(boost, 125, (cpi->rc.baseline_gf_interval + 1) * 200); |
| 1930 | 1870 |
| 1931 if (cpi->source_alt_ref_pending && i == 0) | 1871 if (cpi->source_alt_ref_pending && i == 0) |
| 1932 allocation_chunks = ((cpi->baseline_gf_interval + 1) * 100) + boost; | 1872 allocation_chunks = ((cpi->rc.baseline_gf_interval + 1) * 100) + boost; |
| 1933 else | 1873 else |
| 1934 allocation_chunks = (cpi->baseline_gf_interval * 100) + (boost - 100); | 1874 allocation_chunks = (cpi->rc.baseline_gf_interval * 100) + (boost - 100); |
| 1935 | 1875 |
| 1936 // Prevent overflow | 1876 // Prevent overflow |
| 1937 if (boost > 1023) { | 1877 if (boost > 1023) { |
| 1938 int divisor = boost >> 10; | 1878 int divisor = boost >> 10; |
| 1939 boost /= divisor; | 1879 boost /= divisor; |
| 1940 allocation_chunks /= divisor; | 1880 allocation_chunks /= divisor; |
| 1941 } | 1881 } |
| 1942 | 1882 |
| 1943 // Calculate the number of bits to be spent on the gf or arf based on | 1883 // Calculate the number of bits to be spent on the gf or arf based on |
| 1944 // the boost number | 1884 // the boost number |
| 1945 gf_bits = (int)((double)boost * (cpi->twopass.gf_group_bits / | 1885 gf_bits = (int)((double)boost * (cpi->twopass.gf_group_bits / |
| 1946 (double)allocation_chunks)); | 1886 (double)allocation_chunks)); |
| 1947 | 1887 |
| 1948 // If the frame that is to be boosted is simpler than the average for | 1888 // If the frame that is to be boosted is simpler than the average for |
| 1949 // the gf/arf group then use an alternative calculation | 1889 // the gf/arf group then use an alternative calculation |
| 1950 // based on the error score of the frame itself | 1890 // based on the error score of the frame itself |
| 1951 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) { | 1891 if (mod_frame_err < gf_group_err / (double)cpi->rc.baseline_gf_interval) { |
| 1952 double alt_gf_grp_bits = | 1892 double alt_gf_grp_bits = |
| 1953 (double)cpi->twopass.kf_group_bits * | 1893 (double)cpi->twopass.kf_group_bits * |
| 1954 (mod_frame_err * (double)cpi->baseline_gf_interval) / | 1894 (mod_frame_err * (double)cpi->rc.baseline_gf_interval) / |
| 1955 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left); | 1895 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left); |
| 1956 | 1896 |
| 1957 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / | 1897 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / |
| 1958 (double)allocation_chunks)); | 1898 (double)allocation_chunks)); |
| 1959 | 1899 |
| 1960 if (gf_bits > alt_gf_bits) | 1900 if (gf_bits > alt_gf_bits) |
| 1961 gf_bits = alt_gf_bits; | 1901 gf_bits = alt_gf_bits; |
| 1962 } else { | 1902 } else { |
| 1963 // If it is harder than other frames in the group make sure it at | 1903 // If it is harder than other frames in the group make sure it at |
| 1964 // least receives an allocation in keeping with its relative error | 1904 // least receives an allocation in keeping with its relative error |
| 1965 // score, otherwise it may be worse off than an "un-boosted" frame. | 1905 // score, otherwise it may be worse off than an "un-boosted" frame. |
| 1966 int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits * | 1906 int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits * |
| 1967 mod_frame_err / | 1907 mod_frame_err / |
| 1968 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left)); | 1908 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left)); |
| 1969 | 1909 |
| 1970 if (alt_gf_bits > gf_bits) | 1910 if (alt_gf_bits > gf_bits) |
| 1971 gf_bits = alt_gf_bits; | 1911 gf_bits = alt_gf_bits; |
| 1972 } | 1912 } |
| 1973 | 1913 |
| 1974 // Dont allow a negative value for gf_bits | 1914 // Dont allow a negative value for gf_bits |
| 1975 if (gf_bits < 0) | 1915 if (gf_bits < 0) |
| 1976 gf_bits = 0; | 1916 gf_bits = 0; |
| 1977 | 1917 |
| 1978 // Add in minimum for a frame | 1918 // Add in minimum for a frame |
| 1979 gf_bits += cpi->min_frame_bandwidth; | 1919 gf_bits += cpi->rc.min_frame_bandwidth; |
| 1980 | 1920 |
| 1981 if (i == 0) { | 1921 if (i == 0) { |
| 1982 cpi->twopass.gf_bits = gf_bits; | 1922 cpi->twopass.gf_bits = gf_bits; |
| 1983 } | 1923 } |
| 1984 if (i == 1 || (!cpi->source_alt_ref_pending | 1924 if (i == 1 || (!cpi->source_alt_ref_pending |
| 1985 && (cpi->common.frame_type != KEY_FRAME))) { | 1925 && (cpi->common.frame_type != KEY_FRAME))) { |
| 1986 // Per frame bit target for this frame | 1926 // Per frame bit target for this frame |
| 1987 cpi->per_frame_bandwidth = gf_bits; | 1927 cpi->rc.per_frame_bandwidth = gf_bits; |
| 1988 } | 1928 } |
| 1989 } | 1929 } |
| 1990 | 1930 |
| 1991 { | 1931 { |
| 1992 // Adjust KF group bits and error remaining | 1932 // Adjust KF group bits and error remaining |
| 1993 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err; | 1933 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err; |
| 1994 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits; | 1934 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits; |
| 1995 | 1935 |
| 1996 if (cpi->twopass.kf_group_bits < 0) | 1936 if (cpi->twopass.kf_group_bits < 0) |
| 1997 cpi->twopass.kf_group_bits = 0; | 1937 cpi->twopass.kf_group_bits = 0; |
| 1998 | 1938 |
| 1999 // Note the error score left in the remaining frames of the group. | 1939 // Note the error score left in the remaining frames of the group. |
| 2000 // For normal GFs we want to remove the error score for the first frame | 1940 // For normal GFs we want to remove the error score for the first frame |
| 2001 // of the group (except in Key frame case where this has already | 1941 // of the group (except in Key frame case where this has already |
| 2002 // happened) | 1942 // happened) |
| 2003 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) | 1943 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) |
| 2004 cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err | 1944 cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err |
| 2005 - gf_first_frame_err); | 1945 - gf_first_frame_err); |
| 2006 else | 1946 else |
| 2007 cpi->twopass.gf_group_error_left = (int64_t)gf_group_err; | 1947 cpi->twopass.gf_group_error_left = (int64_t)gf_group_err; |
| 2008 | 1948 |
| 2009 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits | 1949 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits |
| 2010 - cpi->min_frame_bandwidth; | 1950 - cpi->rc.min_frame_bandwidth; |
| 2011 | 1951 |
| 2012 if (cpi->twopass.gf_group_bits < 0) | 1952 if (cpi->twopass.gf_group_bits < 0) |
| 2013 cpi->twopass.gf_group_bits = 0; | 1953 cpi->twopass.gf_group_bits = 0; |
| 2014 | 1954 |
| 2015 // This condition could fail if there are two kfs very close together | 1955 // This condition could fail if there are two kfs very close together |
| 2016 // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the | 1956 // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the |
| 2017 // calculation of alt_extra_bits. | 1957 // calculation of alt_extra_bits. |
| 2018 if (cpi->baseline_gf_interval >= 3) { | 1958 if (cpi->rc.baseline_gf_interval >= 3) { |
| 2019 const int boost = cpi->source_alt_ref_pending ? b_boost : cpi->gfu_boost; | 1959 const int boost = cpi->source_alt_ref_pending ? |
| 1960 b_boost : cpi->rc.gfu_boost; |
| 2020 | 1961 |
| 2021 if (boost >= 150) { | 1962 if (boost >= 150) { |
| 2022 int alt_extra_bits; | 1963 int alt_extra_bits; |
| 2023 int pct_extra = (boost - 100) / 50; | 1964 int pct_extra = (boost - 100) / 50; |
| 2024 pct_extra = (pct_extra > 20) ? 20 : pct_extra; | 1965 pct_extra = (pct_extra > 20) ? 20 : pct_extra; |
| 2025 | 1966 |
| 2026 alt_extra_bits = (int)((cpi->twopass.gf_group_bits * pct_extra) / 100); | 1967 alt_extra_bits = (int)((cpi->twopass.gf_group_bits * pct_extra) / 100); |
| 2027 cpi->twopass.gf_group_bits -= alt_extra_bits; | 1968 cpi->twopass.gf_group_bits -= alt_extra_bits; |
| 2028 } | 1969 } |
| 2029 } | 1970 } |
| 2030 } | 1971 } |
| 2031 | 1972 |
| 2032 if (cpi->common.frame_type != KEY_FRAME) { | 1973 if (cpi->common.frame_type != KEY_FRAME) { |
| 2033 FIRSTPASS_STATS sectionstats; | 1974 FIRSTPASS_STATS sectionstats; |
| 2034 | 1975 |
| 2035 zero_stats(§ionstats); | 1976 zero_stats(§ionstats); |
| 2036 reset_fpf_position(cpi, start_pos); | 1977 reset_fpf_position(cpi, start_pos); |
| 2037 | 1978 |
| 2038 for (i = 0; i < cpi->baseline_gf_interval; i++) { | 1979 for (i = 0; i < cpi->rc.baseline_gf_interval; i++) { |
| 2039 input_stats(cpi, &next_frame); | 1980 input_stats(cpi, &next_frame); |
| 2040 accumulate_stats(§ionstats, &next_frame); | 1981 accumulate_stats(§ionstats, &next_frame); |
| 2041 } | 1982 } |
| 2042 | 1983 |
| 2043 avg_stats(§ionstats); | 1984 avg_stats(§ionstats); |
| 2044 | 1985 |
| 2045 cpi->twopass.section_intra_rating = (int) | 1986 cpi->twopass.section_intra_rating = (int) |
| 2046 (sectionstats.intra_error / | 1987 (sectionstats.intra_error / |
| 2047 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 1988 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 2048 | 1989 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2085 } | 2026 } |
| 2086 | 2027 |
| 2087 // Adjust error and bits remaining. | 2028 // Adjust error and bits remaining. |
| 2088 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; | 2029 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; |
| 2089 cpi->twopass.gf_group_bits -= target_frame_size; | 2030 cpi->twopass.gf_group_bits -= target_frame_size; |
| 2090 | 2031 |
| 2091 if (cpi->twopass.gf_group_bits < 0) | 2032 if (cpi->twopass.gf_group_bits < 0) |
| 2092 cpi->twopass.gf_group_bits = 0; | 2033 cpi->twopass.gf_group_bits = 0; |
| 2093 | 2034 |
| 2094 // Add in the minimum number of bits that is set aside for every frame. | 2035 // Add in the minimum number of bits that is set aside for every frame. |
| 2095 target_frame_size += cpi->min_frame_bandwidth; | 2036 target_frame_size += cpi->rc.min_frame_bandwidth; |
| 2096 | 2037 |
| 2097 // Per frame bit target for this frame. | 2038 // Per frame bit target for this frame. |
| 2098 cpi->per_frame_bandwidth = target_frame_size; | 2039 cpi->rc.per_frame_bandwidth = target_frame_size; |
| 2099 } | 2040 } |
| 2100 | 2041 |
| 2101 // Make a damped adjustment to the active max q. | 2042 // Make a damped adjustment to the active max q. |
| 2102 static int adjust_active_maxq(int old_maxqi, int new_maxqi) { | 2043 static int adjust_active_maxq(int old_maxqi, int new_maxqi) { |
| 2103 int i; | 2044 int i; |
| 2104 const double old_q = vp9_convert_qindex_to_q(old_maxqi); | 2045 const double old_q = vp9_convert_qindex_to_q(old_maxqi); |
| 2105 const double new_q = vp9_convert_qindex_to_q(new_maxqi); | 2046 const double new_q = vp9_convert_qindex_to_q(new_maxqi); |
| 2106 const double target_q = ((old_q * 7.0) + new_q) / 8.0; | 2047 const double target_q = ((old_q * 7.0) + new_q) / 8.0; |
| 2107 | 2048 |
| 2108 if (target_q > old_q) { | 2049 if (target_q > old_q) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2128 | 2069 |
| 2129 double this_frame_intra_error; | 2070 double this_frame_intra_error; |
| 2130 double this_frame_coded_error; | 2071 double this_frame_coded_error; |
| 2131 | 2072 |
| 2132 if (!cpi->twopass.stats_in) | 2073 if (!cpi->twopass.stats_in) |
| 2133 return; | 2074 return; |
| 2134 | 2075 |
| 2135 vp9_clear_system_state(); | 2076 vp9_clear_system_state(); |
| 2136 | 2077 |
| 2137 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | 2078 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 2138 cpi->active_worst_quality = cpi->oxcf.cq_level; | 2079 cpi->rc.active_worst_quality = cpi->oxcf.cq_level; |
| 2139 } else { | 2080 } else if (cpi->common.current_video_frame == 0) { |
| 2140 // Special case code for first frame. | 2081 // Special case code for first frame. |
| 2141 if (cpi->common.current_video_frame == 0) { | 2082 int section_target_bandwidth = |
| 2142 int section_target_bandwidth = | 2083 (int)(cpi->twopass.bits_left / frames_left); |
| 2143 (int)(cpi->twopass.bits_left / frames_left); | |
| 2144 cpi->twopass.est_max_qcorrection_factor = 1.0; | |
| 2145 | 2084 |
| 2146 // Set a cq_level in constrained quality mode. | 2085 // guess at maxq needed in 2nd pass |
| 2147 // Commenting this code out for now since it does not seem to be | 2086 cpi->twopass.maxq_max_limit = cpi->rc.worst_quality; |
| 2148 // working well. | 2087 cpi->twopass.maxq_min_limit = cpi->rc.best_quality; |
| 2149 /* | |
| 2150 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { | |
| 2151 int est_cq = estimate_cq(cpi, &cpi->twopass.total_left_stats, | |
| 2152 section_target_bandwidth); | |
| 2153 | 2088 |
| 2154 if (est_cq > cpi->cq_target_quality) | 2089 tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats, |
| 2155 cpi->cq_target_quality = est_cq; | 2090 section_target_bandwidth); |
| 2156 else | |
| 2157 cpi->cq_target_quality = cpi->oxcf.cq_level; | |
| 2158 } | |
| 2159 */ | |
| 2160 | 2091 |
| 2161 // guess at maxq needed in 2nd pass | 2092 cpi->rc.active_worst_quality = tmp_q; |
| 2162 cpi->twopass.maxq_max_limit = cpi->worst_quality; | 2093 cpi->rc.ni_av_qi = tmp_q; |
| 2163 cpi->twopass.maxq_min_limit = cpi->best_quality; | 2094 cpi->rc.avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2164 | 2095 |
| 2165 tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats, | 2096 // Limit the maxq value returned subsequently. |
| 2166 section_target_bandwidth); | 2097 // This increases the risk of overspend or underspend if the initial |
| 2167 | 2098 // estimate for the clip is bad, but helps prevent excessive |
| 2168 cpi->active_worst_quality = tmp_q; | 2099 // variation in Q, especially near the end of a clip |
| 2169 cpi->ni_av_qi = tmp_q; | 2100 // where for example a small overspend may cause Q to crash |
| 2170 cpi->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2101 adjust_maxq_qrange(cpi); |
| 2171 | |
| 2172 #ifndef ONE_SHOT_Q_ESTIMATE | |
| 2173 // Limit the maxq value returned subsequently. | |
| 2174 // This increases the risk of overspend or underspend if the initial | |
| 2175 // estimate for the clip is bad, but helps prevent excessive | |
| 2176 // variation in Q, especially near the end of a clip | |
| 2177 // where for example a small overspend may cause Q to crash | |
| 2178 adjust_maxq_qrange(cpi); | |
| 2179 #endif | |
| 2180 } | |
| 2181 | |
| 2182 #ifndef ONE_SHOT_Q_ESTIMATE | |
| 2183 // The last few frames of a clip almost always have to few or too many | |
| 2184 // bits and for the sake of over exact rate control we dont want to make | |
| 2185 // radical adjustments to the allowed quantizer range just to use up a | |
| 2186 // few surplus bits or get beneath the target rate. | |
| 2187 else if ((cpi->common.current_video_frame < | |
| 2188 (((unsigned int)cpi->twopass.total_stats.count * 255) >> 8)) && | |
| 2189 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) < | |
| 2190 (unsigned int)cpi->twopass.total_stats.count)) { | |
| 2191 int section_target_bandwidth = | |
| 2192 (int)(cpi->twopass.bits_left / frames_left); | |
| 2193 if (frames_left < 1) | |
| 2194 frames_left = 1; | |
| 2195 | |
| 2196 tmp_q = estimate_max_q( | |
| 2197 cpi, | |
| 2198 &cpi->twopass.total_left_stats, | |
| 2199 section_target_bandwidth); | |
| 2200 | |
| 2201 // Make a damped adjustment to active max Q | |
| 2202 cpi->active_worst_quality = | |
| 2203 adjust_active_maxq(cpi->active_worst_quality, tmp_q); | |
| 2204 } | |
| 2205 #endif | |
| 2206 } | 2102 } |
| 2207 vp9_zero(this_frame); | 2103 vp9_zero(this_frame); |
| 2208 if (EOF == input_stats(cpi, &this_frame)) | 2104 if (EOF == input_stats(cpi, &this_frame)) |
| 2209 return; | 2105 return; |
| 2210 | 2106 |
| 2211 this_frame_intra_error = this_frame.intra_error; | 2107 this_frame_intra_error = this_frame.intra_error; |
| 2212 this_frame_coded_error = this_frame.coded_error; | 2108 this_frame_coded_error = this_frame.coded_error; |
| 2213 | 2109 |
| 2214 // keyframe and section processing ! | 2110 // keyframe and section processing ! |
| 2215 if (cpi->twopass.frames_to_key == 0) { | 2111 if (cpi->twopass.frames_to_key == 0) { |
| 2216 // Define next KF group and assign bits to it | 2112 // Define next KF group and assign bits to it |
| 2217 this_frame_copy = this_frame; | 2113 this_frame_copy = this_frame; |
| 2218 find_next_key_frame(cpi, &this_frame_copy); | 2114 find_next_key_frame(cpi, &this_frame_copy); |
| 2219 } | 2115 } |
| 2220 | 2116 |
| 2221 // Is this a GF / ARF (Note that a KF is always also a GF) | 2117 // Is this a GF / ARF (Note that a KF is always also a GF) |
| 2222 if (cpi->frames_till_gf_update_due == 0) { | 2118 if (cpi->rc.frames_till_gf_update_due == 0) { |
| 2223 // Define next gf group and assign bits to it | 2119 // Define next gf group and assign bits to it |
| 2224 this_frame_copy = this_frame; | 2120 this_frame_copy = this_frame; |
| 2225 | 2121 |
| 2226 cpi->gf_zeromotion_pct = 0; | 2122 cpi->gf_zeromotion_pct = 0; |
| 2227 | 2123 |
| 2228 #if CONFIG_MULTIPLE_ARF | 2124 #if CONFIG_MULTIPLE_ARF |
| 2229 if (cpi->multi_arf_enabled) { | 2125 if (cpi->multi_arf_enabled) { |
| 2230 define_fixed_arf_period(cpi); | 2126 define_fixed_arf_period(cpi); |
| 2231 } else { | 2127 } else { |
| 2232 #endif | 2128 #endif |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2246 | 2142 |
| 2247 // If we are going to code an altref frame at the end of the group | 2143 // If we are going to code an altref frame at the end of the group |
| 2248 // and the current frame is not a key frame.... | 2144 // and the current frame is not a key frame.... |
| 2249 // If the previous group used an arf this frame has already benefited | 2145 // If the previous group used an arf this frame has already benefited |
| 2250 // from that arf boost and it should not be given extra bits | 2146 // from that arf boost and it should not be given extra bits |
| 2251 // If the previous group was NOT coded using arf we may want to apply | 2147 // If the previous group was NOT coded using arf we may want to apply |
| 2252 // some boost to this GF as well | 2148 // some boost to this GF as well |
| 2253 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) { | 2149 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) { |
| 2254 // Assign a standard frames worth of bits from those allocated | 2150 // Assign a standard frames worth of bits from those allocated |
| 2255 // to the GF group | 2151 // to the GF group |
| 2256 int bak = cpi->per_frame_bandwidth; | 2152 int bak = cpi->rc.per_frame_bandwidth; |
| 2257 this_frame_copy = this_frame; | 2153 this_frame_copy = this_frame; |
| 2258 assign_std_frame_bits(cpi, &this_frame_copy); | 2154 assign_std_frame_bits(cpi, &this_frame_copy); |
| 2259 cpi->per_frame_bandwidth = bak; | 2155 cpi->rc.per_frame_bandwidth = bak; |
| 2260 } | 2156 } |
| 2261 } else { | 2157 } else { |
| 2262 // Otherwise this is an ordinary frame | 2158 // Otherwise this is an ordinary frame |
| 2263 // Assign bits from those allocated to the GF group | 2159 // Assign bits from those allocated to the GF group |
| 2264 this_frame_copy = this_frame; | 2160 this_frame_copy = this_frame; |
| 2265 assign_std_frame_bits(cpi, &this_frame_copy); | 2161 assign_std_frame_bits(cpi, &this_frame_copy); |
| 2266 } | 2162 } |
| 2267 | 2163 |
| 2268 // Keep a globally available copy of this and the next frame's iiratio. | 2164 // Keep a globally available copy of this and the next frame's iiratio. |
| 2269 cpi->twopass.this_iiratio = (int)(this_frame_intra_error / | 2165 cpi->twopass.this_iiratio = (int)(this_frame_intra_error / |
| 2270 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); | 2166 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); |
| 2271 { | 2167 { |
| 2272 FIRSTPASS_STATS next_frame; | 2168 FIRSTPASS_STATS next_frame; |
| 2273 if (lookup_next_frame_stats(cpi, &next_frame) != EOF) { | 2169 if (lookup_next_frame_stats(cpi, &next_frame) != EOF) { |
| 2274 cpi->twopass.next_iiratio = (int)(next_frame.intra_error / | 2170 cpi->twopass.next_iiratio = (int)(next_frame.intra_error / |
| 2275 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2171 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2276 } | 2172 } |
| 2277 } | 2173 } |
| 2278 | 2174 |
| 2279 // Set nominal per second bandwidth for this frame | 2175 // Set nominal per second bandwidth for this frame |
| 2280 cpi->target_bandwidth = (int)(cpi->per_frame_bandwidth | 2176 cpi->target_bandwidth = (int)(cpi->rc.per_frame_bandwidth |
| 2281 * cpi->output_framerate); | 2177 * cpi->output_framerate); |
| 2282 if (cpi->target_bandwidth < 0) | 2178 if (cpi->target_bandwidth < 0) |
| 2283 cpi->target_bandwidth = 0; | 2179 cpi->target_bandwidth = 0; |
| 2284 | 2180 |
| 2285 cpi->twopass.frames_to_key--; | 2181 cpi->twopass.frames_to_key--; |
| 2286 | 2182 |
| 2287 // Update the total stats remaining structure | 2183 // Update the total stats remaining structure |
| 2288 subtract_stats(&cpi->twopass.total_left_stats, &this_frame); | 2184 subtract_stats(&cpi->twopass.total_left_stats, &this_frame); |
| 2289 } | 2185 } |
| 2290 | 2186 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2403 | 2299 |
| 2404 cpi->common.frame_type = KEY_FRAME; | 2300 cpi->common.frame_type = KEY_FRAME; |
| 2405 | 2301 |
| 2406 // is this a forced key frame by interval | 2302 // is this a forced key frame by interval |
| 2407 cpi->this_key_frame_forced = cpi->next_key_frame_forced; | 2303 cpi->this_key_frame_forced = cpi->next_key_frame_forced; |
| 2408 | 2304 |
| 2409 // Clear the alt ref active flag as this can never be active on a key frame | 2305 // Clear the alt ref active flag as this can never be active on a key frame |
| 2410 cpi->source_alt_ref_active = 0; | 2306 cpi->source_alt_ref_active = 0; |
| 2411 | 2307 |
| 2412 // Kf is always a gf so clear frames till next gf counter | 2308 // Kf is always a gf so clear frames till next gf counter |
| 2413 cpi->frames_till_gf_update_due = 0; | 2309 cpi->rc.frames_till_gf_update_due = 0; |
| 2414 | 2310 |
| 2415 cpi->twopass.frames_to_key = 1; | 2311 cpi->twopass.frames_to_key = 1; |
| 2416 | 2312 |
| 2417 // Take a copy of the initial frame details | 2313 // Take a copy of the initial frame details |
| 2418 first_frame = *this_frame; | 2314 first_frame = *this_frame; |
| 2419 | 2315 |
| 2420 cpi->twopass.kf_group_bits = 0; // Total bits available to kf group | 2316 cpi->twopass.kf_group_bits = 0; // Total bits available to kf group |
| 2421 cpi->twopass.kf_group_error_left = 0; // Group modified error score. | 2317 cpi->twopass.kf_group_error_left = 0; // Group modified error score. |
| 2422 | 2318 |
| 2423 kf_mod_err = calculate_modified_err(cpi, this_frame); | 2319 kf_mod_err = calculate_modified_err(cpi, this_frame); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2566 break; | 2462 break; |
| 2567 | 2463 |
| 2568 // Monitor for static sections. | 2464 // Monitor for static sections. |
| 2569 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 2465 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 2570 zero_motion_accumulator) { | 2466 zero_motion_accumulator) { |
| 2571 zero_motion_accumulator = | 2467 zero_motion_accumulator = |
| 2572 (next_frame.pcnt_inter - next_frame.pcnt_motion); | 2468 (next_frame.pcnt_inter - next_frame.pcnt_motion); |
| 2573 } | 2469 } |
| 2574 | 2470 |
| 2575 // For the first few frames collect data to decide kf boost. | 2471 // For the first few frames collect data to decide kf boost. |
| 2576 if (i <= (cpi->max_gf_interval * 2)) { | 2472 if (i <= (cpi->rc.max_gf_interval * 2)) { |
| 2577 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) | 2473 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min) |
| 2578 r = (IIKFACTOR2 * next_frame.intra_error / | 2474 r = (IIKFACTOR2 * next_frame.intra_error / |
| 2579 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2475 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2580 else | 2476 else |
| 2581 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min / | 2477 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min / |
| 2582 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2478 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2583 | 2479 |
| 2584 if (r > RMAX) | 2480 if (r > RMAX) |
| 2585 r = RMAX; | 2481 r = RMAX; |
| 2586 | 2482 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2624 int alt_kf_bits; | 2520 int alt_kf_bits; |
| 2625 | 2521 |
| 2626 if (kf_boost < (cpi->twopass.frames_to_key * 3)) | 2522 if (kf_boost < (cpi->twopass.frames_to_key * 3)) |
| 2627 kf_boost = (cpi->twopass.frames_to_key * 3); | 2523 kf_boost = (cpi->twopass.frames_to_key * 3); |
| 2628 | 2524 |
| 2629 if (kf_boost < 300) // Min KF boost | 2525 if (kf_boost < 300) // Min KF boost |
| 2630 kf_boost = 300; | 2526 kf_boost = 300; |
| 2631 | 2527 |
| 2632 // Make a note of baseline boost and the zero motion | 2528 // Make a note of baseline boost and the zero motion |
| 2633 // accumulator value for use elsewhere. | 2529 // accumulator value for use elsewhere. |
| 2634 cpi->kf_boost = kf_boost; | 2530 cpi->rc.kf_boost = kf_boost; |
| 2635 cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | 2531 cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2636 | 2532 |
| 2637 // We do three calculations for kf size. | 2533 // We do three calculations for kf size. |
| 2638 // The first is based on the error score for the whole kf group. | 2534 // The first is based on the error score for the whole kf group. |
| 2639 // The second (optionaly) on the key frames own error if this is | 2535 // The second (optionaly) on the key frames own error if this is |
| 2640 // smaller than the average for the group. | 2536 // smaller than the average for the group. |
| 2641 // The final one insures that the frame receives at least the | 2537 // The final one insures that the frame receives at least the |
| 2642 // allocation it would have received based on its own error score vs | 2538 // allocation it would have received based on its own error score vs |
| 2643 // the error score remaining | 2539 // the error score remaining |
| 2644 // Special case if the sequence appears almost totaly static | 2540 // Special case if the sequence appears almost totaly static |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2694 (kf_mod_err / | 2590 (kf_mod_err / |
| 2695 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); | 2591 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left))); |
| 2696 | 2592 |
| 2697 if (alt_kf_bits > cpi->twopass.kf_bits) { | 2593 if (alt_kf_bits > cpi->twopass.kf_bits) { |
| 2698 cpi->twopass.kf_bits = alt_kf_bits; | 2594 cpi->twopass.kf_bits = alt_kf_bits; |
| 2699 } | 2595 } |
| 2700 } | 2596 } |
| 2701 | 2597 |
| 2702 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; | 2598 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits; |
| 2703 // Add in the minimum frame allowance | 2599 // Add in the minimum frame allowance |
| 2704 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; | 2600 cpi->twopass.kf_bits += cpi->rc.min_frame_bandwidth; |
| 2705 | 2601 |
| 2706 // Peer frame bit target for this frame | 2602 // Peer frame bit target for this frame |
| 2707 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; | 2603 cpi->rc.per_frame_bandwidth = cpi->twopass.kf_bits; |
| 2708 // Convert to a per second bitrate | 2604 // Convert to a per second bitrate |
| 2709 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * | 2605 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits * |
| 2710 cpi->output_framerate); | 2606 cpi->output_framerate); |
| 2711 } | 2607 } |
| 2712 | 2608 |
| 2713 // Note the total error score of the kf group minus the key frame itself | 2609 // Note the total error score of the kf group minus the key frame itself |
| 2714 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2610 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2715 | 2611 |
| 2716 // Adjust the count of total modified error left. | 2612 // Adjust the count of total modified error left. |
| 2717 // The count of bits left is adjusted elsewhere based on real coded frame | 2613 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2718 // sizes. | 2614 // sizes. |
| 2719 cpi->twopass.modified_error_left -= kf_group_err; | 2615 cpi->twopass.modified_error_left -= kf_group_err; |
| 2720 } | 2616 } |
| 2617 |
| 2618 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
| 2619 #ifdef DISABLE_RC_LONG_TERM_MEM |
| 2620 cpi->twopass.bits_left -= cpi->rc.this_frame_target; |
| 2621 #else |
| 2622 cpi->twopass.bits_left -= 8 * bytes_used; |
| 2623 #endif |
| 2624 if (!cpi->refresh_alt_ref_frame) { |
| 2625 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; |
| 2626 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * |
| 2627 cpi->oxcf.two_pass_vbrmin_section |
| 2628 / 100); |
| 2629 if (two_pass_min_rate < lower_bounds_min_rate) |
| 2630 two_pass_min_rate = lower_bounds_min_rate; |
| 2631 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / |
| 2632 cpi->oxcf.framerate); |
| 2633 } |
| 2634 } |
| OLD | NEW |