2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: decoder.c,v 1.117 2009/02/05 00:51:03 menno Exp $
56 /* static function declarations */
57 static void* aac_frame_decode(NeAACDecStruct *hDecoder,
58 NeAACDecFrameInfo *hInfo,
59 unsigned char *buffer,
60 unsigned long buffer_size,
61 void **sample_buffer2,
62 unsigned long sample_buffer_size);
63 static void create_channel_config(NeAACDecStruct *hDecoder,
64 NeAACDecFrameInfo *hInfo);
67 char* NEAACDECAPI NeAACDecGetErrorMessage(unsigned char errcode)
69 if (errcode >= NUM_ERROR_MESSAGES)
71 return err_msg[errcode];
74 unsigned long NEAACDECAPI NeAACDecGetCapabilities(void)
78 /* can't do without it */
90 #ifdef ERROR_RESILIENCE
91 cap += ERROR_RESILIENCE_CAP;
94 cap += FIXED_POINT_CAP;
100 const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 };
101 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
104 NeAACDecStruct *hDecoder = NULL;
106 if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
109 memset(hDecoder, 0, sizeof(NeAACDecStruct));
111 hDecoder->cmes = mes;
112 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
113 hDecoder->config.defObjectType = MAIN;
114 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
115 hDecoder->config.downMatrix = 0;
116 hDecoder->adts_header_present = 0;
117 hDecoder->adif_header_present = 0;
118 hDecoder->latm_header_present = 0;
119 #ifdef ERROR_RESILIENCE
120 hDecoder->aacSectionDataResilienceFlag = 0;
121 hDecoder->aacScalefactorDataResilienceFlag = 0;
122 hDecoder->aacSpectralDataResilienceFlag = 0;
124 hDecoder->frameLength = 1024;
127 hDecoder->sample_buffer = NULL;
132 for (i = 0; i < MAX_CHANNELS; i++)
134 hDecoder->window_shape_prev[i] = 0;
135 hDecoder->time_out[i] = NULL;
136 hDecoder->fb_intermed[i] = NULL;
138 hDecoder->ssr_overlap[i] = NULL;
139 hDecoder->prev_fmd[i] = NULL;
142 hDecoder->pred_stat[i] = NULL;
145 hDecoder->ltp_lag[i] = 0;
146 hDecoder->lt_pred_stat[i] = NULL;
151 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
153 hDecoder->sbr[i] = NULL;
157 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
162 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
164 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
167 NeAACDecConfigurationPtr config = &(hDecoder->config);
175 unsigned char NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
176 NeAACDecConfigurationPtr config)
178 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
179 if (hDecoder && config)
181 /* check if we can decode this object type */
182 if (can_decode_ot(config->defObjectType) < 0)
184 hDecoder->config.defObjectType = config->defObjectType;
186 /* samplerate: anything but 0 should be possible */
187 if (config->defSampleRate == 0)
189 hDecoder->config.defSampleRate = config->defSampleRate;
191 /* check output format */
193 if ((config->outputFormat < 1) || (config->outputFormat > 4))
196 if ((config->outputFormat < 1) || (config->outputFormat > 5))
199 hDecoder->config.outputFormat = config->outputFormat;
201 if (config->downMatrix > 1)
203 hDecoder->config.downMatrix = config->downMatrix;
213 static int latmCheck(latm_header *latm, bitfile *ld)
215 uint32_t good=0, bad=0, bits, m;
217 while (ld->bytes_left)
219 bits = faad_latm_frame(latm, ld);
238 long NEAACDECAPI NeAACDecInit(NeAACDecHandle hpDecoder,
239 unsigned char *buffer,
240 unsigned long buffer_size,
241 unsigned long *samplerate,
242 unsigned char *channels)
248 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
251 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
254 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
255 hDecoder->object_type = hDecoder->config.defObjectType;
256 *samplerate = get_sample_rate(hDecoder->sf_index);
263 latm_header *l = &hDecoder->latm_config;
266 faad_initbits(&ld, buffer, buffer_size);
269 memset(l, 0, sizeof(latm_header));
270 is_latm = latmCheck(l, &ld);
273 faad_rewindbits(&ld);
274 if(is_latm && l->ASCbits>0)
277 hDecoder->latm_header_present = 1;
278 x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
280 hDecoder->latm_header_present = 0;
284 /* Check if an ADIF header is present */
285 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
286 (buffer[2] == 'I') && (buffer[3] == 'F'))
288 hDecoder->adif_header_present = 1;
290 get_adif_header(&adif, &ld);
291 faad_byte_align(&ld);
293 hDecoder->sf_index = adif.pce[0].sf_index;
294 hDecoder->object_type = adif.pce[0].object_type + 1;
296 *samplerate = get_sample_rate(hDecoder->sf_index);
297 *channels = adif.pce[0].channels;
299 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
300 hDecoder->pce_set = 1;
302 bits = bit2byte(faad_get_processed_bits(&ld));
304 /* Check if an ADTS header is present */
305 } else if (faad_showbits(&ld, 12) == 0xfff) {
306 hDecoder->adts_header_present = 1;
308 adts.old_format = hDecoder->config.useOldADTSFormat;
309 adts_frame(&adts, &ld);
311 hDecoder->sf_index = adts.sf_index;
312 hDecoder->object_type = adts.profile + 1;
314 *samplerate = get_sample_rate(hDecoder->sf_index);
315 *channels = (adts.channel_configuration > 6) ?
316 2 : adts.channel_configuration;
327 #if (defined(PS_DEC) || defined(DRM_PS))
328 /* check if we have a mono file */
331 /* upMatrix to 2 channels for implicit signalling of PS */
336 hDecoder->channelConfiguration = *channels;
339 /* implicit signalling */
340 if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
343 hDecoder->forceUpSampling = 1;
344 } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
345 hDecoder->downSampledSBR = 1;
349 /* must be done before frameLength is divided by 2 for LD */
351 if (hDecoder->object_type == SSR)
352 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
355 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
358 if (hDecoder->object_type == LD)
359 hDecoder->frameLength >>= 1;
362 if (can_decode_ot(hDecoder->object_type) < 0)
368 /* Init the library using a DecoderSpecificInfo */
369 char NEAACDECAPI NeAACDecInit2(NeAACDecHandle hpDecoder,
370 unsigned char *pBuffer,
371 unsigned long SizeOfDecoderSpecificInfo,
372 unsigned long *samplerate,
373 unsigned char *channels)
375 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
377 mp4AudioSpecificConfig mp4ASC;
379 if((hDecoder == NULL)
381 || (SizeOfDecoderSpecificInfo < 2)
382 || (samplerate == NULL)
383 || (channels == NULL))
388 hDecoder->adif_header_present = 0;
389 hDecoder->adts_header_present = 0;
391 /* decode the audio specific config */
392 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
393 &(hDecoder->pce), hDecoder->latm_header_present);
395 /* copy the relevant info to the decoder handle */
396 *samplerate = mp4ASC.samplingFrequency;
397 if (mp4ASC.channelsConfiguration)
399 *channels = mp4ASC.channelsConfiguration;
401 *channels = hDecoder->pce.channels;
402 hDecoder->pce_set = 1;
404 #if (defined(PS_DEC) || defined(DRM_PS))
405 /* check if we have a mono file */
408 /* upMatrix to 2 channels for implicit signalling of PS */
412 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
413 hDecoder->object_type = mp4ASC.objectTypeIndex;
414 #ifdef ERROR_RESILIENCE
415 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
416 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
417 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
420 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
421 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
422 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
423 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
425 hDecoder->forceUpSampling = 0;
427 /* AAC core decoder samplerate is 2 times as low */
428 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
430 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
438 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
439 if (mp4ASC.frameLengthFlag)
440 #ifdef ALLOW_SMALL_FRAMELENGTH
441 hDecoder->frameLength = 960;
446 /* must be done before frameLength is divided by 2 for LD */
448 if (hDecoder->object_type == SSR)
449 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
452 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
455 if (hDecoder->object_type == LD)
456 hDecoder->frameLength >>= 1;
463 char NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
464 unsigned long samplerate,
465 unsigned char channels)
467 NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
468 if (hDecoder == NULL)
469 return 1; /* error */
471 NeAACDecClose(*hDecoder);
473 *hDecoder = NeAACDecOpen();
475 /* Special object type defined for DRM */
476 (*hDecoder)->config.defObjectType = DRM_ER_LC;
478 (*hDecoder)->config.defSampleRate = samplerate;
479 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
480 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
481 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
482 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
484 (*hDecoder)->frameLength = 960;
485 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
486 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
488 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
489 (*hDecoder)->channelConfiguration = 2;
491 (*hDecoder)->channelConfiguration = 1;
494 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
495 (*hDecoder)->sbr_present_flag = 0;
497 (*hDecoder)->sbr_present_flag = 1;
500 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
506 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hpDecoder)
509 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
511 if (hDecoder == NULL)
515 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
516 printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
517 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
518 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
519 printf("output: %I64d cycles\n", hDecoder->output_cycles);
522 for (i = 0; i < MAX_CHANNELS; i++)
524 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
525 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
527 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
528 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
531 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
534 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
539 if (hDecoder->object_type == SSR)
540 ssr_filter_bank_end(hDecoder->fb);
543 filter_bank_end(hDecoder->fb);
545 drc_end(hDecoder->drc);
547 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
550 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
552 if (hDecoder->sbr[i])
553 sbrDecodeEnd(hDecoder->sbr[i]);
557 if (hDecoder) faad_free(hDecoder);
560 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
562 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
565 hDecoder->postSeekResetFlag = 1;
568 hDecoder->frame = frame;
572 static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
574 hInfo->num_front_channels = 0;
575 hInfo->num_side_channels = 0;
576 hInfo->num_back_channels = 0;
577 hInfo->num_lfe_channels = 0;
578 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
580 if (hDecoder->downMatrix)
582 hInfo->num_front_channels = 2;
583 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
584 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
588 /* check if there is a PCE */
589 if (hDecoder->pce_set)
591 uint8_t i, chpos = 0;
592 uint8_t chdir, back_center = 0;
594 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
595 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
596 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
597 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
599 chdir = hInfo->num_front_channels;
602 #if (defined(PS_DEC) || defined(DRM_PS))
603 /* When PS is enabled output is always stereo */
604 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
605 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
607 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
611 for (i = 0; i < chdir; i += 2)
613 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
614 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
617 for (i = 0; i < hInfo->num_side_channels; i += 2)
619 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
620 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
623 chdir = hInfo->num_back_channels;
629 for (i = 0; i < chdir; i += 2)
631 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
632 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
636 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
639 for (i = 0; i < hInfo->num_lfe_channels; i++)
641 hInfo->channel_position[chpos++] = LFE_CHANNEL;
645 switch (hDecoder->channelConfiguration)
648 #if (defined(PS_DEC) || defined(DRM_PS))
649 /* When PS is enabled output is always stereo */
650 hInfo->num_front_channels = 2;
651 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
652 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
654 hInfo->num_front_channels = 1;
655 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
659 hInfo->num_front_channels = 2;
660 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
661 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
664 hInfo->num_front_channels = 3;
665 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
666 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
667 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
670 hInfo->num_front_channels = 3;
671 hInfo->num_back_channels = 1;
672 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
673 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
674 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
675 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
678 hInfo->num_front_channels = 3;
679 hInfo->num_back_channels = 2;
680 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
681 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
682 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
683 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
684 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
687 hInfo->num_front_channels = 3;
688 hInfo->num_back_channels = 2;
689 hInfo->num_lfe_channels = 1;
690 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
691 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
692 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
693 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
694 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
695 hInfo->channel_position[5] = LFE_CHANNEL;
698 hInfo->num_front_channels = 3;
699 hInfo->num_side_channels = 2;
700 hInfo->num_back_channels = 2;
701 hInfo->num_lfe_channels = 1;
702 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
703 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
704 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
705 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
706 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
707 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
708 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
709 hInfo->channel_position[7] = LFE_CHANNEL;
711 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
714 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
715 if (ch & 1) /* there's either a center front or a center back channel */
717 uint8_t ch1 = (ch-1)/2;
718 if (hDecoder->first_syn_ele == ID_SCE)
720 hInfo->num_front_channels = ch1 + 1;
721 hInfo->num_back_channels = ch1;
722 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
723 for (i = 1; 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+1; i < ch; i+=2)
730 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
731 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
734 hInfo->num_front_channels = ch1;
735 hInfo->num_back_channels = ch1 + 1;
736 for (i = 0; i < ch1; i+=2)
738 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
739 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
741 for (i = ch1; i < ch-1; i+=2)
743 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
744 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
746 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
749 uint8_t ch1 = (ch)/2;
750 hInfo->num_front_channels = ch1;
751 hInfo->num_back_channels = ch1;
754 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
755 for (i = 1; i <= ch1; i+=2)
757 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
758 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
760 for (i = ch1+1; i < ch-1; i+=2)
762 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
763 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
765 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
767 for (i = 0; i < ch1; i+=2)
769 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
772 for (i = ch1; i < ch; i+=2)
774 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
779 hInfo->num_lfe_channels = hDecoder->has_lfe;
780 for (i = ch; i < hDecoder->fr_channels; i++)
782 hInfo->channel_position[i] = LFE_CHANNEL;
790 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hpDecoder,
791 NeAACDecFrameInfo *hInfo,
792 unsigned char *buffer,
793 unsigned long buffer_size)
795 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
796 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
799 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hpDecoder,
800 NeAACDecFrameInfo *hInfo,
801 unsigned char *buffer,
802 unsigned long buffer_size,
803 void **sample_buffer,
804 unsigned long sample_buffer_size)
806 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
807 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
813 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
814 sample_buffer, sample_buffer_size);
819 #define ERROR_STATE_INIT 6
821 static void conceal_output(NeAACDecStruct *hDecoder, uint16_t frame_len,
822 uint8_t out_ch, void *sample_buffer)
828 static void* aac_frame_decode(NeAACDecStruct *hDecoder,
829 NeAACDecFrameInfo *hInfo,
830 unsigned char *buffer,
831 unsigned long buffer_size,
832 void **sample_buffer2,
833 unsigned long sample_buffer_size)
836 uint8_t channels = 0;
837 uint8_t output_channels = 0;
839 uint32_t bitsconsumed;
842 uint32_t startbit=0, endbit=0, payload_bits=0;
845 int64_t count = faad_get_ts();
849 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
855 printf("%d\n", buffer_size*8);
858 frame_len = hDecoder->frameLength;
861 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
862 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
864 #ifdef USE_TIME_LIMIT
865 if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
867 hDecoder->TL_count += 1024;
869 hInfo->error = (NUM_ERROR_MESSAGES-1);
875 /* check for some common metadata tag types in the bitstream
876 * No need to return an error
879 if (buffer_size >= 128)
881 if (memcmp(buffer, "TAG", 3) == 0)
884 hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
885 /* no error, but no output either */
891 /* initialize the bitstream */
892 faad_initbits(&ld, buffer, buffer_size);
897 for (i = 0; i < ((buffer_size+3)>>2); i++)
901 buf = faad_getbitbuffer(&ld, 32);
902 //temp = getdword((void*)buf);
903 temp = *((uint32_t*)buf);
904 printf("0x%.8X\n", temp);
908 faad_initbits(&ld, buffer, buffer_size);
913 if(hDecoder->latm_header_present)
915 payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
916 startbit = faad_get_processed_bits(&ld);
917 if(payload_bits == -1U)
926 if (hDecoder->object_type == DRM_ER_LC)
928 /* We do not support stereo right now */
929 if (0) //(hDecoder->channelConfiguration == 2)
931 hInfo->error = 28; // Throw CRC error
936 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
940 if (hDecoder->adts_header_present)
944 adts.old_format = hDecoder->config.useOldADTSFormat;
945 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
948 /* MPEG2 does byte_alignment() here,
949 * but ADTS header is always multiple of 8 bits in MPEG2
950 * so not needed to actually do it.
958 /* decode the complete bitstream */
960 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC))
962 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
965 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
971 if(hDecoder->latm_header_present)
973 endbit = faad_get_processed_bits(&ld);
974 if(endbit-startbit > payload_bits)
975 fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
976 endbit-startbit, payload_bits);
977 if(hDecoder->latm_config.otherDataLenBits > 0)
978 faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
979 faad_byte_align(&ld);
983 channels = hDecoder->fr_channels;
985 if (hInfo->error > 0)
989 if (channels == 0 || channels > MAX_CHANNELS)
991 /* invalid number of channels */
996 /* no more bit reading after this */
997 bitsconsumed = faad_get_processed_bits(&ld);
998 hInfo->bytesconsumed = bit2byte(bitsconsumed);
1007 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1009 && !hDecoder->latm_header_present
1013 if (hDecoder->channelConfiguration == 0)
1014 hDecoder->channelConfiguration = channels;
1016 if (channels == 8) /* 7.1 */
1017 hDecoder->channelConfiguration = 7;
1018 if (channels == 7) /* not a standard channelConfiguration */
1019 hDecoder->channelConfiguration = 0;
1022 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1024 hDecoder->downMatrix = 1;
1025 output_channels = 2;
1027 output_channels = channels;
1030 #if (defined(PS_DEC) || defined(DRM_PS))
1031 hDecoder->upMatrix = 0;
1032 /* check if we have a mono file */
1033 if (output_channels == 1)
1035 /* upMatrix to 2 channels for implicit signalling of PS */
1036 hDecoder->upMatrix = 1;
1037 output_channels = 2;
1041 /* Make a channel configuration based on either a PCE or a channelConfiguration */
1042 create_channel_config(hDecoder, hInfo);
1044 /* number of samples in this frame */
1045 hInfo->samples = frame_len*output_channels;
1046 /* number of channels in this frame */
1047 hInfo->channels = output_channels;
1049 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1051 hInfo->object_type = hDecoder->object_type;
1053 hInfo->sbr = NO_SBR;
1055 hInfo->header_type = RAW;
1056 if (hDecoder->adif_header_present)
1057 hInfo->header_type = ADIF;
1058 if (hDecoder->adts_header_present)
1059 hInfo->header_type = ADTS;
1061 if (hDecoder->latm_header_present)
1062 hInfo->header_type = LATM;
1064 #if (defined(PS_DEC) || defined(DRM_PS))
1065 hInfo->ps = hDecoder->ps_used_global;
1068 /* check if frame has channel elements */
1075 /* allocate the buffer for the final samples */
1076 if ((hDecoder->sample_buffer == NULL) ||
1077 (hDecoder->alloced_channels != output_channels))
1079 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1080 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1081 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1083 uint8_t stride = str[hDecoder->config.outputFormat-1];
1085 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1087 stride = 2 * stride;
1090 /* check if we want to use internal sample_buffer */
1091 if (sample_buffer_size == 0)
1093 if (hDecoder->sample_buffer)
1094 faad_free(hDecoder->sample_buffer);
1095 hDecoder->sample_buffer = NULL;
1096 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
1097 } else if (sample_buffer_size < frame_len*output_channels*stride) {
1098 /* provided sample buffer is not big enough */
1102 hDecoder->alloced_channels = output_channels;
1105 if (sample_buffer_size == 0)
1107 sample_buffer = hDecoder->sample_buffer;
1109 sample_buffer = *sample_buffer2;
1113 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1117 /* this data is different when SBR is used or when the data is upsampled */
1118 if (!hDecoder->downSampledSBR)
1121 hInfo->samples *= 2;
1122 hInfo->samplerate *= 2;
1125 /* check if every element was provided with SBR data */
1126 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1128 if (hDecoder->sbr[ele] == NULL)
1136 if (hDecoder->sbr_present_flag == 1)
1138 hInfo->object_type = HE_AAC;
1139 hInfo->sbr = SBR_UPSAMPLED;
1141 hInfo->sbr = NO_SBR_UPSAMPLED;
1143 if (hDecoder->downSampledSBR)
1145 hInfo->sbr = SBR_DOWNSAMPLED;
1151 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1152 output_channels, frame_len, hDecoder->config.outputFormat);
1156 //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1160 hDecoder->postSeekResetFlag = 0;
1164 if (hDecoder->object_type != LD)
1167 if (hDecoder->frame <= 1)
1171 /* LD encoders will give lower delay */
1172 if (hDecoder->frame <= 0)
1183 count = faad_get_ts() - count;
1184 hDecoder->cycles += count;
1187 return sample_buffer;
1193 hDecoder->error_state = ERROR_STATE_INIT;
1196 /* reset filterbank state */
1197 for (i = 0; i < MAX_CHANNELS; i++)
1199 if (hDecoder->fb_intermed[i] != NULL)
1201 memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1205 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1207 if (hDecoder->sbr[i] != NULL)
1209 sbrReset(hDecoder->sbr[i]);