comparison ratecontrol.c @ 3681:4f547f0da680 libavcodec

second pass encoding changes: - warn rather than fail when specified bitrate need not be used - fail with a useful message when specified bitrate is too low - print potentially useful information if verbose patch based mostly on suggestions from Michael Niedermayer and Loren Merritt
author corey
date Tue, 05 Sep 2006 15:46:06 +0000
parents dac97d2d69d5
children acf9ca729bd2
comparison
equal deleted inserted replaced
3680:7690bafea6e0 3681:4f547f0da680
788 788
789 static int init_pass2(MpegEncContext *s) 789 static int init_pass2(MpegEncContext *s)
790 { 790 {
791 RateControlContext *rcc= &s->rc_context; 791 RateControlContext *rcc= &s->rc_context;
792 AVCodecContext *a= s->avctx; 792 AVCodecContext *a= s->avctx;
793 int i; 793 int i, toobig;
794 double fps= 1/av_q2d(s->avctx->time_base); 794 double fps= 1/av_q2d(s->avctx->time_base);
795 double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1 795 double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1
796 uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits 796 uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits
797 uint64_t all_const_bits; 797 uint64_t all_const_bits;
798 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps); 798 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
799 double rate_factor=0; 799 double rate_factor=0;
800 double step; 800 double step;
801 //int last_i_frame=-10000000; 801 //int last_i_frame=-10000000;
802 const int filter_size= (int)(a->qblur*4) | 1; 802 const int filter_size= (int)(a->qblur*4) | 1;
803 double expected_bits; 803 double expected_bits;
804 double *qscale, *blured_qscale; 804 double *qscale, *blured_qscale, qscale_sum;
805 805
806 /* find complexity & const_bits & decide the pict_types */ 806 /* find complexity & const_bits & decide the pict_types */
807 for(i=0; i<rcc->num_entries; i++){ 807 for(i=0; i<rcc->num_entries; i++){
808 RateControlEntry *rce= &rcc->entry[i]; 808 RateControlEntry *rce= &rcc->entry[i];
809 809
823 return -1; 823 return -1;
824 } 824 }
825 825
826 qscale= av_malloc(sizeof(double)*rcc->num_entries); 826 qscale= av_malloc(sizeof(double)*rcc->num_entries);
827 blured_qscale= av_malloc(sizeof(double)*rcc->num_entries); 827 blured_qscale= av_malloc(sizeof(double)*rcc->num_entries);
828 toobig = 0;
828 829
829 for(step=256*256; step>0.0000001; step*=0.5){ 830 for(step=256*256; step>0.0000001; step*=0.5){
830 expected_bits=0; 831 expected_bits=0;
831 rate_factor+= step; 832 rate_factor+= step;
832 833
876 877
877 rce->expected_bits= expected_bits; 878 rce->expected_bits= expected_bits;
878 expected_bits += bits; 879 expected_bits += bits;
879 } 880 }
880 881
881 // printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor); 882 /*
882 if(expected_bits > all_available_bits) rate_factor-= step; 883 av_log(s->avctx, AV_LOG_INFO,
884 "expected_bits: %f all_available_bits: %d rate_factor: %f\n",
885 expected_bits, (int)all_available_bits, rate_factor);
886 */
887 if(expected_bits > all_available_bits) {
888 rate_factor-= step;
889 ++toobig;
890 }
883 } 891 }
884 av_free(qscale); 892 av_free(qscale);
885 av_free(blured_qscale); 893 av_free(blured_qscale);
886 894
887 if(fabs(expected_bits/all_available_bits - 1.0) > 0.01 ){ 895 /* check bitrate calculations and print info */
888 av_log(s->avctx, AV_LOG_ERROR, "Error: 2pass curve failed to converge\n"); 896 qscale_sum = 0.0;
897 for(i=0; i<rcc->num_entries; i++){
898 /* av_log(s->avctx, AV_LOG_DEBUG, "[lavc rc] entry[%d].new_qscale = %.3f qp = %.3f\n",
899 i, rcc->entry[i].new_qscale, rcc->entry[i].new_qscale / FF_QP2LAMBDA); */
900 qscale_sum += clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA, s->avctx->qmin, s->avctx->qmax);
901 }
902 assert(toobig <= 40);
903 av_log(s->avctx, AV_LOG_DEBUG,
904 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
905 s->bit_rate,
906 (int)(expected_bits / ((double)all_available_bits/s->bit_rate)));
907 av_log(s->avctx, AV_LOG_DEBUG,
908 "[lavc rc] estimated target average qp: %.3f\n",
909 (float)qscale_sum / rcc->num_entries);
910 if (toobig == 0) {
911 av_log(s->avctx, AV_LOG_INFO,
912 "[lavc rc] Using all of requested bitrate is not "
913 "necessary for this video with these parameters.\n");
914 } else if (toobig == 40) {
915 av_log(s->avctx, AV_LOG_ERROR,
916 "[lavc rc] Error: bitrate too low for this video "
917 "with these parameters.\n");
889 return -1; 918 return -1;
919 } else if (fabs(expected_bits/all_available_bits - 1.0) > 0.01) {
920 av_log(s->avctx, AV_LOG_ERROR,
921 "[lavc rc] Error: 2pass curve failed to converge\n");
922 return -1;
890 } 923 }
891 924
892 return 0; 925 return 0;
893 } 926 }