comparison ac3dec.c @ 5412:3f6a6dda6b2d libavcodec

remove broken downmixing. will add new implementation later.
author jbr
date Sat, 28 Jul 2007 19:33:19 +0000
parents c001b06c0ac5
children 28c20633db55
comparison
equal deleted inserted replaced
5411:362aec4ef932 5412:3f6a6dda6b2d
36 #include "ac3_parser.h" 36 #include "ac3_parser.h"
37 #include "bitstream.h" 37 #include "bitstream.h"
38 #include "dsputil.h" 38 #include "dsputil.h"
39 #include "random.h" 39 #include "random.h"
40 40
41 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
42
43 /* table for exponent to scale_factor mapping 41 /* table for exponent to scale_factor mapping
44 * scale_factor[i] = 2 ^ -(i + 15) 42 * scale_factor[i] = 2 ^ -(i + 15)
45 */ 43 */
46 static float scale_factors[25]; 44 static float scale_factors[25];
47 45
78 76
79 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB }; 77 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
80 78
81 #define BLOCK_SIZE 256 79 #define BLOCK_SIZE 256
82 80
83 /* Output and input configurations. */ 81 #define AC3_OUTPUT_LFEON 8
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
89 82
90 typedef struct { 83 typedef struct {
91 int acmod; 84 int acmod;
92 int cmixlev; 85 int cmixlev;
93 int surmixlev; 86 int surmixlev;
130 int frame_size; 123 int frame_size;
131 124
132 int nchans; //number of total channels 125 int nchans; //number of total channels
133 int nfchans; //number of full-bandwidth channels 126 int nfchans; //number of full-bandwidth channels
134 int lfeon; //lfe channel in use 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 float dynrng; //dynamic range gain 131 float dynrng; //dynamic range gain
137 float dynrng2; //dynamic range gain for 1+1 mode 132 float dynrng2; //dynamic range gain for 1+1 mode
138 float chcoeffs[6]; //normalized channel coefficients
139 float cplco[5][18]; //coupling coordinates 133 float cplco[5][18]; //coupling coordinates
140 int ncplbnd; //number of coupling bands 134 int ncplbnd; //number of coupling bands
141 int ncplsubnd; //number of coupling sub bands 135 int ncplsubnd; //number of coupling sub bands
142 int cplstrtmant; //coupling start mantissa 136 int cplstrtmant; //coupling start mantissa
143 int cplendmant; //coupling end mantissa 137 int cplendmant; //coupling end mantissa
148 int8_t dexps[5][256]; //decoded fbw channel exponents 142 int8_t dexps[5][256]; //decoded fbw channel exponents
149 int8_t dlfeexps[256]; //decoded lfe channel exponents 143 int8_t dlfeexps[256]; //decoded lfe channel exponents
150 uint8_t cplbap[256]; //coupling bit allocation pointers 144 uint8_t cplbap[256]; //coupling bit allocation pointers
151 uint8_t bap[5][256]; //fbw channel bit allocation pointers 145 uint8_t bap[5][256]; //fbw channel bit allocation pointers
152 uint8_t lfebap[256]; //lfe channel bit allocation pointers 146 uint8_t lfebap[256]; //lfe channel bit allocation pointers
153
154 int blkoutput; //output configuration for block
155 147
156 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients 148 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
157 149
158 /* For IMDCT. */ 150 /* For IMDCT. */
159 MDCTContext imdct_512; //for 512 sample imdct transform 151 MDCTContext imdct_512; //for 512 sample imdct transform
335 ctx->sampling_rate = hdr.sample_rate; 327 ctx->sampling_rate = hdr.sample_rate;
336 ctx->bit_rate = hdr.bit_rate; 328 ctx->bit_rate = hdr.bit_rate;
337 ctx->nchans = hdr.channels; 329 ctx->nchans = hdr.channels;
338 ctx->nfchans = ctx->nchans - ctx->lfeon; 330 ctx->nfchans = ctx->nchans - ctx->lfeon;
339 ctx->frame_size = hdr.frame_size; 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 if(ctx->lfeon) 336 if(ctx->lfeon)
342 ctx->blkoutput |= AC3_OUTPUT_LFEON; 337 ctx->output_mode |= AC3_OUTPUT_LFEON;
343 338
344 /* skip over portion of header which has already been read */ 339 /* skip over portion of header which has already been read */
345 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16); 340 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
346 skip_bits(gb, 16); // skip crc1 341 skip_bits(gb, 16); // skip crc1
347 skip_bits(gb, 8); // skip fscod and frmsizecod 342 skip_bits(gb, 8); // skip fscod and frmsizecod
455 end += 12; 450 end += 12;
456 cplbndstrc >>= 1; 451 cplbndstrc >>= 1;
457 } 452 }
458 cplbndstrc >>= 1; 453 cplbndstrc >>= 1;
459 for (ch = 0; ch < ctx->nfchans; ch++) 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 bnd++; 456 bnd++;
462 457
463 while (start < end) { 458 while (start < end) {
464 tbap = bap[start]; 459 tbap = bap[start];
465 switch(tbap) { 460 switch(tbap) {
533 GetBitContext *gb = &ctx->gb; 528 GetBitContext *gb = &ctx->gb;
534 int i, gcode, tbap, dithflag, end; 529 int i, gcode, tbap, dithflag, end;
535 uint8_t *exps; 530 uint8_t *exps;
536 uint8_t *bap; 531 uint8_t *bap;
537 float *coeffs; 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 if (ch_index != -1) { /* fbw channels */ 534 if (ch_index != -1) { /* fbw channels */
544 dithflag = ctx->dithflag[ch_index]; 535 dithflag = ctx->dithflag[ch_index];
545 exps = ctx->dexps[ch_index]; 536 exps = ctx->dexps[ch_index];
546 bap = ctx->bap[ch_index]; 537 bap = ctx->bap[ch_index];
562 if (!dithflag) { 553 if (!dithflag) {
563 coeffs[i] = 0; 554 coeffs[i] = 0;
564 continue; 555 continue;
565 } 556 }
566 else { 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 coeffs[i] *= LEVEL_MINUS_3DB; 559 coeffs[i] *= LEVEL_MINUS_3DB;
569 continue; 560 continue;
570 } 561 }
571 562
572 case 1: 563 case 1:
575 m->l3_quantizers[0] = l3_quantizers_1[gcode]; 566 m->l3_quantizers[0] = l3_quantizers_1[gcode];
576 m->l3_quantizers[1] = l3_quantizers_2[gcode]; 567 m->l3_quantizers[1] = l3_quantizers_2[gcode];
577 m->l3_quantizers[2] = l3_quantizers_3[gcode]; 568 m->l3_quantizers[2] = l3_quantizers_3[gcode];
578 m->l3ptr = 0; 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 continue; 572 continue;
582 573
583 case 2: 574 case 2:
584 if (m->l5ptr > 2) { 575 if (m->l5ptr > 2) {
585 gcode = get_bits(gb, 7); 576 gcode = get_bits(gb, 7);
586 m->l5_quantizers[0] = l5_quantizers_1[gcode]; 577 m->l5_quantizers[0] = l5_quantizers_1[gcode];
587 m->l5_quantizers[1] = l5_quantizers_2[gcode]; 578 m->l5_quantizers[1] = l5_quantizers_2[gcode];
588 m->l5_quantizers[2] = l5_quantizers_3[gcode]; 579 m->l5_quantizers[2] = l5_quantizers_3[gcode];
589 m->l5ptr = 0; 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 continue; 583 continue;
593 584
594 case 3: 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 continue; 587 continue;
597 588
598 case 4: 589 case 4:
599 if (m->l11ptr > 1) { 590 if (m->l11ptr > 1) {
600 gcode = get_bits(gb, 7); 591 gcode = get_bits(gb, 7);
601 m->l11_quantizers[0] = l11_quantizers_1[gcode]; 592 m->l11_quantizers[0] = l11_quantizers_1[gcode];
602 m->l11_quantizers[1] = l11_quantizers_2[gcode]; 593 m->l11_quantizers[1] = l11_quantizers_2[gcode];
603 m->l11ptr = 0; 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 continue; 597 continue;
607 598
608 case 5: 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 continue; 601 continue;
611 602
612 default: 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 continue; 605 continue;
615 } 606 }
616 } 607 }
617 608
618 return 0; 609 return 0;
699 if (ctx->rematflg[3]) 690 if (ctx->rematflg[3])
700 do_rematrixing1(ctx, bnd4, end); 691 do_rematrixing1(ctx, bnd4, end);
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 /* This function performs the imdct on 256 sample transform 695 /* This function performs the imdct on 256 sample transform
1197 * coefficients. 696 * coefficients.
1198 */ 697 */
1199 static void do_imdct_256(AC3DecodeContext *ctx, int chindex) 698 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1200 { 699 {
1237 /* IMDCT Transform. */ 736 /* IMDCT Transform. */
1238 static inline void do_imdct(AC3DecodeContext *ctx) 737 static inline void do_imdct(AC3DecodeContext *ctx)
1239 { 738 {
1240 int ch; 739 int ch;
1241 740
1242 if (ctx->blkoutput & AC3_OUTPUT_LFEON) { 741 if (ctx->output_mode & AC3_OUTPUT_LFEON) {
1243 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 742 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1244 ctx->transform_coeffs[0], ctx->tmp_imdct); 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 for (ch=1; ch<=ctx->nfchans; ch++) { 749 for (ch=1; ch<=ctx->nfchans; ch++) {
1247 if (ctx->blksw[ch-1]) 750 if (ctx->blksw[ch-1])
1248 do_imdct_256(ctx, ch); 751 do_imdct_256(ctx, ch);
1249 else 752 else
1265 */ 768 */
1266 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk) 769 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
1267 { 770 {
1268 int nfchans = ctx->nfchans; 771 int nfchans = ctx->nfchans;
1269 int acmod = ctx->acmod; 772 int acmod = ctx->acmod;
1270 int i, bnd, rbnd, seg, grpsize; 773 int i, bnd, rbnd, seg, grpsize, ch;
1271 GetBitContext *gb = &ctx->gb; 774 GetBitContext *gb = &ctx->gb;
1272 int bit_alloc_flags = 0; 775 int bit_alloc_flags = 0;
1273 int8_t *dexps; 776 int8_t *dexps;
1274 int mstrcplco, cplcoexp, cplcomant; 777 int mstrcplco, cplcoexp, cplcomant;
1275 int dynrng, chbwcod, ngrps, cplabsexp, skipl; 778 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1293 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 796 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1294 } else if(blk == 0) { 797 } else if(blk == 0) {
1295 ctx->dynrng2 = 1.0; 798 ctx->dynrng2 = 1.0;
1296 } 799 }
1297 } 800 }
1298
1299 get_downmix_coeffs(ctx);
1300 801
1301 if (get_bits1(gb)) { /* coupling strategy */ 802 if (get_bits1(gb)) { /* coupling strategy */
1302 ctx->cplinu = get_bits1(gb); 803 ctx->cplinu = get_bits1(gb);
1303 ctx->cplbndstrc = 0; 804 ctx->cplbndstrc = 0;
1304 if (ctx->cplinu) { /* coupling in use */ 805 if (ctx->cplinu) { /* coupling in use */
1535 1036
1536 /* recover coefficients if rematrixing is in use */ 1037 /* recover coefficients if rematrixing is in use */
1537 if(ctx->acmod == AC3_ACMOD_STEREO) 1038 if(ctx->acmod == AC3_ACMOD_STEREO)
1538 do_rematrixing(ctx); 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 do_imdct(ctx); 1059 do_imdct(ctx);
1543 1060
1544 return 0; 1061 return 0;
1545 } 1062 }
1583 } 1100 }
1584 1101
1585 avctx->sample_rate = ctx->sampling_rate; 1102 avctx->sample_rate = ctx->sampling_rate;
1586 avctx->bit_rate = ctx->bit_rate; 1103 avctx->bit_rate = ctx->bit_rate;
1587 1104
1105 /* channel config */
1588 if (avctx->channels == 0) { 1106 if (avctx->channels == 0) {
1589 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED; 1107 avctx->channels = ctx->out_channels;
1590 if (ctx->lfeon) 1108 }
1591 ctx->blkoutput |= AC3_OUTPUT_LFEON; 1109 if(avctx->channels != ctx->out_channels) {
1592 avctx->channels = ctx->nfchans + ctx->lfeon; 1110 av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n",
1593 } 1111 avctx->channels);
1594 else if (avctx->channels == 1) 1112 return -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;
1609 } 1113 }
1610 1114
1611 //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); 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);
1612 1116
1613 //Parse the Audio Blocks. 1117 //Parse the Audio Blocks.
1615 if (ac3_parse_audio_block(ctx, i)) { 1119 if (ac3_parse_audio_block(ctx, i)) {
1616 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); 1120 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1617 *data_size = 0; 1121 *data_size = 0;
1618 return ctx->frame_size; 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 for (k = 0; k < BLOCK_SIZE; k++) 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 *(out_samples++) = convert(int_ptr[j][k]); 1127 *(out_samples++) = convert(int_ptr[j][k]);
1624 } 1128 }
1625 *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t); 1129 *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1626 return ctx->frame_size; 1130 return ctx->frame_size;
1627 } 1131 }