comparison src/aac/libfaad2/decoder.c @ 3004:8867d3491f60

Sync with FAAD 2.7; will fix warnings in separate commits.
author Tony Vroon <chainsaw@gentoo.org>
date Wed, 08 Apr 2009 20:12:57 +0100
parents f1b6f1b2cdb3
children 3d6a2732f26a
comparison
equal deleted inserted replaced
3003:8b7a44631121 3004:8867d3491f60
1 /* 1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com 3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4 ** 4 **
5 ** This program is free software; you can redistribute it and/or modify 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 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 7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version. 8 ** (at your option) any later version.
9 ** 9 **
10 ** This program is distributed in the hope that it will be useful, 10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details. 13 ** GNU General Public License for more details.
14 ** 14 **
15 ** You should have received a copy of the GNU General Public License 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 16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 ** 18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly 19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden. 20 ** forbidden.
21 ** 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 **
22 ** Commercial non-GPL licensing of this software is possible. 25 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
24 ** 27 **
25 ** $Id: decoder.c,v 1.107 2004/09/08 09:43:11 gcp Exp $ 28 ** $Id: decoder.c,v 1.117 2009/02/05 00:51:03 menno Exp $
26 **/ 29 **/
27 30
28 #include "common.h" 31 #include "common.h"
29 #include "structs.h" 32 #include "structs.h"
30 33
31 #include <stdlib.h> 34 #include <stdlib.h>
35 #include <stdio.h>
32 #include <string.h> 36 #include <string.h>
33 37
34 #include "decoder.h"
35 #include "mp4.h" 38 #include "mp4.h"
36 #include "syntax.h" 39 #include "syntax.h"
37 #include "error.h" 40 #include "error.h"
38 #include "output.h" 41 #include "output.h"
39 #include "filtbank.h" 42 #include "filtbank.h"
49 #ifdef ANALYSIS 52 #ifdef ANALYSIS
50 uint16_t dbg_count; 53 uint16_t dbg_count;
51 #endif 54 #endif
52 55
53 /* static function declarations */ 56 /* static function declarations */
54 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, 57 static void* aac_frame_decode(NeAACDecStruct *hDecoder,
55 uint8_t *buffer, uint32_t buffer_size, 58 NeAACDecFrameInfo *hInfo,
56 void **sample_buffer, uint32_t sample_buffer_size); 59 unsigned char *buffer,
57 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo); 60 unsigned long buffer_size,
58 61 void **sample_buffer2,
59 62 unsigned long sample_buffer_size);
60 char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode) 63 static void create_channel_config(NeAACDecStruct *hDecoder,
64 NeAACDecFrameInfo *hInfo);
65
66
67 char* NEAACDECAPI NeAACDecGetErrorMessage(unsigned char errcode)
61 { 68 {
62 if (errcode >= NUM_ERROR_MESSAGES) 69 if (errcode >= NUM_ERROR_MESSAGES)
63 return NULL; 70 return NULL;
64 return err_msg[errcode]; 71 return err_msg[errcode];
65 } 72 }
66 73
67 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void) 74 unsigned long NEAACDECAPI NeAACDecGetCapabilities(void)
68 { 75 {
69 uint32_t cap = 0; 76 uint32_t cap = 0;
70 77
71 /* can't do without it */ 78 /* can't do without it */
72 cap += LC_DEC_CAP; 79 cap += LC_DEC_CAP;
88 #endif 95 #endif
89 96
90 return cap; 97 return cap;
91 } 98 }
92 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 };
93 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void) 101 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
94 { 102 {
95 uint8_t i; 103 uint8_t i;
96 NeAACDecHandle hDecoder = NULL; 104 NeAACDecStruct *hDecoder = NULL;
97 105
98 if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL) 106 if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
99 return NULL; 107 return NULL;
100 108
101 memset(hDecoder, 0, sizeof(NeAACDecStruct)); 109 memset(hDecoder, 0, sizeof(NeAACDecStruct));
102 110
111 hDecoder->cmes = mes;
103 hDecoder->config.outputFormat = FAAD_FMT_16BIT; 112 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
104 hDecoder->config.defObjectType = MAIN; 113 hDecoder->config.defObjectType = MAIN;
105 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */ 114 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
106 hDecoder->config.downMatrix = 0; 115 hDecoder->config.downMatrix = 0;
107 hDecoder->adts_header_present = 0; 116 hDecoder->adts_header_present = 0;
108 hDecoder->adif_header_present = 0; 117 hDecoder->adif_header_present = 0;
118 hDecoder->latm_header_present = 0;
109 #ifdef ERROR_RESILIENCE 119 #ifdef ERROR_RESILIENCE
110 hDecoder->aacSectionDataResilienceFlag = 0; 120 hDecoder->aacSectionDataResilienceFlag = 0;
111 hDecoder->aacScalefactorDataResilienceFlag = 0; 121 hDecoder->aacScalefactorDataResilienceFlag = 0;
112 hDecoder->aacSpectralDataResilienceFlag = 0; 122 hDecoder->aacSpectralDataResilienceFlag = 0;
113 #endif 123 #endif
114 hDecoder->frameLength = 1024; 124 hDecoder->frameLength = 1024;
115 125
116 hDecoder->frame = 0; 126 hDecoder->frame = 0;
117 hDecoder->sample_buffer = NULL; 127 hDecoder->sample_buffer = NULL;
128
129 hDecoder->__r1 = 1;
130 hDecoder->__r2 = 1;
118 131
119 for (i = 0; i < MAX_CHANNELS; i++) 132 for (i = 0; i < MAX_CHANNELS; i++)
120 { 133 {
121 hDecoder->window_shape_prev[i] = 0; 134 hDecoder->window_shape_prev[i] = 0;
122 hDecoder->time_out[i] = NULL; 135 hDecoder->time_out[i] = NULL;
144 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0)); 157 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
145 158
146 return hDecoder; 159 return hDecoder;
147 } 160 }
148 161
149 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder) 162 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
150 { 163 {
164 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
151 if (hDecoder) 165 if (hDecoder)
152 { 166 {
153 NeAACDecConfigurationPtr config = &(hDecoder->config); 167 NeAACDecConfigurationPtr config = &(hDecoder->config);
154 168
155 return config; 169 return config;
156 } 170 }
157 171
158 return NULL; 172 return NULL;
159 } 173 }
160 174
161 uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder, 175 unsigned char NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
162 NeAACDecConfigurationPtr config) 176 NeAACDecConfigurationPtr config)
163 { 177 {
178 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
164 if (hDecoder && config) 179 if (hDecoder && config)
165 { 180 {
166 /* check if we can decode this object type */ 181 /* check if we can decode this object type */
167 if (can_decode_ot(config->defObjectType) < 0) 182 if (can_decode_ot(config->defObjectType) < 0)
168 return 0; 183 return 0;
192 } 207 }
193 208
194 return 0; 209 return 0;
195 } 210 }
196 211
197 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, 212
198 uint32_t buffer_size, 213 static int latmCheck(latm_header *latm, bitfile *ld)
199 uint32_t *samplerate, uint8_t *channels) 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)
200 { 243 {
201 uint32_t bits = 0; 244 uint32_t bits = 0;
202 bitfile ld; 245 bitfile ld;
203 adif_header adif; 246 adif_header adif;
204 adts_header adts; 247 adts_header adts;
248 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
249
205 250
206 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) 251 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
207 return -1; 252 return -1;
208 253
209 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); 254 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
211 *samplerate = get_sample_rate(hDecoder->sf_index); 256 *samplerate = get_sample_rate(hDecoder->sf_index);
212 *channels = 1; 257 *channels = 1;
213 258
214 if (buffer != NULL) 259 if (buffer != NULL)
215 { 260 {
261 #if 0
262 int is_latm;
263 latm_header *l = &hDecoder->latm_config;
264 #endif
265
216 faad_initbits(&ld, buffer, buffer_size); 266 faad_initbits(&ld, buffer, buffer_size);
217 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
218 /* Check if an ADIF header is present */ 284 /* Check if an ADIF header is present */
219 if ((buffer[0] == 'A') && (buffer[1] == 'D') && 285 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
220 (buffer[2] == 'I') && (buffer[3] == 'F')) 286 (buffer[2] == 'I') && (buffer[3] == 'F'))
221 { 287 {
222 hDecoder->adif_header_present = 1; 288 hDecoder->adif_header_present = 1;
223 289
224 get_adif_header(&adif, &ld); 290 get_adif_header(&adif, &ld);
225 faad_byte_align(&ld); 291 faad_byte_align(&ld);
226 292
255 faad_endbits(&ld); 321 faad_endbits(&ld);
256 return -1; 322 return -1;
257 } 323 }
258 faad_endbits(&ld); 324 faad_endbits(&ld);
259 } 325 }
260 hDecoder->channelConfiguration = *channels;
261 326
262 #if (defined(PS_DEC) || defined(DRM_PS)) 327 #if (defined(PS_DEC) || defined(DRM_PS))
263 /* check if we have a mono file */ 328 /* check if we have a mono file */
264 if (*channels == 1) 329 if (*channels == 1)
265 { 330 {
266 /* upMatrix to 2 channels for implicit signalling of PS */ 331 /* upMatrix to 2 channels for implicit signalling of PS */
267 *channels = 2; 332 *channels = 2;
268 } 333 }
269 #endif 334 #endif
270 335
336 hDecoder->channelConfiguration = *channels;
337
271 #ifdef SBR_DEC 338 #ifdef SBR_DEC
272 /* implicit signalling */ 339 /* implicit signalling */
273 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) 340 if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
274 { 341 {
275 *samplerate *= 2; 342 *samplerate *= 2;
276 hDecoder->forceUpSampling = 1; 343 hDecoder->forceUpSampling = 1;
277 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) { 344 } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
278 hDecoder->downSampledSBR = 1; 345 hDecoder->downSampledSBR = 1;
279 } 346 }
280 #endif 347 #endif
281 348
282 /* must be done before frameLength is divided by 2 for LD */ 349 /* must be done before frameLength is divided by 2 for LD */
297 364
298 return bits; 365 return bits;
299 } 366 }
300 367
301 /* Init the library using a DecoderSpecificInfo */ 368 /* Init the library using a DecoderSpecificInfo */
302 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer, 369 char NEAACDECAPI NeAACDecInit2(NeAACDecHandle hpDecoder,
303 uint32_t SizeOfDecoderSpecificInfo, 370 unsigned char *pBuffer,
304 uint32_t *samplerate, uint8_t *channels) 371 unsigned long SizeOfDecoderSpecificInfo,
305 { 372 unsigned long *samplerate,
373 unsigned char *channels)
374 {
375 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
306 int8_t rc; 376 int8_t rc;
307 mp4AudioSpecificConfig mp4ASC; 377 mp4AudioSpecificConfig mp4ASC;
308 378
309 if((hDecoder == NULL) 379 if((hDecoder == NULL)
310 || (pBuffer == NULL) 380 || (pBuffer == NULL)
318 hDecoder->adif_header_present = 0; 388 hDecoder->adif_header_present = 0;
319 hDecoder->adts_header_present = 0; 389 hDecoder->adts_header_present = 0;
320 390
321 /* decode the audio specific config */ 391 /* decode the audio specific config */
322 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC, 392 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
323 &(hDecoder->pce)); 393 &(hDecoder->pce), hDecoder->latm_header_present);
324 394
325 /* copy the relevant info to the decoder handle */ 395 /* copy the relevant info to the decoder handle */
326 *samplerate = mp4ASC.samplingFrequency; 396 *samplerate = mp4ASC.samplingFrequency;
327 if (mp4ASC.channelsConfiguration) 397 if (mp4ASC.channelsConfiguration)
328 { 398 {
388 458
389 return 0; 459 return 0;
390 } 460 }
391 461
392 #ifdef DRM 462 #ifdef DRM
393 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate, 463 char NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
394 uint8_t channels) 464 unsigned long samplerate,
395 { 465 unsigned char channels)
466 {
467 NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
396 if (hDecoder == NULL) 468 if (hDecoder == NULL)
397 return 1; /* error */ 469 return 1; /* error */
398 470
399 NeAACDecClose(*hDecoder); 471 NeAACDecClose(*hDecoder);
400 472
420 492
421 #ifdef SBR_DEC 493 #ifdef SBR_DEC
422 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) 494 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
423 (*hDecoder)->sbr_present_flag = 0; 495 (*hDecoder)->sbr_present_flag = 0;
424 else 496 else
425 (*hDecoder)->sbr_present_flag = 1; 497 (*hDecoder)->sbr_present_flag = 1;
426 #endif 498 #endif
427 499
428 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); 500 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
429 501
430 return 0; 502 return 0;
431 } 503 }
432 #endif 504 #endif
433 505
434 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder) 506 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hpDecoder)
435 { 507 {
436 uint8_t i; 508 uint8_t i;
509 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
437 510
438 if (hDecoder == NULL) 511 if (hDecoder == NULL)
439 return; 512 return;
440 513
441 #ifdef PROFILE 514 #ifdef PROFILE
482 #endif 555 #endif
483 556
484 if (hDecoder) faad_free(hDecoder); 557 if (hDecoder) faad_free(hDecoder);
485 } 558 }
486 559
487 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame) 560 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
488 { 561 {
562 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
489 if (hDecoder) 563 if (hDecoder)
490 { 564 {
491 hDecoder->postSeekResetFlag = 1; 565 hDecoder->postSeekResetFlag = 1;
492 566
493 if (frame != -1) 567 if (frame != -1)
494 hDecoder->frame = frame; 568 hDecoder->frame = frame;
495 } 569 }
496 } 570 }
497 571
498 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo) 572 static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
499 { 573 {
500 hInfo->num_front_channels = 0; 574 hInfo->num_front_channels = 0;
501 hInfo->num_side_channels = 0; 575 hInfo->num_side_channels = 0;
502 hInfo->num_back_channels = 0; 576 hInfo->num_back_channels = 0;
503 hInfo->num_lfe_channels = 0; 577 hInfo->num_lfe_channels = 0;
523 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; 597 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
524 598
525 chdir = hInfo->num_front_channels; 599 chdir = hInfo->num_front_channels;
526 if (chdir & 1) 600 if (chdir & 1)
527 { 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
528 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; 607 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
529 chdir--; 608 chdir--;
609 #endif
530 } 610 }
531 for (i = 0; i < chdir; i += 2) 611 for (i = 0; i < chdir; i += 2)
532 { 612 {
533 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; 613 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
534 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; 614 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
563 643
564 } else { 644 } else {
565 switch (hDecoder->channelConfiguration) 645 switch (hDecoder->channelConfiguration)
566 { 646 {
567 case 1: 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
568 hInfo->num_front_channels = 1; 654 hInfo->num_front_channels = 1;
569 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; 655 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
656 #endif
570 break; 657 break;
571 case 2: 658 case 2:
572 hInfo->num_front_channels = 2; 659 hInfo->num_front_channels = 2;
573 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; 660 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
574 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; 661 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
698 break; 785 break;
699 } 786 }
700 } 787 }
701 } 788 }
702 789
703 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder, 790 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hpDecoder,
704 NeAACDecFrameInfo *hInfo, 791 NeAACDecFrameInfo *hInfo,
705 uint8_t *buffer, uint32_t buffer_size) 792 unsigned char *buffer,
706 { 793 unsigned long buffer_size)
794 {
795 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
707 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0); 796 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
708 } 797 }
709 798
710 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder, 799 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hpDecoder,
711 NeAACDecFrameInfo *hInfo, 800 NeAACDecFrameInfo *hInfo,
712 uint8_t *buffer, uint32_t buffer_size, 801 unsigned char *buffer,
713 void **sample_buffer, uint32_t sample_buffer_size) 802 unsigned long buffer_size,
714 { 803 void **sample_buffer,
804 unsigned long sample_buffer_size)
805 {
806 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
715 if ((sample_buffer == NULL) || (sample_buffer_size == 0)) 807 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
716 { 808 {
717 hInfo->error = 27; 809 hInfo->error = 27;
718 return NULL; 810 return NULL;
719 } 811 }
720 812
721 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, 813 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
722 sample_buffer, sample_buffer_size); 814 sample_buffer, sample_buffer_size);
723 } 815 }
724 816
725 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, 817 #ifdef DRM
726 uint8_t *buffer, uint32_t buffer_size, 818
727 void **sample_buffer2, uint32_t sample_buffer_size) 819 #define ERROR_STATE_INIT 6
728 { 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;
729 uint8_t channels = 0; 836 uint8_t channels = 0;
730 uint8_t output_channels = 0; 837 uint8_t output_channels = 0;
731 bitfile ld; 838 bitfile ld = {0};
732 uint32_t bitsconsumed; 839 uint32_t bitsconsumed;
733 uint16_t frame_len; 840 uint16_t frame_len;
734 void *sample_buffer; 841 void *sample_buffer;
842 uint32_t startbit=0, endbit=0, payload_bits=0;
735 843
736 #ifdef PROFILE 844 #ifdef PROFILE
737 int64_t count = faad_get_ts(); 845 int64_t count = faad_get_ts();
738 #endif 846 #endif
739 847
750 frame_len = hDecoder->frameLength; 858 frame_len = hDecoder->frameLength;
751 859
752 860
753 memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); 861 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
754 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); 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
755 890
756 /* initialize the bitstream */ 891 /* initialize the bitstream */
757 faad_initbits(&ld, buffer, buffer_size); 892 faad_initbits(&ld, buffer, buffer_size);
758 893
759 #if 0 894 #if 0
772 faad_endbits(&ld); 907 faad_endbits(&ld);
773 faad_initbits(&ld, buffer, buffer_size); 908 faad_initbits(&ld, buffer, buffer_size);
774 } 909 }
775 #endif 910 #endif
776 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
777 #ifdef DRM 925 #ifdef DRM
778 if (hDecoder->object_type == DRM_ER_LC) 926 if (hDecoder->object_type == DRM_ER_LC)
779 { 927 {
780 /* We do not support stereo right now */ 928 /* We do not support stereo right now */
781 if (0) //(hDecoder->channelConfiguration == 2) 929 if (0) //(hDecoder->channelConfiguration == 2)
782 { 930 {
783 hInfo->error = 8; // Throw CRC error 931 hInfo->error = 28; // Throw CRC error
784 goto error; 932 goto error;
785 } 933 }
786 934
787 faad_getbits(&ld, 8 935 faad_getbits(&ld, 8
788 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); 936 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
806 #ifdef ANALYSIS 954 #ifdef ANALYSIS
807 dbg_count = 0; 955 dbg_count = 0;
808 #endif 956 #endif
809 957
810 /* decode the complete bitstream */ 958 /* decode the complete bitstream */
811 #ifdef SCALABLE_DEC 959 #ifdef DRM
812 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC)) 960 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC))
813 { 961 {
814 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); 962 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
815 } else { 963 } else {
816 #endif 964 #endif
817 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); 965 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
818 #ifdef SCALABLE_DEC 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);
819 } 980 }
820 #endif 981 #endif
821 982
822 channels = hDecoder->fr_channels; 983 channels = hDecoder->fr_channels;
823 984
841 goto error; 1002 goto error;
842 } 1003 }
843 faad_endbits(&ld); 1004 faad_endbits(&ld);
844 1005
845 1006
846 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present) 1007 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1008 #if 0
1009 && !hDecoder->latm_header_present
1010 #endif
1011 )
847 { 1012 {
848 if (hDecoder->channelConfiguration == 0) 1013 if (hDecoder->channelConfiguration == 0)
849 hDecoder->channelConfiguration = channels; 1014 hDecoder->channelConfiguration = channels;
850 1015
851 if (channels == 8) /* 7.1 */ 1016 if (channels == 8) /* 7.1 */
890 hInfo->header_type = RAW; 1055 hInfo->header_type = RAW;
891 if (hDecoder->adif_header_present) 1056 if (hDecoder->adif_header_present)
892 hInfo->header_type = ADIF; 1057 hInfo->header_type = ADIF;
893 if (hDecoder->adts_header_present) 1058 if (hDecoder->adts_header_present)
894 hInfo->header_type = ADTS; 1059 hInfo->header_type = ADTS;
1060 #if 0
1061 if (hDecoder->latm_header_present)
1062 hInfo->header_type = LATM;
1063 #endif
895 #if (defined(PS_DEC) || defined(DRM_PS)) 1064 #if (defined(PS_DEC) || defined(DRM_PS))
896 hInfo->ps = hDecoder->ps_used_global; 1065 hInfo->ps = hDecoder->ps_used_global;
897 #endif 1066 #endif
898 1067
899 /* check if frame has channel elements */ 1068 /* check if frame has channel elements */
976 hInfo->sbr = SBR_DOWNSAMPLED; 1145 hInfo->sbr = SBR_DOWNSAMPLED;
977 } 1146 }
978 } 1147 }
979 #endif 1148 #endif
980 1149
1150
981 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, 1151 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
982 output_channels, frame_len, hDecoder->config.outputFormat); 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
983 1158
984 1159
985 hDecoder->postSeekResetFlag = 0; 1160 hDecoder->postSeekResetFlag = 0;
986 1161
987 hDecoder->frame++; 1162 hDecoder->frame++;
1011 1186
1012 return sample_buffer; 1187 return sample_buffer;
1013 1188
1014 error: 1189 error:
1015 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
1016 faad_endbits(&ld); 1215 faad_endbits(&ld);
1017 1216
1018 /* cleanup */ 1217 /* cleanup */
1019 #ifdef ANALYSIS 1218 #ifdef ANALYSIS
1020 fflush(stdout); 1219 fflush(stdout);