comparison ac3dec.c @ 11559:444f4b594fdb libavcodec

Add spectral extension to the E-AC-3 decoder. Original patch by Justin, updated and resubmitted by Christophe Gisquet, christophe D gisquet A gmail
author cehoyos
date Tue, 30 Mar 2010 22:09:14 +0000
parents e26ea20d293a
children 8a4984c5cacc
comparison
equal deleted inserted replaced
11558:44c5c540722c 11559:444f4b594fdb
813 } 813 }
814 } while(i--); 814 } while(i--);
815 815
816 /* spectral extension strategy */ 816 /* spectral extension strategy */
817 if (s->eac3 && (!blk || get_bits1(gbc))) { 817 if (s->eac3 && (!blk || get_bits1(gbc))) {
818 if (get_bits1(gbc)) { 818 s->spx_in_use = get_bits1(gbc);
819 av_log_missing_feature(s->avctx, "Spectral extension", 1); 819 if (s->spx_in_use) {
820 return -1; 820 int dst_start_freq, dst_end_freq, src_start_freq,
821 } 821 start_subband, end_subband;
822 /* TODO: parse spectral extension strategy info */ 822
823 } 823 /* determine which channels use spx */
824 824 if (s->channel_mode == AC3_CHMODE_MONO) {
825 /* TODO: spectral extension coordinates */ 825 s->channel_uses_spx[1] = 1;
826 } else {
827 for (ch = 1; ch <= fbw_channels; ch++)
828 s->channel_uses_spx[ch] = get_bits1(gbc);
829 }
830
831 /* get the frequency bins of the spx copy region and the spx start
832 and end subbands */
833 dst_start_freq = get_bits(gbc, 2);
834 start_subband = get_bits(gbc, 3) + 2;
835 if (start_subband > 7)
836 start_subband += start_subband - 7;
837 end_subband = get_bits(gbc, 3) + 5;
838 if (end_subband > 7)
839 end_subband += end_subband - 7;
840 dst_start_freq = dst_start_freq * 12 + 25;
841 src_start_freq = start_subband * 12 + 25;
842 dst_end_freq = end_subband * 12 + 25;
843
844 /* check validity of spx ranges */
845 if (start_subband >= end_subband) {
846 av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
847 "range (%d >= %d)\n", start_subband, end_subband);
848 return -1;
849 }
850 if (dst_start_freq >= src_start_freq) {
851 av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension "
852 "copy start bin (%d >= %d)\n", dst_start_freq, src_start_freq);
853 return -1;
854 }
855
856 s->spx_dst_start_freq = dst_start_freq;
857 s->spx_src_start_freq = src_start_freq;
858 s->spx_dst_end_freq = dst_end_freq;
859
860 decode_band_structure(gbc, blk, s->eac3, 0,
861 start_subband, end_subband,
862 ff_eac3_default_spx_band_struct,
863 &s->num_spx_bands,
864 s->spx_band_sizes);
865 } else {
866 for (ch = 1; ch <= fbw_channels; ch++) {
867 s->channel_uses_spx[ch] = 0;
868 s->first_spx_coords[ch] = 1;
869 }
870 }
871 }
872
873 /* spectral extension coordinates */
874 if (s->spx_in_use) {
875 for (ch = 1; ch <= fbw_channels; ch++) {
876 if (s->channel_uses_spx[ch]) {
877 if (s->first_spx_coords[ch] || get_bits1(gbc)) {
878 float spx_blend;
879 int bin, master_spx_coord;
880
881 s->first_spx_coords[ch] = 0;
882 spx_blend = get_bits(gbc, 5) * (1.0f/32);
883 master_spx_coord = get_bits(gbc, 2) * 3;
884
885 bin = s->spx_src_start_freq;
886 for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
887 int bandsize;
888 int spx_coord_exp, spx_coord_mant;
889 float nratio, sblend, nblend, spx_coord;
890
891 /* calculate blending factors */
892 bandsize = s->spx_band_sizes[bnd];
893 nratio = ((float)((bin + (bandsize >> 1))) / s->spx_dst_end_freq) - spx_blend;
894 nratio = av_clipf(nratio, 0.0f, 1.0f);
895 nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3) to give unity variance
896 sblend = sqrtf(1.0f - nratio);
897 bin += bandsize;
898
899 /* decode spx coordinates */
900 spx_coord_exp = get_bits(gbc, 4);
901 spx_coord_mant = get_bits(gbc, 2);
902 if (spx_coord_exp == 15) spx_coord_mant <<= 1;
903 else spx_coord_mant += 4;
904 spx_coord_mant <<= (25 - spx_coord_exp - master_spx_coord);
905 spx_coord = spx_coord_mant * (1.0f/(1<<23));
906
907 /* multiply noise and signal blending factors by spx coordinate */
908 s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
909 s->spx_signal_blend[ch][bnd] = sblend * spx_coord;
910 }
911 }
912 } else {
913 s->first_spx_coords[ch] = 1;
914 }
915 }
916 }
826 917
827 /* coupling strategy */ 918 /* coupling strategy */
828 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) { 919 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
829 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 920 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
830 if (!s->eac3) 921 if (!s->eac3)
857 /* phase flags in use */ 948 /* phase flags in use */
858 if (channel_mode == AC3_CHMODE_STEREO) 949 if (channel_mode == AC3_CHMODE_STEREO)
859 s->phase_flags_in_use = get_bits1(gbc); 950 s->phase_flags_in_use = get_bits1(gbc);
860 951
861 /* coupling frequency range */ 952 /* coupling frequency range */
862 /* TODO: modify coupling end freq if spectral extension is used */
863 cpl_start_subband = get_bits(gbc, 4); 953 cpl_start_subband = get_bits(gbc, 4);
864 cpl_end_subband = get_bits(gbc, 4) + 3; 954 cpl_end_subband = s->spx_in_use ? (s->spx_src_start_freq - 37) / 12 :
955 get_bits(gbc, 4) + 3;
865 if (cpl_start_subband >= cpl_end_subband) { 956 if (cpl_start_subband >= cpl_end_subband) {
866 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n", 957 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d >= %d)\n",
867 cpl_start_subband, cpl_end_subband); 958 cpl_start_subband, cpl_end_subband);
868 return -1; 959 return -1;
869 } 960 }
932 1023
933 /* stereo rematrixing strategy and band structure */ 1024 /* stereo rematrixing strategy and band structure */
934 if (channel_mode == AC3_CHMODE_STEREO) { 1025 if (channel_mode == AC3_CHMODE_STEREO) {
935 if ((s->eac3 && !blk) || get_bits1(gbc)) { 1026 if ((s->eac3 && !blk) || get_bits1(gbc)) {
936 s->num_rematrixing_bands = 4; 1027 s->num_rematrixing_bands = 4;
937 if(cpl_in_use && s->start_freq[CPL_CH] <= 61) 1028 if (cpl_in_use && s->start_freq[CPL_CH] <= 61) {
938 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37); 1029 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37);
1030 } else if (s->spx_in_use && s->spx_src_start_freq <= 61) {
1031 s->num_rematrixing_bands--;
1032 }
939 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) 1033 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
940 s->rematrixing_flags[bnd] = get_bits1(gbc); 1034 s->rematrixing_flags[bnd] = get_bits1(gbc);
941 } else if (!blk) { 1035 } else if (!blk) {
942 av_log(s->avctx, AV_LOG_WARNING, "Warning: new rematrixing strategy not present in block 0\n"); 1036 av_log(s->avctx, AV_LOG_WARNING, "Warning: new rematrixing strategy not present in block 0\n");
943 s->num_rematrixing_bands = 0; 1037 s->num_rematrixing_bands = 0;
958 if (s->exp_strategy[blk][ch] != EXP_REUSE) { 1052 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
959 int group_size; 1053 int group_size;
960 int prev = s->end_freq[ch]; 1054 int prev = s->end_freq[ch];
961 if (s->channel_in_cpl[ch]) 1055 if (s->channel_in_cpl[ch])
962 s->end_freq[ch] = s->start_freq[CPL_CH]; 1056 s->end_freq[ch] = s->start_freq[CPL_CH];
1057 else if (s->channel_uses_spx[ch])
1058 s->end_freq[ch] = s->spx_src_start_freq;
963 else { 1059 else {
964 int bandwidth_code = get_bits(gbc, 6); 1060 int bandwidth_code = get_bits(gbc, 6);
965 if (bandwidth_code > 60) { 1061 if (bandwidth_code > 60) {
966 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code); 1062 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code);
967 return -1; 1063 return -1;
1154 this also uncouples channels if coupling is in use. */ 1250 this also uncouples channels if coupling is in use. */
1155 decode_transform_coeffs(s, blk); 1251 decode_transform_coeffs(s, blk);
1156 1252
1157 /* TODO: generate enhanced coupling coordinates and uncouple */ 1253 /* TODO: generate enhanced coupling coordinates and uncouple */
1158 1254
1159 /* TODO: apply spectral extension */
1160
1161 /* recover coefficients if rematrixing is in use */ 1255 /* recover coefficients if rematrixing is in use */
1162 if(s->channel_mode == AC3_CHMODE_STEREO) 1256 if(s->channel_mode == AC3_CHMODE_STEREO)
1163 do_rematrixing(s); 1257 do_rematrixing(s);
1164 1258
1165 /* apply scaling to coefficients (headroom, dynrng) */ 1259 /* apply scaling to coefficients (headroom, dynrng) */
1169 gain *= s->dynamic_range[2-ch]; 1263 gain *= s->dynamic_range[2-ch];
1170 } else { 1264 } else {
1171 gain *= s->dynamic_range[0]; 1265 gain *= s->dynamic_range[0];
1172 } 1266 }
1173 s->dsp.int32_to_float_fmul_scalar(s->transform_coeffs[ch], s->fixed_coeffs[ch], gain, 256); 1267 s->dsp.int32_to_float_fmul_scalar(s->transform_coeffs[ch], s->fixed_coeffs[ch], gain, 256);
1268 }
1269
1270 /* apply spectral extension to high frequency bins */
1271 if (s->spx_in_use) {
1272 ff_eac3_apply_spectral_extension(s);
1174 } 1273 }
1175 1274
1176 /* downmix and MDCT. order depends on whether block switching is used for 1275 /* downmix and MDCT. order depends on whether block switching is used for
1177 any channel in this block. this is because coefficients for the long 1276 any channel in this block. this is because coefficients for the long
1178 and short transforms cannot be mixed. */ 1277 and short transforms cannot be mixed. */