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