| 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 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #define RMAX 512.0 | 42 #define RMAX 512.0 |
| 43 #define GF_RMAX 96.0 | 43 #define GF_RMAX 96.0 |
| 44 #define ERR_DIVISOR 150.0 | 44 #define ERR_DIVISOR 150.0 |
| 45 #define MIN_DECAY_FACTOR 0.1 | 45 #define MIN_DECAY_FACTOR 0.1 |
| 46 | 46 |
| 47 #define KF_MB_INTRA_MIN 150 | 47 #define KF_MB_INTRA_MIN 150 |
| 48 #define GF_MB_INTRA_MIN 100 | 48 #define GF_MB_INTRA_MIN 100 |
| 49 | 49 |
| 50 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 50 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 51 | 51 |
| 52 #define MIN_KF_BOOST 300 |
| 53 |
| 54 #define DISABLE_RC_LONG_TERM_MEM 0 |
| 55 |
| 52 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 56 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 53 YV12_BUFFER_CONFIG temp = *a; | 57 YV12_BUFFER_CONFIG temp = *a; |
| 54 *a = *b; | 58 *a = *b; |
| 55 *b = temp; | 59 *b = temp; |
| 56 } | 60 } |
| 57 | 61 |
| 58 static int select_cq_level(int qindex) { | 62 static int select_cq_level(int qindex) { |
| 59 int ret_val = QINDEX_RANGE - 1; | 63 int ret_val = QINDEX_RANGE - 1; |
| 60 int i; | 64 int i; |
| 61 | 65 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 126 |
| 123 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) { | 127 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) { |
| 124 if (p->stats_in >= p->stats_in_end) | 128 if (p->stats_in >= p->stats_in_end) |
| 125 return EOF; | 129 return EOF; |
| 126 | 130 |
| 127 *fps = *p->stats_in; | 131 *fps = *p->stats_in; |
| 128 ++p->stats_in; | 132 ++p->stats_in; |
| 129 return 1; | 133 return 1; |
| 130 } | 134 } |
| 131 | 135 |
| 132 static void output_stats(const VP9_COMP *cpi, | 136 static void output_stats(const VP9_COMP *cpi, |
| 133 struct vpx_codec_pkt_list *pktlist, | 137 struct vpx_codec_pkt_list *pktlist, |
| 134 FIRSTPASS_STATS *stats) { | 138 FIRSTPASS_STATS *stats) { |
| 135 struct vpx_codec_cx_pkt pkt; | 139 struct vpx_codec_cx_pkt pkt; |
| 136 pkt.kind = VPX_CODEC_STATS_PKT; | 140 pkt.kind = VPX_CODEC_STATS_PKT; |
| 137 pkt.data.twopass_stats.buf = stats; | 141 pkt.data.twopass_stats.buf = stats; |
| 138 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); | 142 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
| 139 vpx_codec_pkt_list_add(pktlist, &pkt); | 143 vpx_codec_pkt_list_add(pktlist, &pkt); |
| 140 | 144 |
| 141 // TEMP debug code | 145 // TEMP debug code |
| 142 #if OUTPUT_FPF | 146 #if OUTPUT_FPF |
| 143 | 147 |
| 144 { | 148 { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 section->MVc /= section->count; | 259 section->MVc /= section->count; |
| 256 section->mvc_abs /= section->count; | 260 section->mvc_abs /= section->count; |
| 257 section->MVrv /= section->count; | 261 section->MVrv /= section->count; |
| 258 section->MVcv /= section->count; | 262 section->MVcv /= section->count; |
| 259 section->mv_in_out_count /= section->count; | 263 section->mv_in_out_count /= section->count; |
| 260 section->duration /= section->count; | 264 section->duration /= section->count; |
| 261 } | 265 } |
| 262 | 266 |
| 263 // Calculate a modified Error used in distributing bits between easier and | 267 // Calculate a modified Error used in distributing bits between easier and |
| 264 // harder frames. | 268 // harder frames. |
| 265 static double calculate_modified_err(VP9_COMP *cpi, | 269 static double calculate_modified_err(const VP9_COMP *cpi, |
| 266 FIRSTPASS_STATS *this_frame) { | 270 const FIRSTPASS_STATS *this_frame) { |
| 267 struct twopass_rc *const twopass = &cpi->twopass; | 271 const struct twopass_rc *const twopass = &cpi->twopass; |
| 268 const FIRSTPASS_STATS *const stats = &twopass->total_stats; | 272 const FIRSTPASS_STATS *const stats = &twopass->total_stats; |
| 269 const double av_err = stats->ssim_weighted_pred_err / stats->count; | 273 const double av_err = stats->ssim_weighted_pred_err / stats->count; |
| 270 double modified_error = av_err * pow(this_frame->ssim_weighted_pred_err / | 274 double modified_error = av_err * pow(this_frame->ssim_weighted_pred_err / |
| 271 DOUBLE_DIVIDE_CHECK(av_err), | 275 DOUBLE_DIVIDE_CHECK(av_err), |
| 272 cpi->oxcf.two_pass_vbrbias / 100.0); | 276 cpi->oxcf.two_pass_vbrbias / 100.0); |
| 273 | 277 |
| 274 return fclamp(modified_error, | 278 return fclamp(modified_error, |
| 275 twopass->modified_error_min, twopass->modified_error_max); | 279 twopass->modified_error_min, twopass->modified_error_max); |
| 276 } | 280 } |
| 277 | 281 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 const uint8_t *pixel = row; | 330 const uint8_t *pixel = row; |
| 327 for (j = 0; j < w; ++j) | 331 for (j = 0; j < w; ++j) |
| 328 sum += weight_table[*pixel++]; | 332 sum += weight_table[*pixel++]; |
| 329 row += buf->y_stride; | 333 row += buf->y_stride; |
| 330 } | 334 } |
| 331 | 335 |
| 332 return MAX(0.1, sum / (w * h)); | 336 return MAX(0.1, sum / (w * h)); |
| 333 } | 337 } |
| 334 | 338 |
| 335 // This function returns the maximum target rate per frame. | 339 // This function returns the maximum target rate per frame. |
| 336 static int frame_max_bits(VP9_COMP *cpi) { | 340 static int frame_max_bits(const VP9_COMP *cpi) { |
| 337 int64_t max_bits = | 341 int64_t max_bits = |
| 338 ((int64_t)cpi->rc.av_per_frame_bandwidth * | 342 ((int64_t)cpi->rc.av_per_frame_bandwidth * |
| 339 (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100; | 343 (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100; |
| 340 | 344 |
| 341 if (max_bits < 0) | 345 if (max_bits < 0) |
| 342 max_bits = 0; | 346 max_bits = 0; |
| 343 else if (max_bits > cpi->rc.max_frame_bandwidth) | 347 else if (max_bits > cpi->rc.max_frame_bandwidth) |
| 344 max_bits = cpi->rc.max_frame_bandwidth; | 348 max_bits = cpi->rc.max_frame_bandwidth; |
| 345 | 349 |
| 346 return (int)max_bits; | 350 return (int)max_bits; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 } | 462 } |
| 459 | 463 |
| 460 void vp9_first_pass(VP9_COMP *cpi) { | 464 void vp9_first_pass(VP9_COMP *cpi) { |
| 461 int mb_row, mb_col; | 465 int mb_row, mb_col; |
| 462 MACROBLOCK *const x = &cpi->mb; | 466 MACROBLOCK *const x = &cpi->mb; |
| 463 VP9_COMMON *const cm = &cpi->common; | 467 VP9_COMMON *const cm = &cpi->common; |
| 464 MACROBLOCKD *const xd = &x->e_mbd; | 468 MACROBLOCKD *const xd = &x->e_mbd; |
| 465 TileInfo tile; | 469 TileInfo tile; |
| 466 struct macroblock_plane *const p = x->plane; | 470 struct macroblock_plane *const p = x->plane; |
| 467 struct macroblockd_plane *const pd = xd->plane; | 471 struct macroblockd_plane *const pd = xd->plane; |
| 468 PICK_MODE_CONTEXT *ctx = &x->sb64_context; | 472 const PICK_MODE_CONTEXT *ctx = &x->sb64_context; |
| 469 int i; | 473 int i; |
| 470 | 474 |
| 471 int recon_yoffset, recon_uvoffset; | 475 int recon_yoffset, recon_uvoffset; |
| 472 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); | 476 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 473 YV12_BUFFER_CONFIG *const gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); | 477 YV12_BUFFER_CONFIG *const gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 474 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); | 478 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
| 475 const int recon_y_stride = lst_yv12->y_stride; | 479 const int recon_y_stride = lst_yv12->y_stride; |
| 476 const int recon_uv_stride = lst_yv12->uv_stride; | 480 const int recon_uv_stride = lst_yv12->uv_stride; |
| 477 const int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); | 481 const int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); |
| 478 int64_t intra_error = 0; | 482 int64_t intra_error = 0; |
| 479 int64_t coded_error = 0; | 483 int64_t coded_error = 0; |
| 480 int64_t sr_coded_error = 0; | 484 int64_t sr_coded_error = 0; |
| 481 | 485 |
| 482 int sum_mvr = 0, sum_mvc = 0; | 486 int sum_mvr = 0, sum_mvc = 0; |
| 483 int sum_mvr_abs = 0, sum_mvc_abs = 0; | 487 int sum_mvr_abs = 0, sum_mvc_abs = 0; |
| 484 int64_t sum_mvrs = 0, sum_mvcs = 0; | 488 int64_t sum_mvrs = 0, sum_mvcs = 0; |
| 485 int mvcount = 0; | 489 int mvcount = 0; |
| 486 int intercount = 0; | 490 int intercount = 0; |
| 487 int second_ref_count = 0; | 491 int second_ref_count = 0; |
| 488 int intrapenalty = 256; | 492 int intrapenalty = 256; |
| 489 int neutral_count = 0; | 493 int neutral_count = 0; |
| 490 int new_mv_count = 0; | 494 int new_mv_count = 0; |
| 491 int sum_in_vectors = 0; | 495 int sum_in_vectors = 0; |
| 492 uint32_t lastmv_as_int = 0; | 496 uint32_t lastmv_as_int = 0; |
| 493 struct twopass_rc *const twopass = &cpi->twopass; | 497 struct twopass_rc *const twopass = &cpi->twopass; |
| 494 | 498 const MV zero_mv = {0, 0}; |
| 495 int_mv zero_ref_mv; | |
| 496 | |
| 497 zero_ref_mv.as_int = 0; | |
| 498 | 499 |
| 499 vp9_clear_system_state(); // __asm emms; | 500 vp9_clear_system_state(); // __asm emms; |
| 500 | 501 |
| 501 vp9_setup_src_planes(x, cpi->Source, 0, 0); | 502 vp9_setup_src_planes(x, cpi->Source, 0, 0); |
| 502 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); | 503 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); |
| 503 setup_dst_planes(xd, new_yv12, 0, 0); | 504 setup_dst_planes(xd, new_yv12, 0, 0); |
| 504 | 505 |
| 505 xd->mi_8x8 = cm->mi_grid_visible; | 506 xd->mi_8x8 = cm->mi_grid_visible; |
| 506 // required for vp9_frame_init_quantizer | 507 xd->mi_8x8[0] = cm->mi; // required for vp9_frame_init_quantizer |
| 507 xd->mi_8x8[0] = cm->mi; | |
| 508 | 508 |
| 509 setup_block_dptrs(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); | 509 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
| 510 | 510 |
| 511 vp9_frame_init_quantizer(cpi); | 511 vp9_frame_init_quantizer(cpi); |
| 512 | 512 |
| 513 for (i = 0; i < MAX_MB_PLANE; ++i) { | 513 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 514 p[i].coeff = ctx->coeff_pbuf[i][1]; | 514 p[i].coeff = ctx->coeff_pbuf[i][1]; |
| 515 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; | 515 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; |
| 516 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; | 516 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; |
| 517 p[i].eobs = ctx->eobs_pbuf[i][1]; | 517 p[i].eobs = ctx->eobs_pbuf[i][1]; |
| 518 } | 518 } |
| 519 x->skip_recode = 0; | 519 x->skip_recode = 0; |
| 520 | 520 |
| 521 | 521 vp9_init_mv_probs(cm); |
| 522 // Initialise the MV cost table to the defaults | 522 vp9_initialize_rd_consts(cpi); |
| 523 // if( cm->current_video_frame == 0) | |
| 524 // if ( 0 ) | |
| 525 { | |
| 526 vp9_init_mv_probs(cm); | |
| 527 vp9_initialize_rd_consts(cpi); | |
| 528 } | |
| 529 | 523 |
| 530 // tiling is ignored in the first pass | 524 // tiling is ignored in the first pass |
| 531 vp9_tile_init(&tile, cm, 0, 0); | 525 vp9_tile_init(&tile, cm, 0, 0); |
| 532 | 526 |
| 533 // for each macroblock row in image | 527 // for each macroblock row in image |
| 534 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { | 528 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { |
| 535 int_mv best_ref_mv; | 529 int_mv best_ref_mv; |
| 536 | 530 |
| 537 best_ref_mv.as_int = 0; | 531 best_ref_mv.as_int = 0; |
| 538 | 532 |
| 539 // reset above block coeffs | 533 // reset above block coeffs |
| 540 xd->up_available = (mb_row != 0); | 534 xd->up_available = (mb_row != 0); |
| 541 recon_yoffset = (mb_row * recon_y_stride * 16); | 535 recon_yoffset = (mb_row * recon_y_stride * 16); |
| 542 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); | 536 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); |
| 543 | 537 |
| 544 // Set up limit values for motion vectors to prevent them extending | 538 // Set up limit values for motion vectors to prevent them extending |
| 545 // outside the UMV borders | 539 // outside the UMV borders |
| 546 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); | 540 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
| 547 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) | 541 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
| 548 + BORDER_MV_PIXELS_B16; | 542 + BORDER_MV_PIXELS_B16; |
| 549 | 543 |
| 550 // for each macroblock col in image | 544 // for each macroblock col in image |
| 551 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { | 545 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { |
| 552 int this_error; | 546 int this_error; |
| 553 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 547 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 554 double error_weight = 1.0; | 548 double error_weight = 1.0; |
| 555 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); | 549 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); |
| 556 | 550 |
| 557 vp9_clear_system_state(); // __asm emms; | 551 vp9_clear_system_state(); // __asm emms; |
| 558 | 552 |
| 559 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 553 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 560 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 554 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 561 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 555 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 562 xd->left_available = (mb_col != 0); | 556 xd->left_available = (mb_col != 0); |
| 563 xd->mi_8x8[0]->mbmi.sb_type = bsize; | 557 xd->mi_8x8[0]->mbmi.sb_type = bsize; |
| 564 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; | 558 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 565 set_mi_row_col(xd, &tile, | 559 set_mi_row_col(xd, &tile, |
| 566 mb_row << 1, num_8x8_blocks_high_lookup[bsize], | 560 mb_row << 1, num_8x8_blocks_high_lookup[bsize], |
| 567 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], | 561 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], |
| 568 cm->mi_rows, cm->mi_cols); | 562 cm->mi_rows, cm->mi_cols); |
| 569 | 563 |
| 570 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 564 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 571 int energy = vp9_block_energy(cpi, x, bsize); | 565 const int energy = vp9_block_energy(cpi, x, bsize); |
| 572 error_weight = vp9_vaq_inv_q_ratio(energy); | 566 error_weight = vp9_vaq_inv_q_ratio(energy); |
| 573 } | 567 } |
| 574 | 568 |
| 575 // do intra 16x16 prediction | 569 // do intra 16x16 prediction |
| 576 this_error = vp9_encode_intra(x, use_dc_pred); | 570 this_error = vp9_encode_intra(x, use_dc_pred); |
| 577 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 571 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 578 vp9_clear_system_state(); // __asm emms; | 572 vp9_clear_system_state(); // __asm emms; |
| 579 this_error *= error_weight; | 573 this_error *= error_weight; |
| 580 } | 574 } |
| 581 | 575 |
| 582 // intrapenalty below deals with situations where the intra and inter | 576 // intrapenalty below deals with situations where the intra and inter |
| 583 // error scores are very low (eg a plain black frame). | 577 // error scores are very low (eg a plain black frame). |
| 584 // We do not have special cases in first pass for 0,0 and nearest etc so | 578 // We do not have special cases in first pass for 0,0 and nearest etc so |
| 585 // all inter modes carry an overhead cost estimate for the mv. | 579 // all inter modes carry an overhead cost estimate for the mv. |
| 586 // When the error score is very low this causes us to pick all or lots of | 580 // When the error score is very low this causes us to pick all or lots of |
| 587 // INTRA modes and throw lots of key frames. | 581 // INTRA modes and throw lots of key frames. |
| 588 // This penalty adds a cost matching that of a 0,0 mv to the intra case. | 582 // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 589 this_error += intrapenalty; | 583 this_error += intrapenalty; |
| 590 | 584 |
| 591 // Cumulative intra error total | 585 // Cumulative intra error total |
| 592 intra_error += (int64_t)this_error; | 586 intra_error += (int64_t)this_error; |
| 593 | 587 |
| 594 // Set up limit values for motion vectors to prevent them extending | 588 // Set up limit values for motion vectors to prevent them extending |
| 595 // outside the UMV borders. | 589 // outside the UMV borders. |
| 596 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 590 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 597 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) | 591 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
| 598 + BORDER_MV_PIXELS_B16; | |
| 599 | 592 |
| 600 // Other than for the first frame do a motion search | 593 // Other than for the first frame do a motion search |
| 601 if (cm->current_video_frame > 0) { | 594 if (cm->current_video_frame > 0) { |
| 602 int tmp_err, motion_error; | 595 int tmp_err, motion_error; |
| 603 int_mv mv, tmp_mv; | 596 int_mv mv, tmp_mv; |
| 604 | 597 |
| 605 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 598 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; |
| 606 motion_error = zz_motion_search(cpi, x); | 599 motion_error = zz_motion_search(cpi, x); |
| 607 // Simple 0,0 motion with no mv overhead | 600 // Simple 0,0 motion with no mv overhead |
| 608 mv.as_int = tmp_mv.as_int = 0; | 601 mv.as_int = tmp_mv.as_int = 0; |
| 609 | 602 |
| 610 // Test last reference frame using the previous best mv as the | 603 // Test last reference frame using the previous best mv as the |
| 611 // starting point (best reference) for the search | 604 // starting point (best reference) for the search |
| 612 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, | 605 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, |
| 613 &motion_error); | 606 &motion_error); |
| 614 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 607 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 615 vp9_clear_system_state(); // __asm emms; | 608 vp9_clear_system_state(); // __asm emms; |
| 616 motion_error *= error_weight; | 609 motion_error *= error_weight; |
| 617 } | 610 } |
| 618 | 611 |
| 619 // If the current best reference mv is not centered on 0,0 then do a 0,0 | 612 // If the current best reference mv is not centered on 0,0 then do a 0,0 |
| 620 // based search as well. | 613 // based search as well. |
| 621 if (best_ref_mv.as_int) { | 614 if (best_ref_mv.as_int) { |
| 622 tmp_err = INT_MAX; | 615 tmp_err = INT_MAX; |
| 623 first_pass_motion_search(cpi, x, &zero_ref_mv.as_mv, &tmp_mv.as_mv, | 616 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 624 &tmp_err); | 617 &tmp_err); |
| 625 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 618 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 626 vp9_clear_system_state(); // __asm emms; | 619 vp9_clear_system_state(); // __asm emms; |
| 627 tmp_err *= error_weight; | 620 tmp_err *= error_weight; |
| 628 } | 621 } |
| 629 | 622 |
| 630 if (tmp_err < motion_error) { | 623 if (tmp_err < motion_error) { |
| 631 motion_error = tmp_err; | 624 motion_error = tmp_err; |
| 632 mv.as_int = tmp_mv.as_int; | 625 mv.as_int = tmp_mv.as_int; |
| 633 } | 626 } |
| 634 } | 627 } |
| 635 | 628 |
| 636 // Experimental search in an older reference frame | 629 // Experimental search in an older reference frame |
| 637 if (cm->current_video_frame > 1) { | 630 if (cm->current_video_frame > 1) { |
| 638 // Simple 0,0 motion with no mv overhead | 631 // Simple 0,0 motion with no mv overhead |
| 639 int gf_motion_error; | 632 int gf_motion_error; |
| 640 | 633 |
| 641 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; | 634 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
| 642 gf_motion_error = zz_motion_search(cpi, x); | 635 gf_motion_error = zz_motion_search(cpi, x); |
| 643 | 636 |
| 644 first_pass_motion_search(cpi, x, &zero_ref_mv.as_mv, &tmp_mv.as_mv, | 637 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 645 &gf_motion_error); | 638 &gf_motion_error); |
| 646 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 639 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 647 vp9_clear_system_state(); // __asm emms; | 640 vp9_clear_system_state(); // __asm emms; |
| 648 gf_motion_error *= error_weight; | 641 gf_motion_error *= error_weight; |
| 649 } | 642 } |
| 650 | 643 |
| 651 if ((gf_motion_error < motion_error) && | 644 if (gf_motion_error < motion_error && gf_motion_error < this_error) |
| 652 (gf_motion_error < this_error)) { | |
| 653 second_ref_count++; | 645 second_ref_count++; |
| 654 } | |
| 655 | 646 |
| 656 // Reset to last frame as reference buffer | 647 // Reset to last frame as reference buffer |
| 657 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 648 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; |
| 658 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; | 649 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; |
| 659 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; | 650 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; |
| 660 | 651 |
| 661 // In accumulating a score for the older reference frame | 652 // In accumulating a score for the older reference frame |
| 662 // take the best of the motion predicted score and | 653 // take the best of the motion predicted score and |
| 663 // the intra coded error (just as will be done for) | 654 // the intra coded error (just as will be done for) |
| 664 // accumulation of "coded_error" for the last frame. | 655 // accumulation of "coded_error" for the last frame. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 fps.count = 1.0; | 763 fps.count = 1.0; |
| 773 fps.pcnt_inter = (double)intercount / cm->MBs; | 764 fps.pcnt_inter = (double)intercount / cm->MBs; |
| 774 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; | 765 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; |
| 775 fps.pcnt_neutral = (double)neutral_count / cm->MBs; | 766 fps.pcnt_neutral = (double)neutral_count / cm->MBs; |
| 776 | 767 |
| 777 if (mvcount > 0) { | 768 if (mvcount > 0) { |
| 778 fps.MVr = (double)sum_mvr / mvcount; | 769 fps.MVr = (double)sum_mvr / mvcount; |
| 779 fps.mvr_abs = (double)sum_mvr_abs / mvcount; | 770 fps.mvr_abs = (double)sum_mvr_abs / mvcount; |
| 780 fps.MVc = (double)sum_mvc / mvcount; | 771 fps.MVc = (double)sum_mvc / mvcount; |
| 781 fps.mvc_abs = (double)sum_mvc_abs / mvcount; | 772 fps.mvc_abs = (double)sum_mvc_abs / mvcount; |
| 782 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / | 773 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount; |
| 783 mvcount; | 774 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount; |
| 784 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / | |
| 785 mvcount; | |
| 786 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2); | 775 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2); |
| 787 fps.new_mv_count = new_mv_count; | 776 fps.new_mv_count = new_mv_count; |
| 788 fps.pcnt_motion = (double)mvcount / cpi->common.MBs; | 777 fps.pcnt_motion = (double)mvcount / cm->MBs; |
| 789 } else { | 778 } else { |
| 790 fps.MVr = 0.0; | 779 fps.MVr = 0.0; |
| 791 fps.mvr_abs = 0.0; | 780 fps.mvr_abs = 0.0; |
| 792 fps.MVc = 0.0; | 781 fps.MVc = 0.0; |
| 793 fps.mvc_abs = 0.0; | 782 fps.mvc_abs = 0.0; |
| 794 fps.MVrv = 0.0; | 783 fps.MVrv = 0.0; |
| 795 fps.MVcv = 0.0; | 784 fps.MVcv = 0.0; |
| 796 fps.mv_in_out_count = 0.0; | 785 fps.mv_in_out_count = 0.0; |
| 797 fps.new_mv_count = 0.0; | 786 fps.new_mv_count = 0.0; |
| 798 fps.pcnt_motion = 0.0; | 787 fps.pcnt_motion = 0.0; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 906 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, | 895 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, |
| 907 pt_high); | 896 pt_high); |
| 908 | 897 |
| 909 // Calculate correction factor | 898 // Calculate correction factor |
| 910 if (power_term < 1.0) | 899 if (power_term < 1.0) |
| 911 assert(error_term >= 0.0); | 900 assert(error_term >= 0.0); |
| 912 | 901 |
| 913 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 902 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 914 } | 903 } |
| 915 | 904 |
| 916 static int estimate_max_q(VP9_COMP *cpi, | 905 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats, |
| 917 FIRSTPASS_STATS *fpstats, | 906 int section_target_bandwitdh) { |
| 918 int section_target_bandwitdh) { | |
| 919 int q; | 907 int q; |
| 920 const int num_mbs = cpi->common.MBs; | 908 const int num_mbs = cpi->common.MBs; |
| 921 int target_norm_bits_per_mb; | 909 int target_norm_bits_per_mb; |
| 922 RATE_CONTROL *const rc = &cpi->rc; | 910 const RATE_CONTROL *const rc = &cpi->rc; |
| 923 | 911 |
| 924 const double section_err = fpstats->coded_error / fpstats->count; | 912 const double section_err = fpstats->coded_error / fpstats->count; |
| 925 const double err_per_mb = section_err / num_mbs; | 913 const double err_per_mb = section_err / num_mbs; |
| 926 | 914 |
| 927 if (section_target_bandwitdh <= 0) | 915 if (section_target_bandwitdh <= 0) |
| 928 return rc->worst_quality; // Highest value allowed | 916 return rc->worst_quality; // Highest value allowed |
| 929 | 917 |
| 930 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) | 918 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) |
| 931 ? (512 * section_target_bandwitdh) / num_mbs | 919 ? (512 * section_target_bandwitdh) / num_mbs |
| 932 : 512 * (section_target_bandwitdh / num_mbs); | 920 : 512 * (section_target_bandwitdh / num_mbs); |
| 933 | 921 |
| 934 // Try and pick a max Q that will be high enough to encode the | 922 // Try and pick a max Q that will be high enough to encode the |
| 935 // content at the given rate. | 923 // content at the given rate. |
| 936 for (q = rc->best_quality; q < rc->worst_quality; q++) { | 924 for (q = rc->best_quality; q < rc->worst_quality; q++) { |
| 937 const double err_correction_factor = calc_correction_factor(err_per_mb, | 925 const double err_correction_factor = calc_correction_factor(err_per_mb, |
| 938 ERR_DIVISOR, 0.5, 0.90, q); | 926 ERR_DIVISOR, 0.5, 0.90, q); |
| 939 const int bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, | 927 const int bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 940 err_correction_factor); | 928 err_correction_factor); |
| 941 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | 929 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 942 break; | 930 break; |
| 943 } | 931 } |
| 944 | 932 |
| 945 // Restriction on active max q for constrained quality mode. | 933 // Restriction on active max q for constrained quality mode. |
| 946 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) | 934 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) |
| 947 q = MAX(q, cpi->cq_target_quality); | 935 q = MAX(q, cpi->cq_target_quality); |
| 948 | 936 |
| 949 return q; | 937 return q; |
| 950 } | 938 } |
| 951 | 939 |
| 952 // For cq mode estimate a cq level that matches the observed | |
| 953 // complexity and data rate. | |
| 954 static int estimate_cq(VP9_COMP *cpi, | |
| 955 FIRSTPASS_STATS *fpstats, | |
| 956 int section_target_bandwitdh) { | |
| 957 int q; | |
| 958 int num_mbs = cpi->common.MBs; | |
| 959 int target_norm_bits_per_mb; | |
| 960 | |
| 961 double section_err = (fpstats->coded_error / fpstats->count); | |
| 962 double err_per_mb = section_err / num_mbs; | |
| 963 double err_correction_factor; | |
| 964 double clip_iiratio; | |
| 965 double clip_iifactor; | |
| 966 | |
| 967 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) | |
| 968 ? (512 * section_target_bandwitdh) / num_mbs | |
| 969 : 512 * (section_target_bandwitdh / num_mbs); | |
| 970 | |
| 971 | |
| 972 // II ratio correction factor for clip as a whole | |
| 973 clip_iiratio = cpi->twopass.total_stats.intra_error / | |
| 974 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats.coded_error); | |
| 975 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); | |
| 976 if (clip_iifactor < 0.80) | |
| 977 clip_iifactor = 0.80; | |
| 978 | |
| 979 // Try and pick a Q that can encode the content at the given rate. | |
| 980 for (q = 0; q < MAXQ; q++) { | |
| 981 int bits_per_mb_at_this_q; | |
| 982 | |
| 983 // Error per MB based correction factor | |
| 984 err_correction_factor = | |
| 985 calc_correction_factor(err_per_mb, 100.0, 0.5, 0.90, q) * clip_iifactor; | |
| 986 | |
| 987 bits_per_mb_at_this_q = | |
| 988 vp9_rc_bits_per_mb(INTER_FRAME, q, err_correction_factor); | |
| 989 | |
| 990 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | |
| 991 break; | |
| 992 } | |
| 993 | |
| 994 // Clip value to range "best allowed to (worst allowed - 1)" | |
| 995 q = select_cq_level(q); | |
| 996 if (q >= cpi->rc.worst_quality) | |
| 997 q = cpi->rc.worst_quality - 1; | |
| 998 if (q < cpi->rc.best_quality) | |
| 999 q = cpi->rc.best_quality; | |
| 1000 | |
| 1001 return q; | |
| 1002 } | |
| 1003 | |
| 1004 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); | 940 extern void vp9_new_framerate(VP9_COMP *cpi, double framerate); |
| 1005 | 941 |
| 1006 void vp9_init_second_pass(VP9_COMP *cpi) { | 942 void vp9_init_second_pass(VP9_COMP *cpi) { |
| 1007 FIRSTPASS_STATS this_frame; | 943 FIRSTPASS_STATS this_frame; |
| 1008 FIRSTPASS_STATS *start_pos; | 944 FIRSTPASS_STATS *start_pos; |
| 1009 struct twopass_rc *const twopass = &cpi->twopass; | 945 struct twopass_rc *const twopass = &cpi->twopass; |
| 1010 const VP9_CONFIG *const oxcf = &cpi->oxcf; | 946 const VP9_CONFIG *const oxcf = &cpi->oxcf; |
| 1011 | 947 |
| 1012 zero_stats(&twopass->total_stats); | 948 zero_stats(&twopass->total_stats); |
| 1013 zero_stats(&twopass->total_left_stats); | 949 zero_stats(&twopass->total_left_stats); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 | 1017 |
| 1082 reset_fpf_position(twopass, start_pos); | 1018 reset_fpf_position(twopass, start_pos); |
| 1083 } | 1019 } |
| 1084 } | 1020 } |
| 1085 | 1021 |
| 1086 void vp9_end_second_pass(VP9_COMP *cpi) { | 1022 void vp9_end_second_pass(VP9_COMP *cpi) { |
| 1087 } | 1023 } |
| 1088 | 1024 |
| 1089 // This function gives and estimate of how badly we believe | 1025 // This function gives and estimate of how badly we believe |
| 1090 // the prediction quality is decaying from frame to frame. | 1026 // the prediction quality is decaying from frame to frame. |
| 1091 static double get_prediction_decay_rate(VP9_COMP *cpi, | 1027 static double get_prediction_decay_rate(const VP9_COMMON *cm, |
| 1092 FIRSTPASS_STATS *next_frame) { | 1028 const FIRSTPASS_STATS *next_frame) { |
| 1093 // Look at the observed drop in prediction quality between the last frame | 1029 // Look at the observed drop in prediction quality between the last frame |
| 1094 // and the GF buffer (which contains an older frame). | 1030 // and the GF buffer (which contains an older frame). |
| 1095 const double mb_sr_err_diff = (next_frame->sr_coded_error - | 1031 const double mb_sr_err_diff = (next_frame->sr_coded_error - |
| 1096 next_frame->coded_error) / cpi->common.MBs; | 1032 next_frame->coded_error) / cm->MBs; |
| 1097 const double second_ref_decay = mb_sr_err_diff <= 512.0 | 1033 const double second_ref_decay = mb_sr_err_diff <= 512.0 |
| 1098 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) | 1034 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) |
| 1099 : 0.85; | 1035 : 0.85; |
| 1100 | 1036 |
| 1101 return MIN(second_ref_decay, next_frame->pcnt_inter); | 1037 return MIN(second_ref_decay, next_frame->pcnt_inter); |
| 1102 } | 1038 } |
| 1103 | 1039 |
| 1104 // Function to test for a condition where a complex transition is followed | 1040 // Function to test for a condition where a complex transition is followed |
| 1105 // by a static section. For example in slide shows where there is a fade | 1041 // by a static section. For example in slide shows where there is a fade |
| 1106 // between slides. This is to help with more optimal kf and gf positioning. | 1042 // between slides. This is to help with more optimal kf and gf positioning. |
| 1107 static int detect_transition_to_still( | 1043 static int detect_transition_to_still(VP9_COMP *cpi, int frame_interval, |
| 1108 VP9_COMP *cpi, | 1044 int still_interval, |
| 1109 int frame_interval, | 1045 double loop_decay_rate, |
| 1110 int still_interval, | 1046 double last_decay_rate) { |
| 1111 double loop_decay_rate, | |
| 1112 double last_decay_rate) { | |
| 1113 int trans_to_still = 0; | 1047 int trans_to_still = 0; |
| 1114 | 1048 |
| 1115 // Break clause to detect very still sections after motion | 1049 // Break clause to detect very still sections after motion |
| 1116 // For example a static image after a fade or other transition | 1050 // For example a static image after a fade or other transition |
| 1117 // instead of a clean scene cut. | 1051 // instead of a clean scene cut. |
| 1118 if (frame_interval > MIN_GF_INTERVAL && | 1052 if (frame_interval > MIN_GF_INTERVAL && |
| 1119 loop_decay_rate >= 0.999 && | 1053 loop_decay_rate >= 0.999 && |
| 1120 last_decay_rate < 0.9) { | 1054 last_decay_rate < 0.9) { |
| 1121 int j; | 1055 int j; |
| 1122 FIRSTPASS_STATS *position = cpi->twopass.stats_in; | 1056 FIRSTPASS_STATS *position = cpi->twopass.stats_in; |
| 1123 FIRSTPASS_STATS tmp_next_frame; | 1057 FIRSTPASS_STATS tmp_next_frame; |
| 1124 double zz_inter; | |
| 1125 | 1058 |
| 1126 // Look ahead a few frames to see if static condition | 1059 // Look ahead a few frames to see if static condition |
| 1127 // persists... | 1060 // persists... |
| 1128 for (j = 0; j < still_interval; j++) { | 1061 for (j = 0; j < still_interval; j++) { |
| 1129 if (EOF == input_stats(&cpi->twopass, &tmp_next_frame)) | 1062 if (EOF == input_stats(&cpi->twopass, &tmp_next_frame)) |
| 1130 break; | 1063 break; |
| 1131 | 1064 |
| 1132 zz_inter = (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion); | 1065 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999) |
| 1133 if (zz_inter < 0.999) | |
| 1134 break; | 1066 break; |
| 1135 } | 1067 } |
| 1136 // Reset file position | 1068 |
| 1137 reset_fpf_position(&cpi->twopass, position); | 1069 reset_fpf_position(&cpi->twopass, position); |
| 1138 | 1070 |
| 1139 // Only if it does do we signal a transition to still | 1071 // Only if it does do we signal a transition to still |
| 1140 if (j == still_interval) | 1072 if (j == still_interval) |
| 1141 trans_to_still = 1; | 1073 trans_to_still = 1; |
| 1142 } | 1074 } |
| 1143 | 1075 |
| 1144 return trans_to_still; | 1076 return trans_to_still; |
| 1145 } | 1077 } |
| 1146 | 1078 |
| 1147 // This function detects a flash through the high relative pcnt_second_ref | 1079 // This function detects a flash through the high relative pcnt_second_ref |
| 1148 // score in the frame following a flash frame. The offset passed in should | 1080 // score in the frame following a flash frame. The offset passed in should |
| 1149 // reflect this | 1081 // reflect this |
| 1150 static int detect_flash(VP9_COMP *cpi, int offset) { | 1082 static int detect_flash(const struct twopass_rc *twopass, int offset) { |
| 1151 FIRSTPASS_STATS next_frame; | 1083 FIRSTPASS_STATS next_frame; |
| 1152 | 1084 |
| 1153 int flash_detected = 0; | 1085 int flash_detected = 0; |
| 1154 | 1086 |
| 1155 // Read the frame data. | 1087 // Read the frame data. |
| 1156 // The return is FALSE (no flash detected) if not a valid frame | 1088 // The return is FALSE (no flash detected) if not a valid frame |
| 1157 if (read_frame_stats(&cpi->twopass, &next_frame, offset) != EOF) { | 1089 if (read_frame_stats(twopass, &next_frame, offset) != EOF) { |
| 1158 // What we are looking for here is a situation where there is a | 1090 // What we are looking for here is a situation where there is a |
| 1159 // brief break in prediction (such as a flash) but subsequent frames | 1091 // brief break in prediction (such as a flash) but subsequent frames |
| 1160 // are reasonably well predicted by an earlier (pre flash) frame. | 1092 // are reasonably well predicted by an earlier (pre flash) frame. |
| 1161 // The recovery after a flash is indicated by a high pcnt_second_ref | 1093 // The recovery after a flash is indicated by a high pcnt_second_ref |
| 1162 // comapred to pcnt_inter. | 1094 // comapred to pcnt_inter. |
| 1163 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && | 1095 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && |
| 1164 next_frame.pcnt_second_ref >= 0.5) | 1096 next_frame.pcnt_second_ref >= 0.5) |
| 1165 flash_detected = 1; | 1097 flash_detected = 1; |
| 1166 } | 1098 } |
| 1167 | 1099 |
| 1168 return flash_detected; | 1100 return flash_detected; |
| 1169 } | 1101 } |
| 1170 | 1102 |
| 1171 // Update the motion related elements to the GF arf boost calculation | 1103 // Update the motion related elements to the GF arf boost calculation |
| 1172 static void accumulate_frame_motion_stats( | 1104 static void accumulate_frame_motion_stats( |
| 1173 FIRSTPASS_STATS *this_frame, | 1105 FIRSTPASS_STATS *this_frame, |
| 1174 double *this_frame_mv_in_out, | 1106 double *this_frame_mv_in_out, |
| 1175 double *mv_in_out_accumulator, | 1107 double *mv_in_out_accumulator, |
| 1176 double *abs_mv_in_out_accumulator, | 1108 double *abs_mv_in_out_accumulator, |
| 1177 double *mv_ratio_accumulator) { | 1109 double *mv_ratio_accumulator) { |
| 1178 double motion_pct; | 1110 double motion_pct; |
| 1179 | 1111 |
| 1180 // Accumulate motion stats. | 1112 // Accumulate motion stats. |
| 1181 motion_pct = this_frame->pcnt_motion; | 1113 motion_pct = this_frame->pcnt_motion; |
| 1182 | 1114 |
| 1183 // Accumulate Motion In/Out of frame stats | 1115 // Accumulate Motion In/Out of frame stats |
| 1184 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; | 1116 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; |
| 1185 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; | 1117 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; |
| 1186 *abs_mv_in_out_accumulator += | 1118 *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct); |
| 1187 fabs(this_frame->mv_in_out_count * motion_pct); | |
| 1188 | 1119 |
| 1189 // Accumulate a measure of how uniform (or conversely how random) | 1120 // Accumulate a measure of how uniform (or conversely how random) |
| 1190 // the motion field is. (A ratio of absmv / mv) | 1121 // the motion field is. (A ratio of absmv / mv) |
| 1191 if (motion_pct > 0.05) { | 1122 if (motion_pct > 0.05) { |
| 1192 double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / | 1123 const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / |
| 1193 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); | 1124 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); |
| 1194 | 1125 |
| 1195 double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / | 1126 const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / |
| 1196 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); | 1127 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); |
| 1197 | 1128 |
| 1198 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs) | 1129 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs) |
| 1199 ? (this_frame_mvr_ratio * motion_pct) | 1130 ? (this_frame_mvr_ratio * motion_pct) |
| 1200 : this_frame->mvr_abs * motion_pct; | 1131 : this_frame->mvr_abs * motion_pct; |
| 1201 | 1132 |
| 1202 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs) | 1133 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs) |
| 1203 ? (this_frame_mvc_ratio * motion_pct) | 1134 ? (this_frame_mvc_ratio * motion_pct) |
| 1204 : this_frame->mvc_abs * motion_pct; | 1135 : this_frame->mvc_abs * motion_pct; |
| 1205 } | 1136 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1228 else | 1159 else |
| 1229 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); | 1160 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
| 1230 | 1161 |
| 1231 return MIN(frame_boost, GF_RMAX); | 1162 return MIN(frame_boost, GF_RMAX); |
| 1232 } | 1163 } |
| 1233 | 1164 |
| 1234 static int calc_arf_boost(VP9_COMP *cpi, int offset, | 1165 static int calc_arf_boost(VP9_COMP *cpi, int offset, |
| 1235 int f_frames, int b_frames, | 1166 int f_frames, int b_frames, |
| 1236 int *f_boost, int *b_boost) { | 1167 int *f_boost, int *b_boost) { |
| 1237 FIRSTPASS_STATS this_frame; | 1168 FIRSTPASS_STATS this_frame; |
| 1238 | 1169 struct twopass_rc *const twopass = &cpi->twopass; |
| 1239 int i; | 1170 int i; |
| 1240 double boost_score = 0.0; | 1171 double boost_score = 0.0; |
| 1241 double mv_ratio_accumulator = 0.0; | 1172 double mv_ratio_accumulator = 0.0; |
| 1242 double decay_accumulator = 1.0; | 1173 double decay_accumulator = 1.0; |
| 1243 double this_frame_mv_in_out = 0.0; | 1174 double this_frame_mv_in_out = 0.0; |
| 1244 double mv_in_out_accumulator = 0.0; | 1175 double mv_in_out_accumulator = 0.0; |
| 1245 double abs_mv_in_out_accumulator = 0.0; | 1176 double abs_mv_in_out_accumulator = 0.0; |
| 1246 int arf_boost; | 1177 int arf_boost; |
| 1247 int flash_detected = 0; | 1178 int flash_detected = 0; |
| 1248 | 1179 |
| 1249 // Search forward from the proposed arf/next gf position | 1180 // Search forward from the proposed arf/next gf position |
| 1250 for (i = 0; i < f_frames; i++) { | 1181 for (i = 0; i < f_frames; i++) { |
| 1251 if (read_frame_stats(&cpi->twopass, &this_frame, (i + offset)) == EOF) | 1182 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) |
| 1252 break; | 1183 break; |
| 1253 | 1184 |
| 1254 // Update the motion related elements to the boost calculation | 1185 // Update the motion related elements to the boost calculation |
| 1255 accumulate_frame_motion_stats(&this_frame, | 1186 accumulate_frame_motion_stats(&this_frame, |
| 1256 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1187 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1257 &abs_mv_in_out_accumulator, | 1188 &abs_mv_in_out_accumulator, |
| 1258 &mv_ratio_accumulator); | 1189 &mv_ratio_accumulator); |
| 1259 | 1190 |
| 1260 // We want to discount the flash frame itself and the recovery | 1191 // We want to discount the flash frame itself and the recovery |
| 1261 // frame that follows as both will have poor scores. | 1192 // frame that follows as both will have poor scores. |
| 1262 flash_detected = detect_flash(cpi, (i + offset)) || | 1193 flash_detected = detect_flash(twopass, i + offset) || |
| 1263 detect_flash(cpi, (i + offset + 1)); | 1194 detect_flash(twopass, i + offset + 1); |
| 1264 | 1195 |
| 1265 // Cumulative effect of prediction quality decay | 1196 // Cumulative effect of prediction quality decay |
| 1266 if (!flash_detected) { | 1197 if (!flash_detected) { |
| 1267 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); | 1198 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1268 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1199 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1269 ? MIN_DECAY_FACTOR : decay_accumulator; | 1200 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1270 } | 1201 } |
| 1271 | 1202 |
| 1272 boost_score += (decay_accumulator * | 1203 boost_score += (decay_accumulator * |
| 1273 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1204 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); |
| 1274 } | 1205 } |
| 1275 | 1206 |
| 1276 *f_boost = (int)boost_score; | 1207 *f_boost = (int)boost_score; |
| 1277 | 1208 |
| 1278 // Reset for backward looking loop | 1209 // Reset for backward looking loop |
| 1279 boost_score = 0.0; | 1210 boost_score = 0.0; |
| 1280 mv_ratio_accumulator = 0.0; | 1211 mv_ratio_accumulator = 0.0; |
| 1281 decay_accumulator = 1.0; | 1212 decay_accumulator = 1.0; |
| 1282 this_frame_mv_in_out = 0.0; | 1213 this_frame_mv_in_out = 0.0; |
| 1283 mv_in_out_accumulator = 0.0; | 1214 mv_in_out_accumulator = 0.0; |
| 1284 abs_mv_in_out_accumulator = 0.0; | 1215 abs_mv_in_out_accumulator = 0.0; |
| 1285 | 1216 |
| 1286 // Search backward towards last gf position | 1217 // Search backward towards last gf position |
| 1287 for (i = -1; i >= -b_frames; i--) { | 1218 for (i = -1; i >= -b_frames; i--) { |
| 1288 if (read_frame_stats(&cpi->twopass, &this_frame, (i + offset)) == EOF) | 1219 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) |
| 1289 break; | 1220 break; |
| 1290 | 1221 |
| 1291 // Update the motion related elements to the boost calculation | 1222 // Update the motion related elements to the boost calculation |
| 1292 accumulate_frame_motion_stats(&this_frame, | 1223 accumulate_frame_motion_stats(&this_frame, |
| 1293 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1224 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1294 &abs_mv_in_out_accumulator, | 1225 &abs_mv_in_out_accumulator, |
| 1295 &mv_ratio_accumulator); | 1226 &mv_ratio_accumulator); |
| 1296 | 1227 |
| 1297 // We want to discount the the flash frame itself and the recovery | 1228 // We want to discount the the flash frame itself and the recovery |
| 1298 // frame that follows as both will have poor scores. | 1229 // frame that follows as both will have poor scores. |
| 1299 flash_detected = detect_flash(cpi, (i + offset)) || | 1230 flash_detected = detect_flash(twopass, i + offset) || |
| 1300 detect_flash(cpi, (i + offset + 1)); | 1231 detect_flash(twopass, i + offset + 1); |
| 1301 | 1232 |
| 1302 // Cumulative effect of prediction quality decay | 1233 // Cumulative effect of prediction quality decay |
| 1303 if (!flash_detected) { | 1234 if (!flash_detected) { |
| 1304 decay_accumulator *= get_prediction_decay_rate(cpi, &this_frame); | 1235 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1305 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1236 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1306 ? MIN_DECAY_FACTOR : decay_accumulator; | 1237 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1307 } | 1238 } |
| 1308 | 1239 |
| 1309 boost_score += (decay_accumulator * | 1240 boost_score += (decay_accumulator * |
| 1310 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1241 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); |
| 1311 } | 1242 } |
| 1312 *b_boost = (int)boost_score; | 1243 *b_boost = (int)boost_score; |
| 1313 | 1244 |
| 1314 arf_boost = (*f_boost + *b_boost); | 1245 arf_boost = (*f_boost + *b_boost); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1454 } | 1385 } |
| 1455 printf("\n"); | 1386 printf("\n"); |
| 1456 #endif | 1387 #endif |
| 1457 } | 1388 } |
| 1458 #endif | 1389 #endif |
| 1459 | 1390 |
| 1460 // Analyse and define a gf/arf group. | 1391 // Analyse and define a gf/arf group. |
| 1461 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1392 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1462 FIRSTPASS_STATS next_frame = { 0 }; | 1393 FIRSTPASS_STATS next_frame = { 0 }; |
| 1463 FIRSTPASS_STATS *start_pos; | 1394 FIRSTPASS_STATS *start_pos; |
| 1395 struct twopass_rc *const twopass = &cpi->twopass; |
| 1464 int i; | 1396 int i; |
| 1465 double boost_score = 0.0; | 1397 double boost_score = 0.0; |
| 1466 double old_boost_score = 0.0; | 1398 double old_boost_score = 0.0; |
| 1467 double gf_group_err = 0.0; | 1399 double gf_group_err = 0.0; |
| 1468 double gf_first_frame_err = 0.0; | 1400 double gf_first_frame_err = 0.0; |
| 1469 double mod_frame_err = 0.0; | 1401 double mod_frame_err = 0.0; |
| 1470 | 1402 |
| 1471 double mv_ratio_accumulator = 0.0; | 1403 double mv_ratio_accumulator = 0.0; |
| 1472 double decay_accumulator = 1.0; | 1404 double decay_accumulator = 1.0; |
| 1473 double zero_motion_accumulator = 1.0; | 1405 double zero_motion_accumulator = 1.0; |
| 1474 | 1406 |
| 1475 double loop_decay_rate = 1.00; // Starting decay rate | 1407 double loop_decay_rate = 1.00; // Starting decay rate |
| 1476 double last_loop_decay_rate = 1.00; | 1408 double last_loop_decay_rate = 1.00; |
| 1477 | 1409 |
| 1478 double this_frame_mv_in_out = 0.0; | 1410 double this_frame_mv_in_out = 0.0; |
| 1479 double mv_in_out_accumulator = 0.0; | 1411 double mv_in_out_accumulator = 0.0; |
| 1480 double abs_mv_in_out_accumulator = 0.0; | 1412 double abs_mv_in_out_accumulator = 0.0; |
| 1481 double mv_ratio_accumulator_thresh; | 1413 double mv_ratio_accumulator_thresh; |
| 1482 int max_bits = frame_max_bits(cpi); // Max for a single frame | 1414 const int max_bits = frame_max_bits(cpi); // Max for a single frame |
| 1483 | 1415 |
| 1484 unsigned int allow_alt_ref = | 1416 unsigned int allow_alt_ref = cpi->oxcf.play_alternate && |
| 1485 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames; | 1417 cpi->oxcf.lag_in_frames; |
| 1486 | 1418 |
| 1487 int f_boost = 0; | 1419 int f_boost = 0; |
| 1488 int b_boost = 0; | 1420 int b_boost = 0; |
| 1489 int flash_detected; | 1421 int flash_detected; |
| 1490 int active_max_gf_interval; | 1422 int active_max_gf_interval; |
| 1491 RATE_CONTROL *const rc = &cpi->rc; | 1423 RATE_CONTROL *const rc = &cpi->rc; |
| 1492 | 1424 |
| 1493 cpi->twopass.gf_group_bits = 0; | 1425 twopass->gf_group_bits = 0; |
| 1494 | 1426 |
| 1495 vp9_clear_system_state(); // __asm emms; | 1427 vp9_clear_system_state(); // __asm emms; |
| 1496 | 1428 |
| 1497 start_pos = cpi->twopass.stats_in; | 1429 start_pos = twopass->stats_in; |
| 1498 | 1430 |
| 1499 // Load stats for the current frame. | 1431 // Load stats for the current frame. |
| 1500 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1432 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1501 | 1433 |
| 1502 // Note the error of the frame at the start of the group (this will be | 1434 // Note the error of the frame at the start of the group (this will be |
| 1503 // the GF frame error if we code a normal gf | 1435 // the GF frame error if we code a normal gf |
| 1504 gf_first_frame_err = mod_frame_err; | 1436 gf_first_frame_err = mod_frame_err; |
| 1505 | 1437 |
| 1506 // If this is a key frame or the overlay from a previous arf then | 1438 // If this is a key frame or the overlay from a previous arf then |
| 1507 // The error score / cost of this frame has already been accounted for. | 1439 // The error score / cost of this frame has already been accounted for. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1518 // At high Q when there are few bits to spare we are better with a longer | 1450 // At high Q when there are few bits to spare we are better with a longer |
| 1519 // interval to spread the cost of the GF. | 1451 // interval to spread the cost of the GF. |
| 1520 // | 1452 // |
| 1521 active_max_gf_interval = | 1453 active_max_gf_interval = |
| 1522 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); | 1454 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); |
| 1523 | 1455 |
| 1524 if (active_max_gf_interval > rc->max_gf_interval) | 1456 if (active_max_gf_interval > rc->max_gf_interval) |
| 1525 active_max_gf_interval = rc->max_gf_interval; | 1457 active_max_gf_interval = rc->max_gf_interval; |
| 1526 | 1458 |
| 1527 i = 0; | 1459 i = 0; |
| 1528 while ((i < cpi->twopass.static_scene_max_gf_interval) && | 1460 while (i < twopass->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1529 (i < rc->frames_to_key)) { | |
| 1530 i++; // Increment the loop counter | 1461 i++; // Increment the loop counter |
| 1531 | 1462 |
| 1532 // Accumulate error score of frames in this gf group | 1463 // Accumulate error score of frames in this gf group |
| 1533 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1464 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1534 gf_group_err += mod_frame_err; | 1465 gf_group_err += mod_frame_err; |
| 1535 | 1466 |
| 1536 if (EOF == input_stats(&cpi->twopass, &next_frame)) | 1467 if (EOF == input_stats(twopass, &next_frame)) |
| 1537 break; | 1468 break; |
| 1538 | 1469 |
| 1539 // Test for the case where there is a brief flash but the prediction | 1470 // Test for the case where there is a brief flash but the prediction |
| 1540 // quality back to an earlier frame is then restored. | 1471 // quality back to an earlier frame is then restored. |
| 1541 flash_detected = detect_flash(cpi, 0); | 1472 flash_detected = detect_flash(twopass, 0); |
| 1542 | 1473 |
| 1543 // Update the motion related elements to the boost calculation | 1474 // Update the motion related elements to the boost calculation |
| 1544 accumulate_frame_motion_stats(&next_frame, | 1475 accumulate_frame_motion_stats(&next_frame, |
| 1545 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1476 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1546 &abs_mv_in_out_accumulator, | 1477 &abs_mv_in_out_accumulator, |
| 1547 &mv_ratio_accumulator); | 1478 &mv_ratio_accumulator); |
| 1548 | 1479 |
| 1549 // Cumulative effect of prediction quality decay | 1480 // Cumulative effect of prediction quality decay |
| 1550 if (!flash_detected) { | 1481 if (!flash_detected) { |
| 1551 last_loop_decay_rate = loop_decay_rate; | 1482 last_loop_decay_rate = loop_decay_rate; |
| 1552 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); | 1483 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 1553 decay_accumulator = decay_accumulator * loop_decay_rate; | 1484 decay_accumulator = decay_accumulator * loop_decay_rate; |
| 1554 | 1485 |
| 1555 // Monitor for static sections. | 1486 // Monitor for static sections. |
| 1556 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1487 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 1557 zero_motion_accumulator) { | 1488 zero_motion_accumulator) { |
| 1558 zero_motion_accumulator = | 1489 zero_motion_accumulator = next_frame.pcnt_inter - |
| 1559 (next_frame.pcnt_inter - next_frame.pcnt_motion); | 1490 next_frame.pcnt_motion; |
| 1560 } | 1491 } |
| 1561 | 1492 |
| 1562 // Break clause to detect very still sections after motion | 1493 // Break clause to detect very still sections after motion |
| 1563 // (for example a static image after a fade or other transition). | 1494 // (for example a static image after a fade or other transition). |
| 1564 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, | 1495 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, |
| 1565 last_loop_decay_rate)) { | 1496 last_loop_decay_rate)) { |
| 1566 allow_alt_ref = 0; | 1497 allow_alt_ref = 0; |
| 1567 break; | 1498 break; |
| 1568 } | 1499 } |
| 1569 } | 1500 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1587 ((boost_score - old_boost_score) < IIFACTOR)))) { | 1518 ((boost_score - old_boost_score) < IIFACTOR)))) { |
| 1588 boost_score = old_boost_score; | 1519 boost_score = old_boost_score; |
| 1589 break; | 1520 break; |
| 1590 } | 1521 } |
| 1591 | 1522 |
| 1592 *this_frame = next_frame; | 1523 *this_frame = next_frame; |
| 1593 | 1524 |
| 1594 old_boost_score = boost_score; | 1525 old_boost_score = boost_score; |
| 1595 } | 1526 } |
| 1596 | 1527 |
| 1597 cpi->twopass.gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1528 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
| 1598 | 1529 |
| 1599 // Don't allow a gf too near the next kf | 1530 // Don't allow a gf too near the next kf |
| 1600 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { | 1531 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { |
| 1601 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { | 1532 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { |
| 1602 i++; | 1533 i++; |
| 1603 | 1534 |
| 1604 if (EOF == input_stats(&cpi->twopass, this_frame)) | 1535 if (EOF == input_stats(twopass, this_frame)) |
| 1605 break; | 1536 break; |
| 1606 | 1537 |
| 1607 if (i < rc->frames_to_key) { | 1538 if (i < rc->frames_to_key) { |
| 1608 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1539 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1609 gf_group_err += mod_frame_err; | 1540 gf_group_err += mod_frame_err; |
| 1610 } | 1541 } |
| 1611 } | 1542 } |
| 1612 } | 1543 } |
| 1613 | 1544 |
| 1614 #if CONFIG_MULTIPLE_ARF | 1545 #if CONFIG_MULTIPLE_ARF |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1628 rc->baseline_gf_interval = i - 1; | 1559 rc->baseline_gf_interval = i - 1; |
| 1629 else | 1560 else |
| 1630 rc->baseline_gf_interval = i; | 1561 rc->baseline_gf_interval = i; |
| 1631 | 1562 |
| 1632 // Should we use the alternate reference frame | 1563 // Should we use the alternate reference frame |
| 1633 if (allow_alt_ref && | 1564 if (allow_alt_ref && |
| 1634 (i < cpi->oxcf.lag_in_frames) && | 1565 (i < cpi->oxcf.lag_in_frames) && |
| 1635 (i >= MIN_GF_INTERVAL) && | 1566 (i >= MIN_GF_INTERVAL) && |
| 1636 // for real scene cuts (not forced kfs) dont allow arf very near kf. | 1567 // for real scene cuts (not forced kfs) dont allow arf very near kf. |
| 1637 (rc->next_key_frame_forced || | 1568 (rc->next_key_frame_forced || |
| 1638 (i <= (rc->frames_to_key - MIN_GF_INTERVAL))) && | 1569 (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) { |
| 1639 ((next_frame.pcnt_inter > 0.75) || | |
| 1640 (next_frame.pcnt_second_ref > 0.5)) && | |
| 1641 ((mv_in_out_accumulator / (double)i > -0.2) || | |
| 1642 (mv_in_out_accumulator > -2.0)) && | |
| 1643 (boost_score > 100)) { | |
| 1644 | |
| 1645 // Alternative boost calculation for alt ref | 1570 // Alternative boost calculation for alt ref |
| 1646 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, | 1571 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
| 1647 &b_boost); | 1572 &b_boost); |
| 1648 rc->source_alt_ref_pending = 1; | 1573 rc->source_alt_ref_pending = 1; |
| 1649 | 1574 |
| 1650 #if CONFIG_MULTIPLE_ARF | 1575 #if CONFIG_MULTIPLE_ARF |
| 1651 // Set the ARF schedule. | 1576 // Set the ARF schedule. |
| 1652 if (cpi->multi_arf_enabled) { | 1577 if (cpi->multi_arf_enabled) { |
| 1653 schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0); | 1578 schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0); |
| 1654 } | 1579 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1698 printf("Weight: "); | 1623 printf("Weight: "); |
| 1699 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1624 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1700 printf("%4d ", cpi->arf_weight[i]); | 1625 printf("%4d ", cpi->arf_weight[i]); |
| 1701 } | 1626 } |
| 1702 printf("\n"); | 1627 printf("\n"); |
| 1703 } | 1628 } |
| 1704 #endif | 1629 #endif |
| 1705 #endif | 1630 #endif |
| 1706 | 1631 |
| 1707 // Calculate the bits to be allocated to the group as a whole | 1632 // Calculate the bits to be allocated to the group as a whole |
| 1708 if ((cpi->twopass.kf_group_bits > 0) && | 1633 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { |
| 1709 (cpi->twopass.kf_group_error_left > 0)) { | 1634 twopass->gf_group_bits = (int64_t)(cpi->twopass.kf_group_bits * |
| 1710 cpi->twopass.gf_group_bits = | |
| 1711 (int64_t)(cpi->twopass.kf_group_bits * | |
| 1712 (gf_group_err / cpi->twopass.kf_group_error_left)); | 1635 (gf_group_err / cpi->twopass.kf_group_error_left)); |
| 1713 } else { | 1636 } else { |
| 1714 cpi->twopass.gf_group_bits = 0; | 1637 twopass->gf_group_bits = 0; |
| 1715 } | 1638 } |
| 1716 cpi->twopass.gf_group_bits = | 1639 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? |
| 1717 (cpi->twopass.gf_group_bits < 0) | 1640 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? |
| 1718 ? 0 | 1641 twopass->kf_group_bits : twopass->gf_group_bits; |
| 1719 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits) | |
| 1720 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits; | |
| 1721 | 1642 |
| 1722 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | 1643 // Clip cpi->twopass.gf_group_bits based on user supplied data rate |
| 1723 // variability limit (cpi->oxcf.two_pass_vbrmax_section) | 1644 // variability limit (cpi->oxcf.two_pass_vbrmax_section) |
| 1724 if (cpi->twopass.gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) | 1645 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) |
| 1725 cpi->twopass.gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; | 1646 twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; |
| 1726 | 1647 |
| 1727 // Reset the file position | 1648 // Reset the file position |
| 1728 reset_fpf_position(&cpi->twopass, start_pos); | 1649 reset_fpf_position(twopass, start_pos); |
| 1729 | 1650 |
| 1730 // Assign bits to the arf or gf. | 1651 // Assign bits to the arf or gf. |
| 1731 for (i = 0; i <= (rc->source_alt_ref_pending && | 1652 for (i = 0; i <= (rc->source_alt_ref_pending && |
| 1732 cpi->common.frame_type != KEY_FRAME); ++i) { | 1653 cpi->common.frame_type != KEY_FRAME); ++i) { |
| 1733 int allocation_chunks; | 1654 int allocation_chunks; |
| 1734 int q = rc->last_q[INTER_FRAME]; | 1655 int q = rc->last_q[INTER_FRAME]; |
| 1735 int gf_bits; | 1656 int gf_bits; |
| 1736 | 1657 |
| 1737 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; | 1658 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; |
| 1738 | 1659 |
| 1739 // Set max and minimum boost and hence minimum allocation | 1660 // Set max and minimum boost and hence minimum allocation |
| 1740 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); | 1661 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); |
| 1741 | 1662 |
| 1742 if (rc->source_alt_ref_pending && i == 0) | 1663 if (rc->source_alt_ref_pending && i == 0) |
| 1743 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost; | 1664 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost; |
| 1744 else | 1665 else |
| 1745 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100); | 1666 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100); |
| 1746 | 1667 |
| 1747 // Prevent overflow | 1668 // Prevent overflow |
| 1748 if (boost > 1023) { | 1669 if (boost > 1023) { |
| 1749 int divisor = boost >> 10; | 1670 int divisor = boost >> 10; |
| 1750 boost /= divisor; | 1671 boost /= divisor; |
| 1751 allocation_chunks /= divisor; | 1672 allocation_chunks /= divisor; |
| 1752 } | 1673 } |
| 1753 | 1674 |
| 1754 // Calculate the number of bits to be spent on the gf or arf based on | 1675 // Calculate the number of bits to be spent on the gf or arf based on |
| 1755 // the boost number | 1676 // the boost number |
| 1756 gf_bits = (int)((double)boost * (cpi->twopass.gf_group_bits / | 1677 gf_bits = (int)((double)boost * (twopass->gf_group_bits / |
| 1757 (double)allocation_chunks)); | 1678 (double)allocation_chunks)); |
| 1758 | 1679 |
| 1759 // If the frame that is to be boosted is simpler than the average for | 1680 // If the frame that is to be boosted is simpler than the average for |
| 1760 // the gf/arf group then use an alternative calculation | 1681 // the gf/arf group then use an alternative calculation |
| 1761 // based on the error score of the frame itself | 1682 // based on the error score of the frame itself |
| 1762 if (rc->baseline_gf_interval < 1 || | 1683 if (rc->baseline_gf_interval < 1 || |
| 1763 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) { | 1684 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) { |
| 1764 double alt_gf_grp_bits = (double)cpi->twopass.kf_group_bits * | 1685 double alt_gf_grp_bits = (double)twopass->kf_group_bits * |
| 1765 (mod_frame_err * (double)rc->baseline_gf_interval) / | 1686 (mod_frame_err * (double)rc->baseline_gf_interval) / |
| 1766 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left); | 1687 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left); |
| 1767 | 1688 |
| 1768 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / | 1689 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / |
| 1769 (double)allocation_chunks)); | 1690 (double)allocation_chunks)); |
| 1770 | 1691 |
| 1771 if (gf_bits > alt_gf_bits) | 1692 if (gf_bits > alt_gf_bits) |
| 1772 gf_bits = alt_gf_bits; | 1693 gf_bits = alt_gf_bits; |
| 1773 } else { | 1694 } else { |
| 1774 // If it is harder than other frames in the group make sure it at | 1695 // If it is harder than other frames in the group make sure it at |
| 1775 // least receives an allocation in keeping with its relative error | 1696 // least receives an allocation in keeping with its relative error |
| 1776 // score, otherwise it may be worse off than an "un-boosted" frame. | 1697 // score, otherwise it may be worse off than an "un-boosted" frame. |
| 1777 int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits * | 1698 int alt_gf_bits = (int)((double)twopass->kf_group_bits * |
| 1778 mod_frame_err / | 1699 mod_frame_err / |
| 1779 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left)); | 1700 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left)); |
| 1780 | 1701 |
| 1781 if (alt_gf_bits > gf_bits) | 1702 if (alt_gf_bits > gf_bits) |
| 1782 gf_bits = alt_gf_bits; | 1703 gf_bits = alt_gf_bits; |
| 1783 } | 1704 } |
| 1784 | 1705 |
| 1785 // Dont allow a negative value for gf_bits | 1706 // Dont allow a negative value for gf_bits |
| 1786 if (gf_bits < 0) | 1707 if (gf_bits < 0) |
| 1787 gf_bits = 0; | 1708 gf_bits = 0; |
| 1788 | 1709 |
| 1789 if (i == 0) { | 1710 if (i == 0) { |
| 1790 cpi->twopass.gf_bits = gf_bits; | 1711 twopass->gf_bits = gf_bits; |
| 1791 } | 1712 } |
| 1792 if (i == 1 || | 1713 if (i == 1 || |
| 1793 (!rc->source_alt_ref_pending && | 1714 (!rc->source_alt_ref_pending && |
| 1794 (cpi->common.frame_type != KEY_FRAME))) { | 1715 cpi->common.frame_type != KEY_FRAME)) { |
| 1795 // Per frame bit target for this frame | 1716 // Per frame bit target for this frame |
| 1796 rc->per_frame_bandwidth = gf_bits; | 1717 vp9_rc_set_frame_target(cpi, gf_bits); |
| 1797 } | 1718 } |
| 1798 } | 1719 } |
| 1799 | 1720 |
| 1800 { | 1721 { |
| 1801 // Adjust KF group bits and error remaining | 1722 // Adjust KF group bits and error remaining |
| 1802 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err; | 1723 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1803 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits; | 1724 twopass->kf_group_bits -= twopass->gf_group_bits; |
| 1804 | 1725 |
| 1805 if (cpi->twopass.kf_group_bits < 0) | 1726 if (twopass->kf_group_bits < 0) |
| 1806 cpi->twopass.kf_group_bits = 0; | 1727 twopass->kf_group_bits = 0; |
| 1807 | 1728 |
| 1808 // If this is an arf update we want to remove the score for the | 1729 // If this is an arf update we want to remove the score for the |
| 1809 // overlay frame at the end which will usually be very cheap to code. | 1730 // overlay frame at the end which will usually be very cheap to code. |
| 1810 // The overlay frame has already in effect been coded so we want to spread | 1731 // The overlay frame has already in effect been coded so we want to spread |
| 1811 // the remaining bits amoung the other frames/ | 1732 // the remaining bits amoung the other frames/ |
| 1812 // For normal GFs remove the score for the GF itself unless this is | 1733 // For normal GFs remove the score for the GF itself unless this is |
| 1813 // also a key frame in which case it has already been accounted for. | 1734 // also a key frame in which case it has already been accounted for. |
| 1814 if (rc->source_alt_ref_pending) { | 1735 if (rc->source_alt_ref_pending) { |
| 1815 cpi->twopass.gf_group_error_left = (int64_t)gf_group_err - mod_frame_err; | 1736 twopass->gf_group_error_left = (int64_t)gf_group_err - mod_frame_err; |
| 1816 } else if (cpi->common.frame_type != KEY_FRAME) { | 1737 } else if (cpi->common.frame_type != KEY_FRAME) { |
| 1817 cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err | 1738 twopass->gf_group_error_left = (int64_t)(gf_group_err |
| 1818 - gf_first_frame_err); | 1739 - gf_first_frame_err); |
| 1819 } else { | 1740 } else { |
| 1820 cpi->twopass.gf_group_error_left = (int64_t)gf_group_err; | 1741 twopass->gf_group_error_left = (int64_t)gf_group_err; |
| 1821 } | 1742 } |
| 1822 | 1743 |
| 1823 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits; | 1744 twopass->gf_group_bits -= twopass->gf_bits; |
| 1824 | 1745 |
| 1825 if (cpi->twopass.gf_group_bits < 0) | 1746 if (twopass->gf_group_bits < 0) |
| 1826 cpi->twopass.gf_group_bits = 0; | 1747 twopass->gf_group_bits = 0; |
| 1827 | 1748 |
| 1828 // This condition could fail if there are two kfs very close together | 1749 // This condition could fail if there are two kfs very close together |
| 1829 // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the | 1750 // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the |
| 1830 // calculation of alt_extra_bits. | 1751 // calculation of alt_extra_bits. |
| 1831 if (rc->baseline_gf_interval >= 3) { | 1752 if (rc->baseline_gf_interval >= 3) { |
| 1832 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; | 1753 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; |
| 1833 | 1754 |
| 1834 if (boost >= 150) { | 1755 if (boost >= 150) { |
| 1835 int alt_extra_bits; | 1756 const int pct_extra = MIN(20, (boost - 100) / 50); |
| 1836 int pct_extra = (boost - 100) / 50; | 1757 const int alt_extra_bits = (int)((twopass->gf_group_bits * pct_extra) / |
| 1837 pct_extra = (pct_extra > 20) ? 20 : pct_extra; | 1758 100); |
| 1838 | 1759 twopass->gf_group_bits -= alt_extra_bits; |
| 1839 alt_extra_bits = (int)((cpi->twopass.gf_group_bits * pct_extra) / 100); | |
| 1840 cpi->twopass.gf_group_bits -= alt_extra_bits; | |
| 1841 } | 1760 } |
| 1842 } | 1761 } |
| 1843 } | 1762 } |
| 1844 | 1763 |
| 1845 if (cpi->common.frame_type != KEY_FRAME) { | 1764 if (cpi->common.frame_type != KEY_FRAME) { |
| 1846 FIRSTPASS_STATS sectionstats; | 1765 FIRSTPASS_STATS sectionstats; |
| 1847 | 1766 |
| 1848 zero_stats(§ionstats); | 1767 zero_stats(§ionstats); |
| 1849 reset_fpf_position(&cpi->twopass, start_pos); | 1768 reset_fpf_position(twopass, start_pos); |
| 1850 | 1769 |
| 1851 for (i = 0; i < rc->baseline_gf_interval; i++) { | 1770 for (i = 0; i < rc->baseline_gf_interval; i++) { |
| 1852 input_stats(&cpi->twopass, &next_frame); | 1771 input_stats(twopass, &next_frame); |
| 1853 accumulate_stats(§ionstats, &next_frame); | 1772 accumulate_stats(§ionstats, &next_frame); |
| 1854 } | 1773 } |
| 1855 | 1774 |
| 1856 avg_stats(§ionstats); | 1775 avg_stats(§ionstats); |
| 1857 | 1776 |
| 1858 cpi->twopass.section_intra_rating = (int) | 1777 twopass->section_intra_rating = (int) |
| 1859 (sectionstats.intra_error / | 1778 (sectionstats.intra_error / |
| 1860 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 1779 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 1861 | 1780 |
| 1862 reset_fpf_position(&cpi->twopass, start_pos); | 1781 reset_fpf_position(twopass, start_pos); |
| 1863 } | 1782 } |
| 1864 } | 1783 } |
| 1865 | 1784 |
| 1866 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. | 1785 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. |
| 1867 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { | 1786 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 1868 int target_frame_size; | 1787 int target_frame_size; |
| 1869 double modified_err; | 1788 double modified_err; |
| 1870 double err_fraction; | 1789 double err_fraction; |
| 1871 const int max_bits = frame_max_bits(cpi); // Max for a single frame. | 1790 const int max_bits = frame_max_bits(cpi); // Max for a single frame. |
| 1872 | 1791 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1888 MIN(max_bits, (int)cpi->twopass.gf_group_bits)); | 1807 MIN(max_bits, (int)cpi->twopass.gf_group_bits)); |
| 1889 | 1808 |
| 1890 // Adjust error and bits remaining. | 1809 // Adjust error and bits remaining. |
| 1891 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; | 1810 cpi->twopass.gf_group_error_left -= (int64_t)modified_err; |
| 1892 cpi->twopass.gf_group_bits -= target_frame_size; | 1811 cpi->twopass.gf_group_bits -= target_frame_size; |
| 1893 | 1812 |
| 1894 if (cpi->twopass.gf_group_bits < 0) | 1813 if (cpi->twopass.gf_group_bits < 0) |
| 1895 cpi->twopass.gf_group_bits = 0; | 1814 cpi->twopass.gf_group_bits = 0; |
| 1896 | 1815 |
| 1897 // Per frame bit target for this frame. | 1816 // Per frame bit target for this frame. |
| 1898 cpi->rc.per_frame_bandwidth = target_frame_size; | 1817 vp9_rc_set_frame_target(cpi, target_frame_size); |
| 1899 } | |
| 1900 | |
| 1901 static int test_for_kf_one_pass(VP9_COMP *cpi) { | |
| 1902 // Placeholder function for auto key frame | |
| 1903 return 0; | |
| 1904 } | 1818 } |
| 1905 | 1819 |
| 1906 static int test_candidate_kf(VP9_COMP *cpi, | 1820 static int test_candidate_kf(VP9_COMP *cpi, |
| 1907 FIRSTPASS_STATS *last_frame, | 1821 const FIRSTPASS_STATS *last_frame, |
| 1908 FIRSTPASS_STATS *this_frame, | 1822 const FIRSTPASS_STATS *this_frame, |
| 1909 FIRSTPASS_STATS *next_frame) { | 1823 const FIRSTPASS_STATS *next_frame) { |
| 1910 int is_viable_kf = 0; | 1824 int is_viable_kf = 0; |
| 1911 | 1825 |
| 1912 // Does the frame satisfy the primary criteria of a key frame | 1826 // Does the frame satisfy the primary criteria of a key frame |
| 1913 // If so, then examine how well it predicts subsequent frames | 1827 // If so, then examine how well it predicts subsequent frames |
| 1914 if ((this_frame->pcnt_second_ref < 0.10) && | 1828 if ((this_frame->pcnt_second_ref < 0.10) && |
| 1915 (next_frame->pcnt_second_ref < 0.10) && | 1829 (next_frame->pcnt_second_ref < 0.10) && |
| 1916 ((this_frame->pcnt_inter < 0.05) || | 1830 ((this_frame->pcnt_inter < 0.05) || |
| 1917 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) && | 1831 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) && |
| 1918 ((this_frame->intra_error / | 1832 ((this_frame->intra_error / |
| 1919 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && | 1833 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1999 FIRSTPASS_STATS next_frame; | 1913 FIRSTPASS_STATS next_frame; |
| 2000 FIRSTPASS_STATS *start_position; | 1914 FIRSTPASS_STATS *start_position; |
| 2001 | 1915 |
| 2002 double decay_accumulator = 1.0; | 1916 double decay_accumulator = 1.0; |
| 2003 double zero_motion_accumulator = 1.0; | 1917 double zero_motion_accumulator = 1.0; |
| 2004 double boost_score = 0; | 1918 double boost_score = 0; |
| 2005 double loop_decay_rate; | 1919 double loop_decay_rate; |
| 2006 | 1920 |
| 2007 double kf_mod_err = 0.0; | 1921 double kf_mod_err = 0.0; |
| 2008 double kf_group_err = 0.0; | 1922 double kf_group_err = 0.0; |
| 2009 double kf_group_intra_err = 0.0; | |
| 2010 double kf_group_coded_err = 0.0; | |
| 2011 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; | 1923 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| 2012 | 1924 |
| 2013 RATE_CONTROL *const rc = &cpi->rc; | 1925 RATE_CONTROL *const rc = &cpi->rc; |
| 2014 struct twopass_rc *const twopass = &cpi->twopass; | 1926 struct twopass_rc *const twopass = &cpi->twopass; |
| 2015 | 1927 |
| 2016 vp9_zero(next_frame); | 1928 vp9_zero(next_frame); |
| 2017 | 1929 |
| 2018 vp9_clear_system_state(); // __asm emms; | 1930 vp9_clear_system_state(); // __asm emms; |
| 2019 | 1931 |
| 2020 start_position = twopass->stats_in; | 1932 start_position = twopass->stats_in; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2038 twopass->kf_group_error_left = 0; // Group modified error score. | 1950 twopass->kf_group_error_left = 0; // Group modified error score. |
| 2039 | 1951 |
| 2040 kf_mod_err = calculate_modified_err(cpi, this_frame); | 1952 kf_mod_err = calculate_modified_err(cpi, this_frame); |
| 2041 | 1953 |
| 2042 // find the next keyframe | 1954 // find the next keyframe |
| 2043 i = 0; | 1955 i = 0; |
| 2044 while (twopass->stats_in < twopass->stats_in_end) { | 1956 while (twopass->stats_in < twopass->stats_in_end) { |
| 2045 // Accumulate kf group error | 1957 // Accumulate kf group error |
| 2046 kf_group_err += calculate_modified_err(cpi, this_frame); | 1958 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 2047 | 1959 |
| 2048 // These figures keep intra and coded error counts for all frames including | |
| 2049 // key frames in the group. The effect of the key frame itself can be | |
| 2050 // subtracted out using the first_frame data collected above. | |
| 2051 kf_group_intra_err += this_frame->intra_error; | |
| 2052 kf_group_coded_err += this_frame->coded_error; | |
| 2053 | |
| 2054 // load a the next frame's stats | 1960 // load a the next frame's stats |
| 2055 last_frame = *this_frame; | 1961 last_frame = *this_frame; |
| 2056 input_stats(twopass, this_frame); | 1962 input_stats(twopass, this_frame); |
| 2057 | 1963 |
| 2058 // Provided that we are not at the end of the file... | 1964 // Provided that we are not at the end of the file... |
| 2059 if (cpi->oxcf.auto_key && | 1965 if (cpi->oxcf.auto_key && |
| 2060 lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 1966 lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 2061 // Normal scene cut check | 1967 // Normal scene cut check |
| 2062 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) | 1968 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) |
| 2063 break; | 1969 break; |
| 2064 | 1970 |
| 2065 | 1971 |
| 2066 // How fast is prediction quality decaying | 1972 // How fast is prediction quality decaying |
| 2067 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); | 1973 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 2068 | 1974 |
| 2069 // We want to know something about the recent past... rather than | 1975 // We want to know something about the recent past... rather than |
| 2070 // as used elsewhere where we are concened with decay in prediction | 1976 // as used elsewhere where we are concened with decay in prediction |
| 2071 // quality since the last GF or KF. | 1977 // quality since the last GF or KF. |
| 2072 recent_loop_decay[i % 8] = loop_decay_rate; | 1978 recent_loop_decay[i % 8] = loop_decay_rate; |
| 2073 decay_accumulator = 1.0; | 1979 decay_accumulator = 1.0; |
| 2074 for (j = 0; j < 8; j++) | 1980 for (j = 0; j < 8; j++) |
| 2075 decay_accumulator *= recent_loop_decay[j]; | 1981 decay_accumulator *= recent_loop_decay[j]; |
| 2076 | 1982 |
| 2077 // Special check for transition or high motion followed by a | 1983 // Special check for transition or high motion followed by a |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2103 | 2009 |
| 2104 rc->frames_to_key /= 2; | 2010 rc->frames_to_key /= 2; |
| 2105 | 2011 |
| 2106 // Copy first frame details | 2012 // Copy first frame details |
| 2107 tmp_frame = first_frame; | 2013 tmp_frame = first_frame; |
| 2108 | 2014 |
| 2109 // Reset to the start of the group | 2015 // Reset to the start of the group |
| 2110 reset_fpf_position(twopass, start_position); | 2016 reset_fpf_position(twopass, start_position); |
| 2111 | 2017 |
| 2112 kf_group_err = 0; | 2018 kf_group_err = 0; |
| 2113 kf_group_intra_err = 0; | |
| 2114 kf_group_coded_err = 0; | |
| 2115 | 2019 |
| 2116 // Rescan to get the correct error data for the forced kf group | 2020 // Rescan to get the correct error data for the forced kf group |
| 2117 for (i = 0; i < rc->frames_to_key; i++) { | 2021 for (i = 0; i < rc->frames_to_key; i++) { |
| 2118 // Accumulate kf group errors | 2022 // Accumulate kf group errors |
| 2119 kf_group_err += calculate_modified_err(cpi, &tmp_frame); | 2023 kf_group_err += calculate_modified_err(cpi, &tmp_frame); |
| 2120 kf_group_intra_err += tmp_frame.intra_error; | |
| 2121 kf_group_coded_err += tmp_frame.coded_error; | |
| 2122 | 2024 |
| 2123 // Load a the next frame's stats | 2025 // Load the next frame's stats. |
| 2124 input_stats(twopass, &tmp_frame); | 2026 input_stats(twopass, &tmp_frame); |
| 2125 } | 2027 } |
| 2126 rc->next_key_frame_forced = 1; | 2028 rc->next_key_frame_forced = 1; |
| 2127 } else if (twopass->stats_in == twopass->stats_in_end) { | 2029 } else if (twopass->stats_in == twopass->stats_in_end) { |
| 2128 rc->next_key_frame_forced = 1; | 2030 rc->next_key_frame_forced = 1; |
| 2129 } else { | 2031 } else { |
| 2130 rc->next_key_frame_forced = 0; | 2032 rc->next_key_frame_forced = 0; |
| 2131 } | 2033 } |
| 2132 | 2034 |
| 2133 // Special case for the last key frame of the file | 2035 // Special case for the last key frame of the file |
| 2134 if (twopass->stats_in >= twopass->stats_in_end) { | 2036 if (twopass->stats_in >= twopass->stats_in_end) { |
| 2135 // Accumulate kf group error | 2037 // Accumulate kf group error |
| 2136 kf_group_err += calculate_modified_err(cpi, this_frame); | 2038 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 2137 | |
| 2138 // These figures keep intra and coded error counts for all frames including | |
| 2139 // key frames in the group. The effect of the key frame itself can be | |
| 2140 // subtracted out using the first_frame data collected above. | |
| 2141 kf_group_intra_err += this_frame->intra_error; | |
| 2142 kf_group_coded_err += this_frame->coded_error; | |
| 2143 } | 2039 } |
| 2144 | 2040 |
| 2145 // Calculate the number of bits that should be assigned to the kf group. | 2041 // Calculate the number of bits that should be assigned to the kf group. |
| 2146 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { | 2042 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { |
| 2147 // Max for a single normal frame (not key frame) | 2043 // Max for a single normal frame (not key frame) |
| 2148 int max_bits = frame_max_bits(cpi); | 2044 int max_bits = frame_max_bits(cpi); |
| 2149 | 2045 |
| 2150 // Maximum bits for the kf group | 2046 // Maximum bits for the kf group |
| 2151 int64_t max_grp_bits; | 2047 int64_t max_grp_bits; |
| 2152 | 2048 |
| 2153 // Default allocation based on bits left and relative | 2049 // Default allocation based on bits left and relative |
| 2154 // complexity of the section | 2050 // complexity of the section |
| 2155 twopass->kf_group_bits = (int64_t)(twopass->bits_left * | 2051 twopass->kf_group_bits = (int64_t)(twopass->bits_left * |
| 2156 (kf_group_err / twopass->modified_error_left)); | 2052 (kf_group_err / twopass->modified_error_left)); |
| 2157 | 2053 |
| 2158 // Clip based on maximum per frame rate defined by the user. | 2054 // Clip based on maximum per frame rate defined by the user. |
| 2159 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; | 2055 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
| 2160 if (twopass->kf_group_bits > max_grp_bits) | 2056 if (twopass->kf_group_bits > max_grp_bits) |
| 2161 twopass->kf_group_bits = max_grp_bits; | 2057 twopass->kf_group_bits = max_grp_bits; |
| 2162 } else { | 2058 } else { |
| 2163 twopass->kf_group_bits = 0; | 2059 twopass->kf_group_bits = 0; |
| 2164 } | 2060 } |
| 2165 // Reset the first pass file position | 2061 // Reset the first pass file position |
| 2166 reset_fpf_position(twopass, start_position); | 2062 reset_fpf_position(twopass, start_position); |
| 2167 | 2063 |
| 2168 // Determine how big to make this keyframe based on how well the subsequent | 2064 // Determine how big to make this keyframe based on how well the subsequent |
| 2169 // frames use inter blocks. | 2065 // frames use inter blocks. |
| 2170 decay_accumulator = 1.0; | 2066 decay_accumulator = 1.0; |
| 2171 boost_score = 0.0; | 2067 boost_score = 0.0; |
| 2172 loop_decay_rate = 1.00; // Starting decay rate | |
| 2173 | 2068 |
| 2174 // Scan through the kf group collating various stats. | 2069 // Scan through the kf group collating various stats. |
| 2175 for (i = 0; i < rc->frames_to_key; i++) { | 2070 for (i = 0; i < rc->frames_to_key; i++) { |
| 2176 double r; | 2071 double r; |
| 2177 | 2072 |
| 2178 if (EOF == input_stats(twopass, &next_frame)) | 2073 if (EOF == input_stats(twopass, &next_frame)) |
| 2179 break; | 2074 break; |
| 2180 | 2075 |
| 2181 // Monitor for static sections. | 2076 // Monitor for static sections. |
| 2182 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 2077 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 2183 zero_motion_accumulator) { | 2078 zero_motion_accumulator) { |
| 2184 zero_motion_accumulator = | 2079 zero_motion_accumulator = |
| 2185 (next_frame.pcnt_inter - next_frame.pcnt_motion); | 2080 (next_frame.pcnt_inter - next_frame.pcnt_motion); |
| 2186 } | 2081 } |
| 2187 | 2082 |
| 2188 // For the first few frames collect data to decide kf boost. | 2083 // For the first few frames collect data to decide kf boost. |
| 2189 if (i <= (rc->max_gf_interval * 2)) { | 2084 if (i <= (rc->max_gf_interval * 2)) { |
| 2190 if (next_frame.intra_error > twopass->kf_intra_err_min) | 2085 if (next_frame.intra_error > twopass->kf_intra_err_min) |
| 2191 r = (IIKFACTOR2 * next_frame.intra_error / | 2086 r = (IIKFACTOR2 * next_frame.intra_error / |
| 2192 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2087 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2193 else | 2088 else |
| 2194 r = (IIKFACTOR2 * twopass->kf_intra_err_min / | 2089 r = (IIKFACTOR2 * twopass->kf_intra_err_min / |
| 2195 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2090 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2196 | 2091 |
| 2197 if (r > RMAX) | 2092 if (r > RMAX) |
| 2198 r = RMAX; | 2093 r = RMAX; |
| 2199 | 2094 |
| 2200 // How fast is prediction quality decaying | 2095 // How fast is prediction quality decaying |
| 2201 if (!detect_flash(cpi, 0)) { | 2096 if (!detect_flash(twopass, 0)) { |
| 2202 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); | 2097 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 2203 decay_accumulator *= loop_decay_rate; | 2098 decay_accumulator *= loop_decay_rate; |
| 2204 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 2099 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 2205 ? MIN_DECAY_FACTOR : decay_accumulator; | 2100 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 2206 } | 2101 } |
| 2207 | 2102 |
| 2208 boost_score += (decay_accumulator * r); | 2103 boost_score += (decay_accumulator * r); |
| 2209 } | 2104 } |
| 2210 } | 2105 } |
| 2211 | 2106 |
| 2212 { | 2107 { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2231 | 2126 |
| 2232 // Work out how many bits to allocate for the key frame itself | 2127 // Work out how many bits to allocate for the key frame itself |
| 2233 if (1) { | 2128 if (1) { |
| 2234 int kf_boost = (int)boost_score; | 2129 int kf_boost = (int)boost_score; |
| 2235 int allocation_chunks; | 2130 int allocation_chunks; |
| 2236 int alt_kf_bits; | 2131 int alt_kf_bits; |
| 2237 | 2132 |
| 2238 if (kf_boost < (rc->frames_to_key * 3)) | 2133 if (kf_boost < (rc->frames_to_key * 3)) |
| 2239 kf_boost = (rc->frames_to_key * 3); | 2134 kf_boost = (rc->frames_to_key * 3); |
| 2240 | 2135 |
| 2241 if (kf_boost < 300) // Min KF boost | 2136 if (kf_boost < MIN_KF_BOOST) |
| 2242 kf_boost = 300; | 2137 kf_boost = MIN_KF_BOOST; |
| 2243 | 2138 |
| 2244 // Make a note of baseline boost and the zero motion | 2139 // Make a note of baseline boost and the zero motion |
| 2245 // accumulator value for use elsewhere. | 2140 // accumulator value for use elsewhere. |
| 2246 rc->kf_boost = kf_boost; | 2141 rc->kf_boost = kf_boost; |
| 2247 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | 2142 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2248 | 2143 |
| 2249 // We do three calculations for kf size. | 2144 // We do three calculations for kf size. |
| 2250 // The first is based on the error score for the whole kf group. | 2145 // The first is based on the error score for the whole kf group. |
| 2251 // The second (optionaly) on the key frames own error if this is | 2146 // The second (optionally) on the key frames own error if this is |
| 2252 // smaller than the average for the group. | 2147 // smaller than the average for the group. |
| 2253 // The final one insures that the frame receives at least the | 2148 // The final one insures that the frame receives at least the |
| 2254 // allocation it would have received based on its own error score vs | 2149 // allocation it would have received based on its own error score vs |
| 2255 // the error score remaining | 2150 // the error score remaining |
| 2256 // Special case if the sequence appears almost totaly static | 2151 // Special case if the sequence appears almost totaly static |
| 2257 // In this case we want to spend almost all of the bits on the | 2152 // In this case we want to spend almost all of the bits on the |
| 2258 // key frame. | 2153 // key frame. |
| 2259 // cpi->rc.frames_to_key-1 because key frame itself is taken | 2154 // cpi->rc.frames_to_key-1 because key frame itself is taken |
| 2260 // care of by kf_boost. | 2155 // care of by kf_boost. |
| 2261 if (zero_motion_accumulator >= 0.99) { | 2156 if (zero_motion_accumulator >= 0.99) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2272 } | 2167 } |
| 2273 | 2168 |
| 2274 twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0 | 2169 twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0 |
| 2275 : twopass->kf_group_bits; | 2170 : twopass->kf_group_bits; |
| 2276 | 2171 |
| 2277 // Calculate the number of bits to be spent on the key frame | 2172 // Calculate the number of bits to be spent on the key frame |
| 2278 twopass->kf_bits = (int)((double)kf_boost * | 2173 twopass->kf_bits = (int)((double)kf_boost * |
| 2279 ((double)twopass->kf_group_bits / allocation_chunks)); | 2174 ((double)twopass->kf_group_bits / allocation_chunks)); |
| 2280 | 2175 |
| 2281 // If the key frame is actually easier than the average for the | 2176 // If the key frame is actually easier than the average for the |
| 2282 // kf group (which does sometimes happen... eg a blank intro frame) | 2177 // kf group (which does sometimes happen, e.g. a blank intro frame) |
| 2283 // Then use an alternate calculation based on the kf error score | 2178 // then use an alternate calculation based on the kf error score |
| 2284 // which should give a smaller key frame. | 2179 // which should give a smaller key frame. |
| 2285 if (kf_mod_err < kf_group_err / rc->frames_to_key) { | 2180 if (kf_mod_err < kf_group_err / rc->frames_to_key) { |
| 2286 double alt_kf_grp_bits = ((double)twopass->bits_left * | 2181 double alt_kf_grp_bits = ((double)twopass->bits_left * |
| 2287 (kf_mod_err * (double)rc->frames_to_key) / | 2182 (kf_mod_err * (double)rc->frames_to_key) / |
| 2288 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); | 2183 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); |
| 2289 | 2184 |
| 2290 alt_kf_bits = (int)((double)kf_boost * | 2185 alt_kf_bits = (int)((double)kf_boost * |
| 2291 (alt_kf_grp_bits / (double)allocation_chunks)); | 2186 (alt_kf_grp_bits / (double)allocation_chunks)); |
| 2292 | 2187 |
| 2293 if (twopass->kf_bits > alt_kf_bits) | 2188 if (twopass->kf_bits > alt_kf_bits) |
| 2294 twopass->kf_bits = alt_kf_bits; | 2189 twopass->kf_bits = alt_kf_bits; |
| 2295 } else { | 2190 } else { |
| 2296 // Else if it is much harder than other frames in the group make sure | 2191 // Else if it is much harder than other frames in the group make sure |
| 2297 // it at least receives an allocation in keeping with its relative | 2192 // it at least receives an allocation in keeping with its relative |
| 2298 // error score | 2193 // error score |
| 2299 alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / | 2194 alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / |
| 2300 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); | 2195 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); |
| 2301 | 2196 |
| 2302 if (alt_kf_bits > twopass->kf_bits) { | 2197 if (alt_kf_bits > twopass->kf_bits) { |
| 2303 twopass->kf_bits = alt_kf_bits; | 2198 twopass->kf_bits = alt_kf_bits; |
| 2304 } | 2199 } |
| 2305 } | 2200 } |
| 2306 | |
| 2307 twopass->kf_group_bits -= twopass->kf_bits; | 2201 twopass->kf_group_bits -= twopass->kf_bits; |
| 2308 | 2202 // Per frame bit target for this frame. |
| 2309 // Peer frame bit target for this frame | 2203 vp9_rc_set_frame_target(cpi, twopass->kf_bits); |
| 2310 rc->per_frame_bandwidth = twopass->kf_bits; | |
| 2311 // Convert to a per second bitrate | |
| 2312 cpi->target_bandwidth = (int)(twopass->kf_bits * cpi->output_framerate); | |
| 2313 } | 2204 } |
| 2314 | 2205 |
| 2315 // Note the total error score of the kf group minus the key frame itself | 2206 // Note the total error score of the kf group minus the key frame itself |
| 2316 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2207 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2317 | 2208 |
| 2318 // Adjust the count of total modified error left. | 2209 // Adjust the count of total modified error left. |
| 2319 // The count of bits left is adjusted elsewhere based on real coded frame | 2210 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2320 // sizes. | 2211 // sizes. |
| 2321 twopass->modified_error_left -= kf_group_err; | 2212 twopass->modified_error_left -= kf_group_err; |
| 2322 } | 2213 } |
| 2323 | 2214 |
| 2324 void vp9_get_svc_params(VP9_COMP *cpi) { | 2215 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { |
| 2325 VP9_COMMON *const cm = &cpi->common; | |
| 2326 if ((cm->current_video_frame == 0) || | |
| 2327 (cm->frame_flags & FRAMEFLAGS_KEY) || | |
| 2328 (cpi->oxcf.auto_key && (cpi->rc.frames_since_key % | |
| 2329 cpi->key_frame_frequency == 0))) { | |
| 2330 cm->frame_type = KEY_FRAME; | |
| 2331 } else { | |
| 2332 cm->frame_type = INTER_FRAME; | |
| 2333 } | |
| 2334 cpi->rc.frames_till_gf_update_due = INT_MAX; | |
| 2335 cpi->rc.baseline_gf_interval = INT_MAX; | |
| 2336 } | |
| 2337 | |
| 2338 void vp9_get_one_pass_params(VP9_COMP *cpi) { | |
| 2339 VP9_COMMON *const cm = &cpi->common; | |
| 2340 if (!cpi->refresh_alt_ref_frame && | |
| 2341 (cm->current_video_frame == 0 || | |
| 2342 cm->frame_flags & FRAMEFLAGS_KEY || | |
| 2343 cpi->rc.frames_to_key == 0 || | |
| 2344 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { | |
| 2345 cm->frame_type = KEY_FRAME; | |
| 2346 cpi->rc.this_key_frame_forced = cm->current_video_frame != 0 && | |
| 2347 cpi->rc.frames_to_key == 0; | |
| 2348 cpi->rc.frames_to_key = cpi->key_frame_frequency; | |
| 2349 cpi->rc.kf_boost = 300; | |
| 2350 } else { | |
| 2351 cm->frame_type = INTER_FRAME; | |
| 2352 } | |
| 2353 if (cpi->rc.frames_till_gf_update_due == 0) { | |
| 2354 cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval; | |
| 2355 cpi->refresh_golden_frame = 1; | |
| 2356 } | |
| 2357 } | |
| 2358 | |
| 2359 void vp9_get_one_pass_cbr_params(VP9_COMP *cpi) { | |
| 2360 VP9_COMMON *const cm = &cpi->common; | |
| 2361 if ((cm->current_video_frame == 0 || | |
| 2362 cm->frame_flags & FRAMEFLAGS_KEY || | |
| 2363 cpi->rc.frames_to_key == 0 || | |
| 2364 (cpi->oxcf.auto_key && test_for_kf_one_pass(cpi)))) { | |
| 2365 cm->frame_type = KEY_FRAME; | |
| 2366 cpi->rc.this_key_frame_forced = cm->current_video_frame != 0 && | |
| 2367 cpi->rc.frames_to_key == 0; | |
| 2368 cpi->rc.frames_to_key = cpi->key_frame_frequency; | |
| 2369 cpi->rc.kf_boost = 300; | |
| 2370 } else { | |
| 2371 cm->frame_type = INTER_FRAME; | |
| 2372 } | |
| 2373 // Don't use gf_update by default in CBR mode. | |
| 2374 cpi->rc.frames_till_gf_update_due = INT_MAX; | |
| 2375 cpi->rc.baseline_gf_interval = INT_MAX; | |
| 2376 } | |
| 2377 | |
| 2378 void vp9_get_first_pass_params(VP9_COMP *cpi) { | |
| 2379 VP9_COMMON *const cm = &cpi->common; | 2216 VP9_COMMON *const cm = &cpi->common; |
| 2380 if (!cpi->refresh_alt_ref_frame && | 2217 if (!cpi->refresh_alt_ref_frame && |
| 2381 (cm->current_video_frame == 0 || | 2218 (cm->current_video_frame == 0 || |
| 2382 cm->frame_flags & FRAMEFLAGS_KEY)) { | 2219 cm->frame_flags & FRAMEFLAGS_KEY)) { |
| 2383 cm->frame_type = KEY_FRAME; | 2220 cm->frame_type = KEY_FRAME; |
| 2384 } else { | 2221 } else { |
| 2385 cm->frame_type = INTER_FRAME; | 2222 cm->frame_type = INTER_FRAME; |
| 2386 } | 2223 } |
| 2387 // Do not use periodic key frames | 2224 // Do not use periodic key frames |
| 2388 cpi->rc.frames_to_key = INT_MAX; | 2225 cpi->rc.frames_to_key = INT_MAX; |
| 2389 } | 2226 } |
| 2390 | 2227 |
| 2391 void vp9_get_second_pass_params(VP9_COMP *cpi) { | 2228 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2392 int tmp_q; | 2229 VP9_COMMON *const cm = &cpi->common; |
| 2393 int frames_left = (int)(cpi->twopass.total_stats.count - | 2230 RATE_CONTROL *const rc = &cpi->rc; |
| 2394 cpi->common.current_video_frame); | 2231 struct twopass_rc *const twopass = &cpi->twopass; |
| 2395 | 2232 const int frames_left = (int)(twopass->total_stats.count - |
| 2233 cm->current_video_frame); |
| 2396 FIRSTPASS_STATS this_frame; | 2234 FIRSTPASS_STATS this_frame; |
| 2397 FIRSTPASS_STATS this_frame_copy; | 2235 FIRSTPASS_STATS this_frame_copy; |
| 2398 RATE_CONTROL *rc = &cpi->rc; | |
| 2399 | 2236 |
| 2400 double this_frame_intra_error; | 2237 double this_frame_intra_error; |
| 2401 double this_frame_coded_error; | 2238 double this_frame_coded_error; |
| 2239 int target; |
| 2240 |
| 2241 if (!twopass->stats_in) |
| 2242 return; |
| 2402 | 2243 |
| 2403 if (cpi->refresh_alt_ref_frame) { | 2244 if (cpi->refresh_alt_ref_frame) { |
| 2404 cpi->common.frame_type = INTER_FRAME; | 2245 cm->frame_type = INTER_FRAME; |
| 2246 vp9_rc_set_frame_target(cpi, twopass->gf_bits); |
| 2405 return; | 2247 return; |
| 2406 } | 2248 } |
| 2407 if (!cpi->twopass.stats_in) | |
| 2408 return; | |
| 2409 | 2249 |
| 2410 vp9_clear_system_state(); | 2250 vp9_clear_system_state(); |
| 2411 | 2251 |
| 2412 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { | 2252 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
| 2413 rc->active_worst_quality = cpi->oxcf.cq_level; | 2253 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2414 } else if (cpi->common.current_video_frame == 0) { | 2254 } else if (cm->current_video_frame == 0) { |
| 2415 // Special case code for first frame. | 2255 // Special case code for first frame. |
| 2416 int section_target_bandwidth = | 2256 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2417 (int)(cpi->twopass.bits_left / frames_left); | 2257 frames_left); |
| 2418 | 2258 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2419 tmp_q = estimate_max_q(cpi, &cpi->twopass.total_left_stats, | 2259 section_target_bandwidth); |
| 2420 section_target_bandwidth); | 2260 twopass->active_worst_quality = tmp_q; |
| 2421 | |
| 2422 rc->active_worst_quality = tmp_q; | |
| 2423 rc->ni_av_qi = tmp_q; | 2261 rc->ni_av_qi = tmp_q; |
| 2424 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2262 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2425 | 2263 |
| 2426 // Limit the maxq value returned subsequently. | 2264 // Limit the maxq value returned subsequently. |
| 2427 // This increases the risk of overspend or underspend if the initial | 2265 // This increases the risk of overspend or underspend if the initial |
| 2428 // estimate for the clip is bad, but helps prevent excessive | 2266 // estimate for the clip is bad, but helps prevent excessive |
| 2429 // variation in Q, especially near the end of a clip | 2267 // variation in Q, especially near the end of a clip |
| 2430 // where for example a small overspend may cause Q to crash | 2268 // where for example a small overspend may cause Q to crash |
| 2431 // adjust_maxq_qrange(cpi); | 2269 // adjust_maxq_qrange(cpi); |
| 2432 } | 2270 } |
| 2433 vp9_zero(this_frame); | 2271 vp9_zero(this_frame); |
| 2434 if (EOF == input_stats(&cpi->twopass, &this_frame)) | 2272 if (EOF == input_stats(twopass, &this_frame)) |
| 2435 return; | 2273 return; |
| 2436 | 2274 |
| 2437 this_frame_intra_error = this_frame.intra_error; | 2275 this_frame_intra_error = this_frame.intra_error; |
| 2438 this_frame_coded_error = this_frame.coded_error; | 2276 this_frame_coded_error = this_frame.coded_error; |
| 2439 | 2277 |
| 2440 // keyframe and section processing ! | 2278 // keyframe and section processing ! |
| 2441 if (rc->frames_to_key == 0 || | 2279 if (rc->frames_to_key == 0 || |
| 2442 (cpi->common.frame_flags & FRAMEFLAGS_KEY)) { | 2280 (cm->frame_flags & FRAMEFLAGS_KEY)) { |
| 2443 // Define next KF group and assign bits to it | 2281 // Define next KF group and assign bits to it |
| 2444 this_frame_copy = this_frame; | 2282 this_frame_copy = this_frame; |
| 2445 find_next_key_frame(cpi, &this_frame_copy); | 2283 find_next_key_frame(cpi, &this_frame_copy); |
| 2446 } else { | 2284 } else { |
| 2447 cpi->common.frame_type = INTER_FRAME; | 2285 cm->frame_type = INTER_FRAME; |
| 2448 } | 2286 } |
| 2449 | 2287 |
| 2450 // Is this a GF / ARF (Note that a KF is always also a GF) | 2288 // Is this a GF / ARF (Note that a KF is always also a GF) |
| 2451 if (rc->frames_till_gf_update_due == 0) { | 2289 if (rc->frames_till_gf_update_due == 0) { |
| 2452 // Define next gf group and assign bits to it | 2290 // Define next gf group and assign bits to it |
| 2453 this_frame_copy = this_frame; | 2291 this_frame_copy = this_frame; |
| 2454 | 2292 |
| 2455 #if CONFIG_MULTIPLE_ARF | 2293 #if CONFIG_MULTIPLE_ARF |
| 2456 if (cpi->multi_arf_enabled) { | 2294 if (cpi->multi_arf_enabled) { |
| 2457 define_fixed_arf_period(cpi); | 2295 define_fixed_arf_period(cpi); |
| 2458 } else { | 2296 } else { |
| 2459 #endif | 2297 #endif |
| 2460 define_gf_group(cpi, &this_frame_copy); | 2298 define_gf_group(cpi, &this_frame_copy); |
| 2461 #if CONFIG_MULTIPLE_ARF | 2299 #if CONFIG_MULTIPLE_ARF |
| 2462 } | 2300 } |
| 2463 #endif | 2301 #endif |
| 2464 | 2302 |
| 2465 if (cpi->twopass.gf_zeromotion_pct > 995) { | 2303 if (twopass->gf_zeromotion_pct > 995) { |
| 2466 // As long as max_thresh for encode breakout is small enough, it is ok | 2304 // As long as max_thresh for encode breakout is small enough, it is ok |
| 2467 // to enable it for no-show frame, i.e. set enable_encode_breakout to 2. | 2305 // to enable it for show frame, i.e. set allow_encode_breakout to 2. |
| 2468 if (!cpi->common.show_frame) | 2306 if (!cm->show_frame) |
| 2469 cpi->enable_encode_breakout = 0; | 2307 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; |
| 2470 else | 2308 else |
| 2471 cpi->enable_encode_breakout = 2; | 2309 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; |
| 2472 } | 2310 } |
| 2473 | 2311 |
| 2474 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2312 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2475 cpi->refresh_golden_frame = 1; | 2313 cpi->refresh_golden_frame = 1; |
| 2476 } else { | 2314 } else { |
| 2477 // Otherwise this is an ordinary frame | 2315 // Otherwise this is an ordinary frame |
| 2478 // Assign bits from those allocated to the GF group | 2316 // Assign bits from those allocated to the GF group |
| 2479 this_frame_copy = this_frame; | 2317 this_frame_copy = this_frame; |
| 2480 assign_std_frame_bits(cpi, &this_frame_copy); | 2318 assign_std_frame_bits(cpi, &this_frame_copy); |
| 2481 } | 2319 } |
| 2482 | 2320 |
| 2483 // Keep a globally available copy of this and the next frame's iiratio. | 2321 // Keep a globally available copy of this and the next frame's iiratio. |
| 2484 cpi->twopass.this_iiratio = (int)(this_frame_intra_error / | 2322 twopass->this_iiratio = (int)(this_frame_intra_error / |
| 2485 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); | 2323 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); |
| 2486 { | 2324 { |
| 2487 FIRSTPASS_STATS next_frame; | 2325 FIRSTPASS_STATS next_frame; |
| 2488 if (lookup_next_frame_stats(&cpi->twopass, &next_frame) != EOF) { | 2326 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 2489 cpi->twopass.next_iiratio = (int)(next_frame.intra_error / | 2327 twopass->next_iiratio = (int)(next_frame.intra_error / |
| 2490 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2328 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2491 } | 2329 } |
| 2492 } | 2330 } |
| 2493 | 2331 |
| 2494 // Set nominal per second bandwidth for this frame | 2332 if (cpi->common.frame_type == KEY_FRAME) |
| 2495 cpi->target_bandwidth = (int)(rc->per_frame_bandwidth * | 2333 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); |
| 2496 cpi->output_framerate); | 2334 else |
| 2497 if (cpi->target_bandwidth < 0) | 2335 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); |
| 2498 cpi->target_bandwidth = 0; | 2336 vp9_rc_set_frame_target(cpi, target); |
| 2499 | 2337 |
| 2500 // Update the total stats remaining structure | 2338 // Update the total stats remaining structure |
| 2501 subtract_stats(&cpi->twopass.total_left_stats, &this_frame); | 2339 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2502 } | 2340 } |
| 2503 | 2341 |
| 2504 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { | 2342 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
| 2505 #ifdef DISABLE_RC_LONG_TERM_MEM | 2343 #ifdef DISABLE_RC_LONG_TERM_MEM |
| 2506 cpi->twopass.bits_left -= cpi->rc.this_frame_target; | 2344 cpi->twopass.bits_left -= cpi->rc.this_frame_target; |
| 2507 #else | 2345 #else |
| 2508 cpi->twopass.bits_left -= 8 * bytes_used; | 2346 cpi->twopass.bits_left -= 8 * bytes_used; |
| 2347 // Update bits left to the kf and gf groups to account for overshoot or |
| 2348 // undershoot on these frames |
| 2349 if (cm->frame_type == KEY_FRAME) { |
| 2350 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - |
| 2351 cpi->rc.projected_frame_size; |
| 2352 |
| 2353 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
| 2354 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { |
| 2355 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - |
| 2356 cpi->rc.projected_frame_size; |
| 2357 |
| 2358 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
| 2359 } |
| 2509 #endif | 2360 #endif |
| 2510 } | 2361 } |
| OLD | NEW |