Imported Debian version 0.1.3.1
[deb_fdk-aac.git] / libAACdec / src / aacdecoder_lib.cpp
1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder **************************
85
86 Author(s): Manuel Jander
87 Description:
88
89 ******************************************************************************/
90
91 #include "aacdecoder_lib.h"
92
93 #include "aac_ram.h"
94 #include "aacdecoder.h"
95 #include "tpdec_lib.h"
96 #include "FDK_core.h" /* FDK_tools version info */
97
98
99 #include "sbrdecoder.h"
100
101
102
103
104 #include "conceal.h"
105
106 #include "aacdec_drc.h"
107
108
109
110 /* Decoder library info */
111 #define AACDECODER_LIB_VL0 2
112 #define AACDECODER_LIB_VL1 5
113 #define AACDECODER_LIB_VL2 5
114 #define AACDECODER_LIB_TITLE "AAC Decoder Lib"
115 #define AACDECODER_LIB_BUILD_DATE __DATE__
116 #define AACDECODER_LIB_BUILD_TIME __TIME__
117
118 static AAC_DECODER_ERROR
119 setConcealMethod ( const HANDLE_AACDECODER self,
120 const INT method );
121
122
123 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_GetFreeBytes ( const HANDLE_AACDECODER self, UINT *pFreeBytes){
124
125 /* reset free bytes */
126 *pFreeBytes = 0;
127
128 /* check handle */
129 if(!self)
130 return AAC_DEC_INVALID_HANDLE;
131
132 /* return nr of free bytes */
133 HANDLE_FDK_BITSTREAM hBs = transportDec_GetBitstream(self->hInput, 0);
134 *pFreeBytes = FDKgetFreeBits(hBs) >> 3;
135
136 /* success */
137 return AAC_DEC_OK;
138 }
139
140 /**
141 * Config Decoder using a CSAudioSpecificConfig struct.
142 */
143 static
144 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Config(HANDLE_AACDECODER self, const CSAudioSpecificConfig *pAscStruct)
145 {
146 AAC_DECODER_ERROR err;
147
148 /* Initialize AAC core decoder, and update self->streaminfo */
149 err = CAacDecoder_Init(self, pAscStruct);
150
151 return err;
152 }
153
154 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_ConfigRaw (
155 HANDLE_AACDECODER self,
156 UCHAR *conf[],
157 const UINT length[] )
158 {
159 AAC_DECODER_ERROR err = AAC_DEC_OK;
160 TRANSPORTDEC_ERROR errTp;
161 UINT layer, nrOfLayers = self->nrOfLayers;
162
163 for(layer = 0; layer < nrOfLayers; layer++){
164 if(length[layer] > 0){
165 errTp = transportDec_OutOfBandConfig(self->hInput, conf[layer], length[layer], layer);
166 if (errTp != TRANSPORTDEC_OK) {
167 switch (errTp) {
168 case TRANSPORTDEC_NEED_TO_RESTART:
169 err = AAC_DEC_NEED_TO_RESTART;
170 break;
171 case TRANSPORTDEC_UNSUPPORTED_FORMAT:
172 err = AAC_DEC_UNSUPPORTED_FORMAT;
173 break;
174 default:
175 err = AAC_DEC_UNKNOWN;
176 break;
177 }
178 /* if baselayer is OK we continue decoding */
179 if(layer >= 1){
180 self->nrOfLayers = layer;
181 }
182 break;
183 }
184 }
185 }
186
187 return err;
188 }
189
190
191
192 static INT aacDecoder_ConfigCallback(void *handle, const CSAudioSpecificConfig *pAscStruct)
193 {
194 HANDLE_AACDECODER self = (HANDLE_AACDECODER)handle;
195 AAC_DECODER_ERROR err = AAC_DEC_OK;
196 TRANSPORTDEC_ERROR errTp;
197
198 {
199 {
200 err = aacDecoder_Config(self, pAscStruct);
201 }
202 }
203 if (err == AAC_DEC_OK) {
204 if ( self->flags & (AC_USAC|AC_RSVD50|AC_LD|AC_ELD)
205 && CConcealment_GetDelay(&self->concealCommonData) > 0 )
206 {
207 /* Revert to error concealment method Noise Substitution.
208 Because interpolation is not implemented for USAC/RSVD50 or
209 the additional delay is unwanted for low delay codecs. */
210 setConcealMethod(self, 1);
211 #ifdef DEBUG
212 FDKprintf(" Concealment method was reverted to 1 !\n");
213 #endif
214 }
215 errTp = TRANSPORTDEC_OK;
216 } else {
217 if (IS_INIT_ERROR(err)) {
218 errTp = TRANSPORTDEC_UNSUPPORTED_FORMAT;
219 } /* Fatal errors */
220 else if (err == AAC_DEC_NEED_TO_RESTART) {
221 errTp = TRANSPORTDEC_NEED_TO_RESTART;
222 } else {
223 errTp = TRANSPORTDEC_UNKOWN_ERROR;
224 }
225 }
226
227 return errTp;
228 }
229
230
231
232 LINKSPEC_CPP AAC_DECODER_ERROR
233 aacDecoder_AncDataInit ( HANDLE_AACDECODER self,
234 UCHAR *buffer,
235 int size )
236 {
237 CAncData *ancData = &self->ancData;
238
239 return CAacDecoder_AncDataInit(ancData, buffer, size);
240 }
241
242
243 LINKSPEC_CPP AAC_DECODER_ERROR
244 aacDecoder_AncDataGet ( HANDLE_AACDECODER self,
245 int index,
246 UCHAR **ptr,
247 int *size )
248 {
249 CAncData *ancData = &self->ancData;
250
251 return CAacDecoder_AncDataGet(ancData, index, ptr, size);
252 }
253
254
255 static AAC_DECODER_ERROR
256 setConcealMethod ( const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */
257 const INT method )
258 {
259 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
260 CConcealParams *pConcealData = NULL;
261 HANDLE_SBRDECODER hSbrDec = NULL;
262 HANDLE_AAC_DRC hDrcInfo = NULL;
263 HANDLE_PCM_DOWNMIX hPcmDmx = NULL;
264 CConcealmentMethod backupMethod = ConcealMethodNone;
265 int backupDelay = 0;
266 int bsDelay = 0;
267
268 /* check decoder handle */
269 if (self != NULL) {
270 pConcealData = &self->concealCommonData;
271 hSbrDec = self->hSbrDecoder;
272 hDrcInfo = self->hDrcInfo;
273 hPcmDmx = self->hPcmUtils;
274 }
275
276
277 /* Get current method/delay */
278 backupMethod = CConcealment_GetMethod(pConcealData);
279 backupDelay = CConcealment_GetDelay(pConcealData);
280
281 /* Be sure to set AAC and SBR concealment method simultaneously! */
282 errorStatus =
283 CConcealment_SetParams(
284 pConcealData,
285 (int)method, // concealMethod
286 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeOutSlope
287 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealFadeInSlope
288 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED, // concealMuteRelease
289 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED // concealComfNoiseLevel
290 );
291 if ( (errorStatus != AAC_DEC_OK)
292 && (errorStatus != AAC_DEC_INVALID_HANDLE) ) {
293 goto bail;
294 }
295
296 /* Get new delay */
297 bsDelay = CConcealment_GetDelay(pConcealData);
298
299 {
300 SBR_ERROR sbrErr = SBRDEC_OK;
301
302 /* set SBR bitstream delay */
303 sbrErr = sbrDecoder_SetParam (
304 hSbrDec,
305 SBR_SYSTEM_BITSTREAM_DELAY,
306 bsDelay
307 );
308
309 switch (sbrErr) {
310 case SBRDEC_OK:
311 case SBRDEC_NOT_INITIALIZED:
312 if (self != NULL) {
313 /* save the param value and set later
314 (when SBR has been initialized) */
315 self->sbrParams.bsDelay = bsDelay;
316 }
317 break;
318 default:
319 errorStatus = AAC_DEC_SET_PARAM_FAIL;
320 goto bail;
321 }
322 }
323
324 errorStatus =
325 aacDecoder_drcSetParam (
326 hDrcInfo,
327 DRC_BS_DELAY,
328 bsDelay
329 );
330 if ( (errorStatus != AAC_DEC_OK)
331 && (errorStatus != AAC_DEC_INVALID_HANDLE) ) {
332 goto bail;
333 }
334
335 if (errorStatus == AAC_DEC_OK) {
336 PCMDMX_ERROR err =
337 pcmDmx_SetParam (
338 hPcmDmx,
339 DMX_BS_DATA_DELAY,
340 bsDelay
341 );
342 switch (err) {
343 case PCMDMX_INVALID_HANDLE:
344 errorStatus = AAC_DEC_INVALID_HANDLE;
345 case PCMDMX_OK:
346 break;
347 default:
348 errorStatus = AAC_DEC_SET_PARAM_FAIL;
349 goto bail;
350 }
351 }
352
353
354 bail:
355 if ( (errorStatus != AAC_DEC_OK)
356 && (errorStatus != AAC_DEC_INVALID_HANDLE) )
357 {
358 /* Revert to the initial state */
359 CConcealment_SetParams (
360 pConcealData,
361 (int)backupMethod,
362 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
363 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
364 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED,
365 AACDEC_CONCEAL_PARAM_NOT_SPECIFIED
366 );
367 /* Revert SBR bitstream delay */
368 sbrDecoder_SetParam (
369 hSbrDec,
370 SBR_SYSTEM_BITSTREAM_DELAY,
371 backupDelay
372 );
373 /* Revert DRC bitstream delay */
374 aacDecoder_drcSetParam (
375 hDrcInfo,
376 DRC_BS_DELAY,
377 backupDelay
378 );
379 /* Revert PCM mixdown bitstream delay */
380 pcmDmx_SetParam (
381 hPcmDmx,
382 DMX_BS_DATA_DELAY,
383 backupDelay
384 );
385 }
386
387 return errorStatus;
388 }
389
390
391 LINKSPEC_CPP AAC_DECODER_ERROR
392 aacDecoder_SetParam ( const HANDLE_AACDECODER self, /*!< Handle of the decoder instance */
393 const AACDEC_PARAM param, /*!< Parameter to set */
394 const INT value) /*!< Parameter valued */
395 {
396 AAC_DECODER_ERROR errorStatus = AAC_DEC_OK;
397 CConcealParams *pConcealData = NULL;
398 HANDLE_AAC_DRC hDrcInfo = NULL;
399 HANDLE_PCM_DOWNMIX hPcmDmx = NULL;
400
401 /* check decoder handle */
402 if (self != NULL) {
403 pConcealData = &self->concealCommonData;
404 hDrcInfo = self->hDrcInfo;
405 hPcmDmx = self->hPcmUtils;
406 } else {
407 errorStatus = AAC_DEC_INVALID_HANDLE;
408 }
409
410 /* configure the subsystems */
411 switch (param)
412 {
413 case AAC_PCM_OUTPUT_INTERLEAVED:
414 if (value < 0 || value > 1) {
415 return AAC_DEC_SET_PARAM_FAIL;
416 }
417 if (self == NULL) {
418 return AAC_DEC_INVALID_HANDLE;
419 }
420 self->outputInterleaved = value;
421 break;
422
423 case AAC_PCM_OUTPUT_CHANNELS:
424 if (value < -1 || value > (6)) {
425 return AAC_DEC_SET_PARAM_FAIL;
426 }
427 {
428 PCMDMX_ERROR err;
429
430 err = pcmDmx_SetParam (
431 hPcmDmx,
432 NUMBER_OF_OUTPUT_CHANNELS,
433 value );
434
435 switch (err) {
436 case PCMDMX_OK:
437 break;
438 case PCMDMX_INVALID_HANDLE:
439 return AAC_DEC_INVALID_HANDLE;
440 default:
441 return AAC_DEC_SET_PARAM_FAIL;
442 }
443 }
444 break;
445
446 case AAC_PCM_DUAL_CHANNEL_OUTPUT_MODE:
447 {
448 PCMDMX_ERROR err;
449
450 err = pcmDmx_SetParam (
451 hPcmDmx,
452 DUAL_CHANNEL_DOWNMIX_MODE,
453 value );
454
455 switch (err) {
456 case PCMDMX_OK:
457 break;
458 case PCMDMX_INVALID_HANDLE:
459 return AAC_DEC_INVALID_HANDLE;
460 default:
461 return AAC_DEC_SET_PARAM_FAIL;
462 }
463 }
464 break;
465
466 case AAC_PCM_OUTPUT_CHANNEL_MAPPING:
467 switch (value) {
468 case 0:
469 if (self != NULL) {
470 self->channelOutputMapping = channelMappingTablePassthrough;
471 }
472 break;
473 case 1:
474 if (self != NULL) {
475 self->channelOutputMapping = channelMappingTableWAV;
476 }
477 break;
478 default:
479 errorStatus = AAC_DEC_SET_PARAM_FAIL;
480 break;
481 }
482 break;
483
484
485 case AAC_QMF_LOWPOWER:
486 if (value < -1 || value > 1) {
487 return AAC_DEC_SET_PARAM_FAIL;
488 }
489 if (self == NULL) {
490 return AAC_DEC_INVALID_HANDLE;
491 }
492
493 /**
494 * Set QMF mode (might be overriden)
495 * 0:HQ (complex)
496 * 1:LP (partially complex)
497 */
498 self->qmfModeUser = (QMF_MODE)value;
499 break;
500
501
502 case AAC_DRC_ATTENUATION_FACTOR:
503 /* DRC compression factor (where 0 is no and 127 is max compression) */
504 errorStatus =
505 aacDecoder_drcSetParam (
506 hDrcInfo,
507 DRC_CUT_SCALE,
508 value
509 );
510 break;
511
512 case AAC_DRC_BOOST_FACTOR:
513 /* DRC boost factor (where 0 is no and 127 is max boost) */
514 errorStatus =
515 aacDecoder_drcSetParam (
516 hDrcInfo,
517 DRC_BOOST_SCALE,
518 value
519 );
520 break;
521
522 case AAC_DRC_REFERENCE_LEVEL:
523 /* DRC reference level quantized in 0.25dB steps using values [0..127] it is '-' for analog scaling */
524 errorStatus =
525 aacDecoder_drcSetParam (
526 hDrcInfo,
527 TARGET_REF_LEVEL,
528 value
529 );
530 break;
531
532 case AAC_DRC_HEAVY_COMPRESSION:
533 /* Don't need to overwrite cut/boost values */
534 errorStatus =
535 aacDecoder_drcSetParam (
536 hDrcInfo,
537 APPLY_HEAVY_COMPRESSION,
538 value
539 );
540 break;
541
542
543 case AAC_TPDEC_CLEAR_BUFFER:
544 transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1);
545 self->streamInfo.numLostAccessUnits = 0;
546 self->streamInfo.numBadBytes = 0;
547 self->streamInfo.numTotalBytes = 0;
548 /* aacDecoder_SignalInterruption(self); */
549 break;
550
551 case AAC_CONCEAL_METHOD:
552 /* Changing the concealment method can introduce additional bitstream delay. And
553 that in turn affects sub libraries and modules which makes the whole thing quite
554 complex. So the complete changing routine is packed into a helper function which
555 keeps all modules and libs in a consistent state even in the case an error occures. */
556 errorStatus = setConcealMethod ( self, value );
557 break;
558
559 default:
560 return AAC_DEC_SET_PARAM_FAIL;
561 } /* switch(param) */
562
563 return (errorStatus);
564 }
565
566
567 LINKSPEC_CPP HANDLE_AACDECODER aacDecoder_Open(TRANSPORT_TYPE transportFmt, UINT nrOfLayers)
568 {
569 AAC_DECODER_INSTANCE *aacDec = NULL;
570 HANDLE_TRANSPORTDEC pIn;
571 int err = 0;
572
573 /* Allocate transport layer struct. */
574 pIn = transportDec_Open(transportFmt, TP_FLAG_MPEG4);
575 if (pIn == NULL) {
576 return NULL;
577 }
578
579 transportDec_SetParam(pIn, TPDEC_PARAM_IGNORE_BUFFERFULLNESS, 1);
580
581 /* Allocate AAC decoder core struct. */
582 aacDec = CAacDecoder_Open(transportFmt);
583
584 if (aacDec == NULL) {
585 transportDec_Close(&pIn);
586 goto bail;
587 }
588 aacDec->hInput = pIn;
589
590 aacDec->nrOfLayers = nrOfLayers;
591
592 aacDec->channelOutputMapping = channelMappingTableWAV;
593
594 /* Register Config Update callback. */
595 transportDec_RegisterAscCallback(pIn, aacDecoder_ConfigCallback, (void*)aacDec);
596
597 /* open SBR decoder */
598 if ( SBRDEC_OK != sbrDecoder_Open ( &aacDec->hSbrDecoder )) {
599 err = -1;
600 goto bail;
601 }
602 aacDec->qmfModeUser = NOT_DEFINED;
603 transportDec_RegisterSbrCallback(aacDec->hInput, (cbSbr_t)sbrDecoder_Header, (void*)aacDec->hSbrDecoder);
604
605
606 pcmDmx_Open( &aacDec->hPcmUtils );
607 if (aacDec->hPcmUtils == NULL) {
608 err = -1;
609 goto bail;
610 }
611
612
613
614 /* Assure that all modules have same delay */
615 if ( setConcealMethod(aacDec, CConcealment_GetMethod(&aacDec->concealCommonData)) ) {
616 err = -1;
617 goto bail;
618 }
619
620 bail:
621 if (err == -1) {
622 aacDecoder_Close(aacDec);
623 aacDec = NULL;
624 }
625 return aacDec;
626 }
627
628 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_Fill(
629 HANDLE_AACDECODER self,
630 UCHAR *pBuffer[],
631 const UINT bufferSize[],
632 UINT *pBytesValid
633 )
634 {
635 TRANSPORTDEC_ERROR tpErr;
636 /* loop counter for layers; if not TT_MP4_RAWPACKETS used as index for only
637 available layer */
638 INT layer = 0;
639 INT nrOfLayers = self->nrOfLayers;
640
641 {
642 for (layer = 0; layer < nrOfLayers; layer++){
643 {
644 tpErr = transportDec_FillData( self->hInput, pBuffer[layer], bufferSize[layer], &pBytesValid[layer], layer );
645 if (tpErr != TRANSPORTDEC_OK) {
646 return AAC_DEC_UNKNOWN; /* Must be an internal error */
647 }
648 }
649 }
650 }
651
652 return AAC_DEC_OK;
653 }
654
655
656 static void aacDecoder_SignalInterruption(HANDLE_AACDECODER self)
657 {
658 CAacDecoder_SignalInterruption(self);
659
660 if ( self->hSbrDecoder != NULL ) {
661 sbrDecoder_SetParam(self->hSbrDecoder, SBR_BS_INTERRUPTION, 0);
662 }
663 }
664
665 static void aacDecoder_UpdateBitStreamCounters(CStreamInfo *pSi, HANDLE_FDK_BITSTREAM hBs, int nBits, AAC_DECODER_ERROR ErrorStatus)
666 {
667 /* calculate bit difference (amount of bits moved forward) */
668 nBits = nBits - FDKgetValidBits(hBs);
669
670 /* Note: The amount of bits consumed might become negative when parsing a
671 bit stream with several sub frames, and we find out at the last sub frame
672 that the total frame length does not match the sum of sub frame length.
673 If this happens, the transport decoder might want to rewind to the supposed
674 ending of the transport frame, and this position might be before the last
675 access unit beginning. */
676
677 /* Calc bitrate. */
678 if (pSi->frameSize > 0) {
679 pSi->bitRate = (nBits * pSi->sampleRate)/pSi->frameSize;
680 }
681
682 /* bit/byte counters */
683 {
684 int nBytes;
685
686 nBytes = nBits>>3;
687 pSi->numTotalBytes += nBytes;
688 if (IS_OUTPUT_VALID(ErrorStatus)) {
689 pSi->numTotalAccessUnits++;
690 }
691 if (IS_DECODE_ERROR(ErrorStatus)) {
692 pSi->numBadBytes += nBytes;
693 pSi->numBadAccessUnits++;
694 }
695 }
696 }
697
698 static INT aacDecoder_EstimateNumberOfLostFrames(HANDLE_AACDECODER self)
699 {
700 INT n;
701
702 transportDec_GetMissingAccessUnitCount( &n, self->hInput);
703
704 return n;
705 }
706
707 LINKSPEC_CPP AAC_DECODER_ERROR aacDecoder_DecodeFrame(
708 HANDLE_AACDECODER self,
709 INT_PCM *pTimeData,
710 const INT timeDataSize,
711 const UINT flags)
712 {
713 AAC_DECODER_ERROR ErrorStatus;
714 INT layer;
715 INT nBits;
716 INT interleaved = self->outputInterleaved;
717 HANDLE_FDK_BITSTREAM hBs;
718 int fTpInterruption = 0; /* Transport originated interruption detection. */
719 int fTpConceal = 0; /* Transport originated concealment. */
720
721
722 if (self == NULL) {
723 return AAC_DEC_INVALID_HANDLE;
724 }
725
726 if (flags & AACDEC_INTR) {
727 self->streamInfo.numLostAccessUnits = 0;
728 }
729
730 hBs = transportDec_GetBitstream(self->hInput, 0);
731
732 /* Get current bits position for bitrate calculation. */
733 nBits = FDKgetValidBits(hBs);
734 if (! (flags & (AACDEC_CONCEAL | AACDEC_FLUSH) ) )
735 {
736 TRANSPORTDEC_ERROR err;
737
738 for(layer = 0; layer < self->nrOfLayers; layer++)
739 {
740 err = transportDec_ReadAccessUnit(self->hInput, layer);
741 if (err != TRANSPORTDEC_OK) {
742 switch (err) {
743 case TRANSPORTDEC_NOT_ENOUGH_BITS:
744 ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS;
745 goto bail;
746 case TRANSPORTDEC_SYNC_ERROR:
747 self->streamInfo.numLostAccessUnits = aacDecoder_EstimateNumberOfLostFrames(self);
748 fTpInterruption = 1;
749 break;
750 case TRANSPORTDEC_NEED_TO_RESTART:
751 ErrorStatus = AAC_DEC_NEED_TO_RESTART;
752 goto bail;
753 case TRANSPORTDEC_CRC_ERROR:
754 fTpConceal = 1;
755 break;
756 default:
757 ErrorStatus = AAC_DEC_UNKNOWN;
758 goto bail;
759 }
760 }
761 }
762 } else {
763 if (self->streamInfo.numLostAccessUnits > 0) {
764 self->streamInfo.numLostAccessUnits--;
765 }
766 }
767
768 /* Signal bit stream interruption to other modules if required. */
769 if ( fTpInterruption || (flags & (AACDEC_INTR|AACDEC_CLRHIST)) )
770 {
771 aacDecoder_SignalInterruption(self);
772 if ( ! (flags & AACDEC_INTR) ) {
773 ErrorStatus = AAC_DEC_TRANSPORT_SYNC_ERROR;
774 goto bail;
775 }
776 }
777
778 /* Empty bit buffer in case of flush request. */
779 if (flags & AACDEC_FLUSH)
780 {
781 transportDec_SetParam(self->hInput, TPDEC_PARAM_RESET, 1);
782 self->streamInfo.numLostAccessUnits = 0;
783 self->streamInfo.numBadBytes = 0;
784 self->streamInfo.numTotalBytes = 0;
785 }
786
787
788 ErrorStatus = CAacDecoder_DecodeFrame(self,
789 flags | (fTpConceal ? AACDEC_CONCEAL : 0),
790 pTimeData,
791 timeDataSize,
792 interleaved);
793
794 if (!(flags & (AACDEC_CONCEAL|AACDEC_FLUSH))) {
795 TRANSPORTDEC_ERROR tpErr;
796 tpErr = transportDec_EndAccessUnit(self->hInput);
797 if (tpErr != TRANSPORTDEC_OK) {
798 self->frameOK = 0;
799 }
800 }
801
802 /* If the current pTimeData does not contain a valid signal, there nothing else we can do, so bail. */
803 if ( ! IS_OUTPUT_VALID(ErrorStatus) ) {
804 goto bail;
805 }
806
807 {
808 /* Export data into streaminfo structure */
809 self->streamInfo.sampleRate = self->streamInfo.aacSampleRate;
810 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame;
811 }
812 self->streamInfo.numChannels = self->streamInfo.aacNumChannels;
813
814
815
816 CAacDecoder_SyncQmfMode(self);
817
818 /* sbr decoder */
819
820 if (ErrorStatus || (flags & AACDEC_CONCEAL) || self->pAacDecoderStaticChannelInfo[0]->concealmentInfo.concealState > ConcealState_FadeIn)
821 {
822 self->frameOK = 0; /* if an error has occured do concealment in the SBR decoder too */
823 }
824
825 if (self->sbrEnabled)
826 {
827 SBR_ERROR sbrError = SBRDEC_OK;
828
829 /* set params */
830 sbrDecoder_SetParam ( self->hSbrDecoder,
831 SBR_SYSTEM_BITSTREAM_DELAY,
832 self->sbrParams.bsDelay);
833
834 if ( self->streamInfo.aot == AOT_ER_AAC_ELD ) {
835 /* Configure QMF */
836 sbrDecoder_SetParam ( self->hSbrDecoder,
837 SBR_LD_QMF_TIME_ALIGN,
838 (self->flags & AC_LD_MPS) ? 1 : 0 );
839 }
840
841
842
843
844 /* apply SBR processing */
845 sbrError = sbrDecoder_Apply ( self->hSbrDecoder,
846 pTimeData,
847 &self->streamInfo.numChannels,
848 &self->streamInfo.sampleRate,
849 self->channelOutputMapping[self->streamInfo.numChannels-1],
850 interleaved,
851 self->frameOK,
852 &self->psPossible);
853
854
855 if (sbrError == SBRDEC_OK) {
856
857 /* Update data in streaminfo structure. Assume that the SBR upsampling factor is either 1 or 2 */
858 self->flags |= AC_SBR_PRESENT;
859 if (self->streamInfo.aacSampleRate != self->streamInfo.sampleRate) {
860 if (self->streamInfo.frameSize == 768) {
861 self->streamInfo.frameSize = (self->streamInfo.aacSamplesPerFrame * 8) / 3;
862 } else {
863 self->streamInfo.frameSize = self->streamInfo.aacSamplesPerFrame << 1;
864 }
865 }
866
867 if (self->psPossible) {
868 self->flags |= AC_PS_PRESENT;
869 self->channelType[0] = ACT_FRONT;
870 self->channelType[1] = ACT_FRONT;
871 self->channelIndices[0] = 0;
872 self->channelIndices[1] = 1;
873 } else {
874 self->flags &= ~AC_PS_PRESENT;
875 }
876 }
877 }
878
879
880 if ( flags & (AACDEC_INTR | AACDEC_CLRHIST) ) {
881 /* delete data from the past (e.g. mixdown coeficients) */
882 pcmDmx_Reset( self->hPcmUtils, PCMDMX_RESET_BS_DATA );
883 }
884 /* do PCM post processing */
885 pcmDmx_ApplyFrame (
886 self->hPcmUtils,
887 pTimeData,
888 self->streamInfo.frameSize,
889 &self->streamInfo.numChannels,
890 interleaved,
891 self->channelType,
892 self->channelIndices,
893 self->channelOutputMapping
894 );
895
896
897
898 /* Signal interruption to take effect in next frame. */
899 if ( flags & AACDEC_FLUSH ) {
900 aacDecoder_SignalInterruption(self);
901 }
902
903 /* Update externally visible copy of flags */
904 self->streamInfo.flags = self->flags;
905
906 bail:
907
908 /* Update Statistics */
909 aacDecoder_UpdateBitStreamCounters(&self->streamInfo, hBs, nBits, ErrorStatus);
910
911 return ErrorStatus;
912 }
913
914 LINKSPEC_CPP void aacDecoder_Close ( HANDLE_AACDECODER self )
915 {
916 if (self == NULL)
917 return;
918
919
920
921 if (self->hPcmUtils != NULL) {
922 pcmDmx_Close( &self->hPcmUtils );
923 }
924
925
926
927 if (self->hSbrDecoder != NULL) {
928 sbrDecoder_Close(&self->hSbrDecoder);
929 }
930
931 if (self->hInput != NULL) {
932 transportDec_Close(&self->hInput);
933 }
934
935 CAacDecoder_Close(self);
936 }
937
938
939 LINKSPEC_CPP CStreamInfo* aacDecoder_GetStreamInfo ( HANDLE_AACDECODER self )
940 {
941 return CAacDecoder_GetStreamInfo(self);
942 }
943
944 LINKSPEC_CPP INT aacDecoder_GetLibInfo ( LIB_INFO *info )
945 {
946 int i;
947
948 if (info == NULL) {
949 return -1;
950 }
951
952 sbrDecoder_GetLibInfo( info );
953 transportDec_GetLibInfo( info );
954 FDK_toolsGetLibInfo( info );
955 pcmDmx_GetLibInfo( info );
956
957 /* search for next free tab */
958 for (i = 0; i < FDK_MODULE_LAST; i++) {
959 if (info[i].module_id == FDK_NONE) break;
960 }
961 if (i == FDK_MODULE_LAST) {
962 return -1;
963 }
964 info += i;
965
966 info->module_id = FDK_AACDEC;
967 /* build own library info */
968 info->version = LIB_VERSION(AACDECODER_LIB_VL0, AACDECODER_LIB_VL1, AACDECODER_LIB_VL2);
969 LIB_VERSION_STRING(info);
970 info->build_date = AACDECODER_LIB_BUILD_DATE;
971 info->build_time = AACDECODER_LIB_BUILD_TIME;
972 info->title = AACDECODER_LIB_TITLE;
973
974 /* Set flags */
975 info->flags = 0
976 | CAPF_AAC_LC
977 | CAPF_AAC_VCB11
978 | CAPF_AAC_HCR
979 | CAPF_AAC_RVLC
980 | CAPF_ER_AAC_LD
981 | CAPF_ER_AAC_ELD
982 | CAPF_AAC_CONCEALMENT
983 | CAPF_AAC_DRC
984
985 | CAPF_AAC_MPEG4
986
987
988 | CAPF_AAC_1024
989 | CAPF_AAC_960
990
991 | CAPF_AAC_512
992
993 | CAPF_AAC_480
994
995 ;
996 /* End of flags */
997
998 return 0;
999 }
1000
1001
1002
1003