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