OKed sections of code from the SoC AAC decoder
[libav.git] / libavcodec / aac.c
CommitLineData
71e9a1b8
RS
1/*
2 * AAC decoder
3 * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4 * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
24 * @file aac.c
25 * AAC decoder
26 * @author Oded Shimon ( ods15 ods15 dyndns org )
27 * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
28 */
29
30/*
31 * supported tools
32 *
33 * Support? Name
34 * N (code in SoC repo) gain control
35 * Y block switching
36 * Y window shapes - standard
37 * N window shapes - Low Delay
38 * Y filterbank - standard
39 * N (code in SoC repo) filterbank - Scalable Sample Rate
40 * Y Temporal Noise Shaping
41 * N (code in SoC repo) Long Term Prediction
42 * Y intensity stereo
43 * Y channel coupling
44 * N frequency domain prediction
45 * Y Perceptual Noise Substitution
46 * Y Mid/Side stereo
47 * N Scalable Inverse AAC Quantization
48 * N Frequency Selective Switch
49 * N upsampling filter
50 * Y quantization & coding - AAC
51 * N quantization & coding - TwinVQ
52 * N quantization & coding - BSAC
53 * N AAC Error Resilience tools
54 * N Error Resilience payload syntax
55 * N Error Protection tool
56 * N CELP
57 * N Silence Compression
58 * N HVXC
59 * N HVXC 4kbits/s VR
60 * N Structured Audio tools
61 * N Structured Audio Sample Bank Format
62 * N MIDI
63 * N Harmonic and Individual Lines plus Noise
64 * N Text-To-Speech Interface
65 * N (in progress) Spectral Band Replication
66 * Y (not in this code) Layer-1
67 * Y (not in this code) Layer-2
68 * Y (not in this code) Layer-3
69 * N SinuSoidal Coding (Transient, Sinusoid, Noise)
70 * N (planned) Parametric Stereo
71 * N Direct Stream Transfer
72 *
73 * Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
74 * - HE AAC v2 comprises LC AAC with Spectral Band Replication and
75 Parametric Stereo.
76 */
77
78
79#include "avcodec.h"
80#include "bitstream.h"
81#include "dsputil.h"
82
83#include "aac.h"
84#include "aactab.h"
85#include "mpeg4audio.h"
86
87#include <assert.h>
88#include <errno.h>
89#include <math.h>
90#include <string.h>
91
92#ifndef CONFIG_HARDCODED_TABLES
93 static float ff_aac_ivquant_tab[IVQUANT_SIZE];
94#endif /* CONFIG_HARDCODED_TABLES */
95
96static VLC vlc_scalefactors;
97static VLC vlc_spectral[11];
98
99
100 num_front = get_bits(gb, 4);
101 num_side = get_bits(gb, 4);
102 num_back = get_bits(gb, 4);
103 num_lfe = get_bits(gb, 2);
104 num_assoc_data = get_bits(gb, 3);
105 num_cc = get_bits(gb, 4);
106
107 newpcs->mono_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1;
108 newpcs->stereo_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1;
109
110 if (get_bits1(gb)) {
111 newpcs->mixdown_coeff_index = get_bits(gb, 2);
112 newpcs->pseudo_surround = get_bits1(gb);
113 }
114
115 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_FRONT, gb, num_front);
116 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_SIDE, gb, num_side );
117 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_BACK, gb, num_back );
118 program_config_element_parse_tags(NULL, newpcs->che_type[ID_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
119
120 skip_bits_long(gb, 4 * num_assoc_data);
121
122 program_config_element_parse_tags(newpcs->che_type[ID_CCE], newpcs->che_type[ID_CCE], AAC_CHANNEL_CC, gb, num_cc );
123
124 align_get_bits(gb);
125
126 /* comment field, first byte is length */
127 skip_bits_long(gb, 8 * get_bits(gb, 8));
128
129static av_cold int aac_decode_init(AVCodecContext * avccontext) {
130 AACContext * ac = avccontext->priv_data;
131 int i;
132
133 ac->avccontext = avccontext;
134
135 avccontext->sample_rate = ac->m4ac.sample_rate;
136 avccontext->frame_size = 1024;
137
138 AAC_INIT_VLC_STATIC( 0, 144);
139 AAC_INIT_VLC_STATIC( 1, 114);
140 AAC_INIT_VLC_STATIC( 2, 188);
141 AAC_INIT_VLC_STATIC( 3, 180);
142 AAC_INIT_VLC_STATIC( 4, 172);
143 AAC_INIT_VLC_STATIC( 5, 140);
144 AAC_INIT_VLC_STATIC( 6, 168);
145 AAC_INIT_VLC_STATIC( 7, 114);
146 AAC_INIT_VLC_STATIC( 8, 262);
147 AAC_INIT_VLC_STATIC( 9, 248);
148 AAC_INIT_VLC_STATIC(10, 384);
149
150 dsputil_init(&ac->dsp, avccontext);
151
152 // -1024 - Compensate wrong IMDCT method.
153 // 32768 - Required to scale values to the correct range for the bias method
154 // for float to int16 conversion.
155
156 if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
157 ac->add_bias = 385.0f;
158 ac->sf_scale = 1. / (-1024. * 32768.);
159 ac->sf_offset = 0;
160 } else {
161 ac->add_bias = 0.0f;
162 ac->sf_scale = 1. / -1024.;
163 ac->sf_offset = 60;
164 }
165
166#ifndef CONFIG_HARDCODED_TABLES
167 for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++)
168 ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i;
169#endif /* CONFIG_HARDCODED_TABLES */
170
171 INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]),
172 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
173 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
174 352);
175
176 ff_mdct_init(&ac->mdct, 11, 1);
177 ff_mdct_init(&ac->mdct_small, 8, 1);
178 return 0;
179}
180
181 int byte_align = get_bits1(gb);
182 int count = get_bits(gb, 8);
183 if (count == 255)
184 count += get_bits(gb, 8);
185 if (byte_align)
186 align_get_bits(gb);
187 skip_bits_long(gb, 8 * count);
188}
189
190/**
191 * inverse quantization
192 *
193 * @param a quantized value to be dequantized
194 * @return Returns dequantized value.
195 */
196static inline float ivquant(int a) {
197 if (a + (unsigned int)IVQUANT_SIZE/2 - 1 < (unsigned int)IVQUANT_SIZE - 1)
198 return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1];
199 else
200 return cbrtf(fabsf(a)) * a;
201}
202
203 * @param pulse pointer to pulse data struct
204 * @param icoef array of quantized spectral data
205 */
206static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) {
207 int i, off = ics->swb_offset[pulse->start];
208 for (i = 0; i < pulse->num_pulse; i++) {
209 int ic;
210 off += pulse->offset[i];
211 ic = (icoef[off] - 1)>>31;
212 icoef[off] += (pulse->amp[i]^ic) - ic;
213 }
214}
215
216static av_cold int aac_decode_close(AVCodecContext * avccontext) {
217 AACContext * ac = avccontext->priv_data;
218 int i, j;
219
220 for (i = 0; i < MAX_TAGID; i++) {
221 for(j = 0; j < 4; j++)
222 av_freep(&ac->che[j][i]);
223 }
224
225 ff_mdct_end(&ac->mdct);
226 ff_mdct_end(&ac->mdct_small);
227 av_freep(&ac->interleaved_output);
228 return 0 ;
229}
230
231AVCodec aac_decoder = {
232 "aac",
233 CODEC_TYPE_AUDIO,
234 CODEC_ID_AAC,
235 sizeof(AACContext),
236 aac_decode_init,
237 NULL,
238 aac_decode_close,
239 aac_decode_frame,
240 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
241};