Coverage Report

Created: 2026-01-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libDRCdec/src/drcDec_reader.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-D DRC decoder library **************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "fixpoint_math.h"
104
#include "drcDec_reader.h"
105
#include "drcDec_tools.h"
106
#include "drcDec_rom.h"
107
#include "drcDecoder.h"
108
109
/* MPEG-D DRC AMD 1 */
110
111
45.5k
#define UNIDRCCONFEXT_PARAM_DRC 0x1
112
59.2k
#define UNIDRCCONFEXT_V1 0x2
113
2.39k
#define UNIDRCLOUDEXT_EQ 0x1
114
115
3.48k
#define UNIDRCGAINEXT_TERM 0x0
116
7.57k
#define UNIDRCLOUDEXT_TERM 0x0
117
191k
#define UNIDRCCONFEXT_TERM 0x0
118
119
33.4k
static int _getZ(const int nNodesMax) {
120
  /* Z is the minimum codeword length that is needed to encode all possible
121
   * timeDelta values */
122
  /* Z = ceil(log2(2*nNodesMax)) */
123
33.4k
  int Z = 1;
124
233k
  while ((1 << Z) < (2 * nNodesMax)) {
125
200k
    Z++;
126
200k
  }
127
33.4k
  return Z;
128
33.4k
}
129
130
35.5k
static int _getTimeDeltaMin(const GAIN_SET* pGset, const int deltaTminDefault) {
131
35.5k
  if (pGset->timeDeltaMinPresent) {
132
3.06k
    return pGset->timeDeltaMin;
133
32.5k
  } else {
134
32.5k
    return deltaTminDefault;
135
32.5k
  }
136
35.5k
}
137
138
/* compare and assign */
139
2.68M
static inline int _compAssign(UCHAR* dest, const UCHAR src) {
140
2.68M
  int diff = 0;
141
2.68M
  if (*dest != src) diff = 1;
142
2.68M
  *dest = src;
143
2.68M
  return diff;
144
2.68M
}
145
146
5.49k
static inline int _compAssign(ULONG* dest, const ULONG src) {
147
5.49k
  int diff = 0;
148
5.49k
  if (*dest != src) diff = 1;
149
5.49k
  *dest = src;
150
5.49k
  return diff;
151
5.49k
}
152
153
typedef const SCHAR (*Huffman)[2];
154
155
int _decodeHuffmanCW(Huffman h, /*!< pointer to huffman codebook table */
156
                     HANDLE_FDK_BITSTREAM hBs) /*!< Handle to bitbuffer */
157
298k
{
158
298k
  SCHAR index = 0;
159
298k
  int value, bit;
160
161
1.14M
  while (index >= 0) {
162
842k
    bit = FDKreadBits(hBs, 1);
163
842k
    index = h[index][bit];
164
842k
  }
165
166
298k
  value = index + 64; /* Add offset */
167
168
298k
  return value;
169
298k
}
170
171
/**********/
172
/* uniDrc */
173
/**********/
174
175
DRC_ERROR
176
drcDec_readUniDrc(HANDLE_FDK_BITSTREAM hBs, HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
177
                  HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet,
178
                  const int frameSize, const int deltaTminDefault,
179
0
                  HANDLE_UNI_DRC_GAIN hUniDrcGain) {
180
0
  DRC_ERROR err = DE_OK;
181
0
  int loudnessInfoSetPresent, uniDrcConfigPresent;
182
183
0
  loudnessInfoSetPresent = FDKreadBits(hBs, 1);
184
0
  if (loudnessInfoSetPresent) {
185
0
    uniDrcConfigPresent = FDKreadBits(hBs, 1);
186
0
    if (uniDrcConfigPresent) {
187
0
      err = drcDec_readUniDrcConfig(hBs, hUniDrcConfig);
188
0
      if (err) {
189
        /* clear config, if parsing error occured */
190
0
        FDKmemclear(hUniDrcConfig, sizeof(UNI_DRC_CONFIG));
191
0
        hUniDrcConfig->diff = 1;
192
0
      }
193
0
    }
194
0
    err = drcDec_readLoudnessInfoSet(hBs, hLoudnessInfoSet);
195
0
    if (err) {
196
      /* clear config, if parsing error occured */
197
0
      FDKmemclear(hLoudnessInfoSet, sizeof(LOUDNESS_INFO_SET));
198
0
      hLoudnessInfoSet->diff = 1;
199
0
    }
200
0
  }
201
202
0
  err = drcDec_readUniDrcGain(hBs, hUniDrcConfig, frameSize, deltaTminDefault,
203
0
                              hUniDrcGain);
204
205
0
  return err;
206
0
}
207
208
/**************/
209
/* uniDrcGain */
210
/**************/
211
212
static FIXP_SGL _decodeGainInitial(
213
33.4k
    HANDLE_FDK_BITSTREAM hBs, const GAIN_CODING_PROFILE gainCodingProfile) {
214
33.4k
  int sign, magn;
215
33.4k
  FIXP_SGL gainInitial = (FIXP_SGL)0;
216
33.4k
  switch (gainCodingProfile) {
217
24.2k
    case GCP_REGULAR:
218
24.2k
      sign = FDKreadBits(hBs, 1);
219
24.2k
      magn = FDKreadBits(hBs, 8);
220
221
24.2k
      gainInitial =
222
24.2k
          (FIXP_SGL)(magn << (FRACT_BITS - 1 - 3 - 7)); /* magn * 0.125; */
223
24.2k
      if (sign) gainInitial = -gainInitial;
224
24.2k
      break;
225
4.06k
    case GCP_FADING:
226
4.06k
      sign = FDKreadBits(hBs, 1);
227
4.06k
      if (sign == 0)
228
2.11k
        gainInitial = (FIXP_SGL)0;
229
1.95k
      else {
230
1.95k
        magn = FDKreadBits(hBs, 10);
231
1.95k
        gainInitial = -(FIXP_SGL)(
232
1.95k
            (magn + 1) << (FRACT_BITS - 1 - 3 - 7)); /* - (magn + 1) * 0.125; */
233
1.95k
      }
234
4.06k
      break;
235
5.17k
    case GCP_CLIPPING_DUCKING:
236
5.17k
      sign = FDKreadBits(hBs, 1);
237
5.17k
      if (sign == 0)
238
2.14k
        gainInitial = (FIXP_SGL)0;
239
3.03k
      else {
240
3.03k
        magn = FDKreadBits(hBs, 8);
241
3.03k
        gainInitial = -(FIXP_SGL)(
242
3.03k
            (magn + 1) << (FRACT_BITS - 1 - 3 - 7)); /* - (magn + 1) * 0.125; */
243
3.03k
      }
244
5.17k
      break;
245
0
    case GCP_CONSTANT:
246
0
      break;
247
33.4k
  }
248
33.4k
  return gainInitial;
249
33.4k
}
250
251
14.8k
static int _decodeNNodes(HANDLE_FDK_BITSTREAM hBs) {
252
14.8k
  int nNodes = 0, endMarker = 0;
253
254
  /* decode number of nodes */
255
177k
  while (endMarker != 1) {
256
162k
    nNodes++;
257
162k
    if (nNodes >= 128) break;
258
162k
    endMarker = FDKreadBits(hBs, 1);
259
162k
  }
260
14.8k
  return nNodes;
261
14.8k
}
262
263
static void _decodeGains(HANDLE_FDK_BITSTREAM hBs,
264
                         const GAIN_CODING_PROFILE gainCodingProfile,
265
14.8k
                         const int nNodes, GAIN_NODE* pNodes) {
266
14.8k
  int k, deltaGain;
267
14.8k
  Huffman deltaGainCodebook;
268
269
14.8k
  pNodes[0].gainDb = _decodeGainInitial(hBs, gainCodingProfile);
270
271
14.8k
  if (gainCodingProfile == GCP_CLIPPING_DUCKING) {
272
3.18k
    deltaGainCodebook = (Huffman)&deltaGain_codingProfile_2_huffman;
273
11.6k
  } else {
274
11.6k
    deltaGainCodebook = (Huffman)&deltaGain_codingProfile_0_1_huffman;
275
11.6k
  }
276
277
162k
  for (k = 1; k < nNodes; k++) {
278
147k
    deltaGain = _decodeHuffmanCW(deltaGainCodebook, hBs);
279
147k
    if (k >= 16) continue;
280
    /* gain_dB_e = 7 */
281
88.9k
    pNodes[k].gainDb =
282
88.9k
        pNodes[k - 1].gainDb +
283
88.9k
        (FIXP_SGL)(deltaGain << (FRACT_BITS - 1 - 7 -
284
88.9k
                                 3)); /* pNodes[k-1].gainDb + 0.125*deltaGain */
285
88.9k
  }
286
14.8k
}
287
288
static void _decodeSlopes(HANDLE_FDK_BITSTREAM hBs,
289
                          const GAIN_INTERPOLATION_TYPE gainInterpolationType,
290
14.8k
                          const int nNodes, GAIN_NODE* pNodes) {
291
14.8k
  int k = 0;
292
293
14.8k
  if (gainInterpolationType == GIT_SPLINE) {
294
    /* decode slope steepness */
295
162k
    for (k = 0; k < nNodes; k++) {
296
151k
      _decodeHuffmanCW((Huffman)&slopeSteepness_huffman, hBs);
297
151k
    }
298
11.3k
  }
299
14.8k
}
300
301
154k
static int _decodeTimeDelta(HANDLE_FDK_BITSTREAM hBs, const int Z) {
302
154k
  int prefix, mu;
303
304
154k
  prefix = FDKreadBits(hBs, 2);
305
154k
  switch (prefix) {
306
95.4k
    case 0x0:
307
95.4k
      return 1;
308
25.4k
    case 0x1:
309
25.4k
      mu = FDKreadBits(hBs, 2);
310
25.4k
      return mu + 2;
311
19.6k
    case 0x2:
312
19.6k
      mu = FDKreadBits(hBs, 3);
313
19.6k
      return mu + 6;
314
14.1k
    case 0x3:
315
14.1k
      mu = FDKreadBits(hBs, Z);
316
14.1k
      return mu + 14;
317
0
    default:
318
0
      return 0;
319
154k
  }
320
154k
}
321
322
static void _decodeTimes(HANDLE_FDK_BITSTREAM hBs, const int deltaTmin,
323
                         const int frameSize, const int fullFrame,
324
                         const int timeOffset, const int Z, const int nNodes,
325
14.8k
                         GAIN_NODE* pNodes) {
326
14.8k
  int timeDelta, k;
327
14.8k
  int timeOffs = timeOffset;
328
14.8k
  int frameEndFlag, nodeTimeTmp, nodeResFlag;
329
330
14.8k
  if (fullFrame == 0) {
331
11.9k
    frameEndFlag = FDKreadBits(hBs, 1);
332
11.9k
  } else {
333
2.93k
    frameEndFlag = 1;
334
2.93k
  }
335
336
14.8k
  if (frameEndFlag ==
337
14.8k
      1) { /* frameEndFlag == 1 signals that the last node is at the end of the
338
              DRC frame */
339
8.03k
    nodeResFlag = 0;
340
58.4k
    for (k = 0; k < nNodes - 1; k++) {
341
      /* decode a delta time value */
342
50.4k
      timeDelta = _decodeTimeDelta(hBs, Z);
343
50.4k
      if (k >= (16 - 1)) continue;
344
      /* frameEndFlag == 1 needs special handling for last node with node
345
       * reservoir */
346
27.2k
      nodeTimeTmp = timeOffs + timeDelta * deltaTmin;
347
27.2k
      if (nodeTimeTmp > frameSize + timeOffset) {
348
9.00k
        if (nodeResFlag == 0) {
349
1.43k
          pNodes[k].time = frameSize + timeOffset;
350
1.43k
          nodeResFlag = 1;
351
1.43k
        }
352
9.00k
        pNodes[k + 1].time = nodeTimeTmp;
353
18.2k
      } else {
354
18.2k
        pNodes[k].time = nodeTimeTmp;
355
18.2k
      }
356
27.2k
      timeOffs = nodeTimeTmp;
357
27.2k
    }
358
8.03k
    if (nodeResFlag == 0) {
359
6.59k
      k = fMin(k, 16 - 1);
360
6.59k
      pNodes[k].time = frameSize + timeOffset;
361
6.59k
    }
362
8.03k
  } else {
363
111k
    for (k = 0; k < nNodes; k++) {
364
      /* decode a delta time value */
365
104k
      timeDelta = _decodeTimeDelta(hBs, Z);
366
104k
      if (k >= 16) continue;
367
68.5k
      pNodes[k].time = timeOffs + timeDelta * deltaTmin;
368
68.5k
      timeOffs = pNodes[k].time;
369
68.5k
    }
370
6.84k
  }
371
14.8k
}
372
373
static void _readNodes(HANDLE_FDK_BITSTREAM hBs, GAIN_SET* gainSet,
374
                       const int frameSize, const int timeDeltaMin,
375
33.4k
                       UCHAR* pNNodes, GAIN_NODE* pNodes) {
376
33.4k
  int timeOffset, drcGainCodingMode, nNodes;
377
33.4k
  int Z = _getZ(frameSize / timeDeltaMin);
378
33.4k
  if (gainSet->timeAlignment == 0) {
379
30.0k
    timeOffset = -1;
380
30.0k
  } else {
381
3.36k
    timeOffset = -timeDeltaMin +
382
3.36k
                 (timeDeltaMin - 1) /
383
3.36k
                     2; /* timeOffset = - deltaTmin + floor((deltaTmin-1)/2); */
384
3.36k
  }
385
386
33.4k
  drcGainCodingMode = FDKreadBits(hBs, 1);
387
33.4k
  if (drcGainCodingMode == 0) {
388
    /* "simple" mode: only one node at the end of the frame with slope = 0 */
389
18.5k
    nNodes = 1;
390
18.5k
    pNodes[0].gainDb = _decodeGainInitial(
391
18.5k
        hBs, (GAIN_CODING_PROFILE)gainSet->gainCodingProfile);
392
18.5k
    pNodes[0].time = frameSize + timeOffset;
393
18.5k
  } else {
394
14.8k
    nNodes = _decodeNNodes(hBs);
395
396
14.8k
    _decodeSlopes(hBs, (GAIN_INTERPOLATION_TYPE)gainSet->gainInterpolationType,
397
14.8k
                  nNodes, pNodes);
398
399
14.8k
    _decodeTimes(hBs, timeDeltaMin, frameSize, gainSet->fullFrame, timeOffset,
400
14.8k
                 Z, nNodes, pNodes);
401
402
14.8k
    _decodeGains(hBs, (GAIN_CODING_PROFILE)gainSet->gainCodingProfile, nNodes,
403
14.8k
                 pNodes);
404
14.8k
  }
405
33.4k
  *pNNodes = (UCHAR)nNodes;
406
33.4k
}
407
408
static void _readDrcGainSequence(HANDLE_FDK_BITSTREAM hBs, GAIN_SET* gainSet,
409
                                 const int frameSize, const int timeDeltaMin,
410
35.5k
                                 UCHAR* pNNodes, GAIN_NODE pNodes[16]) {
411
35.5k
  SHORT timeBufPrevFrame[16], timeBufCurFrame[16];
412
35.5k
  int nNodesNodeRes, nNodesCur, k, m;
413
414
35.5k
  if (gainSet->gainCodingProfile == GCP_CONSTANT) {
415
2.10k
    *pNNodes = 1;
416
2.10k
    pNodes[0].time = frameSize - 1;
417
2.10k
    pNodes[0].gainDb = (FIXP_SGL)0;
418
33.4k
  } else {
419
33.4k
    _readNodes(hBs, gainSet, frameSize, timeDeltaMin, pNNodes, pNodes);
420
421
    /* count number of nodes in node reservoir */
422
33.4k
    nNodesNodeRes = 0;
423
33.4k
    nNodesCur = 0;
424
    /* count and buffer nodes from node reservoir */
425
214k
    for (k = 0; k < *pNNodes; k++) {
426
181k
      if (k >= 16) continue;
427
122k
      if (pNodes[k].time >= frameSize) {
428
        /* write node reservoir times into buffer */
429
34.8k
        timeBufPrevFrame[nNodesNodeRes] = pNodes[k].time;
430
34.8k
        nNodesNodeRes++;
431
87.5k
      } else { /* times from current frame */
432
87.5k
        timeBufCurFrame[nNodesCur] = pNodes[k].time;
433
87.5k
        nNodesCur++;
434
87.5k
      }
435
122k
    }
436
    /* compose right time order (bit reservoir first) */
437
68.3k
    for (k = 0; k < nNodesNodeRes; k++) {
438
      /* subtract two time frameSize: one to remove node reservoir offset and
439
       * one to get the negative index relative to the current frame
440
       */
441
34.8k
      pNodes[k].time = timeBufPrevFrame[k] - 2 * frameSize;
442
34.8k
    }
443
    /* ...and times from current frame */
444
120k
    for (m = 0; m < nNodesCur; m++, k++) {
445
87.5k
      pNodes[k].time = timeBufCurFrame[m];
446
87.5k
    }
447
33.4k
  }
448
35.5k
}
449
450
static DRC_ERROR _readUniDrcGainExtension(HANDLE_FDK_BITSTREAM hBs,
451
1.63k
                                          UNI_DRC_GAIN_EXTENSION* pExt) {
452
1.63k
  DRC_ERROR err = DE_OK;
453
1.63k
  int k, bitSizeLen, extSizeBits, bitSize;
454
455
1.63k
  k = 0;
456
1.63k
  pExt->uniDrcGainExtType[k] = FDKreadBits(hBs, 4);
457
3.48k
  while (pExt->uniDrcGainExtType[k] != UNIDRCGAINEXT_TERM) {
458
1.99k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
459
1.84k
    bitSizeLen = FDKreadBits(hBs, 3);
460
1.84k
    extSizeBits = bitSizeLen + 4;
461
462
1.84k
    bitSize = FDKreadBits(hBs, extSizeBits);
463
1.84k
    pExt->extBitSize[k] = bitSize + 1;
464
465
1.84k
    switch (pExt->uniDrcGainExtType[k]) {
466
      /* add future extensions here */
467
1.84k
      default:
468
1.84k
        FDKpushFor(hBs, pExt->extBitSize[k]);
469
1.84k
        break;
470
1.84k
    }
471
1.84k
    k++;
472
1.84k
    pExt->uniDrcGainExtType[k] = FDKreadBits(hBs, 4);
473
1.84k
  }
474
475
1.48k
  return err;
476
1.63k
}
477
478
DRC_ERROR
479
drcDec_readUniDrcGain(HANDLE_FDK_BITSTREAM hBs,
480
                      HANDLE_UNI_DRC_CONFIG hUniDrcConfig, const int frameSize,
481
                      const int deltaTminDefault,
482
7.95k
                      HANDLE_UNI_DRC_GAIN hUniDrcGain) {
483
7.95k
  DRC_ERROR err = DE_OK;
484
7.95k
  int seq, gainSequenceCount;
485
7.95k
  DRC_COEFFICIENTS_UNI_DRC* pCoef =
486
7.95k
      selectDrcCoefficients(hUniDrcConfig, LOCATION_SELECTED);
487
7.95k
  if (hUniDrcGain == NULL) return DE_NOT_OK;
488
7.95k
  hUniDrcGain->status = 0;
489
7.95k
  if (pCoef) {
490
7.74k
    gainSequenceCount = fMin(pCoef->gainSequenceCount, (UCHAR)12);
491
7.74k
  } else {
492
212
    gainSequenceCount = 0;
493
212
  }
494
495
43.5k
  for (seq = 0; seq < gainSequenceCount; seq++) {
496
35.8k
    UCHAR index = pCoef->gainSetIndexForGainSequence[seq];
497
35.8k
    GAIN_SET* gainSet;
498
35.8k
    int timeDeltaMin;
499
35.8k
    UCHAR tmpNNodes = 0;
500
35.8k
    GAIN_NODE tmpNodes[16];
501
502
35.8k
    if ((index >= pCoef->gainSetCount) || (index >= 12)) return DE_NOT_OK;
503
35.5k
    gainSet = &(pCoef->gainSet[index]);
504
505
35.5k
    timeDeltaMin = _getTimeDeltaMin(gainSet, deltaTminDefault);
506
507
35.5k
    _readDrcGainSequence(hBs, gainSet, frameSize, timeDeltaMin, &tmpNNodes,
508
35.5k
                         tmpNodes);
509
510
35.5k
    hUniDrcGain->nNodes[seq] = tmpNNodes;
511
35.5k
    FDKmemcpy(hUniDrcGain->gainNode[seq], tmpNodes,
512
35.5k
              fMin(tmpNNodes, (UCHAR)16) * sizeof(GAIN_NODE));
513
35.5k
  }
514
515
7.64k
  if (pCoef && (gainSequenceCount ==
516
7.43k
                pCoef->gainSequenceCount)) { /* all sequences have been read */
517
6.87k
    hUniDrcGain->uniDrcGainExtPresent = FDKreadBits(hBs, 1);
518
6.87k
    if (hUniDrcGain->uniDrcGainExtPresent == 1) {
519
1.63k
      err = _readUniDrcGainExtension(hBs, &(hUniDrcGain->uniDrcGainExtension));
520
1.63k
      if (err) return err;
521
1.63k
    }
522
6.87k
  }
523
524
7.49k
  if (err == DE_OK && gainSequenceCount > 0) {
525
7.21k
    hUniDrcGain->status = 1;
526
7.21k
  }
527
7.49k
  return err;
528
7.64k
}
529
530
/****************/
531
/* uniDrcConfig */
532
/****************/
533
534
static void _decodeDuckingModification(HANDLE_FDK_BITSTREAM hBs,
535
55.4k
                                       DUCKING_MODIFICATION* pDMod, int isBox) {
536
55.4k
  int bsDuckingScaling, sigma, mu;
537
538
55.4k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
539
55.4k
  pDMod->duckingScalingPresent = FDKreadBits(hBs, 1);
540
541
55.4k
  if (pDMod->duckingScalingPresent) {
542
11.7k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
543
11.7k
    bsDuckingScaling = FDKreadBits(hBs, 4);
544
11.7k
    sigma = bsDuckingScaling >> 3;
545
11.7k
    mu = bsDuckingScaling & 0x7;
546
547
11.7k
    if (sigma) {
548
5.90k
      pDMod->duckingScaling = (FIXP_SGL)(
549
5.90k
          (7 - mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 - 0.125 * (1 + mu); */
550
5.90k
    } else {
551
5.87k
      pDMod->duckingScaling = (FIXP_SGL)(
552
5.87k
          (9 + mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 + 0.125 * (1 + mu); */
553
5.87k
    }
554
43.6k
  } else {
555
43.6k
    pDMod->duckingScaling = (FIXP_SGL)(1 << (FRACT_BITS - 1 - 2)); /* 1.0 */
556
43.6k
  }
557
55.4k
}
558
559
static void _decodeGainModification(HANDLE_FDK_BITSTREAM hBs, const int version,
560
                                    int bandCount, GAIN_MODIFICATION* pGMod,
561
33.9k
                                    int isBox) {
562
33.9k
  int sign, bsGainOffset, bsAttenuationScaling, bsAmplificationScaling;
563
564
33.9k
  if (version > 0) {
565
9.84k
    int b, shapeFilterPresent;
566
567
9.84k
    if (isBox) {
568
0
      FDKpushFor(hBs, 4); /* reserved */
569
0
      bandCount = FDKreadBits(hBs, 4);
570
0
    }
571
572
19.7k
    for (b = 0; b < bandCount; b++) {
573
9.87k
      if (isBox) {
574
0
        FDKpushFor(hBs, 4); /* reserved */
575
0
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
576
0
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
577
0
        pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
578
0
        pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
579
0
      }
580
581
9.87k
      if (!isBox)
582
9.87k
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
583
9.87k
      if (pGMod[b].targetCharacteristicLeftPresent) {
584
2.60k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
585
2.60k
        pGMod[b].targetCharacteristicLeftIndex = FDKreadBits(hBs, 4);
586
2.60k
      }
587
9.87k
      if (!isBox)
588
9.87k
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
589
9.87k
      if (pGMod[b].targetCharacteristicRightPresent) {
590
2.03k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
591
2.03k
        pGMod[b].targetCharacteristicRightIndex = FDKreadBits(hBs, 4);
592
2.03k
      }
593
9.87k
      if (!isBox) pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
594
9.87k
      if (pGMod[b].gainScalingPresent) {
595
2.38k
        bsAttenuationScaling = FDKreadBits(hBs, 4);
596
2.38k
        pGMod[b].attenuationScaling = (FIXP_SGL)(
597
2.38k
            bsAttenuationScaling
598
2.38k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
599
2.38k
        bsAmplificationScaling = FDKreadBits(hBs, 4);
600
2.38k
        pGMod[b].amplificationScaling = (FIXP_SGL)(
601
2.38k
            bsAmplificationScaling
602
2.38k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
603
2.38k
      }
604
9.87k
      if (!isBox) pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
605
9.87k
      if (pGMod[b].gainOffsetPresent) {
606
2.96k
        if (isBox) FDKpushFor(hBs, 2); /* reserved */
607
2.96k
        sign = FDKreadBits(hBs, 1);
608
2.96k
        bsGainOffset = FDKreadBits(hBs, 5);
609
2.96k
        pGMod[b].gainOffset = (FIXP_SGL)(
610
2.96k
            (1 + bsGainOffset)
611
2.96k
            << (FRACT_BITS - 1 - 2 - 4)); /* (1+bsGainOffset) * 0.25; */
612
2.96k
        if (sign) {
613
1.62k
          pGMod[b].gainOffset = -pGMod[b].gainOffset;
614
1.62k
        }
615
2.96k
      }
616
9.87k
    }
617
9.84k
    if (bandCount == 1) {
618
9.53k
      shapeFilterPresent = FDKreadBits(hBs, 1);
619
9.53k
      if (shapeFilterPresent) {
620
2.61k
        if (isBox) FDKpushFor(hBs, 3); /* reserved */
621
2.61k
        FDKpushFor(hBs, 4);            /* pGMod->shapeFilterIndex */
622
6.92k
      } else {
623
6.92k
        if (isBox) FDKpushFor(hBs, 7); /* reserved */
624
6.92k
      }
625
9.53k
    }
626
24.1k
  } else {
627
24.1k
    int b, gainScalingPresent, gainOffsetPresent;
628
24.1k
    FIXP_SGL attenuationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
629
24.1k
             amplificationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
630
24.1k
             gainOffset = (FIXP_SGL)0;
631
24.1k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
632
24.1k
    gainScalingPresent = FDKreadBits(hBs, 1);
633
24.1k
    if (gainScalingPresent) {
634
3.66k
      bsAttenuationScaling = FDKreadBits(hBs, 4);
635
3.66k
      attenuationScaling = (FIXP_SGL)(
636
3.66k
          bsAttenuationScaling
637
3.66k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
638
3.66k
      bsAmplificationScaling = FDKreadBits(hBs, 4);
639
3.66k
      amplificationScaling = (FIXP_SGL)(
640
3.66k
          bsAmplificationScaling
641
3.66k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
642
3.66k
    }
643
24.1k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
644
24.1k
    gainOffsetPresent = FDKreadBits(hBs, 1);
645
24.1k
    if (gainOffsetPresent) {
646
9.45k
      if (isBox) FDKpushFor(hBs, 2); /* reserved */
647
9.45k
      sign = FDKreadBits(hBs, 1);
648
9.45k
      bsGainOffset = FDKreadBits(hBs, 5);
649
9.45k
      gainOffset =
650
9.45k
          (FIXP_SGL)((1 + bsGainOffset) << (FRACT_BITS - 1 - 2 -
651
9.45k
                                            4)); /* (1+bsGainOffset) * 0.25; */
652
9.45k
      if (sign) {
653
6.34k
        gainOffset = -gainOffset;
654
6.34k
      }
655
9.45k
    }
656
120k
    for (b = 0; b < 4; b++) {
657
96.6k
      pGMod[b].targetCharacteristicLeftPresent = 0;
658
96.6k
      pGMod[b].targetCharacteristicRightPresent = 0;
659
96.6k
      pGMod[b].gainScalingPresent = gainScalingPresent;
660
96.6k
      pGMod[b].attenuationScaling = attenuationScaling;
661
96.6k
      pGMod[b].amplificationScaling = amplificationScaling;
662
96.6k
      pGMod[b].gainOffsetPresent = gainOffsetPresent;
663
96.6k
      pGMod[b].gainOffset = gainOffset;
664
96.6k
    }
665
24.1k
  }
666
33.9k
}
667
668
static void _readDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs, const int version,
669
44.6k
                                   DRC_CHARACTERISTIC* pDChar, int isBox) {
670
44.6k
  if (version == 0) {
671
32.4k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
672
32.4k
    pDChar->cicpIndex = FDKreadBits(hBs, 7);
673
32.4k
    if (pDChar->cicpIndex > 0) {
674
27.8k
      pDChar->present = 1;
675
27.8k
      pDChar->isCICP = 1;
676
27.8k
    } else {
677
4.53k
      pDChar->present = 0;
678
4.53k
    }
679
32.4k
  } else {
680
12.1k
    pDChar->present = FDKreadBits(hBs, 1);
681
12.1k
    if (isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
682
12.1k
    if (pDChar->present) {
683
4.83k
      if (!isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
684
4.83k
      if (pDChar->isCICP) {
685
2.04k
        if (isBox) FDKpushFor(hBs, 1); /* reserved */
686
2.04k
        pDChar->cicpIndex = FDKreadBits(hBs, 7);
687
2.79k
      } else {
688
2.79k
        pDChar->custom.left = FDKreadBits(hBs, 4);
689
2.79k
        pDChar->custom.right = FDKreadBits(hBs, 4);
690
2.79k
      }
691
4.83k
    }
692
12.1k
  }
693
44.6k
}
694
695
static void _readBandBorder(HANDLE_FDK_BITSTREAM hBs, BAND_BORDER* pBBord,
696
29.1k
                            int drcBandType, int isBox) {
697
29.1k
  if (drcBandType) {
698
23.9k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
699
23.9k
    pBBord->crossoverFreqIndex = FDKreadBits(hBs, 4);
700
23.9k
  } else {
701
5.22k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
702
5.22k
    pBBord->startSubBandIndex = FDKreadBits(hBs, 10);
703
5.22k
  }
704
29.1k
}
705
706
static DRC_ERROR _readGainSet(HANDLE_FDK_BITSTREAM hBs, const int version,
707
                              int* gainSequenceIndex, GAIN_SET* pGSet,
708
129k
                              int isBox) {
709
129k
  if (isBox) FDKpushFor(hBs, 2); /* reserved */
710
129k
  pGSet->gainCodingProfile = FDKreadBits(hBs, 2);
711
129k
  pGSet->gainInterpolationType = FDKreadBits(hBs, 1);
712
129k
  pGSet->fullFrame = FDKreadBits(hBs, 1);
713
129k
  pGSet->timeAlignment = FDKreadBits(hBs, 1);
714
129k
  pGSet->timeDeltaMinPresent = FDKreadBits(hBs, 1);
715
716
129k
  if (pGSet->timeDeltaMinPresent) {
717
22.1k
    int bsTimeDeltaMin;
718
22.1k
    if (isBox) FDKpushFor(hBs, 5); /* reserved */
719
22.1k
    bsTimeDeltaMin = FDKreadBits(hBs, 11);
720
22.1k
    pGSet->timeDeltaMin = bsTimeDeltaMin + 1;
721
22.1k
  }
722
723
129k
  if (pGSet->gainCodingProfile != GCP_CONSTANT) {
724
116k
    int i;
725
116k
    if (isBox) FDKpushFor(hBs, 3); /* reserved */
726
116k
    pGSet->bandCount = FDKreadBits(hBs, 4);
727
116k
    if (pGSet->bandCount > 4) return DE_MEMORY_ERROR;
728
729
101k
    if ((pGSet->bandCount > 1) || isBox) {
730
12.6k
      pGSet->drcBandType = FDKreadBits(hBs, 1);
731
12.6k
    }
732
733
146k
    for (i = 0; i < pGSet->bandCount; i++) {
734
44.6k
      if (version == 0) {
735
32.4k
        *gainSequenceIndex = (*gainSequenceIndex) + 1;
736
32.4k
      } else {
737
12.1k
        int indexPresent;
738
12.1k
        indexPresent = (isBox) ? 1 : FDKreadBits(hBs, 1);
739
12.1k
        if (indexPresent) {
740
4.02k
          int bsIndex;
741
4.02k
          bsIndex = FDKreadBits(hBs, 6);
742
4.02k
          *gainSequenceIndex = bsIndex;
743
8.16k
        } else {
744
8.16k
          *gainSequenceIndex = (*gainSequenceIndex) + 1;
745
8.16k
        }
746
12.1k
      }
747
44.6k
      pGSet->gainSequenceIndex[i] = *gainSequenceIndex;
748
44.6k
      _readDrcCharacteristic(hBs, version, &(pGSet->drcCharacteristic[i]),
749
44.6k
                             isBox);
750
44.6k
    }
751
130k
    for (i = 1; i < pGSet->bandCount; i++) {
752
29.1k
      _readBandBorder(hBs, &(pGSet->bandBorder[i]), pGSet->drcBandType, isBox);
753
29.1k
    }
754
101k
  } else {
755
13.1k
    pGSet->bandCount = 1;
756
13.1k
    *gainSequenceIndex = (*gainSequenceIndex) + 1;
757
13.1k
    pGSet->gainSequenceIndex[0] = *gainSequenceIndex;
758
13.1k
  }
759
760
114k
  return DE_OK;
761
129k
}
762
763
static DRC_ERROR _readCustomDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs,
764
                                              const CHARACTERISTIC_SIDE side,
765
                                              UCHAR* pCharacteristicFormat,
766
                                              CUSTOM_DRC_CHAR* pCChar,
767
47.6k
                                              int isBox) {
768
47.6k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
769
47.6k
  *pCharacteristicFormat = FDKreadBits(hBs, 1);
770
47.6k
  if (*pCharacteristicFormat == CF_SIGMOID) {
771
23.3k
    int bsGain, bsIoRatio, bsExp;
772
23.3k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
773
23.3k
    bsGain = FDKreadBits(hBs, 6);
774
23.3k
    if (side == CS_LEFT) {
775
11.8k
      pCChar->sigmoid.gain = (FIXP_SGL)(bsGain << (FRACT_BITS - 1 - 6));
776
11.8k
    } else {
777
11.4k
      pCChar->sigmoid.gain = (FIXP_SGL)(-bsGain << (FRACT_BITS - 1 - 6));
778
11.4k
    }
779
23.3k
    bsIoRatio = FDKreadBits(hBs, 4);
780
    /* pCChar->sigmoid.ioRatio = 0.05 + 0.15 * bsIoRatio; */
781
23.3k
    pCChar->sigmoid.ioRatio =
782
23.3k
        FL2FXCONST_SGL(0.05f / (float)(1 << 2)) +
783
23.3k
        (FIXP_SGL)((((3 * bsIoRatio) << (FRACT_BITS - 1)) / 5) >> 4);
784
23.3k
    bsExp = FDKreadBits(hBs, 4);
785
23.3k
    if (bsExp < 15) {
786
19.7k
      pCChar->sigmoid.exp = (FIXP_SGL)((1 + 2 * bsExp) << (FRACT_BITS - 1 - 5));
787
19.7k
    } else {
788
3.64k
      pCChar->sigmoid.exp = (FIXP_SGL)MAXVAL_SGL; /* represents infinity */
789
3.64k
    }
790
23.3k
    pCChar->sigmoid.flipSign = FDKreadBits(hBs, 1);
791
24.2k
  } else { /* CF_NODES */
792
24.2k
    int i, bsCharacteristicNodeCount, bsNodeLevelDelta, bsNodeGain;
793
24.2k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
794
24.2k
    bsCharacteristicNodeCount = FDKreadBits(hBs, 2);
795
24.2k
    pCChar->nodes.characteristicNodeCount = bsCharacteristicNodeCount + 1;
796
24.2k
    if (pCChar->nodes.characteristicNodeCount > 4) return DE_MEMORY_ERROR;
797
24.2k
    pCChar->nodes.nodeLevel[0] = DRC_INPUT_LOUDNESS_TARGET_SGL;
798
24.2k
    pCChar->nodes.nodeGain[0] = (FIXP_SGL)0;
799
103k
    for (i = 0; i < pCChar->nodes.characteristicNodeCount; i++) {
800
79.1k
      if (isBox) FDKpushFor(hBs, 3); /* reserved */
801
79.1k
      bsNodeLevelDelta = FDKreadBits(hBs, 5);
802
79.1k
      if (side == CS_LEFT) {
803
49.0k
        pCChar->nodes.nodeLevel[i + 1] =
804
49.0k
            pCChar->nodes.nodeLevel[i] -
805
49.0k
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
806
49.0k
      } else {
807
30.1k
        pCChar->nodes.nodeLevel[i + 1] =
808
30.1k
            pCChar->nodes.nodeLevel[i] +
809
30.1k
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
810
30.1k
      }
811
79.1k
      bsNodeGain = FDKreadBits(hBs, 8);
812
79.1k
      pCChar->nodes.nodeGain[i + 1] = (FIXP_SGL)(
813
79.1k
          (bsNodeGain - 128)
814
79.1k
          << (FRACT_BITS - 1 - 1 - 7)); /* 0.5f * bsNodeGain - 64.0f; */
815
79.1k
    }
816
24.2k
  }
817
47.6k
  return DE_OK;
818
47.6k
}
819
820
128k
static void _skipLoudEqInstructions(HANDLE_FDK_BITSTREAM hBs) {
821
128k
  int i;
822
128k
  int downmixIdPresent, additionalDownmixIdPresent,
823
128k
      additionalDownmixIdCount = 0;
824
128k
  int drcSetIdPresent, additionalDrcSetIdPresent, additionalDrcSetIdCount = 0;
825
128k
  int eqSetIdPresent, additionalEqSetIdPresent, additionalEqSetIdCount = 0;
826
128k
  int loudEqGainSequenceCount, drcCharacteristicFormatIsCICP;
827
828
128k
  FDKpushFor(hBs, 4); /* loudEqSetId */
829
128k
  FDKpushFor(hBs, 4); /* drcLocation */
830
128k
  downmixIdPresent = FDKreadBits(hBs, 1);
831
128k
  if (downmixIdPresent) {
832
46.8k
    FDKpushFor(hBs, 7); /* downmixId */
833
46.8k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
834
46.8k
    if (additionalDownmixIdPresent) {
835
14.7k
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
836
975k
      for (i = 0; i < additionalDownmixIdCount; i++) {
837
960k
        FDKpushFor(hBs, 7); /* additionalDownmixId */
838
960k
      }
839
14.7k
    }
840
46.8k
  }
841
842
128k
  drcSetIdPresent = FDKreadBits(hBs, 1);
843
128k
  if (drcSetIdPresent) {
844
27.6k
    FDKpushFor(hBs, 6); /* drcSetId */
845
27.6k
    additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
846
27.6k
    if (additionalDrcSetIdPresent) {
847
9.08k
      additionalDrcSetIdCount = FDKreadBits(hBs, 6);
848
312k
      for (i = 0; i < additionalDrcSetIdCount; i++) {
849
303k
        FDKpushFor(hBs, 6); /* additionalDrcSetId; */
850
303k
      }
851
9.08k
    }
852
27.6k
  }
853
854
128k
  eqSetIdPresent = FDKreadBits(hBs, 1);
855
128k
  if (eqSetIdPresent) {
856
38.2k
    FDKpushFor(hBs, 6); /* eqSetId */
857
38.2k
    additionalEqSetIdPresent = FDKreadBits(hBs, 1);
858
38.2k
    if (additionalEqSetIdPresent) {
859
7.98k
      additionalEqSetIdCount = FDKreadBits(hBs, 6);
860
350k
      for (i = 0; i < additionalEqSetIdCount; i++) {
861
342k
        FDKpushFor(hBs, 6); /* additionalEqSetId; */
862
342k
      }
863
7.98k
    }
864
38.2k
  }
865
866
128k
  FDKpushFor(hBs, 1); /* loudnessAfterDrc */
867
128k
  FDKpushFor(hBs, 1); /* loudnessAfterEq */
868
128k
  loudEqGainSequenceCount = FDKreadBits(hBs, 6);
869
2.56M
  for (i = 0; i < loudEqGainSequenceCount; i++) {
870
2.43M
    FDKpushFor(hBs, 6); /* gainSequenceIndex */
871
2.43M
    drcCharacteristicFormatIsCICP = FDKreadBits(hBs, 1);
872
2.43M
    if (drcCharacteristicFormatIsCICP) {
873
1.10M
      FDKpushFor(hBs, 7); /* drcCharacteristic */
874
1.33M
    } else {
875
1.33M
      FDKpushFor(hBs, 4); /* drcCharacteristicLeftIndex */
876
1.33M
      FDKpushFor(hBs, 4); /* drcCharacteristicRightIndex */
877
1.33M
    }
878
2.43M
    FDKpushFor(hBs, 6); /* frequencyRangeIndex */
879
2.43M
    FDKpushFor(hBs, 3); /* bsLoudEqScaling */
880
2.43M
    FDKpushFor(hBs, 5); /* bsLoudEqOffset */
881
2.43M
  }
882
128k
}
883
884
70.5k
static void _skipEqSubbandGainSpline(HANDLE_FDK_BITSTREAM hBs) {
885
70.5k
  int nEqNodes, k, bits;
886
70.5k
  nEqNodes = FDKreadBits(hBs, 5);
887
70.5k
  nEqNodes += 2;
888
846k
  for (k = 0; k < nEqNodes; k++) {
889
775k
    bits = FDKreadBits(hBs, 1);
890
775k
    if (!bits) {
891
393k
      FDKpushFor(hBs, 4);
892
393k
    }
893
775k
  }
894
70.5k
  FDKpushFor(hBs, 4 * (nEqNodes - 1));
895
70.5k
  bits = FDKreadBits(hBs, 2);
896
70.5k
  switch (bits) {
897
40.4k
    case 0:
898
40.4k
      FDKpushFor(hBs, 5);
899
40.4k
      break;
900
8.87k
    case 1:
901
16.8k
    case 2:
902
16.8k
      FDKpushFor(hBs, 4);
903
16.8k
      break;
904
13.3k
    case 3:
905
13.3k
      FDKpushFor(hBs, 3);
906
13.3k
      break;
907
70.5k
  }
908
70.5k
  FDKpushFor(hBs, 5 * (nEqNodes - 1));
909
70.5k
}
910
911
17.6k
static void _skipEqCoefficients(HANDLE_FDK_BITSTREAM hBs) {
912
17.6k
  int j, k;
913
17.6k
  int eqDelayMaxPresent;
914
17.6k
  int uniqueFilterBlockCount, filterElementCount, filterElementGainPresent;
915
17.6k
  int uniqueTdFilterElementCount, eqFilterFormat, bsRealZeroRadiusOneCount,
916
17.6k
      realZeroCount, genericZeroCount, realPoleCount, complexPoleCount,
917
17.6k
      firFilterOrder;
918
17.6k
  int uniqueEqSubbandGainsCount, eqSubbandGainRepresentation,
919
17.6k
      eqSubbandGainCount;
920
17.6k
  int eqSubbandGainFormat;
921
922
17.6k
  eqDelayMaxPresent = FDKreadBits(hBs, 1);
923
17.6k
  if (eqDelayMaxPresent) {
924
4.73k
    FDKpushFor(hBs, 8); /* bsEqDelayMax */
925
4.73k
  }
926
927
17.6k
  uniqueFilterBlockCount = FDKreadBits(hBs, 6);
928
257k
  for (j = 0; j < uniqueFilterBlockCount; j++) {
929
239k
    filterElementCount = FDKreadBits(hBs, 6);
930
4.96M
    for (k = 0; k < filterElementCount; k++) {
931
4.72M
      FDKpushFor(hBs, 6); /* filterElementIndex */
932
4.72M
      filterElementGainPresent = FDKreadBits(hBs, 1);
933
4.72M
      if (filterElementGainPresent) {
934
1.94M
        FDKpushFor(hBs, 10); /* bsFilterElementGain */
935
1.94M
      }
936
4.72M
    }
937
239k
  }
938
17.6k
  uniqueTdFilterElementCount = FDKreadBits(hBs, 6);
939
145k
  for (j = 0; j < uniqueTdFilterElementCount; j++) {
940
127k
    eqFilterFormat = FDKreadBits(hBs, 1);
941
127k
    if (eqFilterFormat == 0) { /* pole/zero */
942
84.5k
      bsRealZeroRadiusOneCount = FDKreadBits(hBs, 3);
943
84.5k
      realZeroCount = FDKreadBits(hBs, 6);
944
84.5k
      genericZeroCount = FDKreadBits(hBs, 6);
945
84.5k
      realPoleCount = FDKreadBits(hBs, 4);
946
84.5k
      complexPoleCount = FDKreadBits(hBs, 4);
947
84.5k
      FDKpushFor(hBs, 2 * bsRealZeroRadiusOneCount * 1);
948
84.5k
      FDKpushFor(hBs, realZeroCount * 8);
949
84.5k
      FDKpushFor(hBs, genericZeroCount * 14);
950
84.5k
      FDKpushFor(hBs, realPoleCount * 8);
951
84.5k
      FDKpushFor(hBs, complexPoleCount * 14);
952
84.5k
    } else { /* FIR coefficients */
953
43.0k
      firFilterOrder = FDKreadBits(hBs, 7);
954
43.0k
      FDKpushFor(hBs, 1);
955
43.0k
      FDKpushFor(hBs, (firFilterOrder / 2 + 1) * 11);
956
43.0k
    }
957
127k
  }
958
17.6k
  uniqueEqSubbandGainsCount = FDKreadBits(hBs, 6);
959
17.6k
  if (uniqueEqSubbandGainsCount > 0) {
960
6.09k
    eqSubbandGainRepresentation = FDKreadBits(hBs, 1);
961
6.09k
    eqSubbandGainFormat = FDKreadBits(hBs, 4);
962
6.09k
    switch (eqSubbandGainFormat) {
963
397
      case GF_QMF32:
964
397
        eqSubbandGainCount = 32;
965
397
        break;
966
797
      case GF_QMFHYBRID39:
967
797
        eqSubbandGainCount = 39;
968
797
        break;
969
346
      case GF_QMF64:
970
346
        eqSubbandGainCount = 64;
971
346
        break;
972
269
      case GF_QMFHYBRID71:
973
269
        eqSubbandGainCount = 71;
974
269
        break;
975
195
      case GF_QMF128:
976
195
        eqSubbandGainCount = 128;
977
195
        break;
978
338
      case GF_QMFHYBRID135:
979
338
        eqSubbandGainCount = 135;
980
338
        break;
981
287
      case GF_UNIFORM:
982
3.74k
      default:
983
3.74k
        eqSubbandGainCount = FDKreadBits(hBs, 8);
984
3.74k
        eqSubbandGainCount++;
985
3.74k
        break;
986
6.09k
    }
987
170k
    for (k = 0; k < uniqueEqSubbandGainsCount; k++) {
988
164k
      if (eqSubbandGainRepresentation == 1) {
989
70.5k
        _skipEqSubbandGainSpline(hBs);
990
93.7k
      } else {
991
93.7k
        FDKpushFor(hBs, eqSubbandGainCount * 9);
992
93.7k
      }
993
164k
    }
994
6.09k
  }
995
17.6k
}
996
997
static void _skipTdFilterCascade(HANDLE_FDK_BITSTREAM hBs,
998
31.3k
                                 const int eqChannelGroupCount) {
999
31.3k
  int i, eqCascadeGainPresent, filterBlockCount, eqPhaseAlignmentPresent;
1000
47.9k
  for (i = 0; i < eqChannelGroupCount; i++) {
1001
16.6k
    eqCascadeGainPresent = FDKreadBits(hBs, 1);
1002
16.6k
    if (eqCascadeGainPresent) {
1003
5.28k
      FDKpushFor(hBs, 10); /* bsEqCascadeGain */
1004
5.28k
    }
1005
16.6k
    filterBlockCount = FDKreadBits(hBs, 4);
1006
16.6k
    FDKpushFor(hBs, filterBlockCount * 7); /* filterBlockIndex */
1007
16.6k
  }
1008
31.3k
  eqPhaseAlignmentPresent = FDKreadBits(hBs, 1);
1009
31.3k
  {
1010
31.3k
    if (eqPhaseAlignmentPresent) {
1011
22.4k
      for (i = 0; i < eqChannelGroupCount; i++) {
1012
5.91k
        FDKpushFor(hBs, (eqChannelGroupCount - i - 1) * 1);
1013
5.91k
      }
1014
16.5k
    }
1015
31.3k
  }
1016
31.3k
}
1017
1018
static DRC_ERROR _skipEqInstructions(HANDLE_FDK_BITSTREAM hBs,
1019
119k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1020
119k
  DRC_ERROR err = DE_OK;
1021
119k
  int c, i, k, channelCount;
1022
119k
  int downmixIdPresent, downmixId, eqApplyToDownmix, additionalDownmixIdPresent,
1023
119k
      additionalDownmixIdCount = 0;
1024
119k
  int additionalDrcSetIdPresent, additionalDrcSetIdCount;
1025
119k
  int dependsOnEqSetPresent, eqChannelGroupCount, tdFilterCascadePresent,
1026
119k
      subbandGainsPresent, eqTransitionDurationPresent;
1027
119k
  UCHAR eqChannelGroupForChannel[8];
1028
1029
119k
  FDKpushFor(hBs, 6); /* eqSetId */
1030
119k
  FDKpushFor(hBs, 4); /* eqSetComplexityLevel */
1031
119k
  downmixIdPresent = FDKreadBits(hBs, 1);
1032
119k
  if (downmixIdPresent) {
1033
41.1k
    downmixId = FDKreadBits(hBs, 7);
1034
41.1k
    eqApplyToDownmix = FDKreadBits(hBs, 1);
1035
41.1k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1036
41.1k
    if (additionalDownmixIdPresent) {
1037
21.0k
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
1038
21.0k
      FDKpushFor(hBs, additionalDownmixIdCount * 7); /* additionalDownmixId */
1039
21.0k
    }
1040
77.9k
  } else {
1041
77.9k
    downmixId = 0;
1042
77.9k
    eqApplyToDownmix = 0;
1043
77.9k
  }
1044
119k
  FDKpushFor(hBs, 6); /* drcSetId */
1045
119k
  additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
1046
119k
  if (additionalDrcSetIdPresent) {
1047
33.8k
    additionalDrcSetIdCount = FDKreadBits(hBs, 6);
1048
1.05M
    for (i = 0; i < additionalDrcSetIdCount; i++) {
1049
1.01M
      FDKpushFor(hBs, 6); /* additionalDrcSetId */
1050
1.01M
    }
1051
33.8k
  }
1052
119k
  FDKpushFor(hBs, 16); /* eqSetPurpose */
1053
119k
  dependsOnEqSetPresent = FDKreadBits(hBs, 1);
1054
119k
  if (dependsOnEqSetPresent) {
1055
32.7k
    FDKpushFor(hBs, 6); /* dependsOnEqSet */
1056
86.3k
  } else {
1057
86.3k
    FDKpushFor(hBs, 1); /* noIndependentEqUse */
1058
86.3k
  }
1059
1060
119k
  channelCount = hUniDrcConfig->channelLayout.baseChannelCount;
1061
119k
  if ((downmixIdPresent == 1) && (eqApplyToDownmix == 1) && (downmixId != 0) &&
1062
15.0k
      (downmixId != DOWNMIX_ID_ANY_DOWNMIX) &&
1063
9.51k
      (additionalDownmixIdCount == 0)) {
1064
5.17k
    DOWNMIX_INSTRUCTIONS* pDown =
1065
5.17k
        selectDownmixInstructions(hUniDrcConfig, downmixId);
1066
5.17k
    if (pDown == NULL) return DE_NOT_OK;
1067
1068
276
    channelCount =
1069
276
        pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1070
113k
  } else if ((downmixId == DOWNMIX_ID_ANY_DOWNMIX) ||
1071
108k
             (additionalDownmixIdCount > 1)) {
1072
19.9k
    channelCount = 1;
1073
19.9k
  }
1074
1075
114k
  eqChannelGroupCount = 0;
1076
197k
  for (c = 0; c < channelCount; c++) {
1077
83.4k
    int newGroup = 1;
1078
83.4k
    if (c >= 8) return DE_MEMORY_ERROR;
1079
83.2k
    eqChannelGroupForChannel[c] = FDKreadBits(hBs, 7);
1080
177k
    for (k = 0; k < c; k++) {
1081
94.1k
      if (eqChannelGroupForChannel[c] == eqChannelGroupForChannel[k]) {
1082
74.3k
        newGroup = 0;
1083
74.3k
      }
1084
94.1k
    }
1085
83.2k
    if (newGroup == 1) {
1086
47.7k
      eqChannelGroupCount += 1;
1087
47.7k
    }
1088
83.2k
  }
1089
113k
  tdFilterCascadePresent = FDKreadBits(hBs, 1);
1090
113k
  if (tdFilterCascadePresent) {
1091
31.3k
    _skipTdFilterCascade(hBs, eqChannelGroupCount);
1092
31.3k
  }
1093
113k
  subbandGainsPresent = FDKreadBits(hBs, 1);
1094
113k
  if (subbandGainsPresent) {
1095
34.2k
    FDKpushFor(hBs, eqChannelGroupCount * 6); /* subbandGainsIndex */
1096
34.2k
  }
1097
113k
  eqTransitionDurationPresent = FDKreadBits(hBs, 1);
1098
113k
  if (eqTransitionDurationPresent) {
1099
30.4k
    FDKpushFor(hBs, 5); /* bsEqTransitionDuration */
1100
30.4k
  }
1101
113k
  return err;
1102
114k
}
1103
1104
6.79k
static void _skipDrcCoefficientsBasic(HANDLE_FDK_BITSTREAM hBs) {
1105
6.79k
  FDKpushFor(hBs, 4); /* drcLocation */
1106
6.79k
  FDKpushFor(hBs, 7); /* drcCharacteristic */
1107
6.79k
}
1108
1109
static DRC_ERROR _readDrcCoefficientsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1110
                                            const int version,
1111
96.9k
                                            DRC_COEFFICIENTS_UNI_DRC* pCoef) {
1112
96.9k
  DRC_ERROR err = DE_OK;
1113
96.9k
  int i, bsDrcFrameSize;
1114
96.9k
  int gainSequenceIndex = -1;
1115
1116
96.9k
  pCoef->drcLocation = FDKreadBits(hBs, 4);
1117
96.9k
  pCoef->drcFrameSizePresent = FDKreadBits(hBs, 1);
1118
1119
96.9k
  if (pCoef->drcFrameSizePresent == 1) {
1120
37.6k
    bsDrcFrameSize = FDKreadBits(hBs, 15);
1121
37.6k
    pCoef->drcFrameSize = bsDrcFrameSize + 1;
1122
37.6k
  }
1123
96.9k
  if (version == 0) {
1124
51.8k
    int gainSequenceCount = 0, gainSetCount;
1125
51.8k
    pCoef->characteristicLeftCount = 0;
1126
51.8k
    pCoef->characteristicRightCount = 0;
1127
51.8k
    gainSetCount = FDKreadBits(hBs, 6);
1128
51.8k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1129
109k
    for (i = 0; i < gainSetCount; i++) {
1130
63.4k
      GAIN_SET tmpGset;
1131
63.4k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1132
63.4k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1133
63.4k
      if (err) return err;
1134
57.5k
      gainSequenceCount += tmpGset.bandCount;
1135
1136
57.5k
      if (i >= 12) continue;
1137
28.6k
      pCoef->gainSet[i] = tmpGset;
1138
28.6k
    }
1139
46.0k
    pCoef->gainSequenceCount = gainSequenceCount;
1140
46.0k
  } else { /* (version == 1) */
1141
45.1k
    UCHAR drcCharacteristicLeftPresent, drcCharacteristicRightPresent;
1142
45.1k
    UCHAR shapeFiltersPresent, shapeFilterCount, tmpPresent;
1143
45.1k
    int gainSetCount;
1144
45.1k
    drcCharacteristicLeftPresent = FDKreadBits(hBs, 1);
1145
45.1k
    if (drcCharacteristicLeftPresent) {
1146
4.17k
      pCoef->characteristicLeftCount = FDKreadBits(hBs, 4);
1147
4.17k
      if ((pCoef->characteristicLeftCount + 1) > 16) return DE_MEMORY_ERROR;
1148
31.1k
      for (i = 0; i < pCoef->characteristicLeftCount; i++) {
1149
26.9k
        err = _readCustomDrcCharacteristic(
1150
26.9k
            hBs, CS_LEFT, &(pCoef->characteristicLeftFormat[i + 1]),
1151
26.9k
            &(pCoef->customCharacteristicLeft[i + 1]), 0);
1152
26.9k
        if (err) return err;
1153
26.9k
      }
1154
4.17k
    }
1155
45.1k
    drcCharacteristicRightPresent = FDKreadBits(hBs, 1);
1156
45.1k
    if (drcCharacteristicRightPresent) {
1157
2.46k
      pCoef->characteristicRightCount = FDKreadBits(hBs, 4);
1158
2.46k
      if ((pCoef->characteristicRightCount + 1) > 16) return DE_MEMORY_ERROR;
1159
23.1k
      for (i = 0; i < pCoef->characteristicRightCount; i++) {
1160
20.6k
        err = _readCustomDrcCharacteristic(
1161
20.6k
            hBs, CS_RIGHT, &(pCoef->characteristicRightFormat[i + 1]),
1162
20.6k
            &(pCoef->customCharacteristicRight[i + 1]), 0);
1163
20.6k
        if (err) return err;
1164
20.6k
      }
1165
2.46k
    }
1166
45.1k
    shapeFiltersPresent = FDKreadBits(hBs, 1);
1167
45.1k
    if (shapeFiltersPresent) {
1168
10.2k
      shapeFilterCount = FDKreadBits(hBs, 4);
1169
121k
      for (i = 0; i < shapeFilterCount; i++) {
1170
111k
        tmpPresent = FDKreadBits(hBs, 1);
1171
111k
        if (tmpPresent) /* lfCutParams */
1172
29.3k
          FDKpushFor(hBs, 5);
1173
1174
111k
        tmpPresent = FDKreadBits(hBs, 1);
1175
111k
        if (tmpPresent) /* lfBoostParams */
1176
47.6k
          FDKpushFor(hBs, 5);
1177
1178
111k
        tmpPresent = FDKreadBits(hBs, 1);
1179
111k
        if (tmpPresent) /* hfCutParams */
1180
22.9k
          FDKpushFor(hBs, 5);
1181
1182
111k
        tmpPresent = FDKreadBits(hBs, 1);
1183
111k
        if (tmpPresent) /* hfBoostParams */
1184
35.3k
          FDKpushFor(hBs, 5);
1185
111k
      }
1186
10.2k
    }
1187
45.1k
    pCoef->gainSequenceCount = FDKreadBits(hBs, 6);
1188
45.1k
    gainSetCount = FDKreadBits(hBs, 6);
1189
45.1k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1190
102k
    for (i = 0; i < gainSetCount; i++) {
1191
66.4k
      GAIN_SET tmpGset;
1192
66.4k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1193
66.4k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1194
66.4k
      if (err) return err;
1195
1196
57.1k
      if (i >= 12) continue;
1197
29.9k
      pCoef->gainSet[i] = tmpGset;
1198
29.9k
    }
1199
45.1k
  }
1200
1.06M
  for (i = 0; i < 12; i++) {
1201
982k
    pCoef->gainSetIndexForGainSequence[i] = 255;
1202
982k
  }
1203
130k
  for (i = 0; i < pCoef->gainSetCount; i++) {
1204
48.7k
    int b;
1205
81.7k
    for (b = 0; b < pCoef->gainSet[i].bandCount; b++) {
1206
32.9k
      if (pCoef->gainSet[i].gainSequenceIndex[b] >= 12) continue;
1207
27.8k
      pCoef->gainSetIndexForGainSequence[pCoef->gainSet[i]
1208
27.8k
                                             .gainSequenceIndex[b]] = i;
1209
27.8k
    }
1210
48.7k
  }
1211
1212
81.8k
  return err;
1213
96.9k
}
1214
1215
19.9k
static void _skipDrcInstructionsBasic(HANDLE_FDK_BITSTREAM hBs) {
1216
19.9k
  int drcSetEffect;
1217
19.9k
  int additionalDownmixIdPresent, additionalDownmixIdCount,
1218
19.9k
      limiterPeakTargetPresent;
1219
19.9k
  int drcSetTargetLoudnessPresent, drcSetTargetLoudnessValueLowerPresent;
1220
1221
19.9k
  FDKpushFor(hBs, 6); /* drcSetId */
1222
19.9k
  FDKpushFor(hBs, 4); /* drcLocation */
1223
19.9k
  FDKpushFor(hBs, 7); /* downmixId */
1224
19.9k
  additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1225
19.9k
  if (additionalDownmixIdPresent) {
1226
2.28k
    additionalDownmixIdCount = FDKreadBits(hBs, 3);
1227
2.28k
    FDKpushFor(hBs, 7 * additionalDownmixIdCount); /* additionalDownmixId */
1228
2.28k
  }
1229
1230
19.9k
  drcSetEffect = FDKreadBits(hBs, 16);
1231
19.9k
  if (!(drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF))) {
1232
17.7k
    limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1233
17.7k
    if (limiterPeakTargetPresent) {
1234
924
      FDKpushFor(hBs, 8); /* bsLimiterPeakTarget */
1235
924
    }
1236
17.7k
  }
1237
1238
19.9k
  drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1239
19.9k
  if (drcSetTargetLoudnessPresent) {
1240
1.66k
    FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueUpper */
1241
1.66k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1242
1.66k
    if (drcSetTargetLoudnessValueLowerPresent) {
1243
796
      FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueLower */
1244
796
    }
1245
1.66k
  }
1246
19.9k
}
1247
1248
static DRC_ERROR _readDrcInstructionsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1249
                                            const int version,
1250
                                            HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
1251
336k
                                            DRC_INSTRUCTIONS_UNI_DRC* pInst) {
1252
336k
  DRC_ERROR err = DE_OK;
1253
336k
  int i, g, c;
1254
336k
  int downmixIdPresent, additionalDownmixIdPresent, additionalDownmixIdCount;
1255
336k
  int bsLimiterPeakTarget, channelCount;
1256
336k
  DRC_COEFFICIENTS_UNI_DRC* pCoef = NULL;
1257
336k
  int repeatParameters, bsRepeatParametersCount;
1258
336k
  int repeatSequenceIndex, bsRepeatSequenceCount;
1259
336k
  SCHAR* gainSetIndex = pInst->gainSetIndex;
1260
336k
  SCHAR channelGroupForChannel[8];
1261
336k
  DUCKING_MODIFICATION duckingModificationForChannelGroup[8];
1262
1263
336k
  pInst->drcSetId = FDKreadBits(hBs, 6);
1264
336k
  if (version == 0) {
1265
    /* Assume all v0 DRC sets to be manageable in terms of complexity */
1266
291k
    pInst->drcSetComplexityLevel = 2;
1267
291k
  } else {
1268
44.8k
    pInst->drcSetComplexityLevel = FDKreadBits(hBs, 4);
1269
44.8k
  }
1270
336k
  pInst->drcLocation = FDKreadBits(hBs, 4);
1271
336k
  if (version == 0) {
1272
291k
    downmixIdPresent = 1;
1273
291k
  } else {
1274
44.8k
    downmixIdPresent = FDKreadBits(hBs, 1);
1275
44.8k
  }
1276
336k
  if (downmixIdPresent) {
1277
302k
    pInst->downmixId[0] = FDKreadBits(hBs, 7);
1278
302k
    if (version == 0) {
1279
291k
      if (pInst->downmixId[0] == 0)
1280
251k
        pInst->drcApplyToDownmix = 0;
1281
40.1k
      else
1282
40.1k
        pInst->drcApplyToDownmix = 1;
1283
291k
    } else {
1284
10.8k
      pInst->drcApplyToDownmix = FDKreadBits(hBs, 1);
1285
10.8k
    }
1286
1287
302k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1288
302k
    if (additionalDownmixIdPresent) {
1289
29.2k
      additionalDownmixIdCount = FDKreadBits(hBs, 3);
1290
29.2k
      if ((1 + additionalDownmixIdCount) > 8) return DE_MEMORY_ERROR;
1291
92.6k
      for (i = 0; i < additionalDownmixIdCount; i++) {
1292
63.3k
        pInst->downmixId[i + 1] = FDKreadBits(hBs, 7);
1293
63.3k
      }
1294
29.2k
      pInst->downmixIdCount = 1 + additionalDownmixIdCount;
1295
273k
    } else {
1296
273k
      pInst->downmixIdCount = 1;
1297
273k
    }
1298
302k
  } else {
1299
33.9k
    pInst->downmixId[0] = 0;
1300
33.9k
    pInst->downmixIdCount = 1;
1301
33.9k
  }
1302
1303
336k
  pInst->drcSetEffect = FDKreadBits(hBs, 16);
1304
1305
336k
  if ((pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) == 0) {
1306
304k
    pInst->limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1307
304k
    if (pInst->limiterPeakTargetPresent) {
1308
13.6k
      bsLimiterPeakTarget = FDKreadBits(hBs, 8);
1309
13.6k
      pInst->limiterPeakTarget = -(FIXP_SGL)(
1310
13.6k
          bsLimiterPeakTarget
1311
13.6k
          << (FRACT_BITS - 1 - 3 - 5)); /* - bsLimiterPeakTarget * 0.125; */
1312
13.6k
    }
1313
304k
  }
1314
1315
336k
  pInst->drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1316
1317
  /* set default values */
1318
336k
  pInst->drcSetTargetLoudnessValueUpper = 0;
1319
336k
  pInst->drcSetTargetLoudnessValueLower = -63;
1320
1321
336k
  if (pInst->drcSetTargetLoudnessPresent == 1) {
1322
24.9k
    int bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1323
24.9k
    int drcSetTargetLoudnessValueLowerPresent;
1324
24.9k
    bsDrcSetTargetLoudnessValueUpper = FDKreadBits(hBs, 6);
1325
24.9k
    pInst->drcSetTargetLoudnessValueUpper =
1326
24.9k
        bsDrcSetTargetLoudnessValueUpper - 63;
1327
24.9k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1328
24.9k
    if (drcSetTargetLoudnessValueLowerPresent == 1) {
1329
6.33k
      bsDrcSetTargetLoudnessValueLower = FDKreadBits(hBs, 6);
1330
6.33k
      pInst->drcSetTargetLoudnessValueLower =
1331
6.33k
          bsDrcSetTargetLoudnessValueLower - 63;
1332
6.33k
    }
1333
24.9k
  }
1334
1335
336k
  pInst->dependsOnDrcSetPresent = FDKreadBits(hBs, 1);
1336
1337
336k
  pInst->noIndependentUse = 0;
1338
336k
  if (pInst->dependsOnDrcSetPresent) {
1339
22.0k
    pInst->dependsOnDrcSet = FDKreadBits(hBs, 6);
1340
314k
  } else {
1341
314k
    pInst->noIndependentUse = FDKreadBits(hBs, 1);
1342
314k
  }
1343
1344
336k
  if (version == 0) {
1345
291k
    pInst->requiresEq = 0;
1346
291k
  } else {
1347
44.8k
    pInst->requiresEq = FDKreadBits(hBs, 1);
1348
44.8k
  }
1349
1350
336k
  pCoef = selectDrcCoefficients(hUniDrcConfig, pInst->drcLocation);
1351
1352
336k
  pInst->drcChannelCount = channelCount =
1353
336k
      hUniDrcConfig->channelLayout.baseChannelCount;
1354
1355
336k
  if (pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) {
1356
32.5k
    DUCKING_MODIFICATION* pDModForChannel =
1357
32.5k
        pInst->duckingModificationForChannel;
1358
32.5k
    c = 0;
1359
86.0k
    while (c < channelCount) {
1360
55.4k
      int bsGainSetIndex;
1361
55.4k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1362
55.4k
      if (c >= 8) return DE_MEMORY_ERROR;
1363
55.4k
      gainSetIndex[c] = bsGainSetIndex - 1;
1364
55.4k
      _decodeDuckingModification(hBs, &(pDModForChannel[c]), 0);
1365
1366
55.4k
      c++;
1367
55.4k
      repeatParameters = FDKreadBits(hBs, 1);
1368
55.4k
      if (repeatParameters == 1) {
1369
4.31k
        bsRepeatParametersCount = FDKreadBits(hBs, 5);
1370
4.31k
        bsRepeatParametersCount += 1;
1371
20.5k
        for (i = 0; i < bsRepeatParametersCount; i++) {
1372
18.1k
          if (c >= 8) return DE_MEMORY_ERROR;
1373
16.1k
          gainSetIndex[c] = gainSetIndex[c - 1];
1374
16.1k
          pDModForChannel[c] = pDModForChannel[c - 1];
1375
16.1k
          c++;
1376
16.1k
        }
1377
4.31k
      }
1378
55.4k
    }
1379
30.6k
    if (c > channelCount) {
1380
278
      return DE_NOT_OK;
1381
278
    }
1382
1383
30.3k
    err = deriveDrcChannelGroups(
1384
30.3k
        pInst->drcSetEffect, pInst->drcChannelCount, gainSetIndex,
1385
30.3k
        pDModForChannel, &pInst->nDrcChannelGroups,
1386
30.3k
        pInst->gainSetIndexForChannelGroup, channelGroupForChannel,
1387
30.3k
        duckingModificationForChannelGroup);
1388
30.3k
    if (err) return (err);
1389
304k
  } else {
1390
304k
    int deriveChannelCount = 0;
1391
304k
    if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1392
269k
        (pInst->downmixId[0] != DOWNMIX_ID_BASE_LAYOUT) &&
1393
21.5k
        (pInst->downmixId[0] != DOWNMIX_ID_ANY_DOWNMIX) &&
1394
19.2k
        (pInst->downmixIdCount == 1)) {
1395
12.9k
      if (hUniDrcConfig->downmixInstructionsCount != 0) {
1396
600
        DOWNMIX_INSTRUCTIONS* pDown =
1397
600
            selectDownmixInstructions(hUniDrcConfig, pInst->downmixId[0]);
1398
600
        if (pDown == NULL) return DE_NOT_OK;
1399
273
        pInst->drcChannelCount = channelCount =
1400
273
            pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1401
12.3k
      } else {
1402
12.3k
        deriveChannelCount = 1;
1403
12.3k
        channelCount = 1;
1404
12.3k
      }
1405
291k
    } else if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1406
256k
               ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1407
254k
                (pInst->downmixIdCount > 1))) {
1408
      /* Set maximum channel count as upper border. The effective channel count
1409
       * is set at the process function. */
1410
9.07k
      pInst->drcChannelCount = 8;
1411
9.07k
      channelCount = 1;
1412
9.07k
    }
1413
1414
303k
    c = 0;
1415
1.01M
    while (c < channelCount) {
1416
715k
      int bsGainSetIndex;
1417
715k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1418
715k
      if (c >= 8) return DE_MEMORY_ERROR;
1419
715k
      gainSetIndex[c] = bsGainSetIndex - 1;
1420
715k
      c++;
1421
715k
      repeatSequenceIndex = FDKreadBits(hBs, 1);
1422
1423
715k
      if (repeatSequenceIndex == 1) {
1424
9.48k
        bsRepeatSequenceCount = FDKreadBits(hBs, 5);
1425
9.48k
        bsRepeatSequenceCount += 1;
1426
9.48k
        if (deriveChannelCount) {
1427
4.53k
          channelCount = 1 + bsRepeatSequenceCount;
1428
4.53k
        }
1429
37.9k
        for (i = 0; i < bsRepeatSequenceCount; i++) {
1430
30.7k
          if (c >= 8) return DE_MEMORY_ERROR;
1431
28.4k
          gainSetIndex[c] = bsGainSetIndex - 1;
1432
28.4k
          c++;
1433
28.4k
        }
1434
9.48k
      }
1435
715k
    }
1436
301k
    if (c > channelCount) {
1437
283
      return DE_NOT_OK;
1438
283
    }
1439
300k
    if (deriveChannelCount) {
1440
11.0k
      pInst->drcChannelCount = channelCount;
1441
11.0k
    }
1442
1443
    /* DOWNMIX_ID_ANY_DOWNMIX: channelCount is 1. Distribute gainSetIndex to all
1444
     * channels. */
1445
300k
    if ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1446
298k
        (pInst->downmixIdCount > 1)) {
1447
71.1k
      for (c = 1; c < pInst->drcChannelCount; c++) {
1448
62.0k
        gainSetIndex[c] = gainSetIndex[0];
1449
62.0k
      }
1450
9.09k
    }
1451
1452
300k
    err = deriveDrcChannelGroups(pInst->drcSetEffect, pInst->drcChannelCount,
1453
300k
                                 gainSetIndex, NULL, &pInst->nDrcChannelGroups,
1454
300k
                                 pInst->gainSetIndexForChannelGroup,
1455
300k
                                 channelGroupForChannel, NULL);
1456
300k
    if (err) return (err);
1457
1458
334k
    for (g = 0; g < pInst->nDrcChannelGroups; g++) {
1459
33.9k
      int set, bandCount;
1460
33.9k
      set = pInst->gainSetIndexForChannelGroup[g];
1461
1462
      /* get bandCount */
1463
33.9k
      if (pCoef != NULL && set < pCoef->gainSetCount) {
1464
1.43k
        bandCount = pCoef->gainSet[set].bandCount;
1465
32.5k
      } else {
1466
32.5k
        bandCount = 1;
1467
32.5k
      }
1468
1469
33.9k
      _decodeGainModification(hBs, version, bandCount,
1470
33.9k
                              pInst->gainModificationForChannelGroup[g], 0);
1471
33.9k
    }
1472
300k
  }
1473
1474
321k
  return err;
1475
336k
}
1476
1477
static DRC_ERROR _readChannelLayout(HANDLE_FDK_BITSTREAM hBs,
1478
367k
                                    CHANNEL_LAYOUT* pChan) {
1479
367k
  DRC_ERROR err = DE_OK;
1480
1481
367k
  pChan->baseChannelCount = FDKreadBits(hBs, 7);
1482
1483
367k
  if (pChan->baseChannelCount > 8) return DE_NOT_OK;
1484
1485
348k
  pChan->layoutSignalingPresent = FDKreadBits(hBs, 1);
1486
1487
348k
  if (pChan->layoutSignalingPresent) {
1488
14.0k
    pChan->definedLayout = FDKreadBits(hBs, 8);
1489
1490
14.0k
    if (pChan->definedLayout == 0) {
1491
1.15k
      int i;
1492
4.10k
      for (i = 0; i < pChan->baseChannelCount; i++) {
1493
2.95k
        if (i < 8) {
1494
2.95k
          pChan->speakerPosition[i] = FDKreadBits(hBs, 7);
1495
2.95k
        } else {
1496
0
          FDKpushFor(hBs, 7);
1497
0
        }
1498
2.95k
      }
1499
1.15k
    }
1500
14.0k
  }
1501
348k
  return err;
1502
367k
}
1503
1504
static DRC_ERROR _readDownmixInstructions(HANDLE_FDK_BITSTREAM hBs,
1505
                                          const int version,
1506
                                          CHANNEL_LAYOUT* pChan,
1507
2.14M
                                          DOWNMIX_INSTRUCTIONS* pDown) {
1508
2.14M
  DRC_ERROR err = DE_OK;
1509
1510
2.14M
  pDown->downmixId = FDKreadBits(hBs, 7);
1511
2.14M
  pDown->targetChannelCount = FDKreadBits(hBs, 7);
1512
2.14M
  pDown->targetLayout = FDKreadBits(hBs, 8);
1513
2.14M
  pDown->downmixCoefficientsPresent = FDKreadBits(hBs, 1);
1514
1515
2.14M
  if (pDown->downmixCoefficientsPresent) {
1516
757k
    int nDownmixCoeffs = pDown->targetChannelCount * pChan->baseChannelCount;
1517
757k
    int i;
1518
757k
    if (nDownmixCoeffs > 8 * 8) return DE_NOT_OK;
1519
755k
    if (version == 0) {
1520
465k
      pDown->bsDownmixOffset = 0;
1521
484k
      for (i = 0; i < nDownmixCoeffs; i++) {
1522
        /* LFE downmix coefficients are not supported. */
1523
18.5k
        pDown->downmixCoefficient[i] = downmixCoeff[FDKreadBits(hBs, 4)];
1524
18.5k
      }
1525
465k
    } else {
1526
289k
      pDown->bsDownmixOffset = FDKreadBits(hBs, 4);
1527
408k
      for (i = 0; i < nDownmixCoeffs; i++) {
1528
118k
        pDown->downmixCoefficient[i] = downmixCoeffV1[FDKreadBits(hBs, 5)];
1529
118k
      }
1530
289k
    }
1531
755k
  }
1532
2.14M
  return err;
1533
2.14M
}
1534
1535
static DRC_ERROR _readDrcExtensionV1(HANDLE_FDK_BITSTREAM hBs,
1536
59.2k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1537
59.2k
  DRC_ERROR err = DE_OK;
1538
59.2k
  int downmixInstructionsV1Present;
1539
59.2k
  int drcCoeffsAndInstructionsUniDrcV1Present;
1540
59.2k
  int loudEqInstructionsPresent, loudEqInstructionsCount;
1541
59.2k
  int eqPresent, eqInstructionsCount;
1542
59.2k
  int i, offset;
1543
59.2k
  int diff = hUniDrcConfig->diff;
1544
1545
59.2k
  downmixInstructionsV1Present = FDKreadBits(hBs, 1);
1546
59.2k
  if (downmixInstructionsV1Present == 1) {
1547
30.7k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1,
1548
30.7k
                        FDKreadBits(hBs, 7));
1549
30.7k
    offset = hUniDrcConfig->downmixInstructionsCountV0;
1550
30.7k
    hUniDrcConfig->downmixInstructionsCount = fMin(
1551
30.7k
        (UCHAR)(offset + hUniDrcConfig->downmixInstructionsCountV1), (UCHAR)6);
1552
910k
    for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV1; i++) {
1553
880k
      DOWNMIX_INSTRUCTIONS tmpDown;
1554
880k
      FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1555
880k
      err = _readDownmixInstructions(hBs, 1, &hUniDrcConfig->channelLayout,
1556
880k
                                     &tmpDown);
1557
880k
      if (err) return err;
1558
879k
      if ((offset + i) >= 6) continue;
1559
24.5k
      if (!diff)
1560
213
        diff |= (FDKmemcmp(&tmpDown,
1561
213
                           &(hUniDrcConfig->downmixInstructions[offset + i]),
1562
213
                           sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1563
24.5k
      hUniDrcConfig->downmixInstructions[offset + i] = tmpDown;
1564
24.5k
    }
1565
30.7k
  } else {
1566
28.4k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1, 0);
1567
28.4k
  }
1568
1569
57.8k
  drcCoeffsAndInstructionsUniDrcV1Present = FDKreadBits(hBs, 1);
1570
57.8k
  if (drcCoeffsAndInstructionsUniDrcV1Present == 1) {
1571
29.7k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1,
1572
29.7k
                        FDKreadBits(hBs, 3));
1573
29.7k
    offset = hUniDrcConfig->drcCoefficientsUniDrcCountV0;
1574
29.7k
    hUniDrcConfig->drcCoefficientsUniDrcCount =
1575
29.7k
        fMin((UCHAR)(offset + hUniDrcConfig->drcCoefficientsUniDrcCountV1),
1576
29.7k
             (UCHAR)2);
1577
65.5k
    for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV1; i++) {
1578
45.1k
      DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1579
45.1k
      FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1580
45.1k
      err = _readDrcCoefficientsUniDrc(hBs, 1, &tmpCoef);
1581
45.1k
      if (err) return err;
1582
35.8k
      if ((offset + i) >= 2) continue;
1583
20.2k
      if (!diff)
1584
529
        diff |= (FDKmemcmp(&tmpCoef,
1585
529
                           &(hUniDrcConfig->drcCoefficientsUniDrc[offset + i]),
1586
529
                           sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1587
20.2k
      hUniDrcConfig->drcCoefficientsUniDrc[offset + i] = tmpCoef;
1588
20.2k
    }
1589
1590
20.4k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1,
1591
20.4k
                        FDKreadBits(hBs, 6));
1592
20.4k
    offset = hUniDrcConfig->drcInstructionsUniDrcCount;
1593
20.4k
    hUniDrcConfig->drcInstructionsUniDrcCount =
1594
20.4k
        fMin((UCHAR)(offset + hUniDrcConfig->drcInstructionsUniDrcCountV1),
1595
20.4k
             (UCHAR)12);
1596
56.4k
    for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCount; i++) {
1597
44.8k
      DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1598
44.8k
      FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1599
44.8k
      err = _readDrcInstructionsUniDrc(hBs, 1, hUniDrcConfig, &tmpInst);
1600
44.8k
      if (err) return err;
1601
35.9k
      if ((offset + i) >= 12) continue;
1602
35.7k
      if (!diff)
1603
874
        diff |= (FDKmemcmp(&tmpInst,
1604
874
                           &(hUniDrcConfig->drcInstructionsUniDrc[offset + i]),
1605
874
                           sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1606
35.7k
      hUniDrcConfig->drcInstructionsUniDrc[offset + i] = tmpInst;
1607
35.7k
    }
1608
28.1k
  } else {
1609
28.1k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1, 0);
1610
28.1k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1, 0);
1611
28.1k
  }
1612
1613
39.7k
  loudEqInstructionsPresent = FDKreadBits(hBs, 1);
1614
39.7k
  if (loudEqInstructionsPresent == 1) {
1615
12.6k
    loudEqInstructionsCount = FDKreadBits(hBs, 4);
1616
141k
    for (i = 0; i < loudEqInstructionsCount; i++) {
1617
128k
      _skipLoudEqInstructions(hBs);
1618
128k
    }
1619
12.6k
  }
1620
1621
39.7k
  eqPresent = FDKreadBits(hBs, 1);
1622
39.7k
  if (eqPresent == 1) {
1623
17.6k
    _skipEqCoefficients(hBs);
1624
17.6k
    eqInstructionsCount = FDKreadBits(hBs, 4);
1625
136k
    for (i = 0; i < eqInstructionsCount; i++) {
1626
119k
      _skipEqInstructions(hBs, hUniDrcConfig);
1627
119k
    }
1628
17.6k
  }
1629
1630
39.7k
  hUniDrcConfig->diff = diff;
1631
1632
39.7k
  return err;
1633
57.8k
}
1634
1635
static DRC_ERROR _readUniDrcConfigExtension(
1636
77.1k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1637
77.1k
  DRC_ERROR err = DE_OK;
1638
77.1k
  int k, bitSizeLen, extSizeBits, bitSize;
1639
77.1k
  INT nBitsRemaining;
1640
77.1k
  UNI_DRC_CONFIG_EXTENSION* pExt = &(hUniDrcConfig->uniDrcConfigExt);
1641
1642
77.1k
  k = 0;
1643
77.1k
  pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1644
191k
  while (pExt->uniDrcConfigExtType[k] != UNIDRCCONFEXT_TERM) {
1645
175k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1646
171k
    bitSizeLen = FDKreadBits(hBs, 4);
1647
171k
    extSizeBits = bitSizeLen + 4;
1648
1649
171k
    bitSize = FDKreadBits(hBs, extSizeBits);
1650
171k
    pExt->extBitSize[k] = bitSize + 1;
1651
171k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1652
1653
171k
    switch (pExt->uniDrcConfigExtType[k]) {
1654
59.2k
      case UNIDRCCONFEXT_V1:
1655
59.2k
        err = _readDrcExtensionV1(hBs, hUniDrcConfig);
1656
59.2k
        if (err) return err;
1657
39.7k
        if (nBitsRemaining !=
1658
39.7k
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1659
38.1k
          return DE_NOT_OK;
1660
1.58k
        break;
1661
45.5k
      case UNIDRCCONFEXT_PARAM_DRC:
1662
      /* add future extensions here */
1663
112k
      default:
1664
112k
        FDKpushFor(hBs, pExt->extBitSize[k]);
1665
112k
        break;
1666
171k
    }
1667
114k
    k++;
1668
114k
    pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1669
114k
  }
1670
1671
15.8k
  return err;
1672
77.1k
}
1673
1674
DRC_ERROR
1675
drcDec_readUniDrcConfig(HANDLE_FDK_BITSTREAM hBs,
1676
367k
                        HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1677
367k
  DRC_ERROR err = DE_OK;
1678
367k
  int i, diff = 0;
1679
367k
  int drcDescriptionBasicPresent, drcCoefficientsBasicCount,
1680
367k
      drcInstructionsBasicCount;
1681
367k
  CHANNEL_LAYOUT tmpChan;
1682
367k
  FDKmemclear(&tmpChan, sizeof(CHANNEL_LAYOUT));
1683
367k
  if (hUniDrcConfig == NULL) return DE_NOT_OK;
1684
1685
367k
  diff |= _compAssign(&hUniDrcConfig->sampleRatePresent, FDKreadBits(hBs, 1));
1686
1687
367k
  if (hUniDrcConfig->sampleRatePresent == 1) {
1688
5.49k
    diff |=
1689
5.49k
        _compAssign(&hUniDrcConfig->sampleRate, FDKreadBits(hBs, 18) + 1000);
1690
5.49k
  }
1691
1692
367k
  diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV0,
1693
367k
                      FDKreadBits(hBs, 7));
1694
1695
367k
  drcDescriptionBasicPresent = FDKreadBits(hBs, 1);
1696
367k
  if (drcDescriptionBasicPresent == 1) {
1697
6.11k
    drcCoefficientsBasicCount = FDKreadBits(hBs, 3);
1698
6.11k
    drcInstructionsBasicCount = FDKreadBits(hBs, 4);
1699
361k
  } else {
1700
361k
    drcCoefficientsBasicCount = 0;
1701
361k
    drcInstructionsBasicCount = 0;
1702
361k
  }
1703
1704
367k
  diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV0,
1705
367k
                      FDKreadBits(hBs, 3));
1706
367k
  diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV0,
1707
367k
                      FDKreadBits(hBs, 6));
1708
1709
367k
  err = _readChannelLayout(hBs, &tmpChan);
1710
367k
  if (err) return err;
1711
1712
348k
  if (!diff)
1713
292k
    diff |= (FDKmemcmp(&tmpChan, &hUniDrcConfig->channelLayout,
1714
292k
                       sizeof(CHANNEL_LAYOUT)) != 0);
1715
348k
  hUniDrcConfig->channelLayout = tmpChan;
1716
1717
348k
  hUniDrcConfig->downmixInstructionsCount =
1718
348k
      fMin(hUniDrcConfig->downmixInstructionsCountV0, (UCHAR)6);
1719
1.61M
  for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV0; i++) {
1720
1.26M
    DOWNMIX_INSTRUCTIONS tmpDown;
1721
1.26M
    FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1722
1.26M
    err = _readDownmixInstructions(hBs, 0, &hUniDrcConfig->channelLayout,
1723
1.26M
                                   &tmpDown);
1724
1.26M
    if (err) return err;
1725
1.26M
    if (i >= 6) continue;
1726
298k
    if (!diff)
1727
86.1k
      diff |= (FDKmemcmp(&tmpDown, &(hUniDrcConfig->downmixInstructions[i]),
1728
86.1k
                         sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1729
298k
    hUniDrcConfig->downmixInstructions[i] = tmpDown;
1730
298k
  }
1731
1732
355k
  for (i = 0; i < drcCoefficientsBasicCount; i++) {
1733
6.79k
    _skipDrcCoefficientsBasic(hBs);
1734
6.79k
  }
1735
368k
  for (i = 0; i < drcInstructionsBasicCount; i++) {
1736
19.9k
    _skipDrcInstructionsBasic(hBs);
1737
19.9k
  }
1738
1739
348k
  hUniDrcConfig->drcCoefficientsUniDrcCount =
1740
348k
      fMin(hUniDrcConfig->drcCoefficientsUniDrcCountV0, (UCHAR)2);
1741
394k
  for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV0; i++) {
1742
51.8k
    DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1743
51.8k
    FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1744
51.8k
    err = _readDrcCoefficientsUniDrc(hBs, 0, &tmpCoef);
1745
51.8k
    if (err) return err;
1746
46.0k
    if (i >= 2) continue;
1747
40.1k
    if (!diff)
1748
11.4k
      diff |= (FDKmemcmp(&tmpCoef, &(hUniDrcConfig->drcCoefficientsUniDrc[i]),
1749
11.4k
                         sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1750
40.1k
    hUniDrcConfig->drcCoefficientsUniDrc[i] = tmpCoef;
1751
40.1k
  }
1752
1753
342k
  hUniDrcConfig->drcInstructionsUniDrcCount =
1754
342k
      fMin(hUniDrcConfig->drcInstructionsUniDrcCountV0, (UCHAR)12);
1755
627k
  for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCountV0; i++) {
1756
291k
    DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1757
291k
    FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1758
291k
    err = _readDrcInstructionsUniDrc(hBs, 0, hUniDrcConfig, &tmpInst);
1759
291k
    if (err) return err;
1760
285k
    if (i >= 12) continue;
1761
143k
    if (!diff)
1762
74.0k
      diff |= (FDKmemcmp(&tmpInst, &(hUniDrcConfig->drcInstructionsUniDrc[i]),
1763
74.0k
                         sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1764
143k
    hUniDrcConfig->drcInstructionsUniDrc[i] = tmpInst;
1765
143k
  }
1766
1767
335k
  diff |=
1768
335k
      _compAssign(&hUniDrcConfig->uniDrcConfigExtPresent, FDKreadBits(hBs, 1));
1769
335k
  hUniDrcConfig->diff = diff;
1770
1771
335k
  if (hUniDrcConfig->uniDrcConfigExtPresent == 1) {
1772
77.1k
    err = _readUniDrcConfigExtension(hBs, hUniDrcConfig);
1773
77.1k
    if (err) return err;
1774
77.1k
  }
1775
1776
274k
  return err;
1777
335k
}
1778
1779
/*******************/
1780
/* loudnessInfoSet */
1781
/*******************/
1782
1783
static DRC_ERROR _decodeMethodValue(HANDLE_FDK_BITSTREAM hBs,
1784
                                    const UCHAR methodDefinition,
1785
66.7k
                                    FIXP_DBL* methodValue, INT isBox) {
1786
66.7k
  int tmp;
1787
66.7k
  FIXP_DBL val;
1788
66.7k
  switch (methodDefinition) {
1789
12.9k
    case MD_UNKNOWN_OTHER:
1790
18.4k
    case MD_PROGRAM_LOUDNESS:
1791
22.4k
    case MD_ANCHOR_LOUDNESS:
1792
23.3k
    case MD_MAX_OF_LOUDNESS_RANGE:
1793
27.2k
    case MD_MOMENTARY_LOUDNESS_MAX:
1794
33.7k
    case MD_SHORT_TERM_LOUDNESS_MAX:
1795
33.7k
      tmp = FDKreadBits(hBs, 8);
1796
33.7k
      val = FL2FXCONST_DBL(-57.75f / (float)(1 << 7)) +
1797
33.7k
            (FIXP_DBL)(
1798
33.7k
                tmp << (DFRACT_BITS - 1 - 2 - 7)); /* -57.75 + tmp * 0.25; */
1799
33.7k
      break;
1800
5.01k
    case MD_LOUDNESS_RANGE:
1801
5.01k
      tmp = FDKreadBits(hBs, 8);
1802
5.01k
      if (tmp == 0)
1803
367
        val = (FIXP_DBL)0;
1804
4.64k
      else if (tmp <= 128)
1805
3.79k
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 2 - 7)); /* tmp * 0.25; */
1806
850
      else if (tmp <= 204) {
1807
330
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 1 - 7)) -
1808
330
              FL2FXCONST_DBL(32.0f / (float)(1 << 7)); /* 0.5 * tmp - 32.0f; */
1809
520
      } else {
1810
        /* downscale by 1 more bit to prevent overflow at intermediate result */
1811
520
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 8)) -
1812
520
              FL2FXCONST_DBL(134.0f / (float)(1 << 8)); /* tmp - 134.0; */
1813
520
        val <<= 1;
1814
520
      }
1815
5.01k
      break;
1816
15.7k
    case MD_MIXING_LEVEL:
1817
15.7k
      tmp = FDKreadBits(hBs, isBox ? 8 : 5);
1818
15.7k
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)) +
1819
15.7k
            FL2FXCONST_DBL(80.0f / (float)(1 << 7)); /* tmp + 80.0; */
1820
15.7k
      break;
1821
3.50k
    case MD_ROOM_TYPE:
1822
3.50k
      tmp = FDKreadBits(hBs, isBox ? 8 : 2);
1823
3.50k
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)); /* tmp; */
1824
3.50k
      break;
1825
1.41k
    case MD_SHORT_TERM_LOUDNESS:
1826
1.41k
      tmp = FDKreadBits(hBs, 8);
1827
1.41k
      val = FL2FXCONST_DBL(-116.0f / (float)(1 << 7)) +
1828
1.41k
            (FIXP_DBL)(
1829
1.41k
                tmp << (DFRACT_BITS - 1 - 1 - 7)); /* -116.0 + tmp * 0.5; */
1830
1.41k
      break;
1831
7.33k
    default:
1832
7.33k
      return DE_NOT_OK; /* invalid methodDefinition value */
1833
66.7k
  }
1834
59.4k
  *methodValue = val;
1835
59.4k
  return DE_OK;
1836
66.7k
}
1837
1838
static DRC_ERROR _readLoudnessMeasurement(HANDLE_FDK_BITSTREAM hBs,
1839
66.7k
                                          LOUDNESS_MEASUREMENT* pMeas) {
1840
66.7k
  DRC_ERROR err = DE_OK;
1841
1842
66.7k
  pMeas->methodDefinition = FDKreadBits(hBs, 4);
1843
66.7k
  err =
1844
66.7k
      _decodeMethodValue(hBs, pMeas->methodDefinition, &pMeas->methodValue, 0);
1845
66.7k
  if (err) return err;
1846
59.4k
  pMeas->measurementSystem = FDKreadBits(hBs, 4);
1847
59.4k
  pMeas->reliability = FDKreadBits(hBs, 2);
1848
1849
59.4k
  return err;
1850
66.7k
}
1851
1852
static DRC_ERROR _readLoudnessInfo(HANDLE_FDK_BITSTREAM hBs, const int version,
1853
60.6k
                                   LOUDNESS_INFO* loudnessInfo) {
1854
60.6k
  DRC_ERROR err = DE_OK;
1855
60.6k
  int bsSamplePeakLevel, bsTruePeakLevel, i;
1856
60.6k
  int measurementCount;
1857
1858
60.6k
  loudnessInfo->drcSetId = FDKreadBits(hBs, 6);
1859
60.6k
  if (version >= 1) {
1860
19.1k
    loudnessInfo->eqSetId = FDKreadBits(hBs, 6);
1861
41.5k
  } else {
1862
41.5k
    loudnessInfo->eqSetId = 0;
1863
41.5k
  }
1864
60.6k
  loudnessInfo->downmixId = FDKreadBits(hBs, 7);
1865
1866
60.6k
  loudnessInfo->samplePeakLevelPresent = FDKreadBits(hBs, 1);
1867
60.6k
  if (loudnessInfo->samplePeakLevelPresent) {
1868
9.91k
    bsSamplePeakLevel = FDKreadBits(hBs, 12);
1869
9.91k
    if (bsSamplePeakLevel == 0) {
1870
624
      loudnessInfo->samplePeakLevelPresent = 0;
1871
624
      loudnessInfo->samplePeakLevel = (FIXP_DBL)0;
1872
9.29k
    } else { /* 20.0 - bsSamplePeakLevel * 0.03125; */
1873
9.29k
      loudnessInfo->samplePeakLevel =
1874
9.29k
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1875
9.29k
          (FIXP_DBL)(bsSamplePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1876
9.29k
    }
1877
9.91k
  }
1878
1879
60.6k
  loudnessInfo->truePeakLevelPresent = FDKreadBits(hBs, 1);
1880
60.6k
  if (loudnessInfo->truePeakLevelPresent) {
1881
15.7k
    bsTruePeakLevel = FDKreadBits(hBs, 12);
1882
15.7k
    if (bsTruePeakLevel == 0) {
1883
390
      loudnessInfo->truePeakLevelPresent = 0;
1884
390
      loudnessInfo->truePeakLevel = (FIXP_DBL)0;
1885
15.3k
    } else {
1886
15.3k
      loudnessInfo->truePeakLevel =
1887
15.3k
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1888
15.3k
          (FIXP_DBL)(bsTruePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1889
15.3k
    }
1890
15.7k
    loudnessInfo->truePeakLevelMeasurementSystem = FDKreadBits(hBs, 4);
1891
15.7k
    loudnessInfo->truePeakLevelReliability = FDKreadBits(hBs, 2);
1892
15.7k
  }
1893
1894
60.6k
  measurementCount = FDKreadBits(hBs, 4);
1895
60.6k
  loudnessInfo->measurementCount = fMin(measurementCount, 8);
1896
120k
  for (i = 0; i < measurementCount; i++) {
1897
66.7k
    LOUDNESS_MEASUREMENT tmpMeas;
1898
66.7k
    FDKmemclear(&tmpMeas, sizeof(LOUDNESS_MEASUREMENT));
1899
66.7k
    err = _readLoudnessMeasurement(hBs, &tmpMeas);
1900
66.7k
    if (err) return err;
1901
59.4k
    if (i >= 8) continue;
1902
57.9k
    loudnessInfo->loudnessMeasurement[i] = tmpMeas;
1903
57.9k
  }
1904
1905
53.2k
  return err;
1906
60.6k
}
1907
1908
static DRC_ERROR _readLoudnessInfoSetExtEq(
1909
2.39k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1910
2.39k
  DRC_ERROR err = DE_OK;
1911
2.39k
  int i, offset;
1912
2.39k
  int diff = hLoudnessInfoSet->diff;
1913
1914
2.39k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV1,
1915
2.39k
                      FDKreadBits(hBs, 6));
1916
2.39k
  diff |=
1917
2.39k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV1, FDKreadBits(hBs, 6));
1918
1919
2.39k
  offset = hLoudnessInfoSet->loudnessInfoAlbumCountV0;
1920
2.39k
  hLoudnessInfoSet->loudnessInfoAlbumCount = fMin(
1921
2.39k
      (UCHAR)(offset + hLoudnessInfoSet->loudnessInfoAlbumCountV1), (UCHAR)12);
1922
12.4k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV1; i++) {
1923
10.2k
    LOUDNESS_INFO tmpLoud;
1924
10.2k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1925
10.2k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1926
10.2k
    if (err) return err;
1927
10.0k
    if ((offset + i) >= 12) continue;
1928
3.03k
    if (!diff)
1929
513
      diff |= (FDKmemcmp(&tmpLoud,
1930
513
                         &(hLoudnessInfoSet->loudnessInfoAlbum[offset + i]),
1931
513
                         sizeof(LOUDNESS_INFO)) != 0);
1932
3.03k
    hLoudnessInfoSet->loudnessInfoAlbum[offset + i] = tmpLoud;
1933
3.03k
  }
1934
1935
2.12k
  offset = hLoudnessInfoSet->loudnessInfoCountV0;
1936
2.12k
  hLoudnessInfoSet->loudnessInfoCount =
1937
2.12k
      fMin((UCHAR)(offset + hLoudnessInfoSet->loudnessInfoCountV1), (UCHAR)12);
1938
10.6k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV1; i++) {
1939
8.82k
    LOUDNESS_INFO tmpLoud;
1940
8.82k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1941
8.82k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1942
8.82k
    if (err) return err;
1943
8.50k
    if ((offset + i) >= 12) continue;
1944
2.45k
    if (!diff)
1945
348
      diff |=
1946
348
          (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[offset + i]),
1947
348
                     sizeof(LOUDNESS_INFO)) != 0);
1948
2.45k
    hLoudnessInfoSet->loudnessInfo[offset + i] = tmpLoud;
1949
2.45k
  }
1950
1.80k
  hLoudnessInfoSet->diff = diff;
1951
1.80k
  return err;
1952
2.12k
}
1953
1954
static DRC_ERROR _readLoudnessInfoSetExtension(
1955
3.56k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1956
3.56k
  DRC_ERROR err = DE_OK;
1957
3.56k
  int k, bitSizeLen, extSizeBits, bitSize;
1958
3.56k
  INT nBitsRemaining;
1959
3.56k
  LOUDNESS_INFO_SET_EXTENSION* pExt = &(hLoudnessInfoSet->loudnessInfoSetExt);
1960
1961
3.56k
  k = 0;
1962
3.56k
  pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1963
7.57k
  while (pExt->loudnessInfoSetExtType[k] != UNIDRCLOUDEXT_TERM) {
1964
5.52k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1965
5.30k
    bitSizeLen = FDKreadBits(hBs, 4);
1966
5.30k
    extSizeBits = bitSizeLen + 4;
1967
1968
5.30k
    bitSize = FDKreadBits(hBs, extSizeBits);
1969
5.30k
    pExt->extBitSize[k] = bitSize + 1;
1970
5.30k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1971
1972
5.30k
    switch (pExt->loudnessInfoSetExtType[k]) {
1973
2.39k
      case UNIDRCLOUDEXT_EQ:
1974
2.39k
        err = _readLoudnessInfoSetExtEq(hBs, hLoudnessInfoSet);
1975
2.39k
        if (err) return err;
1976
1.80k
        if (nBitsRemaining !=
1977
1.80k
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1978
702
          return DE_NOT_OK;
1979
1.10k
        break;
1980
      /* add future extensions here */
1981
2.90k
      default:
1982
2.90k
        FDKpushFor(hBs, pExt->extBitSize[k]);
1983
2.90k
        break;
1984
5.30k
    }
1985
4.01k
    k++;
1986
4.01k
    pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1987
4.01k
  }
1988
1989
2.05k
  return err;
1990
3.56k
}
1991
1992
/* Parser for loundessInfoSet() */
1993
DRC_ERROR
1994
drcDec_readLoudnessInfoSet(HANDLE_FDK_BITSTREAM hBs,
1995
239k
                           HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1996
239k
  DRC_ERROR err = DE_OK;
1997
239k
  int i, diff = 0;
1998
239k
  if (hLoudnessInfoSet == NULL) return DE_NOT_OK;
1999
2000
239k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV0,
2001
239k
                      FDKreadBits(hBs, 6));
2002
239k
  diff |=
2003
239k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV0, FDKreadBits(hBs, 6));
2004
2005
239k
  hLoudnessInfoSet->loudnessInfoAlbumCount =
2006
239k
      fMin(hLoudnessInfoSet->loudnessInfoAlbumCountV0, (UCHAR)12);
2007
254k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV0; i++) {
2008
21.2k
    LOUDNESS_INFO tmpLoud;
2009
21.2k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2010
21.2k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2011
21.2k
    if (err) return err;
2012
15.2k
    if (i >= 12) continue;
2013
10.4k
    if (!diff)
2014
607
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfoAlbum[i]),
2015
607
                         sizeof(LOUDNESS_INFO)) != 0);
2016
10.4k
    hLoudnessInfoSet->loudnessInfoAlbum[i] = tmpLoud;
2017
10.4k
  }
2018
2019
233k
  hLoudnessInfoSet->loudnessInfoCount =
2020
233k
      fMin(hLoudnessInfoSet->loudnessInfoCountV0, (UCHAR)12);
2021
253k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV0; i++) {
2022
20.2k
    LOUDNESS_INFO tmpLoud;
2023
20.2k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2024
20.2k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2025
20.2k
    if (err) return err;
2026
19.5k
    if (i >= 12) continue;
2027
17.0k
    if (!diff)
2028
12.1k
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[i]),
2029
12.1k
                         sizeof(LOUDNESS_INFO)) != 0);
2030
17.0k
    hLoudnessInfoSet->loudnessInfo[i] = tmpLoud;
2031
17.0k
  }
2032
2033
232k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoSetExtPresent,
2034
232k
                      FDKreadBits(hBs, 1));
2035
232k
  hLoudnessInfoSet->diff = diff;
2036
2037
232k
  if (hLoudnessInfoSet->loudnessInfoSetExtPresent) {
2038
3.56k
    err = _readLoudnessInfoSetExtension(hBs, hLoudnessInfoSet);
2039
3.56k
    if (err) return err;
2040
3.56k
  }
2041
2042
231k
  return err;
2043
232k
}