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 #ifdef ERROR_RESILIENCE
110 hDecoder
->aacSectionDataResilienceFlag
= 0;
111 hDecoder
->aacScalefactorDataResilienceFlag
= 0;
112 hDecoder
->aacSpectralDataResilienceFlag
= 0;
114 hDecoder
->frameLength
= 1024;
117 hDecoder
->sample_buffer
= NULL
;
119 for (i
= 0; i
< MAX_CHANNELS
; i
++)
121 hDecoder
->window_shape_prev
[i
] = 0;
122 hDecoder
->time_out
[i
] = NULL
;
123 hDecoder
->fb_intermed
[i
] = NULL
;
125 hDecoder
->ssr_overlap
[i
] = NULL
;
126 hDecoder
->prev_fmd
[i
] = NULL
;
129 hDecoder
->pred_stat
[i
] = NULL
;
132 hDecoder
->ltp_lag
[i
] = 0;
133 hDecoder
->lt_pred_stat
[i
] = NULL
;
138 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
140 hDecoder
->sbr
[i
] = NULL
;
144 hDecoder
->drc
= drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
149 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
153 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
161 uint8_t NEAACDECAPI
NeAACDecSetConfiguration(NeAACDecHandle hDecoder
,
162 NeAACDecConfigurationPtr config
)
164 if (hDecoder
&& config
)
166 /* check if we can decode this object type */
167 if (can_decode_ot(config
->defObjectType
) < 0)
169 hDecoder
->config
.defObjectType
= config
->defObjectType
;
171 /* samplerate: anything but 0 should be possible */
172 if (config
->defSampleRate
== 0)
174 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
176 /* check output format */
178 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
181 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
184 hDecoder
->config
.outputFormat
= config
->outputFormat
;
186 if (config
->downMatrix
> 1)
188 hDecoder
->config
.downMatrix
= config
->downMatrix
;
197 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
198 uint32_t buffer_size
,
199 uint32_t *samplerate
, uint8_t *channels
)
206 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
209 hDecoder
->sf_index
= get_sr_index(hDecoder
->config
.defSampleRate
);
210 hDecoder
->object_type
= hDecoder
->config
.defObjectType
;
211 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
216 faad_initbits(&ld
, buffer
, buffer_size
);
218 /* Check if an ADIF header is present */
219 if ((buffer
[0] == 'A') && (buffer
[1] == 'D') &&
220 (buffer
[2] == 'I') && (buffer
[3] == 'F'))
222 hDecoder
->adif_header_present
= 1;
224 get_adif_header(&adif
, &ld
);
225 faad_byte_align(&ld
);
227 hDecoder
->sf_index
= adif
.pce
[0].sf_index
;
228 hDecoder
->object_type
= adif
.pce
[0].object_type
+ 1;
230 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
231 *channels
= adif
.pce
[0].channels
;
233 memcpy(&(hDecoder
->pce
), &(adif
.pce
[0]), sizeof(program_config
));
234 hDecoder
->pce_set
= 1;
236 bits
= bit2byte(faad_get_processed_bits(&ld
));
238 /* Check if an ADTS header is present */
239 } else if (faad_showbits(&ld
, 12) == 0xfff) {
240 hDecoder
->adts_header_present
= 1;
242 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
243 adts_frame(&adts
, &ld
);
245 hDecoder
->sf_index
= adts
.sf_index
;
246 hDecoder
->object_type
= adts
.profile
+ 1;
248 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
249 *channels
= (adts
.channel_configuration
> 6) ?
250 2 : adts
.channel_configuration
;
260 hDecoder
->channelConfiguration
= *channels
;
262 #if (defined(PS_DEC) || defined(DRM_PS))
263 /* check if we have a mono file */
266 /* upMatrix to 2 channels for implicit signalling of PS */
272 /* implicit signalling */
273 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
276 hDecoder
->forceUpSampling
= 1;
277 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
278 hDecoder
->downSampledSBR
= 1;
282 /* must be done before frameLength is divided by 2 for LD */
284 if (hDecoder
->object_type
== SSR
)
285 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
288 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
291 if (hDecoder
->object_type
== LD
)
292 hDecoder
->frameLength
>>= 1;
295 if (can_decode_ot(hDecoder
->object_type
) < 0)
301 /* Init the library using a DecoderSpecificInfo */
302 int8_t NEAACDECAPI
NeAACDecInit2(NeAACDecHandle hDecoder
, uint8_t *pBuffer
,
303 uint32_t SizeOfDecoderSpecificInfo
,
304 uint32_t *samplerate
, uint8_t *channels
)
307 mp4AudioSpecificConfig mp4ASC
;
309 if((hDecoder
== NULL
)
311 || (SizeOfDecoderSpecificInfo
< 2)
312 || (samplerate
== NULL
)
313 || (channels
== NULL
))
318 hDecoder
->adif_header_present
= 0;
319 hDecoder
->adts_header_present
= 0;
321 /* decode the audio specific config */
322 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
325 /* copy the relevant info to the decoder handle */
326 *samplerate
= mp4ASC
.samplingFrequency
;
327 if (mp4ASC
.channelsConfiguration
)
329 *channels
= mp4ASC
.channelsConfiguration
;
331 *channels
= hDecoder
->pce
.channels
;
332 hDecoder
->pce_set
= 1;
334 #if (defined(PS_DEC) || defined(DRM_PS))
335 /* check if we have a mono file */
338 /* upMatrix to 2 channels for implicit signalling of PS */
342 hDecoder
->sf_index
= mp4ASC
.samplingFrequencyIndex
;
343 hDecoder
->object_type
= mp4ASC
.objectTypeIndex
;
344 #ifdef ERROR_RESILIENCE
345 hDecoder
->aacSectionDataResilienceFlag
= mp4ASC
.aacSectionDataResilienceFlag
;
346 hDecoder
->aacScalefactorDataResilienceFlag
= mp4ASC
.aacScalefactorDataResilienceFlag
;
347 hDecoder
->aacSpectralDataResilienceFlag
= mp4ASC
.aacSpectralDataResilienceFlag
;
350 hDecoder
->sbr_present_flag
= mp4ASC
.sbr_present_flag
;
351 hDecoder
->downSampledSBR
= mp4ASC
.downSampledSBR
;
352 if (hDecoder
->config
.dontUpSampleImplicitSBR
== 0)
353 hDecoder
->forceUpSampling
= mp4ASC
.forceUpSampling
;
355 hDecoder
->forceUpSampling
= 0;
357 /* AAC core decoder samplerate is 2 times as low */
358 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || hDecoder
->forceUpSampling
== 1)
360 hDecoder
->sf_index
= get_sr_index(mp4ASC
.samplingFrequency
/ 2);
368 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
369 if (mp4ASC
.frameLengthFlag
)
370 #ifdef ALLOW_SMALL_FRAMELENGTH
371 hDecoder
->frameLength
= 960;
376 /* must be done before frameLength is divided by 2 for LD */
378 if (hDecoder
->object_type
== SSR
)
379 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
382 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
385 if (hDecoder
->object_type
== LD
)
386 hDecoder
->frameLength
>>= 1;
393 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
396 if (hDecoder
== NULL
)
397 return 1; /* error */
399 NeAACDecClose(*hDecoder
);
401 *hDecoder
= NeAACDecOpen();
403 /* Special object type defined for DRM */
404 (*hDecoder
)->config
.defObjectType
= DRM_ER_LC
;
406 (*hDecoder
)->config
.defSampleRate
= samplerate
;
407 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
408 (*hDecoder
)->aacSectionDataResilienceFlag
= 1; /* VCB11 */
409 (*hDecoder
)->aacScalefactorDataResilienceFlag
= 0; /* no RVLC */
410 (*hDecoder
)->aacSpectralDataResilienceFlag
= 1; /* HCR */
412 (*hDecoder
)->frameLength
= 960;
413 (*hDecoder
)->sf_index
= get_sr_index((*hDecoder
)->config
.defSampleRate
);
414 (*hDecoder
)->object_type
= (*hDecoder
)->config
.defObjectType
;
416 if ((channels
== DRMCH_STEREO
) || (channels
== DRMCH_SBR_STEREO
))
417 (*hDecoder
)->channelConfiguration
= 2;
419 (*hDecoder
)->channelConfiguration
= 1;
422 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
423 (*hDecoder
)->sbr_present_flag
= 0;
425 (*hDecoder
)->sbr_present_flag
= 1;
428 (*hDecoder
)->fb
= filter_bank_init((*hDecoder
)->frameLength
);
434 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
438 if (hDecoder
== NULL
)
442 printf("AAC decoder total: %I64d cycles\n", hDecoder
->cycles
);
443 printf("requant: %I64d cycles\n", hDecoder
->requant_cycles
);
444 printf("spectral_data: %I64d cycles\n", hDecoder
->spectral_cycles
);
445 printf("scalefactors: %I64d cycles\n", hDecoder
->scalefac_cycles
);
446 printf("output: %I64d cycles\n", hDecoder
->output_cycles
);
449 for (i
= 0; i
< MAX_CHANNELS
; i
++)
451 if (hDecoder
->time_out
[i
]) faad_free(hDecoder
->time_out
[i
]);
452 if (hDecoder
->fb_intermed
[i
]) faad_free(hDecoder
->fb_intermed
[i
]);
454 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
455 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
458 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
461 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
466 if (hDecoder
->object_type
== SSR
)
467 ssr_filter_bank_end(hDecoder
->fb
);
470 filter_bank_end(hDecoder
->fb
);
472 drc_end(hDecoder
->drc
);
474 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
477 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
479 if (hDecoder
->sbr
[i
])
480 sbrDecodeEnd(hDecoder
->sbr
[i
]);
484 if (hDecoder
) faad_free(hDecoder
);
487 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
491 hDecoder
->postSeekResetFlag
= 1;
494 hDecoder
->frame
= frame
;
498 static void create_channel_config(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
)
500 hInfo
->num_front_channels
= 0;
501 hInfo
->num_side_channels
= 0;
502 hInfo
->num_back_channels
= 0;
503 hInfo
->num_lfe_channels
= 0;
504 memset(hInfo
->channel_position
, 0, MAX_CHANNELS
*sizeof(uint8_t));
506 if (hDecoder
->downMatrix
)
508 hInfo
->num_front_channels
= 2;
509 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
510 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
514 /* check if there is a PCE */
515 if (hDecoder
->pce_set
)
517 uint8_t i
, chpos
= 0;
518 uint8_t chdir
, back_center
= 0;
520 hInfo
->num_front_channels
= hDecoder
->pce
.num_front_channels
;
521 hInfo
->num_side_channels
= hDecoder
->pce
.num_side_channels
;
522 hInfo
->num_back_channels
= hDecoder
->pce
.num_back_channels
;
523 hInfo
->num_lfe_channels
= hDecoder
->pce
.num_lfe_channels
;
525 chdir
= hInfo
->num_front_channels
;
528 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
531 for (i
= 0; i
< chdir
; i
+= 2)
533 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_LEFT
;
534 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_RIGHT
;
537 for (i
= 0; i
< hInfo
->num_side_channels
; i
+= 2)
539 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_LEFT
;
540 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_RIGHT
;
543 chdir
= hInfo
->num_back_channels
;
549 for (i
= 0; i
< chdir
; i
+= 2)
551 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
552 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
556 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_CENTER
;
559 for (i
= 0; i
< hInfo
->num_lfe_channels
; i
++)
561 hInfo
->channel_position
[chpos
++] = LFE_CHANNEL
;
565 switch (hDecoder
->channelConfiguration
)
568 hInfo
->num_front_channels
= 1;
569 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
572 hInfo
->num_front_channels
= 2;
573 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
574 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
577 hInfo
->num_front_channels
= 3;
578 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
579 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
580 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
583 hInfo
->num_front_channels
= 3;
584 hInfo
->num_back_channels
= 1;
585 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
586 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
587 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
588 hInfo
->channel_position
[3] = BACK_CHANNEL_CENTER
;
591 hInfo
->num_front_channels
= 3;
592 hInfo
->num_back_channels
= 2;
593 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
594 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
595 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
596 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
597 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
600 hInfo
->num_front_channels
= 3;
601 hInfo
->num_back_channels
= 2;
602 hInfo
->num_lfe_channels
= 1;
603 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
604 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
605 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
606 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
607 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
608 hInfo
->channel_position
[5] = LFE_CHANNEL
;
611 hInfo
->num_front_channels
= 3;
612 hInfo
->num_side_channels
= 2;
613 hInfo
->num_back_channels
= 2;
614 hInfo
->num_lfe_channels
= 1;
615 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
616 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
617 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
618 hInfo
->channel_position
[3] = SIDE_CHANNEL_LEFT
;
619 hInfo
->channel_position
[4] = SIDE_CHANNEL_RIGHT
;
620 hInfo
->channel_position
[5] = BACK_CHANNEL_LEFT
;
621 hInfo
->channel_position
[6] = BACK_CHANNEL_RIGHT
;
622 hInfo
->channel_position
[7] = LFE_CHANNEL
;
624 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
627 uint8_t ch
= hDecoder
->fr_channels
- hDecoder
->has_lfe
;
628 if (ch
& 1) /* there's either a center front or a center back channel */
630 uint8_t ch1
= (ch
-1)/2;
631 if (hDecoder
->first_syn_ele
== ID_SCE
)
633 hInfo
->num_front_channels
= ch1
+ 1;
634 hInfo
->num_back_channels
= ch1
;
635 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
636 for (i
= 1; i
<= ch1
; i
+=2)
638 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
639 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
641 for (i
= ch1
+1; i
< ch
; i
+=2)
643 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
644 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
647 hInfo
->num_front_channels
= ch1
;
648 hInfo
->num_back_channels
= ch1
+ 1;
649 for (i
= 0; i
< ch1
; i
+=2)
651 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
652 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
654 for (i
= ch1
; i
< ch
-1; i
+=2)
656 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
657 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
659 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
662 uint8_t ch1
= (ch
)/2;
663 hInfo
->num_front_channels
= ch1
;
664 hInfo
->num_back_channels
= ch1
;
667 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
668 for (i
= 1; i
<= ch1
; i
+=2)
670 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
671 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
673 for (i
= ch1
+1; i
< ch
-1; i
+=2)
675 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
676 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
678 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
680 for (i
= 0; i
< ch1
; i
+=2)
682 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
683 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
685 for (i
= ch1
; i
< ch
; i
+=2)
687 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
688 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
692 hInfo
->num_lfe_channels
= hDecoder
->has_lfe
;
693 for (i
= ch
; i
< hDecoder
->fr_channels
; i
++)
695 hInfo
->channel_position
[i
] = LFE_CHANNEL
;
703 void* NEAACDECAPI
NeAACDecDecode(NeAACDecHandle hDecoder
,
704 NeAACDecFrameInfo
*hInfo
,
705 uint8_t *buffer
, uint32_t buffer_size
)
707 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
, NULL
, 0);
710 void* NEAACDECAPI
NeAACDecDecode2(NeAACDecHandle hDecoder
,
711 NeAACDecFrameInfo
*hInfo
,
712 uint8_t *buffer
, uint32_t buffer_size
,
713 void **sample_buffer
, uint32_t sample_buffer_size
)
715 if ((sample_buffer
== NULL
) || (sample_buffer_size
== 0))
721 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
,
722 sample_buffer
, sample_buffer_size
);
725 static void* aac_frame_decode(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
726 uint8_t *buffer
, uint32_t buffer_size
,
727 void **sample_buffer2
, uint32_t sample_buffer_size
)
729 uint8_t channels
= 0;
730 uint8_t output_channels
= 0;
732 uint32_t bitsconsumed
;
737 int64_t count
= faad_get_ts();
741 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
747 printf("%d\n", buffer_size
*8);
750 frame_len
= hDecoder
->frameLength
;
753 memset(hInfo
, 0, sizeof(NeAACDecFrameInfo
));
754 memset(hDecoder
->internal_channel
, 0, MAX_CHANNELS
*sizeof(hDecoder
->internal_channel
[0]));
756 /* initialize the bitstream */
757 faad_initbits(&ld
, buffer
, buffer_size
);
762 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
766 buf
= faad_getbitbuffer(&ld
, 32);
767 //temp = getdword((void*)buf);
768 temp
= *((uint32_t*)buf
);
769 printf("0x%.8X\n", temp
);
773 faad_initbits(&ld
, buffer
, buffer_size
);
778 if (hDecoder
->object_type
== DRM_ER_LC
)
780 /* We do not support stereo right now */
781 if (0) //(hDecoder->channelConfiguration == 2)
783 hInfo
->error
= 8; // Throw CRC error
788 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
792 if (hDecoder
->adts_header_present
)
796 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
797 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
800 /* MPEG2 does byte_alignment() here,
801 * but ADTS header is always multiple of 8 bits in MPEG2
802 * so not needed to actually do it.
810 /* decode the complete bitstream */
812 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
814 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
817 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
822 channels
= hDecoder
->fr_channels
;
824 if (hInfo
->error
> 0)
828 if (channels
== 0 || channels
> MAX_CHANNELS
)
830 /* invalid number of channels */
835 /* no more bit reading after this */
836 bitsconsumed
= faad_get_processed_bits(&ld
);
837 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
846 if (!hDecoder
->adts_header_present
&& !hDecoder
->adif_header_present
)
848 if (hDecoder
->channelConfiguration
== 0)
849 hDecoder
->channelConfiguration
= channels
;
851 if (channels
== 8) /* 7.1 */
852 hDecoder
->channelConfiguration
= 7;
853 if (channels
== 7) /* not a standard channelConfiguration */
854 hDecoder
->channelConfiguration
= 0;
857 if ((channels
== 5 || channels
== 6) && hDecoder
->config
.downMatrix
)
859 hDecoder
->downMatrix
= 1;
862 output_channels
= channels
;
865 #if (defined(PS_DEC) || defined(DRM_PS))
866 hDecoder
->upMatrix
= 0;
867 /* check if we have a mono file */
868 if (output_channels
== 1)
870 /* upMatrix to 2 channels for implicit signalling of PS */
871 hDecoder
->upMatrix
= 1;
876 /* Make a channel configuration based on either a PCE or a channelConfiguration */
877 create_channel_config(hDecoder
, hInfo
);
879 /* number of samples in this frame */
880 hInfo
->samples
= frame_len
*output_channels
;
881 /* number of channels in this frame */
882 hInfo
->channels
= output_channels
;
884 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
886 hInfo
->object_type
= hDecoder
->object_type
;
890 hInfo
->header_type
= RAW
;
891 if (hDecoder
->adif_header_present
)
892 hInfo
->header_type
= ADIF
;
893 if (hDecoder
->adts_header_present
)
894 hInfo
->header_type
= ADTS
;
895 #if (defined(PS_DEC) || defined(DRM_PS))
896 hInfo
->ps
= hDecoder
->ps_used_global
;
899 /* check if frame has channel elements */
906 /* allocate the buffer for the final samples */
907 if ((hDecoder
->sample_buffer
== NULL
) ||
908 (hDecoder
->alloced_channels
!= output_channels
))
910 static const uint8_t str
[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
911 sizeof(float32_t
), sizeof(double), sizeof(int16_t), sizeof(int16_t),
912 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
914 uint8_t stride
= str
[hDecoder
->config
.outputFormat
-1];
916 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
921 /* check if we want to use internal sample_buffer */
922 if (sample_buffer_size
== 0)
924 if (hDecoder
->sample_buffer
)
925 faad_free(hDecoder
->sample_buffer
);
926 hDecoder
->sample_buffer
= NULL
;
927 hDecoder
->sample_buffer
= faad_malloc(frame_len
*output_channels
*stride
);
928 } else if (sample_buffer_size
< frame_len
*output_channels
*stride
) {
929 /* provided sample buffer is not big enough */
933 hDecoder
->alloced_channels
= output_channels
;
936 if (sample_buffer_size
== 0)
938 sample_buffer
= hDecoder
->sample_buffer
;
940 sample_buffer
= *sample_buffer2
;
944 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
948 /* this data is different when SBR is used or when the data is upsampled */
949 if (!hDecoder
->downSampledSBR
)
953 hInfo
->samplerate
*= 2;
956 /* check if every element was provided with SBR data */
957 for (ele
= 0; ele
< hDecoder
->fr_ch_ele
; ele
++)
959 if (hDecoder
->sbr
[ele
] == NULL
)
967 if (hDecoder
->sbr_present_flag
== 1)
969 hInfo
->object_type
= HE_AAC
;
970 hInfo
->sbr
= SBR_UPSAMPLED
;
972 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
974 if (hDecoder
->downSampledSBR
)
976 hInfo
->sbr
= SBR_DOWNSAMPLED
;
981 sample_buffer
= output_to_PCM(hDecoder
, hDecoder
->time_out
, sample_buffer
,
982 output_channels
, frame_len
, hDecoder
->config
.outputFormat
);
985 hDecoder
->postSeekResetFlag
= 0;
989 if (hDecoder
->object_type
!= LD
)
992 if (hDecoder
->frame
<= 1)
996 /* LD encoders will give lower delay */
997 if (hDecoder
->frame
<= 0)
1008 count
= faad_get_ts() - count
;
1009 hDecoder
->cycles
+= count
;
1012 return sample_buffer
;