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