| 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 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 rcf = rc->rate_correction_factors[rf_lvl]; | 370 rcf = rc->rate_correction_factors[rf_lvl]; |
| 371 } else { | 371 } else { |
| 372 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 372 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 373 !rc->is_src_frame_alt_ref && !cpi->use_svc && | 373 !rc->is_src_frame_alt_ref && !cpi->use_svc && |
| 374 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) | 374 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) |
| 375 rcf = rc->rate_correction_factors[GF_ARF_STD]; | 375 rcf = rc->rate_correction_factors[GF_ARF_STD]; |
| 376 else | 376 else |
| 377 rcf = rc->rate_correction_factors[INTER_NORMAL]; | 377 rcf = rc->rate_correction_factors[INTER_NORMAL]; |
| 378 } | 378 } |
| 379 rcf *= rcf_mult[rc->frame_size_selector]; | 379 rcf *= rcf_mult[rc->frame_size_selector]; |
| 380 return rcf > MAX_BPB_FACTOR ? MAX_BPB_FACTOR : rcf; | 380 return fclamp(rcf, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
| 381 } | 381 } |
| 382 | 382 |
| 383 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { | 383 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { |
| 384 RATE_CONTROL *const rc = &cpi->rc; | 384 RATE_CONTROL *const rc = &cpi->rc; |
| 385 | 385 |
| 386 // Normalize RCF to account for the size-dependent scaling factor. | 386 // Normalize RCF to account for the size-dependent scaling factor. |
| 387 factor /= rcf_mult[cpi->rc.frame_size_selector]; | 387 factor /= rcf_mult[cpi->rc.frame_size_selector]; |
| 388 | 388 |
| 389 factor = fclamp(factor, MIN_BPB_FACTOR, MAX_BPB_FACTOR); |
| 390 |
| 389 if (cpi->common.frame_type == KEY_FRAME) { | 391 if (cpi->common.frame_type == KEY_FRAME) { |
| 390 rc->rate_correction_factors[KF_STD] = factor; | 392 rc->rate_correction_factors[KF_STD] = factor; |
| 391 } else if (cpi->oxcf.pass == 2) { | 393 } else if (cpi->oxcf.pass == 2) { |
| 392 RATE_FACTOR_LEVEL rf_lvl = | 394 RATE_FACTOR_LEVEL rf_lvl = |
| 393 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 395 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
| 394 rc->rate_correction_factors[rf_lvl] = factor; | 396 rc->rate_correction_factors[rf_lvl] = factor; |
| 395 } else { | 397 } else { |
| 396 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 398 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
| 397 !rc->is_src_frame_alt_ref && !cpi->use_svc && | 399 !rc->is_src_frame_alt_ref && !cpi->use_svc && |
| 398 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) | 400 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 assert(*top_index <= rc->worst_quality && | 749 assert(*top_index <= rc->worst_quality && |
| 748 *top_index >= rc->best_quality); | 750 *top_index >= rc->best_quality); |
| 749 assert(*bottom_index <= rc->worst_quality && | 751 assert(*bottom_index <= rc->worst_quality && |
| 750 *bottom_index >= rc->best_quality); | 752 *bottom_index >= rc->best_quality); |
| 751 assert(q <= rc->worst_quality && q >= rc->best_quality); | 753 assert(q <= rc->worst_quality && q >= rc->best_quality); |
| 752 return q; | 754 return q; |
| 753 } | 755 } |
| 754 | 756 |
| 755 static int get_active_cq_level(const RATE_CONTROL *rc, | 757 static int get_active_cq_level(const RATE_CONTROL *rc, |
| 756 const VP9EncoderConfig *const oxcf) { | 758 const VP9EncoderConfig *const oxcf) { |
| 757 static const double cq_adjust_threshold = 0.5; | 759 static const double cq_adjust_threshold = 0.1; |
| 758 int active_cq_level = oxcf->cq_level; | 760 int active_cq_level = oxcf->cq_level; |
| 759 if (oxcf->rc_mode == VPX_CQ && | 761 if (oxcf->rc_mode == VPX_CQ && |
| 760 rc->total_target_bits > 0) { | 762 rc->total_target_bits > 0) { |
| 761 const double x = (double)rc->total_actual_bits / rc->total_target_bits; | 763 const double x = (double)rc->total_actual_bits / rc->total_target_bits; |
| 762 if (x < cq_adjust_threshold) { | 764 if (x < cq_adjust_threshold) { |
| 763 active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); | 765 active_cq_level = (int)(active_cq_level * x / cq_adjust_threshold); |
| 764 } | 766 } |
| 765 } | 767 } |
| 766 return active_cq_level; | 768 return active_cq_level; |
| 767 } | 769 } |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 // q to fall below the cq level. | 1045 // q to fall below the cq level. |
| 1044 if ((oxcf->rc_mode == VPX_CQ) && | 1046 if ((oxcf->rc_mode == VPX_CQ) && |
| 1045 (active_best_quality < cq_level)) { | 1047 (active_best_quality < cq_level)) { |
| 1046 active_best_quality = cq_level; | 1048 active_best_quality = cq_level; |
| 1047 } | 1049 } |
| 1048 } | 1050 } |
| 1049 } | 1051 } |
| 1050 | 1052 |
| 1051 // Extension to max or min Q if undershoot or overshoot is outside | 1053 // Extension to max or min Q if undershoot or overshoot is outside |
| 1052 // the permitted range. | 1054 // the permitted range. |
| 1053 if ((cpi->oxcf.rc_mode == VPX_VBR) && | 1055 if ((cpi->oxcf.rc_mode != VPX_Q) && |
| 1054 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { | 1056 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { |
| 1055 if (frame_is_intra_only(cm) || | 1057 if (frame_is_intra_only(cm) || |
| 1056 (!rc->is_src_frame_alt_ref && | 1058 (!rc->is_src_frame_alt_ref && |
| 1057 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { | 1059 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
| 1058 active_best_quality -= cpi->twopass.extend_minq; | 1060 active_best_quality -= cpi->twopass.extend_minq; |
| 1059 active_worst_quality += (cpi->twopass.extend_maxq / 2); | 1061 active_worst_quality += (cpi->twopass.extend_maxq / 2); |
| 1060 } else { | 1062 } else { |
| 1061 active_best_quality -= cpi->twopass.extend_minq / 2; | 1063 active_best_quality -= cpi->twopass.extend_minq / 2; |
| 1062 active_worst_quality += cpi->twopass.extend_maxq; | 1064 active_worst_quality += cpi->twopass.extend_maxq; |
| 1063 } | 1065 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 } | 1224 } |
| 1223 } | 1225 } |
| 1224 | 1226 |
| 1225 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { | 1227 void vp9_rc_postencode_update(VP9_COMP *cpi, uint64_t bytes_used) { |
| 1226 const VP9_COMMON *const cm = &cpi->common; | 1228 const VP9_COMMON *const cm = &cpi->common; |
| 1227 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1229 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1228 RATE_CONTROL *const rc = &cpi->rc; | 1230 RATE_CONTROL *const rc = &cpi->rc; |
| 1229 const int qindex = cm->base_qindex; | 1231 const int qindex = cm->base_qindex; |
| 1230 | 1232 |
| 1231 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { | 1233 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { |
| 1232 vp9_cyclic_refresh_update_actual_count(cpi); | 1234 vp9_cyclic_refresh_postencode(cpi); |
| 1233 } | 1235 } |
| 1234 | 1236 |
| 1235 // Update rate control heuristics | 1237 // Update rate control heuristics |
| 1236 rc->projected_frame_size = (int)(bytes_used << 3); | 1238 rc->projected_frame_size = (int)(bytes_used << 3); |
| 1237 | 1239 |
| 1238 // Post encode loop adjustment of Q prediction. | 1240 // Post encode loop adjustment of Q prediction. |
| 1239 vp9_rc_update_rate_correction_factors(cpi); | 1241 vp9_rc_update_rate_correction_factors(cpi); |
| 1240 | 1242 |
| 1241 // Keep a record of last Q and ambient average Q. | 1243 // Keep a record of last Q and ambient average Q. |
| 1242 if (cm->frame_type == KEY_FRAME) { | 1244 if (cm->frame_type == KEY_FRAME) { |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1528 cm->frame_type = KEY_FRAME; | 1530 cm->frame_type = KEY_FRAME; |
| 1529 rc->this_key_frame_forced = cm->current_video_frame != 0 && | 1531 rc->this_key_frame_forced = cm->current_video_frame != 0 && |
| 1530 rc->frames_to_key == 0; | 1532 rc->frames_to_key == 0; |
| 1531 rc->frames_to_key = cpi->oxcf.key_freq; | 1533 rc->frames_to_key = cpi->oxcf.key_freq; |
| 1532 rc->kf_boost = DEFAULT_KF_BOOST; | 1534 rc->kf_boost = DEFAULT_KF_BOOST; |
| 1533 rc->source_alt_ref_active = 0; | 1535 rc->source_alt_ref_active = 0; |
| 1534 } else { | 1536 } else { |
| 1535 cm->frame_type = INTER_FRAME; | 1537 cm->frame_type = INTER_FRAME; |
| 1536 } | 1538 } |
| 1537 if (rc->frames_till_gf_update_due == 0) { | 1539 if (rc->frames_till_gf_update_due == 0) { |
| 1538 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; | 1540 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) |
| 1541 vp9_cyclic_refresh_set_golden_update(cpi); |
| 1542 else |
| 1543 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
| 1539 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 1544 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
| 1540 // NOTE: frames_till_gf_update_due must be <= frames_to_key. | 1545 // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
| 1541 if (rc->frames_till_gf_update_due > rc->frames_to_key) | 1546 if (rc->frames_till_gf_update_due > rc->frames_to_key) |
| 1542 rc->frames_till_gf_update_due = rc->frames_to_key; | 1547 rc->frames_till_gf_update_due = rc->frames_to_key; |
| 1543 cpi->refresh_golden_frame = 1; | 1548 cpi->refresh_golden_frame = 1; |
| 1544 rc->gfu_boost = DEFAULT_GF_BOOST; | 1549 rc->gfu_boost = DEFAULT_GF_BOOST; |
| 1545 } | 1550 } |
| 1546 | 1551 |
| 1547 // Any update/change of global cyclic refresh parameters (amount/delta-qp) | 1552 // Any update/change of global cyclic refresh parameters (amount/delta-qp) |
| 1548 // should be done here, before the frame qp is selected. | 1553 // should be done here, before the frame qp is selected. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1588 | 1593 |
| 1589 // Look up the current projected bits per block for the base index | 1594 // Look up the current projected bits per block for the base index |
| 1590 const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0, | 1595 const int base_bits_per_mb = vp9_rc_bits_per_mb(frame_type, qindex, 1.0, |
| 1591 bit_depth); | 1596 bit_depth); |
| 1592 | 1597 |
| 1593 // Find the target bits per mb based on the base value and given ratio. | 1598 // Find the target bits per mb based on the base value and given ratio. |
| 1594 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); | 1599 const int target_bits_per_mb = (int)(rate_target_ratio * base_bits_per_mb); |
| 1595 | 1600 |
| 1596 // Convert the q target to an index | 1601 // Convert the q target to an index |
| 1597 for (i = rc->best_quality; i < rc->worst_quality; ++i) { | 1602 for (i = rc->best_quality; i < rc->worst_quality; ++i) { |
| 1598 target_index = i; | 1603 if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <= |
| 1599 if (vp9_rc_bits_per_mb(frame_type, i, 1.0, bit_depth) <= target_bits_per_mb) | 1604 target_bits_per_mb) { |
| 1605 target_index = i; |
| 1600 break; | 1606 break; |
| 1607 } |
| 1601 } | 1608 } |
| 1602 | |
| 1603 return target_index - qindex; | 1609 return target_index - qindex; |
| 1604 } | 1610 } |
| 1605 | 1611 |
| 1606 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi, | 1612 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi, |
| 1607 RATE_CONTROL *const rc) { | 1613 RATE_CONTROL *const rc) { |
| 1608 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1614 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1609 // Set Maximum gf/arf interval | 1615 // Set Maximum gf/arf interval |
| 1610 rc->max_gf_interval = 16; | 1616 rc->max_gf_interval = 16; |
| 1611 | 1617 |
| 1612 // Extended interval for genuinely static scenes | 1618 // Extended interval for genuinely static scenes |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1676 (vbr_bits_off_target < -max_delta) ? max_delta | 1682 (vbr_bits_off_target < -max_delta) ? max_delta |
| 1677 : (int)-vbr_bits_off_target; | 1683 : (int)-vbr_bits_off_target; |
| 1678 } | 1684 } |
| 1679 } | 1685 } |
| 1680 | 1686 |
| 1681 void vp9_set_target_rate(VP9_COMP *cpi) { | 1687 void vp9_set_target_rate(VP9_COMP *cpi) { |
| 1682 RATE_CONTROL *const rc = &cpi->rc; | 1688 RATE_CONTROL *const rc = &cpi->rc; |
| 1683 int target_rate = rc->base_frame_target; | 1689 int target_rate = rc->base_frame_target; |
| 1684 | 1690 |
| 1685 // Correction to rate target based on prior over or under shoot. | 1691 // Correction to rate target based on prior over or under shoot. |
| 1686 if (cpi->oxcf.rc_mode == VPX_VBR) | 1692 if (cpi->oxcf.rc_mode == VPX_VBR || cpi->oxcf.rc_mode == VPX_CQ) |
| 1687 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); | 1693 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); |
| 1688 vp9_rc_set_frame_target(cpi, target_rate); | 1694 vp9_rc_set_frame_target(cpi, target_rate); |
| 1689 } | 1695 } |
| OLD | NEW |