| 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 <limits.h> |
| 12 #include <math.h> | 12 #include <math.h> |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 | 14 |
| 15 #include "./vpx_scale_rtcd.h" | 15 #include "./vpx_scale_rtcd.h" |
| 16 | 16 |
| 17 #include "vpx_mem/vpx_mem.h" | 17 #include "vpx_mem/vpx_mem.h" |
| 18 #include "vpx_scale/vpx_scale.h" | 18 #include "vpx_scale/vpx_scale.h" |
| 19 #include "vpx_scale/yv12config.h" | 19 #include "vpx_scale/yv12config.h" |
| 20 | 20 |
| 21 #include "vp9/common/vp9_entropymv.h" | 21 #include "vp9/common/vp9_entropymv.h" |
| 22 #include "vp9/common/vp9_quant_common.h" | 22 #include "vp9/common/vp9_quant_common.h" |
| 23 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes() | 23 #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes() |
| 24 #include "vp9/common/vp9_systemdependent.h" | 24 #include "vp9/common/vp9_systemdependent.h" |
| 25 | |
| 26 #include "vp9/encoder/vp9_aq_variance.h" | 25 #include "vp9/encoder/vp9_aq_variance.h" |
| 27 #include "vp9/encoder/vp9_block.h" | 26 #include "vp9/encoder/vp9_block.h" |
| 28 #include "vp9/encoder/vp9_encodeframe.h" | 27 #include "vp9/encoder/vp9_encodeframe.h" |
| 29 #include "vp9/encoder/vp9_encodemb.h" | 28 #include "vp9/encoder/vp9_encodemb.h" |
| 30 #include "vp9/encoder/vp9_encodemv.h" | 29 #include "vp9/encoder/vp9_encodemv.h" |
| 31 #include "vp9/encoder/vp9_encoder.h" | 30 #include "vp9/encoder/vp9_encoder.h" |
| 32 #include "vp9/encoder/vp9_extend.h" | 31 #include "vp9/encoder/vp9_extend.h" |
| 33 #include "vp9/encoder/vp9_firstpass.h" | 32 #include "vp9/encoder/vp9_firstpass.h" |
| 34 #include "vp9/encoder/vp9_mcomp.h" | 33 #include "vp9/encoder/vp9_mcomp.h" |
| 35 #include "vp9/encoder/vp9_quantize.h" | 34 #include "vp9/encoder/vp9_quantize.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 49 #define FACTOR_PT_LOW 0.5 | 48 #define FACTOR_PT_LOW 0.5 |
| 50 #define FACTOR_PT_HIGH 0.9 | 49 #define FACTOR_PT_HIGH 0.9 |
| 51 | 50 |
| 52 #define KF_MB_INTRA_MIN 150 | 51 #define KF_MB_INTRA_MIN 150 |
| 53 #define GF_MB_INTRA_MIN 100 | 52 #define GF_MB_INTRA_MIN 100 |
| 54 | 53 |
| 55 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 54 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 56 | 55 |
| 57 #define MIN_KF_BOOST 300 | 56 #define MIN_KF_BOOST 300 |
| 58 #define MIN_GF_INTERVAL 4 | 57 #define MIN_GF_INTERVAL 4 |
| 59 #define LONG_TERM_VBR_CORRECTION | |
| 60 | 58 |
| 61 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 59 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
| 62 YV12_BUFFER_CONFIG temp = *a; | 60 YV12_BUFFER_CONFIG temp = *a; |
| 63 *a = *b; | 61 *a = *b; |
| 64 *b = temp; | 62 *b = temp; |
| 65 } | 63 } |
| 66 | 64 |
| 67 static int gfboost_qadjust(int qindex) { | 65 static int gfboost_qadjust(int qindex) { |
| 68 const double q = vp9_convert_qindex_to_q(qindex); | 66 const double q = vp9_convert_qindex_to_q(qindex); |
| 69 return (int)((0.00000828 * q * q * q) + | 67 return (int)((0.00000828 * q * q * q) + |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 section->MVc -= frame->MVc; | 216 section->MVc -= frame->MVc; |
| 219 section->mvc_abs -= frame->mvc_abs; | 217 section->mvc_abs -= frame->mvc_abs; |
| 220 section->MVrv -= frame->MVrv; | 218 section->MVrv -= frame->MVrv; |
| 221 section->MVcv -= frame->MVcv; | 219 section->MVcv -= frame->MVcv; |
| 222 section->mv_in_out_count -= frame->mv_in_out_count; | 220 section->mv_in_out_count -= frame->mv_in_out_count; |
| 223 section->new_mv_count -= frame->new_mv_count; | 221 section->new_mv_count -= frame->new_mv_count; |
| 224 section->count -= frame->count; | 222 section->count -= frame->count; |
| 225 section->duration -= frame->duration; | 223 section->duration -= frame->duration; |
| 226 } | 224 } |
| 227 | 225 |
| 228 static void avg_stats(FIRSTPASS_STATS *section) { | |
| 229 if (section->count < 1.0) | |
| 230 return; | |
| 231 | |
| 232 section->intra_error /= section->count; | |
| 233 section->coded_error /= section->count; | |
| 234 section->sr_coded_error /= section->count; | |
| 235 section->pcnt_inter /= section->count; | |
| 236 section->pcnt_second_ref /= section->count; | |
| 237 section->pcnt_neutral /= section->count; | |
| 238 section->pcnt_motion /= section->count; | |
| 239 section->MVr /= section->count; | |
| 240 section->mvr_abs /= section->count; | |
| 241 section->MVc /= section->count; | |
| 242 section->mvc_abs /= section->count; | |
| 243 section->MVrv /= section->count; | |
| 244 section->MVcv /= section->count; | |
| 245 section->mv_in_out_count /= section->count; | |
| 246 section->duration /= section->count; | |
| 247 } | |
| 248 | 226 |
| 249 // Calculate a modified Error used in distributing bits between easier and | 227 // Calculate a modified Error used in distributing bits between easier and |
| 250 // harder frames. | 228 // harder frames. |
| 251 static double calculate_modified_err(const TWO_PASS *twopass, | 229 static double calculate_modified_err(const TWO_PASS *twopass, |
| 252 const VP9EncoderConfig *oxcf, | 230 const VP9EncoderConfig *oxcf, |
| 253 const FIRSTPASS_STATS *this_frame) { | 231 const FIRSTPASS_STATS *this_frame) { |
| 254 const FIRSTPASS_STATS *const stats = &twopass->total_stats; | 232 const FIRSTPASS_STATS *const stats = &twopass->total_stats; |
| 255 const double av_err = stats->coded_error / stats->count; | 233 const double av_err = stats->coded_error / stats->count; |
| 256 const double modified_error = av_err * | 234 const double modified_error = av_err * |
| 257 pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err), | 235 pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 271 max_bits = rc->max_frame_bandwidth; | 249 max_bits = rc->max_frame_bandwidth; |
| 272 | 250 |
| 273 return (int)max_bits; | 251 return (int)max_bits; |
| 274 } | 252 } |
| 275 | 253 |
| 276 void vp9_init_first_pass(VP9_COMP *cpi) { | 254 void vp9_init_first_pass(VP9_COMP *cpi) { |
| 277 zero_stats(&cpi->twopass.total_stats); | 255 zero_stats(&cpi->twopass.total_stats); |
| 278 } | 256 } |
| 279 | 257 |
| 280 void vp9_end_first_pass(VP9_COMP *cpi) { | 258 void vp9_end_first_pass(VP9_COMP *cpi) { |
| 281 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 259 if (is_spatial_svc(cpi)) { |
| 282 int i; | 260 int i; |
| 283 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) { | 261 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) { |
| 284 output_stats(&cpi->svc.layer_context[i].twopass.total_stats, | 262 output_stats(&cpi->svc.layer_context[i].twopass.total_stats, |
| 285 cpi->output_pkt_list); | 263 cpi->output_pkt_list); |
| 286 } | 264 } |
| 287 } else { | 265 } else { |
| 288 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); | 266 output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); |
| 289 } | 267 } |
| 290 } | 268 } |
| 291 | 269 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 if (cpi->use_fp_mb_stats) { | 437 if (cpi->use_fp_mb_stats) { |
| 460 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs); | 438 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs); |
| 461 } | 439 } |
| 462 #endif | 440 #endif |
| 463 | 441 |
| 464 vp9_clear_system_state(); | 442 vp9_clear_system_state(); |
| 465 | 443 |
| 466 set_first_pass_params(cpi); | 444 set_first_pass_params(cpi); |
| 467 vp9_set_quantizer(cm, find_fp_qindex()); | 445 vp9_set_quantizer(cm, find_fp_qindex()); |
| 468 | 446 |
| 469 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 447 if (is_spatial_svc(cpi)) { |
| 470 MV_REFERENCE_FRAME ref_frame = LAST_FRAME; | 448 MV_REFERENCE_FRAME ref_frame = LAST_FRAME; |
| 471 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL; | 449 const YV12_BUFFER_CONFIG *scaled_ref_buf = NULL; |
| 472 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass; | 450 twopass = &cpi->svc.layer_context[cpi->svc.spatial_layer_id].twopass; |
| 473 | 451 |
| 474 if (cpi->common.current_video_frame == 0) { | 452 if (cpi->common.current_video_frame == 0) { |
| 475 cpi->ref_frame_flags = 0; | 453 cpi->ref_frame_flags = 0; |
| 476 } else { | 454 } else { |
| 477 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 455 LAYER_CONTEXT *lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 478 if (lc->current_video_frame_in_layer == 0) | 456 if (lc->current_video_frame_in_layer == 0) |
| 479 cpi->ref_frame_flags = VP9_GOLD_FLAG; | 457 cpi->ref_frame_flags = VP9_GOLD_FLAG; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 // outside the UMV borders. | 524 // outside the UMV borders. |
| 547 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); | 525 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
| 548 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) | 526 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
| 549 + BORDER_MV_PIXELS_B16; | 527 + BORDER_MV_PIXELS_B16; |
| 550 | 528 |
| 551 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { | 529 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
| 552 int this_error; | 530 int this_error; |
| 553 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); | 531 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
| 554 double error_weight = 1.0; | 532 double error_weight = 1.0; |
| 555 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); | 533 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); |
| 534 #if CONFIG_FP_MB_STATS |
| 535 const int mb_index = mb_row * cm->mb_cols + mb_col; |
| 536 #endif |
| 556 | 537 |
| 557 vp9_clear_system_state(); | 538 vp9_clear_system_state(); |
| 558 | 539 |
| 559 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; | 540 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 560 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; | 541 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 561 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; | 542 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 562 xd->left_available = (mb_col != 0); | 543 xd->left_available = (mb_col != 0); |
| 563 xd->mi[0]->mbmi.sb_type = bsize; | 544 xd->mi[0]->mbmi.sb_type = bsize; |
| 564 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; | 545 xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
| 565 set_mi_row_col(xd, &tile, | 546 set_mi_row_col(xd, &tile, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 592 // When the error score is very low this causes us to pick all or lots of | 573 // When the error score is very low this causes us to pick all or lots of |
| 593 // INTRA modes and throw lots of key frames. | 574 // INTRA modes and throw lots of key frames. |
| 594 // This penalty adds a cost matching that of a 0,0 mv to the intra case. | 575 // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 595 this_error += intrapenalty; | 576 this_error += intrapenalty; |
| 596 | 577 |
| 597 // Accumulate the intra error. | 578 // Accumulate the intra error. |
| 598 intra_error += (int64_t)this_error; | 579 intra_error += (int64_t)this_error; |
| 599 | 580 |
| 600 #if CONFIG_FP_MB_STATS | 581 #if CONFIG_FP_MB_STATS |
| 601 if (cpi->use_fp_mb_stats) { | 582 if (cpi->use_fp_mb_stats) { |
| 602 // TODO(pengchong): store some related block statistics here | 583 // initialization |
| 584 cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 603 } | 585 } |
| 604 #endif | 586 #endif |
| 605 | 587 |
| 606 // Set up limit values for motion vectors to prevent them extending | 588 // Set up limit values for motion vectors to prevent them extending |
| 607 // outside the UMV borders. | 589 // outside the UMV borders. |
| 608 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); | 590 x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
| 609 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; | 591 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
| 610 | 592 |
| 611 // Other than for the first frame do a motion search. | 593 // Other than for the first frame do a motion search. |
| 612 if (cm->current_video_frame > 0) { | 594 if (cm->current_video_frame > 0) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 624 // frame as the reference. Skip the further motion search on | 606 // frame as the reference. Skip the further motion search on |
| 625 // reconstructed frame if this error is small. | 607 // reconstructed frame if this error is small. |
| 626 unscaled_last_source_buf_2d.buf = | 608 unscaled_last_source_buf_2d.buf = |
| 627 cpi->unscaled_last_source->y_buffer + recon_yoffset; | 609 cpi->unscaled_last_source->y_buffer + recon_yoffset; |
| 628 unscaled_last_source_buf_2d.stride = | 610 unscaled_last_source_buf_2d.stride = |
| 629 cpi->unscaled_last_source->y_stride; | 611 cpi->unscaled_last_source->y_stride; |
| 630 raw_motion_error = get_prediction_error(bsize, &x->plane[0].src, | 612 raw_motion_error = get_prediction_error(bsize, &x->plane[0].src, |
| 631 &unscaled_last_source_buf_2d); | 613 &unscaled_last_source_buf_2d); |
| 632 | 614 |
| 633 // TODO(pengchong): Replace the hard-coded threshold | 615 // TODO(pengchong): Replace the hard-coded threshold |
| 634 if (raw_motion_error > 25 || | 616 if (raw_motion_error > 25 || is_spatial_svc(cpi)) { |
| 635 (cpi->use_svc && cpi->svc.number_temporal_layers == 1)) { | |
| 636 // Test last reference frame using the previous best mv as the | 617 // Test last reference frame using the previous best mv as the |
| 637 // starting point (best reference) for the search. | 618 // starting point (best reference) for the search. |
| 638 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, | 619 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, &mv.as_mv, |
| 639 &motion_error); | 620 &motion_error); |
| 640 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 621 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
| 641 vp9_clear_system_state(); | 622 vp9_clear_system_state(); |
| 642 motion_error = (int)(motion_error * error_weight); | 623 motion_error = (int)(motion_error * error_weight); |
| 643 } | 624 } |
| 644 | 625 |
| 645 // If the current best reference mv is not centered on 0,0 then do a | 626 // If the current best reference mv is not centered on 0,0 then do a |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 } else { | 674 } else { |
| 694 sr_coded_error += motion_error; | 675 sr_coded_error += motion_error; |
| 695 } | 676 } |
| 696 } else { | 677 } else { |
| 697 sr_coded_error += motion_error; | 678 sr_coded_error += motion_error; |
| 698 } | 679 } |
| 699 | 680 |
| 700 // Start by assuming that intra mode is best. | 681 // Start by assuming that intra mode is best. |
| 701 best_ref_mv.as_int = 0; | 682 best_ref_mv.as_int = 0; |
| 702 | 683 |
| 684 #if CONFIG_FP_MB_STATS |
| 685 if (cpi->use_fp_mb_stats) { |
| 686 // intra predication statistics |
| 687 cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 688 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK; |
| 689 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK; |
| 690 if (this_error > FPMB_ERROR_LARGE_TH) { |
| 691 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK; |
| 692 } else if (this_error < FPMB_ERROR_SMALL_TH) { |
| 693 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK; |
| 694 } |
| 695 } |
| 696 #endif |
| 697 |
| 703 if (motion_error <= this_error) { | 698 if (motion_error <= this_error) { |
| 704 // Keep a count of cases where the inter and intra were very close | 699 // Keep a count of cases where the inter and intra were very close |
| 705 // and very low. This helps with scene cut detection for example in | 700 // and very low. This helps with scene cut detection for example in |
| 706 // cropped clips with black bars at the sides or top and bottom. | 701 // cropped clips with black bars at the sides or top and bottom. |
| 707 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && | 702 if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
| 708 this_error < 2 * intrapenalty) | 703 this_error < 2 * intrapenalty) |
| 709 ++neutral_count; | 704 ++neutral_count; |
| 710 | 705 |
| 711 mv.as_mv.row *= 8; | 706 mv.as_mv.row *= 8; |
| 712 mv.as_mv.col *= 8; | 707 mv.as_mv.col *= 8; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 723 sum_mvc += mv.as_mv.col; | 718 sum_mvc += mv.as_mv.col; |
| 724 sum_mvc_abs += abs(mv.as_mv.col); | 719 sum_mvc_abs += abs(mv.as_mv.col); |
| 725 sum_mvrs += mv.as_mv.row * mv.as_mv.row; | 720 sum_mvrs += mv.as_mv.row * mv.as_mv.row; |
| 726 sum_mvcs += mv.as_mv.col * mv.as_mv.col; | 721 sum_mvcs += mv.as_mv.col * mv.as_mv.col; |
| 727 ++intercount; | 722 ++intercount; |
| 728 | 723 |
| 729 best_ref_mv.as_int = mv.as_int; | 724 best_ref_mv.as_int = mv.as_int; |
| 730 | 725 |
| 731 #if CONFIG_FP_MB_STATS | 726 #if CONFIG_FP_MB_STATS |
| 732 if (cpi->use_fp_mb_stats) { | 727 if (cpi->use_fp_mb_stats) { |
| 733 // TODO(pengchong): save some related block statistics here | 728 // inter predication statistics |
| 729 cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 730 cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK; |
| 731 cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK; |
| 732 if (this_error > FPMB_ERROR_LARGE_TH) { |
| 733 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 734 FPMB_ERROR_LARGE_MASK; |
| 735 } else if (this_error < FPMB_ERROR_SMALL_TH) { |
| 736 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 737 FPMB_ERROR_SMALL_MASK; |
| 738 } |
| 734 } | 739 } |
| 735 #endif | 740 #endif |
| 736 | 741 |
| 737 if (mv.as_int) { | 742 if (mv.as_int) { |
| 738 ++mvcount; | 743 ++mvcount; |
| 739 | 744 |
| 745 #if CONFIG_FP_MB_STATS |
| 746 if (cpi->use_fp_mb_stats) { |
| 747 cpi->twopass.frame_mb_stats_buf[mb_index] &= |
| 748 ~FPMB_MOTION_ZERO_MASK; |
| 749 // check estimated motion direction |
| 750 if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) { |
| 751 // right direction |
| 752 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 753 FPMB_MOTION_RIGHT_MASK; |
| 754 } else if (mv.as_mv.row < 0 && |
| 755 abs(mv.as_mv.row) >= abs(mv.as_mv.col)) { |
| 756 // up direction |
| 757 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 758 FPMB_MOTION_UP_MASK; |
| 759 } else if (mv.as_mv.col < 0 && |
| 760 abs(mv.as_mv.col) >= abs(mv.as_mv.row)) { |
| 761 // left direction |
| 762 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 763 FPMB_MOTION_LEFT_MASK; |
| 764 } else { |
| 765 // down direction |
| 766 cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 767 FPMB_MOTION_DOWN_MASK; |
| 768 } |
| 769 } |
| 770 #endif |
| 771 |
| 740 // Non-zero vector, was it different from the last non zero vector? | 772 // Non-zero vector, was it different from the last non zero vector? |
| 741 if (mv.as_int != lastmv_as_int) | 773 if (mv.as_int != lastmv_as_int) |
| 742 ++new_mv_count; | 774 ++new_mv_count; |
| 743 lastmv_as_int = mv.as_int; | 775 lastmv_as_int = mv.as_int; |
| 744 | 776 |
| 745 // Does the row vector point inwards or outwards? | 777 // Does the row vector point inwards or outwards? |
| 746 if (mb_row < cm->mb_rows / 2) { | 778 if (mb_row < cm->mb_rows / 2) { |
| 747 if (mv.as_mv.row > 0) | 779 if (mv.as_mv.row > 0) |
| 748 --sum_in_vectors; | 780 --sum_in_vectors; |
| 749 else if (mv.as_mv.row < 0) | 781 else if (mv.as_mv.row < 0) |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 if (gld_yv12 != NULL) { | 888 if (gld_yv12 != NULL) { |
| 857 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 889 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| 858 } | 890 } |
| 859 twopass->sr_update_lag = 1; | 891 twopass->sr_update_lag = 1; |
| 860 } else { | 892 } else { |
| 861 ++twopass->sr_update_lag; | 893 ++twopass->sr_update_lag; |
| 862 } | 894 } |
| 863 | 895 |
| 864 vp9_extend_frame_borders(new_yv12); | 896 vp9_extend_frame_borders(new_yv12); |
| 865 | 897 |
| 866 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 898 if (is_spatial_svc(cpi)) { |
| 867 vp9_update_reference_frames(cpi); | 899 vp9_update_reference_frames(cpi); |
| 868 } else { | 900 } else { |
| 869 // Swap frame pointers so last frame refers to the frame we just compressed. | 901 // Swap frame pointers so last frame refers to the frame we just compressed. |
| 870 swap_yv12(lst_yv12, new_yv12); | 902 swap_yv12(lst_yv12, new_yv12); |
| 871 } | 903 } |
| 872 | 904 |
| 873 // Special case for the first frame. Copy into the GF buffer as a second | 905 // Special case for the first frame. Copy into the GF buffer as a second |
| 874 // reference. | 906 // reference. |
| 875 if (cm->current_video_frame == 0 && gld_yv12 != NULL) { | 907 if (cm->current_video_frame == 0 && gld_yv12 != NULL) { |
| 876 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 908 vp8_yv12_copy_frame(lst_yv12, gld_yv12); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 922 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 954 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 923 | 955 |
| 924 if (section_target_bandwidth <= 0) { | 956 if (section_target_bandwidth <= 0) { |
| 925 return rc->worst_quality; // Highest value allowed | 957 return rc->worst_quality; // Highest value allowed |
| 926 } else { | 958 } else { |
| 927 const int num_mbs = cpi->common.MBs; | 959 const int num_mbs = cpi->common.MBs; |
| 928 const double section_err = stats->coded_error / stats->count; | 960 const double section_err = stats->coded_error / stats->count; |
| 929 const double err_per_mb = section_err / num_mbs; | 961 const double err_per_mb = section_err / num_mbs; |
| 930 const double speed_term = 1.0 + 0.04 * oxcf->speed; | 962 const double speed_term = 1.0 + 0.04 * oxcf->speed; |
| 931 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << | 963 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << |
| 932 BPER_MB_NORMBITS) / num_mbs; | 964 BPER_MB_NORMBITS) / num_mbs; |
| 933 int q; | 965 int q; |
| 934 int is_svc_upper_layer = 0; | 966 int is_svc_upper_layer = 0; |
| 935 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1 && | 967 if (is_spatial_svc(cpi) && cpi->svc.spatial_layer_id > 0) |
| 936 cpi->svc.spatial_layer_id > 0) { | |
| 937 is_svc_upper_layer = 1; | 968 is_svc_upper_layer = 1; |
| 938 } | |
| 939 | 969 |
| 940 // Try and pick a max Q that will be high enough to encode the | 970 // Try and pick a max Q that will be high enough to encode the |
| 941 // content at the given rate. | 971 // content at the given rate. |
| 942 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 972 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 943 const double factor = | 973 const double factor = |
| 944 calc_correction_factor(err_per_mb, ERR_DIVISOR, | 974 calc_correction_factor(err_per_mb, ERR_DIVISOR, |
| 945 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : | 975 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
| 946 FACTOR_PT_LOW, FACTOR_PT_HIGH, q); | 976 FACTOR_PT_LOW, FACTOR_PT_HIGH, q); |
| 947 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, | 977 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 948 factor * speed_term); | 978 factor * speed_term); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 // and the GF buffer (which contains an older frame). | 1072 // and the GF buffer (which contains an older frame). |
| 1043 const double mb_sr_err_diff = (next_frame->sr_coded_error - | 1073 const double mb_sr_err_diff = (next_frame->sr_coded_error - |
| 1044 next_frame->coded_error) / cm->MBs; | 1074 next_frame->coded_error) / cm->MBs; |
| 1045 const double second_ref_decay = mb_sr_err_diff <= 512.0 | 1075 const double second_ref_decay = mb_sr_err_diff <= 512.0 |
| 1046 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) | 1076 ? fclamp(pow(1.0 - (mb_sr_err_diff / 512.0), 0.5), 0.85, 1.0) |
| 1047 : 0.85; | 1077 : 0.85; |
| 1048 | 1078 |
| 1049 return MIN(second_ref_decay, next_frame->pcnt_inter); | 1079 return MIN(second_ref_decay, next_frame->pcnt_inter); |
| 1050 } | 1080 } |
| 1051 | 1081 |
| 1082 // This function gives an estimate of how badly we believe the prediction |
| 1083 // quality is decaying from frame to frame. |
| 1084 static double get_zero_motion_factor(const VP9_COMMON *cm, |
| 1085 const FIRSTPASS_STATS *frame) { |
| 1086 const double sr_ratio = frame->coded_error / |
| 1087 DOUBLE_DIVIDE_CHECK(frame->sr_coded_error); |
| 1088 const double zero_motion_pct = frame->pcnt_inter - |
| 1089 frame->pcnt_motion; |
| 1090 |
| 1091 return MIN(sr_ratio, zero_motion_pct); |
| 1092 } |
| 1093 |
| 1094 |
| 1052 // Function to test for a condition where a complex transition is followed | 1095 // Function to test for a condition where a complex transition is followed |
| 1053 // by a static section. For example in slide shows where there is a fade | 1096 // by a static section. For example in slide shows where there is a fade |
| 1054 // between slides. This is to help with more optimal kf and gf positioning. | 1097 // between slides. This is to help with more optimal kf and gf positioning. |
| 1055 static int detect_transition_to_still(TWO_PASS *twopass, | 1098 static int detect_transition_to_still(TWO_PASS *twopass, |
| 1056 int frame_interval, int still_interval, | 1099 int frame_interval, int still_interval, |
| 1057 double loop_decay_rate, | 1100 double loop_decay_rate, |
| 1058 double last_decay_rate) { | 1101 double last_decay_rate) { |
| 1059 int trans_to_still = 0; | 1102 int trans_to_still = 0; |
| 1060 | 1103 |
| 1061 // Break clause to detect very still sections after motion | 1104 // Break clause to detect very still sections after motion |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1565 &abs_mv_in_out_accumulator, | 1608 &abs_mv_in_out_accumulator, |
| 1566 &mv_ratio_accumulator); | 1609 &mv_ratio_accumulator); |
| 1567 | 1610 |
| 1568 // Accumulate the effect of prediction quality decay. | 1611 // Accumulate the effect of prediction quality decay. |
| 1569 if (!flash_detected) { | 1612 if (!flash_detected) { |
| 1570 last_loop_decay_rate = loop_decay_rate; | 1613 last_loop_decay_rate = loop_decay_rate; |
| 1571 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); | 1614 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); |
| 1572 decay_accumulator = decay_accumulator * loop_decay_rate; | 1615 decay_accumulator = decay_accumulator * loop_decay_rate; |
| 1573 | 1616 |
| 1574 // Monitor for static sections. | 1617 // Monitor for static sections. |
| 1575 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1618 zero_motion_accumulator = |
| 1576 zero_motion_accumulator) { | 1619 MIN(zero_motion_accumulator, |
| 1577 zero_motion_accumulator = next_frame.pcnt_inter - | 1620 get_zero_motion_factor(&cpi->common, &next_frame)); |
| 1578 next_frame.pcnt_motion; | |
| 1579 } | |
| 1580 | 1621 |
| 1581 // Break clause to detect very still sections after motion. For example, | 1622 // Break clause to detect very still sections after motion. For example, |
| 1582 // a static image after a fade or other transition. | 1623 // a static image after a fade or other transition. |
| 1583 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, | 1624 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, |
| 1584 last_loop_decay_rate)) { | 1625 last_loop_decay_rate)) { |
| 1585 allow_alt_ref = 0; | 1626 allow_alt_ref = 0; |
| 1586 break; | 1627 break; |
| 1587 } | 1628 } |
| 1588 } | 1629 } |
| 1589 | 1630 |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1939 | 1980 |
| 1940 // Scan through the kf group collating various stats used to deteermine | 1981 // Scan through the kf group collating various stats used to deteermine |
| 1941 // how many bits to spend on it. | 1982 // how many bits to spend on it. |
| 1942 decay_accumulator = 1.0; | 1983 decay_accumulator = 1.0; |
| 1943 boost_score = 0.0; | 1984 boost_score = 0.0; |
| 1944 for (i = 0; i < rc->frames_to_key; ++i) { | 1985 for (i = 0; i < rc->frames_to_key; ++i) { |
| 1945 if (EOF == input_stats(twopass, &next_frame)) | 1986 if (EOF == input_stats(twopass, &next_frame)) |
| 1946 break; | 1987 break; |
| 1947 | 1988 |
| 1948 // Monitor for static sections. | 1989 // Monitor for static sections. |
| 1949 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) < | 1990 zero_motion_accumulator = |
| 1950 zero_motion_accumulator) { | 1991 MIN(zero_motion_accumulator, |
| 1951 zero_motion_accumulator = (next_frame.pcnt_inter - | 1992 get_zero_motion_factor(&cpi->common, &next_frame)); |
| 1952 next_frame.pcnt_motion); | |
| 1953 } | |
| 1954 | 1993 |
| 1955 // For the first few frames collect data to decide kf boost. | 1994 // For the first few frames collect data to decide kf boost. |
| 1956 if (i <= (rc->max_gf_interval * 2)) { | 1995 if (i <= (rc->max_gf_interval * 2)) { |
| 1957 double r; | 1996 double r; |
| 1958 if (next_frame.intra_error > twopass->kf_intra_err_min) | 1997 if (next_frame.intra_error > twopass->kf_intra_err_min) |
| 1959 r = (IIKFACTOR2 * next_frame.intra_error / | 1998 r = (IIKFACTOR2 * next_frame.intra_error / |
| 1960 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 1999 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| 1961 else | 2000 else |
| 1962 r = (IIKFACTOR2 * twopass->kf_intra_err_min / | 2001 r = (IIKFACTOR2 * twopass->kf_intra_err_min / |
| 1963 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); | 2002 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2058 cpi->refresh_alt_ref_frame = 0; | 2097 cpi->refresh_alt_ref_frame = 0; |
| 2059 cpi->rc.is_src_frame_alt_ref = 1; | 2098 cpi->rc.is_src_frame_alt_ref = 1; |
| 2060 break; | 2099 break; |
| 2061 case ARF_UPDATE: | 2100 case ARF_UPDATE: |
| 2062 cpi->refresh_last_frame = 0; | 2101 cpi->refresh_last_frame = 0; |
| 2063 cpi->refresh_golden_frame = 0; | 2102 cpi->refresh_golden_frame = 0; |
| 2064 cpi->refresh_alt_ref_frame = 1; | 2103 cpi->refresh_alt_ref_frame = 1; |
| 2065 break; | 2104 break; |
| 2066 default: | 2105 default: |
| 2067 assert(0); | 2106 assert(0); |
| 2107 break; |
| 2068 } | 2108 } |
| 2069 if (cpi->use_svc && cpi->svc.number_temporal_layers == 1) { | 2109 if (is_spatial_svc(cpi)) { |
| 2070 cpi->refresh_golden_frame = 0; | 2110 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0) |
| 2111 cpi->refresh_golden_frame = 0; |
| 2071 if (cpi->alt_ref_source == NULL) | 2112 if (cpi->alt_ref_source == NULL) |
| 2072 cpi->refresh_alt_ref_frame = 0; | 2113 cpi->refresh_alt_ref_frame = 0; |
| 2073 } | 2114 } |
| 2074 } | 2115 } |
| 2075 | 2116 |
| 2076 | 2117 |
| 2077 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { | 2118 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2078 VP9_COMMON *const cm = &cpi->common; | 2119 VP9_COMMON *const cm = &cpi->common; |
| 2079 RATE_CONTROL *const rc = &cpi->rc; | 2120 RATE_CONTROL *const rc = &cpi->rc; |
| 2080 TWO_PASS *const twopass = &cpi->twopass; | 2121 TWO_PASS *const twopass = &cpi->twopass; |
| 2081 int frames_left; | 2122 int frames_left; |
| 2082 FIRSTPASS_STATS this_frame; | 2123 FIRSTPASS_STATS this_frame; |
| 2083 FIRSTPASS_STATS this_frame_copy; | 2124 FIRSTPASS_STATS this_frame_copy; |
| 2084 | 2125 |
| 2085 int target_rate; | 2126 int target_rate; |
| 2086 LAYER_CONTEXT *lc = NULL; | 2127 LAYER_CONTEXT *lc = NULL; |
| 2087 const int is_spatial_svc = (cpi->use_svc && | 2128 |
| 2088 cpi->svc.number_temporal_layers == 1); | 2129 if (is_spatial_svc(cpi)) { |
| 2089 if (is_spatial_svc) { | |
| 2090 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; | 2130 lc = &cpi->svc.layer_context[cpi->svc.spatial_layer_id]; |
| 2091 frames_left = (int)(twopass->total_stats.count - | 2131 frames_left = (int)(twopass->total_stats.count - |
| 2092 lc->current_video_frame_in_layer); | 2132 lc->current_video_frame_in_layer); |
| 2093 } else { | 2133 } else { |
| 2094 frames_left = (int)(twopass->total_stats.count - | 2134 frames_left = (int)(twopass->total_stats.count - |
| 2095 cm->current_video_frame); | 2135 cm->current_video_frame); |
| 2096 } | 2136 } |
| 2097 | 2137 |
| 2098 if (!twopass->stats_in) | 2138 if (!twopass->stats_in) |
| 2099 return; | 2139 return; |
| 2100 | 2140 |
| 2101 // If this is an arf frame then we dont want to read the stats file or | 2141 // If this is an arf frame then we dont want to read the stats file or |
| 2102 // advance the input pointer as we already have what we need. | 2142 // advance the input pointer as we already have what we need. |
| 2103 if (twopass->gf_group.update_type[twopass->gf_group.index] == ARF_UPDATE) { | 2143 if (twopass->gf_group.update_type[twopass->gf_group.index] == ARF_UPDATE) { |
| 2104 int target_rate; | 2144 int target_rate; |
| 2105 configure_buffer_updates(cpi); | 2145 configure_buffer_updates(cpi); |
| 2106 target_rate = twopass->gf_group.bit_allocation[twopass->gf_group.index]; | 2146 target_rate = twopass->gf_group.bit_allocation[twopass->gf_group.index]; |
| 2107 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); | 2147 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2108 rc->base_frame_target = target_rate; | 2148 rc->base_frame_target = target_rate; |
| 2109 #ifdef LONG_TERM_VBR_CORRECTION | 2149 |
| 2110 // Correction to rate target based on prior over or under shoot. | 2150 // Correction to rate target based on prior over or under shoot. |
| 2111 if (cpi->oxcf.rc_mode == VPX_VBR) | 2151 if (cpi->oxcf.rc_mode == VPX_VBR) |
| 2112 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); | 2152 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); |
| 2113 #endif | 2153 |
| 2114 vp9_rc_set_frame_target(cpi, target_rate); | 2154 vp9_rc_set_frame_target(cpi, target_rate); |
| 2115 cm->frame_type = INTER_FRAME; | 2155 cm->frame_type = INTER_FRAME; |
| 2116 | 2156 |
| 2117 if (is_spatial_svc) { | 2157 if (is_spatial_svc(cpi)) { |
| 2118 if (cpi->svc.spatial_layer_id == 0) { | 2158 if (cpi->svc.spatial_layer_id == 0) { |
| 2119 lc->is_key_frame = 0; | 2159 lc->is_key_frame = 0; |
| 2120 } else { | 2160 } else { |
| 2121 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2161 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2122 | 2162 |
| 2123 if (lc->is_key_frame) | 2163 if (lc->is_key_frame) |
| 2124 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 2164 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2125 } | 2165 } |
| 2126 } | 2166 } |
| 2127 | 2167 |
| 2128 return; | 2168 return; |
| 2129 } | 2169 } |
| 2130 | 2170 |
| 2131 vp9_clear_system_state(); | 2171 vp9_clear_system_state(); |
| 2132 | 2172 |
| 2133 if (is_spatial_svc && twopass->kf_intra_err_min == 0) { | 2173 if (is_spatial_svc(cpi) && twopass->kf_intra_err_min == 0) { |
| 2134 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; | 2174 twopass->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; |
| 2135 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; | 2175 twopass->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; |
| 2136 } | 2176 } |
| 2137 | 2177 |
| 2138 if (cpi->oxcf.rc_mode == VPX_Q) { | 2178 if (cpi->oxcf.rc_mode == VPX_Q) { |
| 2139 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2179 twopass->active_worst_quality = cpi->oxcf.cq_level; |
| 2140 } else if (cm->current_video_frame == 0 || | 2180 } else if (cm->current_video_frame == 0 || |
| 2141 (is_spatial_svc && lc->current_video_frame_in_layer == 0)) { | 2181 (is_spatial_svc(cpi) && |
| 2182 lc->current_video_frame_in_layer == 0)) { |
| 2142 // Special case code for first frame. | 2183 // Special case code for first frame. |
| 2143 const int section_target_bandwidth = (int)(twopass->bits_left / | 2184 const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2144 frames_left); | 2185 frames_left); |
| 2145 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2186 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, |
| 2146 section_target_bandwidth); | 2187 section_target_bandwidth); |
| 2147 twopass->active_worst_quality = tmp_q; | 2188 twopass->active_worst_quality = tmp_q; |
| 2148 rc->ni_av_qi = tmp_q; | 2189 rc->ni_av_qi = tmp_q; |
| 2149 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); | 2190 rc->avg_q = vp9_convert_qindex_to_q(tmp_q); |
| 2150 } | 2191 } |
| 2151 vp9_zero(this_frame); | 2192 vp9_zero(this_frame); |
| 2152 if (EOF == input_stats(twopass, &this_frame)) | 2193 if (EOF == input_stats(twopass, &this_frame)) |
| 2153 return; | 2194 return; |
| 2154 | 2195 |
| 2155 // Local copy of the current frame's first pass stats. | 2196 // Local copy of the current frame's first pass stats. |
| 2156 this_frame_copy = this_frame; | 2197 this_frame_copy = this_frame; |
| 2157 | 2198 |
| 2158 // Keyframe and section processing. | 2199 // Keyframe and section processing. |
| 2159 if (rc->frames_to_key == 0 || | 2200 if (rc->frames_to_key == 0 || |
| 2160 (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2201 (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2161 // Define next KF group and assign bits to it. | 2202 // Define next KF group and assign bits to it. |
| 2162 find_next_key_frame(cpi, &this_frame_copy); | 2203 find_next_key_frame(cpi, &this_frame_copy); |
| 2163 } else { | 2204 } else { |
| 2164 cm->frame_type = INTER_FRAME; | 2205 cm->frame_type = INTER_FRAME; |
| 2165 } | 2206 } |
| 2166 | 2207 |
| 2167 if (is_spatial_svc) { | 2208 if (is_spatial_svc(cpi)) { |
| 2168 if (cpi->svc.spatial_layer_id == 0) { | 2209 if (cpi->svc.spatial_layer_id == 0) { |
| 2169 lc->is_key_frame = (cm->frame_type == KEY_FRAME); | 2210 lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
| 2211 if (lc->is_key_frame) |
| 2212 cpi->ref_frame_flags &= |
| 2213 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2170 } else { | 2214 } else { |
| 2171 cm->frame_type = INTER_FRAME; | 2215 cm->frame_type = INTER_FRAME; |
| 2172 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2216 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2173 | 2217 |
| 2174 if (lc->is_key_frame) { | 2218 if (lc->is_key_frame) { |
| 2175 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 2219 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2176 } | 2220 } |
| 2177 } | 2221 } |
| 2178 } | 2222 } |
| 2179 | 2223 |
| 2180 // Define a new GF/ARF group. (Should always enter here for key frames). | 2224 // Define a new GF/ARF group. (Should always enter here for key frames). |
| 2181 if (rc->frames_till_gf_update_due == 0) { | 2225 if (rc->frames_till_gf_update_due == 0) { |
| 2182 define_gf_group(cpi, &this_frame_copy); | 2226 define_gf_group(cpi, &this_frame_copy); |
| 2183 | 2227 |
| 2184 if (twopass->gf_zeromotion_pct > 995) { | 2228 if (twopass->gf_zeromotion_pct > 995) { |
| 2185 // As long as max_thresh for encode breakout is small enough, it is ok | 2229 // As long as max_thresh for encode breakout is small enough, it is ok |
| 2186 // to enable it for show frame, i.e. set allow_encode_breakout to | 2230 // to enable it for show frame, i.e. set allow_encode_breakout to |
| 2187 // ENCODE_BREAKOUT_LIMITED. | 2231 // ENCODE_BREAKOUT_LIMITED. |
| 2188 if (!cm->show_frame) | 2232 if (!cm->show_frame) |
| 2189 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; | 2233 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; |
| 2190 else | 2234 else |
| 2191 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; | 2235 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; |
| 2192 } | 2236 } |
| 2193 | 2237 |
| 2194 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2238 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 2195 if (!is_spatial_svc) | 2239 if (!is_spatial_svc(cpi)) |
| 2196 cpi->refresh_golden_frame = 1; | 2240 cpi->refresh_golden_frame = 1; |
| 2197 } | 2241 } |
| 2198 | 2242 |
| 2199 configure_buffer_updates(cpi); | 2243 configure_buffer_updates(cpi); |
| 2200 | 2244 |
| 2201 target_rate = twopass->gf_group.bit_allocation[twopass->gf_group.index]; | 2245 target_rate = twopass->gf_group.bit_allocation[twopass->gf_group.index]; |
| 2202 if (cpi->common.frame_type == KEY_FRAME) | 2246 if (cpi->common.frame_type == KEY_FRAME) |
| 2203 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); | 2247 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); |
| 2204 else | 2248 else |
| 2205 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); | 2249 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2206 | 2250 |
| 2207 rc->base_frame_target = target_rate; | 2251 rc->base_frame_target = target_rate; |
| 2208 #ifdef LONG_TERM_VBR_CORRECTION | 2252 |
| 2209 // Correction to rate target based on prior over or under shoot. | 2253 // Correction to rate target based on prior over or under shoot. |
| 2210 if (cpi->oxcf.rc_mode == VPX_VBR) | 2254 if (cpi->oxcf.rc_mode == VPX_VBR) |
| 2211 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); | 2255 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); |
| 2212 #endif | 2256 |
| 2213 vp9_rc_set_frame_target(cpi, target_rate); | 2257 vp9_rc_set_frame_target(cpi, target_rate); |
| 2214 | 2258 |
| 2215 // Update the total stats remaining structure. | 2259 // Update the total stats remaining structure. |
| 2216 subtract_stats(&twopass->total_left_stats, &this_frame); | 2260 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2217 } | 2261 } |
| 2218 | 2262 |
| 2219 void vp9_twopass_postencode_update(VP9_COMP *cpi) { | 2263 void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
| 2220 TWO_PASS *const twopass = &cpi->twopass; | 2264 TWO_PASS *const twopass = &cpi->twopass; |
| 2221 RATE_CONTROL *const rc = &cpi->rc; | 2265 RATE_CONTROL *const rc = &cpi->rc; |
| 2222 #ifdef LONG_TERM_VBR_CORRECTION | 2266 |
| 2223 // In this experimental mode, the VBR correction is done exclusively through | 2267 // VBR correction is done through rc->vbr_bits_off_target. Based on the |
| 2224 // rc->vbr_bits_off_target. Based on the sign of this value, a limited % | 2268 // sign of this value, a limited % adjustment is made to the target rate |
| 2225 // adjustment is made to the target rate of subsequent frames, to try and | 2269 // of subsequent frames, to try and push it back towards 0. This method |
| 2226 // push it back towards 0. This mode is less likely to suffer from | 2270 // is designed to prevent extreme behaviour at the end of a clip |
| 2227 // extreme behaviour at the end of a clip or group of frames. | 2271 // or group of frames. |
| 2228 const int bits_used = rc->base_frame_target; | 2272 const int bits_used = rc->base_frame_target; |
| 2229 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; | 2273 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; |
| 2230 #else | |
| 2231 // In this mode, VBR correction is acheived by altering bits_left, | |
| 2232 // kf_group_bits & gf_group_bits to reflect any deviation from the target | |
| 2233 // rate in this frame. This alters the allocation of bits to the | |
| 2234 // remaning frames in the group / clip. | |
| 2235 // | |
| 2236 // This method can give rise to unstable behaviour near the end of a clip | |
| 2237 // or kf/gf group of frames where any accumulated error is corrected over an | |
| 2238 // ever decreasing number of frames. Hence we change the balance of target | |
| 2239 // vs. actual bitrate gradually as we progress towards the end of the | |
| 2240 // sequence in order to mitigate this effect. | |
| 2241 const double progress = | |
| 2242 (double)(twopass->stats_in - twopass->stats_in_start) / | |
| 2243 (twopass->stats_in_end - twopass->stats_in_start); | |
| 2244 const int bits_used = (int)(progress * rc->this_frame_target + | |
| 2245 (1.0 - progress) * rc->projected_frame_size); | |
| 2246 #endif | |
| 2247 | 2274 |
| 2248 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); | 2275 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); |
| 2249 | 2276 |
| 2250 #ifdef LONG_TERM_VBR_CORRECTION | |
| 2251 if (cpi->common.frame_type != KEY_FRAME && | 2277 if (cpi->common.frame_type != KEY_FRAME && |
| 2252 !vp9_is_upper_layer_key_frame(cpi)) { | 2278 !vp9_is_upper_layer_key_frame(cpi)) { |
| 2253 #else | |
| 2254 if (cpi->common.frame_type == KEY_FRAME || | |
| 2255 vp9_is_upper_layer_key_frame(cpi)) { | |
| 2256 // For key frames kf_group_bits already had the target bits subtracted out. | |
| 2257 // So now update to the correct value based on the actual bits used. | |
| 2258 twopass->kf_group_bits += rc->this_frame_target - bits_used; | |
| 2259 } else { | |
| 2260 #endif | |
| 2261 twopass->kf_group_bits -= bits_used; | 2279 twopass->kf_group_bits -= bits_used; |
| 2262 } | 2280 } |
| 2263 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); | 2281 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); |
| 2264 | 2282 |
| 2265 // Increment the gf group index ready for the next frame. | 2283 // Increment the gf group index ready for the next frame. |
| 2266 ++twopass->gf_group.index; | 2284 ++twopass->gf_group.index; |
| 2267 } | 2285 } |
| OLD | NEW |