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