refine previous translation
[mplayer/glamo.git] / libfaad2 / decoder.c
blob3b33e11b3294b5288c119182c6b84811e5854765
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
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.
9 **
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
20 ** forbidden.
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 $
26 **/
28 #include "common.h"
29 #include "structs.h"
31 #include <stdlib.h>
32 #include <string.h>
34 #include "decoder.h"
35 #include "mp4.h"
36 #include "syntax.h"
37 #include "error.h"
38 #include "output.h"
39 #include "filtbank.h"
40 #include "drc.h"
41 #ifdef SBR_DEC
42 #include "sbr_dec.h"
43 #include "sbr_syntax.h"
44 #endif
45 #ifdef SSR_DEC
46 #include "ssr.h"
47 #endif
49 #ifdef ANALYSIS
50 uint16_t dbg_count;
51 #endif
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)
63 return NULL;
64 return err_msg[errcode];
67 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
69 uint32_t cap = 0;
71 /* can't do without it */
72 cap += LC_DEC_CAP;
74 #ifdef MAIN_DEC
75 cap += MAIN_DEC_CAP;
76 #endif
77 #ifdef LTP_DEC
78 cap += LTP_DEC_CAP;
79 #endif
80 #ifdef LD_DEC
81 cap += LD_DEC_CAP;
82 #endif
83 #ifdef ERROR_RESILIENCE
84 cap += ERROR_RESILIENCE_CAP;
85 #endif
86 #ifdef FIXED_POINT
87 cap += FIXED_POINT_CAP;
88 #endif
90 return cap;
93 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
95 uint8_t i;
96 NeAACDecHandle hDecoder = NULL;
98 if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
99 return 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;
114 #endif
115 hDecoder->frameLength = 1024;
117 hDecoder->frame = 0;
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;
125 #ifdef SSR_DEC
126 hDecoder->ssr_overlap[i] = NULL;
127 hDecoder->prev_fmd[i] = NULL;
128 #endif
129 #ifdef MAIN_DEC
130 hDecoder->pred_stat[i] = NULL;
131 #endif
132 #ifdef LTP_DEC
133 hDecoder->ltp_lag[i] = 0;
134 hDecoder->lt_pred_stat[i] = NULL;
135 #endif
138 #ifdef SBR_DEC
139 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
141 hDecoder->sbr[i] = NULL;
143 #endif
145 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
147 return hDecoder;
150 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
152 if (hDecoder)
154 NeAACDecConfigurationPtr config = &(hDecoder->config);
156 return config;
159 return NULL;
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)
169 return 0;
170 hDecoder->config.defObjectType = config->defObjectType;
172 /* samplerate: anything but 0 should be possible */
173 if (config->defSampleRate == 0)
174 return 0;
175 hDecoder->config.defSampleRate = config->defSampleRate;
177 /* check output format */
178 #ifdef FIXED_POINT
179 if ((config->outputFormat < 1) || (config->outputFormat > 4))
180 return 0;
181 #else
182 if ((config->outputFormat < 1) || (config->outputFormat > 5))
183 return 0;
184 #endif
185 hDecoder->config.outputFormat = config->outputFormat;
187 if (config->downMatrix > 1)
188 return 0;
189 hDecoder->config.downMatrix = config->downMatrix;
191 /* OK */
192 return 1;
195 return 0;
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);
205 if(bits==-1U)
206 bad++;
207 else
209 good++;
210 while(bits>0)
212 m = min(bits, 8);
213 faad_getbits(ld, m);
214 bits -= m;
219 return (good>0);
223 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
224 uint32_t buffer_size,
225 uint32_t *samplerate, uint8_t *channels)
227 uint32_t bits = 0;
228 bitfile ld;
229 adif_header adif;
230 adts_header adts;
232 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
233 return -1;
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);
238 *channels = 1;
240 if (buffer != NULL)
242 int is_latm;
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);
248 l->inited = 0;
249 l->frameLength = 0;
250 faad_rewindbits(&ld);
251 if(is_latm && l->ASCbits>0)
253 int32_t x;
254 hDecoder->latm_header_present = 1;
255 x = NeAACDecInit2(hDecoder, &l->ASC, (l->ASCbits+7)/8, samplerate, channels);
256 if(x!=0)
257 hDecoder->latm_header_present = 0;
258 return x;
260 else
261 /* Check if an ADIF header is present */
262 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
263 (buffer[2] == 'I') && (buffer[3] == 'F'))
265 hDecoder->adif_header_present = 1;
267 get_adif_header(&adif, &ld);
268 faad_byte_align(&ld);
270 hDecoder->sf_index = adif.pce[0].sf_index;
271 hDecoder->object_type = adif.pce[0].object_type + 1;
273 *samplerate = get_sample_rate(hDecoder->sf_index);
274 *channels = adif.pce[0].channels;
276 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
277 hDecoder->pce_set = 1;
279 bits = bit2byte(faad_get_processed_bits(&ld));
281 /* Check if an ADTS header is present */
282 } else if (faad_showbits(&ld, 12) == 0xfff) {
283 hDecoder->adts_header_present = 1;
285 adts.old_format = hDecoder->config.useOldADTSFormat;
286 adts_frame(&adts, &ld);
288 hDecoder->sf_index = adts.sf_index;
289 hDecoder->object_type = adts.profile + 1;
291 *samplerate = get_sample_rate(hDecoder->sf_index);
292 *channels = (adts.channel_configuration > 6) ?
293 2 : adts.channel_configuration;
296 if (ld.error)
298 faad_endbits(&ld);
299 return -1;
301 faad_endbits(&ld);
303 hDecoder->channelConfiguration = *channels;
305 #if (defined(PS_DEC) || defined(DRM_PS))
306 /* check if we have a mono file */
307 if (*channels == 1)
309 /* upMatrix to 2 channels for implicit signalling of PS */
310 *channels = 2;
312 #endif
314 #ifdef SBR_DEC
315 /* implicit signalling */
316 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
318 *samplerate *= 2;
319 hDecoder->forceUpSampling = 1;
320 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
321 hDecoder->downSampledSBR = 1;
323 #endif
325 /* must be done before frameLength is divided by 2 for LD */
326 #ifdef SSR_DEC
327 if (hDecoder->object_type == SSR)
328 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
329 else
330 #endif
331 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
333 #ifdef LD_DEC
334 if (hDecoder->object_type == LD)
335 hDecoder->frameLength >>= 1;
336 #endif
338 if (can_decode_ot(hDecoder->object_type) < 0)
339 return -1;
341 return bits;
344 /* Init the library using a DecoderSpecificInfo */
345 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
346 uint32_t SizeOfDecoderSpecificInfo,
347 uint32_t *samplerate, uint8_t *channels)
349 int8_t rc;
350 mp4AudioSpecificConfig mp4ASC;
352 if((hDecoder == NULL)
353 || (pBuffer == NULL)
354 || (SizeOfDecoderSpecificInfo < 2)
355 || (samplerate == NULL)
356 || (channels == NULL))
358 return -1;
361 hDecoder->adif_header_present = 0;
362 hDecoder->adts_header_present = 0;
364 /* decode the audio specific config */
365 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
366 &(hDecoder->pce), hDecoder->latm_header_present);
368 /* copy the relevant info to the decoder handle */
369 *samplerate = mp4ASC.samplingFrequency;
370 if (mp4ASC.channelsConfiguration)
372 *channels = mp4ASC.channelsConfiguration;
373 } else {
374 *channels = hDecoder->pce.channels;
375 hDecoder->pce_set = 1;
377 #if (defined(PS_DEC) || defined(DRM_PS))
378 /* check if we have a mono file */
379 if (*channels == 1)
381 /* upMatrix to 2 channels for implicit signalling of PS */
382 *channels = 2;
384 #endif
385 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
386 hDecoder->object_type = mp4ASC.objectTypeIndex;
387 #ifdef ERROR_RESILIENCE
388 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
389 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
390 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
391 #endif
392 #ifdef SBR_DEC
393 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
394 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
395 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
396 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
397 else
398 hDecoder->forceUpSampling = 0;
400 /* AAC core decoder samplerate is 2 times as low */
401 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
403 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
405 #endif
407 if (rc != 0)
409 return rc;
411 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
412 if (mp4ASC.frameLengthFlag)
413 #ifdef ALLOW_SMALL_FRAMELENGTH
414 hDecoder->frameLength = 960;
415 #else
416 return -1;
417 #endif
419 /* must be done before frameLength is divided by 2 for LD */
420 #ifdef SSR_DEC
421 if (hDecoder->object_type == SSR)
422 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
423 else
424 #endif
425 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
427 #ifdef LD_DEC
428 if (hDecoder->object_type == LD)
429 hDecoder->frameLength >>= 1;
430 #endif
432 return 0;
435 #ifdef DRM
436 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
437 uint8_t channels)
439 if (hDecoder == NULL)
440 return 1; /* error */
442 NeAACDecClose(*hDecoder);
444 *hDecoder = NeAACDecOpen();
446 /* Special object type defined for DRM */
447 (*hDecoder)->config.defObjectType = DRM_ER_LC;
449 (*hDecoder)->config.defSampleRate = samplerate;
450 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
451 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
452 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
453 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
454 #endif
455 (*hDecoder)->frameLength = 960;
456 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
457 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
459 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
460 (*hDecoder)->channelConfiguration = 2;
461 else
462 (*hDecoder)->channelConfiguration = 1;
464 #ifdef SBR_DEC
465 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
466 (*hDecoder)->sbr_present_flag = 0;
467 else
468 (*hDecoder)->sbr_present_flag = 1;
469 #endif
471 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
473 return 0;
475 #endif
477 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
479 uint8_t i;
481 if (hDecoder == NULL)
482 return;
484 #ifdef PROFILE
485 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
486 printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
487 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
488 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
489 printf("output: %I64d cycles\n", hDecoder->output_cycles);
490 #endif
492 for (i = 0; i < MAX_CHANNELS; i++)
494 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
495 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
496 #ifdef SSR_DEC
497 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
498 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
499 #endif
500 #ifdef MAIN_DEC
501 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
502 #endif
503 #ifdef LTP_DEC
504 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
505 #endif
508 #ifdef SSR_DEC
509 if (hDecoder->object_type == SSR)
510 ssr_filter_bank_end(hDecoder->fb);
511 else
512 #endif
513 filter_bank_end(hDecoder->fb);
515 drc_end(hDecoder->drc);
517 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
519 #ifdef SBR_DEC
520 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
522 if (hDecoder->sbr[i])
523 sbrDecodeEnd(hDecoder->sbr[i]);
525 #endif
527 if (hDecoder) faad_free(hDecoder);
530 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
532 if (hDecoder)
534 hDecoder->postSeekResetFlag = 1;
536 if (frame != -1)
537 hDecoder->frame = frame;
541 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
543 hInfo->num_front_channels = 0;
544 hInfo->num_side_channels = 0;
545 hInfo->num_back_channels = 0;
546 hInfo->num_lfe_channels = 0;
547 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
549 if (hDecoder->downMatrix)
551 hInfo->num_front_channels = 2;
552 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
553 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
554 return;
557 /* check if there is a PCE */
558 if (hDecoder->pce_set)
560 uint8_t i, chpos = 0;
561 uint8_t chdir, back_center = 0;
563 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
564 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
565 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
566 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
568 chdir = hInfo->num_front_channels;
569 if (chdir & 1)
571 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
572 chdir--;
574 for (i = 0; i < chdir; i += 2)
576 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
577 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
580 for (i = 0; i < hInfo->num_side_channels; i += 2)
582 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
583 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
586 chdir = hInfo->num_back_channels;
587 if (chdir & 1)
589 back_center = 1;
590 chdir--;
592 for (i = 0; i < chdir; i += 2)
594 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
595 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
597 if (back_center)
599 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
602 for (i = 0; i < hInfo->num_lfe_channels; i++)
604 hInfo->channel_position[chpos++] = LFE_CHANNEL;
607 } else {
608 switch (hDecoder->channelConfiguration)
610 case 1:
611 hInfo->num_front_channels = 1;
612 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
613 break;
614 case 2:
615 hInfo->num_front_channels = 2;
616 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
617 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
618 break;
619 case 3:
620 hInfo->num_front_channels = 3;
621 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
622 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
623 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
624 break;
625 case 4:
626 hInfo->num_front_channels = 3;
627 hInfo->num_back_channels = 1;
628 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
629 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
630 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
631 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
632 break;
633 case 5:
634 hInfo->num_front_channels = 3;
635 hInfo->num_back_channels = 2;
636 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
637 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
638 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
639 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
640 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
641 break;
642 case 6:
643 hInfo->num_front_channels = 3;
644 hInfo->num_back_channels = 2;
645 hInfo->num_lfe_channels = 1;
646 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
647 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
648 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
649 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
650 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
651 hInfo->channel_position[5] = LFE_CHANNEL;
652 break;
653 case 7:
654 hInfo->num_front_channels = 3;
655 hInfo->num_side_channels = 2;
656 hInfo->num_back_channels = 2;
657 hInfo->num_lfe_channels = 1;
658 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
659 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
660 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
661 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
662 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
663 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
664 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
665 hInfo->channel_position[7] = LFE_CHANNEL;
666 break;
667 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
669 uint8_t i;
670 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
671 if (ch & 1) /* there's either a center front or a center back channel */
673 uint8_t ch1 = (ch-1)/2;
674 if (hDecoder->first_syn_ele == ID_SCE)
676 hInfo->num_front_channels = ch1 + 1;
677 hInfo->num_back_channels = ch1;
678 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
679 for (i = 1; i <= ch1; i+=2)
681 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
682 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
684 for (i = ch1+1; i < ch; i+=2)
686 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
687 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
689 } else {
690 hInfo->num_front_channels = ch1;
691 hInfo->num_back_channels = ch1 + 1;
692 for (i = 0; i < ch1; i+=2)
694 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
695 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
697 for (i = ch1; i < ch-1; i+=2)
699 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
700 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
702 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
704 } else {
705 uint8_t ch1 = (ch)/2;
706 hInfo->num_front_channels = ch1;
707 hInfo->num_back_channels = ch1;
708 if (ch1 & 1)
710 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
711 for (i = 1; i <= ch1; i+=2)
713 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
714 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
716 for (i = ch1+1; i < ch-1; i+=2)
718 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
719 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
721 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
722 } else {
723 for (i = 0; i < ch1; i+=2)
725 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
726 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
728 for (i = ch1; i < ch; i+=2)
730 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
731 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
735 hInfo->num_lfe_channels = hDecoder->has_lfe;
736 for (i = ch; i < hDecoder->fr_channels; i++)
738 hInfo->channel_position[i] = LFE_CHANNEL;
741 break;
746 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
747 NeAACDecFrameInfo *hInfo,
748 uint8_t *buffer, uint32_t buffer_size)
750 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
753 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
754 NeAACDecFrameInfo *hInfo,
755 uint8_t *buffer, uint32_t buffer_size,
756 void **sample_buffer, uint32_t sample_buffer_size)
758 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
760 hInfo->error = 27;
761 return NULL;
764 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
765 sample_buffer, sample_buffer_size);
768 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
769 uint8_t *buffer, uint32_t buffer_size,
770 void **sample_buffer2, uint32_t sample_buffer_size)
772 uint8_t channels = 0;
773 uint8_t output_channels = 0;
774 bitfile ld;
775 uint32_t bitsconsumed;
776 uint16_t frame_len;
777 void *sample_buffer;
778 uint32_t startbit=0, endbit=0, payload_bits=0;
780 #ifdef PROFILE
781 int64_t count = faad_get_ts();
782 #endif
784 /* safety checks */
785 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
787 return NULL;
790 #if 0
791 printf("%d\n", buffer_size*8);
792 #endif
794 frame_len = hDecoder->frameLength;
797 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
798 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
800 /* initialize the bitstream */
801 faad_initbits(&ld, buffer, buffer_size);
803 #if 0
805 int i;
806 for (i = 0; i < ((buffer_size+3)>>2); i++)
808 uint8_t *buf;
809 uint32_t temp = 0;
810 buf = faad_getbitbuffer(&ld, 32);
811 //temp = getdword((void*)buf);
812 temp = *((uint32_t*)buf);
813 printf("0x%.8X\n", temp);
814 free(buf);
816 faad_endbits(&ld);
817 faad_initbits(&ld, buffer, buffer_size);
819 #endif
821 if(hDecoder->latm_header_present)
823 payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
824 startbit = faad_get_processed_bits(&ld);
825 if(payload_bits == -1U)
827 hInfo->error = 1;
828 goto error;
832 #ifdef DRM
833 if (hDecoder->object_type == DRM_ER_LC)
835 /* We do not support stereo right now */
836 if (0) //(hDecoder->channelConfiguration == 2)
838 hInfo->error = 8; // Throw CRC error
839 goto error;
842 faad_getbits(&ld, 8
843 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
845 #endif
847 if (hDecoder->adts_header_present)
849 adts_header adts;
851 adts.old_format = hDecoder->config.useOldADTSFormat;
852 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
853 goto error;
855 /* MPEG2 does byte_alignment() here,
856 * but ADTS header is always multiple of 8 bits in MPEG2
857 * so not needed to actually do it.
861 #ifdef ANALYSIS
862 dbg_count = 0;
863 #endif
865 /* decode the complete bitstream */
866 #ifdef SCALABLE_DEC
867 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
869 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
870 } else {
871 #endif
872 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
873 #ifdef SCALABLE_DEC
875 #endif
877 if(hDecoder->latm_header_present)
879 endbit = faad_get_processed_bits(&ld);
880 if(endbit-startbit > payload_bits)
881 fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
882 endbit-startbit, payload_bits);
883 if(hDecoder->latm_config.otherDataLenBits > 0)
884 faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
885 faad_byte_align(&ld);
888 channels = hDecoder->fr_channels;
890 if (hInfo->error > 0)
891 goto error;
893 /* safety check */
894 if (channels == 0 || channels > MAX_CHANNELS)
896 /* invalid number of channels */
897 hInfo->error = 12;
898 goto error;
901 /* no more bit reading after this */
902 bitsconsumed = faad_get_processed_bits(&ld);
903 hInfo->bytesconsumed = bit2byte(bitsconsumed);
904 if (ld.error)
906 hInfo->error = 14;
907 goto error;
909 faad_endbits(&ld);
912 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present && !hDecoder->latm_header_present)
914 if (hDecoder->channelConfiguration == 0)
915 hDecoder->channelConfiguration = channels;
917 if (channels == 8) /* 7.1 */
918 hDecoder->channelConfiguration = 7;
919 if (channels == 7) /* not a standard channelConfiguration */
920 hDecoder->channelConfiguration = 0;
923 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
925 hDecoder->downMatrix = 1;
926 output_channels = 2;
927 } else {
928 output_channels = channels;
931 #if (defined(PS_DEC) || defined(DRM_PS))
932 hDecoder->upMatrix = 0;
933 /* check if we have a mono file */
934 if (output_channels == 1)
936 /* upMatrix to 2 channels for implicit signalling of PS */
937 hDecoder->upMatrix = 1;
938 output_channels = 2;
940 #endif
942 /* Make a channel configuration based on either a PCE or a channelConfiguration */
943 create_channel_config(hDecoder, hInfo);
945 /* number of samples in this frame */
946 hInfo->samples = frame_len*output_channels;
947 /* number of channels in this frame */
948 hInfo->channels = output_channels;
949 /* samplerate */
950 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
951 /* object type */
952 hInfo->object_type = hDecoder->object_type;
953 /* sbr */
954 hInfo->sbr = NO_SBR;
955 /* header type */
956 hInfo->header_type = RAW;
957 if (hDecoder->adif_header_present)
958 hInfo->header_type = ADIF;
959 if (hDecoder->adts_header_present)
960 hInfo->header_type = ADTS;
961 if (hDecoder->latm_header_present)
962 hInfo->header_type = LATM;
963 #if (defined(PS_DEC) || defined(DRM_PS))
964 hInfo->ps = hDecoder->ps_used_global;
965 #endif
967 /* check if frame has channel elements */
968 if (channels == 0)
970 hDecoder->frame++;
971 return NULL;
974 /* allocate the buffer for the final samples */
975 if ((hDecoder->sample_buffer == NULL) ||
976 (hDecoder->alloced_channels != output_channels))
978 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
979 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
980 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
982 uint8_t stride = str[hDecoder->config.outputFormat-1];
983 #ifdef SBR_DEC
984 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
986 stride = 2 * stride;
988 #endif
989 /* check if we want to use internal sample_buffer */
990 if (sample_buffer_size == 0)
992 if (hDecoder->sample_buffer)
993 faad_free(hDecoder->sample_buffer);
994 hDecoder->sample_buffer = NULL;
995 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
996 } else if (sample_buffer_size < frame_len*output_channels*stride) {
997 /* provided sample buffer is not big enough */
998 hInfo->error = 27;
999 return NULL;
1001 hDecoder->alloced_channels = output_channels;
1004 if (sample_buffer_size == 0)
1006 sample_buffer = hDecoder->sample_buffer;
1007 } else {
1008 sample_buffer = *sample_buffer2;
1011 #ifdef SBR_DEC
1012 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1014 uint8_t ele;
1016 /* this data is different when SBR is used or when the data is upsampled */
1017 if (!hDecoder->downSampledSBR)
1019 frame_len *= 2;
1020 hInfo->samples *= 2;
1021 hInfo->samplerate *= 2;
1024 /* check if every element was provided with SBR data */
1025 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1027 if (hDecoder->sbr[ele] == NULL)
1029 hInfo->error = 25;
1030 goto error;
1034 /* sbr */
1035 if (hDecoder->sbr_present_flag == 1)
1037 hInfo->object_type = HE_AAC;
1038 hInfo->sbr = SBR_UPSAMPLED;
1039 } else {
1040 hInfo->sbr = NO_SBR_UPSAMPLED;
1042 if (hDecoder->downSampledSBR)
1044 hInfo->sbr = SBR_DOWNSAMPLED;
1047 #endif
1049 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1050 output_channels, frame_len, hDecoder->config.outputFormat);
1053 hDecoder->postSeekResetFlag = 0;
1055 hDecoder->frame++;
1056 #ifdef LD_DEC
1057 if (hDecoder->object_type != LD)
1059 #endif
1060 if (hDecoder->frame <= 1)
1061 hInfo->samples = 0;
1062 #ifdef LD_DEC
1063 } else {
1064 /* LD encoders will give lower delay */
1065 if (hDecoder->frame <= 0)
1066 hInfo->samples = 0;
1068 #endif
1070 /* cleanup */
1071 #ifdef ANALYSIS
1072 fflush(stdout);
1073 #endif
1075 #ifdef PROFILE
1076 count = faad_get_ts() - count;
1077 hDecoder->cycles += count;
1078 #endif
1080 return sample_buffer;
1082 error:
1084 faad_endbits(&ld);
1086 /* cleanup */
1087 #ifdef ANALYSIS
1088 fflush(stdout);
1089 #endif
1091 return NULL;