make ff_rate_control_init() bail out if rc_strategy==1 and lavc wasn't
[ffmpeg.git] / libavcodec / ratecontrol.c
1 /*
2  * Rate control for video encoders
3  *
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file ratecontrol.c
23  * Rate control for video encoders.
24  */
25
26 #include "avcodec.h"
27 #include "dsputil.h"
28 #include "mpegvideo.h"
29
30 #undef NDEBUG // allways check asserts, the speed effect is far too small to disable them
31 #include <assert.h>
32
33 #ifndef M_E
34 #define M_E 2.718281828
35 #endif
36
37 static int init_pass2(MpegEncContext *s);
38 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num);
39
40 void ff_write_pass1_stats(MpegEncContext *s){
41     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",
42             s->current_picture_ptr->display_picture_number, s->current_picture_ptr->coded_picture_number, s->pict_type,
43             s->current_picture.quality, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits,
44             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);
45 }
46
47 int ff_rate_control_init(MpegEncContext *s)
48 {
49     RateControlContext *rcc= &s->rc_context;
50     int i;
51     emms_c();
52
53     for(i=0; i<5; i++){
54         rcc->pred[i].coeff= FF_QP2LAMBDA * 7.0;
55         rcc->pred[i].count= 1.0;
56
57         rcc->pred[i].decay= 0.4;
58         rcc->i_cplx_sum [i]=
59         rcc->p_cplx_sum [i]=
60         rcc->mv_bits_sum[i]=
61         rcc->qscale_sum [i]=
62         rcc->frame_count[i]= 1; // 1 is better cuz of 1/0 and such
63         rcc->last_qscale_for[i]=FF_QP2LAMBDA * 5;
64     }
65     rcc->buffer_index= s->avctx->rc_initial_buffer_occupancy;
66
67     if(s->flags&CODEC_FLAG_PASS2){
68         int i;
69         char *p;
70
71         /* find number of pics */
72         p= s->avctx->stats_in;
73         for(i=-1; p; i++){
74             p= strchr(p+1, ';');
75         }
76         i+= s->max_b_frames;
77         if(i<=0 || i>=INT_MAX / sizeof(RateControlEntry))
78             return -1;
79         rcc->entry = (RateControlEntry*)av_mallocz(i*sizeof(RateControlEntry));
80         rcc->num_entries= i;
81
82         /* init all to skipped p frames (with b frames we might have a not encoded frame at the end FIXME) */
83         for(i=0; i<rcc->num_entries; i++){
84             RateControlEntry *rce= &rcc->entry[i];
85             rce->pict_type= rce->new_pict_type=P_TYPE;
86             rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2;
87             rce->misc_bits= s->mb_num + 10;
88             rce->mb_var_sum= s->mb_num*100;
89         }
90
91         /* read stats */
92         p= s->avctx->stats_in;
93         for(i=0; i<rcc->num_entries - s->max_b_frames; i++){
94             RateControlEntry *rce;
95             int picture_number;
96             int e;
97             char *next;
98
99             next= strchr(p, ';');
100             if(next){
101                 (*next)=0; //sscanf in unbelieavle slow on looong strings //FIXME copy / dont write
102                 next++;
103             }
104             e= sscanf(p, " in:%d ", &picture_number);
105
106             assert(picture_number >= 0);
107             assert(picture_number < rcc->num_entries);
108             rce= &rcc->entry[picture_number];
109
110             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",
111                    &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits,
112                    &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count, &rce->skip_count, &rce->header_bits);
113             if(e!=14){
114                 av_log(s->avctx, AV_LOG_ERROR, "statistics are damaged at line %d, parser out=%d\n", i, e);
115                 return -1;
116             }
117
118             p= next;
119         }
120         //FIXME maybe move to end
121         if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) {
122 #ifdef CONFIG_XVID
123             return ff_xvid_rate_control_init(s);
124 #else
125             av_log(s->avctx, AV_LOG_ERROR, "XviD ratecontrol requires libavcodec compiled with XviD support\n");
126             return -1;
127 #endif
128         }
129
130         if(init_pass2(s) < 0) return -1;
131     }
132
133     if(!(s->flags&CODEC_FLAG_PASS2)){
134
135         rcc->short_term_qsum=0.001;
136         rcc->short_term_qcount=0.001;
137
138         rcc->pass1_rc_eq_output_sum= 0.001;
139         rcc->pass1_wanted_bits=0.001;
140
141         /* init stuff with the user specified complexity */
142         if(s->avctx->rc_initial_cplx){
143             for(i=0; i<60*30; i++){
144                 double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num;
145                 RateControlEntry rce;
146                 double q;
147
148                 if     (i%((s->gop_size+3)/4)==0) rce.pict_type= I_TYPE;
149                 else if(i%(s->max_b_frames+1))    rce.pict_type= B_TYPE;
150                 else                              rce.pict_type= P_TYPE;
151
152                 rce.new_pict_type= rce.pict_type;
153                 rce.mc_mb_var_sum= bits*s->mb_num/100000;
154                 rce.mb_var_sum   = s->mb_num;
155                 rce.qscale   = FF_QP2LAMBDA * 2;
156                 rce.f_code   = 2;
157                 rce.b_code   = 1;
158                 rce.misc_bits= 1;
159
160                 if(s->pict_type== I_TYPE){
161                     rce.i_count   = s->mb_num;
162                     rce.i_tex_bits= bits;
163                     rce.p_tex_bits= 0;
164                     rce.mv_bits= 0;
165                 }else{
166                     rce.i_count   = 0; //FIXME we do know this approx
167                     rce.i_tex_bits= 0;
168                     rce.p_tex_bits= bits*0.9;
169                     rce.mv_bits= bits*0.1;
170                 }
171                 rcc->i_cplx_sum [rce.pict_type] += rce.i_tex_bits*rce.qscale;
172                 rcc->p_cplx_sum [rce.pict_type] += rce.p_tex_bits*rce.qscale;
173                 rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
174                 rcc->frame_count[rce.pict_type] ++;
175
176                 bits= rce.i_tex_bits + rce.p_tex_bits;
177
178                 q= get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum, i);
179                 rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base)); //FIXME missbehaves a little for variable fps
180             }
181         }
182
183     }
184
185     return 0;
186 }
187
188 void ff_rate_control_uninit(MpegEncContext *s)
189 {
190     RateControlContext *rcc= &s->rc_context;
191     emms_c();
192
193     av_freep(&rcc->entry);
194
195 #ifdef CONFIG_XVID
196     if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
197         ff_xvid_rate_control_uninit(s);
198 #endif
199 }
200
201 static inline double qp2bits(RateControlEntry *rce, double qp){
202     if(qp<=0.0){
203         av_log(NULL, AV_LOG_ERROR, "qp<=0.0\n");
204     }
205     return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ qp;
206 }
207
208 static inline double bits2qp(RateControlEntry *rce, double bits){
209     if(bits<0.9){
210         av_log(NULL, AV_LOG_ERROR, "bits<0.9\n");
211     }
212     return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ bits;
213 }
214
215 int ff_vbv_update(MpegEncContext *s, int frame_size){
216     RateControlContext *rcc= &s->rc_context;
217     const double fps= 1/av_q2d(s->avctx->time_base);
218     const int buffer_size= s->avctx->rc_buffer_size;
219     const double min_rate= s->avctx->rc_min_rate/fps;
220     const double max_rate= s->avctx->rc_max_rate/fps;
221
222 //printf("%d %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate);
223     if(buffer_size){
224         int left;
225
226         rcc->buffer_index-= frame_size;
227         if(rcc->buffer_index < 0){
228             av_log(s->avctx, AV_LOG_ERROR, "rc buffer underflow\n");
229             rcc->buffer_index= 0;
230         }
231
232         left= buffer_size - rcc->buffer_index - 1;
233         rcc->buffer_index += clip(left, min_rate, max_rate);
234
235         if(rcc->buffer_index > buffer_size){
236             int stuffing= ceil((rcc->buffer_index - buffer_size)/8);
237
238             if(stuffing < 4 && s->codec_id == CODEC_ID_MPEG4)
239                 stuffing=4;
240             rcc->buffer_index -= 8*stuffing;
241
242             if(s->avctx->debug & FF_DEBUG_RC)
243                 av_log(s->avctx, AV_LOG_DEBUG, "stuffing %d bytes\n", stuffing);
244
245             return stuffing;
246         }
247     }
248     return 0;
249 }
250
251 /**
252  * modifies the bitrate curve from pass1 for one frame
253  */
254 static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num){
255     RateControlContext *rcc= &s->rc_context;
256     AVCodecContext *a= s->avctx;
257     double q, bits;
258     const int pict_type= rce->new_pict_type;
259     const double mb_num= s->mb_num;
260     int i;
261
262     double const_values[]={
263         M_PI,
264         M_E,
265         rce->i_tex_bits*rce->qscale,
266         rce->p_tex_bits*rce->qscale,
267         (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale,
268         rce->mv_bits/mb_num,
269         rce->pict_type == B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
270         rce->i_count/mb_num,
271         rce->mc_mb_var_sum/mb_num,
272         rce->mb_var_sum/mb_num,
273         rce->pict_type == I_TYPE,
274         rce->pict_type == P_TYPE,
275         rce->pict_type == B_TYPE,
276         rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
277         a->qcompress,
278 /*        rcc->last_qscale_for[I_TYPE],
279         rcc->last_qscale_for[P_TYPE],
280         rcc->last_qscale_for[B_TYPE],
281         rcc->next_non_b_qscale,*/
282         rcc->i_cplx_sum[I_TYPE] / (double)rcc->frame_count[I_TYPE],
283         rcc->i_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
284         rcc->p_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
285         rcc->p_cplx_sum[B_TYPE] / (double)rcc->frame_count[B_TYPE],
286         (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
287         0
288     };
289     static const char *const_names[]={
290         "PI",
291         "E",
292         "iTex",
293         "pTex",
294         "tex",
295         "mv",
296         "fCode",
297         "iCount",
298         "mcVar",
299         "var",
300         "isI",
301         "isP",
302         "isB",
303         "avgQP",
304         "qComp",
305 /*        "lastIQP",
306         "lastPQP",
307         "lastBQP",
308         "nextNonBQP",*/
309         "avgIITex",
310         "avgPITex",
311         "avgPPTex",
312         "avgBPTex",
313         "avgTex",
314         NULL
315     };
316     static double (*func1[])(void *, double)={
317         (void *)bits2qp,
318         (void *)qp2bits,
319         NULL
320     };
321     static const char *func1_names[]={
322         "bits2qp",
323         "qp2bits",
324         NULL
325     };
326
327     bits= ff_eval(s->avctx->rc_eq, const_values, const_names, func1, func1_names, NULL, NULL, rce);
328
329     rcc->pass1_rc_eq_output_sum+= bits;
330     bits*=rate_factor;
331     if(bits<0.0) bits=0.0;
332     bits+= 1.0; //avoid 1/0 issues
333
334     /* user override */
335     for(i=0; i<s->avctx->rc_override_count; i++){
336         RcOverride *rco= s->avctx->rc_override;
337         if(rco[i].start_frame > frame_num) continue;
338         if(rco[i].end_frame   < frame_num) continue;
339
340         if(rco[i].qscale)
341             bits= qp2bits(rce, rco[i].qscale); //FIXME move at end to really force it?
342         else
343             bits*= rco[i].quality_factor;
344     }
345
346     q= bits2qp(rce, bits);
347
348     /* I/B difference */
349     if     (pict_type==I_TYPE && s->avctx->i_quant_factor<0.0)
350         q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
351     else if(pict_type==B_TYPE && s->avctx->b_quant_factor<0.0)
352         q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
353
354     return q;
355 }
356
357 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){
358     RateControlContext *rcc= &s->rc_context;
359     AVCodecContext *a= s->avctx;
360     const int pict_type= rce->new_pict_type;
361     const double last_p_q    = rcc->last_qscale_for[P_TYPE];
362     const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type];
363
364     if     (pict_type==I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==P_TYPE))
365         q= last_p_q    *ABS(a->i_quant_factor) + a->i_quant_offset;
366     else if(pict_type==B_TYPE && a->b_quant_factor>0.0)
367         q= last_non_b_q*    a->b_quant_factor  + a->b_quant_offset;
368
369     /* last qscale / qdiff stuff */
370     if(rcc->last_non_b_pict_type==pict_type || pict_type!=I_TYPE){
371         double last_q= rcc->last_qscale_for[pict_type];
372         const int maxdiff= FF_QP2LAMBDA * a->max_qdiff;
373
374         if     (q > last_q + maxdiff) q= last_q + maxdiff;
375         else if(q < last_q - maxdiff) q= last_q - maxdiff;
376     }
377
378     rcc->last_qscale_for[pict_type]= q; //Note we cant do that after blurring
379
380     if(pict_type!=B_TYPE)
381         rcc->last_non_b_pict_type= pict_type;
382
383     return q;
384 }
385
386 /**
387  * gets the qmin & qmax for pict_type
388  */
389 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type){
390     int qmin= s->avctx->lmin;
391     int qmax= s->avctx->lmax;
392
393     assert(qmin <= qmax);
394
395     if(pict_type==B_TYPE){
396         qmin= (int)(qmin*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
397         qmax= (int)(qmax*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
398     }else if(pict_type==I_TYPE){
399         qmin= (int)(qmin*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
400         qmax= (int)(qmax*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
401     }
402
403     qmin= clip(qmin, 1, FF_LAMBDA_MAX);
404     qmax= clip(qmax, 1, FF_LAMBDA_MAX);
405
406     if(qmax<qmin) qmax= qmin;
407
408     *qmin_ret= qmin;
409     *qmax_ret= qmax;
410 }
411
412 static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num){
413     RateControlContext *rcc= &s->rc_context;
414     int qmin, qmax;
415     double bits;
416     const int pict_type= rce->new_pict_type;
417     const double buffer_size= s->avctx->rc_buffer_size;
418     const double fps= 1/av_q2d(s->avctx->time_base);
419     const double min_rate= s->avctx->rc_min_rate / fps;
420     const double max_rate= s->avctx->rc_max_rate / fps;
421
422     get_qminmax(&qmin, &qmax, s, pict_type);
423
424     /* modulation */
425     if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==P_TYPE)
426         q*= s->avctx->rc_qmod_amp;
427
428     bits= qp2bits(rce, q);
429 //printf("q:%f\n", q);
430     /* buffer overflow/underflow protection */
431     if(buffer_size){
432         double expected_size= rcc->buffer_index;
433         double q_limit;
434
435         if(min_rate){
436             double d= 2*(buffer_size - expected_size)/buffer_size;
437             if(d>1.0) d=1.0;
438             else if(d<0.0001) d=0.0001;
439             q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
440
441             q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1));
442             if(q > q_limit){
443                 if(s->avctx->debug&FF_DEBUG_RC){
444                     av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
445                 }
446                 q= q_limit;
447             }
448         }
449
450         if(max_rate){
451             double d= 2*expected_size/buffer_size;
452             if(d>1.0) d=1.0;
453             else if(d<0.0001) d=0.0001;
454             q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
455
456             q_limit= bits2qp(rce, FFMAX(rcc->buffer_index/3, 1));
457             if(q < q_limit){
458                 if(s->avctx->debug&FF_DEBUG_RC){
459                     av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit);
460                 }
461                 q= q_limit;
462             }
463         }
464     }
465 //printf("q:%f max:%f min:%f size:%f index:%d bits:%f agr:%f\n", q,max_rate, min_rate, buffer_size, rcc->buffer_index, bits, s->avctx->rc_buffer_aggressivity);
466     if(s->avctx->rc_qsquish==0.0 || qmin==qmax){
467         if     (q<qmin) q=qmin;
468         else if(q>qmax) q=qmax;
469     }else{
470         double min2= log(qmin);
471         double max2= log(qmax);
472
473         q= log(q);
474         q= (q - min2)/(max2-min2) - 0.5;
475         q*= -4.0;
476         q= 1.0/(1.0 + exp(q));
477         q= q*(max2-min2) + min2;
478
479         q= exp(q);
480     }
481
482     return q;
483 }
484
485 //----------------------------------
486 // 1 Pass Code
487
488 static double predict_size(Predictor *p, double q, double var)
489 {
490      return p->coeff*var / (q*p->count);
491 }
492
493 /*
494 static double predict_qp(Predictor *p, double size, double var)
495 {
496 //printf("coeff:%f, count:%f, var:%f, size:%f//\n", p->coeff, p->count, var, size);
497      return p->coeff*var / (size*p->count);
498 }
499 */
500
501 static void update_predictor(Predictor *p, double q, double var, double size)
502 {
503     double new_coeff= size*q / (var + 1);
504     if(var<10) return;
505
506     p->count*= p->decay;
507     p->coeff*= p->decay;
508     p->count++;
509     p->coeff+= new_coeff;
510 }
511
512 static void adaptive_quantization(MpegEncContext *s, double q){
513     int i;
514     const float lumi_masking= s->avctx->lumi_masking / (128.0*128.0);
515     const float dark_masking= s->avctx->dark_masking / (128.0*128.0);
516     const float temp_cplx_masking= s->avctx->temporal_cplx_masking;
517     const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
518     const float p_masking = s->avctx->p_masking;
519     const float border_masking = s->avctx->border_masking;
520     float bits_sum= 0.0;
521     float cplx_sum= 0.0;
522     float cplx_tab[s->mb_num];
523     float bits_tab[s->mb_num];
524     const int qmin= s->avctx->mb_lmin;
525     const int qmax= s->avctx->mb_lmax;
526     Picture * const pic= &s->current_picture;
527     const int mb_width = s->mb_width;
528     const int mb_height = s->mb_height;
529
530     for(i=0; i<s->mb_num; i++){
531         const int mb_xy= s->mb_index2xy[i];
532         float temp_cplx= sqrt(pic->mc_mb_var[mb_xy]); //FIXME merge in pow()
533         float spat_cplx= sqrt(pic->mb_var[mb_xy]);
534         const int lumi= pic->mb_mean[mb_xy];
535         float bits, cplx, factor;
536         int mb_x = mb_xy % s->mb_stride;
537         int mb_y = mb_xy / s->mb_stride;
538         int mb_distance;
539         float mb_factor = 0.0;
540 #if 0
541         if(spat_cplx < q/3) spat_cplx= q/3; //FIXME finetune
542         if(temp_cplx < q/3) temp_cplx= q/3; //FIXME finetune
543 #endif
544         if(spat_cplx < 4) spat_cplx= 4; //FIXME finetune
545         if(temp_cplx < 4) temp_cplx= 4; //FIXME finetune
546
547         if((s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTRA)){//FIXME hq mode
548             cplx= spat_cplx;
549             factor= 1.0 + p_masking;
550         }else{
551             cplx= temp_cplx;
552             factor= pow(temp_cplx, - temp_cplx_masking);
553         }
554         factor*=pow(spat_cplx, - spatial_cplx_masking);
555
556         if(lumi>127)
557             factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking);
558         else
559             factor*= (1.0 - (lumi-128)*(lumi-128)*dark_masking);
560
561         if(mb_x < mb_width/5){
562             mb_distance = mb_width/5 - mb_x;
563             mb_factor = (float)mb_distance / (float)(mb_width/5);
564         }else if(mb_x > 4*mb_width/5){
565             mb_distance = mb_x - 4*mb_width/5;
566             mb_factor = (float)mb_distance / (float)(mb_width/5);
567         }
568         if(mb_y < mb_height/5){
569             mb_distance = mb_height/5 - mb_y;
570             mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5));
571         }else if(mb_y > 4*mb_height/5){
572             mb_distance = mb_y - 4*mb_height/5;
573             mb_factor = FFMAX(mb_factor, (float)mb_distance / (float)(mb_height/5));
574         }
575
576         factor*= 1.0 - border_masking*mb_factor;
577
578         if(factor<0.00001) factor= 0.00001;
579
580         bits= cplx*factor;
581         cplx_sum+= cplx;
582         bits_sum+= bits;
583         cplx_tab[i]= cplx;
584         bits_tab[i]= bits;
585     }
586
587     /* handle qmin/qmax cliping */
588     if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
589         float factor= bits_sum/cplx_sum;
590         for(i=0; i<s->mb_num; i++){
591             float newq= q*cplx_tab[i]/bits_tab[i];
592             newq*= factor;
593
594             if     (newq > qmax){
595                 bits_sum -= bits_tab[i];
596                 cplx_sum -= cplx_tab[i]*q/qmax;
597             }
598             else if(newq < qmin){
599                 bits_sum -= bits_tab[i];
600                 cplx_sum -= cplx_tab[i]*q/qmin;
601             }
602         }
603         if(bits_sum < 0.001) bits_sum= 0.001;
604         if(cplx_sum < 0.001) cplx_sum= 0.001;
605     }
606
607     for(i=0; i<s->mb_num; i++){
608         const int mb_xy= s->mb_index2xy[i];
609         float newq= q*cplx_tab[i]/bits_tab[i];
610         int intq;
611
612         if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
613             newq*= bits_sum/cplx_sum;
614         }
615
616         intq= (int)(newq + 0.5);
617
618         if     (intq > qmax) intq= qmax;
619         else if(intq < qmin) intq= qmin;
620 //if(i%s->mb_width==0) printf("\n");
621 //printf("%2d%3d ", intq, ff_sqrt(s->mc_mb_var[i]));
622         s->lambda_table[mb_xy]= intq;
623     }
624 }
625
626 void ff_get_2pass_fcode(MpegEncContext *s){
627     RateControlContext *rcc= &s->rc_context;
628     int picture_number= s->picture_number;
629     RateControlEntry *rce;
630
631     rce= &rcc->entry[picture_number];
632     s->f_code= rce->f_code;
633     s->b_code= rce->b_code;
634 }
635
636 //FIXME rd or at least approx for dquant
637
638 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
639 {
640     float q;
641     int qmin, qmax;
642     float br_compensation;
643     double diff;
644     double short_term_q;
645     double fps;
646     int picture_number= s->picture_number;
647     int64_t wanted_bits;
648     RateControlContext *rcc= &s->rc_context;
649     AVCodecContext *a= s->avctx;
650     RateControlEntry local_rce, *rce;
651     double bits;
652     double rate_factor;
653     int var;
654     const int pict_type= s->pict_type;
655     Picture * const pic= &s->current_picture;
656     emms_c();
657
658 #ifdef CONFIG_XVID
659     if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
660         return ff_xvid_rate_estimate_qscale(s, dry_run);
661 #endif
662
663     get_qminmax(&qmin, &qmax, s, pict_type);
664
665     fps= 1/av_q2d(s->avctx->time_base);
666 //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate);
667         /* update predictors */
668     if(picture_number>2 && !dry_run){
669         const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
670         update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
671     }
672
673     if(s->flags&CODEC_FLAG_PASS2){
674         assert(picture_number>=0);
675         assert(picture_number<rcc->num_entries);
676         rce= &rcc->entry[picture_number];
677         wanted_bits= rce->expected_bits;
678     }else{
679         rce= &local_rce;
680         wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
681     }
682
683     diff= s->total_bits - wanted_bits;
684     br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance;
685     if(br_compensation<=0.0) br_compensation=0.001;
686
687     var= pict_type == I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum;
688
689     short_term_q = 0; /* avoid warning */
690     if(s->flags&CODEC_FLAG_PASS2){
691         if(pict_type!=I_TYPE)
692             assert(pict_type == rce->new_pict_type);
693
694         q= rce->new_qscale / br_compensation;
695 //printf("%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type);
696     }else{
697         rce->pict_type=
698         rce->new_pict_type= pict_type;
699         rce->mc_mb_var_sum= pic->mc_mb_var_sum;
700         rce->mb_var_sum   = pic->   mb_var_sum;
701         rce->qscale   = FF_QP2LAMBDA * 2;
702         rce->f_code   = s->f_code;
703         rce->b_code   = s->b_code;
704         rce->misc_bits= 1;
705
706         bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
707         if(pict_type== I_TYPE){
708             rce->i_count   = s->mb_num;
709             rce->i_tex_bits= bits;
710             rce->p_tex_bits= 0;
711             rce->mv_bits= 0;
712         }else{
713             rce->i_count   = 0; //FIXME we do know this approx
714             rce->i_tex_bits= 0;
715             rce->p_tex_bits= bits*0.9;
716
717             rce->mv_bits= bits*0.1;
718         }
719         rcc->i_cplx_sum [pict_type] += rce->i_tex_bits*rce->qscale;
720         rcc->p_cplx_sum [pict_type] += rce->p_tex_bits*rce->qscale;
721         rcc->mv_bits_sum[pict_type] += rce->mv_bits;
722         rcc->frame_count[pict_type] ++;
723
724         bits= rce->i_tex_bits + rce->p_tex_bits;
725         rate_factor= rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum * br_compensation;
726
727         q= get_qscale(s, rce, rate_factor, picture_number);
728
729         assert(q>0.0);
730 //printf("%f ", q);
731         q= get_diff_limited_q(s, rce, q);
732 //printf("%f ", q);
733         assert(q>0.0);
734
735         if(pict_type==P_TYPE || s->intra_only){ //FIXME type dependant blur like in 2-pass
736             rcc->short_term_qsum*=a->qblur;
737             rcc->short_term_qcount*=a->qblur;
738
739             rcc->short_term_qsum+= q;
740             rcc->short_term_qcount++;
741 //printf("%f ", q);
742             q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount;
743 //printf("%f ", q);
744         }
745         assert(q>0.0);
746
747         q= modify_qscale(s, rce, q, picture_number);
748
749         rcc->pass1_wanted_bits+= s->bit_rate/fps;
750
751         assert(q>0.0);
752     }
753
754     if(s->avctx->debug&FF_DEBUG_RC){
755         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",
756         av_get_pict_type_char(pict_type), qmin, q, qmax, picture_number, (int)wanted_bits/1000, (int)s->total_bits/1000,
757         br_compensation, short_term_q, s->frame_bits, pic->mb_var_sum, pic->mc_mb_var_sum, s->bit_rate/1000, (int)fps
758         );
759     }
760
761     if     (q<qmin) q=qmin;
762     else if(q>qmax) q=qmax;
763
764     if(s->adaptive_quant)
765         adaptive_quantization(s, q);
766     else
767         q= (int)(q + 0.5);
768
769     if(!dry_run){
770         rcc->last_qscale= q;
771         rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum;
772         rcc->last_mb_var_sum= pic->mb_var_sum;
773     }
774 #if 0
775 {
776     static int mvsum=0, texsum=0;
777     mvsum += s->mv_bits;
778     texsum += s->i_tex_bits + s->p_tex_bits;
779     printf("%d %d//\n\n", mvsum, texsum);
780 }
781 #endif
782     return q;
783 }
784
785 //----------------------------------------------
786 // 2-Pass code
787
788 static int init_pass2(MpegEncContext *s)
789 {
790     RateControlContext *rcc= &s->rc_context;
791     AVCodecContext *a= s->avctx;
792     int i;
793     double fps= 1/av_q2d(s->avctx->time_base);
794     double complexity[5]={0,0,0,0,0};   // aproximate bits at quant=1
795     double avg_quantizer[5];
796     uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits
797     uint64_t available_bits[5];
798     uint64_t all_const_bits;
799     uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
800     double rate_factor=0;
801     double step;
802     //int last_i_frame=-10000000;
803     const int filter_size= (int)(a->qblur*4) | 1;
804     double expected_bits;
805     double *qscale, *blured_qscale;
806
807     /* find complexity & const_bits & decide the pict_types */
808     for(i=0; i<rcc->num_entries; i++){
809         RateControlEntry *rce= &rcc->entry[i];
810
811         rce->new_pict_type= rce->pict_type;
812         rcc->i_cplx_sum [rce->pict_type] += rce->i_tex_bits*rce->qscale;
813         rcc->p_cplx_sum [rce->pict_type] += rce->p_tex_bits*rce->qscale;
814         rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
815         rcc->frame_count[rce->pict_type] ++;
816
817         complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
818         const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
819     }
820     all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];
821
822     if(all_available_bits < all_const_bits){
823         av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is to low\n");
824         return -1;
825     }
826
827     /* find average quantizers */
828     avg_quantizer[P_TYPE]=0;
829     for(step=256*256; step>0.0000001; step*=0.5){
830         double expected_bits=0;
831         avg_quantizer[P_TYPE]+= step;
832
833         avg_quantizer[I_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset;
834         avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
835
836         expected_bits=
837             + all_const_bits
838             + complexity[I_TYPE]/avg_quantizer[I_TYPE]
839             + complexity[P_TYPE]/avg_quantizer[P_TYPE]
840             + complexity[B_TYPE]/avg_quantizer[B_TYPE];
841
842         if(expected_bits < all_available_bits) avg_quantizer[P_TYPE]-= step;
843 //printf("%f %lld %f\n", expected_bits, all_available_bits, avg_quantizer[P_TYPE]);
844     }
845 //printf("qp_i:%f, qp_p:%f, qp_b:%f\n", avg_quantizer[I_TYPE],avg_quantizer[P_TYPE],avg_quantizer[B_TYPE]);
846
847     for(i=0; i<5; i++){
848         available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i];
849     }
850 //printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
851
852     qscale= av_malloc(sizeof(double)*rcc->num_entries);
853     blured_qscale= av_malloc(sizeof(double)*rcc->num_entries);
854
855     for(step=256*256; step>0.0000001; step*=0.5){
856         expected_bits=0;
857         rate_factor+= step;
858
859         rcc->buffer_index= s->avctx->rc_buffer_size/2;
860
861         /* find qscale */
862         for(i=0; i<rcc->num_entries; i++){
863             qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i);
864         }
865         assert(filter_size%2==1);
866
867         /* fixed I/B QP relative to P mode */
868         for(i=rcc->num_entries-1; i>=0; i--){
869             RateControlEntry *rce= &rcc->entry[i];
870
871             qscale[i]= get_diff_limited_q(s, rce, qscale[i]);
872         }
873
874         /* smooth curve */
875         for(i=0; i<rcc->num_entries; i++){
876             RateControlEntry *rce= &rcc->entry[i];
877             const int pict_type= rce->new_pict_type;
878             int j;
879             double q=0.0, sum=0.0;
880
881             for(j=0; j<filter_size; j++){
882                 int index= i+j-filter_size/2;
883                 double d= index-i;
884                 double coeff= a->qblur==0 ? 1.0 : exp(-d*d/(a->qblur * a->qblur));
885
886                 if(index < 0 || index >= rcc->num_entries) continue;
887                 if(pict_type != rcc->entry[index].new_pict_type) continue;
888                 q+= qscale[index] * coeff;
889                 sum+= coeff;
890             }
891             blured_qscale[i]= q/sum;
892         }
893
894         /* find expected bits */
895         for(i=0; i<rcc->num_entries; i++){
896             RateControlEntry *rce= &rcc->entry[i];
897             double bits;
898             rce->new_qscale= modify_qscale(s, rce, blured_qscale[i], i);
899             bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
900 //printf("%d %f\n", rce->new_bits, blured_qscale[i]);
901             bits += 8*ff_vbv_update(s, bits);
902
903             rce->expected_bits= expected_bits;
904             expected_bits += bits;
905         }
906
907 //        printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor);
908         if(expected_bits > all_available_bits) rate_factor-= step;
909     }
910     av_free(qscale);
911     av_free(blured_qscale);
912
913     if(abs(expected_bits/all_available_bits - 1.0) > 0.01 ){
914         av_log(s->avctx, AV_LOG_ERROR, "Error: 2pass curve failed to converge\n");
915         return -1;
916     }
917
918     return 0;
919 }