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