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