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.104 2004/06/30 12:45:56 menno 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 hDecoder
->apply_sf_func
= apply_scalefactors_sse
;
151 hDecoder
->apply_sf_func
= apply_scalefactors
;
158 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
162 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
170 uint8_t NEAACDECAPI
NeAACDecSetConfiguration(NeAACDecHandle hDecoder
,
171 NeAACDecConfigurationPtr config
)
173 if (hDecoder
&& config
)
175 /* check if we can decode this object type */
176 if (can_decode_ot(config
->defObjectType
) < 0)
178 hDecoder
->config
.defObjectType
= config
->defObjectType
;
180 /* samplerate: anything but 0 should be possible */
181 if (config
->defSampleRate
== 0)
183 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
185 /* check output format */
187 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
190 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
193 hDecoder
->config
.outputFormat
= config
->outputFormat
;
195 if (config
->downMatrix
> 1)
197 hDecoder
->config
.downMatrix
= config
->downMatrix
;
206 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
207 uint32_t buffer_size
,
208 uint32_t *samplerate
, uint8_t *channels
)
215 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
218 hDecoder
->sf_index
= get_sr_index(hDecoder
->config
.defSampleRate
);
219 hDecoder
->object_type
= hDecoder
->config
.defObjectType
;
220 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
225 faad_initbits(&ld
, buffer
, buffer_size
);
227 /* Check if an ADIF header is present */
228 if ((buffer
[0] == 'A') && (buffer
[1] == 'D') &&
229 (buffer
[2] == 'I') && (buffer
[3] == 'F'))
231 hDecoder
->adif_header_present
= 1;
233 get_adif_header(&adif
, &ld
);
234 faad_byte_align(&ld
);
236 hDecoder
->sf_index
= adif
.pce
[0].sf_index
;
237 hDecoder
->object_type
= adif
.pce
[0].object_type
+ 1;
239 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
240 *channels
= adif
.pce
[0].channels
;
242 memcpy(&(hDecoder
->pce
), &(adif
.pce
[0]), sizeof(program_config
));
243 hDecoder
->pce_set
= 1;
245 bits
= bit2byte(faad_get_processed_bits(&ld
));
247 /* Check if an ADTS header is present */
248 } else if (faad_showbits(&ld
, 12) == 0xfff) {
249 hDecoder
->adts_header_present
= 1;
251 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
252 adts_frame(&adts
, &ld
);
254 hDecoder
->sf_index
= adts
.sf_index
;
255 hDecoder
->object_type
= adts
.profile
+ 1;
257 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
258 *channels
= (adts
.channel_configuration
> 6) ?
259 2 : adts
.channel_configuration
;
269 hDecoder
->channelConfiguration
= *channels
;
271 #if (defined(PS_DEC) || defined(DRM_PS))
272 /* check if we have a mono file */
275 /* upMatrix to 2 channels for implicit signalling of PS */
281 /* implicit signalling */
282 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
285 hDecoder
->forceUpSampling
= 1;
286 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
287 hDecoder
->downSampledSBR
= 1;
291 /* must be done before frameLength is divided by 2 for LD */
293 if (hDecoder
->object_type
== SSR
)
294 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
297 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
300 if (hDecoder
->object_type
== LD
)
301 hDecoder
->frameLength
>>= 1;
304 if (can_decode_ot(hDecoder
->object_type
) < 0)
310 /* Init the library using a DecoderSpecificInfo */
311 int8_t NEAACDECAPI
NeAACDecInit2(NeAACDecHandle hDecoder
, uint8_t *pBuffer
,
312 uint32_t SizeOfDecoderSpecificInfo
,
313 uint32_t *samplerate
, uint8_t *channels
)
316 mp4AudioSpecificConfig mp4ASC
;
318 if((hDecoder
== NULL
)
320 || (SizeOfDecoderSpecificInfo
< 2)
321 || (samplerate
== NULL
)
322 || (channels
== NULL
))
327 hDecoder
->adif_header_present
= 0;
328 hDecoder
->adts_header_present
= 0;
330 /* decode the audio specific config */
331 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
334 /* copy the relevant info to the decoder handle */
335 *samplerate
= mp4ASC
.samplingFrequency
;
336 if (mp4ASC
.channelsConfiguration
)
338 *channels
= mp4ASC
.channelsConfiguration
;
340 *channels
= hDecoder
->pce
.channels
;
341 hDecoder
->pce_set
= 1;
343 #if (defined(PS_DEC) || defined(DRM_PS))
344 /* check if we have a mono file */
347 /* upMatrix to 2 channels for implicit signalling of PS */
351 hDecoder
->sf_index
= mp4ASC
.samplingFrequencyIndex
;
352 hDecoder
->object_type
= mp4ASC
.objectTypeIndex
;
353 #ifdef ERROR_RESILIENCE
354 hDecoder
->aacSectionDataResilienceFlag
= mp4ASC
.aacSectionDataResilienceFlag
;
355 hDecoder
->aacScalefactorDataResilienceFlag
= mp4ASC
.aacScalefactorDataResilienceFlag
;
356 hDecoder
->aacSpectralDataResilienceFlag
= mp4ASC
.aacSpectralDataResilienceFlag
;
359 hDecoder
->sbr_present_flag
= mp4ASC
.sbr_present_flag
;
360 hDecoder
->downSampledSBR
= mp4ASC
.downSampledSBR
;
361 if (hDecoder
->config
.dontUpSampleImplicitSBR
== 0)
362 hDecoder
->forceUpSampling
= mp4ASC
.forceUpSampling
;
364 hDecoder
->forceUpSampling
= 0;
366 /* AAC core decoder samplerate is 2 times as low */
367 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || hDecoder
->forceUpSampling
== 1)
369 hDecoder
->sf_index
= get_sr_index(mp4ASC
.samplingFrequency
/ 2);
377 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
378 if (mp4ASC
.frameLengthFlag
)
379 #ifdef ALLOW_SMALL_FRAMELENGTH
380 hDecoder
->frameLength
= 960;
385 /* must be done before frameLength is divided by 2 for LD */
387 if (hDecoder
->object_type
== SSR
)
388 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
391 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
394 if (hDecoder
->object_type
== LD
)
395 hDecoder
->frameLength
>>= 1;
402 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
405 if (hDecoder
== NULL
)
406 return 1; /* error */
408 NeAACDecClose(*hDecoder
);
410 *hDecoder
= NeAACDecOpen();
412 /* Special object type defined for DRM */
413 (*hDecoder
)->config
.defObjectType
= DRM_ER_LC
;
415 (*hDecoder
)->config
.defSampleRate
= samplerate
;
416 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
417 (*hDecoder
)->aacSectionDataResilienceFlag
= 1; /* VCB11 */
418 (*hDecoder
)->aacScalefactorDataResilienceFlag
= 0; /* no RVLC */
419 (*hDecoder
)->aacSpectralDataResilienceFlag
= 1; /* HCR */
421 (*hDecoder
)->frameLength
= 960;
422 (*hDecoder
)->sf_index
= get_sr_index((*hDecoder
)->config
.defSampleRate
);
423 (*hDecoder
)->object_type
= (*hDecoder
)->config
.defObjectType
;
425 if ((channels
== DRMCH_STEREO
) || (channels
== DRMCH_SBR_STEREO
))
426 (*hDecoder
)->channelConfiguration
= 2;
428 (*hDecoder
)->channelConfiguration
= 1;
431 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
432 (*hDecoder
)->sbr_present_flag
= 0;
434 (*hDecoder
)->sbr_present_flag
= 1;
437 (*hDecoder
)->fb
= filter_bank_init((*hDecoder
)->frameLength
);
443 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
447 if (hDecoder
== NULL
)
451 printf("AAC decoder total: %I64d cycles\n", hDecoder
->cycles
);
452 printf("requant: %I64d cycles\n", hDecoder
->requant_cycles
);
453 printf("spectral_data: %I64d cycles\n", hDecoder
->spectral_cycles
);
454 printf("scalefactors: %I64d cycles\n", hDecoder
->scalefac_cycles
);
455 printf("output: %I64d cycles\n", hDecoder
->output_cycles
);
458 for (i
= 0; i
< MAX_CHANNELS
; i
++)
460 if (hDecoder
->time_out
[i
]) faad_free(hDecoder
->time_out
[i
]);
461 if (hDecoder
->fb_intermed
[i
]) faad_free(hDecoder
->fb_intermed
[i
]);
463 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
464 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
467 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
470 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
475 if (hDecoder
->object_type
== SSR
)
476 ssr_filter_bank_end(hDecoder
->fb
);
479 filter_bank_end(hDecoder
->fb
);
481 drc_end(hDecoder
->drc
);
483 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
486 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
488 if (hDecoder
->sbr
[i
])
489 sbrDecodeEnd(hDecoder
->sbr
[i
]);
493 if (hDecoder
) faad_free(hDecoder
);
496 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
500 hDecoder
->postSeekResetFlag
= 1;
503 hDecoder
->frame
= frame
;
507 static void create_channel_config(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
)
509 hInfo
->num_front_channels
= 0;
510 hInfo
->num_side_channels
= 0;
511 hInfo
->num_back_channels
= 0;
512 hInfo
->num_lfe_channels
= 0;
513 memset(hInfo
->channel_position
, 0, MAX_CHANNELS
*sizeof(uint8_t));
515 if (hDecoder
->downMatrix
)
517 hInfo
->num_front_channels
= 2;
518 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
519 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
523 /* check if there is a PCE */
524 if (hDecoder
->pce_set
)
526 uint8_t i
, chpos
= 0;
527 uint8_t chdir
, back_center
= 0;
529 hInfo
->num_front_channels
= hDecoder
->pce
.num_front_channels
;
530 hInfo
->num_side_channels
= hDecoder
->pce
.num_side_channels
;
531 hInfo
->num_back_channels
= hDecoder
->pce
.num_back_channels
;
532 hInfo
->num_lfe_channels
= hDecoder
->pce
.num_lfe_channels
;
534 chdir
= hInfo
->num_front_channels
;
537 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
540 for (i
= 0; i
< chdir
; i
+= 2)
542 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_LEFT
;
543 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_RIGHT
;
546 for (i
= 0; i
< hInfo
->num_side_channels
; i
+= 2)
548 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_LEFT
;
549 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_RIGHT
;
552 chdir
= hInfo
->num_back_channels
;
558 for (i
= 0; i
< chdir
; i
+= 2)
560 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
561 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
565 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_CENTER
;
568 for (i
= 0; i
< hInfo
->num_lfe_channels
; i
++)
570 hInfo
->channel_position
[chpos
++] = LFE_CHANNEL
;
574 switch (hDecoder
->channelConfiguration
)
577 hInfo
->num_front_channels
= 1;
578 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
581 hInfo
->num_front_channels
= 2;
582 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
583 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
586 hInfo
->num_front_channels
= 3;
587 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
588 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
589 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
592 hInfo
->num_front_channels
= 3;
593 hInfo
->num_back_channels
= 1;
594 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
595 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
596 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
597 hInfo
->channel_position
[3] = BACK_CHANNEL_CENTER
;
600 hInfo
->num_front_channels
= 3;
601 hInfo
->num_back_channels
= 2;
602 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
603 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
604 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
605 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
606 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
609 hInfo
->num_front_channels
= 3;
610 hInfo
->num_back_channels
= 2;
611 hInfo
->num_lfe_channels
= 1;
612 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
613 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
614 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
615 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
616 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
617 hInfo
->channel_position
[5] = LFE_CHANNEL
;
620 hInfo
->num_front_channels
= 3;
621 hInfo
->num_side_channels
= 2;
622 hInfo
->num_back_channels
= 2;
623 hInfo
->num_lfe_channels
= 1;
624 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
625 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
626 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
627 hInfo
->channel_position
[3] = SIDE_CHANNEL_LEFT
;
628 hInfo
->channel_position
[4] = SIDE_CHANNEL_RIGHT
;
629 hInfo
->channel_position
[5] = BACK_CHANNEL_LEFT
;
630 hInfo
->channel_position
[6] = BACK_CHANNEL_RIGHT
;
631 hInfo
->channel_position
[7] = LFE_CHANNEL
;
633 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
636 uint8_t ch
= hDecoder
->fr_channels
- hDecoder
->has_lfe
;
637 if (ch
& 1) /* there's either a center front or a center back channel */
639 uint8_t ch1
= (ch
-1)/2;
640 if (hDecoder
->first_syn_ele
== ID_SCE
)
642 hInfo
->num_front_channels
= ch1
+ 1;
643 hInfo
->num_back_channels
= ch1
;
644 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
645 for (i
= 1; i
<= ch1
; i
+=2)
647 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
648 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
650 for (i
= ch1
+1; i
< ch
; i
+=2)
652 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
653 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
656 hInfo
->num_front_channels
= ch1
;
657 hInfo
->num_back_channels
= ch1
+ 1;
658 for (i
= 0; i
< ch1
; i
+=2)
660 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
661 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
663 for (i
= ch1
; i
< ch
-1; i
+=2)
665 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
666 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
668 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
671 uint8_t ch1
= (ch
)/2;
672 hInfo
->num_front_channels
= ch1
;
673 hInfo
->num_back_channels
= ch1
;
676 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
677 for (i
= 1; i
<= ch1
; i
+=2)
679 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
680 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
682 for (i
= ch1
+1; i
< ch
-1; i
+=2)
684 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
685 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
687 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
689 for (i
= 0; i
< ch1
; i
+=2)
691 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
692 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
694 for (i
= ch1
; i
< ch
; i
+=2)
696 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
697 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
701 hInfo
->num_lfe_channels
= hDecoder
->has_lfe
;
702 for (i
= ch
; i
< hDecoder
->fr_channels
; i
++)
704 hInfo
->channel_position
[i
] = LFE_CHANNEL
;
712 void* NEAACDECAPI
NeAACDecDecode(NeAACDecHandle hDecoder
,
713 NeAACDecFrameInfo
*hInfo
,
714 uint8_t *buffer
, uint32_t buffer_size
)
716 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
, NULL
, 0);
719 void* NEAACDECAPI
NeAACDecDecode2(NeAACDecHandle hDecoder
,
720 NeAACDecFrameInfo
*hInfo
,
721 uint8_t *buffer
, uint32_t buffer_size
,
722 void **sample_buffer
, uint32_t sample_buffer_size
)
724 if ((sample_buffer
== NULL
) || (sample_buffer_size
== 0))
730 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
,
731 sample_buffer
, sample_buffer_size
);
734 static void* aac_frame_decode(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
735 uint8_t *buffer
, uint32_t buffer_size
,
736 void **sample_buffer2
, uint32_t sample_buffer_size
)
738 uint8_t channels
= 0;
739 uint8_t output_channels
= 0;
741 uint32_t bitsconsumed
;
746 int64_t count
= faad_get_ts();
750 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
756 printf("%d\n", buffer_size
*8);
759 frame_len
= hDecoder
->frameLength
;
762 memset(hInfo
, 0, sizeof(NeAACDecFrameInfo
));
763 memset(hDecoder
->internal_channel
, 0, MAX_CHANNELS
*sizeof(hDecoder
->internal_channel
[0]));
765 /* initialize the bitstream */
766 faad_initbits(&ld
, buffer
, buffer_size
);
771 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
775 buf
= faad_getbitbuffer(&ld
, 32);
776 //temp = getdword((void*)buf);
777 temp
= *((uint32_t*)buf
);
778 printf("0x%.8X\n", temp
);
782 faad_initbits(&ld
, buffer
, buffer_size
);
787 if (hDecoder
->object_type
== DRM_ER_LC
)
789 /* We do not support stereo right now */
790 if (0) //(hDecoder->channelConfiguration == 2)
792 hInfo
->error
= 8; // Throw CRC error
797 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
801 if (hDecoder
->adts_header_present
)
805 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
806 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
809 /* MPEG2 does byte_alignment() here,
810 * but ADTS header is always multiple of 8 bits in MPEG2
811 * so not needed to actually do it.
819 /* decode the complete bitstream */
821 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
823 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
826 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
831 channels
= hDecoder
->fr_channels
;
833 if (hInfo
->error
> 0)
837 if (channels
== 0 || channels
> MAX_CHANNELS
)
839 /* invalid number of channels */
844 /* no more bit reading after this */
845 bitsconsumed
= faad_get_processed_bits(&ld
);
846 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
855 if (!hDecoder
->adts_header_present
&& !hDecoder
->adif_header_present
)
857 if (hDecoder
->channelConfiguration
== 0)
858 hDecoder
->channelConfiguration
= channels
;
860 if (channels
== 8) /* 7.1 */
861 hDecoder
->channelConfiguration
= 7;
862 if (channels
== 7) /* not a standard channelConfiguration */
863 hDecoder
->channelConfiguration
= 0;
866 if ((channels
== 5 || channels
== 6) && hDecoder
->config
.downMatrix
)
868 hDecoder
->downMatrix
= 1;
871 output_channels
= channels
;
874 #if (defined(PS_DEC) || defined(DRM_PS))
875 hDecoder
->upMatrix
= 0;
876 /* check if we have a mono file */
877 if (output_channels
== 1)
879 /* upMatrix to 2 channels for implicit signalling of PS */
880 hDecoder
->upMatrix
= 1;
885 /* Make a channel configuration based on either a PCE or a channelConfiguration */
886 create_channel_config(hDecoder
, hInfo
);
888 /* number of samples in this frame */
889 hInfo
->samples
= frame_len
*output_channels
;
890 /* number of channels in this frame */
891 hInfo
->channels
= output_channels
;
893 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
895 hInfo
->object_type
= hDecoder
->object_type
;
899 hInfo
->header_type
= RAW
;
900 if (hDecoder
->adif_header_present
)
901 hInfo
->header_type
= ADIF
;
902 if (hDecoder
->adts_header_present
)
903 hInfo
->header_type
= ADTS
;
905 /* check if frame has channel elements */
912 /* allocate the buffer for the final samples */
913 if ((hDecoder
->sample_buffer
== NULL
) ||
914 (hDecoder
->alloced_channels
!= output_channels
))
916 static const uint8_t str
[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
917 sizeof(float32_t
), sizeof(double), sizeof(int16_t), sizeof(int16_t),
918 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
920 uint8_t stride
= str
[hDecoder
->config
.outputFormat
-1];
922 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
927 /* check if we want to use internal sample_buffer */
928 if (sample_buffer_size
== 0)
930 if (hDecoder
->sample_buffer
)
931 faad_free(hDecoder
->sample_buffer
);
932 hDecoder
->sample_buffer
= NULL
;
933 hDecoder
->sample_buffer
= faad_malloc(frame_len
*output_channels
*stride
);
934 } else if (sample_buffer_size
< frame_len
*output_channels
*stride
) {
935 /* provided sample buffer is not big enough */
939 hDecoder
->alloced_channels
= output_channels
;
942 if (sample_buffer_size
== 0)
944 sample_buffer
= hDecoder
->sample_buffer
;
946 sample_buffer
= *sample_buffer2
;
950 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
954 /* this data is different when SBR is used or when the data is upsampled */
955 if (!hDecoder
->downSampledSBR
)
959 hInfo
->samplerate
*= 2;
962 /* check if every element was provided with SBR data */
963 for (ele
= 0; ele
< hDecoder
->fr_ch_ele
; ele
++)
965 if (hDecoder
->sbr
[ele
] == NULL
)
973 if (hDecoder
->sbr_present_flag
== 1)
975 hInfo
->object_type
= HE_AAC
;
976 hInfo
->sbr
= SBR_UPSAMPLED
;
978 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
980 if (hDecoder
->downSampledSBR
)
982 hInfo
->sbr
= SBR_DOWNSAMPLED
;
987 sample_buffer
= output_to_PCM(hDecoder
, hDecoder
->time_out
, sample_buffer
,
988 output_channels
, frame_len
, hDecoder
->config
.outputFormat
);
991 hDecoder
->postSeekResetFlag
= 0;
995 if (hDecoder
->object_type
!= LD
)
998 if (hDecoder
->frame
<= 1)
1002 /* LD encoders will give lower delay */
1003 if (hDecoder
->frame
<= 0)
1014 count
= faad_get_ts() - count
;
1015 hDecoder
->cycles
+= count
;
1018 return sample_buffer
;