Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_ratectrl.c

Issue 375983002: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 // Clip the buffer level to the maximum specified buffer size. 179 // Clip the buffer level to the maximum specified buffer size.
180 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size); 180 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
181 rc->buffer_level = rc->bits_off_target; 181 rc->buffer_level = rc->bits_off_target;
182 182
183 if (cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR) { 183 if (cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR) {
184 update_layer_buffer_level(&cpi->svc, encoded_frame_size); 184 update_layer_buffer_level(&cpi->svc, encoded_frame_size);
185 } 185 }
186 } 186 }
187 187
188 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) { 188 void vp9_rc_init(const VP9EncoderConfig *oxcf, int pass, RATE_CONTROL *rc) {
189 int i;
190
189 if (pass == 0 && oxcf->rc_mode == VPX_CBR) { 191 if (pass == 0 && oxcf->rc_mode == VPX_CBR) {
190 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; 192 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q;
191 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; 193 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q;
192 } else { 194 } else {
193 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + 195 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q +
194 oxcf->best_allowed_q) / 2; 196 oxcf->best_allowed_q) / 2;
195 rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + 197 rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q +
196 oxcf->best_allowed_q) / 2; 198 oxcf->best_allowed_q) / 2;
197 } 199 }
198 200
(...skipping 21 matching lines...) Expand all
220 222
221 rc->frames_till_gf_update_due = 0; 223 rc->frames_till_gf_update_due = 0;
222 224
223 rc->ni_av_qi = oxcf->worst_allowed_q; 225 rc->ni_av_qi = oxcf->worst_allowed_q;
224 rc->ni_tot_qi = 0; 226 rc->ni_tot_qi = 0;
225 rc->ni_frames = 0; 227 rc->ni_frames = 0;
226 228
227 rc->tot_q = 0.0; 229 rc->tot_q = 0.0;
228 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q); 230 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q);
229 231
230 rc->rate_correction_factor = 1.0; 232 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) {
231 rc->key_frame_rate_correction_factor = 1.0; 233 rc->rate_correction_factors[i] = 1.0;
232 rc->gf_rate_correction_factor = 1.0; 234 }
233 } 235 }
234 236
235 int vp9_rc_drop_frame(VP9_COMP *cpi) { 237 int vp9_rc_drop_frame(VP9_COMP *cpi) {
236 const VP9EncoderConfig *oxcf = &cpi->oxcf; 238 const VP9EncoderConfig *oxcf = &cpi->oxcf;
237 RATE_CONTROL *const rc = &cpi->rc; 239 RATE_CONTROL *const rc = &cpi->rc;
238 240
239 if (!oxcf->drop_frames_water_mark) { 241 if (!oxcf->drop_frames_water_mark) {
240 return 0; 242 return 0;
241 } else { 243 } else {
242 if (rc->buffer_level < 0) { 244 if (rc->buffer_level < 0) {
(...skipping 21 matching lines...) Expand all
264 } 266 }
265 } else { 267 } else {
266 rc->decimation_count = 0; 268 rc->decimation_count = 0;
267 return 0; 269 return 0;
268 } 270 }
269 } 271 }
270 } 272 }
271 } 273 }
272 274
273 static double get_rate_correction_factor(const VP9_COMP *cpi) { 275 static double get_rate_correction_factor(const VP9_COMP *cpi) {
276 const RATE_CONTROL *const rc = &cpi->rc;
277
274 if (cpi->common.frame_type == KEY_FRAME) { 278 if (cpi->common.frame_type == KEY_FRAME) {
275 return cpi->rc.key_frame_rate_correction_factor; 279 return rc->rate_correction_factors[KF_STD];
280 } else if (cpi->pass == 2) {
281 RATE_FACTOR_LEVEL rf_lvl =
282 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
283 return rc->rate_correction_factors[rf_lvl];
276 } else { 284 } else {
277 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 285 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
278 !cpi->rc.is_src_frame_alt_ref && 286 !rc->is_src_frame_alt_ref &&
279 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) 287 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
280 return cpi->rc.gf_rate_correction_factor; 288 return rc->rate_correction_factors[GF_ARF_STD];
281 else 289 else
282 return cpi->rc.rate_correction_factor; 290 return rc->rate_correction_factors[INTER_NORMAL];
283 } 291 }
284 } 292 }
285 293
286 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { 294 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
295 RATE_CONTROL *const rc = &cpi->rc;
296
287 if (cpi->common.frame_type == KEY_FRAME) { 297 if (cpi->common.frame_type == KEY_FRAME) {
288 cpi->rc.key_frame_rate_correction_factor = factor; 298 rc->rate_correction_factors[KF_STD] = factor;
299 } else if (cpi->pass == 2) {
300 RATE_FACTOR_LEVEL rf_lvl =
301 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
302 rc->rate_correction_factors[rf_lvl] = factor;
289 } else { 303 } else {
290 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 304 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
291 !cpi->rc.is_src_frame_alt_ref && 305 !rc->is_src_frame_alt_ref &&
292 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) 306 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR))
293 cpi->rc.gf_rate_correction_factor = factor; 307 rc->rate_correction_factors[GF_ARF_STD] = factor;
294 else 308 else
295 cpi->rc.rate_correction_factor = factor; 309 rc->rate_correction_factors[INTER_NORMAL] = factor;
296 } 310 }
297 } 311 }
298 312
299 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { 313 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
300 const VP9_COMMON *const cm = &cpi->common; 314 const VP9_COMMON *const cm = &cpi->common;
301 int correction_factor = 100; 315 int correction_factor = 100;
302 double rate_correction_factor = get_rate_correction_factor(cpi); 316 double rate_correction_factor = get_rate_correction_factor(cpi);
303 double adjustment_limit; 317 double adjustment_limit;
304 318
305 int projected_size_based_on_q = 0; 319 int projected_size_based_on_q = 0;
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 const VP9_COMMON *const cm = &cpi->common; 635 const VP9_COMMON *const cm = &cpi->common;
622 const RATE_CONTROL *const rc = &cpi->rc; 636 const RATE_CONTROL *const rc = &cpi->rc;
623 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 637 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
624 const int cq_level = get_active_cq_level(rc, oxcf); 638 const int cq_level = get_active_cq_level(rc, oxcf);
625 int active_best_quality; 639 int active_best_quality;
626 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi); 640 int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
627 int q; 641 int q;
628 642
629 if (frame_is_intra_only(cm)) { 643 if (frame_is_intra_only(cm)) {
630 active_best_quality = rc->best_quality; 644 active_best_quality = rc->best_quality;
631 #if !CONFIG_MULTIPLE_ARF 645
632 // Handle the special case for key frames forced when we have75 reached 646 // Handle the special case for key frames forced when we have reached
633 // the maximum key frame interval. Here force the Q to a range 647 // the maximum key frame interval. Here force the Q to a range
634 // based on the ambient Q to reduce the risk of popping. 648 // based on the ambient Q to reduce the risk of popping.
635 if (rc->this_key_frame_forced) { 649 if (rc->this_key_frame_forced) {
636 int qindex = rc->last_boosted_qindex; 650 int qindex = rc->last_boosted_qindex;
637 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 651 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
638 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 652 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
639 last_boosted_q * 0.75); 653 last_boosted_q * 0.75);
640 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 654 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
641 } else { 655 } else {
642 // not first frame of one pass and kf_boost is set 656 // not first frame of one pass and kf_boost is set
(...skipping 10 matching lines...) Expand all
653 if ((cm->width * cm->height) <= (352 * 288)) { 667 if ((cm->width * cm->height) <= (352 * 288)) {
654 q_adj_factor -= 0.25; 668 q_adj_factor -= 0.25;
655 } 669 }
656 670
657 // Convert the adjustment factor to a qindex delta 671 // Convert the adjustment factor to a qindex delta
658 // on active_best_quality. 672 // on active_best_quality.
659 q_val = vp9_convert_qindex_to_q(active_best_quality); 673 q_val = vp9_convert_qindex_to_q(active_best_quality);
660 active_best_quality += vp9_compute_qdelta(rc, q_val, 674 active_best_quality += vp9_compute_qdelta(rc, q_val,
661 q_val * q_adj_factor); 675 q_val * q_adj_factor);
662 } 676 }
663 #else
664 double current_q;
665 // Force the KF quantizer to be 30% of the active_worst_quality.
666 current_q = vp9_convert_qindex_to_q(active_worst_quality);
667 active_best_quality = active_worst_quality
668 + vp9_compute_qdelta(rc, current_q, current_q * 0.3);
669 #endif
670 } else if (!rc->is_src_frame_alt_ref && 677 } else if (!rc->is_src_frame_alt_ref &&
671 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 678 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
672 // Use the lower of active_worst_quality and recent 679 // Use the lower of active_worst_quality and recent
673 // average Q as basis for GF/ARF best Q limit unless last frame was 680 // average Q as basis for GF/ARF best Q limit unless last frame was
674 // a key frame. 681 // a key frame.
675 if (rc->frames_since_key > 1 && 682 if (rc->frames_since_key > 1 &&
676 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 683 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
677 q = rc->avg_frame_qindex[INTER_FRAME]; 684 q = rc->avg_frame_qindex[INTER_FRAME];
678 } else { 685 } else {
679 q = rc->avg_frame_qindex[KEY_FRAME]; 686 q = rc->avg_frame_qindex[KEY_FRAME];
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 768 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
762 active_best_quality, active_worst_quality); 769 active_best_quality, active_worst_quality);
763 if (q > *top_index) { 770 if (q > *top_index) {
764 // Special case when we are targeting the max allowed rate 771 // Special case when we are targeting the max allowed rate
765 if (rc->this_frame_target >= rc->max_frame_bandwidth) 772 if (rc->this_frame_target >= rc->max_frame_bandwidth)
766 *top_index = q; 773 *top_index = q;
767 else 774 else
768 q = *top_index; 775 q = *top_index;
769 } 776 }
770 } 777 }
771 #if CONFIG_MULTIPLE_ARF
772 // Force the quantizer determined by the coding order pattern.
773 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
774 cpi->oxcf.rc_mode != VPX_Q) {
775 double new_q;
776 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
777 int level = cpi->this_frame_weight;
778 assert(level >= 0);
779 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
780 q = active_worst_quality +
781 vp9_compute_qdelta(rc, current_q, new_q);
782 778
783 *bottom_index = q;
784 *top_index = q;
785 printf("frame:%d q:%d\n", cm->current_video_frame, q);
786 }
787 #endif
788 assert(*top_index <= rc->worst_quality && 779 assert(*top_index <= rc->worst_quality &&
789 *top_index >= rc->best_quality); 780 *top_index >= rc->best_quality);
790 assert(*bottom_index <= rc->worst_quality && 781 assert(*bottom_index <= rc->worst_quality &&
791 *bottom_index >= rc->best_quality); 782 *bottom_index >= rc->best_quality);
792 assert(q <= rc->worst_quality && q >= rc->best_quality); 783 assert(q <= rc->worst_quality && q >= rc->best_quality);
793 return q; 784 return q;
794 } 785 }
795 786
796 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, 787 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
797 int *bottom_index, 788 int *bottom_index,
798 int *top_index) { 789 int *top_index) {
799 const VP9_COMMON *const cm = &cpi->common; 790 const VP9_COMMON *const cm = &cpi->common;
800 const RATE_CONTROL *const rc = &cpi->rc; 791 const RATE_CONTROL *const rc = &cpi->rc;
801 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 792 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
802 const int cq_level = get_active_cq_level(rc, oxcf); 793 const int cq_level = get_active_cq_level(rc, oxcf);
803 int active_best_quality; 794 int active_best_quality;
804 int active_worst_quality = cpi->twopass.active_worst_quality; 795 int active_worst_quality = cpi->twopass.active_worst_quality;
805 int q; 796 int q;
806 797
807 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { 798 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) {
808 #if !CONFIG_MULTIPLE_ARF
809 // Handle the special case for key frames forced when we have75 reached 799 // Handle the special case for key frames forced when we have75 reached
810 // the maximum key frame interval. Here force the Q to a range 800 // the maximum key frame interval. Here force the Q to a range
811 // based on the ambient Q to reduce the risk of popping. 801 // based on the ambient Q to reduce the risk of popping.
812 if (rc->this_key_frame_forced) { 802 if (rc->this_key_frame_forced) {
813 int qindex = rc->last_boosted_qindex; 803 int qindex = rc->last_boosted_qindex;
814 double last_boosted_q = vp9_convert_qindex_to_q(qindex); 804 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
815 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q, 805 int delta_qindex = vp9_compute_qdelta(rc, last_boosted_q,
816 last_boosted_q * 0.75); 806 last_boosted_q * 0.75);
817 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality); 807 active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
818 } else { 808 } else {
(...skipping 14 matching lines...) Expand all
833 823
834 // Make a further adjustment based on the kf zero motion measure. 824 // Make a further adjustment based on the kf zero motion measure.
835 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct); 825 q_adj_factor += 0.05 - (0.001 * (double)cpi->twopass.kf_zeromotion_pct);
836 826
837 // Convert the adjustment factor to a qindex delta 827 // Convert the adjustment factor to a qindex delta
838 // on active_best_quality. 828 // on active_best_quality.
839 q_val = vp9_convert_qindex_to_q(active_best_quality); 829 q_val = vp9_convert_qindex_to_q(active_best_quality);
840 active_best_quality += vp9_compute_qdelta(rc, q_val, 830 active_best_quality += vp9_compute_qdelta(rc, q_val,
841 q_val * q_adj_factor); 831 q_val * q_adj_factor);
842 } 832 }
843 #else
844 double current_q;
845 // Force the KF quantizer to be 30% of the active_worst_quality.
846 current_q = vp9_convert_qindex_to_q(active_worst_quality);
847 active_best_quality = active_worst_quality
848 + vp9_compute_qdelta(rc, current_q, current_q * 0.3);
849 #endif
850 } else if (!rc->is_src_frame_alt_ref && 833 } else if (!rc->is_src_frame_alt_ref &&
851 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 834 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
852 // Use the lower of active_worst_quality and recent 835 // Use the lower of active_worst_quality and recent
853 // average Q as basis for GF/ARF best Q limit unless last frame was 836 // average Q as basis for GF/ARF best Q limit unless last frame was
854 // a key frame. 837 // a key frame.
855 if (rc->frames_since_key > 1 && 838 if (rc->frames_since_key > 1 &&
856 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { 839 rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
857 q = rc->avg_frame_qindex[INTER_FRAME]; 840 q = rc->avg_frame_qindex[INTER_FRAME];
858 } else { 841 } else {
859 q = active_worst_quality; 842 q = active_worst_quality;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
902 // Clip the active best and worst quality values to limits. 885 // Clip the active best and worst quality values to limits.
903 active_best_quality = clamp(active_best_quality, 886 active_best_quality = clamp(active_best_quality,
904 rc->best_quality, rc->worst_quality); 887 rc->best_quality, rc->worst_quality);
905 active_worst_quality = clamp(active_worst_quality, 888 active_worst_quality = clamp(active_worst_quality,
906 active_best_quality, rc->worst_quality); 889 active_best_quality, rc->worst_quality);
907 890
908 *top_index = active_worst_quality; 891 *top_index = active_worst_quality;
909 *bottom_index = active_best_quality; 892 *bottom_index = active_best_quality;
910 893
911 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY 894 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
895 vp9_clear_system_state();
912 { 896 {
913 int qdelta = 0; 897 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
914 vp9_clear_system_state(); 898 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = {
915 899 1.00, // INTER_NORMAL
916 // Limit Q range for the adaptive loop. 900 1.00, // INTER_HIGH
917 if ((cm->frame_type == KEY_FRAME || vp9_is_upper_layer_key_frame(cpi)) && 901 1.50, // GF_ARF_LOW
918 !rc->this_key_frame_forced) { 902 1.75, // GF_ARF_STD
919 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 903 2.00, // KF_STD
920 active_worst_quality, 2.0); 904 };
921 } else if (!rc->is_src_frame_alt_ref && 905 const double rate_factor =
922 (oxcf->rc_mode != VPX_CBR) && 906 rate_factor_deltas[gf_group->rf_level[gf_group->index]];
923 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 907 int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type,
924 qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, 908 active_worst_quality, rate_factor);
925 active_worst_quality, 1.75);
926 }
927 *top_index = active_worst_quality + qdelta; 909 *top_index = active_worst_quality + qdelta;
928 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index; 910 *top_index = (*top_index > *bottom_index) ? *top_index : *bottom_index;
929 } 911 }
930 #endif 912 #endif
931 913
932 if (oxcf->rc_mode == VPX_Q) { 914 if (oxcf->rc_mode == VPX_Q) {
933 q = active_best_quality; 915 q = active_best_quality;
934 // Special case code to try and match quality with forced key frames. 916 // Special case code to try and match quality with forced key frames.
935 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) { 917 } else if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced) {
936 q = rc->last_boosted_qindex; 918 q = rc->last_boosted_qindex;
937 } else { 919 } else {
938 q = vp9_rc_regulate_q(cpi, rc->this_frame_target, 920 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
939 active_best_quality, active_worst_quality); 921 active_best_quality, active_worst_quality);
940 if (q > *top_index) { 922 if (q > *top_index) {
941 // Special case when we are targeting the max allowed rate. 923 // Special case when we are targeting the max allowed rate.
942 if (rc->this_frame_target >= rc->max_frame_bandwidth) 924 if (rc->this_frame_target >= rc->max_frame_bandwidth)
943 *top_index = q; 925 *top_index = q;
944 else 926 else
945 q = *top_index; 927 q = *top_index;
946 } 928 }
947 } 929 }
948 #if CONFIG_MULTIPLE_ARF
949 // Force the quantizer determined by the coding order pattern.
950 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
951 cpi->oxcf.rc_mode != VPX_Q) {
952 double new_q;
953 double current_q = vp9_convert_qindex_to_q(active_worst_quality);
954 int level = cpi->this_frame_weight;
955 assert(level >= 0);
956 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
957 q = active_worst_quality +
958 vp9_compute_qdelta(rc, current_q, new_q);
959 930
960 *bottom_index = q;
961 *top_index = q;
962 printf("frame:%d q:%d\n", cm->current_video_frame, q);
963 }
964 #endif
965 assert(*top_index <= rc->worst_quality && 931 assert(*top_index <= rc->worst_quality &&
966 *top_index >= rc->best_quality); 932 *top_index >= rc->best_quality);
967 assert(*bottom_index <= rc->worst_quality && 933 assert(*bottom_index <= rc->worst_quality &&
968 *bottom_index >= rc->best_quality); 934 *bottom_index >= rc->best_quality);
969 assert(q <= rc->worst_quality && q >= rc->best_quality); 935 assert(q <= rc->worst_quality && q >= rc->best_quality);
970 return q; 936 return q;
971 } 937 }
972 938
973 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi, 939 int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
974 int *bottom_index, int *top_index) { 940 int *bottom_index, int *top_index) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 // Target rate per SB64 (including partial SB64s. 985 // Target rate per SB64 (including partial SB64s.
1020 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / 986 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) /
1021 (cm->width * cm->height); 987 (cm->width * cm->height);
1022 } 988 }
1023 989
1024 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { 990 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
1025 // this frame refreshes means next frames don't unless specified by user 991 // this frame refreshes means next frames don't unless specified by user
1026 RATE_CONTROL *const rc = &cpi->rc; 992 RATE_CONTROL *const rc = &cpi->rc;
1027 rc->frames_since_golden = 0; 993 rc->frames_since_golden = 0;
1028 994
1029 #if CONFIG_MULTIPLE_ARF 995 // Mark the alt ref as done (setting to 0 means no further alt refs pending).
1030 if (!cpi->multi_arf_enabled) 996 rc->source_alt_ref_pending = 0;
1031 #endif
1032 // Clear the alternate reference update pending flag.
1033 rc->source_alt_ref_pending = 0;
1034 997
1035 // Set the alternate reference frame active flag 998 // Set the alternate reference frame active flag
1036 rc->source_alt_ref_active = 1; 999 rc->source_alt_ref_active = 1;
1037 } 1000 }
1038 1001
1039 static void update_golden_frame_stats(VP9_COMP *cpi) { 1002 static void update_golden_frame_stats(VP9_COMP *cpi) {
1040 RATE_CONTROL *const rc = &cpi->rc; 1003 RATE_CONTROL *const rc = &cpi->rc;
1041 1004
1042 // Update the Golden frame usage counts. 1005 // Update the Golden frame usage counts.
1043 if (cpi->refresh_golden_frame) { 1006 if (cpi->refresh_golden_frame) {
1044 // this frame refreshes means next frames don't unless specified by user 1007 // this frame refreshes means next frames don't unless specified by user
1045 rc->frames_since_golden = 0; 1008 rc->frames_since_golden = 0;
1046 1009
1047 if (!rc->source_alt_ref_pending) 1010 if (cpi->pass == 2) {
1011 if (!rc->source_alt_ref_pending &&
1012 cpi->twopass.gf_group.rf_level[0] == GF_ARF_STD)
1048 rc->source_alt_ref_active = 0; 1013 rc->source_alt_ref_active = 0;
1014 } else if (!rc->source_alt_ref_pending) {
1015 rc->source_alt_ref_active = 0;
1016 }
1049 1017
1050 // Decrement count down till next gf 1018 // Decrement count down till next gf
1051 if (rc->frames_till_gf_update_due > 0) 1019 if (rc->frames_till_gf_update_due > 0)
1052 rc->frames_till_gf_update_due--; 1020 rc->frames_till_gf_update_due--;
1053 1021
1054 } else if (!cpi->refresh_alt_ref_frame) { 1022 } else if (!cpi->refresh_alt_ref_frame) {
1055 // Decrement count down till next gf 1023 // Decrement count down till next gf
1056 if (rc->frames_till_gf_update_due > 0) 1024 if (rc->frames_till_gf_update_due > 0)
1057 rc->frames_till_gf_update_due--; 1025 rc->frames_till_gf_update_due--;
1058 1026
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1381 return target_index - qindex; 1349 return target_index - qindex;
1382 } 1350 }
1383 1351
1384 void vp9_rc_set_gf_max_interval(const VP9EncoderConfig *const oxcf, 1352 void vp9_rc_set_gf_max_interval(const VP9EncoderConfig *const oxcf,
1385 RATE_CONTROL *const rc) { 1353 RATE_CONTROL *const rc) {
1386 // Set Maximum gf/arf interval 1354 // Set Maximum gf/arf interval
1387 rc->max_gf_interval = 16; 1355 rc->max_gf_interval = 16;
1388 1356
1389 // Extended interval for genuinely static scenes 1357 // Extended interval for genuinely static scenes
1390 rc->static_scene_max_gf_interval = oxcf->key_freq >> 1; 1358 rc->static_scene_max_gf_interval = oxcf->key_freq >> 1;
1359 if (rc->static_scene_max_gf_interval > (MAX_LAG_BUFFERS * 2))
1360 rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2;
1391 1361
1392 if (is_altref_enabled(oxcf)) { 1362 if (is_altref_enabled(oxcf)) {
1393 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) 1363 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1)
1394 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; 1364 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1;
1395 } 1365 }
1396 1366
1397 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) 1367 if (rc->max_gf_interval > rc->static_scene_max_gf_interval)
1398 rc->max_gf_interval = rc->static_scene_max_gf_interval; 1368 rc->max_gf_interval = rc->static_scene_max_gf_interval;
1399 } 1369 }
1400 1370
(...skipping 16 matching lines...) Expand all
1417 // a very high rate is given on the command line or the the rate cannnot 1387 // a very high rate is given on the command line or the the rate cannnot
1418 // be acheived because of a user specificed max q (e.g. when the user 1388 // be acheived because of a user specificed max q (e.g. when the user
1419 // specifies lossless encode. 1389 // specifies lossless encode.
1420 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * 1390 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1421 oxcf->two_pass_vbrmax_section) / 100); 1391 oxcf->two_pass_vbrmax_section) / 100);
1422 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 1392 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
1423 vbr_max_bits); 1393 vbr_max_bits);
1424 1394
1425 vp9_rc_set_gf_max_interval(oxcf, rc); 1395 vp9_rc_set_gf_max_interval(oxcf, rc);
1426 } 1396 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ratectrl.h ('k') | source/libvpx/vp9/encoder/vp9_rd.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698