comparison ac3dec.c @ 8142:f17b1eb9ccd1 libavcodec

revert r15812 (E-AC-3 Spectral Extension) pending further review
author jbr
date Thu, 13 Nov 2008 22:38:38 +0000
parents f6f70b671287
children 5057047bcc52
comparison
equal deleted inserted replaced
8141:b95fd961d507 8142:f17b1eb9ccd1
747 band_struct[n_subbands-1] = 0; 747 band_struct[n_subbands-1] = 0;
748 748
749 /* calculate number of bands and band sizes based on band structure. 749 /* calculate number of bands and band sizes based on band structure.
750 note that the first 4 subbands in enhanced coupling span only 6 bins 750 note that the first 4 subbands in enhanced coupling span only 6 bins
751 instead of 12. */ 751 instead of 12. */
752 n_bands = n_subbands;
753 if (num_bands || band_sizes ) { 752 if (num_bands || band_sizes ) {
753 n_bands = n_subbands;
754 bnd_sz[0] = ecpl ? 6 : 12; 754 bnd_sz[0] = ecpl ? 6 : 12;
755 for (bnd = 0, subbnd = 1; subbnd < n_subbands; subbnd++) { 755 for (bnd = 0, subbnd = 1; subbnd < n_subbands; subbnd++) {
756 int subbnd_size = (ecpl && subbnd < 4) ? 6 : 12; 756 int subbnd_size = (ecpl && subbnd < 4) ? 6 : 12;
757 if (band_struct[subbnd-1]) { 757 if (band_struct[subbnd-1]) {
758 n_bands--; 758 n_bands--;
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 s->spx_in_use[blk] = get_bits1(gbc); 821 if (get_bits1(gbc)) {
822 if (s->spx_in_use[blk]) { 822 av_log_missing_feature(s->avctx, "Spectral extension", 1);
823 int begf, endf; 823 return -1;
824 int spx_end_subband; 824 }
825 825 /* TODO: parse spectral extension strategy info */
826 /* determine which channels use spx */ 826 }
827 if (s->channel_mode == AC3_CHMODE_MONO) { 827
828 s->channel_in_spx[1] = 1; 828 /* TODO: spectral extension coordinates */
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 }
901 829
902 /* coupling strategy */ 830 /* coupling strategy */
903 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) { 831 if (s->eac3 ? s->cpl_strategy_exists[blk] : get_bits1(gbc)) {
904 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 832 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
905 if (!s->eac3) 833 if (!s->eac3)
932 /* phase flags in use */ 860 /* phase flags in use */
933 if (channel_mode == AC3_CHMODE_STEREO) 861 if (channel_mode == AC3_CHMODE_STEREO)
934 s->phase_flags_in_use = get_bits1(gbc); 862 s->phase_flags_in_use = get_bits1(gbc);
935 863
936 /* coupling frequency range */ 864 /* coupling frequency range */
865 /* TODO: modify coupling end freq if spectral extension is used */
937 cpl_start_subband = get_bits(gbc, 4); 866 cpl_start_subband = get_bits(gbc, 4);
938 if (s->spx_in_use[blk]) { 867 cpl_end_subband = get_bits(gbc, 4) + 3;
939 cpl_end_subband = s->spx_start_subband - 1;
940 } else {
941 cpl_end_subband = get_bits(gbc, 4) + 3;
942 }
943 s->num_cpl_subbands = cpl_end_subband - cpl_start_subband; 868 s->num_cpl_subbands = cpl_end_subband - cpl_start_subband;
944 if (s->num_cpl_subbands < 0) { 869 if (s->num_cpl_subbands < 0) {
945 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d > %d)\n", 870 av_log(s->avctx, AV_LOG_ERROR, "invalid coupling range (%d > %d)\n",
946 cpl_start_subband, cpl_end_subband); 871 cpl_start_subband, cpl_end_subband);
947 return -1; 872 return -1;
1012 937
1013 /* stereo rematrixing strategy and band structure */ 938 /* stereo rematrixing strategy and band structure */
1014 if (channel_mode == AC3_CHMODE_STEREO) { 939 if (channel_mode == AC3_CHMODE_STEREO) {
1015 if ((s->eac3 && !blk) || get_bits1(gbc)) { 940 if ((s->eac3 && !blk) || get_bits1(gbc)) {
1016 s->num_rematrixing_bands = 4; 941 s->num_rematrixing_bands = 4;
1017 if (cpl_in_use) { 942 if(cpl_in_use && s->start_freq[CPL_CH] <= 61)
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 }
1025 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) 944 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++)
1026 s->rematrixing_flags[bnd] = get_bits1(gbc); 945 s->rematrixing_flags[bnd] = get_bits1(gbc);
1027 } else if (!blk) { 946 } else if (!blk) {
1028 av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n"); 947 av_log(s->avctx, AV_LOG_ERROR, "new rematrixing strategy must be present in block 0\n");
1029 return -1; 948 return -1;
1044 if (s->exp_strategy[blk][ch] != EXP_REUSE) { 963 if (s->exp_strategy[blk][ch] != EXP_REUSE) {
1045 int group_size; 964 int group_size;
1046 int prev = s->end_freq[ch]; 965 int prev = s->end_freq[ch];
1047 if (s->channel_in_cpl[ch]) 966 if (s->channel_in_cpl[ch])
1048 s->end_freq[ch] = s->start_freq[CPL_CH]; 967 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;
1051 else { 968 else {
1052 int bandwidth_code = get_bits(gbc, 6); 969 int bandwidth_code = get_bits(gbc, 6);
1053 if (bandwidth_code > 60) { 970 if (bandwidth_code > 60) {
1054 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code); 971 av_log(s->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60\n", bandwidth_code);
1055 return -1; 972 return -1;
1084 if (get_bits1(gbc)) { 1001 if (get_bits1(gbc)) {
1085 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift; 1002 s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1086 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift; 1003 s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gbc, 2)] >> s->bit_alloc_params.sr_shift;
1087 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)]; 1004 s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gbc, 2)];
1088 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)]; 1005 s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gbc, 2)];
1089 s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)]; 1006 s->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gbc, 3)];
1090 for(ch=!cpl_in_use; ch<=s->channels; ch++) 1007 for(ch=!cpl_in_use; ch<=s->channels; ch++)
1091 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 1008 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
1092 } else if (!blk) { 1009 } else if (!blk) {
1093 av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must be present in block 0\n"); 1010 av_log(s->avctx, AV_LOG_ERROR, "new bit allocation info must be present in block 0\n");
1094 return -1; 1011 return -1;
1236 this also uncouples channels if coupling is in use. */ 1153 this also uncouples channels if coupling is in use. */
1237 decode_transform_coeffs(s, blk); 1154 decode_transform_coeffs(s, blk);
1238 1155
1239 /* TODO: generate enhanced coupling coordinates and uncouple */ 1156 /* TODO: generate enhanced coupling coordinates and uncouple */
1240 1157
1158 /* TODO: apply spectral extension */
1159
1241 /* recover coefficients if rematrixing is in use */ 1160 /* recover coefficients if rematrixing is in use */
1242 if(s->channel_mode == AC3_CHMODE_STEREO) 1161 if(s->channel_mode == AC3_CHMODE_STEREO)
1243 do_rematrixing(s); 1162 do_rematrixing(s);
1244
1245 ff_eac3_apply_spectral_extension(s);
1246 1163
1247 /* apply scaling to coefficients (headroom, dynrng) */ 1164 /* apply scaling to coefficients (headroom, dynrng) */
1248 for(ch=1; ch<=s->channels; ch++) { 1165 for(ch=1; ch<=s->channels; ch++) {
1249 float gain = s->mul_bias / 4194304.0f; 1166 float gain = s->mul_bias / 4194304.0f;
1250 if(s->channel_mode == AC3_CHMODE_DUALMONO) { 1167 if(s->channel_mode == AC3_CHMODE_DUALMONO) {