Coverage Report

Created: 2026-07-16 06:55

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