~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/ratecontrol.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include "mpegvideo.h"
32
32
#include "eval.h"
33
33
 
34
 
#undef NDEBUG // allways check asserts, the speed effect is far too small to disable them
 
34
#undef NDEBUG // Always check asserts, the speed effect is far too small to disable them.
35
35
#include <assert.h>
36
36
 
37
37
#ifndef M_E
66
66
{
67
67
    RateControlContext *rcc= &s->rc_context;
68
68
    int i;
69
 
    char *error = NULL;
 
69
    const char *error = NULL;
70
70
    static const char *const_names[]={
71
71
        "PI",
72
72
        "E",
121
121
        rcc->p_cplx_sum [i]=
122
122
        rcc->mv_bits_sum[i]=
123
123
        rcc->qscale_sum [i]=
124
 
        rcc->frame_count[i]= 1; // 1 is better cuz of 1/0 and such
 
124
        rcc->frame_count[i]= 1; // 1 is better because of 1/0 and such
125
125
        rcc->last_qscale_for[i]=FF_QP2LAMBDA * 5;
126
126
    }
127
127
    rcc->buffer_index= s->avctx->rc_initial_buffer_occupancy;
138
138
        i+= s->max_b_frames;
139
139
        if(i<=0 || i>=INT_MAX / sizeof(RateControlEntry))
140
140
            return -1;
141
 
        rcc->entry = (RateControlEntry*)av_mallocz(i*sizeof(RateControlEntry));
 
141
        rcc->entry = av_mallocz(i*sizeof(RateControlEntry));
142
142
        rcc->num_entries= i;
143
143
 
144
144
        /* init all to skipped p frames (with b frames we might have a not encoded frame at the end FIXME) */
145
145
        for(i=0; i<rcc->num_entries; i++){
146
146
            RateControlEntry *rce= &rcc->entry[i];
147
 
            rce->pict_type= rce->new_pict_type=P_TYPE;
 
147
            rce->pict_type= rce->new_pict_type=FF_P_TYPE;
148
148
            rce->qscale= rce->new_qscale=FF_QP2LAMBDA * 2;
149
149
            rce->misc_bits= s->mb_num + 10;
150
150
            rce->mb_var_sum= s->mb_num*100;
160
160
 
161
161
            next= strchr(p, ';');
162
162
            if(next){
163
 
                (*next)=0; //sscanf in unbelieavle slow on looong strings //FIXME copy / dont write
 
163
                (*next)=0; //sscanf in unbelievably slow on looong strings //FIXME copy / do not write
164
164
                next++;
165
165
            }
166
166
            e= sscanf(p, " in:%d ", &picture_number);
184
184
 
185
185
        //FIXME maybe move to end
186
186
        if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID) {
187
 
#ifdef CONFIG_XVID
 
187
#ifdef CONFIG_LIBXVID
188
188
            return ff_xvid_rate_control_init(s);
189
189
#else
190
190
            av_log(s->avctx, AV_LOG_ERROR, "XviD ratecontrol requires libavcodec compiled with XviD support\n");
201
201
        rcc->pass1_rc_eq_output_sum= 0.001;
202
202
        rcc->pass1_wanted_bits=0.001;
203
203
 
 
204
        if(s->avctx->qblur > 1.0){
 
205
            av_log(s->avctx, AV_LOG_ERROR, "qblur too large\n");
 
206
            return -1;
 
207
        }
204
208
        /* init stuff with the user specified complexity */
205
209
        if(s->avctx->rc_initial_cplx){
206
210
            for(i=0; i<60*30; i++){
208
212
                RateControlEntry rce;
209
213
                double q;
210
214
 
211
 
                if     (i%((s->gop_size+3)/4)==0) rce.pict_type= I_TYPE;
212
 
                else if(i%(s->max_b_frames+1))    rce.pict_type= B_TYPE;
213
 
                else                              rce.pict_type= P_TYPE;
 
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;
214
218
 
215
219
                rce.new_pict_type= rce.pict_type;
216
220
                rce.mc_mb_var_sum= bits*s->mb_num/100000;
220
224
                rce.b_code   = 1;
221
225
                rce.misc_bits= 1;
222
226
 
223
 
                if(s->pict_type== I_TYPE){
 
227
                if(s->pict_type== FF_I_TYPE){
224
228
                    rce.i_count   = s->mb_num;
225
229
                    rce.i_tex_bits= bits;
226
230
                    rce.p_tex_bits= 0;
239
243
                bits= rce.i_tex_bits + rce.p_tex_bits;
240
244
 
241
245
                q= get_qscale(s, &rce, rcc->pass1_wanted_bits/rcc->pass1_rc_eq_output_sum, i);
242
 
                rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base)); //FIXME missbehaves a little for variable fps
 
246
                rcc->pass1_wanted_bits+= s->bit_rate/(1/av_q2d(s->avctx->time_base)); //FIXME misbehaves a little for variable fps
243
247
            }
244
248
        }
245
249
 
256
260
    ff_eval_free(rcc->rc_eq_eval);
257
261
    av_freep(&rcc->entry);
258
262
 
259
 
#ifdef CONFIG_XVID
 
263
#ifdef CONFIG_LIBXVID
260
264
    if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
261
265
        ff_xvid_rate_control_uninit(s);
262
266
#endif
316
320
        rce->p_tex_bits*rce->qscale,
317
321
        (rce->i_tex_bits + rce->p_tex_bits)*(double)rce->qscale,
318
322
        rce->mv_bits/mb_num,
319
 
        rce->pict_type == B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
 
323
        rce->pict_type == FF_B_TYPE ? (rce->f_code + rce->b_code)*0.5 : rce->f_code,
320
324
        rce->i_count/mb_num,
321
325
        rce->mc_mb_var_sum/mb_num,
322
326
        rce->mb_var_sum/mb_num,
323
 
        rce->pict_type == I_TYPE,
324
 
        rce->pict_type == P_TYPE,
325
 
        rce->pict_type == B_TYPE,
 
327
        rce->pict_type == FF_I_TYPE,
 
328
        rce->pict_type == FF_P_TYPE,
 
329
        rce->pict_type == FF_B_TYPE,
326
330
        rcc->qscale_sum[pict_type] / (double)rcc->frame_count[pict_type],
327
331
        a->qcompress,
328
 
/*        rcc->last_qscale_for[I_TYPE],
329
 
        rcc->last_qscale_for[P_TYPE],
330
 
        rcc->last_qscale_for[B_TYPE],
 
332
/*        rcc->last_qscale_for[FF_I_TYPE],
 
333
        rcc->last_qscale_for[FF_P_TYPE],
 
334
        rcc->last_qscale_for[FF_B_TYPE],
331
335
        rcc->next_non_b_qscale,*/
332
 
        rcc->i_cplx_sum[I_TYPE] / (double)rcc->frame_count[I_TYPE],
333
 
        rcc->i_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
334
 
        rcc->p_cplx_sum[P_TYPE] / (double)rcc->frame_count[P_TYPE],
335
 
        rcc->p_cplx_sum[B_TYPE] / (double)rcc->frame_count[B_TYPE],
 
336
        rcc->i_cplx_sum[FF_I_TYPE] / (double)rcc->frame_count[FF_I_TYPE],
 
337
        rcc->i_cplx_sum[FF_P_TYPE] / (double)rcc->frame_count[FF_P_TYPE],
 
338
        rcc->p_cplx_sum[FF_P_TYPE] / (double)rcc->frame_count[FF_P_TYPE],
 
339
        rcc->p_cplx_sum[FF_B_TYPE] / (double)rcc->frame_count[FF_B_TYPE],
336
340
        (rcc->i_cplx_sum[pict_type] + rcc->p_cplx_sum[pict_type]) / (double)rcc->frame_count[pict_type],
337
341
        0
338
342
    };
363
367
    q= bits2qp(rce, bits);
364
368
 
365
369
    /* I/B difference */
366
 
    if     (pict_type==I_TYPE && s->avctx->i_quant_factor<0.0)
 
370
    if     (pict_type==FF_I_TYPE && s->avctx->i_quant_factor<0.0)
367
371
        q= -q*s->avctx->i_quant_factor + s->avctx->i_quant_offset;
368
 
    else if(pict_type==B_TYPE && s->avctx->b_quant_factor<0.0)
 
372
    else if(pict_type==FF_B_TYPE && s->avctx->b_quant_factor<0.0)
369
373
        q= -q*s->avctx->b_quant_factor + s->avctx->b_quant_offset;
 
374
    if(q<1) q=1;
370
375
 
371
376
    return q;
372
377
}
375
380
    RateControlContext *rcc= &s->rc_context;
376
381
    AVCodecContext *a= s->avctx;
377
382
    const int pict_type= rce->new_pict_type;
378
 
    const double last_p_q    = rcc->last_qscale_for[P_TYPE];
 
383
    const double last_p_q    = rcc->last_qscale_for[FF_P_TYPE];
379
384
    const double last_non_b_q= rcc->last_qscale_for[rcc->last_non_b_pict_type];
380
385
 
381
 
    if     (pict_type==I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==P_TYPE))
 
386
    if     (pict_type==FF_I_TYPE && (a->i_quant_factor>0.0 || rcc->last_non_b_pict_type==FF_P_TYPE))
382
387
        q= last_p_q    *FFABS(a->i_quant_factor) + a->i_quant_offset;
383
 
    else if(pict_type==B_TYPE && a->b_quant_factor>0.0)
 
388
    else if(pict_type==FF_B_TYPE && a->b_quant_factor>0.0)
384
389
        q= last_non_b_q*    a->b_quant_factor  + a->b_quant_offset;
 
390
    if(q<1) q=1;
385
391
 
386
392
    /* last qscale / qdiff stuff */
387
 
    if(rcc->last_non_b_pict_type==pict_type || pict_type!=I_TYPE){
 
393
    if(rcc->last_non_b_pict_type==pict_type || pict_type!=FF_I_TYPE){
388
394
        double last_q= rcc->last_qscale_for[pict_type];
389
395
        const int maxdiff= FF_QP2LAMBDA * a->max_qdiff;
390
396
 
392
398
        else if(q < last_q - maxdiff) q= last_q - maxdiff;
393
399
    }
394
400
 
395
 
    rcc->last_qscale_for[pict_type]= q; //Note we cant do that after blurring
 
401
    rcc->last_qscale_for[pict_type]= q; //Note we cannot do that after blurring
396
402
 
397
 
    if(pict_type!=B_TYPE)
 
403
    if(pict_type!=FF_B_TYPE)
398
404
        rcc->last_non_b_pict_type= pict_type;
399
405
 
400
406
    return q;
409
415
 
410
416
    assert(qmin <= qmax);
411
417
 
412
 
    if(pict_type==B_TYPE){
 
418
    if(pict_type==FF_B_TYPE){
413
419
        qmin= (int)(qmin*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
414
420
        qmax= (int)(qmax*FFABS(s->avctx->b_quant_factor)+s->avctx->b_quant_offset + 0.5);
415
 
    }else if(pict_type==I_TYPE){
 
421
    }else if(pict_type==FF_I_TYPE){
416
422
        qmin= (int)(qmin*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
417
423
        qmax= (int)(qmax*FFABS(s->avctx->i_quant_factor)+s->avctx->i_quant_offset + 0.5);
418
424
    }
439
445
    get_qminmax(&qmin, &qmax, s, pict_type);
440
446
 
441
447
    /* modulation */
442
 
    if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==P_TYPE)
 
448
    if(s->avctx->rc_qmod_freq && frame_num%s->avctx->rc_qmod_freq==0 && pict_type==FF_P_TYPE)
443
449
        q*= s->avctx->rc_qmod_amp;
444
450
 
445
451
    bits= qp2bits(rce, q);
601
607
        bits_tab[i]= bits;
602
608
    }
603
609
 
604
 
    /* handle qmin/qmax cliping */
 
610
    /* handle qmin/qmax clipping */
605
611
    if(s->flags&CODEC_FLAG_NORMALIZE_AQP){
606
612
        float factor= bits_sum/cplx_sum;
607
613
        for(i=0; i<s->mb_num; i++){
672
678
    Picture * const pic= &s->current_picture;
673
679
    emms_c();
674
680
 
675
 
#ifdef CONFIG_XVID
 
681
#ifdef CONFIG_LIBXVID
676
682
    if((s->flags&CODEC_FLAG_PASS2) && s->avctx->rc_strategy == FF_RC_STRATEGY_XVID)
677
683
        return ff_xvid_rate_estimate_qscale(s, dry_run);
678
684
#endif
683
689
//printf("input_pic_num:%d pic_num:%d frame_rate:%d\n", s->input_picture_number, s->picture_number, s->frame_rate);
684
690
        /* update predictors */
685
691
    if(picture_number>2 && !dry_run){
686
 
        const int last_var= s->last_pict_type == I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
 
692
        const int last_var= s->last_pict_type == FF_I_TYPE ? rcc->last_mb_var_sum : rcc->last_mc_mb_var_sum;
687
693
        update_predictor(&rcc->pred[s->last_pict_type], rcc->last_qscale, sqrt(last_var), s->frame_bits);
688
694
    }
689
695
 
693
699
        rce= &rcc->entry[picture_number];
694
700
        wanted_bits= rce->expected_bits;
695
701
    }else{
 
702
        Picture *dts_pic;
696
703
        rce= &local_rce;
697
 
        wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
 
704
 
 
705
        //FIXME add a dts field to AVFrame and ensure its set and use it here instead of reordering
 
706
        //but the reordering is simpler for now until h.264 b pyramid must be handeld
 
707
        if(s->pict_type == FF_B_TYPE || s->low_delay)
 
708
            dts_pic= s->current_picture_ptr;
 
709
        else
 
710
            dts_pic= s->last_picture_ptr;
 
711
 
 
712
//if(dts_pic)
 
713
//            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);
 
714
 
 
715
        if(!dts_pic || dts_pic->pts == AV_NOPTS_VALUE)
 
716
            wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
 
717
        else
 
718
            wanted_bits= (uint64_t)(s->bit_rate*(double)dts_pic->pts/fps);
698
719
    }
699
720
 
700
721
    diff= s->total_bits - wanted_bits;
701
722
    br_compensation= (a->bit_rate_tolerance - diff)/a->bit_rate_tolerance;
702
723
    if(br_compensation<=0.0) br_compensation=0.001;
703
724
 
704
 
    var= pict_type == I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum;
 
725
    var= pict_type == FF_I_TYPE ? pic->mb_var_sum : pic->mc_mb_var_sum;
705
726
 
706
727
    short_term_q = 0; /* avoid warning */
707
728
    if(s->flags&CODEC_FLAG_PASS2){
708
 
        if(pict_type!=I_TYPE)
 
729
        if(pict_type!=FF_I_TYPE)
709
730
            assert(pict_type == rce->new_pict_type);
710
731
 
711
732
        q= rce->new_qscale / br_compensation;
721
742
        rce->misc_bits= 1;
722
743
 
723
744
        bits= predict_size(&rcc->pred[pict_type], rce->qscale, sqrt(var));
724
 
        if(pict_type== I_TYPE){
 
745
        if(pict_type== FF_I_TYPE){
725
746
            rce->i_count   = s->mb_num;
726
747
            rce->i_tex_bits= bits;
727
748
            rce->p_tex_bits= 0;
751
772
//printf("%f ", q);
752
773
        assert(q>0.0);
753
774
 
754
 
        if(pict_type==P_TYPE || s->intra_only){ //FIXME type dependent blur like in 2-pass
 
775
        if(pict_type==FF_P_TYPE || s->intra_only){ //FIXME type dependent blur like in 2-pass
755
776
            rcc->short_term_qsum*=a->qblur;
756
777
            rcc->short_term_qcount*=a->qblur;
757
778
 
819
840
    //int last_i_frame=-10000000;
820
841
    const int filter_size= (int)(a->qblur*4) | 1;
821
842
    double expected_bits;
822
 
    double *qscale, *blured_qscale, qscale_sum;
 
843
    double *qscale, *blurred_qscale, qscale_sum;
823
844
 
824
845
    /* find complexity & const_bits & decide the pict_types */
825
846
    for(i=0; i<rcc->num_entries; i++){
834
855
        complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
835
856
        const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
836
857
    }
837
 
    all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];
 
858
    all_const_bits= const_bits[FF_I_TYPE] + const_bits[FF_P_TYPE] + const_bits[FF_B_TYPE];
838
859
 
839
860
    if(all_available_bits < all_const_bits){
840
861
        av_log(s->avctx, AV_LOG_ERROR, "requested bitrate is too low\n");
842
863
    }
843
864
 
844
865
    qscale= av_malloc(sizeof(double)*rcc->num_entries);
845
 
    blured_qscale= av_malloc(sizeof(double)*rcc->num_entries);
 
866
    blurred_qscale= av_malloc(sizeof(double)*rcc->num_entries);
846
867
    toobig = 0;
847
868
 
848
869
    for(step=256*256; step>0.0000001; step*=0.5){
881
902
                q+= qscale[index] * coeff;
882
903
                sum+= coeff;
883
904
            }
884
 
            blured_qscale[i]= q/sum;
 
905
            blurred_qscale[i]= q/sum;
885
906
        }
886
907
 
887
908
        /* find expected bits */
888
909
        for(i=0; i<rcc->num_entries; i++){
889
910
            RateControlEntry *rce= &rcc->entry[i];
890
911
            double bits;
891
 
            rce->new_qscale= modify_qscale(s, rce, blured_qscale[i], i);
 
912
            rce->new_qscale= modify_qscale(s, rce, blurred_qscale[i], i);
892
913
            bits= qp2bits(rce, rce->new_qscale) + rce->mv_bits + rce->misc_bits;
893
 
//printf("%d %f\n", rce->new_bits, blured_qscale[i]);
 
914
//printf("%d %f\n", rce->new_bits, blurred_qscale[i]);
894
915
            bits += 8*ff_vbv_update(s, bits);
895
916
 
896
917
            rce->expected_bits= expected_bits;
908
929
        }
909
930
    }
910
931
    av_free(qscale);
911
 
    av_free(blured_qscale);
 
932
    av_free(blurred_qscale);
912
933
 
913
934
    /* check bitrate calculations and print info */
914
935
    qscale_sum = 0.0;