Revision 7bfd22f2 libavcodec/ac3dec.c

View differences:

libavcodec/ac3dec.c
38 38
#include "dsputil.h"
39 39
#include "random.h"
40 40

  
41
static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
42

  
43 41
/* table for exponent to scale_factor mapping
44 42
 * scale_factor[i] = 2 ^ -(i + 15)
45 43
 */
......
80 78

  
81 79
#define BLOCK_SIZE    256
82 80

  
83
/* Output and input configurations. */
84
#define AC3_OUTPUT_UNMODIFIED   0x01
85
#define AC3_OUTPUT_MONO         0x02
86
#define AC3_OUTPUT_STEREO       0x04
87
#define AC3_OUTPUT_DOLBY        0x08
88
#define AC3_OUTPUT_LFEON        0x10
81
#define AC3_OUTPUT_LFEON  8
89 82

  
90 83
typedef struct {
91 84
    int acmod;
......
132 125
    int      nchans;            //number of total channels
133 126
    int      nfchans;           //number of full-bandwidth channels
134 127
    int      lfeon;             //lfe channel in use
128
    int      output_mode;       ///< output channel configuration
129
    int      out_channels;      ///< number of output channels
135 130

  
136 131
    float    dynrng;            //dynamic range gain
137 132
    float    dynrng2;           //dynamic range gain for 1+1 mode
138
    float    chcoeffs[6];       //normalized channel coefficients
139 133
    float    cplco[5][18];      //coupling coordinates
140 134
    int      ncplbnd;           //number of coupling bands
141 135
    int      ncplsubnd;         //number of coupling sub bands
......
151 145
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
152 146
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
153 147

  
154
    int      blkoutput;         //output configuration for block
155

  
156 148
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
157 149

  
158 150
    /* For IMDCT. */
......
337 329
    ctx->nchans                       = hdr.channels;
338 330
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
339 331
    ctx->frame_size                   = hdr.frame_size;
340
    ctx->blkoutput                    = nfchans_tbl[ctx->acmod];
332

  
333
    /* set default output to all source channels */
334
    ctx->out_channels = ctx->nchans;
335
    ctx->output_mode = ctx->acmod;
341 336
    if(ctx->lfeon)
342
        ctx->blkoutput |= AC3_OUTPUT_LFEON;
337
        ctx->output_mode |= AC3_OUTPUT_LFEON;
343 338

  
344 339
    /* skip over portion of header which has already been read */
345 340
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
......
457 452
        }
458 453
        cplbndstrc >>= 1;
459 454
        for (ch = 0; ch < ctx->nfchans; ch++)
460
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
455
            cplcos[ch] = ctx->cplco[ch][bnd];
461 456
        bnd++;
462 457

  
463 458
        while (start < end) {
......
535 530
    uint8_t *exps;
536 531
    uint8_t *bap;
537 532
    float *coeffs;
538
    float factors[25];
539

  
540
    for (i = 0; i < 25; i++)
541
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
542 533

  
543 534
    if (ch_index != -1) { /* fbw channels */
544 535
        dithflag = ctx->dithflag[ch_index];
......
564 555
                    continue;
565 556
                }
566 557
                else {
567
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
558
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[i]];
568 559
                    coeffs[i] *= LEVEL_MINUS_3DB;
569 560
                    continue;
570 561
                }
......
577 568
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
578 569
                    m->l3ptr = 0;
579 570
                }
580
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
571
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[i]];
581 572
                continue;
582 573

  
583 574
            case 2:
......
588 579
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
589 580
                    m->l5ptr = 0;
590 581
                }
591
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
582
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[i]];
592 583
                continue;
593 584

  
594 585
            case 3:
595
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
586
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[i]];
596 587
                continue;
597 588

  
598 589
            case 4:
......
602 593
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
603 594
                    m->l11ptr = 0;
604 595
                }
605
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
596
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[i]];
606 597
                continue;
607 598

  
608 599
            case 5:
609
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
600
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[i]];
610 601
                continue;
611 602

  
612 603
            default:
613
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
604
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[i]];
614 605
                continue;
615 606
        }
616 607
    }
......
701 692
    }
702 693
}
703 694

  
704
/* This function sets the normalized channel coefficients.
705
 * Transform coefficients are multipllied by the channel
706
 * coefficients to get normalized transform coefficients.
707
 */
708
static void get_downmix_coeffs(AC3DecodeContext *ctx)
709
{
710
    int from = ctx->acmod;
711
    int to = ctx->blkoutput;
712
    float clev = clevs[ctx->cmixlev];
713
    float slev = slevs[ctx->surmixlev];
714
    float nf = 1.0; //normalization factor for downmix coeffs
715
    int i;
716

  
717
    if (!ctx->acmod) {
718
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
719
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
720
    } else {
721
        for (i = 0; i < ctx->nfchans; i++)
722
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
723
    }
724

  
725
    if (to == AC3_OUTPUT_UNMODIFIED)
726
        return;
727

  
728
    switch (from) {
729
        case AC3_ACMOD_DUALMONO:
730
            switch (to) {
731
                case AC3_OUTPUT_MONO:
732
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
733
                    nf = 0.5;
734
                    ctx->chcoeffs[0] *= nf;
735
                    ctx->chcoeffs[1] *= nf;
736
                    break;
737
            }
738
            break;
739
        case AC3_ACMOD_MONO:
740
            switch (to) {
741
                case AC3_OUTPUT_STEREO:
742
                    nf = LEVEL_MINUS_3DB;
743
                    ctx->chcoeffs[0] *= nf;
744
                    break;
745
            }
746
            break;
747
        case AC3_ACMOD_STEREO:
748
            switch (to) {
749
                case AC3_OUTPUT_MONO:
750
                    nf = LEVEL_MINUS_3DB;
751
                    ctx->chcoeffs[0] *= nf;
752
                    ctx->chcoeffs[1] *= nf;
753
                    break;
754
            }
755
            break;
756
        case AC3_ACMOD_3F:
757
            switch (to) {
758
                case AC3_OUTPUT_MONO:
759
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
760
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
761
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
762
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
763
                    break;
764
                case AC3_OUTPUT_STEREO:
765
                    nf = 1.0 / (1.0 + clev);
766
                    ctx->chcoeffs[0] *= nf;
767
                    ctx->chcoeffs[2] *= nf;
768
                    ctx->chcoeffs[1] *= (nf * clev);
769
                    break;
770
            }
771
            break;
772
        case AC3_ACMOD_2F1R:
773
            switch (to) {
774
                case AC3_OUTPUT_MONO:
775
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
776
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
777
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
778
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
779
                    break;
780
                case AC3_OUTPUT_STEREO:
781
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
782
                    ctx->chcoeffs[0] *= nf;
783
                    ctx->chcoeffs[1] *= nf;
784
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
785
                    break;
786
                case AC3_OUTPUT_DOLBY:
787
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
788
                    ctx->chcoeffs[0] *= nf;
789
                    ctx->chcoeffs[1] *= nf;
790
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
791
                    break;
792
            }
793
            break;
794
        case AC3_ACMOD_3F1R:
795
            switch (to) {
796
                case AC3_OUTPUT_MONO:
797
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
798
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
799
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
800
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
801
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
802
                    break;
803
                case AC3_OUTPUT_STEREO:
804
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
805
                    ctx->chcoeffs[0] *= nf;
806
                    ctx->chcoeffs[2] *= nf;
807
                    ctx->chcoeffs[1] *= (nf * clev);
808
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
809
                    break;
810
                case AC3_OUTPUT_DOLBY:
811
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
812
                    ctx->chcoeffs[0] *= nf;
813
                    ctx->chcoeffs[1] *= nf;
814
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
815
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
816
                    break;
817
            }
818
            break;
819
        case AC3_ACMOD_2F2R:
820
            switch (to) {
821
                case AC3_OUTPUT_MONO:
822
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
823
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
824
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
825
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
826
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
827
                    break;
828
                case AC3_OUTPUT_STEREO:
829
                    nf = 1.0 / (1.0 + slev);
830
                    ctx->chcoeffs[0] *= nf;
831
                    ctx->chcoeffs[1] *= nf;
832
                    ctx->chcoeffs[2] *= (nf * slev);
833
                    ctx->chcoeffs[3] *= (nf * slev);
834
                    break;
835
                case AC3_OUTPUT_DOLBY:
836
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
837
                    ctx->chcoeffs[0] *= nf;
838
                    ctx->chcoeffs[1] *= nf;
839
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
840
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
841
                    break;
842
            }
843
            break;
844
        case AC3_ACMOD_3F2R:
845
            switch (to) {
846
                case AC3_OUTPUT_MONO:
847
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
848
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
849
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
850
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
851
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
852
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
853
                    break;
854
                case AC3_OUTPUT_STEREO:
855
                    nf = 1.0 / (1.0 + clev + slev);
856
                    ctx->chcoeffs[0] *= nf;
857
                    ctx->chcoeffs[2] *= nf;
858
                    ctx->chcoeffs[1] *= (nf * clev);
859
                    ctx->chcoeffs[3] *= (nf * slev);
860
                    ctx->chcoeffs[4] *= (nf * slev);
861
                    break;
862
                case AC3_OUTPUT_DOLBY:
863
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
864
                    ctx->chcoeffs[0] *= nf;
865
                    ctx->chcoeffs[1] *= nf;
866
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
867
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
868
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
869
                    break;
870
            }
871
            break;
872
    }
873
}
874

  
875
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
876
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
877
{
878
    int i;
879
    float (*output)[BLOCK_SIZE] = ctx->output;
880

  
881
    for (i = 0; i < 256; i++)
882
        output[1][i] += output[2][i];
883
    memset(output[2], 0, sizeof(output[2]));
884
}
885

  
886
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
887
{
888
    int i;
889
    float tmp;
890
    float (*output)[BLOCK_SIZE] = ctx->output;
891

  
892
    for (i = 0; i < 256; i++) {
893
        tmp = output[1][i] + output[2][i];
894
        output[1][i] = output[2][i] = tmp;
895
    }
896
}
897

  
898
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
899
{
900
    int i;
901
    float (*output)[BLOCK_SIZE] = ctx->output;
902

  
903
    for (i = 0; i < 256; i++)
904
        output[2][i] = output[1][i];
905
}
906

  
907
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
908
{
909
    int i;
910
    float (*output)[BLOCK_SIZE] = ctx->output;
911

  
912
    for (i = 0; i < 256; i++)
913
        output[1][i] += output[2][i];
914
    memset(output[2], 0, sizeof(output[2]));
915
}
916

  
917
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
918
{
919
    int i;
920
    float (*output)[BLOCK_SIZE] = ctx->output;
921

  
922
    for (i = 0; i < 256; i++)
923
        output[1][i] += (output[2][i] + output[3][i]);
924
    memset(output[2], 0, sizeof(output[2]));
925
    memset(output[3], 0, sizeof(output[3]));
926
}
927

  
928
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
929
{
930
    int i;
931
    float (*output)[BLOCK_SIZE] = ctx->output;
932

  
933
    for (i = 0; i < 256; i++) {
934
        output[1][i] += output[2][i];
935
        output[2][i] += output[3][i];
936
    }
937
    memset(output[3], 0, sizeof(output[3]));
938
}
939

  
940
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
941
{
942
    int i;
943
    float (*output)[BLOCK_SIZE] = ctx->output;
944

  
945
    for (i = 0; i < 256; i++)
946
        output[1][i] += (output[2][i] + output[3][i]);
947
    memset(output[2], 0, sizeof(output[2]));
948
    memset(output[3], 0, sizeof(output[3]));
949

  
950
}
951

  
952
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
953
{
954
    int i;
955
    float (*output)[BLOCK_SIZE] = ctx->output;
956

  
957
    for (i = 0; i < 256; i++) {
958
        output[1][i] += output[2][i];
959
        output[2][i] += output[3][i];
960
    }
961
    memset(output[3], 0, sizeof(output[3]));
962
}
963

  
964
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
965
{
966
    int i;
967
    float (*output)[BLOCK_SIZE] = ctx->output;
968

  
969
    for (i = 0; i < 256; i++) {
970
        output[1][i] -= output[3][i];
971
        output[2][i] += output[3][i];
972
    }
973
    memset(output[3], 0, sizeof(output[3]));
974
}
975

  
976
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
977
{
978
    int i;
979
    float (*output)[BLOCK_SIZE] = ctx->output;
980

  
981
    for (i = 0; i < 256; i++)
982
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
983
    memset(output[2], 0, sizeof(output[2]));
984
    memset(output[3], 0, sizeof(output[3]));
985
    memset(output[4], 0, sizeof(output[4]));
986
}
987

  
988
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
989
{
990
    int i;
991
    float (*output)[BLOCK_SIZE] = ctx->output;
992

  
993
    for (i = 0; i < 256; i++) {
994
        output[1][i] += (output[2][i] + output[4][i]);
995
        output[2][i] += (output[3][i] + output[4][i]);
996
    }
997
    memset(output[3], 0, sizeof(output[3]));
998
    memset(output[4], 0, sizeof(output[4]));
999
}
1000

  
1001
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1002
{
1003
    int i;
1004
    float (*output)[BLOCK_SIZE] = ctx->output;
1005

  
1006
    for (i = 0; i < 256; i++) {
1007
        output[1][i] += (output[2][i] - output[4][i]);
1008
        output[2][i] += (output[3][i] + output[4][i]);
1009
    }
1010
    memset(output[3], 0, sizeof(output[3]));
1011
    memset(output[4], 0, sizeof(output[4]));
1012
}
1013

  
1014
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1015
{
1016
    int i;
1017
    float (*output)[BLOCK_SIZE] = ctx->output;
1018

  
1019
    for (i = 0; i < 256; i++)
1020
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1021
    memset(output[2], 0, sizeof(output[2]));
1022
    memset(output[3], 0, sizeof(output[3]));
1023
    memset(output[4], 0, sizeof(output[4]));
1024
}
1025

  
1026
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1027
{
1028
    int i;
1029
    float (*output)[BLOCK_SIZE] = ctx->output;
1030

  
1031
    for (i = 0; i < 256; i++) {
1032
        output[1][i] += output[3][i];
1033
        output[2][i] += output[4][i];
1034
    }
1035
    memset(output[3], 0, sizeof(output[3]));
1036
    memset(output[4], 0, sizeof(output[4]));
1037
}
1038

  
1039
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1040
{
1041
    int i;
1042
    float (*output)[BLOCK_SIZE] = ctx->output;
1043

  
1044
    for (i = 0; i < 256; i++) {
1045
        output[1][i] -= output[3][i];
1046
        output[2][i] += output[4][i];
1047
    }
1048
    memset(output[3], 0, sizeof(output[3]));
1049
    memset(output[4], 0, sizeof(output[4]));
1050
}
1051

  
1052
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1053
{
1054
    int i;
1055
    float (*output)[BLOCK_SIZE] = ctx->output;
1056

  
1057
    for (i = 0; i < 256; i++)
1058
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1059
    memset(output[2], 0, sizeof(output[2]));
1060
    memset(output[3], 0, sizeof(output[3]));
1061
    memset(output[4], 0, sizeof(output[4]));
1062
    memset(output[5], 0, sizeof(output[5]));
1063
}
1064

  
1065
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1066
{
1067
    int i;
1068
    float (*output)[BLOCK_SIZE] = ctx->output;
1069

  
1070
    for (i = 0; i < 256; i++) {
1071
        output[1][i] += (output[2][i] + output[4][i]);
1072
        output[2][i] += (output[3][i] + output[5][i]);
1073
    }
1074
    memset(output[3], 0, sizeof(output[3]));
1075
    memset(output[4], 0, sizeof(output[4]));
1076
    memset(output[5], 0, sizeof(output[5]));
1077
}
1078

  
1079
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1080
{
1081
    int i;
1082
    float (*output)[BLOCK_SIZE] = ctx->output;
1083

  
1084
    for (i = 0; i < 256; i++) {
1085
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1086
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1087
    }
1088
    memset(output[3], 0, sizeof(output[3]));
1089
    memset(output[4], 0, sizeof(output[4]));
1090
    memset(output[5], 0, sizeof(output[5]));
1091
}
1092
/*********** END DOWNMIX FUNCTIONS ***********/
1093

  
1094
/* Downmix the output.
1095
 * This function downmixes the output when the number of input
1096
 * channels is not equal to the number of output channels requested.
1097
 */
1098
static void do_downmix(AC3DecodeContext *ctx)
1099
{
1100
    int from = ctx->acmod;
1101
    int to = ctx->blkoutput;
1102

  
1103
    if (to == AC3_OUTPUT_UNMODIFIED)
1104
        return;
1105

  
1106
    switch (from) {
1107
        case AC3_ACMOD_DUALMONO:
1108
            switch (to) {
1109
                case AC3_OUTPUT_MONO:
1110
                    mix_dualmono_to_mono(ctx);
1111
                    break;
1112
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1113
                    mix_dualmono_to_stereo(ctx);
1114
                    break;
1115
            }
1116
            break;
1117
        case AC3_ACMOD_MONO:
1118
            switch (to) {
1119
                case AC3_OUTPUT_STEREO:
1120
                    upmix_mono_to_stereo(ctx);
1121
                    break;
1122
            }
1123
            break;
1124
        case AC3_ACMOD_STEREO:
1125
            switch (to) {
1126
                case AC3_OUTPUT_MONO:
1127
                    mix_stereo_to_mono(ctx);
1128
                    break;
1129
            }
1130
            break;
1131
        case AC3_ACMOD_3F:
1132
            switch (to) {
1133
                case AC3_OUTPUT_MONO:
1134
                    mix_3f_to_mono(ctx);
1135
                    break;
1136
                case AC3_OUTPUT_STEREO:
1137
                    mix_3f_to_stereo(ctx);
1138
                    break;
1139
            }
1140
            break;
1141
        case AC3_ACMOD_2F1R:
1142
            switch (to) {
1143
                case AC3_OUTPUT_MONO:
1144
                    mix_2f_1r_to_mono(ctx);
1145
                    break;
1146
                case AC3_OUTPUT_STEREO:
1147
                    mix_2f_1r_to_stereo(ctx);
1148
                    break;
1149
                case AC3_OUTPUT_DOLBY:
1150
                    mix_2f_1r_to_dolby(ctx);
1151
                    break;
1152
            }
1153
            break;
1154
        case AC3_ACMOD_3F1R:
1155
            switch (to) {
1156
                case AC3_OUTPUT_MONO:
1157
                    mix_3f_1r_to_mono(ctx);
1158
                    break;
1159
                case AC3_OUTPUT_STEREO:
1160
                    mix_3f_1r_to_stereo(ctx);
1161
                    break;
1162
                case AC3_OUTPUT_DOLBY:
1163
                    mix_3f_1r_to_dolby(ctx);
1164
                    break;
1165
            }
1166
            break;
1167
        case AC3_ACMOD_2F2R:
1168
            switch (to) {
1169
                case AC3_OUTPUT_MONO:
1170
                    mix_2f_2r_to_mono(ctx);
1171
                    break;
1172
                case AC3_OUTPUT_STEREO:
1173
                    mix_2f_2r_to_stereo(ctx);
1174
                    break;
1175
                case AC3_OUTPUT_DOLBY:
1176
                    mix_2f_2r_to_dolby(ctx);
1177
                    break;
1178
            }
1179
            break;
1180
        case AC3_ACMOD_3F2R:
1181
            switch (to) {
1182
                case AC3_OUTPUT_MONO:
1183
                    mix_3f_2r_to_mono(ctx);
1184
                    break;
1185
                case AC3_OUTPUT_STEREO:
1186
                    mix_3f_2r_to_stereo(ctx);
1187
                    break;
1188
                case AC3_OUTPUT_DOLBY:
1189
                    mix_3f_2r_to_dolby(ctx);
1190
                    break;
1191
            }
1192
            break;
1193
    }
1194
}
1195

  
1196 695
/* This function performs the imdct on 256 sample transform
1197 696
 * coefficients.
1198 697
 */
......
1239 738
{
1240 739
    int ch;
1241 740

  
1242
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
741
    if (ctx->output_mode & AC3_OUTPUT_LFEON) {
1243 742
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1244 743
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
744
        ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
745
                                     ctx->window, ctx->delay[0], 384, 256, 1);
746
        ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256,
747
                                     ctx->window, 256);
1245 748
    }
1246 749
    for (ch=1; ch<=ctx->nfchans; ch++) {
1247 750
        if (ctx->blksw[ch-1])
......
1267 770
{
1268 771
    int nfchans = ctx->nfchans;
1269 772
    int acmod = ctx->acmod;
1270
    int i, bnd, rbnd, seg, grpsize;
773
    int i, bnd, rbnd, seg, grpsize, ch;
1271 774
    GetBitContext *gb = &ctx->gb;
1272 775
    int bit_alloc_flags = 0;
1273 776
    int8_t *dexps;
......
1296 799
        }
1297 800
    }
1298 801

  
1299
    get_downmix_coeffs(ctx);
1300

  
1301 802
    if (get_bits1(gb)) { /* coupling strategy */
1302 803
        ctx->cplinu = get_bits1(gb);
1303 804
        ctx->cplbndstrc = 0;
......
1537 1038
    if(ctx->acmod == AC3_ACMOD_STEREO)
1538 1039
        do_rematrixing(ctx);
1539 1040

  
1540
    do_downmix(ctx);
1041
    /* apply scaling to coefficients (headroom, dynrng) */
1042
    if(ctx->lfeon) {
1043
        for(i=0; i<7; i++) {
1044
            ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
1045
        }
1046
    }
1047
    for(ch=1; ch<=ctx->nfchans; ch++) {
1048
        float gain = 2.0f;
1049
        if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
1050
            gain *= ctx->dynrng2;
1051
        } else {
1052
            gain *= ctx->dynrng;
1053
        }
1054
        for(i=0; i<ctx->endmant[ch-1]; i++) {
1055
            ctx->transform_coeffs[ch][i] *= gain;
1056
        }
1057
    }
1541 1058

  
1542 1059
    do_imdct(ctx);
1543 1060

  
......
1585 1102
    avctx->sample_rate = ctx->sampling_rate;
1586 1103
    avctx->bit_rate = ctx->bit_rate;
1587 1104

  
1105
    /* channel config */
1588 1106
    if (avctx->channels == 0) {
1589
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1590
        if (ctx->lfeon)
1591
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1592
        avctx->channels = ctx->nfchans + ctx->lfeon;
1107
        avctx->channels = ctx->out_channels;
1593 1108
    }
1594
    else if (avctx->channels == 1)
1595
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1596
    else if (avctx->channels == 2) {
1597
        if (ctx->dsurmod == 0x02)
1598
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1599
        else
1600
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1601
    }
1602
    else {
1603
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1604
            av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n",avctx->channels, ctx->nfchans + ctx->lfeon);
1605
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1606
        if (ctx->lfeon)
1607
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1608
        avctx->channels = ctx->nfchans + ctx->lfeon;
1109
    if(avctx->channels != ctx->out_channels) {
1110
        av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n",
1111
               avctx->channels);
1112
        return -1;
1609 1113
    }
1610 1114

  
1611 1115
    //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
......
1617 1121
            *data_size = 0;
1618 1122
            return ctx->frame_size;
1619 1123
        }
1620
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1124
        start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
1621 1125
        for (k = 0; k < BLOCK_SIZE; k++)
1622
            for (j = start; j <= avctx->channels; j++)
1126
            for (j = start; j <= ctx->nfchans; j++)
1623 1127
                *(out_samples++) = convert(int_ptr[j][k]);
1624 1128
    }
1625 1129
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);

Also available in: Unified diff