1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4 © Copyright 1995 - 2020 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 Dec bitstream decoder
100
101 *******************************************************************************/
102
103 #include "sac_bitdec.h"
104
105 #include "sac_dec_errorcodes.h"
106 #include "nlc_dec.h"
107 #include "sac_rom.h"
108 #include "FDK_matrixCalloc.h"
109 #include "sac_tsd.h"
110
111 enum {
112 ottVsTotInactiv = 0,
113 ottVsTotDb1Activ = 1,
114 ottVsTotDb2Activ = 2,
115 ottVsTotDb1Db2Activ = 3
116 };
117
SpatialDecDecodeHelperInfo(SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,UPMIXTYPE upmixType)118 static SACDEC_ERROR SpatialDecDecodeHelperInfo(
119 SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, UPMIXTYPE upmixType) {
120 int i;
121 UINT syntaxFlags;
122
123 /* Determine bit stream syntax */
124 syntaxFlags = 0;
125 switch (pSpatialSpecificConfig->coreCodec) {
126 case AOT_ER_AAC_ELD:
127 case AOT_ER_AAC_LD:
128 syntaxFlags |= SACDEC_SYNTAX_LD;
129 break;
130 case AOT_USAC:
131 syntaxFlags |= SACDEC_SYNTAX_USAC;
132 break;
133 case AOT_NONE:
134 default:
135 return MPS_UNSUPPORTED_FORMAT;
136 }
137
138 pSpatialSpecificConfig->syntaxFlags = syntaxFlags;
139
140 switch (pSpatialSpecificConfig->treeConfig) {
141 case TREE_212: {
142 pSpatialSpecificConfig->ottCLDdefault[0] = 0;
143 } break;
144 default:
145 return MPS_INVALID_TREECONFIG;
146 }
147
148 if (syntaxFlags & SACDEC_SYNTAX_USAC) {
149 if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
150 pSpatialSpecificConfig->numOttBandsIPD =
151 pSpatialSpecificConfig->bsOttBandsPhase;
152 } else {
153 int numParameterBands;
154
155 numParameterBands = pSpatialSpecificConfig->freqRes;
156 switch (numParameterBands) {
157 case 4:
158 case 5:
159 pSpatialSpecificConfig->numOttBandsIPD = 2;
160 break;
161 case 7:
162 pSpatialSpecificConfig->numOttBandsIPD = 3;
163 break;
164 case 10:
165 pSpatialSpecificConfig->numOttBandsIPD = 5;
166 break;
167 case 14:
168 pSpatialSpecificConfig->numOttBandsIPD = 7;
169 break;
170 case 20:
171 case 28:
172 pSpatialSpecificConfig->numOttBandsIPD = 10;
173 break;
174 default:
175 return MPS_INVALID_PARAMETERBANDS;
176 }
177 }
178 } else {
179 pSpatialSpecificConfig->numOttBandsIPD = 0;
180 }
181 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
182 {
183 pSpatialSpecificConfig->bitstreamOttBands[i] =
184 pSpatialSpecificConfig->freqRes;
185 }
186 {
187 pSpatialSpecificConfig->numOttBands[i] =
188 pSpatialSpecificConfig->bitstreamOttBands[i];
189 if (syntaxFlags & SACDEC_SYNTAX_USAC &&
190 !pSpatialSpecificConfig->bsOttBandsPhasePresent) {
191 if (pSpatialSpecificConfig->bResidualCoding &&
192 pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent &&
193 (pSpatialSpecificConfig->numOttBandsIPD <
194 pSpatialSpecificConfig->ResidualConfig[i].nResidualBands)) {
195 pSpatialSpecificConfig->numOttBandsIPD =
196 pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
197 }
198 }
199 }
200 } /* i */
201
202 return MPS_OK;
203 }
204
205 /*******************************************************************************
206 Functionname: SpatialDecParseExtensionConfig
207 *******************************************************************************
208
209 Description:
210
211 Arguments:
212
213 Return:
214
215 *******************************************************************************/
216
SpatialDecParseExtensionConfig(HANDLE_FDK_BITSTREAM bitstream,SPATIAL_SPECIFIC_CONFIG * config,int numOttBoxes,int numTttBoxes,int numOutChan,int bitsAvailable)217 static SACDEC_ERROR SpatialDecParseExtensionConfig(
218 HANDLE_FDK_BITSTREAM bitstream, SPATIAL_SPECIFIC_CONFIG *config,
219 int numOttBoxes, int numTttBoxes, int numOutChan, int bitsAvailable) {
220 SACDEC_ERROR err = MPS_OK;
221 INT ba = bitsAvailable;
222
223 config->sacExtCnt = 0;
224 config->bResidualCoding = 0;
225
226 ba = fMin((int)FDKgetValidBits(bitstream), ba);
227
228 while ((ba >= 8) && (config->sacExtCnt < MAX_NUM_EXT_TYPES)) {
229 int bitsRead, nFillBits;
230 INT tmp;
231 UINT sacExtLen;
232
233 config->sacExtType[config->sacExtCnt] = FDKreadBits(bitstream, 4);
234 ba -= 4;
235
236 sacExtLen = FDKreadBits(bitstream, 4);
237 ba -= 4;
238
239 if (sacExtLen == 15) {
240 sacExtLen += FDKreadBits(bitstream, 8);
241 ba -= 8;
242 if (sacExtLen == 15 + 255) {
243 sacExtLen += FDKreadBits(bitstream, 16);
244 ba -= 16;
245 }
246 }
247
248 tmp = (INT)FDKgetValidBits(
249 bitstream); /* Extension config payload start anchor. */
250 if ((tmp <= 0) || (tmp < (INT)sacExtLen * 8) || (ba < (INT)sacExtLen * 8)) {
251 err = MPS_PARSE_ERROR;
252 goto bail;
253 }
254
255 switch (config->sacExtType[config->sacExtCnt]) {
256 default:; /* unknown extension data => do nothing */
257 }
258
259 /* skip remaining extension data */
260 bitsRead = tmp - FDKgetValidBits(bitstream);
261 nFillBits = 8 * sacExtLen - bitsRead;
262
263 if (nFillBits < 0) {
264 err = MPS_PARSE_ERROR;
265 goto bail;
266 } else {
267 /* Skip fill bits or an unkown extension. */
268 FDKpushFor(bitstream, nFillBits);
269 }
270
271 ba -= 8 * sacExtLen;
272 config->sacExtCnt++;
273 }
274
275 bail:
276 return err;
277 }
278
SpatialDecParseSpecificConfigHeader(HANDLE_FDK_BITSTREAM bitstream,SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,AUDIO_OBJECT_TYPE coreCodec,SPATIAL_DEC_UPMIX_TYPE upmixType)279 SACDEC_ERROR SpatialDecParseSpecificConfigHeader(
280 HANDLE_FDK_BITSTREAM bitstream,
281 SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
282 AUDIO_OBJECT_TYPE coreCodec, SPATIAL_DEC_UPMIX_TYPE upmixType) {
283 SACDEC_ERROR err = MPS_OK;
284 INT numFillBits;
285 int sacHeaderLen = 0;
286 int sacTimeAlignFlag = 0;
287
288 sacTimeAlignFlag = FDKreadBits(bitstream, 1);
289 sacHeaderLen = FDKreadBits(bitstream, 7);
290
291 if (sacHeaderLen == 127) {
292 sacHeaderLen += FDKreadBits(bitstream, 16);
293 }
294 numFillBits = (INT)FDKgetValidBits(bitstream);
295
296 err = SpatialDecParseSpecificConfig(bitstream, pSpatialSpecificConfig,
297 sacHeaderLen, coreCodec);
298
299 numFillBits -=
300 (INT)FDKgetValidBits(bitstream); /* the number of read bits (tmpBits) */
301 numFillBits = (8 * sacHeaderLen) - numFillBits;
302 if (numFillBits < 0) {
303 /* Parsing went wrong */
304 err = MPS_PARSE_ERROR;
305 }
306 /* Move to the very end of the SSC */
307 FDKpushBiDirectional(bitstream, numFillBits);
308
309 if ((err == MPS_OK) && sacTimeAlignFlag) {
310 /* not supported */
311 FDKreadBits(bitstream, 16);
312 err = MPS_UNSUPPORTED_CONFIG;
313 }
314
315 /* Derive additional helper variables */
316 SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, (UPMIXTYPE)upmixType);
317
318 return err;
319 }
320
SpatialDecParseMps212Config(HANDLE_FDK_BITSTREAM bitstream,SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,int samplingRate,AUDIO_OBJECT_TYPE coreCodec,INT stereoConfigIndex,INT coreSbrFrameLengthIndex)321 SACDEC_ERROR SpatialDecParseMps212Config(
322 HANDLE_FDK_BITSTREAM bitstream,
323 SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, int samplingRate,
324 AUDIO_OBJECT_TYPE coreCodec, INT stereoConfigIndex,
325 INT coreSbrFrameLengthIndex) {
326 int i;
327
328 FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
329
330 pSpatialSpecificConfig->stereoConfigIndex = stereoConfigIndex;
331 pSpatialSpecificConfig->coreSbrFrameLengthIndex = coreSbrFrameLengthIndex;
332 pSpatialSpecificConfig->freqRes =
333 (SPATIALDEC_FREQ_RES)freqResTable[FDKreadBits(bitstream, 3)];
334 if (pSpatialSpecificConfig->freqRes == 0) {
335 return MPS_PARSE_ERROR; /* reserved value */
336 }
337
338 switch (coreCodec) {
339 case AOT_DRM_USAC:
340 pSpatialSpecificConfig->bsFixedGainDMX =
341 (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
342 /* tempShapeConfig = (bsTempShapeConfigDrm == 1) ? 3 : 0 */
343 pSpatialSpecificConfig->tempShapeConfig =
344 (SPATIALDEC_TS_CONF)(FDKreadBits(bitstream, 1) * 3);
345 pSpatialSpecificConfig->decorrConfig = (SPATIALDEC_DECORR_CONF)0;
346 pSpatialSpecificConfig->bsDecorrType = 0;
347 break;
348 case AOT_USAC:
349 pSpatialSpecificConfig->bsFixedGainDMX =
350 (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
351 pSpatialSpecificConfig->tempShapeConfig =
352 (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
353 pSpatialSpecificConfig->decorrConfig =
354 (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
355 if (pSpatialSpecificConfig->decorrConfig > 2) {
356 return MPS_PARSE_ERROR; /* reserved value */
357 }
358 pSpatialSpecificConfig->bsDecorrType = 0;
359 break;
360 default:
361 return MPS_UNSUPPORTED_FORMAT;
362 }
363 pSpatialSpecificConfig->nTimeSlots = (coreSbrFrameLengthIndex == 4) ? 64 : 32;
364 pSpatialSpecificConfig->bsHighRateMode = (UCHAR)FDKreadBits(bitstream, 1);
365
366 {
367 pSpatialSpecificConfig->bsPhaseCoding = (UCHAR)FDKreadBits(bitstream, 1);
368 pSpatialSpecificConfig->bsOttBandsPhasePresent =
369 (UCHAR)FDKreadBits(bitstream, 1);
370 if (pSpatialSpecificConfig->bsOttBandsPhasePresent) {
371 if (MAX_PARAMETER_BANDS < (pSpatialSpecificConfig->bsOttBandsPhase =
372 FDKreadBits(bitstream, 5))) {
373 return MPS_PARSE_ERROR;
374 }
375 } else {
376 pSpatialSpecificConfig->bsOttBandsPhase = 0;
377 }
378 }
379
380 if (stereoConfigIndex > 1) { /* do residual coding */
381 pSpatialSpecificConfig->bResidualCoding = 1;
382 pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 1;
383 if (pSpatialSpecificConfig->freqRes <
384 (pSpatialSpecificConfig->ResidualConfig->nResidualBands =
385 FDKreadBits(bitstream, 5))) {
386 return MPS_PARSE_ERROR;
387 }
388 pSpatialSpecificConfig->bsOttBandsPhase =
389 fMax(pSpatialSpecificConfig->bsOttBandsPhase,
390 pSpatialSpecificConfig->ResidualConfig->nResidualBands);
391 pSpatialSpecificConfig->bsPseudoLr = (UCHAR)FDKreadBits(bitstream, 1);
392
393 if (pSpatialSpecificConfig->bsPhaseCoding) {
394 pSpatialSpecificConfig->bsPhaseCoding = 3;
395 }
396 } else {
397 pSpatialSpecificConfig->bResidualCoding = 0;
398 pSpatialSpecificConfig->ResidualConfig->bResidualPresent = 0;
399 }
400
401 if (pSpatialSpecificConfig->tempShapeConfig == 2) {
402 switch (coreCodec) {
403 case AOT_USAC:
404 pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
405 break;
406 default: /* added to avoid compiler warning */
407 break; /* added to avoid compiler warning */
408 }
409 }
410
411 /* Static parameters */
412
413 pSpatialSpecificConfig->samplingFreq =
414 samplingRate; /* wrong for stereoConfigIndex == 3 but value is unused */
415 pSpatialSpecificConfig->treeConfig = SPATIALDEC_MODE_RSVD7;
416 pSpatialSpecificConfig->nOttBoxes =
417 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
418 pSpatialSpecificConfig->nInputChannels =
419 treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
420 pSpatialSpecificConfig->nOutputChannels =
421 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
422
423 pSpatialSpecificConfig->bArbitraryDownmix = 0;
424
425 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
426 pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
427 }
428
429 if (coreCodec == AOT_DRM_USAC) {
430 /* MPS payload is MPEG conform -> no need for pseudo DRM AOT */
431 coreCodec = AOT_USAC;
432 }
433 pSpatialSpecificConfig->coreCodec = coreCodec;
434
435 /* Derive additional helper variables */
436 SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
437
438 return MPS_OK;
439 }
440
SpatialDecParseSpecificConfig(HANDLE_FDK_BITSTREAM bitstream,SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,int sacHeaderLen,AUDIO_OBJECT_TYPE coreCodec)441 SACDEC_ERROR SpatialDecParseSpecificConfig(
442 HANDLE_FDK_BITSTREAM bitstream,
443 SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, int sacHeaderLen,
444 AUDIO_OBJECT_TYPE coreCodec) {
445 SACDEC_ERROR err = MPS_OK;
446 int i;
447 int bsSamplingFreqIndex;
448 int bsFreqRes, b3DaudioMode = 0;
449 int numHeaderBits;
450 int cfgStartPos, bitsAvailable;
451
452 FDKmemclear(pSpatialSpecificConfig, sizeof(SPATIAL_SPECIFIC_CONFIG));
453
454 cfgStartPos = FDKgetValidBits(bitstream);
455 /* It might be that we do not know the SSC length beforehand. */
456 if (sacHeaderLen == 0) {
457 bitsAvailable = cfgStartPos;
458 } else {
459 bitsAvailable = 8 * sacHeaderLen;
460 if (bitsAvailable > cfgStartPos) {
461 err = MPS_PARSE_ERROR;
462 goto bail;
463 }
464 }
465
466 bsSamplingFreqIndex = FDKreadBits(bitstream, 4);
467
468 if (bsSamplingFreqIndex == 15) {
469 pSpatialSpecificConfig->samplingFreq = FDKreadBits(bitstream, 24);
470 } else {
471 pSpatialSpecificConfig->samplingFreq =
472 samplingFreqTable[bsSamplingFreqIndex];
473 if (pSpatialSpecificConfig->samplingFreq == 0) {
474 err = MPS_PARSE_ERROR;
475 goto bail;
476 }
477 }
478
479 pSpatialSpecificConfig->nTimeSlots = FDKreadBits(bitstream, 5) + 1;
480 if ((pSpatialSpecificConfig->nTimeSlots < 1) ||
481 (pSpatialSpecificConfig->nTimeSlots > MAX_TIME_SLOTS)) {
482 err = MPS_PARSE_ERROR;
483 goto bail;
484 }
485
486 bsFreqRes = FDKreadBits(bitstream, 3);
487
488 pSpatialSpecificConfig->freqRes =
489 (SPATIALDEC_FREQ_RES)freqResTable_LD[bsFreqRes];
490
491 {
492 UINT treeConfig = FDKreadBits(bitstream, 4);
493
494 switch (treeConfig) {
495 case SPATIALDEC_MODE_RSVD7:
496 pSpatialSpecificConfig->treeConfig = (SPATIALDEC_TREE_CONFIG)treeConfig;
497 break;
498 default:
499 err = MPS_UNSUPPORTED_CONFIG;
500 goto bail;
501 }
502 }
503
504 {
505 pSpatialSpecificConfig->nOttBoxes =
506 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
507 pSpatialSpecificConfig->nTttBoxes =
508 treePropertyTable[pSpatialSpecificConfig->treeConfig].numTttBoxes;
509 pSpatialSpecificConfig->nInputChannels =
510 treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
511 pSpatialSpecificConfig->nOutputChannels =
512 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
513 }
514
515 pSpatialSpecificConfig->quantMode =
516 (SPATIALDEC_QUANT_MODE)FDKreadBits(bitstream, 2);
517
518 pSpatialSpecificConfig->bArbitraryDownmix = FDKreadBits(bitstream, 1);
519
520 pSpatialSpecificConfig->bsFixedGainDMX =
521 (SPATIALDEC_FIXED_GAINS)FDKreadBits(bitstream, 3);
522
523 pSpatialSpecificConfig->tempShapeConfig =
524 (SPATIALDEC_TS_CONF)FDKreadBits(bitstream, 2);
525 if (pSpatialSpecificConfig->tempShapeConfig > 2) {
526 return MPS_PARSE_ERROR; /* reserved value */
527 }
528
529 pSpatialSpecificConfig->decorrConfig =
530 (SPATIALDEC_DECORR_CONF)FDKreadBits(bitstream, 2);
531 if (pSpatialSpecificConfig->decorrConfig > 2) {
532 return MPS_PARSE_ERROR; /* reserved value */
533 }
534
535 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
536 pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
537 }
538
539 for (i = 0; i < pSpatialSpecificConfig->nTttBoxes; i++) {
540 int bTttDualMode = FDKreadBits(bitstream, 1);
541 FDKreadBits(bitstream, 3); /* not supported */
542
543 if (bTttDualMode) {
544 FDKreadBits(bitstream, 8); /* not supported */
545 }
546 }
547
548 if (pSpatialSpecificConfig->tempShapeConfig == 2) {
549 pSpatialSpecificConfig->envQuantMode = FDKreadBits(bitstream, 1);
550 }
551
552 if (b3DaudioMode) {
553 if (FDKreadBits(bitstream, 2) == 0) { /* b3DaudioHRTFset ? */
554 int hc;
555 int HRTFnumBand;
556 int HRTFfreqRes = FDKreadBits(bitstream, 3);
557 int HRTFnumChan = FDKreadBits(bitstream, 4);
558 int HRTFasymmetric = FDKreadBits(bitstream, 1);
559
560 HRTFnumBand = freqResTable_LD[HRTFfreqRes];
561
562 for (hc = 0; hc < HRTFnumChan; hc++) {
563 FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFlevelLeft[hc][hb] */
564 if (HRTFasymmetric) {
565 FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFlevelRight[hc][hb] */
566 }
567 if (FDKreadBits(bitstream, 1)) { /* HRTFphase[hc] ? */
568 FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFphaseLR[hc][hb] */
569 }
570 if (FDKreadBits(bitstream, 1)) { /* HRTFicc[hc] ? */
571 FDKpushFor(bitstream, HRTFnumBand * 6); /* HRTFiccLR[hc][hb] */
572 }
573 }
574 }
575 }
576
577 FDKbyteAlign(bitstream,
578 cfgStartPos); /* ISO/IEC FDIS 23003-1: 5.2. ... byte alignment
579 with respect to the beginning of the syntactic
580 element in which ByteAlign() occurs. */
581
582 numHeaderBits = cfgStartPos - (INT)FDKgetValidBits(bitstream);
583 bitsAvailable -= numHeaderBits;
584 if (bitsAvailable < 0) {
585 err = MPS_PARSE_ERROR;
586 goto bail;
587 }
588
589 pSpatialSpecificConfig->sacExtCnt = 0;
590 pSpatialSpecificConfig->bResidualCoding = 0;
591
592 err = SpatialDecParseExtensionConfig(
593 bitstream, pSpatialSpecificConfig, pSpatialSpecificConfig->nOttBoxes,
594 pSpatialSpecificConfig->nTttBoxes,
595 pSpatialSpecificConfig->nOutputChannels, bitsAvailable);
596
597 FDKbyteAlign(
598 bitstream,
599 cfgStartPos); /* Same alignment anchor as above because
600 SpatialExtensionConfig() always reads full bytes */
601
602 pSpatialSpecificConfig->coreCodec = coreCodec;
603
604 SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
605
606 bail:
607 if (sacHeaderLen > 0) {
608 /* If the config is of known length then assure that the
609 bitbuffer is exactly at its end when leaving the function. */
610 FDKpushBiDirectional(
611 bitstream,
612 (sacHeaderLen * 8) - (cfgStartPos - (INT)FDKgetValidBits(bitstream)));
613 }
614
615 return err;
616 }
617
SpatialDecDefaultSpecificConfig(SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,AUDIO_OBJECT_TYPE coreCodec,int samplingFreq,int nTimeSlots,int sacDecoderLevel,int isBlind,int numCoreChannels)618 int SpatialDecDefaultSpecificConfig(
619 SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig,
620 AUDIO_OBJECT_TYPE coreCodec, int samplingFreq, int nTimeSlots,
621 int sacDecoderLevel, int isBlind, int numCoreChannels)
622
623 {
624 int err = MPS_OK;
625 int i;
626
627 FDK_ASSERT(coreCodec != AOT_NONE);
628 FDK_ASSERT(nTimeSlots > 0);
629 FDK_ASSERT(samplingFreq > 0);
630
631 pSpatialSpecificConfig->coreCodec = coreCodec;
632 pSpatialSpecificConfig->samplingFreq = samplingFreq;
633 pSpatialSpecificConfig->nTimeSlots = nTimeSlots;
634 if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
635 (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
636 pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_23;
637 else
638 pSpatialSpecificConfig->freqRes = SPATIALDEC_FREQ_RES_28;
639
640 {
641 pSpatialSpecificConfig->treeConfig =
642 SPATIALDEC_MODE_RSVD7; /* 212 configuration */
643 }
644
645 {
646 pSpatialSpecificConfig->nOttBoxes =
647 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOttBoxes;
648 pSpatialSpecificConfig->nInputChannels =
649 treePropertyTable[pSpatialSpecificConfig->treeConfig].numInputChannels;
650 pSpatialSpecificConfig->nOutputChannels =
651 treePropertyTable[pSpatialSpecificConfig->treeConfig].numOutputChannels;
652 }
653
654 pSpatialSpecificConfig->quantMode = SPATIALDEC_QUANT_FINE_DEF;
655 pSpatialSpecificConfig->bArbitraryDownmix = 0;
656 pSpatialSpecificConfig->bResidualCoding = 0;
657 if ((pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_ELD) ||
658 (pSpatialSpecificConfig->coreCodec == AOT_ER_AAC_LD))
659 pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_RSVD2;
660 else
661 pSpatialSpecificConfig->bsFixedGainDMX = SPATIALDEC_GAIN_MODE0;
662
663 pSpatialSpecificConfig->tempShapeConfig = SPATIALDEC_TS_TPNOWHITE;
664 pSpatialSpecificConfig->decorrConfig = SPATIALDEC_DECORR_MODE0;
665
666 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
667 pSpatialSpecificConfig->OttConfig[i].nOttBands = 0;
668 }
669
670 return err;
671 }
672
673 /*******************************************************************************
674 Functionname: coarse2fine
675 *******************************************************************************
676
677 Description:
678 Parameter index mapping from coarse to fine quantization
679
680 Arguments:
681
682 Input:
683
684 Output:
685
686 *******************************************************************************/
coarse2fine(SCHAR * data,DATA_TYPE dataType,int startBand,int numBands)687 static void coarse2fine(SCHAR *data, DATA_TYPE dataType, int startBand,
688 int numBands) {
689 int i;
690
691 for (i = startBand; i < startBand + numBands; i++) {
692 data[i] <<= 1;
693 }
694
695 if (dataType == t_CLD) {
696 for (i = startBand; i < startBand + numBands; i++) {
697 if (data[i] == -14)
698 data[i] = -15;
699 else if (data[i] == 14)
700 data[i] = 15;
701 }
702 }
703 }
704
705 /*******************************************************************************
706 Functionname: fine2coarse
707 *******************************************************************************
708
709 Description:
710 Parameter index mapping from fine to coarse quantization
711
712 Arguments:
713
714 Input:
715
716 Output:
717
718 *******************************************************************************/
fine2coarse(SCHAR * data,DATA_TYPE dataType,int startBand,int numBands)719 static void fine2coarse(SCHAR *data, DATA_TYPE dataType, int startBand,
720 int numBands) {
721 int i;
722
723 for (i = startBand; i < startBand + numBands; i++) {
724 /* Note: the if cases below actually make a difference (negative values) */
725 if (dataType == t_CLD)
726 data[i] /= 2;
727 else
728 data[i] >>= 1;
729 }
730 }
731
732 /*******************************************************************************
733 Functionname: getStrideMap
734 *******************************************************************************
735
736 Description:
737 Index Mapping accroding to pbStrides
738
739 Arguments:
740
741 Input:
742
743 Output:
744
745 *******************************************************************************/
getStrideMap(int freqResStride,int startBand,int stopBand,int * aStrides)746 static int getStrideMap(int freqResStride, int startBand, int stopBand,
747 int *aStrides) {
748 int i, pb, pbStride, dataBands, strOffset;
749
750 pbStride = pbStrideTable[freqResStride];
751 dataBands = (stopBand - startBand - 1) / pbStride + 1;
752
753 aStrides[0] = startBand;
754 for (pb = 1; pb <= dataBands; pb++) {
755 aStrides[pb] = aStrides[pb - 1] + pbStride;
756 }
757 strOffset = 0;
758 while (aStrides[dataBands] > stopBand) {
759 if (strOffset < dataBands) strOffset++;
760 for (i = strOffset; i <= dataBands; i++) {
761 aStrides[i]--;
762 }
763 }
764
765 return dataBands;
766 }
767
768 /*******************************************************************************
769 Functionname: ecDataDec
770 *******************************************************************************
771
772 Description:
773 Do delta decoding and dequantization
774
775 Arguments:
776
777 Input:
778
779 Output:
780
781
782 *******************************************************************************/
783
ecDataDec(const SPATIAL_BS_FRAME * frame,UINT syntaxFlags,HANDLE_FDK_BITSTREAM bitstream,LOSSLESSDATA * const llData,SCHAR (* data)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],SCHAR ** lastdata,int datatype,int boxIdx,int startBand,int stopBand,SCHAR defaultValue)784 static SACDEC_ERROR ecDataDec(
785 const SPATIAL_BS_FRAME *frame, UINT syntaxFlags,
786 HANDLE_FDK_BITSTREAM bitstream, LOSSLESSDATA *const llData,
787 SCHAR (*data)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS], SCHAR **lastdata,
788 int datatype, int boxIdx, int startBand, int stopBand, SCHAR defaultValue) {
789 SACDEC_ERROR err = MPS_OK;
790 int i, j, pb, dataSets, setIdx, bsDataPair, dataBands, oldQuantCoarseXXX;
791 INT aStrides[MAX_PARAMETER_BANDS + 1] = {0};
792
793 dataSets = 0;
794 for (i = 0; i < frame->numParameterSets; i++) {
795 llData->bsXXXDataMode[i] = (SCHAR)FDKreadBits(bitstream, 2);
796
797 if ((frame->bsIndependencyFlag == 1) && (i == 0) &&
798 (llData->bsXXXDataMode[i] == 1 ||
799 llData->bsXXXDataMode[i] == 2)) { /* This check catches bitstreams
800 generated by older encoder that
801 cause trouble */
802 return MPS_PARSE_ERROR;
803 }
804 if ((i >= frame->numParameterSets - 1) &&
805 (llData->bsXXXDataMode[i] ==
806 2)) { /* The interpolation mode must not be active for the last
807 parameter set */
808 return MPS_PARSE_ERROR;
809 }
810
811 if (llData->bsXXXDataMode[i] == 3) {
812 dataSets++;
813 }
814 }
815
816 setIdx = 0;
817 bsDataPair = 0;
818 oldQuantCoarseXXX = llData->state->bsQuantCoarseXXXprevParse;
819
820 for (i = 0; i < frame->numParameterSets; i++) {
821 if (llData->bsXXXDataMode[i] == 0) {
822 for (pb = startBand; pb < stopBand; pb++) {
823 lastdata[boxIdx][pb] = defaultValue;
824 }
825
826 oldQuantCoarseXXX = 0;
827 }
828
829 if (llData->bsXXXDataMode[i] == 3) {
830 if (bsDataPair) {
831 bsDataPair = 0;
832 } else {
833 bsDataPair = FDKreadBits(bitstream, 1);
834 llData->bsQuantCoarseXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 1);
835 llData->bsFreqResStrideXXX[setIdx] = (UCHAR)FDKreadBits(bitstream, 2);
836
837 if (llData->bsQuantCoarseXXX[setIdx] != oldQuantCoarseXXX) {
838 if (oldQuantCoarseXXX) {
839 coarse2fine(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
840 stopBand - startBand);
841 } else {
842 fine2coarse(lastdata[boxIdx], (DATA_TYPE)datatype, startBand,
843 stopBand - startBand);
844 }
845 }
846
847 dataBands = getStrideMap(llData->bsFreqResStrideXXX[setIdx], startBand,
848 stopBand, aStrides);
849
850 for (pb = 0; pb < dataBands; pb++) {
851 lastdata[boxIdx][startBand + pb] = lastdata[boxIdx][aStrides[pb]];
852 }
853
854 if (boxIdx > MAX_NUM_OTT) return MPS_INVALID_BOXIDX;
855 if ((setIdx + bsDataPair) > MAX_PARAMETER_SETS)
856 return MPS_INVALID_SETIDX;
857
858 /* DECODER_TYPE defined in FDK_tools */
859 DECODER_TYPE this_decoder_type = SAC_DECODER;
860 if (syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) {
861 this_decoder_type = USAC_DECODER;
862 } else if (syntaxFlags & SACDEC_SYNTAX_LD) {
863 this_decoder_type = SAOC_DECODER;
864 }
865
866 err = (SACDEC_ERROR)EcDataPairDec(
867 this_decoder_type, bitstream, data[boxIdx][setIdx + 0],
868 data[boxIdx][setIdx + 1], lastdata[boxIdx], (DATA_TYPE)datatype,
869 startBand, dataBands, bsDataPair, llData->bsQuantCoarseXXX[setIdx],
870 !(frame->bsIndependencyFlag && (i == 0)) || (setIdx > 0));
871 if (err != MPS_OK) goto bail;
872
873 if (datatype == t_IPD) {
874 const SCHAR mask = (llData->bsQuantCoarseXXX[setIdx]) ? 7 : 15;
875 for (pb = 0; pb < dataBands; pb++) {
876 for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
877 lastdata[boxIdx][j] =
878 data[boxIdx][setIdx + bsDataPair][startBand + pb] & mask;
879 }
880 }
881 } else {
882 for (pb = 0; pb < dataBands; pb++) {
883 for (j = aStrides[pb]; j < aStrides[pb + 1]; j++) {
884 lastdata[boxIdx][j] =
885 data[boxIdx][setIdx + bsDataPair][startBand + pb];
886 }
887 }
888 }
889
890 oldQuantCoarseXXX = llData->bsQuantCoarseXXX[setIdx];
891
892 if (bsDataPair) {
893 llData->bsQuantCoarseXXX[setIdx + 1] =
894 llData->bsQuantCoarseXXX[setIdx];
895 llData->bsFreqResStrideXXX[setIdx + 1] =
896 llData->bsFreqResStrideXXX[setIdx];
897 }
898 setIdx += bsDataPair + 1;
899 } /* !bsDataPair */
900 } /* llData->bsXXXDataMode[i] == 3 */
901 }
902
903 llData->state->bsQuantCoarseXXXprevParse = oldQuantCoarseXXX;
904
905 bail:
906 return err;
907 }
908
909 /*******************************************************************************
910 Functionname: parseArbitraryDownmixData
911 *******************************************************************************
912
913 Description:
914
915 Arguments:
916
917 Return:
918
919 *******************************************************************************/
parseArbitraryDownmixData(spatialDec * self,const SPATIAL_SPECIFIC_CONFIG * pSSC,const UINT syntaxFlags,const SPATIAL_BS_FRAME * frame,HANDLE_FDK_BITSTREAM bitstream)920 static SACDEC_ERROR parseArbitraryDownmixData(
921 spatialDec *self, const SPATIAL_SPECIFIC_CONFIG *pSSC,
922 const UINT syntaxFlags, const SPATIAL_BS_FRAME *frame,
923 HANDLE_FDK_BITSTREAM bitstream) {
924 SACDEC_ERROR err = MPS_OK;
925 int ch;
926 int offset = pSSC->nOttBoxes;
927
928 /* CLD (arbitrary down-mix gains) */
929 for (ch = 0; ch < pSSC->nInputChannels; ch++) {
930 err = ecDataDec(frame, syntaxFlags, bitstream,
931 &frame->CLDLosslessData[offset + ch],
932 frame->cmpArbdmxGainIdx, self->cmpArbdmxGainIdxPrev, t_CLD,
933 ch, 0, pSSC->freqRes, arbdmxGainDefault);
934 if (err != MPS_OK) return err;
935 }
936
937 return err;
938
939 } /* parseArbitraryDownmixData */
940
941 /*******************************************************************************
942 Functionname: SpatialDecParseFrame
943 *******************************************************************************
944
945 Description:
946
947 Arguments:
948
949 Input:
950
951 Output:
952
953 *******************************************************************************/
954
nBitsParamSlot(int i)955 static int nBitsParamSlot(int i) {
956 int bitsParamSlot;
957
958 bitsParamSlot = fMax(0, DFRACT_BITS - 1 - fNormz((FIXP_DBL)i));
959 if ((1 << bitsParamSlot) < i) {
960 bitsParamSlot++;
961 }
962 FDK_ASSERT((bitsParamSlot >= 0) && (bitsParamSlot <= 32));
963
964 return bitsParamSlot;
965 }
966
SpatialDecParseFrameData(spatialDec_struct * self,SPATIAL_BS_FRAME * frame,HANDLE_FDK_BITSTREAM bitstream,const SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig,UPMIXTYPE upmixType,int fGlobalIndependencyFlag)967 SACDEC_ERROR SpatialDecParseFrameData(
968 spatialDec_struct *self, SPATIAL_BS_FRAME *frame,
969 HANDLE_FDK_BITSTREAM bitstream,
970 const SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig, UPMIXTYPE upmixType,
971 int fGlobalIndependencyFlag) {
972 SACDEC_ERROR err = MPS_OK;
973 int bsFramingType, dataBands, ps, pg, i;
974 int pb;
975 int numTempShapeChan = 0;
976 int bsNumOutputChannels =
977 treePropertyTable[pSpatialSpecificConfig->treeConfig]
978 .numOutputChannels; /* CAUTION: Maybe different to
979 pSpatialSpecificConfig->treeConfig in some
980 modes! */
981 int paramSetErr = 0;
982 UINT alignAnchor = FDKgetValidBits(
983 bitstream); /* Anchor for ByteAlign() function. See comment below. */
984 UINT syntaxFlags;
985
986 syntaxFlags = pSpatialSpecificConfig->syntaxFlags;
987
988 if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
989 pSpatialSpecificConfig->bsHighRateMode == 0) {
990 bsFramingType = 0; /* fixed framing */
991 frame->numParameterSets = 1;
992 } else {
993 bsFramingType = FDKreadBits(bitstream, 1);
994 if (syntaxFlags & SACDEC_SYNTAX_LD)
995 frame->numParameterSets = FDKreadBits(bitstream, 1) + 1;
996 else
997 frame->numParameterSets = FDKreadBits(bitstream, 3) + 1;
998 }
999
1000 /* Any error after this line shall trigger parameter invalidation at bail
1001 * label. */
1002 paramSetErr = 1;
1003
1004 if (frame->numParameterSets >= MAX_PARAMETER_SETS) {
1005 goto bail;
1006 }
1007
1008 /* Basic config check. */
1009 if (pSpatialSpecificConfig->nInputChannels <= 0 ||
1010 pSpatialSpecificConfig->nOutputChannels <= 0) {
1011 err = MPS_UNSUPPORTED_CONFIG;
1012 goto bail;
1013 }
1014
1015 if (bsFramingType) {
1016 int prevParamSlot = -1;
1017 int bitsParamSlot;
1018
1019 {
1020 bitsParamSlot = nBitsParamSlot(pSpatialSpecificConfig->nTimeSlots);
1021
1022 for (i = 0; i < frame->numParameterSets; i++) {
1023 frame->paramSlot[i] = FDKreadBits(bitstream, bitsParamSlot);
1024 /* Sanity check */
1025 if ((frame->paramSlot[i] <= prevParamSlot) ||
1026 (frame->paramSlot[i] >= pSpatialSpecificConfig->nTimeSlots)) {
1027 err = MPS_PARSE_ERROR;
1028 goto bail;
1029 }
1030 prevParamSlot = frame->paramSlot[i];
1031 }
1032 }
1033 } else {
1034 for (i = 0; i < frame->numParameterSets; i++) {
1035 frame->paramSlot[i] = ((pSpatialSpecificConfig->nTimeSlots * (i + 1)) /
1036 frame->numParameterSets) -
1037 1;
1038 }
1039 }
1040
1041 if ((syntaxFlags & (SACDEC_SYNTAX_USAC | SACDEC_SYNTAX_RSVD50)) &&
1042 fGlobalIndependencyFlag) {
1043 frame->bsIndependencyFlag = 1;
1044 } else {
1045 frame->bsIndependencyFlag = (UCHAR)FDKreadBits(bitstream, 1);
1046 }
1047
1048 /*
1049 * OttData()
1050 */
1051 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1052 err = ecDataDec(frame, syntaxFlags, bitstream, &frame->CLDLosslessData[i],
1053 frame->cmpOttCLDidx, self->cmpOttCLDidxPrev, t_CLD, i, 0,
1054 pSpatialSpecificConfig->bitstreamOttBands[i],
1055 pSpatialSpecificConfig->ottCLDdefault[i]);
1056 if (err != MPS_OK) {
1057 goto bail;
1058 }
1059 } /* i < numOttBoxes */
1060
1061 {
1062 for (i = 0; i < pSpatialSpecificConfig->nOttBoxes; i++) {
1063 err = ecDataDec(frame, syntaxFlags, bitstream, &frame->ICCLosslessData[i],
1064 frame->cmpOttICCidx, self->cmpOttICCidxPrev, t_ICC, i, 0,
1065 pSpatialSpecificConfig->bitstreamOttBands[i], ICCdefault);
1066 if (err != MPS_OK) {
1067 goto bail;
1068 }
1069 } /* i < numOttBoxes */
1070 } /* !oneICC */
1071
1072 if ((pSpatialSpecificConfig->treeConfig == SPATIALDEC_MODE_RSVD7) &&
1073 (pSpatialSpecificConfig->bsPhaseCoding)) {
1074 frame->phaseMode = FDKreadBits(bitstream, 1);
1075
1076 if (frame->phaseMode == 0) {
1077 for (pb = 0; pb < pSpatialSpecificConfig->numOttBandsIPD; pb++) {
1078 self->cmpOttIPDidxPrev[0][pb] = 0;
1079 for (i = 0; i < frame->numParameterSets; i++) {
1080 frame->cmpOttIPDidx[0][i][pb] = 0;
1081 // frame->ottIPDidx[0][i][pb] = 0;
1082 }
1083 /* self->ottIPDidxPrev[0][pb] = 0; */
1084 }
1085 frame->OpdSmoothingMode = 0;
1086 } else {
1087 frame->OpdSmoothingMode = FDKreadBits(bitstream, 1);
1088 err = ecDataDec(frame, syntaxFlags, bitstream, &frame->IPDLosslessData[0],
1089 frame->cmpOttIPDidx, self->cmpOttIPDidxPrev, t_IPD, 0, 0,
1090 pSpatialSpecificConfig->numOttBandsIPD, IPDdefault);
1091 if (err != MPS_OK) {
1092 goto bail;
1093 }
1094 }
1095 }
1096
1097 /*
1098 * SmgData()
1099 */
1100
1101 {
1102 if (!pSpatialSpecificConfig->bsHighRateMode &&
1103 (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1104 for (ps = 0; ps < frame->numParameterSets; ps++) {
1105 frame->bsSmoothMode[ps] = 0;
1106 }
1107 } else {
1108 for (ps = 0; ps < frame->numParameterSets; ps++) {
1109 frame->bsSmoothMode[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1110 if (frame->bsSmoothMode[ps] >= 2) {
1111 frame->bsSmoothTime[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1112 }
1113 if (frame->bsSmoothMode[ps] == 3) {
1114 frame->bsFreqResStrideSmg[ps] = (UCHAR)FDKreadBits(bitstream, 2);
1115 dataBands = (pSpatialSpecificConfig->freqRes - 1) /
1116 pbStrideTable[frame->bsFreqResStrideSmg[ps]] +
1117 1;
1118 for (pg = 0; pg < dataBands; pg++) {
1119 frame->bsSmgData[ps][pg] = (UCHAR)FDKreadBits(bitstream, 1);
1120 }
1121 }
1122 } /* ps < numParameterSets */
1123 }
1124 }
1125
1126 /*
1127 * TempShapeData()
1128 */
1129 if ((pSpatialSpecificConfig->tempShapeConfig == 3) &&
1130 (syntaxFlags & SACDEC_SYNTAX_USAC)) {
1131 int TsdErr;
1132 TsdErr = TsdRead(bitstream, pSpatialSpecificConfig->nTimeSlots,
1133 &frame->TsdData[0]);
1134 if (TsdErr) {
1135 err = MPS_PARSE_ERROR;
1136 goto bail;
1137 }
1138 } else {
1139 frame->TsdData[0].bsTsdEnable = 0;
1140 }
1141
1142 for (i = 0; i < bsNumOutputChannels; i++) {
1143 frame->tempShapeEnableChannelSTP[i] = 0;
1144 frame->tempShapeEnableChannelGES[i] = 0;
1145 }
1146
1147 if ((pSpatialSpecificConfig->tempShapeConfig == 1) ||
1148 (pSpatialSpecificConfig->tempShapeConfig == 2)) {
1149 int bsTempShapeEnable = FDKreadBits(bitstream, 1);
1150 if (bsTempShapeEnable) {
1151 numTempShapeChan =
1152 tempShapeChanTable[pSpatialSpecificConfig->tempShapeConfig - 1]
1153 [pSpatialSpecificConfig->treeConfig];
1154 switch (pSpatialSpecificConfig->tempShapeConfig) {
1155 case 1: /* STP */
1156 for (i = 0; i < numTempShapeChan; i++) {
1157 int stpEnable = FDKreadBits(bitstream, 1);
1158 frame->tempShapeEnableChannelSTP[i] = stpEnable;
1159 }
1160 break;
1161 case 2: /* GES */
1162 {
1163 UCHAR gesChannelEnable[MAX_OUTPUT_CHANNELS];
1164
1165 for (i = 0; i < numTempShapeChan; i++) {
1166 gesChannelEnable[i] = (UCHAR)FDKreadBits(bitstream, 1);
1167 frame->tempShapeEnableChannelGES[i] = gesChannelEnable[i];
1168 }
1169 for (i = 0; i < numTempShapeChan; i++) {
1170 if (gesChannelEnable[i]) {
1171 int envShapeData_tmp[MAX_TIME_SLOTS];
1172 if (huff_dec_reshape(bitstream, envShapeData_tmp,
1173 pSpatialSpecificConfig->nTimeSlots) != 0) {
1174 err = MPS_PARSE_ERROR;
1175 goto bail;
1176 }
1177 for (int ts = 0; ts < pSpatialSpecificConfig->nTimeSlots; ts++) {
1178 if (!(envShapeData_tmp[ts] >= 0) &&
1179 (envShapeData_tmp[ts] <= 4)) {
1180 err = MPS_PARSE_ERROR;
1181 goto bail;
1182 }
1183 frame->bsEnvShapeData[i][ts] = (UCHAR)envShapeData_tmp[ts];
1184 }
1185 }
1186 }
1187 } break;
1188 default:
1189 err = MPS_INVALID_TEMPSHAPE;
1190 goto bail;
1191 }
1192 } /* bsTempShapeEnable */
1193 } /* pSpatialSpecificConfig->tempShapeConfig != 0 */
1194
1195 if (pSpatialSpecificConfig->bArbitraryDownmix != 0) {
1196 err = parseArbitraryDownmixData(self, pSpatialSpecificConfig, syntaxFlags,
1197 frame, bitstream);
1198 if (err != MPS_OK) goto bail;
1199 }
1200
1201 if (1 && (!(syntaxFlags & (SACDEC_SYNTAX_USAC)))) {
1202 FDKbyteAlign(bitstream,
1203 alignAnchor); /* ISO/IEC FDIS 23003-1: 5.2. ... byte alignment
1204 with respect to the beginning of the syntactic
1205 element in which ByteAlign() occurs. */
1206 }
1207
1208 bail:
1209 if (err != MPS_OK && paramSetErr != 0) {
1210 /* Since the parameter set data has already been written to the instance we
1211 * need to ... */
1212 frame->numParameterSets = 0; /* ... signal that it is corrupt ... */
1213 }
1214
1215 return err;
1216
1217 } /* SpatialDecParseFrame */
1218
1219 /*******************************************************************************
1220 Functionname: createMapping
1221 *******************************************************************************
1222
1223 Description:
1224
1225 Arguments:
1226
1227 Return:
1228
1229 *******************************************************************************/
createMapping(int aMap[MAX_PARAMETER_BANDS+1],int startBand,int stopBand,int stride)1230 static void createMapping(int aMap[MAX_PARAMETER_BANDS + 1], int startBand,
1231 int stopBand, int stride) {
1232 int inBands, outBands, bandsAchived, bandsDiff, incr, k, i;
1233 int vDk[MAX_PARAMETER_BANDS + 1];
1234 inBands = stopBand - startBand;
1235 outBands = (inBands - 1) / stride + 1;
1236
1237 if (outBands < 1) {
1238 outBands = 1;
1239 }
1240
1241 bandsAchived = outBands * stride;
1242 bandsDiff = inBands - bandsAchived;
1243 for (i = 0; i < outBands; i++) {
1244 vDk[i] = stride;
1245 }
1246
1247 if (bandsDiff > 0) {
1248 incr = -1;
1249 k = outBands - 1;
1250 } else {
1251 incr = 1;
1252 k = 0;
1253 }
1254
1255 while (bandsDiff != 0) {
1256 vDk[k] = vDk[k] - incr;
1257 k = k + incr;
1258 bandsDiff = bandsDiff + incr;
1259 if (k >= outBands) {
1260 if (bandsDiff > 0) {
1261 k = outBands - 1;
1262 } else if (bandsDiff < 0) {
1263 k = 0;
1264 }
1265 }
1266 }
1267 aMap[0] = startBand;
1268 for (i = 0; i < outBands; i++) {
1269 aMap[i + 1] = aMap[i] + vDk[i];
1270 }
1271 } /* createMapping */
1272
1273 /*******************************************************************************
1274 Functionname: mapFrequency
1275 *******************************************************************************
1276
1277 Description:
1278
1279 Arguments:
1280
1281 Return:
1282
1283 *******************************************************************************/
mapFrequency(const SCHAR * pInput,SCHAR * pOutput,int * pMap,int dataBands)1284 static void mapFrequency(const SCHAR *pInput, /* Input */
1285 SCHAR *pOutput, /* Output */
1286 int *pMap, /* Mapping function */
1287 int dataBands) /* Number of data Bands */
1288 {
1289 int i, j;
1290 int startBand0 = pMap[0];
1291
1292 for (i = 0; i < dataBands; i++) {
1293 int startBand, stopBand, value;
1294
1295 value = pInput[i + startBand0];
1296
1297 startBand = pMap[i];
1298 stopBand = pMap[i + 1];
1299 for (j = startBand; j < stopBand; j++) {
1300 pOutput[j] = value;
1301 }
1302 }
1303 }
1304
1305 /*******************************************************************************
1306 Functionname: deq
1307 *******************************************************************************
1308
1309 Description:
1310
1311 Arguments:
1312
1313 Return:
1314
1315 *******************************************************************************/
deqIdx(int value,int paramType)1316 static int deqIdx(int value, int paramType) {
1317 int idx = -1;
1318
1319 switch (paramType) {
1320 case t_CLD:
1321 if (((value + 15) >= 0) && ((value + 15) < 31)) {
1322 idx = (value + 15);
1323 }
1324 break;
1325
1326 case t_ICC:
1327 if ((value >= 0) && (value < 8)) {
1328 idx = value;
1329 }
1330 break;
1331
1332 case t_IPD:
1333 /* (+/-)15 * MAX_PARAMETER_BANDS for differential coding in frequency
1334 * domain (according to rbl) */
1335 if ((value >= -420) && (value <= 420)) {
1336 idx = (value & 0xf);
1337 }
1338 break;
1339
1340 default:
1341 FDK_ASSERT(0);
1342 }
1343
1344 return idx;
1345 }
1346
1347 /*******************************************************************************
1348 Functionname: factorFunct
1349 *******************************************************************************
1350
1351 Description:
1352
1353 Arguments:
1354
1355 Return:
1356
1357 *******************************************************************************/
1358
1359 #define SF_IDX (7)
1360 #define SF_FACTOR (3)
1361 #define SCALE_FACTOR (1 << SF_FACTOR)
1362 #define SCALE_CLD_C1C2 (1 << SF_CLD_C1C2)
1363
factorFunct(FIXP_DBL ottVsTotDb,INT quantMode)1364 static FIXP_DBL factorFunct(FIXP_DBL ottVsTotDb, INT quantMode) {
1365 FIXP_DBL factor;
1366
1367 if (ottVsTotDb > FL2FXCONST_DBL(0.0)) {
1368 ottVsTotDb = FL2FXCONST_DBL(0.0);
1369 }
1370
1371 ottVsTotDb = -ottVsTotDb;
1372
1373 switch (quantMode) {
1374 case 0:
1375 factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1376 break;
1377 case 1:
1378 if (ottVsTotDb >= FL2FXCONST_DBL(21.0f / SCALE_CLD_C1C2))
1379 factor = FL2FXCONST_DBL(5.0f / SCALE_FACTOR);
1380 else if (ottVsTotDb <= FL2FXCONST_DBL(1.0f / SCALE_CLD_C1C2))
1381 factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1382 else
1383 factor = (fMult(FL2FXCONST_DBL(0.2f), ottVsTotDb) +
1384 FL2FXCONST_DBL(0.8f / SCALE_CLD_C1C2))
1385 << (SF_CLD_C1C2 - SF_FACTOR);
1386 break;
1387 case 2:
1388 if (ottVsTotDb >= FL2FXCONST_DBL(25.0f / SCALE_CLD_C1C2)) {
1389 FDK_ASSERT(SF_FACTOR == 3);
1390 factor = (FIXP_DBL)
1391 MAXVAL_DBL; /* avoid warning: FL2FXCONST_DBL(8.0f/SCALE_FACTOR) */
1392 } else if (ottVsTotDb <= FL2FXCONST_DBL(1.0f / SCALE_CLD_C1C2))
1393 factor = FL2FXCONST_DBL(1.0f / SCALE_FACTOR);
1394 else
1395 factor = (fMult(FL2FXCONST_DBL(7.0f / 24.0f), ottVsTotDb) +
1396 FL2FXCONST_DBL((17.0f / 24.0f) / SCALE_CLD_C1C2))
1397 << (SF_CLD_C1C2 - SF_FACTOR);
1398 break;
1399 default:
1400 factor = FL2FXCONST_DBL(0.0f);
1401 }
1402
1403 return (factor);
1404 }
1405
1406 /*******************************************************************************
1407 Functionname: factorCLD
1408 *******************************************************************************
1409
1410 Description:
1411
1412 Arguments:
1413
1414 Return:
1415
1416 *******************************************************************************/
factorCLD(SCHAR * idx,FIXP_DBL ottVsTotDb,FIXP_DBL * ottVsTotDb1,FIXP_DBL * ottVsTotDb2,SCHAR ottVsTotDbMode,INT quantMode)1417 static void factorCLD(SCHAR *idx, FIXP_DBL ottVsTotDb, FIXP_DBL *ottVsTotDb1,
1418 FIXP_DBL *ottVsTotDb2, SCHAR ottVsTotDbMode,
1419 INT quantMode) {
1420 FIXP_DBL factor;
1421 FIXP_DBL cldIdxFract;
1422 INT cldIdx;
1423
1424 factor = factorFunct(ottVsTotDb, quantMode);
1425
1426 cldIdxFract =
1427 fMult((FIXP_DBL)((*idx) << ((DFRACT_BITS - 1) - SF_IDX)), factor);
1428 cldIdxFract += FL2FXCONST_DBL(15.5f / (1 << (SF_FACTOR + SF_IDX)));
1429 cldIdx = fixp_truncateToInt(cldIdxFract, SF_FACTOR + SF_IDX);
1430
1431 cldIdx = fMin(cldIdx, 30);
1432 cldIdx = fMax(cldIdx, 0);
1433
1434 *idx = cldIdx - 15;
1435
1436 if (ottVsTotDbMode & ottVsTotDb1Activ)
1437 (*ottVsTotDb1) = ottVsTotDb + dequantCLD_c1[cldIdx];
1438
1439 if (ottVsTotDbMode & ottVsTotDb2Activ)
1440 (*ottVsTotDb2) = ottVsTotDb + dequantCLD_c1[30 - cldIdx];
1441 }
1442
1443 /*******************************************************************************
1444 Functionname: mapIndexData
1445 *******************************************************************************
1446
1447 Description:
1448
1449 Arguments:
1450
1451 Return:
1452
1453 *******************************************************************************/
mapIndexData(LOSSLESSDATA * llData,SCHAR *** outputDataIdx,SCHAR *** outputIdxData,const SCHAR (* cmpIdxData)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],SCHAR *** diffIdxData,SCHAR xttIdx,SCHAR ** idxPrev,int paramIdx,int paramType,int startBand,int stopBand,SCHAR defaultValue,int numParameterSets,const int * paramSlot,int extendFrame,int quantMode,SpatialDecConcealmentInfo * concealmentInfo,SCHAR ottVsTotDbMode,FIXP_DBL (* pOttVsTotDbIn)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],FIXP_DBL (* pOttVsTotDb1)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],FIXP_DBL (* pOttVsTotDb2)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])1454 static SACDEC_ERROR mapIndexData(
1455 LOSSLESSDATA *llData, SCHAR ***outputDataIdx, SCHAR ***outputIdxData,
1456 const SCHAR (*cmpIdxData)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1457 SCHAR ***diffIdxData, SCHAR xttIdx, SCHAR **idxPrev, int paramIdx,
1458 int paramType, int startBand, int stopBand, SCHAR defaultValue,
1459 int numParameterSets, const int *paramSlot, int extendFrame, int quantMode,
1460 SpatialDecConcealmentInfo *concealmentInfo, SCHAR ottVsTotDbMode,
1461 FIXP_DBL (*pOttVsTotDbIn)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1462 FIXP_DBL (*pOttVsTotDb1)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1463 FIXP_DBL (*pOttVsTotDb2)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS]) {
1464 int aParamSlots[MAX_PARAMETER_SETS];
1465 int aInterpolate[MAX_PARAMETER_SETS] = {0};
1466
1467 int dataSets;
1468 int aMap[MAX_PARAMETER_BANDS + 1];
1469
1470 int setIdx, i, band, parmSlot;
1471 int dataBands;
1472 int ps, pb;
1473 int i1;
1474
1475 if (numParameterSets > MAX_PARAMETER_SETS) return MPS_WRONG_PARAMETERSETS;
1476
1477 dataSets = 0;
1478 for (i = 0; i < numParameterSets; i++) {
1479 if (llData->bsXXXDataMode[i] == 3) {
1480 aParamSlots[dataSets] = i;
1481 dataSets++;
1482 }
1483 }
1484
1485 setIdx = 0;
1486
1487 /* Main concealment stage is here: */
1488 SpatialDecConcealment_Apply(
1489 concealmentInfo, cmpIdxData[xttIdx],
1490 (diffIdxData != NULL) ? diffIdxData[xttIdx] : NULL, idxPrev[xttIdx],
1491 llData->bsXXXDataMode, startBand, stopBand, defaultValue, paramType,
1492 numParameterSets);
1493
1494 /* Prepare data */
1495 for (i = 0; i < numParameterSets; i++) {
1496 if (llData->bsXXXDataMode[i] == 0) {
1497 llData->nocmpQuantCoarseXXX[i] = 0;
1498 for (band = startBand; band < stopBand; band++) {
1499 outputIdxData[xttIdx][i][band] = defaultValue;
1500 }
1501 for (band = startBand; band < stopBand; band++) {
1502 idxPrev[xttIdx][band] = outputIdxData[xttIdx][i][band];
1503 }
1504 /* Because the idxPrev are also set to the defaultValue -> signalize fine
1505 */
1506 llData->state->bsQuantCoarseXXXprev = 0;
1507 }
1508
1509 if (llData->bsXXXDataMode[i] == 1) {
1510 for (band = startBand; band < stopBand; band++) {
1511 outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1512 }
1513 llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1514 }
1515
1516 if (llData->bsXXXDataMode[i] == 2) {
1517 for (band = startBand; band < stopBand; band++) {
1518 outputIdxData[xttIdx][i][band] = idxPrev[xttIdx][band];
1519 }
1520 llData->nocmpQuantCoarseXXX[i] = llData->state->bsQuantCoarseXXXprev;
1521 aInterpolate[i] = 1;
1522 } else {
1523 aInterpolate[i] = 0;
1524 }
1525
1526 if (llData->bsXXXDataMode[i] == 3) {
1527 int stride;
1528
1529 parmSlot = aParamSlots[setIdx];
1530 stride = pbStrideTable[llData->bsFreqResStrideXXX[setIdx]];
1531 dataBands = (stopBand - startBand - 1) / stride + 1;
1532 createMapping(aMap, startBand, stopBand, stride);
1533 mapFrequency(&cmpIdxData[xttIdx][setIdx][0],
1534 &outputIdxData[xttIdx][parmSlot][0], aMap, dataBands);
1535 for (band = startBand; band < stopBand; band++) {
1536 idxPrev[xttIdx][band] = outputIdxData[xttIdx][parmSlot][band];
1537 }
1538 llData->state->bsQuantCoarseXXXprev = llData->bsQuantCoarseXXX[setIdx];
1539 llData->nocmpQuantCoarseXXX[i] = llData->bsQuantCoarseXXX[setIdx];
1540
1541 setIdx++;
1542 }
1543 if (diffIdxData != NULL) {
1544 for (band = startBand; band < stopBand; band++) {
1545 outputIdxData[xttIdx][i][band] += diffIdxData[xttIdx][i][band];
1546 }
1547 }
1548 } /* for( i = 0 ; i < numParameterSets; i++ ) */
1549
1550 /* Map all coarse data to fine */
1551 for (i = 0; i < numParameterSets; i++) {
1552 if (llData->nocmpQuantCoarseXXX[i] == 1) {
1553 coarse2fine(outputIdxData[xttIdx][i], (DATA_TYPE)paramType, startBand,
1554 stopBand - startBand);
1555 llData->nocmpQuantCoarseXXX[i] = 0;
1556 }
1557 }
1558
1559 /* Interpolate */
1560 i1 = 0;
1561 for (i = 0; i < numParameterSets; i++) {
1562 if (aInterpolate[i] != 1) {
1563 i1 = i;
1564 } else {
1565 int xi, i2, x1, x2;
1566
1567 for (i2 = i; i2 < numParameterSets; i2++) {
1568 if (aInterpolate[i2] != 1) break;
1569 }
1570 if (i2 >= numParameterSets) return MPS_WRONG_PARAMETERSETS;
1571
1572 x1 = paramSlot[i1];
1573 xi = paramSlot[i];
1574 x2 = paramSlot[i2];
1575
1576 for (band = startBand; band < stopBand; band++) {
1577 int yi, y1, y2;
1578 y1 = outputIdxData[xttIdx][i1][band];
1579 y2 = outputIdxData[xttIdx][i2][band];
1580 if (x1 != x2) {
1581 yi = y1 + (xi - x1) * (y2 - y1) / (x2 - x1);
1582 } else {
1583 yi = y1 /*+ (xi-x1)*(y2-y1)/1e-12*/;
1584 }
1585 outputIdxData[xttIdx][i][band] = yi;
1586 }
1587 }
1588 } /* for( i = 0 ; i < numParameterSets; i++ ) */
1589
1590 /* Dequantize data and apply factorCLD if necessary */
1591 for (ps = 0; ps < numParameterSets; ps++) {
1592 if (quantMode && (paramType == t_CLD)) {
1593 if (pOttVsTotDbIn == 0) return MPS_WRONG_OTT;
1594 if ((pOttVsTotDb1 == 0) && (ottVsTotDbMode & ottVsTotDb1Activ))
1595 return MPS_WRONG_OTT;
1596 if ((pOttVsTotDb2 == 0) && (ottVsTotDbMode & ottVsTotDb2Activ))
1597 return MPS_WRONG_OTT;
1598
1599 for (pb = startBand; pb < stopBand; pb++) {
1600 factorCLD(&(outputIdxData[xttIdx][ps][pb]), (*pOttVsTotDbIn)[ps][pb],
1601 (pOttVsTotDb1 != NULL) ? &((*pOttVsTotDb1)[ps][pb]) : NULL,
1602 (pOttVsTotDb2 != NULL) ? &((*pOttVsTotDb2)[ps][pb]) : NULL,
1603 ottVsTotDbMode, quantMode);
1604 }
1605 }
1606
1607 /* Dequantize data */
1608 for (band = startBand; band < stopBand; band++) {
1609 outputDataIdx[xttIdx][ps][band] =
1610 deqIdx(outputIdxData[xttIdx][ps][band], paramType);
1611 if (outputDataIdx[xttIdx][ps][band] == -1) {
1612 outputDataIdx[xttIdx][ps][band] = defaultValue;
1613 }
1614 }
1615 } /* for( i = 0 ; i < numParameterSets; i++ ) */
1616
1617 if (extendFrame) {
1618 if (paramType == t_IPD) {
1619 llData->bsQuantCoarseXXX[numParameterSets] =
1620 llData->bsQuantCoarseXXX[numParameterSets - 1];
1621 }
1622 for (band = startBand; band < stopBand; band++) {
1623 outputDataIdx[xttIdx][numParameterSets][band] =
1624 outputDataIdx[xttIdx][numParameterSets - 1][band];
1625 }
1626 }
1627
1628 return MPS_OK;
1629 }
1630
1631 /*******************************************************************************
1632 Functionname: decodeAndMapFrameOtt
1633 *******************************************************************************
1634
1635 Description:
1636 Do delta decoding and dequantization
1637
1638 Arguments:
1639
1640 Input:
1641
1642 Output:
1643
1644 *******************************************************************************/
decodeAndMapFrameOtt(HANDLE_SPATIAL_DEC self,SPATIAL_BS_FRAME * pCurBs)1645 static SACDEC_ERROR decodeAndMapFrameOtt(HANDLE_SPATIAL_DEC self,
1646 SPATIAL_BS_FRAME *pCurBs) {
1647 int i, ottIdx;
1648 int numOttBoxes;
1649
1650 SACDEC_ERROR err = MPS_OK;
1651
1652 numOttBoxes = self->numOttBoxes;
1653
1654 switch (self->treeConfig) {
1655 default: {
1656 if (self->quantMode != 0) {
1657 goto bail;
1658 }
1659 }
1660 for (i = 0; i < numOttBoxes; i++) {
1661 err = mapIndexData(
1662 &pCurBs->CLDLosslessData[i], /* LOSSLESSDATA *llData,*/
1663 self->ottCLD__FDK, self->outIdxData,
1664 pCurBs
1665 ->cmpOttCLDidx, /* int
1666 cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1667 */
1668 NULL, /* no differential data */
1669 i, /* int xttIdx, Which ott/ttt index to use for input and
1670 output buffers */
1671 self->ottCLDidxPrev, /* int
1672 idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1673 */
1674 i, t_CLD, 0, /* int startBand, */
1675 self->pConfigCurrent->bitstreamOttBands[i], /* int stopBand, */
1676 self->pConfigCurrent->ottCLDdefault[i], /* int defaultValue, */
1677 pCurBs->numParameterSets, /* int numParameterSets) */
1678 pCurBs->paramSlot, self->extendFrame, self->quantMode,
1679 &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1680 if (err != MPS_OK) goto bail;
1681
1682 } /* for(i = 0; i < numOttBoxes ; i++ ) */
1683 break;
1684 } /* case */
1685
1686 for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1687 /* Read ICC */
1688 err = mapIndexData(
1689 &pCurBs->ICCLosslessData[ottIdx], /* LOSSLESSDATA *llData,*/
1690 self->ottICC__FDK, self->outIdxData,
1691 pCurBs
1692 ->cmpOttICCidx, /* int
1693 cmpIdxData[MAX_NUM_OTT][MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS],
1694 */
1695 self->ottICCdiffidx, /* differential data */
1696 ottIdx, /* int xttIdx, Which ott/ttt index to use for input and
1697 output buffers */
1698 self->ottICCidxPrev, /* int idxPrev[MAX_NUM_OTT][MAX_PARAMETER_BANDS],
1699 */
1700 ottIdx, t_ICC, 0, /* int startBand, */
1701 self->pConfigCurrent->bitstreamOttBands[ottIdx], /* int stopBand, */
1702 ICCdefault, /* int defaultValue, */
1703 pCurBs->numParameterSets, /* int numParameterSets) */
1704 pCurBs->paramSlot, self->extendFrame, self->quantMode,
1705 &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1706 if (err != MPS_OK) goto bail;
1707 } /* ottIdx */
1708
1709 if ((self->treeConfig == TREE_212) && (self->phaseCoding)) {
1710 if (pCurBs->phaseMode == 0) {
1711 for (int pb = 0; pb < self->pConfigCurrent->numOttBandsIPD; pb++) {
1712 self->ottIPDidxPrev[0][pb] = 0;
1713 }
1714 }
1715 for (ottIdx = 0; ottIdx < numOttBoxes; ottIdx++) {
1716 err = mapIndexData(
1717 &pCurBs->IPDLosslessData[ottIdx], self->ottIPD__FDK, self->outIdxData,
1718 pCurBs->cmpOttIPDidx, NULL, ottIdx, self->ottIPDidxPrev, ottIdx,
1719 t_IPD, 0, self->numOttBandsIPD, IPDdefault, pCurBs->numParameterSets,
1720 pCurBs->paramSlot, self->extendFrame, self->quantMode,
1721 &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1722 }
1723 }
1724
1725 bail:
1726
1727 return MPS_OK;
1728
1729 } /* decodeAndMapFrameOtt */
1730
1731 /*******************************************************************************
1732 Functionname: decodeAndMapFrameSmg
1733 *******************************************************************************
1734
1735 Description:
1736 Decode smoothing flags
1737
1738 Arguments:
1739
1740 Input:
1741
1742 Output:
1743
1744
1745 *******************************************************************************/
decodeAndMapFrameSmg(HANDLE_SPATIAL_DEC self,const SPATIAL_BS_FRAME * frame)1746 static SACDEC_ERROR decodeAndMapFrameSmg(HANDLE_SPATIAL_DEC self,
1747 const SPATIAL_BS_FRAME *frame) {
1748 int ps, pb, pg, pbStride, dataBands, pbStart, pbStop,
1749 aGroupToBand[MAX_PARAMETER_BANDS + 1];
1750
1751 if (frame->numParameterSets > MAX_PARAMETER_SETS)
1752 return MPS_WRONG_PARAMETERSETS;
1753 if (self->bitstreamParameterBands > MAX_PARAMETER_BANDS)
1754 return MPS_WRONG_PARAMETERBANDS;
1755
1756 for (ps = 0; ps < frame->numParameterSets; ps++) {
1757 switch (frame->bsSmoothMode[ps]) {
1758 case 0:
1759 self->smgTime[ps] = 256;
1760 FDKmemclear(self->smgData[ps],
1761 self->bitstreamParameterBands * sizeof(UCHAR));
1762 break;
1763
1764 case 1:
1765 if (ps > 0) {
1766 self->smgTime[ps] = self->smgTime[ps - 1];
1767 FDKmemcpy(self->smgData[ps], self->smgData[ps - 1],
1768 self->bitstreamParameterBands * sizeof(UCHAR));
1769 } else {
1770 self->smgTime[ps] = self->smoothState->prevSmgTime;
1771 FDKmemcpy(self->smgData[ps], self->smoothState->prevSmgData,
1772 self->bitstreamParameterBands * sizeof(UCHAR));
1773 }
1774 break;
1775
1776 case 2:
1777 self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1778 for (pb = 0; pb < self->bitstreamParameterBands; pb++) {
1779 self->smgData[ps][pb] = 1;
1780 }
1781 break;
1782
1783 case 3:
1784 self->smgTime[ps] = smgTimeTable[frame->bsSmoothTime[ps]];
1785 pbStride = pbStrideTable[frame->bsFreqResStrideSmg[ps]];
1786 dataBands = (self->bitstreamParameterBands - 1) / pbStride + 1;
1787 createMapping(aGroupToBand, 0, self->bitstreamParameterBands, pbStride);
1788 for (pg = 0; pg < dataBands; pg++) {
1789 pbStart = aGroupToBand[pg];
1790 pbStop = aGroupToBand[pg + 1];
1791 for (pb = pbStart; pb < pbStop; pb++) {
1792 self->smgData[ps][pb] = frame->bsSmgData[ps][pg];
1793 }
1794 }
1795 break;
1796 }
1797 }
1798
1799 self->smoothState->prevSmgTime = self->smgTime[frame->numParameterSets - 1];
1800 FDKmemcpy(self->smoothState->prevSmgData,
1801 self->smgData[frame->numParameterSets - 1],
1802 self->bitstreamParameterBands * sizeof(UCHAR));
1803
1804 if (self->extendFrame) {
1805 self->smgTime[frame->numParameterSets] =
1806 self->smgTime[frame->numParameterSets - 1];
1807 FDKmemcpy(self->smgData[frame->numParameterSets],
1808 self->smgData[frame->numParameterSets - 1],
1809 self->bitstreamParameterBands * sizeof(UCHAR));
1810 }
1811
1812 return MPS_OK;
1813 }
1814
1815 /*******************************************************************************
1816 Functionname: decodeAndMapFrameArbdmx
1817 *******************************************************************************
1818
1819 Description:
1820 Do delta decoding and dequantization
1821
1822 Arguments:
1823
1824 Input:
1825
1826 Output:
1827
1828 *******************************************************************************/
decodeAndMapFrameArbdmx(HANDLE_SPATIAL_DEC self,const SPATIAL_BS_FRAME * frame)1829 static SACDEC_ERROR decodeAndMapFrameArbdmx(HANDLE_SPATIAL_DEC self,
1830 const SPATIAL_BS_FRAME *frame) {
1831 SACDEC_ERROR err = MPS_OK;
1832 int ch;
1833 int offset = self->numOttBoxes;
1834
1835 for (ch = 0; ch < self->numInputChannels; ch++) {
1836 err = mapIndexData(&frame->CLDLosslessData[offset + ch],
1837 self->arbdmxGain__FDK, self->outIdxData,
1838 frame->cmpArbdmxGainIdx, NULL, /* no differential data */
1839 ch, self->arbdmxGainIdxPrev, offset + ch, t_CLD, 0,
1840 self->bitstreamParameterBands,
1841 0 /*self->arbdmxGainDefault*/, frame->numParameterSets,
1842 frame->paramSlot, self->extendFrame, 0,
1843 &(self->concealInfo), ottVsTotInactiv, NULL, NULL, NULL);
1844 if (err != MPS_OK) goto bail;
1845 }
1846
1847 bail:
1848 return err;
1849 } /* decodeAndMapFrameArbdmx */
1850
1851 /*******************************************************************************
1852 Functionname: SpatialDecDecodeFrame
1853 *******************************************************************************
1854
1855 Description:
1856
1857 Arguments:
1858
1859 Return:
1860
1861 *******************************************************************************/
SpatialDecDecodeFrame(spatialDec * self,SPATIAL_BS_FRAME * frame)1862 SACDEC_ERROR SpatialDecDecodeFrame(spatialDec *self, SPATIAL_BS_FRAME *frame) {
1863 SACDEC_ERROR err = MPS_OK;
1864
1865 self->extendFrame = 0;
1866 if (frame->paramSlot[frame->numParameterSets - 1] != self->timeSlots - 1) {
1867 self->extendFrame = 1;
1868 }
1869
1870 self->TsdTs = 0;
1871
1872 /****** DTDF and MAP DATA ********/
1873 if ((err = decodeAndMapFrameOtt(self, frame)) != MPS_OK) goto bail;
1874
1875 if ((err = decodeAndMapFrameSmg(self, frame)) != MPS_OK) goto bail;
1876
1877 if (self->arbitraryDownmix != 0) {
1878 if ((err = decodeAndMapFrameArbdmx(self, frame)) != MPS_OK) goto bail;
1879 }
1880
1881 if (self->extendFrame) {
1882 frame->numParameterSets =
1883 fixMin(MAX_PARAMETER_SETS, frame->numParameterSets + 1);
1884 frame->paramSlot[frame->numParameterSets - 1] = self->timeSlots - 1;
1885
1886 for (int p = 0; p < frame->numParameterSets; p++) {
1887 if (frame->paramSlot[p] > self->timeSlots - 1) {
1888 frame->paramSlot[p] = self->timeSlots - 1;
1889 err = MPS_PARSE_ERROR;
1890 }
1891 }
1892 if (err != MPS_OK) {
1893 goto bail;
1894 }
1895 }
1896
1897 bail:
1898 return err;
1899 } /* SpatialDecDecodeFrame() */
1900
1901 /*******************************************************************************
1902 Functionname: SpatialDecodeHeader
1903 *******************************************************************************
1904
1905 Description:
1906
1907 Arguments:
1908
1909 Return:
1910
1911 *******************************************************************************/
1912
SpatialDecDecodeHeader(spatialDec * self,SPATIAL_SPECIFIC_CONFIG * pSpatialSpecificConfig)1913 SACDEC_ERROR SpatialDecDecodeHeader(
1914 spatialDec *self, SPATIAL_SPECIFIC_CONFIG *pSpatialSpecificConfig) {
1915 SACDEC_ERROR err = MPS_OK;
1916 int i;
1917
1918 self->samplingFreq = pSpatialSpecificConfig->samplingFreq;
1919 self->timeSlots = pSpatialSpecificConfig->nTimeSlots;
1920 self->frameLength = self->timeSlots * self->qmfBands;
1921 self->bitstreamParameterBands = pSpatialSpecificConfig->freqRes;
1922
1923 if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD)
1924 self->hybridBands = self->qmfBands;
1925 else
1926 self->hybridBands = SacGetHybridSubbands(self->qmfBands);
1927 self->tp_hybBandBorder = 12;
1928
1929 self->numParameterBands = self->bitstreamParameterBands;
1930
1931 if (self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_LD) {
1932 switch (self->numParameterBands) {
1933 case 4:
1934 self->kernels = kernels_4_to_64;
1935 break;
1936 case 5:
1937 self->kernels = kernels_5_to_64;
1938 break;
1939 case 7:
1940 self->kernels = kernels_7_to_64;
1941 break;
1942 case 9:
1943 self->kernels = kernels_9_to_64;
1944 break;
1945 case 12:
1946 self->kernels = kernels_12_to_64;
1947 break;
1948 case 15:
1949 self->kernels = kernels_15_to_64;
1950 break;
1951 case 23:
1952 self->kernels = kernels_23_to_64;
1953 break;
1954 default:
1955 return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1956 }
1957 } else {
1958 switch (self->numParameterBands) {
1959 case 4:
1960 self->kernels = kernels_4_to_71;
1961 break;
1962 case 5:
1963 self->kernels = kernels_5_to_71;
1964 break;
1965 case 7:
1966 self->kernels = kernels_7_to_71;
1967 break;
1968 case 10:
1969 self->kernels = kernels_10_to_71;
1970 break;
1971 case 14:
1972 self->kernels = kernels_14_to_71;
1973 break;
1974 case 20:
1975 self->kernels = kernels_20_to_71;
1976 break;
1977 case 28:
1978 self->kernels = kernels_28_to_71;
1979 break;
1980 default:
1981 return MPS_INVALID_PARAMETERBANDS; /* unsupported numParameterBands */
1982 }
1983 }
1984
1985 /* create param to hyb band table */
1986 FDKmemclear(self->param2hyb, (MAX_PARAMETER_BANDS + 1) * sizeof(int));
1987 for (i = 0; i < self->hybridBands; i++) {
1988 self->param2hyb[self->kernels[i] + 1] = i + 1;
1989 }
1990 {
1991 int pb = self->kernels[i - 1] + 2;
1992 for (; pb < (MAX_PARAMETER_BANDS + 1); pb++) {
1993 self->param2hyb[pb] = i;
1994 }
1995 for (pb = 0; pb < MAX_PARAMETER_BANDS; pb += 1) {
1996 self->kernels_width[pb] = self->param2hyb[pb + 1] - self->param2hyb[pb];
1997 }
1998 }
1999
2000 self->treeConfig = pSpatialSpecificConfig->treeConfig;
2001
2002 self->numOttBoxes = pSpatialSpecificConfig->nOttBoxes;
2003
2004 self->numInputChannels = pSpatialSpecificConfig->nInputChannels;
2005
2006 self->numOutputChannels = pSpatialSpecificConfig->nOutputChannels;
2007
2008 self->quantMode = pSpatialSpecificConfig->quantMode;
2009
2010 self->arbitraryDownmix = pSpatialSpecificConfig->bArbitraryDownmix;
2011
2012 self->numM2rows = self->numOutputChannels;
2013
2014 {
2015 self->residualCoding = 0;
2016 if (self->arbitraryDownmix == 2)
2017 self->arbitraryDownmix = 1; /* no arbitrary downmix residuals */
2018 }
2019 if ((self->pConfigCurrent->syntaxFlags & SACDEC_SYNTAX_USAC)) {
2020 self->residualCoding = pSpatialSpecificConfig->bResidualCoding;
2021 }
2022
2023 self->clipProtectGain__FDK =
2024 FX_CFG2FX_DBL(clipGainTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX]);
2025 self->clipProtectGainSF__FDK =
2026 clipGainSFTable__FDK[pSpatialSpecificConfig->bsFixedGainDMX];
2027
2028 self->tempShapeConfig = pSpatialSpecificConfig->tempShapeConfig;
2029
2030 self->decorrConfig = pSpatialSpecificConfig->decorrConfig;
2031
2032 if (self->upmixType == UPMIXTYPE_BYPASS) {
2033 self->numOutputChannels = self->numInputChannels;
2034 }
2035
2036 self->numOutputChannelsAT = self->numOutputChannels;
2037
2038 self->numOttBandsIPD = pSpatialSpecificConfig->numOttBandsIPD;
2039 self->phaseCoding = pSpatialSpecificConfig->bsPhaseCoding;
2040 for (i = 0; i < self->numOttBoxes; i++) {
2041 {
2042 self->pConfigCurrent->bitstreamOttBands[i] =
2043 self->bitstreamParameterBands;
2044 }
2045 self->numOttBands[i] = self->pConfigCurrent->bitstreamOttBands[i];
2046 } /* i */
2047
2048 if (self->residualCoding) {
2049 int numBoxes = self->numOttBoxes;
2050 for (i = 0; i < numBoxes; i++) {
2051 self->residualPresent[i] =
2052 pSpatialSpecificConfig->ResidualConfig[i].bResidualPresent;
2053
2054 if (self->residualPresent[i]) {
2055 self->residualBands[i] =
2056 pSpatialSpecificConfig->ResidualConfig[i].nResidualBands;
2057 /* conversion from hybrid bands to qmf bands */
2058 self->residualQMFBands[i] =
2059 fMax(self->param2hyb[self->residualBands[i]] + 3 - 10,
2060 3); /* simplification for the lowest 10 hybrid bands */
2061 } else {
2062 self->residualBands[i] = 0;
2063 self->residualQMFBands[i] = 0;
2064 }
2065 }
2066 } /* self->residualCoding */
2067 else {
2068 int boxes = self->numOttBoxes;
2069 for (i = 0; i < boxes; i += 1) {
2070 self->residualPresent[i] = 0;
2071 self->residualBands[i] = 0;
2072 }
2073 }
2074
2075 switch (self->treeConfig) {
2076 case TREE_212:
2077 self->numDirektSignals = 1;
2078 self->numDecorSignals = 1;
2079 self->numXChannels = 1;
2080 if (self->arbitraryDownmix == 2) {
2081 self->numXChannels += 1;
2082 }
2083 self->numVChannels = self->numDirektSignals + self->numDecorSignals;
2084 break;
2085 default:
2086 return MPS_INVALID_TREECONFIG;
2087 }
2088
2089 self->highRateMode = pSpatialSpecificConfig->bsHighRateMode;
2090 self->decorrType = pSpatialSpecificConfig->bsDecorrType;
2091
2092 SpatialDecDecodeHelperInfo(pSpatialSpecificConfig, UPMIXTYPE_NORMAL);
2093
2094 return err;
2095 }
2096
2097 /*******************************************************************************
2098 Functionname: SpatialDecCreateBsFrame
2099 *******************************************************************************
2100
2101 Description: Create spatial bitstream structure
2102
2103 Arguments: spatialDec* self
2104 const SPATIAL_BS_FRAME **bsFrame
2105
2106 Return: -
2107
2108 *******************************************************************************/
SpatialDecCreateBsFrame(SPATIAL_BS_FRAME * bsFrame,BS_LL_STATE * llState)2109 SACDEC_ERROR SpatialDecCreateBsFrame(SPATIAL_BS_FRAME *bsFrame,
2110 BS_LL_STATE *llState) {
2111 SPATIAL_BS_FRAME *pBs = bsFrame;
2112
2113 const int maxNumOtt = MAX_NUM_OTT;
2114 const int maxNumInputChannels = MAX_INPUT_CHANNELS;
2115
2116 FDK_ALLOCATE_MEMORY_1D_P(
2117 pBs->cmpOttIPDidx, maxNumOtt * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS,
2118 SCHAR, SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2119
2120 /* Arbitrary Downmix */
2121 FDK_ALLOCATE_MEMORY_1D_P(
2122 pBs->cmpArbdmxGainIdx,
2123 maxNumInputChannels * MAX_PARAMETER_SETS * MAX_PARAMETER_BANDS, SCHAR,
2124 SCHAR(*)[MAX_PARAMETER_SETS][MAX_PARAMETER_BANDS])
2125
2126 /* Lossless control */
2127 FDK_ALLOCATE_MEMORY_1D(pBs->CLDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2128 FDK_ALLOCATE_MEMORY_1D(pBs->ICCLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2129
2130 FDK_ALLOCATE_MEMORY_1D(pBs->IPDLosslessData, MAX_NUM_PARAMETERS, LOSSLESSDATA)
2131
2132 pBs->newBsData = 0;
2133 pBs->numParameterSets = 1;
2134
2135 /* Link lossless states */
2136 for (int x = 0; x < MAX_NUM_PARAMETERS; x++) {
2137 pBs->CLDLosslessData[x].state = &llState->CLDLosslessState[x];
2138 pBs->ICCLosslessData[x].state = &llState->ICCLosslessState[x];
2139
2140 pBs->IPDLosslessData[x].state = &llState->IPDLosslessState[x];
2141 }
2142
2143 return MPS_OK;
2144
2145 bail:
2146 return MPS_OUTOFMEMORY;
2147 }
2148
2149 /*******************************************************************************
2150 Functionname: SpatialDecCloseBsFrame
2151 *******************************************************************************
2152
2153 Description: Close spatial bitstream structure
2154
2155 Arguments: spatialDec* self
2156
2157 Return: -
2158
2159 *******************************************************************************/
SpatialDecCloseBsFrame(SPATIAL_BS_FRAME * pBs)2160 void SpatialDecCloseBsFrame(SPATIAL_BS_FRAME *pBs) {
2161 if (pBs != NULL) {
2162 /* These arrays contain the compact indices, only one value per pbstride,
2163 * only paramsets actually containing data. */
2164
2165 FDK_FREE_MEMORY_1D(pBs->cmpOttIPDidx);
2166
2167 /* Arbitrary Downmix */
2168 FDK_FREE_MEMORY_1D(pBs->cmpArbdmxGainIdx);
2169
2170 /* Lossless control */
2171 FDK_FREE_MEMORY_1D(pBs->IPDLosslessData);
2172 FDK_FREE_MEMORY_1D(pBs->CLDLosslessData);
2173 FDK_FREE_MEMORY_1D(pBs->ICCLosslessData);
2174 }
2175 }
2176