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 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 } else { | 353 } else { |
354 rc->decimation_count = 0; | 354 rc->decimation_count = 0; |
355 return 0; | 355 return 0; |
356 } | 356 } |
357 } | 357 } |
358 } | 358 } |
359 } | 359 } |
360 | 360 |
361 static double get_rate_correction_factor(const VP9_COMP *cpi) { | 361 static double get_rate_correction_factor(const VP9_COMP *cpi) { |
362 const RATE_CONTROL *const rc = &cpi->rc; | 362 const RATE_CONTROL *const rc = &cpi->rc; |
| 363 double rcf; |
363 | 364 |
364 if (cpi->common.frame_type == KEY_FRAME) { | 365 if (cpi->common.frame_type == KEY_FRAME) { |
365 return rc->rate_correction_factors[KF_STD]; | 366 rcf = rc->rate_correction_factors[KF_STD]; |
366 } else if (cpi->oxcf.pass == 2) { | 367 } else if (cpi->oxcf.pass == 2) { |
367 RATE_FACTOR_LEVEL rf_lvl = | 368 RATE_FACTOR_LEVEL rf_lvl = |
368 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 369 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
369 return rc->rate_correction_factors[rf_lvl]; | 370 rcf = rc->rate_correction_factors[rf_lvl]; |
370 } else { | 371 } else { |
371 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 372 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
372 !rc->is_src_frame_alt_ref && !cpi->use_svc && | 373 !rc->is_src_frame_alt_ref && !cpi->use_svc && |
373 (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)) |
374 return rc->rate_correction_factors[GF_ARF_STD]; | 375 rcf = rc->rate_correction_factors[GF_ARF_STD]; |
375 else | 376 else |
376 return rc->rate_correction_factors[INTER_NORMAL]; | 377 rcf = rc->rate_correction_factors[INTER_NORMAL]; |
377 } | 378 } |
| 379 rcf *= rcf_mult[rc->frame_size_selector]; |
| 380 return rcf > MAX_BPB_FACTOR ? MAX_BPB_FACTOR : rcf; |
378 } | 381 } |
379 | 382 |
380 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { | 383 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { |
381 RATE_CONTROL *const rc = &cpi->rc; | 384 RATE_CONTROL *const rc = &cpi->rc; |
382 | 385 |
| 386 // Normalize RCF to account for the size-dependent scaling factor. |
| 387 factor /= rcf_mult[cpi->rc.frame_size_selector]; |
| 388 |
383 if (cpi->common.frame_type == KEY_FRAME) { | 389 if (cpi->common.frame_type == KEY_FRAME) { |
384 rc->rate_correction_factors[KF_STD] = factor; | 390 rc->rate_correction_factors[KF_STD] = factor; |
385 } else if (cpi->oxcf.pass == 2) { | 391 } else if (cpi->oxcf.pass == 2) { |
386 RATE_FACTOR_LEVEL rf_lvl = | 392 RATE_FACTOR_LEVEL rf_lvl = |
387 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; | 393 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; |
388 rc->rate_correction_factors[rf_lvl] = factor; | 394 rc->rate_correction_factors[rf_lvl] = factor; |
389 } else { | 395 } else { |
390 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && | 396 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && |
391 !rc->is_src_frame_alt_ref && !cpi->use_svc && | 397 !rc->is_src_frame_alt_ref && !cpi->use_svc && |
392 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) | 398 (cpi->oxcf.rc_mode != VPX_CBR || cpi->oxcf.gf_cbr_boost_pct > 20)) |
393 rc->rate_correction_factors[GF_ARF_STD] = factor; | 399 rc->rate_correction_factors[GF_ARF_STD] = factor; |
394 else | 400 else |
395 rc->rate_correction_factors[INTER_NORMAL] = factor; | 401 rc->rate_correction_factors[INTER_NORMAL] = factor; |
396 } | 402 } |
397 } | 403 } |
398 | 404 |
399 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { | 405 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi) { |
400 const VP9_COMMON *const cm = &cpi->common; | 406 const VP9_COMMON *const cm = &cpi->common; |
401 int correction_factor = 100; | 407 int correction_factor = 100; |
402 double rate_correction_factor = get_rate_correction_factor(cpi); | 408 double rate_correction_factor = get_rate_correction_factor(cpi); |
403 double adjustment_limit; | 409 double adjustment_limit; |
404 | 410 |
405 int projected_size_based_on_q = 0; | 411 int projected_size_based_on_q = 0; |
406 | 412 |
407 // Do not update the rate factors for arf overlay frames. | 413 // Do not update the rate factors for arf overlay frames. |
408 if (cpi->rc.is_src_frame_alt_ref) | 414 if (cpi->rc.is_src_frame_alt_ref) |
409 return; | 415 return; |
(...skipping 14 matching lines...) Expand all Loading... |
424 rate_correction_factor, | 430 rate_correction_factor, |
425 cm->bit_depth); | 431 cm->bit_depth); |
426 } | 432 } |
427 // Work out a size correction factor. | 433 // Work out a size correction factor. |
428 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) | 434 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) |
429 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / | 435 correction_factor = (int)((100 * (int64_t)cpi->rc.projected_frame_size) / |
430 projected_size_based_on_q); | 436 projected_size_based_on_q); |
431 | 437 |
432 // More heavily damped adjustment used if we have been oscillating either side | 438 // More heavily damped adjustment used if we have been oscillating either side |
433 // of target. | 439 // of target. |
434 switch (damp_var) { | 440 adjustment_limit = 0.25 + |
435 case 0: | 441 0.5 * MIN(1, fabs(log10(0.01 * correction_factor))); |
436 adjustment_limit = 0.75; | |
437 break; | |
438 case 1: | |
439 adjustment_limit = 0.25 + | |
440 0.5 * MIN(1, fabs(log10(0.01 * correction_factor))); | |
441 break; | |
442 case 2: | |
443 default: | |
444 adjustment_limit = 0.25; | |
445 break; | |
446 } | |
447 | 442 |
448 cpi->rc.q_2_frame = cpi->rc.q_1_frame; | 443 cpi->rc.q_2_frame = cpi->rc.q_1_frame; |
449 cpi->rc.q_1_frame = cm->base_qindex; | 444 cpi->rc.q_1_frame = cm->base_qindex; |
450 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; | 445 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame; |
451 if (correction_factor > 110) | 446 if (correction_factor > 110) |
452 cpi->rc.rc_1_frame = -1; | 447 cpi->rc.rc_1_frame = -1; |
453 else if (correction_factor < 90) | 448 else if (correction_factor < 90) |
454 cpi->rc.rc_1_frame = 1; | 449 cpi->rc.rc_1_frame = 1; |
455 else | 450 else |
456 cpi->rc.rc_1_frame = 0; | 451 cpi->rc.rc_1_frame = 0; |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
915 } | 910 } |
916 | 911 |
917 assert(*top_index <= rc->worst_quality && | 912 assert(*top_index <= rc->worst_quality && |
918 *top_index >= rc->best_quality); | 913 *top_index >= rc->best_quality); |
919 assert(*bottom_index <= rc->worst_quality && | 914 assert(*bottom_index <= rc->worst_quality && |
920 *bottom_index >= rc->best_quality); | 915 *bottom_index >= rc->best_quality); |
921 assert(q <= rc->worst_quality && q >= rc->best_quality); | 916 assert(q <= rc->worst_quality && q >= rc->best_quality); |
922 return q; | 917 return q; |
923 } | 918 } |
924 | 919 |
| 920 int vp9_frame_type_qdelta(const VP9_COMP *cpi, int rf_level, int q) { |
| 921 static const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { |
| 922 1.00, // INTER_NORMAL |
| 923 1.00, // INTER_HIGH |
| 924 1.50, // GF_ARF_LOW |
| 925 1.75, // GF_ARF_STD |
| 926 2.00, // KF_STD |
| 927 }; |
| 928 static const FRAME_TYPE frame_type[RATE_FACTOR_LEVELS] = |
| 929 {INTER_FRAME, INTER_FRAME, INTER_FRAME, INTER_FRAME, KEY_FRAME}; |
| 930 const VP9_COMMON *const cm = &cpi->common; |
| 931 int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, frame_type[rf_level], |
| 932 q, rate_factor_deltas[rf_level], |
| 933 cm->bit_depth); |
| 934 return qdelta; |
| 935 } |
| 936 |
925 #define STATIC_MOTION_THRESH 95 | 937 #define STATIC_MOTION_THRESH 95 |
926 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, | 938 static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi, |
927 int *bottom_index, | 939 int *bottom_index, |
928 int *top_index) { | 940 int *top_index) { |
929 const VP9_COMMON *const cm = &cpi->common; | 941 const VP9_COMMON *const cm = &cpi->common; |
930 const RATE_CONTROL *const rc = &cpi->rc; | 942 const RATE_CONTROL *const rc = &cpi->rc; |
931 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 943 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 944 const GF_GROUP *gf_group = &cpi->twopass.gf_group; |
932 const int cq_level = get_active_cq_level(rc, oxcf); | 945 const int cq_level = get_active_cq_level(rc, oxcf); |
933 int active_best_quality; | 946 int active_best_quality; |
934 int active_worst_quality = cpi->twopass.active_worst_quality; | 947 int active_worst_quality = cpi->twopass.active_worst_quality; |
935 int q; | 948 int q; |
936 int *inter_minq; | 949 int *inter_minq; |
937 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); | 950 ASSIGN_MINQ_TABLE(cm->bit_depth, inter_minq); |
938 | 951 |
939 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { | 952 if (frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) { |
940 // Handle the special case for key frames forced when we have reached | 953 // Handle the special case for key frames forced when we have reached |
941 // the maximum key frame interval. Here force the Q to a range | 954 // the maximum key frame interval. Here force the Q to a range |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 | 1016 |
1004 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); | 1017 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
1005 | 1018 |
1006 // Constrained quality use slightly lower active best. | 1019 // Constrained quality use slightly lower active best. |
1007 active_best_quality = active_best_quality * 15 / 16; | 1020 active_best_quality = active_best_quality * 15 / 16; |
1008 | 1021 |
1009 } else if (oxcf->rc_mode == VPX_Q) { | 1022 } else if (oxcf->rc_mode == VPX_Q) { |
1010 if (!cpi->refresh_alt_ref_frame) { | 1023 if (!cpi->refresh_alt_ref_frame) { |
1011 active_best_quality = cq_level; | 1024 active_best_quality = cq_level; |
1012 } else { | 1025 } else { |
1013 const GF_GROUP *const gf_group = &cpi->twopass.gf_group; | |
1014 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); | 1026 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
1015 | 1027 |
1016 // Modify best quality for second level arfs. For mode VPX_Q this | 1028 // Modify best quality for second level arfs. For mode VPX_Q this |
1017 // becomes the baseline frame q. | 1029 // becomes the baseline frame q. |
1018 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW) | 1030 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW) |
1019 active_best_quality = (active_best_quality + cq_level + 1) / 2; | 1031 active_best_quality = (active_best_quality + cq_level + 1) / 2; |
1020 } | 1032 } |
1021 } else { | 1033 } else { |
1022 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); | 1034 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); |
1023 } | 1035 } |
1024 } else { | 1036 } else { |
1025 if (oxcf->rc_mode == VPX_Q) { | 1037 if (oxcf->rc_mode == VPX_Q) { |
1026 active_best_quality = cq_level; | 1038 active_best_quality = cq_level; |
1027 } else { | 1039 } else { |
1028 active_best_quality = inter_minq[active_worst_quality]; | 1040 active_best_quality = inter_minq[active_worst_quality]; |
1029 | 1041 |
1030 // For the constrained quality mode we don't want | 1042 // For the constrained quality mode we don't want |
1031 // q to fall below the cq level. | 1043 // q to fall below the cq level. |
1032 if ((oxcf->rc_mode == VPX_CQ) && | 1044 if ((oxcf->rc_mode == VPX_CQ) && |
1033 (active_best_quality < cq_level)) { | 1045 (active_best_quality < cq_level)) { |
1034 active_best_quality = cq_level; | 1046 active_best_quality = cq_level; |
1035 } | 1047 } |
1036 } | 1048 } |
1037 } | 1049 } |
1038 | 1050 |
1039 // Extenstion to max or min Q if undershoot or overshoot is outside | 1051 // Extension to max or min Q if undershoot or overshoot is outside |
1040 // the permitted range. | 1052 // the permitted range. |
1041 if ((cpi->oxcf.rc_mode == VPX_VBR) && | 1053 if ((cpi->oxcf.rc_mode == VPX_VBR) && |
1042 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { | 1054 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { |
1043 if (frame_is_intra_only(cm) || | 1055 if (frame_is_intra_only(cm) || |
1044 (!rc->is_src_frame_alt_ref && | 1056 (!rc->is_src_frame_alt_ref && |
1045 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { | 1057 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
1046 active_best_quality -= cpi->twopass.extend_minq; | 1058 active_best_quality -= cpi->twopass.extend_minq; |
1047 active_worst_quality += (cpi->twopass.extend_maxq / 2); | 1059 active_worst_quality += (cpi->twopass.extend_maxq / 2); |
1048 } else { | 1060 } else { |
1049 active_best_quality -= cpi->twopass.extend_minq / 2; | 1061 active_best_quality -= cpi->twopass.extend_minq / 2; |
1050 active_worst_quality += cpi->twopass.extend_maxq; | 1062 active_worst_quality += cpi->twopass.extend_maxq; |
1051 } | 1063 } |
1052 } | 1064 } |
1053 | 1065 |
1054 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY | 1066 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
1055 vp9_clear_system_state(); | 1067 vp9_clear_system_state(); |
1056 // Static forced key frames Q restrictions dealt with elsewhere. | 1068 // Static forced key frames Q restrictions dealt with elsewhere. |
1057 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || | 1069 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || |
1058 !rc->this_key_frame_forced || | 1070 !rc->this_key_frame_forced || |
1059 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { | 1071 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { |
1060 const GF_GROUP *const gf_group = &cpi->twopass.gf_group; | 1072 int qdelta = vp9_frame_type_qdelta(cpi, gf_group->rf_level[gf_group->index], |
1061 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { | 1073 active_worst_quality); |
1062 1.00, // INTER_NORMAL | 1074 active_worst_quality = MAX(active_worst_quality + qdelta, |
1063 1.00, // INTER_HIGH | 1075 active_best_quality); |
1064 1.50, // GF_ARF_LOW | |
1065 1.75, // GF_ARF_STD | |
1066 2.00, // KF_STD | |
1067 }; | |
1068 const double rate_factor = | |
1069 rate_factor_deltas[gf_group->rf_level[gf_group->index]]; | |
1070 int qdelta = vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, | |
1071 active_worst_quality, rate_factor, | |
1072 cm->bit_depth); | |
1073 active_worst_quality = active_worst_quality + qdelta; | |
1074 active_worst_quality = MAX(active_worst_quality, active_best_quality); | |
1075 } | 1076 } |
1076 #endif | 1077 #endif |
1077 | 1078 |
1078 // Clip the active best and worst quality values to limits. | 1079 // Modify active_best_quality for downscaled normal frames. |
| 1080 if (rc->frame_size_selector != UNSCALED && !frame_is_kf_gf_arf(cpi)) { |
| 1081 int qdelta = vp9_compute_qdelta_by_rate(rc, cm->frame_type, |
| 1082 active_best_quality, 2.0, |
| 1083 cm->bit_depth); |
| 1084 active_best_quality = MAX(active_best_quality + qdelta, rc->best_quality); |
| 1085 } |
| 1086 |
1079 active_best_quality = clamp(active_best_quality, | 1087 active_best_quality = clamp(active_best_quality, |
1080 rc->best_quality, rc->worst_quality); | 1088 rc->best_quality, rc->worst_quality); |
1081 active_worst_quality = clamp(active_worst_quality, | 1089 active_worst_quality = clamp(active_worst_quality, |
1082 active_best_quality, rc->worst_quality); | 1090 active_best_quality, rc->worst_quality); |
1083 | 1091 |
1084 if (oxcf->rc_mode == VPX_Q) { | 1092 if (oxcf->rc_mode == VPX_Q) { |
1085 q = active_best_quality; | 1093 q = active_best_quality; |
1086 // Special case code to try and match quality with forced key frames. | 1094 // Special case code to try and match quality with forced key frames. |
1087 } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) && | 1095 } else if ((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi)) && |
1088 rc->this_key_frame_forced) { | 1096 rc->this_key_frame_forced) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1155 cpi->rc.max_frame_bandwidth); | 1163 cpi->rc.max_frame_bandwidth); |
1156 } | 1164 } |
1157 } | 1165 } |
1158 | 1166 |
1159 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { | 1167 void vp9_rc_set_frame_target(VP9_COMP *cpi, int target) { |
1160 const VP9_COMMON *const cm = &cpi->common; | 1168 const VP9_COMMON *const cm = &cpi->common; |
1161 RATE_CONTROL *const rc = &cpi->rc; | 1169 RATE_CONTROL *const rc = &cpi->rc; |
1162 | 1170 |
1163 rc->this_frame_target = target; | 1171 rc->this_frame_target = target; |
1164 | 1172 |
| 1173 // Modify frame size target when down-scaling. |
| 1174 if (cpi->oxcf.resize_mode == RESIZE_DYNAMIC && |
| 1175 rc->frame_size_selector != UNSCALED) |
| 1176 rc->this_frame_target = (int)(rc->this_frame_target |
| 1177 * rate_thresh_mult[rc->frame_size_selector]); |
| 1178 |
1165 // Target rate per SB64 (including partial SB64s. | 1179 // Target rate per SB64 (including partial SB64s. |
1166 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / | 1180 rc->sb64_target_rate = ((int64_t)rc->this_frame_target * 64 * 64) / |
1167 (cm->width * cm->height); | 1181 (cm->width * cm->height); |
1168 } | 1182 } |
1169 | 1183 |
1170 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { | 1184 static void update_alt_ref_frame_stats(VP9_COMP *cpi) { |
1171 // this frame refreshes means next frames don't unless specified by user | 1185 // this frame refreshes means next frames don't unless specified by user |
1172 RATE_CONTROL *const rc = &cpi->rc; | 1186 RATE_CONTROL *const rc = &cpi->rc; |
1173 rc->frames_since_golden = 0; | 1187 rc->frames_since_golden = 0; |
1174 | 1188 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1215 const int qindex = cm->base_qindex; | 1229 const int qindex = cm->base_qindex; |
1216 | 1230 |
1217 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { | 1231 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->seg.enabled) { |
1218 vp9_cyclic_refresh_update_actual_count(cpi); | 1232 vp9_cyclic_refresh_update_actual_count(cpi); |
1219 } | 1233 } |
1220 | 1234 |
1221 // Update rate control heuristics | 1235 // Update rate control heuristics |
1222 rc->projected_frame_size = (int)(bytes_used << 3); | 1236 rc->projected_frame_size = (int)(bytes_used << 3); |
1223 | 1237 |
1224 // Post encode loop adjustment of Q prediction. | 1238 // Post encode loop adjustment of Q prediction. |
1225 vp9_rc_update_rate_correction_factors( | 1239 vp9_rc_update_rate_correction_factors(cpi); |
1226 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) ? 2 : | |
1227 ((oxcf->rc_mode == VPX_CBR) ? 1 : 0)); | |
1228 | 1240 |
1229 // Keep a record of last Q and ambient average Q. | 1241 // Keep a record of last Q and ambient average Q. |
1230 if (cm->frame_type == KEY_FRAME) { | 1242 if (cm->frame_type == KEY_FRAME) { |
1231 rc->last_q[KEY_FRAME] = qindex; | 1243 rc->last_q[KEY_FRAME] = qindex; |
1232 rc->avg_frame_qindex[KEY_FRAME] = | 1244 rc->avg_frame_qindex[KEY_FRAME] = |
1233 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); | 1245 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); |
1234 } else { | 1246 } else { |
1235 if (rc->is_src_frame_alt_ref || | 1247 if (rc->is_src_frame_alt_ref || |
1236 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) || | 1248 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) || |
1237 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) { | 1249 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) { |
1238 rc->last_q[INTER_FRAME] = qindex; | 1250 rc->last_q[INTER_FRAME] = qindex; |
1239 rc->avg_frame_qindex[INTER_FRAME] = | 1251 rc->avg_frame_qindex[INTER_FRAME] = |
1240 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); | 1252 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[INTER_FRAME] + qindex, 2); |
1241 rc->ni_frames++; | 1253 rc->ni_frames++; |
1242 rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth); | 1254 rc->tot_q += vp9_convert_qindex_to_q(qindex, cm->bit_depth); |
1243 rc->avg_q = rc->tot_q / rc->ni_frames; | 1255 rc->avg_q = rc->tot_q / rc->ni_frames; |
1244 // Calculate the average Q for normal inter frames (not key or GFU | 1256 // Calculate the average Q for normal inter frames (not key or GFU |
1245 // frames). | 1257 // frames). |
1246 rc->ni_tot_qi += qindex; | 1258 rc->ni_tot_qi += qindex; |
1247 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; | 1259 rc->ni_av_qi = rc->ni_tot_qi / rc->ni_frames; |
1248 } | 1260 } |
1249 } | 1261 } |
1250 | 1262 |
1251 // Keep record of last boosted (KF/KF/ARF) Q value. | 1263 // Keep record of last boosted (KF/KF/ARF) Q value. |
1252 // If the current frame is coded at a lower Q then we also update it. | 1264 // If the current frame is coded at a lower Q then we also update it. |
1253 // If all mbs in this group are skipped only update if the Q value is | 1265 // If all mbs in this group are skipped only update if the Q value is |
1254 // better than that already stored. | 1266 // better than that already stored. |
1255 // This is used to help set quality in forced key frames to reduce popping | 1267 // This is used to help set quality in forced key frames to reduce popping |
1256 if ((qindex < rc->last_boosted_qindex) || | 1268 if ((qindex < rc->last_boosted_qindex) || |
1257 (((cm->frame_type == KEY_FRAME) || cpi->refresh_alt_ref_frame || | 1269 (cm->frame_type == KEY_FRAME) || |
| 1270 (!rc->constrained_gf_group && |
| 1271 (cpi->refresh_alt_ref_frame || |
1258 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { | 1272 (cpi->refresh_golden_frame && !rc->is_src_frame_alt_ref)))) { |
1259 rc->last_boosted_qindex = qindex; | 1273 rc->last_boosted_qindex = qindex; |
1260 } | 1274 } |
1261 if (cm->frame_type == KEY_FRAME) | 1275 if (cm->frame_type == KEY_FRAME) |
1262 rc->last_kf_qindex = qindex; | 1276 rc->last_kf_qindex = qindex; |
1263 | 1277 |
1264 update_buffer_level(cpi, rc->projected_frame_size); | 1278 update_buffer_level(cpi, rc->projected_frame_size); |
1265 | 1279 |
1266 // Rolling monitors of whether we are over or underspending used to help | 1280 // Rolling monitors of whether we are over or underspending used to help |
1267 // regulate min and Max Q in two pass. | 1281 // regulate min and Max Q in two pass. |
(...skipping 21 matching lines...) Expand all Loading... |
1289 else | 1303 else |
1290 // Update the Golden frame stats as appropriate. | 1304 // Update the Golden frame stats as appropriate. |
1291 update_golden_frame_stats(cpi); | 1305 update_golden_frame_stats(cpi); |
1292 | 1306 |
1293 if (cm->frame_type == KEY_FRAME) | 1307 if (cm->frame_type == KEY_FRAME) |
1294 rc->frames_since_key = 0; | 1308 rc->frames_since_key = 0; |
1295 if (cm->show_frame) { | 1309 if (cm->show_frame) { |
1296 rc->frames_since_key++; | 1310 rc->frames_since_key++; |
1297 rc->frames_to_key--; | 1311 rc->frames_to_key--; |
1298 } | 1312 } |
| 1313 |
| 1314 // Trigger the resizing of the next frame if it is scaled. |
| 1315 cpi->resize_pending = |
| 1316 rc->next_frame_size_selector != rc->frame_size_selector; |
| 1317 rc->frame_size_selector = rc->next_frame_size_selector; |
1299 } | 1318 } |
1300 | 1319 |
1301 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { | 1320 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { |
1302 // Update buffer level with zero size, update frame counters, and return. | 1321 // Update buffer level with zero size, update frame counters, and return. |
1303 update_buffer_level(cpi, 0); | 1322 update_buffer_level(cpi, 0); |
1304 cpi->common.last_frame_type = cpi->common.frame_type; | |
1305 cpi->rc.frames_since_key++; | 1323 cpi->rc.frames_since_key++; |
1306 cpi->rc.frames_to_key--; | 1324 cpi->rc.frames_to_key--; |
1307 cpi->rc.rc_2_frame = 0; | 1325 cpi->rc.rc_2_frame = 0; |
1308 cpi->rc.rc_1_frame = 0; | 1326 cpi->rc.rc_1_frame = 0; |
1309 } | 1327 } |
1310 | 1328 |
1311 // Use this macro to turn on/off use of alt-refs in one-pass mode. | 1329 // Use this macro to turn on/off use of alt-refs in one-pass mode. |
1312 #define USE_ALTREF_FOR_ONE_PASS 1 | 1330 #define USE_ALTREF_FOR_ONE_PASS 1 |
1313 | 1331 |
1314 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { | 1332 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1351 rc->frames_to_key = cpi->oxcf.key_freq; | 1369 rc->frames_to_key = cpi->oxcf.key_freq; |
1352 rc->kf_boost = DEFAULT_KF_BOOST; | 1370 rc->kf_boost = DEFAULT_KF_BOOST; |
1353 rc->source_alt_ref_active = 0; | 1371 rc->source_alt_ref_active = 0; |
1354 } else { | 1372 } else { |
1355 cm->frame_type = INTER_FRAME; | 1373 cm->frame_type = INTER_FRAME; |
1356 } | 1374 } |
1357 if (rc->frames_till_gf_update_due == 0) { | 1375 if (rc->frames_till_gf_update_due == 0) { |
1358 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; | 1376 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
1359 rc->frames_till_gf_update_due = rc->baseline_gf_interval; | 1377 rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
1360 // NOTE: frames_till_gf_update_due must be <= frames_to_key. | 1378 // NOTE: frames_till_gf_update_due must be <= frames_to_key. |
1361 if (rc->frames_till_gf_update_due > rc->frames_to_key) | 1379 if (rc->frames_till_gf_update_due > rc->frames_to_key) { |
1362 rc->frames_till_gf_update_due = rc->frames_to_key; | 1380 rc->frames_till_gf_update_due = rc->frames_to_key; |
| 1381 rc->constrained_gf_group = 1; |
| 1382 } else { |
| 1383 rc->constrained_gf_group = 0; |
| 1384 } |
1363 cpi->refresh_golden_frame = 1; | 1385 cpi->refresh_golden_frame = 1; |
1364 rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS; | 1386 rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS; |
1365 rc->gfu_boost = DEFAULT_GF_BOOST; | 1387 rc->gfu_boost = DEFAULT_GF_BOOST; |
1366 } | 1388 } |
1367 if (cm->frame_type == KEY_FRAME) | 1389 if (cm->frame_type == KEY_FRAME) |
1368 target = calc_iframe_target_size_one_pass_vbr(cpi); | 1390 target = calc_iframe_target_size_one_pass_vbr(cpi); |
1369 else | 1391 else |
1370 target = calc_pframe_target_size_one_pass_vbr(cpi); | 1392 target = calc_pframe_target_size_one_pass_vbr(cpi); |
1371 vp9_rc_set_frame_target(cpi, target); | 1393 vp9_rc_set_frame_target(cpi, target); |
1372 } | 1394 } |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1658 | 1680 |
1659 void vp9_set_target_rate(VP9_COMP *cpi) { | 1681 void vp9_set_target_rate(VP9_COMP *cpi) { |
1660 RATE_CONTROL *const rc = &cpi->rc; | 1682 RATE_CONTROL *const rc = &cpi->rc; |
1661 int target_rate = rc->base_frame_target; | 1683 int target_rate = rc->base_frame_target; |
1662 | 1684 |
1663 // Correction to rate target based on prior over or under shoot. | 1685 // Correction to rate target based on prior over or under shoot. |
1664 if (cpi->oxcf.rc_mode == VPX_VBR) | 1686 if (cpi->oxcf.rc_mode == VPX_VBR) |
1665 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); | 1687 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); |
1666 vp9_rc_set_frame_target(cpi, target_rate); | 1688 vp9_rc_set_frame_target(cpi, target_rate); |
1667 } | 1689 } |
OLD | NEW |