Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ratecontrol.c @ 19531051

History | View | Annotate | Download (30.2 KB)

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