35 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
39 #define M_E 2.718281828
46 snprintf(s->
avctx->
stats_out, 256,
"in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d;\n",
97 static double (*
const func1[])(
void *, double)={
102 static const char *
const func1_names[]={
161 next= strchr(p,
';');
166 e= sscanf(p,
" in:%d ", &picture_number);
168 assert(picture_number >= 0);
169 assert(picture_number < rcc->num_entries);
170 rce= &rcc->
entry[picture_number];
172 e+=sscanf(p,
" in:%*d out:%*d type:%d q:%f itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d skipcount:%d hbits:%d",
210 for(i=0; i<60*30; i++){
287 int stuffing= ceil((rcc->
buffer_index - buffer_size)/8);
310 const double mb_num= s->
mb_num;
349 if(bits<0.0) bits=0.0;
356 if(rco[i].
end_frame < frame_num)
continue;
359 bits=
qp2bits(rce, rco[i].qscale);
394 if (q > last_q + maxdiff) q= last_q + maxdiff;
395 else if(q < last_q - maxdiff) q= last_q - maxdiff;
413 assert(qmin <= qmax);
426 if(qmax<qmin) qmax= qmin;
454 double d= 2*(buffer_size - expected_size)/buffer_size;
456 else if(d<0.0001) d=0.0001;
469 double d= 2*expected_size/buffer_size;
471 else if(d<0.0001) d=0.0001;
486 else if(q>qmax) q=qmax;
488 double min2= log(qmin);
489 double max2= log(qmax);
492 q= (q - min2)/(max2-min2) - 0.5;
494 q= 1.0/(1.0 + exp(q));
495 q= q*(max2-min2) + min2;
521 double new_coeff= size*q / (var + 1);
527 p->
coeff+= new_coeff;
540 float cplx_tab[s->
mb_num];
541 float bits_tab[s->
mb_num];
548 for(i=0; i<s->
mb_num; i++){
550 float temp_cplx= sqrt(pic->
mc_mb_var[mb_xy]);
551 float spat_cplx= sqrt(pic->
mb_var[mb_xy]);
552 const int lumi= pic->
mb_mean[mb_xy];
553 float bits, cplx, factor;
557 float mb_factor = 0.0;
559 if(spat_cplx < q/3) spat_cplx= q/3;
560 if(temp_cplx < q/3) temp_cplx= q/3;
562 if(spat_cplx < 4) spat_cplx= 4;
563 if(temp_cplx < 4) temp_cplx= 4;
567 factor= 1.0 + p_masking;
570 factor= pow(temp_cplx, - temp_cplx_masking);
572 factor*=pow(spat_cplx, - spatial_cplx_masking);
575 factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking);
577 factor*= (1.0 - (lumi-128)*(lumi-128)*dark_masking);
579 if(mb_x < mb_width/5){
580 mb_distance = mb_width/5 - mb_x;
581 mb_factor = (float)mb_distance / (
float)(mb_width/5);
582 }
else if(mb_x > 4*mb_width/5){
583 mb_distance = mb_x - 4*mb_width/5;
584 mb_factor = (float)mb_distance / (
float)(mb_width/5);
586 if(mb_y < mb_height/5){
587 mb_distance = mb_height/5 - mb_y;
588 mb_factor =
FFMAX(mb_factor, (
float)mb_distance / (
float)(mb_height/5));
589 }
else if(mb_y > 4*mb_height/5){
590 mb_distance = mb_y - 4*mb_height/5;
591 mb_factor =
FFMAX(mb_factor, (
float)mb_distance / (
float)(mb_height/5));
594 factor*= 1.0 - border_masking*mb_factor;
596 if(factor<0.00001) factor= 0.00001;
607 float factor= bits_sum/cplx_sum;
608 for(i=0; i<s->
mb_num; i++){
609 float newq= q*cplx_tab[i]/bits_tab[i];
613 bits_sum -= bits_tab[i];
614 cplx_sum -= cplx_tab[i]*q/qmax;
616 else if(newq < qmin){
617 bits_sum -= bits_tab[i];
618 cplx_sum -= cplx_tab[i]*q/qmin;
621 if(bits_sum < 0.001) bits_sum= 0.001;
622 if(cplx_sum < 0.001) cplx_sum= 0.001;
625 for(i=0; i<s->
mb_num; i++){
627 float newq= q*cplx_tab[i]/bits_tab[i];
631 newq*= bits_sum/cplx_sum;
634 intq= (int)(newq + 0.5);
636 if (intq > qmax) intq= qmax;
637 else if(intq < qmin) intq= qmin;
649 rce= &rcc->
entry[picture_number];
660 float br_compensation;
686 if(picture_number>2 && !dry_run){
692 assert(picture_number>=0);
693 assert(picture_number<rcc->num_entries);
694 rce= &rcc->
entry[picture_number];
711 wanted_bits= (uint64_t)(s->
bit_rate*(
double)picture_number/fps);
713 wanted_bits = (uint64_t)(s->
bit_rate*(
double)dts_pic->
f.
pts / fps);
718 if(br_compensation<=0.0) br_compensation=0.001;
760 q=
get_qscale(s, rce, rate_factor, picture_number);
790 av_log(s->
avctx,
AV_LOG_DEBUG,
"%c qp:%d<%2.1f<%d %d want:%d total:%d comp:%f st_q:%2.2f size:%d var:%d/%d br:%d fps:%d\n",
797 else if(q>qmax) q=qmax;
821 double complexity[5]={0,0,0,0,0};
822 uint64_t const_bits[5]={0,0,0,0,0};
823 uint64_t all_const_bits;
825 double rate_factor=0;
828 const int filter_size= (int)(a->
qblur*4) | 1;
829 double expected_bits;
830 double *qscale, *blurred_qscale, qscale_sum;
847 if(all_available_bits < all_const_bits){
856 for(step=256*256; step>0.0000001; step*=0.5){
868 assert(filter_size%2==1);
882 double q=0.0, sum=0.0;
884 for(j=0; j<filter_size; j++){
885 int index= i+j-filter_size/2;
889 if(index < 0 || index >= rcc->
num_entries)
continue;
891 q+= qscale[
index] * coeff;
894 blurred_qscale[i]= q/sum;
907 expected_bits +=
bits;
915 if(expected_bits > all_available_bits) {
930 assert(toobig <= 40);
932 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
934 (
int)(expected_bits / ((
double)all_available_bits/s->
bit_rate)));
936 "[lavc rc] estimated target average qp: %.3f\n",
940 "[lavc rc] Using all of requested bitrate is not "
941 "necessary for this video with these parameters.\n");
942 }
else if (toobig == 40) {
944 "[lavc rc] Error: bitrate too low for this video "
945 "with these parameters.\n");
947 }
else if (fabs(expected_bits/all_available_bits - 1.0) > 0.01) {
949 "[lavc rc] Error: 2pass curve failed to converge\n");