2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
5 © Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
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.
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.
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.
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.
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:
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.
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.
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
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.
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."
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.
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
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.
73 5. CONTACT INFORMATION
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
78 91058 Erlangen, Germany
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
86 \brief SBR decoder frontend
87 This module provides a frontend to the SBR decoder. The function openSBR() is called for
88 initialization. The function sbrDecoder_Apply() is called for each frame. sbr_Apply() will call the
89 required functions to decode the raw SBR data (provided by env_extr.cpp), to decode the envelope data and noise floor levels [decodeSbrData()],
90 and to finally apply SBR to the current frame [sbr_dec()].
92 \sa sbrDecoder_Apply(), \ref documentationOverview
96 \page documentationOverview Overview of important information resources and source code documentation
98 The primary source code documentation is based on generated and cross-referenced HTML files using
99 <a HREF="http://www.doxygen.org">doxygen</a>. As part of this documentation
100 you can find more extensive descriptions about key concepts and algorithms at the following locations:
104 \li Buffer management: sbrDecoder_Apply() and sbr_dec()
105 \li Internal scale factors to maximize SNR on fixed point processors: #QMF_SCALE_FACTOR
106 \li Special mantissa-exponent format: Created in requantizeEnvelopeData() and used in calculateSbrEnvelope()
108 <h2>Algorithmic details</h2>
109 \li About the SBR data format: \ref SBR_HEADER_ELEMENT and \ref SBR_STANDARD_ELEMENT
110 \li Details about the bitstream decoder: env_extr.cpp
111 \li Details about the QMF filterbank and the provided polyphase implementation: qmf_dec.cpp
112 \li Details about the transposer: lpp_tran.cpp
113 \li Details about the envelope adjuster: env_calc.cpp
117 #include "sbrdecoder.h"
119 #include "FDK_bitstream.h"
121 #include "sbrdec_freq_sca.h"
122 #include "env_extr.h"
128 #include "lpp_tran.h"
129 #include "transcendent.h"
132 #include "sbrdec_drc.h"
134 #include "psbitdec.h"
137 /* Decoder library info */
138 #define SBRDECODER_LIB_VL0 2
139 #define SBRDECODER_LIB_VL1 2
140 #define SBRDECODER_LIB_VL2 3
141 #define SBRDECODER_LIB_TITLE "SBR Decoder"
142 #define SBRDECODER_LIB_BUILD_DATE __DATE__
143 #define SBRDECODER_LIB_BUILD_TIME __TIME__
148 static UCHAR
getHeaderSlot( UCHAR currentSlot
, UCHAR hdrSlotUsage
[(1)+1] )
152 UCHAR slot
= hdrSlotUsage
[currentSlot
];
154 FDK_ASSERT((1)+1 < 32);
156 for (s
= 0; s
< (1)+1; s
++) {
157 if ( (hdrSlotUsage
[s
] == slot
)
167 for (s
= 0; s
< (1)+1; s
++) {
168 occupied
|= 1 << hdrSlotUsage
[s
];
170 for (s
= 0; s
< (1)+1; s
++) {
171 if ( !(occupied
& 0x1) ) {
182 static void copySbrHeader( HANDLE_SBR_HEADER_DATA hDst
, const HANDLE_SBR_HEADER_DATA hSrc
)
184 /* copy the whole header memory (including pointers) */
185 FDKmemcpy( hDst
, hSrc
, sizeof(SBR_HEADER_DATA
) );
187 /* update pointers */
188 hDst
->freqBandData
.freqBandTable
[0] = hDst
->freqBandData
.freqBandTableLo
;
189 hDst
->freqBandData
.freqBandTable
[1] = hDst
->freqBandData
.freqBandTableHi
;
194 \brief Reset SBR decoder.
196 Reset should only be called if SBR has been sucessfully detected by
197 an appropriate checkForPayload() function.
202 SBR_ERROR
sbrDecoder_ResetElement (
203 HANDLE_SBRDECODER self
,
207 const MP4_ELEMENT_ID elementID
,
208 const int elementIndex
,
212 SBR_ERROR sbrError
= SBRDEC_OK
;
213 HANDLE_SBR_HEADER_DATA hSbrHeader
;
216 int i
, synDownsampleFac
;
218 /* Check in/out samplerates */
219 if ( sampleRateIn
< 6400
220 || sampleRateIn
> 48000
223 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
227 if ( sampleRateOut
> 96000 )
229 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
233 /* Set QMF mode flags */
234 if (self
->flags
& SBRDEC_LOW_POWER
)
235 qmfFlags
|= QMF_FLAG_LP
;
237 if (self
->coreCodec
== AOT_ER_AAC_ELD
) {
238 if (self
->flags
& SBRDEC_LD_MPS_QMF
) {
239 qmfFlags
|= QMF_FLAG_MPSLDFB
;
241 qmfFlags
|= QMF_FLAG_CLDFB
;
245 /* Set downsampling factor for synthesis filter bank */
246 if (sampleRateOut
== 0)
248 /* no single rate mode */
249 sampleRateOut
= sampleRateIn
<<1; /* In case of implicit signalling, assume dual rate SBR */
252 if ( sampleRateIn
== sampleRateOut
) {
253 synDownsampleFac
= 2;
255 synDownsampleFac
= 1;
258 self
->synDownsampleFac
= synDownsampleFac
;
259 self
->sampleRateOut
= sampleRateOut
;
264 for (i
= 0; i
< (1)+1; i
++)
266 hSbrHeader
= &(self
->sbrHeader
[elementIndex
][i
]);
268 /* init a default header such that we can at least do upsampling later */
269 sbrError
= initHeaderData(
279 if (sbrError
!= SBRDEC_OK
) {
283 /* Init SBR channels going to be assigned to a SBR element */
287 for (ch
=0; ch
<self
->pSbrElement
[elementIndex
]->nChannels
; ch
++)
289 /* and create sbrDec */
290 sbrError
= createSbrDec (self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
],
292 &self
->pSbrElement
[elementIndex
]->transposerSettings
,
299 if (sbrError
!= SBRDEC_OK
) {
305 //FDKmemclear(sbr_OverlapBuffer, sizeof(sbr_OverlapBuffer));
307 if (self
->numSbrElements
== 1) {
308 switch ( self
->coreCodec
) {
312 case AOT_ER_AAC_SCAL
:
314 case AOT_DRM_SURROUND
:
315 if (CreatePsDec ( &self
->hParametricStereoDec
, samplesPerFrame
)) {
316 sbrError
= SBRDEC_CREATE_ERROR
;
325 /* Init frame delay slot handling */
326 self
->pSbrElement
[elementIndex
]->useFrameSlot
= 0;
327 for (i
= 0; i
< ((1)+1); i
++) {
328 self
->pSbrElement
[elementIndex
]->useHeaderSlot
[i
] = i
;
337 SBR_ERROR
sbrDecoder_Open ( HANDLE_SBRDECODER
* pSelf
)
339 HANDLE_SBRDECODER self
= NULL
;
340 SBR_ERROR sbrError
= SBRDEC_OK
;
342 /* Get memory for this instance */
343 self
= GetRam_SbrDecoder();
345 sbrError
= SBRDEC_MEM_ALLOC_FAILED
;
349 self
->workBuffer1
= GetRam_SbrDecWorkBuffer1();
350 self
->workBuffer2
= GetRam_SbrDecWorkBuffer2();
352 if ( self
->workBuffer1
== NULL
353 || self
->workBuffer2
== NULL
)
355 sbrError
= SBRDEC_MEM_ALLOC_FAILED
;
360 Already zero because of calloc
361 self->numSbrElements = 0;
362 self->numSbrChannels = 0;
363 self->codecFrameSize = 0;
366 self
->numDelayFrames
= (1); /* set to the max value by default */
375 * \brief determine if the given core codec AOT can be processed or not.
376 * \param coreCodec core codec audio object type.
377 * \return 1 if SBR can be processed, 0 if SBR cannot be processed/applied.
380 int sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec
)
386 case AOT_ER_AAC_SCAL
:
395 void sbrDecoder_DestroyElement (
396 HANDLE_SBRDECODER self
,
397 const int elementIndex
400 if (self
->pSbrElement
[elementIndex
] != NULL
) {
403 for (ch
=0; ch
<SBRDEC_MAX_CH_PER_ELEMENT
; ch
++) {
404 if (self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] != NULL
) {
405 deleteSbrDec( self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] );
406 FreeRam_SbrDecChannel( &self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] );
407 self
->numSbrChannels
-= 1;
410 FreeRam_SbrDecElement( &self
->pSbrElement
[elementIndex
] );
411 self
->numSbrElements
-= 1;
416 SBR_ERROR
sbrDecoder_InitElement (
417 HANDLE_SBRDECODER self
,
418 const int sampleRateIn
,
419 const int sampleRateOut
,
420 const int samplesPerFrame
,
421 const AUDIO_OBJECT_TYPE coreCodec
,
422 const MP4_ELEMENT_ID elementID
,
423 const int elementIndex
426 SBR_ERROR sbrError
= SBRDEC_OK
;
428 int nSbrElementsStart
= self
->numSbrElements
;
430 /* Check core codec AOT */
431 if (! sbrDecoder_isCoreCodecValid(coreCodec
) || elementIndex
>= (4)) {
432 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
436 if ( elementID
!= ID_SCE
&& elementID
!= ID_CPE
&& elementID
!= ID_LFE
)
438 sbrError
= SBRDEC_UNSUPPORTED_CONFIG
;
442 if ( self
->sampleRateIn
== sampleRateIn
443 && self
->codecFrameSize
== samplesPerFrame
444 && self
->coreCodec
== coreCodec
445 && self
->pSbrElement
[elementIndex
] != NULL
446 && self
->pSbrElement
[elementIndex
]->elementID
== elementID
453 self
->sampleRateIn
= sampleRateIn
;
454 self
->codecFrameSize
= samplesPerFrame
;
455 self
->coreCodec
= coreCodec
;
458 self
->flags
|= (coreCodec
== AOT_ER_AAC_ELD
) ? SBRDEC_ELD_GRID
: 0;
460 /* Init SBR elements */
464 if (self
->pSbrElement
[elementIndex
] == NULL
) {
465 self
->pSbrElement
[elementIndex
] = GetRam_SbrDecElement(elementIndex
);
466 if (self
->pSbrElement
[elementIndex
] == NULL
) {
467 sbrError
= SBRDEC_MEM_ALLOC_FAILED
;
470 self
->numSbrElements
++;
472 self
->numSbrChannels
-= self
->pSbrElement
[elementIndex
]->nChannels
;
475 /* Save element ID for sanity checks and to have a fallback for concealment. */
476 self
->pSbrElement
[elementIndex
]->elementID
= elementID
;
478 /* Determine amount of channels for this element */
481 case ID_CPE
: elChannels
=2;
484 case ID_SCE
: elChannels
=1;
486 default: elChannels
=0;
490 /* Handle case of Parametric Stereo */
491 if ( elementIndex
== 0 && elementID
== ID_SCE
) {
496 case AOT_ER_AAC_SCAL
:
498 case AOT_DRM_SURROUND
:
506 self
->pSbrElement
[elementIndex
]->nChannels
= elChannels
;
508 for (ch
=0; ch
<elChannels
; ch
++)
510 if (self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] == NULL
) {
511 self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] = GetRam_SbrDecChannel(chCnt
);
512 if (self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] == NULL
) {
513 sbrError
= SBRDEC_MEM_ALLOC_FAILED
;
517 self
->numSbrChannels
++;
519 sbrDecoder_drcInitChannel( &self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
]->SbrDec
.sbrDrcChannel
);
521 /* Add reference pointer to workbuffers. */
522 self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
]->SbrDec
.WorkBuffer1
= self
->workBuffer1
;
523 self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
]->SbrDec
.WorkBuffer2
= self
->workBuffer2
;
526 if (elChannels
== 1 && self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] != NULL
) {
527 deleteSbrDec( self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] );
528 FreeRam_SbrDecChannel( &self
->pSbrElement
[elementIndex
]->pSbrChannel
[ch
] );
532 /* clear error flags for all delay slots */
533 FDKmemclear(self
->pSbrElement
[elementIndex
]->frameErrorFlag
, ((1)+1)*sizeof(UCHAR
));
535 /* Initialize this instance */
536 sbrError
= sbrDecoder_ResetElement(
543 (coreCodec
== AOT_ER_AAC_ELD
) ? 0 : (6)
549 if (sbrError
!= SBRDEC_OK
) {
550 if (nSbrElementsStart
< self
->numSbrElements
) {
551 /* Free the memory allocated for this element */
552 sbrDecoder_DestroyElement( self
, elementIndex
);
553 } else if (self
->pSbrElement
[elementIndex
] != NULL
) {
554 /* Set error flag to trigger concealment */
555 self
->pSbrElement
[elementIndex
]->frameErrorFlag
[self
->pSbrElement
[elementIndex
]->useFrameSlot
] = 1;
563 * \brief Apply decoded SBR header for one element.
564 * \param self SBR decoder instance handle
565 * \param hSbrHeader SBR header handle to be processed.
566 * \param hSbrChannel pointer array to the SBR element channels corresponding to the SBR header.
567 * \param headerStatus header status value returned from SBR header parser.
568 * \param numElementChannels amount of channels for the SBR element whos header is to be processed.
571 SBR_ERROR
sbrDecoder_HeaderUpdate(
572 HANDLE_SBRDECODER self
,
573 HANDLE_SBR_HEADER_DATA hSbrHeader
,
574 SBR_HEADER_STATUS headerStatus
,
575 HANDLE_SBR_CHANNEL hSbrChannel
[],
576 const int numElementChannels
579 SBR_ERROR errorStatus
= SBRDEC_OK
;
582 change of control data, reset decoder
584 errorStatus
= resetFreqBandTables(hSbrHeader
, self
->flags
);
586 if (errorStatus
== SBRDEC_OK
) {
587 if (hSbrHeader
->syncState
== UPSAMPLING
&& headerStatus
!= HEADER_RESET
)
589 /* As the default header would limit the frequency range,
590 lowSubband and highSubband must be patched. */
591 hSbrHeader
->freqBandData
.lowSubband
= hSbrHeader
->numberOfAnalysisBands
;
592 hSbrHeader
->freqBandData
.highSubband
= hSbrHeader
->numberOfAnalysisBands
;
595 /* Trigger a reset before processing this slot */
596 hSbrHeader
->status
|= SBRDEC_HDR_STAT_RESET
;
602 INT
sbrDecoder_Header (
603 HANDLE_SBRDECODER self
,
604 HANDLE_FDK_BITSTREAM hBs
,
605 const INT sampleRateIn
,
606 const INT sampleRateOut
,
607 const INT samplesPerFrame
,
608 const AUDIO_OBJECT_TYPE coreCodec
,
609 const MP4_ELEMENT_ID elementID
,
610 const INT elementIndex
613 SBR_HEADER_STATUS headerStatus
;
614 HANDLE_SBR_HEADER_DATA hSbrHeader
;
615 SBR_ERROR sbrError
= SBRDEC_OK
;
618 if ( self
== NULL
|| elementIndex
> (4) )
620 return SBRDEC_UNSUPPORTED_CONFIG
;
623 if (! sbrDecoder_isCoreCodecValid(coreCodec
)) {
624 return SBRDEC_UNSUPPORTED_CONFIG
;
627 sbrError
= sbrDecoder_InitElement(
637 if (sbrError
!= SBRDEC_OK
) {
641 headerIndex
= getHeaderSlot(self
->pSbrElement
[elementIndex
]->useFrameSlot
,
642 self
->pSbrElement
[elementIndex
]->useHeaderSlot
);
643 hSbrHeader
= &(self
->sbrHeader
[elementIndex
][headerIndex
]);
645 headerStatus
= sbrGetHeaderData ( hSbrHeader
,
652 SBR_DECODER_ELEMENT
*pSbrElement
;
654 pSbrElement
= self
->pSbrElement
[elementIndex
];
657 if (pSbrElement
!= NULL
) {
658 if ( (elementID
== ID_CPE
&& pSbrElement
->nChannels
!= 2)
659 || (elementID
!= ID_CPE
&& pSbrElement
->nChannels
!= 1) )
661 return SBRDEC_UNSUPPORTED_CONFIG
;
663 if ( headerStatus
== HEADER_RESET
) {
665 sbrError
= sbrDecoder_HeaderUpdate(
669 pSbrElement
->pSbrChannel
,
670 pSbrElement
->nChannels
673 if (sbrError
== SBRDEC_OK
) {
674 hSbrHeader
->syncState
= SBR_HEADER
;
675 hSbrHeader
->status
|= SBRDEC_HDR_STAT_UPDATE
;
678 Since we already have overwritten the old SBR header the only way out is UPSAMPLING!
679 This will be prepared in the next step.
689 SBR_ERROR
sbrDecoder_SetParam (HANDLE_SBRDECODER self
,
690 const SBRDEC_PARAM param
,
693 SBR_ERROR errorStatus
= SBRDEC_OK
;
695 /* configure the subsystems */
698 case SBR_SYSTEM_BITSTREAM_DELAY
:
699 if (value
< 0 || value
> (1)) {
700 errorStatus
= SBRDEC_SET_PARAM_FAIL
;
704 errorStatus
= SBRDEC_NOT_INITIALIZED
;
706 self
->numDelayFrames
= (UCHAR
)value
;
711 errorStatus
= SBRDEC_NOT_INITIALIZED
;
714 self
->flags
|= SBRDEC_LOW_POWER
;
716 self
->flags
&= ~SBRDEC_LOW_POWER
;
720 case SBR_LD_QMF_TIME_ALIGN
:
722 errorStatus
= SBRDEC_NOT_INITIALIZED
;
725 self
->flags
|= SBRDEC_LD_MPS_QMF
;
727 self
->flags
&= ~SBRDEC_LD_MPS_QMF
;
731 case SBR_BS_INTERRUPTION
:
736 errorStatus
= SBRDEC_NOT_INITIALIZED
;
740 /* Loop over SBR elements */
741 for (elementIndex
= 0; elementIndex
< self
->numSbrElements
; elementIndex
++)
743 HANDLE_SBR_HEADER_DATA hSbrHeader
;
744 int headerIndex
= getHeaderSlot(self
->pSbrElement
[elementIndex
]->useFrameSlot
,
745 self
->pSbrElement
[elementIndex
]->useHeaderSlot
);
747 hSbrHeader
= &(self
->sbrHeader
[elementIndex
][headerIndex
]);
749 /* Set sync state UPSAMPLING for the corresponding slot.
750 This switches off bitstream parsing until a new header arrives. */
751 hSbrHeader
->syncState
= UPSAMPLING
;
752 hSbrHeader
->status
|= SBRDEC_HDR_STAT_UPDATE
;
757 errorStatus
= SBRDEC_SET_PARAM_FAIL
;
759 } /* switch(param) */
761 return (errorStatus
);
765 SBRDEC_DRC_CHANNEL
* sbrDecoder_drcGetChannel( const HANDLE_SBRDECODER self
, const INT channel
)
767 SBRDEC_DRC_CHANNEL
*pSbrDrcChannelData
= NULL
;
768 int elementIndex
, elChanIdx
=0, numCh
=0;
770 for (elementIndex
= 0; (elementIndex
< (4)) && (numCh
<= channel
); elementIndex
++)
772 SBR_DECODER_ELEMENT
*pSbrElement
= self
->pSbrElement
[elementIndex
];
776 if (pSbrElement
== NULL
) break;
778 /* Determine amount of channels for this element */
779 switch (pSbrElement
->elementID
) {
780 case ID_CPE
: elChannels
= 2;
783 case ID_SCE
: elChannels
= 1;
786 default: elChannels
= 0;
790 /* Limit with actual allocated element channels */
791 elChannels
= FDKmin(elChannels
, pSbrElement
->nChannels
);
793 for (c
= 0; (c
< elChannels
) && (numCh
<= channel
); c
++) {
794 if (pSbrElement
->pSbrChannel
[elChanIdx
] != NULL
) {
803 if (elChanIdx
< 0 || elementIndex
< 0) {
807 if ( self
->pSbrElement
[elementIndex
] != NULL
) {
808 if ( self
->pSbrElement
[elementIndex
]->pSbrChannel
[elChanIdx
] != NULL
)
810 pSbrDrcChannelData
= &self
->pSbrElement
[elementIndex
]->pSbrChannel
[elChanIdx
]->SbrDec
.sbrDrcChannel
;
814 return (pSbrDrcChannelData
);
817 SBR_ERROR
sbrDecoder_drcFeedChannel ( HANDLE_SBRDECODER self
,
820 FIXP_DBL
*pNextFact_mag
,
822 SHORT drcInterpolationScheme
,
826 SBRDEC_DRC_CHANNEL
*pSbrDrcChannelData
= NULL
;
827 int band
, isValidData
= 0;
830 return SBRDEC_NOT_INITIALIZED
;
832 if (ch
> (6) || pNextFact_mag
== NULL
) {
833 return SBRDEC_SET_PARAM_FAIL
;
836 /* Search for gain values different to 1.0f */
837 for (band
= 0; band
< numBands
; band
+= 1) {
838 if ( !((pNextFact_mag
[band
] == FL2FXCONST_DBL(0.5)) && (nextFact_exp
== 1))
839 && !((pNextFact_mag
[band
] == (FIXP_DBL
)MAXVAL_DBL
) && (nextFact_exp
== 0)) ) {
845 /* Find the right SBR channel */
846 pSbrDrcChannelData
= sbrDecoder_drcGetChannel( self
, ch
);
848 if ( pSbrDrcChannelData
!= NULL
) {
849 if ( pSbrDrcChannelData
->enable
|| isValidData
)
850 { /* Activate processing only with real and valid data */
853 pSbrDrcChannelData
->enable
= 1;
854 pSbrDrcChannelData
->numBandsNext
= numBands
;
856 pSbrDrcChannelData
->winSequenceNext
= winSequence
;
857 pSbrDrcChannelData
->drcInterpolationSchemeNext
= drcInterpolationScheme
;
858 pSbrDrcChannelData
->nextFact_exp
= nextFact_exp
;
860 for (i
= 0; i
< (int)numBands
; i
++) {
861 pSbrDrcChannelData
->bandTopNext
[i
] = pBandTop
[i
];
862 pSbrDrcChannelData
->nextFact_mag
[i
] = pNextFact_mag
[i
];
871 void sbrDecoder_drcDisable ( HANDLE_SBRDECODER self
,
874 SBRDEC_DRC_CHANNEL
*pSbrDrcChannelData
= NULL
;
878 || (self
->numSbrElements
== 0)
879 || (self
->numSbrChannels
== 0) ) {
883 /* Find the right SBR channel */
884 pSbrDrcChannelData
= sbrDecoder_drcGetChannel( self
, ch
);
886 if ( pSbrDrcChannelData
!= NULL
) {
887 sbrDecoder_drcInitChannel( pSbrDrcChannelData
);
893 SBR_ERROR
sbrDecoder_Parse(
894 HANDLE_SBRDECODER self
,
895 HANDLE_FDK_BITSTREAM hBs
,
899 MP4_ELEMENT_ID prevElement
,
901 int fGlobalIndependencyFlag
904 SBR_DECODER_ELEMENT
*hSbrElement
;
905 HANDLE_SBR_HEADER_DATA hSbrHeader
;
906 HANDLE_SBR_CHANNEL
*pSbrChannel
;
908 SBR_FRAME_DATA
*hFrameDataLeft
;
909 SBR_FRAME_DATA
*hFrameDataRight
;
911 SBR_ERROR errorStatus
= SBRDEC_OK
;
912 SBR_SYNC_STATE initialSyncState
;
913 SBR_HEADER_STATUS headerStatus
= HEADER_NOT_PRESENT
;
919 int fDoDecodeSbrData
= 1;
921 int lastSlot
, lastHdrSlot
= 0, thisHdrSlot
;
923 /* Remember start position of SBR element */
924 startPos
= FDKgetValidBits(hBs
);
926 /* SBR sanity checks */
927 if ( self
== NULL
|| self
->pSbrElement
[elementIndex
] == NULL
) {
928 errorStatus
= SBRDEC_NOT_INITIALIZED
;
932 hSbrElement
= self
->pSbrElement
[elementIndex
];
934 lastSlot
= (hSbrElement
->useFrameSlot
> 0) ? hSbrElement
->useFrameSlot
-1 : self
->numDelayFrames
;
935 lastHdrSlot
= hSbrElement
->useHeaderSlot
[lastSlot
];
936 thisHdrSlot
= getHeaderSlot( hSbrElement
->useFrameSlot
, hSbrElement
->useHeaderSlot
); /* Get a free header slot not used by frames not processed yet. */
938 /* Assign the free slot to store a new header if there is one. */
939 hSbrHeader
= &self
->sbrHeader
[elementIndex
][thisHdrSlot
];
941 pSbrChannel
= hSbrElement
->pSbrChannel
;
942 stereo
= (hSbrElement
->elementID
== ID_CPE
) ? 1 : 0;
944 hFrameDataLeft
= &self
->pSbrElement
[elementIndex
]->pSbrChannel
[0]->frameData
[hSbrElement
->useFrameSlot
];
945 hFrameDataRight
= &self
->pSbrElement
[elementIndex
]->pSbrChannel
[1]->frameData
[hSbrElement
->useFrameSlot
];
947 initialSyncState
= hSbrHeader
->syncState
;
949 /* reset PS flag; will be set after PS was found */
950 self
->flags
&= ~SBRDEC_PS_DECODED
;
952 if (hSbrHeader
->status
& SBRDEC_HDR_STAT_UPDATE
) {
953 /* Got a new header from extern (e.g. from an ASC) */
954 headerStatus
= HEADER_OK
;
955 hSbrHeader
->status
&= ~SBRDEC_HDR_STAT_UPDATE
;
957 else if (thisHdrSlot
!= lastHdrSlot
) {
958 /* Copy the last header into this slot otherwise the
959 header compare will trigger more HEADER_RESETs than needed. */
960 copySbrHeader( hSbrHeader
, &self
->sbrHeader
[elementIndex
][lastHdrSlot
] );
964 Check if bit stream data is valid and matches the element context
966 if ( ((prevElement
!= ID_SCE
) && (prevElement
!= ID_CPE
)) || prevElement
!= hSbrElement
->elementID
) {
967 /* In case of LFE we also land here, since there is no LFE SBR element (do upsampling only) */
968 fDoDecodeSbrData
= 0;
971 if (fDoDecodeSbrData
)
973 if ((INT
)FDKgetValidBits(hBs
) <= 0) {
974 fDoDecodeSbrData
= 0;
981 if (fDoDecodeSbrData
)
984 switch (self
->coreCodec
) {
986 FDKpushFor (hBs
, 10);
987 /* check sbrcrc later: we don't know the payload length now */
990 CRCLen
= bsPayLen
- 10; /* change: 0 => i */
992 fDoDecodeSbrData
= 0;
994 fDoDecodeSbrData
= SbrCrcCheck (hBs
, CRCLen
);
999 } /* if (fDoDecodeSbrData) */
1002 Read in the header data and issue a reset if change occured
1004 if (fDoDecodeSbrData
)
1006 int sbrHeaderPresent
;
1009 sbrHeaderPresent
= FDKreadBit(hBs
);
1012 if ( sbrHeaderPresent
) {
1013 headerStatus
= sbrGetHeaderData (hSbrHeader
,
1019 if (headerStatus
== HEADER_RESET
)
1021 errorStatus
= sbrDecoder_HeaderUpdate(
1026 hSbrElement
->nChannels
1029 if (errorStatus
== SBRDEC_OK
) {
1030 hSbrHeader
->syncState
= SBR_HEADER
;
1032 hSbrHeader
->syncState
= SBR_NOT_INITIALIZED
;
1036 if (errorStatus
!= SBRDEC_OK
) {
1037 fDoDecodeSbrData
= 0;
1039 } /* if (fDoDecodeSbrData) */
1042 Print debugging output only if state has changed
1045 /* read frame data */
1046 if ((hSbrHeader
->syncState
>= SBR_HEADER
) && fDoDecodeSbrData
) {
1048 /* read the SBR element data */
1050 sbrFrameOk
= sbrGetChannelPairElement(hSbrHeader
,
1055 self
->pSbrElement
[elementIndex
]->transposerSettings
.overlap
);
1058 if (self
->hParametricStereoDec
!= NULL
) {
1059 /* update slot index for PS bitstream parsing */
1060 self
->hParametricStereoDec
->bsLastSlot
= self
->hParametricStereoDec
->bsReadSlot
;
1061 self
->hParametricStereoDec
->bsReadSlot
= hSbrElement
->useFrameSlot
;
1063 sbrFrameOk
= sbrGetSingleChannelElement(hSbrHeader
,
1066 self
->hParametricStereoDec
,
1068 self
->pSbrElement
[elementIndex
]->transposerSettings
.overlap
);
1071 fDoDecodeSbrData
= 0;
1077 valBits
= bsPayLen
- ((INT
)startPos
- (INT
)FDKgetValidBits(hBs
));
1079 valBits
= (INT
)FDKgetValidBits(hBs
);
1082 if ( crcFlag
== 1 ) {
1083 switch (self
->coreCodec
) {
1084 case AOT_ER_AAC_ELD
:
1086 /* late crc check for eld */
1087 INT payloadbits
= (INT
)startPos
- (INT
)FDKgetValidBits(hBs
) - startPos
;
1088 INT crcLen
= payloadbits
- 10;
1089 FDKpushBack(hBs
, payloadbits
);
1090 fDoDecodeSbrData
= SbrCrcCheck (hBs
, crcLen
);
1091 FDKpushFor(hBs
, crcLen
);
1099 /* sanity check of remaining bits */
1101 fDoDecodeSbrData
= 0;
1103 switch (self
->coreCodec
) {
1108 /* This sanity check is only meaningful with General Audio bitstreams */
1109 int alignBits
= valBits
& 0x7;
1111 if (valBits
> alignBits
) {
1112 fDoDecodeSbrData
= 0;
1117 /* No sanity check available */
1124 if (!fDoDecodeSbrData
) {
1125 /* Set error flag for this slot to trigger concealment */
1126 self
->pSbrElement
[elementIndex
]->frameErrorFlag
[hSbrElement
->useFrameSlot
] = 1;
1127 errorStatus
= SBRDEC_PARSE_ERROR
;
1129 /* Everything seems to be ok so clear the error flag */
1130 self
->pSbrElement
[elementIndex
]->frameErrorFlag
[hSbrElement
->useFrameSlot
] = 0;
1134 /* Turn coupling off explicitely to avoid access to absent right frame data
1135 that might occur with corrupt bitstreams. */
1136 hFrameDataLeft
->coupling
= COUPLING_OFF
;
1140 if (errorStatus
== SBRDEC_OK
) {
1141 if (headerStatus
== HEADER_NOT_PRESENT
) {
1142 /* Use the old header for this frame */
1143 hSbrElement
->useHeaderSlot
[hSbrElement
->useFrameSlot
] = lastHdrSlot
;
1145 /* Use the new header for this frame */
1146 hSbrElement
->useHeaderSlot
[hSbrElement
->useFrameSlot
] = thisHdrSlot
;
1149 /* Move frame pointer to the next slot which is up to be decoded/applied next */
1150 hSbrElement
->useFrameSlot
= (hSbrElement
->useFrameSlot
+1) % (self
->numDelayFrames
+1);
1153 *count
-= startPos
- FDKgetValidBits(hBs
);
1160 * \brief Render one SBR element into time domain signal.
1161 * \param self SBR decoder handle
1162 * \param timeData pointer to output buffer
1163 * \param interleaved flag indicating interleaved channel output
1164 * \param channelMapping pointer to UCHAR array where next 2 channel offsets are stored.
1165 * \param elementIndex enumerating index of the SBR element to render.
1166 * \param numInChannels number of channels from core coder (reading stride).
1167 * \param numOutChannels pointer to a location to return number of output channels.
1168 * \param psPossible flag indicating if PS is possible or not.
1169 * \return SBRDEC_OK if successfull, else error code
1172 sbrDecoder_DecodeElement (
1173 HANDLE_SBRDECODER self
,
1175 const int interleaved
,
1176 const UCHAR
*channelMapping
,
1177 const int elementIndex
,
1178 const int numInChannels
,
1179 int *numOutChannels
,
1180 const int psPossible
1183 SBR_DECODER_ELEMENT
*hSbrElement
= self
->pSbrElement
[elementIndex
];
1184 HANDLE_SBR_CHANNEL
*pSbrChannel
= self
->pSbrElement
[elementIndex
]->pSbrChannel
;
1185 HANDLE_SBR_HEADER_DATA hSbrHeader
= &self
->sbrHeader
[elementIndex
][hSbrElement
->useHeaderSlot
[hSbrElement
->useFrameSlot
]];
1186 HANDLE_PS_DEC h_ps_d
= self
->hParametricStereoDec
;
1188 /* get memory for frame data from scratch */
1189 SBR_FRAME_DATA
*hFrameDataLeft
= &hSbrElement
->pSbrChannel
[0]->frameData
[hSbrElement
->useFrameSlot
];
1190 SBR_FRAME_DATA
*hFrameDataRight
= &hSbrElement
->pSbrChannel
[1]->frameData
[hSbrElement
->useFrameSlot
];
1192 SBR_ERROR errorStatus
= SBRDEC_OK
;
1195 INT strideIn
, strideOut
, offset0
, offset1
;
1196 INT codecFrameSize
= self
->codecFrameSize
;
1198 int stereo
= (hSbrElement
->elementID
== ID_CPE
) ? 1 : 0;
1199 int numElementChannels
= hSbrElement
->nChannels
; /* Number of channels of the current SBR element */
1201 /* Update the header error flag */
1202 hSbrHeader
->frameErrorFlag
= hSbrElement
->frameErrorFlag
[hSbrElement
->useFrameSlot
];
1205 Prepare filterbank for upsampling if no valid bit stream data is available.
1207 if ( hSbrHeader
->syncState
== SBR_NOT_INITIALIZED
)
1209 errorStatus
= initHeaderData(
1212 self
->sampleRateOut
,
1217 if (errorStatus
!= SBRDEC_OK
) {
1221 hSbrHeader
->syncState
= UPSAMPLING
;
1223 errorStatus
= sbrDecoder_HeaderUpdate(
1228 hSbrElement
->nChannels
1231 if (errorStatus
!= SBRDEC_OK
) {
1232 hSbrHeader
->syncState
= SBR_NOT_INITIALIZED
;
1238 if (hSbrHeader
->status
& SBRDEC_HDR_STAT_RESET
) {
1240 for (ch
= 0 ; ch
< numElementChannels
; ch
++) {
1241 SBR_ERROR errorStatusTmp
= SBRDEC_OK
;
1243 errorStatusTmp
= resetSbrDec (
1244 &pSbrChannel
[ch
]->SbrDec
,
1246 &pSbrChannel
[ch
]->prevFrameData
,
1247 self
->flags
& SBRDEC_LOW_POWER
,
1248 self
->synDownsampleFac
1251 if (errorStatusTmp
!= SBRDEC_OK
) {
1252 errorStatus
= errorStatusTmp
;
1255 hSbrHeader
->status
&= ~SBRDEC_HDR_STAT_RESET
;
1259 if ( (hSbrHeader
->syncState
== SBR_ACTIVE
)
1260 || ((hSbrHeader
->syncState
== SBR_HEADER
) && (hSbrHeader
->frameErrorFlag
== 0)) )
1262 errorStatus
= SBRDEC_OK
;
1264 decodeSbrData (hSbrHeader
,
1266 &pSbrChannel
[0]->prevFrameData
,
1267 (stereo
) ? hFrameDataRight
: NULL
,
1268 (stereo
) ? &pSbrChannel
[1]->prevFrameData
: NULL
);
1271 /* Now we have a full parameter set and can do parameter
1272 based concealment instead of plain upsampling. */
1273 hSbrHeader
->syncState
= SBR_ACTIVE
;
1276 /* decode PS data if available */
1277 if (h_ps_d
!= NULL
&& psPossible
) {
1280 /* define which frame delay line slot to process */
1281 h_ps_d
->processSlot
= hSbrElement
->useFrameSlot
;
1283 applyPs
= DecodePs(h_ps_d
, hSbrHeader
->frameErrorFlag
);
1284 self
->flags
|= (applyPs
) ? SBRDEC_PS_DECODED
: 0;
1287 /* Set strides for reading and writing */
1289 strideIn
= numInChannels
;
1291 strideOut
= (numInChannels
< 2) ? 2 : numInChannels
;
1293 strideOut
= numInChannels
;
1294 offset0
= channelMapping
[0];
1295 offset1
= channelMapping
[1];
1299 offset0
= channelMapping
[0]*2*codecFrameSize
;
1300 offset1
= channelMapping
[1]*2*codecFrameSize
;
1303 /* use same buffers for left and right channel and apply PS per timeslot */
1304 /* Process left channel */
1305 //FDKprintf("self->codecFrameSize %d\t%d\n",self->codecFrameSize,self->sampleRateIn);
1306 sbr_dec (&pSbrChannel
[0]->SbrDec
,
1309 &pSbrChannel
[1]->SbrDec
,
1315 &pSbrChannel
[0]->prevFrameData
,
1316 (hSbrHeader
->syncState
== SBR_ACTIVE
),
1322 /* Process right channel */
1323 sbr_dec (&pSbrChannel
[1]->SbrDec
,
1332 &pSbrChannel
[1]->prevFrameData
,
1333 (hSbrHeader
->syncState
== SBR_ACTIVE
),
1339 if (h_ps_d
!= NULL
) {
1340 /* save PS status for next run */
1341 h_ps_d
->psDecodedPrv
= (self
->flags
& SBRDEC_PS_DECODED
) ? 1 : 0 ;
1347 FDK_ASSERT(strideOut
> 1);
1348 if ( !(self
->flags
& SBRDEC_PS_DECODED
) ) {
1349 /* A decoder which is able to decode PS has to produce a stereo output even if no PS data is availble. */
1350 /* So copy left channel to right channel. */
1354 FDK_ASSERT(strideOut
== 2);
1357 for (i
= codecFrameSize
; i
--; )
1359 INT_PCM tmp
; /* This temporal variable is required because some compilers can't do *ptr++ = *ptr++ correctly. */
1360 tmp
= *ptr
++; *ptr
++ = tmp
;
1361 tmp
= *ptr
++; *ptr
++ = tmp
;
1364 FDKmemcpy( timeData
+2*codecFrameSize
, timeData
, 2*codecFrameSize
*sizeof(INT_PCM
) );
1367 *numOutChannels
= 2; /* Output minimum two channels when PS is enabled. */
1374 SBR_ERROR
sbrDecoder_Apply ( HANDLE_SBRDECODER self
,
1378 const UCHAR channelMapping
[(6)],
1379 const int interleaved
,
1380 const int coreDecodedOk
,
1383 SBR_ERROR errorStatus
= SBRDEC_OK
;
1387 int numCoreChannels
= *numChannels
;
1388 int numSbrChannels
= 0;
1390 psPossible
= *psDecoded
;
1392 if (self
->numSbrElements
< 1) {
1393 /* exit immediately to avoid access violations */
1394 return SBRDEC_CREATE_ERROR
;
1397 /* Sanity check of allocated SBR elements. */
1398 for (sbrElementNum
=0; sbrElementNum
<self
->numSbrElements
; sbrElementNum
++) {
1399 if (self
->pSbrElement
[sbrElementNum
] == NULL
) {
1400 return SBRDEC_CREATE_ERROR
;
1404 if (self
->numSbrElements
!= 1 || self
->pSbrElement
[0]->elementID
!= ID_SCE
) {
1409 /* In case of non-interleaved time domain data and upsampling, make room for bigger SBR output. */
1410 if (self
->synDownsampleFac
== 1 && interleaved
== 0) {
1411 int c
, outputFrameSize
;
1414 self
->pSbrElement
[0]->pSbrChannel
[0]->SbrDec
.SynthesisQMF
.no_channels
1415 * self
->pSbrElement
[0]->pSbrChannel
[0]->SbrDec
.SynthesisQMF
.no_col
;
1417 for (c
=numCoreChannels
-1; c
>0; c
--) {
1418 FDKmemmove(timeData
+ c
*outputFrameSize
, timeData
+ c
*self
->codecFrameSize
, self
->codecFrameSize
*sizeof(INT_PCM
));
1423 /* Make sure that even if no SBR data was found/parsed *psDecoded is returned 1 if psPossible was 0. */
1424 if (psPossible
== 0) {
1425 self
->flags
&= ~SBRDEC_PS_DECODED
;
1428 /* Loop over SBR elements */
1429 for (sbrElementNum
= 0; sbrElementNum
<self
->numSbrElements
; sbrElementNum
++)
1433 if (psPossible
&& self
->pSbrElement
[sbrElementNum
]->pSbrChannel
[1] == NULL
) {
1434 errorStatus
= SBRDEC_UNSUPPORTED_CONFIG
;
1438 numElementChan
= (self
->pSbrElement
[sbrElementNum
]->elementID
== ID_CPE
) ? 2 : 1;
1440 /* If core signal is bad then force upsampling */
1441 if ( ! coreDecodedOk
) {
1442 self
->pSbrElement
[sbrElementNum
]->frameErrorFlag
[self
->pSbrElement
[sbrElementNum
]->useFrameSlot
] = 1;
1445 errorStatus
= sbrDecoder_DecodeElement (
1456 if (errorStatus
!= SBRDEC_OK
) {
1460 numSbrChannels
+= numElementChan
;
1461 channelMapping
+= numElementChan
;
1463 if (numSbrChannels
>= numCoreChannels
) {
1468 /* Update numChannels and samplerate */
1469 *numChannels
= numSbrChannels
;
1470 *sampleRate
= self
->sampleRateOut
;
1471 *psDecoded
= (self
->flags
& SBRDEC_PS_DECODED
) ? 1 : 0;
1481 SBR_ERROR
sbrDecoder_Close ( HANDLE_SBRDECODER
*pSelf
)
1483 HANDLE_SBRDECODER self
= *pSelf
;
1488 if (self
->hParametricStereoDec
!= NULL
) {
1489 DeletePsDec ( &self
->hParametricStereoDec
);
1492 if (self
->workBuffer1
!= NULL
) {
1493 FreeRam_SbrDecWorkBuffer1(&self
->workBuffer1
);
1495 if (self
->workBuffer2
!= NULL
) {
1496 FreeRam_SbrDecWorkBuffer2(&self
->workBuffer2
);
1499 for (i
= 0; i
< (4); i
++) {
1500 sbrDecoder_DestroyElement( self
, i
);
1503 FreeRam_SbrDecoder(pSelf
);
1510 INT
sbrDecoder_GetLibInfo( LIB_INFO
*info
)
1518 /* search for next free tab */
1519 for (i
= 0; i
< FDK_MODULE_LAST
; i
++) {
1520 if (info
[i
].module_id
== FDK_NONE
)
1523 if (i
== FDK_MODULE_LAST
)
1527 info
->module_id
= FDK_SBRDEC
;
1528 info
->version
= LIB_VERSION(SBRDECODER_LIB_VL0
, SBRDECODER_LIB_VL1
, SBRDECODER_LIB_VL2
);
1529 LIB_VERSION_STRING(info
);
1530 info
->build_date
= (char *)SBRDECODER_LIB_BUILD_DATE
;
1531 info
->build_time
= (char *)SBRDECODER_LIB_BUILD_TIME
;
1532 info
->title
= (char *)SBRDECODER_LIB_TITLE
;
1539 | CAPF_SBR_CONCEALMENT