error concealment needs the mbintra_table so it should allways be allocated
[libav.git] / libavcodec / ratecontrol.c
CommitLineData
8b4c7dbc 1/*
2ef0f2b2
FB
2 * Rate control for video encoders
3 *
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
8b4c7dbc 20#include "avcodec.h"
2ef0f2b2 21#include "dsputil.h"
8b4c7dbc
MN
22#include "mpegvideo.h"
23
24#define STATS_FILE "lavc_stats.txt"
25
26static int init_pass2(MpegEncContext *s);
27
28void ff_write_pass1_stats(MpegEncContext *s){
29 RateControlContext *rcc= &s->rc_context;
30// fprintf(c->stats_file, "type:%d q:%d icount:%d pcount:%d scount:%d itex:%d ptex%d mv:%d misc:%d fcode:%d bcode:%d\")
31 fprintf(rcc->stats_file, "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d\n",
32 s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type,
33 s->qscale, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, s->f_code, s->b_code);
34}
35
36int ff_rate_control_init(MpegEncContext *s)
37{
38 RateControlContext *rcc= &s->rc_context;
39 emms_c();
40
41 if(s->flags&CODEC_FLAG_PASS1){
42 rcc->stats_file= fopen(STATS_FILE, "w");
43 if(!rcc->stats_file){
44 fprintf(stderr, "failed to open " STATS_FILE "\n");
45 return -1;
46 }
47 } else if(s->flags&CODEC_FLAG_PASS2){
48 int size;
49 int i;
50
51 rcc->stats_file= fopen(STATS_FILE, "r");
52 if(!rcc->stats_file){
53 fprintf(stderr, "failed to open " STATS_FILE "\n");
54 return -1;
55 }
56
57 /* find number of pics without reading the file twice :) */
58 fseek(rcc->stats_file, 0, SEEK_END);
59 size= ftell(rcc->stats_file);
60 fseek(rcc->stats_file, 0, SEEK_SET);
61
62 size/= 64; // we need at least 64 byte to store a line ...
63 rcc->entry = (RateControlEntry*)av_mallocz(size*sizeof(RateControlEntry));
64
65 for(i=0; !feof(rcc->stats_file); i++){
66 RateControlEntry *rce;
67 int picture_number;
68 int e;
69
70 e= fscanf(rcc->stats_file, "in:%d ", &picture_number);
71 rce= &rcc->entry[picture_number];
72 e+=fscanf(rcc->stats_file, "out:%*d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%*d bcode:%*d\n",
73 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits);
74 if(e!=7){
75 fprintf(stderr, STATS_FILE " is damaged\n");
76 return -1;
77 }
78 }
79 rcc->num_entries= i;
80
81 if(init_pass2(s) < 0) return -1;
82 }
83
84 /* no 2pass stuff, just normal 1-pass */
85 //initial values, they dont really matter as they will be totally different within a few frames
86 s->i_pred.coeff= s->p_pred.coeff= 7.0;
87 s->i_pred.count= s->p_pred.count= 1.0;
88
89 s->i_pred.decay= s->p_pred.decay= 0.4;
90
91 // use more bits at the beginning, otherwise high motion at the begin will look like shit
92 s->qsum=100 * s->qmin;
93 s->qcount=100;
94
95 s->short_term_qsum=0.001;
96 s->short_term_qcount=0.001;
97
98 return 0;
99}
100
101void ff_rate_control_uninit(MpegEncContext *s)
102{
103 RateControlContext *rcc= &s->rc_context;
104 emms_c();
105
6000abfa
FB
106 if(rcc->stats_file)
107 fclose(rcc->stats_file);
108 rcc->stats_file = NULL;
109 av_freep(&rcc->entry);
8b4c7dbc
MN
110}
111
112//----------------------------------
113// 1 Pass Code
114
115static double predict(Predictor *p, double q, double var)
116{
117 return p->coeff*var / (q*p->count);
118}
119
120static void update_predictor(Predictor *p, double q, double var, double size)
121{
122 double new_coeff= size*q / (var + 1);
123 if(var<1000) return;
124
125 p->count*= p->decay;
126 p->coeff*= p->decay;
127 p->count++;
128 p->coeff+= new_coeff;
129}
130
131int ff_rate_estimate_qscale(MpegEncContext *s)
132{
133 int qmin= s->qmin;
134 int qmax= s->qmax;
135 int rate_q=5;
136 float q;
137 int qscale;
138 float br_compensation;
139 double diff;
140 double short_term_q;
141 double long_term_q;
142 double fps;
143 int picture_number= s->input_picture_number - s->max_b_frames;
144 int64_t wanted_bits;
145 emms_c();
146
147 fps= (double)s->frame_rate / FRAME_RATE_BASE;
148 wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
149// printf("%d %d %d\n", picture_number, (int)wanted_bits, (int)s->total_bits);
150
151 if(s->pict_type==B_TYPE){
9effc0c7
MN
152 qmin= (int)(qmin*s->b_quant_factor+s->b_quant_offset + 0.5);
153 qmax= (int)(qmax*s->b_quant_factor+s->b_quant_offset + 0.5);
8b4c7dbc 154 }
4b7b1960 155 if(qmin<1) qmin=1;
8b4c7dbc
MN
156 if(qmax>31) qmax=31;
157 if(qmax<=qmin) qmax= qmin;
158
159 /* update predictors */
160 if(picture_number>2){
161 if(s->pict_type!=B_TYPE && s->last_non_b_pict_type == P_TYPE){
162//printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
163 update_predictor(&s->p_pred, s->last_non_b_qscale, s->last_non_b_mc_mb_var, s->pb_frame_bits);
164 }
165 }
166
167 if(s->pict_type == I_TYPE){
168 short_term_q= s->short_term_qsum/s->short_term_qcount;
169
170 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
171
172 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
173 }else if(s->pict_type==B_TYPE){
9effc0c7 174 q= (int)(s->last_non_b_qscale*s->b_quant_factor+s->b_quant_offset + 0.5);
8b4c7dbc
MN
175 }else{ //P Frame
176 int i;
177 int diff, best_diff=1000000000;
178 for(i=1; i<=31; i++){
9effc0c7 179 diff= predict(&s->p_pred, i, s->mc_mb_var_sum) - (double)s->bit_rate/fps;
8b4c7dbc
MN
180 if(diff<0) diff= -diff;
181 if(diff<best_diff){
182 best_diff= diff;
183 rate_q= i;
184 }
185 }
186 s->short_term_qsum*=s->qblur;
187 s->short_term_qcount*=s->qblur;
188
189 s->short_term_qsum+= rate_q;
190 s->short_term_qcount++;
191 short_term_q= s->short_term_qsum/s->short_term_qcount;
192
193 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
194
195// q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
196 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
197 }
198
199 diff= s->total_bits - wanted_bits;
200 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
201 if(br_compensation<=0.0) br_compensation=0.001;
202 q/=br_compensation;
203//printf("%f %f %f\n", q, br_compensation, short_term_q);
204 qscale= (int)(q + 0.5);
205 if (qscale<qmin) qscale=qmin;
206 else if(qscale>qmax) qscale=qmax;
207
208 if(s->pict_type!=B_TYPE){
209 s->qsum+= qscale;
210 s->qcount++;
211 if (qscale<s->last_non_b_qscale-s->max_qdiff) qscale=s->last_non_b_qscale-s->max_qdiff;
212 else if(qscale>s->last_non_b_qscale+s->max_qdiff) qscale=s->last_non_b_qscale+s->max_qdiff;
213 }
214//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
215// rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
216//printf("%d %d\n", s->bit_rate, (int)fps);
217 return qscale;
218}
219
220//----------------------------------------------
221// 2-Pass code
222
223static int init_pass2(MpegEncContext *s)
224{
225 RateControlContext *rcc= &s->rc_context;
226 int i;
227 double fps= (double)s->frame_rate / FRAME_RATE_BASE;
228 double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1
229 double avg_quantizer[5];
230 uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits
231 uint64_t available_bits[5];
232 uint64_t all_const_bits;
233 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
234 int num_frames[5]={0,0,0,0,0};
235 double rate_factor=0;
236 double step;
237 int last_i_frame=-10000000;
238
239 /* find complexity & const_bits & decide the pict_types */
240 for(i=0; i<rcc->num_entries; i++){
241 RateControlEntry *rce= &rcc->entry[i];
242
243 if(s->b_frame_strategy==0 || s->max_b_frames==0){
244 rce->new_pict_type= rce->pict_type;
245 }else{
246 int j;
247 int next_non_b_type=P_TYPE;
248
249 switch(rce->pict_type){
250 case I_TYPE:
251 if(i-last_i_frame>s->gop_size/2){ //FIXME this is not optimal
252 rce->new_pict_type= I_TYPE;
253 last_i_frame= i;
254 }else{
255 rce->new_pict_type= P_TYPE; // will be caught by the scene detection anyway
256 }
257 break;
258 case P_TYPE:
259 rce->new_pict_type= P_TYPE;
260 break;
261 case B_TYPE:
262 for(j=i+1; j<i+s->max_b_frames+2 && j<rcc->num_entries; j++){
263 if(rcc->entry[j].pict_type != B_TYPE){
264 next_non_b_type= rcc->entry[j].pict_type;
265 break;
266 }
267 }
268 if(next_non_b_type==I_TYPE)
269 rce->new_pict_type= P_TYPE;
270 else
271 rce->new_pict_type= B_TYPE;
272 break;
273 }
274 }
275
276 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
277 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
278 num_frames[rce->new_pict_type]++;
279 }
280 all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];
281
282 if(all_available_bits < all_const_bits){
283 fprintf(stderr, "requested bitrate is to low\n");
284 return -1;
285 }
286
287// avg_complexity= complexity/rcc->num_entries;
288 avg_quantizer[P_TYPE]=
289 avg_quantizer[I_TYPE]= (complexity[I_TYPE]+complexity[P_TYPE] + complexity[B_TYPE]/s->b_quant_factor)
290 / (all_available_bits - all_const_bits);
9effc0c7 291 avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*s->b_quant_factor + s->b_quant_offset;
8b4c7dbc
MN
292//printf("avg quantizer: %f %f\n", avg_quantizer[P_TYPE], avg_quantizer[B_TYPE]);
293
294 for(i=0; i<5; i++){
295 available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i];
296 }
297//printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
298
299 for(step=256*256; step>0.0000001; step*=0.5){
300 uint64_t expected_bits=0;
301 rate_factor+= step;
302 /* find qscale */
303 for(i=0; i<rcc->num_entries; i++){
304 RateControlEntry *rce= &rcc->entry[i];
305 double short_term_q, q, bits_left;
306 const int pict_type= rce->new_pict_type;
307 int qmin= s->qmin;
308 int qmax= s->qmax;
309
310 if(pict_type==B_TYPE){
9effc0c7
MN
311 qmin= (int)(qmin*s->b_quant_factor+s->b_quant_offset + 0.5);
312 qmax= (int)(qmax*s->b_quant_factor+s->b_quant_offset + 0.5);
8b4c7dbc 313 }
4b7b1960 314 if(qmin<1) qmin=1;
8b4c7dbc
MN
315 if(qmax>31) qmax=31;
316 if(qmax<=qmin) qmax= qmin;
317
318 switch(s->rc_strategy){
319 case 0:
320 bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor - rce->misc_bits - rce->mv_bits;
321 if(bits_left<1.0) bits_left=1.0;
322 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;
323 break;
324 case 1:
325 bits_left= (available_bits[pict_type] - const_bits[pict_type])/num_frames[pict_type]*rate_factor;
326 if(bits_left<1.0) bits_left=1.0;
327 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;
328 break;
329 case 2:
330 bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor;
331 if(bits_left<1.0) bits_left=1.0;
332 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits + rce->mv_bits)/bits_left;
333 break;
334 default:
335 fprintf(stderr, "unknown strategy\n");
336 short_term_q=3; //gcc warning fix
337 }
338
339 if(short_term_q>31.0) short_term_q=31.0;
340 else if (short_term_q<1.0) short_term_q=1.0;
341
342 q= 1/((1/avg_quantizer[pict_type] - 1/short_term_q)*s->qcompress + 1/short_term_q);
343 if (q<qmin) q=qmin;
344 else if(q>qmax) q=qmax;
90864f27 345//printf("lq:%f, sq:%f t:%f q:%f\n", avg_quantizer[rce->pict_type], short_term_q, bits_left, q);
8b4c7dbc
MN
346 rce->new_qscale= q;
347 }
348
349 /* smooth curve */
350
351 /* find expected bits */
352 for(i=0; i<rcc->num_entries; i++){
353 RateControlEntry *rce= &rcc->entry[i];
354 double factor= rce->qscale / rce->new_qscale;
355
356 rce->expected_bits= expected_bits;
357 expected_bits += (int)(rce->misc_bits + rce->mv_bits + (rce->i_tex_bits + rce->p_tex_bits)*factor + 0.5);
358 }
359
360// printf("%d %d %f\n", (int)expected_bits, (int)all_available_bits, rate_factor);
361 if(expected_bits > all_available_bits) rate_factor-= step;
362 }
363
364 return 0;
365}
366
367int ff_rate_estimate_qscale_pass2(MpegEncContext *s)
368{
369 int qmin= s->qmin;
370 int qmax= s->qmax;
371 float q;
372 int qscale;
373 float br_compensation;
374 double diff;
375 int picture_number= s->picture_number;
376 RateControlEntry *rce= &s->rc_context.entry[picture_number];
377 int64_t wanted_bits= rce->expected_bits;
378 emms_c();
379
380// printf("%d %d %d\n", picture_number, (int)wanted_bits, (int)s->total_bits);
381
382 if(s->pict_type==B_TYPE){
9effc0c7
MN
383 qmin= (int)(qmin*s->b_quant_factor+s->b_quant_offset + 0.5);
384 qmax= (int)(qmax*s->b_quant_factor+s->b_quant_offset + 0.5);
8b4c7dbc 385 }
4b7b1960 386 if(qmin<1) qmin=1;
8b4c7dbc
MN
387 if(qmax>31) qmax=31;
388 if(qmax<=qmin) qmax= qmin;
389
390 q= rce->new_qscale;
391
392 diff= s->total_bits - wanted_bits;
393 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
394 if(br_compensation<=0.0) br_compensation=0.001;
395 q/=br_compensation;
396
397 qscale= (int)(q + 0.5);
398 if (qscale<qmin) qscale=qmin;
399 else if(qscale>qmax) qscale=qmax;
400// printf("%d %d %d %d type:%d\n", qmin, qscale, qmax, picture_number, s->pict_type); fflush(stdout);
401 return qscale;
402}