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