Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ratecontrol.c @ 405469ce

History | View | Annotate | Download (25 KB)

1
/*
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
 */
20
#include <math.h>
21
#include "common.h"
22
#include "avcodec.h"
23
#include "dsputil.h"
24
#include "mpegvideo.h"
25

    
26
#undef NDEBUG // allways check asserts, the speed effect is far too small to disable them
27
#include <assert.h>
28

    
29
static int init_pass2(MpegEncContext *s);
30
static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num);
31

    
32
void ff_write_pass1_stats(MpegEncContext *s){
33
    sprintf(s->avctx->stats_out, "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;\n",
34
            s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type, 
35
            s->qscale, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, 
36
            s->f_code, s->b_code, s->mc_mb_var_sum, s->mb_var_sum, s->i_count);
37
}
38

    
39
int ff_rate_control_init(MpegEncContext *s)
40
{
41
    RateControlContext *rcc= &s->rc_context;
42
    int i;
43
    emms_c();
44

    
45
    for(i=0; i<5; i++){
46
        rcc->pred[i].coeff= 7.0;
47
        rcc->pred[i].count= 1.0;
48
    
49
        rcc->pred[i].decay= 0.4;
50
        rcc->i_cplx_sum [i]=
51
        rcc->p_cplx_sum [i]=
52
        rcc->mv_bits_sum[i]=
53
        rcc->qscale_sum [i]=
54
        rcc->frame_count[i]= 1; // 1 is better cuz of 1/0 and such
55
        rcc->last_qscale_for[i]=5;
56
    }
57
    rcc->buffer_index= s->avctx->rc_buffer_size/2;
58

    
59
    rcc->next_non_b_qscale=10;
60
    rcc->next_p_qscale=10;
61
    
62
    if(s->flags&CODEC_FLAG_PASS2){
63
        int i;
64
        char *p;
65

    
66
        /* find number of pics */
67
        p= s->avctx->stats_in;
68
        for(i=-1; p; i++){
69
            p= strchr(p+1, ';');
70
        }
71
        i+= s->max_b_frames;
72
        rcc->entry = (RateControlEntry*)av_mallocz(i*sizeof(RateControlEntry));
73
        rcc->num_entries= i;
74
        
75
        /* init all to skiped p frames (with b frames we might have a not encoded frame at the end FIXME) */
76
        for(i=0; i<rcc->num_entries; i++){
77
            RateControlEntry *rce= &rcc->entry[i];
78
            rce->pict_type= rce->new_pict_type=P_TYPE;
79
            rce->qscale= rce->new_qscale=2;
80
            rce->misc_bits= s->mb_num + 10;
81
            rce->mb_var_sum= s->mb_num*100;
82
        }        
83
        
84
        /* read stats */
85
        p= s->avctx->stats_in;
86
        for(i=0; i<rcc->num_entries - s->max_b_frames; i++){
87
            RateControlEntry *rce;
88
            int picture_number;
89
            int e;
90
            char *next;
91

    
92
            next= strchr(p, ';');
93
            if(next){
94
                (*next)=0; //sscanf in unbelieavle slow on looong strings //FIXME copy / dont write
95
                next++;
96
            }
97
            e= sscanf(p, " in:%d ", &picture_number);
98

    
99
            assert(picture_number >= 0);
100
            assert(picture_number < rcc->num_entries);
101
            rce= &rcc->entry[picture_number];
102

    
103
            e+=sscanf(p, " 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",
104
                   &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits, 
105
                   &rce->f_code, &rce->b_code, &rce->mc_mb_var_sum, &rce->mb_var_sum, &rce->i_count);
106
            if(e!=12){
107
                fprintf(stderr, "statistics are damaged at line %d, parser out=%d\n", i, e);
108
                return -1;
109
            }
110
            p= next;
111
        }
112
        
113
        if(init_pass2(s) < 0) return -1;
114
    }
115
     
116
    if(!(s->flags&CODEC_FLAG_PASS2)){
117

    
118
        rcc->short_term_qsum=0.001;
119
        rcc->short_term_qcount=0.001;
120
    
121
        rcc->pass1_bits       =0.001;
122
        rcc->pass1_wanted_bits=0.001;
123
        
124
        /* init stuff with the user specified complexity */
125
        if(s->avctx->rc_initial_cplx){
126
            for(i=0; i<60*30; i++){
127
                double bits= s->avctx->rc_initial_cplx * (i/10000.0 + 1.0)*s->mb_num;
128
                RateControlEntry rce;
129
                double q;
130
                
131
                if     (i%((s->gop_size+3)/4)==0) rce.pict_type= I_TYPE;
132
                else if(i%(s->max_b_frames+1))    rce.pict_type= B_TYPE;
133
                else                              rce.pict_type= P_TYPE;
134

    
135
                rce.new_pict_type= rce.pict_type;
136
                rce.mc_mb_var_sum= bits*s->mb_num/100000;
137
                rce.mb_var_sum   = s->mb_num;
138
                rce.qscale   = 2;
139
                rce.f_code   = 2;
140
                rce.b_code   = 1;
141
                rce.misc_bits= 1;
142

    
143
                if(s->pict_type== I_TYPE){
144
                    rce.i_count   = s->mb_num;
145
                    rce.i_tex_bits= bits;
146
                    rce.p_tex_bits= 0;
147
                    rce.mv_bits= 0;
148
                }else{
149
                    rce.i_count   = 0; //FIXME we do know this approx
150
                    rce.i_tex_bits= 0;
151
                    rce.p_tex_bits= bits*0.9;
152
                    rce.mv_bits= bits*0.1;
153
                }
154
                rcc->i_cplx_sum [rce.pict_type] += rce.i_tex_bits*rce.qscale;
155
                rcc->p_cplx_sum [rce.pict_type] += rce.p_tex_bits*rce.qscale;
156
                rcc->mv_bits_sum[rce.pict_type] += rce.mv_bits;
157
                rcc->frame_count[rce.pict_type] ++;
158

    
159
                bits= rce.i_tex_bits + rce.p_tex_bits;
160

    
161
                q= get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_bits, i);
162
                rcc->pass1_wanted_bits+= s->bit_rate/(s->frame_rate / (double)FRAME_RATE_BASE);
163
            }
164
        }
165

    
166
    }
167
    
168
    return 0;
169
}
170

    
171
void ff_rate_control_uninit(MpegEncContext *s)
172
{
173
    RateControlContext *rcc= &s->rc_context;
174
    emms_c();
175

    
176
    av_freep(&rcc->entry);
177
}
178

    
179
static inline double qp2bits(RateControlEntry *rce, double qp){
180
    if(qp<=0.0){
181
        fprintf(stderr, "qp<=0.0\n");
182
    }
183
    return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ qp;
184
}
185

    
186
static inline double bits2qp(RateControlEntry *rce, double bits){
187
    if(bits<0.9){
188
        fprintf(stderr, "bits<0.9\n");
189
    }
190
    return rce->qscale * (double)(rce->i_tex_bits + rce->p_tex_bits+1)/ bits;
191
}
192
    
193
static void update_rc_buffer(MpegEncContext *s, int frame_size){
194
    RateControlContext *rcc= &s->rc_context;
195
    const double fps= (double)s->frame_rate / FRAME_RATE_BASE;
196
    const double buffer_size= s->avctx->rc_buffer_size;
197
    const double min_rate= s->avctx->rc_min_rate/fps;
198
    const double max_rate= s->avctx->rc_max_rate/fps;
199

    
200
    if(buffer_size){
201
        rcc->buffer_index-= frame_size;
202
        if(rcc->buffer_index < buffer_size/2 /*FIXME /2 */ || min_rate==0){
203
            rcc->buffer_index+= max_rate;
204
            if(rcc->buffer_index >= buffer_size)
205
                rcc->buffer_index= buffer_size-1;
206
        }else{
207
            rcc->buffer_index+= min_rate;
208
        }
209
        
210
        if(rcc->buffer_index < 0)
211
            fprintf(stderr, "rc buffer underflow\n");
212
        if(rcc->buffer_index >= s->avctx->rc_buffer_size)
213
            fprintf(stderr, "rc buffer overflow\n");
214
    }
215
}
216

    
217
/**
218
 * modifies the bitrate curve from pass1 for one frame
219
 */
220
static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_factor, int frame_num){
221
    RateControlContext *rcc= &s->rc_context;
222
    double q, bits;
223
    const int pict_type= rce->new_pict_type;
224
    const double mb_num= s->mb_num;  
225
    int i;
226
    const double last_q= rcc->last_qscale_for[pict_type];
227

    
228
    double const_values[]={
229
        M_PI,
230
        M_E,
231
        rce->i_tex_bits*rce->qscale,
232
        rce->p_tex_bits*rce->qscale,
233
        (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale,
234
        rce->mv_bits/mb_num,
235
        rce->pict_type == B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
236
        rce->i_count/mb_num,
237
        rce->mc_mb_var_sum/mb_num,
238
        rce->mb_var_sum/mb_num,
239
        rce->pict_type == I_TYPE,
240
        rce->pict_type == P_TYPE,
241
        rce->pict_type == B_TYPE,
242
        rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
243
        s->qcompress,
244
/*        rcc->last_qscale_for[I_TYPE],
245
        rcc->last_qscale_for[P_TYPE],
246
        rcc->last_qscale_for[B_TYPE],
247
        rcc->next_non_b_qscale,*/
248
        rcc->i_cplx_sum[I_TYPE] / (double)rcc->frame_count[I_TYPE],
249
        rcc->i_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
250
        rcc->p_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
251
        rcc->p_cplx_sum[B_TYPE] / (double)rcc->frame_count[B_TYPE],
252
        (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
253
        0
254
    };
255
    char *const_names[]={
256
        "PI",
257
        "E",
258
        "iTex",
259
        "pTex",
260
        "tex",
261
        "mv",
262
        "fCode",
263
        "iCount",
264
        "mcVar",
265
        "var",
266
        "isI",
267
        "isP",
268
        "isB",
269
        "avgQP",
270
        "qComp",
271
/*        "lastIQP",
272
        "lastPQP",
273
        "lastBQP",
274
        "nextNonBQP",*/
275
        "avgIITex",
276
        "avgPITex",
277
        "avgPPTex",
278
        "avgBPTex",
279
        "avgTex",
280
        NULL
281
    };
282
    double (*func1[])(void *, double)={
283
        bits2qp,
284
        qp2bits,
285
        NULL
286
    };
287
    char *func1_names[]={
288
        "bits2qp",
289
        "qp2bits",
290
        NULL
291
    };
292

    
293
    bits= ff_eval(s->avctx->rc_eq, const_values, const_names, func1, func1_names, NULL, NULL, rce);
294
    
295
    rcc->pass1_bits+= bits;
296
    bits*=rate_factor;
297
    if(bits<0.0) bits=0.0;
298
    bits+= 1.0; //avoid 1/0 issues
299
    
300
    /* user override */
301
    for(i=0; i<s->avctx->rc_override_count; i++){
302
        RcOverride *rco= s->avctx->rc_override;
303
        if(rco[i].start_frame > frame_num) continue;
304
        if(rco[i].end_frame   < frame_num) continue;
305
    
306
        if(rco[i].qscale) 
307
            bits= qp2bits(rce, rco[i].qscale); //FIXME move at end to really force it?
308
        else
309
            bits*= rco[i].quality_factor;
310
    }
311

    
312
    q= bits2qp(rce, bits);
313
    
314
    /* I/B difference */
315
    if     (pict_type==I_TYPE && s->avctx->i_quant_factor<0.0)
316
        q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
317
    else if(pict_type==B_TYPE && s->avctx->b_quant_factor<0.0)
318
        q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
319
    
320
    /* last qscale / qdiff stuff */
321
    if     (q > last_q + s->max_qdiff) q= last_q + s->max_qdiff;
322
    else if(q < last_q - s->max_qdiff) q= last_q - s->max_qdiff;
323

    
324
    rcc->last_qscale_for[pict_type]= q; //Note we cant do that after blurring
325
    
326
    return q;
327
}
328

    
329
/**
330
 * gets the qmin & qmax for pict_type
331
 */
332
static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type){
333
    int qmin= s->qmin;                                                       
334
    int qmax= s->qmax;
335

    
336
    if(pict_type==B_TYPE){
337
        qmin= (int)(qmin*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
338
        qmax= (int)(qmax*ABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
339
    }else if(pict_type==I_TYPE){
340
        qmin= (int)(qmin*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
341
        qmax= (int)(qmax*ABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
342
    }
343

    
344
    if(qmin<1) qmin=1;
345
    if(qmin==1 && s->qmin>1) qmin=2; //avoid qmin=1 unless the user wants qmin=1
346

    
347
    if(qmin<3 && s->max_qcoeff<=128 && pict_type==I_TYPE) qmin=3; //reduce cliping problems
348

    
349
    if(qmax>31) qmax=31;
350
    if(qmax<=qmin) qmax= qmin= (qmax+qmin+1)>>1;
351
    
352
    *qmin_ret= qmin;
353
    *qmax_ret= qmax;
354
}
355

    
356
static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, int frame_num){
357
    RateControlContext *rcc= &s->rc_context;
358
    int qmin, qmax;
359
    double bits;
360
    const int pict_type= rce->new_pict_type;
361
    const double buffer_size= s->avctx->rc_buffer_size;
362
    const double min_rate= s->avctx->rc_min_rate;
363
    const double max_rate= s->avctx->rc_max_rate;
364
    
365
    get_qminmax(&qmin, &qmax, s, pict_type);
366

    
367
    /* modulation */
368
    if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==P_TYPE)
369
        q*= s->avctx->rc_qmod_amp;
370

    
371
    bits= qp2bits(rce, q);
372

    
373
    /* buffer overflow/underflow protection */
374
    if(buffer_size){
375
        double expected_size= rcc->buffer_index - bits;
376

    
377
        if(min_rate){
378
            double d= 2*(buffer_size - (expected_size + min_rate))/buffer_size;
379
            if(d>1.0) d=1.0;
380
            q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
381
        }
382

    
383
        if(max_rate){
384
            double d= 2*expected_size/buffer_size;
385
            if(d>1.0) d=1.0;
386
            q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity);
387
        }
388
    }
389

    
390
    if(s->avctx->rc_qsquish==0.0 || qmin==qmax){
391
        if     (q<qmin) q=qmin;
392
        else if(q>qmax) q=qmax;
393
    }else{
394
        double min2= log(qmin);
395
        double max2= log(qmax);
396
        
397
        q= log(q);
398
        q= (q - min2)/(max2-min2) - 0.5;
399
        q*= -4.0;
400
        q= 1.0/(1.0 + exp(q));
401
        q= q*(max2-min2) + min2;
402
        
403
        q= exp(q);
404
    }
405

    
406
    return q;
407
}
408

    
409
//----------------------------------
410
// 1 Pass Code
411

    
412
static double predict_size(Predictor *p, double q, double var)
413
{
414
     return p->coeff*var / (q*p->count);
415
}
416

    
417
static double predict_qp(Predictor *p, double size, double var)
418
{
419
//printf("coeff:%f, count:%f, var:%f, size:%f//\n", p->coeff, p->count, var, size);
420
     return p->coeff*var / (size*p->count);
421
}
422

    
423
static void update_predictor(Predictor *p, double q, double var, double size)
424
{
425
    double new_coeff= size*q / (var + 1);
426
    if(var<10) return;
427

    
428
    p->count*= p->decay;
429
    p->coeff*= p->decay;
430
    p->count++;
431
    p->coeff+= new_coeff;
432
}
433

    
434
int ff_rate_estimate_qscale(MpegEncContext *s)
435
{
436
    float q;
437
    int qscale, qmin, qmax;
438
    float br_compensation;
439
    double diff;
440
    double short_term_q;
441
    double fps;
442
    int picture_number= s->picture_number;
443
    int64_t wanted_bits;
444
    RateControlContext *rcc= &s->rc_context;
445
    RateControlEntry local_rce, *rce;
446
    double bits;
447
    double rate_factor;
448
    int var;
449
    const int pict_type= s->pict_type;
450
    emms_c();
451

    
452
    get_qminmax(&qmin, &qmax, s, pict_type);
453

    
454
    fps= (double)s->frame_rate / FRAME_RATE_BASE;
455
//printf("input_picture_number:%d picture_number:%d\n", s->input_picture_number, s->picture_number);
456
        /* update predictors */
457
    if(picture_number>2){
458
        const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
459
        update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
460
    }
461

    
462
    if(s->flags&CODEC_FLAG_PASS2){
463
        assert(picture_number>=0);
464
        assert(picture_number<rcc->num_entries);
465
        rce= &rcc->entry[picture_number];
466
        wanted_bits= rce->expected_bits;
467
    }else{
468
        rce= &local_rce;
469
        wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
470
    }
471

    
472
    diff= s->total_bits - wanted_bits;
473
    br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
474
    if(br_compensation<=0.0) br_compensation=0.001;
475

    
476
    var= pict_type == I_TYPE ? s->mb_var_sum : s->mc_mb_var_sum;
477
    
478
    if(s->flags&CODEC_FLAG_PASS2){
479
        if(pict_type!=I_TYPE)
480
            assert(pict_type == rce->new_pict_type);
481

    
482
        q= rce->new_qscale / br_compensation;
483
//printf("%f %f %f last:%d var:%d type:%d//\n", q, rce->new_qscale, br_compensation, s->frame_bits, var, pict_type);
484
    }else{
485
        rce->pict_type= 
486
        rce->new_pict_type= pict_type;
487
        rce->mc_mb_var_sum= s->mc_mb_var_sum;
488
        rce->mb_var_sum   = s->   mb_var_sum;
489
        rce->qscale   = 2;
490
        rce->f_code   = s->f_code;
491
        rce->b_code   = s->b_code;
492
        rce->misc_bits= 1;
493

    
494
        if(picture_number>0)
495
            update_rc_buffer(s, s->frame_bits);
496

    
497
        bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
498
        if(pict_type== I_TYPE){
499
            rce->i_count   = s->mb_num;
500
            rce->i_tex_bits= bits;
501
            rce->p_tex_bits= 0;
502
            rce->mv_bits= 0;
503
        }else{
504
            rce->i_count   = 0; //FIXME we do know this approx
505
            rce->i_tex_bits= 0;
506
            rce->p_tex_bits= bits*0.9;
507
            
508
            rce->mv_bits= bits*0.1;
509
        }
510
        rcc->i_cplx_sum [pict_type] += rce->i_tex_bits*rce->qscale;
511
        rcc->p_cplx_sum [pict_type] += rce->p_tex_bits*rce->qscale;
512
        rcc->mv_bits_sum[pict_type] += rce->mv_bits;
513
        rcc->frame_count[pict_type] ++;
514

    
515
        bits= rce->i_tex_bits + rce->p_tex_bits;
516
        rate_factor= rcc->pass1_wanted_bits/rcc->pass1_bits * br_compensation;
517
    
518
        q= get_qscale(s, rce, rate_factor, picture_number);
519

    
520
        assert(q>0.0);
521
//printf("%f ", q);
522
        if     (pict_type==I_TYPE && s->avctx->i_quant_factor>0.0)
523
            q= rcc->next_p_qscale*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
524
        else if(pict_type==B_TYPE && s->avctx->b_quant_factor>0.0)
525
            q= rcc->next_non_b_qscale*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
526
//printf("%f ", q);
527
        assert(q>0.0);
528

    
529
        if(pict_type==P_TYPE || s->intra_only){ //FIXME type dependant blur like in 2-pass
530
            rcc->short_term_qsum*=s->qblur;
531
            rcc->short_term_qcount*=s->qblur;
532

    
533
            rcc->short_term_qsum+= q;
534
            rcc->short_term_qcount++;
535
//printf("%f ", q);
536
            q= short_term_q= rcc->short_term_qsum/rcc->short_term_qcount;
537
//printf("%f ", q);
538
        }
539
        q= modify_qscale(s, rce, q, picture_number);
540

    
541
        rcc->pass1_wanted_bits+= s->bit_rate/fps;
542

    
543
        assert(q>0.0);
544

    
545
        if(pict_type != B_TYPE) rcc->next_non_b_qscale= q;
546
        if(pict_type == P_TYPE) rcc->next_p_qscale= q;
547
    }
548
//printf("qmin:%d, qmax:%d, q:%f\n", qmin, qmax, q);
549
    
550

    
551
    if     (q<qmin) q=qmin; 
552
    else if(q>qmax) q=qmax;
553
        
554
//    printf("%f %d %d %d\n", q, picture_number, (int)wanted_bits, (int)s->total_bits);
555
    
556

    
557
//printf("%f %f %f\n", q, br_compensation, short_term_q);
558
    qscale= (int)(q + 0.5);
559
//printf("%d ", qscale);
560
    
561
//printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, 
562
//       rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
563
//printf("%d %d\n", s->bit_rate, (int)fps);
564

    
565
    rcc->last_qscale= qscale;
566
    rcc->last_mc_mb_var_sum= s->mc_mb_var_sum;
567
    rcc->last_mb_var_sum= s->mb_var_sum;
568
    return qscale;
569
}
570

    
571
//----------------------------------------------
572
// 2-Pass code
573

    
574
static int init_pass2(MpegEncContext *s)
575
{
576
    RateControlContext *rcc= &s->rc_context;
577
    int i;
578
    double fps= (double)s->frame_rate / FRAME_RATE_BASE;
579
    double complexity[5]={0,0,0,0,0};   // aproximate bits at quant=1
580
    double avg_quantizer[5];
581
    uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits
582
    uint64_t available_bits[5];
583
    uint64_t all_const_bits;
584
    uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
585
    double rate_factor=0;
586
    double step;
587
    int last_i_frame=-10000000;
588
    const int filter_size= (int)(s->qblur*4) | 1;  
589
    double expected_bits;
590
    double *qscale, *blured_qscale;
591

    
592
    /* find complexity & const_bits & decide the pict_types */
593
    for(i=0; i<rcc->num_entries; i++){
594
        RateControlEntry *rce= &rcc->entry[i];
595
        
596
        if(s->b_frame_strategy==0 || s->max_b_frames==0){
597
            rce->new_pict_type= rce->pict_type;
598
        }else{
599
            int j;
600
            int next_non_b_type=P_TYPE;
601

    
602
            switch(rce->pict_type){
603
            case I_TYPE:
604
                if(i-last_i_frame>s->gop_size/2){ //FIXME this is not optimal
605
                    rce->new_pict_type= I_TYPE;
606
                    last_i_frame= i;
607
                }else{
608
                    rce->new_pict_type= P_TYPE; // will be caught by the scene detection anyway
609
                }
610
                break;
611
            case P_TYPE:
612
                rce->new_pict_type= P_TYPE;
613
                break;
614
            case B_TYPE:
615
                for(j=i+1; j<i+s->max_b_frames+2 && j<rcc->num_entries; j++){
616
                    if(rcc->entry[j].pict_type != B_TYPE){
617
                        next_non_b_type= rcc->entry[j].pict_type;
618
                        break;
619
                    }
620
                }
621
                if(next_non_b_type==I_TYPE)
622
                    rce->new_pict_type= P_TYPE;
623
                else
624
                    rce->new_pict_type= B_TYPE;
625
                break;
626
            }
627
        }
628
        rcc->i_cplx_sum [rce->pict_type] += rce->i_tex_bits*rce->qscale;
629
        rcc->p_cplx_sum [rce->pict_type] += rce->p_tex_bits*rce->qscale;
630
        rcc->mv_bits_sum[rce->pict_type] += rce->mv_bits;
631
        rcc->frame_count[rce->pict_type] ++;
632

    
633
        complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
634
        const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
635
    }
636
    all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];
637
    
638
    if(all_available_bits < all_const_bits){
639
        fprintf(stderr, "requested bitrate is to low\n");
640
        return -1;
641
    }
642
    
643
    /* find average quantizers */
644
    avg_quantizer[P_TYPE]=0;
645
    for(step=256*256; step>0.0000001; step*=0.5){
646
        double expected_bits=0;
647
        avg_quantizer[P_TYPE]+= step;
648
        
649
        avg_quantizer[I_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->i_quant_factor) + s->avctx->i_quant_offset;
650
        avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*ABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
651
        
652
        expected_bits= 
653
            + all_const_bits 
654
            + complexity[I_TYPE]/avg_quantizer[I_TYPE]
655
            + complexity[P_TYPE]/avg_quantizer[P_TYPE]
656
            + complexity[B_TYPE]/avg_quantizer[B_TYPE];
657
            
658
        if(expected_bits < all_available_bits) avg_quantizer[P_TYPE]-= step;
659
//printf("%f %lld %f\n", expected_bits, all_available_bits, avg_quantizer[P_TYPE]);
660
    }
661
//printf("qp_i:%f, qp_p:%f, qp_b:%f\n", avg_quantizer[I_TYPE],avg_quantizer[P_TYPE],avg_quantizer[B_TYPE]);
662

    
663
    for(i=0; i<5; i++){
664
        available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i];
665
    }
666
//printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
667
        
668
    qscale= malloc(sizeof(double)*rcc->num_entries);
669
    blured_qscale= malloc(sizeof(double)*rcc->num_entries);
670

    
671
    for(step=256*256; step>0.0000001; step*=0.5){
672
        expected_bits=0;
673
        rate_factor+= step;
674
        
675
        rcc->buffer_index= s->avctx->rc_buffer_size/2;
676

    
677
        /* find qscale */
678
        for(i=0; i<rcc->num_entries; i++){
679
            qscale[i]= get_qscale(s, &rcc->entry[i], rate_factor, i);
680
        }
681
        assert(filter_size%2==1);
682

    
683
        /* fixed I/B QP relative to P mode */
684
        rcc->next_non_b_qscale= 10;
685
        rcc->next_p_qscale= 10;
686
        for(i=rcc->num_entries-1; i>=0; i--){
687
            RateControlEntry *rce= &rcc->entry[i];
688
            const int pict_type= rce->new_pict_type;
689
        
690
            if     (pict_type==I_TYPE && s->avctx->i_quant_factor>0.0)
691
                qscale[i]= rcc->next_p_qscale*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
692
            else if(pict_type==B_TYPE && s->avctx->b_quant_factor>0.0)
693
                qscale[i]= rcc->next_non_b_qscale*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
694

    
695
            if(pict_type!=B_TYPE) 
696
                rcc->next_non_b_qscale= qscale[i];
697
            if(pict_type==P_TYPE) 
698
                rcc->next_p_qscale= qscale[i];
699
        }
700

    
701
        /* smooth curve */
702
        for(i=0; i<rcc->num_entries; i++){
703
            RateControlEntry *rce= &rcc->entry[i];
704
            const int pict_type= rce->new_pict_type;
705
            int j;
706
            double q=0.0, sum=0.0;
707
        
708
            for(j=0; j<filter_size; j++){
709
                int index= i+j-filter_size/2;
710
                double d= index-i;
711
                double coeff= s->qblur==0 ? 1.0 : exp(-d*d/(s->qblur * s->qblur));
712
            
713
                if(index < 0 || index >= rcc->num_entries) continue;
714
                if(pict_type != rcc->entry[index].new_pict_type) continue;
715
                q+= qscale[index] * coeff;
716
                sum+= coeff;
717
            }
718
            blured_qscale[i]= q/sum;
719
        }
720
    
721
        /* find expected bits */
722
        for(i=0; i<rcc->num_entries; i++){
723
            RateControlEntry *rce= &rcc->entry[i];
724
            double bits;
725
            rce->new_qscale= modify_qscale(s, rce, blured_qscale[i], i);
726
            bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
727
//printf("%d %f\n", rce->new_bits, blured_qscale[i]);
728
            update_rc_buffer(s, bits);
729

    
730
            rce->expected_bits= expected_bits;
731
            expected_bits += bits;
732
        }
733

    
734
//        printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor);
735
        if(expected_bits > all_available_bits) rate_factor-= step;
736
    }
737
    free(qscale);
738
    free(blured_qscale);
739

    
740
    if(abs(expected_bits/all_available_bits - 1.0) > 0.01 ){
741
        fprintf(stderr, "Error: 2pass curve failed to converge\n");
742
        return -1;
743
    }
744

    
745
    return 0;
746
}