Coverage Report

Created: 2025-10-13 06:42

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