00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/intmath.h"
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "ratecontrol.h"
00032 #include "mpegvideo.h"
00033 #include "libavutil/eval.h"
00034
00035 #undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
00036 #include <assert.h>
00037
00038 #ifndef M_E
00039 #define M_E 2.718281828
00040 #endif
00041
00042 static int init_pass2(MpegEncContext *s);
00043 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num);
00044
00045 void ff_write_pass1_stats(MpegEncContext *s){
00046 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",
00047 s->current_picture_ptr->f.display_picture_number, s->current_picture_ptr->f.coded_picture_number, s->pict_type,
00048 s->current_picture.f.quality, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits,
00049 s->f_code, s->b_code, s->current_picture.mc_mb_var_sum, s->current_picture.mb_var_sum, s->i_count, s->skip_count, s->header_bits);
00050 }
00051
00052 static inline double qp2bits(RateControlEntry *rce, double qp){
00053 if(qp<=0.0){
00054 av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
00055 }
00056 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ qp;
00057 }
00058
00059 static inline double bits2qp(RateControlEntry *rce, double bits){
00060 if(bits<0.9){
00061 av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
00062 }
00063 return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ bits;
00064 }
00065
00066 int ff_rate_control_init(MpegEncContext *s)
00067 {
00068 RateControlContext *rcc= &s->rc_context;
00069 int i, res;
00070 static const char * const const_names[]={
00071 "PI",
00072 "E",
00073 "iTex",
00074 "pTex",
00075 "tex",
00076 "mv",
00077 "fCode",
00078 "iCount",
00079 "mcVar",
00080 "var",
00081 "isI",
00082 "isP",
00083 "isB",
00084 "avgQP",
00085 "qComp",
00086
00087
00088
00089
00090 "avgIITex",
00091 "avgPITex",
00092 "avgPPTex",
00093 "avgBPTex",
00094 "avgTex",
00095 NULL
00096 };
00097 static double (* const func1[])(void *, double)={
00098 (void *)bits2qp,
00099 (void *)qp2bits,
00100 NULL
00101 };
00102 static const char * const func1_names[]={
00103 "bits2qp",
00104 "qp2bits",
00105 NULL
00106 };
00107 emms_c();
00108
00109 res = av_expr_parse(&rcc->rc_eq_eval, s->avctx->rc_eq ? s->avctx->rc_eq : "tex^qComp", const_names, func1_names, func1, NULL, NULL, 0, s->avctx);
00110 if (res < 0) {
00111 av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->avctx->rc_eq);
00112 return res;
00113 }
00114
00115 for(i=0; i<5; i++){
00116 rcc->pred[i].coeff= FF_QP2LAMBDA * 7.0;
00117 rcc->pred[i].count= 1.0;
00118
00119 rcc->pred[i].decay= 0.4;
00120 rcc->i_cplx_sum [i]=
00121 rcc->p_cplx_sum [i]=
00122 rcc->mv_bits_sum[i]=
00123 rcc->qscale_sum [i]=
00124 rcc->frame_count[i]= 1;
00125 rcc->last_qscale_for[i]=FF_QP2LAMBDA * 5;
00126 }
00127 rcc->buffer_index= s->avctx->rc_initial_buffer_occupancy;
00128 if (!rcc->buffer_index)
00129 rcc->buffer_index = s->avctx->rc_buffer_size * 3 / 4;
00130
00131 if(s->flags&CODEC_FLAG_PASS2){
00132 int i;
00133 char *p;
00134
00135
00136 p= s->avctx->stats_in;
00137 for(i=-1; p; i++){
00138 p= strchr(p+1, ';');
00139 }
00140 i+= s->max_b_frames;
00141 if(i<=0 || i>=INT_MAX / sizeof(RateControlEntry))
00142 return -1;
00143 rcc->entry = av_mallocz(i*sizeof(RateControlEntry));
00144 rcc->num_entries= i;
00145
00146
00147 for(i=0; i<rcc->num_entries; i++){
00148 RateControlEntry *rce= &rcc->entry[i];
00149 rce->pict_type= rce->new_pict_type=AV_PICTURE_TYPE_P;
00150 rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2;
00151 rce->misc_bits= s->mb_num + 10;
00152 rce->mb_var_sum= s->mb_num*100;
00153 }
00154
00155
00156 p= s->avctx->stats_in;
00157 for(i=0; i<rcc->num_entries - s->max_b_frames; i++){
00158 RateControlEntry *rce;
00159 int picture_number;
00160 int e;
00161 char *next;
00162
00163 next= strchr(p, ';');
00164 if(next){
00165 (*next)=0;
00166 next++;
00167 }
00168 e= sscanf(p, " in:%d ", &picture_number);
00169
00170 assert(picture_number >= 0);
00171 assert(picture_number < rcc->num_entries);
00172 rce= &rcc->entry[picture_number];
00173
00174 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",
00175 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits,
00176 &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count, &rce->skip_count, &rce->header_bits);
00177 if(e!=14){
00178 av_log(s->avctx, AV_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e);
00179 return -1;
00180 }
00181
00182 p= next;
00183 }
00184
00185 if(init_pass2(s) < 0) return -1;
00186
00187
00188 if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) {
00189 #if CONFIG_LIBXVID
00190 return ff_xvid_rate_control_init(s);
00191 #else
00192 av_log(s->avctx, AV_LOG_ERROR, "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
00193 return -1;
00194 #endif
00195 }
00196 }
00197
00198 if(!(s->flags&CODEC_FLAG_PASS2)){
00199
00200 rcc->short_term_qsum=0.001;
00201 rcc->short_term_qcount=0.001;
00202
00203 rcc->pass1_rc_eq_output_sum= 0.001;
00204 rcc->pass1_wanted_bits=0.001;
00205
00206 if(s->avctx->qblur > 1.0){
00207 av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n");
00208 return -1;
00209 }
00210
00211 if(s->avctx->rc_initial_cplx){
00212 for(i=0; i<60*30; i++){
00213 double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num;
00214 RateControlEntry rce;
00215
00216 if (i%((s->gop_size+3)/4)==0) rce.pict_type= AV_PICTURE_TYPE_I;
00217 else if(i%(s->max_b_frames+1)) rce.pict_type= AV_PICTURE_TYPE_B;
00218 else rce.pict_type= AV_PICTURE_TYPE_P;
00219
00220 rce.new_pict_type= rce.pict_type;
00221 rce.mc_mb_var_sum= bits*s->mb_num/100000;
00222 rce.mb_var_sum = s->mb_num;
00223 rce.qscale = FF_QP2LAMBDA * 2;
00224 rce.f_code = 2;
00225 rce.b_code = 1;
00226 rce.misc_bits= 1;
00227
00228 if(s->pict_type== AV_PICTURE_TYPE_I){
00229 rce.i_count = s->mb_num;
00230 rce.i_tex_bits= bits;
00231 rce.p_tex_bits= 0;
00232 rce.mv_bits= 0;
00233 }else{
00234 rce.i_count = 0;
00235 rce.i_tex_bits= 0;
00236 rce.p_tex_bits= bits*0.9;
00237 rce.mv_bits= bits*0.1;
00238 }
00239 rcc->i_cplx_sum [rce.pict_type] += rce.i_tex_bits*rce.qscale;
00240 rcc->p_cplx_sum [rce.pict_type] += rce.p_tex_bits*rce.qscale;
00241 rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
00242 rcc->frame_count[rce.pict_type] ++;
00243
00244 get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum, i);
00245 rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base));
00246 }
00247 }
00248
00249 }
00250
00251 return 0;
00252 }
00253
00254 void ff_rate_control_uninit(MpegEncContext *s)
00255 {
00256 RateControlContext *rcc= &s->rc_context;
00257 emms_c();
00258
00259 av_expr_free(rcc->rc_eq_eval);
00260 av_freep(&rcc->entry);
00261
00262 #if CONFIG_LIBXVID
00263 if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
00264 ff_xvid_rate_control_uninit(s);
00265 #endif
00266 }
00267
00268 int ff_vbv_update(MpegEncContext *s, int frame_size){
00269 RateControlContext *rcc= &s->rc_context;
00270 const double fps= 1/av_q2d(s->avctx->time_base);
00271 const int buffer_size= s->avctx->rc_buffer_size;
00272 const double min_rate= s->avctx->rc_min_rate/fps;
00273 const double max_rate= s->avctx->rc_max_rate/fps;
00274
00275
00276 if(buffer_size){
00277 int left;
00278
00279 rcc->buffer_index-= frame_size;
00280 if(rcc->buffer_index < 0){
00281 av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n");
00282 rcc->buffer_index= 0;
00283 }
00284
00285 left= buffer_size - rcc->buffer_index - 1;
00286 rcc->buffer_index += av_clip(left, min_rate, max_rate);
00287
00288 if(rcc->buffer_index > buffer_size){
00289 int stuffing= ceil((rcc->buffer_index - buffer_size)/8);
00290
00291 if(stuffing < 4 && s->codec_id == CODEC_ID_MPEG4)
00292 stuffing=4;
00293 rcc->buffer_index -= 8*stuffing;
00294
00295 if(s->avctx->debug & FF_DEBUG_RC)
00296 av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
00297
00298 return stuffing;
00299 }
00300 }
00301 return 0;
00302 }
00303
00307 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num){
00308 RateControlContext *rcc= &s->rc_context;
00309 AVCodecContext *a= s->avctx;
00310 double q, bits;
00311 const int pict_type= rce->new_pict_type;
00312 const double mb_num= s->mb_num;
00313 int i;
00314
00315 double const_values[]={
00316 M_PI,
00317 M_E,
00318 rce->i_tex_bits*rce->qscale,
00319 rce->p_tex_bits*rce->qscale,
00320 (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale,
00321 rce->mv_bits/mb_num,
00322 rce->pict_type == AV_PICTURE_TYPE_B ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
00323 rce->i_count/mb_num,
00324 rce->mc_mb_var_sum/mb_num,
00325 rce->mb_var_sum/mb_num,
00326 rce->pict_type == AV_PICTURE_TYPE_I,
00327 rce->pict_type == AV_PICTURE_TYPE_P,
00328 rce->pict_type == AV_PICTURE_TYPE_B,
00329 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
00330 a->qcompress,
00331
00332
00333
00334
00335 rcc->i_cplx_sum[AV_PICTURE_TYPE_I] / (double)rcc->frame_count[AV_PICTURE_TYPE_I],
00336 rcc->i_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
00337 rcc->p_cplx_sum[AV_PICTURE_TYPE_P] / (double)rcc->frame_count[AV_PICTURE_TYPE_P],
00338 rcc->p_cplx_sum[AV_PICTURE_TYPE_B] / (double)rcc->frame_count[AV_PICTURE_TYPE_B],
00339 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
00340 0
00341 };
00342
00343 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
00344 if (isnan(bits)) {
00345 av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->avctx->rc_eq);
00346 return -1;
00347 }
00348
00349 rcc->pass1_rc_eq_output_sum+= bits;
00350 bits*=rate_factor;
00351 if(bits<0.0) bits=0.0;
00352 bits+= 1.0;
00353
00354
00355 for(i=0; i<s->avctx->rc_override_count; i++){
00356 RcOverride *rco= s->avctx->rc_override;
00357 if(rco[i].start_frame > frame_num) continue;
00358 if(rco[i].end_frame < frame_num) continue;
00359
00360 if(rco[i].qscale)
00361 bits= qp2bits(rce, rco[i].qscale);
00362 else
00363 bits*= rco[i].quality_factor;
00364 }
00365
00366 q= bits2qp(rce, bits);
00367
00368
00369 if (pict_type==AV_PICTURE_TYPE_I && s->avctx->i_quant_factor<0.0)
00370 q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
00371 else if(pict_type==AV_PICTURE_TYPE_B && s->avctx->b_quant_factor<0.0)
00372 q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
00373 if(q<1) q=1;
00374
00375 return q;
00376 }
00377
00378 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){
00379 RateControlContext *rcc= &s->rc_context;
00380 AVCodecContext *a= s->avctx;
00381 const int pict_type= rce->new_pict_type;
00382 const double last_p_q = rcc->last_qscale_for[AV_PICTURE_TYPE_P];
00383 const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type];
00384
00385 if (pict_type==AV_PICTURE_TYPE_I && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==AV_PICTURE_TYPE_P))
00386 q= last_p_q *FFABS(a->i_quant_factor) + a->i_quant_offset;
00387 else if(pict_type==AV_PICTURE_TYPE_B && a->b_quant_factor>0.0)
00388 q= last_non_b_q* a->b_quant_factor + a->b_quant_offset;
00389 if(q<1) q=1;
00390
00391
00392 if(rcc->last_non_b_pict_type==pict_type || pict_type!=AV_PICTURE_TYPE_I){
00393 double last_q= rcc->last_qscale_for[pict_type];
00394 const int maxdiff= FF_QP2LAMBDA * a->max_qdiff;
00395
00396 if (q > last_q + maxdiff) q= last_q + maxdiff;
00397 else if(q < last_q - maxdiff) q= last_q - maxdiff;
00398 }
00399
00400 rcc->last_qscale_for[pict_type]= q;
00401
00402 if(pict_type!=AV_PICTURE_TYPE_B)
00403 rcc->last_non_b_pict_type= pict_type;
00404
00405 return q;
00406 }
00407
00411 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type){
00412 int qmin= s->avctx->lmin;
00413 int qmax= s->avctx->lmax;
00414
00415 assert(qmin <= qmax);
00416
00417 if(pict_type==AV_PICTURE_TYPE_B){
00418 qmin= (int)(qmin*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
00419 qmax= (int)(qmax*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
00420 }else if(pict_type==AV_PICTURE_TYPE_I){
00421 qmin= (int)(qmin*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
00422 qmax= (int)(qmax*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
00423 }
00424
00425 qmin= av_clip(qmin, 1, FF_LAMBDA_MAX);
00426 qmax= av_clip(qmax, 1, FF_LAMBDA_MAX);
00427
00428 if(qmax<qmin) qmax= qmin;
00429
00430 *qmin_ret= qmin;
00431 *qmax_ret= qmax;
00432 }
00433
00434 static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num){
00435 RateControlContext *rcc= &s->rc_context;
00436 int qmin, qmax;
00437 const int pict_type= rce->new_pict_type;
00438 const double buffer_size= s->avctx->rc_buffer_size;
00439 const double fps= 1/av_q2d(s->avctx->time_base);
00440 const double min_rate= s->avctx->rc_min_rate / fps;
00441 const double max_rate= s->avctx->rc_max_rate / fps;
00442
00443 get_qminmax(&qmin, &qmax, s, pict_type);
00444
00445
00446 if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==AV_PICTURE_TYPE_P)
00447 q*= s->avctx->rc_qmod_amp;
00448
00449
00450
00451 if(buffer_size){
00452 double expected_size= rcc->buffer_index;
00453 double q_limit;
00454
00455 if(min_rate){
00456 double d= 2*(buffer_size - expected_size)/buffer_size;
00457 if(d>1.0) d=1.0;
00458 else if(d<0.0001) d=0.0001;
00459 q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
00460
00461 q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index) * s->avctx->rc_min_vbv_overflow_use, 1));
00462 if(q > q_limit){
00463 if(s->avctx->debug&FF_DEBUG_RC){
00464 av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
00465 }
00466 q= q_limit;
00467 }
00468 }
00469
00470 if(max_rate){
00471 double d= 2*expected_size/buffer_size;
00472 if(d>1.0) d=1.0;
00473 else if(d<0.0001) d=0.0001;
00474 q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
00475
00476 q_limit= bits2qp(rce, FFMAX(rcc->buffer_index * s->avctx->rc_max_available_vbv_use, 1));
00477 if(q < q_limit){
00478 if(s->avctx->debug&FF_DEBUG_RC){
00479 av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
00480 }
00481 q= q_limit;
00482 }
00483 }
00484 }
00485
00486 if(s->avctx->rc_qsquish==0.0 || qmin==qmax){
00487 if (q<qmin) q=qmin;
00488 else if(q>qmax) q=qmax;
00489 }else{
00490 double min2= log(qmin);
00491 double max2= log(qmax);
00492
00493 q= log(q);
00494 q= (q - min2)/(max2-min2) - 0.5;
00495 q*= -4.0;
00496 q= 1.0/(1.0 + exp(q));
00497 q= q*(max2-min2) + min2;
00498
00499 q= exp(q);
00500 }
00501
00502 return q;
00503 }
00504
00505
00506
00507
00508 static double predict_size(Predictor *p, double q, double var)
00509 {
00510 return p->coeff*var / (q*p->count);
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 static void update_predictor(Predictor *p, double q, double var, double size)
00522 {
00523 double new_coeff= size*q / (var + 1);
00524 if(var<10) return;
00525
00526 p->count*= p->decay;
00527 p->coeff*= p->decay;
00528 p->count++;
00529 p->coeff+= new_coeff;
00530 }
00531
00532 static void adaptive_quantization(MpegEncContext *s, double q){
00533 int i;
00534 const float lumi_masking= s->avctx->lumi_masking / (128.0*128.0);
00535 const float dark_masking= s->avctx->dark_masking / (128.0*128.0);
00536 const float temp_cplx_masking= s->avctx->temporal_cplx_masking;
00537 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
00538 const float p_masking = s->avctx->p_masking;
00539 const float border_masking = s->avctx->border_masking;
00540 float bits_sum= 0.0;
00541 float cplx_sum= 0.0;
00542 float *cplx_tab = av_malloc(s->mb_num * sizeof(*cplx_tab));
00543 float *bits_tab = av_malloc(s->mb_num * sizeof(*bits_tab));
00544 const int qmin= s->avctx->mb_lmin;
00545 const int qmax= s->avctx->mb_lmax;
00546 Picture * const pic= &s->current_picture;
00547 const int mb_width = s->mb_width;
00548 const int mb_height = s->mb_height;
00549
00550 for(i=0; i<s->mb_num; i++){
00551 const int mb_xy= s->mb_index2xy[i];
00552 float temp_cplx= sqrt(pic->mc_mb_var[mb_xy]);
00553 float spat_cplx= sqrt(pic->mb_var[mb_xy]);
00554 const int lumi= pic->mb_mean[mb_xy];
00555 float bits, cplx, factor;
00556 int mb_x = mb_xy % s->mb_stride;
00557 int mb_y = mb_xy / s->mb_stride;
00558 int mb_distance;
00559 float mb_factor = 0.0;
00560 #if 0
00561 if(spat_cplx < q/3) spat_cplx= q/3;
00562 if(temp_cplx < q/3) temp_cplx= q/3;
00563 #endif
00564 if(spat_cplx < 4) spat_cplx= 4;
00565 if(temp_cplx < 4) temp_cplx= 4;
00566
00567 if((s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTRA)){
00568 cplx= spat_cplx;
00569 factor= 1.0 + p_masking;
00570 }else{
00571 cplx= temp_cplx;
00572 factor= pow(temp_cplx, - temp_cplx_masking);
00573 }
00574 factor*=pow(spat_cplx, - spatial_cplx_masking);
00575
00576 if(lumi>127)
00577 factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking);
00578 else
00579 factor*= (1.0 - (lumi-128)*(lumi-128)*dark_masking);
00580
00581 if(mb_x < mb_width/5){
00582 mb_distance = mb_width/5 - mb_x;
00583 mb_factor = (float)mb_distance / (float)(mb_width/5);
00584 }else if(mb_x > 4*mb_width/5){
00585 mb_distance = mb_x - 4*mb_width/5;
00586 mb_factor = (float)mb_distance / (float)(mb_width/5);
00587 }
00588 if(mb_y < mb_height/5){
00589 mb_distance = mb_height/5 - mb_y;
00590 mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5));
00591 }else if(mb_y > 4*mb_height/5){
00592 mb_distance = mb_y - 4*mb_height/5;
00593 mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5));
00594 }
00595
00596 factor*= 1.0 - border_masking*mb_factor;
00597
00598 if(factor<0.00001) factor= 0.00001;
00599
00600 bits= cplx*factor;
00601 cplx_sum+= cplx;
00602 bits_sum+= bits;
00603 cplx_tab[i]= cplx;
00604 bits_tab[i]= bits;
00605 }
00606
00607
00608 if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
00609 float factor= bits_sum/cplx_sum;
00610 for(i=0; i<s->mb_num; i++){
00611 float newq= q*cplx_tab[i]/bits_tab[i];
00612 newq*= factor;
00613
00614 if (newq > qmax){
00615 bits_sum -= bits_tab[i];
00616 cplx_sum -= cplx_tab[i]*q/qmax;
00617 }
00618 else if(newq < qmin){
00619 bits_sum -= bits_tab[i];
00620 cplx_sum -= cplx_tab[i]*q/qmin;
00621 }
00622 }
00623 if(bits_sum < 0.001) bits_sum= 0.001;
00624 if(cplx_sum < 0.001) cplx_sum= 0.001;
00625 }
00626
00627 for(i=0; i<s->mb_num; i++){
00628 const int mb_xy= s->mb_index2xy[i];
00629 float newq= q*cplx_tab[i]/bits_tab[i];
00630 int intq;
00631
00632 if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
00633 newq*= bits_sum/cplx_sum;
00634 }
00635
00636 intq= (int)(newq + 0.5);
00637
00638 if (intq > qmax) intq= qmax;
00639 else if(intq < qmin) intq= qmin;
00640
00641
00642 s->lambda_table[mb_xy]= intq;
00643 }
00644
00645 av_free(cplx_tab);
00646 av_free(bits_tab);
00647 }
00648
00649 void ff_get_2pass_fcode(MpegEncContext *s){
00650 RateControlContext *rcc= &s->rc_context;
00651 int picture_number= s->picture_number;
00652 RateControlEntry *rce;
00653
00654 rce= &rcc->entry[picture_number];
00655 s->f_code= rce->f_code;
00656 s->b_code= rce->b_code;
00657 }
00658
00659
00660
00661 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
00662 {
00663 float q;
00664 int qmin, qmax;
00665 float br_compensation;
00666 double diff;
00667 double short_term_q;
00668 double fps;
00669 int picture_number= s->picture_number;
00670 int64_t wanted_bits;
00671 RateControlContext *rcc= &s->rc_context;
00672 AVCodecContext *a= s->avctx;
00673 RateControlEntry local_rce, *rce;
00674 double bits;
00675 double rate_factor;
00676 int var;
00677 const int pict_type= s->pict_type;
00678 Picture * const pic= &s->current_picture;
00679 emms_c();
00680
00681 #if CONFIG_LIBXVID
00682 if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
00683 return ff_xvid_rate_estimate_qscale(s, dry_run);
00684 #endif
00685
00686 get_qminmax(&qmin, &qmax, s, pict_type);
00687
00688 fps= 1/av_q2d(s->avctx->time_base);
00689
00690
00691 if(picture_number>2 && !dry_run){
00692 const int last_var= s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
00693 update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
00694 }
00695
00696 if(s->flags&CODEC_FLAG_PASS2){
00697 assert(picture_number>=0);
00698 assert(picture_number<rcc->num_entries);
00699 rce= &rcc->entry[picture_number];
00700 wanted_bits= rce->expected_bits;
00701 }else{
00702 Picture *dts_pic;
00703 rce= &local_rce;
00704
00705
00706
00707 if(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay)
00708 dts_pic= s->current_picture_ptr;
00709 else
00710 dts_pic= s->last_picture_ptr;
00711
00712
00713
00714
00715 if (!dts_pic || dts_pic->f.pts == AV_NOPTS_VALUE)
00716 wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
00717 else
00718 wanted_bits = (uint64_t)(s->bit_rate*(double)dts_pic->f.pts / fps);
00719 }
00720
00721 diff= s->total_bits - wanted_bits;
00722 br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance;
00723 if(br_compensation<=0.0) br_compensation=0.001;
00724
00725 var= pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum;
00726
00727 short_term_q = 0;
00728 if(s->flags&CODEC_FLAG_PASS2){
00729 if(pict_type!=AV_PICTURE_TYPE_I)
00730 assert(pict_type == rce->new_pict_type);
00731
00732 q= rce->new_qscale / br_compensation;
00733
00734 }else{
00735 rce->pict_type=
00736 rce->new_pict_type= pict_type;
00737 rce->mc_mb_var_sum= pic->mc_mb_var_sum;
00738 rce->mb_var_sum = pic-> mb_var_sum;
00739 rce->qscale = FF_QP2LAMBDA * 2;
00740 rce->f_code = s->f_code;
00741 rce->b_code = s->b_code;
00742 rce->misc_bits= 1;
00743
00744 bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
00745 if(pict_type== AV_PICTURE_TYPE_I){
00746 rce->i_count = s->mb_num;
00747 rce->i_tex_bits= bits;
00748 rce->p_tex_bits= 0;
00749 rce->mv_bits= 0;
00750 }else{
00751 rce->i_count = 0;
00752 rce->i_tex_bits= 0;
00753 rce->p_tex_bits= bits*0.9;
00754
00755 rce->mv_bits= bits*0.1;
00756 }
00757 rcc->i_cplx_sum [pict_type] += rce->i_tex_bits*rce->qscale;
00758 rcc->p_cplx_sum [pict_type] += rce->p_tex_bits*rce->qscale;
00759 rcc->mv_bits_sum[pict_type] += rce->mv_bits;
00760 rcc->frame_count[pict_type] ++;
00761
00762 bits= rce->i_tex_bits + rce->p_tex_bits;
00763 rate_factor= rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum * br_compensation;
00764
00765 q= get_qscale(s, rce, rate_factor, picture_number);
00766 if (q < 0)
00767 return -1;
00768
00769 assert(q>0.0);
00770
00771 q= get_diff_limited_q(s, rce, q);
00772
00773 assert(q>0.0);
00774
00775 if(pict_type==AV_PICTURE_TYPE_P || s->intra_only){
00776 rcc->short_term_qsum*=a->qblur;
00777 rcc->short_term_qcount*=a->qblur;
00778
00779 rcc->short_term_qsum+= q;
00780 rcc->short_term_qcount++;
00781
00782 q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount;
00783
00784 }
00785 assert(q>0.0);
00786
00787 q= modify_qscale(s, rce, q, picture_number);
00788
00789 rcc->pass1_wanted_bits+= s->bit_rate/fps;
00790
00791 assert(q>0.0);
00792 }
00793
00794 if(s->avctx->debug&FF_DEBUG_RC){
00795 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",
00796 av_get_picture_type_char(pict_type), qmin, q, qmax, picture_number, (int)wanted_bits/1000, (int)s->total_bits/1000,
00797 br_compensation, short_term_q, s->frame_bits, pic->mb_var_sum, pic->mc_mb_var_sum, s->bit_rate/1000, (int)fps
00798 );
00799 }
00800
00801 if (q<qmin) q=qmin;
00802 else if(q>qmax) q=qmax;
00803
00804 if(s->adaptive_quant)
00805 adaptive_quantization(s, q);
00806 else
00807 q= (int)(q + 0.5);
00808
00809 if(!dry_run){
00810 rcc->last_qscale= q;
00811 rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum;
00812 rcc->last_mb_var_sum= pic->mb_var_sum;
00813 }
00814 return q;
00815 }
00816
00817
00818
00819
00820 static int init_pass2(MpegEncContext *s)
00821 {
00822 RateControlContext *rcc= &s->rc_context;
00823 AVCodecContext *a= s->avctx;
00824 int i, toobig;
00825 double fps= 1/av_q2d(s->avctx->time_base);
00826 double complexity[5]={0,0,0,0,0};
00827 uint64_t const_bits[5]={0,0,0,0,0};
00828 uint64_t all_const_bits;
00829 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
00830 double rate_factor=0;
00831 double step;
00832
00833 const int filter_size= (int)(a->qblur*4) | 1;
00834 double expected_bits;
00835 double *qscale, *blurred_qscale, qscale_sum;
00836
00837
00838 for(i=0; i<rcc->num_entries; i++){
00839 RateControlEntry *rce= &rcc->entry[i];
00840
00841 rce->new_pict_type= rce->pict_type;
00842 rcc->i_cplx_sum [rce->pict_type] += rce->i_tex_bits*rce->qscale;
00843 rcc->p_cplx_sum [rce->pict_type] += rce->p_tex_bits*rce->qscale;
00844 rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
00845 rcc->frame_count[rce->pict_type] ++;
00846
00847 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
00848 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
00849 }
00850 all_const_bits= const_bits[AV_PICTURE_TYPE_I] + const_bits[AV_PICTURE_TYPE_P] + const_bits[AV_PICTURE_TYPE_B];
00851
00852 if(all_available_bits < all_const_bits){
00853 av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
00854 return -1;
00855 }
00856
00857 qscale= av_malloc(sizeof(double)*rcc->num_entries);
00858 blurred_qscale= av_malloc(sizeof(double)*rcc->num_entries);
00859 toobig = 0;
00860
00861 for(step=256*256; step>0.0000001; step*=0.5){
00862 expected_bits=0;
00863 rate_factor+= step;
00864
00865 rcc->buffer_index= s->avctx->rc_buffer_size/2;
00866
00867
00868 for(i=0; i<rcc->num_entries; i++){
00869 RateControlEntry *rce= &rcc->entry[i];
00870 qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i);
00871 rcc->last_qscale_for[rce->pict_type] = qscale[i];
00872 }
00873 assert(filter_size%2==1);
00874
00875
00876 for(i=FFMAX(0, rcc->num_entries-300); i<rcc->num_entries; i++){
00877 RateControlEntry *rce= &rcc->entry[i];
00878
00879 qscale[i]= get_diff_limited_q(s, rce, qscale[i]);
00880 }
00881
00882 for(i=rcc->num_entries-1; i>=0; i--){
00883 RateControlEntry *rce= &rcc->entry[i];
00884
00885 qscale[i]= get_diff_limited_q(s, rce, qscale[i]);
00886 }
00887
00888
00889 for(i=0; i<rcc->num_entries; i++){
00890 RateControlEntry *rce= &rcc->entry[i];
00891 const int pict_type= rce->new_pict_type;
00892 int j;
00893 double q=0.0, sum=0.0;
00894
00895 for(j=0; j<filter_size; j++){
00896 int index= i+j-filter_size/2;
00897 double d= index-i;
00898 double coeff= a->qblur==0 ? 1.0 : exp(-d*d/(a->qblur * a->qblur));
00899
00900 if(index < 0 || index >= rcc->num_entries) continue;
00901 if(pict_type != rcc->entry[index].new_pict_type) continue;
00902 q+= qscale[index] * coeff;
00903 sum+= coeff;
00904 }
00905 blurred_qscale[i]= q/sum;
00906 }
00907
00908
00909 for(i=0; i<rcc->num_entries; i++){
00910 RateControlEntry *rce= &rcc->entry[i];
00911 double bits;
00912 rce->new_qscale= modify_qscale(s, rce, blurred_qscale[i], i);
00913 bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
00914
00915 bits += 8*ff_vbv_update(s, bits);
00916
00917 rce->expected_bits= expected_bits;
00918 expected_bits += bits;
00919 }
00920
00921
00922
00923
00924
00925
00926 if(expected_bits > all_available_bits) {
00927 rate_factor-= step;
00928 ++toobig;
00929 }
00930 }
00931 av_free(qscale);
00932 av_free(blurred_qscale);
00933
00934
00935 qscale_sum = 0.0;
00936 for(i=0; i<rcc->num_entries; i++){
00937
00938
00939 qscale_sum += av_clip(rcc->entry[i].new_qscale / FF_QP2LAMBDA, s->avctx->qmin, s->avctx->qmax);
00940 }
00941 assert(toobig <= 40);
00942 av_log(s->avctx, AV_LOG_DEBUG,
00943 "[lavc rc] requested bitrate: %d bps expected bitrate: %d bps\n",
00944 s->bit_rate,
00945 (int)(expected_bits / ((double)all_available_bits/s->bit_rate)));
00946 av_log(s->avctx, AV_LOG_DEBUG,
00947 "[lavc rc] estimated target average qp: %.3f\n",
00948 (float)qscale_sum / rcc->num_entries);
00949 if (toobig == 0) {
00950 av_log(s->avctx, AV_LOG_INFO,
00951 "[lavc rc] Using all of requested bitrate is not "
00952 "necessary for this video with these parameters.\n");
00953 } else if (toobig == 40) {
00954 av_log(s->avctx, AV_LOG_ERROR,
00955 "[lavc rc] Error: bitrate too low for this video "
00956 "with these parameters.\n");
00957 return -1;
00958 } else if (fabs(expected_bits/all_available_bits - 1.0) > 0.01) {
00959 av_log(s->avctx, AV_LOG_ERROR,
00960 "[lavc rc] Error: 2pass curve failed to converge\n");
00961 return -1;
00962 }
00963
00964 return 0;
00965 }