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 transport format decoder library *********************
96
97 Author(s): Daniel Homm
98
99 Description:
100
101 *******************************************************************************/
102
103 #include "tpdec_lib.h"
104 #include "tp_data.h"
105
106 #include "FDK_crc.h"
107
108 #include "common_fix.h"
109
110 /**
111 * The following arrays provide the IDs of the consecutive elements for each
112 * channel configuration. Every channel_configuration has to be finalized with
113 * ID_NONE.
114 */
115 static const MP4_ELEMENT_ID channel_configuration_0[] = {ID_NONE};
116 static const MP4_ELEMENT_ID channel_configuration_1[] = {ID_SCE, ID_NONE};
117 static const MP4_ELEMENT_ID channel_configuration_2[] = {ID_CPE, ID_NONE};
118 static const MP4_ELEMENT_ID channel_configuration_3[] = {ID_SCE, ID_CPE,
119 ID_NONE};
120 static const MP4_ELEMENT_ID channel_configuration_4[] = {ID_SCE, ID_CPE, ID_SCE,
121 ID_NONE};
122 static const MP4_ELEMENT_ID channel_configuration_5[] = {ID_SCE, ID_CPE, ID_CPE,
123 ID_NONE};
124 static const MP4_ELEMENT_ID channel_configuration_6[] = {ID_SCE, ID_CPE, ID_CPE,
125 ID_LFE, ID_NONE};
126 static const MP4_ELEMENT_ID channel_configuration_7[] = {
127 ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE};
128 static const MP4_ELEMENT_ID channel_configuration_8[] = {
129 ID_NONE}; /* reserved */
130 static const MP4_ELEMENT_ID channel_configuration_9[] = {
131 ID_NONE}; /* reserved */
132 static const MP4_ELEMENT_ID channel_configuration_10[] = {
133 ID_NONE}; /* reserved */
134 static const MP4_ELEMENT_ID channel_configuration_11[] = {
135 ID_SCE, ID_CPE, ID_CPE, ID_SCE, ID_LFE, ID_NONE};
136 static const MP4_ELEMENT_ID channel_configuration_12[] = {
137 ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE};
138 static const MP4_ELEMENT_ID channel_configuration_13[] = {
139 ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_CPE, ID_SCE, ID_LFE, ID_LFE, ID_SCE,
140 ID_CPE, ID_CPE, ID_SCE, ID_CPE, ID_SCE, ID_SCE, ID_CPE, ID_NONE};
141 static const MP4_ELEMENT_ID channel_configuration_14[] = {
142 ID_SCE, ID_CPE, ID_CPE, ID_LAST, ID_CPE, ID_NONE};
143
144 static const MP4_ELEMENT_ID *channel_configuration_array[] = {
145 channel_configuration_0, channel_configuration_1,
146 channel_configuration_2, channel_configuration_3,
147 channel_configuration_4, channel_configuration_5,
148 channel_configuration_6, channel_configuration_7,
149 channel_configuration_8, channel_configuration_9,
150 channel_configuration_10, channel_configuration_11,
151 channel_configuration_12, channel_configuration_13,
152 channel_configuration_14};
153
154 #define TP_USAC_MAX_CHANNEL_CONFIGURATION_INDEX (13)
155 #define SC_CHANNEL_CONFIG_TAB_SIZE (TP_USAC_MAX_CHANNEL_CONFIGURATION_INDEX + 1)
156
157 /* channel config structure used for sanity check */
158 typedef struct {
159 SCHAR nCh; /* number of channels */
160 SCHAR nSCE; /* number of SCE's */
161 SCHAR nCPE; /* number of CPE's */
162 SCHAR nLFE; /* number of LFE's */
163 } SC_CHANNEL_CONFIG;
164
165 static const SC_CHANNEL_CONFIG sc_chan_config_tab[SC_CHANNEL_CONFIG_TAB_SIZE] =
166 {
167 /* nCh, nSCE, nCPE, nLFE, cci */
168 {0, 0, 0, 0}, /* 0 */
169 {1, 1, 0, 0}, /* 1 */
170 {2, 0, 1, 0}, /* 2 */
171 {3, 1, 1, 0}, /* 3 */
172 {4, 2, 1, 0}, /* 4 */
173 {5, 1, 2, 0}, /* 5 */
174 {6, 1, 2, 1}, /* 6 */
175 {8, 1, 3, 1}, /* 7 */
176 {2, 2, 0, 0}, /* 8 */
177 {3, 1, 1, 0}, /* 9 */
178 {4, 0, 2, 0}, /* 10 */
179 {7, 2, 2, 1}, /* 11 */
180 {8, 1, 3, 1}, /* 12 */
181 {24, 6, 8, 2} /* 13 */
182 };
183
CProgramConfig_Reset(CProgramConfig * pPce)184 void CProgramConfig_Reset(CProgramConfig *pPce) { pPce->elCounter = 0; }
185
CProgramConfig_Init(CProgramConfig * pPce)186 void CProgramConfig_Init(CProgramConfig *pPce) {
187 FDKmemclear(pPce, sizeof(CProgramConfig));
188 pPce->SamplingFrequencyIndex = 0xf;
189 }
190
CProgramConfig_IsValid(const CProgramConfig * pPce)191 int CProgramConfig_IsValid(const CProgramConfig *pPce) {
192 return ((pPce->isValid) ? 1 : 0);
193 }
194
195 #define PCE_HEIGHT_EXT_SYNC (0xAC)
196
197 /*
198 * Read the extension for height info.
199 * return 0 if successfull,
200 * -1 if the CRC failed,
201 * -2 if invalid HeightInfo.
202 */
CProgramConfig_ReadHeightExt(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,int * const bytesAvailable,const UINT alignmentAnchor)203 static int CProgramConfig_ReadHeightExt(CProgramConfig *pPce,
204 HANDLE_FDK_BITSTREAM bs,
205 int *const bytesAvailable,
206 const UINT alignmentAnchor) {
207 int err = 0;
208 FDK_CRCINFO crcInfo; /* CRC state info */
209 INT crcReg;
210 FDKcrcInit(&crcInfo, 0x07, 0xFF, 8);
211 crcReg = FDKcrcStartReg(&crcInfo, bs, 0);
212 UINT startAnchor = FDKgetValidBits(bs);
213
214 FDK_ASSERT(pPce != NULL);
215 FDK_ASSERT(bs != NULL);
216 FDK_ASSERT(bytesAvailable != NULL);
217
218 if ((startAnchor >= 24) && (*bytesAvailable >= 3) &&
219 (FDKreadBits(bs, 8) == PCE_HEIGHT_EXT_SYNC)) {
220 int i;
221
222 for (i = 0; i < pPce->NumFrontChannelElements; i++) {
223 if ((pPce->FrontElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
224 PC_NUM_HEIGHT_LAYER) {
225 err = -2; /* height information is out of the valid range */
226 }
227 }
228 for (i = 0; i < pPce->NumSideChannelElements; i++) {
229 if ((pPce->SideElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
230 PC_NUM_HEIGHT_LAYER) {
231 err = -2; /* height information is out of the valid range */
232 }
233 }
234 for (i = 0; i < pPce->NumBackChannelElements; i++) {
235 if ((pPce->BackElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
236 PC_NUM_HEIGHT_LAYER) {
237 err = -2; /* height information is out of the valid range */
238 }
239 }
240 FDKbyteAlign(bs, alignmentAnchor);
241
242 FDKcrcEndReg(&crcInfo, bs, crcReg);
243 if ((USHORT)FDKreadBits(bs, 8) != FDKcrcGetCRC(&crcInfo)) {
244 /* CRC failed */
245 err = -1;
246 }
247 if (err != 0) {
248 /* Reset whole height information in case an error occured during parsing.
249 The return value ensures that pPce->isValid is set to 0 and implicit
250 channel mapping is used. */
251 FDKmemclear(pPce->FrontElementHeightInfo,
252 sizeof(pPce->FrontElementHeightInfo));
253 FDKmemclear(pPce->SideElementHeightInfo,
254 sizeof(pPce->SideElementHeightInfo));
255 FDKmemclear(pPce->BackElementHeightInfo,
256 sizeof(pPce->BackElementHeightInfo));
257 }
258 } else {
259 /* No valid extension data found -> restore the initial bitbuffer state */
260 FDKpushBack(bs, (INT)startAnchor - (INT)FDKgetValidBits(bs));
261 }
262
263 /* Always report the bytes read. */
264 *bytesAvailable -= ((INT)startAnchor - (INT)FDKgetValidBits(bs)) >> 3;
265
266 return (err);
267 }
268
CProgramConfig_Read(CProgramConfig * pPce,HANDLE_FDK_BITSTREAM bs,UINT alignmentAnchor)269 void CProgramConfig_Read(CProgramConfig *pPce, HANDLE_FDK_BITSTREAM bs,
270 UINT alignmentAnchor) {
271 int i, err = 0;
272 int commentBytes;
273
274 pPce->NumEffectiveChannels = 0;
275 pPce->NumChannels = 0;
276 pPce->ElementInstanceTag = (UCHAR)FDKreadBits(bs, 4);
277 pPce->Profile = (UCHAR)FDKreadBits(bs, 2);
278 pPce->SamplingFrequencyIndex = (UCHAR)FDKreadBits(bs, 4);
279 pPce->NumFrontChannelElements = (UCHAR)FDKreadBits(bs, 4);
280 pPce->NumSideChannelElements = (UCHAR)FDKreadBits(bs, 4);
281 pPce->NumBackChannelElements = (UCHAR)FDKreadBits(bs, 4);
282 pPce->NumLfeChannelElements = (UCHAR)FDKreadBits(bs, 2);
283 pPce->NumAssocDataElements = (UCHAR)FDKreadBits(bs, 3);
284 pPce->NumValidCcElements = (UCHAR)FDKreadBits(bs, 4);
285
286 if ((pPce->MonoMixdownPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
287 pPce->MonoMixdownElementNumber = (UCHAR)FDKreadBits(bs, 4);
288 }
289
290 if ((pPce->StereoMixdownPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
291 pPce->StereoMixdownElementNumber = (UCHAR)FDKreadBits(bs, 4);
292 }
293
294 if ((pPce->MatrixMixdownIndexPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
295 pPce->MatrixMixdownIndex = (UCHAR)FDKreadBits(bs, 2);
296 pPce->PseudoSurroundEnable = (UCHAR)FDKreadBits(bs, 1);
297 }
298
299 for (i = 0; i < pPce->NumFrontChannelElements; i++) {
300 pPce->FrontElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
301 pPce->FrontElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
302 pPce->NumChannels += pPce->FrontElementIsCpe[i] ? 2 : 1;
303 }
304
305 for (i = 0; i < pPce->NumSideChannelElements; i++) {
306 pPce->SideElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
307 pPce->SideElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
308 pPce->NumChannels += pPce->SideElementIsCpe[i] ? 2 : 1;
309 }
310
311 for (i = 0; i < pPce->NumBackChannelElements; i++) {
312 pPce->BackElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
313 pPce->BackElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
314 pPce->NumChannels += pPce->BackElementIsCpe[i] ? 2 : 1;
315 }
316
317 pPce->NumEffectiveChannels = pPce->NumChannels;
318
319 for (i = 0; i < pPce->NumLfeChannelElements; i++) {
320 pPce->LfeElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
321 pPce->NumChannels += 1;
322 }
323
324 for (i = 0; i < pPce->NumAssocDataElements; i++) {
325 pPce->AssocDataElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
326 }
327
328 for (i = 0; i < pPce->NumValidCcElements; i++) {
329 pPce->CcElementIsIndSw[i] = (UCHAR)FDKreadBits(bs, 1);
330 pPce->ValidCcElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
331 }
332
333 FDKbyteAlign(bs, alignmentAnchor);
334
335 pPce->CommentFieldBytes = (UCHAR)FDKreadBits(bs, 8);
336 commentBytes = pPce->CommentFieldBytes;
337
338 /* Search for height info extension and read it if available */
339 err = CProgramConfig_ReadHeightExt(pPce, bs, &commentBytes, alignmentAnchor);
340
341 for (i = 0; i < commentBytes; i++) {
342 UCHAR text;
343
344 text = (UCHAR)FDKreadBits(bs, 8);
345
346 if (i < PC_COMMENTLENGTH) {
347 pPce->Comment[i] = text;
348 }
349 }
350
351 pPce->isValid = (err) ? 0 : 1;
352 }
353
354 /*
355 * Compare two program configurations.
356 * Returns the result of the comparison:
357 * -1 - completely different
358 * 0 - completely equal
359 * 1 - different but same channel configuration
360 * 2 - different channel configuration but same number of channels
361 */
CProgramConfig_Compare(const CProgramConfig * const pPce1,const CProgramConfig * const pPce2)362 int CProgramConfig_Compare(const CProgramConfig *const pPce1,
363 const CProgramConfig *const pPce2) {
364 int result = 0; /* Innocent until proven false. */
365
366 if (FDKmemcmp(pPce1, pPce2, sizeof(CProgramConfig)) !=
367 0) { /* Configurations are not completely equal.
368 So look into details and analyse the channel configurations: */
369 result = -1;
370
371 if (pPce1->NumChannels ==
372 pPce2->NumChannels) { /* Now the logic changes. We first assume to have
373 the same channel configuration and then prove
374 if this assumption is true. */
375 result = 1;
376
377 /* Front channels */
378 if (pPce1->NumFrontChannelElements != pPce2->NumFrontChannelElements) {
379 result = 2; /* different number of front channel elements */
380 } else {
381 int el, numCh1 = 0, numCh2 = 0;
382 for (el = 0; el < pPce1->NumFrontChannelElements; el += 1) {
383 if (pPce1->FrontElementHeightInfo[el] !=
384 pPce2->FrontElementHeightInfo[el]) {
385 result = 2; /* different height info */
386 break;
387 }
388 numCh1 += pPce1->FrontElementIsCpe[el] ? 2 : 1;
389 numCh2 += pPce2->FrontElementIsCpe[el] ? 2 : 1;
390 }
391 if (numCh1 != numCh2) {
392 result = 2; /* different number of front channels */
393 }
394 }
395 /* Side channels */
396 if (pPce1->NumSideChannelElements != pPce2->NumSideChannelElements) {
397 result = 2; /* different number of side channel elements */
398 } else {
399 int el, numCh1 = 0, numCh2 = 0;
400 for (el = 0; el < pPce1->NumSideChannelElements; el += 1) {
401 if (pPce1->SideElementHeightInfo[el] !=
402 pPce2->SideElementHeightInfo[el]) {
403 result = 2; /* different height info */
404 break;
405 }
406 numCh1 += pPce1->SideElementIsCpe[el] ? 2 : 1;
407 numCh2 += pPce2->SideElementIsCpe[el] ? 2 : 1;
408 }
409 if (numCh1 != numCh2) {
410 result = 2; /* different number of side channels */
411 }
412 }
413 /* Back channels */
414 if (pPce1->NumBackChannelElements != pPce2->NumBackChannelElements) {
415 result = 2; /* different number of back channel elements */
416 } else {
417 int el, numCh1 = 0, numCh2 = 0;
418 for (el = 0; el < pPce1->NumBackChannelElements; el += 1) {
419 if (pPce1->BackElementHeightInfo[el] !=
420 pPce2->BackElementHeightInfo[el]) {
421 result = 2; /* different height info */
422 break;
423 }
424 numCh1 += pPce1->BackElementIsCpe[el] ? 2 : 1;
425 numCh2 += pPce2->BackElementIsCpe[el] ? 2 : 1;
426 }
427 if (numCh1 != numCh2) {
428 result = 2; /* different number of back channels */
429 }
430 }
431 /* LFE channels */
432 if (pPce1->NumLfeChannelElements != pPce2->NumLfeChannelElements) {
433 result = 2; /* different number of lfe channels */
434 }
435 /* LFEs are always SCEs so we don't need to count the channels. */
436 }
437 }
438
439 return result;
440 }
441
CProgramConfig_GetDefault(CProgramConfig * pPce,const UINT channelConfig)442 void CProgramConfig_GetDefault(CProgramConfig *pPce, const UINT channelConfig) {
443 FDK_ASSERT(pPce != NULL);
444
445 /* Init PCE */
446 CProgramConfig_Init(pPce);
447 pPce->Profile =
448 1; /* Set AAC LC because it is the only supported object type. */
449
450 switch (channelConfig) {
451 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
452 case 32: /* 7.1 side channel configuration as defined in FDK_audio.h */
453 pPce->NumFrontChannelElements = 2;
454 pPce->FrontElementIsCpe[0] = 0;
455 pPce->FrontElementIsCpe[1] = 1;
456 pPce->NumSideChannelElements = 1;
457 pPce->SideElementIsCpe[0] = 1;
458 pPce->NumBackChannelElements = 1;
459 pPce->BackElementIsCpe[0] = 1;
460 pPce->NumLfeChannelElements = 1;
461 pPce->NumChannels = 8;
462 pPce->NumEffectiveChannels = 7;
463 pPce->isValid = 1;
464 break;
465 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
466 case 12: /* 3/0/4.1ch surround back */
467 pPce->BackElementIsCpe[1] = 1;
468 pPce->NumChannels += 1;
469 pPce->NumEffectiveChannels += 1;
470 case 11: /* 3/0/3.1ch */
471 pPce->NumFrontChannelElements += 2;
472 pPce->FrontElementIsCpe[0] = 0;
473 pPce->FrontElementIsCpe[1] = 1;
474 pPce->NumBackChannelElements += 2;
475 pPce->BackElementIsCpe[0] = 1;
476 pPce->BackElementIsCpe[1] += 0;
477 pPce->NumLfeChannelElements += 1;
478 pPce->NumChannels += 7;
479 pPce->NumEffectiveChannels += 6;
480 pPce->isValid = 1;
481 break;
482 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
483 case 14: /* 2/0/0-3/0/2-0.1ch front height */
484 pPce->FrontElementHeightInfo[2] = 1; /* Top speaker */
485 case 7: /* 5/0/2.1ch front */
486 pPce->NumFrontChannelElements += 1;
487 pPce->FrontElementIsCpe[2] = 1;
488 pPce->NumChannels += 2;
489 pPce->NumEffectiveChannels += 2;
490 case 6: /* 3/0/2.1ch */
491 pPce->NumLfeChannelElements += 1;
492 pPce->NumChannels += 1;
493 case 5: /* 3/0/2.0ch */
494 case 4: /* 3/0/1.0ch */
495 pPce->NumBackChannelElements += 1;
496 pPce->BackElementIsCpe[0] = (channelConfig > 4) ? 1 : 0;
497 pPce->NumChannels += (channelConfig > 4) ? 2 : 1;
498 pPce->NumEffectiveChannels += (channelConfig > 4) ? 2 : 1;
499 case 3: /* 3/0/0.0ch */
500 pPce->NumFrontChannelElements += 1;
501 pPce->FrontElementIsCpe[1] = 1;
502 pPce->NumChannels += 2;
503 pPce->NumEffectiveChannels += 2;
504 case 1: /* 1/0/0.0ch */
505 pPce->NumFrontChannelElements += 1;
506 pPce->FrontElementIsCpe[0] = 0;
507 pPce->NumChannels += 1;
508 pPce->NumEffectiveChannels += 1;
509 pPce->isValid = 1;
510 break;
511 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
512 case 2: /* 2/0/0.ch */
513 pPce->NumFrontChannelElements = 1;
514 pPce->FrontElementIsCpe[0] = 1;
515 pPce->NumChannels += 2;
516 pPce->NumEffectiveChannels += 2;
517 pPce->isValid = 1;
518 break;
519 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
520 default:
521 pPce->isValid = 0; /* To be explicit! */
522 break;
523 }
524
525 if (pPce->isValid) {
526 /* Create valid element instance tags */
527 int el, elTagSce = 0, elTagCpe = 0;
528
529 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
530 pPce->FrontElementTagSelect[el] =
531 (pPce->FrontElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
532 }
533 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
534 pPce->SideElementTagSelect[el] =
535 (pPce->SideElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
536 }
537 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
538 pPce->BackElementTagSelect[el] =
539 (pPce->BackElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
540 }
541 elTagSce = 0;
542 for (el = 0; el < pPce->NumLfeChannelElements; el += 1) {
543 pPce->LfeElementTagSelect[el] = elTagSce++;
544 }
545 }
546 }
547
548 /**
549 * \brief get implicit audio channel type for given channelConfig and MPEG
550 * ordered channel index
551 * \param channelConfig MPEG channelConfiguration from 1 upto 14
552 * \param index MPEG channel order index
553 * \return audio channel type.
554 */
getImplicitAudioChannelTypeAndIndex(AUDIO_CHANNEL_TYPE * chType,UCHAR * chIndex,UINT channelConfig,UINT index)555 static void getImplicitAudioChannelTypeAndIndex(AUDIO_CHANNEL_TYPE *chType,
556 UCHAR *chIndex,
557 UINT channelConfig,
558 UINT index) {
559 if (index < 3) {
560 *chType = ACT_FRONT;
561 *chIndex = index;
562 } else {
563 switch (channelConfig) {
564 case 4: /* SCE, CPE, SCE */
565 case 5: /* SCE, CPE, CPE */
566 case 6: /* SCE, CPE, CPE, LFE */
567 switch (index) {
568 case 3:
569 case 4:
570 *chType = ACT_BACK;
571 *chIndex = index - 3;
572 break;
573 case 5:
574 *chType = ACT_LFE;
575 *chIndex = 0;
576 break;
577 }
578 break;
579 case 7: /* SCE,CPE,CPE,CPE,LFE */
580 switch (index) {
581 case 3:
582 case 4:
583 *chType = ACT_FRONT;
584 *chIndex = index;
585 break;
586 case 5:
587 case 6:
588 *chType = ACT_BACK;
589 *chIndex = index - 5;
590 break;
591 case 7:
592 *chType = ACT_LFE;
593 *chIndex = 0;
594 break;
595 }
596 break;
597 case 11: /* SCE,CPE,CPE,SCE,LFE */
598 if (index < 6) {
599 *chType = ACT_BACK;
600 *chIndex = index - 3;
601 } else {
602 *chType = ACT_LFE;
603 *chIndex = 0;
604 }
605 break;
606 case 12: /* SCE,CPE,CPE,CPE,LFE */
607 if (index < 7) {
608 *chType = ACT_BACK;
609 *chIndex = index - 3;
610 } else {
611 *chType = ACT_LFE;
612 *chIndex = 0;
613 }
614 break;
615 case 14: /* SCE,CPE,CPE,LFE,CPE */
616 switch (index) {
617 case 3:
618 case 4:
619 *chType = ACT_BACK;
620 *chIndex = index - 3;
621 break;
622 case 5:
623 *chType = ACT_LFE;
624 *chIndex = 0;
625 break;
626 case 6:
627 case 7:
628 *chType = ACT_FRONT_TOP;
629 *chIndex = index - 6; /* handle the top layer independently */
630 break;
631 }
632 break;
633 default:
634 *chType = ACT_NONE;
635 break;
636 }
637 }
638 }
639
CProgramConfig_LookupElement(CProgramConfig * pPce,UINT channelConfig,const UINT tag,const UINT channelIdx,UCHAR chMapping[],AUDIO_CHANNEL_TYPE chType[],UCHAR chIndex[],const UINT chDescrLen,UCHAR * elMapping,MP4_ELEMENT_ID elList[],MP4_ELEMENT_ID elType)640 int CProgramConfig_LookupElement(CProgramConfig *pPce, UINT channelConfig,
641 const UINT tag, const UINT channelIdx,
642 UCHAR chMapping[], AUDIO_CHANNEL_TYPE chType[],
643 UCHAR chIndex[], const UINT chDescrLen,
644 UCHAR *elMapping, MP4_ELEMENT_ID elList[],
645 MP4_ELEMENT_ID elType) {
646 if (channelConfig > 0) {
647 /* Constant channel mapping must have
648 been set during initialization. */
649 if (IS_CHANNEL_ELEMENT(elType)) {
650 *elMapping = pPce->elCounter;
651 if (elList[pPce->elCounter] != elType &&
652 !IS_USAC_CHANNEL_ELEMENT(elType)) {
653 /* Not in the list */
654 if ((channelConfig == 2) &&
655 (elType == ID_SCE)) { /* This scenario occurs with HE-AAC v2 streams
656 of buggy encoders. In other decoder
657 implementations decoding of this kind of
658 streams is desired. */
659 channelConfig = 1;
660 } else if ((elList[pPce->elCounter] == ID_LFE) &&
661 (elType ==
662 ID_SCE)) { /* Decode bitstreams which wrongly use ID_SCE
663 instead of ID_LFE element type. */
664 ;
665 } else {
666 return 0;
667 }
668 }
669 /* Assume all front channels */
670 getImplicitAudioChannelTypeAndIndex(
671 &chType[channelIdx], &chIndex[channelIdx], channelConfig, channelIdx);
672 if (elType == ID_CPE || elType == ID_USAC_CPE) {
673 chType[channelIdx + 1] = chType[channelIdx];
674 chIndex[channelIdx + 1] = chIndex[channelIdx] + 1;
675 }
676 pPce->elCounter++;
677 }
678 /* Accept all non-channel elements, too. */
679 return 1;
680 } else {
681 if ((!pPce->isValid) || (pPce->NumChannels > chDescrLen)) {
682 /* Implicit channel mapping. */
683 if (IS_USAC_CHANNEL_ELEMENT(elType)) {
684 *elMapping = pPce->elCounter++;
685 } else if (IS_MP4_CHANNEL_ELEMENT(elType)) {
686 /* Store all channel element IDs */
687 elList[pPce->elCounter] = elType;
688 *elMapping = pPce->elCounter++;
689 }
690 } else {
691 /* Accept the additional channel(s), only if the tag is in the lists */
692 int isCpe = 0, i;
693 /* Element counter */
694 int ec[PC_NUM_HEIGHT_LAYER] = {0};
695 /* Channel counters */
696 int cc[PC_NUM_HEIGHT_LAYER] = {0};
697 int fc[PC_NUM_HEIGHT_LAYER] = {0}; /* front channel counter */
698 int sc[PC_NUM_HEIGHT_LAYER] = {0}; /* side channel counter */
699 int bc[PC_NUM_HEIGHT_LAYER] = {0}; /* back channel counter */
700 int lc = 0; /* lfe channel counter */
701
702 /* General MPEG (PCE) composition rules:
703 - Over all:
704 <normal height channels><top height channels><bottom height
705 channels>
706 - Within each height layer:
707 <front channels><side channels><back channels>
708 - Exception:
709 The LFE channels have no height info and thus they are arranged at
710 the very end of the normal height layer channels.
711 */
712
713 switch (elType) {
714 case ID_CPE:
715 isCpe = 1;
716 case ID_SCE:
717 /* search in front channels */
718 for (i = 0; i < pPce->NumFrontChannelElements; i++) {
719 int heightLayer = pPce->FrontElementHeightInfo[i];
720 if (isCpe == pPce->FrontElementIsCpe[i] &&
721 pPce->FrontElementTagSelect[i] == tag) {
722 int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
723 AUDIO_CHANNEL_TYPE aChType =
724 (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_FRONT);
725 for (h = heightLayer - 1; h >= 0; h -= 1) {
726 int el;
727 /* Count front channels/elements */
728 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
729 if (pPce->FrontElementHeightInfo[el] == h) {
730 elIdx += 1;
731 chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
732 }
733 }
734 /* Count side channels/elements */
735 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
736 if (pPce->SideElementHeightInfo[el] == h) {
737 elIdx += 1;
738 chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
739 }
740 }
741 /* Count back channels/elements */
742 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
743 if (pPce->BackElementHeightInfo[el] == h) {
744 elIdx += 1;
745 chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
746 }
747 }
748 if (h == 0) { /* normal height */
749 elIdx += pPce->NumLfeChannelElements;
750 chIdx += pPce->NumLfeChannelElements;
751 }
752 }
753 chMapping[chIdx] = channelIdx;
754 chType[chIdx] = aChType;
755 chIndex[chIdx] = fc[heightLayer];
756 if (isCpe) {
757 chMapping[chIdx + 1] = channelIdx + 1;
758 chType[chIdx + 1] = aChType;
759 chIndex[chIdx + 1] = fc[heightLayer] + 1;
760 }
761 *elMapping = elIdx;
762 return 1;
763 }
764 ec[heightLayer] += 1;
765 if (pPce->FrontElementIsCpe[i]) {
766 cc[heightLayer] += 2;
767 fc[heightLayer] += 2;
768 } else {
769 cc[heightLayer] += 1;
770 fc[heightLayer] += 1;
771 }
772 }
773 /* search in side channels */
774 for (i = 0; i < pPce->NumSideChannelElements; i++) {
775 int heightLayer = pPce->SideElementHeightInfo[i];
776 if (isCpe == pPce->SideElementIsCpe[i] &&
777 pPce->SideElementTagSelect[i] == tag) {
778 int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
779 AUDIO_CHANNEL_TYPE aChType =
780 (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_SIDE);
781 for (h = heightLayer - 1; h >= 0; h -= 1) {
782 int el;
783 /* Count front channels/elements */
784 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
785 if (pPce->FrontElementHeightInfo[el] == h) {
786 elIdx += 1;
787 chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
788 }
789 }
790 /* Count side channels/elements */
791 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
792 if (pPce->SideElementHeightInfo[el] == h) {
793 elIdx += 1;
794 chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
795 }
796 }
797 /* Count back channels/elements */
798 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
799 if (pPce->BackElementHeightInfo[el] == h) {
800 elIdx += 1;
801 chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
802 }
803 }
804 if (h ==
805 0) { /* LFE channels belong to the normal height layer */
806 elIdx += pPce->NumLfeChannelElements;
807 chIdx += pPce->NumLfeChannelElements;
808 }
809 }
810 chMapping[chIdx] = channelIdx;
811 chType[chIdx] = aChType;
812 chIndex[chIdx] = sc[heightLayer];
813 if (isCpe) {
814 chMapping[chIdx + 1] = channelIdx + 1;
815 chType[chIdx + 1] = aChType;
816 chIndex[chIdx + 1] = sc[heightLayer] + 1;
817 }
818 *elMapping = elIdx;
819 return 1;
820 }
821 ec[heightLayer] += 1;
822 if (pPce->SideElementIsCpe[i]) {
823 cc[heightLayer] += 2;
824 sc[heightLayer] += 2;
825 } else {
826 cc[heightLayer] += 1;
827 sc[heightLayer] += 1;
828 }
829 }
830 /* search in back channels */
831 for (i = 0; i < pPce->NumBackChannelElements; i++) {
832 int heightLayer = pPce->BackElementHeightInfo[i];
833 if (isCpe == pPce->BackElementIsCpe[i] &&
834 pPce->BackElementTagSelect[i] == tag) {
835 int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
836 AUDIO_CHANNEL_TYPE aChType =
837 (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_BACK);
838 for (h = heightLayer - 1; h >= 0; h -= 1) {
839 int el;
840 /* Count front channels/elements */
841 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
842 if (pPce->FrontElementHeightInfo[el] == h) {
843 elIdx += 1;
844 chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
845 }
846 }
847 /* Count side channels/elements */
848 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
849 if (pPce->SideElementHeightInfo[el] == h) {
850 elIdx += 1;
851 chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
852 }
853 }
854 /* Count back channels/elements */
855 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
856 if (pPce->BackElementHeightInfo[el] == h) {
857 elIdx += 1;
858 chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
859 }
860 }
861 if (h == 0) { /* normal height */
862 elIdx += pPce->NumLfeChannelElements;
863 chIdx += pPce->NumLfeChannelElements;
864 }
865 }
866 chMapping[chIdx] = channelIdx;
867 chType[chIdx] = aChType;
868 chIndex[chIdx] = bc[heightLayer];
869 if (isCpe) {
870 chMapping[chIdx + 1] = channelIdx + 1;
871 chType[chIdx + 1] = aChType;
872 chIndex[chIdx + 1] = bc[heightLayer] + 1;
873 }
874 *elMapping = elIdx;
875 return 1;
876 }
877 ec[heightLayer] += 1;
878 if (pPce->BackElementIsCpe[i]) {
879 cc[heightLayer] += 2;
880 bc[heightLayer] += 2;
881 } else {
882 cc[heightLayer] += 1;
883 bc[heightLayer] += 1;
884 }
885 }
886 break;
887
888 case ID_LFE: { /* Unfortunately we have to go through all normal height
889 layer elements to get the position of the LFE
890 channels. Start with counting the front
891 channels/elements at normal height */
892 for (i = 0; i < pPce->NumFrontChannelElements; i += 1) {
893 int heightLayer = pPce->FrontElementHeightInfo[i];
894 ec[heightLayer] += 1;
895 cc[heightLayer] += (pPce->FrontElementIsCpe[i]) ? 2 : 1;
896 }
897 /* Count side channels/elements at normal height */
898 for (i = 0; i < pPce->NumSideChannelElements; i += 1) {
899 int heightLayer = pPce->SideElementHeightInfo[i];
900 ec[heightLayer] += 1;
901 cc[heightLayer] += (pPce->SideElementIsCpe[i]) ? 2 : 1;
902 }
903 /* Count back channels/elements at normal height */
904 for (i = 0; i < pPce->NumBackChannelElements; i += 1) {
905 int heightLayer = pPce->BackElementHeightInfo[i];
906 ec[heightLayer] += 1;
907 cc[heightLayer] += (pPce->BackElementIsCpe[i]) ? 2 : 1;
908 }
909
910 /* search in lfe channels */
911 for (i = 0; i < pPce->NumLfeChannelElements; i++) {
912 int elIdx =
913 ec[0]; /* LFE channels belong to the normal height layer */
914 int chIdx = cc[0];
915 if (pPce->LfeElementTagSelect[i] == tag) {
916 chMapping[chIdx] = channelIdx;
917 *elMapping = elIdx;
918 chType[chIdx] = ACT_LFE;
919 chIndex[chIdx] = lc;
920 return 1;
921 }
922 ec[0] += 1;
923 cc[0] += 1;
924 lc += 1;
925 }
926 } break;
927
928 /* Non audio elements */
929 case ID_CCE:
930 /* search in cce channels */
931 for (i = 0; i < pPce->NumValidCcElements; i++) {
932 if (pPce->ValidCcElementTagSelect[i] == tag) {
933 return 1;
934 }
935 }
936 break;
937 case ID_DSE:
938 /* search associated data elements */
939 for (i = 0; i < pPce->NumAssocDataElements; i++) {
940 if (pPce->AssocDataElementTagSelect[i] == tag) {
941 return 1;
942 }
943 }
944 break;
945 default:
946 return 0;
947 }
948 return 0; /* not found in any list */
949 }
950 }
951
952 return 1;
953 }
954
955 #define SPEAKER_PLANE_NORMAL 0
956 #define SPEAKER_PLANE_TOP 1
957 #define SPEAKER_PLANE_BOTTOM 2
958
CProgramConfig_GetChannelDescription(const UINT chConfig,const CProgramConfig * pPce,AUDIO_CHANNEL_TYPE chType[],UCHAR chIndex[])959 void CProgramConfig_GetChannelDescription(const UINT chConfig,
960 const CProgramConfig *pPce,
961 AUDIO_CHANNEL_TYPE chType[],
962 UCHAR chIndex[]) {
963 FDK_ASSERT(chType != NULL);
964 FDK_ASSERT(chIndex != NULL);
965
966 if ((chConfig == 0) && (pPce != NULL)) {
967 if (pPce->isValid) {
968 int spkPlane, chIdx = 0;
969 for (spkPlane = SPEAKER_PLANE_NORMAL; spkPlane <= SPEAKER_PLANE_BOTTOM;
970 spkPlane += 1) {
971 int elIdx, grpChIdx = 0;
972 for (elIdx = 0; elIdx < pPce->NumFrontChannelElements; elIdx += 1) {
973 if (pPce->FrontElementHeightInfo[elIdx] == spkPlane) {
974 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_FRONT);
975 chIndex[chIdx++] = grpChIdx++;
976 if (pPce->FrontElementIsCpe[elIdx]) {
977 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_FRONT);
978 chIndex[chIdx++] = grpChIdx++;
979 }
980 }
981 }
982 grpChIdx = 0;
983 for (elIdx = 0; elIdx < pPce->NumSideChannelElements; elIdx += 1) {
984 if (pPce->SideElementHeightInfo[elIdx] == spkPlane) {
985 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_SIDE);
986 chIndex[chIdx++] = grpChIdx++;
987 if (pPce->SideElementIsCpe[elIdx]) {
988 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_SIDE);
989 chIndex[chIdx++] = grpChIdx++;
990 }
991 }
992 }
993 grpChIdx = 0;
994 for (elIdx = 0; elIdx < pPce->NumBackChannelElements; elIdx += 1) {
995 if (pPce->BackElementHeightInfo[elIdx] == spkPlane) {
996 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_BACK);
997 chIndex[chIdx++] = grpChIdx++;
998 if (pPce->BackElementIsCpe[elIdx]) {
999 chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_BACK);
1000 chIndex[chIdx++] = grpChIdx++;
1001 }
1002 }
1003 }
1004 grpChIdx = 0;
1005 if (spkPlane == SPEAKER_PLANE_NORMAL) {
1006 for (elIdx = 0; elIdx < pPce->NumLfeChannelElements; elIdx += 1) {
1007 chType[chIdx] = ACT_LFE;
1008 chIndex[chIdx++] = grpChIdx++;
1009 }
1010 }
1011 }
1012 }
1013 } else {
1014 int chIdx;
1015 for (chIdx = 0; chIdx < getNumberOfTotalChannels(chConfig); chIdx += 1) {
1016 getImplicitAudioChannelTypeAndIndex(&chType[chIdx], &chIndex[chIdx],
1017 chConfig, chIdx);
1018 }
1019 }
1020 }
1021
CProgramConfig_GetPceChMap(const CProgramConfig * pPce,UCHAR pceChMap[],const UINT pceChMapLen)1022 int CProgramConfig_GetPceChMap(const CProgramConfig *pPce, UCHAR pceChMap[],
1023 const UINT pceChMapLen) {
1024 const UCHAR *nElements = &pPce->NumFrontChannelElements;
1025 const UCHAR *elHeight[3], *elIsCpe[3];
1026 unsigned chIdx, plane, grp, offset, totCh[3], numCh[3][4];
1027
1028 FDK_ASSERT(pPce != NULL);
1029 FDK_ASSERT(pceChMap != NULL);
1030
1031 /* Init counter: */
1032 FDKmemclear(totCh, 3 * sizeof(unsigned));
1033 FDKmemclear(numCh, 3 * 4 * sizeof(unsigned));
1034
1035 /* Analyse PCE: */
1036 elHeight[0] = pPce->FrontElementHeightInfo;
1037 elIsCpe[0] = pPce->FrontElementIsCpe;
1038 elHeight[1] = pPce->SideElementHeightInfo;
1039 elIsCpe[1] = pPce->SideElementIsCpe;
1040 elHeight[2] = pPce->BackElementHeightInfo;
1041 elIsCpe[2] = pPce->BackElementIsCpe;
1042
1043 for (plane = 0; plane <= SPEAKER_PLANE_BOTTOM; plane += 1) {
1044 for (grp = 0; grp < 3; grp += 1) { /* front, side, back */
1045 unsigned el;
1046 for (el = 0; el < nElements[grp]; el += 1) {
1047 if (elHeight[grp][el] == plane) {
1048 unsigned elCh = elIsCpe[grp][el] ? 2 : 1;
1049 numCh[plane][grp] += elCh;
1050 totCh[plane] += elCh;
1051 }
1052 }
1053 }
1054 if (plane == SPEAKER_PLANE_NORMAL) {
1055 unsigned elCh = pPce->NumLfeChannelElements;
1056 numCh[plane][grp] += elCh;
1057 totCh[plane] += elCh;
1058 }
1059 }
1060 /* Sanity checks: */
1061 chIdx = totCh[SPEAKER_PLANE_NORMAL] + totCh[SPEAKER_PLANE_TOP] +
1062 totCh[SPEAKER_PLANE_BOTTOM];
1063 if (chIdx > pceChMapLen) {
1064 return -1;
1065 }
1066
1067 /* Create map: */
1068 offset = grp = 0;
1069 unsigned grpThresh = numCh[SPEAKER_PLANE_NORMAL][grp];
1070 for (chIdx = 0; chIdx < totCh[SPEAKER_PLANE_NORMAL]; chIdx += 1) {
1071 while ((chIdx >= grpThresh) && (grp < 3)) {
1072 offset += numCh[1][grp] + numCh[2][grp];
1073 grp += 1;
1074 grpThresh += numCh[SPEAKER_PLANE_NORMAL][grp];
1075 }
1076 pceChMap[chIdx] = chIdx + offset;
1077 }
1078 offset = 0;
1079 for (grp = 0; grp < 4; grp += 1) { /* front, side, back and lfe */
1080 offset += numCh[SPEAKER_PLANE_NORMAL][grp];
1081 for (plane = SPEAKER_PLANE_TOP; plane <= SPEAKER_PLANE_BOTTOM; plane += 1) {
1082 unsigned mapCh;
1083 for (mapCh = 0; mapCh < numCh[plane][grp]; mapCh += 1) {
1084 pceChMap[chIdx++] = offset;
1085 offset += 1;
1086 }
1087 }
1088 }
1089 return 0;
1090 }
1091
CProgramConfig_GetElementTable(const CProgramConfig * pPce,MP4_ELEMENT_ID elList[],const INT elListSize,UCHAR * pChMapIdx)1092 int CProgramConfig_GetElementTable(const CProgramConfig *pPce,
1093 MP4_ELEMENT_ID elList[],
1094 const INT elListSize, UCHAR *pChMapIdx) {
1095 int i, el = 0;
1096
1097 FDK_ASSERT(elList != NULL);
1098 FDK_ASSERT(pChMapIdx != NULL);
1099 FDK_ASSERT(pPce != NULL);
1100
1101 *pChMapIdx = 0;
1102
1103 if ((elListSize <
1104 pPce->NumFrontChannelElements + pPce->NumSideChannelElements +
1105 pPce->NumBackChannelElements + pPce->NumLfeChannelElements) ||
1106 (pPce->NumChannels == 0)) {
1107 return 0;
1108 }
1109
1110 for (i = 0; i < pPce->NumFrontChannelElements; i += 1) {
1111 elList[el++] = (pPce->FrontElementIsCpe[i]) ? ID_CPE : ID_SCE;
1112 }
1113
1114 for (i = 0; i < pPce->NumSideChannelElements; i += 1) {
1115 elList[el++] = (pPce->SideElementIsCpe[i]) ? ID_CPE : ID_SCE;
1116 }
1117
1118 for (i = 0; i < pPce->NumBackChannelElements; i += 1) {
1119 elList[el++] = (pPce->BackElementIsCpe[i]) ? ID_CPE : ID_SCE;
1120 }
1121
1122 for (i = 0; i < pPce->NumLfeChannelElements; i += 1) {
1123 elList[el++] = ID_LFE;
1124 }
1125
1126 /* Find an corresponding channel configuration if possible */
1127 switch (pPce->NumChannels) {
1128 case 1:
1129 case 2:
1130 /* One and two channels have no alternatives. */
1131 *pChMapIdx = pPce->NumChannels;
1132 break;
1133 case 3:
1134 case 4:
1135 case 5:
1136 case 6: { /* Test if the number of channels can be used as channel config:
1137 */
1138 C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
1139 /* Create a PCE for the config to test ... */
1140 CProgramConfig_GetDefault(tmpPce, pPce->NumChannels);
1141 /* ... and compare it with the given one. */
1142 *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE))
1143 ? pPce->NumChannels
1144 : 0;
1145 /* If compare result is 0 or 1 we can be sure that it is channel
1146 * config 11. */
1147 C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
1148 } break;
1149 case 7: {
1150 C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
1151 /* Create a PCE for the config to test ... */
1152 CProgramConfig_GetDefault(tmpPce, 11);
1153 /* ... and compare it with the given one. */
1154 *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE)) ? 11 : 0;
1155 /* If compare result is 0 or 1 we can be sure that it is channel
1156 * config 11. */
1157 C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
1158 } break;
1159 case 8: { /* Try the four possible 7.1ch configurations. One after the
1160 other. */
1161 UCHAR testCfg[4] = {32, 14, 12, 7};
1162 C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
1163 for (i = 0; i < 4; i += 1) {
1164 /* Create a PCE for the config to test ... */
1165 CProgramConfig_GetDefault(tmpPce, testCfg[i]);
1166 /* ... and compare it with the given one. */
1167 if (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE)) {
1168 /* If the compare result is 0 or 1 than the two channel configurations
1169 * match. */
1170 /* Explicit mapping of 7.1 side channel configuration to 7.1 rear
1171 * channel mapping. */
1172 *pChMapIdx = (testCfg[i] == 32) ? 12 : testCfg[i];
1173 }
1174 }
1175 C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
1176 } break;
1177 default:
1178 /* The PCE does not match any predefined channel configuration. */
1179 *pChMapIdx = 0;
1180 break;
1181 }
1182
1183 return el;
1184 }
1185
getAOT(HANDLE_FDK_BITSTREAM bs)1186 static AUDIO_OBJECT_TYPE getAOT(HANDLE_FDK_BITSTREAM bs) {
1187 int tmp = 0;
1188
1189 tmp = FDKreadBits(bs, 5);
1190 if (tmp == AOT_ESCAPE) {
1191 int tmp2 = FDKreadBits(bs, 6);
1192 tmp = 32 + tmp2;
1193 }
1194
1195 return (AUDIO_OBJECT_TYPE)tmp;
1196 }
1197
getSampleRate(HANDLE_FDK_BITSTREAM bs,UCHAR * index,int nBits)1198 static INT getSampleRate(HANDLE_FDK_BITSTREAM bs, UCHAR *index, int nBits) {
1199 INT sampleRate;
1200 int idx;
1201
1202 idx = FDKreadBits(bs, nBits);
1203 if (idx == (1 << nBits) - 1) {
1204 if (FDKgetValidBits(bs) < 24) {
1205 return 0;
1206 }
1207 sampleRate = FDKreadBits(bs, 24);
1208 } else {
1209 sampleRate = SamplingRateTable[idx];
1210 }
1211
1212 *index = idx;
1213
1214 return sampleRate;
1215 }
1216
GaSpecificConfig_Parse(CSGaSpecificConfig * self,CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM bs,UINT ascStartAnchor)1217 static TRANSPORTDEC_ERROR GaSpecificConfig_Parse(CSGaSpecificConfig *self,
1218 CSAudioSpecificConfig *asc,
1219 HANDLE_FDK_BITSTREAM bs,
1220 UINT ascStartAnchor) {
1221 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1222
1223 self->m_frameLengthFlag = FDKreadBits(bs, 1);
1224
1225 self->m_dependsOnCoreCoder = FDKreadBits(bs, 1);
1226
1227 if (self->m_dependsOnCoreCoder) self->m_coreCoderDelay = FDKreadBits(bs, 14);
1228
1229 self->m_extensionFlag = FDKreadBits(bs, 1);
1230
1231 if (asc->m_channelConfiguration == 0) {
1232 CProgramConfig_Read(&asc->m_progrConfigElement, bs, ascStartAnchor);
1233 }
1234
1235 if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
1236 self->m_layer = FDKreadBits(bs, 3);
1237 }
1238
1239 if (self->m_extensionFlag) {
1240 if (asc->m_aot == AOT_ER_BSAC) {
1241 self->m_numOfSubFrame = FDKreadBits(bs, 5);
1242 self->m_layerLength = FDKreadBits(bs, 11);
1243 }
1244
1245 if ((asc->m_aot == AOT_ER_AAC_LC) || (asc->m_aot == AOT_ER_AAC_LTP) ||
1246 (asc->m_aot == AOT_ER_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_LD)) {
1247 asc->m_vcb11Flag = FDKreadBits(bs, 1); /* aacSectionDataResilienceFlag */
1248 asc->m_rvlcFlag =
1249 FDKreadBits(bs, 1); /* aacScalefactorDataResilienceFlag */
1250 asc->m_hcrFlag = FDKreadBits(bs, 1); /* aacSpectralDataResilienceFlag */
1251 }
1252
1253 self->m_extensionFlag3 = FDKreadBits(bs, 1);
1254 }
1255 return (ErrorStatus);
1256 }
1257
skipSbrHeader(HANDLE_FDK_BITSTREAM hBs,int isUsac)1258 static INT skipSbrHeader(HANDLE_FDK_BITSTREAM hBs, int isUsac) {
1259 /* Dummy parse SbrDfltHeader() */
1260 INT dflt_header_extra1, dflt_header_extra2, bitsToSkip = 0;
1261
1262 if (!isUsac) {
1263 bitsToSkip = 6;
1264 FDKpushFor(hBs, 6); /* amp res 1, xover freq 3, reserved 2 */
1265 }
1266 bitsToSkip += 8;
1267 FDKpushFor(hBs, 8); /* start / stop freq */
1268 bitsToSkip += 2;
1269 dflt_header_extra1 = FDKreadBit(hBs);
1270 dflt_header_extra2 = FDKreadBit(hBs);
1271 bitsToSkip += 5 * dflt_header_extra1 + 6 * dflt_header_extra2;
1272 FDKpushFor(hBs, 5 * dflt_header_extra1 + 6 * dflt_header_extra2);
1273
1274 return bitsToSkip;
1275 }
1276
ld_sbr_header(CSAudioSpecificConfig * asc,const INT dsFactor,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1277 static INT ld_sbr_header(CSAudioSpecificConfig *asc, const INT dsFactor,
1278 HANDLE_FDK_BITSTREAM hBs, CSTpCallBacks *cb) {
1279 const int channelConfiguration = asc->m_channelConfiguration;
1280 int i = 0, j = 0;
1281 INT error = 0;
1282 MP4_ELEMENT_ID element = ID_NONE;
1283
1284 /* check whether the channelConfiguration is defined in
1285 * channel_configuration_array */
1286 if (channelConfiguration < 0 ||
1287 channelConfiguration > (INT)(sizeof(channel_configuration_array) /
1288 sizeof(MP4_ELEMENT_ID **) -
1289 1)) {
1290 return TRANSPORTDEC_PARSE_ERROR;
1291 }
1292
1293 /* read elements of the passed channel_configuration until there is ID_NONE */
1294 while ((element = channel_configuration_array[channelConfiguration][j]) !=
1295 ID_NONE) {
1296 if (element == ID_SCE || element == ID_CPE) {
1297 error |= cb->cbSbr(
1298 cb->cbSbrData, hBs, asc->m_samplingFrequency / dsFactor,
1299 asc->m_extensionSamplingFrequency / dsFactor,
1300 asc->m_samplesPerFrame / dsFactor, AOT_ER_AAC_ELD, element, i++, 0, 0,
1301 asc->configMode, &asc->SbrConfigChanged, dsFactor);
1302 if (error != TRANSPORTDEC_OK) {
1303 goto bail;
1304 }
1305 }
1306 j++;
1307 }
1308 bail:
1309 return error;
1310 }
1311
EldSpecificConfig_Parse(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1312 static TRANSPORTDEC_ERROR EldSpecificConfig_Parse(CSAudioSpecificConfig *asc,
1313 HANDLE_FDK_BITSTREAM hBs,
1314 CSTpCallBacks *cb) {
1315 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1316 CSEldSpecificConfig *esc = &asc->m_sc.m_eldSpecificConfig;
1317 ASC_ELD_EXT_TYPE eldExtType;
1318 int eldExtLen, len, cnt, ldSbrLen = 0, eldExtLenSum, numSbrHeader = 0,
1319 sbrIndex;
1320
1321 unsigned char downscale_fill_nibble;
1322
1323 FDKmemclear(esc, sizeof(CSEldSpecificConfig));
1324
1325 esc->m_frameLengthFlag = FDKreadBits(hBs, 1);
1326 if (esc->m_frameLengthFlag) {
1327 asc->m_samplesPerFrame = 480;
1328 } else {
1329 asc->m_samplesPerFrame = 512;
1330 }
1331
1332 asc->m_vcb11Flag = FDKreadBits(hBs, 1);
1333 asc->m_rvlcFlag = FDKreadBits(hBs, 1);
1334 asc->m_hcrFlag = FDKreadBits(hBs, 1);
1335
1336 esc->m_sbrPresentFlag = FDKreadBits(hBs, 1);
1337
1338 if (esc->m_sbrPresentFlag == 1) {
1339 esc->m_sbrSamplingRate =
1340 FDKreadBits(hBs, 1); /* 0: single rate, 1: dual rate */
1341 esc->m_sbrCrcFlag = FDKreadBits(hBs, 1);
1342
1343 asc->m_extensionSamplingFrequency = asc->m_samplingFrequency
1344 << esc->m_sbrSamplingRate;
1345
1346 if (cb->cbSbr != NULL) {
1347 /* ELD reduced delay mode: LD-SBR initialization has to know the downscale
1348 information. Postpone LD-SBR initialization and read ELD extension
1349 information first. */
1350 switch (asc->m_channelConfiguration) {
1351 case 1:
1352 case 2:
1353 numSbrHeader = 1;
1354 break;
1355 case 3:
1356 numSbrHeader = 2;
1357 break;
1358 case 4:
1359 case 5:
1360 case 6:
1361 numSbrHeader = 3;
1362 break;
1363 case 7:
1364 case 11:
1365 case 12:
1366 case 14:
1367 numSbrHeader = 4;
1368 break;
1369 default:
1370 numSbrHeader = 0;
1371 break;
1372 }
1373 for (sbrIndex = 0; sbrIndex < numSbrHeader; sbrIndex++) {
1374 ldSbrLen += skipSbrHeader(hBs, 0);
1375 }
1376 } else {
1377 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1378 }
1379 }
1380 esc->m_useLdQmfTimeAlign = 0;
1381
1382 /* new ELD syntax */
1383 eldExtLenSum = FDKgetValidBits(hBs);
1384 esc->m_downscaledSamplingFrequency = asc->m_samplingFrequency;
1385 /* parse ExtTypeConfigData */
1386 while (
1387 ((eldExtType = (ASC_ELD_EXT_TYPE)FDKreadBits(hBs, 4)) != ELDEXT_TERM) &&
1388 ((INT)FDKgetValidBits(hBs) >= 0)) {
1389 eldExtLen = len = FDKreadBits(hBs, 4);
1390 if (len == 0xf) {
1391 len = FDKreadBits(hBs, 8);
1392 eldExtLen += len;
1393
1394 if (len == 0xff) {
1395 len = FDKreadBits(hBs, 16);
1396 eldExtLen += len;
1397 }
1398 }
1399
1400 switch (eldExtType) {
1401 case ELDEXT_LDSAC:
1402 esc->m_useLdQmfTimeAlign = 1;
1403 if (cb->cbSsc != NULL) {
1404 ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbSsc(
1405 cb->cbSscData, hBs, asc->m_aot, asc->m_extensionSamplingFrequency,
1406 1, /* stereoConfigIndex */
1407 -1, /* nTimeSlots: read from bitstream */
1408 eldExtLen, asc->configMode, &asc->SacConfigChanged);
1409 if (ErrorStatus != TRANSPORTDEC_OK) {
1410 return TRANSPORTDEC_PARSE_ERROR;
1411 }
1412 break;
1413 }
1414
1415 /* fall-through */
1416 default:
1417 for (cnt = 0; cnt < eldExtLen; cnt++) {
1418 FDKreadBits(hBs, 8);
1419 }
1420 break;
1421
1422 case ELDEXT_DOWNSCALEINFO:
1423 UCHAR tmpDownscaleFreqIdx;
1424 esc->m_downscaledSamplingFrequency =
1425 getSampleRate(hBs, &tmpDownscaleFreqIdx, 4);
1426 if (esc->m_downscaledSamplingFrequency == 0) {
1427 return TRANSPORTDEC_PARSE_ERROR;
1428 }
1429 downscale_fill_nibble = FDKreadBits(hBs, 4);
1430 if (downscale_fill_nibble != 0x0) {
1431 return TRANSPORTDEC_PARSE_ERROR;
1432 }
1433 break;
1434 }
1435 }
1436
1437 if ((INT)FDKgetValidBits(hBs) < 0) {
1438 return TRANSPORTDEC_PARSE_ERROR;
1439 }
1440
1441 if (esc->m_sbrPresentFlag == 1 && numSbrHeader != 0) {
1442 INT dsFactor = 1; /* Downscale factor must be 1 or even for SBR */
1443 if (esc->m_downscaledSamplingFrequency != 0) {
1444 if (asc->m_samplingFrequency % esc->m_downscaledSamplingFrequency != 0) {
1445 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1446 }
1447 dsFactor = asc->m_samplingFrequency / esc->m_downscaledSamplingFrequency;
1448 if (dsFactor != 1 && (dsFactor)&1) {
1449 return TRANSPORTDEC_UNSUPPORTED_FORMAT; /* SBR needs an even downscale
1450 factor */
1451 }
1452 if (dsFactor != 1 && dsFactor != 2 && dsFactor != 4) {
1453 dsFactor = 1; /* don't apply dsf for not yet supported even dsfs */
1454 }
1455 if ((INT)asc->m_samplesPerFrame % dsFactor != 0) {
1456 return TRANSPORTDEC_UNSUPPORTED_FORMAT; /* frameSize/dsf must be an
1457 integer number */
1458 }
1459 }
1460 eldExtLenSum = eldExtLenSum - FDKgetValidBits(hBs);
1461 FDKpushBack(hBs, eldExtLenSum + ldSbrLen);
1462 if (0 != ld_sbr_header(asc, dsFactor, hBs, cb)) {
1463 return TRANSPORTDEC_PARSE_ERROR;
1464 }
1465 FDKpushFor(hBs, eldExtLenSum);
1466 }
1467 return (ErrorStatus);
1468 }
1469
1470 /*
1471 Subroutine to store config in UCHAR buffer. Bit stream position does not change.
1472 */
StoreConfigAsBitstream(HANDLE_FDK_BITSTREAM hBs,const INT configSize_bits,UCHAR * configTargetBuffer,const USHORT configTargetBufferSize_bytes)1473 static UINT StoreConfigAsBitstream(
1474 HANDLE_FDK_BITSTREAM hBs, const INT configSize_bits, /* If < 0 (> 0) config
1475 to read is before
1476 (after) current bit
1477 stream position. */
1478 UCHAR *configTargetBuffer, const USHORT configTargetBufferSize_bytes) {
1479 FDK_BITSTREAM usacConf;
1480 UINT const nBits = fAbs(configSize_bits);
1481 UINT j, tmp;
1482
1483 if (nBits > 8 * (UINT)configTargetBufferSize_bytes) {
1484 return 1;
1485 }
1486 FDKmemclear(configTargetBuffer, configTargetBufferSize_bytes);
1487
1488 FDKinitBitStream(&usacConf, configTargetBuffer, configTargetBufferSize_bytes,
1489 nBits, BS_WRITER);
1490 if (configSize_bits < 0) {
1491 FDKpushBack(hBs, nBits);
1492 }
1493 for (j = nBits; j > 31; j -= 32) {
1494 tmp = FDKreadBits(hBs, 32);
1495 FDKwriteBits(&usacConf, tmp, 32);
1496 }
1497 if (j > 0) {
1498 tmp = FDKreadBits(hBs, j);
1499 FDKwriteBits(&usacConf, tmp, j);
1500 }
1501 FDKsyncCache(&usacConf);
1502 if (configSize_bits > 0) {
1503 FDKpushBack(hBs, nBits);
1504 }
1505
1506 return 0;
1507 }
1508
1509 /* maps coreSbrFrameLengthIndex to coreCoderFrameLength */
1510 static const USHORT usacFrameLength[8] = {768, 1024, 2048, 2048, 4096, 0, 0, 0};
1511 /* maps coreSbrFrameLengthIndex to sbrRatioIndex */
1512 static const UCHAR sbrRatioIndex[8] = {0, 0, 2, 3, 1, 0, 0, 0};
1513
1514 /*
1515 subroutine for parsing extension element configuration:
1516 UsacExtElementConfig() q.v. ISO/IEC FDIS 23003-3:2011(E) Table 14
1517 rsv603daExtElementConfig() q.v. ISO/IEC DIS 23008-3 Table 13
1518 */
extElementConfig(CSUsacExtElementConfig * extElement,HANDLE_FDK_BITSTREAM hBs,const CSTpCallBacks * cb,const UCHAR numSignalsInGroup,const UINT coreFrameLength,const int subStreamIndex,const AUDIO_OBJECT_TYPE aot)1519 static TRANSPORTDEC_ERROR extElementConfig(CSUsacExtElementConfig *extElement,
1520 HANDLE_FDK_BITSTREAM hBs,
1521 const CSTpCallBacks *cb,
1522 const UCHAR numSignalsInGroup,
1523 const UINT coreFrameLength,
1524 const int subStreamIndex,
1525 const AUDIO_OBJECT_TYPE aot) {
1526 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1527
1528 USAC_EXT_ELEMENT_TYPE usacExtElementType =
1529 (USAC_EXT_ELEMENT_TYPE)escapedValue(hBs, 4, 8, 16);
1530
1531 /* recurve extension elements which are invalid for USAC */
1532 if (aot == AOT_USAC) {
1533 switch (usacExtElementType) {
1534 case ID_EXT_ELE_FILL:
1535 case ID_EXT_ELE_MPEGS:
1536 case ID_EXT_ELE_SAOC:
1537 case ID_EXT_ELE_AUDIOPREROLL:
1538 case ID_EXT_ELE_UNI_DRC:
1539 break;
1540 default:
1541 usacExtElementType = ID_EXT_ELE_UNKNOWN;
1542 break;
1543 }
1544 }
1545
1546 extElement->usacExtElementType = usacExtElementType;
1547 int usacExtElementConfigLength = escapedValue(hBs, 4, 8, 16);
1548 extElement->usacExtElementConfigLength = (USHORT)usacExtElementConfigLength;
1549 INT bsAnchor;
1550
1551 if (FDKreadBit(hBs)) /* usacExtElementDefaultLengthPresent */
1552 extElement->usacExtElementDefaultLength = escapedValue(hBs, 8, 16, 0) + 1;
1553 else
1554 extElement->usacExtElementDefaultLength = 0;
1555
1556 extElement->usacExtElementPayloadFrag = FDKreadBit(hBs);
1557
1558 bsAnchor = (INT)FDKgetValidBits(hBs);
1559
1560 switch (usacExtElementType) {
1561 case ID_EXT_ELE_UNKNOWN:
1562 case ID_EXT_ELE_FILL:
1563 break;
1564 case ID_EXT_ELE_AUDIOPREROLL:
1565 /* No configuration element */
1566 extElement->usacExtElementHasAudioPreRoll = 1;
1567 break;
1568 case ID_EXT_ELE_UNI_DRC: {
1569 if (cb->cbUniDrc != NULL) {
1570 ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbUniDrc(
1571 cb->cbUniDrcData, hBs, usacExtElementConfigLength,
1572 0, /* uniDrcConfig */
1573 subStreamIndex, 0, aot);
1574 if (ErrorStatus != TRANSPORTDEC_OK) {
1575 return ErrorStatus;
1576 }
1577 }
1578 } break;
1579 default:
1580 break;
1581 }
1582
1583 /* Adjust bit stream position. This is required because of byte alignment and
1584 * unhandled extensions. */
1585 {
1586 INT left_bits = (usacExtElementConfigLength << 3) -
1587 (bsAnchor - (INT)FDKgetValidBits(hBs));
1588 if (left_bits >= 0) {
1589 FDKpushFor(hBs, left_bits);
1590 } else {
1591 /* parsed too many bits */
1592 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1593 }
1594 }
1595
1596 return ErrorStatus;
1597 }
1598
1599 /*
1600 subroutine for parsing the USAC / RSVD60 configuration extension:
1601 UsacConfigExtension() q.v. ISO/IEC FDIS 23003-3:2011(E) Table 15
1602 rsv603daConfigExtension() q.v. ISO/IEC DIS 23008-3 Table 14
1603 */
configExtension(CSUsacConfig * usc,HANDLE_FDK_BITSTREAM hBs,const CSTpCallBacks * cb)1604 static TRANSPORTDEC_ERROR configExtension(CSUsacConfig *usc,
1605 HANDLE_FDK_BITSTREAM hBs,
1606 const CSTpCallBacks *cb) {
1607 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1608
1609 int numConfigExtensions;
1610 CONFIG_EXT_ID usacConfigExtType;
1611 int usacConfigExtLength;
1612
1613 numConfigExtensions = (int)escapedValue(hBs, 2, 4, 8) + 1;
1614 for (int confExtIdx = 0; confExtIdx < numConfigExtensions; confExtIdx++) {
1615 INT nbits;
1616 int loudnessInfoSetConfigExtensionPosition = FDKgetValidBits(hBs);
1617 usacConfigExtType = (CONFIG_EXT_ID)escapedValue(hBs, 4, 8, 16);
1618 usacConfigExtLength = (int)escapedValue(hBs, 4, 8, 16);
1619
1620 /* Start bit position of config extension */
1621 nbits = (INT)FDKgetValidBits(hBs);
1622
1623 /* Return an error in case the bitbuffer fill level is too low. */
1624 if (nbits < usacConfigExtLength * 8) {
1625 return TRANSPORTDEC_PARSE_ERROR;
1626 }
1627
1628 switch (usacConfigExtType) {
1629 case ID_CONFIG_EXT_FILL:
1630 for (int i = 0; i < usacConfigExtLength; i++) {
1631 if (FDKreadBits(hBs, 8) != 0xa5) {
1632 return TRANSPORTDEC_PARSE_ERROR;
1633 }
1634 }
1635 break;
1636 case ID_CONFIG_EXT_LOUDNESS_INFO: {
1637 if (cb->cbUniDrc != NULL) {
1638 ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbUniDrc(
1639 cb->cbUniDrcData, hBs, usacConfigExtLength,
1640 1, /* loudnessInfoSet */
1641 0, loudnessInfoSetConfigExtensionPosition, AOT_USAC);
1642 if (ErrorStatus != TRANSPORTDEC_OK) {
1643 return ErrorStatus;
1644 }
1645 }
1646 } break;
1647 default:
1648 break;
1649 }
1650
1651 /* Skip remaining bits. If too many bits were parsed, assume error. */
1652 usacConfigExtLength =
1653 8 * usacConfigExtLength - (nbits - (INT)FDKgetValidBits(hBs));
1654 if (usacConfigExtLength < 0) {
1655 return TRANSPORTDEC_PARSE_ERROR;
1656 }
1657 FDKpushFor(hBs, usacConfigExtLength);
1658 }
1659
1660 return ErrorStatus;
1661 }
1662
1663 /* This function unifies decoder config parsing of USAC and RSV60:
1664 rsv603daDecoderConfig() ISO/IEC DIS 23008-3 Table 8
1665 UsacDecoderConfig() ISO/IEC FDIS 23003-3 Table 6
1666 */
UsacRsv60DecoderConfig_Parse(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,const CSTpCallBacks * cb)1667 static TRANSPORTDEC_ERROR UsacRsv60DecoderConfig_Parse(
1668 CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM hBs,
1669 const CSTpCallBacks *cb) {
1670 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
1671 CSUsacConfig *usc = &asc->m_sc.m_usacConfig;
1672 int i, numberOfElements;
1673 int channelElementIdx =
1674 0; /* index for elements which contain audio channels (sce, cpe, lfe) */
1675 SC_CHANNEL_CONFIG sc_chan_config = {0, 0, 0, 0};
1676
1677 numberOfElements = (int)escapedValue(hBs, 4, 8, 16) + 1;
1678 usc->m_usacNumElements = numberOfElements;
1679 if (numberOfElements > TP_USAC_MAX_ELEMENTS) {
1680 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1681 }
1682 usc->m_nUsacChannels = 0;
1683 usc->m_channelConfigurationIndex = asc->m_channelConfiguration;
1684
1685 if (asc->m_aot == AOT_USAC) {
1686 sc_chan_config = sc_chan_config_tab[usc->m_channelConfigurationIndex];
1687
1688 if (sc_chan_config.nCh > (SCHAR)TP_USAC_MAX_SPEAKERS) {
1689 return TRANSPORTDEC_PARSE_ERROR;
1690 }
1691 }
1692
1693 for (i = 0; i < numberOfElements; i++) {
1694 MP4_ELEMENT_ID usacElementType = (MP4_ELEMENT_ID)(
1695 FDKreadBits(hBs, 2) | USAC_ID_BIT); /* set USAC_ID_BIT to map
1696 usacElementType to
1697 MP4_ELEMENT_ID enum */
1698 usc->element[i].usacElementType = usacElementType;
1699
1700 /* sanity check: update element counter */
1701 if (asc->m_aot == AOT_USAC) {
1702 switch (usacElementType) {
1703 case ID_USAC_SCE:
1704 sc_chan_config.nSCE--;
1705 break;
1706 case ID_USAC_CPE:
1707 sc_chan_config.nCPE--;
1708 break;
1709 case ID_USAC_LFE:
1710 sc_chan_config.nLFE--;
1711 break;
1712 default:
1713 break;
1714 }
1715 if (usc->m_channelConfigurationIndex) {
1716 /* sanity check: no element counter may be smaller zero */
1717 if (sc_chan_config.nCPE < 0 || sc_chan_config.nSCE < 0 ||
1718 sc_chan_config.nLFE < 0) {
1719 return TRANSPORTDEC_PARSE_ERROR;
1720 }
1721 }
1722 }
1723
1724 switch (usacElementType) {
1725 case ID_USAC_SCE:
1726 /* UsacCoreConfig() ISO/IEC FDIS 23003-3 Table 10 */
1727 if (FDKreadBit(hBs)) { /* tw_mdct */
1728 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1729 }
1730 usc->element[i].m_noiseFilling = FDKreadBits(hBs, 1);
1731 /* end of UsacCoreConfig() */
1732 if (usc->m_sbrRatioIndex > 0) {
1733 if (cb->cbSbr == NULL) {
1734 return TRANSPORTDEC_UNKOWN_ERROR;
1735 }
1736 /* SbrConfig() ISO/IEC FDIS 23003-3 Table 11 */
1737 usc->element[i].m_harmonicSBR = FDKreadBit(hBs);
1738 usc->element[i].m_interTes = FDKreadBit(hBs);
1739 usc->element[i].m_pvc = FDKreadBit(hBs);
1740 if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
1741 asc->m_extensionSamplingFrequency,
1742 asc->m_samplesPerFrame, asc->m_aot, ID_SCE,
1743 channelElementIdx, usc->element[i].m_harmonicSBR,
1744 usc->element[i].m_stereoConfigIndex, asc->configMode,
1745 &asc->SbrConfigChanged, 1)) {
1746 return TRANSPORTDEC_PARSE_ERROR;
1747 }
1748 /* end of SbrConfig() */
1749 }
1750 usc->m_nUsacChannels += 1;
1751 channelElementIdx++;
1752 break;
1753
1754 case ID_USAC_CPE:
1755 /* UsacCoreConfig() ISO/IEC FDIS 23003-3 Table 10 */
1756 if (FDKreadBit(hBs)) { /* tw_mdct */
1757 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
1758 }
1759 usc->element[i].m_noiseFilling = FDKreadBits(hBs, 1);
1760 /* end of UsacCoreConfig() */
1761 if (usc->m_sbrRatioIndex > 0) {
1762 if (cb->cbSbr == NULL) return TRANSPORTDEC_UNKOWN_ERROR;
1763 /* SbrConfig() ISO/IEC FDIS 23003-3 */
1764 usc->element[i].m_harmonicSBR = FDKreadBit(hBs);
1765 usc->element[i].m_interTes = FDKreadBit(hBs);
1766 usc->element[i].m_pvc = FDKreadBit(hBs);
1767 {
1768 INT bitsToSkip = skipSbrHeader(hBs, 1);
1769 /* read stereoConfigIndex */
1770 usc->element[i].m_stereoConfigIndex = FDKreadBits(hBs, 2);
1771 /* rewind */
1772 FDKpushBack(hBs, bitsToSkip + 2);
1773 }
1774 {
1775 MP4_ELEMENT_ID el_type =
1776 (usc->element[i].m_stereoConfigIndex == 1 ||
1777 usc->element[i].m_stereoConfigIndex == 2)
1778 ? ID_SCE
1779 : ID_CPE;
1780 if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
1781 asc->m_extensionSamplingFrequency,
1782 asc->m_samplesPerFrame, asc->m_aot, el_type,
1783 channelElementIdx, usc->element[i].m_harmonicSBR,
1784 usc->element[i].m_stereoConfigIndex, asc->configMode,
1785 &asc->SbrConfigChanged, 1)) {
1786 return TRANSPORTDEC_PARSE_ERROR;
1787 }
1788 }
1789 /* end of SbrConfig() */
1790
1791 usc->element[i].m_stereoConfigIndex =
1792 FDKreadBits(hBs, 2); /* Needed in RM5 syntax */
1793
1794 if (usc->element[i].m_stereoConfigIndex > 0) {
1795 if (cb->cbSsc != NULL) {
1796 /* Mps212Config() ISO/IEC FDIS 23003-3 */
1797 if (cb->cbSsc(cb->cbSscData, hBs, asc->m_aot,
1798 asc->m_extensionSamplingFrequency,
1799 usc->element[i].m_stereoConfigIndex,
1800 usc->m_coreSbrFrameLengthIndex,
1801 0, /* don't know the length */
1802 asc->configMode, &asc->SacConfigChanged)) {
1803 return TRANSPORTDEC_PARSE_ERROR;
1804 }
1805 /* end of Mps212Config() */
1806 } else {
1807 return TRANSPORTDEC_UNKOWN_ERROR;
1808 }
1809 }
1810 } else {
1811 usc->element[i].m_stereoConfigIndex = 0;
1812 }
1813 usc->m_nUsacChannels += 2;
1814
1815 channelElementIdx++;
1816 break;
1817
1818 case ID_USAC_LFE:
1819 usc->element[i].m_noiseFilling = 0;
1820 usc->m_nUsacChannels += 1;
1821 if (usc->m_sbrRatioIndex > 0) {
1822 /* Use SBR for upsampling */
1823 if (cb->cbSbr == NULL) return ErrorStatus = TRANSPORTDEC_UNKOWN_ERROR;
1824 usc->element[i].m_harmonicSBR = (UCHAR)0;
1825 usc->element[i].m_interTes = (UCHAR)0;
1826 usc->element[i].m_pvc = (UCHAR)0;
1827 if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
1828 asc->m_extensionSamplingFrequency,
1829 asc->m_samplesPerFrame, asc->m_aot, ID_LFE,
1830 channelElementIdx, usc->element[i].m_harmonicSBR,
1831 usc->element[i].m_stereoConfigIndex, asc->configMode,
1832 &asc->SbrConfigChanged, 1)) {
1833 return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
1834 }
1835 }
1836 channelElementIdx++;
1837 break;
1838
1839 case ID_USAC_EXT:
1840 ErrorStatus = extElementConfig(&usc->element[i].extElement, hBs, cb, 0,
1841 asc->m_samplesPerFrame, 0, asc->m_aot);
1842
1843 if (ErrorStatus) {
1844 return ErrorStatus;
1845 }
1846 break;
1847
1848 default:
1849 /* non USAC-element encountered */
1850 return TRANSPORTDEC_PARSE_ERROR;
1851 }
1852 }
1853
1854 if (asc->m_aot == AOT_USAC) {
1855 if (usc->m_channelConfigurationIndex) {
1856 /* sanity check: all element counter must be zero */
1857 if (sc_chan_config.nCPE | sc_chan_config.nSCE | sc_chan_config.nLFE) {
1858 return TRANSPORTDEC_PARSE_ERROR;
1859 }
1860 } else {
1861 /* sanity check: number of audio channels shall be equal to or smaller
1862 * than the accumulated sum of all channels */
1863 if ((INT)(-2 * sc_chan_config.nCPE - sc_chan_config.nSCE -
1864 sc_chan_config.nLFE) < (INT)usc->numAudioChannels) {
1865 return TRANSPORTDEC_PARSE_ERROR;
1866 }
1867 }
1868 }
1869
1870 return ErrorStatus;
1871 }
1872
1873 /* Mapping of coreSbrFrameLengthIndex defined by Table 70 in ISO/IEC 23003-3 */
UsacConfig_SetCoreSbrFrameLengthIndex(CSAudioSpecificConfig * asc,int coreSbrFrameLengthIndex)1874 static TRANSPORTDEC_ERROR UsacConfig_SetCoreSbrFrameLengthIndex(
1875 CSAudioSpecificConfig *asc, int coreSbrFrameLengthIndex) {
1876 int sbrRatioIndex_val;
1877
1878 if (coreSbrFrameLengthIndex > 4) {
1879 return TRANSPORTDEC_PARSE_ERROR; /* reserved values */
1880 }
1881 asc->m_sc.m_usacConfig.m_coreSbrFrameLengthIndex = coreSbrFrameLengthIndex;
1882 asc->m_samplesPerFrame = usacFrameLength[coreSbrFrameLengthIndex];
1883 sbrRatioIndex_val = sbrRatioIndex[coreSbrFrameLengthIndex];
1884 asc->m_sc.m_usacConfig.m_sbrRatioIndex = sbrRatioIndex_val;
1885
1886 if (sbrRatioIndex_val > 0) {
1887 asc->m_sbrPresentFlag = 1;
1888 asc->m_extensionSamplingFrequency = asc->m_samplingFrequency;
1889 asc->m_extensionSamplingFrequencyIndex = asc->m_samplingFrequencyIndex;
1890 switch (sbrRatioIndex_val) {
1891 case 1: /* sbrRatio = 4:1 */
1892 asc->m_samplingFrequency >>= 2;
1893 asc->m_samplesPerFrame >>= 2;
1894 break;
1895 case 2: /* sbrRatio = 8:3 */
1896 asc->m_samplingFrequency = (asc->m_samplingFrequency * 3) / 8;
1897 asc->m_samplesPerFrame = (asc->m_samplesPerFrame * 3) / 8;
1898 break;
1899 case 3: /* sbrRatio = 2:1 */
1900 asc->m_samplingFrequency >>= 1;
1901 asc->m_samplesPerFrame >>= 1;
1902 break;
1903 default:
1904 return TRANSPORTDEC_PARSE_ERROR;
1905 }
1906 asc->m_samplingFrequencyIndex =
1907 getSamplingRateIndex(asc->m_samplingFrequency, 4);
1908 }
1909
1910 return TRANSPORTDEC_OK;
1911 }
1912
UsacConfig_Parse(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,CSTpCallBacks * cb)1913 static TRANSPORTDEC_ERROR UsacConfig_Parse(CSAudioSpecificConfig *asc,
1914 HANDLE_FDK_BITSTREAM hBs,
1915 CSTpCallBacks *cb) {
1916 int usacSamplingFrequency, channelConfigurationIndex, coreSbrFrameLengthIndex;
1917 TRANSPORTDEC_ERROR err = TRANSPORTDEC_OK;
1918
1919 /* Start bit position of usacConfig */
1920 INT nbits = (INT)FDKgetValidBits(hBs);
1921
1922 usacSamplingFrequency = getSampleRate(hBs, &asc->m_samplingFrequencyIndex, 5);
1923 asc->m_samplingFrequency = (UINT)usacSamplingFrequency;
1924
1925 coreSbrFrameLengthIndex = FDKreadBits(hBs, 3);
1926 if (UsacConfig_SetCoreSbrFrameLengthIndex(asc, coreSbrFrameLengthIndex) !=
1927 TRANSPORTDEC_OK) {
1928 return TRANSPORTDEC_PARSE_ERROR;
1929 }
1930
1931 channelConfigurationIndex = FDKreadBits(hBs, 5);
1932 if (channelConfigurationIndex > 2) {
1933 return TRANSPORTDEC_PARSE_ERROR; /* only channelConfigurationIndex = [1,2]
1934 are supported */
1935 }
1936
1937 if (channelConfigurationIndex == 0) {
1938 return TRANSPORTDEC_PARSE_ERROR; /* only channelConfigurationIndex = [1,2]
1939 are supported */
1940 }
1941 asc->m_channelConfiguration = channelConfigurationIndex;
1942
1943 err = UsacRsv60DecoderConfig_Parse(asc, hBs, cb);
1944 if (err != TRANSPORTDEC_OK) {
1945 return err;
1946 }
1947
1948 if (FDKreadBits(hBs, 1)) { /* usacConfigExtensionPresent */
1949 err = configExtension(&asc->m_sc.m_usacConfig, hBs, cb);
1950 if (err != TRANSPORTDEC_OK) {
1951 return err;
1952 }
1953 }
1954
1955 /* sanity check whether number of channels signaled in UsacDecoderConfig()
1956 matches the number of channels required by channelConfigurationIndex */
1957 if ((channelConfigurationIndex > 0) &&
1958 (sc_chan_config_tab[channelConfigurationIndex].nCh !=
1959 asc->m_sc.m_usacConfig.m_nUsacChannels)) {
1960 return TRANSPORTDEC_PARSE_ERROR;
1961 }
1962
1963 /* Copy UsacConfig() to asc->m_sc.m_usacConfig.UsacConfig[] buffer. */
1964 INT configSize_bits = (INT)FDKgetValidBits(hBs) - nbits;
1965 StoreConfigAsBitstream(hBs, configSize_bits,
1966 asc->m_sc.m_usacConfig.UsacConfig,
1967 TP_USAC_MAX_CONFIG_LEN);
1968 asc->m_sc.m_usacConfig.UsacConfigBits = fAbs(configSize_bits);
1969
1970 return err;
1971 }
1972
AudioSpecificConfig_ExtensionParse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,CSTpCallBacks * cb)1973 static TRANSPORTDEC_ERROR AudioSpecificConfig_ExtensionParse(
1974 CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs, CSTpCallBacks *cb) {
1975 TP_ASC_EXTENSION_ID lastAscExt, ascExtId = ASCEXT_UNKOWN;
1976 INT bitsAvailable = (INT)FDKgetValidBits(bs);
1977
1978 while (bitsAvailable >= 11) {
1979 lastAscExt = ascExtId;
1980 ascExtId = (TP_ASC_EXTENSION_ID)FDKreadBits(bs, 11);
1981 bitsAvailable -= 11;
1982
1983 switch (ascExtId) {
1984 case ASCEXT_SBR: /* 0x2b7 */
1985 if ((self->m_extensionAudioObjectType != AOT_SBR) &&
1986 (bitsAvailable >= 5)) {
1987 self->m_extensionAudioObjectType = getAOT(bs);
1988
1989 if ((self->m_extensionAudioObjectType == AOT_SBR) ||
1990 (self->m_extensionAudioObjectType ==
1991 AOT_ER_BSAC)) { /* Get SBR extension configuration */
1992 self->m_sbrPresentFlag = FDKreadBits(bs, 1);
1993 if (self->m_aot == AOT_USAC && self->m_sbrPresentFlag > 0 &&
1994 self->m_sc.m_usacConfig.m_sbrRatioIndex == 0) {
1995 return TRANSPORTDEC_PARSE_ERROR;
1996 }
1997
1998 if (self->m_sbrPresentFlag == 1) {
1999 self->m_extensionSamplingFrequency = getSampleRate(
2000 bs, &self->m_extensionSamplingFrequencyIndex, 4);
2001
2002 if ((INT)self->m_extensionSamplingFrequency <= 0) {
2003 return TRANSPORTDEC_PARSE_ERROR;
2004 }
2005 }
2006 if (self->m_extensionAudioObjectType == AOT_ER_BSAC) {
2007 self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
2008 }
2009 }
2010 /* Update counter because of variable length fields (AOT and sampling
2011 * rate) */
2012 bitsAvailable = (INT)FDKgetValidBits(bs);
2013 }
2014 break;
2015 case ASCEXT_PS: /* 0x548 */
2016 if ((lastAscExt == ASCEXT_SBR) &&
2017 (self->m_extensionAudioObjectType == AOT_SBR) &&
2018 (bitsAvailable > 0)) { /* Get PS extension configuration */
2019 self->m_psPresentFlag = FDKreadBits(bs, 1);
2020 bitsAvailable -= 1;
2021 }
2022 break;
2023 case ASCEXT_MPS: /* 0x76a */
2024 if (self->m_extensionAudioObjectType == AOT_MPEGS) break;
2025 case ASCEXT_LDMPS: /* 0x7cc */
2026 if ((ascExtId == ASCEXT_LDMPS) &&
2027 (self->m_extensionAudioObjectType == AOT_LD_MPEGS))
2028 break;
2029 if (bitsAvailable >= 1) {
2030 bitsAvailable -= 1;
2031 if (FDKreadBits(bs, 1)) { /* self->m_mpsPresentFlag */
2032 int sscLen = FDKreadBits(bs, 8);
2033 bitsAvailable -= 8;
2034 if (sscLen == 0xFF) {
2035 sscLen += FDKreadBits(bs, 16);
2036 bitsAvailable -= 16;
2037 }
2038 FDKpushFor(bs, sscLen); /* Skip SSC to be able to read the next
2039 extension if there is one. */
2040
2041 bitsAvailable -= sscLen * 8;
2042 }
2043 }
2044 break;
2045 case ASCEXT_SAOC:
2046 if ((ascExtId == ASCEXT_SAOC) &&
2047 (self->m_extensionAudioObjectType == AOT_SAOC))
2048 break;
2049 if (FDKreadBits(bs, 1)) { /* saocPresent */
2050 int saocscLen = FDKreadBits(bs, 8);
2051 bitsAvailable -= 8;
2052 if (saocscLen == 0xFF) {
2053 saocscLen += FDKreadBits(bs, 16);
2054 bitsAvailable -= 16;
2055 }
2056 FDKpushFor(bs, saocscLen);
2057 bitsAvailable -= saocscLen * 8;
2058 }
2059 break;
2060 default:
2061 /* Just ignore anything. */
2062 return TRANSPORTDEC_OK;
2063 }
2064 }
2065
2066 return TRANSPORTDEC_OK;
2067 }
2068
2069 /*
2070 * API Functions
2071 */
2072
AudioSpecificConfig_Init(CSAudioSpecificConfig * asc)2073 void AudioSpecificConfig_Init(CSAudioSpecificConfig *asc) {
2074 FDKmemclear(asc, sizeof(CSAudioSpecificConfig));
2075
2076 /* Init all values that should not be zero. */
2077 asc->m_aot = AOT_NONE;
2078 asc->m_samplingFrequencyIndex = 0xf;
2079 asc->m_epConfig = -1;
2080 asc->m_extensionAudioObjectType = AOT_NULL_OBJECT;
2081 CProgramConfig_Init(&asc->m_progrConfigElement);
2082 }
2083
AudioSpecificConfig_Parse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,int fExplicitBackwardCompatible,CSTpCallBacks * cb,UCHAR configMode,UCHAR configChanged,AUDIO_OBJECT_TYPE m_aot)2084 TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
2085 CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs,
2086 int fExplicitBackwardCompatible, CSTpCallBacks *cb, UCHAR configMode,
2087 UCHAR configChanged, AUDIO_OBJECT_TYPE m_aot) {
2088 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
2089 UINT ascStartAnchor = FDKgetValidBits(bs);
2090 int frameLengthFlag = -1;
2091
2092 AudioSpecificConfig_Init(self);
2093
2094 self->configMode = configMode;
2095 self->AacConfigChanged = configChanged;
2096 self->SbrConfigChanged = configChanged;
2097 self->SacConfigChanged = configChanged;
2098
2099 if (m_aot != AOT_NULL_OBJECT) {
2100 self->m_aot = m_aot;
2101 } else {
2102 self->m_aot = getAOT(bs);
2103 self->m_samplingFrequency =
2104 getSampleRate(bs, &self->m_samplingFrequencyIndex, 4);
2105 if (self->m_samplingFrequency <= 0 ||
2106 (self->m_samplingFrequency > 96000 && self->m_aot != 39) ||
2107 self->m_samplingFrequency > 4 * 96000) {
2108 return TRANSPORTDEC_PARSE_ERROR;
2109 }
2110
2111 self->m_channelConfiguration = FDKreadBits(bs, 4);
2112
2113 /* SBR extension ( explicit non-backwards compatible mode ) */
2114 self->m_sbrPresentFlag = 0;
2115 self->m_psPresentFlag = 0;
2116
2117 if (self->m_aot == AOT_SBR || self->m_aot == AOT_PS) {
2118 self->m_extensionAudioObjectType = AOT_SBR;
2119
2120 self->m_sbrPresentFlag = 1;
2121 if (self->m_aot == AOT_PS) {
2122 self->m_psPresentFlag = 1;
2123 }
2124
2125 self->m_extensionSamplingFrequency =
2126 getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
2127 self->m_aot = getAOT(bs);
2128
2129 switch (self->m_aot) {
2130 case AOT_AAC_LC:
2131 break;
2132 case AOT_ER_BSAC:
2133 break;
2134 default:
2135 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
2136 }
2137
2138 if (self->m_aot == AOT_ER_BSAC) {
2139 self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
2140 }
2141 } else {
2142 self->m_extensionAudioObjectType = AOT_NULL_OBJECT;
2143 }
2144 }
2145
2146 /* Parse whatever specific configs */
2147 switch (self->m_aot) {
2148 case AOT_AAC_LC:
2149 case AOT_AAC_SCAL:
2150 case AOT_ER_AAC_LC:
2151 case AOT_ER_AAC_LD:
2152 case AOT_ER_AAC_SCAL:
2153 case AOT_ER_BSAC:
2154 if ((ErrorStatus = GaSpecificConfig_Parse(&self->m_sc.m_gaSpecificConfig,
2155 self, bs, ascStartAnchor)) !=
2156 TRANSPORTDEC_OK) {
2157 return (ErrorStatus);
2158 }
2159 frameLengthFlag = self->m_sc.m_gaSpecificConfig.m_frameLengthFlag;
2160 break;
2161 case AOT_MPEGS:
2162 if (cb->cbSsc != NULL) {
2163 if (cb->cbSsc(cb->cbSscData, bs, self->m_aot, self->m_samplingFrequency,
2164 1, -1, /* nTimeSlots: read from bitstream */
2165 0, /* don't know the length */
2166 self->configMode, &self->SacConfigChanged)) {
2167 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
2168 }
2169 } else {
2170 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
2171 }
2172 break;
2173 case AOT_ER_AAC_ELD:
2174 if ((ErrorStatus = EldSpecificConfig_Parse(self, bs, cb)) !=
2175 TRANSPORTDEC_OK) {
2176 return (ErrorStatus);
2177 }
2178 frameLengthFlag = self->m_sc.m_eldSpecificConfig.m_frameLengthFlag;
2179 self->m_sbrPresentFlag = self->m_sc.m_eldSpecificConfig.m_sbrPresentFlag;
2180 self->m_extensionSamplingFrequency =
2181 (self->m_sc.m_eldSpecificConfig.m_sbrSamplingRate + 1) *
2182 self->m_samplingFrequency;
2183 break;
2184 case AOT_USAC:
2185 if ((ErrorStatus = UsacConfig_Parse(self, bs, cb)) != TRANSPORTDEC_OK) {
2186 return (ErrorStatus);
2187 }
2188 break;
2189
2190 default:
2191 return TRANSPORTDEC_UNSUPPORTED_FORMAT;
2192 }
2193
2194 /* Frame length */
2195 switch (self->m_aot) {
2196 case AOT_AAC_LC:
2197 case AOT_AAC_SCAL:
2198 case AOT_ER_AAC_LC:
2199 case AOT_ER_AAC_SCAL:
2200 case AOT_ER_BSAC:
2201 /*case AOT_USAC:*/
2202 if (!frameLengthFlag)
2203 self->m_samplesPerFrame = 1024;
2204 else
2205 self->m_samplesPerFrame = 960;
2206 break;
2207 case AOT_ER_AAC_LD:
2208 if (!frameLengthFlag)
2209 self->m_samplesPerFrame = 512;
2210 else
2211 self->m_samplesPerFrame = 480;
2212 break;
2213 default:
2214 break;
2215 }
2216
2217 switch (self->m_aot) {
2218 case AOT_ER_AAC_LC:
2219 case AOT_ER_AAC_LD:
2220 case AOT_ER_AAC_ELD:
2221 case AOT_ER_AAC_SCAL:
2222 case AOT_ER_CELP:
2223 case AOT_ER_HVXC:
2224 case AOT_ER_BSAC:
2225 self->m_epConfig = FDKreadBits(bs, 2);
2226
2227 if (self->m_epConfig > 1) {
2228 return TRANSPORTDEC_UNSUPPORTED_FORMAT; // EPCONFIG;
2229 }
2230 break;
2231 default:
2232 break;
2233 }
2234
2235 if (fExplicitBackwardCompatible &&
2236 (self->m_aot == AOT_AAC_LC || self->m_aot == AOT_ER_AAC_LD ||
2237 self->m_aot == AOT_ER_BSAC)) {
2238 ErrorStatus = AudioSpecificConfig_ExtensionParse(self, bs, cb);
2239 }
2240
2241 /* Copy config() to asc->config[] buffer. */
2242 if ((ErrorStatus == TRANSPORTDEC_OK) && (self->m_aot == AOT_USAC)) {
2243 INT configSize_bits = (INT)FDKgetValidBits(bs) - (INT)ascStartAnchor;
2244 StoreConfigAsBitstream(bs, configSize_bits, self->config,
2245 TP_USAC_MAX_CONFIG_LEN);
2246 self->configBits = fAbs(configSize_bits);
2247 }
2248
2249 return (ErrorStatus);
2250 }
2251
Drm_xHEAACDecoderConfig(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM hBs,int audioMode,CSTpCallBacks * cb)2252 static TRANSPORTDEC_ERROR Drm_xHEAACDecoderConfig(
2253 CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM hBs, int audioMode,
2254 CSTpCallBacks *cb /* use cb == NULL to signal config check only mode */
2255 ) {
2256 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
2257 CSUsacConfig *usc = &asc->m_sc.m_usacConfig;
2258 int elemIdx = 0;
2259
2260 usc->element[elemIdx].m_stereoConfigIndex = 0;
2261
2262 usc->m_usacNumElements = 1; /* Currently all extension elements are skipped
2263 -> only one SCE or CPE. */
2264
2265 switch (audioMode) {
2266 case 0: /* mono: ID_USAC_SCE */
2267 usc->element[elemIdx].usacElementType = ID_USAC_SCE;
2268 usc->m_nUsacChannels = 1;
2269 usc->element[elemIdx].m_noiseFilling = FDKreadBits(hBs, 1);
2270 if (usc->m_sbrRatioIndex > 0) {
2271 if (cb == NULL) {
2272 return ErrorStatus;
2273 }
2274 if (cb->cbSbr != NULL) {
2275 usc->element[elemIdx].m_harmonicSBR = FDKreadBit(hBs);
2276 usc->element[elemIdx].m_interTes = FDKreadBit(hBs);
2277 usc->element[elemIdx].m_pvc = FDKreadBit(hBs);
2278 if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
2279 asc->m_extensionSamplingFrequency,
2280 asc->m_samplesPerFrame, asc->m_aot, ID_SCE, elemIdx,
2281 usc->element[elemIdx].m_harmonicSBR,
2282 usc->element[elemIdx].m_stereoConfigIndex,
2283 asc->configMode, &asc->SbrConfigChanged, 1)) {
2284 return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2285 }
2286 }
2287 }
2288 break;
2289 case 2: /* stereo: ID_USAC_CPE */
2290 usc->element[elemIdx].usacElementType = ID_USAC_CPE;
2291 usc->m_nUsacChannels = 2;
2292 usc->element[elemIdx].m_noiseFilling = FDKreadBits(hBs, 1);
2293 if (usc->m_sbrRatioIndex > 0) {
2294 usc->element[elemIdx].m_harmonicSBR = FDKreadBit(hBs);
2295 usc->element[elemIdx].m_interTes = FDKreadBit(hBs);
2296 usc->element[elemIdx].m_pvc = FDKreadBit(hBs);
2297 {
2298 INT bitsToSkip = skipSbrHeader(hBs, 1);
2299 /* read stereoConfigIndex */
2300 usc->element[elemIdx].m_stereoConfigIndex = FDKreadBits(hBs, 2);
2301 /* rewind */
2302 FDKpushBack(hBs, bitsToSkip + 2);
2303 }
2304 /*
2305 The application of the following tools is mutually exclusive per audio
2306 stream configuration (see clause 5.3.2, xHE-AAC codec configuration):
2307 - MPS212 parametric stereo tool with residual coding
2308 (stereoConfigIndex>1); and
2309 - QMF based Harmonic Transposer (harmonicSBR==1).
2310 */
2311 if ((usc->element[elemIdx].m_stereoConfigIndex > 1) &&
2312 usc->element[elemIdx].m_harmonicSBR) {
2313 return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2314 }
2315 /*
2316 The 4:1 sbrRatio (sbrRatioIndex==1 in [11]) may only be employed:
2317 - in mono operation; or
2318 - in stereo operation if parametric stereo (MPS212) without residual
2319 coding is applied, i.e. if stereoConfigIndex==1 (see clause 5.3.2,
2320 xHE-AAC codec configuration).
2321 */
2322 if ((usc->m_sbrRatioIndex == 1) &&
2323 (usc->element[elemIdx].m_stereoConfigIndex != 1)) {
2324 return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2325 }
2326 if (cb == NULL) {
2327 return ErrorStatus;
2328 }
2329 {
2330 MP4_ELEMENT_ID el_type =
2331 (usc->element[elemIdx].m_stereoConfigIndex == 1 ||
2332 usc->element[elemIdx].m_stereoConfigIndex == 2)
2333 ? ID_SCE
2334 : ID_CPE;
2335 if (cb->cbSbr == NULL) return ErrorStatus = TRANSPORTDEC_UNKOWN_ERROR;
2336 if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
2337 asc->m_extensionSamplingFrequency,
2338 asc->m_samplesPerFrame, asc->m_aot, el_type, elemIdx,
2339 usc->element[elemIdx].m_harmonicSBR,
2340 usc->element[elemIdx].m_stereoConfigIndex,
2341 asc->configMode, &asc->SbrConfigChanged, 1)) {
2342 return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2343 }
2344 }
2345 /*usc->element[elemIdx].m_stereoConfigIndex =*/FDKreadBits(hBs, 2);
2346 if (usc->element[elemIdx].m_stereoConfigIndex > 0) {
2347 if (cb->cbSsc != NULL) {
2348 ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbSsc(
2349 cb->cbSscData, hBs,
2350 AOT_DRM_USAC, /* syntax differs from MPEG Mps212Config() */
2351 asc->m_extensionSamplingFrequency,
2352 usc->element[elemIdx].m_stereoConfigIndex,
2353 usc->m_coreSbrFrameLengthIndex, 0, /* don't know the length */
2354 asc->configMode, &asc->SacConfigChanged);
2355 } else {
2356 /* ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT; */
2357 }
2358 }
2359 }
2360 break;
2361 default:
2362 return TRANSPORTDEC_PARSE_ERROR;
2363 }
2364
2365 return ErrorStatus;
2366 }
2367
Drm_xHEAACStaticConfig(CSAudioSpecificConfig * asc,HANDLE_FDK_BITSTREAM bs,int audioMode,CSTpCallBacks * cb)2368 TRANSPORTDEC_ERROR Drm_xHEAACStaticConfig(
2369 CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM bs, int audioMode,
2370 CSTpCallBacks *cb /* use cb == NULL to signal config check only mode */
2371 ) {
2372 int coreSbrFrameLengthIndexDrm = FDKreadBits(bs, 2);
2373 if (UsacConfig_SetCoreSbrFrameLengthIndex(
2374 asc, coreSbrFrameLengthIndexDrm + 1) != TRANSPORTDEC_OK) {
2375 return TRANSPORTDEC_PARSE_ERROR;
2376 }
2377
2378 asc->m_channelConfiguration = (audioMode) ? 2 : 1;
2379
2380 if (Drm_xHEAACDecoderConfig(asc, bs, audioMode, cb) != TRANSPORTDEC_OK) {
2381 return TRANSPORTDEC_PARSE_ERROR;
2382 }
2383
2384 return TRANSPORTDEC_OK;
2385 }
2386
2387 /* Mapping of DRM audio sampling rate field to MPEG usacSamplingFrequencyIndex
2388 */
2389 const UCHAR mapSr2MPEGIdx[8] = {
2390 0x1b, /* 9.6 kHz */
2391 0x09, /* 12.0 kHz */
2392 0x08, /* 16.0 kHz */
2393 0x17, /* 19.2 kHz */
2394 0x06, /* 24.0 kHz */
2395 0x05, /* 32.0 kHz */
2396 0x12, /* 38.4 kHz */
2397 0x03 /* 48.0 kHz */
2398 };
2399
DrmRawSdcAudioConfig_Parse(CSAudioSpecificConfig * self,HANDLE_FDK_BITSTREAM bs,CSTpCallBacks * cb,UCHAR configMode,UCHAR configChanged)2400 TRANSPORTDEC_ERROR DrmRawSdcAudioConfig_Parse(
2401 CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs,
2402 CSTpCallBacks *cb, /* use cb == NULL to signal config check only mode */
2403 UCHAR configMode, UCHAR configChanged) {
2404 TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
2405
2406 AudioSpecificConfig_Init(self);
2407
2408 if ((INT)FDKgetValidBits(bs) < 16) {
2409 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2410 goto bail;
2411 } else {
2412 /* DRM - Audio information data entity - type 9
2413 - Short Id 2 bits (not part of the config buffer)
2414 - Stream Id 2 bits (not part of the config buffer)
2415 - audio coding 2 bits
2416 - SBR flag 1 bit
2417 - audio mode 2 bits
2418 - audio sampling rate 3 bits
2419 - text flag 1 bit
2420 - enhancement flag 1 bit
2421 - coder field 5 bits
2422 - rfa 1 bit */
2423
2424 int audioCoding, audioMode, cSamplingFreq, coderField, sfIdx, sbrFlag;
2425
2426 self->configMode = configMode;
2427 self->AacConfigChanged = configChanged;
2428 self->SbrConfigChanged = configChanged;
2429 self->SacConfigChanged = configChanged;
2430
2431 /* Read the SDC field */
2432 audioCoding = FDKreadBits(bs, 2);
2433 sbrFlag = FDKreadBits(bs, 1);
2434 audioMode = FDKreadBits(bs, 2);
2435 cSamplingFreq = FDKreadBits(bs, 3); /* audio sampling rate */
2436
2437 FDKreadBits(bs, 2); /* Text and enhancement flag */
2438 coderField = FDKreadBits(bs, 5);
2439 FDKreadBits(bs, 1); /* rfa */
2440
2441 /* Evaluate configuration and fill the ASC */
2442 if (audioCoding == 3) {
2443 sfIdx = (int)mapSr2MPEGIdx[cSamplingFreq];
2444 sbrFlag = 0; /* rfa */
2445 } else {
2446 switch (cSamplingFreq) {
2447 case 0: /* 8 kHz */
2448 sfIdx = 11;
2449 break;
2450 case 1: /* 12 kHz */
2451 sfIdx = 9;
2452 break;
2453 case 2: /* 16 kHz */
2454 sfIdx = 8;
2455 break;
2456 case 3: /* 24 kHz */
2457 sfIdx = 6;
2458 break;
2459 case 5: /* 48 kHz */
2460 sfIdx = 3;
2461 break;
2462 case 4: /* reserved */
2463 case 6: /* reserved */
2464 case 7: /* reserved */
2465 default:
2466 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2467 goto bail;
2468 }
2469 }
2470
2471 self->m_samplingFrequencyIndex = sfIdx;
2472 self->m_samplingFrequency = SamplingRateTable[sfIdx];
2473
2474 if (sbrFlag) {
2475 UINT i;
2476 int tmp = -1;
2477 self->m_sbrPresentFlag = 1;
2478 self->m_extensionAudioObjectType = AOT_SBR;
2479 self->m_extensionSamplingFrequency = self->m_samplingFrequency << 1;
2480 for (i = 0;
2481 i < (sizeof(SamplingRateTable) / sizeof(SamplingRateTable[0]));
2482 i++) {
2483 if (SamplingRateTable[i] == self->m_extensionSamplingFrequency) {
2484 tmp = i;
2485 break;
2486 }
2487 }
2488 self->m_extensionSamplingFrequencyIndex = tmp;
2489 }
2490
2491 switch (audioCoding) {
2492 case 0: /* AAC */
2493 if ((coderField >> 2) && (audioMode != 1)) {
2494 self->m_aot = AOT_DRM_SURROUND; /* Set pseudo AOT for Drm Surround */
2495 } else {
2496 self->m_aot = AOT_DRM_AAC; /* Set pseudo AOT for Drm AAC */
2497 }
2498 switch (audioMode) {
2499 case 1: /* parametric stereo */
2500 self->m_psPresentFlag = 1;
2501 case 0: /* mono */
2502 self->m_channelConfiguration = 1;
2503 break;
2504 case 2: /* stereo */
2505 self->m_channelConfiguration = 2;
2506 break;
2507 default:
2508 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2509 goto bail;
2510 }
2511 self->m_vcb11Flag = 1;
2512 self->m_hcrFlag = 1;
2513 self->m_samplesPerFrame = 960;
2514 self->m_epConfig = 1;
2515 break;
2516 case 1: /* CELP */
2517 self->m_aot = AOT_ER_CELP;
2518 self->m_channelConfiguration = 1;
2519 break;
2520 case 2: /* HVXC */
2521 self->m_aot = AOT_ER_HVXC;
2522 self->m_channelConfiguration = 1;
2523 break;
2524 case 3: /* xHE-AAC */
2525 {
2526 /* payload is MPEG conform -> no pseudo DRM AOT needed */
2527 self->m_aot = AOT_USAC;
2528 }
2529 switch (audioMode) {
2530 case 0: /* mono */
2531 case 2: /* stereo */
2532 /* codec specific config 8n bits */
2533 ErrorStatus = Drm_xHEAACStaticConfig(self, bs, audioMode, cb);
2534 break;
2535 default:
2536 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2537 goto bail;
2538 }
2539 break;
2540 default:
2541 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2542 self->m_aot = AOT_NONE;
2543 break;
2544 }
2545
2546 if (self->m_psPresentFlag && !self->m_sbrPresentFlag) {
2547 ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
2548 goto bail;
2549 }
2550 }
2551
2552 bail:
2553 return (ErrorStatus);
2554 }
2555