1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4 © Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6
7 1. INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33
34 2. COPYRIGHT LICENSE
35
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60
61 3. NO PATENT LICENSE
62
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70
71 4. DISCLAIMER
72
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83
84 5. CONTACT INFORMATION
85
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94
95 /*********************** MPEG surround decoder library *************************
96
97 Author(s):
98
99 Description: SAC Decoder Library Interface
100
101 *******************************************************************************/
102
103 #include "sac_dec_lib.h"
104 #include "sac_dec_interface.h"
105 #include "sac_dec.h"
106 #include "sac_bitdec.h"
107 #include "FDK_matrixCalloc.h"
108
109 #define MPS_DATA_BUFFER_SIZE (2048)
110
111 /**
112 * \brief MPEG Surround data indication.
113 **/
114 typedef enum {
115 MPEGS_ANCTYPE_FRAME = 0, /*!< MPEG Surround frame, see ISO/IEC 23003-1 */
116 MPEGS_ANCTYPE_HEADER_AND_FRAME = 1, /*!< MPEG Surround header and MPEG
117 Surround frame, see ISO/IEC 23003-1 */
118 MPEGS_ANCTYPE_RESERVED_1 = 2, /*!< reserved, see ISO/IEC 23003-1 */
119 MPEGS_ANCTYPE_RESERVED_2 = 3 /*!< reserved, see ISO/IEC 23003-1*/
120 } MPEGS_ANCTYPE;
121
122 /**
123 * \brief MPEG Surround data segment indication.
124 **/
125 typedef enum {
126 MPEGS_CONTINUE = 0, /*!< Indicates if data segment continues a data block. */
127 MPEGS_STOP = 1, /*!< Indicates if data segment ends a data block. */
128 MPEGS_START = 2, /*!< Indicates if data segment begins a data block. */
129 MPEGS_START_STOP =
130 3 /*!< Indicates if data segment begins and ends a data block. */
131 } MPEGS_ANCSTARTSTOP;
132
133 /**
134 * \brief MPEG Surround synchronizaiton state.
135 *
136 * CAUTION: Changing the enumeration values can break the sync mechanism
137 *because it is based on comparing the state values.
138 **/
139 typedef enum {
140 MPEGS_SYNC_LOST =
141 0, /*!< Indicates lost sync because of current discontinuity. */
142 MPEGS_SYNC_FOUND = 1, /*!< Parsed a valid header and (re)intialization was
143 successfully completed. */
144 MPEGS_SYNC_COMPLETE = 2 /*!< In sync and continuous. Found an independent
145 frame in addition to MPEGS_SYNC_FOUND.
146 Precondition: MPEGS_SYNC_FOUND. */
147 } MPEGS_SYNCSTATE;
148
149 /**
150 * \brief MPEG Surround operation mode.
151 **/
152 typedef enum {
153 MPEGS_OPMODE_EMM = 0, /*!< Mode: Enhanced Matrix Mode (Blind) */
154 MPEGS_OPMODE_MPS_PAYLOAD = 1, /*!< Mode: Normal, Stereo or Binaural */
155 MPEGS_OPMODE_NO_MPS_PAYLOAD = 2 /*!< Mode: no MPEG Surround payload */
156 } MPEGS_OPMODE;
157
158 /**
159 * \brief MPEG Surround init flags.
160 **/
161 typedef enum {
162 MPEGS_INIT_OK = 0x00000000, /*!< indicate correct initialization */
163 MPEGS_INIT_ENFORCE_REINIT =
164 0x00000001, /*!< indicate complete initialization */
165
166 MPEGS_INIT_CHANGE_OUTPUT_MODE =
167 0x00000010, /*!< indicate change of the output mode */
168 MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX =
169 0x00000020, /*!< indicate change of low power/high quality */
170 MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE =
171 0x00000040, /*!< indicate change of qmf/time interface */
172 MPEGS_INIT_CHANGE_HEADER = 0x00000080, /*!< indicate change of header */
173
174 MPEGS_INIT_ERROR_PAYLOAD =
175 0x00000100, /*!< indicate payload/ancType/ancStartStop error */
176
177 MPEGS_INIT_BS_INTERRUPTION =
178 0x00001000, /*!< indicate bitstream interruption */
179 MPEGS_INIT_CLEAR_HISTORY =
180 0x00002000, /*!< indicate that all states shall be cleared */
181
182 /* Re-initialization of submodules */
183
184 MPEGS_INIT_CHANGE_CONCEAL_PARAMS = 0x00100000, /*!< indicate a change of at
185 least one error concealment
186 param */
187
188 /* No re-initialization needed, currently not used */
189 MPEGS_INIT_CHANGE_BYPASS_MODE =
190 0x01000000, /*!< indicate change of bypass mode */
191
192 /* Re-initialization needed, currently not used */
193 MPEGS_INIT_ERROR_ANC_TYPE = 0x10000000, /*!< indicate ancType error*/
194 MPEGS_INIT_ERROR_ANC_STARTSTOP =
195 0x20000000 /*!< indicate ancStartStop error */
196 } MPEGS_INIT_FLAGS;
197
198 struct MpegSurroundDecoder {
199 HANDLE_FDK_QMF_DOMAIN pQmfDomain;
200 UCHAR mpsData[MPS_DATA_BUFFER_SIZE]; /* Buffer for MPS payload accross more
201 than one segment */
202 INT mpsDataBits; /* Amount of bits in mpsData */
203 /* MPEG Surround decoder */
204 SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig[1]; /* SSC delay line which is
205 used during decoding */
206 spatialDec *pSpatialDec;
207 SPATIAL_SPECIFIC_CONFIG
208 spatialSpecificConfigBackup; /* SSC used while parsing */
209
210 /* Creation parameter */
211 UCHAR mpegSurroundDecoderLevel;
212 /* Run-time parameter */
213 UCHAR mpegSurroundSscIsGlobalCfg; /* Flag telling that the SSC
214 (::spatialSpecificConfig) is a
215 out-of-band configuration. */
216 UCHAR mpegSurroundUseTimeInterface;
217
218 SPATIAL_BS_FRAME
219 bsFrames[1]; /* Bitstream Structs that contain data read from the
220 SpatialFrame() bitstream element */
221 BS_LL_STATE llState; /* Bit stream parser state memory */
222 UCHAR bsFrameParse; /* Current parse frame context index */
223 UCHAR bsFrameDecode; /* Current decode/apply frame context index */
224 UCHAR bsFrameDelay; /* Amount of frames delay between parsing and processing.
225 Required i.e. for interpolation error concealment. */
226
227 /* User prameters */
228 SPATIALDEC_PARAM mpegSurroundUserParams;
229
230 /* Internal flags */
231 SPATIAL_DEC_UPMIX_TYPE upmixType;
232 int initFlags[1];
233 MPEGS_ANCSTARTSTOP ancStartStopPrev;
234 MPEGS_SYNCSTATE fOnSync[1];
235
236 /* Inital decoder configuration */
237 SPATIAL_DEC_CONFIG decConfig;
238 };
239
240 SACDEC_ERROR
241 static sscCheckOutOfBand(const SPATIAL_SPECIFIC_CONFIG *pSsc,
242 const INT coreCodec, const INT sampleRate,
243 const INT frameSize);
244
245 static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc);
246
247 /**
248 * \brief Get the number of QMF bands from the sampling frequency (in Hz)
249 **/
mpegSurroundDecoder_GetNrOfQmfBands(const SPATIAL_SPECIFIC_CONFIG * pSsc,UINT sampleRate)250 static int mpegSurroundDecoder_GetNrOfQmfBands(
251 const SPATIAL_SPECIFIC_CONFIG *pSsc, UINT sampleRate) {
252 UINT samplingFrequency = sampleRate;
253 int qmfBands = 64;
254
255 if (pSsc != NULL) {
256 switch (pSsc->coreCodec) {
257 case AOT_USAC:
258 if ((pSsc->stereoConfigIndex == 3)) {
259 static const UCHAR mapIdx2QmfBands[3] = {24, 32, 16};
260 FDK_ASSERT((pSsc->coreSbrFrameLengthIndex >= 2) &&
261 (pSsc->coreSbrFrameLengthIndex <= 4));
262 qmfBands = mapIdx2QmfBands[pSsc->coreSbrFrameLengthIndex - 2];
263 }
264 return qmfBands;
265 default:
266 samplingFrequency = pSsc->samplingFreq;
267 break;
268 }
269 }
270
271 /* number of QMF bands depend on sampling frequency, see FDIS 23003-1:2006
272 * Chapter 6.3.3 */
273 if (samplingFrequency < 27713) {
274 qmfBands = 32;
275 }
276 if (samplingFrequency > 55426) {
277 qmfBands = 128;
278 }
279
280 return qmfBands;
281 }
282
283 /**
284 * \brief Analyse init flags
285 **/
mpegSurroundDecoder_CalcInitFlags(SPATIAL_SPECIFIC_CONFIG * pSsc1,SPATIAL_SPECIFIC_CONFIG * pSsc2,int upmixTypeFlag,int binauralQualityFlag,int partiallyComplexFlag,int * ctrlFlags)286 static int mpegSurroundDecoder_CalcInitFlags(SPATIAL_SPECIFIC_CONFIG *pSsc1,
287 SPATIAL_SPECIFIC_CONFIG *pSsc2,
288 int upmixTypeFlag,
289 int binauralQualityFlag,
290 int partiallyComplexFlag,
291 int *ctrlFlags) {
292 /* Analyse core coder */
293 if (pSsc1->coreCodec != pSsc2->coreCodec) {
294 *ctrlFlags |= MASK_MPEGS_INIT_ALL_STATES;
295 *ctrlFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
296 } else {
297 /* Analyse elements for initialization of space analysis qmf filterbank */
298 if ((partiallyComplexFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
299 (pSsc1->samplingFreq != pSsc2->samplingFreq)) {
300 *ctrlFlags |= MPEGS_INIT_STATES_ANA_QMF_FILTER;
301 *ctrlFlags |= MPEGS_INIT_STATES_ANA_HYB_FILTER;
302 }
303
304 /* Analyse elements for initialization of space synthesis qmf filterbank */
305 if ((upmixTypeFlag) || (partiallyComplexFlag) ||
306 (pSsc1->treeConfig != pSsc2->treeConfig) ||
307 (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
308 (pSsc1->bsFixedGainDMX != pSsc2->bsFixedGainDMX)) {
309 *ctrlFlags |= MPEGS_INIT_STATES_SYN_QMF_FILTER;
310 }
311
312 /* Analyse elements for initialization of decorrelator */
313 if ((upmixTypeFlag) || (partiallyComplexFlag) ||
314 (pSsc1->treeConfig != pSsc2->treeConfig) ||
315 (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
316 (pSsc1->decorrConfig != pSsc2->decorrConfig)) {
317 *ctrlFlags |= MPEGS_INIT_STATES_DECORRELATOR;
318 }
319
320 /* Analyse elements for initialization of m1 and m2 calculation */
321 if ((upmixTypeFlag) || (binauralQualityFlag) ||
322 (pSsc1->treeConfig != pSsc2->treeConfig) ||
323 (pSsc1->samplingFreq != pSsc2->samplingFreq))
324
325 {
326 *ctrlFlags |= MPEGS_INIT_STATES_M1M2;
327 }
328
329 /* Analyse elements for initialization of GES */
330 if ((upmixTypeFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
331 (pSsc1->tempShapeConfig != pSsc2->tempShapeConfig)) {
332 *ctrlFlags |= MPEGS_INIT_STATES_GES;
333 }
334
335 /* Analyse elements for initialization of FDreverb */
336 if ((upmixTypeFlag) || (binauralQualityFlag) || (partiallyComplexFlag) ||
337 (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
338 (pSsc1->nTimeSlots != pSsc2->nTimeSlots)) {
339 *ctrlFlags |= MPEGS_INIT_STATES_REVERB;
340 }
341
342 /* Reset previous frame data whenever the config changes */
343 if (*ctrlFlags & MPEGS_INIT_CONFIG) {
344 *ctrlFlags |= MPEGS_INIT_STATES_PARAM;
345 }
346 }
347
348 return MPS_OK;
349 }
350
351 /**
352 * \brief Reset MPEG Surround status info
353 **/
updateMpegSurroundDecoderStatus(CMpegSurroundDecoder * pMpegSurroundDecoder,int initFlags,MPEGS_SYNCSTATE fOnSync,MPEGS_ANCSTARTSTOP ancStartStopPrev)354 static void updateMpegSurroundDecoderStatus(
355 CMpegSurroundDecoder *pMpegSurroundDecoder, int initFlags,
356 MPEGS_SYNCSTATE fOnSync, MPEGS_ANCSTARTSTOP ancStartStopPrev) {
357 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
358 initFlags;
359 if ((pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg != 0) &&
360 (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] >=
361 MPEGS_SYNC_FOUND) &&
362 (fOnSync < MPEGS_SYNC_FOUND)) {
363 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
364 MPEGS_SYNC_FOUND;
365 } else {
366 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
367 fOnSync;
368 }
369 pMpegSurroundDecoder->ancStartStopPrev = ancStartStopPrev;
370 }
371
372 static SACDEC_ERROR mpegSurroundDecoder_Create(
373 CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
374 HANDLE_FDK_QMF_DOMAIN pQmfDomain);
375
376 SAC_INSTANCE_AVAIL
mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(CMpegSurroundDecoder * pMpegSurroundDecoder)377 mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(
378 CMpegSurroundDecoder *pMpegSurroundDecoder) {
379 SAC_INSTANCE_AVAIL instanceAvailable = SAC_INSTANCE_NOT_FULL_AVAILABLE;
380
381 if (pMpegSurroundDecoder->pSpatialDec != NULL) {
382 instanceAvailable = SAC_INSTANCE_FULL_AVAILABLE;
383 }
384
385 return instanceAvailable;
386 }
387
mpegSurroundDecoder_Open(CMpegSurroundDecoder ** pMpegSurroundDecoder,int stereoConfigIndex,HANDLE_FDK_QMF_DOMAIN pQmfDomain)388 SACDEC_ERROR mpegSurroundDecoder_Open(
389 CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
390 HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
391 SACDEC_ERROR error;
392
393 error = mpegSurroundDecoder_Create(pMpegSurroundDecoder, stereoConfigIndex,
394 pQmfDomain);
395
396 return error;
397 }
398
399 /**
400 * \brief Renamed function from getUpmixType to check_UParam_Build_DecConfig.
401 * This function checks if user params, decoder config and SSC are valid
402 * and if the decoder build can handle all this settings.
403 * The upmix type may be modified by this function.
404 * It is called in initMpegSurroundDecoder() after the ssc parse check,
405 * to have all checks in one place and to ensure these checks are always
406 * performed if config changes (inband and out-of-band).
407 *
408 * \param pUserParams User data handle.
409 * \param pDecConfig decoder config handle.
410 * \param pSsc spatial specific config handle.
411 * \param pUpmixType upmix type which is set by this function
412 *
413 * \return MPS_OK on sucess, and else on failure.
414 */
check_UParam_Build_DecConfig(SPATIALDEC_PARAM const * pUserParams,SPATIAL_DEC_CONFIG const * pDecConfig,const SPATIAL_SPECIFIC_CONFIG * pSsc,SPATIAL_DEC_UPMIX_TYPE * pUpmixType)415 static SACDEC_ERROR check_UParam_Build_DecConfig(
416 SPATIALDEC_PARAM const *pUserParams, SPATIAL_DEC_CONFIG const *pDecConfig,
417 const SPATIAL_SPECIFIC_CONFIG *pSsc, SPATIAL_DEC_UPMIX_TYPE *pUpmixType) {
418 int dmxChannels, outChannels, maxNumOutChannels;
419
420 FDK_ASSERT(pUserParams != NULL);
421 FDK_ASSERT(pUpmixType != NULL);
422
423 /* checks if implementation can handle the Ssc */
424
425 switch (pSsc->treeConfig) {
426 case SPATIALDEC_MODE_RSVD7: /* 212 */
427 dmxChannels = 1;
428 outChannels = 2;
429 break;
430 default:
431 return MPS_UNSUPPORTED_CONFIG;
432 }
433
434 /* ------------------------------------------- */
435
436 /* Analyse pDecConfig params */
437 switch (pDecConfig->binauralMode) {
438 case BINAURAL_NONE:
439 break;
440 default:
441 return MPS_UNSUPPORTED_CONFIG;
442 }
443
444 switch (pDecConfig->decoderMode) {
445 case EXT_HQ_ONLY:
446 break;
447 default:
448 return MPS_UNSUPPORTED_CONFIG;
449 }
450
451 switch (pDecConfig->maxNumOutputChannels) {
452 case OUTPUT_CHANNELS_DEFAULT:
453 /* No special restrictions -> Get the level restriction: */
454 switch (pDecConfig->decoderLevel) {
455 case DECODER_LEVEL_0:
456 maxNumOutChannels = 2;
457 break;
458 default:
459 return MPS_UNSUPPORTED_CONFIG;
460 }
461 break;
462 case OUTPUT_CHANNELS_2_0:
463 maxNumOutChannels = 2;
464 break;
465 default:
466 return MPS_UNSUPPORTED_CONFIG;
467 }
468 /* ------------------------- */
469
470 /* check if we can handle user params */
471 if (pUserParams->blindEnable == 1) {
472 return MPS_UNSUPPORTED_CONFIG;
473 }
474 {
475 switch ((SAC_DEC_OUTPUT_MODE)pUserParams->outputMode) {
476 case SACDEC_OUT_MODE_NORMAL:
477 if (maxNumOutChannels >= outChannels) {
478 *pUpmixType = UPMIX_TYPE_NORMAL;
479 } else {
480 { *pUpmixType = UPMIX_TYPE_BYPASS; }
481 }
482 break;
483 case SACDEC_OUT_MODE_STEREO:
484 if (dmxChannels == 1) {
485 if (outChannels == 2) {
486 *pUpmixType = UPMIX_TYPE_NORMAL;
487 }
488 } else {
489 *pUpmixType = UPMIX_TYPE_BYPASS;
490 }
491 break;
492 case SACDEC_OUT_MODE_6CHANNEL:
493 if (outChannels > 6) {
494 { *pUpmixType = UPMIX_TYPE_BYPASS; }
495 } else {
496 *pUpmixType = UPMIX_TYPE_NORMAL;
497 }
498 break;
499 default:
500 return MPS_UNSUPPORTED_CONFIG;
501 }
502 }
503
504 return MPS_OK;
505 }
506
507 /**
508 * \brief Init MPEG Surround decoder.
509 **/
initMpegSurroundDecoder(CMpegSurroundDecoder * pMpegSurroundDecoder)510 static SACDEC_ERROR initMpegSurroundDecoder(
511 CMpegSurroundDecoder *pMpegSurroundDecoder) {
512 SACDEC_ERROR err;
513 int initFlags = MPEGS_INIT_NONE, initFlagsDec;
514 int upmixTypeCurr = pMpegSurroundDecoder->upmixType;
515
516 FDK_ASSERT(pMpegSurroundDecoder != NULL);
517
518 SPATIAL_SPECIFIC_CONFIG *const pSSCinput =
519 &pMpegSurroundDecoder->spatialSpecificConfigBackup;
520 SPATIAL_SPECIFIC_CONFIG *const pSSCtarget =
521 &pMpegSurroundDecoder
522 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
523 initFlagsDec =
524 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode];
525
526 if (pSSCinput->coreCodec != AOT_USAC) {
527 /* here we check if we have a valid Ssc */
528 err = sscParseCheck(pSSCinput);
529 if (err != MPS_OK) goto bail;
530 }
531
532 /* here we check if Ssc matches build; also check UParams and DecConfig */
533 /* if desired upmixType is changes */
534 err = check_UParam_Build_DecConfig(
535 &pMpegSurroundDecoder->mpegSurroundUserParams,
536 &pMpegSurroundDecoder->decConfig, pSSCinput,
537 &pMpegSurroundDecoder->upmixType);
538 if (err != MPS_OK) goto bail;
539
540 /* init config */
541 if (initFlagsDec & MPEGS_INIT_CHANGE_HEADER) {
542 initFlags |= MPEGS_INIT_CONFIG;
543 }
544 /* init all states */
545 if (initFlagsDec & MPEGS_INIT_CLEAR_HISTORY) {
546 initFlags |= MASK_MPEGS_INIT_ALL_STATES;
547 }
548 if (initFlagsDec & MPEGS_INIT_CHANGE_CONCEAL_PARAMS) {
549 initFlags |= MPEGS_INIT_PARAMS_ERROR_CONCEALMENT;
550 }
551
552 if (initFlagsDec & MPEGS_INIT_ENFORCE_REINIT) {
553 /* init all states */
554 initFlags |= MASK_MPEGS_INIT_ALL_STATES;
555 initFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
556 } else {
557 /* analyse states which have to be initialized */
558 mpegSurroundDecoder_CalcInitFlags(
559 pSSCtarget, pSSCinput,
560 (upmixTypeCurr !=
561 pMpegSurroundDecoder->upmixType), /* upmixType changed */
562 0, (initFlagsDec & MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX) ? 1 : 0,
563 &initFlags);
564 }
565
566 {
567 int nrOfQmfBands;
568 FDKmemcpy(pSSCtarget, pSSCinput, sizeof(SPATIAL_SPECIFIC_CONFIG));
569
570 nrOfQmfBands = mpegSurroundDecoder_GetNrOfQmfBands(
571 pSSCtarget, pSSCtarget->samplingFreq);
572 err = FDK_SpatialDecInit(
573 pMpegSurroundDecoder->pSpatialDec,
574 &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
575 pSSCtarget, nrOfQmfBands, pMpegSurroundDecoder->upmixType,
576 &pMpegSurroundDecoder->mpegSurroundUserParams, initFlags);
577
578 if (err != MPS_OK) goto bail;
579
580 /* Signal that we got a header and can go on decoding */
581 if (err == MPS_OK) {
582 initFlagsDec = MPEGS_INIT_OK;
583 {
584 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
585 MPEGS_SYNC_FOUND;
586 }
587 }
588 }
589
590 bail:
591 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] =
592 initFlagsDec;
593 return err;
594 }
595
596 /**
597 * \brief Init MPEG Surround decoder.
598 **/
mpegSurroundDecoder_Init(CMpegSurroundDecoder * pMpegSurroundDecoder)599 SACDEC_ERROR mpegSurroundDecoder_Init(
600 CMpegSurroundDecoder *pMpegSurroundDecoder) {
601 SACDEC_ERROR err = MPS_OK;
602
603 if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
604 err = initMpegSurroundDecoder(pMpegSurroundDecoder);
605 }
606 return err;
607 }
608
609 /**
610 * \brief Open MPEG Surround decoder.
611 **/
mpegSurroundDecoder_Create(CMpegSurroundDecoder ** pMpegSurroundDecoder,int stereoConfigIndex,HANDLE_FDK_QMF_DOMAIN pQmfDomain)612 static SACDEC_ERROR mpegSurroundDecoder_Create(
613 CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
614 HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
615 SACDEC_ERROR err = MPS_OK;
616 CMpegSurroundDecoder *sacDec = NULL;
617 spatialDec *self = NULL;
618
619 /* decoderLevel decoderMode maxNumOutputChannels binauralMode */
620 static const SPATIAL_DEC_CONFIG decConfig = {
621 (CFG_LEVEL)(0), EXT_HQ_ONLY, OUTPUT_CHANNELS_DEFAULT, BINAURAL_NONE};
622
623 if (*pMpegSurroundDecoder == NULL) {
624 FDK_ALLOCATE_MEMORY_1D(*pMpegSurroundDecoder, 1, CMpegSurroundDecoder)
625
626 for (int i = 0; i < 1; i++) {
627 err = SpatialDecCreateBsFrame(&(*pMpegSurroundDecoder)->bsFrames[i],
628 &(*pMpegSurroundDecoder)->llState);
629 if (err != MPS_OK) {
630 sacDec = *pMpegSurroundDecoder;
631 goto bail;
632 }
633 }
634 (*pMpegSurroundDecoder)->pQmfDomain = pQmfDomain;
635
636 (*pMpegSurroundDecoder)->bsFrameDelay = 1;
637 (*pMpegSurroundDecoder)->bsFrameParse = 0;
638 (*pMpegSurroundDecoder)->bsFrameDecode = 0;
639
640 return err;
641 } else {
642 sacDec = *pMpegSurroundDecoder;
643 }
644
645 if (sacDec->pSpatialDec == NULL) {
646 if ((self = FDK_SpatialDecOpen(&decConfig, stereoConfigIndex)) == NULL) {
647 err = MPS_OUTOFMEMORY;
648 goto bail;
649 }
650 } else {
651 self = sacDec->pSpatialDec;
652 }
653
654 self->pQmfDomain = sacDec->pQmfDomain;
655
656 sacDec->pSpatialDec = self;
657
658 /* default parameter set */
659 sacDec->mpegSurroundUserParams.outputMode = SACDEC_OUT_MODE_NORMAL;
660 sacDec->mpegSurroundUserParams.blindEnable = 0;
661 sacDec->mpegSurroundUserParams.bypassMode = 0;
662 sacDec->mpegSurroundUserParams.concealMethod = 1;
663 sacDec->mpegSurroundUserParams.concealNumKeepFrames = 10;
664 sacDec->mpegSurroundUserParams.concealFadeOutSlopeLength = 5;
665 sacDec->mpegSurroundUserParams.concealFadeInSlopeLength = 5;
666 sacDec->mpegSurroundUserParams.concealNumReleaseFrames = 3;
667 sacDec->mpegSurroundSscIsGlobalCfg = 0;
668 sacDec->mpegSurroundUseTimeInterface = 1;
669 sacDec->mpegSurroundDecoderLevel = decConfig.decoderLevel;
670
671 sacDec->upmixType = UPMIX_TYPE_NORMAL;
672
673 /* signalize spatial decoder re-initalization */
674 updateMpegSurroundDecoderStatus(sacDec, MPEGS_INIT_ENFORCE_REINIT,
675 MPEGS_SYNC_LOST, MPEGS_STOP);
676
677 /* return decoder instance */
678 *pMpegSurroundDecoder = sacDec;
679 sacDec->decConfig = decConfig;
680
681 SpatialDecInitParserContext(sacDec->pSpatialDec);
682
683 return err;
684
685 bail:
686 if (sacDec != NULL) {
687 mpegSurroundDecoder_Close(sacDec);
688 }
689 *pMpegSurroundDecoder = NULL;
690 if (err == MPS_OK) {
691 return MPS_OUTOFMEMORY;
692 } else {
693 return err;
694 }
695 }
696
697 /**
698 * \brief Config MPEG Surround decoder.
699 **/
mpegSurroundDecoder_Config(CMpegSurroundDecoder * pMpegSurroundDecoder,HANDLE_FDK_BITSTREAM hBs,AUDIO_OBJECT_TYPE coreCodec,INT samplingRate,INT frameSize,INT stereoConfigIndex,INT coreSbrFrameLengthIndex,INT configBytes,const UCHAR configMode,UCHAR * configChanged)700 SACDEC_ERROR mpegSurroundDecoder_Config(
701 CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
702 AUDIO_OBJECT_TYPE coreCodec, INT samplingRate, INT frameSize,
703 INT stereoConfigIndex, INT coreSbrFrameLengthIndex, INT configBytes,
704 const UCHAR configMode, UCHAR *configChanged) {
705 SACDEC_ERROR err = MPS_OK;
706 SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig;
707 SPATIAL_SPECIFIC_CONFIG *pSsc =
708 &pMpegSurroundDecoder->spatialSpecificConfigBackup;
709
710 switch (coreCodec) {
711 case AOT_DRM_USAC:
712 case AOT_USAC:
713 if (configMode == AC_CM_DET_CFG_CHANGE) {
714 /* In config detection mode write spatial specific config parameters
715 * into temporarily allocated structure */
716 err = SpatialDecParseMps212Config(
717 hBs, &spatialSpecificConfig, samplingRate, coreCodec,
718 stereoConfigIndex, coreSbrFrameLengthIndex);
719 pSsc = &spatialSpecificConfig;
720 } else {
721 err = SpatialDecParseMps212Config(
722 hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
723 samplingRate, coreCodec, stereoConfigIndex,
724 coreSbrFrameLengthIndex);
725 }
726 break;
727 case AOT_ER_AAC_ELD:
728 case AOT_ER_AAC_LD:
729 if (configMode == AC_CM_DET_CFG_CHANGE) {
730 /* In config detection mode write spatial specific config parameters
731 * into temporarily allocated structure */
732 err = SpatialDecParseSpecificConfig(hBs, &spatialSpecificConfig,
733 configBytes, coreCodec);
734 pSsc = &spatialSpecificConfig;
735 } else {
736 err = SpatialDecParseSpecificConfig(
737 hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
738 configBytes, coreCodec);
739 }
740 break;
741 default:
742 err = MPS_UNSUPPORTED_FORMAT;
743 break;
744 }
745
746 if (err != MPS_OK) {
747 goto bail;
748 }
749
750 err = sscCheckOutOfBand(pSsc, coreCodec, samplingRate, frameSize);
751
752 if (err != MPS_OK) {
753 goto bail;
754 }
755
756 if (configMode & AC_CM_DET_CFG_CHANGE) {
757 return err;
758 }
759
760 if (configMode & AC_CM_ALLOC_MEM) {
761 if (*configChanged) {
762 err = mpegSurroundDecoder_Open(&pMpegSurroundDecoder, stereoConfigIndex,
763 NULL);
764 if (err) {
765 return err;
766 }
767 }
768 }
769
770 {
771 SPATIAL_SPECIFIC_CONFIG *sscParse =
772 &pMpegSurroundDecoder
773 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
774
775 if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
776 &pMpegSurroundDecoder->spatialSpecificConfigBackup, sscParse)) {
777 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
778 MPEGS_INIT_CHANGE_HEADER;
779 /* Error resilience code */
780 if (pMpegSurroundDecoder->pSpatialDec == NULL) {
781 err = MPS_NOTOK;
782 goto bail;
783 }
784 SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
785 pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
786 &pMpegSurroundDecoder
787 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
788 }
789 }
790
791 if (err == MPS_OK) {
792 /* We got a valid out-of-band configuration so label it accordingly. */
793 pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 1;
794 }
795
796 bail:
797 return err;
798 }
799
800 /**
801 * \brief Determine MPEG Surround operation mode.
802 **/
mpegSurroundOperationMode(CMpegSurroundDecoder * pMpegSurroundDecoder,int mpsDataBits)803 static MPEGS_OPMODE mpegSurroundOperationMode(
804 CMpegSurroundDecoder *pMpegSurroundDecoder, int mpsDataBits) {
805 MPEGS_OPMODE mode;
806
807 {
808 if ((mpsDataBits > 0) &&
809 (pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable == 0)) {
810 mode = MPEGS_OPMODE_MPS_PAYLOAD; /* Mode: Normal, Stereo or Binaural */
811 } else {
812 mode = MPEGS_OPMODE_NO_MPS_PAYLOAD; /* Mode: No MPEG Surround Payload */
813 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
814 MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
815 MPEGS_STOP);
816 }
817 }
818
819 return (mode);
820 }
821
822 /**
823 * \brief Check ssc for parse errors.
824 * This one is called in initMpegSurroundDecoder()
825 * to ensure checking of inband and out-of-band mps configs.
826 * Only parse errors checked here! Check for valid config is done
827 * in check_UParam_Build_DecConfig()!
828 *
829 * \param pSsc spatial specific config handle.
830 *
831 * \return MPS_OK on sucess, and else on parse error.
832 */
sscParseCheck(const SPATIAL_SPECIFIC_CONFIG * pSsc)833 static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc) {
834 if (pSsc->samplingFreq > 96000) return MPS_PARSE_ERROR;
835 if (pSsc->samplingFreq < 8000) return MPS_PARSE_ERROR;
836
837 if ((pSsc->treeConfig < 0) || (pSsc->treeConfig > 7)) {
838 return MPS_PARSE_ERROR;
839 }
840
841 if ((pSsc->quantMode < 0) || (pSsc->quantMode > 2)) {
842 return MPS_PARSE_ERROR;
843 }
844
845 /* now we are sure there were no parsing errors */
846
847 return MPS_OK;
848 }
849
850 /**
851 * \brief Check number of time slots
852 *
853 * Basically the mps frame length must be a multiple of the core coder frame
854 * length. The below table shows all valid configurations in detail. See ISO/IEC
855 * 23003-1: "Table 4A - Allowed values for bsFrameLength in the Baseline MPEG
856 * Surround Profile"
857 *
858 * Downmix Coder Downmix Code Allowed values for bsFrameLength
859 * Allowed frame sizes for normal, downsampled and upsampled MPS Framelength
860 * (QMF Samples)
861 *
862 * AAC 1024 16 15, 31, 47, 63 1024 2048 3072 4096
863 * downsampled MPS 32 31, 63 1024 2048 upsampled MPS
864 * 8 7, 15, 23, 31, 39, 47, 55, 63, 71 1024 2048 3072 4096
865 * 5120 6144 7168 8192 9216
866 *
867 * AAC 960 15 14, 29, 44, 59 960 1920 2880 3840
868 * downsampled MPS 30 29, 59 960 1920 upsampled MPS
869 * 7,5 14, 29, 44, 59 1920 3840 5760 7680
870 *
871 * HE-AAC 1024/2048 32 31, 63 2048 4096 downsampled MPS
872 * 64 63 2048 upsampled MPS
873 * 16 15, 31, 47, 63 2048 4096 6144 8192
874 *
875 * HE-AAC 960/1920 30 29, 59 1920 3840 downsampled MPS
876 * 60 59 1920 upsampled MPS
877 * 15 14, 29, 44, 59 1920 3840 5760 7680
878 *
879 * BSAC 16 15, 31, 47, 63 1024 2048 3072 4096
880 * downsampled MPS 32 31, 63 1024 2048 upsampled MPS
881 * 8 7, 15, 23, 31, 39, 47, 55, 63, 71 1024 2048 3072 4096
882 * 5120 6144 7168 8192 9216
883 *
884 * BSAC with SBR 32 31, 63 2048 4096 downsampled MPS
885 * 64 63 2048 upsampled MPS
886 * 16 15, 31, 47, 63 2048 4096 6144 8192
887 *
888 * AAC LD 512 8 7, 15, 23, 31, 39, 47, 55, 63, 71
889 * 512 1024 1536 2048 2560 3072 3584 4096 4608 downsampled MPS
890 * 16 15, 31, 47, 63 512 1024 1536 2048
891 *
892 * AAC ELD 512 8 7, 15, 23, 31, 39, 47, 55, 63, 71
893 * 512 1024 1536 2048 2560 3072 3584 4096 4608 downsampled MPS
894 * 16 15, 31, 47, 63 512 1024 1536 2048
895 *
896 * AAC ELD with SBR 512/1024 16 15, 31, 47, 63 1024 2048 3072 4096
897 * downsampled MPS 32 31, 63 1024 2048 upsampled MPS
898 * 8 7, 15, 23, 31, 39, 47, 55, 63, 71 1024 2048 3072 4096
899 * 5120 6144 7168 8192 9216
900 *
901 * MPEG1/2 Layer II 18 17, 35, 53, 71 1152 2304 3456 4608
902 * downsampled MPS 36 35, 71 1152 2304
903 *
904 * MPEG1/2 Layer III 18 17, 35, 53, 71 1152 2304 3456 4608
905 * downsampled MPS 36 35, 71 1152 2304
906 *
907 * \param frameLength
908 * \param qmfBands
909 * \param timeSlots
910 *
911 * \return error code
912 */
checkTimeSlots(int frameLength,int qmfBands,int timeSlots)913 SACDEC_ERROR checkTimeSlots(int frameLength, int qmfBands, int timeSlots) {
914 int len;
915 int maxFrameLength;
916
917 if (qmfBands == 64) {
918 /* normal MPEG Surround */
919 switch (frameLength) {
920 case 960:
921 case 1920:
922 maxFrameLength = 3840;
923 break;
924 case 1024:
925 case 2048:
926 maxFrameLength = 4096;
927 break;
928 case 512:
929 case 1152:
930 maxFrameLength = 4608;
931 break;
932 default:
933 return MPS_PARSE_ERROR;
934 }
935 } else if (qmfBands == 32) {
936 /* downsampled MPEG Surround */
937 switch (frameLength) {
938 case 960:
939 case 1920:
940 maxFrameLength = 1920;
941 break;
942 case 512:
943 case 1024:
944 case 2048:
945 maxFrameLength = 2048;
946 break;
947 case 1152:
948 maxFrameLength = 2304;
949 break;
950 default:
951 return MPS_PARSE_ERROR;
952 }
953 } else if (qmfBands == 128) {
954 /* upsampled MPEG Surround */
955 switch (frameLength) {
956 case 1920:
957 maxFrameLength = 7680;
958 break;
959 case 1024:
960 maxFrameLength = 9216;
961 break;
962 case 2048:
963 maxFrameLength = 8192;
964 break;
965 case 512:
966 case 960:
967 case 1152:
968 /* no break, no support for upsampled MPEG Surround */
969 default:
970 return MPS_PARSE_ERROR;
971 }
972 } else {
973 return MPS_PARSE_ERROR;
974 }
975
976 len = frameLength;
977
978 while (len <= maxFrameLength) {
979 if (len == timeSlots * qmfBands) {
980 return MPS_OK;
981 }
982 len += frameLength;
983 }
984 return MPS_PARSE_ERROR;
985 }
986
987 /**
988 * \brief Check ssc for consistency (e.g. bit errors could cause trouble)
989 * First of currently two ssc-checks.
990 * This (old) one is called in mpegSurroundDecoder_Apply()
991 * only if inband mps config is contained in stream.
992 *
993 * New ssc check is split in two functions sscParseCheck() and
994 * check_UParam_Build_DecConfig(). sscParseCheck() checks only for correct
995 * parsing. check_UParam_Build_DecConfig() is used to check if we have a
996 * valid config. Both are called in initMpegSurroundDecoder() to ensure
997 * checking of inband and out-of-band mps configs.
998 *
999 * If this function can be integrated into the new functions.
1000 * We can remove this one.
1001 *
1002 * \param pSsc spatial specific config handle.
1003 * \param frameLength
1004 * \param sampleRate
1005 *
1006 * \return MPS_OK on sucess, and else on failure.
1007 */
sscCheckInBand(SPATIAL_SPECIFIC_CONFIG * pSsc,int frameLength,int sampleRate)1008 static SACDEC_ERROR sscCheckInBand(SPATIAL_SPECIFIC_CONFIG *pSsc,
1009 int frameLength, int sampleRate) {
1010 SACDEC_ERROR err = MPS_OK;
1011 int qmfBands;
1012
1013 FDK_ASSERT(pSsc != NULL);
1014
1015 /* check ssc for parse errors */
1016 if (sscParseCheck(pSsc) != MPS_OK) {
1017 err = MPS_PARSE_ERROR;
1018 }
1019
1020 /* core fs and mps fs must match */
1021 if (pSsc->samplingFreq != sampleRate) {
1022 err = MPS_PARSE_ERROR /* MPEGSDEC_SSC_PARSE_ERROR */;
1023 }
1024
1025 qmfBands = mpegSurroundDecoder_GetNrOfQmfBands(pSsc, pSsc->samplingFreq);
1026
1027 if (checkTimeSlots(frameLength, qmfBands, pSsc->nTimeSlots) != MPS_OK) {
1028 err = MPS_PARSE_ERROR;
1029 }
1030
1031 return err;
1032 }
1033
1034 SACDEC_ERROR
mpegSurroundDecoder_ConfigureQmfDomain(CMpegSurroundDecoder * pMpegSurroundDecoder,SAC_INPUT_CONFIG sac_dec_interface,UINT coreSamplingRate,AUDIO_OBJECT_TYPE coreCodec)1035 mpegSurroundDecoder_ConfigureQmfDomain(
1036 CMpegSurroundDecoder *pMpegSurroundDecoder,
1037 SAC_INPUT_CONFIG sac_dec_interface, UINT coreSamplingRate,
1038 AUDIO_OBJECT_TYPE coreCodec) {
1039 SACDEC_ERROR err = MPS_OK;
1040 FDK_QMF_DOMAIN_GC *pGC = NULL;
1041
1042 if (pMpegSurroundDecoder == NULL) {
1043 return MPS_INVALID_HANDLE;
1044 }
1045
1046 FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1047
1048 pGC = &pMpegSurroundDecoder->pQmfDomain->globalConf;
1049 if (pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg) {
1050 SPATIAL_SPECIFIC_CONFIG *pSSC =
1051 &pMpegSurroundDecoder->spatialSpecificConfigBackup;
1052 if (sac_dec_interface == SAC_INTERFACE_TIME) {
1053 /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
1054 pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
1055 pSSC, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
1056 SAC_INTERFACE_TIME */
1057 pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
1058 pGC->nInputChannels_requested =
1059 fMax((UINT)pSSC->nInputChannels, (UINT)pGC->nInputChannels_requested);
1060 }
1061 pGC->nOutputChannels_requested =
1062 fMax((UINT)pSSC->nOutputChannels, (UINT)pGC->nOutputChannels_requested);
1063 } else {
1064 if (sac_dec_interface == SAC_INTERFACE_TIME) {
1065 /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
1066 pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
1067 NULL, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
1068 SAC_INTERFACE_TIME */
1069 pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
1070 pGC->nInputChannels_requested =
1071 pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels;
1072 }
1073 pGC->nOutputChannels_requested =
1074 pMpegSurroundDecoder->pSpatialDec->createParams.maxNumOutputChannels;
1075 }
1076 pGC->nQmfProcBands_requested = 64;
1077 pGC->nQmfProcChannels_requested =
1078 fMin((INT)pGC->nInputChannels_requested,
1079 pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels);
1080
1081 if (coreCodec == AOT_ER_AAC_ELD) {
1082 pGC->flags_requested |= QMF_FLAG_MPSLDFB;
1083 pGC->flags_requested &= ~QMF_FLAG_CLDFB;
1084 }
1085
1086 return err;
1087 }
1088
1089 /**
1090 * \brief Check out-of-band config
1091 *
1092 * \param pSsc spatial specific config handle.
1093 * \param coreCodec core codec.
1094 * \param sampleRate sampling frequency.
1095 *
1096 * \return errorStatus
1097 */
1098 SACDEC_ERROR
sscCheckOutOfBand(const SPATIAL_SPECIFIC_CONFIG * pSsc,const INT coreCodec,const INT sampleRate,const INT frameSize)1099 sscCheckOutOfBand(const SPATIAL_SPECIFIC_CONFIG *pSsc, const INT coreCodec,
1100 const INT sampleRate, const INT frameSize) {
1101 FDK_ASSERT(pSsc != NULL);
1102 int qmfBands = 0;
1103
1104 /* check ssc for parse errors */
1105 if (sscParseCheck(pSsc) != MPS_OK) {
1106 return MPS_PARSE_ERROR;
1107 }
1108
1109 switch (coreCodec) {
1110 case AOT_USAC:
1111 case AOT_DRM_USAC:
1112 /* ISO/IEC 23003-1:2007(E), Chapter 6.3.3, Support for lower and higher
1113 * sampling frequencies */
1114 if (pSsc->samplingFreq >= 55426) {
1115 return MPS_PARSE_ERROR;
1116 }
1117 break;
1118 case AOT_ER_AAC_LD:
1119 case AOT_ER_AAC_ELD:
1120 /* core fs and mps fs must match */
1121 if (pSsc->samplingFreq != sampleRate) {
1122 return MPS_PARSE_ERROR;
1123 }
1124
1125 /* ISO/IEC 14496-3:2009 FDAM 3: Chapter 1.5.2.3, Levels for the Low Delay
1126 * AAC v2 profile */
1127 if (pSsc->samplingFreq > 48000) {
1128 return MPS_PARSE_ERROR;
1129 }
1130
1131 qmfBands = mpegSurroundDecoder_GetNrOfQmfBands(pSsc, pSsc->samplingFreq);
1132 switch (frameSize) {
1133 case 480:
1134 if (!((qmfBands == 32) && (pSsc->nTimeSlots == 15))) {
1135 return MPS_PARSE_ERROR;
1136 }
1137 break;
1138 case 960:
1139 if (!((qmfBands == 64) && (pSsc->nTimeSlots == 15))) {
1140 return MPS_PARSE_ERROR;
1141 }
1142 break;
1143 case 512:
1144 if (!(((qmfBands == 32) && (pSsc->nTimeSlots == 16)) ||
1145 ((qmfBands == 64) && (pSsc->nTimeSlots == 8)))) {
1146 return MPS_PARSE_ERROR;
1147 }
1148 break;
1149 case 1024:
1150 if (!((qmfBands == 64) && (pSsc->nTimeSlots == 16))) {
1151 return MPS_PARSE_ERROR;
1152 }
1153 break;
1154 default:
1155 return MPS_PARSE_ERROR;
1156 }
1157 break;
1158 default:
1159 return MPS_PARSE_ERROR;
1160 break;
1161 }
1162
1163 return MPS_OK;
1164 }
1165
1166 /**
1167 * \brief Decode MPEG Surround frame.
1168 **/
mpegSurroundDecoder_ParseNoHeader(CMpegSurroundDecoder * pMpegSurroundDecoder,HANDLE_FDK_BITSTREAM hBs,int * pMpsDataBits,int fGlobalIndependencyFlag)1169 int mpegSurroundDecoder_ParseNoHeader(
1170 CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
1171 int *pMpsDataBits, int fGlobalIndependencyFlag) {
1172 SACDEC_ERROR err = MPS_OK;
1173 SPATIAL_SPECIFIC_CONFIG *sscParse;
1174 int bitsAvail, numSacBits;
1175
1176 if (pMpegSurroundDecoder == NULL || hBs == NULL) {
1177 return MPS_INVALID_HANDLE;
1178 }
1179
1180 sscParse = &pMpegSurroundDecoder
1181 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
1182
1183 bitsAvail = FDKgetValidBits(hBs);
1184
1185 /* First spatial specific config is parsed into spatialSpecificConfigBackup,
1186 * second spatialSpecificConfigBackup is copied into
1187 * spatialSpecificConfig[bsFrameDecode] */
1188 if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
1189 FDKmemcpy(sscParse, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1190 sizeof(SPATIAL_SPECIFIC_CONFIG));
1191 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
1192 MPEGS_SYNC_FOUND;
1193 }
1194
1195 if (bitsAvail <= 0) {
1196 err = MPS_PARSE_ERROR;
1197 } else {
1198 err = SpatialDecParseFrameData(
1199 pMpegSurroundDecoder->pSpatialDec,
1200 &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse],
1201 hBs, sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
1202 fGlobalIndependencyFlag);
1203 if (err == MPS_OK) {
1204 pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1205 .newBsData = 1;
1206 }
1207 }
1208
1209 numSacBits = bitsAvail - (INT)FDKgetValidBits(hBs);
1210
1211 if (numSacBits > bitsAvail) {
1212 pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1213 .newBsData = 0;
1214 err = MPS_PARSE_ERROR;
1215 }
1216
1217 *pMpsDataBits -= numSacBits;
1218
1219 return err;
1220 }
1221
1222 /**
1223 * \brief Check, if ancType is valid.
1224 **/
isValidAncType(CMpegSurroundDecoder * pMpegSurroundDecoder,int ancType)1225 static int isValidAncType(CMpegSurroundDecoder *pMpegSurroundDecoder,
1226 int ancType) {
1227 int ret = 1;
1228
1229 if ((ancType != MPEGS_ANCTYPE_HEADER_AND_FRAME) &&
1230 (ancType != MPEGS_ANCTYPE_FRAME)) {
1231 ret = 0;
1232 }
1233
1234 if (ret == 0) {
1235 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1236 MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1237 MPEGS_STOP);
1238 }
1239
1240 return (ret);
1241 }
1242
1243 /**
1244 * \brief Check, if ancStartStop is valid.
1245 **/
isValidAncStartStop(CMpegSurroundDecoder * pMpegSurroundDecoder,int ancStartStop)1246 static int isValidAncStartStop(CMpegSurroundDecoder *pMpegSurroundDecoder,
1247 int ancStartStop) {
1248 int ret = 1;
1249
1250 switch (ancStartStop) {
1251 case MPEGS_START:
1252 /* Sequence start - start and continue - start not allowed */
1253 if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START) ||
1254 (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_CONTINUE)) {
1255 ret = 0;
1256 }
1257 break;
1258
1259 case MPEGS_STOP:
1260 /* MPS payload of the previous frame must be valid if current type is stop
1261 Sequence startstop - stop and stop - stop not allowed
1262 Sequence startstop - continue and stop - continue are allowed */
1263 if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_STOP) ||
1264 (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START_STOP)) {
1265 ret = 0;
1266 }
1267 break;
1268
1269 case MPEGS_CONTINUE:
1270 case MPEGS_START_STOP:
1271 /* No error detection possible for this states */
1272 break;
1273 }
1274
1275 if (ret == 0) {
1276 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1277 MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1278 MPEGS_STOP);
1279 } else {
1280 pMpegSurroundDecoder->ancStartStopPrev = (MPEGS_ANCSTARTSTOP)ancStartStop;
1281 }
1282
1283 return (ret);
1284 }
1285
mpegSurroundDecoder_Parse(CMpegSurroundDecoder * pMpegSurroundDecoder,HANDLE_FDK_BITSTREAM hBs,int * pMpsDataBits,AUDIO_OBJECT_TYPE coreCodec,int sampleRate,int frameSize,int fGlobalIndependencyFlag)1286 int mpegSurroundDecoder_Parse(CMpegSurroundDecoder *pMpegSurroundDecoder,
1287 HANDLE_FDK_BITSTREAM hBs, int *pMpsDataBits,
1288 AUDIO_OBJECT_TYPE coreCodec, int sampleRate,
1289 int frameSize, int fGlobalIndependencyFlag) {
1290 SACDEC_ERROR err = MPS_OK;
1291 SPATIAL_SPECIFIC_CONFIG *sscParse;
1292 SPATIAL_BS_FRAME *bsFrame;
1293 HANDLE_FDK_BITSTREAM hMpsBsData = NULL;
1294 FDK_BITSTREAM mpsBsData;
1295 int mpsDataBits = *pMpsDataBits;
1296 int mpsBsBits;
1297 MPEGS_ANCTYPE ancType;
1298 MPEGS_ANCSTARTSTOP ancStartStop;
1299
1300 if (pMpegSurroundDecoder == NULL) {
1301 return MPS_INVALID_HANDLE;
1302 }
1303
1304 FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1305
1306 mpsBsBits = (INT)FDKgetValidBits(hBs);
1307
1308 sscParse = &pMpegSurroundDecoder
1309 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
1310 bsFrame = &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse];
1311
1312 /*
1313 Find operation mode of mpeg surround decoder:
1314 - MPEGS_OPMODE_EMM: Mode: Enhanced Matrix Mode (Blind)
1315 - MPEGS_OPMODE_MPS_PAYLOAD: Mode: Normal, Stereo or Binaural
1316 - MPEGS_OPMODE_NO_MPS_PAYLOAD: Mode: No MpegSurround Payload
1317 */
1318 {
1319 /* Parse ancType and ancStartStop */
1320 ancType = (MPEGS_ANCTYPE)FDKreadBits(hBs, 2);
1321 ancStartStop = (MPEGS_ANCSTARTSTOP)FDKreadBits(hBs, 2);
1322 mpsDataBits -= 4;
1323
1324 /* Set valid anc type flag, if ancType signals a payload with either header
1325 * and frame or frame */
1326 if (isValidAncType(pMpegSurroundDecoder, ancType)) {
1327 /* Set valid anc startstop flag, if transmitted sequence is not illegal */
1328 if (isValidAncStartStop(pMpegSurroundDecoder, ancStartStop)) {
1329 switch (ancStartStop) {
1330 case MPEGS_START:
1331 /* Assuming that core coder frame size (AAC) is smaller than MPS
1332 coder frame size. Save audio data for next frame. */
1333 if (mpsDataBits > MPS_DATA_BUFFER_SIZE * 8) {
1334 err = MPS_NOTOK;
1335 goto bail;
1336 }
1337 for (int i = 0; i < mpsDataBits / 8; i++) {
1338 pMpegSurroundDecoder->mpsData[i] = FDKreadBits(hBs, 8);
1339 }
1340 pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
1341 break;
1342
1343 case MPEGS_CONTINUE:
1344 case MPEGS_STOP:
1345 /* Assuming that core coder frame size (AAC) is smaller than MPS
1346 coder frame size. Save audio data for next frame. */
1347 if ((pMpegSurroundDecoder->mpsDataBits + mpsDataBits) >
1348 MPS_DATA_BUFFER_SIZE * 8) {
1349 err = MPS_NOTOK;
1350 goto bail;
1351 }
1352 for (int i = 0; i < mpsDataBits / 8; i++) {
1353 pMpegSurroundDecoder
1354 ->mpsData[(pMpegSurroundDecoder->mpsDataBits / 8) + i] =
1355 FDKreadBits(hBs, 8);
1356 }
1357 pMpegSurroundDecoder->mpsDataBits += mpsDataBits;
1358 FDKinitBitStream(&mpsBsData, pMpegSurroundDecoder->mpsData,
1359 MAX_BUFSIZE_BYTES,
1360 pMpegSurroundDecoder->mpsDataBits, BS_READER);
1361 hMpsBsData = &mpsBsData;
1362 break;
1363
1364 case MPEGS_START_STOP:
1365 pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
1366 hMpsBsData = hBs;
1367 break;
1368
1369 default:
1370 FDK_ASSERT(0);
1371 }
1372
1373 if ((ancStartStop == MPEGS_STOP) ||
1374 (ancStartStop == MPEGS_START_STOP)) {
1375 switch (ancType) {
1376 case MPEGS_ANCTYPE_HEADER_AND_FRAME: {
1377 int parseResult, bitsRead;
1378 SPATIAL_SPECIFIC_CONFIG spatialSpecificConfigTmp =
1379 pMpegSurroundDecoder->spatialSpecificConfigBackup;
1380
1381 /* Parse spatial specific config */
1382 bitsRead = (INT)FDKgetValidBits(hMpsBsData);
1383
1384 err = SpatialDecParseSpecificConfigHeader(
1385 hMpsBsData,
1386 &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
1387 pMpegSurroundDecoder->upmixType);
1388
1389 bitsRead = (bitsRead - (INT)FDKgetValidBits(hMpsBsData));
1390 parseResult = ((err == MPS_OK) ? bitsRead : -bitsRead);
1391
1392 if (parseResult < 0) {
1393 parseResult = -parseResult;
1394 err = MPS_PARSE_ERROR;
1395 } else if (err == MPS_OK) {
1396 /* Check SSC for consistency (e.g. bit errors could cause
1397 * trouble) */
1398 err = sscCheckInBand(
1399 &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1400 frameSize, sampleRate);
1401 }
1402 if (err != MPS_OK) {
1403 pMpegSurroundDecoder->spatialSpecificConfigBackup =
1404 spatialSpecificConfigTmp;
1405 break;
1406 }
1407
1408 pMpegSurroundDecoder->mpsDataBits -= parseResult;
1409
1410 /* Initiate re-initialization, if header has changed */
1411 if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
1412 &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1413 sscParse) == MPS_UNEQUAL_SSC) {
1414 pMpegSurroundDecoder
1415 ->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
1416 MPEGS_INIT_CHANGE_HEADER;
1417 SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
1418 /* We found a valid in-band configuration. Therefore any
1419 * previous config is invalid now. */
1420 pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 0;
1421 }
1422 }
1423 FDK_FALLTHROUGH;
1424 case MPEGS_ANCTYPE_FRAME:
1425
1426 if (pMpegSurroundDecoder
1427 ->initFlags[pMpegSurroundDecoder->bsFrameParse] &
1428 MPEGS_INIT_ERROR_PAYLOAD) {
1429 err = MPS_PARSE_ERROR;
1430 break;
1431 }
1432
1433 /* First spatial specific config is parsed into
1434 * spatialSpecificConfigBackup, second spatialSpecificConfigBackup
1435 * is copied into spatialSpecificConfig[bsFrameDecode] */
1436 if (pMpegSurroundDecoder
1437 ->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
1438 FDKmemcpy(sscParse,
1439 &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1440 sizeof(SPATIAL_SPECIFIC_CONFIG));
1441 pMpegSurroundDecoder
1442 ->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
1443 MPEGS_SYNC_FOUND;
1444 }
1445
1446 if (pMpegSurroundDecoder
1447 ->fOnSync[pMpegSurroundDecoder->bsFrameParse] >=
1448 MPEGS_SYNC_FOUND) {
1449 int nbits = 0, bitsAvail;
1450
1451 if (err != MPS_OK) {
1452 break;
1453 }
1454
1455 bitsAvail = FDKgetValidBits(hMpsBsData);
1456
1457 if (bitsAvail <= 0) {
1458 err = MPS_PARSE_ERROR;
1459 } else {
1460 err = SpatialDecParseFrameData(
1461 pMpegSurroundDecoder->pSpatialDec, bsFrame, hMpsBsData,
1462 sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
1463 fGlobalIndependencyFlag);
1464 if (err == MPS_OK) {
1465 bsFrame->newBsData = 1;
1466 }
1467 }
1468
1469 nbits = bitsAvail - (INT)FDKgetValidBits(hMpsBsData);
1470
1471 if ((nbits > bitsAvail) ||
1472 (nbits > pMpegSurroundDecoder->mpsDataBits) ||
1473 (pMpegSurroundDecoder->mpsDataBits > nbits + 7 &&
1474 !IS_LOWDELAY(coreCodec))) {
1475 bsFrame->newBsData = 0;
1476 err = MPS_PARSE_ERROR;
1477 break;
1478 }
1479 pMpegSurroundDecoder->mpsDataBits -= nbits;
1480 }
1481 break;
1482
1483 default: /* added to avoid compiler warning */
1484 err = MPS_NOTOK;
1485 break; /* added to avoid compiler warning */
1486 } /* switch (ancType) */
1487
1488 if (err == MPS_OK) {
1489 pMpegSurroundDecoder->ancStartStopPrev = ancStartStop;
1490 } else {
1491 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1492 MPEGS_INIT_ERROR_PAYLOAD,
1493 MPEGS_SYNC_LOST, MPEGS_STOP);
1494 pMpegSurroundDecoder->mpsDataBits = 0;
1495 }
1496 } /* (ancStartStop == MPEGS_STOP) || (ancStartStop == MPEGS_START_STOP)
1497 */
1498 } /* validAncStartStop */
1499 } /* validAncType */
1500 }
1501
1502 bail:
1503
1504 *pMpsDataBits -= (mpsBsBits - (INT)FDKgetValidBits(hBs));
1505
1506 return err;
1507 }
1508
mpegSurroundDecoder_Apply(CMpegSurroundDecoder * pMpegSurroundDecoder,INT_PCM * input,PCM_MPS * pTimeData,const int timeDataSize,int timeDataFrameSize,int * nChannels,int * frameSize,int sampleRate,AUDIO_OBJECT_TYPE coreCodec,AUDIO_CHANNEL_TYPE channelType[],UCHAR channelIndices[],const FDK_channelMapDescr * const mapDescr)1509 int mpegSurroundDecoder_Apply(CMpegSurroundDecoder *pMpegSurroundDecoder,
1510 INT_PCM *input, PCM_MPS *pTimeData,
1511 const int timeDataSize, int timeDataFrameSize,
1512 int *nChannels, int *frameSize, int sampleRate,
1513 AUDIO_OBJECT_TYPE coreCodec,
1514 AUDIO_CHANNEL_TYPE channelType[],
1515 UCHAR channelIndices[],
1516 const FDK_channelMapDescr *const mapDescr) {
1517 SACDEC_ERROR err = MPS_OK;
1518 PCM_MPS *pTimeOut = pTimeData;
1519 UINT initControlFlags = 0, controlFlags = 0;
1520 int timeDataRequiredSize = 0;
1521 int newData;
1522
1523 if (pMpegSurroundDecoder == NULL) {
1524 return MPS_INVALID_HANDLE;
1525 }
1526
1527 FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
1528
1529 if (!FDK_chMapDescr_isValid(mapDescr)) {
1530 return MPS_INVALID_HANDLE;
1531 }
1532
1533 if ((*nChannels <= 0) || (*nChannels > 2)) {
1534 return MPS_NOTOK;
1535 }
1536
1537 pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
1538 &pMpegSurroundDecoder
1539 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
1540 newData = pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
1541 .newBsData;
1542
1543 switch (mpegSurroundOperationMode(pMpegSurroundDecoder, 1000)) {
1544 case MPEGS_OPMODE_MPS_PAYLOAD:
1545 if (pMpegSurroundDecoder
1546 ->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
1547 err = initMpegSurroundDecoder(pMpegSurroundDecoder);
1548 }
1549
1550 if (err == MPS_OK) {
1551 if ((pMpegSurroundDecoder
1552 ->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
1553 MPEGS_SYNC_COMPLETE) &&
1554 (pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
1555 .bsIndependencyFlag == 1)) {
1556 /* We got a valid header and independently decodeable frame data.
1557 -> Go to the next sync level and start processing. */
1558 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1559 MPEGS_SYNC_COMPLETE;
1560 }
1561 } else {
1562 /* We got a valid config header but found an error while parsing the
1563 bitstream. Wait for the next independent frame and apply error
1564 conealment in the meantime. */
1565 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1566 MPEGS_SYNC_FOUND;
1567 controlFlags |= MPEGS_CONCEAL;
1568 err = MPS_OK;
1569 }
1570 /*
1571 Concealment:
1572 - Bitstream is available, no sync found during bitstream processing
1573 - Bitstream is available, sync lost due to corrupted bitstream
1574 - Bitstream is available, sync found but no independent frame
1575 */
1576 if (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
1577 MPEGS_SYNC_COMPLETE) {
1578 controlFlags |= MPEGS_CONCEAL;
1579 }
1580 break;
1581
1582 case MPEGS_OPMODE_NO_MPS_PAYLOAD:
1583 /* Concealment: No bitstream is available */
1584 controlFlags |= MPEGS_CONCEAL;
1585 break;
1586
1587 default:
1588 err = MPS_NOTOK;
1589 }
1590
1591 if (err != MPS_OK) {
1592 goto bail;
1593 }
1594
1595 /*
1596 * Force BypassMode if choosen by user
1597 */
1598 if (pMpegSurroundDecoder->mpegSurroundUserParams.bypassMode) {
1599 controlFlags |= MPEGS_BYPASSMODE;
1600 }
1601
1602 if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
1603 int startWithDfltCfg = 0;
1604 /*
1605 * Init with a default configuration if we came here and are still not
1606 * initialized.
1607 */
1608 if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] &
1609 MPEGS_INIT_ENFORCE_REINIT) {
1610 /* Get default spatial specific config */
1611 if (FDK_SpatialDecInitDefaultSpatialSpecificConfig(
1612 &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
1613 *nChannels, sampleRate,
1614 *frameSize /
1615 mpegSurroundDecoder_GetNrOfQmfBands(NULL, sampleRate),
1616 pMpegSurroundDecoder->mpegSurroundDecoderLevel,
1617 pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable)) {
1618 err = MPS_NOTOK;
1619 goto bail;
1620 }
1621
1622 /* Initiate re-initialization, if header has changed */
1623 if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
1624 &pMpegSurroundDecoder->spatialSpecificConfigBackup,
1625 &pMpegSurroundDecoder->spatialSpecificConfig
1626 [pMpegSurroundDecoder->bsFrameDecode]) == MPS_UNEQUAL_SSC) {
1627 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1628 MPEGS_INIT_CHANGE_HEADER;
1629 SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
1630 }
1631
1632 startWithDfltCfg = 1;
1633 }
1634
1635 /* First spatial specific config is parsed into spatialSpecificConfigBackup,
1636 * second spatialSpecificConfigBackup is copied into spatialSpecificConfig
1637 */
1638 err = initMpegSurroundDecoder(pMpegSurroundDecoder);
1639
1640 if (startWithDfltCfg) {
1641 /* initialized with default config, but no sync found */
1642 /* maybe use updateMpegSurroundDecoderStatus later on */
1643 pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
1644 MPEGS_SYNC_LOST;
1645 }
1646
1647 /* Since we do not have state MPEGS_SYNC_COMPLETE apply concealment */
1648 controlFlags |= MPEGS_CONCEAL;
1649
1650 if (err != MPS_OK) {
1651 goto bail;
1652 }
1653 }
1654
1655 /*
1656 * Process MPEG Surround Audio
1657 */
1658 initControlFlags = controlFlags;
1659
1660 /* Check that provided output buffer is large enough. */
1661 if (pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis == 0) {
1662 err = MPS_UNSUPPORTED_FORMAT;
1663 goto bail;
1664 }
1665 timeDataRequiredSize =
1666 (timeDataFrameSize *
1667 pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT *
1668 pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
1669 pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
1670 if (timeDataSize < timeDataRequiredSize) {
1671 err = MPS_OUTPUT_BUFFER_TOO_SMALL;
1672 goto bail;
1673 }
1674
1675 if ((pMpegSurroundDecoder->pSpatialDec->pConfigCurrent->syntaxFlags &
1676 SACDEC_SYNTAX_USAC) &&
1677 (pMpegSurroundDecoder->pSpatialDec->stereoConfigIndex > 1)) {
1678 FDK_ASSERT(timeDataRequiredSize >= timeDataFrameSize * *nChannels);
1679 /* Place samples comprising QMF time slots spaced at QMF output Band raster
1680 * to allow slot wise processing */
1681 int timeDataFrameSizeOut =
1682 (timeDataFrameSize *
1683 pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
1684 pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
1685 pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput =
1686 pTimeData + timeDataFrameSizeOut - timeDataFrameSize;
1687 for (int i = *nChannels - 1; i >= 0; i--) {
1688 FDKmemmove(pTimeData + (i + 1) * timeDataFrameSizeOut - timeDataFrameSize,
1689 pTimeData + timeDataFrameSize * i,
1690 sizeof(PCM_MPS) * timeDataFrameSize);
1691 FDKmemclear(pTimeData + i * timeDataFrameSizeOut,
1692 sizeof(PCM_MPS) * (timeDataFrameSizeOut - timeDataFrameSize));
1693 }
1694 } else {
1695 if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface) {
1696 FDKmemcpy(input, pTimeData,
1697 sizeof(INT_PCM) * (*nChannels) * (*frameSize));
1698 pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput = input;
1699 }
1700 }
1701
1702 /*
1703 * Process MPEG Surround Audio
1704 */
1705 err = SpatialDecApplyFrame(
1706 pMpegSurroundDecoder->pSpatialDec,
1707 &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
1708 pMpegSurroundDecoder->mpegSurroundUseTimeInterface ? INPUTMODE_TIME
1709 : INPUTMODE_QMF_SBR,
1710 pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput, NULL, NULL,
1711 pTimeOut, *frameSize, &controlFlags, *nChannels, mapDescr);
1712 *nChannels = pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT;
1713
1714 if (err !=
1715 MPS_OK) { /* A fatal error occured. Go back to start and try again: */
1716 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1717 MPEGS_INIT_ENFORCE_REINIT, MPEGS_SYNC_LOST,
1718 MPEGS_STOP);
1719 *frameSize =
1720 0; /* Declare that framework can not use the data in pTimeOut. */
1721 } else {
1722 if (((controlFlags & MPEGS_CONCEAL) &&
1723 !(initControlFlags & MPEGS_CONCEAL)) ||
1724 (pMpegSurroundDecoder->pSpatialDec->errInt !=
1725 MPS_OK)) { /* Account for errors that occured in
1726 SpatialDecApplyFrame(): */
1727 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1728 MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
1729 MPEGS_STOP);
1730 }
1731 }
1732
1733 if ((err == MPS_OK) && !(controlFlags & MPEGS_BYPASSMODE) &&
1734 !(pMpegSurroundDecoder->upmixType == UPMIX_TYPE_BYPASS)) {
1735 SpatialDecChannelProperties(pMpegSurroundDecoder->pSpatialDec, channelType,
1736 channelIndices, mapDescr);
1737 }
1738
1739 bail:
1740
1741 if (newData) {
1742 /* numParameterSetsPrev shall only be read in the decode process, because of
1743 that we can update this state variable here */
1744 pMpegSurroundDecoder->pSpatialDec->numParameterSetsPrev =
1745 pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
1746 .numParameterSets;
1747 }
1748
1749 return (err);
1750 }
1751
1752 /**
1753 * \brief Free config dependent MPEG Surround memory.
1754 **/
mpegSurroundDecoder_FreeMem(CMpegSurroundDecoder * pMpegSurroundDecoder)1755 SACDEC_ERROR mpegSurroundDecoder_FreeMem(
1756 CMpegSurroundDecoder *pMpegSurroundDecoder) {
1757 SACDEC_ERROR err = MPS_OK;
1758
1759 if (pMpegSurroundDecoder != NULL) {
1760 FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
1761 pMpegSurroundDecoder->pSpatialDec = NULL;
1762 }
1763
1764 return err;
1765 }
1766
1767 /**
1768 * \brief Close MPEG Surround decoder.
1769 **/
mpegSurroundDecoder_Close(CMpegSurroundDecoder * pMpegSurroundDecoder)1770 void mpegSurroundDecoder_Close(CMpegSurroundDecoder *pMpegSurroundDecoder) {
1771 if (pMpegSurroundDecoder != NULL) {
1772 FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
1773 pMpegSurroundDecoder->pSpatialDec = NULL;
1774
1775 for (int i = 0; i < 1; i++) {
1776 SpatialDecCloseBsFrame(&pMpegSurroundDecoder->bsFrames[i]);
1777 }
1778
1779 FDK_FREE_MEMORY_1D(pMpegSurroundDecoder);
1780 }
1781 }
1782
1783 #define SACDEC_VL0 2
1784 #define SACDEC_VL1 0
1785 #define SACDEC_VL2 0
1786
mpegSurroundDecoder_GetLibInfo(LIB_INFO * info)1787 int mpegSurroundDecoder_GetLibInfo(LIB_INFO *info) {
1788 int i;
1789
1790 if (info == NULL) {
1791 return -1;
1792 }
1793
1794 /* search for next free tab */
1795 for (i = 0; i < FDK_MODULE_LAST; i++) {
1796 if (info[i].module_id == FDK_NONE) break;
1797 }
1798 if (i == FDK_MODULE_LAST) return -1;
1799
1800 info += i;
1801
1802 info->module_id = FDK_MPSDEC;
1803 #ifdef __ANDROID__
1804 info->build_date = "";
1805 info->build_time = "";
1806 #else
1807 info->build_date = __DATE__;
1808 info->build_time = __TIME__;
1809 #endif
1810 info->title = "MPEG Surround Decoder";
1811 info->version = LIB_VERSION(SACDEC_VL0, SACDEC_VL1, SACDEC_VL2);
1812 LIB_VERSION_STRING(info);
1813 info->flags = 0 | CAPF_MPS_LD | CAPF_MPS_USAC | CAPF_MPS_HQ |
1814 CAPF_MPS_1CH_IN | CAPF_MPS_2CH_OUT; /* end flags */
1815
1816 return 0;
1817 }
1818
mpegSurroundDecoder_SetParam(CMpegSurroundDecoder * pMpegSurroundDecoder,const SACDEC_PARAM param,const INT value)1819 SACDEC_ERROR mpegSurroundDecoder_SetParam(
1820 CMpegSurroundDecoder *pMpegSurroundDecoder, const SACDEC_PARAM param,
1821 const INT value) {
1822 SACDEC_ERROR err = MPS_OK;
1823 SPATIALDEC_PARAM *pUserParams = NULL;
1824
1825 /* check decoder handle */
1826 if (pMpegSurroundDecoder != NULL) {
1827 /* init local shortcuts */
1828 pUserParams = &pMpegSurroundDecoder->mpegSurroundUserParams;
1829 } else {
1830 err = MPS_INVALID_HANDLE;
1831 /* check the parameter values before exiting. */
1832 }
1833
1834 /* apply param value */
1835 switch (param) {
1836 case SACDEC_OUTPUT_MODE:
1837 switch ((SAC_DEC_OUTPUT_MODE)value) {
1838 case SACDEC_OUT_MODE_NORMAL:
1839 case SACDEC_OUT_MODE_STEREO:
1840 break;
1841 default:
1842 err = MPS_INVALID_PARAMETER;
1843 }
1844 if (err == MPS_OK) {
1845 if (0) {
1846 err = MPS_INVALID_PARAMETER;
1847 } else if (pUserParams->outputMode != (UCHAR)value) {
1848 pUserParams->outputMode = (UCHAR)value;
1849 pMpegSurroundDecoder
1850 ->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1851 MPEGS_INIT_CHANGE_OUTPUT_MODE;
1852 }
1853 }
1854 break;
1855
1856 case SACDEC_INTERFACE:
1857 if (value < 0 || value > 1) {
1858 err = MPS_INVALID_PARAMETER;
1859 }
1860 if (err != MPS_OK) {
1861 goto bail;
1862 }
1863 if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface != (UCHAR)value) {
1864 pMpegSurroundDecoder->mpegSurroundUseTimeInterface = (UCHAR)value;
1865 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1866 MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE;
1867 }
1868 break;
1869
1870 case SACDEC_BS_INTERRUPTION:
1871 if ((err == MPS_OK) && (value != 0)) {
1872 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1873 MPEGS_INIT_BS_INTERRUPTION,
1874 MPEGS_SYNC_LOST, MPEGS_STOP);
1875 }
1876 break;
1877
1878 case SACDEC_CLEAR_HISTORY:
1879 if ((err == MPS_OK) && (value != 0)) {
1880 /* Just reset the states and go on. */
1881 updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
1882 MPEGS_INIT_CLEAR_HISTORY,
1883 MPEGS_SYNC_LOST, MPEGS_STOP);
1884 }
1885 break;
1886
1887 case SACDEC_CONCEAL_NUM_KEEP_FRAMES:
1888 if (value < 0) { /* Check valid value range */
1889 err = MPS_INVALID_PARAMETER;
1890 }
1891 if (err != MPS_OK) {
1892 goto bail;
1893 }
1894 if (pUserParams->concealNumKeepFrames != (UINT)value) {
1895 pUserParams->concealNumKeepFrames = (UINT)value;
1896 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1897 MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1898 }
1899 break;
1900
1901 case SACDEC_CONCEAL_FADE_OUT_SLOPE_LENGTH:
1902 if (value < 0) { /* Check valid value range */
1903 err = MPS_INVALID_PARAMETER;
1904 }
1905 if (err != MPS_OK) {
1906 goto bail;
1907 }
1908 if (pUserParams->concealFadeOutSlopeLength != (UINT)value) {
1909 pUserParams->concealFadeOutSlopeLength = (UINT)value;
1910 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1911 MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1912 }
1913 break;
1914
1915 case SACDEC_CONCEAL_FADE_IN_SLOPE_LENGTH:
1916 if (value < 0) { /* Check valid value range */
1917 err = MPS_INVALID_PARAMETER;
1918 }
1919 if (err != MPS_OK) {
1920 goto bail;
1921 }
1922 if (pUserParams->concealFadeInSlopeLength != (UINT)value) {
1923 pUserParams->concealFadeInSlopeLength = (UINT)value;
1924 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1925 MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1926 }
1927 break;
1928
1929 case SACDEC_CONCEAL_NUM_RELEASE_FRAMES:
1930 if (value < 0) { /* Check valid value range */
1931 err = MPS_INVALID_PARAMETER;
1932 }
1933 if (err != MPS_OK) {
1934 goto bail;
1935 }
1936 if (pUserParams->concealNumReleaseFrames != (UINT)value) {
1937 pUserParams->concealNumReleaseFrames = (UINT)value;
1938 pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
1939 MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
1940 }
1941 break;
1942
1943 default:
1944 err = MPS_INVALID_PARAMETER;
1945 break;
1946 } /* switch(param) */
1947
1948 bail:
1949 return err;
1950 }
1951
mpegSurroundDecoder_IsPseudoLR(CMpegSurroundDecoder * pMpegSurroundDecoder,int * bsPseudoLr)1952 SACDEC_ERROR mpegSurroundDecoder_IsPseudoLR(
1953 CMpegSurroundDecoder *pMpegSurroundDecoder, int *bsPseudoLr) {
1954 if (pMpegSurroundDecoder != NULL) {
1955 const SPATIAL_SPECIFIC_CONFIG *sscDecode =
1956 &pMpegSurroundDecoder
1957 ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
1958 *bsPseudoLr = (int)sscDecode->bsPseudoLr;
1959 return MPS_OK;
1960 } else
1961 return MPS_INVALID_HANDLE;
1962 }
1963
1964 /**
1965 * \brief Get the signal delay caused by the MPEG Surround decoder module.
1966 **/
mpegSurroundDecoder_GetDelay(const CMpegSurroundDecoder * self)1967 UINT mpegSurroundDecoder_GetDelay(const CMpegSurroundDecoder *self) {
1968 INT outputDelay = 0;
1969
1970 if (self != NULL) {
1971 const SPATIAL_SPECIFIC_CONFIG *sscDecode =
1972 &self->spatialSpecificConfig[self->bsFrameDecode];
1973 AUDIO_OBJECT_TYPE coreCodec = sscDecode->coreCodec;
1974
1975 /* See chapter 4.5 (delay and synchronization) of ISO/IEC FDIS 23003-1 and
1976 chapter 5.4.3 of ISO/IEC FDIS 23003-2 for details on the following
1977 figures. */
1978
1979 if (coreCodec > AOT_NULL_OBJECT) {
1980 if (IS_LOWDELAY(coreCodec)) {
1981 /* All low delay variants (ER-AAC-(E)LD): */
1982 outputDelay += 256;
1983 } else if (!IS_USAC(coreCodec)) {
1984 /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...)
1985 * branch: */
1986 outputDelay += 320 + 257; /* cos to exp delay + QMF synthesis */
1987 if (self->mpegSurroundUseTimeInterface) {
1988 outputDelay += 320 + 384; /* QMF and hybrid analysis */
1989 }
1990 }
1991 }
1992 }
1993
1994 return (outputDelay);
1995 }
1996