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