A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
22** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**
25** $Id$
26**/
27
28#include "common.h"
29#include "structs.h"
30
31#include <stdlib.h>
32#include <string.h>
33
34#include "decoder.h"
35#include "mp4.h"
36#include "syntax.h"
37#include "error.h"
38/* rockbox: not used
39#include "output.h"
40*/
41#include "filtbank.h"
42#include "drc.h"
43#ifdef SBR_DEC
44#include "sbr_dec.h"
45#include "sbr_syntax.h"
46#endif
47#ifdef SSR_DEC
48#include "ssr.h"
49#endif
50
51/* Globals */
52#ifdef ANALYSIS
53uint16_t dbg_count;
54#endif
55
56/* static variables */
57static NeAACDecStruct s_AACDec;
58static real_t s_fb_intermed [MAX_CHANNELS][1*FRAME_LEN] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
59static real_t s_time_buf_1024[MAX_CHANNELS][1*FRAME_LEN] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
60#ifdef SBR_DEC
61#ifdef FAAD_STATIC_ALLOC
62static real_t s_time_buf_2048[MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
63#endif
64#endif
65#ifdef SSR_DEC
66static real_t s_ssr_overlap [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
67static real_t s_prev_fmd [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
68#endif
69#ifdef MAIN_DEC
70static pred_state s_pred_stat[MAX_CHANNELS][1*FRAME_LEN] MEM_ALIGN_ATTR;
71#endif
72#ifdef LTP_DEC
73static int16_t s_lt_pred_stat[MAX_CHANNELS][4*FRAME_LEN] MEM_ALIGN_ATTR;
74#endif
75
76
77/* static function declarations */
78static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
79 uint8_t *buffer, uint32_t buffer_size);
80/* not used by rockbox
81static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
82*/
83
84char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
85{
86 if (errcode >= NUM_ERROR_MESSAGES)
87 return NULL;
88 return err_msg[errcode];
89}
90
91/* rockbox: not used */
92#if 0
93uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
94{
95 uint32_t cap = 0;
96
97 /* can't do without it */
98 cap += LC_DEC_CAP;
99
100#ifdef MAIN_DEC
101 cap += MAIN_DEC_CAP;
102#endif
103#ifdef LTP_DEC
104 cap += LTP_DEC_CAP;
105#endif
106#ifdef LD_DEC
107 cap += LD_DEC_CAP;
108#endif
109#ifdef ERROR_RESILIENCE
110 cap += ERROR_RESILIENCE_CAP;
111#endif
112#ifdef FIXED_POINT
113 cap += FIXED_POINT_CAP;
114#endif
115
116 return cap;
117}
118#endif
119
120NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
121{
122 uint8_t i;
123 NeAACDecHandle hDecoder = NULL;
124
125 #if defined(CPU_COLDFIRE)
126 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
127 #endif
128
129 hDecoder = &s_AACDec;
130
131 memset(hDecoder , 0, sizeof(NeAACDecStruct));
132 memset(s_fb_intermed, 0, sizeof(s_fb_intermed));
133#ifdef SSR_DEC
134 memset(s_ssr_overlap, 0, sizeof(s_ssr_overlap));
135 memset(s_prev_fmd , 0, sizeof(s_prev_fmd));
136#endif
137#ifdef LTP_DEC
138 memset(s_lt_pred_stat, 0, sizeof(s_s_lt_pred_statpred_stat));
139#endif
140
141 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
142 hDecoder->config.defObjectType = MAIN;
143 hDecoder->config.defSampleRate = 44100;
144 hDecoder->config.downMatrix = 0;
145 hDecoder->adts_header_present = 0;
146 hDecoder->adif_header_present = 0;
147#ifdef ERROR_RESILIENCE
148 hDecoder->aacSectionDataResilienceFlag = 0;
149 hDecoder->aacScalefactorDataResilienceFlag = 0;
150 hDecoder->aacSpectralDataResilienceFlag = 0;
151#endif
152 hDecoder->frameLength = FRAME_LEN;
153
154 hDecoder->frame = 0;
155
156 for (i = 0; i < MAX_CHANNELS; i++)
157 {
158 hDecoder->window_shape_prev[i] = 0;
159 hDecoder->time_out[i] = NULL;
160 hDecoder->fb_intermed[i] = s_fb_intermed[i];
161#ifdef SSR_DEC
162 hDecoder->ssr_overlap[i] = s_ssr_overlap[i];
163 hDecoder->prev_fmd[i] = s_prev_fmd[i];
164 for (int k = 0; k < 2048; k++)
165 hDecoder->prev_fmd[i][k] = REAL_CONST(-1);
166#endif
167#ifdef MAIN_DEC
168 hDecoder->pred_stat[i] = s_pred_stat[i];
169 reset_all_predictors(hDecoder->pred_stat[channel], FRAME_LEN);
170#endif
171#ifdef LTP_DEC
172 hDecoder->ltp_lag[i] = 0;
173 hDecoder->lt_pred_stat[i] = s_lt_pred_stat[i];
174#endif
175 }
176
177#ifdef SBR_DEC
178 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
179 {
180 hDecoder->sbr[i] = NULL;
181 }
182#endif
183
184 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
185
186 return hDecoder;
187}
188
189NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
190{
191 if (hDecoder)
192 {
193 NeAACDecConfigurationPtr config = &(hDecoder->config);
194
195 return config;
196 }
197
198 return NULL;
199}
200
201uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
202 NeAACDecConfigurationPtr config)
203{
204 if (hDecoder && config)
205 {
206 /* check if we can decode this object type */
207 if (can_decode_ot(config->defObjectType) < 0)
208 return 0;
209 hDecoder->config.defObjectType = config->defObjectType;
210
211 /* samplerate: anything but 0 should be possible */
212 if (config->defSampleRate == 0)
213 return 0;
214 hDecoder->config.defSampleRate = config->defSampleRate;
215
216 /* check output format */
217#ifdef FIXED_POINT
218 if ((config->outputFormat < 1) || (config->outputFormat > 4))
219 return 0;
220#else
221 if ((config->outputFormat < 1) || (config->outputFormat > 5))
222 return 0;
223#endif
224 hDecoder->config.outputFormat = config->outputFormat;
225
226 if (config->downMatrix > 1)
227 return 0;
228 hDecoder->config.downMatrix = config->downMatrix;
229
230 /* OK */
231 return 1;
232 }
233
234 return 0;
235}
236
237int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
238 uint32_t buffer_size,
239 uint32_t *samplerate, uint8_t *channels)
240{
241 uint32_t i;
242 uint32_t bits = 0;
243 bitfile ld;
244 adif_header adif;
245 adts_header adts;
246
247 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
248 return -1;
249
250 adts.old_format = hDecoder->config.useOldADTSFormat;
251 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
252 hDecoder->object_type = hDecoder->config.defObjectType;
253 *samplerate = get_sample_rate(hDecoder->sf_index);
254 *channels = 1;
255
256 if (buffer != NULL)
257 {
258 faad_initbits(&ld, buffer, buffer_size);
259
260 /* Check if an ADIF header is present */
261 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
262 (buffer[2] == 'I') && (buffer[3] == 'F'))
263 {
264 hDecoder->adif_header_present = 1;
265
266 get_adif_header(&adif, &ld);
267 faad_byte_align(&ld);
268
269 hDecoder->sf_index = adif.pce[0].sf_index;
270 hDecoder->object_type = adif.pce[0].object_type + 1;
271
272 *samplerate = get_sample_rate(hDecoder->sf_index);
273 *channels = adif.pce[0].channels;
274
275 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
276 hDecoder->pce_set = 1;
277
278 bits = bit2byte(faad_get_processed_bits(&ld));
279
280 /* Check if an ADTS header is present */
281 } else if (adts_frame(&adts, &ld) == 0) {
282 hDecoder->adts_header_present = 1;
283
284 hDecoder->sf_index = adts.sf_index;
285 hDecoder->object_type = adts.profile + 1;
286
287 *samplerate = get_sample_rate(hDecoder->sf_index);
288 *channels = (adts.channel_configuration > 6) ?
289 2 : adts.channel_configuration;
290 }
291
292 if (ld.error)
293 {
294 faad_endbits(&ld);
295 return -1;
296 }
297 faad_endbits(&ld);
298 }
299 hDecoder->channelConfiguration = *channels;
300
301#if (defined(PS_DEC) || defined(DRM_PS))
302 /* check if we have a mono file */
303 if (*channels == 1)
304 {
305 /* upMatrix to 2 channels for implicit signalling of PS */
306 *channels = 2;
307 }
308#endif
309
310 /* A maximum of MAX_CHANNELS channels is supported. */
311 if (*channels > MAX_CHANNELS)
312 {
313 return -1;
314 }
315
316#ifdef SBR_DEC
317 /* implicit signalling */
318 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
319 {
320 *samplerate *= 2;
321 hDecoder->forceUpSampling = 1;
322 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
323 hDecoder->downSampledSBR = 1;
324 }
325#endif
326
327 /* must be done before frameLength is divided by 2 for LD */
328#ifdef SSR_DEC
329 if (hDecoder->object_type == SSR)
330 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
331 else
332#endif
333
334
335#ifdef LD_DEC
336 if (hDecoder->object_type == LD)
337 hDecoder->frameLength >>= 1;
338#endif
339
340 for (i=0; i<MAX_CHANNELS; ++i)
341 {
342#ifdef SBR_DEC
343 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
344 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
345 {
346#ifdef FAAD_STATIC_ALLOC
347 hDecoder->time_out[i] = s_time_buf_2048[i];
348#else
349 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
350 if (hDecoder->time_out[i] == NULL)
351 {
352 /* could not allocate memory */
353 return -1;
354 }
355#endif
356 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
357 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
358 }
359 else
360#endif
361 {
362 hDecoder->time_out[i] = s_time_buf_1024[i];
363 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
364 }
365 }
366
367 if (can_decode_ot(hDecoder->object_type) < 0)
368 return -1;
369
370 return bits;
371}
372
373/* Init the library using a DecoderSpecificInfo */
374int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
375 uint32_t SizeOfDecoderSpecificInfo,
376 uint32_t *samplerate, uint8_t *channels)
377{
378 int8_t rc;
379 uint32_t i;
380 mp4AudioSpecificConfig mp4ASC;
381
382 if((hDecoder == NULL)
383 || (pBuffer == NULL)
384 || (SizeOfDecoderSpecificInfo < 2)
385 || (samplerate == NULL)
386 || (channels == NULL))
387 {
388 return -1;
389 }
390
391 hDecoder->adif_header_present = 0;
392 hDecoder->adts_header_present = 0;
393
394 /* decode the audio specific config */
395 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
396 &(hDecoder->pce));
397
398 /* copy the relevant info to the decoder handle */
399 *samplerate = mp4ASC.samplingFrequency;
400 if (mp4ASC.channelsConfiguration)
401 {
402 *channels = mp4ASC.channelsConfiguration;
403 } else {
404 *channels = hDecoder->pce.channels;
405 hDecoder->pce_set = 1;
406 }
407#if (defined(PS_DEC) || defined(DRM_PS))
408 /* check if we have a mono file */
409 if (*channels == 1)
410 {
411 /* upMatrix to 2 channels for implicit signalling of PS */
412 *channels = 2;
413 }
414#endif
415
416 /* A maximum of MAX_CHANNELS channels is supported. */
417 if (*channels > MAX_CHANNELS)
418 {
419 return -1;
420 }
421
422 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
423 hDecoder->object_type = mp4ASC.objectTypeIndex;
424#ifdef ERROR_RESILIENCE
425 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
426 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
427 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
428#endif
429#ifdef SBR_DEC
430 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
431 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
432 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
433 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
434 else
435 hDecoder->forceUpSampling = 0;
436
437 /* AAC core decoder samplerate is 2 times as low */
438 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
439 {
440 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
441 }
442#endif
443
444 if (rc != 0)
445 {
446 return rc;
447 }
448 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
449 if (mp4ASC.frameLengthFlag)
450#ifdef ALLOW_SMALL_FRAMELENGTH
451 hDecoder->frameLength = 960;
452#else
453 return -1;
454#endif
455
456 /* must be done before frameLength is divided by 2 for LD */
457#ifdef SSR_DEC
458 if (hDecoder->object_type == SSR)
459 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
460 else
461#endif
462
463#ifdef LD_DEC
464 if (hDecoder->object_type == LD)
465 hDecoder->frameLength >>= 1;
466#endif
467
468 for (i=0; i<MAX_CHANNELS; ++i)
469 {
470#ifdef SBR_DEC
471 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
472 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
473 {
474#ifdef FAAD_STATIC_ALLOC
475 hDecoder->time_out[i] = s_time_buf_2048[i];
476#else
477 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
478 if (hDecoder->time_out[i] == NULL)
479 {
480 /* could not allocate memory */
481 return -1;
482 }
483#endif
484 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
485 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
486 }
487 else
488#endif
489 {
490 hDecoder->time_out[i] = s_time_buf_1024[i];
491 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
492 }
493 }
494
495 return 0;
496}
497
498#ifdef DRM
499int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
500 uint8_t channels)
501{
502 if (hDecoder == NULL)
503 return 1; /* error */
504
505 *hDecoder = NeAACDecOpen();
506
507 /* Special object type defined for DRM */
508 (*hDecoder)->config.defObjectType = DRM_ER_LC;
509
510 (*hDecoder)->config.defSampleRate = samplerate;
511#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
512 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
513 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
514 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
515#endif
516 (*hDecoder)->frameLength = 960;
517 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
518 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
519
520 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
521 (*hDecoder)->channelConfiguration = 2;
522 else
523 (*hDecoder)->channelConfiguration = 1;
524
525#ifdef SBR_DEC
526 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
527 (*hDecoder)->sbr_present_flag = 0;
528 else
529 (*hDecoder)->sbr_present_flag = 1;
530#endif
531
532
533 return 0;
534}
535#endif
536
537void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
538{
539 if (hDecoder)
540 {
541 hDecoder->postSeekResetFlag = 1;
542
543 if (frame != -1)
544 hDecoder->frame = frame;
545 }
546}
547
548/* not used by rockbox */
549#if 0
550static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
551{
552 hInfo->num_front_channels = 0;
553 hInfo->num_side_channels = 0;
554 hInfo->num_back_channels = 0;
555 hInfo->num_lfe_channels = 0;
556 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
557
558 if (hDecoder->downMatrix)
559 {
560 hInfo->num_front_channels = 2;
561 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
562 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
563 return;
564 }
565
566 /* check if there is a PCE */
567 if (hDecoder->pce_set)
568 {
569 uint8_t i, chpos = 0;
570 uint8_t chdir, back_center = 0;
571
572 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
573 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
574 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
575 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
576
577 chdir = hInfo->num_front_channels;
578 if (chdir & 1)
579 {
580 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
581 chdir--;
582 }
583 for (i = 0; i < chdir; i += 2)
584 {
585 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
586 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
587 }
588
589 for (i = 0; i < hInfo->num_side_channels; i += 2)
590 {
591 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
592 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
593 }
594
595 chdir = hInfo->num_back_channels;
596 if (chdir & 1)
597 {
598 back_center = 1;
599 chdir--;
600 }
601 for (i = 0; i < chdir; i += 2)
602 {
603 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
604 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
605 }
606 if (back_center)
607 {
608 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
609 }
610
611 for (i = 0; i < hInfo->num_lfe_channels; i++)
612 {
613 hInfo->channel_position[chpos++] = LFE_CHANNEL;
614 }
615
616 } else {
617 switch (hDecoder->channelConfiguration)
618 {
619 case 1:
620 hInfo->num_front_channels = 1;
621 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
622 break;
623 case 2:
624 hInfo->num_front_channels = 2;
625 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
626 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
627 break;
628 case 3:
629 hInfo->num_front_channels = 3;
630 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
631 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
632 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
633 break;
634 case 4:
635 hInfo->num_front_channels = 3;
636 hInfo->num_back_channels = 1;
637 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
638 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
639 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
640 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
641 break;
642 case 5:
643 hInfo->num_front_channels = 3;
644 hInfo->num_back_channels = 2;
645 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
646 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
647 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
648 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
649 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
650 break;
651 case 6:
652 hInfo->num_front_channels = 3;
653 hInfo->num_back_channels = 2;
654 hInfo->num_lfe_channels = 1;
655 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
656 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
657 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
658 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
659 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
660 hInfo->channel_position[5] = LFE_CHANNEL;
661 break;
662 case 7:
663 hInfo->num_front_channels = 3;
664 hInfo->num_side_channels = 2;
665 hInfo->num_back_channels = 2;
666 hInfo->num_lfe_channels = 1;
667 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
668 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
669 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
670 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
671 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
672 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
673 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
674 hInfo->channel_position[7] = LFE_CHANNEL;
675 break;
676 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
677 {
678 uint8_t i;
679 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
680 if (ch & 1) /* there's either a center front or a center back channel */
681 {
682 uint8_t ch1 = (ch-1)/2;
683 if (hDecoder->first_syn_ele == ID_SCE)
684 {
685 hInfo->num_front_channels = ch1 + 1;
686 hInfo->num_back_channels = ch1;
687 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
688 for (i = 1; i <= ch1; i+=2)
689 {
690 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
691 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
692 }
693 for (i = ch1+1; i < ch; i+=2)
694 {
695 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
696 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
697 }
698 } else {
699 hInfo->num_front_channels = ch1;
700 hInfo->num_back_channels = ch1 + 1;
701 for (i = 0; i < ch1; i+=2)
702 {
703 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
704 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
705 }
706 for (i = ch1; i < ch-1; i+=2)
707 {
708 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
709 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
710 }
711 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
712 }
713 } else {
714 uint8_t ch1 = (ch)/2;
715 hInfo->num_front_channels = ch1;
716 hInfo->num_back_channels = ch1;
717 if (ch1 & 1)
718 {
719 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
720 for (i = 1; i <= ch1; i+=2)
721 {
722 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
723 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
724 }
725 for (i = ch1+1; i < ch-1; i+=2)
726 {
727 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
728 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
729 }
730 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
731 } else {
732 for (i = 0; i < ch1; i+=2)
733 {
734 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
735 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
736 }
737 for (i = ch1; i < ch; i+=2)
738 {
739 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
740 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
741 }
742 }
743 }
744 hInfo->num_lfe_channels = hDecoder->has_lfe;
745 for (i = ch; i < hDecoder->fr_channels; i++)
746 {
747 hInfo->channel_position[i] = LFE_CHANNEL;
748 }
749 }
750 break;
751 }
752 }
753}
754#endif
755
756void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
757 NeAACDecFrameInfo *hInfo,
758 uint8_t *buffer, uint32_t buffer_size)
759{
760 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size);
761}
762
763static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
764 uint8_t *buffer, uint32_t buffer_size)
765{
766 uint8_t channels = 0;
767 uint8_t output_channels = 0;
768 bitfile ld;
769 uint32_t bitsconsumed;
770 uint16_t frame_len;
771
772#ifdef PROFILE
773 int64_t count = faad_get_ts();
774#endif
775
776 /* safety checks */
777 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
778 {
779 return NULL;
780 }
781
782#if 0
783 printf("%d\n", buffer_size*8);
784#endif
785
786 frame_len = hDecoder->frameLength;
787
788
789 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
790 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
791
792 /* initialize the bitstream */
793 faad_initbits(&ld, buffer, buffer_size);
794
795#if 0
796 {
797 int i;
798 for (i = 0; i < ((buffer_size+3)>>2); i++)
799 {
800 uint8_t *buf;
801 uint32_t temp = 0;
802 buf = faad_getbitbuffer(&ld, 32);
803 //temp = getdword((void*)buf);
804 temp = *((uint32_t*)buf);
805 printf("0x%.8X\n", temp);
806 free(buf);
807 }
808 faad_endbits(&ld);
809 faad_initbits(&ld, buffer, buffer_size);
810 }
811#endif
812
813#ifdef DRM
814 if (hDecoder->object_type == DRM_ER_LC)
815 {
816 /* We do not support stereo right now */
817 if (0) //(hDecoder->channelConfiguration == 2)
818 {
819 hInfo->error = 8; // Throw CRC error
820 goto error;
821 }
822
823 faad_getbits(&ld, 8
824 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
825 }
826#endif
827
828 if (hDecoder->adts_header_present)
829 {
830 adts_header adts;
831
832 adts.old_format = hDecoder->config.useOldADTSFormat;
833 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
834 goto error;
835
836 /* MPEG2 does byte_alignment() here,
837 * but ADTS header is always multiple of 8 bits in MPEG2
838 * so not needed to actually do it.
839 */
840 }
841
842#ifdef ANALYSIS
843 dbg_count = 0;
844#endif
845
846 /* decode the complete bitstream */
847#ifdef SCALABLE_DEC
848 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
849 {
850 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
851 } else {
852#endif
853 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
854#ifdef SCALABLE_DEC
855 }
856#endif
857
858 channels = hDecoder->fr_channels;
859
860 if (hInfo->error > 0)
861 goto error;
862
863 /* safety check */
864 if (channels == 0 || channels > MAX_CHANNELS)
865 {
866 /* invalid number of channels */
867 hInfo->error = 12;
868 goto error;
869 }
870
871 /* no more bit reading after this */
872 bitsconsumed = faad_get_processed_bits(&ld);
873 hInfo->bytesconsumed = bit2byte(bitsconsumed);
874 if (ld.error)
875 {
876 hInfo->error = 14;
877 goto error;
878 }
879 faad_endbits(&ld);
880
881
882 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
883 {
884 if (hDecoder->channelConfiguration == 0)
885 hDecoder->channelConfiguration = channels;
886
887 if (channels == 8) /* 7.1 */
888 hDecoder->channelConfiguration = 7;
889 if (channels == 7) /* not a standard channelConfiguration */
890 hDecoder->channelConfiguration = 0;
891 }
892
893 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
894 {
895 hDecoder->downMatrix = 1;
896 output_channels = 2;
897 } else {
898 output_channels = channels;
899 }
900
901#if (defined(PS_DEC) || defined(DRM_PS))
902 hDecoder->upMatrix = 0;
903 /* check if we have a mono file */
904 if (output_channels == 1)
905 {
906 /* upMatrix to 2 channels for implicit signalling of PS */
907 hDecoder->upMatrix = 1;
908 output_channels = 2;
909 }
910#endif
911
912 /* Make a channel configuration based on either a PCE or a channelConfiguration */
913 /* not used by rockbox
914 create_channel_config(hDecoder, hInfo);
915 */
916
917 /* number of samples in this frame */
918 hInfo->samples = frame_len*output_channels;
919 /* number of channels in this frame */
920 hInfo->channels = output_channels;
921 /* samplerate */
922 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
923 /* object type */
924 hInfo->object_type = hDecoder->object_type;
925 /* sbr */
926 hInfo->sbr = NO_SBR;
927 /* header type */
928 hInfo->header_type = RAW;
929 if (hDecoder->adif_header_present)
930 hInfo->header_type = ADIF;
931 if (hDecoder->adts_header_present)
932 hInfo->header_type = ADTS;
933#if (defined(PS_DEC) || defined(DRM_PS))
934 hInfo->ps = hDecoder->ps_used_global;
935#endif
936
937 /* check if frame has channel elements */
938 if (channels == 0)
939 {
940 hDecoder->frame++;
941 return NULL;
942 }
943
944 /* allocate the buffer for the final samples */
945 if (hDecoder->alloced_channels != output_channels)
946 {
947 hDecoder->alloced_channels = output_channels;
948 }
949
950#ifdef SBR_DEC
951 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
952 {
953 uint8_t ele;
954
955 /* this data is different when SBR is used or when the data is upsampled */
956 if (!hDecoder->downSampledSBR)
957 {
958 frame_len *= 2;
959 hInfo->samples *= 2;
960 hInfo->samplerate *= 2;
961 }
962
963 /* check if every element was provided with SBR data */
964 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
965 {
966 if (hDecoder->sbr[ele] == NULL)
967 {
968 hInfo->error = 25;
969 goto error;
970 }
971 }
972
973 /* sbr */
974 if (hDecoder->sbr_present_flag == 1)
975 {
976 hInfo->object_type = HE_AAC;
977 hInfo->sbr = SBR_UPSAMPLED;
978 } else {
979 hInfo->sbr = NO_SBR_UPSAMPLED;
980 }
981 if (hDecoder->downSampledSBR)
982 {
983 hInfo->sbr = SBR_DOWNSAMPLED;
984 }
985 }
986#endif
987
988 hDecoder->postSeekResetFlag = 0;
989
990 hDecoder->frame++;
991#ifdef LD_DEC
992 if (hDecoder->object_type != LD)
993 {
994#endif
995 if (hDecoder->frame <= 1)
996 hInfo->samples = 0;
997#ifdef LD_DEC
998 } else {
999 /* LD encoders will give lower delay */
1000 if (hDecoder->frame <= 0)
1001 hInfo->samples = 0;
1002 }
1003#endif
1004
1005 /* cleanup */
1006#ifdef ANALYSIS
1007 fflush(stdout);
1008#endif
1009
1010#ifdef PROFILE
1011 count = faad_get_ts() - count;
1012 hDecoder->cycles += count;
1013#endif
1014
1015 return hDecoder; /* return void* != NULL */
1016
1017error:
1018
1019 faad_endbits(&ld);
1020
1021 /* cleanup */
1022#ifdef ANALYSIS
1023 fflush(stdout);
1024#endif
1025
1026 return NULL;
1027}