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