2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: decoder.c,v 1.107 2004/09/08 09:43:11 gcp Exp $
43 #include "sbr_syntax.h"
53 /* static function declarations */
54 static void* aac_frame_decode(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
55 uint8_t *buffer
, uint32_t buffer_size
,
56 void **sample_buffer
, uint32_t sample_buffer_size
);
57 static void create_channel_config(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
);
60 char* NEAACDECAPI
NeAACDecGetErrorMessage(uint8_t errcode
)
62 if (errcode
>= NUM_ERROR_MESSAGES
)
64 return err_msg
[errcode
];
67 uint32_t NEAACDECAPI
NeAACDecGetCapabilities(void)
71 /* can't do without it */
83 #ifdef ERROR_RESILIENCE
84 cap
+= ERROR_RESILIENCE_CAP
;
87 cap
+= FIXED_POINT_CAP
;
93 NeAACDecHandle NEAACDECAPI
NeAACDecOpen(void)
96 NeAACDecHandle hDecoder
= NULL
;
98 if ((hDecoder
= (NeAACDecHandle
)faad_malloc(sizeof(NeAACDecStruct
))) == NULL
)
101 memset(hDecoder
, 0, sizeof(NeAACDecStruct
));
103 hDecoder
->config
.outputFormat
= FAAD_FMT_16BIT
;
104 hDecoder
->config
.defObjectType
= MAIN
;
105 hDecoder
->config
.defSampleRate
= 44100; /* Default: 44.1kHz */
106 hDecoder
->config
.downMatrix
= 0;
107 hDecoder
->adts_header_present
= 0;
108 hDecoder
->adif_header_present
= 0;
109 hDecoder
->latm_header_present
= 0;
110 #ifdef ERROR_RESILIENCE
111 hDecoder
->aacSectionDataResilienceFlag
= 0;
112 hDecoder
->aacScalefactorDataResilienceFlag
= 0;
113 hDecoder
->aacSpectralDataResilienceFlag
= 0;
115 hDecoder
->frameLength
= 1024;
118 hDecoder
->sample_buffer
= NULL
;
120 for (i
= 0; i
< MAX_CHANNELS
; i
++)
122 hDecoder
->window_shape_prev
[i
] = 0;
123 hDecoder
->time_out
[i
] = NULL
;
124 hDecoder
->fb_intermed
[i
] = NULL
;
126 hDecoder
->ssr_overlap
[i
] = NULL
;
127 hDecoder
->prev_fmd
[i
] = NULL
;
130 hDecoder
->pred_stat
[i
] = NULL
;
133 hDecoder
->ltp_lag
[i
] = 0;
134 hDecoder
->lt_pred_stat
[i
] = NULL
;
139 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
141 hDecoder
->sbr
[i
] = NULL
;
145 hDecoder
->drc
= drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
150 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
154 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
162 uint8_t NEAACDECAPI
NeAACDecSetConfiguration(NeAACDecHandle hDecoder
,
163 NeAACDecConfigurationPtr config
)
165 if (hDecoder
&& config
)
167 /* check if we can decode this object type */
168 if (can_decode_ot(config
->defObjectType
) < 0)
170 hDecoder
->config
.defObjectType
= config
->defObjectType
;
172 /* samplerate: anything but 0 should be possible */
173 if (config
->defSampleRate
== 0)
175 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
177 /* check output format */
179 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
182 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
185 hDecoder
->config
.outputFormat
= config
->outputFormat
;
187 if (config
->downMatrix
> 1)
189 hDecoder
->config
.downMatrix
= config
->downMatrix
;
198 static int latmCheck(latm_header
*latm
, bitfile
*ld
)
200 uint32_t good
=0, bad
=0, bits
, m
;
202 while(!ld
->error
&& !ld
->no_more_reading
)
204 bits
= faad_latm_frame(latm
, ld
);
223 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
224 uint32_t buffer_size
,
225 uint32_t *samplerate
, uint8_t *channels
)
232 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
235 hDecoder
->sf_index
= get_sr_index(hDecoder
->config
.defSampleRate
);
236 hDecoder
->object_type
= hDecoder
->config
.defObjectType
;
237 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
243 latm_header
*l
= &hDecoder
->latm_config
;
244 faad_initbits(&ld
, buffer
, buffer_size
);
246 memset(l
, 0, sizeof(latm_header
));
247 is_latm
= latmCheck(l
, &ld
);
250 faad_rewindbits(&ld
);
251 if(is_latm
&& l
->ASCbits
>0)
254 hDecoder
->latm_header_present
= 1;
255 x
= NeAACDecInit2(hDecoder
, &l
->ASC
, (l
->ASCbits
+7)/8, samplerate
, channels
);
257 hDecoder
->latm_header_present
= 0;
261 /* Check if an ADIF header is present */
262 if ((buffer
[0] == 'A') && (buffer
[1] == 'D') &&
263 (buffer
[2] == 'I') && (buffer
[3] == 'F'))
265 hDecoder
->adif_header_present
= 1;
267 get_adif_header(&adif
, &ld
);
268 faad_byte_align(&ld
);
270 hDecoder
->sf_index
= adif
.pce
[0].sf_index
;
271 hDecoder
->object_type
= adif
.pce
[0].object_type
+ 1;
273 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
274 *channels
= adif
.pce
[0].channels
;
276 memcpy(&(hDecoder
->pce
), &(adif
.pce
[0]), sizeof(program_config
));
277 hDecoder
->pce_set
= 1;
279 bits
= bit2byte(faad_get_processed_bits(&ld
));
281 /* Check if an ADTS header is present */
282 } else if (faad_showbits(&ld
, 12) == 0xfff) {
283 hDecoder
->adts_header_present
= 1;
285 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
286 adts_frame(&adts
, &ld
);
288 hDecoder
->sf_index
= adts
.sf_index
;
289 hDecoder
->object_type
= adts
.profile
+ 1;
291 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
292 *channels
= (adts
.channel_configuration
> 6) ?
293 2 : adts
.channel_configuration
;
303 hDecoder
->channelConfiguration
= *channels
;
305 #if (defined(PS_DEC) || defined(DRM_PS))
306 /* check if we have a mono file */
309 /* upMatrix to 2 channels for implicit signalling of PS */
315 /* implicit signalling */
316 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
319 hDecoder
->forceUpSampling
= 1;
320 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
321 hDecoder
->downSampledSBR
= 1;
325 /* must be done before frameLength is divided by 2 for LD */
327 if (hDecoder
->object_type
== SSR
)
328 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
331 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
334 if (hDecoder
->object_type
== LD
)
335 hDecoder
->frameLength
>>= 1;
338 if (can_decode_ot(hDecoder
->object_type
) < 0)
344 /* Init the library using a DecoderSpecificInfo */
345 int8_t NEAACDECAPI
NeAACDecInit2(NeAACDecHandle hDecoder
, uint8_t *pBuffer
,
346 uint32_t SizeOfDecoderSpecificInfo
,
347 uint32_t *samplerate
, uint8_t *channels
)
350 mp4AudioSpecificConfig mp4ASC
;
352 if((hDecoder
== NULL
)
354 || (SizeOfDecoderSpecificInfo
< 2)
355 || (samplerate
== NULL
)
356 || (channels
== NULL
))
361 hDecoder
->adif_header_present
= 0;
362 hDecoder
->adts_header_present
= 0;
364 /* decode the audio specific config */
365 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
366 &(hDecoder
->pce
), hDecoder
->latm_header_present
);
368 /* copy the relevant info to the decoder handle */
369 *samplerate
= mp4ASC
.samplingFrequency
;
370 if (mp4ASC
.channelsConfiguration
)
372 *channels
= mp4ASC
.channelsConfiguration
;
374 *channels
= hDecoder
->pce
.channels
;
375 hDecoder
->pce_set
= 1;
377 #if (defined(PS_DEC) || defined(DRM_PS))
378 /* check if we have a mono file */
381 /* upMatrix to 2 channels for implicit signalling of PS */
385 hDecoder
->sf_index
= mp4ASC
.samplingFrequencyIndex
;
386 hDecoder
->object_type
= mp4ASC
.objectTypeIndex
;
387 #ifdef ERROR_RESILIENCE
388 hDecoder
->aacSectionDataResilienceFlag
= mp4ASC
.aacSectionDataResilienceFlag
;
389 hDecoder
->aacScalefactorDataResilienceFlag
= mp4ASC
.aacScalefactorDataResilienceFlag
;
390 hDecoder
->aacSpectralDataResilienceFlag
= mp4ASC
.aacSpectralDataResilienceFlag
;
393 hDecoder
->sbr_present_flag
= mp4ASC
.sbr_present_flag
;
394 hDecoder
->downSampledSBR
= mp4ASC
.downSampledSBR
;
395 if (hDecoder
->config
.dontUpSampleImplicitSBR
== 0)
396 hDecoder
->forceUpSampling
= mp4ASC
.forceUpSampling
;
398 hDecoder
->forceUpSampling
= 0;
400 /* AAC core decoder samplerate is 2 times as low */
401 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || hDecoder
->forceUpSampling
== 1)
403 hDecoder
->sf_index
= get_sr_index(mp4ASC
.samplingFrequency
/ 2);
411 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
412 if (mp4ASC
.frameLengthFlag
)
413 #ifdef ALLOW_SMALL_FRAMELENGTH
414 hDecoder
->frameLength
= 960;
419 /* must be done before frameLength is divided by 2 for LD */
421 if (hDecoder
->object_type
== SSR
)
422 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
425 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
428 if (hDecoder
->object_type
== LD
)
429 hDecoder
->frameLength
>>= 1;
436 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
439 if (hDecoder
== NULL
)
440 return 1; /* error */
442 NeAACDecClose(*hDecoder
);
444 *hDecoder
= NeAACDecOpen();
446 /* Special object type defined for DRM */
447 (*hDecoder
)->config
.defObjectType
= DRM_ER_LC
;
449 (*hDecoder
)->config
.defSampleRate
= samplerate
;
450 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
451 (*hDecoder
)->aacSectionDataResilienceFlag
= 1; /* VCB11 */
452 (*hDecoder
)->aacScalefactorDataResilienceFlag
= 0; /* no RVLC */
453 (*hDecoder
)->aacSpectralDataResilienceFlag
= 1; /* HCR */
455 (*hDecoder
)->frameLength
= 960;
456 (*hDecoder
)->sf_index
= get_sr_index((*hDecoder
)->config
.defSampleRate
);
457 (*hDecoder
)->object_type
= (*hDecoder
)->config
.defObjectType
;
459 if ((channels
== DRMCH_STEREO
) || (channels
== DRMCH_SBR_STEREO
))
460 (*hDecoder
)->channelConfiguration
= 2;
462 (*hDecoder
)->channelConfiguration
= 1;
465 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
466 (*hDecoder
)->sbr_present_flag
= 0;
468 (*hDecoder
)->sbr_present_flag
= 1;
471 (*hDecoder
)->fb
= filter_bank_init((*hDecoder
)->frameLength
);
477 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
481 if (hDecoder
== NULL
)
485 printf("AAC decoder total: %I64d cycles\n", hDecoder
->cycles
);
486 printf("requant: %I64d cycles\n", hDecoder
->requant_cycles
);
487 printf("spectral_data: %I64d cycles\n", hDecoder
->spectral_cycles
);
488 printf("scalefactors: %I64d cycles\n", hDecoder
->scalefac_cycles
);
489 printf("output: %I64d cycles\n", hDecoder
->output_cycles
);
492 for (i
= 0; i
< MAX_CHANNELS
; i
++)
494 if (hDecoder
->time_out
[i
]) faad_free(hDecoder
->time_out
[i
]);
495 if (hDecoder
->fb_intermed
[i
]) faad_free(hDecoder
->fb_intermed
[i
]);
497 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
498 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
501 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
504 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
509 if (hDecoder
->object_type
== SSR
)
510 ssr_filter_bank_end(hDecoder
->fb
);
513 filter_bank_end(hDecoder
->fb
);
515 drc_end(hDecoder
->drc
);
517 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
520 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
522 if (hDecoder
->sbr
[i
])
523 sbrDecodeEnd(hDecoder
->sbr
[i
]);
527 if (hDecoder
) faad_free(hDecoder
);
530 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
534 hDecoder
->postSeekResetFlag
= 1;
537 hDecoder
->frame
= frame
;
541 static void create_channel_config(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
)
543 hInfo
->num_front_channels
= 0;
544 hInfo
->num_side_channels
= 0;
545 hInfo
->num_back_channels
= 0;
546 hInfo
->num_lfe_channels
= 0;
547 memset(hInfo
->channel_position
, 0, MAX_CHANNELS
*sizeof(uint8_t));
549 if (hDecoder
->downMatrix
)
551 hInfo
->num_front_channels
= 2;
552 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
553 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
557 /* check if there is a PCE */
558 if (hDecoder
->pce_set
)
560 uint8_t i
, chpos
= 0;
561 uint8_t chdir
, back_center
= 0;
563 hInfo
->num_front_channels
= hDecoder
->pce
.num_front_channels
;
564 hInfo
->num_side_channels
= hDecoder
->pce
.num_side_channels
;
565 hInfo
->num_back_channels
= hDecoder
->pce
.num_back_channels
;
566 hInfo
->num_lfe_channels
= hDecoder
->pce
.num_lfe_channels
;
568 chdir
= hInfo
->num_front_channels
;
571 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
574 for (i
= 0; i
< chdir
; i
+= 2)
576 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_LEFT
;
577 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_RIGHT
;
580 for (i
= 0; i
< hInfo
->num_side_channels
; i
+= 2)
582 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_LEFT
;
583 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_RIGHT
;
586 chdir
= hInfo
->num_back_channels
;
592 for (i
= 0; i
< chdir
; i
+= 2)
594 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
595 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
599 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_CENTER
;
602 for (i
= 0; i
< hInfo
->num_lfe_channels
; i
++)
604 hInfo
->channel_position
[chpos
++] = LFE_CHANNEL
;
608 switch (hDecoder
->channelConfiguration
)
611 hInfo
->num_front_channels
= 1;
612 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
615 hInfo
->num_front_channels
= 2;
616 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
617 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
620 hInfo
->num_front_channels
= 3;
621 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
622 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
623 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
626 hInfo
->num_front_channels
= 3;
627 hInfo
->num_back_channels
= 1;
628 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
629 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
630 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
631 hInfo
->channel_position
[3] = BACK_CHANNEL_CENTER
;
634 hInfo
->num_front_channels
= 3;
635 hInfo
->num_back_channels
= 2;
636 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
637 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
638 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
639 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
640 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
643 hInfo
->num_front_channels
= 3;
644 hInfo
->num_back_channels
= 2;
645 hInfo
->num_lfe_channels
= 1;
646 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
647 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
648 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
649 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
650 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
651 hInfo
->channel_position
[5] = LFE_CHANNEL
;
654 hInfo
->num_front_channels
= 3;
655 hInfo
->num_side_channels
= 2;
656 hInfo
->num_back_channels
= 2;
657 hInfo
->num_lfe_channels
= 1;
658 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
659 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
660 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
661 hInfo
->channel_position
[3] = SIDE_CHANNEL_LEFT
;
662 hInfo
->channel_position
[4] = SIDE_CHANNEL_RIGHT
;
663 hInfo
->channel_position
[5] = BACK_CHANNEL_LEFT
;
664 hInfo
->channel_position
[6] = BACK_CHANNEL_RIGHT
;
665 hInfo
->channel_position
[7] = LFE_CHANNEL
;
667 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
670 uint8_t ch
= hDecoder
->fr_channels
- hDecoder
->has_lfe
;
671 if (ch
& 1) /* there's either a center front or a center back channel */
673 uint8_t ch1
= (ch
-1)/2;
674 if (hDecoder
->first_syn_ele
== ID_SCE
)
676 hInfo
->num_front_channels
= ch1
+ 1;
677 hInfo
->num_back_channels
= ch1
;
678 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
679 for (i
= 1; i
<= ch1
; i
+=2)
681 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
682 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
684 for (i
= ch1
+1; i
< ch
; i
+=2)
686 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
687 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
690 hInfo
->num_front_channels
= ch1
;
691 hInfo
->num_back_channels
= ch1
+ 1;
692 for (i
= 0; i
< ch1
; i
+=2)
694 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
695 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
697 for (i
= ch1
; i
< ch
-1; i
+=2)
699 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
700 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
702 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
705 uint8_t ch1
= (ch
)/2;
706 hInfo
->num_front_channels
= ch1
;
707 hInfo
->num_back_channels
= ch1
;
710 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
711 for (i
= 1; i
<= ch1
; i
+=2)
713 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
714 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
716 for (i
= ch1
+1; i
< ch
-1; i
+=2)
718 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
719 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
721 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
723 for (i
= 0; i
< ch1
; i
+=2)
725 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
726 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
728 for (i
= ch1
; i
< ch
; i
+=2)
730 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
731 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
735 hInfo
->num_lfe_channels
= hDecoder
->has_lfe
;
736 for (i
= ch
; i
< hDecoder
->fr_channels
; i
++)
738 hInfo
->channel_position
[i
] = LFE_CHANNEL
;
746 void* NEAACDECAPI
NeAACDecDecode(NeAACDecHandle hDecoder
,
747 NeAACDecFrameInfo
*hInfo
,
748 uint8_t *buffer
, uint32_t buffer_size
)
750 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
, NULL
, 0);
753 void* NEAACDECAPI
NeAACDecDecode2(NeAACDecHandle hDecoder
,
754 NeAACDecFrameInfo
*hInfo
,
755 uint8_t *buffer
, uint32_t buffer_size
,
756 void **sample_buffer
, uint32_t sample_buffer_size
)
758 if ((sample_buffer
== NULL
) || (sample_buffer_size
== 0))
764 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
,
765 sample_buffer
, sample_buffer_size
);
768 static void* aac_frame_decode(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
769 uint8_t *buffer
, uint32_t buffer_size
,
770 void **sample_buffer2
, uint32_t sample_buffer_size
)
772 uint8_t channels
= 0;
773 uint8_t output_channels
= 0;
775 uint32_t bitsconsumed
;
778 uint32_t startbit
=0, endbit
=0, payload_bits
=0;
781 int64_t count
= faad_get_ts();
785 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
791 printf("%d\n", buffer_size
*8);
794 frame_len
= hDecoder
->frameLength
;
797 memset(hInfo
, 0, sizeof(NeAACDecFrameInfo
));
798 memset(hDecoder
->internal_channel
, 0, MAX_CHANNELS
*sizeof(hDecoder
->internal_channel
[0]));
800 /* initialize the bitstream */
801 faad_initbits(&ld
, buffer
, buffer_size
);
806 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
810 buf
= faad_getbitbuffer(&ld
, 32);
811 //temp = getdword((void*)buf);
812 temp
= *((uint32_t*)buf
);
813 printf("0x%.8X\n", temp
);
817 faad_initbits(&ld
, buffer
, buffer_size
);
821 if(hDecoder
->latm_header_present
)
823 payload_bits
= faad_latm_frame(&hDecoder
->latm_config
, &ld
);
824 startbit
= faad_get_processed_bits(&ld
);
825 if(payload_bits
== -1U)
833 if (hDecoder
->object_type
== DRM_ER_LC
)
835 /* We do not support stereo right now */
836 if (0) //(hDecoder->channelConfiguration == 2)
838 hInfo
->error
= 8; // Throw CRC error
843 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
847 if (hDecoder
->adts_header_present
)
851 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
852 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
855 /* MPEG2 does byte_alignment() here,
856 * but ADTS header is always multiple of 8 bits in MPEG2
857 * so not needed to actually do it.
865 /* decode the complete bitstream */
867 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
869 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
872 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
877 if(hDecoder
->latm_header_present
)
879 endbit
= faad_get_processed_bits(&ld
);
880 if(endbit
-startbit
> payload_bits
)
881 fprintf(stderr
, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
882 endbit
-startbit
, payload_bits
);
883 if(hDecoder
->latm_config
.otherDataLenBits
> 0)
884 faad_getbits(&ld
, hDecoder
->latm_config
.otherDataLenBits
);
885 faad_byte_align(&ld
);
888 channels
= hDecoder
->fr_channels
;
890 if (hInfo
->error
> 0)
894 if (channels
== 0 || channels
> MAX_CHANNELS
)
896 /* invalid number of channels */
901 /* no more bit reading after this */
902 bitsconsumed
= faad_get_processed_bits(&ld
);
903 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
912 if (!hDecoder
->adts_header_present
&& !hDecoder
->adif_header_present
&& !hDecoder
->latm_header_present
)
914 if (hDecoder
->channelConfiguration
== 0)
915 hDecoder
->channelConfiguration
= channels
;
917 if (channels
== 8) /* 7.1 */
918 hDecoder
->channelConfiguration
= 7;
919 if (channels
== 7) /* not a standard channelConfiguration */
920 hDecoder
->channelConfiguration
= 0;
923 if ((channels
== 5 || channels
== 6) && hDecoder
->config
.downMatrix
)
925 hDecoder
->downMatrix
= 1;
928 output_channels
= channels
;
931 #if (defined(PS_DEC) || defined(DRM_PS))
932 hDecoder
->upMatrix
= 0;
933 /* check if we have a mono file */
934 if (output_channels
== 1)
936 /* upMatrix to 2 channels for implicit signalling of PS */
937 hDecoder
->upMatrix
= 1;
942 /* Make a channel configuration based on either a PCE or a channelConfiguration */
943 create_channel_config(hDecoder
, hInfo
);
945 /* number of samples in this frame */
946 hInfo
->samples
= frame_len
*output_channels
;
947 /* number of channels in this frame */
948 hInfo
->channels
= output_channels
;
950 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
952 hInfo
->object_type
= hDecoder
->object_type
;
956 hInfo
->header_type
= RAW
;
957 if (hDecoder
->adif_header_present
)
958 hInfo
->header_type
= ADIF
;
959 if (hDecoder
->adts_header_present
)
960 hInfo
->header_type
= ADTS
;
961 if (hDecoder
->latm_header_present
)
962 hInfo
->header_type
= LATM
;
963 #if (defined(PS_DEC) || defined(DRM_PS))
964 hInfo
->ps
= hDecoder
->ps_used_global
;
967 /* check if frame has channel elements */
974 /* allocate the buffer for the final samples */
975 if ((hDecoder
->sample_buffer
== NULL
) ||
976 (hDecoder
->alloced_channels
!= output_channels
))
978 static const uint8_t str
[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
979 sizeof(float32_t
), sizeof(double), sizeof(int16_t), sizeof(int16_t),
980 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
982 uint8_t stride
= str
[hDecoder
->config
.outputFormat
-1];
984 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
989 /* check if we want to use internal sample_buffer */
990 if (sample_buffer_size
== 0)
992 if (hDecoder
->sample_buffer
)
993 faad_free(hDecoder
->sample_buffer
);
994 hDecoder
->sample_buffer
= NULL
;
995 hDecoder
->sample_buffer
= faad_malloc(frame_len
*output_channels
*stride
);
996 } else if (sample_buffer_size
< frame_len
*output_channels
*stride
) {
997 /* provided sample buffer is not big enough */
1001 hDecoder
->alloced_channels
= output_channels
;
1004 if (sample_buffer_size
== 0)
1006 sample_buffer
= hDecoder
->sample_buffer
;
1008 sample_buffer
= *sample_buffer2
;
1012 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
1016 /* this data is different when SBR is used or when the data is upsampled */
1017 if (!hDecoder
->downSampledSBR
)
1020 hInfo
->samples
*= 2;
1021 hInfo
->samplerate
*= 2;
1024 /* check if every element was provided with SBR data */
1025 for (ele
= 0; ele
< hDecoder
->fr_ch_ele
; ele
++)
1027 if (hDecoder
->sbr
[ele
] == NULL
)
1035 if (hDecoder
->sbr_present_flag
== 1)
1037 hInfo
->object_type
= HE_AAC
;
1038 hInfo
->sbr
= SBR_UPSAMPLED
;
1040 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
1042 if (hDecoder
->downSampledSBR
)
1044 hInfo
->sbr
= SBR_DOWNSAMPLED
;
1049 sample_buffer
= output_to_PCM(hDecoder
, hDecoder
->time_out
, sample_buffer
,
1050 output_channels
, frame_len
, hDecoder
->config
.outputFormat
);
1053 hDecoder
->postSeekResetFlag
= 0;
1057 if (hDecoder
->object_type
!= LD
)
1060 if (hDecoder
->frame
<= 1)
1064 /* LD encoders will give lower delay */
1065 if (hDecoder
->frame
<= 0)
1076 count
= faad_get_ts() - count
;
1077 hDecoder
->cycles
+= count
;
1080 return sample_buffer
;