Add myself as ARM maintainer
[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"
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
cc0591da 94 static float ff_aac_pow2sf_tab[316];
71e9a1b8
RS
95#endif /* CONFIG_HARDCODED_TABLES */
96
97static VLC vlc_scalefactors;
98static VLC vlc_spectral[11];
99
100
9cc04edf 101/**
62a57fae
RS
102 * Configure output channel order based on the current program configuration element.
103 *
104 * @param che_pos current channel position configuration
105 * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
106 *
107 * @return Returns error status. 0 - OK, !0 - error
108 */
109static int output_configure(AACContext *ac, enum ChannelPosition che_pos[4][MAX_ELEM_ID],
110 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID]) {
111 AVCodecContext *avctx = ac->avccontext;
112 int i, type, channels = 0;
113
114 if(!memcmp(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0])))
115 return 0; /* no change */
116
117 memcpy(che_pos, new_che_pos, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
118
119 /* Allocate or free elements depending on if they are in the
120 * current program configuration.
121 *
122 * Set up default 1:1 output mapping.
123 *
124 * For a 5.1 stream the output order will be:
125 * [ Front Left ] [ Front Right ] [ Center ] [ LFE ] [ Surround Left ] [ Surround Right ]
126 */
127
128 for(i = 0; i < MAX_ELEM_ID; i++) {
129 for(type = 0; type < 4; type++) {
130 if(che_pos[type][i]) {
131 if(!ac->che[type][i] && !(ac->che[type][i] = av_mallocz(sizeof(ChannelElement))))
132 return AVERROR(ENOMEM);
133 if(type != TYPE_CCE) {
134 ac->output_data[channels++] = ac->che[type][i]->ch[0].ret;
135 if(type == TYPE_CPE) {
136 ac->output_data[channels++] = ac->che[type][i]->ch[1].ret;
137 }
138 }
139 } else
140 av_freep(&ac->che[type][i]);
141 }
142 }
143
144 avctx->channels = channels;
145 return 0;
146}
147
148/**
9cc04edf
RS
149 * Decode an array of 4 bit element IDs, optionally interleaved with a stereo/mono switching bit.
150 *
151 * @param cpe_map Stereo (Channel Pair Element) map, NULL if stereo bit is not present.
152 * @param sce_map mono (Single Channel Element) map
153 * @param type speaker type/position for these channels
154 */
155static void decode_channel_map(enum ChannelPosition *cpe_map,
156 enum ChannelPosition *sce_map, enum ChannelPosition type, GetBitContext * gb, int n) {
157 while(n--) {
158 enum ChannelPosition *map = cpe_map && get_bits1(gb) ? cpe_map : sce_map; // stereo or mono map
159 map[get_bits(gb, 4)] = type;
160 }
161}
162
163/**
164 * Decode program configuration element; reference: table 4.2.
165 *
166 * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
167 *
168 * @return Returns error status. 0 - OK, !0 - error
169 */
170static int decode_pce(AACContext * ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
171 GetBitContext * gb) {
172 int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
173
174 skip_bits(gb, 2); // object_type
175
176 ac->m4ac.sampling_index = get_bits(gb, 4);
177 if(ac->m4ac.sampling_index > 11) {
178 av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
179 return -1;
180 }
181 ac->m4ac.sample_rate = ff_mpeg4audio_sample_rates[ac->m4ac.sampling_index];
71e9a1b8
RS
182 num_front = get_bits(gb, 4);
183 num_side = get_bits(gb, 4);
184 num_back = get_bits(gb, 4);
185 num_lfe = get_bits(gb, 2);
186 num_assoc_data = get_bits(gb, 3);
187 num_cc = get_bits(gb, 4);
188
cc0591da
RS
189 if (get_bits1(gb))
190 skip_bits(gb, 4); // mono_mixdown_tag
191 if (get_bits1(gb))
192 skip_bits(gb, 4); // stereo_mixdown_tag
71e9a1b8 193
cc0591da
RS
194 if (get_bits1(gb))
195 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
71e9a1b8 196
cc0591da
RS
197 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front);
198 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side );
199 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back );
200 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe );
71e9a1b8
RS
201
202 skip_bits_long(gb, 4 * num_assoc_data);
203
cc0591da 204 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc );
71e9a1b8
RS
205
206 align_get_bits(gb);
207
208 /* comment field, first byte is length */
209 skip_bits_long(gb, 8 * get_bits(gb, 8));
cc0591da
RS
210 return 0;
211}
71e9a1b8 212
9cc04edf
RS
213/**
214 * Set up channel positions based on a default channel configuration
215 * as specified in table 1.17.
216 *
217 * @param new_che_pos New channel position configuration - we only do something if it differs from the current one.
218 *
219 * @return Returns error status. 0 - OK, !0 - error
220 */
221static int set_default_channel_config(AACContext *ac, enum ChannelPosition new_che_pos[4][MAX_ELEM_ID],
222 int channel_config)
223{
224 if(channel_config < 1 || channel_config > 7) {
225 av_log(ac->avccontext, AV_LOG_ERROR, "invalid default channel configuration (%d)\n",
226 channel_config);
227 return -1;
228 }
229
230 /* default channel configurations:
231 *
232 * 1ch : front center (mono)
233 * 2ch : L + R (stereo)
234 * 3ch : front center + L + R
235 * 4ch : front center + L + R + back center
236 * 5ch : front center + L + R + back stereo
237 * 6ch : front center + L + R + back stereo + LFE
238 * 7ch : front center + L + R + outer front left + outer front right + back stereo + LFE
239 */
240
241 if(channel_config != 2)
242 new_che_pos[TYPE_SCE][0] = AAC_CHANNEL_FRONT; // front center (or mono)
243 if(channel_config > 1)
244 new_che_pos[TYPE_CPE][0] = AAC_CHANNEL_FRONT; // L + R (or stereo)
245 if(channel_config == 4)
246 new_che_pos[TYPE_SCE][1] = AAC_CHANNEL_BACK; // back center
247 if(channel_config > 4)
248 new_che_pos[TYPE_CPE][(channel_config == 7) + 1]
249 = AAC_CHANNEL_BACK; // back stereo
250 if(channel_config > 5)
251 new_che_pos[TYPE_LFE][0] = AAC_CHANNEL_LFE; // LFE
252 if(channel_config == 7)
253 new_che_pos[TYPE_CPE][1] = AAC_CHANNEL_FRONT; // outer front left + outer front right
254
255 return 0;
256}
257
62a57fae
RS
258/**
259 * Decode GA "General Audio" specific configuration; reference: table 4.1.
260 *
261 * @return Returns error status. 0 - OK, !0 - error
262 */
263static int decode_ga_specific_config(AACContext * ac, GetBitContext * gb, int channel_config) {
264 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
265 int extension_flag, ret;
266
267 if(get_bits1(gb)) { // frameLengthFlag
268 av_log_missing_feature(ac->avccontext, "960/120 MDCT window is", 1);
9cc04edf
RS
269 return -1;
270 }
271
272 if (get_bits1(gb)) // dependsOnCoreCoder
273 skip_bits(gb, 14); // coreCoderDelay
274 extension_flag = get_bits1(gb);
275
276 if(ac->m4ac.object_type == AOT_AAC_SCALABLE ||
277 ac->m4ac.object_type == AOT_ER_AAC_SCALABLE)
278 skip_bits(gb, 3); // layerNr
279
280 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
281 if (channel_config == 0) {
282 skip_bits(gb, 4); // element_instance_tag
283 if((ret = decode_pce(ac, new_che_pos, gb)))
284 return ret;
285 } else {
286 if((ret = set_default_channel_config(ac, new_che_pos, channel_config)))
287 return ret;
288 }
289 if((ret = output_configure(ac, ac->che_pos, new_che_pos)))
290 return ret;
291
292 if (extension_flag) {
293 switch (ac->m4ac.object_type) {
294 case AOT_ER_BSAC:
295 skip_bits(gb, 5); // numOfSubFrame
296 skip_bits(gb, 11); // layer_length
297 break;
298 case AOT_ER_AAC_LC:
299 case AOT_ER_AAC_LTP:
300 case AOT_ER_AAC_SCALABLE:
301 case AOT_ER_AAC_LD:
302 skip_bits(gb, 3); /* aacSectionDataResilienceFlag
303 * aacScalefactorDataResilienceFlag
304 * aacSpectralDataResilienceFlag
305 */
306 break;
307 }
308 skip_bits1(gb); // extensionFlag3 (TBD in version 3)
309 }
310 return 0;
311}
312
313/**
314 * Decode audio specific configuration; reference: table 1.13.
315 *
316 * @param data pointer to AVCodecContext extradata
317 * @param data_size size of AVCCodecContext extradata
318 *
319 * @return Returns error status. 0 - OK, !0 - error
320 */
321static int decode_audio_specific_config(AACContext * ac, void *data, int data_size) {
322 GetBitContext gb;
323 int i;
324
325 init_get_bits(&gb, data, data_size * 8);
326
327 if((i = ff_mpeg4audio_get_config(&ac->m4ac, data, data_size)) < 0)
328 return -1;
329 if(ac->m4ac.sampling_index > 11) {
330 av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
331 return -1;
332 }
333
334 skip_bits_long(&gb, i);
335
336 switch (ac->m4ac.object_type) {
337 case AOT_AAC_LC:
338 if (decode_ga_specific_config(ac, &gb, ac->m4ac.chan_config))
339 return -1;
340 break;
341 default:
342 av_log(ac->avccontext, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
343 ac->m4ac.sbr == 1? "SBR+" : "", ac->m4ac.object_type);
344 return -1;
345 }
346 return 0;
347}
348
62a57fae
RS
349/**
350 * linear congruential pseudorandom number generator
351 *
352 * @param previous_val pointer to the current state of the generator
353 *
354 * @return Returns a 32-bit pseudorandom integer
355 */
356static av_always_inline int lcg_random(int previous_val) {
357 return previous_val * 1664525 + 1013904223;
358}
359
71e9a1b8
RS
360static av_cold int aac_decode_init(AVCodecContext * avccontext) {
361 AACContext * ac = avccontext->priv_data;
362 int i;
363
364 ac->avccontext = avccontext;
365
cc0591da
RS
366 if (avccontext->extradata_size <= 0 ||
367 decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size))
368 return -1;
369
9cc04edf 370 avccontext->sample_fmt = SAMPLE_FMT_S16;
71e9a1b8
RS
371 avccontext->sample_rate = ac->m4ac.sample_rate;
372 avccontext->frame_size = 1024;
373
374 AAC_INIT_VLC_STATIC( 0, 144);
375 AAC_INIT_VLC_STATIC( 1, 114);
376 AAC_INIT_VLC_STATIC( 2, 188);
377 AAC_INIT_VLC_STATIC( 3, 180);
378 AAC_INIT_VLC_STATIC( 4, 172);
379 AAC_INIT_VLC_STATIC( 5, 140);
380 AAC_INIT_VLC_STATIC( 6, 168);
381 AAC_INIT_VLC_STATIC( 7, 114);
382 AAC_INIT_VLC_STATIC( 8, 262);
383 AAC_INIT_VLC_STATIC( 9, 248);
384 AAC_INIT_VLC_STATIC(10, 384);
385
386 dsputil_init(&ac->dsp, avccontext);
387
9cc04edf
RS
388 ac->random_state = 0x1f2e3d4c;
389
71e9a1b8
RS
390 // -1024 - Compensate wrong IMDCT method.
391 // 32768 - Required to scale values to the correct range for the bias method
392 // for float to int16 conversion.
393
394 if(ac->dsp.float_to_int16 == ff_float_to_int16_c) {
395 ac->add_bias = 385.0f;
396 ac->sf_scale = 1. / (-1024. * 32768.);
397 ac->sf_offset = 0;
398 } else {
399 ac->add_bias = 0.0f;
400 ac->sf_scale = 1. / -1024.;
401 ac->sf_offset = 60;
402 }
403
404#ifndef CONFIG_HARDCODED_TABLES
cc0591da
RS
405 for (i = 0; i < 316; i++)
406 ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.);
71e9a1b8
RS
407#endif /* CONFIG_HARDCODED_TABLES */
408
409 INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]),
410 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]),
411 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
412 352);
413
414 ff_mdct_init(&ac->mdct, 11, 1);
415 ff_mdct_init(&ac->mdct_small, 8, 1);
416 return 0;
417}
418
9cc04edf
RS
419/**
420 * Skip data_stream_element; reference: table 4.10.
421 */
422static void skip_data_stream_element(GetBitContext * gb) {
71e9a1b8
RS
423 int byte_align = get_bits1(gb);
424 int count = get_bits(gb, 8);
425 if (count == 255)
426 count += get_bits(gb, 8);
427 if (byte_align)
428 align_get_bits(gb);
429 skip_bits_long(gb, 8 * count);
430}
431
432/**
9cc04edf
RS
433 * Decode Individual Channel Stream info; reference: table 4.6.
434 *
435 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
436 */
437static int decode_ics_info(AACContext * ac, IndividualChannelStream * ics, GetBitContext * gb, int common_window) {
438 if (get_bits1(gb)) {
439 av_log(ac->avccontext, AV_LOG_ERROR, "Reserved bit set.\n");
440 memset(ics, 0, sizeof(IndividualChannelStream));
441 return -1;
442 }
443 ics->window_sequence[1] = ics->window_sequence[0];
444 ics->window_sequence[0] = get_bits(gb, 2);
445 ics->use_kb_window[1] = ics->use_kb_window[0];
446 ics->use_kb_window[0] = get_bits1(gb);
447 ics->num_window_groups = 1;
448 ics->group_len[0] = 1;
449
62a57fae
RS
450 if (get_bits1(gb)) {
451 av_log_missing_feature(ac->avccontext, "Predictor bit set but LTP is", 1);
452 memset(ics, 0, sizeof(IndividualChannelStream));
453 return -1;
454 }
455 }
456
457 if(ics->max_sfb > ics->num_swb) {
458 av_log(ac->avccontext, AV_LOG_ERROR,
459 "Number of scalefactor bands in group (%d) exceeds limit (%d).\n",
460 ics->max_sfb, ics->num_swb);
461 memset(ics, 0, sizeof(IndividualChannelStream));
462 return -1;
463 }
464
9cc04edf
RS
465 return 0;
466}
467
468/**
9cc04edf
RS
469 * Decode band types (section_data payload); reference: table 4.46.
470 *
471 * @param band_type array of the used band type
472 * @param band_type_run_end array of the last scalefactor band of a band type run
473 *
474 * @return Returns error status. 0 - OK, !0 - error
475 */
476static int decode_band_types(AACContext * ac, enum BandType band_type[120],
cc0591da
RS
477 int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) {
478 int g, idx = 0;
479 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
480 for (g = 0; g < ics->num_window_groups; g++) {
481 int k = 0;
482 while (k < ics->max_sfb) {
483 uint8_t sect_len = k;
484 int sect_len_incr;
485 int sect_band_type = get_bits(gb, 4);
486 if (sect_band_type == 12) {
487 av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n");
488 return -1;
489 }
490 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1)
491 sect_len += sect_len_incr;
492 sect_len += sect_len_incr;
493 if (sect_len > ics->max_sfb) {
494 av_log(ac->avccontext, AV_LOG_ERROR,
495 "Number of bands (%d) exceeds limit (%d).\n",
496 sect_len, ics->max_sfb);
497 return -1;
498 }
9cc04edf
RS
499 }
500 }
501 return 0;
502}
cc0591da 503
9cc04edf
RS
504/**
505 * Decode scalefactors; reference: table 4.47.
cc0591da 506 *
cc0591da
RS
507 * @param global_gain first scalefactor value as scalefactors are differentially coded
508 * @param band_type array of the used band type
509 * @param band_type_run_end array of the last scalefactor band of a band type run
510 * @param sf array of scalefactors or intensity stereo positions
511 *
512 * @return Returns error status. 0 - OK, !0 - error
513 */
514static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb,
9edae4ad 515 unsigned int global_gain, IndividualChannelStream * ics,
cc0591da
RS
516 enum BandType band_type[120], int band_type_run_end[120]) {
517 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0);
518 int g, i, idx = 0;
519 int offset[3] = { global_gain, global_gain - 90, 100 };
520 int noise_flag = 1;
521 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" };
cc0591da
RS
522 for (g = 0; g < ics->num_window_groups; g++) {
523 for (i = 0; i < ics->max_sfb;) {
524 int run_end = band_type_run_end[idx];
525 if (band_type[idx] == ZERO_BT) {
526 for(; i < run_end; i++, idx++)
527 sf[idx] = 0.;
528 }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) {
cc0591da
RS
529 for(; i < run_end; i++, idx++) {
530 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
531 if(offset[2] > 255U) {
532 av_log(ac->avccontext, AV_LOG_ERROR,
533 "%s (%d) out of range.\n", sf_str[2], offset[2]);
534 return -1;
535 }
536 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300];
cc0591da
RS
537 }
538 }else if(band_type[idx] == NOISE_BT) {
539 for(; i < run_end; i++, idx++) {
540 if(noise_flag-- > 0)
541 offset[1] += get_bits(gb, 9) - 256;
542 else
543 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
544 if(offset[1] > 255U) {
545 av_log(ac->avccontext, AV_LOG_ERROR,
546 "%s (%d) out of range.\n", sf_str[1], offset[1]);
547 return -1;
548 }
549 sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset];
cc0591da
RS
550 }
551 }else {
552 for(; i < run_end; i++, idx++) {
553 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
554 if(offset[0] > 255U) {
555 av_log(ac->avccontext, AV_LOG_ERROR,
556 "%s (%d) out of range.\n", sf_str[0], offset[0]);
557 return -1;
558 }
559 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset];
cc0591da
RS
560 }
561 }
562 }
563 }
564 return 0;
565}
566
567/**
568 * Decode pulse data; reference: table 4.7.
569 */
848a5815 570static void decode_pulses(Pulse * pulse, GetBitContext * gb, const uint16_t * swb_offset) {
cc0591da
RS
571 int i;
572 pulse->num_pulse = get_bits(gb, 2) + 1;
848a5815
RS
573 pulse->pos[0] = get_bits(gb, 5) + swb_offset[get_bits(gb, 6)];
574 pulse->amp[0] = get_bits(gb, 4);
575 for (i = 1; i < pulse->num_pulse; i++) {
576 pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i-1];
577 pulse->amp[i] = get_bits(gb, 4);
cc0591da
RS
578 }
579}
580
581/**
9cc04edf
RS
582 * Decode Mid/Side data; reference: table 4.54.
583 *
584 * @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
585 * [1] mask is decoded from bitstream; [2] mask is all 1s;
586 * [3] reserved for scalable AAC
587 */
588static void decode_mid_side_stereo(ChannelElement * cpe, GetBitContext * gb,
589 int ms_present) {
62a57fae
RS
590 int idx;
591 if (ms_present == 1) {
592 for (idx = 0; idx < cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb; idx++)
593 cpe->ms_mask[idx] = get_bits1(gb);
594 } else if (ms_present == 2) {
595 memset(cpe->ms_mask, 1, cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb * sizeof(cpe->ms_mask[0]));
596 }
597}
9cc04edf
RS
598
599/**
9cc04edf
RS
600 * Decode an individual_channel_stream payload; reference: table 4.44.
601 *
602 * @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
603 * @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
604 *
605 * @return Returns error status. 0 - OK, !0 - error
606 */
607static int decode_ics(AACContext * ac, SingleChannelElement * sce, GetBitContext * gb, int common_window, int scale_flag) {
9cc04edf
RS
608 Pulse pulse;
609 TemporalNoiseShaping * tns = &sce->tns;
610 IndividualChannelStream * ics = &sce->ics;
611 float * out = sce->coeffs;
612 int global_gain, pulse_present = 0;
613
848a5815
RS
614 /* This assignment is to silence a GCC warning about the variable being used
615 * uninitialized when in fact it always is.
9cc04edf
RS
616 */
617 pulse.num_pulse = 0;
9cc04edf
RS
618
619 global_gain = get_bits(gb, 8);
620
621 if (!common_window && !scale_flag) {
622 if (decode_ics_info(ac, ics, gb, 0) < 0)
623 return -1;
624 }
625
626 if (decode_band_types(ac, sce->band_type, sce->band_type_run_end, gb, ics) < 0)
627 return -1;
628 if (decode_scalefactors(ac, sce->sf, gb, global_gain, ics, sce->band_type, sce->band_type_run_end) < 0)
629 return -1;
630
631 pulse_present = 0;
632 if (!scale_flag) {
633 if ((pulse_present = get_bits1(gb))) {
634 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
635 av_log(ac->avccontext, AV_LOG_ERROR, "Pulse tool not allowed in eight short sequence.\n");
636 return -1;
637 }
848a5815 638 decode_pulses(&pulse, gb, ics->swb_offset);
9cc04edf
RS
639 }
640 if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
641 return -1;
642 if (get_bits1(gb)) {
643 av_log_missing_feature(ac->avccontext, "SSR", 1);
644 return -1;
645 }
646 }
647
848a5815 648 if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present, &pulse, ics, sce->band_type) < 0)
9cc04edf 649 return -1;
9cc04edf
RS
650 return 0;
651}
652
653/**
654 * Decode a channel_pair_element; reference: table 4.4.
655 *
656 * @param elem_id Identifies the instance of a syntax element.
657 *
658 * @return Returns error status. 0 - OK, !0 - error
659 */
660static int decode_cpe(AACContext * ac, GetBitContext * gb, int elem_id) {
661 int i, ret, common_window, ms_present = 0;
662 ChannelElement * cpe;
663
664 cpe = ac->che[TYPE_CPE][elem_id];
665 common_window = get_bits1(gb);
666 if (common_window) {
667 if (decode_ics_info(ac, &cpe->ch[0].ics, gb, 1))
668 return -1;
669 i = cpe->ch[1].ics.use_kb_window[0];
670 cpe->ch[1].ics = cpe->ch[0].ics;
671 cpe->ch[1].ics.use_kb_window[1] = i;
672 ms_present = get_bits(gb, 2);
673 if(ms_present == 3) {
674 av_log(ac->avccontext, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
675 return -1;
676 } else if(ms_present)
677 decode_mid_side_stereo(cpe, gb, ms_present);
678 }
679 if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
680 return ret;
681 if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
682 return ret;
683
684 if (common_window && ms_present)
685 apply_mid_side_stereo(cpe);
686
848a5815 687 apply_intensity_stereo(cpe, ms_present);
9cc04edf
RS
688 return 0;
689}
690
62a57fae
RS
691 coup->coupling_point = 2*get_bits1(gb);
692 coup->num_coupled = get_bits(gb, 3);
693 for (c = 0; c <= coup->num_coupled; c++) {
694 num_gain++;
695 coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
696 coup->id_select[c] = get_bits(gb, 4);
697 if (coup->type[c] == TYPE_CPE) {
698 coup->ch_select[c] = get_bits(gb, 2);
699 if (coup->ch_select[c] == 3)
700 num_gain++;
701 } else
702 coup->ch_select[c] = 1;
703 }
704 coup->coupling_point += get_bits1(gb);
705
706 if (coup->coupling_point == 2) {
707 av_log(ac->avccontext, AV_LOG_ERROR,
708 "Independently switched CCE with 'invalid' domain signalled.\n");
709 memset(coup, 0, sizeof(ChannelCoupling));
710 return -1;
711 }
712
713 sign = get_bits(gb, 1);
714 scale = pow(2., pow(2., get_bits(gb, 2) - 3));
715
716 if ((ret = decode_ics(ac, sce, gb, 0, 0)))
717 return ret;
718
719 for (c = 0; c < num_gain; c++) {
720 int cge = 1;
721 int gain = 0;
722 float gain_cache = 1.;
723 if (c) {
724 cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
725 gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
726 gain_cache = pow(scale, gain);
727 }
728 for (g = 0; g < sce->ics.num_window_groups; g++)
729 for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++)
730 if (sce->band_type[idx] != ZERO_BT) {
731 if (!cge) {
732 int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
733 if (t) {
734 int s = 1;
735 if (sign) {
736 s -= 2 * (t & 0x1);
737 t >>= 1;
738 }
739 gain += t;
740 gain_cache = pow(scale, gain) * s;
741 }
742 }
743 coup->gain[c][idx] = gain_cache;
744 }
745 }
746 return 0;
747}
748
9cc04edf
RS
749/**
750 * Decode Spectral Band Replication extension data; reference: table 4.55.
cc0591da
RS
751 *
752 * @param crc flag indicating the presence of CRC checksum
753 * @param cnt length of TYPE_FIL syntactic element in bytes
9cc04edf 754 *
cc0591da
RS
755 * @return Returns number of bytes consumed from the TYPE_FIL element.
756 */
757static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) {
758 // TODO : sbr_extension implementation
9edae4ad 759 av_log_missing_feature(ac->avccontext, "SBR", 0);
cc0591da
RS
760 skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type
761 return cnt;
762}
763
9cc04edf 764/**
62a57fae
RS
765 * Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
766 *
767 * @return Returns number of bytes consumed.
768 */
769static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc, GetBitContext * gb) {
770 int i;
771 int num_excl_chan = 0;
772
773 do {
774 for (i = 0; i < 7; i++)
775 che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
776 } while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
777
778 return num_excl_chan / 7;
779}
780
781/**
9cc04edf
RS
782 * Decode dynamic range information; reference: table 4.52.
783 *
784 * @param cnt length of TYPE_FIL syntactic element in bytes
785 *
786 * @return Returns number of bytes consumed.
787 */
788static int decode_dynamic_range(DynamicRangeControl *che_drc, GetBitContext * gb, int cnt) {
789 int n = 1;
790 int drc_num_bands = 1;
791 int i;
792
793 /* pce_tag_present? */
794 if(get_bits1(gb)) {
795 che_drc->pce_instance_tag = get_bits(gb, 4);
796 skip_bits(gb, 4); // tag_reserved_bits
797 n++;
798 }
799
800 /* excluded_chns_present? */
801 if(get_bits1(gb)) {
802 n += decode_drc_channel_exclusions(che_drc, gb);
803 }
804
805 /* drc_bands_present? */
806 if (get_bits1(gb)) {
807 che_drc->band_incr = get_bits(gb, 4);
808 che_drc->interpolation_scheme = get_bits(gb, 4);
809 n++;
810 drc_num_bands += che_drc->band_incr;
811 for (i = 0; i < drc_num_bands; i++) {
812 che_drc->band_top[i] = get_bits(gb, 8);
813 n++;
814 }
815 }
816
817 /* prog_ref_level_present? */
818 if (get_bits1(gb)) {
819 che_drc->prog_ref_level = get_bits(gb, 7);
820 skip_bits1(gb); // prog_ref_level_reserved_bits
821 n++;
822 }
823
824 for (i = 0; i < drc_num_bands; i++) {
825 che_drc->dyn_rng_sgn[i] = get_bits1(gb);
826 che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
827 n++;
828 }
829
830 return n;
831}
832
833/**
834 * Decode extension data (incomplete); reference: table 4.51.
835 *
836 * @param cnt length of TYPE_FIL syntactic element in bytes
837 *
838 * @return Returns number of bytes consumed
839 */
840static int decode_extension_payload(AACContext * ac, GetBitContext * gb, int cnt) {
cc0591da
RS
841 int crc_flag = 0;
842 int res = cnt;
843 switch (get_bits(gb, 4)) { // extension type
844 case EXT_SBR_DATA_CRC:
845 crc_flag++;
846 case EXT_SBR_DATA:
847 res = decode_sbr_extension(ac, gb, crc_flag, cnt);
848 break;
849 case EXT_DYNAMIC_RANGE:
850 res = decode_dynamic_range(&ac->che_drc, gb, cnt);
851 break;
852 case EXT_FILL:
853 case EXT_FILL_DATA:
854 case EXT_DATA_ELEMENT:
855 default:
856 skip_bits_long(gb, 8*cnt - 4);
857 break;
858 };
859 return res;
860}
861
862/**
9cc04edf
RS
863 * Conduct IMDCT and windowing.
864 */
865static void imdct_and_windowing(AACContext * ac, SingleChannelElement * sce) {
866 IndividualChannelStream * ics = &sce->ics;
867 float * in = sce->coeffs;
868 float * out = sce->ret;
869 float * saved = sce->saved;
848a5815
RS
870 const float * lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
871 const float * swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
872 const float * lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
873 const float * swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
9cc04edf
RS
874 float * buf = ac->buf_mdct;
875 int i;
876
62a57fae
RS
877 if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
878 if (ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE)
879 av_log(ac->avccontext, AV_LOG_WARNING,
880 "Transition from an ONLY_LONG or LONG_STOP to an EIGHT_SHORT sequence detected. "
881 "If you heard an audible artifact, please submit the sample to the FFmpeg developers.\n");
882 for (i = 0; i < 2048; i += 256) {
883 ff_imdct_calc(&ac->mdct_small, buf + i, in + i/2);
884 ac->dsp.vector_fmul_reverse(ac->revers + i/2, buf + i + 128, swindow, 128);
885 }
886 for (i = 0; i < 448; i++) out[i] = saved[i] + ac->add_bias;
887
888 ac->dsp.vector_fmul_add_add(out + 448 + 0*128, buf + 0*128, swindow_prev, saved + 448 , ac->add_bias, 128, 1);
889 ac->dsp.vector_fmul_add_add(out + 448 + 1*128, buf + 2*128, swindow, ac->revers + 0*128, ac->add_bias, 128, 1);
890 ac->dsp.vector_fmul_add_add(out + 448 + 2*128, buf + 4*128, swindow, ac->revers + 1*128, ac->add_bias, 128, 1);
891 ac->dsp.vector_fmul_add_add(out + 448 + 3*128, buf + 6*128, swindow, ac->revers + 2*128, ac->add_bias, 128, 1);
892 ac->dsp.vector_fmul_add_add(out + 448 + 4*128, buf + 8*128, swindow, ac->revers + 3*128, ac->add_bias, 64, 1);
893
894#if 0
895 vector_fmul_add_add_add(&ac->dsp, out + 448 + 1*128, buf + 2*128, swindow, saved + 448 + 1*128, ac->revers + 0*128, ac->add_bias, 128);
896 vector_fmul_add_add_add(&ac->dsp, out + 448 + 2*128, buf + 4*128, swindow, saved + 448 + 2*128, ac->revers + 1*128, ac->add_bias, 128);
897 vector_fmul_add_add_add(&ac->dsp, out + 448 + 3*128, buf + 6*128, swindow, saved + 448 + 3*128, ac->revers + 2*128, ac->add_bias, 128);
898 vector_fmul_add_add_add(&ac->dsp, out + 448 + 4*128, buf + 8*128, swindow, saved + 448 + 4*128, ac->revers + 3*128, ac->add_bias, 64);
899#endif
900
901 ac->dsp.vector_fmul_add_add(saved, buf + 1024 + 64, swindow + 64, ac->revers + 3*128+64, 0, 64, 1);
902 ac->dsp.vector_fmul_add_add(saved + 64, buf + 1024 + 2*128, swindow, ac->revers + 4*128, 0, 128, 1);
903 ac->dsp.vector_fmul_add_add(saved + 192, buf + 1024 + 4*128, swindow, ac->revers + 5*128, 0, 128, 1);
904 ac->dsp.vector_fmul_add_add(saved + 320, buf + 1024 + 6*128, swindow, ac->revers + 6*128, 0, 128, 1);
905 memcpy( saved + 448, ac->revers + 7*128, 128 * sizeof(float));
906 memset( saved + 576, 0, 448 * sizeof(float));
907 } else {
908 ff_imdct_calc(&ac->mdct, buf, in);
909 if (ics->window_sequence[0] == LONG_STOP_SEQUENCE) {
910 for (i = 0; i < 448; i++) out[i] = saved[i] + ac->add_bias;
911 ac->dsp.vector_fmul_add_add(out + 448, buf + 448, swindow_prev, saved + 448, ac->add_bias, 128, 1);
912 for (i = 576; i < 1024; i++) out[i] = buf[i] + saved[i] + ac->add_bias;
913 } else {
914 ac->dsp.vector_fmul_add_add(out, buf, lwindow_prev, saved, ac->add_bias, 1024, 1);
915 }
916 if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
917 memcpy(saved, buf + 1024, 448 * sizeof(float));
918 ac->dsp.vector_fmul_reverse(saved + 448, buf + 1024 + 448, swindow, 128);
919 memset(saved + 576, 0, 448 * sizeof(float));
920 } else {
921 ac->dsp.vector_fmul_reverse(saved, buf + 1024, lwindow, 1024);
922 }
923 }
924}
925
9cc04edf 926/**
cc0591da
RS
927 * Apply dependent channel coupling (applied before IMDCT).
928 *
929 * @param index index into coupling gain array
930 */
931static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) {
932 IndividualChannelStream * ics = &cc->ch[0].ics;
933 const uint16_t * offsets = ics->swb_offset;
934 float * dest = sce->coeffs;
935 const float * src = cc->ch[0].coeffs;
936 int g, i, group, k, idx = 0;
937 if(ac->m4ac.object_type == AOT_AAC_LTP) {
938 av_log(ac->avccontext, AV_LOG_ERROR,
939 "Dependent coupling is not supported together with LTP\n");
940 return;
941 }
942 for (g = 0; g < ics->num_window_groups; g++) {
943 for (i = 0; i < ics->max_sfb; i++, idx++) {
944 if (cc->ch[0].band_type[idx] != ZERO_BT) {
cc0591da
RS
945 for (group = 0; group < ics->group_len[g]; group++) {
946 for (k = offsets[i]; k < offsets[i+1]; k++) {
947 // XXX dsputil-ize
9edae4ad 948 dest[group*128+k] += cc->coup.gain[index][idx] * src[group*128+k];
cc0591da
RS
949 }
950 }
951 }
952 }
953 dest += ics->group_len[g]*128;
954 src += ics->group_len[g]*128;
955 }
956}
957
958/**
959 * Apply independent channel coupling (applied after IMDCT).
960 *
961 * @param index index into coupling gain array
962 */
963static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) {
964 int i;
cc0591da 965 for (i = 0; i < 1024; i++)
9edae4ad 966 sce->ret[i] += cc->coup.gain[index][0] * (cc->ch[0].ret[i] - ac->add_bias);
cc0591da
RS
967}
968
62a57fae
RS
969 }
970 }
971 }
972}
973
974static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, const uint8_t * buf, int buf_size) {
975 AACContext * ac = avccontext->priv_data;
976 GetBitContext gb;
977 enum RawDataBlockType elem_type;
978 int err, elem_id, data_size_tmp;
979
980 init_get_bits(&gb, buf, buf_size*8);
981
982 // parse
983 while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
984 elem_id = get_bits(&gb, 4);
985 err = -1;
986
987 if(elem_type == TYPE_SCE && elem_id == 1 &&
988 !ac->che[TYPE_SCE][elem_id] && ac->che[TYPE_LFE][0]) {
989 /* Some streams incorrectly code 5.1 audio as SCE[0] CPE[0] CPE[1] SCE[1]
990 instead of SCE[0] CPE[0] CPE[0] LFE[0]. If we seem to have
991 encountered such a stream, transfer the LFE[0] element to SCE[1] */
992 ac->che[TYPE_SCE][elem_id] = ac->che[TYPE_LFE][0];
993 ac->che[TYPE_LFE][0] = NULL;
994 }
995 if(elem_type && elem_type < TYPE_DSE) {
996 if(!ac->che[elem_type][elem_id])
997 return -1;
998 if(elem_type != TYPE_CCE)
999 ac->che[elem_type][elem_id]->coup.coupling_point = 4;
1000 }
1001
1002 switch (elem_type) {
1003
1004 case TYPE_SCE:
1005 err = decode_ics(ac, &ac->che[TYPE_SCE][elem_id]->ch[0], &gb, 0, 0);
1006 break;
1007
1008 case TYPE_CPE:
1009 err = decode_cpe(ac, &gb, elem_id);
1010 break;
1011
1012 case TYPE_CCE:
1013 err = decode_cce(ac, &gb, ac->che[TYPE_SCE][elem_id]);
1014 break;
1015
1016 case TYPE_LFE:
1017 err = decode_ics(ac, &ac->che[TYPE_LFE][elem_id]->ch[0], &gb, 0, 0);
1018 break;
1019
1020 case TYPE_DSE:
1021 skip_data_stream_element(&gb);
1022 err = 0;
1023 break;
1024
1025 case TYPE_PCE:
1026 {
1027 enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
1028 memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
1029 if((err = decode_pce(ac, new_che_pos, &gb)))
1030 break;
1031 err = output_configure(ac, ac->che_pos, new_che_pos);
1032 break;
1033 }
1034
1035 case TYPE_FIL:
1036 if (elem_id == 15)
1037 elem_id += get_bits(&gb, 8) - 1;
1038 while (elem_id > 0)
1039 elem_id -= decode_extension_payload(ac, &gb, elem_id);
1040 err = 0; /* FIXME */
1041 break;
1042
1043 default:
1044 err = -1; /* should not happen, but keeps compiler happy */
1045 break;
1046 }
1047
1048 if(err)
1049 return err;
1050 }
1051
1052 spectral_to_sample(ac);
1053
9cc04edf
RS
1054 if (!ac->is_saved) {
1055 ac->is_saved = 1;
1056 *data_size = 0;
848a5815 1057 return buf_size;
9cc04edf
RS
1058 }
1059
1060 data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
1061 if(*data_size < data_size_tmp) {
1062 av_log(avccontext, AV_LOG_ERROR,
1063 "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
1064 *data_size, data_size_tmp);
1065 return -1;
1066 }
1067 *data_size = data_size_tmp;
1068
1069 ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
1070
1071 return buf_size;
1072}
1073
71e9a1b8
RS
1074static av_cold int aac_decode_close(AVCodecContext * avccontext) {
1075 AACContext * ac = avccontext->priv_data;
9edae4ad 1076 int i, type;
71e9a1b8 1077
cc0591da 1078 for (i = 0; i < MAX_ELEM_ID; i++) {
9edae4ad
RS
1079 for(type = 0; type < 4; type++)
1080 av_freep(&ac->che[type][i]);
71e9a1b8
RS
1081 }
1082
1083 ff_mdct_end(&ac->mdct);
1084 ff_mdct_end(&ac->mdct_small);
71e9a1b8
RS
1085 return 0 ;
1086}
1087
1088AVCodec aac_decoder = {
1089 "aac",
1090 CODEC_TYPE_AUDIO,
1091 CODEC_ID_AAC,
1092 sizeof(AACContext),
1093 aac_decode_init,
1094 NULL,
1095 aac_decode_close,
1096 aac_decode_frame,
1097 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"),
cc0591da 1098 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
71e9a1b8 1099};