Mercurial > libavcodec.hg
annotate ratecontrol.c @ 3526:7dc8e4a12105 libavcodec
New functions in DSPContext for VC-1 decoding
author | kostya |
---|---|
date | Sun, 30 Jul 2006 03:45:02 +0000 |
parents | 19e9a62ddf1b |
children | be8084e35485 |
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; |
262 | |
263 double const_values[]={ | |
264 M_PI, | |
265 M_E, | |
266 rce->i_tex_bits*rce->qscale, | |
267 rce->p_tex_bits*rce->qscale, | |
268 (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale, | |
269 rce->mv_bits/mb_num, | |
270 rce->pict_type == B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code, | |
271 rce->i_count/mb_num, | |
272 rce->mc_mb_var_sum/mb_num, | |
273 rce->mb_var_sum/mb_num, | |
274 rce->pict_type == I_TYPE, | |
275 rce->pict_type == P_TYPE, | |
276 rce->pict_type == B_TYPE, | |
277 rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type], | |
1687 | 278 a->qcompress, |
612 | 279 /* rcc->last_qscale_for[I_TYPE], |
280 rcc->last_qscale_for[P_TYPE], | |
281 rcc->last_qscale_for[B_TYPE], | |
282 rcc->next_non_b_qscale,*/ | |
283 rcc->i_cplx_sum[I_TYPE] / (double)rcc->frame_count[I_TYPE], | |
284 rcc->i_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE], | |
285 rcc->p_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE], | |
286 rcc->p_cplx_sum[B_TYPE] / (double)rcc->frame_count[B_TYPE], | |
287 (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type], | |
288 0 | |
289 }; | |
1057 | 290 static const char *const_names[]={ |
612 | 291 "PI", |
292 "E", | |
293 "iTex", | |
294 "pTex", | |
295 "tex", | |
296 "mv", | |
297 "fCode", | |
298 "iCount", | |
299 "mcVar", | |
300 "var", | |
301 "isI", | |
302 "isP", | |
303 "isB", | |
304 "avgQP", | |
305 "qComp", | |
306 /* "lastIQP", | |
307 "lastPQP", | |
308 "lastBQP", | |
309 "nextNonBQP",*/ | |
310 "avgIITex", | |
311 "avgPITex", | |
312 "avgPPTex", | |
313 "avgBPTex", | |
314 "avgTex", | |
315 NULL | |
316 }; | |
620
a5aa53b6e648
warning patch by (Dominik Mierzejewski <dominik at rangers dot eu dot org>)
michaelni
parents:
616
diff
changeset
|
317 static double (*func1[])(void *, double)={ |
749 | 318 (void *)bits2qp, |
319 (void *)qp2bits, | |
612 | 320 NULL |
321 }; | |
1057 | 322 static const char *func1_names[]={ |
612 | 323 "bits2qp", |
324 "qp2bits", | |
325 NULL | |
326 }; | |
327 | |
328 bits= ff_eval(s->avctx->rc_eq, const_values, const_names, func1, func1_names, NULL, NULL, rce); | |
2967 | 329 |
707 | 330 rcc->pass1_rc_eq_output_sum+= bits; |
612 | 331 bits*=rate_factor; |
332 if(bits<0.0) bits=0.0; | |
333 bits+= 1.0; //avoid 1/0 issues | |
2967 | 334 |
612 | 335 /* user override */ |
336 for(i=0; i<s->avctx->rc_override_count; i++){ | |
337 RcOverride *rco= s->avctx->rc_override; | |
338 if(rco[i].start_frame > frame_num) continue; | |
339 if(rco[i].end_frame < frame_num) continue; | |
2967 | 340 |
341 if(rco[i].qscale) | |
612 | 342 bits= qp2bits(rce, rco[i].qscale); //FIXME move at end to really force it? |
343 else | |
344 bits*= rco[i].quality_factor; | |
345 } | |
346 | |
347 q= bits2qp(rce, bits); | |
2967 | 348 |
612 | 349 /* I/B difference */ |
350 if (pict_type==I_TYPE && s->avctx->i_quant_factor<0.0) | |
351 q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset; | |
352 else if(pict_type==B_TYPE && s->avctx->b_quant_factor<0.0) | |
353 q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset; | |
2967 | 354 |
679 | 355 return q; |
356 } | |
357 | |
358 static double get_diff_limited_q(MpegEncContext *s, RateControlEntry *rce, double q){ | |
359 RateControlContext *rcc= &s->rc_context; | |
360 AVCodecContext *a= s->avctx; | |
361 const int pict_type= rce->new_pict_type; | |
362 const double last_p_q = rcc->last_qscale_for[P_TYPE]; | |
363 const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type]; | |
2967 | 364 |
679 | 365 if (pict_type==I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==P_TYPE)) |
366 q= last_p_q *ABS(a->i_quant_factor) + a->i_quant_offset; | |
367 else if(pict_type==B_TYPE && a->b_quant_factor>0.0) | |
368 q= last_non_b_q* a->b_quant_factor + a->b_quant_offset; | |
369 | |
612 | 370 /* last qscale / qdiff stuff */ |
679 | 371 if(rcc->last_non_b_pict_type==pict_type || pict_type!=I_TYPE){ |
372 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
|
373 const int maxdiff= FF_QP2LAMBDA * a->max_qdiff; |
930 | 374 |
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
|
375 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
|
376 else if(q < last_q - maxdiff) q= last_q - maxdiff; |
679 | 377 } |
612 | 378 |
379 rcc->last_qscale_for[pict_type]= q; //Note we cant do that after blurring | |
2967 | 380 |
679 | 381 if(pict_type!=B_TYPE) |
382 rcc->last_non_b_pict_type= pict_type; | |
383 | |
612 | 384 return q; |
385 } | |
386 | |
387 /** | |
388 * gets the qmin & qmax for pict_type | |
389 */ | |
390 static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type){ | |
2967 | 391 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
|
392 int qmax= s->avctx->lmax; |
2967 | 393 |
1365 | 394 assert(qmin <= qmax); |
612 | 395 |
396 if(pict_type==B_TYPE){ | |
397 qmin= (int)(qmin*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); | |
398 qmax= (int)(qmax*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5); | |
399 }else if(pict_type==I_TYPE){ | |
400 qmin= (int)(qmin*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); | |
401 qmax= (int)(qmax*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5); | |
402 } | |
403 | |
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
|
404 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
|
405 qmax= clip(qmax, 1, FF_LAMBDA_MAX); |
612 | 406 |
1365 | 407 if(qmax<qmin) qmax= qmin; |
2967 | 408 |
612 | 409 *qmin_ret= qmin; |
410 *qmax_ret= qmax; | |
411 } | |
412 | |
413 static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num){ | |
414 RateControlContext *rcc= &s->rc_context; | |
415 int qmin, qmax; | |
416 double bits; | |
417 const int pict_type= rce->new_pict_type; | |
418 const double buffer_size= s->avctx->rc_buffer_size; | |
2637 | 419 const double fps= 1/av_q2d(s->avctx->time_base); |
1683 | 420 const double min_rate= s->avctx->rc_min_rate / fps; |
421 const double max_rate= s->avctx->rc_max_rate / fps; | |
2967 | 422 |
612 | 423 get_qminmax(&qmin, &qmax, s, pict_type); |
424 | |
425 /* modulation */ | |
426 if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==P_TYPE) | |
427 q*= s->avctx->rc_qmod_amp; | |
428 | |
429 bits= qp2bits(rce, q); | |
678 | 430 //printf("q:%f\n", q); |
612 | 431 /* buffer overflow/underflow protection */ |
432 if(buffer_size){ | |
679 | 433 double expected_size= rcc->buffer_index; |
1697 | 434 double q_limit; |
612 | 435 |
436 if(min_rate){ | |
679 | 437 double d= 2*(buffer_size - expected_size)/buffer_size; |
612 | 438 if(d>1.0) d=1.0; |
678 | 439 else if(d<0.0001) d=0.0001; |
440 q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); | |
679 | 441 |
1697 | 442 q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1)); |
443 if(q > q_limit){ | |
444 if(s->avctx->debug&FF_DEBUG_RC){ | |
445 av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); | |
446 } | |
447 q= q_limit; | |
448 } | |
612 | 449 } |
450 | |
451 if(max_rate){ | |
452 double d= 2*expected_size/buffer_size; | |
453 if(d>1.0) d=1.0; | |
678 | 454 else if(d<0.0001) d=0.0001; |
455 q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); | |
679 | 456 |
1697 | 457 q_limit= bits2qp(rce, FFMAX(rcc->buffer_index/3, 1)); |
458 if(q < q_limit){ | |
459 if(s->avctx->debug&FF_DEBUG_RC){ | |
460 av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); | |
461 } | |
462 q= q_limit; | |
463 } | |
612 | 464 } |
465 } | |
678 | 466 //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 | 467 if(s->avctx->rc_qsquish==0.0 || qmin==qmax){ |
612 | 468 if (q<qmin) q=qmin; |
469 else if(q>qmax) q=qmax; | |
470 }else{ | |
471 double min2= log(qmin); | |
472 double max2= log(qmax); | |
2967 | 473 |
612 | 474 q= log(q); |
475 q= (q - min2)/(max2-min2) - 0.5; | |
476 q*= -4.0; | |
477 q= 1.0/(1.0 + exp(q)); | |
478 q= q*(max2-min2) + min2; | |
2967 | 479 |
612 | 480 q= exp(q); |
481 } | |
2967 | 482 |
612 | 483 return q; |
484 } | |
485 | |
329 | 486 //---------------------------------- |
487 // 1 Pass Code | |
488 | |
612 | 489 static double predict_size(Predictor *p, double q, double var) |
329 | 490 { |
491 return p->coeff*var / (q*p->count); | |
492 } | |
493 | |
949 | 494 /* |
612 | 495 static double predict_qp(Predictor *p, double size, double var) |
496 { | |
497 //printf("coeff:%f, count:%f, var:%f, size:%f//\n", p->coeff, p->count, var, size); | |
498 return p->coeff*var / (size*p->count); | |
499 } | |
949 | 500 */ |
612 | 501 |
329 | 502 static void update_predictor(Predictor *p, double q, double var, double size) |
503 { | |
504 double new_coeff= size*q / (var + 1); | |
612 | 505 if(var<10) return; |
329 | 506 |
507 p->count*= p->decay; | |
508 p->coeff*= p->decay; | |
509 p->count++; | |
510 p->coeff+= new_coeff; | |
511 } | |
512 | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
513 static void adaptive_quantization(MpegEncContext *s, double q){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
514 int i; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
515 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
|
516 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
|
517 const float temp_cplx_masking= s->avctx->temporal_cplx_masking; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
518 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
519 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
|
520 const float border_masking = s->avctx->border_masking; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
521 float bits_sum= 0.0; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
522 float cplx_sum= 0.0; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
523 float cplx_tab[s->mb_num]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
524 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
|
525 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
|
526 const int qmax= s->avctx->mb_lmax; |
903 | 527 Picture * const pic= &s->current_picture; |
2493
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
528 const int mb_width = s->mb_width; |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
529 const int mb_height = s->mb_height; |
2967 | 530 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
531 for(i=0; i<s->mb_num; i++){ |
1180 | 532 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
|
533 float temp_cplx= sqrt(pic->mc_mb_var[mb_xy]); //FIXME merge in pow() |
1180 | 534 float spat_cplx= sqrt(pic->mb_var[mb_xy]); |
535 const int lumi= pic->mb_mean[mb_xy]; | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
536 float bits, cplx, factor; |
2493
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
537 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
|
538 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
|
539 int mb_distance; |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
540 float mb_factor = 0.0; |
2967 | 541 #if 0 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
542 if(spat_cplx < q/3) spat_cplx= q/3; //FIXME finetune |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
543 if(temp_cplx < q/3) temp_cplx= q/3; //FIXME finetune |
2967 | 544 #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
|
545 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
|
546 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
|
547 |
2967 | 548 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
|
549 cplx= spat_cplx; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
550 factor= 1.0 + p_masking; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
551 }else{ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
552 cplx= temp_cplx; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
553 factor= pow(temp_cplx, - temp_cplx_masking); |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
554 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
555 factor*=pow(spat_cplx, - spatial_cplx_masking); |
693
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
556 |
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
557 if(lumi>127) |
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
558 factor*= (1.0 - (lumi-128)*(lumi-128)*lumi_masking); |
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
559 else |
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
560 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
|
561 |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
562 if(mb_x < mb_width/5){ |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
563 mb_distance = mb_width/5 - mb_x; |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
564 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
|
565 }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
|
566 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
|
567 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
|
568 } |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
569 if(mb_y < mb_height/5){ |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
570 mb_distance = mb_height/5 - mb_y; |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
571 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
|
572 }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
|
573 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
|
574 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
|
575 } |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
576 |
258120c61eea
Border processing adaptive quant patch by (Christophe Massiot |cmassiot freebox fr)
michael
parents:
2423
diff
changeset
|
577 factor*= 1.0 - border_masking*mb_factor; |
2967 | 578 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
579 if(factor<0.00001) factor= 0.00001; |
2967 | 580 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
581 bits= cplx*factor; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
582 cplx_sum+= cplx; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
583 bits_sum+= bits; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
584 cplx_tab[i]= cplx; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
585 bits_tab[i]= bits; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
586 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
587 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
588 /* handle qmin/qmax cliping */ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
589 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
|
590 float factor= bits_sum/cplx_sum; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
591 for(i=0; i<s->mb_num; i++){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
592 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
|
593 newq*= factor; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
594 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
595 if (newq > qmax){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
596 bits_sum -= bits_tab[i]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
597 cplx_sum -= cplx_tab[i]*q/qmax; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
598 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
599 else if(newq < qmin){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
600 bits_sum -= bits_tab[i]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
601 cplx_sum -= cplx_tab[i]*q/qmin; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
602 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
603 } |
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
|
604 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
|
605 if(cplx_sum < 0.001) cplx_sum= 0.001; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
606 } |
2967 | 607 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
608 for(i=0; i<s->mb_num; i++){ |
1180 | 609 const int mb_xy= s->mb_index2xy[i]; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
610 float newq= q*cplx_tab[i]/bits_tab[i]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
611 int intq; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
612 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
613 if(s->flags&CODEC_FLAG_NORMALIZE_AQP){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
614 newq*= bits_sum/cplx_sum; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
615 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
616 |
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
|
617 intq= (int)(newq + 0.5); |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
618 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
619 if (intq > qmax) intq= qmax; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
620 else if(intq < qmin) intq= qmin; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
621 //if(i%s->mb_width==0) printf("\n"); |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
622 //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
|
623 s->lambda_table[mb_xy]= intq; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
624 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
625 } |
2981 | 626 |
627 void ff_get_2pass_fcode(MpegEncContext *s){ | |
628 RateControlContext *rcc= &s->rc_context; | |
629 int picture_number= s->picture_number; | |
630 RateControlEntry *rce; | |
631 | |
632 rce= &rcc->entry[picture_number]; | |
633 s->f_code= rce->f_code; | |
634 s->b_code= rce->b_code; | |
635 } | |
636 | |
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
|
637 //FIXME rd or at least approx for dquant |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
638 |
2974 | 639 float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run) |
329 | 640 { |
641 float q; | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
642 int qmin, qmax; |
329 | 643 float br_compensation; |
644 double diff; | |
645 double short_term_q; | |
646 double fps; | |
612 | 647 int picture_number= s->picture_number; |
329 | 648 int64_t wanted_bits; |
612 | 649 RateControlContext *rcc= &s->rc_context; |
1687 | 650 AVCodecContext *a= s->avctx; |
612 | 651 RateControlEntry local_rce, *rce; |
652 double bits; | |
653 double rate_factor; | |
654 int var; | |
655 const int pict_type= s->pict_type; | |
903 | 656 Picture * const pic= &s->current_picture; |
329 | 657 emms_c(); |
658 | |
3065 | 659 #ifdef CONFIG_XVID |
3064 | 660 if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) |
661 return ff_xvid_rate_estimate_qscale(s, dry_run); | |
3065 | 662 #endif |
3064 | 663 |
612 | 664 get_qminmax(&qmin, &qmax, s, pict_type); |
665 | |
2637 | 666 fps= 1/av_q2d(s->avctx->time_base); |
678 | 667 //printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate); |
329 | 668 /* update predictors */ |
2974 | 669 if(picture_number>2 && !dry_run){ |
612 | 670 const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum; |
671 update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits); | |
329 | 672 } |
673 | |
612 | 674 if(s->flags&CODEC_FLAG_PASS2){ |
675 assert(picture_number>=0); | |
676 assert(picture_number<rcc->num_entries); | |
677 rce= &rcc->entry[picture_number]; | |
678 wanted_bits= rce->expected_bits; | |
679 }else{ | |
680 rce= &local_rce; | |
681 wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps); | |
329 | 682 } |
683 | |
684 diff= s->total_bits - wanted_bits; | |
1687 | 685 br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance; |
329 | 686 if(br_compensation<=0.0) br_compensation=0.001; |
612 | 687 |
903 | 688 var= pict_type == I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum; |
2967 | 689 |
1455 | 690 short_term_q = 0; /* avoid warning */ |
612 | 691 if(s->flags&CODEC_FLAG_PASS2){ |
692 if(pict_type!=I_TYPE) | |
693 assert(pict_type == rce->new_pict_type); | |
694 | |
695 q= rce->new_qscale / br_compensation; | |
696 //printf("%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type); | |
697 }else{ | |
2967 | 698 rce->pict_type= |
612 | 699 rce->new_pict_type= pict_type; |
903 | 700 rce->mc_mb_var_sum= pic->mc_mb_var_sum; |
701 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
|
702 rce->qscale = FF_QP2LAMBDA * 2; |
612 | 703 rce->f_code = s->f_code; |
704 rce->b_code = s->b_code; | |
705 rce->misc_bits= 1; | |
706 | |
707 bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var)); | |
708 if(pict_type== I_TYPE){ | |
709 rce->i_count = s->mb_num; | |
710 rce->i_tex_bits= bits; | |
711 rce->p_tex_bits= 0; | |
712 rce->mv_bits= 0; | |
713 }else{ | |
714 rce->i_count = 0; //FIXME we do know this approx | |
715 rce->i_tex_bits= 0; | |
716 rce->p_tex_bits= bits*0.9; | |
2967 | 717 |
612 | 718 rce->mv_bits= bits*0.1; |
719 } | |
720 rcc->i_cplx_sum [pict_type] += rce->i_tex_bits*rce->qscale; | |
721 rcc->p_cplx_sum [pict_type] += rce->p_tex_bits*rce->qscale; | |
722 rcc->mv_bits_sum[pict_type] += rce->mv_bits; | |
723 rcc->frame_count[pict_type] ++; | |
724 | |
725 bits= rce->i_tex_bits + rce->p_tex_bits; | |
707 | 726 rate_factor= rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum * br_compensation; |
2967 | 727 |
612 | 728 q= get_qscale(s, rce, rate_factor, picture_number); |
729 | |
615 | 730 assert(q>0.0); |
612 | 731 //printf("%f ", q); |
679 | 732 q= get_diff_limited_q(s, rce, q); |
612 | 733 //printf("%f ", q); |
734 assert(q>0.0); | |
735 | |
736 if(pict_type==P_TYPE || s->intra_only){ //FIXME type dependant blur like in 2-pass | |
1687 | 737 rcc->short_term_qsum*=a->qblur; |
738 rcc->short_term_qcount*=a->qblur; | |
612 | 739 |
740 rcc->short_term_qsum+= q; | |
741 rcc->short_term_qcount++; | |
742 //printf("%f ", q); | |
743 q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount; | |
744 //printf("%f ", q); | |
745 } | |
678 | 746 assert(q>0.0); |
2967 | 747 |
612 | 748 q= modify_qscale(s, rce, q, picture_number); |
749 | |
750 rcc->pass1_wanted_bits+= s->bit_rate/fps; | |
751 | |
615 | 752 assert(q>0.0); |
612 | 753 } |
930 | 754 |
755 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
|
756 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 | 757 av_get_pict_type_char(pict_type), qmin, q, qmax, picture_number, (int)wanted_bits/1000, (int)s->total_bits/1000, |
930 | 758 br_compensation, short_term_q, s->frame_bits, pic->mb_var_sum, pic->mc_mb_var_sum, s->bit_rate/1000, (int)fps |
759 ); | |
760 } | |
612 | 761 |
2967 | 762 if (q<qmin) q=qmin; |
612 | 763 else if(q>qmax) q=qmax; |
764 | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
765 if(s->adaptive_quant) |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
766 adaptive_quantization(s, q); |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
767 else |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
768 q= (int)(q + 0.5); |
2967 | 769 |
2974 | 770 if(!dry_run){ |
771 rcc->last_qscale= q; | |
772 rcc->last_mc_mb_var_sum= pic->mc_mb_var_sum; | |
773 rcc->last_mb_var_sum= pic->mb_var_sum; | |
774 } | |
903 | 775 #if 0 |
776 { | |
777 static int mvsum=0, texsum=0; | |
778 mvsum += s->mv_bits; | |
779 texsum += s->i_tex_bits + s->p_tex_bits; | |
780 printf("%d %d//\n\n", mvsum, texsum); | |
781 } | |
782 #endif | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
679
diff
changeset
|
783 return q; |
329 | 784 } |
785 | |
786 //---------------------------------------------- | |
787 // 2-Pass code | |
788 | |
789 static int init_pass2(MpegEncContext *s) | |
790 { | |
791 RateControlContext *rcc= &s->rc_context; | |
1687 | 792 AVCodecContext *a= s->avctx; |
329 | 793 int i; |
2637 | 794 double fps= 1/av_q2d(s->avctx->time_base); |
329 | 795 double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1 |
796 double avg_quantizer[5]; | |
797 uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits | |
798 uint64_t available_bits[5]; | |
799 uint64_t all_const_bits; | |
800 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps); | |
801 double rate_factor=0; | |
802 double step; | |
949 | 803 //int last_i_frame=-10000000; |
2967 | 804 const int filter_size= (int)(a->qblur*4) | 1; |
612 | 805 double expected_bits; |
806 double *qscale, *blured_qscale; | |
329 | 807 |
808 /* find complexity & const_bits & decide the pict_types */ | |
809 for(i=0; i<rcc->num_entries; i++){ | |
810 RateControlEntry *rce= &rcc->entry[i]; | |
2967 | 811 |
915 | 812 rce->new_pict_type= rce->pict_type; |
612 | 813 rcc->i_cplx_sum [rce->pict_type] += rce->i_tex_bits*rce->qscale; |
814 rcc->p_cplx_sum [rce->pict_type] += rce->p_tex_bits*rce->qscale; | |
815 rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits; | |
816 rcc->frame_count[rce->pict_type] ++; | |
329 | 817 |
818 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale; | |
819 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits; | |
820 } | |
821 all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE]; | |
2967 | 822 |
329 | 823 if(all_available_bits < all_const_bits){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1505
diff
changeset
|
824 av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is to low\n"); |
329 | 825 return -1; |
826 } | |
2967 | 827 |
612 | 828 /* find average quantizers */ |
829 avg_quantizer[P_TYPE]=0; | |
830 for(step=256*256; step>0.0000001; step*=0.5){ | |
831 double expected_bits=0; | |
832 avg_quantizer[P_TYPE]+= step; | |
2967 | 833 |
612 | 834 avg_quantizer[I_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset; |
835 avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset; | |
2967 | 836 |
837 expected_bits= | |
838 + all_const_bits | |
612 | 839 + complexity[I_TYPE]/avg_quantizer[I_TYPE] |
840 + complexity[P_TYPE]/avg_quantizer[P_TYPE] | |
841 + complexity[B_TYPE]/avg_quantizer[B_TYPE]; | |
2967 | 842 |
612 | 843 if(expected_bits < all_available_bits) avg_quantizer[P_TYPE]-= step; |
844 //printf("%f %lld %f\n", expected_bits, all_available_bits, avg_quantizer[P_TYPE]); | |
845 } | |
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
|
846 //printf("qp_i:%f, qp_p:%f, qp_b:%f\n", avg_quantizer[I_TYPE],avg_quantizer[P_TYPE],avg_quantizer[B_TYPE]); |
329 | 847 |
848 for(i=0; i<5; i++){ | |
849 available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i]; | |
850 } | |
851 //printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits); | |
2967 | 852 |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
977
diff
changeset
|
853 qscale= av_malloc(sizeof(double)*rcc->num_entries); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
977
diff
changeset
|
854 blured_qscale= av_malloc(sizeof(double)*rcc->num_entries); |
612 | 855 |
329 | 856 for(step=256*256; step>0.0000001; step*=0.5){ |
612 | 857 expected_bits=0; |
329 | 858 rate_factor+= step; |
2967 | 859 |
612 | 860 rcc->buffer_index= s->avctx->rc_buffer_size/2; |
861 | |
329 | 862 /* find qscale */ |
863 for(i=0; i<rcc->num_entries; i++){ | |
612 | 864 qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i); |
865 } | |
866 assert(filter_size%2==1); | |
329 | 867 |
612 | 868 /* fixed I/B QP relative to P mode */ |
869 for(i=rcc->num_entries-1; i>=0; i--){ | |
870 RateControlEntry *rce= &rcc->entry[i]; | |
2967 | 871 |
679 | 872 qscale[i]= get_diff_limited_q(s, rce, qscale[i]); |
329 | 873 } |
874 | |
875 /* smooth curve */ | |
612 | 876 for(i=0; i<rcc->num_entries; i++){ |
877 RateControlEntry *rce= &rcc->entry[i]; | |
878 const int pict_type= rce->new_pict_type; | |
879 int j; | |
880 double q=0.0, sum=0.0; | |
2967 | 881 |
612 | 882 for(j=0; j<filter_size; j++){ |
883 int index= i+j-filter_size/2; | |
884 double d= index-i; | |
1687 | 885 double coeff= a->qblur==0 ? 1.0 : exp(-d*d/(a->qblur * a->qblur)); |
2967 | 886 |
612 | 887 if(index < 0 || index >= rcc->num_entries) continue; |
888 if(pict_type != rcc->entry[index].new_pict_type) continue; | |
889 q+= qscale[index] * coeff; | |
890 sum+= coeff; | |
891 } | |
892 blured_qscale[i]= q/sum; | |
893 } | |
2967 | 894 |
329 | 895 /* find expected bits */ |
896 for(i=0; i<rcc->num_entries; i++){ | |
897 RateControlEntry *rce= &rcc->entry[i]; | |
612 | 898 double bits; |
899 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
|
900 bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits; |
612 | 901 //printf("%d %f\n", rce->new_bits, blured_qscale[i]); |
1684 | 902 bits += 8*ff_vbv_update(s, bits); |
612 | 903 |
329 | 904 rce->expected_bits= expected_bits; |
612 | 905 expected_bits += bits; |
329 | 906 } |
907 | |
612 | 908 // printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor); |
329 | 909 if(expected_bits > all_available_bits) rate_factor-= step; |
910 } | |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
977
diff
changeset
|
911 av_free(qscale); |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
977
diff
changeset
|
912 av_free(blured_qscale); |
612 | 913 |
3319 | 914 if(fabs(expected_bits/all_available_bits - 1.0) > 0.01 ){ |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1505
diff
changeset
|
915 av_log(s->avctx, AV_LOG_ERROR, "Error: 2pass curve failed to converge\n"); |
612 | 916 return -1; |
917 } | |
329 | 918 |
919 return 0; | |
920 } |