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

Side by Side Diff: source/libvpx/vp8/encoder/ratectrl.c

Issue 7671004: Update libvpx snapshot to v0.9.7-p1 (Cayuga). (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: '' Created 9 years, 4 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/vp8/encoder/ratectrl.h ('k') | source/libvpx/vp8/encoder/rdopt.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 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 void vp8_setup_key_frame(VP8_COMP *cpi) 303 void vp8_setup_key_frame(VP8_COMP *cpi)
304 { 304 {
305 // Setup for Key frame: 305 // Setup for Key frame:
306 306
307 vp8_default_coef_probs(& cpi->common); 307 vp8_default_coef_probs(& cpi->common);
308 vp8_kf_default_bmode_probs(cpi->common.kf_bmode_prob); 308 vp8_kf_default_bmode_probs(cpi->common.kf_bmode_prob);
309 309
310 vpx_memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv _context)); 310 vpx_memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv _context));
311 { 311 {
312 int flag[2] = {1, 1}; 312 int flag[2] = {1, 1};
313 vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flag); 313 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi- >common.fc.mvc, flag);
314 } 314 }
315 315
316 vpx_memset(cpi->common.fc.pre_mvc, 0, sizeof(cpi->common.fc.pre_mvc)); //in itialize pre_mvc to all zero. 316 vpx_memset(cpi->common.fc.pre_mvc, 0, sizeof(cpi->common.fc.pre_mvc)); //in itialize pre_mvc to all zero.
317 317
318 //cpi->common.filter_level = 0; // Reset every key frame. 318 //cpi->common.filter_level = 0; // Reset every key frame.
319 cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ; 319 cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ;
320 320
321 // Provisional interval before next GF 321 // Provisional interval before next GF
322 if (cpi->auto_gold) 322 if (cpi->auto_gold)
323 //cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL; 323 //cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
324 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 324 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
325 else 325 else
326 cpi->frames_till_gf_update_due = cpi->goldfreq; 326 cpi->frames_till_gf_update_due = cpi->goldfreq;
327 327
328 cpi->common.refresh_golden_frame = TRUE; 328 cpi->common.refresh_golden_frame = TRUE;
329 cpi->common.refresh_alt_ref_frame = TRUE; 329 cpi->common.refresh_alt_ref_frame = TRUE;
330 } 330 }
331 331
332 void vp8_calc_auto_iframe_target_size(VP8_COMP *cpi) 332
333 static int estimate_bits_at_q(int frame_kind, int Q, int MBs,
334 double correction_factor)
335 {
336 int Bpm = (int)(.5 + correction_factor * vp8_bits_per_mb[frame_kind][Q]);
337
338 /* Attempt to retain reasonable accuracy without overflow. The cutoff is
339 * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
340 * largest Bpm takes 20 bits.
341 */
342 if (MBs > (1 << 11))
343 return (Bpm >> BPER_MB_NORMBITS) * MBs;
344 else
345 return (Bpm * MBs) >> BPER_MB_NORMBITS;
346 }
347
348
349 static void calc_iframe_target_size(VP8_COMP *cpi)
333 { 350 {
334 // boost defaults to half second 351 // boost defaults to half second
335 int kf_boost; 352 int kf_boost;
353 int target;
336 354
337 // Clear down mmx registers to allow floating point in what follows 355 // Clear down mmx registers to allow floating point in what follows
338 vp8_clear_system_state(); //__asm emms; 356 vp8_clear_system_state(); //__asm emms;
339 357
340 if (cpi->oxcf.fixed_q >= 0) 358 if (cpi->oxcf.fixed_q >= 0)
341 { 359 {
342 vp8_calc_iframe_target_size(cpi); 360 int Q = cpi->oxcf.key_q;
343 return; 361
362 target = estimate_bits_at_q(INTRA_FRAME, Q, cpi->common.MBs,
363 cpi->key_frame_rate_correction_factor);
344 } 364 }
365 else if (cpi->pass == 2)
366 {
367 // New Two pass RC
368 target = cpi->per_frame_bandwidth;
369 }
370 // First Frame is a special case
371 else if (cpi->common.current_video_frame == 0)
372 {
373 /* 1 Pass there is no information on which to base size so use
374 * bandwidth per second * fraction of the initial buffer
375 * level
376 */
377 target = cpi->oxcf.starting_buffer_level / 2;
345 378
346 if (cpi->pass == 2) 379 if(target > cpi->oxcf.target_bandwidth * 3 / 2)
347 { 380 target = cpi->oxcf.target_bandwidth * 3 / 2;
348 cpi->this_frame_target = cpi->per_frame_bandwidth; // New Two pass RC
349 } 381 }
350 else 382 else
351 { 383 {
384 // if this keyframe was forced, use a more recent Q estimate
385 int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY)
386 ? cpi->avg_frame_qindex : cpi->ni_av_qi;
387
352 // Boost depends somewhat on frame rate 388 // Boost depends somewhat on frame rate
353 kf_boost = (int)(2 * cpi->output_frame_rate - 16); 389 kf_boost = (int)(2 * cpi->output_frame_rate - 16);
354 390
355 // adjustment up based on q 391 // adjustment up based on q
356 kf_boost = kf_boost * kf_boost_qadjustment[cpi->ni_av_qi] / 100; 392 kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100;
357 393
358 // frame separation adjustment ( down) 394 // frame separation adjustment ( down)
359 if (cpi->frames_since_key < cpi->output_frame_rate / 2) 395 if (cpi->frames_since_key < cpi->output_frame_rate / 2)
360 kf_boost = (int)(kf_boost * cpi->frames_since_key / (cpi->output_fra me_rate / 2)); 396 kf_boost = (int)(kf_boost
397 * cpi->frames_since_key / (cpi->output_frame_rate / 2));
361 398
362 if (kf_boost < 16) 399 if (kf_boost < 16)
363 kf_boost = 16; 400 kf_boost = 16;
364 401
365 // Reset the active worst quality to the baseline value for key frames. 402 target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4;
366 cpi->active_worst_quality = cpi->worst_quality;
367
368 cpi->this_frame_target = ((16 + kf_boost) * cpi->per_frame_bandwidth) > > 4;
369 } 403 }
370 404
371 405
372 // Should the next frame be an altref frame 406 if (cpi->oxcf.rc_max_intra_bitrate_pct)
373 if (cpi->pass != 2)
374 { 407 {
375 // For now Alt ref is not allowed except in 2 pass modes. 408 unsigned int max_rate = cpi->per_frame_bandwidth
376 cpi->source_alt_ref_pending = FALSE; 409 * cpi->oxcf.rc_max_intra_bitrate_pct / 100;
377 410
378 /*if ( cpi->oxcf.fixed_q == -1) 411 if (target > max_rate)
379 { 412 target = max_rate;
380 if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_TH RESH*cpi->frames_till_gf_update_due)) ) )
381 cpi->source_alt_ref_pending = TRUE;
382 else
383 cpi->source_alt_ref_pending = FALSE;
384 }*/
385 } 413 }
386 414
387 if (0) 415 cpi->this_frame_target = target;
416
417 // TODO: if we separate rate targeting from Q targetting, move this.
418 // Reset the active worst quality to the baseline value for key frames.
419 if (cpi->pass != 2)
420 cpi->active_worst_quality = cpi->worst_quality;
421
422 #if 0
388 { 423 {
389 FILE *f; 424 FILE *f;
390 425
391 f = fopen("kf_boost.stt", "a"); 426 f = fopen("kf_boost.stt", "a");
392 //fprintf(f, " %8d %10d %10d %10d %10d %10d %10d\n", 427 //fprintf(f, " %8d %10d %10d %10d %10d %10d %10d\n",
393 // cpi->common.current_video_frame, cpi->target_bandwidth, cpi->frames _to_key, kf_boost_qadjustment[cpi->ni_av_qi], cpi->kf_boost, (cpi->this_frame_ta rget *100 / cpi->per_frame_bandwidth), cpi->this_frame_target ); 428 // cpi->common.current_video_frame, cpi->target_bandwidth, cpi->frames _to_key, kf_boost_qadjustment[cpi->ni_av_qi], cpi->kf_boost, (cpi->this_frame_ta rget *100 / cpi->per_frame_bandwidth), cpi->this_frame_target );
394 429
395 fprintf(f, " %8u %10d %10d %10d\n", 430 fprintf(f, " %8u %10d %10d %10d\n",
396 cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_ gf_interval, cpi->source_alt_ref_pending); 431 cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_ gf_interval, cpi->source_alt_ref_pending);
397 432
398 fclose(f); 433 fclose(f);
399 } 434 }
435 #endif
400 } 436 }
401 437
438
402 // Do the best we can to define the parameteres for the next GF based on what i nformation we have available. 439 // Do the best we can to define the parameteres for the next GF based on what i nformation we have available.
403 static void calc_gf_params(VP8_COMP *cpi) 440 static void calc_gf_params(VP8_COMP *cpi)
404 { 441 {
405 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed _q; 442 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed _q;
406 int Boost = 0; 443 int Boost = 0;
407 444
408 int gf_frame_useage = 0; // Golden frame useage since last GF 445 int gf_frame_useage = 0; // Golden frame useage since last GF
409 int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] + 446 int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] +
410 cpi->recent_ref_frame_usage[LAST_FRAME] + 447 cpi->recent_ref_frame_usage[LAST_FRAME] +
411 cpi->recent_ref_frame_usage[GOLDEN_FRAME] + 448 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 594
558 /*if ( cpi->oxcf.fixed_q == -1) 595 /*if ( cpi->oxcf.fixed_q == -1)
559 { 596 {
560 if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + (AF_THRES H*cpi->frames_till_gf_update_due)) ) ) 597 if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + (AF_THRES H*cpi->frames_till_gf_update_due)) ) )
561 cpi->source_alt_ref_pending = TRUE; 598 cpi->source_alt_ref_pending = TRUE;
562 else 599 else
563 cpi->source_alt_ref_pending = FALSE; 600 cpi->source_alt_ref_pending = FALSE;
564 }*/ 601 }*/
565 } 602 }
566 } 603 }
567 /* This is equvialent to estimate_bits_at_q without the rate_correction_factor. */
568 static int baseline_bits_at_q(int frame_kind, int Q, int MBs)
569 {
570 int Bpm = vp8_bits_per_mb[frame_kind][Q];
571
572 /* Attempt to retain reasonable accuracy without overflow. The cutoff is
573 * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
574 * largest Bpm takes 20 bits.
575 */
576 if (MBs > (1 << 11))
577 return (Bpm >> BPER_MB_NORMBITS) * MBs;
578 else
579 return (Bpm * MBs) >> BPER_MB_NORMBITS;
580 }
581
582 void vp8_calc_iframe_target_size(VP8_COMP *cpi)
583 {
584 int Q;
585 int Boost = 100;
586
587 Q = (cpi->oxcf.fixed_q >= 0) ? cpi->oxcf.fixed_q : cpi->avg_frame_qindex;
588
589 if (cpi->auto_adjust_key_quantizer == 1)
590 {
591 // If (auto_adjust_key_quantizer==1) then a lower Q is selected for key- frames.
592 // The enhanced Q is calculated so as to boost the key frame size by a f actor
593 // specified in kf_boost_qadjustment. Also, can adjust based on distance
594 // between key frames.
595
596 // Adjust boost based upon ambient Q
597 Boost = kf_boost_qadjustment[Q];
598
599 // Make the Key frame boost less if the seperation from the previous key frame is small
600 if (cpi->frames_since_key < 16)
601 Boost = Boost * kf_boost_seperation_adjustment[cpi->frames_since_key ] / 100;
602 else
603 Boost = Boost * kf_boost_seperation_adjustment[15] / 100;
604
605 // Apply limits on boost
606 if (Boost > kf_gf_boost_qlimits[Q])
607 Boost = kf_gf_boost_qlimits[Q];
608 else if (Boost < 120)
609 Boost = 120;
610 }
611
612 // Keep a record of the boost that was used
613 cpi->last_boost = Boost;
614
615 // Should the next frame be an altref frame
616 if (cpi->pass != 2)
617 {
618 // For now Alt ref is not allowed except in 2 pass modes.
619 cpi->source_alt_ref_pending = FALSE;
620
621 /*if ( cpi->oxcf.fixed_q == -1)
622 {
623 if ( cpi->oxcf.play_alternate && ( (cpi->last_boost/2) > (100+(AF_TH RESH*cpi->frames_till_gf_update_due)) ) )
624 cpi->source_alt_ref_pending = TRUE;
625 else
626 cpi->source_alt_ref_pending = FALSE;
627 }*/
628 }
629
630 if (cpi->oxcf.fixed_q >= 0)
631 {
632 cpi->this_frame_target = (baseline_bits_at_q(0, Q, cpi->common.MBs) * Bo ost) / 100;
633 }
634 else
635 {
636
637 int bits_per_mb_at_this_q ;
638
639 if (cpi->oxcf.error_resilient_mode == 1)
640 {
641 cpi->this_frame_target = 2 * cpi->av_per_frame_bandwidth;
642 return;
643 }
644
645 // Rate targetted scenario:
646 // Be careful of 32-bit OVERFLOW if restructuring the caluclation of cpi ->this_frame_target
647 bits_per_mb_at_this_q = (int)(.5 +
648 cpi->key_frame_rate_correction_factor * vp 8_bits_per_mb[0][Q]);
649
650 cpi->this_frame_target = (((bits_per_mb_at_this_q * cpi->common.MBs) >> BPER_MB_NORMBITS) * Boost) / 100;
651
652 // Reset the active worst quality to the baseline value for key frames.
653 if (cpi->pass < 2)
654 cpi->active_worst_quality = cpi->worst_quality;
655 }
656 }
657 604
658 605
659 606 static void calc_pframe_target_size(VP8_COMP *cpi)
660 void vp8_calc_pframe_target_size(VP8_COMP *cpi)
661 { 607 {
662 int min_frame_target; 608 int min_frame_target;
663 int Adjustment; 609 int Adjustment;
664 610
665 // Set the min frame bandwidth.
666 //min_frame_target = estimate_min_frame_size( cpi );
667 min_frame_target = 0; 611 min_frame_target = 0;
668 612
669 if (cpi->pass == 2) 613 if (cpi->pass == 2)
670 { 614 {
671 min_frame_target = cpi->min_frame_bandwidth; 615 min_frame_target = cpi->min_frame_bandwidth;
672 616
673 if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) 617 if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5))
674 min_frame_target = cpi->av_per_frame_bandwidth >> 5; 618 min_frame_target = cpi->av_per_frame_bandwidth >> 5;
675 } 619 }
676 else if (min_frame_target < cpi->per_frame_bandwidth / 4) 620 else if (min_frame_target < cpi->per_frame_bandwidth / 4)
677 min_frame_target = cpi->per_frame_bandwidth / 4; 621 min_frame_target = cpi->per_frame_bandwidth / 4;
678 622
679 623
680 // Special alt reference frame case 624 // Special alt reference frame case
681 if (cpi->common.refresh_alt_ref_frame) 625 if (cpi->common.refresh_alt_ref_frame)
682 { 626 {
683 if (cpi->pass == 2) 627 if (cpi->pass == 2)
684 { 628 {
685 cpi->per_frame_bandwidth = cpi->gf_bits; // Pe r frame bit target for the alt ref frame 629 cpi->per_frame_bandwidth = cpi->twopass.gf_bits; // Per frame bit target for the alt ref frame
686 cpi->this_frame_target = cpi->per_frame_bandwidth; 630 cpi->this_frame_target = cpi->per_frame_bandwidth;
687 } 631 }
688 632
689 /* One Pass ??? TBD */ 633 /* One Pass ??? TBD */
690 /*else 634 /*else
691 { 635 {
692 int frames_in_section; 636 int frames_in_section;
693 int allocation_chunks; 637 int allocation_chunks;
694 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->ox cf.fixed_q; 638 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->ox cf.fixed_q;
695 int alt_boost; 639 int alt_boost;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 Adjustment = (cpi->this_frame_target - min_frame_target); 754 Adjustment = (cpi->this_frame_target - min_frame_target);
811 755
812 if (cpi->common.frames_since_golden == (cpi->current_gf_interval >> 1)) 756 if (cpi->common.frames_since_golden == (cpi->current_gf_interval >> 1))
813 cpi->this_frame_target += ((cpi->current_gf_interval - 1) * Adjustment); 757 cpi->this_frame_target += ((cpi->current_gf_interval - 1) * Adjustment);
814 else 758 else
815 cpi->this_frame_target -= Adjustment; 759 cpi->this_frame_target -= Adjustment;
816 } 760 }
817 } 761 }
818 } 762 }
819 763
820 // Set a reduced data rate target for our initial Q calculation.
821 // This should help to save bits during earier sections.
822 if ((cpi->oxcf.under_shoot_pct > 0) && (cpi->oxcf.under_shoot_pct <= 100))
823 cpi->this_frame_target = (cpi->this_frame_target * cpi->oxcf.under_shoot _pct) / 100;
824
825 // Sanity check that the total sum of adjustments is not above the maximum a llowed 764 // Sanity check that the total sum of adjustments is not above the maximum a llowed
826 // That is that having allowed for KF and GF penalties we have not pushed th e 765 // That is that having allowed for KF and GF penalties we have not pushed th e
827 // current interframe target to low. If the adjustment we apply here is not capable of recovering 766 // current interframe target to low. If the adjustment we apply here is not capable of recovering
828 // all the extra bits we have spent in the KF or GF then the remainder will have to be recovered over 767 // all the extra bits we have spent in the KF or GF then the remainder will have to be recovered over
829 // a longer time span via other buffer / rate control mechanisms. 768 // a longer time span via other buffer / rate control mechanisms.
830 if (cpi->this_frame_target < min_frame_target) 769 if (cpi->this_frame_target < min_frame_target)
831 cpi->this_frame_target = min_frame_target; 770 cpi->this_frame_target = min_frame_target;
832 771
833 if (!cpi->common.refresh_alt_ref_frame) 772 if (!cpi->common.refresh_alt_ref_frame)
834 // Note the baseline target data rate for this inter frame. 773 // Note the baseline target data rate for this inter frame.
(...skipping 16 matching lines...) Expand all
851 // 790 //
852 // If we are are below the optimal buffer fullness level and adh erence 791 // If we are are below the optimal buffer fullness level and adh erence
853 // to buffering contraints is important to the end useage then a djust 792 // to buffering contraints is important to the end useage then a djust
854 // the per frame target. 793 // the per frame target.
855 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && 794 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
856 (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) 795 (cpi->buffer_level < cpi->oxcf.optimal_buffer_level))
857 { 796 {
858 percent_low = 797 percent_low =
859 (cpi->oxcf.optimal_buffer_level - cpi->buffer_level) / 798 (cpi->oxcf.optimal_buffer_level - cpi->buffer_level) /
860 one_percent_bits; 799 one_percent_bits;
861
862 if (percent_low > 100)
863 percent_low = 100;
864 else if (percent_low < 0)
865 percent_low = 0;
866 } 800 }
867 // Are we overshooting the long term clip data rate... 801 // Are we overshooting the long term clip data rate...
868 else if (cpi->bits_off_target < 0) 802 else if (cpi->bits_off_target < 0)
869 { 803 {
870 // Adjust per frame data target downwards to compensate. 804 // Adjust per frame data target downwards to compensate.
871 percent_low = (int)(100 * -cpi->bits_off_target / 805 percent_low = (int)(100 * -cpi->bits_off_target /
872 (cpi->total_byte_count * 8)); 806 (cpi->total_byte_count * 8));
873
874 if (percent_low > 100)
875 percent_low = 100;
876 else if (percent_low < 0)
877 percent_low = 0;
878 } 807 }
879 808
809 if (percent_low > cpi->oxcf.under_shoot_pct)
810 percent_low = cpi->oxcf.under_shoot_pct;
811 else if (percent_low < 0)
812 percent_low = 0;
813
880 // lower the target bandwidth for this frame. 814 // lower the target bandwidth for this frame.
881 cpi->this_frame_target = 815 cpi->this_frame_target -= (cpi->this_frame_target * percent_low)
882 (cpi->this_frame_target * (100 - (percent_low / 2))) / 100; 816 / 200;
883 817
884 // Are we using allowing control of active_worst_allowed_q 818 // Are we using allowing control of active_worst_allowed_q
885 // according to buffer level. 819 // according to buffer level.
886 if (cpi->auto_worst_q) 820 if (cpi->auto_worst_q)
887 { 821 {
888 int critical_buffer_level; 822 int critical_buffer_level;
889 823
890 // For streaming applications the most important factor is 824 // For streaming applications the most important factor is
891 // cpi->buffer_level as this takes into account the 825 // cpi->buffer_level as this takes into account the
892 // specified short term buffering constraints. However, 826 // specified short term buffering constraints. However,
(...skipping 16 matching lines...) Expand all
909 critical_buffer_level = cpi->bits_off_target; 843 critical_buffer_level = cpi->bits_off_target;
910 } 844 }
911 845
912 // Set the active worst quality based upon the selected 846 // Set the active worst quality based upon the selected
913 // buffer fullness number. 847 // buffer fullness number.
914 if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) 848 if (critical_buffer_level < cpi->oxcf.optimal_buffer_level)
915 { 849 {
916 if ( critical_buffer_level > 850 if ( critical_buffer_level >
917 (cpi->oxcf.optimal_buffer_level >> 2) ) 851 (cpi->oxcf.optimal_buffer_level >> 2) )
918 { 852 {
919 INT64 qadjustment_range = 853 int64_t qadjustment_range =
920 cpi->worst_quality - cpi->ni_av_qi; 854 cpi->worst_quality - cpi->ni_av_qi;
921 INT64 above_base = 855 int64_t above_base =
922 (critical_buffer_level - 856 (critical_buffer_level -
923 (cpi->oxcf.optimal_buffer_level >> 2)); 857 (cpi->oxcf.optimal_buffer_level >> 2));
924 858
925 // Step active worst quality down from 859 // Step active worst quality down from
926 // cpi->ni_av_qi when (critical_buffer_level == 860 // cpi->ni_av_qi when (critical_buffer_level ==
927 // cpi->optimal_buffer_level) to 861 // cpi->optimal_buffer_level) to
928 // cpi->worst_quality when 862 // cpi->worst_quality when
929 // (critical_buffer_level == 863 // (critical_buffer_level ==
930 // cpi->optimal_buffer_level >> 2) 864 // cpi->optimal_buffer_level >> 2)
931 cpi->active_worst_quality = 865 cpi->active_worst_quality =
(...skipping 11 matching lines...) Expand all
943 cpi->active_worst_quality = cpi->ni_av_qi; 877 cpi->active_worst_quality = cpi->ni_av_qi;
944 } 878 }
945 } 879 }
946 else 880 else
947 { 881 {
948 cpi->active_worst_quality = cpi->worst_quality; 882 cpi->active_worst_quality = cpi->worst_quality;
949 } 883 }
950 } 884 }
951 else 885 else
952 { 886 {
953 int percent_high; 887 int percent_high = 0;
954 888
955 if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) 889 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
890 && (cpi->buffer_level > cpi->oxcf.optimal_buffer_level))
956 { 891 {
957 percent_high = (int)(100 * (cpi->bits_off_target - cpi->oxcf .optimal_buffer_level) / (cpi->total_byte_count * 8)); 892 percent_high = (cpi->buffer_level
893 - cpi->oxcf.optimal_buffer_level)
894 / one_percent_bits;
895 }
896 else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level)
897 {
898 percent_high = (int)((100 * cpi->bits_off_target)
899 / (cpi->total_byte_count * 8));
900 }
958 901
959 if (percent_high > 100) 902 if (percent_high > cpi->oxcf.over_shoot_pct)
960 percent_high = 100; 903 percent_high = cpi->oxcf.over_shoot_pct;
961 else if (percent_high < 0) 904 else if (percent_high < 0)
962 percent_high = 0; 905 percent_high = 0;
963 906
964 cpi->this_frame_target = (cpi->this_frame_target * (100 + (p ercent_high / 2))) / 100; 907 cpi->this_frame_target += (cpi->this_frame_target *
908 percent_high) / 200;
965 909
966 }
967 910
968 // Are we allowing control of active_worst_allowed_q according t o bufferl level. 911 // Are we allowing control of active_worst_allowed_q according t o bufferl level.
969 if (cpi->auto_worst_q) 912 if (cpi->auto_worst_q)
970 { 913 {
971 // When using the relaxed buffer model stick to the user spe cified value 914 // When using the relaxed buffer model stick to the user spe cified value
972 cpi->active_worst_quality = cpi->ni_av_qi; 915 cpi->active_worst_quality = cpi->ni_av_qi;
973 } 916 }
974 else 917 else
975 { 918 {
976 cpi->active_worst_quality = cpi->worst_quality; 919 cpi->active_worst_quality = cpi->worst_quality;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 if (0) // p_gw 1048 if (0) // p_gw
1106 { 1049 {
1107 FILE *f; 1050 FILE *f;
1108 1051
1109 f = fopen("GFexit.stt", "a"); 1052 f = fopen("GFexit.stt", "a");
1110 fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame); 1053 fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame);
1111 fclose(f); 1054 fclose(f);
1112 } 1055 }
1113 1056
1114 #endif 1057 #endif
1115 cpi->initial_gf_use = 0;
1116 1058
1117 if (cpi->auto_adjust_gold_quantizer) 1059 if (cpi->auto_adjust_gold_quantizer)
1118 { 1060 {
1119 calc_gf_params(cpi); 1061 calc_gf_params(cpi);
1120 } 1062 }
1121 1063
1122 // If we are using alternate ref instead of gf then do not apply the boost 1064 // If we are using alternate ref instead of gf then do not apply the boost
1123 // It will instead be applied to the altref update 1065 // It will instead be applied to the altref update
1124 // Jims modified boost 1066 // Jims modified boost
1125 if (!cpi->source_alt_ref_active) 1067 if (!cpi->source_alt_ref_active)
(...skipping 19 matching lines...) Expand all
1145 } 1087 }
1146 1088
1147 // Avoid loss of precision but avoid overflow 1089 // Avoid loss of precision but avoid overflow
1148 if ((bits_in_section >> 7) > allocation_chunks) 1090 if ((bits_in_section >> 7) > allocation_chunks)
1149 cpi->this_frame_target = Boost * (bits_in_section / allocation_chunks); 1091 cpi->this_frame_target = Boost * (bits_in_section / allocation_chunks);
1150 else 1092 else
1151 cpi->this_frame_target = (Boost * bits_in_section) / allocation_chunks; 1093 cpi->this_frame_target = (Boost * bits_in_section) / allocation_chunks;
1152 } 1094 }
1153 } 1095 }
1154 else 1096 else
1155 cpi->this_frame_target = (baseline_bits_at_q(1, Q, cpi->comm on.MBs) * cpi->last_boost) / 100; 1097 cpi->this_frame_target =
1098 (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0)
1099 * cpi->last_boost) / 100;
1156 1100
1157 } 1101 }
1158 // If there is an active ARF at this location use the minimum 1102 // If there is an active ARF at this location use the minimum
1159 // bits on this frame even if it is a contructed arf. 1103 // bits on this frame even if it is a contructed arf.
1160 // The active maximum quantizer insures that an appropriate 1104 // The active maximum quantizer insures that an appropriate
1161 // number of bits will be spent if needed for contstructed ARFs. 1105 // number of bits will be spent if needed for contstructed ARFs.
1162 else 1106 else
1163 { 1107 {
1164 cpi->this_frame_target = 0; 1108 cpi->this_frame_target = 0;
1165 } 1109 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1267 cpi->key_frame_rate_correction_factor = rate_correction_factor; 1211 cpi->key_frame_rate_correction_factor = rate_correction_factor;
1268 else 1212 else
1269 { 1213 {
1270 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_fram e) 1214 if (cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden_fram e)
1271 cpi->gf_rate_correction_factor = rate_correction_factor; 1215 cpi->gf_rate_correction_factor = rate_correction_factor;
1272 else 1216 else
1273 cpi->rate_correction_factor = rate_correction_factor; 1217 cpi->rate_correction_factor = rate_correction_factor;
1274 } 1218 }
1275 } 1219 }
1276 1220
1277 static int estimate_bits_at_q(VP8_COMP *cpi, int Q)
1278 {
1279 int Bpm = (int)(.5 + cpi->rate_correction_factor * vp8_bits_per_mb[INTER_FRA ME][Q]);
1280
1281 /* Attempt to retain reasonable accuracy without overflow. The cutoff is
1282 * chosen such that the maximum product of Bpm and MBs fits 31 bits. The
1283 * largest Bpm takes 20 bits.
1284 */
1285 if (cpi->common.MBs > (1 << 11))
1286 return (Bpm >> BPER_MB_NORMBITS) * cpi->common.MBs;
1287 else
1288 return (Bpm * cpi->common.MBs) >> BPER_MB_NORMBITS;
1289
1290 }
1291
1292 1221
1293 int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) 1222 int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame)
1294 { 1223 {
1295 int Q = cpi->active_worst_quality; 1224 int Q = cpi->active_worst_quality;
1296 1225
1297 // Reset Zbin OQ value 1226 // Reset Zbin OQ value
1298 cpi->zbin_over_quant = 0; 1227 cpi->zbin_over_quant = 0;
1299 1228
1300 if (cpi->oxcf.fixed_q >= 0) 1229 if (cpi->oxcf.fixed_q >= 0)
1301 { 1230 {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1412 if (bits_per_mb_at_this_q <= target_bits_per_mb) // Break out if we get down to the target rate 1341 if (bits_per_mb_at_this_q <= target_bits_per_mb) // Break out if we get down to the target rate
1413 break; 1342 break;
1414 } 1343 }
1415 1344
1416 } 1345 }
1417 } 1346 }
1418 1347
1419 return Q; 1348 return Q;
1420 } 1349 }
1421 1350
1422 static int estimate_min_frame_size(VP8_COMP *cpi) 1351
1352 static int estimate_keyframe_frequency(VP8_COMP *cpi)
1423 { 1353 {
1424 double correction_factor; 1354 int i;
1425 int bits_per_mb_at_max_q;
1426 1355
1427 // This funtion returns a default value for the first few frames untill the correction factor has had time to adapt. 1356 // Average key frame frequency
1428 if (cpi->common.current_video_frame < 10) 1357 int av_key_frame_frequency = 0;
1358
1359 /* First key frame at start of sequence is a special case. We have no
1360 * frequency data.
1361 */
1362 if (cpi->key_frame_count == 1)
1429 { 1363 {
1430 if (cpi->pass == 2) 1364 /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
1431 return (cpi->min_frame_bandwidth); 1365 * whichever is smaller.
1432 else 1366 */
1433 return cpi->per_frame_bandwidth / 3; 1367 int key_freq = cpi->oxcf.key_freq>0 ? cpi->oxcf.key_freq : 1;
1368 av_key_frame_frequency = (int)cpi->output_frame_rate * 2;
1369
1370 if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq)
1371 av_key_frame_frequency = cpi->oxcf.key_freq;
1372
1373 cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1]
1374 = av_key_frame_frequency;
1434 } 1375 }
1376 else
1377 {
1378 unsigned int total_weight = 0;
1379 int last_kf_interval =
1380 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
1435 1381
1436 /* // Select the appropriate correction factor based upon type of frame. 1382 /* reset keyframe context and calculate weighted average of last
1437 if ( cpi->common.frame_type == KEY_FRAME ) 1383 * KEY_FRAME_CONTEXT keyframes
1438 correction_factor = cpi->key_frame_rate_correction_factor; 1384 */
1439 else 1385 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1440 { 1386 {
1441 if ( cpi->common.refresh_alt_ref_frame || cpi->common.refresh_golden _frame ) 1387 if (i < KEY_FRAME_CONTEXT - 1)
1442 correction_factor = cpi->gf_rate_correction_factor; 1388 cpi->prior_key_frame_distance[i]
1389 = cpi->prior_key_frame_distance[i+1];
1443 else 1390 else
1444 correction_factor = cpi->rate_correction_factor; 1391 cpi->prior_key_frame_distance[i] = last_kf_interval;
1445 }*/
1446 1392
1447 // We estimate at half the value we get from vp8_bits_per_mb 1393 av_key_frame_frequency += prior_key_frame_weight[i]
1448 correction_factor = cpi->rate_correction_factor / 2.0; 1394 * cpi->prior_key_frame_distance[i];
1395 total_weight += prior_key_frame_weight[i];
1396 }
1449 1397
1450 bits_per_mb_at_max_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->c ommon.frame_type][MAXQ]); 1398 av_key_frame_frequency /= total_weight;
1451 1399
1452 return (bits_per_mb_at_max_q * cpi->common.MBs) >> BPER_MB_NORMBITS; 1400 }
1401 return av_key_frame_frequency;
1453 } 1402 }
1454 1403
1404
1455 void vp8_adjust_key_frame_context(VP8_COMP *cpi) 1405 void vp8_adjust_key_frame_context(VP8_COMP *cpi)
1456 { 1406 {
1457 int i;
1458 int av_key_frames_per_second;
1459
1460 // Average key frame frequency and size
1461 unsigned int total_weight = 0;
1462 unsigned int av_key_frame_frequency = 0;
1463 unsigned int av_key_frame_bits = 0;
1464
1465 unsigned int output_frame_rate = (unsigned int)(100 * cpi->output_frame_rate );
1466 unsigned int target_bandwidth = (unsigned int)(100 * cpi->target_bandwidth);
1467
1468 // Clear down mmx registers to allow floating point in what follows 1407 // Clear down mmx registers to allow floating point in what follows
1469 vp8_clear_system_state(); //__asm emms; 1408 vp8_clear_system_state();
1470
1471 // Update the count of total key frame bits
1472 cpi->tot_key_frame_bits += cpi->projected_frame_size;
1473
1474 // First key frame at start of sequence is a special case. We have no freque ncy data.
1475 if (cpi->key_frame_count == 1)
1476 {
1477 av_key_frame_frequency = (int)cpi->output_frame_rate * 2; // Assume a default of 1 kf every 2 seconds
1478 av_key_frame_bits = cpi->projected_frame_size;
1479 av_key_frames_per_second = output_frame_rate / av_key_frame_frequency; // Note output_frame_rate not cpi->output_frame_rate
1480 }
1481 else
1482 {
1483 int last_kf_interval =
1484 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
1485
1486 // reset keyframe context and calculate weighted average of last KEY_FRA ME_CONTEXT keyframes
1487 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1488 {
1489 if (i < KEY_FRAME_CONTEXT - 1)
1490 {
1491 cpi->prior_key_frame_size[i] = cpi->prior_key_frame_size[i+1 ];
1492 cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance [i+1];
1493 }
1494 else
1495 {
1496 cpi->prior_key_frame_size[i] = cpi->projected_frame_size;
1497 cpi->prior_key_frame_distance[i] = last_kf_interval;
1498 }
1499
1500 av_key_frame_bits += prior_key_frame_weight[i] * cpi->prior_key _frame_size[i];
1501 av_key_frame_frequency += prior_key_frame_weight[i] * cpi->prior_key _frame_distance[i];
1502 total_weight += prior_key_frame_weight[i];
1503 }
1504
1505 av_key_frame_bits /= total_weight;
1506 av_key_frame_frequency /= total_weight;
1507 av_key_frames_per_second = output_frame_rate / av_key_frame_frequency;
1508
1509 }
1510 1409
1511 // Do we have any key frame overspend to recover? 1410 // Do we have any key frame overspend to recover?
1512 if ((cpi->pass != 2) && (cpi->projected_frame_size > cpi->per_frame_bandwidt h)) 1411 // Two-pass overspend handled elsewhere.
1412 if ((cpi->pass != 2)
1413 && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
1513 { 1414 {
1514 // Update the count of key frame overspend to be recovered in subsequent frames 1415 int overspend;
1515 // A portion of the KF overspend is treated as gf overspend (and hence r ecovered more quickly)
1516 // as the kf is also a gf. Otherwise the few frames following each kf te nd to get more bits
1517 // allocated than those following other gfs.
1518 cpi->kf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_ba ndwidth) * 7 / 8;
1519 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_ba ndwidth) * 1 / 8;
1520 if(!av_key_frame_frequency)
1521 av_key_frame_frequency = 60;
1522 1416
1523 // Work out how much to try and recover per frame. 1417 /* Update the count of key frame overspend to be recovered in
1524 // For one pass we estimate the number of frames to spread it over based upon past history. 1418 * subsequent frames. A portion of the KF overspend is treated as gf
1525 // For two pass we know how many frames there will be till the next kf. 1419 * overspend (and hence recovered more quickly) as the kf is also a
1526 if (cpi->pass == 2) 1420 * gf. Otherwise the few frames following each kf tend to get more
1527 { 1421 * bits allocated than those following other gfs.
1528 if (cpi->frames_to_key > 16) 1422 */
1529 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)cpi-> frames_to_key; 1423 overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth);
1530 else 1424 cpi->kf_overspend_bits += overspend * 7 / 8;
1531 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / 16; 1425 cpi->gf_overspend_bits += overspend * 1 / 8;
1532 } 1426
1533 else 1427 /* Work out how much to try and recover per frame. */
1534 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)av_key_fr ame_frequency; 1428 cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits
1429 / estimate_keyframe_frequency(cpi);
1535 } 1430 }
1536 1431
1537 cpi->frames_since_key = 0; 1432 cpi->frames_since_key = 0;
1538 cpi->last_key_frame_size = cpi->projected_frame_size;
1539 cpi->key_frame_count++; 1433 cpi->key_frame_count++;
1540 } 1434 }
1541 1435
1436
1542 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit) 1437 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit)
1543 { 1438 {
1544 // Set-up bounds on acceptable frame size: 1439 // Set-up bounds on acceptable frame size:
1545 if (cpi->oxcf.fixed_q >= 0) 1440 if (cpi->oxcf.fixed_q >= 0)
1546 { 1441 {
1547 // Fixed Q scenario: frame size never outranges target (there is no targ et!) 1442 // Fixed Q scenario: frame size never outranges target (there is no targ et!)
1548 *frame_under_shoot_limit = 0; 1443 *frame_under_shoot_limit = 0;
1549 *frame_over_shoot_limit = INT_MAX; 1444 *frame_over_shoot_limit = INT_MAX;
1550 } 1445 }
1551 else 1446 else
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1598 else 1493 else
1599 { 1494 {
1600 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; 1495 *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8;
1601 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; 1496 *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8;
1602 } 1497 }
1603 } 1498 }
1604 } 1499 }
1605 } 1500 }
1606 } 1501 }
1607 } 1502 }
1503
1504
1505 // return of 0 means drop frame
1506 int vp8_pick_frame_size(VP8_COMP *cpi)
1507 {
1508 VP8_COMMON *cm = &cpi->common;
1509
1510 if (cm->frame_type == KEY_FRAME)
1511 calc_iframe_target_size(cpi);
1512 else
1513 {
1514 calc_pframe_target_size(cpi);
1515
1516 // Check if we're dropping the frame:
1517 if (cpi->drop_frame)
1518 {
1519 cpi->drop_frame = FALSE;
1520 cpi->drop_count++;
1521 return 0;
1522 }
1523 }
1524 return 1;
1525 }
OLDNEW
« no previous file with comments | « source/libvpx/vp8/encoder/ratectrl.h ('k') | source/libvpx/vp8/encoder/rdopt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698