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
, int latm_stream
)
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;
260 else if (latm_stream
) {
264 /* Check if an ADIF header is present */
265 if ((buffer
[0] == 'A') && (buffer
[1] == 'D') &&
266 (buffer
[2] == 'I') && (buffer
[3] == 'F'))
268 hDecoder
->adif_header_present
= 1;
270 get_adif_header(&adif
, &ld
);
271 faad_byte_align(&ld
);
273 hDecoder
->sf_index
= adif
.pce
[0].sf_index
;
274 hDecoder
->object_type
= adif
.pce
[0].object_type
+ 1;
276 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
277 *channels
= adif
.pce
[0].channels
;
279 memcpy(&(hDecoder
->pce
), &(adif
.pce
[0]), sizeof(program_config
));
280 hDecoder
->pce_set
= 1;
282 bits
= bit2byte(faad_get_processed_bits(&ld
));
284 /* Check if an ADTS header is present */
285 } else if (faad_showbits(&ld
, 12) == 0xfff) {
286 hDecoder
->adts_header_present
= 1;
288 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
289 adts_frame(&adts
, &ld
);
291 hDecoder
->sf_index
= adts
.sf_index
;
292 hDecoder
->object_type
= adts
.profile
+ 1;
294 *samplerate
= get_sample_rate(hDecoder
->sf_index
);
295 *channels
= (adts
.channel_configuration
> 6) ?
296 2 : adts
.channel_configuration
;
306 hDecoder
->channelConfiguration
= *channels
;
308 #if (defined(PS_DEC) || defined(DRM_PS))
309 /* check if we have a mono file */
312 /* upMatrix to 2 channels for implicit signalling of PS */
318 /* implicit signalling */
319 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
322 hDecoder
->forceUpSampling
= 1;
323 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
324 hDecoder
->downSampledSBR
= 1;
328 /* must be done before frameLength is divided by 2 for LD */
330 if (hDecoder
->object_type
== SSR
)
331 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
334 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
337 if (hDecoder
->object_type
== LD
)
338 hDecoder
->frameLength
>>= 1;
341 if (can_decode_ot(hDecoder
->object_type
) < 0)
347 /* Init the library using a DecoderSpecificInfo */
348 int8_t NEAACDECAPI
NeAACDecInit2(NeAACDecHandle hDecoder
, uint8_t *pBuffer
,
349 uint32_t SizeOfDecoderSpecificInfo
,
350 uint32_t *samplerate
, uint8_t *channels
)
353 mp4AudioSpecificConfig mp4ASC
;
355 if((hDecoder
== NULL
)
357 || (SizeOfDecoderSpecificInfo
< 2)
358 || (samplerate
== NULL
)
359 || (channels
== NULL
))
364 hDecoder
->adif_header_present
= 0;
365 hDecoder
->adts_header_present
= 0;
367 /* decode the audio specific config */
368 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
369 &(hDecoder
->pce
), hDecoder
->latm_header_present
);
371 /* copy the relevant info to the decoder handle */
372 *samplerate
= mp4ASC
.samplingFrequency
;
373 if (mp4ASC
.channelsConfiguration
)
375 *channels
= mp4ASC
.channelsConfiguration
;
377 *channels
= hDecoder
->pce
.channels
;
378 hDecoder
->pce_set
= 1;
380 #if (defined(PS_DEC) || defined(DRM_PS))
381 /* check if we have a mono file */
384 /* upMatrix to 2 channels for implicit signalling of PS */
388 hDecoder
->sf_index
= mp4ASC
.samplingFrequencyIndex
;
389 hDecoder
->object_type
= mp4ASC
.objectTypeIndex
;
390 #ifdef ERROR_RESILIENCE
391 hDecoder
->aacSectionDataResilienceFlag
= mp4ASC
.aacSectionDataResilienceFlag
;
392 hDecoder
->aacScalefactorDataResilienceFlag
= mp4ASC
.aacScalefactorDataResilienceFlag
;
393 hDecoder
->aacSpectralDataResilienceFlag
= mp4ASC
.aacSpectralDataResilienceFlag
;
396 hDecoder
->sbr_present_flag
= mp4ASC
.sbr_present_flag
;
397 hDecoder
->downSampledSBR
= mp4ASC
.downSampledSBR
;
398 if (hDecoder
->config
.dontUpSampleImplicitSBR
== 0)
399 hDecoder
->forceUpSampling
= mp4ASC
.forceUpSampling
;
401 hDecoder
->forceUpSampling
= 0;
403 /* AAC core decoder samplerate is 2 times as low */
404 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || hDecoder
->forceUpSampling
== 1)
406 hDecoder
->sf_index
= get_sr_index(mp4ASC
.samplingFrequency
/ 2);
414 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
415 if (mp4ASC
.frameLengthFlag
)
416 #ifdef ALLOW_SMALL_FRAMELENGTH
417 hDecoder
->frameLength
= 960;
422 /* must be done before frameLength is divided by 2 for LD */
424 if (hDecoder
->object_type
== SSR
)
425 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
428 hDecoder
->fb
= filter_bank_init(hDecoder
->frameLength
);
431 if (hDecoder
->object_type
== LD
)
432 hDecoder
->frameLength
>>= 1;
439 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
442 if (hDecoder
== NULL
)
443 return 1; /* error */
445 NeAACDecClose(*hDecoder
);
447 *hDecoder
= NeAACDecOpen();
449 /* Special object type defined for DRM */
450 (*hDecoder
)->config
.defObjectType
= DRM_ER_LC
;
452 (*hDecoder
)->config
.defSampleRate
= samplerate
;
453 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
454 (*hDecoder
)->aacSectionDataResilienceFlag
= 1; /* VCB11 */
455 (*hDecoder
)->aacScalefactorDataResilienceFlag
= 0; /* no RVLC */
456 (*hDecoder
)->aacSpectralDataResilienceFlag
= 1; /* HCR */
458 (*hDecoder
)->frameLength
= 960;
459 (*hDecoder
)->sf_index
= get_sr_index((*hDecoder
)->config
.defSampleRate
);
460 (*hDecoder
)->object_type
= (*hDecoder
)->config
.defObjectType
;
462 if ((channels
== DRMCH_STEREO
) || (channels
== DRMCH_SBR_STEREO
))
463 (*hDecoder
)->channelConfiguration
= 2;
465 (*hDecoder
)->channelConfiguration
= 1;
468 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
469 (*hDecoder
)->sbr_present_flag
= 0;
471 (*hDecoder
)->sbr_present_flag
= 1;
474 (*hDecoder
)->fb
= filter_bank_init((*hDecoder
)->frameLength
);
480 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
484 if (hDecoder
== NULL
)
488 printf("AAC decoder total: %I64d cycles\n", hDecoder
->cycles
);
489 printf("requant: %I64d cycles\n", hDecoder
->requant_cycles
);
490 printf("spectral_data: %I64d cycles\n", hDecoder
->spectral_cycles
);
491 printf("scalefactors: %I64d cycles\n", hDecoder
->scalefac_cycles
);
492 printf("output: %I64d cycles\n", hDecoder
->output_cycles
);
495 for (i
= 0; i
< MAX_CHANNELS
; i
++)
497 if (hDecoder
->time_out
[i
]) faad_free(hDecoder
->time_out
[i
]);
498 if (hDecoder
->fb_intermed
[i
]) faad_free(hDecoder
->fb_intermed
[i
]);
500 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
501 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
504 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
507 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
512 if (hDecoder
->object_type
== SSR
)
513 ssr_filter_bank_end(hDecoder
->fb
);
516 filter_bank_end(hDecoder
->fb
);
518 drc_end(hDecoder
->drc
);
520 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
523 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
525 if (hDecoder
->sbr
[i
])
526 sbrDecodeEnd(hDecoder
->sbr
[i
]);
530 if (hDecoder
) faad_free(hDecoder
);
533 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
537 hDecoder
->postSeekResetFlag
= 1;
540 hDecoder
->frame
= frame
;
544 static void create_channel_config(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
)
546 hInfo
->num_front_channels
= 0;
547 hInfo
->num_side_channels
= 0;
548 hInfo
->num_back_channels
= 0;
549 hInfo
->num_lfe_channels
= 0;
550 memset(hInfo
->channel_position
, 0, MAX_CHANNELS
*sizeof(uint8_t));
552 if (hDecoder
->downMatrix
)
554 hInfo
->num_front_channels
= 2;
555 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
556 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
560 /* check if there is a PCE */
561 if (hDecoder
->pce_set
)
563 uint8_t i
, chpos
= 0;
564 uint8_t chdir
, back_center
= 0;
566 hInfo
->num_front_channels
= hDecoder
->pce
.num_front_channels
;
567 hInfo
->num_side_channels
= hDecoder
->pce
.num_side_channels
;
568 hInfo
->num_back_channels
= hDecoder
->pce
.num_back_channels
;
569 hInfo
->num_lfe_channels
= hDecoder
->pce
.num_lfe_channels
;
571 chdir
= hInfo
->num_front_channels
;
574 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
577 for (i
= 0; i
< chdir
; i
+= 2)
579 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_LEFT
;
580 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_RIGHT
;
583 for (i
= 0; i
< hInfo
->num_side_channels
; i
+= 2)
585 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_LEFT
;
586 hInfo
->channel_position
[chpos
++] = SIDE_CHANNEL_RIGHT
;
589 chdir
= hInfo
->num_back_channels
;
595 for (i
= 0; i
< chdir
; i
+= 2)
597 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
598 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
602 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_CENTER
;
605 for (i
= 0; i
< hInfo
->num_lfe_channels
; i
++)
607 hInfo
->channel_position
[chpos
++] = LFE_CHANNEL
;
611 switch (hDecoder
->channelConfiguration
)
614 hInfo
->num_front_channels
= 1;
615 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
618 hInfo
->num_front_channels
= 2;
619 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
620 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
623 hInfo
->num_front_channels
= 3;
624 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
625 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
626 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
629 hInfo
->num_front_channels
= 3;
630 hInfo
->num_back_channels
= 1;
631 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
632 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
633 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
634 hInfo
->channel_position
[3] = BACK_CHANNEL_CENTER
;
637 hInfo
->num_front_channels
= 3;
638 hInfo
->num_back_channels
= 2;
639 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
640 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
641 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
642 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
643 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
646 hInfo
->num_front_channels
= 3;
647 hInfo
->num_back_channels
= 2;
648 hInfo
->num_lfe_channels
= 1;
649 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
650 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
651 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
652 hInfo
->channel_position
[3] = BACK_CHANNEL_LEFT
;
653 hInfo
->channel_position
[4] = BACK_CHANNEL_RIGHT
;
654 hInfo
->channel_position
[5] = LFE_CHANNEL
;
657 hInfo
->num_front_channels
= 3;
658 hInfo
->num_side_channels
= 2;
659 hInfo
->num_back_channels
= 2;
660 hInfo
->num_lfe_channels
= 1;
661 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
662 hInfo
->channel_position
[1] = FRONT_CHANNEL_LEFT
;
663 hInfo
->channel_position
[2] = FRONT_CHANNEL_RIGHT
;
664 hInfo
->channel_position
[3] = SIDE_CHANNEL_LEFT
;
665 hInfo
->channel_position
[4] = SIDE_CHANNEL_RIGHT
;
666 hInfo
->channel_position
[5] = BACK_CHANNEL_LEFT
;
667 hInfo
->channel_position
[6] = BACK_CHANNEL_RIGHT
;
668 hInfo
->channel_position
[7] = LFE_CHANNEL
;
670 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
673 uint8_t ch
= hDecoder
->fr_channels
- hDecoder
->has_lfe
;
674 if (ch
& 1) /* there's either a center front or a center back channel */
676 uint8_t ch1
= (ch
-1)/2;
677 if (hDecoder
->first_syn_ele
== ID_SCE
)
679 hInfo
->num_front_channels
= ch1
+ 1;
680 hInfo
->num_back_channels
= ch1
;
681 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
682 for (i
= 1; i
<= ch1
; i
+=2)
684 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
685 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
687 for (i
= ch1
+1; i
< ch
; i
+=2)
689 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
690 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
693 hInfo
->num_front_channels
= ch1
;
694 hInfo
->num_back_channels
= ch1
+ 1;
695 for (i
= 0; i
< ch1
; i
+=2)
697 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
698 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
700 for (i
= ch1
; i
< ch
-1; i
+=2)
702 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
703 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
705 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
708 uint8_t ch1
= (ch
)/2;
709 hInfo
->num_front_channels
= ch1
;
710 hInfo
->num_back_channels
= ch1
;
713 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
714 for (i
= 1; i
<= ch1
; i
+=2)
716 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
717 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
719 for (i
= ch1
+1; i
< ch
-1; i
+=2)
721 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
722 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
724 hInfo
->channel_position
[ch
-1] = BACK_CHANNEL_CENTER
;
726 for (i
= 0; i
< ch1
; i
+=2)
728 hInfo
->channel_position
[i
] = FRONT_CHANNEL_LEFT
;
729 hInfo
->channel_position
[i
+1] = FRONT_CHANNEL_RIGHT
;
731 for (i
= ch1
; i
< ch
; i
+=2)
733 hInfo
->channel_position
[i
] = BACK_CHANNEL_LEFT
;
734 hInfo
->channel_position
[i
+1] = BACK_CHANNEL_RIGHT
;
738 hInfo
->num_lfe_channels
= hDecoder
->has_lfe
;
739 for (i
= ch
; i
< hDecoder
->fr_channels
; i
++)
741 hInfo
->channel_position
[i
] = LFE_CHANNEL
;
749 void* NEAACDECAPI
NeAACDecDecode(NeAACDecHandle hDecoder
,
750 NeAACDecFrameInfo
*hInfo
,
751 uint8_t *buffer
, uint32_t buffer_size
)
753 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
, NULL
, 0);
756 void* NEAACDECAPI
NeAACDecDecode2(NeAACDecHandle hDecoder
,
757 NeAACDecFrameInfo
*hInfo
,
758 uint8_t *buffer
, uint32_t buffer_size
,
759 void **sample_buffer
, uint32_t sample_buffer_size
)
761 if ((sample_buffer
== NULL
) || (sample_buffer_size
== 0))
767 return aac_frame_decode(hDecoder
, hInfo
, buffer
, buffer_size
,
768 sample_buffer
, sample_buffer_size
);
771 static void* aac_frame_decode(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
772 uint8_t *buffer
, uint32_t buffer_size
,
773 void **sample_buffer2
, uint32_t sample_buffer_size
)
775 uint8_t channels
= 0;
776 uint8_t output_channels
= 0;
778 uint32_t bitsconsumed
;
781 uint32_t startbit
=0, endbit
=0, payload_bits
=0;
784 int64_t count
= faad_get_ts();
788 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
794 printf("%d\n", buffer_size
*8);
797 frame_len
= hDecoder
->frameLength
;
800 memset(hInfo
, 0, sizeof(NeAACDecFrameInfo
));
801 memset(hDecoder
->internal_channel
, 0, MAX_CHANNELS
*sizeof(hDecoder
->internal_channel
[0]));
803 /* initialize the bitstream */
804 faad_initbits(&ld
, buffer
, buffer_size
);
809 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
813 buf
= faad_getbitbuffer(&ld
, 32);
814 //temp = getdword((void*)buf);
815 temp
= *((uint32_t*)buf
);
816 printf("0x%.8X\n", temp
);
820 faad_initbits(&ld
, buffer
, buffer_size
);
824 if(hDecoder
->latm_header_present
)
826 payload_bits
= faad_latm_frame(&hDecoder
->latm_config
, &ld
);
827 startbit
= faad_get_processed_bits(&ld
);
828 if(payload_bits
== -1U)
836 if (hDecoder
->object_type
== DRM_ER_LC
)
838 /* We do not support stereo right now */
839 if (0) //(hDecoder->channelConfiguration == 2)
841 hInfo
->error
= 8; // Throw CRC error
846 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
850 if (hDecoder
->adts_header_present
)
854 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
855 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
858 /* MPEG2 does byte_alignment() here,
859 * but ADTS header is always multiple of 8 bits in MPEG2
860 * so not needed to actually do it.
868 /* decode the complete bitstream */
870 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
872 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
875 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
880 if(hDecoder
->latm_header_present
)
882 endbit
= faad_get_processed_bits(&ld
);
883 if(endbit
-startbit
> payload_bits
)
884 fprintf(stderr
, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
885 endbit
-startbit
, payload_bits
);
886 if(hDecoder
->latm_config
.otherDataLenBits
> 0)
887 faad_getbits(&ld
, hDecoder
->latm_config
.otherDataLenBits
);
888 faad_byte_align(&ld
);
891 channels
= hDecoder
->fr_channels
;
893 if (hInfo
->error
> 0)
897 if (channels
== 0 || channels
> MAX_CHANNELS
)
899 /* invalid number of channels */
904 /* no more bit reading after this */
905 bitsconsumed
= faad_get_processed_bits(&ld
);
906 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
915 if (!hDecoder
->adts_header_present
&& !hDecoder
->adif_header_present
&& !hDecoder
->latm_header_present
)
917 if (hDecoder
->channelConfiguration
== 0)
918 hDecoder
->channelConfiguration
= channels
;
920 if (channels
== 8) /* 7.1 */
921 hDecoder
->channelConfiguration
= 7;
922 if (channels
== 7) /* not a standard channelConfiguration */
923 hDecoder
->channelConfiguration
= 0;
926 if ((channels
== 5 || channels
== 6) && hDecoder
->config
.downMatrix
)
928 hDecoder
->downMatrix
= 1;
931 output_channels
= channels
;
934 #if (defined(PS_DEC) || defined(DRM_PS))
935 hDecoder
->upMatrix
= 0;
936 /* check if we have a mono file */
937 if (output_channels
== 1)
939 /* upMatrix to 2 channels for implicit signalling of PS */
940 hDecoder
->upMatrix
= 1;
945 /* Make a channel configuration based on either a PCE or a channelConfiguration */
946 create_channel_config(hDecoder
, hInfo
);
948 /* number of samples in this frame */
949 hInfo
->samples
= frame_len
*output_channels
;
950 /* number of channels in this frame */
951 hInfo
->channels
= output_channels
;
953 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
955 hInfo
->object_type
= hDecoder
->object_type
;
959 hInfo
->header_type
= RAW
;
960 if (hDecoder
->adif_header_present
)
961 hInfo
->header_type
= ADIF
;
962 if (hDecoder
->adts_header_present
)
963 hInfo
->header_type
= ADTS
;
964 if (hDecoder
->latm_header_present
)
965 hInfo
->header_type
= LATM
;
966 #if (defined(PS_DEC) || defined(DRM_PS))
967 hInfo
->ps
= hDecoder
->ps_used_global
;
970 /* check if frame has channel elements */
977 /* allocate the buffer for the final samples */
978 if ((hDecoder
->sample_buffer
== NULL
) ||
979 (hDecoder
->alloced_channels
!= output_channels
))
981 static const uint8_t str
[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
982 sizeof(float32_t
), sizeof(double), sizeof(int16_t), sizeof(int16_t),
983 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
985 uint8_t stride
= str
[hDecoder
->config
.outputFormat
-1];
987 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
992 /* check if we want to use internal sample_buffer */
993 if (sample_buffer_size
== 0)
995 if (hDecoder
->sample_buffer
)
996 faad_free(hDecoder
->sample_buffer
);
997 hDecoder
->sample_buffer
= NULL
;
998 hDecoder
->sample_buffer
= faad_malloc(frame_len
*output_channels
*stride
);
999 } else if (sample_buffer_size
< frame_len
*output_channels
*stride
) {
1000 /* provided sample buffer is not big enough */
1004 hDecoder
->alloced_channels
= output_channels
;
1007 if (sample_buffer_size
== 0)
1009 sample_buffer
= hDecoder
->sample_buffer
;
1011 sample_buffer
= *sample_buffer2
;
1015 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
1019 /* this data is different when SBR is used or when the data is upsampled */
1020 if (!hDecoder
->downSampledSBR
)
1023 hInfo
->samples
*= 2;
1024 hInfo
->samplerate
*= 2;
1027 /* check if every element was provided with SBR data */
1028 for (ele
= 0; ele
< hDecoder
->fr_ch_ele
; ele
++)
1030 if (hDecoder
->sbr
[ele
] == NULL
)
1038 if (hDecoder
->sbr_present_flag
== 1)
1040 hInfo
->object_type
= HE_AAC
;
1041 hInfo
->sbr
= SBR_UPSAMPLED
;
1043 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
1045 if (hDecoder
->downSampledSBR
)
1047 hInfo
->sbr
= SBR_DOWNSAMPLED
;
1052 sample_buffer
= output_to_PCM(hDecoder
, hDecoder
->time_out
, sample_buffer
,
1053 output_channels
, frame_len
, hDecoder
->config
.outputFormat
);
1056 hDecoder
->postSeekResetFlag
= 0;
1060 if (hDecoder
->object_type
!= LD
)
1063 if (hDecoder
->frame
<= 1)
1067 /* LD encoders will give lower delay */
1068 if (hDecoder
->frame
<= 0)
1079 count
= faad_get_ts() - count
;
1080 hDecoder
->cycles
+= count
;
1083 return sample_buffer
;