rpm: Remove MEncoder from rpm packaging
[mplayer/glamo.git] / libfaad2 / decoder.c
blob710a3cf796964a2bada88580e99fda63bf2164c7
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, int latm_stream)
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 if (latm_stream) {
261 return -1;
263 else
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;
299 if (ld.error)
301 faad_endbits(&ld);
302 return -1;
304 faad_endbits(&ld);
306 hDecoder->channelConfiguration = *channels;
308 #if (defined(PS_DEC) || defined(DRM_PS))
309 /* check if we have a mono file */
310 if (*channels == 1)
312 /* upMatrix to 2 channels for implicit signalling of PS */
313 *channels = 2;
315 #endif
317 #ifdef SBR_DEC
318 /* implicit signalling */
319 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
321 *samplerate *= 2;
322 hDecoder->forceUpSampling = 1;
323 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
324 hDecoder->downSampledSBR = 1;
326 #endif
328 /* must be done before frameLength is divided by 2 for LD */
329 #ifdef SSR_DEC
330 if (hDecoder->object_type == SSR)
331 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
332 else
333 #endif
334 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
336 #ifdef LD_DEC
337 if (hDecoder->object_type == LD)
338 hDecoder->frameLength >>= 1;
339 #endif
341 if (can_decode_ot(hDecoder->object_type) < 0)
342 return -1;
344 return bits;
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)
352 int8_t rc;
353 mp4AudioSpecificConfig mp4ASC;
355 if((hDecoder == NULL)
356 || (pBuffer == NULL)
357 || (SizeOfDecoderSpecificInfo < 2)
358 || (samplerate == NULL)
359 || (channels == NULL))
361 return -1;
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;
376 } else {
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 */
382 if (*channels == 1)
384 /* upMatrix to 2 channels for implicit signalling of PS */
385 *channels = 2;
387 #endif
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;
394 #endif
395 #ifdef SBR_DEC
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;
400 else
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);
408 #endif
410 if (rc != 0)
412 return rc;
414 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
415 if (mp4ASC.frameLengthFlag)
416 #ifdef ALLOW_SMALL_FRAMELENGTH
417 hDecoder->frameLength = 960;
418 #else
419 return -1;
420 #endif
422 /* must be done before frameLength is divided by 2 for LD */
423 #ifdef SSR_DEC
424 if (hDecoder->object_type == SSR)
425 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
426 else
427 #endif
428 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
430 #ifdef LD_DEC
431 if (hDecoder->object_type == LD)
432 hDecoder->frameLength >>= 1;
433 #endif
435 return 0;
438 #ifdef DRM
439 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
440 uint8_t channels)
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 */
457 #endif
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;
464 else
465 (*hDecoder)->channelConfiguration = 1;
467 #ifdef SBR_DEC
468 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
469 (*hDecoder)->sbr_present_flag = 0;
470 else
471 (*hDecoder)->sbr_present_flag = 1;
472 #endif
474 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
476 return 0;
478 #endif
480 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
482 uint8_t i;
484 if (hDecoder == NULL)
485 return;
487 #ifdef PROFILE
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);
493 #endif
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]);
499 #ifdef SSR_DEC
500 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
501 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
502 #endif
503 #ifdef MAIN_DEC
504 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
505 #endif
506 #ifdef LTP_DEC
507 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
508 #endif
511 #ifdef SSR_DEC
512 if (hDecoder->object_type == SSR)
513 ssr_filter_bank_end(hDecoder->fb);
514 else
515 #endif
516 filter_bank_end(hDecoder->fb);
518 drc_end(hDecoder->drc);
520 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
522 #ifdef SBR_DEC
523 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
525 if (hDecoder->sbr[i])
526 sbrDecodeEnd(hDecoder->sbr[i]);
528 #endif
530 if (hDecoder) faad_free(hDecoder);
533 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
535 if (hDecoder)
537 hDecoder->postSeekResetFlag = 1;
539 if (frame != -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;
557 return;
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;
572 if (chdir & 1)
574 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
575 chdir--;
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;
590 if (chdir & 1)
592 back_center = 1;
593 chdir--;
595 for (i = 0; i < chdir; i += 2)
597 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
598 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
600 if (back_center)
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;
610 } else {
611 switch (hDecoder->channelConfiguration)
613 case 1:
614 hInfo->num_front_channels = 1;
615 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
616 break;
617 case 2:
618 hInfo->num_front_channels = 2;
619 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
620 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
621 break;
622 case 3:
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;
627 break;
628 case 4:
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;
635 break;
636 case 5:
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;
644 break;
645 case 6:
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;
655 break;
656 case 7:
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;
669 break;
670 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
672 uint8_t i;
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;
692 } else {
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;
707 } else {
708 uint8_t ch1 = (ch)/2;
709 hInfo->num_front_channels = ch1;
710 hInfo->num_back_channels = ch1;
711 if (ch1 & 1)
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;
725 } else {
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;
744 break;
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))
763 hInfo->error = 27;
764 return NULL;
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;
777 bitfile ld;
778 uint32_t bitsconsumed;
779 uint16_t frame_len;
780 void *sample_buffer;
781 uint32_t startbit=0, endbit=0, payload_bits=0;
783 #ifdef PROFILE
784 int64_t count = faad_get_ts();
785 #endif
787 /* safety checks */
788 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
790 return NULL;
793 #if 0
794 printf("%d\n", buffer_size*8);
795 #endif
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);
806 #if 0
808 int i;
809 for (i = 0; i < ((buffer_size+3)>>2); i++)
811 uint8_t *buf;
812 uint32_t temp = 0;
813 buf = faad_getbitbuffer(&ld, 32);
814 //temp = getdword((void*)buf);
815 temp = *((uint32_t*)buf);
816 printf("0x%.8X\n", temp);
817 free(buf);
819 faad_endbits(&ld);
820 faad_initbits(&ld, buffer, buffer_size);
822 #endif
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)
830 hInfo->error = 1;
831 goto error;
835 #ifdef DRM
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
842 goto error;
845 faad_getbits(&ld, 8
846 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
848 #endif
850 if (hDecoder->adts_header_present)
852 adts_header adts;
854 adts.old_format = hDecoder->config.useOldADTSFormat;
855 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
856 goto error;
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.
864 #ifdef ANALYSIS
865 dbg_count = 0;
866 #endif
868 /* decode the complete bitstream */
869 #ifdef SCALABLE_DEC
870 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
872 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
873 } else {
874 #endif
875 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
876 #ifdef SCALABLE_DEC
878 #endif
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)
894 goto error;
896 /* safety check */
897 if (channels == 0 || channels > MAX_CHANNELS)
899 /* invalid number of channels */
900 hInfo->error = 12;
901 goto error;
904 /* no more bit reading after this */
905 bitsconsumed = faad_get_processed_bits(&ld);
906 hInfo->bytesconsumed = bit2byte(bitsconsumed);
907 if (ld.error)
909 hInfo->error = 14;
910 goto error;
912 faad_endbits(&ld);
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;
929 output_channels = 2;
930 } else {
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;
941 output_channels = 2;
943 #endif
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;
952 /* samplerate */
953 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
954 /* object type */
955 hInfo->object_type = hDecoder->object_type;
956 /* sbr */
957 hInfo->sbr = NO_SBR;
958 /* header 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;
968 #endif
970 /* check if frame has channel elements */
971 if (channels == 0)
973 hDecoder->frame++;
974 return NULL;
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];
986 #ifdef SBR_DEC
987 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
989 stride = 2 * stride;
991 #endif
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 */
1001 hInfo->error = 27;
1002 return NULL;
1004 hDecoder->alloced_channels = output_channels;
1007 if (sample_buffer_size == 0)
1009 sample_buffer = hDecoder->sample_buffer;
1010 } else {
1011 sample_buffer = *sample_buffer2;
1014 #ifdef SBR_DEC
1015 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1017 uint8_t ele;
1019 /* this data is different when SBR is used or when the data is upsampled */
1020 if (!hDecoder->downSampledSBR)
1022 frame_len *= 2;
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)
1032 hInfo->error = 25;
1033 goto error;
1037 /* sbr */
1038 if (hDecoder->sbr_present_flag == 1)
1040 hInfo->object_type = HE_AAC;
1041 hInfo->sbr = SBR_UPSAMPLED;
1042 } else {
1043 hInfo->sbr = NO_SBR_UPSAMPLED;
1045 if (hDecoder->downSampledSBR)
1047 hInfo->sbr = SBR_DOWNSAMPLED;
1050 #endif
1052 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1053 output_channels, frame_len, hDecoder->config.outputFormat);
1056 hDecoder->postSeekResetFlag = 0;
1058 hDecoder->frame++;
1059 #ifdef LD_DEC
1060 if (hDecoder->object_type != LD)
1062 #endif
1063 if (hDecoder->frame <= 1)
1064 hInfo->samples = 0;
1065 #ifdef LD_DEC
1066 } else {
1067 /* LD encoders will give lower delay */
1068 if (hDecoder->frame <= 0)
1069 hInfo->samples = 0;
1071 #endif
1073 /* cleanup */
1074 #ifdef ANALYSIS
1075 fflush(stdout);
1076 #endif
1078 #ifdef PROFILE
1079 count = faad_get_ts() - count;
1080 hDecoder->cycles += count;
1081 #endif
1083 return sample_buffer;
1085 error:
1087 faad_endbits(&ld);
1089 /* cleanup */
1090 #ifdef ANALYSIS
1091 fflush(stdout);
1092 #endif
1094 return NULL;