]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/decoder.c
ASS!!
[16.git] / src / lib / doslib / ext / faad / decoder.c
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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.
14 ** 
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.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: decoder.c,v 1.117 2009/02/05 00:51:03 menno Exp $
29 **/
30
31 #include "common.h"
32 #include "structs.h"
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "mp4.h"
39 #include "syntax.h"
40 #include "error.h"
41 #include "output.h"
42 #include "filtbank.h"
43 #include "drc.h"
44 #ifdef SBR_DEC
45 #include "sbr_dec.h"
46 #include "sbr_synt.h"
47 #endif
48 #ifdef SSR_DEC
49 #include "ssr.h"
50 #endif
51
52 #ifdef ANALYSIS
53 uint16_t dbg_count;
54 #endif
55
56 /* static function declarations */
57 static void* aac_frame_decode(NeAACDecStruct *hDecoder,
58                               NeAACDecFrameInfo *hInfo,
59                               unsigned char *buffer,
60                               unsigned long buffer_size,
61                               void **sample_buffer2,
62                               unsigned long sample_buffer_size);
63 static void create_channel_config(NeAACDecStruct *hDecoder,
64                                   NeAACDecFrameInfo *hInfo);
65
66
67 char* NEAACDECAPI NeAACDecGetErrorMessage(unsigned char errcode)
68 {
69     if (errcode >= NUM_ERROR_MESSAGES)
70         return NULL;
71     return err_msg[errcode];
72 }
73
74 unsigned long NEAACDECAPI NeAACDecGetCapabilities(void)
75 {
76     uint32_t cap = 0;
77
78     /* can't do without it */
79     cap += LC_DEC_CAP;
80
81 #ifdef MAIN_DEC
82     cap += MAIN_DEC_CAP;
83 #endif
84 #ifdef LTP_DEC
85     cap += LTP_DEC_CAP;
86 #endif
87 #ifdef LD_DEC
88     cap += LD_DEC_CAP;
89 #endif
90 #ifdef ERROR_RESILIENCE
91     cap += ERROR_RESILIENCE_CAP;
92 #endif
93 #ifdef FIXED_POINT
94     cap += FIXED_POINT_CAP;
95 #endif
96
97     return cap;
98 }
99
100 const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 };
101 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
102 {
103     uint8_t i;
104     NeAACDecStruct *hDecoder = NULL;
105
106     if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
107         return NULL;
108
109     memset(hDecoder, 0, sizeof(NeAACDecStruct));
110
111     hDecoder->cmes = mes;
112     hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
113     hDecoder->config.defObjectType = MAIN;
114     hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
115     hDecoder->config.downMatrix = 0;
116     hDecoder->adts_header_present = 0;
117     hDecoder->adif_header_present = 0;
118         hDecoder->latm_header_present = 0;
119 #ifdef ERROR_RESILIENCE
120     hDecoder->aacSectionDataResilienceFlag = 0;
121     hDecoder->aacScalefactorDataResilienceFlag = 0;
122     hDecoder->aacSpectralDataResilienceFlag = 0;
123 #endif
124     hDecoder->frameLength = 1024;
125
126     hDecoder->frame = 0;
127     hDecoder->sample_buffer = NULL;
128
129     hDecoder->__r1 = 1;
130     hDecoder->__r2 = 1;
131
132     for (i = 0; i < MAX_CHANNELS; i++)
133     {
134         hDecoder->window_shape_prev[i] = 0;
135         hDecoder->time_out[i] = NULL;
136         hDecoder->fb_intermed[i] = NULL;
137 #ifdef SSR_DEC
138         hDecoder->ssr_overlap[i] = NULL;
139         hDecoder->prev_fmd[i] = NULL;
140 #endif
141 #ifdef MAIN_DEC
142         hDecoder->pred_stat[i] = NULL;
143 #endif
144 #ifdef LTP_DEC
145         hDecoder->ltp_lag[i] = 0;
146         hDecoder->lt_pred_stat[i] = NULL;
147 #endif
148     }
149
150 #ifdef SBR_DEC
151     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
152     {
153         hDecoder->sbr[i] = NULL;
154     }
155 #endif
156
157     hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
158
159     return hDecoder;
160 }
161
162 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
163 {
164     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
165     if (hDecoder)
166     {
167         NeAACDecConfigurationPtr config = &(hDecoder->config);
168
169         return config;
170     }
171
172     return NULL;
173 }
174
175 unsigned char NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
176                                                    NeAACDecConfigurationPtr config)
177 {
178     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
179     if (hDecoder && config)
180     {
181         /* check if we can decode this object type */
182         if (can_decode_ot(config->defObjectType) < 0)
183             return 0;
184         hDecoder->config.defObjectType = config->defObjectType;
185
186         /* samplerate: anything but 0 should be possible */
187         if (config->defSampleRate == 0)
188             return 0;
189         hDecoder->config.defSampleRate = config->defSampleRate;
190
191         /* check output format */
192 #ifdef FIXED_POINT
193         if ((config->outputFormat < 1) || (config->outputFormat > 4))
194             return 0;
195 #else
196         if ((config->outputFormat < 1) || (config->outputFormat > 5))
197             return 0;
198 #endif
199         hDecoder->config.outputFormat = config->outputFormat;
200
201         if (config->downMatrix > 1)
202             return 0;
203         hDecoder->config.downMatrix = config->downMatrix;
204
205         /* OK */
206         return 1;
207     }
208
209     return 0;
210 }
211
212
213 static int latmCheck(latm_header *latm, bitfile *ld)
214 {
215     uint32_t good=0, bad=0, bits, m;
216
217     while (ld->bytes_left)
218     {
219         bits = faad_latm_frame(latm, ld);
220         if(bits==-1U)
221             bad++;
222         else
223         {
224             good++;
225             while(bits>0)
226             {
227                 m = min(bits, 8);
228                 faad_getbits(ld, m);
229                 bits -= m;
230             }
231         }
232     }
233
234     return (good>0);
235 }
236
237
238 long NEAACDECAPI NeAACDecInit(NeAACDecHandle hpDecoder,
239                               unsigned char *buffer,
240                               unsigned long buffer_size,
241                               unsigned long *samplerate,
242                               unsigned char *channels)
243 {
244     uint32_t bits = 0;
245     bitfile ld;
246     adif_header adif;
247     adts_header adts;
248     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
249
250
251     if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
252         return -1;
253
254     hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
255     hDecoder->object_type = hDecoder->config.defObjectType;
256     *samplerate = get_sample_rate(hDecoder->sf_index);
257     *channels = 1;
258
259     if (buffer != NULL)
260     {
261 #if 0
262         int is_latm;
263         latm_header *l = &hDecoder->latm_config;
264 #endif
265
266         faad_initbits(&ld, buffer, buffer_size);
267  
268 #if 0
269         memset(l, 0, sizeof(latm_header));
270         is_latm = latmCheck(l, &ld);
271         l->inited = 0;
272         l->frameLength = 0;
273         faad_rewindbits(&ld);
274         if(is_latm && l->ASCbits>0)
275         {
276             int32_t x;
277             hDecoder->latm_header_present = 1;
278             x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
279             if(x!=0)
280                 hDecoder->latm_header_present = 0;
281             return x;
282         } else
283 #endif
284         /* Check if an ADIF header is present */
285         if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
286                         (buffer[2] == 'I') && (buffer[3] == 'F'))
287                 {
288             hDecoder->adif_header_present = 1;
289
290             get_adif_header(&adif, &ld);
291             faad_byte_align(&ld);
292
293             hDecoder->sf_index = adif.pce[0].sf_index;
294             hDecoder->object_type = adif.pce[0].object_type + 1;
295
296             *samplerate = get_sample_rate(hDecoder->sf_index);
297             *channels = adif.pce[0].channels;
298
299             memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
300             hDecoder->pce_set = 1;
301
302             bits = bit2byte(faad_get_processed_bits(&ld));
303
304         /* Check if an ADTS header is present */
305         } else if (faad_showbits(&ld, 12) == 0xfff) {
306             hDecoder->adts_header_present = 1;
307
308             adts.old_format = hDecoder->config.useOldADTSFormat;
309             adts_frame(&adts, &ld);
310
311             hDecoder->sf_index = adts.sf_index;
312             hDecoder->object_type = adts.profile + 1;
313
314             *samplerate = get_sample_rate(hDecoder->sf_index);
315             *channels = (adts.channel_configuration > 6) ?
316                 2 : adts.channel_configuration;
317         }
318
319         if (ld.error)
320         {
321             faad_endbits(&ld);
322             return -1;
323         }
324         faad_endbits(&ld);
325     }
326
327 #if (defined(PS_DEC) || defined(DRM_PS))
328     /* check if we have a mono file */
329     if (*channels == 1)
330     {
331         /* upMatrix to 2 channels for implicit signalling of PS */
332         *channels = 2;
333     }
334 #endif
335
336     hDecoder->channelConfiguration = *channels;
337
338 #ifdef SBR_DEC
339     /* implicit signalling */
340     if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
341     {
342         *samplerate *= 2;
343         hDecoder->forceUpSampling = 1;
344     } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
345         hDecoder->downSampledSBR = 1;
346     }
347 #endif
348
349     /* must be done before frameLength is divided by 2 for LD */
350 #ifdef SSR_DEC
351     if (hDecoder->object_type == SSR)
352         hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
353     else
354 #endif
355         hDecoder->fb = filter_bank_init(hDecoder->frameLength);
356
357 #ifdef LD_DEC
358     if (hDecoder->object_type == LD)
359         hDecoder->frameLength >>= 1;
360 #endif
361
362     if (can_decode_ot(hDecoder->object_type) < 0)
363         return -1;
364
365     return bits;
366 }
367
368 /* Init the library using a DecoderSpecificInfo */
369 char NEAACDECAPI NeAACDecInit2(NeAACDecHandle hpDecoder,
370                                unsigned char *pBuffer,
371                                unsigned long SizeOfDecoderSpecificInfo,
372                                unsigned long *samplerate,
373                                unsigned char *channels)
374 {
375     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
376     int8_t rc;
377     mp4AudioSpecificConfig mp4ASC;
378
379     if((hDecoder == NULL)
380         || (pBuffer == NULL)
381         || (SizeOfDecoderSpecificInfo < 2)
382         || (samplerate == NULL)
383         || (channels == NULL))
384     {
385         return -1;
386     }
387
388     hDecoder->adif_header_present = 0;
389     hDecoder->adts_header_present = 0;
390
391     /* decode the audio specific config */
392     rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
393         &(hDecoder->pce), hDecoder->latm_header_present);
394
395     /* copy the relevant info to the decoder handle */
396     *samplerate = mp4ASC.samplingFrequency;
397     if (mp4ASC.channelsConfiguration)
398     {
399         *channels = mp4ASC.channelsConfiguration;
400     } else {
401         *channels = hDecoder->pce.channels;
402         hDecoder->pce_set = 1;
403     }
404 #if (defined(PS_DEC) || defined(DRM_PS))
405     /* check if we have a mono file */
406     if (*channels == 1)
407     {
408         /* upMatrix to 2 channels for implicit signalling of PS */
409         *channels = 2;
410     }
411 #endif
412     hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
413     hDecoder->object_type = mp4ASC.objectTypeIndex;
414 #ifdef ERROR_RESILIENCE
415     hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
416     hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
417     hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
418 #endif
419 #ifdef SBR_DEC
420     hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
421     hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
422     if (hDecoder->config.dontUpSampleImplicitSBR == 0)
423         hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
424     else
425         hDecoder->forceUpSampling = 0;
426
427     /* AAC core decoder samplerate is 2 times as low */
428     if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
429     {
430         hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
431     }
432 #endif
433
434     if (rc != 0)
435     {
436         return rc;
437     }
438     hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
439     if (mp4ASC.frameLengthFlag)
440 #ifdef ALLOW_SMALL_FRAMELENGTH
441         hDecoder->frameLength = 960;
442 #else
443         return -1;
444 #endif
445
446     /* must be done before frameLength is divided by 2 for LD */
447 #ifdef SSR_DEC
448     if (hDecoder->object_type == SSR)
449         hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
450     else
451 #endif
452         hDecoder->fb = filter_bank_init(hDecoder->frameLength);
453
454 #ifdef LD_DEC
455     if (hDecoder->object_type == LD)
456         hDecoder->frameLength >>= 1;
457 #endif
458
459     return 0;
460 }
461
462 #ifdef DRM
463 char NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
464                                  unsigned long samplerate,
465                                  unsigned char channels)
466 {
467     NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
468     if (hDecoder == NULL)
469         return 1; /* error */
470
471     NeAACDecClose(*hDecoder);
472
473     *hDecoder = NeAACDecOpen();
474
475     /* Special object type defined for DRM */
476     (*hDecoder)->config.defObjectType = DRM_ER_LC;
477
478     (*hDecoder)->config.defSampleRate = samplerate;
479 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
480     (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
481     (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
482     (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
483 #endif
484     (*hDecoder)->frameLength = 960;
485     (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
486     (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
487
488     if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
489         (*hDecoder)->channelConfiguration = 2;
490     else
491         (*hDecoder)->channelConfiguration = 1;
492
493 #ifdef SBR_DEC
494     if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
495         (*hDecoder)->sbr_present_flag = 0;
496     else
497         (*hDecoder)->sbr_present_flag = 1;
498 #endif
499
500     (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
501
502     return 0;
503 }
504 #endif
505
506 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hpDecoder)
507 {
508     uint8_t i;
509     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
510
511     if (hDecoder == NULL)
512         return;
513
514 #ifdef PROFILE
515     printf("AAC decoder total:  %I64d cycles\n", hDecoder->cycles);
516     printf("requant:            %I64d cycles\n", hDecoder->requant_cycles);
517     printf("spectral_data:      %I64d cycles\n", hDecoder->spectral_cycles);
518     printf("scalefactors:       %I64d cycles\n", hDecoder->scalefac_cycles);
519     printf("output:             %I64d cycles\n", hDecoder->output_cycles);
520 #endif
521
522     for (i = 0; i < MAX_CHANNELS; i++)
523     {
524         if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
525         if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
526 #ifdef SSR_DEC
527         if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
528         if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
529 #endif
530 #ifdef MAIN_DEC
531         if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
532 #endif
533 #ifdef LTP_DEC
534         if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
535 #endif
536     }
537
538 #ifdef SSR_DEC
539     if (hDecoder->object_type == SSR)
540         ssr_filter_bank_end(hDecoder->fb);
541     else
542 #endif
543         filter_bank_end(hDecoder->fb);
544
545     drc_end(hDecoder->drc);
546
547     if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
548
549 #ifdef SBR_DEC
550     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
551     {
552         if (hDecoder->sbr[i])
553             sbrDecodeEnd(hDecoder->sbr[i]);
554     }
555 #endif
556
557     if (hDecoder) faad_free(hDecoder);
558 }
559
560 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
561 {
562     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
563     if (hDecoder)
564     {
565         hDecoder->postSeekResetFlag = 1;
566
567         if (frame != -1)
568             hDecoder->frame = frame;
569     }
570 }
571
572 static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
573 {
574     hInfo->num_front_channels = 0;
575     hInfo->num_side_channels = 0;
576     hInfo->num_back_channels = 0;
577     hInfo->num_lfe_channels = 0;
578     memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
579
580     if (hDecoder->downMatrix)
581     {
582         hInfo->num_front_channels = 2;
583         hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
584         hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
585         return;
586     }
587
588     /* check if there is a PCE */
589     if (hDecoder->pce_set)
590     {
591         uint8_t i, chpos = 0;
592         uint8_t chdir, back_center = 0;
593
594         hInfo->num_front_channels = hDecoder->pce.num_front_channels;
595         hInfo->num_side_channels = hDecoder->pce.num_side_channels;
596         hInfo->num_back_channels = hDecoder->pce.num_back_channels;
597         hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
598
599         chdir = hInfo->num_front_channels;
600         if (chdir & 1)
601         {
602 #if (defined(PS_DEC) || defined(DRM_PS))
603             /* When PS is enabled output is always stereo */
604             hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
605             hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
606 #else
607             hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
608             chdir--;
609 #endif
610         }
611         for (i = 0; i < chdir; i += 2)
612         {
613             hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
614             hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
615         }
616
617         for (i = 0; i < hInfo->num_side_channels; i += 2)
618         {
619             hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
620             hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
621         }
622
623         chdir = hInfo->num_back_channels;
624         if (chdir & 1)
625         {
626             back_center = 1;
627             chdir--;
628         }
629         for (i = 0; i < chdir; i += 2)
630         {
631             hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
632             hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
633         }
634         if (back_center)
635         {
636             hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
637         }
638
639         for (i = 0; i < hInfo->num_lfe_channels; i++)
640         {
641             hInfo->channel_position[chpos++] = LFE_CHANNEL;
642         }
643
644     } else {
645         switch (hDecoder->channelConfiguration)
646         {
647         case 1:
648 #if (defined(PS_DEC) || defined(DRM_PS))
649             /* When PS is enabled output is always stereo */
650             hInfo->num_front_channels = 2;
651             hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
652             hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
653 #else
654             hInfo->num_front_channels = 1;
655             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
656 #endif
657             break;
658         case 2:
659             hInfo->num_front_channels = 2;
660             hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
661             hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
662             break;
663         case 3:
664             hInfo->num_front_channels = 3;
665             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
666             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
667             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
668             break;
669         case 4:
670             hInfo->num_front_channels = 3;
671             hInfo->num_back_channels = 1;
672             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
673             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
674             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
675             hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
676             break;
677         case 5:
678             hInfo->num_front_channels = 3;
679             hInfo->num_back_channels = 2;
680             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
681             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
682             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
683             hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
684             hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
685             break;
686         case 6:
687             hInfo->num_front_channels = 3;
688             hInfo->num_back_channels = 2;
689             hInfo->num_lfe_channels = 1;
690             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
691             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
692             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
693             hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
694             hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
695             hInfo->channel_position[5] = LFE_CHANNEL;
696             break;
697         case 7:
698             hInfo->num_front_channels = 3;
699             hInfo->num_side_channels = 2;
700             hInfo->num_back_channels = 2;
701             hInfo->num_lfe_channels = 1;
702             hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
703             hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
704             hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
705             hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
706             hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
707             hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
708             hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
709             hInfo->channel_position[7] = LFE_CHANNEL;
710             break;
711         default: /* channelConfiguration == 0 || channelConfiguration > 7 */
712             {
713                 uint8_t i;
714                 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
715                 if (ch & 1) /* there's either a center front or a center back channel */
716                 {
717                     uint8_t ch1 = (ch-1)/2;
718                     if (hDecoder->first_syn_ele == ID_SCE)
719                     {
720                         hInfo->num_front_channels = ch1 + 1;
721                         hInfo->num_back_channels = ch1;
722                         hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
723                         for (i = 1; i <= ch1; i+=2)
724                         {
725                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
726                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
727                         }
728                         for (i = ch1+1; i < ch; i+=2)
729                         {
730                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
731                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
732                         }
733                     } else {
734                         hInfo->num_front_channels = ch1;
735                         hInfo->num_back_channels = ch1 + 1;
736                         for (i = 0; i < ch1; i+=2)
737                         {
738                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
739                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
740                         }
741                         for (i = ch1; i < ch-1; i+=2)
742                         {
743                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
744                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
745                         }
746                         hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
747                     }
748                 } else {
749                     uint8_t ch1 = (ch)/2;
750                     hInfo->num_front_channels = ch1;
751                     hInfo->num_back_channels = ch1;
752                     if (ch1 & 1)
753                     {
754                         hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
755                         for (i = 1; i <= ch1; i+=2)
756                         {
757                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
758                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
759                         }
760                         for (i = ch1+1; i < ch-1; i+=2)
761                         {
762                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
763                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
764                         }
765                         hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
766                     } else {
767                         for (i = 0; i < ch1; i+=2)
768                         {
769                             hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770                             hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771                         }
772                         for (i = ch1; i < ch; i+=2)
773                         {
774                             hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775                             hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776                         }
777                     }
778                 }
779                 hInfo->num_lfe_channels = hDecoder->has_lfe;
780                 for (i = ch; i < hDecoder->fr_channels; i++)
781                 {
782                     hInfo->channel_position[i] = LFE_CHANNEL;
783                 }
784             }
785             break;
786         }
787     }
788 }
789
790 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hpDecoder,
791                                  NeAACDecFrameInfo *hInfo,
792                                  unsigned char *buffer,
793                                  unsigned long buffer_size)
794 {
795     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
796     return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
797 }
798
799 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hpDecoder,
800                                   NeAACDecFrameInfo *hInfo,
801                                   unsigned char *buffer,
802                                   unsigned long buffer_size,
803                                   void **sample_buffer,
804                                   unsigned long sample_buffer_size)
805 {
806     NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
807     if ((sample_buffer == NULL) || (sample_buffer_size == 0))
808     {
809         hInfo->error = 27;
810         return NULL;
811     }
812
813     return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
814         sample_buffer, sample_buffer_size);
815 }
816
817 #ifdef DRM
818
819 #define ERROR_STATE_INIT 6
820
821 static void conceal_output(NeAACDecStruct *hDecoder, uint16_t frame_len,
822                            uint8_t out_ch, void *sample_buffer)
823 {
824     return;
825 }
826 #endif
827
828 static void* aac_frame_decode(NeAACDecStruct *hDecoder,
829                               NeAACDecFrameInfo *hInfo,
830                               unsigned char *buffer,
831                               unsigned long buffer_size,
832                               void **sample_buffer2,
833                               unsigned long sample_buffer_size)
834 {
835     uint16_t i;
836     uint8_t channels = 0;
837     uint8_t output_channels = 0;
838     bitfile ld = {0};
839     uint32_t bitsconsumed;
840     uint16_t frame_len;
841     void *sample_buffer;
842     uint32_t startbit=0, endbit=0, payload_bits=0;
843
844 #ifdef PROFILE
845     int64_t count = faad_get_ts();
846 #endif
847
848     /* safety checks */
849     if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
850     {
851         return NULL;
852     }
853
854 #if 0
855     printf("%d\n", buffer_size*8);
856 #endif
857
858     frame_len = hDecoder->frameLength;
859
860
861     memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
862     memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
863
864 #ifdef USE_TIME_LIMIT
865     if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
866     {
867         hDecoder->TL_count += 1024;
868     } else {
869         hInfo->error = (NUM_ERROR_MESSAGES-1);
870         goto error;
871     }
872 #endif
873
874
875     /* check for some common metadata tag types in the bitstream
876      * No need to return an error
877      */
878     /* ID3 */
879     if (buffer_size >= 128)
880     {
881         if (memcmp(buffer, "TAG", 3) == 0)
882         {
883             /* found it */
884             hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
885             /* no error, but no output either */
886             return NULL;
887         }
888     }
889
890
891     /* initialize the bitstream */
892     faad_initbits(&ld, buffer, buffer_size);
893
894 #if 0
895     {
896         int i;
897         for (i = 0; i < ((buffer_size+3)>>2); i++)
898         {
899             uint8_t *buf;
900             uint32_t temp = 0;
901             buf = faad_getbitbuffer(&ld, 32);
902             //temp = getdword((void*)buf);
903             temp = *((uint32_t*)buf);
904             printf("0x%.8X\n", temp);
905             free(buf);
906         }
907         faad_endbits(&ld);
908         faad_initbits(&ld, buffer, buffer_size);
909     }
910 #endif
911
912 #if 0
913     if(hDecoder->latm_header_present)
914     {
915         payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
916         startbit = faad_get_processed_bits(&ld);
917         if(payload_bits == -1U)
918         {
919             hInfo->error = 1;
920             goto error;
921         }
922     }
923 #endif
924
925 #ifdef DRM
926     if (hDecoder->object_type == DRM_ER_LC)
927     {
928         /* We do not support stereo right now */
929         if (0) //(hDecoder->channelConfiguration == 2)
930         {
931             hInfo->error = 28; // Throw CRC error
932             goto error;
933         }
934
935         faad_getbits(&ld, 8
936             DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
937     }
938 #endif
939
940     if (hDecoder->adts_header_present)
941     {
942         adts_header adts;
943
944         adts.old_format = hDecoder->config.useOldADTSFormat;
945         if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
946             goto error;
947
948         /* MPEG2 does byte_alignment() here,
949          * but ADTS header is always multiple of 8 bits in MPEG2
950          * so not needed to actually do it.
951          */
952     }
953
954 #ifdef ANALYSIS
955     dbg_count = 0;
956 #endif
957
958     /* decode the complete bitstream */
959 #ifdef DRM
960     if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC))
961     {
962         DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
963     } else {
964 #endif
965         raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
966 #ifdef DRM
967     }
968 #endif
969
970 #if 0
971     if(hDecoder->latm_header_present)
972     {
973         endbit = faad_get_processed_bits(&ld);
974         if(endbit-startbit > payload_bits)
975             fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
976                 endbit-startbit, payload_bits);
977         if(hDecoder->latm_config.otherDataLenBits > 0)
978             faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
979         faad_byte_align(&ld);
980     }
981 #endif
982
983     channels = hDecoder->fr_channels;
984
985     if (hInfo->error > 0)
986         goto error;
987
988     /* safety check */
989     if (channels == 0 || channels > MAX_CHANNELS)
990     {
991         /* invalid number of channels */
992         hInfo->error = 12;
993         goto error;
994     }
995
996     /* no more bit reading after this */
997     bitsconsumed = faad_get_processed_bits(&ld);
998     hInfo->bytesconsumed = bit2byte(bitsconsumed);
999     if (ld.error)
1000     {
1001         hInfo->error = 14;
1002         goto error;
1003     }
1004     faad_endbits(&ld);
1005
1006
1007     if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1008 #if 0
1009         && !hDecoder->latm_header_present
1010 #endif
1011         )
1012     {
1013         if (hDecoder->channelConfiguration == 0)
1014             hDecoder->channelConfiguration = channels;
1015
1016         if (channels == 8) /* 7.1 */
1017             hDecoder->channelConfiguration = 7;
1018         if (channels == 7) /* not a standard channelConfiguration */
1019             hDecoder->channelConfiguration = 0;
1020     }
1021
1022     if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1023     {
1024         hDecoder->downMatrix = 1;
1025         output_channels = 2;
1026     } else {
1027         output_channels = channels;
1028     }
1029
1030 #if (defined(PS_DEC) || defined(DRM_PS))
1031     hDecoder->upMatrix = 0;
1032     /* check if we have a mono file */
1033     if (output_channels == 1)
1034     {
1035         /* upMatrix to 2 channels for implicit signalling of PS */
1036         hDecoder->upMatrix = 1;
1037         output_channels = 2;
1038     }
1039 #endif
1040
1041     /* Make a channel configuration based on either a PCE or a channelConfiguration */
1042     create_channel_config(hDecoder, hInfo);
1043
1044     /* number of samples in this frame */
1045     hInfo->samples = frame_len*output_channels;
1046     /* number of channels in this frame */
1047     hInfo->channels = output_channels;
1048     /* samplerate */
1049     hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1050     /* object type */
1051     hInfo->object_type = hDecoder->object_type;
1052     /* sbr */
1053     hInfo->sbr = NO_SBR;
1054     /* header type */
1055     hInfo->header_type = RAW;
1056     if (hDecoder->adif_header_present)
1057         hInfo->header_type = ADIF;
1058     if (hDecoder->adts_header_present)
1059         hInfo->header_type = ADTS;
1060 #if 0
1061     if (hDecoder->latm_header_present)
1062         hInfo->header_type = LATM;
1063 #endif
1064 #if (defined(PS_DEC) || defined(DRM_PS))
1065     hInfo->ps = hDecoder->ps_used_global;
1066 #endif
1067
1068     /* check if frame has channel elements */
1069     if (channels == 0)
1070     {
1071         hDecoder->frame++;
1072         return NULL;
1073     }
1074
1075     /* allocate the buffer for the final samples */
1076     if ((hDecoder->sample_buffer == NULL) ||
1077         (hDecoder->alloced_channels != output_channels))
1078     {
1079         static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1080             sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1081             sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1082         };
1083         uint8_t stride = str[hDecoder->config.outputFormat-1];
1084 #ifdef SBR_DEC
1085         if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1086         {
1087             stride = 2 * stride;
1088         }
1089 #endif
1090         /* check if we want to use internal sample_buffer */
1091         if (sample_buffer_size == 0)
1092         {
1093             if (hDecoder->sample_buffer)
1094                 faad_free(hDecoder->sample_buffer);
1095             hDecoder->sample_buffer = NULL;
1096             hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
1097         } else if (sample_buffer_size < frame_len*output_channels*stride) {
1098             /* provided sample buffer is not big enough */
1099             hInfo->error = 27;
1100             return NULL;
1101         }
1102         hDecoder->alloced_channels = output_channels;
1103     }
1104
1105     if (sample_buffer_size == 0)
1106     {
1107         sample_buffer = hDecoder->sample_buffer;
1108     } else {
1109         sample_buffer = *sample_buffer2;
1110     }
1111
1112 #ifdef SBR_DEC
1113     if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1114     {
1115         uint8_t ele;
1116
1117         /* this data is different when SBR is used or when the data is upsampled */
1118         if (!hDecoder->downSampledSBR)
1119         {
1120             frame_len *= 2;
1121             hInfo->samples *= 2;
1122             hInfo->samplerate *= 2;
1123         }
1124
1125         /* check if every element was provided with SBR data */
1126         for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1127         {
1128             if (hDecoder->sbr[ele] == NULL)
1129             {
1130                 hInfo->error = 25;
1131                 goto error;
1132             }
1133         }
1134
1135         /* sbr */
1136         if (hDecoder->sbr_present_flag == 1)
1137         {
1138             hInfo->object_type = HE_AAC;
1139             hInfo->sbr = SBR_UPSAMPLED;
1140         } else {
1141             hInfo->sbr = NO_SBR_UPSAMPLED;
1142         }
1143         if (hDecoder->downSampledSBR)
1144         {
1145             hInfo->sbr = SBR_DOWNSAMPLED;
1146         }
1147     }
1148 #endif
1149
1150
1151     sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1152         output_channels, frame_len, hDecoder->config.outputFormat);
1153
1154
1155 #ifdef DRM
1156     //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1157 #endif
1158
1159
1160     hDecoder->postSeekResetFlag = 0;
1161
1162     hDecoder->frame++;
1163 #ifdef LD_DEC
1164     if (hDecoder->object_type != LD)
1165     {
1166 #endif
1167         if (hDecoder->frame <= 1)
1168             hInfo->samples = 0;
1169 #ifdef LD_DEC
1170     } else {
1171         /* LD encoders will give lower delay */
1172         if (hDecoder->frame <= 0)
1173             hInfo->samples = 0;
1174     }
1175 #endif
1176
1177     /* cleanup */
1178 #ifdef ANALYSIS
1179     fflush(stdout);
1180 #endif
1181
1182 #ifdef PROFILE
1183     count = faad_get_ts() - count;
1184     hDecoder->cycles += count;
1185 #endif
1186
1187     return sample_buffer;
1188
1189 error:
1190
1191
1192 #ifdef DRM
1193     hDecoder->error_state = ERROR_STATE_INIT;
1194 #endif
1195
1196     /* reset filterbank state */
1197     for (i = 0; i < MAX_CHANNELS; i++)
1198     {
1199         if (hDecoder->fb_intermed[i] != NULL)
1200         {
1201             memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1202         }
1203     }
1204 #ifdef SBR_DEC
1205     for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1206     {
1207         if (hDecoder->sbr[i] != NULL)
1208         {
1209             sbrReset(hDecoder->sbr[i]);
1210         }
1211     }
1212 #endif
1213
1214
1215     faad_endbits(&ld);
1216
1217     /* cleanup */
1218 #ifdef ANALYSIS
1219     fflush(stdout);
1220 #endif
1221
1222     return NULL;
1223 }