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

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

Issue 756673003: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years 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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 double correction_factor, 189 double correction_factor,
190 vpx_bit_depth_t bit_depth) { 190 vpx_bit_depth_t bit_depth) {
191 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,
192 bit_depth)); 192 bit_depth));
193 return MAX(FRAME_OVERHEAD_BITS, 193 return MAX(FRAME_OVERHEAD_BITS,
194 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS); 194 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
195 } 195 }
196 196
197 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) {
198 const RATE_CONTROL *rc = &cpi->rc; 198 const RATE_CONTROL *rc = &cpi->rc;
199 const VP9EncoderConfig *oxcf = &cpi->oxcf;
199 const int min_frame_target = MAX(rc->min_frame_bandwidth, 200 const int min_frame_target = MAX(rc->min_frame_bandwidth,
200 rc->avg_frame_bandwidth >> 5); 201 rc->avg_frame_bandwidth >> 5);
201 if (target < min_frame_target) 202 if (target < min_frame_target)
202 target = min_frame_target; 203 target = min_frame_target;
203 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) { 204 if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
204 // If there is an active ARF at this location use the minimum 205 // If there is an active ARF at this location use the minimum
205 // bits on this frame even if it is a constructed arf. 206 // bits on this frame even if it is a constructed arf.
206 // The active maximum quantizer insures that an appropriate 207 // The active maximum quantizer insures that an appropriate
207 // number of bits will be spent if needed for constructed ARFs. 208 // number of bits will be spent if needed for constructed ARFs.
208 target = min_frame_target; 209 target = min_frame_target;
209 } 210 }
210 // Clip the frame target to the maximum allowed value. 211 // Clip the frame target to the maximum allowed value.
211 if (target > rc->max_frame_bandwidth) 212 if (target > rc->max_frame_bandwidth)
212 target = rc->max_frame_bandwidth; 213 target = rc->max_frame_bandwidth;
214 if (oxcf->rc_max_inter_bitrate_pct) {
215 const int max_rate = rc->avg_frame_bandwidth *
216 oxcf->rc_max_inter_bitrate_pct / 100;
217 target = MIN(target, max_rate);
218 }
213 return target; 219 return target;
214 } 220 }
215 221
216 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) { 222 int vp9_rc_clamp_iframe_target_size(const VP9_COMP *const cpi, int target) {
217 const RATE_CONTROL *rc = &cpi->rc; 223 const RATE_CONTROL *rc = &cpi->rc;
218 const VP9EncoderConfig *oxcf = &cpi->oxcf; 224 const VP9EncoderConfig *oxcf = &cpi->oxcf;
219 if (oxcf->rc_max_intra_bitrate_pct) { 225 if (oxcf->rc_max_intra_bitrate_pct) {
220 const int max_rate = rc->avg_frame_bandwidth * 226 const int max_rate = rc->avg_frame_bandwidth *
221 oxcf->rc_max_intra_bitrate_pct / 100; 227 oxcf->rc_max_intra_bitrate_pct / 100;
222 target = MIN(target, max_rate); 228 target = MIN(target, max_rate);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 const RATE_CONTROL *const rc = &cpi->rc; 362 const RATE_CONTROL *const rc = &cpi->rc;
357 363
358 if (cpi->common.frame_type == KEY_FRAME) { 364 if (cpi->common.frame_type == KEY_FRAME) {
359 return rc->rate_correction_factors[KF_STD]; 365 return rc->rate_correction_factors[KF_STD];
360 } else if (cpi->oxcf.pass == 2) { 366 } else if (cpi->oxcf.pass == 2) {
361 RATE_FACTOR_LEVEL rf_lvl = 367 RATE_FACTOR_LEVEL rf_lvl =
362 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; 368 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
363 return rc->rate_correction_factors[rf_lvl]; 369 return rc->rate_correction_factors[rf_lvl];
364 } else { 370 } else {
365 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 371 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
366 !rc->is_src_frame_alt_ref && 372 !rc->is_src_frame_alt_ref && !cpi->use_svc &&
367 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) 373 cpi->oxcf.rc_mode != VPX_CBR)
368 return rc->rate_correction_factors[GF_ARF_STD]; 374 return rc->rate_correction_factors[GF_ARF_STD];
369 else 375 else
370 return rc->rate_correction_factors[INTER_NORMAL]; 376 return rc->rate_correction_factors[INTER_NORMAL];
371 } 377 }
372 } 378 }
373 379
374 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) { 380 static void set_rate_correction_factor(VP9_COMP *cpi, double factor) {
375 RATE_CONTROL *const rc = &cpi->rc; 381 RATE_CONTROL *const rc = &cpi->rc;
376 382
377 if (cpi->common.frame_type == KEY_FRAME) { 383 if (cpi->common.frame_type == KEY_FRAME) {
378 rc->rate_correction_factors[KF_STD] = factor; 384 rc->rate_correction_factors[KF_STD] = factor;
379 } else if (cpi->oxcf.pass == 2) { 385 } else if (cpi->oxcf.pass == 2) {
380 RATE_FACTOR_LEVEL rf_lvl = 386 RATE_FACTOR_LEVEL rf_lvl =
381 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index]; 387 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
382 rc->rate_correction_factors[rf_lvl] = factor; 388 rc->rate_correction_factors[rf_lvl] = factor;
383 } else { 389 } else {
384 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) && 390 if ((cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame) &&
385 !rc->is_src_frame_alt_ref && 391 !rc->is_src_frame_alt_ref && !cpi->use_svc &&
386 !(cpi->use_svc && cpi->oxcf.rc_mode == VPX_CBR)) 392 cpi->oxcf.rc_mode != VPX_CBR)
387 rc->rate_correction_factors[GF_ARF_STD] = factor; 393 rc->rate_correction_factors[GF_ARF_STD] = factor;
388 else 394 else
389 rc->rate_correction_factors[INTER_NORMAL] = factor; 395 rc->rate_correction_factors[INTER_NORMAL] = factor;
390 } 396 }
391 } 397 }
392 398
393 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) { 399 void vp9_rc_update_rate_correction_factors(VP9_COMP *cpi, int damp_var) {
394 const VP9_COMMON *const cm = &cpi->common; 400 const VP9_COMMON *const cm = &cpi->common;
395 int correction_factor = 100; 401 int correction_factor = 100;
396 double rate_correction_factor = get_rate_correction_factor(cpi); 402 double rate_correction_factor = get_rate_correction_factor(cpi);
(...skipping 20 matching lines...) Expand all
417 correction_factor = (100 * cpi->rc.projected_frame_size) / 423 correction_factor = (100 * cpi->rc.projected_frame_size) /
418 projected_size_based_on_q; 424 projected_size_based_on_q;
419 425
420 // More heavily damped adjustment used if we have been oscillating either side 426 // More heavily damped adjustment used if we have been oscillating either side
421 // of target. 427 // of target.
422 switch (damp_var) { 428 switch (damp_var) {
423 case 0: 429 case 0:
424 adjustment_limit = 0.75; 430 adjustment_limit = 0.75;
425 break; 431 break;
426 case 1: 432 case 1:
427 adjustment_limit = 0.375; 433 adjustment_limit = 0.25 +
434 0.5 * MIN(1, fabs(log10(0.01 * correction_factor)));
428 break; 435 break;
429 case 2: 436 case 2:
430 default: 437 default:
431 adjustment_limit = 0.25; 438 adjustment_limit = 0.25;
432 break; 439 break;
433 } 440 }
434 441
442 cpi->rc.q_2_frame = cpi->rc.q_1_frame;
443 cpi->rc.q_1_frame = cm->base_qindex;
444 cpi->rc.rc_2_frame = cpi->rc.rc_1_frame;
445 if (correction_factor > 110)
446 cpi->rc.rc_1_frame = -1;
447 else if (correction_factor < 90)
448 cpi->rc.rc_1_frame = 1;
449 else
450 cpi->rc.rc_1_frame = 0;
451
435 if (correction_factor > 102) { 452 if (correction_factor > 102) {
436 // We are not already at the worst allowable quality 453 // We are not already at the worst allowable quality
437 correction_factor = (int)(100 + ((correction_factor - 100) * 454 correction_factor = (int)(100 + ((correction_factor - 100) *
438 adjustment_limit)); 455 adjustment_limit));
439 rate_correction_factor = (rate_correction_factor * correction_factor) / 100; 456 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
440
441 // Keep rate_correction_factor within limits 457 // Keep rate_correction_factor within limits
442 if (rate_correction_factor > MAX_BPB_FACTOR) 458 if (rate_correction_factor > MAX_BPB_FACTOR)
443 rate_correction_factor = MAX_BPB_FACTOR; 459 rate_correction_factor = MAX_BPB_FACTOR;
444 } else if (correction_factor < 99) { 460 } else if (correction_factor < 99) {
445 // We are not already at the best allowable quality 461 // We are not already at the best allowable quality
446 correction_factor = (int)(100 - ((100 - correction_factor) * 462 correction_factor = (int)(100 - ((100 - correction_factor) *
447 adjustment_limit)); 463 adjustment_limit));
448 rate_correction_factor = (rate_correction_factor * correction_factor) / 100; 464 rate_correction_factor = (rate_correction_factor * correction_factor) / 100;
449 465
450 // Keep rate_correction_factor within limits 466 // Keep rate_correction_factor within limits
(...skipping 30 matching lines...) Expand all
481 q = i; 497 q = i;
482 else 498 else
483 q = i - 1; 499 q = i - 1;
484 500
485 break; 501 break;
486 } else { 502 } else {
487 last_error = bits_per_mb_at_this_q - target_bits_per_mb; 503 last_error = bits_per_mb_at_this_q - target_bits_per_mb;
488 } 504 }
489 } while (++i <= active_worst_quality); 505 } while (++i <= active_worst_quality);
490 506
507 // In CBR mode, this makes sure q is between oscillating Qs to prevent
508 // resonance.
509 if (cpi->oxcf.rc_mode == VPX_CBR &&
510 (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
511 cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
512 q = clamp(q, MIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
513 MAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
514 }
491 return q; 515 return q;
492 } 516 }
493 517
494 static int get_active_quality(int q, int gfu_boost, int low, int high, 518 static int get_active_quality(int q, int gfu_boost, int low, int high,
495 int *low_motion_minq, int *high_motion_minq) { 519 int *low_motion_minq, int *high_motion_minq) {
496 if (gfu_boost > high) { 520 if (gfu_boost > high) {
497 return low_motion_minq[q]; 521 return low_motion_minq[q];
498 } else if (gfu_boost < low) { 522 } else if (gfu_boost < low) {
499 return high_motion_minq[q]; 523 return high_motion_minq[q];
500 } else { 524 } else {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 // Adjust active_worst_quality level based on buffer level. 574 // Adjust active_worst_quality level based on buffer level.
551 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) { 575 static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
552 // Adjust active_worst_quality: If buffer is above the optimal/target level, 576 // Adjust active_worst_quality: If buffer is above the optimal/target level,
553 // bring active_worst_quality down depending on fullness of buffer. 577 // bring active_worst_quality down depending on fullness of buffer.
554 // If buffer is below the optimal level, let the active_worst_quality go from 578 // If buffer is below the optimal level, let the active_worst_quality go from
555 // ambient Q (at buffer = optimal level) to worst_quality level 579 // ambient Q (at buffer = optimal level) to worst_quality level
556 // (at buffer = critical level). 580 // (at buffer = critical level).
557 const VP9_COMMON *const cm = &cpi->common; 581 const VP9_COMMON *const cm = &cpi->common;
558 const RATE_CONTROL *rc = &cpi->rc; 582 const RATE_CONTROL *rc = &cpi->rc;
559 // Buffer level below which we push active_worst to worst_quality. 583 // Buffer level below which we push active_worst to worst_quality.
560 int64_t critical_level = rc->optimal_buffer_level >> 2; 584 int64_t critical_level = rc->optimal_buffer_level >> 3;
561 int64_t buff_lvl_step = 0; 585 int64_t buff_lvl_step = 0;
562 int adjustment = 0; 586 int adjustment = 0;
563 int active_worst_quality; 587 int active_worst_quality;
588 int ambient_qp;
564 if (cm->frame_type == KEY_FRAME) 589 if (cm->frame_type == KEY_FRAME)
565 return rc->worst_quality * 4 / 5; 590 return rc->worst_quality;
566 if (cm->current_video_frame > 1) 591 // For ambient_qp we use minimum of avg_frame_qindex[KEY_FRAME/INTER_FRAME]
567 active_worst_quality = MIN(rc->worst_quality, 592 // for the first few frames following key frame. These are both initialized
568 rc->avg_frame_qindex[INTER_FRAME] * 5 / 4); 593 // to worst_quality and updated with (3/4, 1/4) average in postencode_update.
569 else 594 // So for first few frames following key, the qp of that key frame is weighted
570 active_worst_quality = MIN(rc->worst_quality, 595 // into the active_worst_quality setting.
571 rc->avg_frame_qindex[KEY_FRAME] * 3 / 2); 596 ambient_qp = (cm->current_video_frame < 5) ?
597 MIN(rc->avg_frame_qindex[INTER_FRAME], rc->avg_frame_qindex[KEY_FRAME]) :
598 rc->avg_frame_qindex[INTER_FRAME];
599 active_worst_quality = MIN(rc->worst_quality,
600 ambient_qp * 5 / 4);
572 if (rc->buffer_level > rc->optimal_buffer_level) { 601 if (rc->buffer_level > rc->optimal_buffer_level) {
573 // Adjust down. 602 // Adjust down.
574 // Maximum limit for down adjustment, ~30%. 603 // Maximum limit for down adjustment, ~30%.
575 int max_adjustment_down = active_worst_quality / 3; 604 int max_adjustment_down = active_worst_quality / 3;
576 if (max_adjustment_down) { 605 if (max_adjustment_down) {
577 buff_lvl_step = ((rc->maximum_buffer_size - 606 buff_lvl_step = ((rc->maximum_buffer_size -
578 rc->optimal_buffer_level) / max_adjustment_down); 607 rc->optimal_buffer_level) / max_adjustment_down);
579 if (buff_lvl_step) 608 if (buff_lvl_step)
580 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) / 609 adjustment = (int)((rc->buffer_level - rc->optimal_buffer_level) /
581 buff_lvl_step); 610 buff_lvl_step);
582 active_worst_quality -= adjustment; 611 active_worst_quality -= adjustment;
583 } 612 }
584 } else if (rc->buffer_level > critical_level) { 613 } else if (rc->buffer_level > critical_level) {
585 // Adjust up from ambient Q. 614 // Adjust up from ambient Q.
586 if (critical_level) { 615 if (critical_level) {
587 buff_lvl_step = (rc->optimal_buffer_level - critical_level); 616 buff_lvl_step = (rc->optimal_buffer_level - critical_level);
588 if (buff_lvl_step) { 617 if (buff_lvl_step) {
589 adjustment = 618 adjustment = (int)((rc->worst_quality - ambient_qp) *
590 (int)((rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) * 619 (rc->optimal_buffer_level - rc->buffer_level) /
591 (rc->optimal_buffer_level - rc->buffer_level) / 620 buff_lvl_step);
592 buff_lvl_step);
593 } 621 }
594 active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment; 622 active_worst_quality = ambient_qp + adjustment;
595 } 623 }
596 } else { 624 } else {
597 // Set to worst_quality if buffer is below critical level. 625 // Set to worst_quality if buffer is below critical level.
598 active_worst_quality = rc->worst_quality; 626 active_worst_quality = rc->worst_quality;
599 } 627 }
600 return active_worst_quality; 628 return active_worst_quality;
601 } 629 }
602 630
603 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi, 631 static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
604 int *bottom_index, 632 int *bottom_index,
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 992
965 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); 993 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
966 994
967 // Constrained quality use slightly lower active best. 995 // Constrained quality use slightly lower active best.
968 active_best_quality = active_best_quality * 15 / 16; 996 active_best_quality = active_best_quality * 15 / 16;
969 997
970 } else if (oxcf->rc_mode == VPX_Q) { 998 } else if (oxcf->rc_mode == VPX_Q) {
971 if (!cpi->refresh_alt_ref_frame) { 999 if (!cpi->refresh_alt_ref_frame) {
972 active_best_quality = cq_level; 1000 active_best_quality = cq_level;
973 } else { 1001 } else {
974 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); 1002 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
1003 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
1004
1005 // Modify best quality for second level arfs. For mode VPX_Q this
1006 // becomes the baseline frame q.
1007 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
1008 active_best_quality = (active_best_quality + cq_level + 1) / 2;
975 } 1009 }
976 } else { 1010 } else {
977 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth); 1011 active_best_quality = get_gf_active_quality(rc, q, cm->bit_depth);
978 } 1012 }
979 } else { 1013 } else {
980 if (oxcf->rc_mode == VPX_Q) { 1014 if (oxcf->rc_mode == VPX_Q) {
981 active_best_quality = cq_level; 1015 active_best_quality = cq_level;
982 } else { 1016 } else {
983 active_best_quality = inter_minq[active_worst_quality]; 1017 active_best_quality = inter_minq[active_worst_quality];
984 1018
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 const VP9_COMMON *const cm = &cpi->common; 1201 const VP9_COMMON *const cm = &cpi->common;
1168 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1202 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1169 RATE_CONTROL *const rc = &cpi->rc; 1203 RATE_CONTROL *const rc = &cpi->rc;
1170 const int qindex = cm->base_qindex; 1204 const int qindex = cm->base_qindex;
1171 1205
1172 // Update rate control heuristics 1206 // Update rate control heuristics
1173 rc->projected_frame_size = (int)(bytes_used << 3); 1207 rc->projected_frame_size = (int)(bytes_used << 3);
1174 1208
1175 // Post encode loop adjustment of Q prediction. 1209 // Post encode loop adjustment of Q prediction.
1176 vp9_rc_update_rate_correction_factors( 1210 vp9_rc_update_rate_correction_factors(
1177 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || 1211 cpi, (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) ? 2 :
1178 oxcf->rc_mode == VPX_CBR) ? 2 : 0); 1212 ((oxcf->rc_mode == VPX_CBR) ? 1 : 0));
1179 1213
1180 // Keep a record of last Q and ambient average Q. 1214 // Keep a record of last Q and ambient average Q.
1181 if (cm->frame_type == KEY_FRAME) { 1215 if (cm->frame_type == KEY_FRAME) {
1182 rc->last_q[KEY_FRAME] = qindex; 1216 rc->last_q[KEY_FRAME] = qindex;
1183 rc->avg_frame_qindex[KEY_FRAME] = 1217 rc->avg_frame_qindex[KEY_FRAME] =
1184 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2); 1218 ROUND_POWER_OF_TWO(3 * rc->avg_frame_qindex[KEY_FRAME] + qindex, 2);
1185 } else { 1219 } else {
1186 if (rc->is_src_frame_alt_ref || 1220 if (rc->is_src_frame_alt_ref ||
1187 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) || 1221 !(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) ||
1188 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) { 1222 (cpi->use_svc && oxcf->rc_mode == VPX_CBR)) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 rc->frames_to_key--; 1282 rc->frames_to_key--;
1249 } 1283 }
1250 } 1284 }
1251 1285
1252 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) { 1286 void vp9_rc_postencode_update_drop_frame(VP9_COMP *cpi) {
1253 // Update buffer level with zero size, update frame counters, and return. 1287 // Update buffer level with zero size, update frame counters, and return.
1254 update_buffer_level(cpi, 0); 1288 update_buffer_level(cpi, 0);
1255 cpi->common.last_frame_type = cpi->common.frame_type; 1289 cpi->common.last_frame_type = cpi->common.frame_type;
1256 cpi->rc.frames_since_key++; 1290 cpi->rc.frames_since_key++;
1257 cpi->rc.frames_to_key--; 1291 cpi->rc.frames_to_key--;
1292 cpi->rc.rc_2_frame = 0;
1293 cpi->rc.rc_1_frame = 0;
1258 } 1294 }
1259 1295
1260 // Use this macro to turn on/off use of alt-refs in one-pass mode. 1296 // Use this macro to turn on/off use of alt-refs in one-pass mode.
1261 #define USE_ALTREF_FOR_ONE_PASS 1 1297 #define USE_ALTREF_FOR_ONE_PASS 1
1262 1298
1263 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { 1299 static int calc_pframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
1264 static const int af_ratio = 10; 1300 static const int af_ratio = 10;
1265 const RATE_CONTROL *const rc = &cpi->rc; 1301 const RATE_CONTROL *const rc = &cpi->rc;
1266 int target; 1302 int target;
1267 #if USE_ALTREF_FOR_ONE_PASS 1303 #if USE_ALTREF_FOR_ONE_PASS
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1320 vp9_rc_set_frame_target(cpi, target); 1356 vp9_rc_set_frame_target(cpi, target);
1321 } 1357 }
1322 1358
1323 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1359 static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1324 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1360 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1325 const RATE_CONTROL *rc = &cpi->rc; 1361 const RATE_CONTROL *rc = &cpi->rc;
1326 const SVC *const svc = &cpi->svc; 1362 const SVC *const svc = &cpi->svc;
1327 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level; 1363 const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
1328 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100; 1364 const int64_t one_pct_bits = 1 + rc->optimal_buffer_level / 100;
1329 int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS); 1365 int min_frame_target = MAX(rc->avg_frame_bandwidth >> 4, FRAME_OVERHEAD_BITS);
1330 int target = rc->avg_frame_bandwidth; 1366 int target;
1367
1368 if (oxcf->gf_cbr_boost_pct) {
1369 const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
1370 target = cpi->refresh_golden_frame ?
1371 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
1372 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100) :
1373 (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
1374 (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
1375 } else {
1376 target = rc->avg_frame_bandwidth;
1377 }
1331 if (svc->number_temporal_layers > 1 && 1378 if (svc->number_temporal_layers > 1 &&
1332 oxcf->rc_mode == VPX_CBR) { 1379 oxcf->rc_mode == VPX_CBR) {
1333 // Note that for layers, avg_frame_bandwidth is the cumulative 1380 // Note that for layers, avg_frame_bandwidth is the cumulative
1334 // per-frame-bandwidth. For the target size of this frame, use the 1381 // per-frame-bandwidth. For the target size of this frame, use the
1335 // layer average frame size (i.e., non-cumulative per-frame-bw). 1382 // layer average frame size (i.e., non-cumulative per-frame-bw).
1336 int current_temporal_layer = svc->temporal_layer_id; 1383 int current_temporal_layer = svc->temporal_layer_id;
1337 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer]; 1384 const LAYER_CONTEXT *lc = &svc->layer_context[current_temporal_layer];
1338 target = lc->avg_frame_size; 1385 target = lc->avg_frame_size;
1339 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS); 1386 min_frame_target = MAX(lc->avg_frame_size >> 4, FRAME_OVERHEAD_BITS);
1340 } 1387 }
1341 if (diff > 0) { 1388 if (diff > 0) {
1342 // Lower the target bandwidth for this frame. 1389 // Lower the target bandwidth for this frame.
1343 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct); 1390 const int pct_low = (int)MIN(diff / one_pct_bits, oxcf->under_shoot_pct);
1344 target -= (target * pct_low) / 200; 1391 target -= (target * pct_low) / 200;
1345 } else if (diff < 0) { 1392 } else if (diff < 0) {
1346 // Increase the target bandwidth for this frame. 1393 // Increase the target bandwidth for this frame.
1347 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct); 1394 const int pct_high = (int)MIN(-diff / one_pct_bits, oxcf->over_shoot_pct);
1348 target += (target * pct_high) / 200; 1395 target += (target * pct_high) / 200;
1349 } 1396 }
1397 if (oxcf->rc_max_inter_bitrate_pct) {
1398 const int max_rate = rc->avg_frame_bandwidth *
1399 oxcf->rc_max_inter_bitrate_pct / 100;
1400 target = MIN(target, max_rate);
1401 }
1350 return MAX(min_frame_target, target); 1402 return MAX(min_frame_target, target);
1351 } 1403 }
1352 1404
1353 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { 1405 static int calc_iframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
1354 const RATE_CONTROL *rc = &cpi->rc; 1406 const RATE_CONTROL *rc = &cpi->rc;
1355 const VP9EncoderConfig *oxcf = &cpi->oxcf; 1407 const VP9EncoderConfig *oxcf = &cpi->oxcf;
1356 const SVC *const svc = &cpi->svc; 1408 const SVC *const svc = &cpi->svc;
1357 int target; 1409 int target;
1358 if (cpi->common.current_video_frame == 0) { 1410 if (cpi->common.current_video_frame == 0) {
1359 target = ((rc->starting_buffer_level / 2) > INT_MAX) 1411 target = ((rc->starting_buffer_level / 2) > INT_MAX)
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1429 if ((cm->current_video_frame == 0 || 1481 if ((cm->current_video_frame == 0 ||
1430 (cpi->frame_flags & FRAMEFLAGS_KEY) || 1482 (cpi->frame_flags & FRAMEFLAGS_KEY) ||
1431 rc->frames_to_key == 0 || 1483 rc->frames_to_key == 0 ||
1432 (cpi->oxcf.auto_key && 0))) { 1484 (cpi->oxcf.auto_key && 0))) {
1433 cm->frame_type = KEY_FRAME; 1485 cm->frame_type = KEY_FRAME;
1434 rc->this_key_frame_forced = cm->current_video_frame != 0 && 1486 rc->this_key_frame_forced = cm->current_video_frame != 0 &&
1435 rc->frames_to_key == 0; 1487 rc->frames_to_key == 0;
1436 rc->frames_to_key = cpi->oxcf.key_freq; 1488 rc->frames_to_key = cpi->oxcf.key_freq;
1437 rc->kf_boost = DEFAULT_KF_BOOST; 1489 rc->kf_boost = DEFAULT_KF_BOOST;
1438 rc->source_alt_ref_active = 0; 1490 rc->source_alt_ref_active = 0;
1439 target = calc_iframe_target_size_one_pass_cbr(cpi);
1440 } else { 1491 } else {
1441 cm->frame_type = INTER_FRAME; 1492 cm->frame_type = INTER_FRAME;
1493 }
1494 if (rc->frames_till_gf_update_due == 0) {
1495 rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1496 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1497 // NOTE: frames_till_gf_update_due must be <= frames_to_key.
1498 if (rc->frames_till_gf_update_due > rc->frames_to_key)
1499 rc->frames_till_gf_update_due = rc->frames_to_key;
1500 cpi->refresh_golden_frame = 1;
1501 rc->gfu_boost = DEFAULT_GF_BOOST;
1502 }
1503
1504 if (cm->frame_type == KEY_FRAME)
1505 target = calc_iframe_target_size_one_pass_cbr(cpi);
1506 else
1442 target = calc_pframe_target_size_one_pass_cbr(cpi); 1507 target = calc_pframe_target_size_one_pass_cbr(cpi);
1443 } 1508
1444 vp9_rc_set_frame_target(cpi, target); 1509 vp9_rc_set_frame_target(cpi, target);
1445 // Don't use gf_update by default in CBR mode.
1446 rc->frames_till_gf_update_due = INT_MAX;
1447 rc->baseline_gf_interval = INT_MAX;
1448 } 1510 }
1449 1511
1450 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget, 1512 int vp9_compute_qdelta(const RATE_CONTROL *rc, double qstart, double qtarget,
1451 vpx_bit_depth_t bit_depth) { 1513 vpx_bit_depth_t bit_depth) {
1452 int start_index = rc->worst_quality; 1514 int start_index = rc->worst_quality;
1453 int target_index = rc->worst_quality; 1515 int target_index = rc->worst_quality;
1454 int i; 1516 int i;
1455 1517
1456 // Convert the average q value to an index. 1518 // Convert the average q value to an index.
1457 for (i = rc->best_quality; i < rc->worst_quality; ++i) { 1519 for (i = rc->best_quality; i < rc->worst_quality; ++i) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1530 // a very high rate is given on the command line or the the rate cannnot 1592 // a very high rate is given on the command line or the the rate cannnot
1531 // be acheived because of a user specificed max q (e.g. when the user 1593 // be acheived because of a user specificed max q (e.g. when the user
1532 // specifies lossless encode. 1594 // specifies lossless encode.
1533 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth * 1595 vbr_max_bits = (int)(((int64_t)rc->avg_frame_bandwidth *
1534 oxcf->two_pass_vbrmax_section) / 100); 1596 oxcf->two_pass_vbrmax_section) / 100);
1535 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), 1597 rc->max_frame_bandwidth = MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P),
1536 vbr_max_bits); 1598 vbr_max_bits);
1537 1599
1538 vp9_rc_set_gf_max_interval(cpi, rc); 1600 vp9_rc_set_gf_max_interval(cpi, rc);
1539 } 1601 }
1602
1603 #define VBR_PCT_ADJUSTMENT_LIMIT 50
1604 // For VBR...adjustment to the frame target based on error from previous frames
1605 static void vbr_rate_correction(VP9_COMP *cpi,
1606 int *this_frame_target,
1607 int64_t vbr_bits_off_target) {
1608 int max_delta;
1609 double position_factor = 1.0;
1610
1611 // How far through the clip are we.
1612 // This number is used to damp the per frame rate correction.
1613 // Range 0 - 1.0
1614 if (cpi->twopass.total_stats.count) {
1615 position_factor = sqrt((double)cpi->common.current_video_frame /
1616 cpi->twopass.total_stats.count);
1617 }
1618 max_delta = (int)(position_factor *
1619 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
1620
1621 // vbr_bits_off_target > 0 means we have extra bits to spend
1622 if (vbr_bits_off_target > 0) {
1623 *this_frame_target +=
1624 (vbr_bits_off_target > max_delta) ? max_delta
1625 : (int)vbr_bits_off_target;
1626 } else {
1627 *this_frame_target -=
1628 (vbr_bits_off_target < -max_delta) ? max_delta
1629 : (int)-vbr_bits_off_target;
1630 }
1631 }
1632
1633 void vp9_set_target_rate(VP9_COMP *cpi) {
1634 RATE_CONTROL *const rc = &cpi->rc;
1635 int target_rate = rc->base_frame_target;
1636
1637 // Correction to rate target based on prior over or under shoot.
1638 if (cpi->oxcf.rc_mode == VPX_VBR)
1639 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target);
1640 vp9_rc_set_frame_target(cpi, target_rate);
1641 }
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