| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 #define INTRA_MODE_PENALTY 1024 | 50 #define INTRA_MODE_PENALTY 1024 |
| 51 #define KF_MAX_BOOST 128.0 | 51 #define KF_MAX_BOOST 128.0 |
| 52 #define MIN_ARF_GF_BOOST 240 | 52 #define MIN_ARF_GF_BOOST 240 |
| 53 #define MIN_DECAY_FACTOR 0.01 | 53 #define MIN_DECAY_FACTOR 0.01 |
| 54 #define MIN_GF_INTERVAL 4 | 54 #define MIN_GF_INTERVAL 4 |
| 55 #define MIN_KF_BOOST 300 | 55 #define MIN_KF_BOOST 300 |
| 56 #define NEW_MV_MODE_PENALTY 32 | 56 #define NEW_MV_MODE_PENALTY 32 |
| 57 #define SVC_FACTOR_PT_LOW 0.45 | 57 #define SVC_FACTOR_PT_LOW 0.45 |
| 58 #define DARK_THRESH 64 | 58 #define DARK_THRESH 64 |
| 59 #define DEFAULT_GRP_WEIGHT 1.0 | 59 #define DEFAULT_GRP_WEIGHT 1.0 |
| 60 #define RC_FACTOR_MIN 0.75 |
| 61 #define RC_FACTOR_MAX 1.75 |
| 60 | 62 |
| 61 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 63 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 62 | 64 |
| 63 #if ARF_STATS_OUTPUT | 65 #if ARF_STATS_OUTPUT |
| 64 unsigned int arf_count = 0; | 66 unsigned int arf_count = 0; |
| 65 #endif | 67 #endif |
| 66 | 68 |
| 67 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | |
| 68 YV12_BUFFER_CONFIG temp = *a; | |
| 69 *a = *b; | |
| 70 *b = temp; | |
| 71 } | |
| 72 | |
| 73 // Resets the first pass file to the given position using a relative seek from | 69 // Resets the first pass file to the given position using a relative seek from |
| 74 // the current position. | 70 // the current position. |
| 75 static void reset_fpf_position(TWO_PASS *p, | 71 static void reset_fpf_position(TWO_PASS *p, |
| 76 const FIRSTPASS_STATS *position) { | 72 const FIRSTPASS_STATS *position) { |
| 77 p->stats_in = position; | 73 p->stats_in = position; |
| 78 } | 74 } |
| 79 | 75 |
| 80 // Read frame stats at an offset from the current position. | 76 // Read frame stats at an offset from the current position. |
| 81 static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) { | 77 static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) { |
| 82 if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) || | 78 if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) || |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 MACROBLOCK *const x = &cpi->td.mb; | 452 MACROBLOCK *const x = &cpi->td.mb; |
| 457 VP9_COMMON *const cm = &cpi->common; | 453 VP9_COMMON *const cm = &cpi->common; |
| 458 MACROBLOCKD *const xd = &x->e_mbd; | 454 MACROBLOCKD *const xd = &x->e_mbd; |
| 459 TileInfo tile; | 455 TileInfo tile; |
| 460 struct macroblock_plane *const p = x->plane; | 456 struct macroblock_plane *const p = x->plane; |
| 461 struct macroblockd_plane *const pd = xd->plane; | 457 struct macroblockd_plane *const pd = xd->plane; |
| 462 const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none; | 458 const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none; |
| 463 int i; | 459 int i; |
| 464 | 460 |
| 465 int recon_yoffset, recon_uvoffset; | 461 int recon_yoffset, recon_uvoffset; |
| 466 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); | |
| 467 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); | |
| 468 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); | |
| 469 int recon_y_stride = lst_yv12->y_stride; | |
| 470 int recon_uv_stride = lst_yv12->uv_stride; | |
| 471 int uv_mb_height = 16 >> (lst_yv12->y_height > lst_yv12->uv_height); | |
| 472 int64_t intra_error = 0; | 462 int64_t intra_error = 0; |
| 473 int64_t coded_error = 0; | 463 int64_t coded_error = 0; |
| 474 int64_t sr_coded_error = 0; | 464 int64_t sr_coded_error = 0; |
| 475 | 465 |
| 476 int sum_mvr = 0, sum_mvc = 0; | 466 int sum_mvr = 0, sum_mvc = 0; |
| 477 int sum_mvr_abs = 0, sum_mvc_abs = 0; | 467 int sum_mvr_abs = 0, sum_mvc_abs = 0; |
| 478 int64_t sum_mvrs = 0, sum_mvcs = 0; | 468 int64_t sum_mvrs = 0, sum_mvcs = 0; |
| 479 int mvcount = 0; | 469 int mvcount = 0; |
| 480 int intercount = 0; | 470 int intercount = 0; |
| 481 int second_ref_count = 0; | 471 int second_ref_count = 0; |
| 482 const int intrapenalty = INTRA_MODE_PENALTY; | 472 const int intrapenalty = INTRA_MODE_PENALTY; |
| 483 int neutral_count = 0; | 473 int neutral_count = 0; |
| 484 int new_mv_count = 0; | 474 int new_mv_count = 0; |
| 485 int sum_in_vectors = 0; | 475 int sum_in_vectors = 0; |
| 486 MV lastmv = {0, 0}; | 476 MV lastmv = {0, 0}; |
| 487 TWO_PASS *twopass = &cpi->twopass; | 477 TWO_PASS *twopass = &cpi->twopass; |
| 488 const MV zero_mv = {0, 0}; | 478 const MV zero_mv = {0, 0}; |
| 479 int recon_y_stride, recon_uv_stride, uv_mb_height; |
| 480 |
| 481 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 482 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 483 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
| 489 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; | 484 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; |
| 485 |
| 490 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? | 486 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? |
| 491 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL; | 487 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL; |
| 492 double intra_factor; | 488 double intra_factor; |
| 493 double brightness_factor; | 489 double brightness_factor; |
| 490 BufferPool *const pool = cm->buffer_pool; |
| 491 |
| 492 // First pass code requires valid last and new frame buffers. |
| 493 assert(new_yv12 != NULL); |
| 494 assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL)); |
| 494 | 495 |
| 495 #if CONFIG_FP_MB_STATS | 496 #if CONFIG_FP_MB_STATS |
| 496 if (cpi->use_fp_mb_stats) { | 497 if (cpi->use_fp_mb_stats) { |
| 497 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs); | 498 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs); |
| 498 } | 499 } |
| 499 #endif | 500 #endif |
| 500 | 501 |
| 501 vp9_clear_system_state(); | 502 vp9_clear_system_state(); |
| 502 | 503 |
| 503 intra_factor = 0.0; | 504 intra_factor = 0.0; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 528 vp9_scale_references(cpi); | 529 vp9_scale_references(cpi); |
| 529 | 530 |
| 530 // Use either last frame or alt frame for motion search. | 531 // Use either last frame or alt frame for motion search. |
| 531 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { | 532 if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
| 532 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); | 533 first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| 533 if (first_ref_buf == NULL) | 534 if (first_ref_buf == NULL) |
| 534 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); | 535 first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 535 } | 536 } |
| 536 | 537 |
| 537 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { | 538 if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { |
| 538 BufferPool *const pool = cm->buffer_pool; | 539 gld_yv12 = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME); |
| 539 const int ref_idx = | 540 if (gld_yv12 == NULL) { |
| 540 cm->ref_frame_map[get_ref_frame_idx(cpi, GOLDEN_FRAME)]; | 541 gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 541 const int scaled_idx = cpi->scaled_ref_idx[GOLDEN_FRAME - 1]; | 542 } |
| 542 | |
| 543 gld_yv12 = (scaled_idx != ref_idx) ? &pool->frame_bufs[scaled_idx].buf : | |
| 544 get_ref_frame_buffer(cpi, GOLDEN_FRAME); | |
| 545 } else { | 543 } else { |
| 546 gld_yv12 = NULL; | 544 gld_yv12 = NULL; |
| 547 } | 545 } |
| 548 | 546 |
| 549 recon_y_stride = new_yv12->y_stride; | |
| 550 recon_uv_stride = new_yv12->uv_stride; | |
| 551 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); | |
| 552 | |
| 553 set_ref_ptrs(cm, xd, | 547 set_ref_ptrs(cm, xd, |
| 554 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE, | 548 (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE, |
| 555 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE); | 549 (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE); |
| 556 | 550 |
| 557 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source, | 551 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source, |
| 558 &cpi->scaled_source); | 552 &cpi->scaled_source); |
| 559 } | 553 } |
| 560 | 554 |
| 561 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); | 555 vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
| 562 | 556 |
| 563 vp9_setup_src_planes(x, cpi->Source, 0, 0); | 557 vp9_setup_src_planes(x, cpi->Source, 0, 0); |
| 564 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL); | |
| 565 vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0); | 558 vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0); |
| 566 | 559 |
| 560 if (!frame_is_intra_only(cm)) { |
| 561 vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL); |
| 562 } |
| 563 |
| 567 xd->mi = cm->mi; | 564 xd->mi = cm->mi; |
| 568 xd->mi[0].src_mi = &xd->mi[0]; | 565 xd->mi[0].src_mi = &xd->mi[0]; |
| 569 | 566 |
| 570 vp9_frame_init_quantizer(cpi); | 567 vp9_frame_init_quantizer(cpi); |
| 571 | 568 |
| 572 for (i = 0; i < MAX_MB_PLANE; ++i) { | 569 for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 573 p[i].coeff = ctx->coeff_pbuf[i][1]; | 570 p[i].coeff = ctx->coeff_pbuf[i][1]; |
| 574 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; | 571 p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; |
| 575 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; | 572 pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; |
| 576 p[i].eobs = ctx->eobs_pbuf[i][1]; | 573 p[i].eobs = ctx->eobs_pbuf[i][1]; |
| 577 } | 574 } |
| 578 x->skip_recode = 0; | 575 x->skip_recode = 0; |
| 579 | 576 |
| 580 vp9_init_mv_probs(cm); | 577 vp9_init_mv_probs(cm); |
| 581 vp9_initialize_rd_consts(cpi); | 578 vp9_initialize_rd_consts(cpi); |
| 582 | 579 |
| 583 // Tiling is ignored in the first pass. | 580 // Tiling is ignored in the first pass. |
| 584 vp9_tile_init(&tile, cm, 0, 0); | 581 vp9_tile_init(&tile, cm, 0, 0); |
| 585 | 582 |
| 583 recon_y_stride = new_yv12->y_stride; |
| 584 recon_uv_stride = new_yv12->uv_stride; |
| 585 uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); |
| 586 |
| 586 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { | 587 for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
| 587 MV best_ref_mv = {0, 0}; | 588 MV best_ref_mv = {0, 0}; |
| 588 | 589 |
| 589 // Reset above block coeffs. | 590 // Reset above block coeffs. |
| 590 xd->up_available = (mb_row != 0); | 591 xd->up_available = (mb_row != 0); |
| 591 recon_yoffset = (mb_row * recon_y_stride * 16); | 592 recon_yoffset = (mb_row * recon_y_stride * 16); |
| 592 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); | 593 recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); |
| 593 | 594 |
| 594 // Set up limit values for motion vectors to prevent them extending | 595 // Set up limit values for motion vectors to prevent them extending |
| 595 // outside the UMV borders. | 596 // outside the UMV borders. |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1011 } | 1012 } |
| 1012 | 1013 |
| 1013 // Copy the previous Last Frame back into gf and and arf buffers if | 1014 // Copy the previous Last Frame back into gf and and arf buffers if |
| 1014 // the prediction is good enough... but also don't allow it to lag too far. | 1015 // the prediction is good enough... but also don't allow it to lag too far. |
| 1015 if ((twopass->sr_update_lag > 3) || | 1016 if ((twopass->sr_update_lag > 3) || |
| 1016 ((cm->current_video_frame > 0) && | 1017 ((cm->current_video_frame > 0) && |
| 1017 (twopass->this_frame_stats.pcnt_inter > 0.20) && | 1018 (twopass->this_frame_stats.pcnt_inter > 0.20) && |
| 1018 ((twopass->this_frame_stats.intra_error / | 1019 ((twopass->this_frame_stats.intra_error / |
| 1019 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { | 1020 DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { |
| 1020 if (gld_yv12 != NULL) { | 1021 if (gld_yv12 != NULL) { |
| 1021 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 1022 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx], |
| 1023 cm->ref_frame_map[cpi->lst_fb_idx]); |
| 1022 } | 1024 } |
| 1023 twopass->sr_update_lag = 1; | 1025 twopass->sr_update_lag = 1; |
| 1024 } else { | 1026 } else { |
| 1025 ++twopass->sr_update_lag; | 1027 ++twopass->sr_update_lag; |
| 1026 } | 1028 } |
| 1027 | 1029 |
| 1028 vp9_extend_frame_borders(new_yv12); | 1030 vp9_extend_frame_borders(new_yv12); |
| 1029 | 1031 |
| 1030 if (lc != NULL) { | 1032 if (lc != NULL) { |
| 1031 vp9_update_reference_frames(cpi); | 1033 vp9_update_reference_frames(cpi); |
| 1032 } else { | 1034 } else { |
| 1033 // Swap frame pointers so last frame refers to the frame we just compressed. | 1035 // The frame we just compressed now becomes the last frame. |
| 1034 swap_yv12(lst_yv12, new_yv12); | 1036 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx], |
| 1037 cm->new_fb_idx); |
| 1035 } | 1038 } |
| 1036 | 1039 |
| 1037 // Special case for the first frame. Copy into the GF buffer as a second | 1040 // Special case for the first frame. Copy into the GF buffer as a second |
| 1038 // reference. | 1041 // reference. |
| 1039 if (cm->current_video_frame == 0 && gld_yv12 != NULL && lc == NULL) { | 1042 if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX && |
| 1040 vp8_yv12_copy_frame(lst_yv12, gld_yv12); | 1043 lc == NULL) { |
| 1044 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx], |
| 1045 cm->ref_frame_map[cpi->lst_fb_idx]); |
| 1041 } | 1046 } |
| 1042 | 1047 |
| 1043 // Use this to see what the first pass reconstruction looks like. | 1048 // Use this to see what the first pass reconstruction looks like. |
| 1044 if (0) { | 1049 if (0) { |
| 1045 char filename[512]; | 1050 char filename[512]; |
| 1046 FILE *recon_file; | 1051 FILE *recon_file; |
| 1047 snprintf(filename, sizeof(filename), "enc%04d.yuv", | 1052 snprintf(filename, sizeof(filename), "enc%04d.yuv", |
| 1048 (int)cm->current_video_frame); | 1053 (int)cm->current_video_frame); |
| 1049 | 1054 |
| 1050 if (cm->current_video_frame == 0) | 1055 if (cm->current_video_frame == 0) |
| (...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1953 #if GROUP_ADAPTIVE_MAXQ | 1958 #if GROUP_ADAPTIVE_MAXQ |
| 1954 // Calculate an estimate of the maxq needed for the group. | 1959 // Calculate an estimate of the maxq needed for the group. |
| 1955 // We are more agressive about correcting for sections | 1960 // We are more agressive about correcting for sections |
| 1956 // where there could be significant overshoot than for easier | 1961 // where there could be significant overshoot than for easier |
| 1957 // sections where we do not wish to risk creating an overshoot | 1962 // sections where we do not wish to risk creating an overshoot |
| 1958 // of the allocated bit budget. | 1963 // of the allocated bit budget. |
| 1959 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { | 1964 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { |
| 1960 const int vbr_group_bits_per_frame = | 1965 const int vbr_group_bits_per_frame = |
| 1961 (int)(gf_group_bits / rc->baseline_gf_interval); | 1966 (int)(gf_group_bits / rc->baseline_gf_interval); |
| 1962 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; | 1967 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; |
| 1963 const int tmp_q = | 1968 int tmp_q; |
| 1969 // rc factor is a weight factor that corrects for local rate control drift. |
| 1970 double rc_factor = 1.0; |
| 1971 if (rc->rate_error_estimate > 0) { |
| 1972 rc_factor = MAX(RC_FACTOR_MIN, |
| 1973 (double)(100 - rc->rate_error_estimate) / 100.0); |
| 1974 } else { |
| 1975 rc_factor = MIN(RC_FACTOR_MAX, |
| 1976 (double)(100 - rc->rate_error_estimate) / 100.0); |
| 1977 } |
| 1978 tmp_q = |
| 1964 get_twopass_worst_quality(cpi, group_av_err, vbr_group_bits_per_frame, | 1979 get_twopass_worst_quality(cpi, group_av_err, vbr_group_bits_per_frame, |
| 1965 twopass->kfgroup_inter_fraction); | 1980 twopass->kfgroup_inter_fraction * rc_factor); |
| 1966 | 1981 twopass->active_worst_quality = |
| 1967 if (tmp_q < twopass->baseline_active_worst_quality) { | 1982 MAX(tmp_q, twopass->active_worst_quality >> 1); |
| 1968 twopass->active_worst_quality = | |
| 1969 (tmp_q + twopass->baseline_active_worst_quality + 1) / 2; | |
| 1970 } else { | |
| 1971 twopass->active_worst_quality = tmp_q; | |
| 1972 } | |
| 1973 } | 1983 } |
| 1974 #endif | 1984 #endif |
| 1975 | 1985 |
| 1976 // Calculate the extra bits to be used for boosted frame(s) | 1986 // Calculate the extra bits to be used for boosted frame(s) |
| 1977 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, | 1987 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 1978 rc->gfu_boost, gf_group_bits); | 1988 rc->gfu_boost, gf_group_bits); |
| 1979 | 1989 |
| 1980 // Adjust KF group bits and error remaining. | 1990 // Adjust KF group bits and error remaining. |
| 1981 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1991 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 1982 | 1992 |
| (...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2570 // The multiplication by 256 reverses a scaling factor of (>> 8) | 2580 // The multiplication by 256 reverses a scaling factor of (>> 8) |
| 2571 // applied when combining MB error values for the frame. | 2581 // applied when combining MB error values for the frame. |
| 2572 twopass->mb_av_energy = | 2582 twopass->mb_av_energy = |
| 2573 log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0); | 2583 log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0); |
| 2574 } | 2584 } |
| 2575 | 2585 |
| 2576 // Update the total stats remaining structure. | 2586 // Update the total stats remaining structure. |
| 2577 subtract_stats(&twopass->total_left_stats, &this_frame); | 2587 subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2578 } | 2588 } |
| 2579 | 2589 |
| 2580 #define MINQ_ADJ_LIMIT 32 | 2590 #define MINQ_ADJ_LIMIT 48 |
| 2581 void vp9_twopass_postencode_update(VP9_COMP *cpi) { | 2591 void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
| 2582 TWO_PASS *const twopass = &cpi->twopass; | 2592 TWO_PASS *const twopass = &cpi->twopass; |
| 2583 RATE_CONTROL *const rc = &cpi->rc; | 2593 RATE_CONTROL *const rc = &cpi->rc; |
| 2584 const int bits_used = rc->base_frame_target; | 2594 const int bits_used = rc->base_frame_target; |
| 2585 | 2595 |
| 2586 // VBR correction is done through rc->vbr_bits_off_target. Based on the | 2596 // VBR correction is done through rc->vbr_bits_off_target. Based on the |
| 2587 // sign of this value, a limited % adjustment is made to the target rate | 2597 // sign of this value, a limited % adjustment is made to the target rate |
| 2588 // of subsequent frames, to try and push it back towards 0. This method | 2598 // of subsequent frames, to try and push it back towards 0. This method |
| 2589 // is designed to prevent extreme behaviour at the end of a clip | 2599 // is designed to prevent extreme behaviour at the end of a clip |
| 2590 // or group of frames. | 2600 // or group of frames. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 2603 if (cpi->common.frame_type != KEY_FRAME && | 2613 if (cpi->common.frame_type != KEY_FRAME && |
| 2604 !vp9_is_upper_layer_key_frame(cpi)) { | 2614 !vp9_is_upper_layer_key_frame(cpi)) { |
| 2605 twopass->kf_group_bits -= bits_used; | 2615 twopass->kf_group_bits -= bits_used; |
| 2606 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; | 2616 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; |
| 2607 } | 2617 } |
| 2608 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); | 2618 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); |
| 2609 | 2619 |
| 2610 // Increment the gf group index ready for the next frame. | 2620 // Increment the gf group index ready for the next frame. |
| 2611 ++twopass->gf_group.index; | 2621 ++twopass->gf_group.index; |
| 2612 | 2622 |
| 2613 // If the rate control is drifting consider adjustment ot min or maxq. | 2623 // If the rate control is drifting consider adjustment to min or maxq. |
| 2614 // Only make adjustments on gf/arf | |
| 2615 if ((cpi->oxcf.rc_mode == VPX_VBR) && | 2624 if ((cpi->oxcf.rc_mode == VPX_VBR) && |
| 2616 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && | 2625 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && |
| 2617 !cpi->rc.is_src_frame_alt_ref) { | 2626 !cpi->rc.is_src_frame_alt_ref) { |
| 2618 const int maxq_adj_limit = | 2627 const int maxq_adj_limit = |
| 2619 rc->worst_quality - twopass->active_worst_quality; | 2628 rc->worst_quality - twopass->active_worst_quality; |
| 2620 | 2629 |
| 2621 // Undershoot. | 2630 // Undershoot. |
| 2622 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) { | 2631 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) { |
| 2623 --twopass->extend_maxq; | 2632 --twopass->extend_maxq; |
| 2624 if (rc->rolling_target_bits >= rc->rolling_actual_bits) | 2633 if (rc->rolling_target_bits >= rc->rolling_actual_bits) |
| 2625 ++twopass->extend_minq; | 2634 ++twopass->extend_minq; |
| 2626 // Overshoot. | 2635 // Overshoot. |
| 2627 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) { | 2636 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) { |
| 2628 --twopass->extend_minq; | 2637 --twopass->extend_minq; |
| 2629 if (rc->rolling_target_bits < rc->rolling_actual_bits) | 2638 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2630 ++twopass->extend_maxq; | 2639 ++twopass->extend_maxq; |
| 2631 } else { | 2640 } else { |
| 2632 // Adjustment for extreme local overshoot. | 2641 // Adjustment for extreme local overshoot. |
| 2633 if (rc->projected_frame_size > (2 * rc->base_frame_target) && | 2642 if (rc->projected_frame_size > (2 * rc->base_frame_target) && |
| 2634 rc->projected_frame_size > (2 * rc->avg_frame_bandwidth)) | 2643 rc->projected_frame_size > (2 * rc->avg_frame_bandwidth)) |
| 2635 ++twopass->extend_maxq; | 2644 ++twopass->extend_maxq; |
| 2636 | 2645 |
| 2637 // Unwind undershoot or overshoot adjustment. | 2646 // Unwind undershoot or overshoot adjustment. |
| 2638 if (rc->rolling_target_bits < rc->rolling_actual_bits) | 2647 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2639 --twopass->extend_minq; | 2648 --twopass->extend_minq; |
| 2640 else if (rc->rolling_target_bits > rc->rolling_actual_bits) | 2649 else if (rc->rolling_target_bits > rc->rolling_actual_bits) |
| 2641 --twopass->extend_maxq; | 2650 --twopass->extend_maxq; |
| 2642 } | 2651 } |
| 2652 |
| 2643 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); | 2653 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); |
| 2644 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); | 2654 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
| 2645 } | 2655 } |
| 2646 } | 2656 } |
| OLD | NEW |