Mercurial > libavcodec.hg
comparison ac3dec.c @ 8136:3085502c4f33 libavcodec
add support for spectral extension
author | jbr |
---|---|
date | Thu, 13 Nov 2008 03:18:13 +0000 |
parents | c82f8f5657ff |
children | c6993d9ed77c |
comparison
equal
deleted
inserted
replaced
8135:c82f8f5657ff | 8136:3085502c4f33 |
---|---|
816 } | 816 } |
817 } while(i--); | 817 } while(i--); |
818 | 818 |
819 /* spectral extension strategy */ | 819 /* spectral extension strategy */ |
820 if (s->eac3 && (!blk || get_bits1(gbc))) { | 820 if (s->eac3 && (!blk || get_bits1(gbc))) { |
821 if (get_bits1(gbc)) { | 821 s->spx_in_use[blk] = get_bits1(gbc); |
822 av_log_missing_feature(s->avctx, "Spectral extension", 1); | 822 if (s->spx_in_use[blk]) { |
823 return -1; | 823 int begf, endf; |
824 } | 824 int spx_end_subband; |
825 /* TODO: parse spectral extension strategy info */ | 825 |
826 } | 826 /* determine which channels use spx */ |
827 | 827 if (s->channel_mode == AC3_CHMODE_MONO) { |
828 /* TODO: spectral extension coordinates */ | 828 s->channel_in_spx[1] = 1; |
829 s->spx_coords_exist[1] = 0; | |
830 } else { | |
831 for (ch = 1; ch <= fbw_channels; ch++) { | |
832 s->channel_in_spx[ch] = get_bits1(gbc); | |
833 s->spx_coords_exist[ch] = 0; | |
834 } | |
835 } | |
836 | |
837 s->spx_copy_start_freq = get_bits(gbc, 2) * 12 + 25; | |
838 begf = get_bits(gbc, 3); | |
839 endf = get_bits(gbc, 3); | |
840 s->spx_start_subband = begf < 6 ? begf+2 : 2*begf-3; | |
841 spx_end_subband = endf < 4 ? endf+5 : 2*endf+3; | |
842 s->num_spx_subbands = spx_end_subband - s->spx_start_subband; | |
843 s->spx_start_freq = s->spx_start_subband * 12 + 25; | |
844 s->spx_end_freq = spx_end_subband * 12 + 25; | |
845 | |
846 decode_band_structure(gbc, blk, s->eac3, 0, | |
847 s->spx_start_subband, spx_end_subband, | |
848 ff_eac3_default_spx_band_struct, | |
849 s->spx_band_struct, NULL, &s->num_spx_bands, | |
850 s->spx_band_sizes); | |
851 } else { | |
852 for (ch = 1; ch <= fbw_channels; ch++) { | |
853 s->channel_in_spx[ch] = 0; | |
854 s->first_spx_coords[ch] = 1; | |
855 } | |
856 } | |
857 } else { | |
858 s->spx_in_use[blk] = blk ? s->spx_in_use[blk-1] : 0; | |
859 } | |
860 | |
861 /* spectral extension coordinates */ | |
862 if (s->spx_in_use[blk]) { | |
863 for (ch = 1; ch <= fbw_channels; ch++) { | |
864 if (s->channel_in_spx[ch]) { | |
865 if (s->first_spx_coords[ch] || get_bits1(gbc)) { | |
866 int bin, spx_blend; | |
867 int master_spx_coord; | |
868 s->first_spx_coords[ch] = 0; | |
869 s->spx_coords_exist[ch] = 1; | |
870 spx_blend = get_bits(gbc, 5) << 18; | |
871 master_spx_coord = get_bits(gbc, 2) * 3; | |
872 bin = s->spx_start_freq; | |
873 for (bnd = 0; bnd < s->num_spx_bands; bnd++) { | |
874 int spx_coord_exp, spx_coord_mant; | |
875 | |
876 /* calculate blending factors */ | |
877 int bandsize = s->spx_band_sizes[bnd]; | |
878 int nratio = (((bin + (bandsize >> 1)) << 23) / s->spx_end_freq) - spx_blend; | |
879 nratio = av_clip(nratio, 0, INT24_MAX); | |
880 s->spx_noise_blend [ch][bnd] = ff_sqrt(( nratio) << 8) * M_SQRT_POW2_15; | |
881 s->spx_signal_blend[ch][bnd] = ff_sqrt((INT24_MAX - nratio) << 8) * M_SQRT_POW2_15; | |
882 bin += bandsize; | |
883 | |
884 /* decode spx coordinates */ | |
885 spx_coord_exp = get_bits(gbc, 4); | |
886 spx_coord_mant = get_bits(gbc, 2); | |
887 if (spx_coord_exp == 15) | |
888 s->spx_coords[ch][bnd] = spx_coord_mant << 26; | |
889 else | |
890 s->spx_coords[ch][bnd] = (spx_coord_mant + 4) << 25; | |
891 s->spx_coords[ch][bnd] >>= (spx_coord_exp + master_spx_coord); | |
892 } | |
893 } else { | |
894 s->spx_coords_exist[ch] = 0; | |
895 } | |
896 } else { | |
897 s->first_spx_coords[ch] = 1; | |
898 } | |
899 } | |
900 } | |
829 | 901 |
830 /* coupling strategy */ | 902 /* coupling strategy */ |
831 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) { | 903 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) { |
832 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); | 904 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); |
833 if (!s->eac3) | 905 if (!s->eac3) |
860 /* phase flags in use */ | 932 /* phase flags in use */ |
861 if (channel_mode == AC3_CHMODE_STEREO) | 933 if (channel_mode == AC3_CHMODE_STEREO) |
862 s->phase_flags_in_use = get_bits1(gbc); | 934 s->phase_flags_in_use = get_bits1(gbc); |
863 | 935 |
864 /* coupling frequency range */ | 936 /* coupling frequency range */ |
865 /* TODO: modify coupling end freq if spectral extension is used */ | |
866 cpl_start_subband = get_bits(gbc, 4); | 937 cpl_start_subband = get_bits(gbc, 4); |
938 if (s->spx_in_use[blk]) { | |
939 cpl_end_subband = s->spx_start_subband - 1; | |
940 } else { | |
867 cpl_end_subband = get_bits(gbc, 4) + 3; | 941 cpl_end_subband = get_bits(gbc, 4) + 3; |
942 } | |
868 s->num_cpl_subbands = cpl_end_subband - cpl_start_subband; | 943 s->num_cpl_subbands = cpl_end_subband - cpl_start_subband; |
869 if (s->num_cpl_subbands < 0) { | 944 if (s->num_cpl_subbands < 0) { |
870 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d > %d)\n", | 945 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d > %d)\n", |
871 cpl_start_subband, cpl_end_subband); | 946 cpl_start_subband, cpl_end_subband); |
872 return -1; | 947 return -1; |
937 | 1012 |
938 /* stereo rematrixing strategy and band structure */ | 1013 /* stereo rematrixing strategy and band structure */ |
939 if (channel_mode == AC3_CHMODE_STEREO) { | 1014 if (channel_mode == AC3_CHMODE_STEREO) { |
940 if ((s->eac3 && !blk) || get_bits1(gbc)) { | 1015 if ((s->eac3 && !blk) || get_bits1(gbc)) { |
941 s->num_rematrixing_bands = 4; | 1016 s->num_rematrixing_bands = 4; |
942 if(cpl_in_use && s->start_freq[CPL_CH] <= 61) | 1017 if (cpl_in_use) { |
1018 if (s->start_freq[CPL_CH] <= 61) | |
943 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37); | 1019 s->num_rematrixing_bands -= 1 + (s->start_freq[CPL_CH] == 37); |
1020 } else if (s->spx_in_use[blk]) { | |
1021 if (s->spx_start_freq <= 61) | |
1022 s->num_rematrixing_bands -= 1 + (s->spx_start_freq <= 37) + | |
1023 (s->spx_start_freq <= 25); | |
1024 } | |
944 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) | 1025 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) |
945 s->rematrixing_flags[bnd] = get_bits1(gbc); | 1026 s->rematrixing_flags[bnd] = get_bits1(gbc); |
946 } else if (!blk) { | 1027 } else if (!blk) { |
947 av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n"); | 1028 av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n"); |
948 return -1; | 1029 return -1; |
963 if (s->exp_strategy[blk][ch] != EXP_REUSE) { | 1044 if (s->exp_strategy[blk][ch] != EXP_REUSE) { |
964 int group_size; | 1045 int group_size; |
965 int prev = s->end_freq[ch]; | 1046 int prev = s->end_freq[ch]; |
966 if (s->channel_in_cpl[ch]) | 1047 if (s->channel_in_cpl[ch]) |
967 s->end_freq[ch] = s->start_freq[CPL_CH]; | 1048 s->end_freq[ch] = s->start_freq[CPL_CH]; |
1049 else if (s->channel_in_spx[ch]) | |
1050 s->end_freq[ch] = s->spx_start_freq; | |
968 else { | 1051 else { |
969 int bandwidth_code = get_bits(gbc, 6); | 1052 int bandwidth_code = get_bits(gbc, 6); |
970 if (bandwidth_code > 60) { | 1053 if (bandwidth_code > 60) { |
971 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code); | 1054 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code); |
972 return -1; | 1055 return -1; |
1153 this also uncouples channels if coupling is in use. */ | 1236 this also uncouples channels if coupling is in use. */ |
1154 decode_transform_coeffs(s, blk); | 1237 decode_transform_coeffs(s, blk); |
1155 | 1238 |
1156 /* TODO: generate enhanced coupling coordinates and uncouple */ | 1239 /* TODO: generate enhanced coupling coordinates and uncouple */ |
1157 | 1240 |
1158 /* TODO: apply spectral extension */ | |
1159 | |
1160 /* recover coefficients if rematrixing is in use */ | 1241 /* recover coefficients if rematrixing is in use */ |
1161 if(s->channel_mode == AC3_CHMODE_STEREO) | 1242 if(s->channel_mode == AC3_CHMODE_STEREO) |
1162 do_rematrixing(s); | 1243 do_rematrixing(s); |
1244 | |
1245 ff_eac3_apply_spectral_extension(s); | |
1163 | 1246 |
1164 /* apply scaling to coefficients (headroom, dynrng) */ | 1247 /* apply scaling to coefficients (headroom, dynrng) */ |
1165 for(ch=1; ch<=s->channels; ch++) { | 1248 for(ch=1; ch<=s->channels; ch++) { |
1166 float gain = s->mul_bias / 4194304.0f; | 1249 float gain = s->mul_bias / 4194304.0f; |
1167 if(s->channel_mode == AC3_CHMODE_DUALMONO) { | 1250 if(s->channel_mode == AC3_CHMODE_DUALMONO) { |