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 20 matching lines...) Expand all Loading... |
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 #define ARF_STATS_OUTPUT 0 |
40 | 40 |
| 41 #define BOOST_BREAKOUT 12.5 |
41 #define BOOST_FACTOR 12.5 | 42 #define BOOST_FACTOR 12.5 |
42 #define ERR_DIVISOR 100.0 | 43 #define ERR_DIVISOR 128.0 |
43 #define FACTOR_PT_LOW 0.5 | 44 #define FACTOR_PT_LOW 0.70 |
44 #define FACTOR_PT_HIGH 0.9 | 45 #define FACTOR_PT_HIGH 0.90 |
45 #define FIRST_PASS_Q 10.0 | 46 #define FIRST_PASS_Q 10.0 |
46 #define GF_MAX_BOOST 96.0 | 47 #define GF_MAX_BOOST 96.0 |
47 #define INTRA_MODE_PENALTY 1024 | 48 #define INTRA_MODE_PENALTY 1024 |
48 #define KF_MAX_BOOST 128.0 | 49 #define KF_MAX_BOOST 128.0 |
| 50 #define MIN_ARF_GF_BOOST 240 |
49 #define MIN_DECAY_FACTOR 0.01 | 51 #define MIN_DECAY_FACTOR 0.01 |
50 #define MIN_GF_INTERVAL 4 | 52 #define MIN_GF_INTERVAL 4 |
51 #define MIN_KF_BOOST 300 | 53 #define MIN_KF_BOOST 300 |
52 #define NEW_MV_MODE_PENALTY 32 | 54 #define NEW_MV_MODE_PENALTY 32 |
53 #define SVC_FACTOR_PT_LOW 0.45 | 55 #define SVC_FACTOR_PT_LOW 0.45 |
54 | 56 |
55 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) | 57 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
56 | 58 |
57 #if ARF_STATS_OUTPUT | 59 #if ARF_STATS_OUTPUT |
58 unsigned int arf_count = 0; | 60 unsigned int arf_count = 0; |
59 #endif | 61 #endif |
60 | 62 |
61 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { | 63 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { |
62 YV12_BUFFER_CONFIG temp = *a; | 64 YV12_BUFFER_CONFIG temp = *a; |
63 *a = *b; | 65 *a = *b; |
64 *b = temp; | 66 *b = temp; |
65 } | 67 } |
66 | 68 |
67 static int gfboost_qadjust(int qindex, vpx_bit_depth_t bit_depth) { | |
68 const double q = vp9_convert_qindex_to_q(qindex, bit_depth); | |
69 return (int)((0.00000828 * q * q * q) + | |
70 (-0.0055 * q * q) + | |
71 (1.32 * q) + 79.3); | |
72 } | |
73 | |
74 // 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 |
75 // the current position. | 70 // the current position. |
76 static void reset_fpf_position(TWO_PASS *p, | 71 static void reset_fpf_position(TWO_PASS *p, |
77 const FIRSTPASS_STATS *position) { | 72 const FIRSTPASS_STATS *position) { |
78 p->stats_in = position; | 73 p->stats_in = position; |
79 } | 74 } |
80 | 75 |
81 // Read frame stats at an offset from the current position. | 76 // Read frame stats at an offset from the current position. |
82 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) { |
83 if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) || | 78 if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) || |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 269 |
275 static unsigned int get_prediction_error(BLOCK_SIZE bsize, | 270 static unsigned int get_prediction_error(BLOCK_SIZE bsize, |
276 const struct buf_2d *src, | 271 const struct buf_2d *src, |
277 const struct buf_2d *ref) { | 272 const struct buf_2d *ref) { |
278 unsigned int sse; | 273 unsigned int sse; |
279 const vp9_variance_fn_t fn = get_block_variance_fn(bsize); | 274 const vp9_variance_fn_t fn = get_block_variance_fn(bsize); |
280 fn(src->buf, src->stride, ref->buf, ref->stride, &sse); | 275 fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
281 return sse; | 276 return sse; |
282 } | 277 } |
283 | 278 |
| 279 #if CONFIG_VP9_HIGHBITDEPTH |
| 280 static vp9_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize, |
| 281 int bd) { |
| 282 switch (bd) { |
| 283 default: |
| 284 switch (bsize) { |
| 285 case BLOCK_8X8: |
| 286 return vp9_highbd_mse8x8; |
| 287 case BLOCK_16X8: |
| 288 return vp9_highbd_mse16x8; |
| 289 case BLOCK_8X16: |
| 290 return vp9_highbd_mse8x16; |
| 291 default: |
| 292 return vp9_highbd_mse16x16; |
| 293 } |
| 294 break; |
| 295 case 10: |
| 296 switch (bsize) { |
| 297 case BLOCK_8X8: |
| 298 return vp9_highbd_10_mse8x8; |
| 299 case BLOCK_16X8: |
| 300 return vp9_highbd_10_mse16x8; |
| 301 case BLOCK_8X16: |
| 302 return vp9_highbd_10_mse8x16; |
| 303 default: |
| 304 return vp9_highbd_10_mse16x16; |
| 305 } |
| 306 break; |
| 307 case 12: |
| 308 switch (bsize) { |
| 309 case BLOCK_8X8: |
| 310 return vp9_highbd_12_mse8x8; |
| 311 case BLOCK_16X8: |
| 312 return vp9_highbd_12_mse16x8; |
| 313 case BLOCK_8X16: |
| 314 return vp9_highbd_12_mse8x16; |
| 315 default: |
| 316 return vp9_highbd_12_mse16x16; |
| 317 } |
| 318 break; |
| 319 } |
| 320 } |
| 321 |
| 322 static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize, |
| 323 const struct buf_2d *src, |
| 324 const struct buf_2d *ref, |
| 325 int bd) { |
| 326 unsigned int sse; |
| 327 const vp9_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd); |
| 328 fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
| 329 return sse; |
| 330 } |
| 331 #endif // CONFIG_VP9_HIGHBITDEPTH |
| 332 |
284 // Refine the motion search range according to the frame dimension | 333 // Refine the motion search range according to the frame dimension |
285 // for first pass test. | 334 // for first pass test. |
286 static int get_search_range(const VP9_COMMON *cm) { | 335 static int get_search_range(const VP9_COMMON *cm) { |
287 int sr = 0; | 336 int sr = 0; |
288 const int dim = MIN(cm->width, cm->height); | 337 const int dim = MIN(cm->width, cm->height); |
289 | 338 |
290 while ((dim << sr) < MAX_FULL_PEL_VAL) | 339 while ((dim << sr) < MAX_FULL_PEL_VAL) |
291 ++sr; | 340 ++sr; |
292 return sr; | 341 return sr; |
293 } | 342 } |
(...skipping 10 matching lines...) Expand all Loading... |
304 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY; | 353 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY; |
305 | 354 |
306 int step_param = 3; | 355 int step_param = 3; |
307 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; | 356 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
308 const int sr = get_search_range(&cpi->common); | 357 const int sr = get_search_range(&cpi->common); |
309 step_param += sr; | 358 step_param += sr; |
310 further_steps -= sr; | 359 further_steps -= sr; |
311 | 360 |
312 // Override the default variance function to use MSE. | 361 // Override the default variance function to use MSE. |
313 v_fn_ptr.vf = get_block_variance_fn(bsize); | 362 v_fn_ptr.vf = get_block_variance_fn(bsize); |
| 363 #if CONFIG_VP9_HIGHBITDEPTH |
| 364 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 365 v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd); |
| 366 } |
| 367 #endif // CONFIG_VP9_HIGHBITDEPTH |
314 | 368 |
315 // Center the initial step/diamond search on best mv. | 369 // Center the initial step/diamond search on best mv. |
316 tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv, | 370 tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv, |
317 step_param, | 371 step_param, |
318 x->sadperbit16, &num00, &v_fn_ptr, ref_mv); | 372 x->sadperbit16, &num00, &v_fn_ptr, ref_mv); |
319 if (tmp_err < INT_MAX) | 373 if (tmp_err < INT_MAX) |
320 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); | 374 tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); |
321 if (tmp_err < INT_MAX - new_mv_mode_penalty) | 375 if (tmp_err < INT_MAX - new_mv_mode_penalty) |
322 tmp_err += new_mv_mode_penalty; | 376 tmp_err += new_mv_mode_penalty; |
323 | 377 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 error_weight = vp9_vaq_inv_q_ratio(energy); | 609 error_weight = vp9_vaq_inv_q_ratio(energy); |
556 } | 610 } |
557 | 611 |
558 // Do intra 16x16 prediction. | 612 // Do intra 16x16 prediction. |
559 x->skip_encode = 0; | 613 x->skip_encode = 0; |
560 xd->mi[0].src_mi->mbmi.mode = DC_PRED; | 614 xd->mi[0].src_mi->mbmi.mode = DC_PRED; |
561 xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ? | 615 xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ? |
562 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; | 616 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; |
563 vp9_encode_intra_block_plane(x, bsize, 0); | 617 vp9_encode_intra_block_plane(x, bsize, 0); |
564 this_error = vp9_get_mb_ss(x->plane[0].src_diff); | 618 this_error = vp9_get_mb_ss(x->plane[0].src_diff); |
| 619 #if CONFIG_VP9_HIGHBITDEPTH |
| 620 if (cm->use_highbitdepth) { |
| 621 switch (cm->bit_depth) { |
| 622 case VPX_BITS_8: |
| 623 break; |
| 624 case VPX_BITS_10: |
| 625 this_error >>= 4; |
| 626 break; |
| 627 case VPX_BITS_12: |
| 628 this_error >>= 8; |
| 629 break; |
| 630 default: |
| 631 assert(0 && "cm->bit_depth should be VPX_BITS_8, " |
| 632 "VPX_BITS_10 or VPX_BITS_12"); |
| 633 return; |
| 634 } |
| 635 } |
| 636 #endif // CONFIG_VP9_HIGHBITDEPTH |
565 | 637 |
566 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 638 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
567 vp9_clear_system_state(); | 639 vp9_clear_system_state(); |
568 this_error = (int)(this_error * error_weight); | 640 this_error = (int)(this_error * error_weight); |
569 } | 641 } |
570 | 642 |
571 // Intrapenalty below deals with situations where the intra and inter | 643 // Intrapenalty below deals with situations where the intra and inter |
572 // error scores are very low (e.g. a plain black frame). | 644 // error scores are very low (e.g. a plain black frame). |
573 // We do not have special cases in first pass for 0,0 and nearest etc so | 645 // We do not have special cases in first pass for 0,0 and nearest etc so |
574 // all inter modes carry an overhead cost estimate for the mv. | 646 // all inter modes carry an overhead cost estimate for the mv. |
(...skipping 19 matching lines...) Expand all Loading... |
594 | 666 |
595 // Other than for the first frame do a motion search. | 667 // Other than for the first frame do a motion search. |
596 if ((lc == NULL && cm->current_video_frame > 0) || | 668 if ((lc == NULL && cm->current_video_frame > 0) || |
597 (lc != NULL && lc->current_video_frame_in_layer > 0)) { | 669 (lc != NULL && lc->current_video_frame_in_layer > 0)) { |
598 int tmp_err, motion_error, raw_motion_error; | 670 int tmp_err, motion_error, raw_motion_error; |
599 // Assume 0,0 motion with no mv overhead. | 671 // Assume 0,0 motion with no mv overhead. |
600 MV mv = {0, 0} , tmp_mv = {0, 0}; | 672 MV mv = {0, 0} , tmp_mv = {0, 0}; |
601 struct buf_2d unscaled_last_source_buf_2d; | 673 struct buf_2d unscaled_last_source_buf_2d; |
602 | 674 |
603 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; | 675 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
604 motion_error = get_prediction_error(bsize, &x->plane[0].src, | 676 #if CONFIG_VP9_HIGHBITDEPTH |
605 &xd->plane[0].pre[0]); | 677 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 678 motion_error = highbd_get_prediction_error( |
| 679 bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd); |
| 680 } else { |
| 681 motion_error = get_prediction_error( |
| 682 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 683 } |
| 684 #else |
| 685 motion_error = get_prediction_error( |
| 686 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 687 #endif // CONFIG_VP9_HIGHBITDEPTH |
606 | 688 |
607 // Compute the motion error of the 0,0 motion using the last source | 689 // Compute the motion error of the 0,0 motion using the last source |
608 // frame as the reference. Skip the further motion search on | 690 // frame as the reference. Skip the further motion search on |
609 // reconstructed frame if this error is small. | 691 // reconstructed frame if this error is small. |
610 unscaled_last_source_buf_2d.buf = | 692 unscaled_last_source_buf_2d.buf = |
611 cpi->unscaled_last_source->y_buffer + recon_yoffset; | 693 cpi->unscaled_last_source->y_buffer + recon_yoffset; |
612 unscaled_last_source_buf_2d.stride = | 694 unscaled_last_source_buf_2d.stride = |
613 cpi->unscaled_last_source->y_stride; | 695 cpi->unscaled_last_source->y_stride; |
614 raw_motion_error = get_prediction_error(bsize, &x->plane[0].src, | 696 #if CONFIG_VP9_HIGHBITDEPTH |
615 &unscaled_last_source_buf_2d); | 697 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 698 raw_motion_error = highbd_get_prediction_error( |
| 699 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd); |
| 700 } else { |
| 701 raw_motion_error = get_prediction_error( |
| 702 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d); |
| 703 } |
| 704 #else |
| 705 raw_motion_error = get_prediction_error( |
| 706 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d); |
| 707 #endif // CONFIG_VP9_HIGHBITDEPTH |
616 | 708 |
617 // TODO(pengchong): Replace the hard-coded threshold | 709 // TODO(pengchong): Replace the hard-coded threshold |
618 if (raw_motion_error > 25 || lc != NULL) { | 710 if (raw_motion_error > 25 || lc != NULL) { |
619 // Test last reference frame using the previous best mv as the | 711 // Test last reference frame using the previous best mv as the |
620 // starting point (best reference) for the search. | 712 // starting point (best reference) for the search. |
621 first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error); | 713 first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error); |
622 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 714 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
623 vp9_clear_system_state(); | 715 vp9_clear_system_state(); |
624 motion_error = (int)(motion_error * error_weight); | 716 motion_error = (int)(motion_error * error_weight); |
625 } | 717 } |
(...skipping 15 matching lines...) Expand all Loading... |
641 } | 733 } |
642 | 734 |
643 // Search in an older reference frame. | 735 // Search in an older reference frame. |
644 if (((lc == NULL && cm->current_video_frame > 1) || | 736 if (((lc == NULL && cm->current_video_frame > 1) || |
645 (lc != NULL && lc->current_video_frame_in_layer > 1)) | 737 (lc != NULL && lc->current_video_frame_in_layer > 1)) |
646 && gld_yv12 != NULL) { | 738 && gld_yv12 != NULL) { |
647 // Assume 0,0 motion with no mv overhead. | 739 // Assume 0,0 motion with no mv overhead. |
648 int gf_motion_error; | 740 int gf_motion_error; |
649 | 741 |
650 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; | 742 xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
651 gf_motion_error = get_prediction_error(bsize, &x->plane[0].src, | 743 #if CONFIG_VP9_HIGHBITDEPTH |
652 &xd->plane[0].pre[0]); | 744 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 745 gf_motion_error = highbd_get_prediction_error( |
| 746 bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd); |
| 747 } else { |
| 748 gf_motion_error = get_prediction_error( |
| 749 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 750 } |
| 751 #else |
| 752 gf_motion_error = get_prediction_error( |
| 753 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 754 #endif // CONFIG_VP9_HIGHBITDEPTH |
653 | 755 |
654 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, | 756 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, |
655 &gf_motion_error); | 757 &gf_motion_error); |
656 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { | 758 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
657 vp9_clear_system_state(); | 759 vp9_clear_system_state(); |
658 gf_motion_error = (int)(gf_motion_error * error_weight); | 760 gf_motion_error = (int)(gf_motion_error * error_weight); |
659 } | 761 } |
660 | 762 |
661 if (gf_motion_error < motion_error && gf_motion_error < this_error) | 763 if (gf_motion_error < motion_error && gf_motion_error < this_error) |
662 ++second_ref_count; | 764 ++second_ref_count; |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 static double calc_correction_factor(double err_per_mb, | 1044 static double calc_correction_factor(double err_per_mb, |
943 double err_divisor, | 1045 double err_divisor, |
944 double pt_low, | 1046 double pt_low, |
945 double pt_high, | 1047 double pt_high, |
946 int q, | 1048 int q, |
947 vpx_bit_depth_t bit_depth) { | 1049 vpx_bit_depth_t bit_depth) { |
948 const double error_term = err_per_mb / err_divisor; | 1050 const double error_term = err_per_mb / err_divisor; |
949 | 1051 |
950 // Adjustment based on actual quantizer to power term. | 1052 // Adjustment based on actual quantizer to power term. |
951 const double power_term = | 1053 const double power_term = |
952 MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.0125 + pt_low, pt_high); | 1054 MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high); |
953 | 1055 |
954 // Calculate correction factor. | 1056 // Calculate correction factor. |
955 if (power_term < 1.0) | 1057 if (power_term < 1.0) |
956 assert(error_term >= 0.0); | 1058 assert(error_term >= 0.0); |
957 | 1059 |
958 return fclamp(pow(error_term, power_term), 0.05, 5.0); | 1060 return fclamp(pow(error_term, power_term), 0.05, 5.0); |
959 } | 1061 } |
960 | 1062 |
| 1063 // Larger image formats are expected to be a little harder to code relatively |
| 1064 // given the same prediction error score. This in part at least relates to the |
| 1065 // increased size and hence coding cost of motion vectors. |
| 1066 #define EDIV_SIZE_FACTOR 800 |
| 1067 |
961 static int get_twopass_worst_quality(const VP9_COMP *cpi, | 1068 static int get_twopass_worst_quality(const VP9_COMP *cpi, |
962 const FIRSTPASS_STATS *stats, | 1069 const FIRSTPASS_STATS *stats, |
963 int section_target_bandwidth) { | 1070 int section_target_bandwidth) { |
964 const RATE_CONTROL *const rc = &cpi->rc; | 1071 const RATE_CONTROL *const rc = &cpi->rc; |
965 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1072 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
966 | 1073 |
967 if (section_target_bandwidth <= 0) { | 1074 if (section_target_bandwidth <= 0) { |
968 return rc->worst_quality; // Highest value allowed | 1075 return rc->worst_quality; // Highest value allowed |
969 } else { | 1076 } else { |
970 const int num_mbs = cpi->common.MBs; | 1077 const int num_mbs = cpi->common.MBs; |
971 const double section_err = stats->coded_error / stats->count; | 1078 const double section_err = stats->coded_error / stats->count; |
972 const double err_per_mb = section_err / num_mbs; | 1079 const double err_per_mb = section_err / num_mbs; |
973 const double speed_term = 1.0 + 0.04 * oxcf->speed; | 1080 const double speed_term = 1.0 + 0.04 * oxcf->speed; |
| 1081 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; |
974 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << | 1082 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << |
975 BPER_MB_NORMBITS) / num_mbs; | 1083 BPER_MB_NORMBITS) / num_mbs; |
| 1084 |
976 int q; | 1085 int q; |
977 int is_svc_upper_layer = 0; | 1086 int is_svc_upper_layer = 0; |
978 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) | 1087 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) |
979 is_svc_upper_layer = 1; | 1088 is_svc_upper_layer = 1; |
980 | 1089 |
981 // Try and pick a max Q that will be high enough to encode the | 1090 // Try and pick a max Q that will be high enough to encode the |
982 // content at the given rate. | 1091 // content at the given rate. |
983 for (q = rc->best_quality; q < rc->worst_quality; ++q) { | 1092 for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
984 const double factor = | 1093 const double factor = |
985 calc_correction_factor(err_per_mb, ERR_DIVISOR, | 1094 calc_correction_factor(err_per_mb, ERR_DIVISOR - ediv_size_correction, |
986 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : | 1095 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
987 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, | 1096 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, |
988 cpi->common.bit_depth); | 1097 cpi->common.bit_depth); |
989 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, | 1098 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, |
990 factor * speed_term, | 1099 factor * speed_term, |
991 cpi->common.bit_depth); | 1100 cpi->common.bit_depth); |
992 if (bits_per_mb <= target_norm_bits_per_mb) | 1101 if (bits_per_mb <= target_norm_bits_per_mb) |
993 break; | 1102 break; |
994 } | 1103 } |
995 | 1104 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 while (s < twopass->stats_in_end) { | 1167 while (s < twopass->stats_in_end) { |
1059 modified_error_total += calculate_modified_err(twopass, oxcf, s); | 1168 modified_error_total += calculate_modified_err(twopass, oxcf, s); |
1060 ++s; | 1169 ++s; |
1061 } | 1170 } |
1062 twopass->modified_error_left = modified_error_total; | 1171 twopass->modified_error_left = modified_error_total; |
1063 } | 1172 } |
1064 | 1173 |
1065 // Reset the vbr bits off target counter | 1174 // Reset the vbr bits off target counter |
1066 cpi->rc.vbr_bits_off_target = 0; | 1175 cpi->rc.vbr_bits_off_target = 0; |
1067 | 1176 |
| 1177 cpi->rc.rate_error_estimate = 0; |
| 1178 |
1068 // Static sequence monitor variables. | 1179 // Static sequence monitor variables. |
1069 twopass->kf_zeromotion_pct = 100; | 1180 twopass->kf_zeromotion_pct = 100; |
1070 twopass->last_kfgroup_zeromotion_pct = 100; | 1181 twopass->last_kfgroup_zeromotion_pct = 100; |
1071 } | 1182 } |
1072 | 1183 |
1073 #define SR_DIFF_PART 0.0015 | 1184 #define SR_DIFF_PART 0.0015 |
1074 #define MOTION_AMP_PART 0.003 | 1185 #define MOTION_AMP_PART 0.003 |
1075 #define INTRA_PART 0.005 | 1186 #define INTRA_PART 0.005 |
1076 #define DEFAULT_DECAY_LIMIT 0.75 | 1187 #define DEFAULT_DECAY_LIMIT 0.75 |
1077 #define LOW_SR_DIFF_TRHESH 0.1 | 1188 #define LOW_SR_DIFF_TRHESH 0.1 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 mvc_ratio : stats->mvc_abs); | 1303 mvc_ratio : stats->mvc_abs); |
1193 } | 1304 } |
1194 } | 1305 } |
1195 | 1306 |
1196 #define BASELINE_ERR_PER_MB 1000.0 | 1307 #define BASELINE_ERR_PER_MB 1000.0 |
1197 static double calc_frame_boost(VP9_COMP *cpi, | 1308 static double calc_frame_boost(VP9_COMP *cpi, |
1198 const FIRSTPASS_STATS *this_frame, | 1309 const FIRSTPASS_STATS *this_frame, |
1199 double this_frame_mv_in_out, | 1310 double this_frame_mv_in_out, |
1200 double max_boost) { | 1311 double max_boost) { |
1201 double frame_boost; | 1312 double frame_boost; |
| 1313 const double lq = |
| 1314 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME], |
| 1315 cpi->common.bit_depth); |
| 1316 const double boost_correction = MIN((0.5 + (lq * 0.015)), 1.5); |
1202 | 1317 |
1203 // Underlying boost factor is based on inter error ratio. | 1318 // Underlying boost factor is based on inter error ratio. |
1204 frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) / | 1319 frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) / |
1205 DOUBLE_DIVIDE_CHECK(this_frame->coded_error); | 1320 DOUBLE_DIVIDE_CHECK(this_frame->coded_error); |
1206 frame_boost = frame_boost * BOOST_FACTOR; | 1321 frame_boost = frame_boost * BOOST_FACTOR * boost_correction; |
1207 | 1322 |
1208 // Increase boost for frames where new data coming into frame (e.g. zoom out). | 1323 // Increase boost for frames where new data coming into frame (e.g. zoom out). |
1209 // Slightly reduce boost if there is a net balance of motion out of the frame | 1324 // Slightly reduce boost if there is a net balance of motion out of the frame |
1210 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. | 1325 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. |
1211 if (this_frame_mv_in_out > 0.0) | 1326 if (this_frame_mv_in_out > 0.0) |
1212 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); | 1327 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
1213 // In the extreme case the boost is halved. | 1328 // In the extreme case the boost is halved. |
1214 else | 1329 else |
1215 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); | 1330 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
1216 | 1331 |
1217 return MIN(frame_boost, max_boost); | 1332 return MIN(frame_boost, max_boost * boost_correction); |
1218 } | 1333 } |
1219 | 1334 |
1220 static int calc_arf_boost(VP9_COMP *cpi, int offset, | 1335 static int calc_arf_boost(VP9_COMP *cpi, int offset, |
1221 int f_frames, int b_frames, | 1336 int f_frames, int b_frames, |
1222 int *f_boost, int *b_boost) { | 1337 int *f_boost, int *b_boost) { |
1223 TWO_PASS *const twopass = &cpi->twopass; | 1338 TWO_PASS *const twopass = &cpi->twopass; |
1224 int i; | 1339 int i; |
1225 double boost_score = 0.0; | 1340 double boost_score = 0.0; |
1226 double mv_ratio_accumulator = 0.0; | 1341 double mv_ratio_accumulator = 0.0; |
1227 double decay_accumulator = 1.0; | 1342 double decay_accumulator = 1.0; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1296 | 1411 |
1297 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, | 1412 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, |
1298 this_frame_mv_in_out, | 1413 this_frame_mv_in_out, |
1299 GF_MAX_BOOST); | 1414 GF_MAX_BOOST); |
1300 } | 1415 } |
1301 *b_boost = (int)boost_score; | 1416 *b_boost = (int)boost_score; |
1302 | 1417 |
1303 arf_boost = (*f_boost + *b_boost); | 1418 arf_boost = (*f_boost + *b_boost); |
1304 if (arf_boost < ((b_frames + f_frames) * 20)) | 1419 if (arf_boost < ((b_frames + f_frames) * 20)) |
1305 arf_boost = ((b_frames + f_frames) * 20); | 1420 arf_boost = ((b_frames + f_frames) * 20); |
| 1421 arf_boost = MAX(arf_boost, MIN_ARF_GF_BOOST); |
1306 | 1422 |
1307 return arf_boost; | 1423 return arf_boost; |
1308 } | 1424 } |
1309 | 1425 |
1310 // Calculate a section intra ratio used in setting max loop filter. | 1426 // Calculate a section intra ratio used in setting max loop filter. |
1311 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, | 1427 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, |
1312 const FIRSTPASS_STATS *end, | 1428 const FIRSTPASS_STATS *end, |
1313 int section_length) { | 1429 int section_length) { |
1314 const FIRSTPASS_STATS *s = begin; | 1430 const FIRSTPASS_STATS *s = begin; |
1315 double intra_error = 0.0; | 1431 double intra_error = 0.0; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1573 double this_frame_mv_in_out = 0.0; | 1689 double this_frame_mv_in_out = 0.0; |
1574 double mv_in_out_accumulator = 0.0; | 1690 double mv_in_out_accumulator = 0.0; |
1575 double abs_mv_in_out_accumulator = 0.0; | 1691 double abs_mv_in_out_accumulator = 0.0; |
1576 double mv_ratio_accumulator_thresh; | 1692 double mv_ratio_accumulator_thresh; |
1577 unsigned int allow_alt_ref = is_altref_enabled(cpi); | 1693 unsigned int allow_alt_ref = is_altref_enabled(cpi); |
1578 | 1694 |
1579 int f_boost = 0; | 1695 int f_boost = 0; |
1580 int b_boost = 0; | 1696 int b_boost = 0; |
1581 int flash_detected; | 1697 int flash_detected; |
1582 int active_max_gf_interval; | 1698 int active_max_gf_interval; |
| 1699 int active_min_gf_interval; |
1583 int64_t gf_group_bits; | 1700 int64_t gf_group_bits; |
1584 double gf_group_error_left; | 1701 double gf_group_error_left; |
1585 int gf_arf_bits; | 1702 int gf_arf_bits; |
1586 | 1703 |
1587 // Reset the GF group data structures unless this is a key | 1704 // Reset the GF group data structures unless this is a key |
1588 // frame in which case it will already have been done. | 1705 // frame in which case it will already have been done. |
1589 if (cpi->common.frame_type != KEY_FRAME) { | 1706 if (cpi->common.frame_type != KEY_FRAME) { |
1590 vp9_zero(twopass->gf_group); | 1707 vp9_zero(twopass->gf_group); |
1591 } | 1708 } |
1592 | 1709 |
1593 vp9_clear_system_state(); | 1710 vp9_clear_system_state(); |
1594 vp9_zero(next_frame); | 1711 vp9_zero(next_frame); |
1595 | 1712 |
1596 // Load stats for the current frame. | 1713 // Load stats for the current frame. |
1597 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1714 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
1598 | 1715 |
1599 // Note the error of the frame at the start of the group. This will be | 1716 // Note the error of the frame at the start of the group. This will be |
1600 // the GF frame error if we code a normal gf. | 1717 // the GF frame error if we code a normal gf. |
1601 gf_first_frame_err = mod_frame_err; | 1718 gf_first_frame_err = mod_frame_err; |
1602 | 1719 |
1603 // If this is a key frame or the overlay from a previous arf then | 1720 // If this is a key frame or the overlay from a previous arf then |
1604 // the error score / cost of this frame has already been accounted for. | 1721 // the error score / cost of this frame has already been accounted for. |
1605 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) | 1722 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) |
1606 gf_group_err -= gf_first_frame_err; | 1723 gf_group_err -= gf_first_frame_err; |
1607 | 1724 |
1608 // Motion breakout threshold for loop below depends on image size. | 1725 // Motion breakout threshold for loop below depends on image size. |
1609 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0; | 1726 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0; |
1610 | 1727 |
1611 // Work out a maximum interval for the GF group. | 1728 // Set a maximum and minimum interval for the GF group. |
1612 // If the image appears almost completely static we can extend beyond this. | 1729 // If the image appears almost completely static we can extend beyond this. |
1613 if (cpi->multi_arf_allowed) { | 1730 { |
1614 active_max_gf_interval = rc->max_gf_interval; | 1731 int int_max_q = |
1615 } else { | 1732 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, |
1616 // The value chosen depends on the active Q range. At low Q we have | 1733 cpi->common.bit_depth)); |
1617 // bits to spare and are better with a smaller interval and smaller boost. | 1734 int int_lbq = |
1618 // At high Q when there are few bits to spare we are better with a longer | 1735 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex, |
1619 // interval to spread the cost of the GF. | 1736 cpi->common.bit_depth)); |
1620 active_max_gf_interval = | 1737 active_min_gf_interval = MIN_GF_INTERVAL + MIN(2, int_max_q / 200); |
1621 12 + ((int)vp9_convert_qindex_to_q(rc->last_q[INTER_FRAME], | 1738 if (active_min_gf_interval > rc->max_gf_interval) |
1622 cpi->common.bit_depth) >> 5); | 1739 active_min_gf_interval = rc->max_gf_interval; |
1623 | 1740 |
1624 if (active_max_gf_interval > rc->max_gf_interval) | 1741 if (cpi->multi_arf_allowed) { |
1625 active_max_gf_interval = rc->max_gf_interval; | 1742 active_max_gf_interval = rc->max_gf_interval; |
| 1743 } else { |
| 1744 // The value chosen depends on the active Q range. At low Q we have |
| 1745 // bits to spare and are better with a smaller interval and smaller boost. |
| 1746 // At high Q when there are few bits to spare we are better with a longer |
| 1747 // interval to spread the cost of the GF. |
| 1748 active_max_gf_interval = 12 + MIN(4, (int_lbq / 6)); |
| 1749 if (active_max_gf_interval > rc->max_gf_interval) |
| 1750 active_max_gf_interval = rc->max_gf_interval; |
| 1751 } |
1626 } | 1752 } |
1627 | 1753 |
1628 i = 0; | 1754 i = 0; |
1629 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { | 1755 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
1630 ++i; | 1756 ++i; |
1631 | 1757 |
1632 // Accumulate error score of frames in this gf group. | 1758 // Accumulate error score of frames in this gf group. |
1633 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); | 1759 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); |
1634 gf_group_err += mod_frame_err; | 1760 gf_group_err += mod_frame_err; |
1635 | 1761 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1671 boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame, | 1797 boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame, |
1672 this_frame_mv_in_out, | 1798 this_frame_mv_in_out, |
1673 GF_MAX_BOOST); | 1799 GF_MAX_BOOST); |
1674 | 1800 |
1675 // Break out conditions. | 1801 // Break out conditions. |
1676 if ( | 1802 if ( |
1677 // Break at active_max_gf_interval unless almost totally static. | 1803 // Break at active_max_gf_interval unless almost totally static. |
1678 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || | 1804 (i >= active_max_gf_interval && (zero_motion_accumulator < 0.995)) || |
1679 ( | 1805 ( |
1680 // Don't break out with a very short interval. | 1806 // Don't break out with a very short interval. |
1681 (i > MIN_GF_INTERVAL) && | 1807 (i > active_min_gf_interval) && |
1682 (!flash_detected) && | 1808 (!flash_detected) && |
1683 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || | 1809 ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || |
1684 (abs_mv_in_out_accumulator > 3.0) || | 1810 (abs_mv_in_out_accumulator > 3.0) || |
1685 (mv_in_out_accumulator < -2.0) || | 1811 (mv_in_out_accumulator < -2.0) || |
1686 ((boost_score - old_boost_score) < BOOST_FACTOR)))) { | 1812 ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) { |
1687 boost_score = old_boost_score; | 1813 boost_score = old_boost_score; |
1688 break; | 1814 break; |
1689 } | 1815 } |
1690 | 1816 |
1691 *this_frame = next_frame; | 1817 *this_frame = next_frame; |
1692 old_boost_score = boost_score; | 1818 old_boost_score = boost_score; |
1693 } | 1819 } |
1694 | 1820 |
1695 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); | 1821 twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
1696 | 1822 |
(...skipping 27 matching lines...) Expand all Loading... |
1724 // Calculate the boost for alt ref. | 1850 // Calculate the boost for alt ref. |
1725 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, | 1851 rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
1726 &b_boost); | 1852 &b_boost); |
1727 rc->source_alt_ref_pending = 1; | 1853 rc->source_alt_ref_pending = 1; |
1728 | 1854 |
1729 // Test to see if multi arf is appropriate. | 1855 // Test to see if multi arf is appropriate. |
1730 cpi->multi_arf_enabled = | 1856 cpi->multi_arf_enabled = |
1731 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) && | 1857 (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) && |
1732 (zero_motion_accumulator < 0.995)) ? 1 : 0; | 1858 (zero_motion_accumulator < 0.995)) ? 1 : 0; |
1733 } else { | 1859 } else { |
1734 rc->gfu_boost = MAX((int)boost_score, 125); | 1860 rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); |
1735 rc->source_alt_ref_pending = 0; | 1861 rc->source_alt_ref_pending = 0; |
1736 } | 1862 } |
1737 | 1863 |
1738 // Reset the file position. | 1864 // Reset the file position. |
1739 reset_fpf_position(twopass, start_pos); | 1865 reset_fpf_position(twopass, start_pos); |
1740 | 1866 |
1741 // Calculate the bits to be allocated to the gf/arf group as a whole | 1867 // Calculate the bits to be allocated to the gf/arf group as a whole |
1742 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); | 1868 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
1743 | 1869 |
1744 // Calculate the extra bits to be used for boosted frame(s) | 1870 // Calculate the extra bits to be used for boosted frame(s) |
1745 { | 1871 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
1746 int q = rc->last_q[INTER_FRAME]; | 1872 rc->gfu_boost, gf_group_bits); |
1747 int boost = | |
1748 (rc->gfu_boost * gfboost_qadjust(q, cpi->common.bit_depth)) / 100; | |
1749 | |
1750 // Set max and minimum boost and hence minimum allocation. | |
1751 boost = clamp(boost, 125, (rc->baseline_gf_interval + 1) * 200); | |
1752 | |
1753 // Calculate the extra bits to be used for boosted frame(s) | |
1754 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, | |
1755 boost, gf_group_bits); | |
1756 } | |
1757 | 1873 |
1758 // Adjust KF group bits and error remaining. | 1874 // Adjust KF group bits and error remaining. |
1759 twopass->kf_group_error_left -= (int64_t)gf_group_err; | 1875 twopass->kf_group_error_left -= (int64_t)gf_group_err; |
1760 | 1876 |
1761 // If this is an arf update we want to remove the score for the overlay | 1877 // If this is an arf update we want to remove the score for the overlay |
1762 // frame at the end which will usually be very cheap to code. | 1878 // frame at the end which will usually be very cheap to code. |
1763 // The overlay frame has already, in effect, been coded so we want to spread | 1879 // The overlay frame has already, in effect, been coded so we want to spread |
1764 // the remaining bits among the other frames. | 1880 // the remaining bits among the other frames. |
1765 // For normal GFs remove the score for the GF itself unless this is | 1881 // For normal GFs remove the score for the GF itself unless this is |
1766 // also a key frame in which case it has already been accounted for. | 1882 // also a key frame in which case it has already been accounted for. |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2094 | 2210 |
2095 // Note the total error score of the kf group minus the key frame itself. | 2211 // Note the total error score of the kf group minus the key frame itself. |
2096 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); | 2212 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
2097 | 2213 |
2098 // Adjust the count of total modified error left. | 2214 // Adjust the count of total modified error left. |
2099 // The count of bits left is adjusted elsewhere based on real coded frame | 2215 // The count of bits left is adjusted elsewhere based on real coded frame |
2100 // sizes. | 2216 // sizes. |
2101 twopass->modified_error_left -= kf_group_err; | 2217 twopass->modified_error_left -= kf_group_err; |
2102 } | 2218 } |
2103 | 2219 |
| 2220 #define VBR_PCT_ADJUSTMENT_LIMIT 50 |
2104 // For VBR...adjustment to the frame target based on error from previous frames | 2221 // For VBR...adjustment to the frame target based on error from previous frames |
2105 void vbr_rate_correction(int * this_frame_target, | 2222 void vbr_rate_correction(VP9_COMP *cpi, |
| 2223 int * this_frame_target, |
2106 const int64_t vbr_bits_off_target) { | 2224 const int64_t vbr_bits_off_target) { |
2107 int max_delta = (*this_frame_target * 15) / 100; | 2225 int max_delta; |
| 2226 double position_factor = 1.0; |
| 2227 |
| 2228 // How far through the clip are we. |
| 2229 // This number is used to damp the per frame rate correction. |
| 2230 // Range 0 - 1.0 |
| 2231 if (cpi->twopass.total_stats.count) { |
| 2232 position_factor = sqrt((double)cpi->common.current_video_frame / |
| 2233 cpi->twopass.total_stats.count); |
| 2234 } |
| 2235 max_delta = (int)(position_factor * |
| 2236 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100)); |
2108 | 2237 |
2109 // vbr_bits_off_target > 0 means we have extra bits to spend | 2238 // vbr_bits_off_target > 0 means we have extra bits to spend |
2110 if (vbr_bits_off_target > 0) { | 2239 if (vbr_bits_off_target > 0) { |
2111 *this_frame_target += | 2240 *this_frame_target += |
2112 (vbr_bits_off_target > max_delta) ? max_delta | 2241 (vbr_bits_off_target > max_delta) ? max_delta |
2113 : (int)vbr_bits_off_target; | 2242 : (int)vbr_bits_off_target; |
2114 } else { | 2243 } else { |
2115 *this_frame_target -= | 2244 *this_frame_target -= |
2116 (vbr_bits_off_target < -max_delta) ? max_delta | 2245 (vbr_bits_off_target < -max_delta) ? max_delta |
2117 : (int)-vbr_bits_off_target; | 2246 : (int)-vbr_bits_off_target; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2195 // advance the input pointer as we already have what we need. | 2324 // advance the input pointer as we already have what we need. |
2196 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) { | 2325 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) { |
2197 int target_rate; | 2326 int target_rate; |
2198 configure_buffer_updates(cpi); | 2327 configure_buffer_updates(cpi); |
2199 target_rate = gf_group->bit_allocation[gf_group->index]; | 2328 target_rate = gf_group->bit_allocation[gf_group->index]; |
2200 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); | 2329 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
2201 rc->base_frame_target = target_rate; | 2330 rc->base_frame_target = target_rate; |
2202 | 2331 |
2203 // Correction to rate target based on prior over or under shoot. | 2332 // Correction to rate target based on prior over or under shoot. |
2204 if (cpi->oxcf.rc_mode == VPX_VBR) | 2333 if (cpi->oxcf.rc_mode == VPX_VBR) |
2205 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); | 2334 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); |
2206 | 2335 |
2207 vp9_rc_set_frame_target(cpi, target_rate); | 2336 vp9_rc_set_frame_target(cpi, target_rate); |
2208 cm->frame_type = INTER_FRAME; | 2337 cm->frame_type = INTER_FRAME; |
2209 | 2338 |
2210 if (lc != NULL) { | 2339 if (lc != NULL) { |
2211 if (cpi->svc.spatial_layer_id == 0) { | 2340 if (cpi->svc.spatial_layer_id == 0) { |
2212 lc->is_key_frame = 0; | 2341 lc->is_key_frame = 0; |
2213 } else { | 2342 } else { |
2214 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2343 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
2215 | 2344 |
(...skipping 11 matching lines...) Expand all Loading... |
2227 twopass->active_worst_quality = cpi->oxcf.cq_level; | 2356 twopass->active_worst_quality = cpi->oxcf.cq_level; |
2228 } else if (cm->current_video_frame == 0 || | 2357 } else if (cm->current_video_frame == 0 || |
2229 (lc != NULL && lc->current_video_frame_in_layer == 0)) { | 2358 (lc != NULL && lc->current_video_frame_in_layer == 0)) { |
2230 // Special case code for first frame. | 2359 // Special case code for first frame. |
2231 const int section_target_bandwidth = (int)(twopass->bits_left / | 2360 const int section_target_bandwidth = (int)(twopass->bits_left / |
2232 frames_left); | 2361 frames_left); |
2233 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, | 2362 const int tmp_q = get_twopass_worst_quality(cpi, &twopass->total_left_stats, |
2234 section_target_bandwidth); | 2363 section_target_bandwidth); |
2235 twopass->active_worst_quality = tmp_q; | 2364 twopass->active_worst_quality = tmp_q; |
2236 rc->ni_av_qi = tmp_q; | 2365 rc->ni_av_qi = tmp_q; |
| 2366 rc->last_q[INTER_FRAME] = tmp_q; |
2237 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); | 2367 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); |
| 2368 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; |
| 2369 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; |
| 2370 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; |
2238 } | 2371 } |
2239 vp9_zero(this_frame); | 2372 vp9_zero(this_frame); |
2240 if (EOF == input_stats(twopass, &this_frame)) | 2373 if (EOF == input_stats(twopass, &this_frame)) |
2241 return; | 2374 return; |
2242 | 2375 |
2243 // Local copy of the current frame's first pass stats. | 2376 // Local copy of the current frame's first pass stats. |
2244 this_frame_copy = this_frame; | 2377 this_frame_copy = this_frame; |
2245 | 2378 |
2246 // Keyframe and section processing. | 2379 // Keyframe and section processing. |
2247 if (rc->frames_to_key == 0 || | 2380 if (rc->frames_to_key == 0 || |
2248 (cpi->frame_flags & FRAMEFLAGS_KEY)) { | 2381 (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
2249 // Define next KF group and assign bits to it. | 2382 // Define next KF group and assign bits to it. |
2250 find_next_key_frame(cpi, &this_frame_copy); | 2383 find_next_key_frame(cpi, &this_frame_copy); |
2251 } else { | 2384 } else { |
2252 cm->frame_type = INTER_FRAME; | 2385 cm->frame_type = INTER_FRAME; |
2253 } | 2386 } |
2254 | 2387 |
2255 if (lc != NULL) { | 2388 if (lc != NULL) { |
2256 if (cpi->svc.spatial_layer_id == 0) { | 2389 if (cpi->svc.spatial_layer_id == 0) { |
2257 lc->is_key_frame = (cm->frame_type == KEY_FRAME); | 2390 lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
2258 if (lc->is_key_frame) { | 2391 if (lc->is_key_frame) { |
2259 cpi->ref_frame_flags &= | 2392 cpi->ref_frame_flags &= |
2260 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); | 2393 (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
2261 lc->frames_from_key_frame = 0; | 2394 lc->frames_from_key_frame = 0; |
| 2395 // Reset the empty frame resolution since we have a key frame. |
| 2396 cpi->svc.empty_frame_width = cm->width; |
| 2397 cpi->svc.empty_frame_height = cm->height; |
2262 } | 2398 } |
2263 } else { | 2399 } else { |
2264 cm->frame_type = INTER_FRAME; | 2400 cm->frame_type = INTER_FRAME; |
2265 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; | 2401 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
2266 | 2402 |
2267 if (lc->is_key_frame) { | 2403 if (lc->is_key_frame) { |
2268 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); | 2404 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
2269 lc->frames_from_key_frame = 0; | 2405 lc->frames_from_key_frame = 0; |
2270 } | 2406 } |
2271 } | 2407 } |
2272 } | 2408 } |
2273 | 2409 |
2274 // Define a new GF/ARF group. (Should always enter here for key frames). | 2410 // Define a new GF/ARF group. (Should always enter here for key frames). |
2275 if (rc->frames_till_gf_update_due == 0) { | 2411 if (rc->frames_till_gf_update_due == 0) { |
2276 define_gf_group(cpi, &this_frame_copy); | 2412 define_gf_group(cpi, &this_frame_copy); |
2277 | 2413 |
2278 if (twopass->gf_zeromotion_pct > 995) { | |
2279 // As long as max_thresh for encode breakout is small enough, it is ok | |
2280 // to enable it for show frame, i.e. set allow_encode_breakout to | |
2281 // ENCODE_BREAKOUT_LIMITED. | |
2282 if (!cm->show_frame) | |
2283 cpi->allow_encode_breakout = ENCODE_BREAKOUT_DISABLED; | |
2284 else | |
2285 cpi->allow_encode_breakout = ENCODE_BREAKOUT_LIMITED; | |
2286 } | |
2287 | |
2288 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 2414 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
2289 if (lc != NULL) | 2415 if (lc != NULL) |
2290 cpi->refresh_golden_frame = 1; | 2416 cpi->refresh_golden_frame = 1; |
2291 | 2417 |
2292 #if ARF_STATS_OUTPUT | 2418 #if ARF_STATS_OUTPUT |
2293 { | 2419 { |
2294 FILE *fpfile; | 2420 FILE *fpfile; |
2295 fpfile = fopen("arf.stt", "a"); | 2421 fpfile = fopen("arf.stt", "a"); |
2296 ++arf_count; | 2422 ++arf_count; |
2297 fprintf(fpfile, "%10d %10d %10d %10ld\n", | 2423 fprintf(fpfile, "%10d %10ld %10d %10d %10ld\n", |
2298 cm->current_video_frame, rc->kf_boost, arf_count, rc->gfu_boost); | 2424 cm->current_video_frame, rc->frames_till_gf_update_due, |
| 2425 rc->kf_boost, arf_count, rc->gfu_boost); |
2299 | 2426 |
2300 fclose(fpfile); | 2427 fclose(fpfile); |
2301 } | 2428 } |
2302 #endif | 2429 #endif |
2303 } | 2430 } |
2304 | 2431 |
2305 configure_buffer_updates(cpi); | 2432 configure_buffer_updates(cpi); |
2306 | 2433 |
2307 target_rate = gf_group->bit_allocation[gf_group->index]; | 2434 target_rate = gf_group->bit_allocation[gf_group->index]; |
2308 if (cpi->common.frame_type == KEY_FRAME) | 2435 if (cpi->common.frame_type == KEY_FRAME) |
2309 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); | 2436 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); |
2310 else | 2437 else |
2311 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); | 2438 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
2312 | 2439 |
2313 rc->base_frame_target = target_rate; | 2440 rc->base_frame_target = target_rate; |
2314 | 2441 |
2315 // Correction to rate target based on prior over or under shoot. | 2442 // Correction to rate target based on prior over or under shoot. |
2316 if (cpi->oxcf.rc_mode == VPX_VBR) | 2443 if (cpi->oxcf.rc_mode == VPX_VBR) |
2317 vbr_rate_correction(&target_rate, rc->vbr_bits_off_target); | 2444 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); |
2318 | 2445 |
2319 vp9_rc_set_frame_target(cpi, target_rate); | 2446 vp9_rc_set_frame_target(cpi, target_rate); |
2320 | 2447 |
2321 // Update the total stats remaining structure. | 2448 // Update the total stats remaining structure. |
2322 subtract_stats(&twopass->total_left_stats, &this_frame); | 2449 subtract_stats(&twopass->total_left_stats, &this_frame); |
2323 } | 2450 } |
2324 | 2451 |
| 2452 #define MINQ_ADJ_LIMIT 32 |
| 2453 #define Q_LIMIT_STEP 1 |
2325 void vp9_twopass_postencode_update(VP9_COMP *cpi) { | 2454 void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
2326 TWO_PASS *const twopass = &cpi->twopass; | 2455 TWO_PASS *const twopass = &cpi->twopass; |
2327 RATE_CONTROL *const rc = &cpi->rc; | 2456 RATE_CONTROL *const rc = &cpi->rc; |
| 2457 const int bits_used = rc->base_frame_target; |
2328 | 2458 |
2329 // VBR correction is done through rc->vbr_bits_off_target. Based on the | 2459 // VBR correction is done through rc->vbr_bits_off_target. Based on the |
2330 // sign of this value, a limited % adjustment is made to the target rate | 2460 // sign of this value, a limited % adjustment is made to the target rate |
2331 // of subsequent frames, to try and push it back towards 0. This method | 2461 // of subsequent frames, to try and push it back towards 0. This method |
2332 // is designed to prevent extreme behaviour at the end of a clip | 2462 // is designed to prevent extreme behaviour at the end of a clip |
2333 // or group of frames. | 2463 // or group of frames. |
2334 const int bits_used = rc->base_frame_target; | |
2335 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; | 2464 rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; |
| 2465 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); |
2336 | 2466 |
2337 twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); | 2467 // Calculate the pct rc error. |
| 2468 if (rc->total_actual_bits) { |
| 2469 rc->rate_error_estimate = |
| 2470 (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits); |
| 2471 rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100); |
| 2472 } else { |
| 2473 rc->rate_error_estimate = 0; |
| 2474 } |
2338 | 2475 |
2339 if (cpi->common.frame_type != KEY_FRAME && | 2476 if (cpi->common.frame_type != KEY_FRAME && |
2340 !vp9_is_upper_layer_key_frame(cpi)) { | 2477 !vp9_is_upper_layer_key_frame(cpi)) { |
2341 twopass->kf_group_bits -= bits_used; | 2478 twopass->kf_group_bits -= bits_used; |
2342 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; | 2479 twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; |
2343 } | 2480 } |
2344 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); | 2481 twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); |
2345 | 2482 |
2346 // Increment the gf group index ready for the next frame. | 2483 // Increment the gf group index ready for the next frame. |
2347 ++twopass->gf_group.index; | 2484 ++twopass->gf_group.index; |
| 2485 |
| 2486 // If the rate control is drifting consider adjustment ot min or maxq. |
| 2487 // Only make adjustments on gf/arf |
| 2488 if ((cpi->oxcf.rc_mode == VPX_VBR) && |
| 2489 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && |
| 2490 !cpi->rc.is_src_frame_alt_ref) { |
| 2491 const int maxq_adj_limit = |
| 2492 rc->worst_quality - twopass->active_worst_quality; |
| 2493 |
| 2494 // Undershoot. |
| 2495 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) { |
| 2496 --twopass->extend_maxq; |
| 2497 if (rc->rolling_target_bits >= rc->rolling_actual_bits) |
| 2498 twopass->extend_minq += Q_LIMIT_STEP; |
| 2499 // Overshoot. |
| 2500 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) { |
| 2501 --twopass->extend_minq; |
| 2502 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2503 twopass->extend_maxq += Q_LIMIT_STEP; |
| 2504 } else { |
| 2505 if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2506 --twopass->extend_minq; |
| 2507 if (rc->rolling_target_bits > rc->rolling_actual_bits) |
| 2508 --twopass->extend_maxq; |
| 2509 } |
| 2510 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); |
| 2511 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
| 2512 } |
2348 } | 2513 } |
OLD | NEW |