comparison ac3dec.c @ 6025:a165936f0d9e libavcodec

downmix before the IMDCT if no block switching is used
author jbr
date Sun, 16 Dec 2007 04:25:50 +0000
parents 90b36c43c4ef
children 9d3f52380cb3
comparison
equal deleted inserted replaced
6024:151508a72dd7 6025:a165936f0d9e
748 } 748 }
749 749
750 /** 750 /**
751 * Downmix the output to mono or stereo. 751 * Downmix the output to mono or stereo.
752 */ 752 */
753 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int fbw_channels, 753 static void ac3_downmix(float samples[][256], int fbw_channels,
754 int output_mode, float coef[AC3_MAX_CHANNELS][2]) 754 int output_mode, float coef[AC3_MAX_CHANNELS][2],
755 int ch_offset)
755 { 756 {
756 int i, j; 757 int i, j;
757 float v0, v1, s0, s1; 758 float v0, v1, s0, s1;
758 759
759 for(i=0; i<256; i++) { 760 for(i=0; i<256; i++) {
760 v0 = v1 = s0 = s1 = 0.0f; 761 v0 = v1 = s0 = s1 = 0.0f;
761 for(j=0; j<fbw_channels; j++) { 762 for(j=ch_offset; j<fbw_channels+ch_offset; j++) {
762 v0 += samples[j][i] * coef[j][0]; 763 v0 += samples[j][i] * coef[j-ch_offset][0];
763 v1 += samples[j][i] * coef[j][1]; 764 v1 += samples[j][i] * coef[j-ch_offset][1];
764 s0 += coef[j][0]; 765 s0 += coef[j-ch_offset][0];
765 s1 += coef[j][1]; 766 s1 += coef[j-ch_offset][1];
766 } 767 }
767 v0 /= s0; 768 v0 /= s0;
768 v1 /= s1; 769 v1 /= s1;
769 if(output_mode == AC3_CHMODE_MONO) { 770 if(output_mode == AC3_CHMODE_MONO) {
770 samples[0][i] = (v0 + v1) * LEVEL_MINUS_3DB; 771 samples[ch_offset][i] = (v0 + v1) * LEVEL_MINUS_3DB;
771 } else if(output_mode == AC3_CHMODE_STEREO) { 772 } else if(output_mode == AC3_CHMODE_STEREO) {
772 samples[0][i] = v0; 773 samples[ch_offset][i] = v0;
773 samples[1][i] = v1; 774 samples[ch_offset+1][i] = v1;
774 } 775 }
775 } 776 }
776 } 777 }
777 778
778 /** 779 /**
783 int fbw_channels = ctx->fbw_channels; 784 int fbw_channels = ctx->fbw_channels;
784 int channel_mode = ctx->channel_mode; 785 int channel_mode = ctx->channel_mode;
785 int i, bnd, seg, ch; 786 int i, bnd, seg, ch;
786 GetBitContext *gb = &ctx->gb; 787 GetBitContext *gb = &ctx->gb;
787 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS]; 788 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
789 int any_block_switching = 0;
790 int num_channels_bak, fbw_channels_bak;
788 791
789 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS); 792 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
790 793
791 /* block switch flags */ 794 /* block switch flags */
792 for (ch = 1; ch <= fbw_channels; ch++) 795 for (ch = 1; ch <= fbw_channels; ch++) {
793 ctx->block_switch[ch] = get_bits1(gb); 796 ctx->block_switch[ch] = get_bits1(gb);
797 any_block_switching |= ctx->block_switch[ch];
798 }
794 799
795 /* dithering flags */ 800 /* dithering flags */
796 ctx->dither_all = 1; 801 ctx->dither_all = 1;
797 for (ch = 1; ch <= fbw_channels; ch++) { 802 for (ch = 1; ch <= fbw_channels; ch++) {
798 ctx->dither_flag[ch] = get_bits1(gb); 803 ctx->dither_flag[ch] = get_bits1(gb);
1060 for(i=0; i<ctx->end_freq[ch]; i++) { 1065 for(i=0; i<ctx->end_freq[ch]; i++) {
1061 ctx->transform_coeffs[ch][i] *= gain; 1066 ctx->transform_coeffs[ch][i] *= gain;
1062 } 1067 }
1063 } 1068 }
1064 1069
1070 /* if no block switching is used, downmixing can be done before IMDCT */
1071 num_channels_bak = ctx->channels;
1072 fbw_channels_bak = ctx->fbw_channels;
1073 if(!any_block_switching) {
1074 if(ctx->channels != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
1075 ctx->fbw_channels == ctx->out_channels)) {
1076 ac3_downmix(ctx->transform_coeffs, ctx->fbw_channels,
1077 ctx->output_mode, ctx->downmix_coeffs, 1);
1078 ctx->channels = ctx->out_channels;
1079 ctx->fbw_channels = ctx->channels - (ctx->output_mode & AC3_OUTPUT_LFEON);
1080 }
1081 }
1082
1065 do_imdct(ctx); 1083 do_imdct(ctx);
1066 1084
1067 /* downmix output if needed */ 1085 /* downmix output now if it wasn't done before IMDCT */
1068 if(ctx->channels != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) && 1086 if(ctx->channels != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
1069 ctx->fbw_channels == ctx->out_channels)) { 1087 ctx->fbw_channels == ctx->out_channels)) {
1070 ac3_downmix(ctx->output, ctx->fbw_channels, ctx->output_mode, 1088 ac3_downmix(ctx->output, ctx->fbw_channels, ctx->output_mode,
1071 ctx->downmix_coeffs); 1089 ctx->downmix_coeffs, 0);
1072 } 1090 }
1073 1091
1074 /* convert float to 16-bit integer */ 1092 /* convert float to 16-bit integer */
1075 for(ch=0; ch<ctx->out_channels; ch++) { 1093 for(ch=0; ch<ctx->out_channels; ch++) {
1076 for(i=0; i<256; i++) { 1094 for(i=0; i<256; i++) {
1077 ctx->output[ch][i] += ctx->add_bias; 1095 ctx->output[ch][i] += ctx->add_bias;
1078 } 1096 }
1079 ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256); 1097 ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256);
1080 } 1098 }
1099
1100 ctx->channels = num_channels_bak;
1101 ctx->fbw_channels = fbw_channels_bak;
1081 1102
1082 return 0; 1103 return 0;
1083 } 1104 }
1084 1105
1085 /** 1106 /**