Revision 7b4076a7 libavcodec/ac3dec.c

View differences:

libavcodec/ac3dec.c
85 85

  
86 86
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
87 87

  
88
/* override ac3.h to include coupling channel */
89
#undef AC3_MAX_CHANNELS
90
#define AC3_MAX_CHANNELS 7
91
#define CPL_CH 0
92

  
88 93
#define AC3_OUTPUT_LFEON  8
89 94

  
90 95
typedef struct {
......
103 108
    int rematstr;
104 109
    int nrematbnd;
105 110
    int rematflg[4];
106
    int cplexpstr;
107
    int lfeexpstr;
108
    int chexpstr[5];
109
    int cplsnroffst;
110
    int cplfgain;
111
    int snroffst[5];
112
    int fgain[5];
113
    int lfesnroffst;
114
    int lfefgain;
115
    int cpldeltbae;
116
    int deltbae[5];
117
    int cpldeltnseg;
118
    uint8_t  cpldeltoffst[8];
119
    uint8_t  cpldeltlen[8];
120
    uint8_t  cpldeltba[8];
121
    int deltnseg[5];
122
    uint8_t  deltoffst[5][8];
123
    uint8_t  deltlen[5][8];
124
    uint8_t  deltba[5][8];
111
    int expstr[AC3_MAX_CHANNELS];
112
    int snroffst[AC3_MAX_CHANNELS];
113
    int fgain[AC3_MAX_CHANNELS];
114
    int deltbae[AC3_MAX_CHANNELS];
115
    int deltnseg[AC3_MAX_CHANNELS];
116
    uint8_t  deltoffst[AC3_MAX_CHANNELS][8];
117
    uint8_t  deltlen[AC3_MAX_CHANNELS][8];
118
    uint8_t  deltba[AC3_MAX_CHANNELS][8];
125 119

  
126 120
    /* Derived Attributes. */
127 121
    int      sampling_rate;
......
131 125
    int      nchans;            //number of total channels
132 126
    int      nfchans;           //number of full-bandwidth channels
133 127
    int      lfeon;             //lfe channel in use
128
    int      lfe_ch;            ///< index of LFE channel
134 129
    int      output_mode;       ///< output channel configuration
135 130
    int      out_channels;      ///< number of output channels
136 131

  
137 132
    float    dynrng;            //dynamic range gain
138 133
    float    dynrng2;           //dynamic range gain for 1+1 mode
139
    float    cplco[5][18];      //coupling coordinates
134
    float    cplco[AC3_MAX_CHANNELS][18];   //coupling coordinates
140 135
    int      ncplbnd;           //number of coupling bands
141 136
    int      ncplsubnd;         //number of coupling sub bands
142
    int      cplstrtmant;       //coupling start mantissa
143
    int      cplendmant;        //coupling end mantissa
144
    int      endmant[5];        //channel end mantissas
137
    int      startmant[AC3_MAX_CHANNELS];   ///< start frequency bin
138
    int      endmant[AC3_MAX_CHANNELS];     //channel end mantissas
145 139
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
146 140

  
147
    int8_t   dcplexps[256];     //decoded coupling exponents
148
    int8_t   dexps[5][256];     //decoded fbw channel exponents
149
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
150
    uint8_t  cplbap[256];       //coupling bit allocation pointers
151
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
152
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
141
    int8_t   dexps[AC3_MAX_CHANNELS][256];  ///< decoded exponents
142
    uint8_t  bap[AC3_MAX_CHANNELS][256];    ///< bit allocation pointers
143
    int16_t  psd[AC3_MAX_CHANNELS][256];    ///< scaled exponents
144
    int16_t  bndpsd[AC3_MAX_CHANNELS][50];  ///< interpolated exponents
145
    int16_t  mask[AC3_MAX_CHANNELS][50];    ///< masking curve values
153 146

  
154
    float transform_coeffs_cpl[256];
155 147
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);  //transform coefficients
156 148

  
157 149
    /* For IMDCT. */
......
159 151
    MDCTContext imdct_256;  //for 256 sample imdct transform
160 152
    DSPContext  dsp;        //for optimization
161 153

  
162
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]);   //output after imdct transform and windowing
163
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]);    //delay - added to the next block
154
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]); //output after imdct transform and windowing
155
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]);  //delay - added to the next block
164 156
    DECLARE_ALIGNED_16(float, tmp_imdct[256]);                  //temporary storage for imdct transform
165 157
    DECLARE_ALIGNED_16(float, tmp_output[512]);                 //temporary storage for output before windowing
166 158
    DECLARE_ALIGNED_16(float, window[256]);                     //window coefficients
......
301 293
    ctx->bit_rate                     = hdr.bit_rate;
302 294
    ctx->nchans                       = hdr.channels;
303 295
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
296
    ctx->lfe_ch                       = ctx->nfchans + 1;
304 297
    ctx->frame_size                   = hdr.frame_size;
305 298

  
306 299
    /* set default output to all source channels */
......
401 394
    int i, j, ch, bnd, subbnd;
402 395

  
403 396
    subbnd = -1;
404
    i = ctx->cplstrtmant;
397
    i = ctx->startmant[CPL_CH];
405 398
    for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
406 399
        do {
407 400
            subbnd++;
408 401
            for(j=0; j<12; j++) {
409 402
                for(ch=1; ch<=ctx->nfchans; ch++) {
410
                    if(ctx->chincpl[ch-1])
411
                        ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd] * 8.0f;
403
                    if(ctx->chincpl[ch])
404
                        ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;
412 405
                }
413 406
                i++;
414 407
            }
......
434 427
    uint8_t *bap;
435 428
    float *coeffs;
436 429

  
437
    if (ch_index >= 0) { /* fbw channels */
438 430
        exps = ctx->dexps[ch_index];
439 431
        bap = ctx->bap[ch_index];
440
        coeffs = ctx->transform_coeffs[ch_index + 1];
441
        start = 0;
432
        coeffs = ctx->transform_coeffs[ch_index];
433
        start = ctx->startmant[ch_index];
442 434
        end = ctx->endmant[ch_index];
443
    } else if (ch_index == -1) {
444
        exps = ctx->dlfeexps;
445
        bap = ctx->lfebap;
446
        coeffs = ctx->transform_coeffs[0];
447
        start = 0;
448
        end = 7;
449
    } else {
450
        exps = ctx->dcplexps;
451
        bap = ctx->cplbap;
452
        coeffs = ctx->transform_coeffs_cpl;
453
        start = ctx->cplstrtmant;
454
        end = ctx->cplendmant;
455
    }
456 435

  
457 436

  
458 437
    for (i = start; i < end; i++) {
......
523 502
    uint8_t *bap;
524 503

  
525 504
    for(ch=1; ch<=ctx->nfchans; ch++) {
526
        if(!ctx->dithflag[ch-1]) {
505
        if(!ctx->dithflag[ch]) {
527 506
            coeffs = ctx->transform_coeffs[ch];
528
            bap = ctx->bap[ch-1];
529
            if(ctx->chincpl[ch-1])
530
                end = ctx->cplstrtmant;
507
            bap = ctx->bap[ch];
508
            if(ctx->chincpl[ch])
509
                end = ctx->startmant[CPL_CH];
531 510
            else
532
                end = ctx->endmant[ch-1];
511
                end = ctx->endmant[ch];
533 512
            for(i=0; i<end; i++) {
534 513
                if(bap[i] == 0)
535 514
                    coeffs[i] = 0.0f;
536 515
            }
537
            if(ctx->chincpl[ch-1]) {
538
                bap = ctx->cplbap;
539
                for(; i<ctx->cplendmant; i++) {
516
            if(ctx->chincpl[ch]) {
517
                bap = ctx->bap[CPL_CH];
518
                for(; i<ctx->endmant[CPL_CH]; i++) {
540 519
                    if(bap[i] == 0)
541 520
                        coeffs[i] = 0.0f;
542 521
                }
......
551 530
 */
552 531
static int get_transform_coeffs(AC3DecodeContext * ctx)
553 532
{
554
    int ch, i, end;
533
    int ch, end;
555 534
    int got_cplchan = 0;
556 535
    mant_groups m;
557 536

  
558 537
    m.b1ptr = m.b2ptr = m.b4ptr = 3;
559 538

  
560
    for (ch = 0; ch < ctx->nfchans; ch++) {
539
    for (ch = 1; ch <= ctx->nchans; ch++) {
561 540
        /* transform coefficients for individual channel */
562 541
        if (get_transform_coeffs_ch(ctx, ch, &m))
563 542
            return -1;
564 543
        /* tranform coefficients for coupling channels */
565 544
        if (ctx->chincpl[ch])  {
566 545
            if (!got_cplchan) {
567
                if (get_transform_coeffs_ch(ctx, -2, &m)) {
546
                if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
568 547
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
569 548
                    return -1;
570 549
                }
571 550
                uncouple_channels(ctx);
572 551
                got_cplchan = 1;
573 552
            }
574
            end = ctx->cplendmant;
553
            end = ctx->endmant[CPL_CH];
575 554
        } else {
576 555
            end = ctx->endmant[ch];
577 556
        }
578 557
        do
579
            ctx->transform_coeffs[ch + 1][end] = 0;
558
            ctx->transform_coeffs[ch][end] = 0;
580 559
        while(++end < 256);
581 560
    }
582
    if (ctx->lfeon) {
583
        if (get_transform_coeffs_ch(ctx, -1, &m))
584
                return -1;
585
        for (i = 7; i < 256; i++) {
586
            ctx->transform_coeffs[0][i] = 0;
587
        }
588
    }
589 561

  
590 562
    /* if any channel doesn't use dithering, zero appropriate coefficients */
591 563
    if(!ctx->dither_all)
......
604 576
    int end, bndend;
605 577
    float tmp0, tmp1;
606 578

  
607
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
579
    end = FFMIN(ctx->endmant[1], ctx->endmant[2]);
608 580

  
609 581
    for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
610 582
        if(ctx->rematflg[bnd]) {
......
664 636
static inline void do_imdct(AC3DecodeContext *ctx)
665 637
{
666 638
    int ch;
639
    int nchans;
667 640

  
668
    if (ctx->output_mode & AC3_OUTPUT_LFEON) {
669
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
670
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
671
        ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
672
                                     ctx->window, ctx->delay[0], 384, 256, 1);
673
        ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256,
674
                                     ctx->window, 256);
675
    }
676
    for (ch=1; ch<=ctx->nfchans; ch++) {
677
        if (ctx->blksw[ch-1]) {
641
    nchans = ctx->nfchans;
642
    if(ctx->output_mode & AC3_OUTPUT_LFEON)
643
        nchans++;
644

  
645
    for (ch=1; ch<=nchans; ch++) {
646
        if (ctx->blksw[ch]) {
678 647
            do_imdct_256(ctx, ch);
679 648
        } else {
680 649
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
681 650
                                          ctx->transform_coeffs[ch],
682 651
                                          ctx->tmp_imdct);
683 652
        }
684
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
685
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
686
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
653
        ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
654
                                     ctx->window, ctx->delay[ch-1], 384, 256, 1);
655
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
687 656
                                     ctx->window, 256);
688 657
    }
689 658
}
......
699 668
    int acmod = ctx->acmod;
700 669
    int i, bnd, seg, ch;
701 670
    GetBitContext *gb = &ctx->gb;
702
    int bit_alloc_flags = 0;
671
    uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
703 672

  
704
    for (ch = 0; ch < nfchans; ch++) /*block switch flag */
673
    memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
674

  
675
    for (ch = 1; ch <= nfchans; ch++) /*block switch flag */
705 676
        ctx->blksw[ch] = get_bits1(gb);
706 677

  
707 678
    ctx->dither_all = 1;
708
    for (ch = 0; ch < nfchans; ch++) { /* dithering flag */
679
    for (ch = 1; ch <= nfchans; ch++) { /* dithering flag */
709 680
        ctx->dithflag[ch] = get_bits1(gb);
710 681
        if(!ctx->dithflag[ch])
711 682
            ctx->dither_all = 0;
......
726 697
    }
727 698

  
728 699
    if (get_bits1(gb)) { /* coupling strategy */
700
        memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
729 701
        ctx->cplinu = get_bits1(gb);
730 702
        if (ctx->cplinu) { /* coupling in use */
731 703
            int cplbegf, cplendf;
732 704

  
733
            for (ch = 0; ch < nfchans; ch++)
705
            for (ch = 1; ch <= nfchans; ch++)
734 706
                ctx->chincpl[ch] = get_bits1(gb);
735 707

  
736 708
            if (acmod == AC3_ACMOD_STEREO)
......
745 717
            }
746 718

  
747 719
            ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
748
            ctx->cplstrtmant = cplbegf * 12 + 37;
749
            ctx->cplendmant = cplendf * 12 + 73;
720
            ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
721
            ctx->endmant[CPL_CH] = cplendf * 12 + 73;
750 722
            for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */
751 723
                if (get_bits1(gb)) {
752 724
                    ctx->cplbndstrc[bnd] = 1;
......
754 726
                }
755 727
            }
756 728
        } else {
757
            for (ch = 0; ch < nfchans; ch++)
729
            for (ch = 1; ch <= nfchans; ch++)
758 730
                ctx->chincpl[ch] = 0;
759 731
        }
760 732
    }
......
762 734
    if (ctx->cplinu) {
763 735
        int cplcoe = 0;
764 736

  
765
        for (ch = 0; ch < nfchans; ch++) {
737
        for (ch = 1; ch <= nfchans; ch++) {
766 738
            if (ctx->chincpl[ch]) {
767 739
                if (get_bits1(gb)) { /* coupling co-ordinates */
768 740
                    int mstrcplco, cplcoexp, cplcomant;
......
784 756
        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
785 757
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
786 758
                if (get_bits1(gb))
787
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
759
                    ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
788 760
            }
789 761
        }
790 762
    }
......
793 765
        ctx->rematstr = get_bits1(gb);
794 766
        if (ctx->rematstr) {
795 767
            ctx->nrematbnd = 4;
796
            if(ctx->cplinu && ctx->cplstrtmant <= 61)
797
                ctx->nrematbnd -= 1 + (ctx->cplstrtmant == 37);
768
            if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
769
                ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
798 770
            for(bnd=0; bnd<ctx->nrematbnd; bnd++)
799 771
                ctx->rematflg[bnd] = get_bits1(gb);
800 772
        }
801 773
    }
802 774

  
803
    ctx->cplexpstr = EXP_REUSE;
804
    ctx->lfeexpstr = EXP_REUSE;
805
    if (ctx->cplinu) /* coupling exponent strategy */
806
        ctx->cplexpstr = get_bits(gb, 2);
807
    for (ch = 0; ch < nfchans; ch++)  /* channel exponent strategy */
808
        ctx->chexpstr[ch] = get_bits(gb, 2);
809
    if (ctx->lfeon)  /* lfe exponent strategy */
810
        ctx->lfeexpstr = get_bits1(gb);
811

  
812
    for (ch = 0; ch < nfchans; ch++) { /* channel bandwidth code */
813
        if (ctx->chexpstr[ch] != EXP_REUSE) {
775
    ctx->expstr[CPL_CH] = EXP_REUSE;
776
    ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
777
    for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
778
        if(ch == ctx->lfe_ch)
779
            ctx->expstr[ch] = get_bits(gb, 1);
780
        else
781
            ctx->expstr[ch] = get_bits(gb, 2);
782
        if(ctx->expstr[ch] != EXP_REUSE)
783
            bit_alloc_stages[ch] = 3;
784
    }
785

  
786
    for (ch = 1; ch <= nfchans; ch++) { /* channel bandwidth code */
787
        ctx->startmant[ch] = 0;
788
        if (ctx->expstr[ch] != EXP_REUSE) {
789
            int prev = ctx->endmant[ch];
814 790
            if (ctx->chincpl[ch])
815
                ctx->endmant[ch] = ctx->cplstrtmant;
791
                ctx->endmant[ch] = ctx->startmant[CPL_CH];
816 792
            else {
817 793
                int chbwcod = get_bits(gb, 6);
818 794
                if (chbwcod > 60) {
......
821 797
                }
822 798
                ctx->endmant[ch] = chbwcod * 3 + 73;
823 799
            }
800
            if(blk > 0 && ctx->endmant[ch] != prev)
801
                memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
824 802
        }
825 803
    }
826

  
827
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
828
        int grpsize, ngrps, absexp;
829
        bit_alloc_flags = 64;
830
        grpsize = 3 << (ctx->cplexpstr - 1);
831
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / grpsize;
832
        absexp = get_bits(gb, 4) << 1;
833
        decode_exponents(gb, ctx->cplexpstr, ngrps, absexp, &ctx->dcplexps[ctx->cplstrtmant]);
834
    }
835

  
836
    for (ch = 0; ch < nfchans; ch++) { /* fbw channel exponents */
837
        if (ctx->chexpstr[ch] != EXP_REUSE) {
838
            int grpsize, ngrps, absexp;
839
            bit_alloc_flags |= 1 << ch;
840
            grpsize = 3 << (ctx->chexpstr[ch] - 1);
804
    ctx->startmant[ctx->lfe_ch] = 0;
805
    ctx->endmant[ctx->lfe_ch] = 7;
806

  
807
    for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
808
        if (ctx->expstr[ch] != EXP_REUSE) {
809
            int grpsize, ngrps;
810
            grpsize = 3 << (ctx->expstr[ch] - 1);
811
            if(ch == CPL_CH)
812
                ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;
813
            else if(ch == ctx->lfe_ch)
814
                ngrps = 2;
815
            else
841 816
            ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;
842
            absexp = ctx->dexps[ch][0] = get_bits(gb, 4);
843
            decode_exponents(gb, ctx->chexpstr[ch], ngrps, absexp, &ctx->dexps[ch][1]);
817
            ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
818
            decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0],
819
                             &ctx->dexps[ch][ctx->startmant[ch]+!!ch]);
820
            if(ch != CPL_CH && ch != ctx->lfe_ch)
844 821
            skip_bits(gb, 2); /* skip gainrng */
845 822
        }
846 823
    }
847 824

  
848
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
849
        int ngrps, absexp;
850
        bit_alloc_flags |= 32;
851
        ngrps = 2;
852
        absexp = ctx->dlfeexps[0] = get_bits(gb, 4);
853
        decode_exponents(gb, ctx->lfeexpstr, ngrps, absexp, &ctx->dlfeexps[1]);
854
    }
855

  
856 825
    if (get_bits1(gb)) { /* bit allocation information */
857
        bit_alloc_flags = 127;
858 826
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
859 827
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
860 828
        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
861 829
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
862 830
        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
831
        for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
832
            bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
833
        }
863 834
    }
864 835

  
865 836
    if (get_bits1(gb)) { /* snroffset */
866 837
        int csnr;
867
        bit_alloc_flags = 127;
868 838
        csnr = (get_bits(gb, 6) - 15) << 4;
869
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
870
            ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
871
            ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
872
        }
873
        for (ch = 0; ch < nfchans; ch++) { /* channel fine snr offset and fast gain code */
839
        for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */
874 840
            ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
875 841
            ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
876 842
        }
877
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
878
            ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
879
            ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
880
        }
843
        memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
881 844
    }
882 845

  
883 846
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
884
        bit_alloc_flags |= 64;
885 847
        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
886 848
        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
849
        bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
887 850
    }
888 851

  
889 852
    if (get_bits1(gb)) { /* delta bit allocation information */
890
        bit_alloc_flags = 127;
891

  
892
        if (ctx->cplinu) {
893
            ctx->cpldeltbae = get_bits(gb, 2);
894
            if (ctx->cpldeltbae == DBA_RESERVED) {
895
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
896
                return -1;
897
            }
898
        }
899

  
900
        for (ch = 0; ch < nfchans; ch++) {
853
        for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
901 854
            ctx->deltbae[ch] = get_bits(gb, 2);
902 855
            if (ctx->deltbae[ch] == DBA_RESERVED) {
903 856
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
904 857
                return -1;
905 858
            }
859
            bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
906 860
        }
907 861

  
908
        if (ctx->cplinu) {
909
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
910
                ctx->cpldeltnseg = get_bits(gb, 3);
911
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
912
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
913
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
914
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
915
                }
916
            }
917
        }
918

  
919
        for (ch = 0; ch < nfchans; ch++) {
862
        for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
920 863
            if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */
921 864
                ctx->deltnseg[ch] = get_bits(gb, 3);
922 865
                for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
......
927 870
            }
928 871
        }
929 872
    } else if(blk == 0) {
930
        if(ctx->cplinu)
931
            ctx->cpldeltbae = DBA_NONE;
932
        for(ch=0; ch<nfchans; ch++) {
873
        for(ch=0; ch<=ctx->nchans; ch++) {
933 874
            ctx->deltbae[ch] = DBA_NONE;
934 875
        }
935 876
    }
936 877

  
937
    if (bit_alloc_flags) {
938
        if (ctx->cplinu && (bit_alloc_flags & 64)) {
939
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
940
                                          ctx->dcplexps, ctx->cplstrtmant,
941
                                          ctx->cplendmant, ctx->cplsnroffst,
942
                                          ctx->cplfgain, 0,
943
                                          ctx->cpldeltbae, ctx->cpldeltnseg,
944
                                          ctx->cpldeltoffst, ctx->cpldeltlen,
945
                                          ctx->cpldeltba);
878
    for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
879
        if(bit_alloc_stages[ch] > 2) {
880
            /* Exponent mapping into PSD and PSD integration */
881
            ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
882
                                      ctx->startmant[ch], ctx->endmant[ch],
883
                                      ctx->psd[ch], ctx->bndpsd[ch]);
946 884
        }
947
        for (ch = 0; ch < nfchans; ch++) {
948
            if ((bit_alloc_flags >> ch) & 1) {
949
                ac3_parametric_bit_allocation(&ctx->bit_alloc_params,
950
                                              ctx->bap[ch], ctx->dexps[ch], 0,
951
                                              ctx->endmant[ch], ctx->snroffst[ch],
952
                                              ctx->fgain[ch], 0, ctx->deltbae[ch],
953
                                              ctx->deltnseg[ch], ctx->deltoffst[ch],
954
                                              ctx->deltlen[ch], ctx->deltba[ch]);
955
            }
885
        if(bit_alloc_stages[ch] > 1) {
886
            /* Compute excitation function, Compute masking curve, and
887
               Apply delta bit allocation */
888
            ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],
889
                                       ctx->startmant[ch], ctx->endmant[ch],
890
                                       ctx->fgain[ch], (ch == ctx->lfe_ch),
891
                                       ctx->deltbae[ch], ctx->deltnseg[ch],
892
                                       ctx->deltoffst[ch], ctx->deltlen[ch],
893
                                       ctx->deltba[ch], ctx->mask[ch]);
956 894
        }
957
        if (ctx->lfeon && (bit_alloc_flags & 32)) {
958
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
959
                                          ctx->dlfeexps, 0, 7, ctx->lfesnroffst,
960
                                          ctx->lfefgain, 1,
961
                                          DBA_NONE, 0, NULL, NULL, NULL);
895
        if(bit_alloc_stages[ch] > 0) {
896
            /* Compute bit allocation */
897
            ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
898
                                      ctx->startmant[ch], ctx->endmant[ch],
899
                                      ctx->snroffst[ch],
900
                                      ctx->bit_alloc_params.floor,
901
                                      ctx->bap[ch]);
962 902
        }
963 903
    }
964 904

  
......
980 920
        do_rematrixing(ctx);
981 921

  
982 922
    /* apply scaling to coefficients (headroom, dynrng) */
983
    if(ctx->lfeon) {
984
        for(i=0; i<7; i++) {
985
            ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
986
        }
987
    }
988
    for(ch=1; ch<=ctx->nfchans; ch++) {
923
    for(ch=1; ch<=ctx->nchans; ch++) {
989 924
        float gain = 2.0f;
990 925
        if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
991 926
            gain *= ctx->dynrng2;
992 927
        } else {
993 928
            gain *= ctx->dynrng;
994 929
        }
995
        for(i=0; i<ctx->endmant[ch-1]; i++) {
930
        for(i=0; i<ctx->endmant[ch]; i++) {
996 931
            ctx->transform_coeffs[ch][i] *= gain;
997 932
        }
998 933
    }
......
1024 959
{
1025 960
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1026 961
    int16_t *out_samples = (int16_t *)data;
1027
    int i, blk, ch, start;
962
    int i, blk, ch;
1028 963
    int32_t *int_ptr[6];
1029 964

  
1030 965
    for (ch = 0; ch < 6; ch++)
......
1062 997
            *data_size = 0;
1063 998
            return ctx->frame_size;
1064 999
        }
1065
        start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
1066 1000
        for (i = 0; i < 256; i++)
1067
            for (ch = start; ch <= ctx->nfchans; ch++)
1001
            for (ch = 0; ch < ctx->out_channels; ch++)
1068 1002
                *(out_samples++) = convert(int_ptr[ch][i]);
1069 1003
    }
1070 1004
    *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);

Also available in: Unified diff