Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_firstpass.c

Issue 592203002: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_lookahead.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 17 matching lines...) Expand all
28 #include "vp9/encoder/vp9_encodemb.h" 28 #include "vp9/encoder/vp9_encodemb.h"
29 #include "vp9/encoder/vp9_encodemv.h" 29 #include "vp9/encoder/vp9_encodemv.h"
30 #include "vp9/encoder/vp9_encoder.h" 30 #include "vp9/encoder/vp9_encoder.h"
31 #include "vp9/encoder/vp9_extend.h" 31 #include "vp9/encoder/vp9_extend.h"
32 #include "vp9/encoder/vp9_firstpass.h" 32 #include "vp9/encoder/vp9_firstpass.h"
33 #include "vp9/encoder/vp9_mcomp.h" 33 #include "vp9/encoder/vp9_mcomp.h"
34 #include "vp9/encoder/vp9_quantize.h" 34 #include "vp9/encoder/vp9_quantize.h"
35 #include "vp9/encoder/vp9_rd.h" 35 #include "vp9/encoder/vp9_rd.h"
36 #include "vp9/encoder/vp9_variance.h" 36 #include "vp9/encoder/vp9_variance.h"
37 37
38 #define OUTPUT_FPF 0 38 #define OUTPUT_FPF 0
39 #define ARF_STATS_OUTPUT 0
39 40
40 #define IIFACTOR 12.5 41 #define BOOST_FACTOR 12.5
41 #define IIKFACTOR1 12.5 42 #define ERR_DIVISOR 100.0
42 #define IIKFACTOR2 15.0 43 #define FACTOR_PT_LOW 0.5
43 #define RMAX 512.0 44 #define FACTOR_PT_HIGH 0.9
44 #define GF_RMAX 96.0 45 #define FIRST_PASS_Q 10.0
45 #define ERR_DIVISOR 150.0 46 #define GF_MAX_BOOST 96.0
46 #define MIN_DECAY_FACTOR 0.1 47 #define INTRA_MODE_PENALTY 1024
47 #define SVC_FACTOR_PT_LOW 0.45 48 #define KF_MAX_BOOST 128.0
48 #define FACTOR_PT_LOW 0.5 49 #define MIN_DECAY_FACTOR 0.01
49 #define FACTOR_PT_HIGH 0.9 50 #define MIN_GF_INTERVAL 4
50 51 #define MIN_KF_BOOST 300
51 #define KF_MB_INTRA_MIN 150 52 #define NEW_MV_MODE_PENALTY 32
52 #define GF_MB_INTRA_MIN 100 53 #define SVC_FACTOR_PT_LOW 0.45
53 54
54 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) 55 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
55 56
56 #define MIN_KF_BOOST 300 57 #if ARF_STATS_OUTPUT
57 #define MIN_GF_INTERVAL 4 58 unsigned int arf_count = 0;
59 #endif
58 60
59 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { 61 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
60 YV12_BUFFER_CONFIG temp = *a; 62 YV12_BUFFER_CONFIG temp = *a;
61 *a = *b; 63 *a = *b;
62 *b = temp; 64 *b = temp;
63 } 65 }
64 66
65 static int gfboost_qadjust(int qindex) { 67 static int gfboost_qadjust(int qindex, vpx_bit_depth_t bit_depth) {
66 const double q = vp9_convert_qindex_to_q(qindex); 68 const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
67 return (int)((0.00000828 * q * q * q) + 69 return (int)((0.00000828 * q * q * q) +
68 (-0.0055 * q * q) + 70 (-0.0055 * q * q) +
69 (1.32 * q) + 79.3); 71 (1.32 * q) + 79.3);
70 } 72 }
71 73
72 // Resets the first pass file to the given position using a relative seek from 74 // Resets the first pass file to the given position using a relative seek from
73 // the current position. 75 // the current position.
74 static void reset_fpf_position(TWO_PASS *p, 76 static void reset_fpf_position(TWO_PASS *p,
75 const FIRSTPASS_STATS *position) { 77 const FIRSTPASS_STATS *position) {
76 p->stats_in = position; 78 p->stats_in = position;
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 return sr; 292 return sr;
291 } 293 }
292 294
293 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 295 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
294 const MV *ref_mv, MV *best_mv, 296 const MV *ref_mv, MV *best_mv,
295 int *best_motion_err) { 297 int *best_motion_err) {
296 MACROBLOCKD *const xd = &x->e_mbd; 298 MACROBLOCKD *const xd = &x->e_mbd;
297 MV tmp_mv = {0, 0}; 299 MV tmp_mv = {0, 0};
298 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; 300 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
299 int num00, tmp_err, n; 301 int num00, tmp_err, n;
300 const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; 302 const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
301 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; 303 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
302 const int new_mv_mode_penalty = 256; 304 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
303 305
304 int step_param = 3; 306 int step_param = 3;
305 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; 307 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
306 const int sr = get_search_range(&cpi->common); 308 const int sr = get_search_range(&cpi->common);
307 step_param += sr; 309 step_param += sr;
308 further_steps -= sr; 310 further_steps -= sr;
309 311
310 // Override the default variance function to use MSE. 312 // Override the default variance function to use MSE.
311 v_fn_ptr.vf = get_block_variance_fn(bsize); 313 v_fn_ptr.vf = get_block_variance_fn(bsize);
312 314
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) { 355 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
354 if (2 * mb_col + 1 < cm->mi_cols) { 356 if (2 * mb_col + 1 < cm->mi_cols) {
355 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 357 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16
356 : BLOCK_16X8; 358 : BLOCK_16X8;
357 } else { 359 } else {
358 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 360 return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16
359 : BLOCK_8X8; 361 : BLOCK_8X8;
360 } 362 }
361 } 363 }
362 364
363 static int find_fp_qindex() { 365 static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
364 int i; 366 int i;
365 367
366 for (i = 0; i < QINDEX_RANGE; ++i) 368 for (i = 0; i < QINDEX_RANGE; ++i)
367 if (vp9_convert_qindex_to_q(i) >= 30.0) 369 if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q)
368 break; 370 break;
369 371
370 if (i == QINDEX_RANGE) 372 if (i == QINDEX_RANGE)
371 i--; 373 i--;
372 374
373 return i; 375 return i;
374 } 376 }
375 377
376 static void set_first_pass_params(VP9_COMP *cpi) { 378 static void set_first_pass_params(VP9_COMP *cpi) {
377 VP9_COMMON *const cm = &cpi->common; 379 VP9_COMMON *const cm = &cpi->common;
(...skipping 29 matching lines...) Expand all
407 int64_t intra_error = 0; 409 int64_t intra_error = 0;
408 int64_t coded_error = 0; 410 int64_t coded_error = 0;
409 int64_t sr_coded_error = 0; 411 int64_t sr_coded_error = 0;
410 412
411 int sum_mvr = 0, sum_mvc = 0; 413 int sum_mvr = 0, sum_mvc = 0;
412 int sum_mvr_abs = 0, sum_mvc_abs = 0; 414 int sum_mvr_abs = 0, sum_mvc_abs = 0;
413 int64_t sum_mvrs = 0, sum_mvcs = 0; 415 int64_t sum_mvrs = 0, sum_mvcs = 0;
414 int mvcount = 0; 416 int mvcount = 0;
415 int intercount = 0; 417 int intercount = 0;
416 int second_ref_count = 0; 418 int second_ref_count = 0;
417 int intrapenalty = 256; 419 const int intrapenalty = INTRA_MODE_PENALTY;
418 int neutral_count = 0; 420 int neutral_count = 0;
419 int new_mv_count = 0; 421 int new_mv_count = 0;
420 int sum_in_vectors = 0; 422 int sum_in_vectors = 0;
421 MV lastmv = {0, 0}; 423 MV lastmv = {0, 0};
422 TWO_PASS *twopass = &cpi->twopass; 424 TWO_PASS *twopass = &cpi->twopass;
423 const MV zero_mv = {0, 0}; 425 const MV zero_mv = {0, 0};
424 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; 426 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
425 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? 427 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
426 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL; 428 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL;
427 429
428 #if CONFIG_FP_MB_STATS 430 #if CONFIG_FP_MB_STATS
429 if (cpi->use_fp_mb_stats) { 431 if (cpi->use_fp_mb_stats) {
430 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs); 432 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs);
431 } 433 }
432 #endif 434 #endif
433 435
434 vp9_clear_system_state(); 436 vp9_clear_system_state();
435 437
436 set_first_pass_params(cpi); 438 set_first_pass_params(cpi);
437 vp9_set_quantizer(cm, find_fp_qindex()); 439 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
438 440
439 if (lc != NULL) { 441 if (lc != NULL) {
440 MV_REFERENCE_FRAME ref_frame = LAST_FRAME;
441 twopass = &lc->twopass; 442 twopass = &lc->twopass;
442 443
443 if (cpi->common.current_video_frame == 0) { 444 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
445 cpi->ref_frame_flags = VP9_LAST_FLAG;
446
447 if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
448 REF_FRAMES) {
449 cpi->gld_fb_idx =
450 cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id;
451 cpi->ref_frame_flags |= VP9_GOLD_FLAG;
452 cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0);
453 } else {
454 cpi->refresh_golden_frame = 0;
455 }
456
457 if (lc->current_video_frame_in_layer == 0)
444 cpi->ref_frame_flags = 0; 458 cpi->ref_frame_flags = 0;
445 } else {
446 if (lc->current_video_frame_in_layer <
447 (unsigned int)cpi->svc.number_temporal_layers)
448 cpi->ref_frame_flags = VP9_GOLD_FLAG;
449 else
450 cpi->ref_frame_flags = VP9_LAST_FLAG | VP9_GOLD_FLAG;
451 }
452 459
453 vp9_scale_references(cpi); 460 vp9_scale_references(cpi);
454 461
455 // Use either last frame or alt frame for motion search. 462 // Use either last frame or alt frame for motion search.
456 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { 463 if (cpi->ref_frame_flags & VP9_LAST_FLAG) {
457 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); 464 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME);
458 ref_frame = LAST_FRAME;
459 if (first_ref_buf == NULL) 465 if (first_ref_buf == NULL)
460 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); 466 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME);
461 } else if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { 467 }
462 first_ref_buf = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME); 468
463 ref_frame = GOLDEN_FRAME; 469 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) {
464 if (first_ref_buf == NULL) 470 const int ref_idx =
465 first_ref_buf = get_ref_frame_buffer(cpi, GOLDEN_FRAME); 471 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)];
472 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1];
473
474 gld_yv12 = (scaled_idx != ref_idx) ? &cm->frame_bufs[scaled_idx].buf :
475 get_ref_frame_buffer(cpi, GOLDEN_FRAME);
476 } else {
477 gld_yv12 = NULL;
466 } 478 }
467 479
468 recon_y_stride = new_yv12->y_stride; 480 recon_y_stride = new_yv12->y_stride;
469 recon_uv_stride = new_yv12->uv_stride; 481 recon_uv_stride = new_yv12->uv_stride;
470 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); 482 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
471 483
472 // Disable golden frame for svc first pass for now. 484 set_ref_ptrs(cm, xd,
473 gld_yv12 = NULL; 485 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE,
474 set_ref_ptrs(cm, xd, ref_frame, NONE); 486 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE);
475 487
476 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source, 488 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
477 &cpi->scaled_source); 489 &cpi->scaled_source);
478 } 490 }
479 491
480 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); 492 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
481 493
482 vp9_setup_src_planes(x, cpi->Source, 0, 0); 494 vp9_setup_src_planes(x, cpi->Source, 0, 0);
483 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL); 495 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
484 vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0); 496 vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
485 497
486 xd->mi = cm->mi_grid_visible; 498 xd->mi = cm->mi;
487 xd->mi[0] = cm->mi; 499 xd->mi[0].src_mi = &xd->mi[0];
488 500
489 vp9_frame_init_quantizer(cpi); 501 vp9_frame_init_quantizer(cpi);
490 502
491 for (i = 0; i < MAX_MB_PLANE; ++i) { 503 for (i = 0; i < MAX_MB_PLANE; ++i) {
492 p[i].coeff = ctx->coeff_pbuf[i][1]; 504 p[i].coeff = ctx->coeff_pbuf[i][1];
493 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; 505 p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
494 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; 506 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
495 p[i].eobs = ctx->eobs_pbuf[i][1]; 507 p[i].eobs = ctx->eobs_pbuf[i][1];
496 } 508 }
497 x->skip_recode = 0; 509 x->skip_recode = 0;
(...skipping 26 matching lines...) Expand all
524 #if CONFIG_FP_MB_STATS 536 #if CONFIG_FP_MB_STATS
525 const int mb_index = mb_row * cm->mb_cols + mb_col; 537 const int mb_index = mb_row * cm->mb_cols + mb_col;
526 #endif 538 #endif
527 539
528 vp9_clear_system_state(); 540 vp9_clear_system_state();
529 541
530 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; 542 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
531 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; 543 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
532 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; 544 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
533 xd->left_available = (mb_col != 0); 545 xd->left_available = (mb_col != 0);
534 xd->mi[0]->mbmi.sb_type = bsize; 546 xd->mi[0].src_mi->mbmi.sb_type = bsize;
535 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; 547 xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME;
536 set_mi_row_col(xd, &tile, 548 set_mi_row_col(xd, &tile,
537 mb_row << 1, num_8x8_blocks_high_lookup[bsize], 549 mb_row << 1, num_8x8_blocks_high_lookup[bsize],
538 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], 550 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
539 cm->mi_rows, cm->mi_cols); 551 cm->mi_rows, cm->mi_cols);
540 552
541 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 553 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
542 const int energy = vp9_block_energy(cpi, x, bsize); 554 const int energy = vp9_block_energy(cpi, x, bsize);
543 error_weight = vp9_vaq_inv_q_ratio(energy); 555 error_weight = vp9_vaq_inv_q_ratio(energy);
544 } 556 }
545 557
546 // Do intra 16x16 prediction. 558 // Do intra 16x16 prediction.
547 x->skip_encode = 0; 559 x->skip_encode = 0;
548 xd->mi[0]->mbmi.mode = DC_PRED; 560 xd->mi[0].src_mi->mbmi.mode = DC_PRED;
549 xd->mi[0]->mbmi.tx_size = use_dc_pred ? 561 xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ?
550 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; 562 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
551 vp9_encode_intra_block_plane(x, bsize, 0); 563 vp9_encode_intra_block_plane(x, bsize, 0);
552 this_error = vp9_get_mb_ss(x->plane[0].src_diff); 564 this_error = vp9_get_mb_ss(x->plane[0].src_diff);
553 565
554 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 566 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
555 vp9_clear_system_state(); 567 vp9_clear_system_state();
556 this_error = (int)(this_error * error_weight); 568 this_error = (int)(this_error * error_weight);
557 } 569 }
558 570
559 // Intrapenalty below deals with situations where the intra and inter 571 // Intrapenalty below deals with situations where the intra and inter
(...skipping 14 matching lines...) Expand all
574 cpi->twopass.frame_mb_stats_buf[mb_index] = 0; 586 cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
575 } 587 }
576 #endif 588 #endif
577 589
578 // Set up limit values for motion vectors to prevent them extending 590 // Set up limit values for motion vectors to prevent them extending
579 // outside the UMV borders. 591 // outside the UMV borders.
580 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); 592 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
581 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; 593 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
582 594
583 // Other than for the first frame do a motion search. 595 // Other than for the first frame do a motion search.
584 if (cm->current_video_frame > 0) { 596 if ((lc == NULL && cm->current_video_frame > 0) ||
597 (lc != NULL && lc->current_video_frame_in_layer > 0)) {
585 int tmp_err, motion_error, raw_motion_error; 598 int tmp_err, motion_error, raw_motion_error;
586 // Assume 0,0 motion with no mv overhead. 599 // Assume 0,0 motion with no mv overhead.
587 MV mv = {0, 0} , tmp_mv = {0, 0}; 600 MV mv = {0, 0} , tmp_mv = {0, 0};
588 struct buf_2d unscaled_last_source_buf_2d; 601 struct buf_2d unscaled_last_source_buf_2d;
589 602
590 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; 603 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
591 motion_error = get_prediction_error(bsize, &x->plane[0].src, 604 motion_error = get_prediction_error(bsize, &x->plane[0].src,
592 &xd->plane[0].pre[0]); 605 &xd->plane[0].pre[0]);
593 606
594 // Compute the motion error of the 0,0 motion using the last source 607 // Compute the motion error of the 0,0 motion using the last source
(...skipping 26 matching lines...) Expand all
621 tmp_err = (int)(tmp_err * error_weight); 634 tmp_err = (int)(tmp_err * error_weight);
622 } 635 }
623 636
624 if (tmp_err < motion_error) { 637 if (tmp_err < motion_error) {
625 motion_error = tmp_err; 638 motion_error = tmp_err;
626 mv = tmp_mv; 639 mv = tmp_mv;
627 } 640 }
628 } 641 }
629 642
630 // Search in an older reference frame. 643 // Search in an older reference frame.
631 if (cm->current_video_frame > 1 && gld_yv12 != NULL) { 644 if (((lc == NULL && cm->current_video_frame > 1) ||
645 (lc != NULL && lc->current_video_frame_in_layer > 1))
646 && gld_yv12 != NULL) {
632 // Assume 0,0 motion with no mv overhead. 647 // Assume 0,0 motion with no mv overhead.
633 int gf_motion_error; 648 int gf_motion_error;
634 649
635 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; 650 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
636 gf_motion_error = get_prediction_error(bsize, &x->plane[0].src, 651 gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
637 &xd->plane[0].pre[0]); 652 &xd->plane[0].pre[0]);
638 653
639 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, 654 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
640 &gf_motion_error); 655 &gf_motion_error);
641 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 656 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 // Keep a count of cases where the inter and intra were very close 703 // Keep a count of cases where the inter and intra were very close
689 // and very low. This helps with scene cut detection for example in 704 // and very low. This helps with scene cut detection for example in
690 // cropped clips with black bars at the sides or top and bottom. 705 // cropped clips with black bars at the sides or top and bottom.
691 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && 706 if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
692 this_error < 2 * intrapenalty) 707 this_error < 2 * intrapenalty)
693 ++neutral_count; 708 ++neutral_count;
694 709
695 mv.row *= 8; 710 mv.row *= 8;
696 mv.col *= 8; 711 mv.col *= 8;
697 this_error = motion_error; 712 this_error = motion_error;
698 xd->mi[0]->mbmi.mode = NEWMV; 713 xd->mi[0].src_mi->mbmi.mode = NEWMV;
699 xd->mi[0]->mbmi.mv[0].as_mv = mv; 714 xd->mi[0].src_mi->mbmi.mv[0].as_mv = mv;
700 xd->mi[0]->mbmi.tx_size = TX_4X4; 715 xd->mi[0].src_mi->mbmi.tx_size = TX_4X4;
701 xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME; 716 xd->mi[0].src_mi->mbmi.ref_frame[0] = LAST_FRAME;
702 xd->mi[0]->mbmi.ref_frame[1] = NONE; 717 xd->mi[0].src_mi->mbmi.ref_frame[1] = NONE;
703 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); 718 vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
704 vp9_encode_sby_pass1(x, bsize); 719 vp9_encode_sby_pass1(x, bsize);
705 sum_mvr += mv.row; 720 sum_mvr += mv.row;
706 sum_mvr_abs += abs(mv.row); 721 sum_mvr_abs += abs(mv.row);
707 sum_mvc += mv.col; 722 sum_mvc += mv.col;
708 sum_mvc_abs += abs(mv.col); 723 sum_mvc_abs += abs(mv.col);
709 sum_mvrs += mv.row * mv.row; 724 sum_mvrs += mv.row * mv.row;
710 sum_mvcs += mv.col * mv.col; 725 sum_mvcs += mv.col * mv.col;
711 ++intercount; 726 ++intercount;
712 727
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 uv_mb_height * cm->mb_cols; 825 uv_mb_height * cm->mb_cols;
811 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - 826 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
812 uv_mb_height * cm->mb_cols; 827 uv_mb_height * cm->mb_cols;
813 828
814 vp9_clear_system_state(); 829 vp9_clear_system_state();
815 } 830 }
816 831
817 vp9_clear_system_state(); 832 vp9_clear_system_state();
818 { 833 {
819 FIRSTPASS_STATS fps; 834 FIRSTPASS_STATS fps;
835 // The minimum error here insures some bit alocation to frames even
836 // in static regions. The allocation per MB declines for larger formats
837 // where the typical "real" energy per MB also falls.
838 // Initial estimate here uses sqrt(mbs) to define the min_err, where the
839 // number of mbs is propotional to image area.
840 const double min_err = 200 * sqrt(cm->MBs);
820 841
821 fps.frame = cm->current_video_frame; 842 fps.frame = cm->current_video_frame;
822 fps.spatial_layer_id = cpi->svc.spatial_layer_id; 843 fps.spatial_layer_id = cpi->svc.spatial_layer_id;
823 fps.intra_error = (double)(intra_error >> 8); 844 fps.coded_error = (double)(coded_error >> 8) + min_err;
824 fps.coded_error = (double)(coded_error >> 8); 845 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
825 fps.sr_coded_error = (double)(sr_coded_error >> 8); 846 fps.intra_error = (double)(intra_error >> 8) + min_err;
826 fps.count = 1.0; 847 fps.count = 1.0;
827 fps.pcnt_inter = (double)intercount / cm->MBs; 848 fps.pcnt_inter = (double)intercount / cm->MBs;
828 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; 849 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs;
829 fps.pcnt_neutral = (double)neutral_count / cm->MBs; 850 fps.pcnt_neutral = (double)neutral_count / cm->MBs;
830 851
831 if (mvcount > 0) { 852 if (mvcount > 0) {
832 fps.MVr = (double)sum_mvr / mvcount; 853 fps.MVr = (double)sum_mvr / mvcount;
833 fps.mvr_abs = (double)sum_mvr_abs / mvcount; 854 fps.mvr_abs = (double)sum_mvr_abs / mvcount;
834 fps.MVc = (double)sum_mvc / mvcount; 855 fps.MVc = (double)sum_mvc / mvcount;
835 fps.mvc_abs = (double)sum_mvc_abs / mvcount; 856 fps.mvc_abs = (double)sum_mvc_abs / mvcount;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 907
887 if (lc != NULL) { 908 if (lc != NULL) {
888 vp9_update_reference_frames(cpi); 909 vp9_update_reference_frames(cpi);
889 } else { 910 } else {
890 // Swap frame pointers so last frame refers to the frame we just compressed. 911 // Swap frame pointers so last frame refers to the frame we just compressed.
891 swap_yv12(lst_yv12, new_yv12); 912 swap_yv12(lst_yv12, new_yv12);
892 } 913 }
893 914
894 // Special case for the first frame. Copy into the GF buffer as a second 915 // Special case for the first frame. Copy into the GF buffer as a second
895 // reference. 916 // reference.
896 if (cm->current_video_frame == 0 && gld_yv12 != NULL) { 917 if (cm->current_video_frame == 0 && gld_yv12 != NULL && lc == NULL) {
897 vp8_yv12_copy_frame(lst_yv12, gld_yv12); 918 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
898 } 919 }
899 920
900 // Use this to see what the first pass reconstruction looks like. 921 // Use this to see what the first pass reconstruction looks like.
901 if (0) { 922 if (0) {
902 char filename[512]; 923 char filename[512];
903 FILE *recon_file; 924 FILE *recon_file;
904 snprintf(filename, sizeof(filename), "enc%04d.yuv", 925 snprintf(filename, sizeof(filename), "enc%04d.yuv",
905 (int)cm->current_video_frame); 926 (int)cm->current_video_frame);
906 927
907 if (cm->current_video_frame == 0) 928 if (cm->current_video_frame == 0)
908 recon_file = fopen(filename, "wb"); 929 recon_file = fopen(filename, "wb");
909 else 930 else
910 recon_file = fopen(filename, "ab"); 931 recon_file = fopen(filename, "ab");
911 932
912 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); 933 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
913 fclose(recon_file); 934 fclose(recon_file);
914 } 935 }
915 936
916 ++cm->current_video_frame; 937 ++cm->current_video_frame;
917 if (cpi->use_svc) 938 if (cpi->use_svc)
918 vp9_inc_frame_in_layer(cpi); 939 vp9_inc_frame_in_layer(cpi);
919 } 940 }
920 941
921 static double calc_correction_factor(double err_per_mb, 942 static double calc_correction_factor(double err_per_mb,
922 double err_divisor, 943 double err_divisor,
923 double pt_low, 944 double pt_low,
924 double pt_high, 945 double pt_high,
925 int q) { 946 int q,
947 vpx_bit_depth_t bit_depth) {
926 const double error_term = err_per_mb / err_divisor; 948 const double error_term = err_per_mb / err_divisor;
927 949
928 // Adjustment based on actual quantizer to power term. 950 // Adjustment based on actual quantizer to power term.
929 const double power_term = MIN(vp9_convert_qindex_to_q(q) * 0.0125 + pt_low, 951 const double power_term =
930 pt_high); 952 MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.0125 + pt_low, pt_high);
931 953
932 // Calculate correction factor. 954 // Calculate correction factor.
933 if (power_term < 1.0) 955 if (power_term < 1.0)
934 assert(error_term >= 0.0); 956 assert(error_term >= 0.0);
935 957
936 return fclamp(pow(error_term, power_term), 0.05, 5.0); 958 return fclamp(pow(error_term, power_term), 0.05, 5.0);
937 } 959 }
938 960
939 static int get_twopass_worst_quality(const VP9_COMP *cpi, 961 static int get_twopass_worst_quality(const VP9_COMP *cpi,
940 const FIRSTPASS_STATS *stats, 962 const FIRSTPASS_STATS *stats,
(...skipping 14 matching lines...) Expand all
955 int is_svc_upper_layer = 0; 977 int is_svc_upper_layer = 0;
956 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) 978 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
957 is_svc_upper_layer = 1; 979 is_svc_upper_layer = 1;
958 980
959 // Try and pick a max Q that will be high enough to encode the 981 // Try and pick a max Q that will be high enough to encode the
960 // content at the given rate. 982 // content at the given rate.
961 for (q = rc->best_quality; q < rc->worst_quality; ++q) { 983 for (q = rc->best_quality; q < rc->worst_quality; ++q) {
962 const double factor = 984 const double factor =
963 calc_correction_factor(err_per_mb, ERR_DIVISOR, 985 calc_correction_factor(err_per_mb, ERR_DIVISOR,
964 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : 986 is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
965 FACTOR_PT_LOW, FACTOR_PT_HIGH, q); 987 FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
988 cpi->common.bit_depth);
966 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, 989 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q,
967 factor * speed_term); 990 factor * speed_term,
991 cpi->common.bit_depth);
968 if (bits_per_mb <= target_norm_bits_per_mb) 992 if (bits_per_mb <= target_norm_bits_per_mb)
969 break; 993 break;
970 } 994 }
971 995
972 // Restriction on active max q for constrained quality mode. 996 // Restriction on active max q for constrained quality mode.
973 if (cpi->oxcf.rc_mode == VPX_CQ) 997 if (cpi->oxcf.rc_mode == VPX_CQ)
974 q = MAX(q, oxcf->cq_level); 998 q = MAX(q, oxcf->cq_level);
975 return q; 999 return q;
976 } 1000 }
977 } 1001 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 vp9_update_spatial_layer_framerate(cpi, frame_rate); 1034 vp9_update_spatial_layer_framerate(cpi, frame_rate);
1011 twopass->bits_left = (int64_t)(stats->duration * 1035 twopass->bits_left = (int64_t)(stats->duration *
1012 svc->layer_context[svc->spatial_layer_id].target_bandwidth / 1036 svc->layer_context[svc->spatial_layer_id].target_bandwidth /
1013 10000000.0); 1037 10000000.0);
1014 } else { 1038 } else {
1015 vp9_new_framerate(cpi, frame_rate); 1039 vp9_new_framerate(cpi, frame_rate);
1016 twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth / 1040 twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth /
1017 10000000.0); 1041 10000000.0);
1018 } 1042 }
1019 1043
1020 // Calculate a minimum intra value to be used in determining the IIratio
1021 // scores used in the second pass. We have this minimum to make sure
1022 // that clips that are static but "low complexity" in the intra domain
1023 // are still boosted appropriately for KF/GF/ARF.
1024 if (!is_two_pass_svc) {
1025 // We don't know the number of MBs for each layer at this point.
1026 // So we will do it later.
1027 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1028 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1029 }
1030
1031 // This variable monitors how far behind the second ref update is lagging. 1044 // This variable monitors how far behind the second ref update is lagging.
1032 twopass->sr_update_lag = 1; 1045 twopass->sr_update_lag = 1;
1033 1046
1034 // Scan the first pass file and calculate a modified total error based upon 1047 // Scan the first pass file and calculate a modified total error based upon
1035 // the bias/power function used to allocate bits. 1048 // the bias/power function used to allocate bits.
1036 { 1049 {
1037 const double avg_error = stats->coded_error / 1050 const double avg_error = stats->coded_error /
1038 DOUBLE_DIVIDE_CHECK(stats->count); 1051 DOUBLE_DIVIDE_CHECK(stats->count);
1039 const FIRSTPASS_STATS *s = twopass->stats_in; 1052 const FIRSTPASS_STATS *s = twopass->stats_in;
1040 double modified_error_total = 0.0; 1053 double modified_error_total = 0.0;
1041 twopass->modified_error_min = (avg_error * 1054 twopass->modified_error_min = (avg_error *
1042 oxcf->two_pass_vbrmin_section) / 100; 1055 oxcf->two_pass_vbrmin_section) / 100;
1043 twopass->modified_error_max = (avg_error * 1056 twopass->modified_error_max = (avg_error *
1044 oxcf->two_pass_vbrmax_section) / 100; 1057 oxcf->two_pass_vbrmax_section) / 100;
1045 while (s < twopass->stats_in_end) { 1058 while (s < twopass->stats_in_end) {
1046 modified_error_total += calculate_modified_err(twopass, oxcf, s); 1059 modified_error_total += calculate_modified_err(twopass, oxcf, s);
1047 ++s; 1060 ++s;
1048 } 1061 }
1049 twopass->modified_error_left = modified_error_total; 1062 twopass->modified_error_left = modified_error_total;
1050 } 1063 }
1051 1064
1052 // Reset the vbr bits off target counter 1065 // Reset the vbr bits off target counter
1053 cpi->rc.vbr_bits_off_target = 0; 1066 cpi->rc.vbr_bits_off_target = 0;
1067
1068 // Static sequence monitor variables.
1069 twopass->kf_zeromotion_pct = 100;
1070 twopass->last_kfgroup_zeromotion_pct = 100;
1071 }
1072
1073 #define SR_DIFF_PART 0.0015
1074 #define MOTION_AMP_PART 0.003
1075 #define INTRA_PART 0.005
1076 #define DEFAULT_DECAY_LIMIT 0.75
1077 #define LOW_SR_DIFF_TRHESH 0.1
1078 #define SR_DIFF_MAX 128.0
1079
1080 static double get_sr_decay_rate(const VP9_COMMON *cm,
1081 const FIRSTPASS_STATS *frame) {
1082 double sr_diff = (frame->sr_coded_error - frame->coded_error) / cm->MBs;
1083 double sr_decay = 1.0;
1084 const double motion_amplitude_factor =
1085 frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2);
1086 const double pcnt_intra = 100 * (1.0 - frame->pcnt_inter);
1087
1088 if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1089 sr_diff = MIN(sr_diff, SR_DIFF_MAX);
1090 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1091 (MOTION_AMP_PART * motion_amplitude_factor) -
1092 (INTRA_PART * pcnt_intra);
1093 }
1094 return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, frame->pcnt_inter));
1054 } 1095 }
1055 1096
1056 // This function gives an estimate of how badly we believe the prediction 1097 // This function gives an estimate of how badly we believe the prediction
1057 // quality is decaying from frame to frame. 1098 // quality is decaying from frame to frame.
1099 static double get_zero_motion_factor(const VP9_COMMON *cm,
1100 const FIRSTPASS_STATS *frame) {
1101 const double zero_motion_pct = frame->pcnt_inter -
1102 frame->pcnt_motion;
1103 double sr_decay = get_sr_decay_rate(cm, frame);
1104 return MIN(sr_decay, zero_motion_pct);
1105 }
1106
1107 #define ZM_POWER_FACTOR 0.75
1108
1058 static double get_prediction_decay_rate(const VP9_COMMON *cm, 1109 static double get_prediction_decay_rate(const VP9_COMMON *cm,
1059 const FIRSTPASS_STATS *next_frame) { 1110 const FIRSTPASS_STATS *next_frame) {
1060 // Look at the observed drop in prediction quality between the last frame 1111 const double sr_decay_rate = get_sr_decay_rate(cm, next_frame);
1061 // and the GF buffer (which contains an older frame). 1112 const double zero_motion_factor =
1062 const double mb_sr_err_diff = (next_frame->sr_coded_error - 1113 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1063 next_frame->coded_error) / cm->MBs; 1114 ZM_POWER_FACTOR));
1064 const double second_ref_decay = mb_sr_err_diff <= 512.0
1065 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0)
1066 : 0.85;
1067 1115
1068 return MIN(second_ref_decay, next_frame->pcnt_inter); 1116 return MAX(zero_motion_factor,
1117 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1069 } 1118 }
1070 1119
1071 // This function gives an estimate of how badly we believe the prediction
1072 // quality is decaying from frame to frame.
1073 static double get_zero_motion_factor(const FIRSTPASS_STATS *frame) {
1074 const double sr_ratio = frame->coded_error /
1075 DOUBLE_DIVIDE_CHECK(frame->sr_coded_error);
1076 const double zero_motion_pct = frame->pcnt_inter -
1077 frame->pcnt_motion;
1078
1079 return MIN(sr_ratio, zero_motion_pct);
1080 }
1081
1082
1083 // Function to test for a condition where a complex transition is followed 1120 // Function to test for a condition where a complex transition is followed
1084 // by a static section. For example in slide shows where there is a fade 1121 // by a static section. For example in slide shows where there is a fade
1085 // between slides. This is to help with more optimal kf and gf positioning. 1122 // between slides. This is to help with more optimal kf and gf positioning.
1086 static int detect_transition_to_still(const TWO_PASS *twopass, 1123 static int detect_transition_to_still(const TWO_PASS *twopass,
1087 int frame_interval, int still_interval, 1124 int frame_interval, int still_interval,
1088 double loop_decay_rate, 1125 double loop_decay_rate,
1089 double last_decay_rate) { 1126 double last_decay_rate) {
1090 // Break clause to detect very still sections after motion 1127 // Break clause to detect very still sections after motion
1091 // For example a static image after a fade or other transition 1128 // For example a static image after a fade or other transition
1092 // instead of a clean scene cut. 1129 // instead of a clean scene cut.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 const double mvc_ratio = fabs(stats->mvc_abs) / 1186 const double mvc_ratio = fabs(stats->mvc_abs) /
1150 DOUBLE_DIVIDE_CHECK(fabs(stats->MVc)); 1187 DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1151 1188
1152 *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ? 1189 *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ?
1153 mvr_ratio : stats->mvr_abs); 1190 mvr_ratio : stats->mvr_abs);
1154 *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ? 1191 *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ?
1155 mvc_ratio : stats->mvc_abs); 1192 mvc_ratio : stats->mvc_abs);
1156 } 1193 }
1157 } 1194 }
1158 1195
1159 // Calculate a baseline boost number for the current frame. 1196 #define BASELINE_ERR_PER_MB 1000.0
1160 static double calc_frame_boost(const TWO_PASS *twopass, 1197 static double calc_frame_boost(VP9_COMP *cpi,
1161 const FIRSTPASS_STATS *this_frame, 1198 const FIRSTPASS_STATS *this_frame,
1162 double this_frame_mv_in_out) { 1199 double this_frame_mv_in_out,
1200 double max_boost) {
1163 double frame_boost; 1201 double frame_boost;
1164 1202
1165 // Underlying boost factor is based on inter intra error ratio. 1203 // Underlying boost factor is based on inter error ratio.
1166 if (this_frame->intra_error > twopass->gf_intra_err_min) 1204 frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) /
1167 frame_boost = (IIFACTOR * this_frame->intra_error / 1205 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1168 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)); 1206 frame_boost = frame_boost * BOOST_FACTOR;
1169 else
1170 frame_boost = (IIFACTOR * twopass->gf_intra_err_min /
1171 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1172 1207
1173 // Increase boost for frames where new data coming into frame (e.g. zoom out). 1208 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1174 // Slightly reduce boost if there is a net balance of motion out of the frame 1209 // Slightly reduce boost if there is a net balance of motion out of the frame
1175 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. 1210 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1176 if (this_frame_mv_in_out > 0.0) 1211 if (this_frame_mv_in_out > 0.0)
1177 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); 1212 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1178 // In the extreme case the boost is halved. 1213 // In the extreme case the boost is halved.
1179 else 1214 else
1180 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); 1215 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1181 1216
1182 return MIN(frame_boost, GF_RMAX); 1217 return MIN(frame_boost, max_boost);
1183 } 1218 }
1184 1219
1185 static int calc_arf_boost(VP9_COMP *cpi, int offset, 1220 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1186 int f_frames, int b_frames, 1221 int f_frames, int b_frames,
1187 int *f_boost, int *b_boost) { 1222 int *f_boost, int *b_boost) {
1188 TWO_PASS *const twopass = &cpi->twopass; 1223 TWO_PASS *const twopass = &cpi->twopass;
1189 int i; 1224 int i;
1190 double boost_score = 0.0; 1225 double boost_score = 0.0;
1191 double mv_ratio_accumulator = 0.0; 1226 double mv_ratio_accumulator = 0.0;
1192 double decay_accumulator = 1.0; 1227 double decay_accumulator = 1.0;
(...skipping 20 matching lines...) Expand all
1213 flash_detected = detect_flash(twopass, i + offset) || 1248 flash_detected = detect_flash(twopass, i + offset) ||
1214 detect_flash(twopass, i + offset + 1); 1249 detect_flash(twopass, i + offset + 1);
1215 1250
1216 // Accumulate the effect of prediction quality decay. 1251 // Accumulate the effect of prediction quality decay.
1217 if (!flash_detected) { 1252 if (!flash_detected) {
1218 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame); 1253 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1219 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR 1254 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1220 ? MIN_DECAY_FACTOR : decay_accumulator; 1255 ? MIN_DECAY_FACTOR : decay_accumulator;
1221 } 1256 }
1222 1257
1223 boost_score += decay_accumulator * calc_frame_boost(twopass, this_frame, 1258 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1224 this_frame_mv_in_out); 1259 this_frame_mv_in_out,
1260 GF_MAX_BOOST);
1225 } 1261 }
1226 1262
1227 *f_boost = (int)boost_score; 1263 *f_boost = (int)boost_score;
1228 1264
1229 // Reset for backward looking loop. 1265 // Reset for backward looking loop.
1230 boost_score = 0.0; 1266 boost_score = 0.0;
1231 mv_ratio_accumulator = 0.0; 1267 mv_ratio_accumulator = 0.0;
1232 decay_accumulator = 1.0; 1268 decay_accumulator = 1.0;
1233 this_frame_mv_in_out = 0.0; 1269 this_frame_mv_in_out = 0.0;
1234 mv_in_out_accumulator = 0.0; 1270 mv_in_out_accumulator = 0.0;
(...skipping 16 matching lines...) Expand all
1251 flash_detected = detect_flash(twopass, i + offset) || 1287 flash_detected = detect_flash(twopass, i + offset) ||
1252 detect_flash(twopass, i + offset + 1); 1288 detect_flash(twopass, i + offset + 1);
1253 1289
1254 // Cumulative effect of prediction quality decay. 1290 // Cumulative effect of prediction quality decay.
1255 if (!flash_detected) { 1291 if (!flash_detected) {
1256 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame); 1292 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame);
1257 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR 1293 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1258 ? MIN_DECAY_FACTOR : decay_accumulator; 1294 ? MIN_DECAY_FACTOR : decay_accumulator;
1259 } 1295 }
1260 1296
1261 boost_score += decay_accumulator * calc_frame_boost(twopass, this_frame, 1297 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1262 this_frame_mv_in_out); 1298 this_frame_mv_in_out,
1299 GF_MAX_BOOST);
1263 } 1300 }
1264 *b_boost = (int)boost_score; 1301 *b_boost = (int)boost_score;
1265 1302
1266 arf_boost = (*f_boost + *b_boost); 1303 arf_boost = (*f_boost + *b_boost);
1267 if (arf_boost < ((b_frames + f_frames) * 20)) 1304 if (arf_boost < ((b_frames + f_frames) * 20))
1268 arf_boost = ((b_frames + f_frames) * 20); 1305 arf_boost = ((b_frames + f_frames) * 20);
1269 1306
1270 return arf_boost; 1307 return arf_boost;
1271 } 1308 }
1272 1309
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
1562 // Note the error of the frame at the start of the group. This will be 1599 // Note the error of the frame at the start of the group. This will be
1563 // the GF frame error if we code a normal gf. 1600 // the GF frame error if we code a normal gf.
1564 gf_first_frame_err = mod_frame_err; 1601 gf_first_frame_err = mod_frame_err;
1565 1602
1566 // If this is a key frame or the overlay from a previous arf then 1603 // If this is a key frame or the overlay from a previous arf then
1567 // the error score / cost of this frame has already been accounted for. 1604 // the error score / cost of this frame has already been accounted for.
1568 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) 1605 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1569 gf_group_err -= gf_first_frame_err; 1606 gf_group_err -= gf_first_frame_err;
1570 1607
1571 // Motion breakout threshold for loop below depends on image size. 1608 // Motion breakout threshold for loop below depends on image size.
1572 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 10.0; 1609 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0;
1573 1610
1574 // Work out a maximum interval for the GF group. 1611 // Work out a maximum interval for the GF group.
1575 // If the image appears almost completely static we can extend beyond this. 1612 // If the image appears almost completely static we can extend beyond this.
1576 if (cpi->multi_arf_allowed) { 1613 if (cpi->multi_arf_allowed) {
1577 active_max_gf_interval = rc->max_gf_interval; 1614 active_max_gf_interval = rc->max_gf_interval;
1578 } else { 1615 } else {
1579 // The value chosen depends on the active Q range. At low Q we have 1616 // The value chosen depends on the active Q range. At low Q we have
1580 // bits to spare and are better with a smaller interval and smaller boost. 1617 // bits to spare and are better with a smaller interval and smaller boost.
1581 // At high Q when there are few bits to spare we are better with a longer 1618 // At high Q when there are few bits to spare we are better with a longer
1582 // interval to spread the cost of the GF. 1619 // interval to spread the cost of the GF.
1583 active_max_gf_interval = 1620 active_max_gf_interval =
1584 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME]) >> 5); 1621 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME],
1622 cpi->common.bit_depth) >> 5);
1585 1623
1586 if (active_max_gf_interval > rc->max_gf_interval) 1624 if (active_max_gf_interval > rc->max_gf_interval)
1587 active_max_gf_interval = rc->max_gf_interval; 1625 active_max_gf_interval = rc->max_gf_interval;
1588 } 1626 }
1589 1627
1590 i = 0; 1628 i = 0;
1591 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { 1629 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1592 ++i; 1630 ++i;
1593 1631
1594 // Accumulate error score of frames in this gf group. 1632 // Accumulate error score of frames in this gf group.
(...skipping 10 matching lines...) Expand all
1605 // Update the motion related elements to the boost calculation. 1643 // Update the motion related elements to the boost calculation.
1606 accumulate_frame_motion_stats(&next_frame, 1644 accumulate_frame_motion_stats(&next_frame,
1607 &this_frame_mv_in_out, &mv_in_out_accumulator, 1645 &this_frame_mv_in_out, &mv_in_out_accumulator,
1608 &abs_mv_in_out_accumulator, 1646 &abs_mv_in_out_accumulator,
1609 &mv_ratio_accumulator); 1647 &mv_ratio_accumulator);
1610 1648
1611 // Accumulate the effect of prediction quality decay. 1649 // Accumulate the effect of prediction quality decay.
1612 if (!flash_detected) { 1650 if (!flash_detected) {
1613 last_loop_decay_rate = loop_decay_rate; 1651 last_loop_decay_rate = loop_decay_rate;
1614 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); 1652 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame);
1653
1615 decay_accumulator = decay_accumulator * loop_decay_rate; 1654 decay_accumulator = decay_accumulator * loop_decay_rate;
1616 1655
1617 // Monitor for static sections. 1656 // Monitor for static sections.
1618 zero_motion_accumulator = MIN(zero_motion_accumulator, 1657 zero_motion_accumulator =
1619 get_zero_motion_factor(&next_frame)); 1658 MIN(zero_motion_accumulator,
1659 get_zero_motion_factor(&cpi->common, &next_frame));
1620 1660
1621 // Break clause to detect very still sections after motion. For example, 1661 // Break clause to detect very still sections after motion. For example,
1622 // a static image after a fade or other transition. 1662 // a static image after a fade or other transition.
1623 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, 1663 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1624 last_loop_decay_rate)) { 1664 last_loop_decay_rate)) {
1625 allow_alt_ref = 0; 1665 allow_alt_ref = 0;
1626 break; 1666 break;
1627 } 1667 }
1628 } 1668 }
1629 1669
1630 // Calculate a boost number for this frame. 1670 // Calculate a boost number for this frame.
1631 boost_score += decay_accumulator * calc_frame_boost(twopass, &next_frame, 1671 boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame,
1632 this_frame_mv_in_out); 1672 this_frame_mv_in_out,
1673 GF_MAX_BOOST);
1633 1674
1634 // Break out conditions. 1675 // Break out conditions.
1635 if ( 1676 if (
1636 // Break at active_max_gf_interval unless almost totally static. 1677 // Break at active_max_gf_interval unless almost totally static.
1637 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || 1678 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1638 ( 1679 (
1639 // Don't break out with a very short interval. 1680 // Don't break out with a very short interval.
1640 (i > MIN_GF_INTERVAL) && 1681 (i > MIN_GF_INTERVAL) &&
1641 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
1642 (!flash_detected) && 1682 (!flash_detected) &&
1643 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || 1683 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
1644 (abs_mv_in_out_accumulator > 3.0) || 1684 (abs_mv_in_out_accumulator > 3.0) ||
1645 (mv_in_out_accumulator < -2.0) || 1685 (mv_in_out_accumulator < -2.0) ||
1646 ((boost_score - old_boost_score) < IIFACTOR)))) { 1686 ((boost_score - old_boost_score) < BOOST_FACTOR)))) {
1647 boost_score = old_boost_score; 1687 boost_score = old_boost_score;
1648 break; 1688 break;
1649 } 1689 }
1650 1690
1651 *this_frame = next_frame; 1691 *this_frame = next_frame;
1652
1653 old_boost_score = boost_score; 1692 old_boost_score = boost_score;
1654 } 1693 }
1655 1694
1656 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); 1695 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0);
1657 1696
1658 // Don't allow a gf too near the next kf.
1659 if ((rc->frames_to_key - i) < MIN_GF_INTERVAL) {
1660 while (i < (rc->frames_to_key + !rc->next_key_frame_forced)) {
1661 ++i;
1662
1663 if (EOF == input_stats(twopass, this_frame))
1664 break;
1665
1666 if (i < rc->frames_to_key) {
1667 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1668 gf_group_err += mod_frame_err;
1669 }
1670 }
1671 }
1672
1673 // Set the interval until the next gf. 1697 // Set the interval until the next gf.
1674 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) 1698 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1675 rc->baseline_gf_interval = i - 1; 1699 rc->baseline_gf_interval = i - 1;
1676 else 1700 else
1677 rc->baseline_gf_interval = i; 1701 rc->baseline_gf_interval = i;
1678 1702
1679 // Only encode alt reference frame in temporal base layer. So 1703 // Only encode alt reference frame in temporal base layer. So
1680 // baseline_gf_interval should be multiple of a temporal layer group 1704 // baseline_gf_interval should be multiple of a temporal layer group
1681 // (typically the frame distance between two base layer frames) 1705 // (typically the frame distance between two base layer frames)
1682 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { 1706 if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) {
1683 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; 1707 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
1684 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); 1708 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
1685 int j; 1709 int j;
1686 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { 1710 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
1687 if (EOF == input_stats(twopass, this_frame)) 1711 if (EOF == input_stats(twopass, this_frame))
1688 break; 1712 break;
1689 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); 1713 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
1690 } 1714 }
1691 rc->baseline_gf_interval = new_gf_interval; 1715 rc->baseline_gf_interval = new_gf_interval;
1692 } 1716 }
1693 1717
1694 rc->frames_till_gf_update_due = rc->baseline_gf_interval; 1718 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1695 1719
1696 // Should we use the alternate reference frame. 1720 // Should we use the alternate reference frame.
1697 if (allow_alt_ref && 1721 if (allow_alt_ref &&
1698 (i < cpi->oxcf.lag_in_frames) && 1722 (i < cpi->oxcf.lag_in_frames) &&
1699 (i >= MIN_GF_INTERVAL) && 1723 (i >= MIN_GF_INTERVAL)) {
1700 // For real scene cuts (not forced kfs) don't allow arf very near kf.
1701 (rc->next_key_frame_forced ||
1702 (i <= (rc->frames_to_key - MIN_GF_INTERVAL)))) {
1703 // Calculate the boost for alt ref. 1724 // Calculate the boost for alt ref.
1704 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, 1725 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
1705 &b_boost); 1726 &b_boost);
1706 rc->source_alt_ref_pending = 1; 1727 rc->source_alt_ref_pending = 1;
1707 1728
1708 // Test to see if multi arf is appropriate. 1729 // Test to see if multi arf is appropriate.
1709 cpi->multi_arf_enabled = 1730 cpi->multi_arf_enabled =
1710 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) && 1731 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) &&
1711 (zero_motion_accumulator < 0.995)) ? 1 : 0; 1732 (zero_motion_accumulator < 0.995)) ? 1 : 0;
1712 } else { 1733 } else {
1713 rc->gfu_boost = (int)boost_score; 1734 rc->gfu_boost = MAX((int)boost_score, 125);
1714 rc->source_alt_ref_pending = 0; 1735 rc->source_alt_ref_pending = 0;
1715 } 1736 }
1716 1737
1717 // Reset the file position. 1738 // Reset the file position.
1718 reset_fpf_position(twopass, start_pos); 1739 reset_fpf_position(twopass, start_pos);
1719 1740
1720 // Calculate the bits to be allocated to the gf/arf group as a whole 1741 // Calculate the bits to be allocated to the gf/arf group as a whole
1721 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); 1742 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
1722 1743
1723 // Calculate the extra bits to be used for boosted frame(s) 1744 // Calculate the extra bits to be used for boosted frame(s)
1724 { 1745 {
1725 int q = rc->last_q[INTER_FRAME]; 1746 int q = rc->last_q[INTER_FRAME];
1726 int boost = (rc->gfu_boost * gfboost_qadjust(q)) / 100; 1747 int boost =
1748 (rc->gfu_boost * gfboost_qadjust(q, cpi->common.bit_depth)) / 100;
1727 1749
1728 // Set max and minimum boost and hence minimum allocation. 1750 // Set max and minimum boost and hence minimum allocation.
1729 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); 1751 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200);
1730 1752
1731 // Calculate the extra bits to be used for boosted frame(s) 1753 // Calculate the extra bits to be used for boosted frame(s)
1732 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, 1754 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
1733 boost, gf_group_bits); 1755 boost, gf_group_bits);
1734 } 1756 }
1735 1757
1736 // Adjust KF group bits and error remaining. 1758 // Adjust KF group bits and error remaining.
(...skipping 20 matching lines...) Expand all
1757 reset_fpf_position(twopass, start_pos); 1779 reset_fpf_position(twopass, start_pos);
1758 1780
1759 // Calculate a section intra ratio used in setting max loop filter. 1781 // Calculate a section intra ratio used in setting max loop filter.
1760 if (cpi->common.frame_type != KEY_FRAME) { 1782 if (cpi->common.frame_type != KEY_FRAME) {
1761 twopass->section_intra_rating = 1783 twopass->section_intra_rating =
1762 calculate_section_intra_ratio(start_pos, twopass->stats_in_end, 1784 calculate_section_intra_ratio(start_pos, twopass->stats_in_end,
1763 rc->baseline_gf_interval); 1785 rc->baseline_gf_interval);
1764 } 1786 }
1765 } 1787 }
1766 1788
1789 // TODO(PGW) Re-examine the use of II ration in this code in the light of#
1790 // changes elsewhere
1791 #define KF_II_MAX 128.0
1767 static int test_candidate_kf(TWO_PASS *twopass, 1792 static int test_candidate_kf(TWO_PASS *twopass,
1768 const FIRSTPASS_STATS *last_frame, 1793 const FIRSTPASS_STATS *last_frame,
1769 const FIRSTPASS_STATS *this_frame, 1794 const FIRSTPASS_STATS *this_frame,
1770 const FIRSTPASS_STATS *next_frame) { 1795 const FIRSTPASS_STATS *next_frame) {
1771 int is_viable_kf = 0; 1796 int is_viable_kf = 0;
1772 1797
1773 // Does the frame satisfy the primary criteria of a key frame? 1798 // Does the frame satisfy the primary criteria of a key frame?
1774 // If so, then examine how well it predicts subsequent frames. 1799 // If so, then examine how well it predicts subsequent frames.
1775 if ((this_frame->pcnt_second_ref < 0.10) && 1800 if ((this_frame->pcnt_second_ref < 0.10) &&
1776 (next_frame->pcnt_second_ref < 0.10) && 1801 (next_frame->pcnt_second_ref < 0.10) &&
1777 ((this_frame->pcnt_inter < 0.05) || 1802 ((this_frame->pcnt_inter < 0.05) ||
1778 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) && 1803 (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < 0.35) &&
1779 ((this_frame->intra_error / 1804 ((this_frame->intra_error /
1780 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) && 1805 DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
1781 ((fabs(last_frame->coded_error - this_frame->coded_error) / 1806 ((fabs(last_frame->coded_error - this_frame->coded_error) /
1782 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) || 1807 DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > 0.40) ||
1783 (fabs(last_frame->intra_error - this_frame->intra_error) / 1808 (fabs(last_frame->intra_error - this_frame->intra_error) /
1784 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) || 1809 DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > 0.40) ||
1785 ((next_frame->intra_error / 1810 ((next_frame->intra_error /
1786 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) { 1811 DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
1787 int i; 1812 int i;
1788 const FIRSTPASS_STATS *start_pos = twopass->stats_in; 1813 const FIRSTPASS_STATS *start_pos = twopass->stats_in;
1789 FIRSTPASS_STATS local_next_frame = *next_frame; 1814 FIRSTPASS_STATS local_next_frame = *next_frame;
1790 double boost_score = 0.0; 1815 double boost_score = 0.0;
1791 double old_boost_score = 0.0; 1816 double old_boost_score = 0.0;
1792 double decay_accumulator = 1.0; 1817 double decay_accumulator = 1.0;
1793 1818
1794 // Examine how well the key frame predicts subsequent frames. 1819 // Examine how well the key frame predicts subsequent frames.
1795 for (i = 0; i < 16; ++i) { 1820 for (i = 0; i < 16; ++i) {
1796 double next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / 1821 double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
1797 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); 1822 DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
1798 1823
1799 if (next_iiratio > RMAX) 1824 if (next_iiratio > KF_II_MAX)
1800 next_iiratio = RMAX; 1825 next_iiratio = KF_II_MAX;
1801 1826
1802 // Cumulative effect of decay in prediction quality. 1827 // Cumulative effect of decay in prediction quality.
1803 if (local_next_frame.pcnt_inter > 0.85) 1828 if (local_next_frame.pcnt_inter > 0.85)
1804 decay_accumulator *= local_next_frame.pcnt_inter; 1829 decay_accumulator *= local_next_frame.pcnt_inter;
1805 else 1830 else
1806 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0; 1831 decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0;
1807 1832
1808 // Keep a running total. 1833 // Keep a running total.
1809 boost_score += (decay_accumulator * next_iiratio); 1834 boost_score += (decay_accumulator * next_iiratio);
1810 1835
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1845 int i, j; 1870 int i, j;
1846 RATE_CONTROL *const rc = &cpi->rc; 1871 RATE_CONTROL *const rc = &cpi->rc;
1847 TWO_PASS *const twopass = &cpi->twopass; 1872 TWO_PASS *const twopass = &cpi->twopass;
1848 GF_GROUP *const gf_group = &twopass->gf_group; 1873 GF_GROUP *const gf_group = &twopass->gf_group;
1849 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1874 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1850 const FIRSTPASS_STATS first_frame = *this_frame; 1875 const FIRSTPASS_STATS first_frame = *this_frame;
1851 const FIRSTPASS_STATS *const start_position = twopass->stats_in; 1876 const FIRSTPASS_STATS *const start_position = twopass->stats_in;
1852 FIRSTPASS_STATS next_frame; 1877 FIRSTPASS_STATS next_frame;
1853 FIRSTPASS_STATS last_frame; 1878 FIRSTPASS_STATS last_frame;
1854 int kf_bits = 0; 1879 int kf_bits = 0;
1880 int loop_decay_counter = 0;
1855 double decay_accumulator = 1.0; 1881 double decay_accumulator = 1.0;
1882 double av_decay_accumulator = 0.0;
1856 double zero_motion_accumulator = 1.0; 1883 double zero_motion_accumulator = 1.0;
1857 double boost_score = 0.0; 1884 double boost_score = 0.0;
1858 double kf_mod_err = 0.0; 1885 double kf_mod_err = 0.0;
1859 double kf_group_err = 0.0; 1886 double kf_group_err = 0.0;
1860 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; 1887 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
1861 1888
1862 vp9_zero(next_frame); 1889 vp9_zero(next_frame);
1863 1890
1864 cpi->common.frame_type = KEY_FRAME; 1891 cpi->common.frame_type = KEY_FRAME;
1865 1892
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1999 if (twopass->kf_group_bits > max_grp_bits) 2026 if (twopass->kf_group_bits > max_grp_bits)
2000 twopass->kf_group_bits = max_grp_bits; 2027 twopass->kf_group_bits = max_grp_bits;
2001 } else { 2028 } else {
2002 twopass->kf_group_bits = 0; 2029 twopass->kf_group_bits = 0;
2003 } 2030 }
2004 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); 2031 twopass->kf_group_bits = MAX(0, twopass->kf_group_bits);
2005 2032
2006 // Reset the first pass file position. 2033 // Reset the first pass file position.
2007 reset_fpf_position(twopass, start_position); 2034 reset_fpf_position(twopass, start_position);
2008 2035
2009 // Scan through the kf group collating various stats used to deteermine 2036 // Scan through the kf group collating various stats used to determine
2010 // how many bits to spend on it. 2037 // how many bits to spend on it.
2011 decay_accumulator = 1.0; 2038 decay_accumulator = 1.0;
2012 boost_score = 0.0; 2039 boost_score = 0.0;
2013 for (i = 0; i < rc->frames_to_key; ++i) { 2040 for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2014 if (EOF == input_stats(twopass, &next_frame)) 2041 if (EOF == input_stats(twopass, &next_frame))
2015 break; 2042 break;
2016 2043
2017 // Monitor for static sections. 2044 // Monitor for static sections.
2018 zero_motion_accumulator =MIN(zero_motion_accumulator, 2045 zero_motion_accumulator =
2019 get_zero_motion_factor(&next_frame)); 2046 MIN(zero_motion_accumulator,
2047 get_zero_motion_factor(&cpi->common, &next_frame));
2020 2048
2021 // For the first few frames collect data to decide kf boost. 2049 // Not all frames in the group are necessarily used in calculating boost.
2022 if (i <= (rc->max_gf_interval * 2)) { 2050 if ((i <= rc->max_gf_interval) ||
2023 double r; 2051 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2024 if (next_frame.intra_error > twopass->kf_intra_err_min) 2052 const double frame_boost =
2025 r = (IIKFACTOR2 * next_frame.intra_error / 2053 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2026 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2027 else
2028 r = (IIKFACTOR2 * twopass->kf_intra_err_min /
2029 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2030
2031 if (r > RMAX)
2032 r = RMAX;
2033 2054
2034 // How fast is prediction quality decaying. 2055 // How fast is prediction quality decaying.
2035 if (!detect_flash(twopass, 0)) { 2056 if (!detect_flash(twopass, 0)) {
2036 const double loop_decay_rate = get_prediction_decay_rate(&cpi->common, 2057 const double loop_decay_rate =
2037 &next_frame); 2058 get_prediction_decay_rate(&cpi->common, &next_frame);
2038 decay_accumulator *= loop_decay_rate; 2059 decay_accumulator *= loop_decay_rate;
2039 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); 2060 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2061 av_decay_accumulator += decay_accumulator;
2062 ++loop_decay_counter;
2040 } 2063 }
2041 2064 boost_score += (decay_accumulator * frame_boost);
2042 boost_score += (decay_accumulator * r);
2043 } 2065 }
2044 } 2066 }
2067 av_decay_accumulator /= (double)loop_decay_counter;
2045 2068
2046 reset_fpf_position(twopass, start_position); 2069 reset_fpf_position(twopass, start_position);
2047 2070
2048 // Store the zero motion percentage 2071 // Store the zero motion percentage
2049 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); 2072 twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2050 2073
2051 // Calculate a section intra ratio used in setting max loop filter. 2074 // Calculate a section intra ratio used in setting max loop filter.
2052 twopass->section_intra_rating = 2075 twopass->section_intra_rating =
2053 calculate_section_intra_ratio(start_position, twopass->stats_in_end, 2076 calculate_section_intra_ratio(start_position, twopass->stats_in_end,
2054 rc->frames_to_key); 2077 rc->frames_to_key);
2055 2078
2079 // Apply various clamps for min and max boost
2080 rc->kf_boost = (int)(av_decay_accumulator * boost_score);
2081 rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3));
2082 rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST);
2083
2056 // Work out how many bits to allocate for the key frame itself. 2084 // Work out how many bits to allocate for the key frame itself.
2057 rc->kf_boost = (int)boost_score;
2058
2059 if (rc->kf_boost < (rc->frames_to_key * 3))
2060 rc->kf_boost = (rc->frames_to_key * 3);
2061 if (rc->kf_boost < MIN_KF_BOOST)
2062 rc->kf_boost = MIN_KF_BOOST;
2063
2064 kf_bits = calculate_boost_bits((rc->frames_to_key - 1), 2085 kf_bits = calculate_boost_bits((rc->frames_to_key - 1),
2065 rc->kf_boost, twopass->kf_group_bits); 2086 rc->kf_boost, twopass->kf_group_bits);
2066 2087
2067 twopass->kf_group_bits -= kf_bits; 2088 twopass->kf_group_bits -= kf_bits;
2068 2089
2069 // Save the bits to spend on the key frame. 2090 // Save the bits to spend on the key frame.
2070 gf_group->bit_allocation[0] = kf_bits; 2091 gf_group->bit_allocation[0] = kf_bits;
2071 gf_group->update_type[0] = KF_UPDATE; 2092 gf_group->update_type[0] = KF_UPDATE;
2072 gf_group->rf_level[0] = KF_STD; 2093 gf_group->rf_level[0] = KF_STD;
2073 2094
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2127 case ARF_UPDATE: 2148 case ARF_UPDATE:
2128 cpi->refresh_last_frame = 0; 2149 cpi->refresh_last_frame = 0;
2129 cpi->refresh_golden_frame = 0; 2150 cpi->refresh_golden_frame = 0;
2130 cpi->refresh_alt_ref_frame = 1; 2151 cpi->refresh_alt_ref_frame = 1;
2131 break; 2152 break;
2132 default: 2153 default:
2133 assert(0); 2154 assert(0);
2134 break; 2155 break;
2135 } 2156 }
2136 if (is_two_pass_svc(cpi)) { 2157 if (is_two_pass_svc(cpi)) {
2158 if (cpi->svc.temporal_layer_id > 0) {
2159 cpi->refresh_last_frame = 0;
2160 cpi->refresh_golden_frame = 0;
2161 }
2137 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0) 2162 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2138 cpi->refresh_golden_frame = 0; 2163 cpi->refresh_golden_frame = 0;
2139 if (cpi->alt_ref_source == NULL) 2164 if (cpi->alt_ref_source == NULL)
2140 cpi->refresh_alt_ref_frame = 0; 2165 cpi->refresh_alt_ref_frame = 0;
2141 } 2166 }
2142 } 2167 }
2143 2168
2144 2169
2145 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { 2170 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2146 VP9_COMMON *const cm = &cpi->common; 2171 VP9_COMMON *const cm = &cpi->common;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
2191 if (lc->is_key_frame) 2216 if (lc->is_key_frame)
2192 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); 2217 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2193 } 2218 }
2194 } 2219 }
2195 2220
2196 return; 2221 return;
2197 } 2222 }
2198 2223
2199 vp9_clear_system_state(); 2224 vp9_clear_system_state();
2200 2225
2201 if (lc != NULL && twopass->kf_intra_err_min == 0) {
2202 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
2203 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
2204 }
2205
2206 if (cpi->oxcf.rc_mode == VPX_Q) { 2226 if (cpi->oxcf.rc_mode == VPX_Q) {
2207 twopass->active_worst_quality = cpi->oxcf.cq_level; 2227 twopass->active_worst_quality = cpi->oxcf.cq_level;
2208 } else if (cm->current_video_frame == 0 || 2228 } else if (cm->current_video_frame == 0 ||
2209 (lc != NULL && lc->current_video_frame_in_layer == 0)) { 2229 (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2210 // Special case code for first frame. 2230 // Special case code for first frame.
2211 const int section_target_bandwidth = (int)(twopass->bits_left / 2231 const int section_target_bandwidth = (int)(twopass->bits_left /
2212 frames_left); 2232 frames_left);
2213 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, 2233 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats,
2214 section_target_bandwidth); 2234 section_target_bandwidth);
2215 twopass->active_worst_quality = tmp_q; 2235 twopass->active_worst_quality = tmp_q;
2216 rc->ni_av_qi = tmp_q; 2236 rc->ni_av_qi = tmp_q;
2217 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); 2237 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2218 } 2238 }
2219 vp9_zero(this_frame); 2239 vp9_zero(this_frame);
2220 if (EOF == input_stats(twopass, &this_frame)) 2240 if (EOF == input_stats(twopass, &this_frame))
2221 return; 2241 return;
2222 2242
2223 // Local copy of the current frame's first pass stats. 2243 // Local copy of the current frame's first pass stats.
2224 this_frame_copy = this_frame; 2244 this_frame_copy = this_frame;
2225 2245
2226 // Keyframe and section processing. 2246 // Keyframe and section processing.
2227 if (rc->frames_to_key == 0 || 2247 if (rc->frames_to_key == 0 ||
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2261 // ENCODE_BREAKOUT_LIMITED. 2281 // ENCODE_BREAKOUT_LIMITED.
2262 if (!cm->show_frame) 2282 if (!cm->show_frame)
2263 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; 2283 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED;
2264 else 2284 else
2265 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; 2285 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED;
2266 } 2286 }
2267 2287
2268 rc->frames_till_gf_update_due = rc->baseline_gf_interval; 2288 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
2269 if (lc != NULL) 2289 if (lc != NULL)
2270 cpi->refresh_golden_frame = 1; 2290 cpi->refresh_golden_frame = 1;
2291
2292 #if ARF_STATS_OUTPUT
2293 {
2294 FILE *fpfile;
2295 fpfile = fopen("arf.stt", "a");
2296 ++arf_count;
2297 fprintf(fpfile, "%10d %10d %10d %10ld\n",
2298 cm->current_video_frame, rc->kf_boost, arf_count, rc->gfu_boost);
2299
2300 fclose(fpfile);
2301 }
2302 #endif
2271 } 2303 }
2272 2304
2273 configure_buffer_updates(cpi); 2305 configure_buffer_updates(cpi);
2274 2306
2275 target_rate = gf_group->bit_allocation[gf_group->index]; 2307 target_rate = gf_group->bit_allocation[gf_group->index];
2276 if (cpi->common.frame_type == KEY_FRAME) 2308 if (cpi->common.frame_type == KEY_FRAME)
2277 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); 2309 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2278 else 2310 else
2279 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); 2311 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2280 2312
(...skipping 19 matching lines...) Expand all
2300 // is designed to prevent extreme behaviour at the end of a clip 2332 // is designed to prevent extreme behaviour at the end of a clip
2301 // or group of frames. 2333 // or group of frames.
2302 const int bits_used = rc->base_frame_target; 2334 const int bits_used = rc->base_frame_target;
2303 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; 2335 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
2304 2336
2305 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); 2337 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0);
2306 2338
2307 if (cpi->common.frame_type != KEY_FRAME && 2339 if (cpi->common.frame_type != KEY_FRAME &&
2308 !vp9_is_upper_layer_key_frame(cpi)) { 2340 !vp9_is_upper_layer_key_frame(cpi)) {
2309 twopass->kf_group_bits -= bits_used; 2341 twopass->kf_group_bits -= bits_used;
2342 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
2310 } 2343 }
2311 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); 2344 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0);
2312 2345
2313 // Increment the gf group index ready for the next frame. 2346 // Increment the gf group index ready for the next frame.
2314 ++twopass->gf_group.index; 2347 ++twopass->gf_group.index;
2315 } 2348 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_lookahead.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698