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