comparison aac.c @ 11401:e340262ba532 libavcodec

Add an HE-AAC v1 decoder. A large portion of this code was orignally authored by Robert Swain. The rest was written by me. Full history is available at: svn://svn.ffmpeg.org/soc/aac-sbr http://github.com/aconverse/ffmpeg-heaac/tree/sbr_pub
author alexc
date Mon, 08 Mar 2010 04:33:02 +0000
parents 9a171a2c04d9
children 96dde15b2e0d
comparison
equal deleted inserted replaced
11400:c166792100a0 11401:e340262ba532
60 * N Structured Audio tools 60 * N Structured Audio tools
61 * N Structured Audio Sample Bank Format 61 * N Structured Audio Sample Bank Format
62 * N MIDI 62 * N MIDI
63 * N Harmonic and Individual Lines plus Noise 63 * N Harmonic and Individual Lines plus Noise
64 * N Text-To-Speech Interface 64 * N Text-To-Speech Interface
65 * N (in progress) Spectral Band Replication 65 * Y Spectral Band Replication
66 * Y (not in this code) Layer-1 66 * Y (not in this code) Layer-1
67 * Y (not in this code) Layer-2 67 * Y (not in this code) Layer-2
68 * Y (not in this code) Layer-3 68 * Y (not in this code) Layer-3
69 * N SinuSoidal Coding (Transient, Sinusoid, Noise) 69 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
70 * N (planned) Parametric Stereo 70 * N (planned) Parametric Stereo
84 #include "lpc.h" 84 #include "lpc.h"
85 85
86 #include "aac.h" 86 #include "aac.h"
87 #include "aactab.h" 87 #include "aactab.h"
88 #include "aacdectab.h" 88 #include "aacdectab.h"
89 #include "sbr.h"
90 #include "aacsbr.h"
89 #include "mpeg4audio.h" 91 #include "mpeg4audio.h"
90 #include "aac_parser.h" 92 #include "aac_parser.h"
91 93
92 #include <assert.h> 94 #include <assert.h>
93 #include <errno.h> 95 #include <errno.h>
178 int *channels) 180 int *channels)
179 { 181 {
180 if (che_pos[type][id]) { 182 if (che_pos[type][id]) {
181 if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement)))) 183 if (!ac->che[type][id] && !(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
182 return AVERROR(ENOMEM); 184 return AVERROR(ENOMEM);
185 ff_aac_sbr_ctx_init(&ac->che[type][id]->sbr);
183 if (type != TYPE_CCE) { 186 if (type != TYPE_CCE) {
184 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret; 187 ac->output_data[(*channels)++] = ac->che[type][id]->ch[0].ret;
185 if (type == TYPE_CPE) { 188 if (type == TYPE_CPE) {
186 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret; 189 ac->output_data[(*channels)++] = ac->che[type][id]->ch[1].ret;
187 } 190 }
188 } 191 }
189 } else 192 } else {
193 if (ac->che[type][id])
194 ff_aac_sbr_ctx_close(&ac->che[type][id]->sbr);
190 av_freep(&ac->che[type][id]); 195 av_freep(&ac->che[type][id]);
196 }
191 return 0; 197 return 0;
192 } 198 }
193 199
194 /** 200 /**
195 * Configure output channel order based on the current program configuration element. 201 * Configure output channel order based on the current program configuration element.
527 AAC_INIT_VLC_STATIC( 6, 306); 533 AAC_INIT_VLC_STATIC( 6, 306);
528 AAC_INIT_VLC_STATIC( 7, 268); 534 AAC_INIT_VLC_STATIC( 7, 268);
529 AAC_INIT_VLC_STATIC( 8, 510); 535 AAC_INIT_VLC_STATIC( 8, 510);
530 AAC_INIT_VLC_STATIC( 9, 366); 536 AAC_INIT_VLC_STATIC( 9, 366);
531 AAC_INIT_VLC_STATIC(10, 462); 537 AAC_INIT_VLC_STATIC(10, 462);
538
539 ff_aac_sbr_init();
532 540
533 dsputil_init(&ac->dsp, avccontext); 541 dsputil_init(&ac->dsp, avccontext);
534 542
535 ac->random_state = 0x1f2e3d4c; 543 ac->random_state = 0x1f2e3d4c;
536 544
1543 } 1551 }
1544 return 0; 1552 return 0;
1545 } 1553 }
1546 1554
1547 /** 1555 /**
1548 * Decode Spectral Band Replication extension data; reference: table 4.55.
1549 *
1550 * @param crc flag indicating the presence of CRC checksum
1551 * @param cnt length of TYPE_FIL syntactic element in bytes
1552 *
1553 * @return Returns number of bytes consumed from the TYPE_FIL element.
1554 */
1555 static int decode_sbr_extension(AACContext *ac, GetBitContext *gb,
1556 int crc, int cnt)
1557 {
1558 // TODO : sbr_extension implementation
1559 av_log_missing_feature(ac->avccontext, "SBR", 0);
1560 skip_bits_long(gb, 8 * cnt - 4); // -4 due to reading extension type
1561 return cnt;
1562 }
1563
1564 /**
1565 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53. 1556 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
1566 * 1557 *
1567 * @return Returns number of bytes consumed. 1558 * @return Returns number of bytes consumed.
1568 */ 1559 */
1569 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, 1560 static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
1639 * 1630 *
1640 * @param cnt length of TYPE_FIL syntactic element in bytes 1631 * @param cnt length of TYPE_FIL syntactic element in bytes
1641 * 1632 *
1642 * @return Returns number of bytes consumed 1633 * @return Returns number of bytes consumed
1643 */ 1634 */
1644 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt) 1635 static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
1636 ChannelElement *che, enum RawDataBlockType elem_type)
1645 { 1637 {
1646 int crc_flag = 0; 1638 int crc_flag = 0;
1647 int res = cnt; 1639 int res = cnt;
1648 switch (get_bits(gb, 4)) { // extension type 1640 switch (get_bits(gb, 4)) { // extension type
1649 case EXT_SBR_DATA_CRC: 1641 case EXT_SBR_DATA_CRC:
1650 crc_flag++; 1642 crc_flag++;
1651 case EXT_SBR_DATA: 1643 case EXT_SBR_DATA:
1652 res = decode_sbr_extension(ac, gb, crc_flag, cnt); 1644 if (!che) {
1645 av_log(ac->avccontext, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
1646 return res;
1647 } else if (!ac->m4ac.sbr) {
1648 av_log(ac->avccontext, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
1649 skip_bits_long(gb, 8 * cnt - 4);
1650 return res;
1651 } else if (ac->m4ac.sbr == -1 && ac->output_configured == OC_LOCKED) {
1652 av_log(ac->avccontext, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
1653 skip_bits_long(gb, 8 * cnt - 4);
1654 return res;
1655 } else {
1656 ac->m4ac.sbr = 1;
1657 }
1658 res = ff_decode_sbr_extension(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
1653 break; 1659 break;
1654 case EXT_DYNAMIC_RANGE: 1660 case EXT_DYNAMIC_RANGE:
1655 res = decode_dynamic_range(&ac->che_drc, gb, cnt); 1661 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
1656 break; 1662 break;
1657 case EXT_FILL: 1663 case EXT_FILL:
1828 int i; 1834 int i;
1829 const float gain = cce->coup.gain[index][0]; 1835 const float gain = cce->coup.gain[index][0];
1830 const float bias = ac->add_bias; 1836 const float bias = ac->add_bias;
1831 const float *src = cce->ch[0].ret; 1837 const float *src = cce->ch[0].ret;
1832 float *dest = target->ret; 1838 float *dest = target->ret;
1833 1839 const int len = 1024 << (ac->m4ac.sbr == 1);
1834 for (i = 0; i < 1024; i++) 1840
1841 for (i = 0; i < len; i++)
1835 dest[i] += gain * (src[i] - bias); 1842 dest[i] += gain * (src[i] - bias);
1836 } 1843 }
1837 1844
1838 /** 1845 /**
1839 * channel coupling transformation interface 1846 * channel coupling transformation interface
1887 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1); 1894 apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
1888 if (che->ch[1].tns.present) 1895 if (che->ch[1].tns.present)
1889 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1); 1896 apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
1890 if (type <= TYPE_CPE) 1897 if (type <= TYPE_CPE)
1891 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling); 1898 apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
1892 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) 1899 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
1893 imdct_and_windowing(ac, &che->ch[0]); 1900 imdct_and_windowing(ac, &che->ch[0]);
1894 if (type == TYPE_CPE) 1901 if (ac->m4ac.sbr > 0) {
1902 ff_sbr_dequant(ac, &che->sbr, type == TYPE_CPE ? TYPE_CPE : TYPE_SCE);
1903 ff_sbr_apply(ac, &che->sbr, 0, che->ch[0].ret, che->ch[0].ret);
1904 }
1905 }
1906 if (type == TYPE_CPE) {
1895 imdct_and_windowing(ac, &che->ch[1]); 1907 imdct_and_windowing(ac, &che->ch[1]);
1908 if (ac->m4ac.sbr > 0)
1909 ff_sbr_apply(ac, &che->sbr, 1, che->ch[1].ret, che->ch[1].ret);
1910 }
1896 if (type <= TYPE_CCE) 1911 if (type <= TYPE_CCE)
1897 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling); 1912 apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, apply_independent_coupling);
1898 } 1913 }
1899 } 1914 }
1900 } 1915 }
1940 int *data_size, AVPacket *avpkt) 1955 int *data_size, AVPacket *avpkt)
1941 { 1956 {
1942 const uint8_t *buf = avpkt->data; 1957 const uint8_t *buf = avpkt->data;
1943 int buf_size = avpkt->size; 1958 int buf_size = avpkt->size;
1944 AACContext *ac = avccontext->priv_data; 1959 AACContext *ac = avccontext->priv_data;
1945 ChannelElement *che = NULL; 1960 ChannelElement *che = NULL, *che_prev = NULL;
1946 GetBitContext gb; 1961 GetBitContext gb;
1947 enum RawDataBlockType elem_type; 1962 enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
1948 int err, elem_id, data_size_tmp; 1963 int err, elem_id, data_size_tmp;
1949 int buf_consumed; 1964 int buf_consumed;
1950 int samples = 1024, multiplier; 1965 int samples = 1024, multiplier;
1951 1966
1952 init_get_bits(&gb, buf, buf_size * 8); 1967 init_get_bits(&gb, buf, buf_size * 8);
2012 if (get_bits_left(&gb) < 8 * elem_id) { 2027 if (get_bits_left(&gb) < 8 * elem_id) {
2013 av_log(avccontext, AV_LOG_ERROR, overread_err); 2028 av_log(avccontext, AV_LOG_ERROR, overread_err);
2014 return -1; 2029 return -1;
2015 } 2030 }
2016 while (elem_id > 0) 2031 while (elem_id > 0)
2017 elem_id -= decode_extension_payload(ac, &gb, elem_id); 2032 elem_id -= decode_extension_payload(ac, &gb, elem_id, che_prev, elem_type_prev);
2018 err = 0; /* FIXME */ 2033 err = 0; /* FIXME */
2019 break; 2034 break;
2020 2035
2021 default: 2036 default:
2022 err = -1; /* should not happen, but keeps compiler happy */ 2037 err = -1; /* should not happen, but keeps compiler happy */
2023 break; 2038 break;
2024 } 2039 }
2025 2040
2041 che_prev = che;
2042 elem_type_prev = elem_type;
2043
2026 if (err) 2044 if (err)
2027 return err; 2045 return err;
2028 2046
2029 if (get_bits_left(&gb) < 3) { 2047 if (get_bits_left(&gb) < 3) {
2030 av_log(avccontext, AV_LOG_ERROR, overread_err); 2048 av_log(avccontext, AV_LOG_ERROR, overread_err);
2032 } 2050 }
2033 } 2051 }
2034 2052
2035 spectral_to_sample(ac); 2053 spectral_to_sample(ac);
2036 2054
2037 multiplier = 0; 2055 multiplier = (ac->m4ac.sbr == 1) ? ac->m4ac.ext_sample_rate > ac->m4ac.sample_rate : 0;
2038 samples <<= multiplier; 2056 samples <<= multiplier;
2039 if (ac->output_configured < OC_LOCKED) { 2057 if (ac->output_configured < OC_LOCKED) {
2040 avccontext->sample_rate = ac->m4ac.sample_rate << multiplier; 2058 avccontext->sample_rate = ac->m4ac.sample_rate << multiplier;
2041 avccontext->frame_size = samples; 2059 avccontext->frame_size = samples;
2042 } 2060 }
2043 2061
2044 data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t); 2062 data_size_tmp = samples * avccontext->channels * sizeof(int16_t);
2045 if (*data_size < data_size_tmp) { 2063 if (*data_size < data_size_tmp) {
2046 av_log(avccontext, AV_LOG_ERROR, 2064 av_log(avccontext, AV_LOG_ERROR,
2047 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n", 2065 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
2048 *data_size, data_size_tmp); 2066 *data_size, data_size_tmp);
2049 return -1; 2067 return -1;
2050 } 2068 }
2051 *data_size = data_size_tmp; 2069 *data_size = data_size_tmp;
2052 2070
2053 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels); 2071 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, samples, avccontext->channels);
2054 2072
2055 if (ac->output_configured) 2073 if (ac->output_configured)
2056 ac->output_configured = OC_LOCKED; 2074 ac->output_configured = OC_LOCKED;
2057 2075
2058 buf_consumed = (get_bits_count(&gb) + 7) >> 3; 2076 buf_consumed = (get_bits_count(&gb) + 7) >> 3;
2063 { 2081 {
2064 AACContext *ac = avccontext->priv_data; 2082 AACContext *ac = avccontext->priv_data;
2065 int i, type; 2083 int i, type;
2066 2084
2067 for (i = 0; i < MAX_ELEM_ID; i++) { 2085 for (i = 0; i < MAX_ELEM_ID; i++) {
2068 for (type = 0; type < 4; type++) 2086 for (type = 0; type < 4; type++) {
2087 if (ac->che[type][i])
2088 ff_aac_sbr_ctx_close(&ac->che[type][i]->sbr);
2069 av_freep(&ac->che[type][i]); 2089 av_freep(&ac->che[type][i]);
2090 }
2070 } 2091 }
2071 2092
2072 ff_mdct_end(&ac->mdct); 2093 ff_mdct_end(&ac->mdct);
2073 ff_mdct_end(&ac->mdct_small); 2094 ff_mdct_end(&ac->mdct_small);
2074 return 0; 2095 return 0;