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 <assert.h> | 11 #include <assert.h> |
12 #include <limits.h> | 12 #include <limits.h> |
13 #include <math.h> | 13 #include <math.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <string.h> | 16 #include <string.h> |
17 | 17 |
18 #include "vpx_mem/vpx_mem.h" | 18 #include "vpx_mem/vpx_mem.h" |
| 19 #include "vpx_ports/mem.h" |
19 | 20 |
20 #include "vp9/common/vp9_alloccommon.h" | 21 #include "vp9/common/vp9_alloccommon.h" |
21 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" | 22 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" |
22 #include "vp9/common/vp9_common.h" | 23 #include "vp9/common/vp9_common.h" |
23 #include "vp9/common/vp9_entropymode.h" | 24 #include "vp9/common/vp9_entropymode.h" |
24 #include "vp9/common/vp9_quant_common.h" | 25 #include "vp9/common/vp9_quant_common.h" |
25 #include "vp9/common/vp9_seg_common.h" | 26 #include "vp9/common/vp9_seg_common.h" |
26 #include "vp9/common/vp9_systemdependent.h" | 27 #include "vp9/common/vp9_systemdependent.h" |
27 | 28 |
28 #include "vp9/encoder/vp9_encodemv.h" | 29 #include "vp9/encoder/vp9_encodemv.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 const double maxq = vp9_convert_qindex_to_q(i, bit_depth); | 130 const double maxq = vp9_convert_qindex_to_q(i, bit_depth); |
130 kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); | 131 kf_low_m[i] = get_minq_index(maxq, 0.000001, -0.0004, 0.150, bit_depth); |
131 kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); | 132 kf_high_m[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
132 arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); | 133 arfgf_low[i] = get_minq_index(maxq, 0.0000015, -0.0009, 0.30, bit_depth); |
133 arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); | 134 arfgf_high[i] = get_minq_index(maxq, 0.0000021, -0.00125, 0.55, bit_depth); |
134 inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth); | 135 inter[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.90, bit_depth); |
135 rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); | 136 rtc[i] = get_minq_index(maxq, 0.00000271, -0.00113, 0.70, bit_depth); |
136 } | 137 } |
137 } | 138 } |
138 | 139 |
139 void vp9_rc_init_minq_luts() { | 140 void vp9_rc_init_minq_luts(void) { |
140 init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, | 141 init_minq_luts(kf_low_motion_minq_8, kf_high_motion_minq_8, |
141 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, | 142 arfgf_low_motion_minq_8, arfgf_high_motion_minq_8, |
142 inter_minq_8, rtc_minq_8, VPX_BITS_8); | 143 inter_minq_8, rtc_minq_8, VPX_BITS_8); |
143 #if CONFIG_VP9_HIGHBITDEPTH | 144 #if CONFIG_VP9_HIGHBITDEPTH |
144 init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, | 145 init_minq_luts(kf_low_motion_minq_10, kf_high_motion_minq_10, |
145 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, | 146 arfgf_low_motion_minq_10, arfgf_high_motion_minq_10, |
146 inter_minq_10, rtc_minq_10, VPX_BITS_10); | 147 inter_minq_10, rtc_minq_10, VPX_BITS_10); |
147 init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, | 148 init_minq_luts(kf_low_motion_minq_12, kf_high_motion_minq_12, |
148 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, | 149 arfgf_low_motion_minq_12, arfgf_high_motion_minq_12, |
149 inter_minq_12, rtc_minq_12, VPX_BITS_12); | 150 inter_minq_12, rtc_minq_12, VPX_BITS_12); |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 const double correction_factor = get_rate_correction_factor(cpi); | 485 const double correction_factor = get_rate_correction_factor(cpi); |
485 | 486 |
486 // Calculate required scaling factor based on target frame size and size of | 487 // Calculate required scaling factor based on target frame size and size of |
487 // frame produced using previous Q. | 488 // frame produced using previous Q. |
488 target_bits_per_mb = | 489 target_bits_per_mb = |
489 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs; | 490 ((uint64_t)target_bits_per_frame << BPER_MB_NORMBITS) / cm->MBs; |
490 | 491 |
491 i = active_best_quality; | 492 i = active_best_quality; |
492 | 493 |
493 do { | 494 do { |
494 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { | 495 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && |
| 496 cm->seg.enabled && |
| 497 cpi->svc.temporal_layer_id == 0 && |
| 498 cpi->svc.spatial_layer_id == 0) { |
495 bits_per_mb_at_this_q = | 499 bits_per_mb_at_this_q = |
496 (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor); | 500 (int)vp9_cyclic_refresh_rc_bits_per_mb(cpi, i, correction_factor); |
497 } else { | 501 } else { |
498 bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i, | 502 bits_per_mb_at_this_q = (int)vp9_rc_bits_per_mb(cm->frame_type, i, |
499 correction_factor, | 503 correction_factor, |
500 cm->bit_depth); | 504 cm->bit_depth); |
501 } | 505 } |
502 | 506 |
503 if (bits_per_mb_at_this_q <= target_bits_per_mb) { | 507 if (bits_per_mb_at_this_q <= target_bits_per_mb) { |
504 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) | 508 if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 } | 1054 } |
1051 } | 1055 } |
1052 | 1056 |
1053 // Extension to max or min Q if undershoot or overshoot is outside | 1057 // Extension to max or min Q if undershoot or overshoot is outside |
1054 // the permitted range. | 1058 // the permitted range. |
1055 if ((cpi->oxcf.rc_mode != VPX_Q) && | 1059 if ((cpi->oxcf.rc_mode != VPX_Q) && |
1056 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { | 1060 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { |
1057 if (frame_is_intra_only(cm) || | 1061 if (frame_is_intra_only(cm) || |
1058 (!rc->is_src_frame_alt_ref && | 1062 (!rc->is_src_frame_alt_ref && |
1059 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { | 1063 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
1060 active_best_quality -= cpi->twopass.extend_minq; | 1064 active_best_quality -= |
| 1065 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast); |
1061 active_worst_quality += (cpi->twopass.extend_maxq / 2); | 1066 active_worst_quality += (cpi->twopass.extend_maxq / 2); |
1062 } else { | 1067 } else { |
1063 active_best_quality -= cpi->twopass.extend_minq / 2; | 1068 active_best_quality -= |
| 1069 (cpi->twopass.extend_minq + cpi->twopass.extend_minq_fast) / 2; |
1064 active_worst_quality += cpi->twopass.extend_maxq; | 1070 active_worst_quality += cpi->twopass.extend_maxq; |
1065 } | 1071 } |
1066 } | 1072 } |
1067 | 1073 |
1068 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY | 1074 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
1069 vp9_clear_system_state(); | 1075 vp9_clear_system_state(); |
1070 // Static forced key frames Q restrictions dealt with elsewhere. | 1076 // Static forced key frames Q restrictions dealt with elsewhere. |
1071 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || | 1077 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || |
1072 !rc->this_key_frame_forced || | 1078 !rc->this_key_frame_forced || |
1073 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { | 1079 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1196 } | 1202 } |
1197 | 1203 |
1198 static void update_golden_frame_stats(VP9_COMP *cpi) { | 1204 static void update_golden_frame_stats(VP9_COMP *cpi) { |
1199 RATE_CONTROL *const rc = &cpi->rc; | 1205 RATE_CONTROL *const rc = &cpi->rc; |
1200 | 1206 |
1201 // Update the Golden frame usage counts. | 1207 // Update the Golden frame usage counts. |
1202 if (cpi->refresh_golden_frame) { | 1208 if (cpi->refresh_golden_frame) { |
1203 // this frame refreshes means next frames don't unless specified by user | 1209 // this frame refreshes means next frames don't unless specified by user |
1204 rc->frames_since_golden = 0; | 1210 rc->frames_since_golden = 0; |
1205 | 1211 |
1206 if (cpi->oxcf.pass == 2) { | 1212 // If we are not using alt ref in the up and coming group clear the arf |
1207 if (!rc->source_alt_ref_pending && | 1213 // active flag. |
1208 cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD) | 1214 if (!rc->source_alt_ref_pending) { |
1209 rc->source_alt_ref_active = 0; | |
1210 } else if (!rc->source_alt_ref_pending) { | |
1211 rc->source_alt_ref_active = 0; | 1215 rc->source_alt_ref_active = 0; |
1212 } | 1216 } |
1213 | 1217 |
1214 // Decrement count down till next gf | 1218 // Decrement count down till next gf |
1215 if (rc->frames_till_gf_update_due > 0) | 1219 if (rc->frames_till_gf_update_due > 0) |
1216 rc->frames_till_gf_update_due--; | 1220 rc->frames_till_gf_update_due--; |
1217 | 1221 |
1218 } else if (!cpi->refresh_alt_ref_frame) { | 1222 } else if (!cpi->refresh_alt_ref_frame) { |
1219 // Decrement count down till next gf | 1223 // Decrement count down till next gf |
1220 if (rc->frames_till_gf_update_due > 0) | 1224 if (rc->frames_till_gf_update_due > 0) |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1662 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * | 1666 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * |
1663 oxcf->two_pass_vbrmax_section) / 100); | 1667 oxcf->two_pass_vbrmax_section) / 100); |
1664 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), | 1668 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), |
1665 vbr_max_bits); | 1669 vbr_max_bits); |
1666 | 1670 |
1667 vp9_rc_set_gf_interval_range(cpi, rc); | 1671 vp9_rc_set_gf_interval_range(cpi, rc); |
1668 } | 1672 } |
1669 | 1673 |
1670 #define VBR_PCT_ADJUSTMENT_LIMIT 50 | 1674 #define VBR_PCT_ADJUSTMENT_LIMIT 50 |
1671 // For VBR...adjustment to the frame target based on error from previous frames | 1675 // For VBR...adjustment to the frame target based on error from previous frames |
1672 static void vbr_rate_correction(VP9_COMP *cpi, | 1676 static void vbr_rate_correction(VP9_COMP *cpi, int *this_frame_target) { |
1673 int *this_frame_target, | 1677 RATE_CONTROL *const rc = &cpi->rc; |
1674 int64_t vbr_bits_off_target) { | 1678 int64_t vbr_bits_off_target = rc->vbr_bits_off_target; |
1675 int max_delta; | 1679 int max_delta; |
1676 double position_factor = 1.0; | 1680 double position_factor = 1.0; |
1677 | 1681 |
1678 // How far through the clip are we. | 1682 // How far through the clip are we. |
1679 // This number is used to damp the per frame rate correction. | 1683 // This number is used to damp the per frame rate correction. |
1680 // Range 0 - 1.0 | 1684 // Range 0 - 1.0 |
1681 if (cpi->twopass.total_stats.count) { | 1685 if (cpi->twopass.total_stats.count) { |
1682 position_factor = sqrt((double)cpi->common.current_video_frame / | 1686 position_factor = sqrt((double)cpi->common.current_video_frame / |
1683 cpi->twopass.total_stats.count); | 1687 cpi->twopass.total_stats.count); |
1684 } | 1688 } |
1685 max_delta = (int)(position_factor * | 1689 max_delta = (int)(position_factor * |
1686 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100)); | 1690 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100)); |
1687 | 1691 |
1688 // vbr_bits_off_target > 0 means we have extra bits to spend | 1692 // vbr_bits_off_target > 0 means we have extra bits to spend |
1689 if (vbr_bits_off_target > 0) { | 1693 if (vbr_bits_off_target > 0) { |
1690 *this_frame_target += | 1694 *this_frame_target += |
1691 (vbr_bits_off_target > max_delta) ? max_delta | 1695 (vbr_bits_off_target > max_delta) ? max_delta |
1692 : (int)vbr_bits_off_target; | 1696 : (int)vbr_bits_off_target; |
1693 } else { | 1697 } else { |
1694 *this_frame_target -= | 1698 *this_frame_target -= |
1695 (vbr_bits_off_target < -max_delta) ? max_delta | 1699 (vbr_bits_off_target < -max_delta) ? max_delta |
1696 : (int)-vbr_bits_off_target; | 1700 : (int)-vbr_bits_off_target; |
1697 } | 1701 } |
| 1702 |
| 1703 // Fast redistribution of bits arising from massive local undershoot. |
| 1704 // Dont do it for kf,arf,gf or overlay frames. |
| 1705 if (!frame_is_kf_gf_arf(cpi) && !rc->is_src_frame_alt_ref && |
| 1706 rc->vbr_bits_off_target_fast) { |
| 1707 int one_frame_bits = MAX(rc->avg_frame_bandwidth, *this_frame_target); |
| 1708 int fast_extra_bits; |
| 1709 fast_extra_bits = |
| 1710 (int)MIN(rc->vbr_bits_off_target_fast, one_frame_bits); |
| 1711 fast_extra_bits = (int)MIN(fast_extra_bits, |
| 1712 MAX(one_frame_bits / 8, rc->vbr_bits_off_target_fast / 8)); |
| 1713 *this_frame_target += (int)fast_extra_bits; |
| 1714 rc->vbr_bits_off_target_fast -= fast_extra_bits; |
| 1715 } |
1698 } | 1716 } |
1699 | 1717 |
1700 void vp9_set_target_rate(VP9_COMP *cpi) { | 1718 void vp9_set_target_rate(VP9_COMP *cpi) { |
1701 RATE_CONTROL *const rc = &cpi->rc; | 1719 RATE_CONTROL *const rc = &cpi->rc; |
1702 int target_rate = rc->base_frame_target; | 1720 int target_rate = rc->base_frame_target; |
1703 | 1721 |
1704 // Correction to rate target based on prior over or under shoot. | 1722 // Correction to rate target based on prior over or under shoot. |
1705 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) | 1723 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) |
1706 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); | 1724 vbr_rate_correction(cpi, &target_rate); |
1707 vp9_rc_set_frame_target(cpi, target_rate); | 1725 vp9_rc_set_frame_target(cpi, target_rate); |
1708 } | 1726 } |
OLD | NEW |