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