if(qmin<2) qmin=2;
if(qmin<1) qmin=1;
int ff_rate_estimate_qscale(MpegEncContext *s){    int qmin= s->qmin;    int qmax= s->qmax;    int rate_q=5;    float q;    int qscale;    float br_compensation;    double diff;    double short_term_q;    double long_term_q;    double fps;    int picture_number= s->input_picture_number - s->max_b_frames;    int64_t wanted_bits;    emms_c();    fps= (double)s->frame_rate / FRAME_RATE_BASE;    wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);//    printf("%d %d %d\n", picture_number, (int)wanted_bits, (int)s->total_bits);        if(s->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;        /* update predictors */    if(picture_number>2){        if(s->pict_type!=B_TYPE && s->last_non_b_pict_type == P_TYPE){//printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);            update_predictor(&s->p_pred, s->last_non_b_qscale, s->last_non_b_mc_mb_var, s->pb_frame_bits);        }    }    if(s->pict_type == I_TYPE){        short_term_q= s->short_term_qsum/s->short_term_qcount;            long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0        q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);    }else if(s->pict_type==B_TYPE){        q= (int)(s->last_non_b_qscale*s->b_quant_factor+0.5);    }else{ //P Frame        int i;        int diff, best_diff=1000000000;        for(i=1; i<=31; i++){            diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;            if(diff<0) diff= -diff;            if(diff<best_diff){                best_diff= diff;                rate_q= i;            }        }        s->short_term_qsum*=s->qblur;        s->short_term_qcount*=s->qblur;        s->short_term_qsum+= rate_q;        s->short_term_qcount++;        short_term_q= s->short_term_qsum/s->short_term_qcount;            long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0//    q= (long_term_q - short_term_q)*s->qcompress + short_term_q;        q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);    }    diff= s->total_bits - wanted_bits;    br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;    if(br_compensation<=0.0) br_compensation=0.001;    q/=br_compensation;//printf("%f %f %f\n", q, br_compensation, short_term_q);    qscale= (int)(q + 0.5);    if     (qscale<qmin) qscale=qmin;    else if(qscale>qmax) qscale=qmax;        if(s->pict_type!=B_TYPE){        s->qsum+= qscale;        s->qcount++;        if     (qscale<s->last_non_b_qscale-s->max_qdiff) qscale=s->last_non_b_qscale-s->max_qdiff;        else if(qscale>s->last_non_b_qscale+s->max_qdiff) qscale=s->last_non_b_qscale+s->max_qdiff;    }//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, //       rate_q, short_term_q, s->mc_mb_var, s->frame_bits);//printf("%d %d\n", s->bit_rate, (int)fps);    return qscale;}