Coverage Report

Created: 2026-10-17 06:51

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