if(qmin<2) qmin=2;
if(qmin<1) qmin=1;
static int init_pass2(MpegEncContext *s){    RateControlContext *rcc= &s->rc_context;    int i;    double fps= (double)s->frame_rate / FRAME_RATE_BASE;    double complexity[5]={0,0,0,0,0};   // aproximate bits at quant=1    double avg_quantizer[5];    uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits    uint64_t available_bits[5];    uint64_t all_const_bits;    uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);    int num_frames[5]={0,0,0,0,0};    double rate_factor=0;    double step;    int last_i_frame=-10000000;    /* find complexity & const_bits & decide the pict_types */    for(i=0; i<rcc->num_entries; i++){        RateControlEntry *rce= &rcc->entry[i];                if(s->b_frame_strategy==0 || s->max_b_frames==0){            rce->new_pict_type= rce->pict_type;        }else{            int j;            int next_non_b_type=P_TYPE;            switch(rce->pict_type){            case I_TYPE:                if(i-last_i_frame>s->gop_size/2){ //FIXME this is not optimal                    rce->new_pict_type= I_TYPE;                    last_i_frame= i;                }else{                    rce->new_pict_type= P_TYPE; // will be caught by the scene detection anyway                }                break;            case P_TYPE:                rce->new_pict_type= P_TYPE;                break;            case B_TYPE:                for(j=i+1; j<i+s->max_b_frames+2 && j<rcc->num_entries; j++){                    if(rcc->entry[j].pict_type != B_TYPE){                        next_non_b_type= rcc->entry[j].pict_type;                        break;                    }                }                if(next_non_b_type==I_TYPE)                    rce->new_pict_type= P_TYPE;                else                    rce->new_pict_type= B_TYPE;                break;            }        }        complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;        const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;        num_frames[rce->new_pict_type]++;    }    all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];        if(all_available_bits < all_const_bits){        fprintf(stderr, "requested bitrate is to low\n");        return -1;    }//    avg_complexity= complexity/rcc->num_entries;    avg_quantizer[P_TYPE]=     avg_quantizer[I_TYPE]=   (complexity[I_TYPE]+complexity[P_TYPE] + complexity[B_TYPE]/s->b_quant_factor)                            / (all_available_bits - all_const_bits);    avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*s->b_quant_factor;//printf("avg quantizer: %f %f\n", avg_quantizer[P_TYPE], avg_quantizer[B_TYPE]);    for(i=0; i<5; i++){        available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i];    }//printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);        for(step=256*256; step>0.0000001; step*=0.5){        uint64_t expected_bits=0;        rate_factor+= step;        /* find qscale */        for(i=0; i<rcc->num_entries; i++){            RateControlEntry *rce= &rcc->entry[i];            double short_term_q, q, bits_left;            const int pict_type= rce->new_pict_type;            int qmin= s->qmin;            int qmax= s->qmax;            if(pict_type==B_TYPE){                qmin= (int)(qmin*s->b_quant_factor+0.5);                qmax= (int)(qmax*s->b_quant_factor+0.5);            }            if(qmin<2) qmin=2;            if(qmax>31) qmax=31;            if(qmax<=qmin) qmax= qmin;                        switch(s->rc_strategy){            case 0:                bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor - rce->misc_bits - rce->mv_bits;                if(bits_left<1.0) bits_left=1.0;                short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;                break;            case 1:                bits_left= (available_bits[pict_type] - const_bits[pict_type])/num_frames[pict_type]*rate_factor;                if(bits_left<1.0) bits_left=1.0;                short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;                break;            case 2:                bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor;                if(bits_left<1.0) bits_left=1.0;                short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits + rce->mv_bits)/bits_left;                break;            default:                fprintf(stderr, "unknown strategy\n");                short_term_q=3; //gcc warning fix            }            if(short_term_q>31.0) short_term_q=31.0;            else if (short_term_q<1.0) short_term_q=1.0;            q= 1/((1/avg_quantizer[pict_type] - 1/short_term_q)*s->qcompress + 1/short_term_q);            if     (q<qmin) q=qmin;            else if(q>qmax) q=qmax;//printf("lq:%f, sq:%f t:%f q:%f\n", avg_quantizer[rce->pict_type], short_term_q, bits_left, q);            rce->new_qscale= q;        }        /* smooth curve */            /* find expected bits */        for(i=0; i<rcc->num_entries; i++){            RateControlEntry *rce= &rcc->entry[i];            double factor= rce->qscale / rce->new_qscale;                        rce->expected_bits= expected_bits;            expected_bits += (int)(rce->misc_bits + rce->mv_bits + (rce->i_tex_bits + rce->p_tex_bits)*factor + 0.5);        }//        printf("%d %d %f\n", (int)expected_bits, (int)all_available_bits, rate_factor);        if(expected_bits > all_available_bits) rate_factor-= step;    }    return 0;}