Commit | Line | Data |
---|---|---|
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" | |
cc0591da | 85 | #include "aacdectab.h" |
71e9a1b8 RS |
86 | #include "mpeg4audio.h" |
87 | ||
88 | #include <assert.h> | |
89 | #include <errno.h> | |
90 | #include <math.h> | |
91 | #include <string.h> | |
92 | ||
93 | #ifndef CONFIG_HARDCODED_TABLES | |
94 | static float ff_aac_ivquant_tab[IVQUANT_SIZE]; | |
cc0591da | 95 | static float ff_aac_pow2sf_tab[316]; |
71e9a1b8 RS |
96 | #endif /* CONFIG_HARDCODED_TABLES */ |
97 | ||
98 | static VLC vlc_scalefactors; | |
99 | static VLC vlc_spectral[11]; | |
100 | ||
101 | ||
9cc04edf RS |
102 | /** |
103 | * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit. | |
104 | * | |
105 | * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present. | |
106 | * @param sce_map mono (Single Channel Element) map | |
107 | * @param type speaker type/position for these channels | |
108 | */ | |
109 | static void decode_channel_map(enum ChannelPosition *cpe_map, | |
110 | enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) { | |
111 | while(n--) { | |
112 | enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map | |
113 | map[get_bits(gb, 4)] = type; | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Decode program configuration element; reference: table 4.2. | |
119 | * | |
120 | * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. | |
121 | * | |
122 | * @return Returns error status. 0 - OK, !0 - error | |
123 | */ | |
124 | static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], | |
125 | GetBitContext * gb) { | |
126 | int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc; | |
127 | ||
128 | skip_bits(gb, 2); // object_type | |
129 | ||
130 | ac->m4ac.sampling_index = get_bits(gb, 4); | |
131 | if(ac->m4ac.sampling_index > 11) { | |
132 | av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); | |
133 | return -1; | |
134 | } | |
135 | ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index]; | |
71e9a1b8 RS |
136 | num_front = get_bits(gb, 4); |
137 | num_side = get_bits(gb, 4); | |
138 | num_back = get_bits(gb, 4); | |
139 | num_lfe = get_bits(gb, 2); | |
140 | num_assoc_data = get_bits(gb, 3); | |
141 | num_cc = get_bits(gb, 4); | |
142 | ||
cc0591da RS |
143 | if (get_bits1(gb)) |
144 | skip_bits(gb, 4); // mono_mixdown_tag | |
145 | if (get_bits1(gb)) | |
146 | skip_bits(gb, 4); // stereo_mixdown_tag | |
71e9a1b8 | 147 | |
cc0591da RS |
148 | if (get_bits1(gb)) |
149 | skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround | |
71e9a1b8 | 150 | |
cc0591da RS |
151 | decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front); |
152 | decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side ); | |
153 | decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back ); | |
154 | decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe ); | |
71e9a1b8 RS |
155 | |
156 | skip_bits_long(gb, 4 * num_assoc_data); | |
157 | ||
cc0591da | 158 | decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc ); |
71e9a1b8 RS |
159 | |
160 | align_get_bits(gb); | |
161 | ||
162 | /* comment field, first byte is length */ | |
163 | skip_bits_long(gb, 8 * get_bits(gb, 8)); | |
cc0591da RS |
164 | return 0; |
165 | } | |
71e9a1b8 | 166 | |
9cc04edf RS |
167 | /** |
168 | * Set up channel positions based on a default channel configuration | |
169 | * as specified in table 1.17. | |
170 | * | |
171 | * @param new_che_pos New channel position configuration - we only do something if it differs from the current one. | |
172 | * | |
173 | * @return Returns error status. 0 - OK, !0 - error | |
174 | */ | |
175 | static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID], | |
176 | int channel_config) | |
177 | { | |
178 | if(channel_config < 1 || channel_config > 7) { | |
179 | av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n", | |
180 | channel_config); | |
181 | return -1; | |
182 | } | |
183 | ||
184 | /* default channel configurations: | |
185 | * | |
186 | * 1ch : front center (mono) | |
187 | * 2ch : L + R (stereo) | |
188 | * 3ch : front center + L + R | |
189 | * 4ch : front center + L + R + back center | |
190 | * 5ch : front center + L + R + back stereo | |
191 | * 6ch : front center + L + R + back stereo + LFE | |
192 | * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE | |
193 | */ | |
194 | ||
195 | if(channel_config != 2) | |
196 | new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono) | |
197 | if(channel_config > 1) | |
198 | new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo) | |
199 | if(channel_config == 4) | |
200 | new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center | |
201 | if(channel_config > 4) | |
202 | new_che_pos[TYPE_CPE][(channel_config == 7) + 1] | |
203 | = AAC_CHANNEL_BACK; // back stereo | |
204 | if(channel_config > 5) | |
205 | new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE | |
206 | if(channel_config == 7) | |
207 | new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | return -1; | |
213 | } | |
214 | ||
215 | if (get_bits1(gb)) // dependsOnCoreCoder | |
216 | skip_bits(gb, 14); // coreCoderDelay | |
217 | extension_flag = get_bits1(gb); | |
218 | ||
219 | if(ac->m4ac.object_type == AOT_AAC_SCALABLE || | |
220 | ac->m4ac.object_type == AOT_ER_AAC_SCALABLE) | |
221 | skip_bits(gb, 3); // layerNr | |
222 | ||
223 | memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])); | |
224 | if (channel_config == 0) { | |
225 | skip_bits(gb, 4); // element_instance_tag | |
226 | if((ret = decode_pce(ac, new_che_pos, gb))) | |
227 | return ret; | |
228 | } else { | |
229 | if((ret = set_default_channel_config(ac, new_che_pos, channel_config))) | |
230 | return ret; | |
231 | } | |
232 | if((ret = output_configure(ac, ac->che_pos, new_che_pos))) | |
233 | return ret; | |
234 | ||
235 | if (extension_flag) { | |
236 | switch (ac->m4ac.object_type) { | |
237 | case AOT_ER_BSAC: | |
238 | skip_bits(gb, 5); // numOfSubFrame | |
239 | skip_bits(gb, 11); // layer_length | |
240 | break; | |
241 | case AOT_ER_AAC_LC: | |
242 | case AOT_ER_AAC_LTP: | |
243 | case AOT_ER_AAC_SCALABLE: | |
244 | case AOT_ER_AAC_LD: | |
245 | skip_bits(gb, 3); /* aacSectionDataResilienceFlag | |
246 | * aacScalefactorDataResilienceFlag | |
247 | * aacSpectralDataResilienceFlag | |
248 | */ | |
249 | break; | |
250 | } | |
251 | skip_bits1(gb); // extensionFlag3 (TBD in version 3) | |
252 | } | |
253 | return 0; | |
254 | } | |
255 | ||
256 | /** | |
257 | * Decode audio specific configuration; reference: table 1.13. | |
258 | * | |
259 | * @param data pointer to AVCodecContext extradata | |
260 | * @param data_size size of AVCCodecContext extradata | |
261 | * | |
262 | * @return Returns error status. 0 - OK, !0 - error | |
263 | */ | |
264 | static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) { | |
265 | GetBitContext gb; | |
266 | int i; | |
267 | ||
268 | init_get_bits(&gb, data, data_size * 8); | |
269 | ||
270 | if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0) | |
271 | return -1; | |
272 | if(ac->m4ac.sampling_index > 11) { | |
273 | av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index); | |
274 | return -1; | |
275 | } | |
276 | ||
277 | skip_bits_long(&gb, i); | |
278 | ||
279 | switch (ac->m4ac.object_type) { | |
280 | case AOT_AAC_LC: | |
281 | if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config)) | |
282 | return -1; | |
283 | break; | |
284 | default: | |
285 | av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n", | |
286 | ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type); | |
287 | return -1; | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
71e9a1b8 RS |
292 | static av_cold int aac_decode_init(AVCodecContext * avccontext) { |
293 | AACContext * ac = avccontext->priv_data; | |
294 | int i; | |
295 | ||
296 | ac->avccontext = avccontext; | |
297 | ||
cc0591da RS |
298 | if (avccontext->extradata_size <= 0 || |
299 | decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size)) | |
300 | return -1; | |
301 | ||
9cc04edf | 302 | avccontext->sample_fmt = SAMPLE_FMT_S16; |
71e9a1b8 RS |
303 | avccontext->sample_rate = ac->m4ac.sample_rate; |
304 | avccontext->frame_size = 1024; | |
305 | ||
306 | AAC_INIT_VLC_STATIC( 0, 144); | |
307 | AAC_INIT_VLC_STATIC( 1, 114); | |
308 | AAC_INIT_VLC_STATIC( 2, 188); | |
309 | AAC_INIT_VLC_STATIC( 3, 180); | |
310 | AAC_INIT_VLC_STATIC( 4, 172); | |
311 | AAC_INIT_VLC_STATIC( 5, 140); | |
312 | AAC_INIT_VLC_STATIC( 6, 168); | |
313 | AAC_INIT_VLC_STATIC( 7, 114); | |
314 | AAC_INIT_VLC_STATIC( 8, 262); | |
315 | AAC_INIT_VLC_STATIC( 9, 248); | |
316 | AAC_INIT_VLC_STATIC(10, 384); | |
317 | ||
318 | dsputil_init(&ac->dsp, avccontext); | |
319 | ||
9cc04edf RS |
320 | ac->random_state = 0x1f2e3d4c; |
321 | ||
71e9a1b8 RS |
322 | // -1024 - Compensate wrong IMDCT method. |
323 | // 32768 - Required to scale values to the correct range for the bias method | |
324 | // for float to int16 conversion. | |
325 | ||
326 | if(ac->dsp.float_to_int16 == ff_float_to_int16_c) { | |
327 | ac->add_bias = 385.0f; | |
328 | ac->sf_scale = 1. / (-1024. * 32768.); | |
329 | ac->sf_offset = 0; | |
330 | } else { | |
331 | ac->add_bias = 0.0f; | |
332 | ac->sf_scale = 1. / -1024.; | |
333 | ac->sf_offset = 60; | |
334 | } | |
335 | ||
336 | #ifndef CONFIG_HARDCODED_TABLES | |
337 | for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++) | |
338 | ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i; | |
cc0591da RS |
339 | for (i = 0; i < 316; i++) |
340 | ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.); | |
71e9a1b8 RS |
341 | #endif /* CONFIG_HARDCODED_TABLES */ |
342 | ||
343 | INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]), | |
344 | ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]), | |
345 | ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), | |
346 | 352); | |
347 | ||
348 | ff_mdct_init(&ac->mdct, 11, 1); | |
349 | ff_mdct_init(&ac->mdct_small, 8, 1); | |
350 | return 0; | |
351 | } | |
352 | ||
9cc04edf RS |
353 | /** |
354 | * Skip data_stream_element; reference: table 4.10. | |
355 | */ | |
356 | static void skip_data_stream_element(GetBitContext * gb) { | |
71e9a1b8 RS |
357 | int byte_align = get_bits1(gb); |
358 | int count = get_bits(gb, 8); | |
359 | if (count == 255) | |
360 | count += get_bits(gb, 8); | |
361 | if (byte_align) | |
362 | align_get_bits(gb); | |
363 | skip_bits_long(gb, 8 * count); | |
364 | } | |
365 | ||
366 | /** | |
9cc04edf RS |
367 | * Decode Individual Channel Stream info; reference: table 4.6. |
368 | * | |
369 | * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. | |
370 | */ | |
371 | static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) { | |
372 | if (get_bits1(gb)) { | |
373 | av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n"); | |
374 | memset(ics, 0, sizeof(IndividualChannelStream)); | |
375 | return -1; | |
376 | } | |
377 | ics->window_sequence[1] = ics->window_sequence[0]; | |
378 | ics->window_sequence[0] = get_bits(gb, 2); | |
379 | ics->use_kb_window[1] = ics->use_kb_window[0]; | |
380 | ics->use_kb_window[0] = get_bits1(gb); | |
381 | ics->num_window_groups = 1; | |
382 | ics->group_len[0] = 1; | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | /** | |
71e9a1b8 RS |
388 | * inverse quantization |
389 | * | |
390 | * @param a quantized value to be dequantized | |
391 | * @return Returns dequantized value. | |
392 | */ | |
393 | static inline float ivquant(int a) { | |
394 | if (a + (unsigned int)IVQUANT_SIZE/2 - 1 < (unsigned int)IVQUANT_SIZE - 1) | |
395 | return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1]; | |
396 | else | |
397 | return cbrtf(fabsf(a)) * a; | |
398 | } | |
399 | ||
9cc04edf RS |
400 | /** |
401 | * Decode band types (section_data payload); reference: table 4.46. | |
402 | * | |
403 | * @param band_type array of the used band type | |
404 | * @param band_type_run_end array of the last scalefactor band of a band type run | |
405 | * | |
406 | * @return Returns error status. 0 - OK, !0 - error | |
407 | */ | |
408 | static int decode_band_types(AACContext * ac, enum BandType band_type[120], | |
cc0591da RS |
409 | int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) { |
410 | int g, idx = 0; | |
411 | const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5; | |
412 | for (g = 0; g < ics->num_window_groups; g++) { | |
413 | int k = 0; | |
414 | while (k < ics->max_sfb) { | |
415 | uint8_t sect_len = k; | |
416 | int sect_len_incr; | |
417 | int sect_band_type = get_bits(gb, 4); | |
418 | if (sect_band_type == 12) { | |
419 | av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n"); | |
420 | return -1; | |
421 | } | |
422 | while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1) | |
423 | sect_len += sect_len_incr; | |
424 | sect_len += sect_len_incr; | |
425 | if (sect_len > ics->max_sfb) { | |
426 | av_log(ac->avccontext, AV_LOG_ERROR, | |
427 | "Number of bands (%d) exceeds limit (%d).\n", | |
428 | sect_len, ics->max_sfb); | |
429 | return -1; | |
430 | } | |
9cc04edf RS |
431 | } |
432 | } | |
433 | return 0; | |
434 | } | |
cc0591da | 435 | |
9cc04edf RS |
436 | /** |
437 | * Decode scalefactors; reference: table 4.47. | |
cc0591da | 438 | * |
cc0591da RS |
439 | * @param global_gain first scalefactor value as scalefactors are differentially coded |
440 | * @param band_type array of the used band type | |
441 | * @param band_type_run_end array of the last scalefactor band of a band type run | |
442 | * @param sf array of scalefactors or intensity stereo positions | |
443 | * | |
444 | * @return Returns error status. 0 - OK, !0 - error | |
445 | */ | |
446 | static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb, | |
9edae4ad | 447 | unsigned int global_gain, IndividualChannelStream * ics, |
cc0591da RS |
448 | enum BandType band_type[120], int band_type_run_end[120]) { |
449 | const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0); | |
450 | int g, i, idx = 0; | |
451 | int offset[3] = { global_gain, global_gain - 90, 100 }; | |
452 | int noise_flag = 1; | |
453 | static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" }; | |
454 | ics->intensity_present = 0; | |
455 | for (g = 0; g < ics->num_window_groups; g++) { | |
456 | for (i = 0; i < ics->max_sfb;) { | |
457 | int run_end = band_type_run_end[idx]; | |
458 | if (band_type[idx] == ZERO_BT) { | |
459 | for(; i < run_end; i++, idx++) | |
460 | sf[idx] = 0.; | |
461 | }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) { | |
462 | ics->intensity_present = 1; | |
463 | for(; i < run_end; i++, idx++) { | |
464 | offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
465 | if(offset[2] > 255U) { | |
466 | av_log(ac->avccontext, AV_LOG_ERROR, | |
467 | "%s (%d) out of range.\n", sf_str[2], offset[2]); | |
468 | return -1; | |
469 | } | |
470 | sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300]; | |
cc0591da RS |
471 | } |
472 | }else if(band_type[idx] == NOISE_BT) { | |
473 | for(; i < run_end; i++, idx++) { | |
474 | if(noise_flag-- > 0) | |
475 | offset[1] += get_bits(gb, 9) - 256; | |
476 | else | |
477 | offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
478 | if(offset[1] > 255U) { | |
479 | av_log(ac->avccontext, AV_LOG_ERROR, | |
480 | "%s (%d) out of range.\n", sf_str[1], offset[1]); | |
481 | return -1; | |
482 | } | |
483 | sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset]; | |
cc0591da RS |
484 | } |
485 | }else { | |
486 | for(; i < run_end; i++, idx++) { | |
487 | offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
488 | if(offset[0] > 255U) { | |
489 | av_log(ac->avccontext, AV_LOG_ERROR, | |
490 | "%s (%d) out of range.\n", sf_str[0], offset[0]); | |
491 | return -1; | |
492 | } | |
493 | sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset]; | |
cc0591da RS |
494 | } |
495 | } | |
496 | } | |
497 | } | |
498 | return 0; | |
499 | } | |
500 | ||
501 | /** | |
502 | * Decode pulse data; reference: table 4.7. | |
503 | */ | |
504 | static void decode_pulses(Pulse * pulse, GetBitContext * gb) { | |
505 | int i; | |
506 | pulse->num_pulse = get_bits(gb, 2) + 1; | |
507 | pulse->start = get_bits(gb, 6); | |
508 | for (i = 0; i < pulse->num_pulse; i++) { | |
509 | pulse->offset[i] = get_bits(gb, 5); | |
510 | pulse->amp [i] = get_bits(gb, 4); | |
511 | } | |
512 | } | |
513 | ||
514 | /** | |
9cc04edf RS |
515 | * Decode Mid/Side data; reference: table 4.54. |
516 | * | |
517 | * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s; | |
518 | * [1] mask is decoded from bitstream; [2] mask is all 1s; | |
519 | * [3] reserved for scalable AAC | |
520 | */ | |
521 | static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb, | |
522 | int ms_present) { | |
523 | ||
524 | /** | |
cc0591da RS |
525 | * Add pulses with particular amplitudes to the quantized spectral data; reference: 4.6.3.3. |
526 | * | |
71e9a1b8 RS |
527 | * @param pulse pointer to pulse data struct |
528 | * @param icoef array of quantized spectral data | |
529 | */ | |
530 | static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) { | |
531 | int i, off = ics->swb_offset[pulse->start]; | |
532 | for (i = 0; i < pulse->num_pulse; i++) { | |
533 | int ic; | |
534 | off += pulse->offset[i]; | |
535 | ic = (icoef[off] - 1)>>31; | |
536 | icoef[off] += (pulse->amp[i]^ic) - ic; | |
537 | } | |
538 | } | |
539 | ||
cc0591da | 540 | /** |
9cc04edf RS |
541 | * Decode an individual_channel_stream payload; reference: table 4.44. |
542 | * | |
543 | * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information. | |
544 | * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.) | |
545 | * | |
546 | * @return Returns error status. 0 - OK, !0 - error | |
547 | */ | |
548 | static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) { | |
549 | int icoeffs[1024]; | |
550 | Pulse pulse; | |
551 | TemporalNoiseShaping * tns = &sce->tns; | |
552 | IndividualChannelStream * ics = &sce->ics; | |
553 | float * out = sce->coeffs; | |
554 | int global_gain, pulse_present = 0; | |
555 | ||
556 | /* These two assignments are to silence some GCC warnings about the | |
557 | * variables being used uninitialised when in fact they always are. | |
558 | */ | |
559 | pulse.num_pulse = 0; | |
560 | pulse.start = 0; | |
561 | ||
562 | global_gain = get_bits(gb, 8); | |
563 | ||
564 | if (!common_window && !scale_flag) { | |
565 | if (decode_ics_info(ac, ics, gb, 0) < 0) | |
566 | return -1; | |
567 | } | |
568 | ||
569 | if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0) | |
570 | return -1; | |
571 | if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0) | |
572 | return -1; | |
573 | ||
574 | pulse_present = 0; | |
575 | if (!scale_flag) { | |
576 | if ((pulse_present = get_bits1(gb))) { | |
577 | if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { | |
578 | av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n"); | |
579 | return -1; | |
580 | } | |
581 | decode_pulses(&pulse, gb); | |
582 | } | |
583 | if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics)) | |
584 | return -1; | |
585 | if (get_bits1(gb)) { | |
586 | av_log_missing_feature(ac->avccontext, "SSR", 1); | |
587 | return -1; | |
588 | } | |
589 | } | |
590 | ||
591 | if (decode_spectrum(ac, icoeffs, gb, ics, sce->band_type) < 0) | |
592 | return -1; | |
593 | if (pulse_present) | |
594 | add_pulses(icoeffs, &pulse, ics); | |
595 | dequant(ac, out, icoeffs, sce->sf, ics, sce->band_type); | |
596 | return 0; | |
597 | } | |
598 | ||
599 | /** | |
600 | * Decode a channel_pair_element; reference: table 4.4. | |
601 | * | |
602 | * @param elem_id Identifies the instance of a syntax element. | |
603 | * | |
604 | * @return Returns error status. 0 - OK, !0 - error | |
605 | */ | |
606 | static int decode_cpe(AACContext * ac, GetBitContext * gb, int elem_id) { | |
607 | int i, ret, common_window, ms_present = 0; | |
608 | ChannelElement * cpe; | |
609 | ||
610 | cpe = ac->che[TYPE_CPE][elem_id]; | |
611 | common_window = get_bits1(gb); | |
612 | if (common_window) { | |
613 | if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1)) | |
614 | return -1; | |
615 | i = cpe->ch[1].ics.use_kb_window[0]; | |
616 | cpe->ch[1].ics = cpe->ch[0].ics; | |
617 | cpe->ch[1].ics.use_kb_window[1] = i; | |
618 | ms_present = get_bits(gb, 2); | |
619 | if(ms_present == 3) { | |
620 | av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n"); | |
621 | return -1; | |
622 | } else if(ms_present) | |
623 | decode_mid_side_stereo(cpe, gb, ms_present); | |
624 | } | |
625 | if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0))) | |
626 | return ret; | |
627 | if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0))) | |
628 | return ret; | |
629 | ||
630 | if (common_window && ms_present) | |
631 | apply_mid_side_stereo(cpe); | |
632 | ||
633 | if (cpe->ch[1].ics.intensity_present) | |
634 | apply_intensity_stereo(cpe, ms_present); | |
635 | return 0; | |
636 | } | |
637 | ||
638 | /** | |
639 | * Decode Spectral Band Replication extension data; reference: table 4.55. | |
cc0591da RS |
640 | * |
641 | * @param crc flag indicating the presence of CRC checksum | |
642 | * @param cnt length of TYPE_FIL syntactic element in bytes | |
9cc04edf | 643 | * |
cc0591da RS |
644 | * @return Returns number of bytes consumed from the TYPE_FIL element. |
645 | */ | |
646 | static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) { | |
647 | // TODO : sbr_extension implementation | |
9edae4ad | 648 | av_log_missing_feature(ac->avccontext, "SBR", 0); |
cc0591da RS |
649 | skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type |
650 | return cnt; | |
651 | } | |
652 | ||
9cc04edf RS |
653 | /** |
654 | * Decode dynamic range information; reference: table 4.52. | |
655 | * | |
656 | * @param cnt length of TYPE_FIL syntactic element in bytes | |
657 | * | |
658 | * @return Returns number of bytes consumed. | |
659 | */ | |
660 | static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) { | |
661 | int n = 1; | |
662 | int drc_num_bands = 1; | |
663 | int i; | |
664 | ||
665 | /* pce_tag_present? */ | |
666 | if(get_bits1(gb)) { | |
667 | che_drc->pce_instance_tag = get_bits(gb, 4); | |
668 | skip_bits(gb, 4); // tag_reserved_bits | |
669 | n++; | |
670 | } | |
671 | ||
672 | /* excluded_chns_present? */ | |
673 | if(get_bits1(gb)) { | |
674 | n += decode_drc_channel_exclusions(che_drc, gb); | |
675 | } | |
676 | ||
677 | /* drc_bands_present? */ | |
678 | if (get_bits1(gb)) { | |
679 | che_drc->band_incr = get_bits(gb, 4); | |
680 | che_drc->interpolation_scheme = get_bits(gb, 4); | |
681 | n++; | |
682 | drc_num_bands += che_drc->band_incr; | |
683 | for (i = 0; i < drc_num_bands; i++) { | |
684 | che_drc->band_top[i] = get_bits(gb, 8); | |
685 | n++; | |
686 | } | |
687 | } | |
688 | ||
689 | /* prog_ref_level_present? */ | |
690 | if (get_bits1(gb)) { | |
691 | che_drc->prog_ref_level = get_bits(gb, 7); | |
692 | skip_bits1(gb); // prog_ref_level_reserved_bits | |
693 | n++; | |
694 | } | |
695 | ||
696 | for (i = 0; i < drc_num_bands; i++) { | |
697 | che_drc->dyn_rng_sgn[i] = get_bits1(gb); | |
698 | che_drc->dyn_rng_ctl[i] = get_bits(gb, 7); | |
699 | n++; | |
700 | } | |
701 | ||
702 | return n; | |
703 | } | |
704 | ||
705 | /** | |
706 | * Decode extension data (incomplete); reference: table 4.51. | |
707 | * | |
708 | * @param cnt length of TYPE_FIL syntactic element in bytes | |
709 | * | |
710 | * @return Returns number of bytes consumed | |
711 | */ | |
712 | static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) { | |
cc0591da RS |
713 | int crc_flag = 0; |
714 | int res = cnt; | |
715 | switch (get_bits(gb, 4)) { // extension type | |
716 | case EXT_SBR_DATA_CRC: | |
717 | crc_flag++; | |
718 | case EXT_SBR_DATA: | |
719 | res = decode_sbr_extension(ac, gb, crc_flag, cnt); | |
720 | break; | |
721 | case EXT_DYNAMIC_RANGE: | |
722 | res = decode_dynamic_range(&ac->che_drc, gb, cnt); | |
723 | break; | |
724 | case EXT_FILL: | |
725 | case EXT_FILL_DATA: | |
726 | case EXT_DATA_ELEMENT: | |
727 | default: | |
728 | skip_bits_long(gb, 8*cnt - 4); | |
729 | break; | |
730 | }; | |
731 | return res; | |
732 | } | |
733 | ||
734 | /** | |
9cc04edf RS |
735 | * Conduct IMDCT and windowing. |
736 | */ | |
737 | static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) { | |
738 | IndividualChannelStream * ics = &sce->ics; | |
739 | float * in = sce->coeffs; | |
740 | float * out = sce->ret; | |
741 | float * saved = sce->saved; | |
742 | const float * lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024; | |
743 | const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128; | |
744 | const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_aac_sine_long_1024; | |
745 | const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_aac_sine_short_128; | |
746 | float * buf = ac->buf_mdct; | |
747 | int i; | |
748 | ||
749 | /** | |
cc0591da RS |
750 | * Apply dependent channel coupling (applied before IMDCT). |
751 | * | |
752 | * @param index index into coupling gain array | |
753 | */ | |
754 | static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { | |
755 | IndividualChannelStream * ics = &cc->ch[0].ics; | |
756 | const uint16_t * offsets = ics->swb_offset; | |
757 | float * dest = sce->coeffs; | |
758 | const float * src = cc->ch[0].coeffs; | |
759 | int g, i, group, k, idx = 0; | |
760 | if(ac->m4ac.object_type == AOT_AAC_LTP) { | |
761 | av_log(ac->avccontext, AV_LOG_ERROR, | |
762 | "Dependent coupling is not supported together with LTP\n"); | |
763 | return; | |
764 | } | |
765 | for (g = 0; g < ics->num_window_groups; g++) { | |
766 | for (i = 0; i < ics->max_sfb; i++, idx++) { | |
767 | if (cc->ch[0].band_type[idx] != ZERO_BT) { | |
cc0591da RS |
768 | for (group = 0; group < ics->group_len[g]; group++) { |
769 | for (k = offsets[i]; k < offsets[i+1]; k++) { | |
770 | // XXX dsputil-ize | |
9edae4ad | 771 | dest[group*128+k] += cc->coup.gain[index][idx] * src[group*128+k]; |
cc0591da RS |
772 | } |
773 | } | |
774 | } | |
775 | } | |
776 | dest += ics->group_len[g]*128; | |
777 | src += ics->group_len[g]*128; | |
778 | } | |
779 | } | |
780 | ||
781 | /** | |
782 | * Apply independent channel coupling (applied after IMDCT). | |
783 | * | |
784 | * @param index index into coupling gain array | |
785 | */ | |
786 | static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { | |
787 | int i; | |
cc0591da | 788 | for (i = 0; i < 1024; i++) |
9edae4ad | 789 | sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias); |
cc0591da RS |
790 | } |
791 | ||
9cc04edf RS |
792 | if (!ac->is_saved) { |
793 | ac->is_saved = 1; | |
794 | *data_size = 0; | |
795 | return 0; | |
796 | } | |
797 | ||
798 | data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t); | |
799 | if(*data_size < data_size_tmp) { | |
800 | av_log(avccontext, AV_LOG_ERROR, | |
801 | "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n", | |
802 | *data_size, data_size_tmp); | |
803 | return -1; | |
804 | } | |
805 | *data_size = data_size_tmp; | |
806 | ||
807 | ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels); | |
808 | ||
809 | return buf_size; | |
810 | } | |
811 | ||
71e9a1b8 RS |
812 | static av_cold int aac_decode_close(AVCodecContext * avccontext) { |
813 | AACContext * ac = avccontext->priv_data; | |
9edae4ad | 814 | int i, type; |
71e9a1b8 | 815 | |
cc0591da | 816 | for (i = 0; i < MAX_ELEM_ID; i++) { |
9edae4ad RS |
817 | for(type = 0; type < 4; type++) |
818 | av_freep(&ac->che[type][i]); | |
71e9a1b8 RS |
819 | } |
820 | ||
821 | ff_mdct_end(&ac->mdct); | |
822 | ff_mdct_end(&ac->mdct_small); | |
71e9a1b8 RS |
823 | return 0 ; |
824 | } | |
825 | ||
826 | AVCodec aac_decoder = { | |
827 | "aac", | |
828 | CODEC_TYPE_AUDIO, | |
829 | CODEC_ID_AAC, | |
830 | sizeof(AACContext), | |
831 | aac_decode_init, | |
832 | NULL, | |
833 | aac_decode_close, | |
834 | aac_decode_frame, | |
835 | .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), | |
cc0591da | 836 | .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, |
71e9a1b8 | 837 | }; |