Coverage Report

Created: 2025-09-05 06:55

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