| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <limits.h> |
| 11 #include <math.h> | 12 #include <math.h> |
| 12 #include <limits.h> | |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 |
| 15 #include "./vpx_scale_rtcd.h" |
| 16 |
| 17 #include "vpx_mem/vpx_mem.h" |
| 18 #include "vpx_scale/vpx_scale.h" |
| 19 #include "vpx_scale/yv12config.h" |
| 20 |
| 21 #include "vp9/common/vp9_entropymv.h" |
| 22 #include "vp9/common/vp9_quant_common.h" |
| 23 #include "vp9/common/vp9_reconinter.h" // setup_dst_planes() |
| 14 #include "vp9/common/vp9_systemdependent.h" | 24 #include "vp9/common/vp9_systemdependent.h" |
| 25 |
| 15 #include "vp9/encoder/vp9_block.h" | 26 #include "vp9/encoder/vp9_block.h" |
| 16 #include "vp9/encoder/vp9_encodeframe.h" | 27 #include "vp9/encoder/vp9_encodeframe.h" |
| 17 #include "vp9/encoder/vp9_encodemb.h" | 28 #include "vp9/encoder/vp9_encodemb.h" |
| 29 #include "vp9/encoder/vp9_encodemv.h" |
| 18 #include "vp9/encoder/vp9_extend.h" | 30 #include "vp9/encoder/vp9_extend.h" |
| 19 #include "vp9/encoder/vp9_firstpass.h" | 31 #include "vp9/encoder/vp9_firstpass.h" |
| 20 #include "vp9/encoder/vp9_mcomp.h" | 32 #include "vp9/encoder/vp9_mcomp.h" |
| 21 #include "vp9/encoder/vp9_onyx_int.h" | 33 #include "vp9/encoder/vp9_onyx_int.h" |
| 34 #include "vp9/encoder/vp9_quantize.h" |
| 35 #include "vp9/encoder/vp9_ratectrl.h" |
| 36 #include "vp9/encoder/vp9_rdopt.h" |
| 37 #include "vp9/encoder/vp9_vaq.h" |
| 22 #include "vp9/encoder/vp9_variance.h" | 38 #include "vp9/encoder/vp9_variance.h" |
| 23 #include "vpx_scale/vpx_scale.h" | |
| 24 #include "vpx_mem/vpx_mem.h" | |
| 25 #include "vpx_scale/yv12config.h" | |
| 26 #include "vp9/encoder/vp9_quantize.h" | |
| 27 #include "vp9/encoder/vp9_rdopt.h" | |
| 28 #include "vp9/encoder/vp9_ratectrl.h" | |
| 29 #include "vp9/common/vp9_quant_common.h" | |
| 30 #include "vp9/common/vp9_entropymv.h" | |
| 31 #include "vp9/encoder/vp9_encodemv.h" | |
| 32 #include "vp9/encoder/vp9_vaq.h" | |
| 33 #include "./vpx_scale_rtcd.h" | |
| 34 // TODO(jkoleszar): for setup_dst_planes | |
| 35 #include "vp9/common/vp9_reconinter.h" | |
| 36 | 39 |
| 37 #define OUTPUT_FPF 0 | 40 #define OUTPUT_FPF 0 |
| 38 | 41 |
| 39 #define IIFACTOR 12.5 | 42 #define IIFACTOR 12.5 |
| 40 #define IIKFACTOR1 12.5 | 43 #define IIKFACTOR1 12.5 |
| 41 #define IIKFACTOR2 15.0 | 44 #define IIKFACTOR2 15.0 |
| 42 #define RMAX 512.0 | 45 #define RMAX 512.0 |
| 43 #define GF_RMAX 96.0 | 46 #define GF_RMAX 96.0 |
| 44 #define ERR_DIVISOR 150.0 | 47 #define ERR_DIVISOR 150.0 |
| 45 #define MIN_DECAY_FACTOR 0.1 | 48 #define MIN_DECAY_FACTOR 0.1 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 58 *a = *b; | 61 *a = *b; |
| 59 *b = temp; | 62 *b = temp; |
| 60 } | 63 } |
| 61 | 64 |
| 62 static int select_cq_level(int qindex) { | 65 static int select_cq_level(int qindex) { |
| 63 int ret_val = QINDEX_RANGE - 1; | 66 int ret_val = QINDEX_RANGE - 1; |
| 64 int i; | 67 int i; |
| 65 | 68 |
| 66 double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0; | 69 double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0; |
| 67 | 70 |
| 68 for (i = 0; i < QINDEX_RANGE; i++) { | 71 for (i = 0; i < QINDEX_RANGE; ++i) { |
| 69 if (target_q <= vp9_convert_qindex_to_q(i)) { | 72 if (target_q <= vp9_convert_qindex_to_q(i)) { |
| 70 ret_val = i; | 73 ret_val = i; |
| 71 break; | 74 break; |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 | 77 |
| 75 return ret_val; | 78 return ret_val; |
| 76 } | 79 } |
| 77 | 80 |
| 78 static int gfboost_qadjust(int qindex) { | 81 static int gfboost_qadjust(int qindex) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 99 static int lookup_next_frame_stats(const struct twopass_rc *p, | 102 static int lookup_next_frame_stats(const struct twopass_rc *p, |
| 100 FIRSTPASS_STATS *next_frame) { | 103 FIRSTPASS_STATS *next_frame) { |
| 101 if (p->stats_in >= p->stats_in_end) | 104 if (p->stats_in >= p->stats_in_end) |
| 102 return EOF; | 105 return EOF; |
| 103 | 106 |
| 104 *next_frame = *p->stats_in; | 107 *next_frame = *p->stats_in; |
| 105 return 1; | 108 return 1; |
| 106 } | 109 } |
| 107 | 110 |
| 108 | 111 |
| 109 // Read frame stats at an offset from the current position | 112 // Read frame stats at an offset from the current position. |
| 110 static int read_frame_stats(const struct twopass_rc *p, | 113 static int read_frame_stats(const struct twopass_rc *p, |
| 111 FIRSTPASS_STATS *frame_stats, int offset) { | 114 FIRSTPASS_STATS *frame_stats, int offset) { |
| 112 const FIRSTPASS_STATS *fps_ptr = p->stats_in; | 115 const FIRSTPASS_STATS *fps_ptr = p->stats_in; |
| 113 | 116 |
| 114 // Check legality of offset | 117 // Check legality of offset. |
| 115 if (offset >= 0) { | 118 if (offset >= 0) { |
| 116 if (&fps_ptr[offset] >= p->stats_in_end) | 119 if (&fps_ptr[offset] >= p->stats_in_end) |
| 117 return EOF; | 120 return EOF; |
| 118 } else if (offset < 0) { | 121 } else if (offset < 0) { |
| 119 if (&fps_ptr[offset] < p->stats_in_start) | 122 if (&fps_ptr[offset] < p->stats_in_start) |
| 120 return EOF; | 123 return EOF; |
| 121 } | 124 } |
| 122 | 125 |
| 123 *frame_stats = fps_ptr[offset]; | 126 *frame_stats = fps_ptr[offset]; |
| 124 return 1; | 127 return 1; |
| 125 } | 128 } |
| 126 | 129 |
| 127 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) { | 130 static int input_stats(struct twopass_rc *p, FIRSTPASS_STATS *fps) { |
| 128 if (p->stats_in >= p->stats_in_end) | 131 if (p->stats_in >= p->stats_in_end) |
| 129 return EOF; | 132 return EOF; |
| 130 | 133 |
| 131 *fps = *p->stats_in; | 134 *fps = *p->stats_in; |
| 132 ++p->stats_in; | 135 ++p->stats_in; |
| 133 return 1; | 136 return 1; |
| 134 } | 137 } |
| 135 | 138 |
| 136 static void output_stats(const VP9_COMP *cpi, | 139 static void output_stats(FIRSTPASS_STATS *stats, |
| 137 struct vpx_codec_pkt_list *pktlist, | 140 struct vpx_codec_pkt_list *pktlist) { |
| 138 FIRSTPASS_STATS *stats) { | |
| 139 struct vpx_codec_cx_pkt pkt; | 141 struct vpx_codec_cx_pkt pkt; |
| 140 pkt.kind = VPX_CODEC_STATS_PKT; | 142 pkt.kind = VPX_CODEC_STATS_PKT; |
| 141 pkt.data.twopass_stats.buf = stats; | 143 pkt.data.twopass_stats.buf = stats; |
| 142 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); | 144 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
| 143 vpx_codec_pkt_list_add(pktlist, &pkt); | 145 vpx_codec_pkt_list_add(pktlist, &pkt); |
| 144 | 146 |
| 145 // TEMP debug code | 147 // TEMP debug code |
| 146 #if OUTPUT_FPF | 148 #if OUTPUT_FPF |
| 147 | |
| 148 { | 149 { |
| 149 FILE *fpfile; | 150 FILE *fpfile; |
| 150 fpfile = fopen("firstpass.stt", "a"); | 151 fpfile = fopen("firstpass.stt", "a"); |
| 151 | 152 |
| 152 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f" | 153 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f" |
| 153 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" | 154 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" |
| 154 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", | 155 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", |
| 155 stats->frame, | 156 stats->frame, |
| 156 stats->intra_error, | 157 stats->intra_error, |
| 157 stats->coded_error, | 158 stats->coded_error, |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 max_bits = cpi->rc.max_frame_bandwidth; | 349 max_bits = cpi->rc.max_frame_bandwidth; |
| 349 | 350 |
| 350 return (int)max_bits; | 351 return (int)max_bits; |
| 351 } | 352 } |
| 352 | 353 |
| 353 void vp9_init_first_pass(VP9_COMP *cpi) { | 354 void vp9_init_first_pass(VP9_COMP *cpi) { |
| 354 zero_stats(&cpi->twopass.total_stats); | 355 zero_stats(&cpi->twopass.total_stats); |
| 355 } | 356 } |
| 356 | 357 |
| 357 void vp9_end_first_pass(VP9_COMP *cpi) { | 358 void vp9_end_first_pass(VP9_COMP *cpi) { |
| 358 output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats); | 359 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); |
| 359 } | 360 } |
| 360 | 361 |
| 361 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { | 362 static vp9_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { |
| 362 switch (bsize) { | 363 switch (bsize) { |
| 363 case BLOCK_8X8: | 364 case BLOCK_8X8: |
| 364 return vp9_mse8x8; | 365 return vp9_mse8x8; |
| 365 case BLOCK_16X8: | 366 case BLOCK_16X8: |
| 366 return vp9_mse16x8; | 367 return vp9_mse16x8; |
| 367 case BLOCK_8X16: | 368 case BLOCK_8X16: |
| 368 return vp9_mse8x16; | 369 return vp9_mse8x16; |
| 369 default: | 370 default: |
| 370 return vp9_mse16x16; | 371 return vp9_mse16x16; |
| 371 } | 372 } |
| 372 } | 373 } |
| 373 | 374 |
| 374 static unsigned int zz_motion_search(const VP9_COMP *cpi, const MACROBLOCK *x) { | 375 static unsigned int zz_motion_search(const MACROBLOCK *x) { |
| 375 const MACROBLOCKD *const xd = &x->e_mbd; | 376 const MACROBLOCKD *const xd = &x->e_mbd; |
| 376 const uint8_t *const src = x->plane[0].src.buf; | 377 const uint8_t *const src = x->plane[0].src.buf; |
| 377 const int src_stride = x->plane[0].src.stride; | 378 const int src_stride = x->plane[0].src.stride; |
| 378 const uint8_t *const ref = xd->plane[0].pre[0].buf; | 379 const uint8_t *const ref = xd->plane[0].pre[0].buf; |
| 379 const int ref_stride = xd->plane[0].pre[0].stride; | 380 const int ref_stride = xd->plane[0].pre[0].stride; |
| 380 | |
| 381 unsigned int sse; | 381 unsigned int sse; |
| 382 vp9_variance_fn_t fn = get_block_variance_fn(xd->mi_8x8[0]->mbmi.sb_type); | 382 vp9_variance_fn_t fn = get_block_variance_fn(xd->mi_8x8[0]->mbmi.sb_type); |
| 383 fn(src, src_stride, ref, ref_stride, &sse); | 383 fn(src, src_stride, ref, ref_stride, &sse); |
| 384 return sse; | 384 return sse; |
| 385 } | 385 } |
| 386 | 386 |
| 387 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, | 387 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
| 388 const MV *ref_mv, MV *best_mv, | 388 const MV *ref_mv, MV *best_mv, |
| 389 int *best_motion_err) { | 389 int *best_motion_err) { |
| 390 MACROBLOCKD *const xd = &x->e_mbd; | 390 MACROBLOCKD *const xd = &x->e_mbd; |
| 391 MV tmp_mv = {0, 0}; | 391 MV tmp_mv = {0, 0}; |
| 392 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; | 392 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; |
| 393 int num00, tmp_err, n, sr = 0; | 393 int num00, tmp_err, n, sr = 0; |
| 394 int step_param = 3; | 394 int step_param = 3; |
| 395 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; | 395 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
| 396 const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type; | 396 const BLOCK_SIZE bsize = xd->mi_8x8[0]->mbmi.sb_type; |
| 397 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; | 397 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; |
| 398 int new_mv_mode_penalty = 256; | 398 int new_mv_mode_penalty = 256; |
| 399 const int quart_frm = MIN(cpi->common.width, cpi->common.height); | 399 const int quart_frm = MIN(cpi->common.width, cpi->common.height); |
| 400 | 400 |
| 401 // refine the motion search range accroding to the frame dimension | 401 // Refine the motion search range according to the frame dimension |
| 402 // for first pass test | 402 // for first pass test. |
| 403 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) | 403 while ((quart_frm << sr) < MAX_FULL_PEL_VAL) |
| 404 sr++; | 404 ++sr; |
| 405 | 405 |
| 406 step_param += sr; | 406 step_param += sr; |
| 407 further_steps -= sr; | 407 further_steps -= sr; |
| 408 | 408 |
| 409 // override the default variance function to use MSE | 409 // Override the default variance function to use MSE. |
| 410 v_fn_ptr.vf = get_block_variance_fn(bsize); | 410 v_fn_ptr.vf = get_block_variance_fn(bsize); |
| 411 | 411 |
| 412 // Initial step/diamond search centred on best mv | 412 // Center the initial step/diamond search on best mv. |
| 413 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 413 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, |
| 414 step_param, | 414 step_param, |
| 415 x->sadperbit16, &num00, &v_fn_ptr, | 415 x->sadperbit16, &num00, &v_fn_ptr, |
| 416 x->nmvjointcost, | 416 x->nmvjointcost, |
| 417 x->mvcost, ref_mv); | 417 x->mvcost, ref_mv); |
| 418 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 418 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 419 tmp_err += new_mv_mode_penalty; | 419 tmp_err += new_mv_mode_penalty; |
| 420 | 420 |
| 421 if (tmp_err < *best_motion_err) { | 421 if (tmp_err < *best_motion_err) { |
| 422 *best_motion_err = tmp_err; | 422 *best_motion_err = tmp_err; |
| 423 best_mv->row = tmp_mv.row; | 423 best_mv->row = tmp_mv.row; |
| 424 best_mv->col = tmp_mv.col; | 424 best_mv->col = tmp_mv.col; |
| 425 } | 425 } |
| 426 | 426 |
| 427 // Further step/diamond searches as necessary | 427 // Carry out further step/diamond searches as necessary. |
| 428 n = num00; | 428 n = num00; |
| 429 num00 = 0; | 429 num00 = 0; |
| 430 | 430 |
| 431 while (n < further_steps) { | 431 while (n < further_steps) { |
| 432 n++; | 432 ++n; |
| 433 | 433 |
| 434 if (num00) { | 434 if (num00) { |
| 435 num00--; | 435 --num00; |
| 436 } else { | 436 } else { |
| 437 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, | 437 tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv, |
| 438 step_param + n, x->sadperbit16, | 438 step_param + n, x->sadperbit16, |
| 439 &num00, &v_fn_ptr, | 439 &num00, &v_fn_ptr, |
| 440 x->nmvjointcost, | 440 x->nmvjointcost, |
| 441 x->mvcost, ref_mv); | 441 x->mvcost, ref_mv); |
| 442 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 442 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 443 tmp_err += new_mv_mode_penalty; | 443 tmp_err += new_mv_mode_penalty; |
| 444 | 444 |
| 445 if (tmp_err < *best_motion_err) { | 445 if (tmp_err < *best_motion_err) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 int intercount = 0; | 490 int intercount = 0; |
| 491 int second_ref_count = 0; | 491 int second_ref_count = 0; |
| 492 int intrapenalty = 256; | 492 int intrapenalty = 256; |
| 493 int neutral_count = 0; | 493 int neutral_count = 0; |
| 494 int new_mv_count = 0; | 494 int new_mv_count = 0; |
| 495 int sum_in_vectors = 0; | 495 int sum_in_vectors = 0; |
| 496 uint32_t lastmv_as_int = 0; | 496 uint32_t lastmv_as_int = 0; |
| 497 struct twopass_rc *const twopass = &cpi->twopass; | 497 struct twopass_rc *const twopass = &cpi->twopass; |
| 498 const MV zero_mv = {0, 0}; | 498 const MV zero_mv = {0, 0}; |
| 499 | 499 |
| 500 vp9_clear_system_state(); // __asm emms; | 500 vp9_clear_system_state(); |
| 501 | 501 |
| 502 vp9_setup_src_planes(x, cpi->Source, 0, 0); | 502 vp9_setup_src_planes(x, cpi->Source, 0, 0); |
| 503 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); | 503 setup_pre_planes(xd, 0, lst_yv12, 0, 0, NULL); |
| 504 setup_dst_planes(xd, new_yv12, 0, 0); | 504 setup_dst_planes(xd, new_yv12, 0, 0); |
| 505 | 505 |
| 506 xd->mi_8x8 = cm->mi_grid_visible; | 506 xd->mi_8x8 = cm->mi_grid_visible; |
| 507 xd->mi_8x8[0] = cm->mi; // required for vp9_frame_init_quantizer | 507 xd->mi_8x8[0] = cm->mi; |
| 508 | 508 |
| 509 vp9_setup_block_planes(&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 vp9_init_mv_probs(cm); | 521 vp9_init_mv_probs(cm); |
| 522 vp9_initialize_rd_consts(cpi); | 522 vp9_initialize_rd_consts(cpi); |
| 523 | 523 |
| 524 // tiling is ignored in the first pass | 524 // Tiling is ignored in the first pass. |
| 525 vp9_tile_init(&tile, cm, 0, 0); | 525 vp9_tile_init(&tile, cm, 0, 0); |
| 526 | 526 |
| 527 // for each macroblock row in image | 527 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
| 528 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) { | |
| 529 int_mv best_ref_mv; | 528 int_mv best_ref_mv; |
| 530 | 529 |
| 531 best_ref_mv.as_int = 0; | 530 best_ref_mv.as_int = 0; |
| 532 | 531 |
| 533 // reset above block coeffs | 532 // Reset above block coeffs. |
| 534 xd->up_available = (mb_row != 0); | 533 xd->up_available = (mb_row != 0); |
| 535 recon_yoffset = (mb_row * recon_y_stride * 16); | 534 recon_yoffset = (mb_row * recon_y_stride * 16); |
| 536 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); | 535 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); |
| 537 | 536 |
| 538 // Set up limit values for motion vectors to prevent them extending | 537 // Set up limit values for motion vectors to prevent them extending |
| 539 // outside the UMV borders | 538 // outside the UMV borders. |
| 540 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); | 539 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
| 541 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) | 540 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
| 542 + BORDER_MV_PIXELS_B16; | 541 + BORDER_MV_PIXELS_B16; |
| 543 | 542 |
| 544 // for each macroblock col in image | 543 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
| 545 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) { | |
| 546 int this_error; | 544 int this_error; |
| 547 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 545 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 548 double error_weight = 1.0; | 546 double error_weight = 1.0; |
| 549 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); | 547 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); |
| 550 | 548 |
| 551 vp9_clear_system_state(); // __asm emms; | 549 vp9_clear_system_state(); |
| 552 | 550 |
| 553 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 551 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 554 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 552 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 555 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 553 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 556 xd->left_available = (mb_col != 0); | 554 xd->left_available = (mb_col != 0); |
| 557 xd->mi_8x8[0]->mbmi.sb_type = bsize; | 555 xd->mi_8x8[0]->mbmi.sb_type = bsize; |
| 558 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; | 556 xd->mi_8x8[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 559 set_mi_row_col(xd, &tile, | 557 set_mi_row_col(xd, &tile, |
| 560 mb_row << 1, num_8x8_blocks_high_lookup[bsize], | 558 mb_row << 1, num_8x8_blocks_high_lookup[bsize], |
| 561 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], | 559 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], |
| 562 cm->mi_rows, cm->mi_cols); | 560 cm->mi_rows, cm->mi_cols); |
| 563 | 561 |
| 564 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 562 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 565 const int energy = vp9_block_energy(cpi, x, bsize); | 563 const int energy = vp9_block_energy(cpi, x, bsize); |
| 566 error_weight = vp9_vaq_inv_q_ratio(energy); | 564 error_weight = vp9_vaq_inv_q_ratio(energy); |
| 567 } | 565 } |
| 568 | 566 |
| 569 // do intra 16x16 prediction | 567 // Do intra 16x16 prediction. |
| 570 this_error = vp9_encode_intra(x, use_dc_pred); | 568 this_error = vp9_encode_intra(x, use_dc_pred); |
| 571 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 569 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 572 vp9_clear_system_state(); // __asm emms; | 570 vp9_clear_system_state(); |
| 573 this_error *= error_weight; | 571 this_error = (int)(this_error * error_weight); |
| 574 } | 572 } |
| 575 | 573 |
| 576 // intrapenalty below deals with situations where the intra and inter | 574 // Intrapenalty below deals with situations where the intra and inter |
| 577 // error scores are very low (eg a plain black frame). | 575 // error scores are very low (e.g. a plain black frame). |
| 578 // We do not have special cases in first pass for 0,0 and nearest etc so | 576 // We do not have special cases in first pass for 0,0 and nearest etc so |
| 579 // all inter modes carry an overhead cost estimate for the mv. | 577 // all inter modes carry an overhead cost estimate for the mv. |
| 580 // When the error score is very low this causes us to pick all or lots of | 578 // When the error score is very low this causes us to pick all or lots of |
| 581 // INTRA modes and throw lots of key frames. | 579 // INTRA modes and throw lots of key frames. |
| 582 // This penalty adds a cost matching that of a 0,0 mv to the intra case. | 580 // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 583 this_error += intrapenalty; | 581 this_error += intrapenalty; |
| 584 | 582 |
| 585 // Cumulative intra error total | 583 // Accumulate the intra error. |
| 586 intra_error += (int64_t)this_error; | 584 intra_error += (int64_t)this_error; |
| 587 | 585 |
| 588 // Set up limit values for motion vectors to prevent them extending | 586 // Set up limit values for motion vectors to prevent them extending |
| 589 // outside the UMV borders. | 587 // outside the UMV borders. |
| 590 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 588 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 591 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; | 589 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
| 592 | 590 |
| 593 // Other than for the first frame do a motion search | 591 // Other than for the first frame do a motion search. |
| 594 if (cm->current_video_frame > 0) { | 592 if (cm->current_video_frame > 0) { |
| 595 int tmp_err, motion_error; | 593 int tmp_err, motion_error; |
| 596 int_mv mv, tmp_mv; | 594 int_mv mv, tmp_mv; |
| 597 | 595 |
| 598 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 596 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; |
| 599 motion_error = zz_motion_search(cpi, x); | 597 motion_error = zz_motion_search(x); |
| 600 // Simple 0,0 motion with no mv overhead | 598 // Assume 0,0 motion with no mv overhead. |
| 601 mv.as_int = tmp_mv.as_int = 0; | 599 mv.as_int = tmp_mv.as_int = 0; |
| 602 | 600 |
| 603 // Test last reference frame using the previous best mv as the | 601 // Test last reference frame using the previous best mv as the |
| 604 // starting point (best reference) for the search | 602 // starting point (best reference) for the search. |
| 605 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, | 603 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, |
| 606 &motion_error); | 604 &motion_error); |
| 607 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 605 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 608 vp9_clear_system_state(); // __asm emms; | 606 vp9_clear_system_state(); |
| 609 motion_error *= error_weight; | 607 motion_error = (int)(motion_error * error_weight); |
| 610 } | 608 } |
| 611 | 609 |
| 612 // If the current best reference mv is not centered on 0,0 then do a 0,0 | 610 // If the current best reference mv is not centered on 0,0 then do a 0,0 |
| 613 // based search as well. | 611 // based search as well. |
| 614 if (best_ref_mv.as_int) { | 612 if (best_ref_mv.as_int) { |
| 615 tmp_err = INT_MAX; | 613 tmp_err = INT_MAX; |
| 616 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, | 614 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 617 &tmp_err); | 615 &tmp_err); |
| 618 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 616 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 619 vp9_clear_system_state(); // __asm emms; | 617 vp9_clear_system_state(); |
| 620 tmp_err *= error_weight; | 618 tmp_err = (int)(tmp_err * error_weight); |
| 621 } | 619 } |
| 622 | 620 |
| 623 if (tmp_err < motion_error) { | 621 if (tmp_err < motion_error) { |
| 624 motion_error = tmp_err; | 622 motion_error = tmp_err; |
| 625 mv.as_int = tmp_mv.as_int; | 623 mv.as_int = tmp_mv.as_int; |
| 626 } | 624 } |
| 627 } | 625 } |
| 628 | 626 |
| 629 // Experimental search in an older reference frame | 627 // Search in an older reference frame. |
| 630 if (cm->current_video_frame > 1) { | 628 if (cm->current_video_frame > 1) { |
| 631 // Simple 0,0 motion with no mv overhead | 629 // Assume 0,0 motion with no mv overhead. |
| 632 int gf_motion_error; | 630 int gf_motion_error; |
| 633 | 631 |
| 634 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; | 632 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
| 635 gf_motion_error = zz_motion_search(cpi, x); | 633 gf_motion_error = zz_motion_search(x); |
| 636 | 634 |
| 637 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, | 635 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv.as_mv, |
| 638 &gf_motion_error); | 636 &gf_motion_error); |
| 639 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 637 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 640 vp9_clear_system_state(); // __asm emms; | 638 vp9_clear_system_state(); |
| 641 gf_motion_error *= error_weight; | 639 gf_motion_error = (int)(gf_motion_error * error_weight); |
| 642 } | 640 } |
| 643 | 641 |
| 644 if (gf_motion_error < motion_error && gf_motion_error < this_error) | 642 if (gf_motion_error < motion_error && gf_motion_error < this_error) |
| 645 second_ref_count++; | 643 ++second_ref_count; |
| 646 | 644 |
| 647 // Reset to last frame as reference buffer | 645 // Reset to last frame as reference buffer. |
| 648 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; | 646 xd->plane[0].pre[0].buf = lst_yv12->y_buffer + recon_yoffset; |
| 649 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; | 647 xd->plane[1].pre[0].buf = lst_yv12->u_buffer + recon_uvoffset; |
| 650 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; | 648 xd->plane[2].pre[0].buf = lst_yv12->v_buffer + recon_uvoffset; |
| 651 | 649 |
| 652 // In accumulating a score for the older reference frame | 650 // In accumulating a score for the older reference frame take the |
| 653 // take the best of the motion predicted score and | 651 // best of the motion predicted score and the intra coded error |
| 654 // the intra coded error (just as will be done for) | 652 // (just as will be done for) accumulation of "coded_error" for |
| 655 // accumulation of "coded_error" for the last frame. | 653 // the last frame. |
| 656 if (gf_motion_error < this_error) | 654 if (gf_motion_error < this_error) |
| 657 sr_coded_error += gf_motion_error; | 655 sr_coded_error += gf_motion_error; |
| 658 else | 656 else |
| 659 sr_coded_error += this_error; | 657 sr_coded_error += this_error; |
| 660 } else { | 658 } else { |
| 661 sr_coded_error += motion_error; | 659 sr_coded_error += motion_error; |
| 662 } | 660 } |
| 663 /* Intra assumed best */ | 661 // Start by assuming that intra mode is best. |
| 664 best_ref_mv.as_int = 0; | 662 best_ref_mv.as_int = 0; |
| 665 | 663 |
| 666 if (motion_error <= this_error) { | 664 if (motion_error <= this_error) { |
| 667 // Keep a count of cases where the inter and intra were | 665 // Keep a count of cases where the inter and intra were very close |
| 668 // very close and very low. This helps with scene cut | 666 // and very low. This helps with scene cut detection for example in |
| 669 // detection for example in cropped clips with black bars | 667 // cropped clips with black bars at the sides or top and bottom. |
| 670 // at the sides or top and bottom. | |
| 671 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && | 668 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
| 672 this_error < 2 * intrapenalty) | 669 this_error < 2 * intrapenalty) |
| 673 neutral_count++; | 670 ++neutral_count; |
| 674 | 671 |
| 675 mv.as_mv.row *= 8; | 672 mv.as_mv.row *= 8; |
| 676 mv.as_mv.col *= 8; | 673 mv.as_mv.col *= 8; |
| 677 this_error = motion_error; | 674 this_error = motion_error; |
| 678 vp9_set_mbmode_and_mvs(xd, NEWMV, &mv.as_mv); | 675 vp9_set_mbmode_and_mvs(xd, NEWMV, &mv.as_mv); |
| 679 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; | 676 xd->mi_8x8[0]->mbmi.tx_size = TX_4X4; |
| 680 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; | 677 xd->mi_8x8[0]->mbmi.ref_frame[0] = LAST_FRAME; |
| 681 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; | 678 xd->mi_8x8[0]->mbmi.ref_frame[1] = NONE; |
| 682 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); | 679 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); |
| 683 vp9_encode_sby(x, bsize); | 680 vp9_encode_sby_pass1(x, bsize); |
| 684 sum_mvr += mv.as_mv.row; | 681 sum_mvr += mv.as_mv.row; |
| 685 sum_mvr_abs += abs(mv.as_mv.row); | 682 sum_mvr_abs += abs(mv.as_mv.row); |
| 686 sum_mvc += mv.as_mv.col; | 683 sum_mvc += mv.as_mv.col; |
| 687 sum_mvc_abs += abs(mv.as_mv.col); | 684 sum_mvc_abs += abs(mv.as_mv.col); |
| 688 sum_mvrs += mv.as_mv.row * mv.as_mv.row; | 685 sum_mvrs += mv.as_mv.row * mv.as_mv.row; |
| 689 sum_mvcs += mv.as_mv.col * mv.as_mv.col; | 686 sum_mvcs += mv.as_mv.col * mv.as_mv.col; |
| 690 intercount++; | 687 ++intercount; |
| 691 | 688 |
| 692 best_ref_mv.as_int = mv.as_int; | 689 best_ref_mv.as_int = mv.as_int; |
| 693 | 690 |
| 694 // Was the vector non-zero | |
| 695 if (mv.as_int) { | 691 if (mv.as_int) { |
| 696 mvcount++; | 692 ++mvcount; |
| 697 | 693 |
| 698 // Was it different from the last non zero vector | 694 // Non-zero vector, was it different from the last non zero vector? |
| 699 if (mv.as_int != lastmv_as_int) | 695 if (mv.as_int != lastmv_as_int) |
| 700 new_mv_count++; | 696 ++new_mv_count; |
| 701 lastmv_as_int = mv.as_int; | 697 lastmv_as_int = mv.as_int; |
| 702 | 698 |
| 703 // Does the Row vector point inwards or outwards | 699 // Does the row vector point inwards or outwards? |
| 704 if (mb_row < cm->mb_rows / 2) { | 700 if (mb_row < cm->mb_rows / 2) { |
| 705 if (mv.as_mv.row > 0) | 701 if (mv.as_mv.row > 0) |
| 706 sum_in_vectors--; | 702 --sum_in_vectors; |
| 707 else if (mv.as_mv.row < 0) | 703 else if (mv.as_mv.row < 0) |
| 708 sum_in_vectors++; | 704 ++sum_in_vectors; |
| 709 } else if (mb_row > cm->mb_rows / 2) { | 705 } else if (mb_row > cm->mb_rows / 2) { |
| 710 if (mv.as_mv.row > 0) | 706 if (mv.as_mv.row > 0) |
| 711 sum_in_vectors++; | 707 ++sum_in_vectors; |
| 712 else if (mv.as_mv.row < 0) | 708 else if (mv.as_mv.row < 0) |
| 713 sum_in_vectors--; | 709 --sum_in_vectors; |
| 714 } | 710 } |
| 715 | 711 |
| 716 // Does the Row vector point inwards or outwards | 712 // Does the col vector point inwards or outwards? |
| 717 if (mb_col < cm->mb_cols / 2) { | 713 if (mb_col < cm->mb_cols / 2) { |
| 718 if (mv.as_mv.col > 0) | 714 if (mv.as_mv.col > 0) |
| 719 sum_in_vectors--; | 715 --sum_in_vectors; |
| 720 else if (mv.as_mv.col < 0) | 716 else if (mv.as_mv.col < 0) |
| 721 sum_in_vectors++; | 717 ++sum_in_vectors; |
| 722 } else if (mb_col > cm->mb_cols / 2) { | 718 } else if (mb_col > cm->mb_cols / 2) { |
| 723 if (mv.as_mv.col > 0) | 719 if (mv.as_mv.col > 0) |
| 724 sum_in_vectors++; | 720 ++sum_in_vectors; |
| 725 else if (mv.as_mv.col < 0) | 721 else if (mv.as_mv.col < 0) |
| 726 sum_in_vectors--; | 722 --sum_in_vectors; |
| 727 } | 723 } |
| 728 } | 724 } |
| 729 } | 725 } |
| 730 } else { | 726 } else { |
| 731 sr_coded_error += (int64_t)this_error; | 727 sr_coded_error += (int64_t)this_error; |
| 732 } | 728 } |
| 733 coded_error += (int64_t)this_error; | 729 coded_error += (int64_t)this_error; |
| 734 | 730 |
| 735 // adjust to the next column of macroblocks | 731 // Adjust to the next column of MBs. |
| 736 x->plane[0].src.buf += 16; | 732 x->plane[0].src.buf += 16; |
| 737 x->plane[1].src.buf += uv_mb_height; | 733 x->plane[1].src.buf += uv_mb_height; |
| 738 x->plane[2].src.buf += uv_mb_height; | 734 x->plane[2].src.buf += uv_mb_height; |
| 739 | 735 |
| 740 recon_yoffset += 16; | 736 recon_yoffset += 16; |
| 741 recon_uvoffset += uv_mb_height; | 737 recon_uvoffset += uv_mb_height; |
| 742 } | 738 } |
| 743 | 739 |
| 744 // adjust to the next row of mbs | 740 // Adjust to the next row of MBs. |
| 745 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; | 741 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; |
| 746 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - | 742 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - |
| 747 uv_mb_height * cm->mb_cols; | 743 uv_mb_height * cm->mb_cols; |
| 748 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - | 744 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - |
| 749 uv_mb_height * cm->mb_cols; | 745 uv_mb_height * cm->mb_cols; |
| 750 | 746 |
| 751 vp9_clear_system_state(); // __asm emms; | 747 vp9_clear_system_state(); |
| 752 } | 748 } |
| 753 | 749 |
| 754 vp9_clear_system_state(); // __asm emms; | 750 vp9_clear_system_state(); |
| 755 { | 751 { |
| 756 FIRSTPASS_STATS fps; | 752 FIRSTPASS_STATS fps; |
| 757 | 753 |
| 758 fps.frame = cm->current_video_frame; | 754 fps.frame = cm->current_video_frame; |
| 759 fps.intra_error = intra_error >> 8; | 755 fps.intra_error = (double)(intra_error >> 8); |
| 760 fps.coded_error = coded_error >> 8; | 756 fps.coded_error = (double)(coded_error >> 8); |
| 761 fps.sr_coded_error = sr_coded_error >> 8; | 757 fps.sr_coded_error = (double)(sr_coded_error >> 8); |
| 762 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source); | 758 fps.ssim_weighted_pred_err = fps.coded_error * simple_weight(cpi->Source); |
| 763 fps.count = 1.0; | 759 fps.count = 1.0; |
| 764 fps.pcnt_inter = (double)intercount / cm->MBs; | 760 fps.pcnt_inter = (double)intercount / cm->MBs; |
| 765 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; | 761 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; |
| 766 fps.pcnt_neutral = (double)neutral_count / cm->MBs; | 762 fps.pcnt_neutral = (double)neutral_count / cm->MBs; |
| 767 | 763 |
| 768 if (mvcount > 0) { | 764 if (mvcount > 0) { |
| 769 fps.MVr = (double)sum_mvr / mvcount; | 765 fps.MVr = (double)sum_mvr / mvcount; |
| 770 fps.mvr_abs = (double)sum_mvr_abs / mvcount; | 766 fps.mvr_abs = (double)sum_mvr_abs / mvcount; |
| 771 fps.MVc = (double)sum_mvc / mvcount; | 767 fps.MVc = (double)sum_mvc / mvcount; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 785 fps.mv_in_out_count = 0.0; | 781 fps.mv_in_out_count = 0.0; |
| 786 fps.new_mv_count = 0.0; | 782 fps.new_mv_count = 0.0; |
| 787 fps.pcnt_motion = 0.0; | 783 fps.pcnt_motion = 0.0; |
| 788 } | 784 } |
| 789 | 785 |
| 790 // TODO(paulwilkins): Handle the case when duration is set to 0, or | 786 // TODO(paulwilkins): Handle the case when duration is set to 0, or |
| 791 // something less than the full time between subsequent values of | 787 // something less than the full time between subsequent values of |
| 792 // cpi->source_time_stamp. | 788 // cpi->source_time_stamp. |
| 793 fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start); | 789 fps.duration = (double)(cpi->source->ts_end - cpi->source->ts_start); |
| 794 | 790 |
| 795 // don't want to do output stats with a stack variable! | 791 // Don't want to do output stats with a stack variable! |
| 796 twopass->this_frame_stats = fps; | 792 twopass->this_frame_stats = fps; |
| 797 output_stats(cpi, cpi->output_pkt_list, &twopass->this_frame_stats); | 793 output_stats(&twopass->this_frame_stats, cpi->output_pkt_list); |
| 798 accumulate_stats(&twopass->total_stats, &fps); | 794 accumulate_stats(&twopass->total_stats, &fps); |
| 799 } | 795 } |
| 800 | 796 |
| 801 // Copy the previous Last Frame back into gf and and arf buffers if | 797 // Copy the previous Last Frame back into gf and and arf buffers if |
| 802 // the prediction is good enough... but also dont allow it to lag too far | 798 // the prediction is good enough... but also don't allow it to lag too far. |
| 803 if ((twopass->sr_update_lag > 3) || | 799 if ((twopass->sr_update_lag > 3) || |
| 804 ((cm->current_video_frame > 0) && | 800 ((cm->current_video_frame > 0) && |
| 805 (twopass->this_frame_stats.pcnt_inter > 0.20) && | 801 (twopass->this_frame_stats.pcnt_inter > 0.20) && |
| 806 ((twopass->this_frame_stats.intra_error / | 802 ((twopass->this_frame_stats.intra_error / |
| 807 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { | 803 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { |
| 808 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 804 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| 809 twopass->sr_update_lag = 1; | 805 twopass->sr_update_lag = 1; |
| 810 } else { | 806 } else { |
| 811 twopass->sr_update_lag++; | 807 ++twopass->sr_update_lag; |
| 812 } | 808 } |
| 813 // swap frame pointers so last frame refers to the frame we just compressed | 809 // Swap frame pointers so last frame refers to the frame we just compressed. |
| 814 swap_yv12(lst_yv12, new_yv12); | 810 swap_yv12(lst_yv12, new_yv12); |
| 815 | 811 |
| 816 vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y); | 812 vp9_extend_frame_borders(lst_yv12); |
| 817 | 813 |
| 818 // Special case for the first frame. Copy into the GF buffer as a second | 814 // Special case for the first frame. Copy into the GF buffer as a second |
| 819 // reference. | 815 // reference. |
| 820 if (cm->current_video_frame == 0) | 816 if (cm->current_video_frame == 0) |
| 821 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 817 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| 822 | 818 |
| 823 // use this to see what the first pass reconstruction looks like | 819 // Use this to see what the first pass reconstruction looks like. |
| 824 if (0) { | 820 if (0) { |
| 825 char filename[512]; | 821 char filename[512]; |
| 826 FILE *recon_file; | 822 FILE *recon_file; |
| 827 snprintf(filename, sizeof(filename), "enc%04d.yuv", | 823 snprintf(filename, sizeof(filename), "enc%04d.yuv", |
| 828 (int)cm->current_video_frame); | 824 (int)cm->current_video_frame); |
| 829 | 825 |
| 830 if (cm->current_video_frame == 0) | 826 if (cm->current_video_frame == 0) |
| 831 recon_file = fopen(filename, "wb"); | 827 recon_file = fopen(filename, "wb"); |
| 832 else | 828 else |
| 833 recon_file = fopen(filename, "ab"); | 829 recon_file = fopen(filename, "ab"); |
| 834 | 830 |
| 835 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); | 831 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); |
| 836 fclose(recon_file); | 832 fclose(recon_file); |
| 837 } | 833 } |
| 838 | 834 |
| 839 cm->current_video_frame++; | 835 ++cm->current_video_frame; |
| 840 } | 836 } |
| 841 | 837 |
| 842 // Estimate a cost per mb attributable to overheads such as the coding of | 838 // Estimate a cost per mb attributable to overheads such as the coding of modes |
| 843 // modes and motion vectors. | 839 // and motion vectors. This currently makes simplistic assumptions for testing. |
| 844 // Currently simplistic in its assumptions for testing. | |
| 845 // | |
| 846 | |
| 847 | |
| 848 static double bitcost(double prob) { | 840 static double bitcost(double prob) { |
| 849 return -(log(prob) / log(2.0)); | 841 return -(log(prob) / log(2.0)); |
| 850 } | 842 } |
| 851 | 843 |
| 852 static int64_t estimate_modemvcost(VP9_COMP *cpi, | |
| 853 FIRSTPASS_STATS *fpstats) { | |
| 854 #if 0 | |
| 855 int mv_cost; | |
| 856 int mode_cost; | |
| 857 | |
| 858 double av_pct_inter = fpstats->pcnt_inter / fpstats->count; | |
| 859 double av_pct_motion = fpstats->pcnt_motion / fpstats->count; | |
| 860 double av_intra = (1.0 - av_pct_inter); | |
| 861 | |
| 862 double zz_cost; | |
| 863 double motion_cost; | |
| 864 double intra_cost; | |
| 865 | |
| 866 zz_cost = bitcost(av_pct_inter - av_pct_motion); | |
| 867 motion_cost = bitcost(av_pct_motion); | |
| 868 intra_cost = bitcost(av_intra); | |
| 869 | |
| 870 // Estimate of extra bits per mv overhead for mbs | |
| 871 // << 9 is the normalization to the (bits * 512) used in vp9_rc_bits_per_mb | |
| 872 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9; | |
| 873 | |
| 874 // Crude estimate of overhead cost from modes | |
| 875 // << 9 is the normalization to (bits * 512) used in vp9_rc_bits_per_mb | |
| 876 mode_cost = | |
| 877 (int)((((av_pct_inter - av_pct_motion) * zz_cost) + | |
| 878 (av_pct_motion * motion_cost) + | |
| 879 (av_intra * intra_cost)) * cpi->common.MBs) << 9; | |
| 880 | |
| 881 // return mv_cost + mode_cost; | |
| 882 // TODO(paulwilkins): Fix overhead costs for extended Q range. | |
| 883 #endif | |
| 884 return 0; | |
| 885 } | |
| 886 | |
| 887 static double calc_correction_factor(double err_per_mb, | 844 static double calc_correction_factor(double err_per_mb, |
| 888 double err_divisor, | 845 double err_divisor, |
| 889 double pt_low, | 846 double pt_low, |
| 890 double pt_high, | 847 double pt_high, |
| 891 int q) { | 848 int q) { |
| 892 const double error_term = err_per_mb / err_divisor; | 849 const double error_term = err_per_mb / err_divisor; |
| 893 | 850 |
| 894 // Adjustment based on actual quantizer to power term. | 851 // Adjustment based on actual quantizer to power term. |
| 895 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, | 852 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, |
| 896 pt_high); | 853 pt_high); |
| 897 | 854 |
| 898 // Calculate correction factor | 855 // Calculate correction factor. |
| 899 if (power_term < 1.0) | 856 if (power_term < 1.0) |
| 900 assert(error_term >= 0.0); | 857 assert(error_term >= 0.0); |
| 901 | 858 |
| 902 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 859 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 903 } | 860 } |
| 904 | 861 |
| 905 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats, | 862 int vp9_twopass_worst_quality(VP9_COMP *cpi, FIRSTPASS_STATS *fpstats, |
| 906 int section_target_bandwitdh) { | 863 int section_target_bandwitdh) { |
| 907 int q; | 864 int q; |
| 908 const int num_mbs = cpi->common.MBs; | 865 const int num_mbs = cpi->common.MBs; |
| 909 int target_norm_bits_per_mb; | 866 int target_norm_bits_per_mb; |
| 910 const RATE_CONTROL *const rc = &cpi->rc; | 867 const RATE_CONTROL *const rc = &cpi->rc; |
| 911 | 868 |
| 912 const double section_err = fpstats->coded_error / fpstats->count; | 869 const double section_err = fpstats->coded_error / fpstats->count; |
| 913 const double err_per_mb = section_err / num_mbs; | 870 const double err_per_mb = section_err / num_mbs; |
| 914 | 871 |
| 915 if (section_target_bandwitdh <= 0) | 872 if (section_target_bandwitdh <= 0) |
| 916 return rc->worst_quality; // Highest value allowed | 873 return rc->worst_quality; // Highest value allowed |
| 917 | 874 |
| 918 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) | 875 target_norm_bits_per_mb = section_target_bandwitdh < (1 << 20) |
| 919 ? (512 * section_target_bandwitdh) / num_mbs | 876 ? (512 * section_target_bandwitdh) / num_mbs |
| 920 : 512 * (section_target_bandwitdh / num_mbs); | 877 : 512 * (section_target_bandwitdh / num_mbs); |
| 921 | 878 |
| 922 // Try and pick a max Q that will be high enough to encode the | 879 // Try and pick a max Q that will be high enough to encode the |
| 923 // content at the given rate. | 880 // content at the given rate. |
| 924 for (q = rc->best_quality; q < rc->worst_quality; q++) { | 881 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 925 const double err_correction_factor = calc_correction_factor(err_per_mb, | 882 const double err_correction_factor = calc_correction_factor(err_per_mb, |
| 926 ERR_DIVISOR, 0.5, 0.90, q); | 883 ERR_DIVISOR, 0.5, 0.90, q); |
| 927 const int bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, | 884 const int bits_per_mb_at_this_q = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 928 err_correction_factor); | 885 err_correction_factor); |
| 929 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) | 886 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) |
| 930 break; | 887 break; |
| 931 } | 888 } |
| 932 | 889 |
| 933 // Restriction on active max q for constrained quality mode. | 890 // Restriction on active max q for constrained quality mode. |
| 934 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) | 891 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 947 | 904 |
| 948 zero_stats(&twopass->total_stats); | 905 zero_stats(&twopass->total_stats); |
| 949 zero_stats(&twopass->total_left_stats); | 906 zero_stats(&twopass->total_left_stats); |
| 950 | 907 |
| 951 if (!twopass->stats_in_end) | 908 if (!twopass->stats_in_end) |
| 952 return; | 909 return; |
| 953 | 910 |
| 954 twopass->total_stats = *twopass->stats_in_end; | 911 twopass->total_stats = *twopass->stats_in_end; |
| 955 twopass->total_left_stats = twopass->total_stats; | 912 twopass->total_left_stats = twopass->total_stats; |
| 956 | 913 |
| 957 // each frame can have a different duration, as the frame rate in the source | 914 // Each frame can have a different duration, as the frame rate in the source |
| 958 // isn't guaranteed to be constant. The frame rate prior to the first frame | 915 // isn't guaranteed to be constant. The frame rate prior to the first frame |
| 959 // encoded in the second pass is a guess. However the sum duration is not. | 916 // encoded in the second pass is a guess. However, the sum duration is not. |
| 960 // Its calculated based on the actual durations of all frames from the first | 917 // It is calculated based on the actual durations of all frames from the |
| 961 // pass. | 918 // first pass. |
| 962 vp9_new_framerate(cpi, 10000000.0 * twopass->total_stats.count / | 919 vp9_new_framerate(cpi, 10000000.0 * twopass->total_stats.count / |
| 963 twopass->total_stats.duration); | 920 twopass->total_stats.duration); |
| 964 | 921 |
| 965 cpi->output_framerate = oxcf->framerate; | 922 cpi->output_framerate = oxcf->framerate; |
| 966 twopass->bits_left = (int64_t)(twopass->total_stats.duration * | 923 twopass->bits_left = (int64_t)(twopass->total_stats.duration * |
| 967 oxcf->target_bandwidth / 10000000.0); | 924 oxcf->target_bandwidth / 10000000.0); |
| 968 | 925 |
| 969 // Calculate a minimum intra value to be used in determining the IIratio | 926 // Calculate a minimum intra value to be used in determining the IIratio |
| 970 // scores used in the second pass. We have this minimum to make sure | 927 // scores used in the second pass. We have this minimum to make sure |
| 971 // that clips that are static but "low complexity" in the intra domain | 928 // that clips that are static but "low complexity" in the intra domain |
| 972 // are still boosted appropriately for KF/GF/ARF | 929 // are still boosted appropriately for KF/GF/ARF. |
| 973 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 930 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 974 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 931 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 975 | 932 |
| 976 // This variable monitors how far behind the second ref update is lagging | 933 // This variable monitors how far behind the second ref update is lagging. |
| 977 twopass->sr_update_lag = 1; | 934 twopass->sr_update_lag = 1; |
| 978 | 935 |
| 979 // Scan the first pass file and calculate an average Intra / Inter error score | 936 // Scan the first pass file and calculate an average Intra / Inter error score |
| 980 // ratio for the sequence. | 937 // ratio for the sequence. |
| 981 { | 938 { |
| 982 double sum_iiratio = 0.0; | 939 double sum_iiratio = 0.0; |
| 983 start_pos = twopass->stats_in; // Note the starting "file" position. | 940 start_pos = twopass->stats_in; |
| 984 | 941 |
| 985 while (input_stats(twopass, &this_frame) != EOF) { | 942 while (input_stats(twopass, &this_frame) != EOF) { |
| 986 const double iiratio = this_frame.intra_error / | 943 const double iiratio = this_frame.intra_error / |
| 987 DOUBLE_DIVIDE_CHECK(this_frame.coded_error); | 944 DOUBLE_DIVIDE_CHECK(this_frame.coded_error); |
| 988 sum_iiratio += fclamp(iiratio, 1.0, 20.0); | 945 sum_iiratio += fclamp(iiratio, 1.0, 20.0); |
| 989 } | 946 } |
| 990 | 947 |
| 991 twopass->avg_iiratio = sum_iiratio / | 948 twopass->avg_iiratio = sum_iiratio / |
| 992 DOUBLE_DIVIDE_CHECK((double)twopass->total_stats.count); | 949 DOUBLE_DIVIDE_CHECK((double)twopass->total_stats.count); |
| 993 | 950 |
| 994 // Reset file position | |
| 995 reset_fpf_position(twopass, start_pos); | 951 reset_fpf_position(twopass, start_pos); |
| 996 } | 952 } |
| 997 | 953 |
| 998 // Scan the first pass file and calculate a modified total error based upon | 954 // Scan the first pass file and calculate a modified total error based upon |
| 999 // the bias/power function used to allocate bits. | 955 // the bias/power function used to allocate bits. |
| 1000 { | 956 { |
| 1001 double av_error = twopass->total_stats.ssim_weighted_pred_err / | 957 double av_error = twopass->total_stats.ssim_weighted_pred_err / |
| 1002 DOUBLE_DIVIDE_CHECK(twopass->total_stats.count); | 958 DOUBLE_DIVIDE_CHECK(twopass->total_stats.count); |
| 1003 | 959 |
| 1004 start_pos = twopass->stats_in; // Note starting "file" position | 960 start_pos = twopass->stats_in; |
| 1005 | 961 |
| 1006 twopass->modified_error_total = 0.0; | 962 twopass->modified_error_total = 0.0; |
| 1007 twopass->modified_error_min = | 963 twopass->modified_error_min = |
| 1008 (av_error * oxcf->two_pass_vbrmin_section) / 100; | 964 (av_error * oxcf->two_pass_vbrmin_section) / 100; |
| 1009 twopass->modified_error_max = | 965 twopass->modified_error_max = |
| 1010 (av_error * oxcf->two_pass_vbrmax_section) / 100; | 966 (av_error * oxcf->two_pass_vbrmax_section) / 100; |
| 1011 | 967 |
| 1012 while (input_stats(twopass, &this_frame) != EOF) { | 968 while (input_stats(twopass, &this_frame) != EOF) { |
| 1013 twopass->modified_error_total += | 969 twopass->modified_error_total += |
| 1014 calculate_modified_err(cpi, &this_frame); | 970 calculate_modified_err(cpi, &this_frame); |
| 1015 } | 971 } |
| 1016 twopass->modified_error_left = twopass->modified_error_total; | 972 twopass->modified_error_left = twopass->modified_error_total; |
| 1017 | 973 |
| 1018 reset_fpf_position(twopass, start_pos); | 974 reset_fpf_position(twopass, start_pos); |
| 1019 } | 975 } |
| 1020 } | 976 } |
| 1021 | 977 |
| 1022 void vp9_end_second_pass(VP9_COMP *cpi) { | 978 // This function gives an estimate of how badly we believe the prediction |
| 1023 } | 979 // quality is decaying from frame to frame. |
| 1024 | |
| 1025 // This function gives and estimate of how badly we believe | |
| 1026 // the prediction quality is decaying from frame to frame. | |
| 1027 static double get_prediction_decay_rate(const VP9_COMMON *cm, | 980 static double get_prediction_decay_rate(const VP9_COMMON *cm, |
| 1028 const FIRSTPASS_STATS *next_frame) { | 981 const FIRSTPASS_STATS *next_frame) { |
| 1029 // Look at the observed drop in prediction quality between the last frame | 982 // Look at the observed drop in prediction quality between the last frame |
| 1030 // and the GF buffer (which contains an older frame). | 983 // and the GF buffer (which contains an older frame). |
| 1031 const double mb_sr_err_diff = (next_frame->sr_coded_error - | 984 const double mb_sr_err_diff = (next_frame->sr_coded_error - |
| 1032 next_frame->coded_error) / cm->MBs; | 985 next_frame->coded_error) / cm->MBs; |
| 1033 const double second_ref_decay = mb_sr_err_diff <= 512.0 | 986 const double second_ref_decay = mb_sr_err_diff <= 512.0 |
| 1034 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) | 987 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) |
| 1035 : 0.85; | 988 : 0.85; |
| 1036 | 989 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1049 // Break clause to detect very still sections after motion | 1002 // Break clause to detect very still sections after motion |
| 1050 // For example a static image after a fade or other transition | 1003 // For example a static image after a fade or other transition |
| 1051 // instead of a clean scene cut. | 1004 // instead of a clean scene cut. |
| 1052 if (frame_interval > MIN_GF_INTERVAL && | 1005 if (frame_interval > MIN_GF_INTERVAL && |
| 1053 loop_decay_rate >= 0.999 && | 1006 loop_decay_rate >= 0.999 && |
| 1054 last_decay_rate < 0.9) { | 1007 last_decay_rate < 0.9) { |
| 1055 int j; | 1008 int j; |
| 1056 FIRSTPASS_STATS *position = cpi->twopass.stats_in; | 1009 FIRSTPASS_STATS *position = cpi->twopass.stats_in; |
| 1057 FIRSTPASS_STATS tmp_next_frame; | 1010 FIRSTPASS_STATS tmp_next_frame; |
| 1058 | 1011 |
| 1059 // Look ahead a few frames to see if static condition | 1012 // Look ahead a few frames to see if static condition persists... |
| 1060 // persists... | 1013 for (j = 0; j < still_interval; ++j) { |
| 1061 for (j = 0; j < still_interval; j++) { | |
| 1062 if (EOF == input_stats(&cpi->twopass, &tmp_next_frame)) | 1014 if (EOF == input_stats(&cpi->twopass, &tmp_next_frame)) |
| 1063 break; | 1015 break; |
| 1064 | 1016 |
| 1065 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999) | 1017 if (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion < 0.999) |
| 1066 break; | 1018 break; |
| 1067 } | 1019 } |
| 1068 | 1020 |
| 1069 reset_fpf_position(&cpi->twopass, position); | 1021 reset_fpf_position(&cpi->twopass, position); |
| 1070 | 1022 |
| 1071 // Only if it does do we signal a transition to still | 1023 // Only if it does do we signal a transition to still. |
| 1072 if (j == still_interval) | 1024 if (j == still_interval) |
| 1073 trans_to_still = 1; | 1025 trans_to_still = 1; |
| 1074 } | 1026 } |
| 1075 | 1027 |
| 1076 return trans_to_still; | 1028 return trans_to_still; |
| 1077 } | 1029 } |
| 1078 | 1030 |
| 1079 // This function detects a flash through the high relative pcnt_second_ref | 1031 // This function detects a flash through the high relative pcnt_second_ref |
| 1080 // score in the frame following a flash frame. The offset passed in should | 1032 // score in the frame following a flash frame. The offset passed in should |
| 1081 // reflect this | 1033 // reflect this. |
| 1082 static int detect_flash(const struct twopass_rc *twopass, int offset) { | 1034 static int detect_flash(const struct twopass_rc *twopass, int offset) { |
| 1083 FIRSTPASS_STATS next_frame; | 1035 FIRSTPASS_STATS next_frame; |
| 1084 | 1036 |
| 1085 int flash_detected = 0; | 1037 int flash_detected = 0; |
| 1086 | 1038 |
| 1087 // Read the frame data. | 1039 // Read the frame data. |
| 1088 // The return is FALSE (no flash detected) if not a valid frame | 1040 // The return is FALSE (no flash detected) if not a valid frame |
| 1089 if (read_frame_stats(twopass, &next_frame, offset) != EOF) { | 1041 if (read_frame_stats(twopass, &next_frame, offset) != EOF) { |
| 1090 // What we are looking for here is a situation where there is a | 1042 // What we are looking for here is a situation where there is a |
| 1091 // brief break in prediction (such as a flash) but subsequent frames | 1043 // brief break in prediction (such as a flash) but subsequent frames |
| 1092 // are reasonably well predicted by an earlier (pre flash) frame. | 1044 // are reasonably well predicted by an earlier (pre flash) frame. |
| 1093 // The recovery after a flash is indicated by a high pcnt_second_ref | 1045 // The recovery after a flash is indicated by a high pcnt_second_ref |
| 1094 // comapred to pcnt_inter. | 1046 // compared to pcnt_inter. |
| 1095 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && | 1047 if (next_frame.pcnt_second_ref > next_frame.pcnt_inter && |
| 1096 next_frame.pcnt_second_ref >= 0.5) | 1048 next_frame.pcnt_second_ref >= 0.5) |
| 1097 flash_detected = 1; | 1049 flash_detected = 1; |
| 1098 } | 1050 } |
| 1099 | 1051 |
| 1100 return flash_detected; | 1052 return flash_detected; |
| 1101 } | 1053 } |
| 1102 | 1054 |
| 1103 // Update the motion related elements to the GF arf boost calculation | 1055 // Update the motion related elements to the GF arf boost calculation. |
| 1104 static void accumulate_frame_motion_stats( | 1056 static void accumulate_frame_motion_stats( |
| 1105 FIRSTPASS_STATS *this_frame, | 1057 FIRSTPASS_STATS *this_frame, |
| 1106 double *this_frame_mv_in_out, | 1058 double *this_frame_mv_in_out, |
| 1107 double *mv_in_out_accumulator, | 1059 double *mv_in_out_accumulator, |
| 1108 double *abs_mv_in_out_accumulator, | 1060 double *abs_mv_in_out_accumulator, |
| 1109 double *mv_ratio_accumulator) { | 1061 double *mv_ratio_accumulator) { |
| 1110 double motion_pct; | 1062 double motion_pct; |
| 1111 | 1063 |
| 1112 // Accumulate motion stats. | 1064 // Accumulate motion stats. |
| 1113 motion_pct = this_frame->pcnt_motion; | 1065 motion_pct = this_frame->pcnt_motion; |
| 1114 | 1066 |
| 1115 // Accumulate Motion In/Out of frame stats | 1067 // Accumulate Motion In/Out of frame stats. |
| 1116 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; | 1068 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct; |
| 1117 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; | 1069 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct; |
| 1118 *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct); | 1070 *abs_mv_in_out_accumulator += fabs(this_frame->mv_in_out_count * motion_pct); |
| 1119 | 1071 |
| 1120 // Accumulate a measure of how uniform (or conversely how random) | 1072 // Accumulate a measure of how uniform (or conversely how random) |
| 1121 // the motion field is. (A ratio of absmv / mv) | 1073 // the motion field is (a ratio of absmv / mv). |
| 1122 if (motion_pct > 0.05) { | 1074 if (motion_pct > 0.05) { |
| 1123 const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / | 1075 const double this_frame_mvr_ratio = fabs(this_frame->mvr_abs) / |
| 1124 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); | 1076 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr)); |
| 1125 | 1077 |
| 1126 const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / | 1078 const double this_frame_mvc_ratio = fabs(this_frame->mvc_abs) / |
| 1127 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); | 1079 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc)); |
| 1128 | 1080 |
| 1129 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs) | 1081 *mv_ratio_accumulator += (this_frame_mvr_ratio < this_frame->mvr_abs) |
| 1130 ? (this_frame_mvr_ratio * motion_pct) | 1082 ? (this_frame_mvr_ratio * motion_pct) |
| 1131 : this_frame->mvr_abs * motion_pct; | 1083 : this_frame->mvr_abs * motion_pct; |
| 1132 | 1084 |
| 1133 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs) | 1085 *mv_ratio_accumulator += (this_frame_mvc_ratio < this_frame->mvc_abs) |
| 1134 ? (this_frame_mvc_ratio * motion_pct) | 1086 ? (this_frame_mvc_ratio * motion_pct) |
| 1135 : this_frame->mvc_abs * motion_pct; | 1087 : this_frame->mvc_abs * motion_pct; |
| 1136 } | 1088 } |
| 1137 } | 1089 } |
| 1138 | 1090 |
| 1139 // Calculate a baseline boost number for the current frame. | 1091 // Calculate a baseline boost number for the current frame. |
| 1140 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame, | 1092 static double calc_frame_boost(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame, |
| 1141 double this_frame_mv_in_out) { | 1093 double this_frame_mv_in_out) { |
| 1142 double frame_boost; | 1094 double frame_boost; |
| 1143 | 1095 |
| 1144 // Underlying boost factor is based on inter intra error ratio | 1096 // Underlying boost factor is based on inter intra error ratio. |
| 1145 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) | 1097 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min) |
| 1146 frame_boost = (IIFACTOR * this_frame->intra_error / | 1098 frame_boost = (IIFACTOR * this_frame->intra_error / |
| 1147 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); | 1099 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
| 1148 else | 1100 else |
| 1149 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min / | 1101 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min / |
| 1150 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); | 1102 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); |
| 1151 | 1103 |
| 1152 // Increase boost for frames where new data coming into frame | 1104 // Increase boost for frames where new data coming into frame (e.g. zoom out). |
| 1153 // (eg zoom out). Slightly reduce boost if there is a net balance | 1105 // Slightly reduce boost if there is a net balance of motion out of the frame |
| 1154 // of motion out of the frame (zoom in). | 1106 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. |
| 1155 // The range for this_frame_mv_in_out is -1.0 to +1.0 | |
| 1156 if (this_frame_mv_in_out > 0.0) | 1107 if (this_frame_mv_in_out > 0.0) |
| 1157 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); | 1108 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
| 1158 // In extreme case boost is halved | 1109 // In the extreme case the boost is halved. |
| 1159 else | 1110 else |
| 1160 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); | 1111 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
| 1161 | 1112 |
| 1162 return MIN(frame_boost, GF_RMAX); | 1113 return MIN(frame_boost, GF_RMAX); |
| 1163 } | 1114 } |
| 1164 | 1115 |
| 1165 static int calc_arf_boost(VP9_COMP *cpi, int offset, | 1116 static int calc_arf_boost(VP9_COMP *cpi, int offset, |
| 1166 int f_frames, int b_frames, | 1117 int f_frames, int b_frames, |
| 1167 int *f_boost, int *b_boost) { | 1118 int *f_boost, int *b_boost) { |
| 1168 FIRSTPASS_STATS this_frame; | 1119 FIRSTPASS_STATS this_frame; |
| 1169 struct twopass_rc *const twopass = &cpi->twopass; | 1120 struct twopass_rc *const twopass = &cpi->twopass; |
| 1170 int i; | 1121 int i; |
| 1171 double boost_score = 0.0; | 1122 double boost_score = 0.0; |
| 1172 double mv_ratio_accumulator = 0.0; | 1123 double mv_ratio_accumulator = 0.0; |
| 1173 double decay_accumulator = 1.0; | 1124 double decay_accumulator = 1.0; |
| 1174 double this_frame_mv_in_out = 0.0; | 1125 double this_frame_mv_in_out = 0.0; |
| 1175 double mv_in_out_accumulator = 0.0; | 1126 double mv_in_out_accumulator = 0.0; |
| 1176 double abs_mv_in_out_accumulator = 0.0; | 1127 double abs_mv_in_out_accumulator = 0.0; |
| 1177 int arf_boost; | 1128 int arf_boost; |
| 1178 int flash_detected = 0; | 1129 int flash_detected = 0; |
| 1179 | 1130 |
| 1180 // Search forward from the proposed arf/next gf position | 1131 // Search forward from the proposed arf/next gf position. |
| 1181 for (i = 0; i < f_frames; i++) { | 1132 for (i = 0; i < f_frames; ++i) { |
| 1182 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) | 1133 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) |
| 1183 break; | 1134 break; |
| 1184 | 1135 |
| 1185 // Update the motion related elements to the boost calculation | 1136 // Update the motion related elements to the boost calculation. |
| 1186 accumulate_frame_motion_stats(&this_frame, | 1137 accumulate_frame_motion_stats(&this_frame, |
| 1187 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1138 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1188 &abs_mv_in_out_accumulator, | 1139 &abs_mv_in_out_accumulator, |
| 1189 &mv_ratio_accumulator); | 1140 &mv_ratio_accumulator); |
| 1190 | 1141 |
| 1191 // We want to discount the flash frame itself and the recovery | 1142 // We want to discount the flash frame itself and the recovery |
| 1192 // frame that follows as both will have poor scores. | 1143 // frame that follows as both will have poor scores. |
| 1193 flash_detected = detect_flash(twopass, i + offset) || | 1144 flash_detected = detect_flash(twopass, i + offset) || |
| 1194 detect_flash(twopass, i + offset + 1); | 1145 detect_flash(twopass, i + offset + 1); |
| 1195 | 1146 |
| 1196 // Cumulative effect of prediction quality decay | 1147 // Accumulate the effect of prediction quality decay. |
| 1197 if (!flash_detected) { | 1148 if (!flash_detected) { |
| 1198 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); | 1149 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1199 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1150 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1200 ? MIN_DECAY_FACTOR : decay_accumulator; | 1151 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1201 } | 1152 } |
| 1202 | 1153 |
| 1203 boost_score += (decay_accumulator * | 1154 boost_score += (decay_accumulator * |
| 1204 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1155 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); |
| 1205 } | 1156 } |
| 1206 | 1157 |
| 1207 *f_boost = (int)boost_score; | 1158 *f_boost = (int)boost_score; |
| 1208 | 1159 |
| 1209 // Reset for backward looking loop | 1160 // Reset for backward looking loop. |
| 1210 boost_score = 0.0; | 1161 boost_score = 0.0; |
| 1211 mv_ratio_accumulator = 0.0; | 1162 mv_ratio_accumulator = 0.0; |
| 1212 decay_accumulator = 1.0; | 1163 decay_accumulator = 1.0; |
| 1213 this_frame_mv_in_out = 0.0; | 1164 this_frame_mv_in_out = 0.0; |
| 1214 mv_in_out_accumulator = 0.0; | 1165 mv_in_out_accumulator = 0.0; |
| 1215 abs_mv_in_out_accumulator = 0.0; | 1166 abs_mv_in_out_accumulator = 0.0; |
| 1216 | 1167 |
| 1217 // Search backward towards last gf position | 1168 // Search backward towards last gf position. |
| 1218 for (i = -1; i >= -b_frames; i--) { | 1169 for (i = -1; i >= -b_frames; --i) { |
| 1219 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) | 1170 if (read_frame_stats(twopass, &this_frame, (i + offset)) == EOF) |
| 1220 break; | 1171 break; |
| 1221 | 1172 |
| 1222 // Update the motion related elements to the boost calculation | 1173 // Update the motion related elements to the boost calculation. |
| 1223 accumulate_frame_motion_stats(&this_frame, | 1174 accumulate_frame_motion_stats(&this_frame, |
| 1224 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1175 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1225 &abs_mv_in_out_accumulator, | 1176 &abs_mv_in_out_accumulator, |
| 1226 &mv_ratio_accumulator); | 1177 &mv_ratio_accumulator); |
| 1227 | 1178 |
| 1228 // We want to discount the the flash frame itself and the recovery | 1179 // We want to discount the the flash frame itself and the recovery |
| 1229 // frame that follows as both will have poor scores. | 1180 // frame that follows as both will have poor scores. |
| 1230 flash_detected = detect_flash(twopass, i + offset) || | 1181 flash_detected = detect_flash(twopass, i + offset) || |
| 1231 detect_flash(twopass, i + offset + 1); | 1182 detect_flash(twopass, i + offset + 1); |
| 1232 | 1183 |
| 1233 // Cumulative effect of prediction quality decay | 1184 // Cumulative effect of prediction quality decay. |
| 1234 if (!flash_detected) { | 1185 if (!flash_detected) { |
| 1235 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); | 1186 decay_accumulator *= get_prediction_decay_rate(&cpi->common, &this_frame); |
| 1236 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 1187 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1237 ? MIN_DECAY_FACTOR : decay_accumulator; | 1188 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1238 } | 1189 } |
| 1239 | 1190 |
| 1240 boost_score += (decay_accumulator * | 1191 boost_score += (decay_accumulator * |
| 1241 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); | 1192 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out)); |
| 1242 } | 1193 } |
| 1243 *b_boost = (int)boost_score; | 1194 *b_boost = (int)boost_score; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1273 for (i = start; i <= end; ++i) { | 1224 for (i = start; i <= end; ++i) { |
| 1274 cfo[idx] = i; | 1225 cfo[idx] = i; |
| 1275 cpi->arf_buffer_idx[idx] = arf_idx; | 1226 cpi->arf_buffer_idx[idx] = arf_idx; |
| 1276 cpi->arf_weight[idx] = -1; | 1227 cpi->arf_weight[idx] = -1; |
| 1277 ++idx; | 1228 ++idx; |
| 1278 } | 1229 } |
| 1279 cpi->new_frame_coding_order_period = idx; | 1230 cpi->new_frame_coding_order_period = idx; |
| 1280 return; | 1231 return; |
| 1281 } | 1232 } |
| 1282 | 1233 |
| 1283 // ARF Group: work out the ARF schedule. | 1234 // ARF Group: Work out the ARF schedule and mark ARF frames as negative. |
| 1284 // Mark ARF frames as negative. | |
| 1285 if (end < 0) { | 1235 if (end < 0) { |
| 1286 // printf("start:%d end:%d\n", -end, -end); | 1236 // printf("start:%d end:%d\n", -end, -end); |
| 1287 // ARF frame is at the end of the range. | 1237 // ARF frame is at the end of the range. |
| 1288 cfo[idx] = end; | 1238 cfo[idx] = end; |
| 1289 // What ARF buffer does this ARF use as predictor. | 1239 // What ARF buffer does this ARF use as predictor. |
| 1290 cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2; | 1240 cpi->arf_buffer_idx[idx] = (arf_idx > 2) ? (arf_idx - 1) : 2; |
| 1291 cpi->arf_weight[idx] = level; | 1241 cpi->arf_weight[idx] = level; |
| 1292 ++idx; | 1242 ++idx; |
| 1293 abs_end = -end; | 1243 abs_end = -end; |
| 1294 } else { | 1244 } else { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1397 double boost_score = 0.0; | 1347 double boost_score = 0.0; |
| 1398 double old_boost_score = 0.0; | 1348 double old_boost_score = 0.0; |
| 1399 double gf_group_err = 0.0; | 1349 double gf_group_err = 0.0; |
| 1400 double gf_first_frame_err = 0.0; | 1350 double gf_first_frame_err = 0.0; |
| 1401 double mod_frame_err = 0.0; | 1351 double mod_frame_err = 0.0; |
| 1402 | 1352 |
| 1403 double mv_ratio_accumulator = 0.0; | 1353 double mv_ratio_accumulator = 0.0; |
| 1404 double decay_accumulator = 1.0; | 1354 double decay_accumulator = 1.0; |
| 1405 double zero_motion_accumulator = 1.0; | 1355 double zero_motion_accumulator = 1.0; |
| 1406 | 1356 |
| 1407 double loop_decay_rate = 1.00; // Starting decay rate | 1357 double loop_decay_rate = 1.00; |
| 1408 double last_loop_decay_rate = 1.00; | 1358 double last_loop_decay_rate = 1.00; |
| 1409 | 1359 |
| 1410 double this_frame_mv_in_out = 0.0; | 1360 double this_frame_mv_in_out = 0.0; |
| 1411 double mv_in_out_accumulator = 0.0; | 1361 double mv_in_out_accumulator = 0.0; |
| 1412 double abs_mv_in_out_accumulator = 0.0; | 1362 double abs_mv_in_out_accumulator = 0.0; |
| 1413 double mv_ratio_accumulator_thresh; | 1363 double mv_ratio_accumulator_thresh; |
| 1414 const int max_bits = frame_max_bits(cpi); // Max for a single frame | 1364 const int max_bits = frame_max_bits(cpi); // Max bits for a single frame. |
| 1415 | 1365 |
| 1416 unsigned int allow_alt_ref = cpi->oxcf.play_alternate && | 1366 unsigned int allow_alt_ref = cpi->oxcf.play_alternate && |
| 1417 cpi->oxcf.lag_in_frames; | 1367 cpi->oxcf.lag_in_frames; |
| 1418 | 1368 |
| 1419 int f_boost = 0; | 1369 int f_boost = 0; |
| 1420 int b_boost = 0; | 1370 int b_boost = 0; |
| 1421 int flash_detected; | 1371 int flash_detected; |
| 1422 int active_max_gf_interval; | 1372 int active_max_gf_interval; |
| 1423 RATE_CONTROL *const rc = &cpi->rc; | 1373 RATE_CONTROL *const rc = &cpi->rc; |
| 1424 | 1374 |
| 1425 twopass->gf_group_bits = 0; | 1375 twopass->gf_group_bits = 0; |
| 1426 | 1376 |
| 1427 vp9_clear_system_state(); // __asm emms; | 1377 vp9_clear_system_state(); |
| 1428 | 1378 |
| 1429 start_pos = twopass->stats_in; | 1379 start_pos = twopass->stats_in; |
| 1430 | 1380 |
| 1431 // Load stats for the current frame. | 1381 // Load stats for the current frame. |
| 1432 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1382 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1433 | 1383 |
| 1434 // Note the error of the frame at the start of the group (this will be | 1384 // Note the error of the frame at the start of the group. This will be |
| 1435 // the GF frame error if we code a normal gf | 1385 // the GF frame error if we code a normal gf. |
| 1436 gf_first_frame_err = mod_frame_err; | 1386 gf_first_frame_err = mod_frame_err; |
| 1437 | 1387 |
| 1438 // If this is a key frame or the overlay from a previous arf then | 1388 // If this is a key frame or the overlay from a previous arf then |
| 1439 // The error score / cost of this frame has already been accounted for. | 1389 // the error score / cost of this frame has already been accounted for. |
| 1440 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1390 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
| 1441 gf_group_err -= gf_first_frame_err; | 1391 gf_group_err -= gf_first_frame_err; |
| 1442 | 1392 |
| 1443 // Motion breakout threshold for loop below depends on image size. | 1393 // Motion breakout threshold for loop below depends on image size. |
| 1444 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0; | 1394 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0; |
| 1445 | 1395 |
| 1446 // Work out a maximum interval for the GF. | 1396 // Work out a maximum interval for the GF. |
| 1447 // If the image appears completely static we can extend beyond this. | 1397 // If the image appears completely static we can extend beyond this. |
| 1448 // The value chosen depends on the active Q range. At low Q we have | 1398 // The value chosen depends on the active Q range. At low Q we have |
| 1449 // bits to spare and are better with a smaller interval and smaller boost. | 1399 // bits to spare and are better with a smaller interval and smaller boost. |
| 1450 // At high Q when there are few bits to spare we are better with a longer | 1400 // At high Q when there are few bits to spare we are better with a longer |
| 1451 // interval to spread the cost of the GF. | 1401 // interval to spread the cost of the GF. |
| 1452 // | 1402 // |
| 1453 active_max_gf_interval = | 1403 active_max_gf_interval = |
| 1454 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); | 1404 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); |
| 1455 | 1405 |
| 1456 if (active_max_gf_interval > rc->max_gf_interval) | 1406 if (active_max_gf_interval > rc->max_gf_interval) |
| 1457 active_max_gf_interval = rc->max_gf_interval; | 1407 active_max_gf_interval = rc->max_gf_interval; |
| 1458 | 1408 |
| 1459 i = 0; | 1409 i = 0; |
| 1460 while (i < twopass->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1410 while (i < twopass->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1461 i++; // Increment the loop counter | 1411 ++i; |
| 1462 | 1412 |
| 1463 // Accumulate error score of frames in this gf group | 1413 // Accumulate error score of frames in this gf group. |
| 1464 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1414 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1465 gf_group_err += mod_frame_err; | 1415 gf_group_err += mod_frame_err; |
| 1466 | 1416 |
| 1467 if (EOF == input_stats(twopass, &next_frame)) | 1417 if (EOF == input_stats(twopass, &next_frame)) |
| 1468 break; | 1418 break; |
| 1469 | 1419 |
| 1470 // Test for the case where there is a brief flash but the prediction | 1420 // Test for the case where there is a brief flash but the prediction |
| 1471 // quality back to an earlier frame is then restored. | 1421 // quality back to an earlier frame is then restored. |
| 1472 flash_detected = detect_flash(twopass, 0); | 1422 flash_detected = detect_flash(twopass, 0); |
| 1473 | 1423 |
| 1474 // Update the motion related elements to the boost calculation | 1424 // Update the motion related elements to the boost calculation. |
| 1475 accumulate_frame_motion_stats(&next_frame, | 1425 accumulate_frame_motion_stats(&next_frame, |
| 1476 &this_frame_mv_in_out, &mv_in_out_accumulator, | 1426 &this_frame_mv_in_out, &mv_in_out_accumulator, |
| 1477 &abs_mv_in_out_accumulator, | 1427 &abs_mv_in_out_accumulator, |
| 1478 &mv_ratio_accumulator); | 1428 &mv_ratio_accumulator); |
| 1479 | 1429 |
| 1480 // Cumulative effect of prediction quality decay | 1430 // Accumulate the effect of prediction quality decay. |
| 1481 if (!flash_detected) { | 1431 if (!flash_detected) { |
| 1482 last_loop_decay_rate = loop_decay_rate; | 1432 last_loop_decay_rate = loop_decay_rate; |
| 1483 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 1433 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 1484 decay_accumulator = decay_accumulator * loop_decay_rate; | 1434 decay_accumulator = decay_accumulator * loop_decay_rate; |
| 1485 | 1435 |
| 1486 // Monitor for static sections. | 1436 // Monitor for static sections. |
| 1487 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1437 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 1488 zero_motion_accumulator) { | 1438 zero_motion_accumulator) { |
| 1489 zero_motion_accumulator = next_frame.pcnt_inter - | 1439 zero_motion_accumulator = next_frame.pcnt_inter - |
| 1490 next_frame.pcnt_motion; | 1440 next_frame.pcnt_motion; |
| 1491 } | 1441 } |
| 1492 | 1442 |
| 1493 // Break clause to detect very still sections after motion | 1443 // Break clause to detect very still sections after motion. For example, |
| 1494 // (for example a static image after a fade or other transition). | 1444 // a static image after a fade or other transition. |
| 1495 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, | 1445 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, |
| 1496 last_loop_decay_rate)) { | 1446 last_loop_decay_rate)) { |
| 1497 allow_alt_ref = 0; | 1447 allow_alt_ref = 0; |
| 1498 break; | 1448 break; |
| 1499 } | 1449 } |
| 1500 } | 1450 } |
| 1501 | 1451 |
| 1502 // Calculate a boost number for this frame | 1452 // Calculate a boost number for this frame. |
| 1503 boost_score += (decay_accumulator * | 1453 boost_score += (decay_accumulator * |
| 1504 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out)); | 1454 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out)); |
| 1505 | 1455 |
| 1506 // Break out conditions. | 1456 // Break out conditions. |
| 1507 if ( | 1457 if ( |
| 1508 // Break at cpi->max_gf_interval unless almost totally static | 1458 // Break at cpi->max_gf_interval unless almost totally static. |
| 1509 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || | 1459 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || |
| 1510 ( | 1460 ( |
| 1511 // Don't break out with a very short interval | 1461 // Don't break out with a very short interval. |
| 1512 (i > MIN_GF_INTERVAL) && | 1462 (i > MIN_GF_INTERVAL) && |
| 1513 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && | 1463 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) && |
| 1514 (!flash_detected) && | 1464 (!flash_detected) && |
| 1515 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || | 1465 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || |
| 1516 (abs_mv_in_out_accumulator > 3.0) || | 1466 (abs_mv_in_out_accumulator > 3.0) || |
| 1517 (mv_in_out_accumulator < -2.0) || | 1467 (mv_in_out_accumulator < -2.0) || |
| 1518 ((boost_score - old_boost_score) < IIFACTOR)))) { | 1468 ((boost_score - old_boost_score) < IIFACTOR)))) { |
| 1519 boost_score = old_boost_score; | 1469 boost_score = old_boost_score; |
| 1520 break; | 1470 break; |
| 1521 } | 1471 } |
| 1522 | 1472 |
| 1523 *this_frame = next_frame; | 1473 *this_frame = next_frame; |
| 1524 | 1474 |
| 1525 old_boost_score = boost_score; | 1475 old_boost_score = boost_score; |
| 1526 } | 1476 } |
| 1527 | 1477 |
| 1528 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1478 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
| 1529 | 1479 |
| 1530 // Don't allow a gf too near the next kf | 1480 // Don't allow a gf too near the next kf. |
| 1531 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { | 1481 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) { |
| 1532 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { | 1482 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) { |
| 1533 i++; | 1483 ++i; |
| 1534 | 1484 |
| 1535 if (EOF == input_stats(twopass, this_frame)) | 1485 if (EOF == input_stats(twopass, this_frame)) |
| 1536 break; | 1486 break; |
| 1537 | 1487 |
| 1538 if (i < rc->frames_to_key) { | 1488 if (i < rc->frames_to_key) { |
| 1539 mod_frame_err = calculate_modified_err(cpi, this_frame); | 1489 mod_frame_err = calculate_modified_err(cpi, this_frame); |
| 1540 gf_group_err += mod_frame_err; | 1490 gf_group_err += mod_frame_err; |
| 1541 } | 1491 } |
| 1542 } | 1492 } |
| 1543 } | 1493 } |
| 1544 | 1494 |
| 1545 #if CONFIG_MULTIPLE_ARF | 1495 #if CONFIG_MULTIPLE_ARF |
| 1546 if (cpi->multi_arf_enabled) { | 1496 if (cpi->multi_arf_enabled) { |
| 1547 // Initialize frame coding order variables. | 1497 // Initialize frame coding order variables. |
| 1548 cpi->new_frame_coding_order_period = 0; | 1498 cpi->new_frame_coding_order_period = 0; |
| 1549 cpi->next_frame_in_order = 0; | 1499 cpi->next_frame_in_order = 0; |
| 1550 cpi->arf_buffered = 0; | 1500 cpi->arf_buffered = 0; |
| 1551 vp9_zero(cpi->frame_coding_order); | 1501 vp9_zero(cpi->frame_coding_order); |
| 1552 vp9_zero(cpi->arf_buffer_idx); | 1502 vp9_zero(cpi->arf_buffer_idx); |
| 1553 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); | 1503 vpx_memset(cpi->arf_weight, -1, sizeof(cpi->arf_weight)); |
| 1554 } | 1504 } |
| 1555 #endif | 1505 #endif |
| 1556 | 1506 |
| 1557 // Set the interval until the next gf. | 1507 // Set the interval until the next gf. |
| 1558 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1508 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
| 1559 rc->baseline_gf_interval = i - 1; | 1509 rc->baseline_gf_interval = i - 1; |
| 1560 else | 1510 else |
| 1561 rc->baseline_gf_interval = i; | 1511 rc->baseline_gf_interval = i; |
| 1562 | 1512 |
| 1563 // Should we use the alternate reference frame | 1513 // Should we use the alternate reference frame. |
| 1564 if (allow_alt_ref && | 1514 if (allow_alt_ref && |
| 1565 (i < cpi->oxcf.lag_in_frames) && | 1515 (i < cpi->oxcf.lag_in_frames) && |
| 1566 (i >= MIN_GF_INTERVAL) && | 1516 (i >= MIN_GF_INTERVAL) && |
| 1567 // for real scene cuts (not forced kfs) dont allow arf very near kf. | 1517 // For real scene cuts (not forced kfs) don't allow arf very near kf. |
| 1568 (rc->next_key_frame_forced || | 1518 (rc->next_key_frame_forced || |
| 1569 (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) { | 1519 (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) { |
| 1570 // Alternative boost calculation for alt ref | 1520 // Calculate the boost for alt ref. |
| 1571 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, | 1521 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
| 1572 &b_boost); | 1522 &b_boost); |
| 1573 rc->source_alt_ref_pending = 1; | 1523 rc->source_alt_ref_pending = 1; |
| 1574 | 1524 |
| 1575 #if CONFIG_MULTIPLE_ARF | 1525 #if CONFIG_MULTIPLE_ARF |
| 1576 // Set the ARF schedule. | 1526 // Set the ARF schedule. |
| 1577 if (cpi->multi_arf_enabled) { | 1527 if (cpi->multi_arf_enabled) { |
| 1578 schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0); | 1528 schedule_frames(cpi, 0, -(rc->baseline_gf_interval - 1), 2, 1, 0); |
| 1579 } | 1529 } |
| 1580 #endif | 1530 #endif |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1622 printf("\n"); | 1572 printf("\n"); |
| 1623 printf("Weight: "); | 1573 printf("Weight: "); |
| 1624 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { | 1574 for (i = 0; i < cpi->new_frame_coding_order_period; ++i) { |
| 1625 printf("%4d ", cpi->arf_weight[i]); | 1575 printf("%4d ", cpi->arf_weight[i]); |
| 1626 } | 1576 } |
| 1627 printf("\n"); | 1577 printf("\n"); |
| 1628 } | 1578 } |
| 1629 #endif | 1579 #endif |
| 1630 #endif | 1580 #endif |
| 1631 | 1581 |
| 1632 // Calculate the bits to be allocated to the group as a whole | 1582 // Calculate the bits to be allocated to the group as a whole. |
| 1633 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { | 1583 if (twopass->kf_group_bits > 0 && twopass->kf_group_error_left > 0) { |
| 1634 twopass->gf_group_bits = (int64_t)(cpi->twopass.kf_group_bits * | 1584 twopass->gf_group_bits = (int64_t)(cpi->twopass.kf_group_bits * |
| 1635 (gf_group_err / cpi->twopass.kf_group_error_left)); | 1585 (gf_group_err / cpi->twopass.kf_group_error_left)); |
| 1636 } else { | 1586 } else { |
| 1637 twopass->gf_group_bits = 0; | 1587 twopass->gf_group_bits = 0; |
| 1638 } | 1588 } |
| 1639 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? | 1589 twopass->gf_group_bits = (twopass->gf_group_bits < 0) ? |
| 1640 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? | 1590 0 : (twopass->gf_group_bits > twopass->kf_group_bits) ? |
| 1641 twopass->kf_group_bits : twopass->gf_group_bits; | 1591 twopass->kf_group_bits : twopass->gf_group_bits; |
| 1642 | 1592 |
| 1643 // Clip cpi->twopass.gf_group_bits based on user supplied data rate | 1593 // Clip cpi->twopass.gf_group_bits based on user supplied data rate |
| 1644 // variability limit (cpi->oxcf.two_pass_vbrmax_section) | 1594 // variability limit, cpi->oxcf.two_pass_vbrmax_section. |
| 1645 if (twopass->gf_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) | 1595 if (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; | 1596 twopass->gf_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; |
| 1647 | 1597 |
| 1648 // Reset the file position | 1598 // Reset the file position. |
| 1649 reset_fpf_position(twopass, start_pos); | 1599 reset_fpf_position(twopass, start_pos); |
| 1650 | 1600 |
| 1651 // Assign bits to the arf or gf. | 1601 // Assign bits to the arf or gf. |
| 1652 for (i = 0; i <= (rc->source_alt_ref_pending && | 1602 for (i = 0; i <= (rc->source_alt_ref_pending && |
| 1653 cpi->common.frame_type != KEY_FRAME); ++i) { | 1603 cpi->common.frame_type != KEY_FRAME); ++i) { |
| 1654 int allocation_chunks; | 1604 int allocation_chunks; |
| 1655 int q = rc->last_q[INTER_FRAME]; | 1605 int q = rc->last_q[INTER_FRAME]; |
| 1656 int gf_bits; | 1606 int gf_bits; |
| 1657 | 1607 |
| 1658 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; | 1608 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; |
| 1659 | 1609 |
| 1660 // Set max and minimum boost and hence minimum allocation | 1610 // Set max and minimum boost and hence minimum allocation. |
| 1661 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); | 1611 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); |
| 1662 | 1612 |
| 1663 if (rc->source_alt_ref_pending && i == 0) | 1613 if (rc->source_alt_ref_pending && i == 0) |
| 1664 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost; | 1614 allocation_chunks = ((rc->baseline_gf_interval + 1) * 100) + boost; |
| 1665 else | 1615 else |
| 1666 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100); | 1616 allocation_chunks = (rc->baseline_gf_interval * 100) + (boost - 100); |
| 1667 | 1617 |
| 1668 // Prevent overflow | 1618 // Prevent overflow. |
| 1669 if (boost > 1023) { | 1619 if (boost > 1023) { |
| 1670 int divisor = boost >> 10; | 1620 int divisor = boost >> 10; |
| 1671 boost /= divisor; | 1621 boost /= divisor; |
| 1672 allocation_chunks /= divisor; | 1622 allocation_chunks /= divisor; |
| 1673 } | 1623 } |
| 1674 | 1624 |
| 1675 // Calculate the number of bits to be spent on the gf or arf based on | 1625 // Calculate the number of bits to be spent on the gf or arf based on |
| 1676 // the boost number | 1626 // the boost number. |
| 1677 gf_bits = (int)((double)boost * (twopass->gf_group_bits / | 1627 gf_bits = (int)((double)boost * (twopass->gf_group_bits / |
| 1678 (double)allocation_chunks)); | 1628 (double)allocation_chunks)); |
| 1679 | 1629 |
| 1680 // If the frame that is to be boosted is simpler than the average for | 1630 // If the frame that is to be boosted is simpler than the average for |
| 1681 // the gf/arf group then use an alternative calculation | 1631 // the gf/arf group then use an alternative calculation |
| 1682 // based on the error score of the frame itself | 1632 // based on the error score of the frame itself. |
| 1683 if (rc->baseline_gf_interval < 1 || | 1633 if (rc->baseline_gf_interval < 1 || |
| 1684 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) { | 1634 mod_frame_err < gf_group_err / (double)rc->baseline_gf_interval) { |
| 1685 double alt_gf_grp_bits = (double)twopass->kf_group_bits * | 1635 double alt_gf_grp_bits = (double)twopass->kf_group_bits * |
| 1686 (mod_frame_err * (double)rc->baseline_gf_interval) / | 1636 (mod_frame_err * (double)rc->baseline_gf_interval) / |
| 1687 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left); | 1637 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left); |
| 1688 | 1638 |
| 1689 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / | 1639 int alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits / |
| 1690 (double)allocation_chunks)); | 1640 (double)allocation_chunks)); |
| 1691 | 1641 |
| 1692 if (gf_bits > alt_gf_bits) | 1642 if (gf_bits > alt_gf_bits) |
| 1693 gf_bits = alt_gf_bits; | 1643 gf_bits = alt_gf_bits; |
| 1694 } else { | 1644 } else { |
| 1695 // If it is harder than other frames in the group make sure it at | 1645 // If it is harder than other frames in the group make sure it at |
| 1696 // least receives an allocation in keeping with its relative error | 1646 // least receives an allocation in keeping with its relative error |
| 1697 // score, otherwise it may be worse off than an "un-boosted" frame. | 1647 // score, otherwise it may be worse off than an "un-boosted" frame. |
| 1698 int alt_gf_bits = (int)((double)twopass->kf_group_bits * | 1648 int alt_gf_bits = (int)((double)twopass->kf_group_bits * |
| 1699 mod_frame_err / | 1649 mod_frame_err / |
| 1700 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left)); | 1650 DOUBLE_DIVIDE_CHECK(twopass->kf_group_error_left)); |
| 1701 | 1651 |
| 1702 if (alt_gf_bits > gf_bits) | 1652 if (alt_gf_bits > gf_bits) |
| 1703 gf_bits = alt_gf_bits; | 1653 gf_bits = alt_gf_bits; |
| 1704 } | 1654 } |
| 1705 | 1655 |
| 1706 // Dont allow a negative value for gf_bits | 1656 // Don't allow a negative value for gf_bits. |
| 1707 if (gf_bits < 0) | 1657 if (gf_bits < 0) |
| 1708 gf_bits = 0; | 1658 gf_bits = 0; |
| 1709 | 1659 |
| 1710 if (i == 0) { | 1660 if (i == 0) { |
| 1711 twopass->gf_bits = gf_bits; | 1661 twopass->gf_bits = gf_bits; |
| 1712 } | 1662 } |
| 1713 if (i == 1 || | 1663 if (i == 1 || |
| 1714 (!rc->source_alt_ref_pending && | 1664 (!rc->source_alt_ref_pending && |
| 1715 cpi->common.frame_type != KEY_FRAME)) { | 1665 cpi->common.frame_type != KEY_FRAME)) { |
| 1716 // Per frame bit target for this frame | 1666 // Calculate the per frame bit target for this frame. |
| 1717 vp9_rc_set_frame_target(cpi, gf_bits); | 1667 vp9_rc_set_frame_target(cpi, gf_bits); |
| 1718 } | 1668 } |
| 1719 } | 1669 } |
| 1720 | 1670 |
| 1721 { | 1671 { |
| 1722 // Adjust KF group bits and error remaining | 1672 // Adjust KF group bits and error remaining. |
| 1723 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1673 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1724 twopass->kf_group_bits -= twopass->gf_group_bits; | 1674 twopass->kf_group_bits -= twopass->gf_group_bits; |
| 1725 | 1675 |
| 1726 if (twopass->kf_group_bits < 0) | 1676 if (twopass->kf_group_bits < 0) |
| 1727 twopass->kf_group_bits = 0; | 1677 twopass->kf_group_bits = 0; |
| 1728 | 1678 |
| 1729 // If this is an arf update we want to remove the score for the | 1679 // If this is an arf update we want to remove the score for the overlay |
| 1730 // overlay frame at the end which will usually be very cheap to code. | 1680 // frame at the end which will usually be very cheap to code. |
| 1731 // The overlay frame has already in effect been coded so we want to spread | 1681 // The overlay frame has already, in effect, been coded so we want to spread |
| 1732 // the remaining bits amoung the other frames/ | 1682 // the remaining bits among the other frames. |
| 1733 // For normal GFs remove the score for the GF itself unless this is | 1683 // For normal GFs remove the score for the GF itself unless this is |
| 1734 // also a key frame in which case it has already been accounted for. | 1684 // also a key frame in which case it has already been accounted for. |
| 1735 if (rc->source_alt_ref_pending) { | 1685 if (rc->source_alt_ref_pending) { |
| 1736 twopass->gf_group_error_left = (int64_t)gf_group_err - mod_frame_err; | 1686 twopass->gf_group_error_left = (int64_t)(gf_group_err - mod_frame_err); |
| 1737 } else if (cpi->common.frame_type != KEY_FRAME) { | 1687 } else if (cpi->common.frame_type != KEY_FRAME) { |
| 1738 twopass->gf_group_error_left = (int64_t)(gf_group_err | 1688 twopass->gf_group_error_left = (int64_t)(gf_group_err |
| 1739 - gf_first_frame_err); | 1689 - gf_first_frame_err); |
| 1740 } else { | 1690 } else { |
| 1741 twopass->gf_group_error_left = (int64_t)gf_group_err; | 1691 twopass->gf_group_error_left = (int64_t)gf_group_err; |
| 1742 } | 1692 } |
| 1743 | 1693 |
| 1744 twopass->gf_group_bits -= twopass->gf_bits; | 1694 twopass->gf_group_bits -= twopass->gf_bits; |
| 1745 | 1695 |
| 1746 if (twopass->gf_group_bits < 0) | 1696 if (twopass->gf_group_bits < 0) |
| 1747 twopass->gf_group_bits = 0; | 1697 twopass->gf_group_bits = 0; |
| 1748 | 1698 |
| 1749 // This condition could fail if there are two kfs very close together | 1699 // This condition could fail if there are two kfs very close together |
| 1750 // despite (MIN_GF_INTERVAL) and would cause a divide by 0 in the | 1700 // despite MIN_GF_INTERVAL and would cause a divide by 0 in the |
| 1751 // calculation of alt_extra_bits. | 1701 // calculation of alt_extra_bits. |
| 1752 if (rc->baseline_gf_interval >= 3) { | 1702 if (rc->baseline_gf_interval >= 3) { |
| 1753 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; | 1703 const int boost = rc->source_alt_ref_pending ? b_boost : rc->gfu_boost; |
| 1754 | 1704 |
| 1755 if (boost >= 150) { | 1705 if (boost >= 150) { |
| 1756 const int pct_extra = MIN(20, (boost - 100) / 50); | 1706 const int pct_extra = MIN(20, (boost - 100) / 50); |
| 1757 const int alt_extra_bits = (int)((twopass->gf_group_bits * pct_extra) / | 1707 const int alt_extra_bits = (int)((twopass->gf_group_bits * pct_extra) / |
| 1758 100); | 1708 100); |
| 1759 twopass->gf_group_bits -= alt_extra_bits; | 1709 twopass->gf_group_bits -= alt_extra_bits; |
| 1760 } | 1710 } |
| 1761 } | 1711 } |
| 1762 } | 1712 } |
| 1763 | 1713 |
| 1764 if (cpi->common.frame_type != KEY_FRAME) { | 1714 if (cpi->common.frame_type != KEY_FRAME) { |
| 1765 FIRSTPASS_STATS sectionstats; | 1715 FIRSTPASS_STATS sectionstats; |
| 1766 | 1716 |
| 1767 zero_stats(§ionstats); | 1717 zero_stats(§ionstats); |
| 1768 reset_fpf_position(twopass, start_pos); | 1718 reset_fpf_position(twopass, start_pos); |
| 1769 | 1719 |
| 1770 for (i = 0; i < rc->baseline_gf_interval; i++) { | 1720 for (i = 0; i < rc->baseline_gf_interval; ++i) { |
| 1771 input_stats(twopass, &next_frame); | 1721 input_stats(twopass, &next_frame); |
| 1772 accumulate_stats(§ionstats, &next_frame); | 1722 accumulate_stats(§ionstats, &next_frame); |
| 1773 } | 1723 } |
| 1774 | 1724 |
| 1775 avg_stats(§ionstats); | 1725 avg_stats(§ionstats); |
| 1776 | 1726 |
| 1777 twopass->section_intra_rating = (int) | 1727 twopass->section_intra_rating = (int) |
| 1778 (sectionstats.intra_error / | 1728 (sectionstats.intra_error / |
| 1779 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 1729 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 1780 | 1730 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1816 // Per frame bit target for this frame. | 1766 // Per frame bit target for this frame. |
| 1817 vp9_rc_set_frame_target(cpi, target_frame_size); | 1767 vp9_rc_set_frame_target(cpi, target_frame_size); |
| 1818 } | 1768 } |
| 1819 | 1769 |
| 1820 static int test_candidate_kf(VP9_COMP *cpi, | 1770 static int test_candidate_kf(VP9_COMP *cpi, |
| 1821 const FIRSTPASS_STATS *last_frame, | 1771 const FIRSTPASS_STATS *last_frame, |
| 1822 const FIRSTPASS_STATS *this_frame, | 1772 const FIRSTPASS_STATS *this_frame, |
| 1823 const FIRSTPASS_STATS *next_frame) { | 1773 const FIRSTPASS_STATS *next_frame) { |
| 1824 int is_viable_kf = 0; | 1774 int is_viable_kf = 0; |
| 1825 | 1775 |
| 1826 // Does the frame satisfy the primary criteria of a key frame | 1776 // Does the frame satisfy the primary criteria of a key frame? |
| 1827 // If so, then examine how well it predicts subsequent frames | 1777 // If so, then examine how well it predicts subsequent frames. |
| 1828 if ((this_frame->pcnt_second_ref < 0.10) && | 1778 if ((this_frame->pcnt_second_ref < 0.10) && |
| 1829 (next_frame->pcnt_second_ref < 0.10) && | 1779 (next_frame->pcnt_second_ref < 0.10) && |
| 1830 ((this_frame->pcnt_inter < 0.05) || | 1780 ((this_frame->pcnt_inter < 0.05) || |
| 1831 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) && | 1781 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) && |
| 1832 ((this_frame->intra_error / | 1782 ((this_frame->intra_error / |
| 1833 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && | 1783 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && |
| 1834 ((fabs(last_frame->coded_error - this_frame->coded_error) / | 1784 ((fabs(last_frame->coded_error - this_frame->coded_error) / |
| 1835 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > | 1785 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) || |
| 1836 .40) || | |
| 1837 (fabs(last_frame->intra_error - this_frame->intra_error) / | 1786 (fabs(last_frame->intra_error - this_frame->intra_error) / |
| 1838 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > | 1787 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) || |
| 1839 .40) || | |
| 1840 ((next_frame->intra_error / | 1788 ((next_frame->intra_error / |
| 1841 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { | 1789 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { |
| 1842 int i; | 1790 int i; |
| 1843 FIRSTPASS_STATS *start_pos; | 1791 FIRSTPASS_STATS *start_pos; |
| 1844 | 1792 |
| 1845 FIRSTPASS_STATS local_next_frame; | 1793 FIRSTPASS_STATS local_next_frame; |
| 1846 | 1794 |
| 1847 double boost_score = 0.0; | 1795 double boost_score = 0.0; |
| 1848 double old_boost_score = 0.0; | 1796 double old_boost_score = 0.0; |
| 1849 double decay_accumulator = 1.0; | 1797 double decay_accumulator = 1.0; |
| 1850 | 1798 |
| 1851 local_next_frame = *next_frame; | 1799 local_next_frame = *next_frame; |
| 1852 | 1800 |
| 1853 // Note the starting file position so we can reset to it | 1801 // Note the starting file position so we can reset to it. |
| 1854 start_pos = cpi->twopass.stats_in; | 1802 start_pos = cpi->twopass.stats_in; |
| 1855 | 1803 |
| 1856 // Examine how well the key frame predicts subsequent frames | 1804 // Examine how well the key frame predicts subsequent frames. |
| 1857 for (i = 0; i < 16; i++) { | 1805 for (i = 0; i < 16; ++i) { |
| 1858 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / | 1806 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / |
| 1859 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); | 1807 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); |
| 1860 | 1808 |
| 1861 if (next_iiratio > RMAX) | 1809 if (next_iiratio > RMAX) |
| 1862 next_iiratio = RMAX; | 1810 next_iiratio = RMAX; |
| 1863 | 1811 |
| 1864 // Cumulative effect of decay in prediction quality | 1812 // Cumulative effect of decay in prediction quality. |
| 1865 if (local_next_frame.pcnt_inter > 0.85) | 1813 if (local_next_frame.pcnt_inter > 0.85) |
| 1866 decay_accumulator *= local_next_frame.pcnt_inter; | 1814 decay_accumulator *= local_next_frame.pcnt_inter; |
| 1867 else | 1815 else |
| 1868 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0; | 1816 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0; |
| 1869 | 1817 |
| 1870 // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter; | 1818 // Keep a running total. |
| 1871 | |
| 1872 // Keep a running total | |
| 1873 boost_score += (decay_accumulator * next_iiratio); | 1819 boost_score += (decay_accumulator * next_iiratio); |
| 1874 | 1820 |
| 1875 // Test various breakout clauses | 1821 // Test various breakout clauses. |
| 1876 if ((local_next_frame.pcnt_inter < 0.05) || | 1822 if ((local_next_frame.pcnt_inter < 0.05) || |
| 1877 (next_iiratio < 1.5) || | 1823 (next_iiratio < 1.5) || |
| 1878 (((local_next_frame.pcnt_inter - | 1824 (((local_next_frame.pcnt_inter - |
| 1879 local_next_frame.pcnt_neutral) < 0.20) && | 1825 local_next_frame.pcnt_neutral) < 0.20) && |
| 1880 (next_iiratio < 3.0)) || | 1826 (next_iiratio < 3.0)) || |
| 1881 ((boost_score - old_boost_score) < 3.0) || | 1827 ((boost_score - old_boost_score) < 3.0) || |
| 1882 (local_next_frame.intra_error < 200) | 1828 (local_next_frame.intra_error < 200)) { |
| 1883 ) { | |
| 1884 break; | 1829 break; |
| 1885 } | 1830 } |
| 1886 | 1831 |
| 1887 old_boost_score = boost_score; | 1832 old_boost_score = boost_score; |
| 1888 | 1833 |
| 1889 // Get the next frame details | 1834 // Get the next frame details |
| 1890 if (EOF == input_stats(&cpi->twopass, &local_next_frame)) | 1835 if (EOF == input_stats(&cpi->twopass, &local_next_frame)) |
| 1891 break; | 1836 break; |
| 1892 } | 1837 } |
| 1893 | 1838 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1920 | 1865 |
| 1921 double kf_mod_err = 0.0; | 1866 double kf_mod_err = 0.0; |
| 1922 double kf_group_err = 0.0; | 1867 double kf_group_err = 0.0; |
| 1923 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; | 1868 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
| 1924 | 1869 |
| 1925 RATE_CONTROL *const rc = &cpi->rc; | 1870 RATE_CONTROL *const rc = &cpi->rc; |
| 1926 struct twopass_rc *const twopass = &cpi->twopass; | 1871 struct twopass_rc *const twopass = &cpi->twopass; |
| 1927 | 1872 |
| 1928 vp9_zero(next_frame); | 1873 vp9_zero(next_frame); |
| 1929 | 1874 |
| 1930 vp9_clear_system_state(); // __asm emms; | 1875 vp9_clear_system_state(); |
| 1931 | 1876 |
| 1932 start_position = twopass->stats_in; | 1877 start_position = twopass->stats_in; |
| 1933 cpi->common.frame_type = KEY_FRAME; | 1878 cpi->common.frame_type = KEY_FRAME; |
| 1934 | 1879 |
| 1935 // is this a forced key frame by interval | 1880 // Is this a forced key frame by interval. |
| 1936 rc->this_key_frame_forced = rc->next_key_frame_forced; | 1881 rc->this_key_frame_forced = rc->next_key_frame_forced; |
| 1937 | 1882 |
| 1938 // Clear the alt ref active flag as this can never be active on a key frame | 1883 // Clear the alt ref active flag as this can never be active on a key frame. |
| 1939 rc->source_alt_ref_active = 0; | 1884 rc->source_alt_ref_active = 0; |
| 1940 | 1885 |
| 1941 // Kf is always a gf so clear frames till next gf counter | 1886 // KF is always a GF so clear frames till next gf counter. |
| 1942 rc->frames_till_gf_update_due = 0; | 1887 rc->frames_till_gf_update_due = 0; |
| 1943 | 1888 |
| 1944 rc->frames_to_key = 1; | 1889 rc->frames_to_key = 1; |
| 1945 | 1890 |
| 1946 // Take a copy of the initial frame details | 1891 // Take a copy of the initial frame details. |
| 1947 first_frame = *this_frame; | 1892 first_frame = *this_frame; |
| 1948 | 1893 |
| 1949 twopass->kf_group_bits = 0; // Total bits available to kf group | 1894 twopass->kf_group_bits = 0; // Total bits available to kf group |
| 1950 twopass->kf_group_error_left = 0; // Group modified error score. | 1895 twopass->kf_group_error_left = 0; // Group modified error score. |
| 1951 | 1896 |
| 1952 kf_mod_err = calculate_modified_err(cpi, this_frame); | 1897 kf_mod_err = calculate_modified_err(cpi, this_frame); |
| 1953 | 1898 |
| 1954 // find the next keyframe | 1899 // Find the next keyframe. |
| 1955 i = 0; | 1900 i = 0; |
| 1956 while (twopass->stats_in < twopass->stats_in_end) { | 1901 while (twopass->stats_in < twopass->stats_in_end) { |
| 1957 // Accumulate kf group error | 1902 // Accumulate kf group error. |
| 1958 kf_group_err += calculate_modified_err(cpi, this_frame); | 1903 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 1959 | 1904 |
| 1960 // load a the next frame's stats | 1905 // Load the next frame's stats. |
| 1961 last_frame = *this_frame; | 1906 last_frame = *this_frame; |
| 1962 input_stats(twopass, this_frame); | 1907 input_stats(twopass, this_frame); |
| 1963 | 1908 |
| 1964 // Provided that we are not at the end of the file... | 1909 // Provided that we are not at the end of the file... |
| 1965 if (cpi->oxcf.auto_key && | 1910 if (cpi->oxcf.auto_key && |
| 1966 lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 1911 lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 1967 // Normal scene cut check | 1912 // Check for a scene cut. |
| 1968 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) | 1913 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) |
| 1969 break; | 1914 break; |
| 1970 | 1915 |
| 1971 | 1916 // How fast is the prediction quality decaying? |
| 1972 // How fast is prediction quality decaying | |
| 1973 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 1917 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 1974 | 1918 |
| 1975 // We want to know something about the recent past... rather than | 1919 // We want to know something about the recent past... rather than |
| 1976 // as used elsewhere where we are concened with decay in prediction | 1920 // as used elsewhere where we are concerned with decay in prediction |
| 1977 // quality since the last GF or KF. | 1921 // quality since the last GF or KF. |
| 1978 recent_loop_decay[i % 8] = loop_decay_rate; | 1922 recent_loop_decay[i % 8] = loop_decay_rate; |
| 1979 decay_accumulator = 1.0; | 1923 decay_accumulator = 1.0; |
| 1980 for (j = 0; j < 8; j++) | 1924 for (j = 0; j < 8; ++j) |
| 1981 decay_accumulator *= recent_loop_decay[j]; | 1925 decay_accumulator *= recent_loop_decay[j]; |
| 1982 | 1926 |
| 1983 // Special check for transition or high motion followed by a | 1927 // Special check for transition or high motion followed by a |
| 1984 // to a static scene. | 1928 // static scene. |
| 1985 if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i, | 1929 if (detect_transition_to_still(cpi, i, cpi->key_frame_frequency - i, |
| 1986 loop_decay_rate, decay_accumulator)) | 1930 loop_decay_rate, decay_accumulator)) |
| 1987 break; | 1931 break; |
| 1988 | 1932 |
| 1989 // Step on to the next frame | 1933 // Step on to the next frame. |
| 1990 rc->frames_to_key++; | 1934 ++rc->frames_to_key; |
| 1991 | 1935 |
| 1992 // If we don't have a real key frame within the next two | 1936 // If we don't have a real key frame within the next two |
| 1993 // forcekeyframeevery intervals then break out of the loop. | 1937 // key_frame_frequency intervals then break out of the loop. |
| 1994 if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency) | 1938 if (rc->frames_to_key >= 2 * (int)cpi->key_frame_frequency) |
| 1995 break; | 1939 break; |
| 1996 } else { | 1940 } else { |
| 1997 rc->frames_to_key++; | 1941 ++rc->frames_to_key; |
| 1998 } | 1942 } |
| 1999 i++; | 1943 ++i; |
| 2000 } | 1944 } |
| 2001 | 1945 |
| 2002 // If there is a max kf interval set by the user we must obey it. | 1946 // If there is a max kf interval set by the user we must obey it. |
| 2003 // We already breakout of the loop above at 2x max. | 1947 // We already breakout of the loop above at 2x max. |
| 2004 // This code centers the extra kf if the actual natural | 1948 // This code centers the extra kf if the actual natural interval |
| 2005 // interval is between 1x and 2x | 1949 // is between 1x and 2x. |
| 2006 if (cpi->oxcf.auto_key && | 1950 if (cpi->oxcf.auto_key && |
| 2007 rc->frames_to_key > (int)cpi->key_frame_frequency) { | 1951 rc->frames_to_key > (int)cpi->key_frame_frequency) { |
| 2008 FIRSTPASS_STATS tmp_frame; | 1952 FIRSTPASS_STATS tmp_frame; |
| 2009 | 1953 |
| 2010 rc->frames_to_key /= 2; | 1954 rc->frames_to_key /= 2; |
| 2011 | 1955 |
| 2012 // Copy first frame details | 1956 // Copy first frame details. |
| 2013 tmp_frame = first_frame; | 1957 tmp_frame = first_frame; |
| 2014 | 1958 |
| 2015 // Reset to the start of the group | 1959 // Reset to the start of the group. |
| 2016 reset_fpf_position(twopass, start_position); | 1960 reset_fpf_position(twopass, start_position); |
| 2017 | 1961 |
| 2018 kf_group_err = 0; | 1962 kf_group_err = 0; |
| 2019 | 1963 |
| 2020 // Rescan to get the correct error data for the forced kf group | 1964 // Rescan to get the correct error data for the forced kf group. |
| 2021 for (i = 0; i < rc->frames_to_key; i++) { | 1965 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2022 // Accumulate kf group errors | 1966 // Accumulate kf group errors. |
| 2023 kf_group_err += calculate_modified_err(cpi, &tmp_frame); | 1967 kf_group_err += calculate_modified_err(cpi, &tmp_frame); |
| 2024 | 1968 |
| 2025 // Load the next frame's stats. | 1969 // Load the next frame's stats. |
| 2026 input_stats(twopass, &tmp_frame); | 1970 input_stats(twopass, &tmp_frame); |
| 2027 } | 1971 } |
| 2028 rc->next_key_frame_forced = 1; | 1972 rc->next_key_frame_forced = 1; |
| 2029 } else if (twopass->stats_in == twopass->stats_in_end) { | 1973 } else if (twopass->stats_in == twopass->stats_in_end) { |
| 2030 rc->next_key_frame_forced = 1; | 1974 rc->next_key_frame_forced = 1; |
| 2031 } else { | 1975 } else { |
| 2032 rc->next_key_frame_forced = 0; | 1976 rc->next_key_frame_forced = 0; |
| 2033 } | 1977 } |
| 2034 | 1978 |
| 2035 // Special case for the last key frame of the file | 1979 // Special case for the last key frame of the file. |
| 2036 if (twopass->stats_in >= twopass->stats_in_end) { | 1980 if (twopass->stats_in >= twopass->stats_in_end) { |
| 2037 // Accumulate kf group error | 1981 // Accumulate kf group error. |
| 2038 kf_group_err += calculate_modified_err(cpi, this_frame); | 1982 kf_group_err += calculate_modified_err(cpi, this_frame); |
| 2039 } | 1983 } |
| 2040 | 1984 |
| 2041 // Calculate the number of bits that should be assigned to the kf group. | 1985 // Calculate the number of bits that should be assigned to the kf group. |
| 2042 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { | 1986 if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { |
| 2043 // Max for a single normal frame (not key frame) | 1987 // Maximum number of bits for a single normal frame (not key frame). |
| 2044 int max_bits = frame_max_bits(cpi); | 1988 int max_bits = frame_max_bits(cpi); |
| 2045 | 1989 |
| 2046 // Maximum bits for the kf group | 1990 // Maximum number of bits allocated to the key frame group. |
| 2047 int64_t max_grp_bits; | 1991 int64_t max_grp_bits; |
| 2048 | 1992 |
| 2049 // Default allocation based on bits left and relative | 1993 // Default allocation based on bits left and relative |
| 2050 // complexity of the section | 1994 // complexity of the section. |
| 2051 twopass->kf_group_bits = (int64_t)(twopass->bits_left * | 1995 twopass->kf_group_bits = (int64_t)(twopass->bits_left * |
| 2052 (kf_group_err / twopass->modified_error_left)); | 1996 (kf_group_err / twopass->modified_error_left)); |
| 2053 | 1997 |
| 2054 // Clip based on maximum per frame rate defined by the user. | 1998 // Clip based on maximum per frame rate defined by the user. |
| 2055 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; | 1999 max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
| 2056 if (twopass->kf_group_bits > max_grp_bits) | 2000 if (twopass->kf_group_bits > max_grp_bits) |
| 2057 twopass->kf_group_bits = max_grp_bits; | 2001 twopass->kf_group_bits = max_grp_bits; |
| 2058 } else { | 2002 } else { |
| 2059 twopass->kf_group_bits = 0; | 2003 twopass->kf_group_bits = 0; |
| 2060 } | 2004 } |
| 2061 // Reset the first pass file position | 2005 // Reset the first pass file position. |
| 2062 reset_fpf_position(twopass, start_position); | 2006 reset_fpf_position(twopass, start_position); |
| 2063 | 2007 |
| 2064 // Determine how big to make this keyframe based on how well the subsequent | 2008 // Determine how big to make this keyframe based on how well the subsequent |
| 2065 // frames use inter blocks. | 2009 // frames use inter blocks. |
| 2066 decay_accumulator = 1.0; | 2010 decay_accumulator = 1.0; |
| 2067 boost_score = 0.0; | 2011 boost_score = 0.0; |
| 2068 | 2012 |
| 2069 // Scan through the kf group collating various stats. | 2013 // Scan through the kf group collating various stats. |
| 2070 for (i = 0; i < rc->frames_to_key; i++) { | 2014 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2071 double r; | 2015 double r; |
| 2072 | 2016 |
| 2073 if (EOF == input_stats(twopass, &next_frame)) | 2017 if (EOF == input_stats(twopass, &next_frame)) |
| 2074 break; | 2018 break; |
| 2075 | 2019 |
| 2076 // Monitor for static sections. | 2020 // Monitor for static sections. |
| 2077 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 2021 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < |
| 2078 zero_motion_accumulator) { | 2022 zero_motion_accumulator) { |
| 2079 zero_motion_accumulator = | 2023 zero_motion_accumulator = |
| 2080 (next_frame.pcnt_inter - next_frame.pcnt_motion); | 2024 (next_frame.pcnt_inter - next_frame.pcnt_motion); |
| 2081 } | 2025 } |
| 2082 | 2026 |
| 2083 // For the first few frames collect data to decide kf boost. | 2027 // For the first few frames collect data to decide kf boost. |
| 2084 if (i <= (rc->max_gf_interval * 2)) { | 2028 if (i <= (rc->max_gf_interval * 2)) { |
| 2085 if (next_frame.intra_error > twopass->kf_intra_err_min) | 2029 if (next_frame.intra_error > twopass->kf_intra_err_min) |
| 2086 r = (IIKFACTOR2 * next_frame.intra_error / | 2030 r = (IIKFACTOR2 * next_frame.intra_error / |
| 2087 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2031 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2088 else | 2032 else |
| 2089 r = (IIKFACTOR2 * twopass->kf_intra_err_min / | 2033 r = (IIKFACTOR2 * twopass->kf_intra_err_min / |
| 2090 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2034 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2091 | 2035 |
| 2092 if (r > RMAX) | 2036 if (r > RMAX) |
| 2093 r = RMAX; | 2037 r = RMAX; |
| 2094 | 2038 |
| 2095 // How fast is prediction quality decaying | 2039 // How fast is prediction quality decaying. |
| 2096 if (!detect_flash(twopass, 0)) { | 2040 if (!detect_flash(twopass, 0)) { |
| 2097 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 2041 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 2098 decay_accumulator *= loop_decay_rate; | 2042 decay_accumulator *= loop_decay_rate; |
| 2099 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR | 2043 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 2100 ? MIN_DECAY_FACTOR : decay_accumulator; | 2044 ? MIN_DECAY_FACTOR : decay_accumulator; |
| 2101 } | 2045 } |
| 2102 | 2046 |
| 2103 boost_score += (decay_accumulator * r); | 2047 boost_score += (decay_accumulator * r); |
| 2104 } | 2048 } |
| 2105 } | 2049 } |
| 2106 | 2050 |
| 2107 { | 2051 { |
| 2108 FIRSTPASS_STATS sectionstats; | 2052 FIRSTPASS_STATS sectionstats; |
| 2109 | 2053 |
| 2110 zero_stats(§ionstats); | 2054 zero_stats(§ionstats); |
| 2111 reset_fpf_position(twopass, start_position); | 2055 reset_fpf_position(twopass, start_position); |
| 2112 | 2056 |
| 2113 for (i = 0; i < rc->frames_to_key; i++) { | 2057 for (i = 0; i < rc->frames_to_key; ++i) { |
| 2114 input_stats(twopass, &next_frame); | 2058 input_stats(twopass, &next_frame); |
| 2115 accumulate_stats(§ionstats, &next_frame); | 2059 accumulate_stats(§ionstats, &next_frame); |
| 2116 } | 2060 } |
| 2117 | 2061 |
| 2118 avg_stats(§ionstats); | 2062 avg_stats(§ionstats); |
| 2119 | 2063 |
| 2120 twopass->section_intra_rating = (int) (sectionstats.intra_error / | 2064 twopass->section_intra_rating = (int) (sectionstats.intra_error / |
| 2121 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); | 2065 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error)); |
| 2122 } | 2066 } |
| 2123 | 2067 |
| 2124 // Reset the first pass file position | 2068 // Reset the first pass file position. |
| 2125 reset_fpf_position(twopass, start_position); | 2069 reset_fpf_position(twopass, start_position); |
| 2126 | 2070 |
| 2127 // Work out how many bits to allocate for the key frame itself | 2071 // Work out how many bits to allocate for the key frame itself. |
| 2128 if (1) { | 2072 if (1) { |
| 2129 int kf_boost = (int)boost_score; | 2073 int kf_boost = (int)boost_score; |
| 2130 int allocation_chunks; | 2074 int allocation_chunks; |
| 2131 int alt_kf_bits; | 2075 int alt_kf_bits; |
| 2132 | 2076 |
| 2133 if (kf_boost < (rc->frames_to_key * 3)) | 2077 if (kf_boost < (rc->frames_to_key * 3)) |
| 2134 kf_boost = (rc->frames_to_key * 3); | 2078 kf_boost = (rc->frames_to_key * 3); |
| 2135 | 2079 |
| 2136 if (kf_boost < MIN_KF_BOOST) | 2080 if (kf_boost < MIN_KF_BOOST) |
| 2137 kf_boost = MIN_KF_BOOST; | 2081 kf_boost = MIN_KF_BOOST; |
| 2138 | 2082 |
| 2139 // Make a note of baseline boost and the zero motion | 2083 // Make a note of baseline boost and the zero motion |
| 2140 // accumulator value for use elsewhere. | 2084 // accumulator value for use elsewhere. |
| 2141 rc->kf_boost = kf_boost; | 2085 rc->kf_boost = kf_boost; |
| 2142 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); | 2086 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2143 | 2087 |
| 2144 // We do three calculations for kf size. | 2088 // Key frame size depends on: |
| 2145 // The first is based on the error score for the whole kf group. | 2089 // (1) the error score for the whole key frame group, |
| 2146 // The second (optionally) on the key frames own error if this is | 2090 // (2) the key frames' own error if this is smaller than the |
| 2147 // smaller than the average for the group. | 2091 // average for the group (optional), |
| 2148 // The final one insures that the frame receives at least the | 2092 // (3) insuring that the frame receives at least the allocation it would |
| 2149 // allocation it would have received based on its own error score vs | 2093 // have received based on its own error score vs the error score |
| 2150 // the error score remaining | 2094 // remaining. |
| 2151 // Special case if the sequence appears almost totaly static | 2095 // Special case: |
| 2152 // In this case we want to spend almost all of the bits on the | 2096 // If the sequence appears almost totally static we want to spend almost |
| 2153 // key frame. | 2097 // all of the bits on the key frame. |
| 2154 // cpi->rc.frames_to_key-1 because key frame itself is taken | 2098 // |
| 2155 // care of by kf_boost. | 2099 // We use (cpi->rc.frames_to_key - 1) below because the key frame itself is |
| 2100 // taken care of by kf_boost. |
| 2156 if (zero_motion_accumulator >= 0.99) { | 2101 if (zero_motion_accumulator >= 0.99) { |
| 2157 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost; | 2102 allocation_chunks = ((rc->frames_to_key - 1) * 10) + kf_boost; |
| 2158 } else { | 2103 } else { |
| 2159 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost; | 2104 allocation_chunks = ((rc->frames_to_key - 1) * 100) + kf_boost; |
| 2160 } | 2105 } |
| 2161 | 2106 |
| 2162 // Prevent overflow | 2107 // Prevent overflow. |
| 2163 if (kf_boost > 1028) { | 2108 if (kf_boost > 1028) { |
| 2164 int divisor = kf_boost >> 10; | 2109 int divisor = kf_boost >> 10; |
| 2165 kf_boost /= divisor; | 2110 kf_boost /= divisor; |
| 2166 allocation_chunks /= divisor; | 2111 allocation_chunks /= divisor; |
| 2167 } | 2112 } |
| 2168 | 2113 |
| 2169 twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0 | 2114 twopass->kf_group_bits = (twopass->kf_group_bits < 0) ? 0 |
| 2170 : twopass->kf_group_bits; | 2115 : twopass->kf_group_bits; |
| 2171 | 2116 |
| 2172 // Calculate the number of bits to be spent on the key frame | 2117 // Calculate the number of bits to be spent on the key frame. |
| 2173 twopass->kf_bits = (int)((double)kf_boost * | 2118 twopass->kf_bits = (int)((double)kf_boost * |
| 2174 ((double)twopass->kf_group_bits / allocation_chunks)); | 2119 ((double)twopass->kf_group_bits / allocation_chunks)); |
| 2175 | 2120 |
| 2176 // If the key frame is actually easier than the average for the | 2121 // If the key frame is actually easier than the average for the |
| 2177 // kf group (which does sometimes happen, e.g. a blank intro frame) | 2122 // kf group (which does sometimes happen, e.g. a blank intro frame) |
| 2178 // then use an alternate calculation based on the kf error score | 2123 // then use an alternate calculation based on the kf error score |
| 2179 // which should give a smaller key frame. | 2124 // which should give a smaller key frame. |
| 2180 if (kf_mod_err < kf_group_err / rc->frames_to_key) { | 2125 if (kf_mod_err < kf_group_err / rc->frames_to_key) { |
| 2181 double alt_kf_grp_bits = ((double)twopass->bits_left * | 2126 double alt_kf_grp_bits = ((double)twopass->bits_left * |
| 2182 (kf_mod_err * (double)rc->frames_to_key) / | 2127 (kf_mod_err * (double)rc->frames_to_key) / |
| 2183 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); | 2128 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left)); |
| 2184 | 2129 |
| 2185 alt_kf_bits = (int)((double)kf_boost * | 2130 alt_kf_bits = (int)((double)kf_boost * |
| 2186 (alt_kf_grp_bits / (double)allocation_chunks)); | 2131 (alt_kf_grp_bits / (double)allocation_chunks)); |
| 2187 | 2132 |
| 2188 if (twopass->kf_bits > alt_kf_bits) | 2133 if (twopass->kf_bits > alt_kf_bits) |
| 2189 twopass->kf_bits = alt_kf_bits; | 2134 twopass->kf_bits = alt_kf_bits; |
| 2190 } else { | 2135 } else { |
| 2191 // Else if it is much harder than other frames in the group make sure | 2136 // Else if it is much harder than other frames in the group make sure |
| 2192 // it at least receives an allocation in keeping with its relative | 2137 // it at least receives an allocation in keeping with its relative |
| 2193 // error score | 2138 // error score. |
| 2194 alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / | 2139 alt_kf_bits = (int)((double)twopass->bits_left * (kf_mod_err / |
| 2195 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); | 2140 DOUBLE_DIVIDE_CHECK(twopass->modified_error_left))); |
| 2196 | 2141 |
| 2197 if (alt_kf_bits > twopass->kf_bits) { | 2142 if (alt_kf_bits > twopass->kf_bits) { |
| 2198 twopass->kf_bits = alt_kf_bits; | 2143 twopass->kf_bits = alt_kf_bits; |
| 2199 } | 2144 } |
| 2200 } | 2145 } |
| 2201 twopass->kf_group_bits -= twopass->kf_bits; | 2146 twopass->kf_group_bits -= twopass->kf_bits; |
| 2202 // Per frame bit target for this frame. | 2147 // Per frame bit target for this frame. |
| 2203 vp9_rc_set_frame_target(cpi, twopass->kf_bits); | 2148 vp9_rc_set_frame_target(cpi, twopass->kf_bits); |
| 2204 } | 2149 } |
| 2205 | 2150 |
| 2206 // Note the total error score of the kf group minus the key frame itself | 2151 // Note the total error score of the kf group minus the key frame itself. |
| 2207 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2152 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
| 2208 | 2153 |
| 2209 // Adjust the count of total modified error left. | 2154 // Adjust the count of total modified error left. |
| 2210 // The count of bits left is adjusted elsewhere based on real coded frame | 2155 // The count of bits left is adjusted elsewhere based on real coded frame |
| 2211 // sizes. | 2156 // sizes. |
| 2212 twopass->modified_error_left -= kf_group_err; | 2157 twopass->modified_error_left -= kf_group_err; |
| 2213 } | 2158 } |
| 2214 | 2159 |
| 2215 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { | 2160 void vp9_rc_get_first_pass_params(VP9_COMP *cpi) { |
| 2216 VP9_COMMON *const cm = &cpi->common; | 2161 VP9_COMMON *const cm = &cpi->common; |
| 2217 if (!cpi->refresh_alt_ref_frame && | 2162 if (!cpi->refresh_alt_ref_frame && |
| 2218 (cm->current_video_frame == 0 || | 2163 (cm->current_video_frame == 0 || |
| 2219 cm->frame_flags & FRAMEFLAGS_KEY)) { | 2164 cm->frame_flags & FRAMEFLAGS_KEY)) { |
| 2220 cm->frame_type = KEY_FRAME; | 2165 cm->frame_type = KEY_FRAME; |
| 2221 } else { | 2166 } else { |
| 2222 cm->frame_type = INTER_FRAME; | 2167 cm->frame_type = INTER_FRAME; |
| 2223 } | 2168 } |
| 2224 // Do not use periodic key frames | 2169 // Do not use periodic key frames. |
| 2225 cpi->rc.frames_to_key = INT_MAX; | 2170 cpi->rc.frames_to_key = INT_MAX; |
| 2226 } | 2171 } |
| 2227 | 2172 |
| 2228 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2173 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2229 VP9_COMMON *const cm = &cpi->common; | 2174 VP9_COMMON *const cm = &cpi->common; |
| 2230 RATE_CONTROL *const rc = &cpi->rc; | 2175 RATE_CONTROL *const rc = &cpi->rc; |
| 2231 struct twopass_rc *const twopass = &cpi->twopass; | 2176 struct twopass_rc *const twopass = &cpi->twopass; |
| 2232 const int frames_left = (int)(twopass->total_stats.count - | 2177 const int frames_left = (int)(twopass->total_stats.count - |
| 2233 cm->current_video_frame); | 2178 cm->current_video_frame); |
| 2234 FIRSTPASS_STATS this_frame; | 2179 FIRSTPASS_STATS this_frame; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2253 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2198 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2254 } else if (cm->current_video_frame == 0) { | 2199 } else if (cm->current_video_frame == 0) { |
| 2255 // Special case code for first frame. | 2200 // Special case code for first frame. |
| 2256 const int section_target_bandwidth = (int)(twopass->bits_left / | 2201 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2257 frames_left); | 2202 frames_left); |
| 2258 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2203 const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2259 section_target_bandwidth); | 2204 section_target_bandwidth); |
| 2260 twopass->active_worst_quality = tmp_q; | 2205 twopass->active_worst_quality = tmp_q; |
| 2261 rc->ni_av_qi = tmp_q; | 2206 rc->ni_av_qi = tmp_q; |
| 2262 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2207 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2263 | |
| 2264 // Limit the maxq value returned subsequently. | |
| 2265 // This increases the risk of overspend or underspend if the initial | |
| 2266 // estimate for the clip is bad, but helps prevent excessive | |
| 2267 // variation in Q, especially near the end of a clip | |
| 2268 // where for example a small overspend may cause Q to crash | |
| 2269 // adjust_maxq_qrange(cpi); | |
| 2270 } | 2208 } |
| 2271 vp9_zero(this_frame); | 2209 vp9_zero(this_frame); |
| 2272 if (EOF == input_stats(twopass, &this_frame)) | 2210 if (EOF == input_stats(twopass, &this_frame)) |
| 2273 return; | 2211 return; |
| 2274 | 2212 |
| 2275 this_frame_intra_error = this_frame.intra_error; | 2213 this_frame_intra_error = this_frame.intra_error; |
| 2276 this_frame_coded_error = this_frame.coded_error; | 2214 this_frame_coded_error = this_frame.coded_error; |
| 2277 | 2215 |
| 2278 // keyframe and section processing ! | 2216 // Keyframe and section processing. |
| 2279 if (rc->frames_to_key == 0 || | 2217 if (rc->frames_to_key == 0 || |
| 2280 (cm->frame_flags & FRAMEFLAGS_KEY)) { | 2218 (cm->frame_flags & FRAMEFLAGS_KEY)) { |
| 2281 // Define next KF group and assign bits to it | 2219 // Define next KF group and assign bits to it. |
| 2282 this_frame_copy = this_frame; | 2220 this_frame_copy = this_frame; |
| 2283 find_next_key_frame(cpi, &this_frame_copy); | 2221 find_next_key_frame(cpi, &this_frame_copy); |
| 2284 } else { | 2222 } else { |
| 2285 cm->frame_type = INTER_FRAME; | 2223 cm->frame_type = INTER_FRAME; |
| 2286 } | 2224 } |
| 2287 | 2225 |
| 2288 // Is this a GF / ARF (Note that a KF is always also a GF) | 2226 // Is this frame a GF / ARF? (Note: a key frame is always also a GF). |
| 2289 if (rc->frames_till_gf_update_due == 0) { | 2227 if (rc->frames_till_gf_update_due == 0) { |
| 2290 // Define next gf group and assign bits to it | 2228 // Define next gf group and assign bits to it. |
| 2291 this_frame_copy = this_frame; | 2229 this_frame_copy = this_frame; |
| 2292 | 2230 |
| 2293 #if CONFIG_MULTIPLE_ARF | 2231 #if CONFIG_MULTIPLE_ARF |
| 2294 if (cpi->multi_arf_enabled) { | 2232 if (cpi->multi_arf_enabled) { |
| 2295 define_fixed_arf_period(cpi); | 2233 define_fixed_arf_period(cpi); |
| 2296 } else { | 2234 } else { |
| 2297 #endif | 2235 #endif |
| 2298 define_gf_group(cpi, &this_frame_copy); | 2236 define_gf_group(cpi, &this_frame_copy); |
| 2299 #if CONFIG_MULTIPLE_ARF | 2237 #if CONFIG_MULTIPLE_ARF |
| 2300 } | 2238 } |
| 2301 #endif | 2239 #endif |
| 2302 | 2240 |
| 2303 if (twopass->gf_zeromotion_pct > 995) { | 2241 if (twopass->gf_zeromotion_pct > 995) { |
| 2304 // As long as max_thresh for encode breakout is small enough, it is ok | 2242 // As long as max_thresh for encode breakout is small enough, it is ok |
| 2305 // to enable it for show frame, i.e. set allow_encode_breakout to 2. | 2243 // to enable it for show frame, i.e. set allow_encode_breakout to |
| 2244 // ENCODE_BREAKOUT_LIMITED. |
| 2306 if (!cm->show_frame) | 2245 if (!cm->show_frame) |
| 2307 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; | 2246 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; |
| 2308 else | 2247 else |
| 2309 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; | 2248 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; |
| 2310 } | 2249 } |
| 2311 | 2250 |
| 2312 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2251 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2313 cpi->refresh_golden_frame = 1; | 2252 cpi->refresh_golden_frame = 1; |
| 2314 } else { | 2253 } else { |
| 2315 // Otherwise this is an ordinary frame | 2254 // Otherwise this is an ordinary frame. |
| 2316 // Assign bits from those allocated to the GF group | 2255 // Assign bits from those allocated to the GF group. |
| 2317 this_frame_copy = this_frame; | 2256 this_frame_copy = this_frame; |
| 2318 assign_std_frame_bits(cpi, &this_frame_copy); | 2257 assign_std_frame_bits(cpi, &this_frame_copy); |
| 2319 } | 2258 } |
| 2320 | 2259 |
| 2321 // Keep a globally available copy of this and the next frame's iiratio. | 2260 // Keep a globally available copy of this and the next frame's iiratio. |
| 2322 twopass->this_iiratio = (int)(this_frame_intra_error / | 2261 twopass->this_iiratio = (int)(this_frame_intra_error / |
| 2323 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); | 2262 DOUBLE_DIVIDE_CHECK(this_frame_coded_error)); |
| 2324 { | 2263 { |
| 2325 FIRSTPASS_STATS next_frame; | 2264 FIRSTPASS_STATS next_frame; |
| 2326 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) { | 2265 if (lookup_next_frame_stats(twopass, &next_frame) != EOF) { |
| 2327 twopass->next_iiratio = (int)(next_frame.intra_error / | 2266 twopass->next_iiratio = (int)(next_frame.intra_error / |
| 2328 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2267 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 2329 } | 2268 } |
| 2330 } | 2269 } |
| 2331 | 2270 |
| 2332 if (cpi->common.frame_type == KEY_FRAME) | 2271 if (cpi->common.frame_type == KEY_FRAME) |
| 2333 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); | 2272 target = vp9_rc_clamp_iframe_target_size(cpi, rc->this_frame_target); |
| 2334 else | 2273 else |
| 2335 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); | 2274 target = vp9_rc_clamp_pframe_target_size(cpi, rc->this_frame_target); |
| 2336 vp9_rc_set_frame_target(cpi, target); | 2275 vp9_rc_set_frame_target(cpi, target); |
| 2337 | 2276 |
| 2338 // Update the total stats remaining structure | 2277 // Update the total stats remaining structure. |
| 2339 subtract_stats(&twopass->total_left_stats, &this_frame); | 2278 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2340 } | 2279 } |
| 2341 | 2280 |
| 2342 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { | 2281 void vp9_twopass_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
| 2343 #ifdef DISABLE_RC_LONG_TERM_MEM | 2282 #ifdef DISABLE_RC_LONG_TERM_MEM |
| 2344 cpi->twopass.bits_left -= cpi->rc.this_frame_target; | 2283 cpi->twopass.bits_left -= cpi->rc.this_frame_target; |
| 2345 #else | 2284 #else |
| 2346 cpi->twopass.bits_left -= 8 * bytes_used; | 2285 cpi->twopass.bits_left -= 8 * bytes_used; |
| 2347 // Update bits left to the kf and gf groups to account for overshoot or | 2286 // Update bits left to the kf and gf groups to account for overshoot or |
| 2348 // undershoot on these frames | 2287 // undershoot on these frames. |
| 2349 if (cm->frame_type == KEY_FRAME) { | 2288 if (cm->frame_type == KEY_FRAME) { |
| 2350 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - | 2289 cpi->twopass.kf_group_bits += cpi->rc.this_frame_target - |
| 2351 cpi->rc.projected_frame_size; | 2290 cpi->rc.projected_frame_size; |
| 2352 | 2291 |
| 2353 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); | 2292 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
| 2354 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { | 2293 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { |
| 2355 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - | 2294 cpi->twopass.gf_group_bits += cpi->rc.this_frame_target - |
| 2356 cpi->rc.projected_frame_size; | 2295 cpi->rc.projected_frame_size; |
| 2357 | 2296 |
| 2358 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); | 2297 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
| 2359 } | 2298 } |
| 2360 #endif | 2299 #endif |
| 2361 } | 2300 } |
| OLD | NEW |