long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits;
long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1);
static int rate_estimate_qscale(MpegEncContext *s){#if 1    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;    int last_qscale= s->qscale;    double fps;    INT64 wanted_bits;    emms_c();    fps= (double)s->frame_rate / FRAME_RATE_BASE;    wanted_bits= s->bit_rate*(double)s->picture_number/fps;        if(s->picture_number>2){        /* update predictors */        if(s->last_pict_type == I_TYPE){        //FIXME        }else{ //P Frame//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->qscale, s->last_mc_mb_var, s->frame_bits);        }    }    if(s->pict_type == I_TYPE){        //FIXME        rate_q= s->qsum/s->qcount;    }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/wanted_bits;//    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;    qscale= (int)(q + 0.5);    if     (qscale<qmin) qscale=qmin;    else if(qscale>qmax) qscale=qmax;        if     (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff;    else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff;    s->qsum+= qscale;    s->qcount++;    s->last_pict_type= s->pict_type;//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, //       rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits);//printf("%d %d\n", s->bit_rate, (int)fps);    return qscale;#else    INT64 diff, total_bits = s->total_bits;    float q;    int qscale;    if (s->pict_type == I_TYPE) {        s->wanted_bits += s->I_frame_bits;    } else {        s->wanted_bits += s->P_frame_bits;    }    diff = s->wanted_bits - total_bits;    q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);    /* adjust for I frame */    if (s->pict_type == I_TYPE && !s->intra_only) {        q /= I_FRAME_SIZE_RATIO;    }    /* using a too small Q scale leeds to problems in mpeg1 and h263       because AC coefficients are clamped to 255 or 127 */    qmin = 3;    if (q < qmin)        q = qmin;    else if (q > 31)        q = 31;    qscale = (int)(q + 0.5);#if defined(DEBUG)    printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n",            s->picture_number,            (double)total_bits,            (double)s->wanted_bits,           (float)s->frame_rate / FRAME_RATE_BASE *            total_bits / s->picture_number,            (int)diff, q);#endif    return qscale;#endif}