comparison ac3dec.c @ 5480:930bca6dd95f libavcodec

add support for downmixing to stereo or mono
author jbr
date Sat, 04 Aug 2007 22:32:17 +0000
parents 943c732c905d
children 15c6ea63cb62
comparison
equal deleted inserted replaced
5479:943c732c905d 5480:930bca6dd95f
74 74
75 /* Adjustmens in dB gain */ 75 /* Adjustmens in dB gain */
76 #define LEVEL_MINUS_3DB 0.7071067811865476 76 #define LEVEL_MINUS_3DB 0.7071067811865476
77 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605 77 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
78 #define LEVEL_MINUS_6DB 0.5000000000000000 78 #define LEVEL_MINUS_6DB 0.5000000000000000
79 #define LEVEL_PLUS_3DB 1.4142135623730951 79 #define LEVEL_MINUS_9DB 0.3535533905932738
80 #define LEVEL_PLUS_6DB 2.0000000000000000
81 #define LEVEL_ZERO 0.0000000000000000 80 #define LEVEL_ZERO 0.0000000000000000
82 81 #define LEVEL_ONE 1.0000000000000000
83 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, 82
84 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB }; 83 static const float gain_levels[6] = {
85 84 LEVEL_ZERO,
86 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB }; 85 LEVEL_ONE,
86 LEVEL_MINUS_3DB,
87 LEVEL_MINUS_4POINT5DB,
88 LEVEL_MINUS_6DB,
89 LEVEL_MINUS_9DB
90 };
91
92 /**
93 * Table for center mix levels
94 * reference: Section 5.4.2.4 cmixlev
95 */
96 static const uint8_t clevs[4] = { 2, 3, 4, 3 };
97
98 /**
99 * Table for surround mix levels
100 * reference: Section 5.4.2.5 surmixlev
101 */
102 static const uint8_t slevs[4] = { 2, 4, 0, 4 };
103
104 /**
105 * Table for default stereo downmixing coefficients
106 * reference: Section 7.8.2 Downmixing Into Two Channels
107 */
108 static const uint8_t ac3_default_coeffs[8][5][2] = {
109 { { 1, 0 }, { 0, 1 }, },
110 { { 2, 2 }, },
111 { { 1, 0 }, { 0, 1 }, },
112 { { 1, 0 }, { 3, 3 }, { 0, 1 }, },
113 { { 1, 0 }, { 0, 1 }, { 4, 4 }, },
114 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 5, 5 }, },
115 { { 1, 0 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
116 { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
117 };
87 118
88 /* override ac3.h to include coupling channel */ 119 /* override ac3.h to include coupling channel */
89 #undef AC3_MAX_CHANNELS 120 #undef AC3_MAX_CHANNELS
90 #define AC3_MAX_CHANNELS 7 121 #define AC3_MAX_CHANNELS 7
91 #define CPL_CH 0 122 #define CPL_CH 0
92 123
93 #define AC3_OUTPUT_LFEON 8 124 #define AC3_OUTPUT_LFEON 8
94 125
95 typedef struct { 126 typedef struct {
96 int acmod; 127 int acmod;
97 int cmixlev;
98 int surmixlev;
99 int dsurmod; 128 int dsurmod;
100 129
101 int blksw[AC3_MAX_CHANNELS]; 130 int blksw[AC3_MAX_CHANNELS];
102 int dithflag[AC3_MAX_CHANNELS]; 131 int dithflag[AC3_MAX_CHANNELS];
103 int dither_all; 132 int dither_all;
127 int lfeon; //lfe channel in use 156 int lfeon; //lfe channel in use
128 int lfe_ch; ///< index of LFE channel 157 int lfe_ch; ///< index of LFE channel
129 int output_mode; ///< output channel configuration 158 int output_mode; ///< output channel configuration
130 int out_channels; ///< number of output channels 159 int out_channels; ///< number of output channels
131 160
161 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients
132 float dynrng; //dynamic range gain 162 float dynrng; //dynamic range gain
133 float dynrng2; //dynamic range gain for 1+1 mode 163 float dynrng2; //dynamic range gain for 1+1 mode
134 float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates 164 float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates
135 int ncplbnd; //number of coupling bands 165 int ncplbnd; //number of coupling bands
136 int ncplsubnd; //number of coupling sub bands 166 int ncplsubnd; //number of coupling sub bands
284 */ 314 */
285 static int ac3_parse_header(AC3DecodeContext *ctx) 315 static int ac3_parse_header(AC3DecodeContext *ctx)
286 { 316 {
287 AC3HeaderInfo hdr; 317 AC3HeaderInfo hdr;
288 GetBitContext *gb = &ctx->gb; 318 GetBitContext *gb = &ctx->gb;
319 float cmixlev, surmixlev;
289 int err, i; 320 int err, i;
290 321
291 err = ff_ac3_parse_header(gb->buffer, &hdr); 322 err = ff_ac3_parse_header(gb->buffer, &hdr);
292 if(err) 323 if(err)
293 return err; 324 return err;
294 325
295 /* get decoding parameters from header info */ 326 /* get decoding parameters from header info */
296 ctx->bit_alloc_params.fscod = hdr.fscod; 327 ctx->bit_alloc_params.fscod = hdr.fscod;
297 ctx->acmod = hdr.acmod; 328 ctx->acmod = hdr.acmod;
298 ctx->cmixlev = hdr.cmixlev; 329 cmixlev = gain_levels[clevs[hdr.cmixlev]];
299 ctx->surmixlev = hdr.surmixlev; 330 surmixlev = gain_levels[slevs[hdr.surmixlev]];
300 ctx->dsurmod = hdr.dsurmod; 331 ctx->dsurmod = hdr.dsurmod;
301 ctx->lfeon = hdr.lfeon; 332 ctx->lfeon = hdr.lfeon;
302 ctx->bit_alloc_params.halfratecod = hdr.halfratecod; 333 ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
303 ctx->sampling_rate = hdr.sample_rate; 334 ctx->sampling_rate = hdr.sample_rate;
304 ctx->bit_rate = hdr.bit_rate; 335 ctx->bit_rate = hdr.bit_rate;
351 if (get_bits1(gb)) { 382 if (get_bits1(gb)) {
352 i = get_bits(gb, 6); //additional bsi length 383 i = get_bits(gb, 6); //additional bsi length
353 do { 384 do {
354 skip_bits(gb, 8); 385 skip_bits(gb, 8);
355 } while(i--); 386 } while(i--);
387 }
388
389 /* set stereo downmixing coefficients
390 reference: Section 7.8.2 Downmixing Into Two Channels */
391 for(i=0; i<ctx->nfchans; i++) {
392 ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->acmod][i][0]];
393 ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->acmod][i][1]];
394 }
395 if(ctx->acmod > 1 && ctx->acmod & 1) {
396 ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = cmixlev;
397 }
398 if(ctx->acmod == AC3_ACMOD_2F1R || ctx->acmod == AC3_ACMOD_3F1R) {
399 int nf = ctx->acmod - 2;
400 ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf][1] = surmixlev * LEVEL_MINUS_3DB;
401 }
402 if(ctx->acmod == AC3_ACMOD_2F2R || ctx->acmod == AC3_ACMOD_3F2R) {
403 int nf = ctx->acmod - 4;
404 ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf+1][1] = surmixlev;
356 } 405 }
357 406
358 return 0; 407 return 0;
359 } 408 }
360 409
660 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 709 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
661 ctx->transform_coeffs[ch], 710 ctx->transform_coeffs[ch],
662 ctx->tmp_imdct); 711 ctx->tmp_imdct);
663 } 712 }
664 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output, 713 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
665 ctx->window, ctx->delay[ch-1], ctx->add_bias, 256, 1); 714 ctx->window, ctx->delay[ch-1], 0, 256, 1);
666 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256, 715 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
667 ctx->window, 256); 716 ctx->window, 256);
717 }
718 }
719
720 /**
721 * Downmixes the output to stereo.
722 */
723 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans,
724 int output_mode, float coef[AC3_MAX_CHANNELS][2])
725 {
726 int i, j;
727 float v0, v1, s0, s1;
728
729 for(i=0; i<256; i++) {
730 v0 = v1 = s0 = s1 = 0.0f;
731 for(j=0; j<nfchans; j++) {
732 v0 += samples[j][i] * coef[j][0];
733 v1 += samples[j][i] * coef[j][1];
734 s0 += coef[j][0];
735 s1 += coef[j][1];
736 }
737 v0 /= s0;
738 v1 /= s1;
739 if(output_mode == AC3_ACMOD_MONO) {
740 samples[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;
741 } else if(output_mode == AC3_ACMOD_STEREO) {
742 samples[0][i] = v0;
743 samples[1][i] = v1;
744 }
668 } 745 }
669 } 746 }
670 747
671 /* Parse the audio block from ac3 bitstream. 748 /* Parse the audio block from ac3 bitstream.
672 * This function extract the audio block from the ac3 bitstream 749 * This function extract the audio block from the ac3 bitstream
943 } 1020 }
944 } 1021 }
945 1022
946 do_imdct(ctx); 1023 do_imdct(ctx);
947 1024
1025 /* downmix output if needed */
1026 if(ctx->nchans != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
1027 ctx->nfchans == ctx->out_channels)) {
1028 ac3_downmix(ctx->output, ctx->nfchans, ctx->output_mode,
1029 ctx->downmix_coeffs);
1030 }
1031
948 /* convert float to 16-bit integer */ 1032 /* convert float to 16-bit integer */
949 for(ch=0; ch<ctx->out_channels; ch++) { 1033 for(ch=0; ch<ctx->out_channels; ch++) {
1034 for(i=0; i<256; i++) {
1035 ctx->output[ch][i] += ctx->add_bias;
1036 }
950 ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256); 1037 ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256);
951 } 1038 }
952 1039
953 return 0; 1040 return 0;
954 } 1041 }
979 1066
980 avctx->sample_rate = ctx->sampling_rate; 1067 avctx->sample_rate = ctx->sampling_rate;
981 avctx->bit_rate = ctx->bit_rate; 1068 avctx->bit_rate = ctx->bit_rate;
982 1069
983 /* channel config */ 1070 /* channel config */
1071 ctx->out_channels = ctx->nchans;
984 if (avctx->channels == 0) { 1072 if (avctx->channels == 0) {
985 avctx->channels = ctx->out_channels; 1073 avctx->channels = ctx->out_channels;
986 } 1074 } else if(ctx->out_channels < avctx->channels) {
987 if(avctx->channels != ctx->out_channels) { 1075 av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n",
988 av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n", 1076 ctx->out_channels, avctx->channels);
989 avctx->channels);
990 return -1; 1077 return -1;
991 } 1078 }
1079 if(avctx->channels == 2) {
1080 ctx->output_mode = AC3_ACMOD_STEREO;
1081 } else if(avctx->channels == 1) {
1082 ctx->output_mode = AC3_ACMOD_MONO;
1083 } else if(avctx->channels != ctx->out_channels) {
1084 av_log(avctx, AV_LOG_ERROR, "Cannot downmix AC3 from %d to %d channels.\n",
1085 ctx->out_channels, avctx->channels);
1086 return -1;
1087 }
1088 ctx->out_channels = avctx->channels;
992 1089
993 //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); 1090 //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);
994 1091
995 //Parse the Audio Blocks. 1092 //Parse the Audio Blocks.
996 for (blk = 0; blk < NB_BLOCKS; blk++) { 1093 for (blk = 0; blk < NB_BLOCKS; blk++) {