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 /*!
85 \file
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()].
91
92 \sa sbrDecoder_Apply(), \ref documentationOverview
93 */
94
95 /*!
96 \page documentationOverview Overview of important information resources and source code documentation
97
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:
101
102 <h2>Programming</h2>
103
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()
107
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
114
115 */
116
117 #include "sbrdecoder.h"
118
119 #include "FDK_bitstream.h"
120
121 #include "sbrdec_freq_sca.h"
122 #include "env_extr.h"
123 #include "sbr_dec.h"
124 #include "env_dec.h"
125 #include "sbr_crc.h"
126 #include "sbr_ram.h"
127 #include "sbr_rom.h"
128 #include "lpp_tran.h"
129 #include "transcendent.h"
130
131
132 #include "sbrdec_drc.h"
133
134 #include "psbitdec.h"
135
136
137 /* Decoder library info */
138 #define SBRDECODER_LIB_VL0 2
139 #define SBRDECODER_LIB_VL1 2
140 #define SBRDECODER_LIB_VL2 6
141 #define SBRDECODER_LIB_TITLE "SBR Decoder"
142 #ifdef __ANDROID__
143 #define SBRDECODER_LIB_BUILD_DATE ""
144 #define SBRDECODER_LIB_BUILD_TIME ""
145 #else
146 #define SBRDECODER_LIB_BUILD_DATE __DATE__
147 #define SBRDECODER_LIB_BUILD_TIME __TIME__
148 #endif
149
150
151
152
getHeaderSlot(UCHAR currentSlot,UCHAR hdrSlotUsage[(1)+1])153 static UCHAR getHeaderSlot( UCHAR currentSlot, UCHAR hdrSlotUsage[(1)+1] )
154 {
155 UINT occupied = 0;
156 int s;
157 UCHAR slot = hdrSlotUsage[currentSlot];
158
159 FDK_ASSERT((1)+1 < 32);
160
161 for (s = 0; s < (1)+1; s++) {
162 if ( (hdrSlotUsage[s] == slot)
163 && (s != slot) ) {
164 occupied = 1;
165 break;
166 }
167 }
168
169 if (occupied) {
170 occupied = 0;
171
172 for (s = 0; s < (1)+1; s++) {
173 occupied |= 1 << hdrSlotUsage[s];
174 }
175 for (s = 0; s < (1)+1; s++) {
176 if ( !(occupied & 0x1) ) {
177 slot = s;
178 break;
179 }
180 occupied >>= 1;
181 }
182 }
183
184 return slot;
185 }
186
copySbrHeader(HANDLE_SBR_HEADER_DATA hDst,const HANDLE_SBR_HEADER_DATA hSrc)187 static void copySbrHeader( HANDLE_SBR_HEADER_DATA hDst, const HANDLE_SBR_HEADER_DATA hSrc )
188 {
189 /* copy the whole header memory (including pointers) */
190 FDKmemcpy( hDst, hSrc, sizeof(SBR_HEADER_DATA) );
191
192 /* update pointers */
193 hDst->freqBandData.freqBandTable[0] = hDst->freqBandData.freqBandTableLo;
194 hDst->freqBandData.freqBandTable[1] = hDst->freqBandData.freqBandTableHi;
195 }
196
197
198 /*!
199 \brief Reset SBR decoder.
200
201 Reset should only be called if SBR has been sucessfully detected by
202 an appropriate checkForPayload() function.
203
204 \return Error code.
205 */
206 static
sbrDecoder_ResetElement(HANDLE_SBRDECODER self,int sampleRateIn,int sampleRateOut,int samplesPerFrame,const MP4_ELEMENT_ID elementID,const int elementIndex,const int overlap)207 SBR_ERROR sbrDecoder_ResetElement (
208 HANDLE_SBRDECODER self,
209 int sampleRateIn,
210 int sampleRateOut,
211 int samplesPerFrame,
212 const MP4_ELEMENT_ID elementID,
213 const int elementIndex,
214 const int overlap
215 )
216 {
217 SBR_ERROR sbrError = SBRDEC_OK;
218 HANDLE_SBR_HEADER_DATA hSbrHeader;
219 UINT qmfFlags = 0;
220
221 int i, synDownsampleFac;
222
223 /* Check in/out samplerates */
224 if ( sampleRateIn < 6400
225 || sampleRateIn > 48000
226 )
227 {
228 sbrError = SBRDEC_UNSUPPORTED_CONFIG;
229 goto bail;
230 }
231
232 if ( sampleRateOut > 96000 )
233 {
234 sbrError = SBRDEC_UNSUPPORTED_CONFIG;
235 goto bail;
236 }
237
238 /* Set QMF mode flags */
239 if (self->flags & SBRDEC_LOW_POWER)
240 qmfFlags |= QMF_FLAG_LP;
241
242 if (self->coreCodec == AOT_ER_AAC_ELD) {
243 if (self->flags & SBRDEC_LD_MPS_QMF) {
244 qmfFlags |= QMF_FLAG_MPSLDFB;
245 } else {
246 qmfFlags |= QMF_FLAG_CLDFB;
247 }
248 }
249
250 /* Set downsampling factor for synthesis filter bank */
251 if (sampleRateOut == 0)
252 {
253 /* no single rate mode */
254 sampleRateOut = sampleRateIn<<1; /* In case of implicit signalling, assume dual rate SBR */
255 }
256
257 if ( sampleRateIn == sampleRateOut ) {
258 synDownsampleFac = 2;
259 self->flags |= SBRDEC_DOWNSAMPLE;
260 } else {
261 synDownsampleFac = 1;
262 self->flags &= ~SBRDEC_DOWNSAMPLE;
263 }
264
265 self->synDownsampleFac = synDownsampleFac;
266 self->sampleRateOut = sampleRateOut;
267
268 {
269 int i;
270
271 for (i = 0; i < (1)+1; i++)
272 {
273 hSbrHeader = &(self->sbrHeader[elementIndex][i]);
274
275 /* init a default header such that we can at least do upsampling later */
276 sbrError = initHeaderData(
277 hSbrHeader,
278 sampleRateIn,
279 sampleRateOut,
280 samplesPerFrame,
281 self->flags
282 );
283 }
284 }
285
286 if (sbrError != SBRDEC_OK) {
287 goto bail;
288 }
289
290 /* Init SBR channels going to be assigned to a SBR element */
291 {
292 int ch;
293
294 for (ch=0; ch<self->pSbrElement[elementIndex]->nChannels; ch++)
295 {
296 /* and create sbrDec */
297 sbrError = createSbrDec (self->pSbrElement[elementIndex]->pSbrChannel[ch],
298 hSbrHeader,
299 &self->pSbrElement[elementIndex]->transposerSettings,
300 synDownsampleFac,
301 qmfFlags,
302 self->flags,
303 overlap,
304 ch );
305
306 if (sbrError != SBRDEC_OK) {
307 goto bail;
308 }
309 }
310 }
311
312 //FDKmemclear(sbr_OverlapBuffer, sizeof(sbr_OverlapBuffer));
313
314 if (self->numSbrElements == 1) {
315 switch ( self->coreCodec ) {
316 case AOT_AAC_LC:
317 case AOT_SBR:
318 case AOT_PS:
319 case AOT_ER_AAC_SCAL:
320 case AOT_DRM_AAC:
321 case AOT_DRM_SURROUND:
322 if (CreatePsDec ( &self->hParametricStereoDec, samplesPerFrame )) {
323 sbrError = SBRDEC_CREATE_ERROR;
324 goto bail;
325 }
326 break;
327 default:
328 break;
329 }
330 }
331
332 /* Init frame delay slot handling */
333 self->pSbrElement[elementIndex]->useFrameSlot = 0;
334 for (i = 0; i < ((1)+1); i++) {
335 self->pSbrElement[elementIndex]->useHeaderSlot[i] = i;
336 }
337
338 bail:
339
340 return sbrError;
341 }
342
343
sbrDecoder_Open(HANDLE_SBRDECODER * pSelf)344 SBR_ERROR sbrDecoder_Open ( HANDLE_SBRDECODER * pSelf )
345 {
346 HANDLE_SBRDECODER self = NULL;
347 SBR_ERROR sbrError = SBRDEC_OK;
348
349 /* Get memory for this instance */
350 self = GetRam_SbrDecoder();
351 if (self == NULL) {
352 sbrError = SBRDEC_MEM_ALLOC_FAILED;
353 goto bail;
354 }
355
356 self->workBuffer1 = GetRam_SbrDecWorkBuffer1();
357 self->workBuffer2 = GetRam_SbrDecWorkBuffer2();
358
359 if ( self->workBuffer1 == NULL
360 || self->workBuffer2 == NULL )
361 {
362 sbrError = SBRDEC_MEM_ALLOC_FAILED;
363 goto bail;
364 }
365
366 /*
367 Already zero because of calloc
368 self->numSbrElements = 0;
369 self->numSbrChannels = 0;
370 self->codecFrameSize = 0;
371 */
372
373 self->numDelayFrames = (1); /* set to the max value by default */
374
375 *pSelf = self;
376
377 bail:
378 return sbrError;
379 }
380
381 /**
382 * \brief determine if the given core codec AOT can be processed or not.
383 * \param coreCodec core codec audio object type.
384 * \return 1 if SBR can be processed, 0 if SBR cannot be processed/applied.
385 */
386 static
sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec)387 int sbrDecoder_isCoreCodecValid(AUDIO_OBJECT_TYPE coreCodec)
388 {
389 switch (coreCodec) {
390 case AOT_AAC_LC:
391 case AOT_SBR:
392 case AOT_PS:
393 case AOT_ER_AAC_SCAL:
394 case AOT_ER_AAC_ELD:
395 return 1;
396 default:
397 return 0;
398 }
399 }
400
401 static
sbrDecoder_DestroyElement(HANDLE_SBRDECODER self,const int elementIndex)402 void sbrDecoder_DestroyElement (
403 HANDLE_SBRDECODER self,
404 const int elementIndex
405 )
406 {
407 if (self->pSbrElement[elementIndex] != NULL) {
408 int ch;
409
410 for (ch=0; ch<SBRDEC_MAX_CH_PER_ELEMENT; ch++) {
411 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) {
412 deleteSbrDec( self->pSbrElement[elementIndex]->pSbrChannel[ch] );
413 FreeRam_SbrDecChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch] );
414 self->numSbrChannels -= 1;
415 }
416 }
417 FreeRam_SbrDecElement( &self->pSbrElement[elementIndex] );
418 self->numSbrElements -= 1;
419 }
420 }
421
422
sbrDecoder_InitElement(HANDLE_SBRDECODER self,const int sampleRateIn,const int sampleRateOut,const int samplesPerFrame,const AUDIO_OBJECT_TYPE coreCodec,const MP4_ELEMENT_ID elementID,const int elementIndex)423 SBR_ERROR sbrDecoder_InitElement (
424 HANDLE_SBRDECODER self,
425 const int sampleRateIn,
426 const int sampleRateOut,
427 const int samplesPerFrame,
428 const AUDIO_OBJECT_TYPE coreCodec,
429 const MP4_ELEMENT_ID elementID,
430 const int elementIndex
431 )
432 {
433 SBR_ERROR sbrError = SBRDEC_OK;
434 int chCnt=0;
435 int nSbrElementsStart = self->numSbrElements;
436
437 /* Check core codec AOT */
438 if (! sbrDecoder_isCoreCodecValid(coreCodec) || elementIndex >= (8)) {
439 sbrError = SBRDEC_UNSUPPORTED_CONFIG;
440 goto bail;
441 }
442
443 if ( elementID != ID_SCE && elementID != ID_CPE && elementID != ID_LFE )
444 {
445 sbrError = SBRDEC_UNSUPPORTED_CONFIG;
446 goto bail;
447 }
448
449 if ( self->sampleRateIn == sampleRateIn
450 && self->codecFrameSize == samplesPerFrame
451 && self->coreCodec == coreCodec
452 && self->pSbrElement[elementIndex] != NULL
453 && self->pSbrElement[elementIndex]->elementID == elementID
454 && !(self->flags & SBRDEC_FORCE_RESET)
455 )
456 {
457 /* Nothing to do */
458 return SBRDEC_OK;
459 }
460
461 self->sampleRateIn = sampleRateIn;
462 self->codecFrameSize = samplesPerFrame;
463 self->coreCodec = coreCodec;
464
465 self->flags = 0;
466 self->flags |= (coreCodec == AOT_ER_AAC_ELD) ? SBRDEC_ELD_GRID : 0;
467
468 /* Init SBR elements */
469 {
470 int elChannels, ch;
471
472 if (self->pSbrElement[elementIndex] == NULL) {
473 self->pSbrElement[elementIndex] = GetRam_SbrDecElement(elementIndex);
474 if (self->pSbrElement[elementIndex] == NULL) {
475 sbrError = SBRDEC_MEM_ALLOC_FAILED;
476 goto bail;
477 }
478 self->numSbrElements ++;
479 } else {
480 self->numSbrChannels -= self->pSbrElement[elementIndex]->nChannels;
481 }
482
483 /* Save element ID for sanity checks and to have a fallback for concealment. */
484 self->pSbrElement[elementIndex]->elementID = elementID;
485
486 /* Determine amount of channels for this element */
487 switch (elementID) {
488 case ID_NONE:
489 case ID_CPE: elChannels=2;
490 break;
491 case ID_LFE:
492 case ID_SCE: elChannels=1;
493 break;
494 default: elChannels=0;
495 break;
496 }
497
498 /* Handle case of Parametric Stereo */
499 if ( elementIndex == 0 && elementID == ID_SCE ) {
500 switch (coreCodec) {
501 case AOT_AAC_LC:
502 case AOT_SBR:
503 case AOT_PS:
504 case AOT_ER_AAC_SCAL:
505 case AOT_DRM_AAC:
506 case AOT_DRM_SURROUND:
507 elChannels = 2;
508 break;
509 default:
510 break;
511 }
512 }
513
514 self->pSbrElement[elementIndex]->nChannels = elChannels;
515
516 for (ch=0; ch<elChannels; ch++)
517 {
518 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
519 self->pSbrElement[elementIndex]->pSbrChannel[ch] = GetRam_SbrDecChannel(chCnt);
520 if (self->pSbrElement[elementIndex]->pSbrChannel[ch] == NULL) {
521 sbrError = SBRDEC_MEM_ALLOC_FAILED;
522 goto bail;
523 }
524 }
525 self->numSbrChannels ++;
526
527 sbrDecoder_drcInitChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.sbrDrcChannel );
528
529 /* Add reference pointer to workbuffers. */
530 self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.WorkBuffer1 = self->workBuffer1;
531 self->pSbrElement[elementIndex]->pSbrChannel[ch]->SbrDec.WorkBuffer2 = self->workBuffer2;
532 chCnt++;
533 }
534 if (elChannels == 1 && self->pSbrElement[elementIndex]->pSbrChannel[ch] != NULL) {
535 deleteSbrDec( self->pSbrElement[elementIndex]->pSbrChannel[ch] );
536 FreeRam_SbrDecChannel( &self->pSbrElement[elementIndex]->pSbrChannel[ch] );
537 }
538 }
539
540 /* clear error flags for all delay slots */
541 FDKmemclear(self->pSbrElement[elementIndex]->frameErrorFlag, ((1)+1)*sizeof(UCHAR));
542
543 /* Initialize this instance */
544 sbrError = sbrDecoder_ResetElement(
545 self,
546 sampleRateIn,
547 sampleRateOut,
548 samplesPerFrame,
549 elementID,
550 elementIndex,
551 (coreCodec == AOT_ER_AAC_ELD) ? 0 : (6)
552 );
553
554
555
556 bail:
557 if (sbrError != SBRDEC_OK) {
558 if (nSbrElementsStart < self->numSbrElements) {
559 /* Free the memory allocated for this element */
560 sbrDecoder_DestroyElement( self, elementIndex );
561 } else if ( (self->pSbrElement[elementIndex] != NULL)
562 && (elementIndex < (8)))
563 { /* Set error flag to trigger concealment */
564 self->pSbrElement[elementIndex]->frameErrorFlag[self->pSbrElement[elementIndex]->useFrameSlot] = 1;
565 }
566 }
567
568 return sbrError;
569 }
570
571 /**
572 * \brief Apply decoded SBR header for one element.
573 * \param self SBR decoder instance handle
574 * \param hSbrHeader SBR header handle to be processed.
575 * \param hSbrChannel pointer array to the SBR element channels corresponding to the SBR header.
576 * \param headerStatus header status value returned from SBR header parser.
577 * \param numElementChannels amount of channels for the SBR element whos header is to be processed.
578 */
579 static
sbrDecoder_HeaderUpdate(HANDLE_SBRDECODER self,HANDLE_SBR_HEADER_DATA hSbrHeader,SBR_HEADER_STATUS headerStatus,HANDLE_SBR_CHANNEL hSbrChannel[],const int numElementChannels)580 SBR_ERROR sbrDecoder_HeaderUpdate(
581 HANDLE_SBRDECODER self,
582 HANDLE_SBR_HEADER_DATA hSbrHeader,
583 SBR_HEADER_STATUS headerStatus,
584 HANDLE_SBR_CHANNEL hSbrChannel[],
585 const int numElementChannels
586 )
587 {
588 SBR_ERROR errorStatus = SBRDEC_OK;
589
590 /*
591 change of control data, reset decoder
592 */
593 errorStatus = resetFreqBandTables(hSbrHeader, self->flags);
594
595 if (errorStatus == SBRDEC_OK) {
596 if (hSbrHeader->syncState == UPSAMPLING && headerStatus != HEADER_RESET)
597 {
598 /* As the default header would limit the frequency range,
599 lowSubband and highSubband must be patched. */
600 hSbrHeader->freqBandData.lowSubband = hSbrHeader->numberOfAnalysisBands;
601 hSbrHeader->freqBandData.highSubband = hSbrHeader->numberOfAnalysisBands;
602 }
603
604 /* Trigger a reset before processing this slot */
605 hSbrHeader->status |= SBRDEC_HDR_STAT_RESET;
606 }
607
608 return errorStatus;
609 }
610
sbrDecoder_Header(HANDLE_SBRDECODER self,HANDLE_FDK_BITSTREAM hBs,const INT sampleRateIn,const INT sampleRateOut,const INT samplesPerFrame,const AUDIO_OBJECT_TYPE coreCodec,const MP4_ELEMENT_ID elementID,const INT elementIndex)611 INT sbrDecoder_Header (
612 HANDLE_SBRDECODER self,
613 HANDLE_FDK_BITSTREAM hBs,
614 const INT sampleRateIn,
615 const INT sampleRateOut,
616 const INT samplesPerFrame,
617 const AUDIO_OBJECT_TYPE coreCodec,
618 const MP4_ELEMENT_ID elementID,
619 const INT elementIndex
620 )
621 {
622 SBR_HEADER_STATUS headerStatus;
623 HANDLE_SBR_HEADER_DATA hSbrHeader;
624 SBR_ERROR sbrError = SBRDEC_OK;
625 int headerIndex;
626
627 if ( self == NULL || elementIndex > (8) )
628 {
629 return SBRDEC_UNSUPPORTED_CONFIG;
630 }
631
632 if (! sbrDecoder_isCoreCodecValid(coreCodec)) {
633 return SBRDEC_UNSUPPORTED_CONFIG;
634 }
635
636 sbrError = sbrDecoder_InitElement(
637 self,
638 sampleRateIn,
639 sampleRateOut,
640 samplesPerFrame,
641 coreCodec,
642 elementID,
643 elementIndex
644 );
645
646 if (sbrError != SBRDEC_OK) {
647 goto bail;
648 }
649
650 headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
651 self->pSbrElement[elementIndex]->useHeaderSlot);
652 hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]);
653
654 headerStatus = sbrGetHeaderData ( hSbrHeader,
655 hBs,
656 self->flags,
657 0);
658
659
660 {
661 SBR_DECODER_ELEMENT *pSbrElement;
662
663 pSbrElement = self->pSbrElement[elementIndex];
664
665 /* Sanity check */
666 if (pSbrElement != NULL) {
667 if ( (elementID == ID_CPE && pSbrElement->nChannels != 2)
668 || (elementID != ID_CPE && pSbrElement->nChannels != 1) )
669 {
670 return SBRDEC_UNSUPPORTED_CONFIG;
671 }
672 if ( headerStatus == HEADER_RESET ) {
673
674 sbrError = sbrDecoder_HeaderUpdate(
675 self,
676 hSbrHeader,
677 headerStatus,
678 pSbrElement->pSbrChannel,
679 pSbrElement->nChannels
680 );
681
682 if (sbrError == SBRDEC_OK) {
683 hSbrHeader->syncState = SBR_HEADER;
684 hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
685 }
686 /* else {
687 Since we already have overwritten the old SBR header the only way out is UPSAMPLING!
688 This will be prepared in the next step.
689 } */
690 }
691 }
692 }
693 bail:
694 return sbrError;
695 }
696
697
sbrDecoder_SetParam(HANDLE_SBRDECODER self,const SBRDEC_PARAM param,const INT value)698 SBR_ERROR sbrDecoder_SetParam (HANDLE_SBRDECODER self,
699 const SBRDEC_PARAM param,
700 const INT value )
701 {
702 SBR_ERROR errorStatus = SBRDEC_OK;
703
704 /* configure the subsystems */
705 switch (param)
706 {
707 case SBR_SYSTEM_BITSTREAM_DELAY:
708 if (value < 0 || value > (1)) {
709 errorStatus = SBRDEC_SET_PARAM_FAIL;
710 break;
711 }
712 if (self == NULL) {
713 errorStatus = SBRDEC_NOT_INITIALIZED;
714 } else {
715 self->numDelayFrames = (UCHAR)value;
716 }
717 break;
718 case SBR_QMF_MODE:
719 if (self == NULL) {
720 errorStatus = SBRDEC_NOT_INITIALIZED;
721 } else {
722 if (value == 1) {
723 self->flags |= SBRDEC_LOW_POWER;
724 } else {
725 self->flags &= ~SBRDEC_LOW_POWER;
726 }
727 }
728 break;
729 case SBR_LD_QMF_TIME_ALIGN:
730 if (self == NULL) {
731 errorStatus = SBRDEC_NOT_INITIALIZED;
732 } else {
733 if (value == 1) {
734 self->flags |= SBRDEC_LD_MPS_QMF;
735 } else {
736 self->flags &= ~SBRDEC_LD_MPS_QMF;
737 }
738 }
739 break;
740 case SBR_FLUSH_DATA:
741 if (value != 0) {
742 if (self == NULL) {
743 errorStatus = SBRDEC_NOT_INITIALIZED;
744 } else {
745 self->flags |= SBRDEC_FLUSH;
746 }
747 }
748 break;
749 case SBR_CLEAR_HISTORY:
750 if (value != 0) {
751 if (self == NULL) {
752 errorStatus = SBRDEC_NOT_INITIALIZED;
753 } else {
754 self->flags |= SBRDEC_FORCE_RESET;
755 }
756 }
757 break;
758 case SBR_BS_INTERRUPTION:
759 {
760 int elementIndex;
761
762 if (self == NULL) {
763 errorStatus = SBRDEC_NOT_INITIALIZED;
764 break;
765 }
766
767 /* Loop over SBR elements */
768 for (elementIndex = 0; elementIndex < self->numSbrElements; elementIndex++) {
769 if (self->pSbrElement[elementIndex] != NULL)
770 {
771 HANDLE_SBR_HEADER_DATA hSbrHeader;
772 int headerIndex = getHeaderSlot(self->pSbrElement[elementIndex]->useFrameSlot,
773 self->pSbrElement[elementIndex]->useHeaderSlot);
774
775 hSbrHeader = &(self->sbrHeader[elementIndex][headerIndex]);
776
777 /* Set sync state UPSAMPLING for the corresponding slot.
778 This switches off bitstream parsing until a new header arrives. */
779 hSbrHeader->syncState = UPSAMPLING;
780 hSbrHeader->status |= SBRDEC_HDR_STAT_UPDATE;
781 } }
782 }
783 break;
784 default:
785 errorStatus = SBRDEC_SET_PARAM_FAIL;
786 break;
787 } /* switch(param) */
788
789 return (errorStatus);
790 }
791
792 static
sbrDecoder_drcGetChannel(const HANDLE_SBRDECODER self,const INT channel)793 SBRDEC_DRC_CHANNEL * sbrDecoder_drcGetChannel( const HANDLE_SBRDECODER self, const INT channel )
794 {
795 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
796 int elementIndex, elChanIdx=0, numCh=0;
797
798 for (elementIndex = 0; (elementIndex < (8)) && (numCh <= channel); elementIndex++)
799 {
800 SBR_DECODER_ELEMENT *pSbrElement = self->pSbrElement[elementIndex];
801 int c, elChannels;
802
803 elChanIdx = 0;
804 if (pSbrElement == NULL) break;
805
806 /* Determine amount of channels for this element */
807 switch (pSbrElement->elementID) {
808 case ID_CPE: elChannels = 2;
809 break;
810 case ID_LFE:
811 case ID_SCE: elChannels = 1;
812 break;
813 case ID_NONE:
814 default: elChannels = 0;
815 break;
816 }
817
818 /* Limit with actual allocated element channels */
819 elChannels = FDKmin(elChannels, pSbrElement->nChannels);
820
821 for (c = 0; (c < elChannels) && (numCh <= channel); c++) {
822 if (pSbrElement->pSbrChannel[elChanIdx] != NULL) {
823 numCh++;
824 elChanIdx++;
825 }
826 }
827 }
828 elementIndex -= 1;
829 elChanIdx -= 1;
830
831 if (elChanIdx < 0 || elementIndex < 0) {
832 return NULL;
833 }
834
835 if ( self->pSbrElement[elementIndex] != NULL ) {
836 if ( self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx] != NULL )
837 {
838 pSbrDrcChannelData = &self->pSbrElement[elementIndex]->pSbrChannel[elChanIdx]->SbrDec.sbrDrcChannel;
839 }
840 }
841
842 return (pSbrDrcChannelData);
843 }
844
sbrDecoder_drcFeedChannel(HANDLE_SBRDECODER self,INT ch,UINT numBands,FIXP_DBL * pNextFact_mag,INT nextFact_exp,SHORT drcInterpolationScheme,UCHAR winSequence,USHORT * pBandTop)845 SBR_ERROR sbrDecoder_drcFeedChannel ( HANDLE_SBRDECODER self,
846 INT ch,
847 UINT numBands,
848 FIXP_DBL *pNextFact_mag,
849 INT nextFact_exp,
850 SHORT drcInterpolationScheme,
851 UCHAR winSequence,
852 USHORT *pBandTop )
853 {
854 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
855 int band, isValidData = 0;
856
857 if (self == NULL) {
858 return SBRDEC_NOT_INITIALIZED;
859 }
860 if (ch > (8) || pNextFact_mag == NULL) {
861 return SBRDEC_SET_PARAM_FAIL;
862 }
863
864 /* Search for gain values different to 1.0f */
865 for (band = 0; band < numBands; band += 1) {
866 if ( !((pNextFact_mag[band] == FL2FXCONST_DBL(0.5)) && (nextFact_exp == 1))
867 && !((pNextFact_mag[band] == (FIXP_DBL)MAXVAL_DBL) && (nextFact_exp == 0)) ) {
868 isValidData = 1;
869 break;
870 }
871 }
872
873 /* Find the right SBR channel */
874 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch );
875
876 if ( pSbrDrcChannelData != NULL ) {
877 if ( pSbrDrcChannelData->enable || isValidData )
878 { /* Activate processing only with real and valid data */
879 int i;
880
881 pSbrDrcChannelData->enable = 1;
882 pSbrDrcChannelData->numBandsNext = numBands;
883
884 pSbrDrcChannelData->winSequenceNext = winSequence;
885 pSbrDrcChannelData->drcInterpolationSchemeNext = drcInterpolationScheme;
886 pSbrDrcChannelData->nextFact_exp = nextFact_exp;
887
888 for (i = 0; i < (int)numBands; i++) {
889 pSbrDrcChannelData->bandTopNext[i] = pBandTop[i];
890 pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i];
891 }
892 }
893 }
894
895 return SBRDEC_OK;
896 }
897
898
sbrDecoder_drcDisable(HANDLE_SBRDECODER self,INT ch)899 void sbrDecoder_drcDisable ( HANDLE_SBRDECODER self,
900 INT ch )
901 {
902 SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
903
904 if ( (self == NULL)
905 || (ch > (8))
906 || (self->numSbrElements == 0)
907 || (self->numSbrChannels == 0) ) {
908 return;
909 }
910
911 /* Find the right SBR channel */
912 pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch );
913
914 if ( pSbrDrcChannelData != NULL ) {
915 sbrDecoder_drcInitChannel( pSbrDrcChannelData );
916 }
917 }
918
919
920
sbrDecoder_Parse(HANDLE_SBRDECODER self,HANDLE_FDK_BITSTREAM hBs,int * count,int bsPayLen,int crcFlag,MP4_ELEMENT_ID prevElement,int elementIndex,int fGlobalIndependencyFlag)921 SBR_ERROR sbrDecoder_Parse(
922 HANDLE_SBRDECODER self,
923 HANDLE_FDK_BITSTREAM hBs,
924 int *count,
925 int bsPayLen,
926 int crcFlag,
927 MP4_ELEMENT_ID prevElement,
928 int elementIndex,
929 int fGlobalIndependencyFlag
930 )
931 {
932 SBR_DECODER_ELEMENT *hSbrElement;
933 HANDLE_SBR_HEADER_DATA hSbrHeader;
934 HANDLE_SBR_CHANNEL *pSbrChannel;
935
936 SBR_FRAME_DATA *hFrameDataLeft;
937 SBR_FRAME_DATA *hFrameDataRight;
938
939 SBR_ERROR errorStatus = SBRDEC_OK;
940 SBR_SYNC_STATE initialSyncState;
941 SBR_HEADER_STATUS headerStatus = HEADER_NOT_PRESENT;
942
943 INT startPos;
944 INT CRCLen = 0;
945
946 int stereo;
947 int fDoDecodeSbrData = 1;
948
949 int lastSlot, lastHdrSlot = 0, thisHdrSlot;
950
951 /* Remember start position of SBR element */
952 startPos = FDKgetValidBits(hBs);
953
954 /* SBR sanity checks */
955 if ( self == NULL || self->pSbrElement[elementIndex] == NULL ) {
956 errorStatus = SBRDEC_NOT_INITIALIZED;
957 goto bail;
958 }
959
960 hSbrElement = self->pSbrElement[elementIndex];
961
962 lastSlot = (hSbrElement->useFrameSlot > 0) ? hSbrElement->useFrameSlot-1 : self->numDelayFrames;
963 lastHdrSlot = hSbrElement->useHeaderSlot[lastSlot];
964 thisHdrSlot = getHeaderSlot( hSbrElement->useFrameSlot, hSbrElement->useHeaderSlot ); /* Get a free header slot not used by frames not processed yet. */
965
966 /* Assign the free slot to store a new header if there is one. */
967 hSbrHeader = &self->sbrHeader[elementIndex][thisHdrSlot];
968
969 pSbrChannel = hSbrElement->pSbrChannel;
970 stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
971
972 hFrameDataLeft = &self->pSbrElement[elementIndex]->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
973 hFrameDataRight = &self->pSbrElement[elementIndex]->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
974
975 initialSyncState = hSbrHeader->syncState;
976
977 /* reset PS flag; will be set after PS was found */
978 self->flags &= ~SBRDEC_PS_DECODED;
979
980 if (hSbrHeader->status & SBRDEC_HDR_STAT_UPDATE) {
981 /* Got a new header from extern (e.g. from an ASC) */
982 headerStatus = HEADER_OK;
983 hSbrHeader->status &= ~SBRDEC_HDR_STAT_UPDATE;
984 }
985 else if (thisHdrSlot != lastHdrSlot) {
986 /* Copy the last header into this slot otherwise the
987 header compare will trigger more HEADER_RESETs than needed. */
988 copySbrHeader( hSbrHeader, &self->sbrHeader[elementIndex][lastHdrSlot] );
989 }
990
991 /*
992 Check if bit stream data is valid and matches the element context
993 */
994 if ( ((prevElement != ID_SCE) && (prevElement != ID_CPE)) || prevElement != hSbrElement->elementID) {
995 /* In case of LFE we also land here, since there is no LFE SBR element (do upsampling only) */
996 fDoDecodeSbrData = 0;
997 }
998
999 if (fDoDecodeSbrData)
1000 {
1001 if ((INT)FDKgetValidBits(hBs) <= 0) {
1002 fDoDecodeSbrData = 0;
1003 }
1004 }
1005
1006 /*
1007 SBR CRC-check
1008 */
1009 if (fDoDecodeSbrData)
1010 {
1011 if (crcFlag == 1) {
1012 switch (self->coreCodec) {
1013 case AOT_ER_AAC_ELD:
1014 FDKpushFor (hBs, 10);
1015 /* check sbrcrc later: we don't know the payload length now */
1016 break;
1017 default:
1018 CRCLen = bsPayLen - 10; /* change: 0 => i */
1019 if (CRCLen < 0) {
1020 fDoDecodeSbrData = 0;
1021 } else {
1022 fDoDecodeSbrData = SbrCrcCheck (hBs, CRCLen);
1023 }
1024 break;
1025 }
1026 }
1027 } /* if (fDoDecodeSbrData) */
1028
1029 /*
1030 Read in the header data and issue a reset if change occured
1031 */
1032 if (fDoDecodeSbrData)
1033 {
1034 int sbrHeaderPresent;
1035
1036 {
1037 sbrHeaderPresent = FDKreadBit(hBs);
1038 }
1039
1040 if ( sbrHeaderPresent ) {
1041 headerStatus = sbrGetHeaderData (hSbrHeader,
1042 hBs,
1043 self->flags,
1044 1);
1045 }
1046
1047 if (headerStatus == HEADER_RESET)
1048 {
1049 errorStatus = sbrDecoder_HeaderUpdate(
1050 self,
1051 hSbrHeader,
1052 headerStatus,
1053 pSbrChannel,
1054 hSbrElement->nChannels
1055 );
1056
1057 if (errorStatus == SBRDEC_OK) {
1058 hSbrHeader->syncState = SBR_HEADER;
1059 } else {
1060 hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1061 }
1062 }
1063
1064 if (errorStatus != SBRDEC_OK) {
1065 fDoDecodeSbrData = 0;
1066 }
1067 } /* if (fDoDecodeSbrData) */
1068
1069 /*
1070 Print debugging output only if state has changed
1071 */
1072
1073 /* read frame data */
1074 if ((hSbrHeader->syncState >= SBR_HEADER) && fDoDecodeSbrData) {
1075 int sbrFrameOk;
1076 /* read the SBR element data */
1077 if (stereo) {
1078 sbrFrameOk = sbrGetChannelPairElement(hSbrHeader,
1079 hFrameDataLeft,
1080 hFrameDataRight,
1081 hBs,
1082 self->flags,
1083 self->pSbrElement[elementIndex]->transposerSettings.overlap);
1084 }
1085 else {
1086 if (self->hParametricStereoDec != NULL) {
1087 /* update slot index for PS bitstream parsing */
1088 self->hParametricStereoDec->bsLastSlot = self->hParametricStereoDec->bsReadSlot;
1089 self->hParametricStereoDec->bsReadSlot = hSbrElement->useFrameSlot;
1090 }
1091 sbrFrameOk = sbrGetSingleChannelElement(hSbrHeader,
1092 hFrameDataLeft,
1093 hBs,
1094 self->hParametricStereoDec,
1095 self->flags,
1096 self->pSbrElement[elementIndex]->transposerSettings.overlap);
1097 }
1098 if (!sbrFrameOk) {
1099 fDoDecodeSbrData = 0;
1100 }
1101 else {
1102 INT valBits;
1103
1104 if (bsPayLen > 0) {
1105 valBits = bsPayLen - ((INT)startPos - (INT)FDKgetValidBits(hBs));
1106 } else {
1107 valBits = (INT)FDKgetValidBits(hBs);
1108 }
1109
1110 if ( crcFlag == 1 ) {
1111 switch (self->coreCodec) {
1112 case AOT_ER_AAC_ELD:
1113 {
1114 /* late crc check for eld */
1115 INT payloadbits = (INT)startPos - (INT)FDKgetValidBits(hBs) - startPos;
1116 INT crcLen = payloadbits - 10;
1117 FDKpushBack(hBs, payloadbits);
1118 fDoDecodeSbrData = SbrCrcCheck (hBs, crcLen);
1119 FDKpushFor(hBs, crcLen);
1120 }
1121 break;
1122 default:
1123 break;
1124 }
1125 }
1126
1127 /* sanity check of remaining bits */
1128 if (valBits < 0) {
1129 fDoDecodeSbrData = 0;
1130 } else {
1131 switch (self->coreCodec) {
1132 case AOT_SBR:
1133 case AOT_PS:
1134 case AOT_AAC_LC:
1135 {
1136 /* This sanity check is only meaningful with General Audio bitstreams */
1137 int alignBits = valBits & 0x7;
1138
1139 if (valBits > alignBits) {
1140 fDoDecodeSbrData = 0;
1141 }
1142 }
1143 break;
1144 default:
1145 /* No sanity check available */
1146 break;
1147 }
1148 }
1149 }
1150 } else {
1151 /* The returned bit count will not be the actual payload size since we did not
1152 parse the frame data. Return an error so that the caller can react respectively. */
1153 errorStatus = SBRDEC_PARSE_ERROR;
1154 }
1155
1156 if (!fDoDecodeSbrData) {
1157 /* Set error flag for this slot to trigger concealment */
1158 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 1;
1159 errorStatus = SBRDEC_PARSE_ERROR;
1160 } else {
1161 /* Everything seems to be ok so clear the error flag */
1162 self->pSbrElement[elementIndex]->frameErrorFlag[hSbrElement->useFrameSlot] = 0;
1163 }
1164
1165 if (!stereo) {
1166 /* Turn coupling off explicitely to avoid access to absent right frame data
1167 that might occur with corrupt bitstreams. */
1168 hFrameDataLeft->coupling = COUPLING_OFF;
1169 }
1170
1171 bail:
1172 if (errorStatus == SBRDEC_OK) {
1173 if (headerStatus == HEADER_NOT_PRESENT) {
1174 /* Use the old header for this frame */
1175 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = lastHdrSlot;
1176 } else {
1177 /* Use the new header for this frame */
1178 hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot] = thisHdrSlot;
1179 }
1180
1181 /* Move frame pointer to the next slot which is up to be decoded/applied next */
1182 hSbrElement->useFrameSlot = (hSbrElement->useFrameSlot+1) % (self->numDelayFrames+1);
1183 }
1184
1185 *count -= startPos - FDKgetValidBits(hBs);
1186
1187 return errorStatus;
1188 }
1189
1190
1191 /**
1192 * \brief Render one SBR element into time domain signal.
1193 * \param self SBR decoder handle
1194 * \param timeData pointer to output buffer
1195 * \param interleaved flag indicating interleaved channel output
1196 * \param channelMapping pointer to UCHAR array where next 2 channel offsets are stored.
1197 * \param elementIndex enumerating index of the SBR element to render.
1198 * \param numInChannels number of channels from core coder (reading stride).
1199 * \param numOutChannels pointer to a location to return number of output channels.
1200 * \param psPossible flag indicating if PS is possible or not.
1201 * \return SBRDEC_OK if successfull, else error code
1202 */
1203 static SBR_ERROR
sbrDecoder_DecodeElement(HANDLE_SBRDECODER self,INT_PCM * timeData,const int interleaved,const UCHAR * channelMapping,const int elementIndex,const int numInChannels,int * numOutChannels,const int psPossible)1204 sbrDecoder_DecodeElement (
1205 HANDLE_SBRDECODER self,
1206 INT_PCM *timeData,
1207 const int interleaved,
1208 const UCHAR *channelMapping,
1209 const int elementIndex,
1210 const int numInChannels,
1211 int *numOutChannels,
1212 const int psPossible
1213 )
1214 {
1215 SBR_DECODER_ELEMENT *hSbrElement = self->pSbrElement[elementIndex];
1216 HANDLE_SBR_CHANNEL *pSbrChannel = self->pSbrElement[elementIndex]->pSbrChannel;
1217 HANDLE_SBR_HEADER_DATA hSbrHeader = &self->sbrHeader[elementIndex][hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]];
1218 HANDLE_PS_DEC h_ps_d = self->hParametricStereoDec;
1219
1220 /* get memory for frame data from scratch */
1221 SBR_FRAME_DATA *hFrameDataLeft = &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
1222 SBR_FRAME_DATA *hFrameDataRight = &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
1223
1224 SBR_ERROR errorStatus = SBRDEC_OK;
1225
1226
1227 INT strideIn, strideOut, offset0, offset1;
1228 INT codecFrameSize = self->codecFrameSize;
1229
1230 int stereo = (hSbrElement->elementID == ID_CPE) ? 1 : 0;
1231 int numElementChannels = hSbrElement->nChannels; /* Number of channels of the current SBR element */
1232
1233 if (self->flags & SBRDEC_FLUSH) {
1234 /* Move frame pointer to the next slot which is up to be decoded/applied next */
1235 hSbrElement->useFrameSlot = (hSbrElement->useFrameSlot+1) % (self->numDelayFrames+1);
1236 /* Update header and frame data pointer because they have already been set */
1237 hSbrHeader = &self->sbrHeader[elementIndex][hSbrElement->useHeaderSlot[hSbrElement->useFrameSlot]];
1238 hFrameDataLeft = &hSbrElement->pSbrChannel[0]->frameData[hSbrElement->useFrameSlot];
1239 hFrameDataRight = &hSbrElement->pSbrChannel[1]->frameData[hSbrElement->useFrameSlot];
1240 }
1241
1242 /* Update the header error flag */
1243 hSbrHeader->frameErrorFlag = hSbrElement->frameErrorFlag[hSbrElement->useFrameSlot];
1244
1245 /*
1246 Prepare filterbank for upsampling if no valid bit stream data is available.
1247 */
1248 if ( hSbrHeader->syncState == SBR_NOT_INITIALIZED )
1249 {
1250 errorStatus = initHeaderData(
1251 hSbrHeader,
1252 self->sampleRateIn,
1253 self->sampleRateOut,
1254 codecFrameSize,
1255 self->flags
1256 );
1257
1258 if (errorStatus != SBRDEC_OK) {
1259 return errorStatus;
1260 }
1261
1262 hSbrHeader->syncState = UPSAMPLING;
1263
1264 errorStatus = sbrDecoder_HeaderUpdate(
1265 self,
1266 hSbrHeader,
1267 HEADER_NOT_PRESENT,
1268 pSbrChannel,
1269 hSbrElement->nChannels
1270 );
1271
1272 if (errorStatus != SBRDEC_OK) {
1273 hSbrHeader->syncState = SBR_NOT_INITIALIZED;
1274 return errorStatus;
1275 }
1276 }
1277
1278 /* reset */
1279 if (hSbrHeader->status & SBRDEC_HDR_STAT_RESET) {
1280 int ch;
1281 for (ch = 0 ; ch < numElementChannels; ch++) {
1282 SBR_ERROR errorStatusTmp = SBRDEC_OK;
1283
1284 errorStatusTmp = resetSbrDec (
1285 &pSbrChannel[ch]->SbrDec,
1286 hSbrHeader,
1287 &pSbrChannel[ch]->prevFrameData,
1288 self->flags & SBRDEC_LOW_POWER,
1289 self->synDownsampleFac
1290 );
1291
1292 if (errorStatusTmp != SBRDEC_OK) {
1293 errorStatus = errorStatusTmp;
1294 }
1295 }
1296 hSbrHeader->status &= ~SBRDEC_HDR_STAT_RESET;
1297 }
1298
1299 /* decoding */
1300 if ( (hSbrHeader->syncState == SBR_ACTIVE)
1301 || ((hSbrHeader->syncState == SBR_HEADER) && (hSbrHeader->frameErrorFlag == 0)) )
1302 {
1303 errorStatus = SBRDEC_OK;
1304
1305 decodeSbrData (hSbrHeader,
1306 hFrameDataLeft,
1307 &pSbrChannel[0]->prevFrameData,
1308 (stereo) ? hFrameDataRight : NULL,
1309 (stereo) ? &pSbrChannel[1]->prevFrameData : NULL);
1310
1311
1312 /* Now we have a full parameter set and can do parameter
1313 based concealment instead of plain upsampling. */
1314 hSbrHeader->syncState = SBR_ACTIVE;
1315 }
1316
1317 /* decode PS data if available */
1318 if (h_ps_d != NULL && psPossible) {
1319 int applyPs = 1;
1320
1321 /* define which frame delay line slot to process */
1322 h_ps_d->processSlot = hSbrElement->useFrameSlot;
1323
1324 applyPs = DecodePs(h_ps_d, hSbrHeader->frameErrorFlag);
1325 self->flags |= (applyPs) ? SBRDEC_PS_DECODED : 0;
1326 }
1327
1328 /* Set strides for reading and writing */
1329 if (interleaved) {
1330 strideIn = numInChannels;
1331 if ( psPossible )
1332 strideOut = (numInChannels < 2) ? 2 : numInChannels;
1333 else
1334 strideOut = numInChannels;
1335 offset0 = channelMapping[0];
1336 offset1 = channelMapping[1];
1337 } else {
1338 strideIn = 1;
1339 strideOut = 1;
1340 offset0 = channelMapping[0]*2*codecFrameSize;
1341 offset1 = channelMapping[1]*2*codecFrameSize;
1342 }
1343
1344 /* use same buffers for left and right channel and apply PS per timeslot */
1345 /* Process left channel */
1346 //FDKprintf("self->codecFrameSize %d\t%d\n",self->codecFrameSize,self->sampleRateIn);
1347 sbr_dec (&pSbrChannel[0]->SbrDec,
1348 timeData + offset0,
1349 timeData + offset0,
1350 &pSbrChannel[1]->SbrDec,
1351 timeData + offset1,
1352 strideIn,
1353 strideOut,
1354 hSbrHeader,
1355 hFrameDataLeft,
1356 &pSbrChannel[0]->prevFrameData,
1357 (hSbrHeader->syncState == SBR_ACTIVE),
1358 h_ps_d,
1359 self->flags
1360 );
1361
1362 if (stereo) {
1363 /* Process right channel */
1364 sbr_dec (&pSbrChannel[1]->SbrDec,
1365 timeData + offset1,
1366 timeData + offset1,
1367 NULL,
1368 NULL,
1369 strideIn,
1370 strideOut,
1371 hSbrHeader,
1372 hFrameDataRight,
1373 &pSbrChannel[1]->prevFrameData,
1374 (hSbrHeader->syncState == SBR_ACTIVE),
1375 NULL,
1376 self->flags
1377 );
1378 }
1379
1380 if (h_ps_d != NULL) {
1381 /* save PS status for next run */
1382 h_ps_d->psDecodedPrv = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0 ;
1383 }
1384
1385 if ( psPossible
1386 )
1387 {
1388 FDK_ASSERT(strideOut > 1);
1389 if ( !(self->flags & SBRDEC_PS_DECODED) ) {
1390 /* A decoder which is able to decode PS has to produce a stereo output even if no PS data is availble. */
1391 /* So copy left channel to right channel. */
1392 if (interleaved) {
1393 INT_PCM *ptr;
1394 INT i;
1395 FDK_ASSERT(strideOut == 2);
1396
1397 ptr = timeData;
1398 for (i = codecFrameSize; i--; )
1399 {
1400 INT_PCM tmp; /* This temporal variable is required because some compilers can't do *ptr++ = *ptr++ correctly. */
1401 tmp = *ptr++; *ptr++ = tmp;
1402 tmp = *ptr++; *ptr++ = tmp;
1403 }
1404 } else {
1405 FDKmemcpy( timeData+2*codecFrameSize, timeData, 2*codecFrameSize*sizeof(INT_PCM) );
1406 }
1407 }
1408 *numOutChannels = 2; /* Output minimum two channels when PS is enabled. */
1409 }
1410
1411 return errorStatus;
1412 }
1413
1414
sbrDecoder_Apply(HANDLE_SBRDECODER self,INT_PCM * timeData,int * numChannels,int * sampleRate,const UCHAR channelMapping[(8)],const int interleaved,const int coreDecodedOk,UCHAR * psDecoded)1415 SBR_ERROR sbrDecoder_Apply ( HANDLE_SBRDECODER self,
1416 INT_PCM *timeData,
1417 int *numChannels,
1418 int *sampleRate,
1419 const UCHAR channelMapping[(8)],
1420 const int interleaved,
1421 const int coreDecodedOk,
1422 UCHAR *psDecoded )
1423 {
1424 SBR_ERROR errorStatus = SBRDEC_OK;
1425
1426 int psPossible = 0;
1427 int sbrElementNum;
1428 int numCoreChannels = *numChannels;
1429 int numSbrChannels = 0;
1430
1431 psPossible = *psDecoded;
1432
1433 if (self->numSbrElements < 1) {
1434 /* exit immediately to avoid access violations */
1435 return SBRDEC_CREATE_ERROR;
1436 }
1437
1438 /* Sanity check of allocated SBR elements. */
1439 for (sbrElementNum=0; sbrElementNum<self->numSbrElements; sbrElementNum++) {
1440 if (self->pSbrElement[sbrElementNum] == NULL) {
1441 return SBRDEC_CREATE_ERROR;
1442 }
1443 }
1444
1445 if (self->numSbrElements != 1 || self->pSbrElement[0]->elementID != ID_SCE) {
1446 psPossible = 0;
1447 }
1448
1449
1450 /* In case of non-interleaved time domain data and upsampling, make room for bigger SBR output. */
1451 if (self->synDownsampleFac == 1 && interleaved == 0) {
1452 int c, outputFrameSize;
1453
1454 outputFrameSize =
1455 self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_channels
1456 * self->pSbrElement[0]->pSbrChannel[0]->SbrDec.SynthesisQMF.no_col;
1457
1458 for (c=numCoreChannels-1; c>0; c--) {
1459 FDKmemmove(timeData + c*outputFrameSize, timeData + c*self->codecFrameSize , self->codecFrameSize*sizeof(INT_PCM));
1460 }
1461 }
1462
1463
1464 /* Make sure that even if no SBR data was found/parsed *psDecoded is returned 1 if psPossible was 0. */
1465 if (psPossible == 0) {
1466 self->flags &= ~SBRDEC_PS_DECODED;
1467 }
1468
1469 /* Loop over SBR elements */
1470 for (sbrElementNum = 0; sbrElementNum<self->numSbrElements; sbrElementNum++)
1471 {
1472 int numElementChan;
1473
1474 if (psPossible && self->pSbrElement[sbrElementNum]->pSbrChannel[1] == NULL) {
1475 errorStatus = SBRDEC_UNSUPPORTED_CONFIG;
1476 goto bail;
1477 }
1478
1479 numElementChan = (self->pSbrElement[sbrElementNum]->elementID == ID_CPE) ? 2 : 1;
1480
1481 /* If core signal is bad then force upsampling */
1482 if ( ! coreDecodedOk ) {
1483 self->pSbrElement[sbrElementNum]->frameErrorFlag[self->pSbrElement[sbrElementNum]->useFrameSlot] = 1;
1484 }
1485
1486 errorStatus = sbrDecoder_DecodeElement (
1487 self,
1488 timeData,
1489 interleaved,
1490 channelMapping,
1491 sbrElementNum,
1492 numCoreChannels,
1493 &numElementChan,
1494 psPossible
1495 );
1496
1497 if (errorStatus != SBRDEC_OK) {
1498 goto bail;
1499 }
1500
1501 numSbrChannels += numElementChan;
1502 channelMapping += numElementChan;
1503
1504 if (numSbrChannels >= numCoreChannels) {
1505 break;
1506 }
1507 }
1508
1509 /* Update numChannels and samplerate */
1510 *numChannels = numSbrChannels;
1511 *sampleRate = self->sampleRateOut;
1512 *psDecoded = (self->flags & SBRDEC_PS_DECODED) ? 1 : 0;
1513
1514
1515
1516 /* Clear reset and flush flag because everything seems to be done successfully. */
1517 self->flags &= ~SBRDEC_FORCE_RESET;
1518 self->flags &= ~SBRDEC_FLUSH;
1519
1520 bail:
1521
1522 return errorStatus;
1523 }
1524
1525
sbrDecoder_Close(HANDLE_SBRDECODER * pSelf)1526 SBR_ERROR sbrDecoder_Close ( HANDLE_SBRDECODER *pSelf )
1527 {
1528 HANDLE_SBRDECODER self = *pSelf;
1529 int i;
1530
1531 if (self != NULL)
1532 {
1533 if (self->hParametricStereoDec != NULL) {
1534 DeletePsDec ( &self->hParametricStereoDec );
1535 }
1536
1537 if (self->workBuffer1 != NULL) {
1538 FreeRam_SbrDecWorkBuffer1(&self->workBuffer1);
1539 }
1540 if (self->workBuffer2 != NULL) {
1541 FreeRam_SbrDecWorkBuffer2(&self->workBuffer2);
1542 }
1543
1544 for (i = 0; i < (8); i++) {
1545 sbrDecoder_DestroyElement( self, i );
1546 }
1547
1548 FreeRam_SbrDecoder(pSelf);
1549 }
1550
1551 return SBRDEC_OK;
1552 }
1553
1554
sbrDecoder_GetLibInfo(LIB_INFO * info)1555 INT sbrDecoder_GetLibInfo( LIB_INFO *info )
1556 {
1557 int i;
1558
1559 if (info == NULL) {
1560 return -1;
1561 }
1562
1563 /* search for next free tab */
1564 for (i = 0; i < FDK_MODULE_LAST; i++) {
1565 if (info[i].module_id == FDK_NONE)
1566 break;
1567 }
1568 if (i == FDK_MODULE_LAST)
1569 return -1;
1570 info += i;
1571
1572 info->module_id = FDK_SBRDEC;
1573 info->version = LIB_VERSION(SBRDECODER_LIB_VL0, SBRDECODER_LIB_VL1, SBRDECODER_LIB_VL2);
1574 LIB_VERSION_STRING(info);
1575 info->build_date = (char *)SBRDECODER_LIB_BUILD_DATE;
1576 info->build_time = (char *)SBRDECODER_LIB_BUILD_TIME;
1577 info->title = (char *)SBRDECODER_LIB_TITLE;
1578
1579 /* Set flags */
1580 info->flags = 0
1581 | CAPF_SBR_HQ
1582 | CAPF_SBR_LP
1583 | CAPF_SBR_PS_MPEG
1584 | CAPF_SBR_CONCEALMENT
1585 | CAPF_SBR_DRC
1586 ;
1587 /* End of flags */
1588
1589 return 0;
1590 }
1591
1592
sbrDecoder_GetDelay(const HANDLE_SBRDECODER self)1593 UINT sbrDecoder_GetDelay( const HANDLE_SBRDECODER self )
1594 {
1595 UINT outputDelay = 0;
1596
1597 if ( self != NULL) {
1598 UINT flags = self->flags;
1599
1600 /* See chapter 1.6.7.2 of ISO/IEC 14496-3 for the GA-SBR figures below. */
1601
1602 /* Are we initialized? */
1603 if ( (self->numSbrChannels > 0)
1604 && (self->numSbrElements > 0) )
1605 {
1606 /* Add QMF synthesis delay */
1607 if ( (flags & SBRDEC_ELD_GRID)
1608 && IS_LOWDELAY(self->coreCodec) ) {
1609 /* Low delay SBR: */
1610 {
1611 outputDelay += (flags & SBRDEC_DOWNSAMPLE) ? 32 : 64; /* QMF synthesis */
1612 }
1613 }
1614 else if (!IS_USAC(self->coreCodec)) {
1615 /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...) branch: */
1616 outputDelay += (flags & SBRDEC_DOWNSAMPLE) ? 481 : 962;
1617 }
1618 }
1619 }
1620
1621 return (outputDelay);
1622 }
1623