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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; | 170 return vp9_ac_quant(qindex, 0, bit_depth) / 4.0; |
171 #endif | 171 #endif |
172 } | 172 } |
173 | 173 |
174 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, | 174 int vp9_rc_bits_per_mb(FRAME_TYPE frame_type, int qindex, |
175 double correction_factor, | 175 double correction_factor, |
176 vpx_bit_depth_t bit_depth) { | 176 vpx_bit_depth_t bit_depth) { |
177 const double q = vp9_convert_qindex_to_q(qindex, bit_depth); | 177 const double q = vp9_convert_qindex_to_q(qindex, bit_depth); |
178 int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; | 178 int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000; |
179 | 179 |
| 180 assert(correction_factor <= MAX_BPB_FACTOR && |
| 181 correction_factor >= MIN_BPB_FACTOR); |
| 182 |
180 // q based adjustment to baseline enumerator | 183 // q based adjustment to baseline enumerator |
181 enumerator += (int)(enumerator * q) >> 12; | 184 enumerator += (int)(enumerator * q) >> 12; |
182 return (int)(enumerator * correction_factor / q); | 185 return (int)(enumerator * correction_factor / q); |
183 } | 186 } |
184 | 187 |
185 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, | 188 static int estimate_bits_at_q(FRAME_TYPE frame_type, int q, int mbs, |
186 double correction_factor, | 189 double correction_factor, |
187 vpx_bit_depth_t bit_depth) { | 190 vpx_bit_depth_t bit_depth) { |
188 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, | 191 const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor, |
189 bit_depth)); | 192 bit_depth)); |
190 return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS; | 193 return MAX(FRAME_OVERHEAD_BITS, |
| 194 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS); |
191 } | 195 } |
192 | 196 |
193 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { | 197 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) { |
194 const RATE_CONTROL *rc = &cpi->rc; | 198 const RATE_CONTROL *rc = &cpi->rc; |
195 const int min_frame_target = MAX(rc->min_frame_bandwidth, | 199 const int min_frame_target = MAX(rc->min_frame_bandwidth, |
196 rc->avg_frame_bandwidth >> 5); | 200 rc->avg_frame_bandwidth >> 5); |
197 if (target < min_frame_target) | 201 if (target < min_frame_target) |
198 target = min_frame_target; | 202 target = min_frame_target; |
199 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { | 203 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { |
200 // If there is an active ARF at this location use the minimum | 204 // If there is an active ARF at this location use the minimum |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; | 273 rc->avg_frame_qindex[KEY_FRAME] = oxcf->worst_allowed_q; |
270 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; | 274 rc->avg_frame_qindex[INTER_FRAME] = oxcf->worst_allowed_q; |
271 } else { | 275 } else { |
272 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + | 276 rc->avg_frame_qindex[KEY_FRAME] = (oxcf->worst_allowed_q + |
273 oxcf->best_allowed_q) / 2; | 277 oxcf->best_allowed_q) / 2; |
274 rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + | 278 rc->avg_frame_qindex[INTER_FRAME] = (oxcf->worst_allowed_q + |
275 oxcf->best_allowed_q) / 2; | 279 oxcf->best_allowed_q) / 2; |
276 } | 280 } |
277 | 281 |
278 rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; | 282 rc->last_q[KEY_FRAME] = oxcf->best_allowed_q; |
279 rc->last_q[INTER_FRAME] = oxcf->best_allowed_q; | 283 rc->last_q[INTER_FRAME] = oxcf->worst_allowed_q; |
280 | 284 |
281 rc->buffer_level = rc->starting_buffer_level; | 285 rc->buffer_level = rc->starting_buffer_level; |
282 rc->bits_off_target = rc->starting_buffer_level; | 286 rc->bits_off_target = rc->starting_buffer_level; |
283 | 287 |
284 rc->rolling_target_bits = rc->avg_frame_bandwidth; | 288 rc->rolling_target_bits = rc->avg_frame_bandwidth; |
285 rc->rolling_actual_bits = rc->avg_frame_bandwidth; | 289 rc->rolling_actual_bits = rc->avg_frame_bandwidth; |
286 rc->long_rolling_target_bits = rc->avg_frame_bandwidth; | 290 rc->long_rolling_target_bits = rc->avg_frame_bandwidth; |
287 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; | 291 rc->long_rolling_actual_bits = rc->avg_frame_bandwidth; |
288 | 292 |
289 rc->total_actual_bits = 0; | 293 rc->total_actual_bits = 0; |
290 rc->total_target_bits = 0; | 294 rc->total_target_bits = 0; |
291 rc->total_target_vs_actual = 0; | 295 rc->total_target_vs_actual = 0; |
292 | 296 |
293 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; | 297 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
294 rc->frames_since_key = 8; // Sensible default for first frame. | 298 rc->frames_since_key = 8; // Sensible default for first frame. |
295 rc->this_key_frame_forced = 0; | 299 rc->this_key_frame_forced = 0; |
296 rc->next_key_frame_forced = 0; | 300 rc->next_key_frame_forced = 0; |
297 rc->source_alt_ref_pending = 0; | 301 rc->source_alt_ref_pending = 0; |
298 rc->source_alt_ref_active = 0; | 302 rc->source_alt_ref_active = 0; |
299 | 303 |
300 rc->frames_till_gf_update_due = 0; | 304 rc->frames_till_gf_update_due = 0; |
301 | |
302 rc->ni_av_qi = oxcf->worst_allowed_q; | 305 rc->ni_av_qi = oxcf->worst_allowed_q; |
303 rc->ni_tot_qi = 0; | 306 rc->ni_tot_qi = 0; |
304 rc->ni_frames = 0; | 307 rc->ni_frames = 0; |
305 | 308 |
306 rc->tot_q = 0.0; | 309 rc->tot_q = 0.0; |
307 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); | 310 rc->avg_q = vp9_convert_qindex_to_q(oxcf->worst_allowed_q, oxcf->bit_depth); |
308 | 311 |
309 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { | 312 for (i = 0; i < RATE_FACTOR_LEVELS; ++i) { |
310 rc->rate_correction_factors[i] = 1.0; | 313 rc->rate_correction_factors[i] = 1.0; |
311 } | 314 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 vp9_clear_system_state(); | 406 vp9_clear_system_state(); |
404 | 407 |
405 // Work out how big we would have expected the frame to be at this Q given | 408 // Work out how big we would have expected the frame to be at this Q given |
406 // the current correction factor. | 409 // the current correction factor. |
407 // Stay in double to avoid int overflow when values are large | 410 // Stay in double to avoid int overflow when values are large |
408 projected_size_based_on_q = estimate_bits_at_q(cm->frame_type, | 411 projected_size_based_on_q = estimate_bits_at_q(cm->frame_type, |
409 cm->base_qindex, cm->MBs, | 412 cm->base_qindex, cm->MBs, |
410 rate_correction_factor, | 413 rate_correction_factor, |
411 cm->bit_depth); | 414 cm->bit_depth); |
412 // Work out a size correction factor. | 415 // Work out a size correction factor. |
413 if (projected_size_based_on_q > 0) | 416 if (projected_size_based_on_q > FRAME_OVERHEAD_BITS) |
414 correction_factor = (100 * cpi->rc.projected_frame_size) / | 417 correction_factor = (100 * cpi->rc.projected_frame_size) / |
415 projected_size_based_on_q; | 418 projected_size_based_on_q; |
416 | 419 |
417 // More heavily damped adjustment used if we have been oscillating either side | 420 // More heavily damped adjustment used if we have been oscillating either side |
418 // of target. | 421 // of target. |
419 switch (damp_var) { | 422 switch (damp_var) { |
420 case 0: | 423 case 0: |
421 adjustment_limit = 0.75; | 424 adjustment_limit = 0.75; |
422 break; | 425 break; |
423 case 1: | 426 case 1: |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 // ambient Q (at buffer = optimal level) to worst_quality level | 555 // ambient Q (at buffer = optimal level) to worst_quality level |
553 // (at buffer = critical level). | 556 // (at buffer = critical level). |
554 const VP9_COMMON *const cm = &cpi->common; | 557 const VP9_COMMON *const cm = &cpi->common; |
555 const RATE_CONTROL *rc = &cpi->rc; | 558 const RATE_CONTROL *rc = &cpi->rc; |
556 // Buffer level below which we push active_worst to worst_quality. | 559 // Buffer level below which we push active_worst to worst_quality. |
557 int64_t critical_level = rc->optimal_buffer_level >> 2; | 560 int64_t critical_level = rc->optimal_buffer_level >> 2; |
558 int64_t buff_lvl_step = 0; | 561 int64_t buff_lvl_step = 0; |
559 int adjustment = 0; | 562 int adjustment = 0; |
560 int active_worst_quality; | 563 int active_worst_quality; |
561 if (cm->frame_type == KEY_FRAME) | 564 if (cm->frame_type == KEY_FRAME) |
562 return rc->worst_quality; | 565 return rc->worst_quality * 4 / 5; |
563 if (cm->current_video_frame > 1) | 566 if (cm->current_video_frame > 1) |
564 active_worst_quality = MIN(rc->worst_quality, | 567 active_worst_quality = MIN(rc->worst_quality, |
565 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); | 568 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); |
566 else | 569 else |
567 active_worst_quality = MIN(rc->worst_quality, | 570 active_worst_quality = MIN(rc->worst_quality, |
568 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); | 571 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); |
569 if (rc->buffer_level > rc->optimal_buffer_level) { | 572 if (rc->buffer_level > rc->optimal_buffer_level) { |
570 // Adjust down. | 573 // Adjust down. |
571 // Maximum limit for down adjustment, ~30%. | 574 // Maximum limit for down adjustment, ~30%. |
572 int max_adjustment_down = active_worst_quality / 3; | 575 int max_adjustment_down = active_worst_quality / 3; |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 | 984 |
982 // For the constrained quality mode we don't want | 985 // For the constrained quality mode we don't want |
983 // q to fall below the cq level. | 986 // q to fall below the cq level. |
984 if ((oxcf->rc_mode == VPX_CQ) && | 987 if ((oxcf->rc_mode == VPX_CQ) && |
985 (active_best_quality < cq_level)) { | 988 (active_best_quality < cq_level)) { |
986 active_best_quality = cq_level; | 989 active_best_quality = cq_level; |
987 } | 990 } |
988 } | 991 } |
989 } | 992 } |
990 | 993 |
| 994 // Extenstion to max or min Q if undershoot or overshoot is outside |
| 995 // the permitted range. |
| 996 if ((cpi->oxcf.rc_mode == VPX_VBR) && |
| 997 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD)) { |
| 998 if (frame_is_intra_only(cm) || |
| 999 (!rc->is_src_frame_alt_ref && |
| 1000 (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) { |
| 1001 active_best_quality -= cpi->twopass.extend_minq; |
| 1002 active_worst_quality += (cpi->twopass.extend_maxq / 2); |
| 1003 } else { |
| 1004 active_best_quality -= cpi->twopass.extend_minq / 2; |
| 1005 active_worst_quality += cpi->twopass.extend_maxq; |
| 1006 } |
| 1007 } |
| 1008 |
991 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY | 1009 #if LIMIT_QRANGE_FOR_ALTREF_AND_KEY |
992 vp9_clear_system_state(); | 1010 vp9_clear_system_state(); |
993 // Static forced key frames Q restrictions dealt with elsewhere. | 1011 // Static forced key frames Q restrictions dealt with elsewhere. |
994 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || | 1012 if (!((frame_is_intra_only(cm) || vp9_is_upper_layer_key_frame(cpi))) || |
995 !rc->this_key_frame_forced || | 1013 !rc->this_key_frame_forced || |
996 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { | 1014 (cpi->twopass.last_kfgroup_zeromotion_pct < STATIC_MOTION_THRESH)) { |
997 const GF_GROUP *const gf_group = &cpi->twopass.gf_group; | 1015 const GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
998 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { | 1016 const double rate_factor_deltas[RATE_FACTOR_LEVELS] = { |
999 1.00, // INTER_NORMAL | 1017 1.00, // INTER_NORMAL |
1000 1.00, // INTER_HIGH | 1018 1.00, // INTER_HIGH |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1475 return target_index - qindex; | 1493 return target_index - qindex; |
1476 } | 1494 } |
1477 | 1495 |
1478 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi, | 1496 void vp9_rc_set_gf_max_interval(const VP9_COMP *const cpi, |
1479 RATE_CONTROL *const rc) { | 1497 RATE_CONTROL *const rc) { |
1480 const VP9EncoderConfig *const oxcf = &cpi->oxcf; | 1498 const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
1481 // Set Maximum gf/arf interval | 1499 // Set Maximum gf/arf interval |
1482 rc->max_gf_interval = 16; | 1500 rc->max_gf_interval = 16; |
1483 | 1501 |
1484 // Extended interval for genuinely static scenes | 1502 // Extended interval for genuinely static scenes |
1485 rc->static_scene_max_gf_interval = oxcf->key_freq >> 1; | 1503 rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2; |
1486 if (rc->static_scene_max_gf_interval > (MAX_LAG_BUFFERS * 2)) | |
1487 rc->static_scene_max_gf_interval = MAX_LAG_BUFFERS * 2; | |
1488 | 1504 |
1489 if (is_altref_enabled(cpi)) { | 1505 if (is_altref_enabled(cpi)) { |
1490 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) | 1506 if (rc->static_scene_max_gf_interval > oxcf->lag_in_frames - 1) |
1491 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; | 1507 rc->static_scene_max_gf_interval = oxcf->lag_in_frames - 1; |
1492 } | 1508 } |
1493 | 1509 |
1494 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) | 1510 if (rc->max_gf_interval > rc->static_scene_max_gf_interval) |
1495 rc->max_gf_interval = rc->static_scene_max_gf_interval; | 1511 rc->max_gf_interval = rc->static_scene_max_gf_interval; |
1496 } | 1512 } |
1497 | 1513 |
(...skipping 16 matching lines...) Expand all Loading... |
1514 // a very high rate is given on the command line or the the rate cannnot | 1530 // a very high rate is given on the command line or the the rate cannnot |
1515 // be acheived because of a user specificed max q (e.g. when the user | 1531 // be acheived because of a user specificed max q (e.g. when the user |
1516 // specifies lossless encode. | 1532 // specifies lossless encode. |
1517 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * | 1533 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * |
1518 oxcf->two_pass_vbrmax_section) / 100); | 1534 oxcf->two_pass_vbrmax_section) / 100); |
1519 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), | 1535 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), |
1520 vbr_max_bits); | 1536 vbr_max_bits); |
1521 | 1537 |
1522 vp9_rc_set_gf_max_interval(cpi, rc); | 1538 vp9_rc_set_gf_max_interval(cpi, rc); |
1523 } | 1539 } |
OLD | NEW |