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.70k
#define UNIDRCCONFEXT_PARAM_DRC 0x1
112
59.0k
#define UNIDRCCONFEXT_V1 0x2
113
4.62k
#define UNIDRCLOUDEXT_EQ 0x1
114
115
6.89k
#define UNIDRCGAINEXT_TERM 0x0
116
12.1k
#define UNIDRCLOUDEXT_TERM 0x0
117
99.4k
#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
3.07M
static inline int _compAssign(UCHAR* dest, const UCHAR src) {
140
3.07M
  int diff = 0;
141
3.07M
  if (*dest != src) diff = 1;
142
3.07M
  *dest = src;
143
3.07M
  return diff;
144
3.07M
}
145
146
7.62k
static inline int _compAssign(ULONG* dest, const ULONG src) {
147
7.62k
  int diff = 0;
148
7.62k
  if (*dest != src) diff = 1;
149
7.62k
  *dest = src;
150
7.62k
  return diff;
151
7.62k
}
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
59.5k
                                       DUCKING_MODIFICATION* pDMod, int isBox) {
536
59.5k
  int bsDuckingScaling, sigma, mu;
537
538
59.5k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
539
59.5k
  pDMod->duckingScalingPresent = FDKreadBits(hBs, 1);
540
541
59.5k
  if (pDMod->duckingScalingPresent) {
542
10.9k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
543
10.9k
    bsDuckingScaling = FDKreadBits(hBs, 4);
544
10.9k
    sigma = bsDuckingScaling >> 3;
545
10.9k
    mu = bsDuckingScaling & 0x7;
546
547
10.9k
    if (sigma) {
548
4.78k
      pDMod->duckingScaling = (FIXP_SGL)(
549
4.78k
          (7 - mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 - 0.125 * (1 + mu); */
550
6.13k
    } else {
551
6.13k
      pDMod->duckingScaling = (FIXP_SGL)(
552
6.13k
          (9 + mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 + 0.125 * (1 + mu); */
553
6.13k
    }
554
48.6k
  } else {
555
48.6k
    pDMod->duckingScaling = (FIXP_SGL)(1 << (FRACT_BITS - 1 - 2)); /* 1.0 */
556
48.6k
  }
557
59.5k
}
558
559
static void _decodeGainModification(HANDLE_FDK_BITSTREAM hBs, const int version,
560
                                    int bandCount, GAIN_MODIFICATION* pGMod,
561
66.4k
                                    int isBox) {
562
66.4k
  int sign, bsGainOffset, bsAttenuationScaling, bsAmplificationScaling;
563
564
66.4k
  if (version > 0) {
565
17.1k
    int b, shapeFilterPresent;
566
567
17.1k
    if (isBox) {
568
0
      FDKpushFor(hBs, 4); /* reserved */
569
0
      bandCount = FDKreadBits(hBs, 4);
570
0
    }
571
572
34.2k
    for (b = 0; b < bandCount; b++) {
573
17.0k
      if (isBox) {
574
0
        FDKpushFor(hBs, 4); /* reserved */
575
0
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
576
0
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
577
0
        pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
578
0
        pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
579
0
      }
580
581
17.0k
      if (!isBox)
582
17.0k
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
583
17.0k
      if (pGMod[b].targetCharacteristicLeftPresent) {
584
4.79k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
585
4.79k
        pGMod[b].targetCharacteristicLeftIndex = FDKreadBits(hBs, 4);
586
4.79k
      }
587
17.0k
      if (!isBox)
588
17.0k
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
589
17.0k
      if (pGMod[b].targetCharacteristicRightPresent) {
590
4.85k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
591
4.85k
        pGMod[b].targetCharacteristicRightIndex = FDKreadBits(hBs, 4);
592
4.85k
      }
593
17.0k
      if (!isBox) pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
594
17.0k
      if (pGMod[b].gainScalingPresent) {
595
4.69k
        bsAttenuationScaling = FDKreadBits(hBs, 4);
596
4.69k
        pGMod[b].attenuationScaling = (FIXP_SGL)(
597
4.69k
            bsAttenuationScaling
598
4.69k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
599
4.69k
        bsAmplificationScaling = FDKreadBits(hBs, 4);
600
4.69k
        pGMod[b].amplificationScaling = (FIXP_SGL)(
601
4.69k
            bsAmplificationScaling
602
4.69k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
603
4.69k
      }
604
17.0k
      if (!isBox) pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
605
17.0k
      if (pGMod[b].gainOffsetPresent) {
606
5.24k
        if (isBox) FDKpushFor(hBs, 2); /* reserved */
607
5.24k
        sign = FDKreadBits(hBs, 1);
608
5.24k
        bsGainOffset = FDKreadBits(hBs, 5);
609
5.24k
        pGMod[b].gainOffset = (FIXP_SGL)(
610
5.24k
            (1 + bsGainOffset)
611
5.24k
            << (FRACT_BITS - 1 - 2 - 4)); /* (1+bsGainOffset) * 0.25; */
612
5.24k
        if (sign) {
613
3.09k
          pGMod[b].gainOffset = -pGMod[b].gainOffset;
614
3.09k
        }
615
5.24k
      }
616
17.0k
    }
617
17.1k
    if (bandCount == 1) {
618
16.4k
      shapeFilterPresent = FDKreadBits(hBs, 1);
619
16.4k
      if (shapeFilterPresent) {
620
5.37k
        if (isBox) FDKpushFor(hBs, 3); /* reserved */
621
5.37k
        FDKpushFor(hBs, 4);            /* pGMod->shapeFilterIndex */
622
11.0k
      } else {
623
11.0k
        if (isBox) FDKpushFor(hBs, 7); /* reserved */
624
11.0k
      }
625
16.4k
    }
626
49.2k
  } else {
627
49.2k
    int b, gainScalingPresent, gainOffsetPresent;
628
49.2k
    FIXP_SGL attenuationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
629
49.2k
             amplificationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
630
49.2k
             gainOffset = (FIXP_SGL)0;
631
49.2k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
632
49.2k
    gainScalingPresent = FDKreadBits(hBs, 1);
633
49.2k
    if (gainScalingPresent) {
634
23.4k
      bsAttenuationScaling = FDKreadBits(hBs, 4);
635
23.4k
      attenuationScaling = (FIXP_SGL)(
636
23.4k
          bsAttenuationScaling
637
23.4k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
638
23.4k
      bsAmplificationScaling = FDKreadBits(hBs, 4);
639
23.4k
      amplificationScaling = (FIXP_SGL)(
640
23.4k
          bsAmplificationScaling
641
23.4k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
642
23.4k
    }
643
49.2k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
644
49.2k
    gainOffsetPresent = FDKreadBits(hBs, 1);
645
49.2k
    if (gainOffsetPresent) {
646
22.0k
      if (isBox) FDKpushFor(hBs, 2); /* reserved */
647
22.0k
      sign = FDKreadBits(hBs, 1);
648
22.0k
      bsGainOffset = FDKreadBits(hBs, 5);
649
22.0k
      gainOffset =
650
22.0k
          (FIXP_SGL)((1 + bsGainOffset) << (FRACT_BITS - 1 - 2 -
651
22.0k
                                            4)); /* (1+bsGainOffset) * 0.25; */
652
22.0k
      if (sign) {
653
13.6k
        gainOffset = -gainOffset;
654
13.6k
      }
655
22.0k
    }
656
246k
    for (b = 0; b < 4; b++) {
657
197k
      pGMod[b].targetCharacteristicLeftPresent = 0;
658
197k
      pGMod[b].targetCharacteristicRightPresent = 0;
659
197k
      pGMod[b].gainScalingPresent = gainScalingPresent;
660
197k
      pGMod[b].attenuationScaling = attenuationScaling;
661
197k
      pGMod[b].amplificationScaling = amplificationScaling;
662
197k
      pGMod[b].gainOffsetPresent = gainOffsetPresent;
663
197k
      pGMod[b].gainOffset = gainOffset;
664
197k
    }
665
49.2k
  }
666
66.4k
}
667
668
static void _readDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs, const int version,
669
42.2k
                                   DRC_CHARACTERISTIC* pDChar, int isBox) {
670
42.2k
  if (version == 0) {
671
22.9k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
672
22.9k
    pDChar->cicpIndex = FDKreadBits(hBs, 7);
673
22.9k
    if (pDChar->cicpIndex > 0) {
674
17.5k
      pDChar->present = 1;
675
17.5k
      pDChar->isCICP = 1;
676
17.5k
    } else {
677
5.38k
      pDChar->present = 0;
678
5.38k
    }
679
22.9k
  } else {
680
19.3k
    pDChar->present = FDKreadBits(hBs, 1);
681
19.3k
    if (isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
682
19.3k
    if (pDChar->present) {
683
7.59k
      if (!isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
684
7.59k
      if (pDChar->isCICP) {
685
3.79k
        if (isBox) FDKpushFor(hBs, 1); /* reserved */
686
3.79k
        pDChar->cicpIndex = FDKreadBits(hBs, 7);
687
3.80k
      } else {
688
3.80k
        pDChar->custom.left = FDKreadBits(hBs, 4);
689
3.80k
        pDChar->custom.right = FDKreadBits(hBs, 4);
690
3.80k
      }
691
7.59k
    }
692
19.3k
  }
693
42.2k
}
694
695
static void _readBandBorder(HANDLE_FDK_BITSTREAM hBs, BAND_BORDER* pBBord,
696
25.8k
                            int drcBandType, int isBox) {
697
25.8k
  if (drcBandType) {
698
14.8k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
699
14.8k
    pBBord->crossoverFreqIndex = FDKreadBits(hBs, 4);
700
14.8k
  } else {
701
11.0k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
702
11.0k
    pBBord->startSubBandIndex = FDKreadBits(hBs, 10);
703
11.0k
  }
704
25.8k
}
705
706
static DRC_ERROR _readGainSet(HANDLE_FDK_BITSTREAM hBs, const int version,
707
                              int* gainSequenceIndex, GAIN_SET* pGSet,
708
203k
                              int isBox) {
709
203k
  if (isBox) FDKpushFor(hBs, 2); /* reserved */
710
203k
  pGSet->gainCodingProfile = FDKreadBits(hBs, 2);
711
203k
  pGSet->gainInterpolationType = FDKreadBits(hBs, 1);
712
203k
  pGSet->fullFrame = FDKreadBits(hBs, 1);
713
203k
  pGSet->timeAlignment = FDKreadBits(hBs, 1);
714
203k
  pGSet->timeDeltaMinPresent = FDKreadBits(hBs, 1);
715
716
203k
  if (pGSet->timeDeltaMinPresent) {
717
13.8k
    int bsTimeDeltaMin;
718
13.8k
    if (isBox) FDKpushFor(hBs, 5); /* reserved */
719
13.8k
    bsTimeDeltaMin = FDKreadBits(hBs, 11);
720
13.8k
    pGSet->timeDeltaMin = bsTimeDeltaMin + 1;
721
13.8k
  }
722
723
203k
  if (pGSet->gainCodingProfile != GCP_CONSTANT) {
724
189k
    int i;
725
189k
    if (isBox) FDKpushFor(hBs, 3); /* reserved */
726
189k
    pGSet->bandCount = FDKreadBits(hBs, 4);
727
189k
    if (pGSet->bandCount > 4) return DE_MEMORY_ERROR;
728
729
183k
    if ((pGSet->bandCount > 1) || isBox) {
730
12.8k
      pGSet->drcBandType = FDKreadBits(hBs, 1);
731
12.8k
    }
732
733
225k
    for (i = 0; i < pGSet->bandCount; i++) {
734
42.2k
      if (version == 0) {
735
22.9k
        *gainSequenceIndex = (*gainSequenceIndex) + 1;
736
22.9k
      } else {
737
19.3k
        int indexPresent;
738
19.3k
        indexPresent = (isBox) ? 1 : FDKreadBits(hBs, 1);
739
19.3k
        if (indexPresent) {
740
6.36k
          int bsIndex;
741
6.36k
          bsIndex = FDKreadBits(hBs, 6);
742
6.36k
          *gainSequenceIndex = bsIndex;
743
12.9k
        } else {
744
12.9k
          *gainSequenceIndex = (*gainSequenceIndex) + 1;
745
12.9k
        }
746
19.3k
      }
747
42.2k
      pGSet->gainSequenceIndex[i] = *gainSequenceIndex;
748
42.2k
      _readDrcCharacteristic(hBs, version, &(pGSet->drcCharacteristic[i]),
749
42.2k
                             isBox);
750
42.2k
    }
751
208k
    for (i = 1; i < pGSet->bandCount; i++) {
752
25.8k
      _readBandBorder(hBs, &(pGSet->bandBorder[i]), pGSet->drcBandType, isBox);
753
25.8k
    }
754
183k
  } else {
755
13.7k
    pGSet->bandCount = 1;
756
13.7k
    *gainSequenceIndex = (*gainSequenceIndex) + 1;
757
13.7k
    pGSet->gainSequenceIndex[0] = *gainSequenceIndex;
758
13.7k
  }
759
760
196k
  return DE_OK;
761
203k
}
762
763
static DRC_ERROR _readCustomDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs,
764
                                              const CHARACTERISTIC_SIDE side,
765
                                              UCHAR* pCharacteristicFormat,
766
                                              CUSTOM_DRC_CHAR* pCChar,
767
64.8k
                                              int isBox) {
768
64.8k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
769
64.8k
  *pCharacteristicFormat = FDKreadBits(hBs, 1);
770
64.8k
  if (*pCharacteristicFormat == CF_SIGMOID) {
771
48.1k
    int bsGain, bsIoRatio, bsExp;
772
48.1k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
773
48.1k
    bsGain = FDKreadBits(hBs, 6);
774
48.1k
    if (side == CS_LEFT) {
775
17.8k
      pCChar->sigmoid.gain = (FIXP_SGL)(bsGain << (FRACT_BITS - 1 - 6));
776
30.3k
    } else {
777
30.3k
      pCChar->sigmoid.gain = (FIXP_SGL)(-bsGain << (FRACT_BITS - 1 - 6));
778
30.3k
    }
779
48.1k
    bsIoRatio = FDKreadBits(hBs, 4);
780
    /* pCChar->sigmoid.ioRatio = 0.05 + 0.15 * bsIoRatio; */
781
48.1k
    pCChar->sigmoid.ioRatio =
782
48.1k
        FL2FXCONST_SGL(0.05f / (float)(1 << 2)) +
783
48.1k
        (FIXP_SGL)((((3 * bsIoRatio) << (FRACT_BITS - 1)) / 5) >> 4);
784
48.1k
    bsExp = FDKreadBits(hBs, 4);
785
48.1k
    if (bsExp < 15) {
786
44.8k
      pCChar->sigmoid.exp = (FIXP_SGL)((1 + 2 * bsExp) << (FRACT_BITS - 1 - 5));
787
44.8k
    } else {
788
3.25k
      pCChar->sigmoid.exp = (FIXP_SGL)MAXVAL_SGL; /* represents infinity */
789
3.25k
    }
790
48.1k
    pCChar->sigmoid.flipSign = FDKreadBits(hBs, 1);
791
48.1k
  } else { /* CF_NODES */
792
16.6k
    int i, bsCharacteristicNodeCount, bsNodeLevelDelta, bsNodeGain;
793
16.6k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
794
16.6k
    bsCharacteristicNodeCount = FDKreadBits(hBs, 2);
795
16.6k
    pCChar->nodes.characteristicNodeCount = bsCharacteristicNodeCount + 1;
796
16.6k
    if (pCChar->nodes.characteristicNodeCount > 4) return DE_MEMORY_ERROR;
797
16.6k
    pCChar->nodes.nodeLevel[0] = DRC_INPUT_LOUDNESS_TARGET_SGL;
798
16.6k
    pCChar->nodes.nodeGain[0] = (FIXP_SGL)0;
799
58.1k
    for (i = 0; i < pCChar->nodes.characteristicNodeCount; i++) {
800
41.5k
      if (isBox) FDKpushFor(hBs, 3); /* reserved */
801
41.5k
      bsNodeLevelDelta = FDKreadBits(hBs, 5);
802
41.5k
      if (side == CS_LEFT) {
803
19.8k
        pCChar->nodes.nodeLevel[i + 1] =
804
19.8k
            pCChar->nodes.nodeLevel[i] -
805
19.8k
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
806
21.6k
      } else {
807
21.6k
        pCChar->nodes.nodeLevel[i + 1] =
808
21.6k
            pCChar->nodes.nodeLevel[i] +
809
21.6k
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
810
21.6k
      }
811
41.5k
      bsNodeGain = FDKreadBits(hBs, 8);
812
41.5k
      pCChar->nodes.nodeGain[i + 1] = (FIXP_SGL)(
813
41.5k
          (bsNodeGain - 128)
814
41.5k
          << (FRACT_BITS - 1 - 1 - 7)); /* 0.5f * bsNodeGain - 64.0f; */
815
41.5k
    }
816
16.6k
  }
817
64.8k
  return DE_OK;
818
64.8k
}
819
820
58.3k
static void _skipLoudEqInstructions(HANDLE_FDK_BITSTREAM hBs) {
821
58.3k
  int i;
822
58.3k
  int downmixIdPresent, additionalDownmixIdPresent,
823
58.3k
      additionalDownmixIdCount = 0;
824
58.3k
  int drcSetIdPresent, additionalDrcSetIdPresent, additionalDrcSetIdCount = 0;
825
58.3k
  int eqSetIdPresent, additionalEqSetIdPresent, additionalEqSetIdCount = 0;
826
58.3k
  int loudEqGainSequenceCount, drcCharacteristicFormatIsCICP;
827
828
58.3k
  FDKpushFor(hBs, 4); /* loudEqSetId */
829
58.3k
  FDKpushFor(hBs, 4); /* drcLocation */
830
58.3k
  downmixIdPresent = FDKreadBits(hBs, 1);
831
58.3k
  if (downmixIdPresent) {
832
9.12k
    FDKpushFor(hBs, 7); /* downmixId */
833
9.12k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
834
9.12k
    if (additionalDownmixIdPresent) {
835
4.79k
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
836
165k
      for (i = 0; i < additionalDownmixIdCount; i++) {
837
160k
        FDKpushFor(hBs, 7); /* additionalDownmixId */
838
160k
      }
839
4.79k
    }
840
9.12k
  }
841
842
58.3k
  drcSetIdPresent = FDKreadBits(hBs, 1);
843
58.3k
  if (drcSetIdPresent) {
844
6.78k
    FDKpushFor(hBs, 6); /* drcSetId */
845
6.78k
    additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
846
6.78k
    if (additionalDrcSetIdPresent) {
847
2.40k
      additionalDrcSetIdCount = FDKreadBits(hBs, 6);
848
44.8k
      for (i = 0; i < additionalDrcSetIdCount; i++) {
849
42.4k
        FDKpushFor(hBs, 6); /* additionalDrcSetId; */
850
42.4k
      }
851
2.40k
    }
852
6.78k
  }
853
854
58.3k
  eqSetIdPresent = FDKreadBits(hBs, 1);
855
58.3k
  if (eqSetIdPresent) {
856
5.67k
    FDKpushFor(hBs, 6); /* eqSetId */
857
5.67k
    additionalEqSetIdPresent = FDKreadBits(hBs, 1);
858
5.67k
    if (additionalEqSetIdPresent) {
859
2.71k
      additionalEqSetIdCount = FDKreadBits(hBs, 6);
860
51.4k
      for (i = 0; i < additionalEqSetIdCount; i++) {
861
48.7k
        FDKpushFor(hBs, 6); /* additionalEqSetId; */
862
48.7k
      }
863
2.71k
    }
864
5.67k
  }
865
866
58.3k
  FDKpushFor(hBs, 1); /* loudnessAfterDrc */
867
58.3k
  FDKpushFor(hBs, 1); /* loudnessAfterEq */
868
58.3k
  loudEqGainSequenceCount = FDKreadBits(hBs, 6);
869
394k
  for (i = 0; i < loudEqGainSequenceCount; i++) {
870
336k
    FDKpushFor(hBs, 6); /* gainSequenceIndex */
871
336k
    drcCharacteristicFormatIsCICP = FDKreadBits(hBs, 1);
872
336k
    if (drcCharacteristicFormatIsCICP) {
873
115k
      FDKpushFor(hBs, 7); /* drcCharacteristic */
874
221k
    } else {
875
221k
      FDKpushFor(hBs, 4); /* drcCharacteristicLeftIndex */
876
221k
      FDKpushFor(hBs, 4); /* drcCharacteristicRightIndex */
877
221k
    }
878
336k
    FDKpushFor(hBs, 6); /* frequencyRangeIndex */
879
336k
    FDKpushFor(hBs, 3); /* bsLoudEqScaling */
880
336k
    FDKpushFor(hBs, 5); /* bsLoudEqOffset */
881
336k
  }
882
58.3k
}
883
884
68.4k
static void _skipEqSubbandGainSpline(HANDLE_FDK_BITSTREAM hBs) {
885
68.4k
  int nEqNodes, k, bits;
886
68.4k
  nEqNodes = FDKreadBits(hBs, 5);
887
68.4k
  nEqNodes += 2;
888
612k
  for (k = 0; k < nEqNodes; k++) {
889
544k
    bits = FDKreadBits(hBs, 1);
890
544k
    if (!bits) {
891
306k
      FDKpushFor(hBs, 4);
892
306k
    }
893
544k
  }
894
68.4k
  FDKpushFor(hBs, 4 * (nEqNodes - 1));
895
68.4k
  bits = FDKreadBits(hBs, 2);
896
68.4k
  switch (bits) {
897
45.0k
    case 0:
898
45.0k
      FDKpushFor(hBs, 5);
899
45.0k
      break;
900
8.33k
    case 1:
901
14.8k
    case 2:
902
14.8k
      FDKpushFor(hBs, 4);
903
14.8k
      break;
904
8.54k
    case 3:
905
8.54k
      FDKpushFor(hBs, 3);
906
8.54k
      break;
907
68.4k
  }
908
68.4k
  FDKpushFor(hBs, 5 * (nEqNodes - 1));
909
68.4k
}
910
911
21.5k
static void _skipEqCoefficients(HANDLE_FDK_BITSTREAM hBs) {
912
21.5k
  int j, k;
913
21.5k
  int eqDelayMaxPresent;
914
21.5k
  int uniqueFilterBlockCount, filterElementCount, filterElementGainPresent;
915
21.5k
  int uniqueTdFilterElementCount, eqFilterFormat, bsRealZeroRadiusOneCount,
916
21.5k
      realZeroCount, genericZeroCount, realPoleCount, complexPoleCount,
917
21.5k
      firFilterOrder;
918
21.5k
  int uniqueEqSubbandGainsCount, eqSubbandGainRepresentation,
919
21.5k
      eqSubbandGainCount;
920
21.5k
  int eqSubbandGainFormat;
921
922
21.5k
  eqDelayMaxPresent = FDKreadBits(hBs, 1);
923
21.5k
  if (eqDelayMaxPresent) {
924
7.45k
    FDKpushFor(hBs, 8); /* bsEqDelayMax */
925
7.45k
  }
926
927
21.5k
  uniqueFilterBlockCount = FDKreadBits(hBs, 6);
928
265k
  for (j = 0; j < uniqueFilterBlockCount; j++) {
929
244k
    filterElementCount = FDKreadBits(hBs, 6);
930
3.44M
    for (k = 0; k < filterElementCount; k++) {
931
3.20M
      FDKpushFor(hBs, 6); /* filterElementIndex */
932
3.20M
      filterElementGainPresent = FDKreadBits(hBs, 1);
933
3.20M
      if (filterElementGainPresent) {
934
1.22M
        FDKpushFor(hBs, 10); /* bsFilterElementGain */
935
1.22M
      }
936
3.20M
    }
937
244k
  }
938
21.5k
  uniqueTdFilterElementCount = FDKreadBits(hBs, 6);
939
163k
  for (j = 0; j < uniqueTdFilterElementCount; j++) {
940
141k
    eqFilterFormat = FDKreadBits(hBs, 1);
941
141k
    if (eqFilterFormat == 0) { /* pole/zero */
942
108k
      bsRealZeroRadiusOneCount = FDKreadBits(hBs, 3);
943
108k
      realZeroCount = FDKreadBits(hBs, 6);
944
108k
      genericZeroCount = FDKreadBits(hBs, 6);
945
108k
      realPoleCount = FDKreadBits(hBs, 4);
946
108k
      complexPoleCount = FDKreadBits(hBs, 4);
947
108k
      FDKpushFor(hBs, 2 * bsRealZeroRadiusOneCount * 1);
948
108k
      FDKpushFor(hBs, realZeroCount * 8);
949
108k
      FDKpushFor(hBs, genericZeroCount * 14);
950
108k
      FDKpushFor(hBs, realPoleCount * 8);
951
108k
      FDKpushFor(hBs, complexPoleCount * 14);
952
108k
    } else { /* FIR coefficients */
953
33.3k
      firFilterOrder = FDKreadBits(hBs, 7);
954
33.3k
      FDKpushFor(hBs, 1);
955
33.3k
      FDKpushFor(hBs, (firFilterOrder / 2 + 1) * 11);
956
33.3k
    }
957
141k
  }
958
21.5k
  uniqueEqSubbandGainsCount = FDKreadBits(hBs, 6);
959
21.5k
  if (uniqueEqSubbandGainsCount > 0) {
960
10.6k
    eqSubbandGainRepresentation = FDKreadBits(hBs, 1);
961
10.6k
    eqSubbandGainFormat = FDKreadBits(hBs, 4);
962
10.6k
    switch (eqSubbandGainFormat) {
963
582
      case GF_QMF32:
964
582
        eqSubbandGainCount = 32;
965
582
        break;
966
457
      case GF_QMFHYBRID39:
967
457
        eqSubbandGainCount = 39;
968
457
        break;
969
929
      case GF_QMF64:
970
929
        eqSubbandGainCount = 64;
971
929
        break;
972
322
      case GF_QMFHYBRID71:
973
322
        eqSubbandGainCount = 71;
974
322
        break;
975
346
      case GF_QMF128:
976
346
        eqSubbandGainCount = 128;
977
346
        break;
978
428
      case GF_QMFHYBRID135:
979
428
        eqSubbandGainCount = 135;
980
428
        break;
981
438
      case GF_UNIFORM:
982
7.57k
      default:
983
7.57k
        eqSubbandGainCount = FDKreadBits(hBs, 8);
984
7.57k
        eqSubbandGainCount++;
985
7.57k
        break;
986
10.6k
    }
987
259k
    for (k = 0; k < uniqueEqSubbandGainsCount; k++) {
988
248k
      if (eqSubbandGainRepresentation == 1) {
989
68.4k
        _skipEqSubbandGainSpline(hBs);
990
180k
      } else {
991
180k
        FDKpushFor(hBs, eqSubbandGainCount * 9);
992
180k
      }
993
248k
    }
994
10.6k
  }
995
21.5k
}
996
997
static void _skipTdFilterCascade(HANDLE_FDK_BITSTREAM hBs,
998
13.9k
                                 const int eqChannelGroupCount) {
999
13.9k
  int i, eqCascadeGainPresent, filterBlockCount, eqPhaseAlignmentPresent;
1000
38.1k
  for (i = 0; i < eqChannelGroupCount; i++) {
1001
24.1k
    eqCascadeGainPresent = FDKreadBits(hBs, 1);
1002
24.1k
    if (eqCascadeGainPresent) {
1003
9.09k
      FDKpushFor(hBs, 10); /* bsEqCascadeGain */
1004
9.09k
    }
1005
24.1k
    filterBlockCount = FDKreadBits(hBs, 4);
1006
24.1k
    FDKpushFor(hBs, filterBlockCount * 7); /* filterBlockIndex */
1007
24.1k
  }
1008
13.9k
  eqPhaseAlignmentPresent = FDKreadBits(hBs, 1);
1009
13.9k
  {
1010
13.9k
    if (eqPhaseAlignmentPresent) {
1011
14.9k
      for (i = 0; i < eqChannelGroupCount; i++) {
1012
8.41k
        FDKpushFor(hBs, (eqChannelGroupCount - i - 1) * 1);
1013
8.41k
      }
1014
6.53k
    }
1015
13.9k
  }
1016
13.9k
}
1017
1018
static DRC_ERROR _skipEqInstructions(HANDLE_FDK_BITSTREAM hBs,
1019
99.1k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1020
99.1k
  DRC_ERROR err = DE_OK;
1021
99.1k
  int c, i, k, channelCount;
1022
99.1k
  int downmixIdPresent, downmixId, eqApplyToDownmix, additionalDownmixIdPresent,
1023
99.1k
      additionalDownmixIdCount = 0;
1024
99.1k
  int additionalDrcSetIdPresent, additionalDrcSetIdCount;
1025
99.1k
  int dependsOnEqSetPresent, eqChannelGroupCount, tdFilterCascadePresent,
1026
99.1k
      subbandGainsPresent, eqTransitionDurationPresent;
1027
99.1k
  UCHAR eqChannelGroupForChannel[8];
1028
1029
99.1k
  FDKpushFor(hBs, 6); /* eqSetId */
1030
99.1k
  FDKpushFor(hBs, 4); /* eqSetComplexityLevel */
1031
99.1k
  downmixIdPresent = FDKreadBits(hBs, 1);
1032
99.1k
  if (downmixIdPresent) {
1033
18.1k
    downmixId = FDKreadBits(hBs, 7);
1034
18.1k
    eqApplyToDownmix = FDKreadBits(hBs, 1);
1035
18.1k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1036
18.1k
    if (additionalDownmixIdPresent) {
1037
6.87k
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
1038
6.87k
      FDKpushFor(hBs, additionalDownmixIdCount * 7); /* additionalDownmixId */
1039
6.87k
    }
1040
80.9k
  } else {
1041
80.9k
    downmixId = 0;
1042
80.9k
    eqApplyToDownmix = 0;
1043
80.9k
  }
1044
99.1k
  FDKpushFor(hBs, 6); /* drcSetId */
1045
99.1k
  additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
1046
99.1k
  if (additionalDrcSetIdPresent) {
1047
14.3k
    additionalDrcSetIdCount = FDKreadBits(hBs, 6);
1048
276k
    for (i = 0; i < additionalDrcSetIdCount; i++) {
1049
262k
      FDKpushFor(hBs, 6); /* additionalDrcSetId */
1050
262k
    }
1051
14.3k
  }
1052
99.1k
  FDKpushFor(hBs, 16); /* eqSetPurpose */
1053
99.1k
  dependsOnEqSetPresent = FDKreadBits(hBs, 1);
1054
99.1k
  if (dependsOnEqSetPresent) {
1055
13.3k
    FDKpushFor(hBs, 6); /* dependsOnEqSet */
1056
85.7k
  } else {
1057
85.7k
    FDKpushFor(hBs, 1); /* noIndependentEqUse */
1058
85.7k
  }
1059
1060
99.1k
  channelCount = hUniDrcConfig->channelLayout.baseChannelCount;
1061
99.1k
  if ((downmixIdPresent == 1) && (eqApplyToDownmix == 1) && (downmixId != 0) &&
1062
9.39k
      (downmixId != DOWNMIX_ID_ANY_DOWNMIX) &&
1063
7.76k
      (additionalDownmixIdCount == 0)) {
1064
4.90k
    DOWNMIX_INSTRUCTIONS* pDown =
1065
4.90k
        selectDownmixInstructions(hUniDrcConfig, downmixId);
1066
4.90k
    if (pDown == NULL) return DE_NOT_OK;
1067
1068
631
    channelCount =
1069
631
        pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1070
94.2k
  } else if ((downmixId == DOWNMIX_ID_ANY_DOWNMIX) ||
1071
92.4k
             (additionalDownmixIdCount > 1)) {
1072
6.36k
    channelCount = 1;
1073
6.36k
  }
1074
1075
94.8k
  eqChannelGroupCount = 0;
1076
287k
  for (c = 0; c < channelCount; c++) {
1077
192k
    int newGroup = 1;
1078
192k
    if (c >= 8) return DE_MEMORY_ERROR;
1079
192k
    eqChannelGroupForChannel[c] = FDKreadBits(hBs, 7);
1080
519k
    for (k = 0; k < c; k++) {
1081
327k
      if (eqChannelGroupForChannel[c] == eqChannelGroupForChannel[k]) {
1082
243k
        newGroup = 0;
1083
243k
      }
1084
327k
    }
1085
192k
    if (newGroup == 1) {
1086
101k
      eqChannelGroupCount += 1;
1087
101k
    }
1088
192k
  }
1089
94.3k
  tdFilterCascadePresent = FDKreadBits(hBs, 1);
1090
94.3k
  if (tdFilterCascadePresent) {
1091
13.9k
    _skipTdFilterCascade(hBs, eqChannelGroupCount);
1092
13.9k
  }
1093
94.3k
  subbandGainsPresent = FDKreadBits(hBs, 1);
1094
94.3k
  if (subbandGainsPresent) {
1095
11.9k
    FDKpushFor(hBs, eqChannelGroupCount * 6); /* subbandGainsIndex */
1096
11.9k
  }
1097
94.3k
  eqTransitionDurationPresent = FDKreadBits(hBs, 1);
1098
94.3k
  if (eqTransitionDurationPresent) {
1099
12.2k
    FDKpushFor(hBs, 5); /* bsEqTransitionDuration */
1100
12.2k
  }
1101
94.3k
  return err;
1102
94.8k
}
1103
1104
22.6k
static void _skipDrcCoefficientsBasic(HANDLE_FDK_BITSTREAM hBs) {
1105
22.6k
  FDKpushFor(hBs, 4); /* drcLocation */
1106
22.6k
  FDKpushFor(hBs, 7); /* drcCharacteristic */
1107
22.6k
}
1108
1109
static DRC_ERROR _readDrcCoefficientsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1110
                                            const int version,
1111
152k
                                            DRC_COEFFICIENTS_UNI_DRC* pCoef) {
1112
152k
  DRC_ERROR err = DE_OK;
1113
152k
  int i, bsDrcFrameSize;
1114
152k
  int gainSequenceIndex = -1;
1115
1116
152k
  pCoef->drcLocation = FDKreadBits(hBs, 4);
1117
152k
  pCoef->drcFrameSizePresent = FDKreadBits(hBs, 1);
1118
1119
152k
  if (pCoef->drcFrameSizePresent == 1) {
1120
20.6k
    bsDrcFrameSize = FDKreadBits(hBs, 15);
1121
20.6k
    pCoef->drcFrameSize = bsDrcFrameSize + 1;
1122
20.6k
  }
1123
152k
  if (version == 0) {
1124
96.3k
    int gainSequenceCount = 0, gainSetCount;
1125
96.3k
    pCoef->characteristicLeftCount = 0;
1126
96.3k
    pCoef->characteristicRightCount = 0;
1127
96.3k
    gainSetCount = FDKreadBits(hBs, 6);
1128
96.3k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1129
178k
    for (i = 0; i < gainSetCount; i++) {
1130
84.9k
      GAIN_SET tmpGset;
1131
84.9k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1132
84.9k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1133
84.9k
      if (err) return err;
1134
81.9k
      gainSequenceCount += tmpGset.bandCount;
1135
1136
81.9k
      if (i >= 12) continue;
1137
37.1k
      pCoef->gainSet[i] = tmpGset;
1138
37.1k
    }
1139
93.3k
    pCoef->gainSequenceCount = gainSequenceCount;
1140
93.3k
  } else { /* (version == 1) */
1141
56.0k
    UCHAR drcCharacteristicLeftPresent, drcCharacteristicRightPresent;
1142
56.0k
    UCHAR shapeFiltersPresent, shapeFilterCount, tmpPresent;
1143
56.0k
    int gainSetCount;
1144
56.0k
    drcCharacteristicLeftPresent = FDKreadBits(hBs, 1);
1145
56.0k
    if (drcCharacteristicLeftPresent) {
1146
6.24k
      pCoef->characteristicLeftCount = FDKreadBits(hBs, 4);
1147
6.24k
      if ((pCoef->characteristicLeftCount + 1) > 16) return DE_MEMORY_ERROR;
1148
31.5k
      for (i = 0; i < pCoef->characteristicLeftCount; i++) {
1149
25.3k
        err = _readCustomDrcCharacteristic(
1150
25.3k
            hBs, CS_LEFT, &(pCoef->characteristicLeftFormat[i + 1]),
1151
25.3k
            &(pCoef->customCharacteristicLeft[i + 1]), 0);
1152
25.3k
        if (err) return err;
1153
25.3k
      }
1154
6.24k
    }
1155
56.0k
    drcCharacteristicRightPresent = FDKreadBits(hBs, 1);
1156
56.0k
    if (drcCharacteristicRightPresent) {
1157
5.23k
      pCoef->characteristicRightCount = FDKreadBits(hBs, 4);
1158
5.23k
      if ((pCoef->characteristicRightCount + 1) > 16) return DE_MEMORY_ERROR;
1159
44.7k
      for (i = 0; i < pCoef->characteristicRightCount; i++) {
1160
39.4k
        err = _readCustomDrcCharacteristic(
1161
39.4k
            hBs, CS_RIGHT, &(pCoef->characteristicRightFormat[i + 1]),
1162
39.4k
            &(pCoef->customCharacteristicRight[i + 1]), 0);
1163
39.4k
        if (err) return err;
1164
39.4k
      }
1165
5.23k
    }
1166
56.0k
    shapeFiltersPresent = FDKreadBits(hBs, 1);
1167
56.0k
    if (shapeFiltersPresent) {
1168
6.99k
      shapeFilterCount = FDKreadBits(hBs, 4);
1169
47.2k
      for (i = 0; i < shapeFilterCount; i++) {
1170
40.2k
        tmpPresent = FDKreadBits(hBs, 1);
1171
40.2k
        if (tmpPresent) /* lfCutParams */
1172
10.3k
          FDKpushFor(hBs, 5);
1173
1174
40.2k
        tmpPresent = FDKreadBits(hBs, 1);
1175
40.2k
        if (tmpPresent) /* lfBoostParams */
1176
9.81k
          FDKpushFor(hBs, 5);
1177
1178
40.2k
        tmpPresent = FDKreadBits(hBs, 1);
1179
40.2k
        if (tmpPresent) /* hfCutParams */
1180
9.79k
          FDKpushFor(hBs, 5);
1181
1182
40.2k
        tmpPresent = FDKreadBits(hBs, 1);
1183
40.2k
        if (tmpPresent) /* hfBoostParams */
1184
8.51k
          FDKpushFor(hBs, 5);
1185
40.2k
      }
1186
6.99k
    }
1187
56.0k
    pCoef->gainSequenceCount = FDKreadBits(hBs, 6);
1188
56.0k
    gainSetCount = FDKreadBits(hBs, 6);
1189
56.0k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1190
170k
    for (i = 0; i < gainSetCount; i++) {
1191
118k
      GAIN_SET tmpGset;
1192
118k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1193
118k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1194
118k
      if (err) return err;
1195
1196
114k
      if (i >= 12) continue;
1197
49.1k
      pCoef->gainSet[i] = tmpGset;
1198
49.1k
    }
1199
56.0k
  }
1200
1.89M
  for (i = 0; i < 12; i++) {
1201
1.75M
    pCoef->gainSetIndexForGainSequence[i] = 255;
1202
1.75M
  }
1203
223k
  for (i = 0; i < pCoef->gainSetCount; i++) {
1204
77.6k
    int b;
1205
121k
    for (b = 0; b < pCoef->gainSet[i].bandCount; b++) {
1206
44.0k
      if (pCoef->gainSet[i].gainSequenceIndex[b] >= 12) continue;
1207
36.6k
      pCoef->gainSetIndexForGainSequence[pCoef->gainSet[i]
1208
36.6k
                                             .gainSequenceIndex[b]] = i;
1209
36.6k
    }
1210
77.6k
  }
1211
1212
145k
  return err;
1213
152k
}
1214
1215
69.2k
static void _skipDrcInstructionsBasic(HANDLE_FDK_BITSTREAM hBs) {
1216
69.2k
  int drcSetEffect;
1217
69.2k
  int additionalDownmixIdPresent, additionalDownmixIdCount,
1218
69.2k
      limiterPeakTargetPresent;
1219
69.2k
  int drcSetTargetLoudnessPresent, drcSetTargetLoudnessValueLowerPresent;
1220
1221
69.2k
  FDKpushFor(hBs, 6); /* drcSetId */
1222
69.2k
  FDKpushFor(hBs, 4); /* drcLocation */
1223
69.2k
  FDKpushFor(hBs, 7); /* downmixId */
1224
69.2k
  additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1225
69.2k
  if (additionalDownmixIdPresent) {
1226
16.7k
    additionalDownmixIdCount = FDKreadBits(hBs, 3);
1227
16.7k
    FDKpushFor(hBs, 7 * additionalDownmixIdCount); /* additionalDownmixId */
1228
16.7k
  }
1229
1230
69.2k
  drcSetEffect = FDKreadBits(hBs, 16);
1231
69.2k
  if (!(drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF))) {
1232
44.1k
    limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1233
44.1k
    if (limiterPeakTargetPresent) {
1234
8.11k
      FDKpushFor(hBs, 8); /* bsLimiterPeakTarget */
1235
8.11k
    }
1236
44.1k
  }
1237
1238
69.2k
  drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1239
69.2k
  if (drcSetTargetLoudnessPresent) {
1240
15.8k
    FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueUpper */
1241
15.8k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1242
15.8k
    if (drcSetTargetLoudnessValueLowerPresent) {
1243
6.63k
      FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueLower */
1244
6.63k
    }
1245
15.8k
  }
1246
69.2k
}
1247
1248
static DRC_ERROR _readDrcInstructionsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1249
                                            const int version,
1250
                                            HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
1251
1.06M
                                            DRC_INSTRUCTIONS_UNI_DRC* pInst) {
1252
1.06M
  DRC_ERROR err = DE_OK;
1253
1.06M
  int i, g, c;
1254
1.06M
  int downmixIdPresent, additionalDownmixIdPresent, additionalDownmixIdCount;
1255
1.06M
  int bsLimiterPeakTarget, channelCount;
1256
1.06M
  DRC_COEFFICIENTS_UNI_DRC* pCoef = NULL;
1257
1.06M
  int repeatParameters, bsRepeatParametersCount;
1258
1.06M
  int repeatSequenceIndex, bsRepeatSequenceCount;
1259
1.06M
  SCHAR* gainSetIndex = pInst->gainSetIndex;
1260
1.06M
  SCHAR channelGroupForChannel[8];
1261
1.06M
  DUCKING_MODIFICATION duckingModificationForChannelGroup[8];
1262
1263
1.06M
  pInst->drcSetId = FDKreadBits(hBs, 6);
1264
1.06M
  if (version == 0) {
1265
    /* Assume all v0 DRC sets to be manageable in terms of complexity */
1266
952k
    pInst->drcSetComplexityLevel = 2;
1267
952k
  } else {
1268
112k
    pInst->drcSetComplexityLevel = FDKreadBits(hBs, 4);
1269
112k
  }
1270
1.06M
  pInst->drcLocation = FDKreadBits(hBs, 4);
1271
1.06M
  if (version == 0) {
1272
952k
    downmixIdPresent = 1;
1273
952k
  } else {
1274
112k
    downmixIdPresent = FDKreadBits(hBs, 1);
1275
112k
  }
1276
1.06M
  if (downmixIdPresent) {
1277
960k
    pInst->downmixId[0] = FDKreadBits(hBs, 7);
1278
960k
    if (version == 0) {
1279
952k
      if (pInst->downmixId[0] == 0)
1280
903k
        pInst->drcApplyToDownmix = 0;
1281
48.8k
      else
1282
48.8k
        pInst->drcApplyToDownmix = 1;
1283
952k
    } else {
1284
8.43k
      pInst->drcApplyToDownmix = FDKreadBits(hBs, 1);
1285
8.43k
    }
1286
1287
960k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1288
960k
    if (additionalDownmixIdPresent) {
1289
32.5k
      additionalDownmixIdCount = FDKreadBits(hBs, 3);
1290
32.5k
      if ((1 + additionalDownmixIdCount) > 8) return DE_MEMORY_ERROR;
1291
134k
      for (i = 0; i < additionalDownmixIdCount; i++) {
1292
101k
        pInst->downmixId[i + 1] = FDKreadBits(hBs, 7);
1293
101k
      }
1294
32.5k
      pInst->downmixIdCount = 1 + additionalDownmixIdCount;
1295
928k
    } else {
1296
928k
      pInst->downmixIdCount = 1;
1297
928k
    }
1298
960k
  } else {
1299
103k
    pInst->downmixId[0] = 0;
1300
103k
    pInst->downmixIdCount = 1;
1301
103k
  }
1302
1303
1.06M
  pInst->drcSetEffect = FDKreadBits(hBs, 16);
1304
1305
1.06M
  if ((pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) == 0) {
1306
1.03M
    pInst->limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1307
1.03M
    if (pInst->limiterPeakTargetPresent) {
1308
18.8k
      bsLimiterPeakTarget = FDKreadBits(hBs, 8);
1309
18.8k
      pInst->limiterPeakTarget = -(FIXP_SGL)(
1310
18.8k
          bsLimiterPeakTarget
1311
18.8k
          << (FRACT_BITS - 1 - 3 - 5)); /* - bsLimiterPeakTarget * 0.125; */
1312
18.8k
    }
1313
1.03M
  }
1314
1315
1.06M
  pInst->drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1316
1317
  /* set default values */
1318
1.06M
  pInst->drcSetTargetLoudnessValueUpper = 0;
1319
1.06M
  pInst->drcSetTargetLoudnessValueLower = -63;
1320
1321
1.06M
  if (pInst->drcSetTargetLoudnessPresent == 1) {
1322
27.1k
    int bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1323
27.1k
    int drcSetTargetLoudnessValueLowerPresent;
1324
27.1k
    bsDrcSetTargetLoudnessValueUpper = FDKreadBits(hBs, 6);
1325
27.1k
    pInst->drcSetTargetLoudnessValueUpper =
1326
27.1k
        bsDrcSetTargetLoudnessValueUpper - 63;
1327
27.1k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1328
27.1k
    if (drcSetTargetLoudnessValueLowerPresent == 1) {
1329
8.63k
      bsDrcSetTargetLoudnessValueLower = FDKreadBits(hBs, 6);
1330
8.63k
      pInst->drcSetTargetLoudnessValueLower =
1331
8.63k
          bsDrcSetTargetLoudnessValueLower - 63;
1332
8.63k
    }
1333
27.1k
  }
1334
1335
1.06M
  pInst->dependsOnDrcSetPresent = FDKreadBits(hBs, 1);
1336
1337
1.06M
  pInst->noIndependentUse = 0;
1338
1.06M
  if (pInst->dependsOnDrcSetPresent) {
1339
24.0k
    pInst->dependsOnDrcSet = FDKreadBits(hBs, 6);
1340
1.04M
  } else {
1341
1.04M
    pInst->noIndependentUse = FDKreadBits(hBs, 1);
1342
1.04M
  }
1343
1344
1.06M
  if (version == 0) {
1345
952k
    pInst->requiresEq = 0;
1346
952k
  } else {
1347
112k
    pInst->requiresEq = FDKreadBits(hBs, 1);
1348
112k
  }
1349
1350
1.06M
  pCoef = selectDrcCoefficients(hUniDrcConfig, pInst->drcLocation);
1351
1352
1.06M
  pInst->drcChannelCount = channelCount =
1353
1.06M
      hUniDrcConfig->channelLayout.baseChannelCount;
1354
1355
1.06M
  if (pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) {
1356
30.4k
    DUCKING_MODIFICATION* pDModForChannel =
1357
30.4k
        pInst->duckingModificationForChannel;
1358
30.4k
    c = 0;
1359
88.3k
    while (c < channelCount) {
1360
59.5k
      int bsGainSetIndex;
1361
59.5k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1362
59.5k
      if (c >= 8) return DE_MEMORY_ERROR;
1363
59.5k
      gainSetIndex[c] = bsGainSetIndex - 1;
1364
59.5k
      _decodeDuckingModification(hBs, &(pDModForChannel[c]), 0);
1365
1366
59.5k
      c++;
1367
59.5k
      repeatParameters = FDKreadBits(hBs, 1);
1368
59.5k
      if (repeatParameters == 1) {
1369
5.14k
        bsRepeatParametersCount = FDKreadBits(hBs, 5);
1370
5.14k
        bsRepeatParametersCount += 1;
1371
24.4k
        for (i = 0; i < bsRepeatParametersCount; i++) {
1372
20.8k
          if (c >= 8) return DE_MEMORY_ERROR;
1373
19.2k
          gainSetIndex[c] = gainSetIndex[c - 1];
1374
19.2k
          pDModForChannel[c] = pDModForChannel[c - 1];
1375
19.2k
          c++;
1376
19.2k
        }
1377
5.14k
      }
1378
59.5k
    }
1379
28.8k
    if (c > channelCount) {
1380
413
      return DE_NOT_OK;
1381
413
    }
1382
1383
28.4k
    err = deriveDrcChannelGroups(
1384
28.4k
        pInst->drcSetEffect, pInst->drcChannelCount, gainSetIndex,
1385
28.4k
        pDModForChannel, &pInst->nDrcChannelGroups,
1386
28.4k
        pInst->gainSetIndexForChannelGroup, channelGroupForChannel,
1387
28.4k
        duckingModificationForChannelGroup);
1388
28.4k
    if (err) return (err);
1389
1.03M
  } else {
1390
1.03M
    int deriveChannelCount = 0;
1391
1.03M
    if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1392
930k
        (pInst->downmixId[0] != DOWNMIX_ID_BASE_LAYOUT) &&
1393
32.4k
        (pInst->downmixId[0] != DOWNMIX_ID_ANY_DOWNMIX) &&
1394
29.5k
        (pInst->downmixIdCount == 1)) {
1395
17.9k
      if (hUniDrcConfig->downmixInstructionsCount != 0) {
1396
1.69k
        DOWNMIX_INSTRUCTIONS* pDown =
1397
1.69k
            selectDownmixInstructions(hUniDrcConfig, pInst->downmixId[0]);
1398
1.69k
        if (pDown == NULL) return DE_NOT_OK;
1399
1.09k
        pInst->drcChannelCount = channelCount =
1400
1.09k
            pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1401
16.3k
      } else {
1402
16.3k
        deriveChannelCount = 1;
1403
16.3k
        channelCount = 1;
1404
16.3k
      }
1405
1.01M
    } else if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1406
912k
               ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1407
909k
                (pInst->downmixIdCount > 1))) {
1408
      /* Set maximum channel count as upper border. The effective channel count
1409
       * is set at the process function. */
1410
14.8k
      pInst->drcChannelCount = 8;
1411
14.8k
      channelCount = 1;
1412
14.8k
    }
1413
1414
1.03M
    c = 0;
1415
7.16M
    while (c < channelCount) {
1416
6.13M
      int bsGainSetIndex;
1417
6.13M
      bsGainSetIndex = FDKreadBits(hBs, 6);
1418
6.13M
      if (c >= 8) return DE_MEMORY_ERROR;
1419
6.13M
      gainSetIndex[c] = bsGainSetIndex - 1;
1420
6.13M
      c++;
1421
6.13M
      repeatSequenceIndex = FDKreadBits(hBs, 1);
1422
1423
6.13M
      if (repeatSequenceIndex == 1) {
1424
12.7k
        bsRepeatSequenceCount = FDKreadBits(hBs, 5);
1425
12.7k
        bsRepeatSequenceCount += 1;
1426
12.7k
        if (deriveChannelCount) {
1427
4.21k
          channelCount = 1 + bsRepeatSequenceCount;
1428
4.21k
        }
1429
61.1k
        for (i = 0; i < bsRepeatSequenceCount; i++) {
1430
51.0k
          if (c >= 8) return DE_MEMORY_ERROR;
1431
48.4k
          gainSetIndex[c] = bsGainSetIndex - 1;
1432
48.4k
          c++;
1433
48.4k
        }
1434
12.7k
      }
1435
6.13M
    }
1436
1.03M
    if (c > channelCount) {
1437
424
      return DE_NOT_OK;
1438
424
    }
1439
1.03M
    if (deriveChannelCount) {
1440
14.7k
      pInst->drcChannelCount = channelCount;
1441
14.7k
    }
1442
1443
    /* DOWNMIX_ID_ANY_DOWNMIX: channelCount is 1. Distribute gainSetIndex to all
1444
     * channels. */
1445
1.03M
    if ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1446
1.02M
        (pInst->downmixIdCount > 1)) {
1447
117k
      for (c = 1; c < pInst->drcChannelCount; c++) {
1448
102k
        gainSetIndex[c] = gainSetIndex[0];
1449
102k
      }
1450
15.3k
    }
1451
1452
1.03M
    err = deriveDrcChannelGroups(pInst->drcSetEffect, pInst->drcChannelCount,
1453
1.03M
                                 gainSetIndex, NULL, &pInst->nDrcChannelGroups,
1454
1.03M
                                 pInst->gainSetIndexForChannelGroup,
1455
1.03M
                                 channelGroupForChannel, NULL);
1456
1.03M
    if (err) return (err);
1457
1458
1.09M
    for (g = 0; g < pInst->nDrcChannelGroups; g++) {
1459
66.4k
      int set, bandCount;
1460
66.4k
      set = pInst->gainSetIndexForChannelGroup[g];
1461
1462
      /* get bandCount */
1463
66.4k
      if (pCoef != NULL && set < pCoef->gainSetCount) {
1464
2.35k
        bandCount = pCoef->gainSet[set].bandCount;
1465
64.0k
      } else {
1466
64.0k
        bandCount = 1;
1467
64.0k
      }
1468
1469
66.4k
      _decodeGainModification(hBs, version, bandCount,
1470
66.4k
                              pInst->gainModificationForChannelGroup[g], 0);
1471
66.4k
    }
1472
1.03M
  }
1473
1474
1.05M
  return err;
1475
1.06M
}
1476
1477
static DRC_ERROR _readChannelLayout(HANDLE_FDK_BITSTREAM hBs,
1478
432k
                                    CHANNEL_LAYOUT* pChan) {
1479
432k
  DRC_ERROR err = DE_OK;
1480
1481
432k
  pChan->baseChannelCount = FDKreadBits(hBs, 7);
1482
1483
432k
  if (pChan->baseChannelCount > 8) return DE_NOT_OK;
1484
1485
417k
  pChan->layoutSignalingPresent = FDKreadBits(hBs, 1);
1486
1487
417k
  if (pChan->layoutSignalingPresent) {
1488
87.8k
    pChan->definedLayout = FDKreadBits(hBs, 8);
1489
1490
87.8k
    if (pChan->definedLayout == 0) {
1491
1.54k
      int i;
1492
8.41k
      for (i = 0; i < pChan->baseChannelCount; i++) {
1493
6.87k
        if (i < 8) {
1494
6.87k
          pChan->speakerPosition[i] = FDKreadBits(hBs, 7);
1495
6.87k
        } else {
1496
0
          FDKpushFor(hBs, 7);
1497
0
        }
1498
6.87k
      }
1499
1.54k
    }
1500
87.8k
  }
1501
417k
  return err;
1502
432k
}
1503
1504
static DRC_ERROR _readDownmixInstructions(HANDLE_FDK_BITSTREAM hBs,
1505
                                          const int version,
1506
                                          CHANNEL_LAYOUT* pChan,
1507
1.87M
                                          DOWNMIX_INSTRUCTIONS* pDown) {
1508
1.87M
  DRC_ERROR err = DE_OK;
1509
1510
1.87M
  pDown->downmixId = FDKreadBits(hBs, 7);
1511
1.87M
  pDown->targetChannelCount = FDKreadBits(hBs, 7);
1512
1.87M
  pDown->targetLayout = FDKreadBits(hBs, 8);
1513
1.87M
  pDown->downmixCoefficientsPresent = FDKreadBits(hBs, 1);
1514
1515
1.87M
  if (pDown->downmixCoefficientsPresent) {
1516
157k
    int nDownmixCoeffs = pDown->targetChannelCount * pChan->baseChannelCount;
1517
157k
    int i;
1518
157k
    if (nDownmixCoeffs > 8 * 8) return DE_NOT_OK;
1519
155k
    if (version == 0) {
1520
129k
      pDown->bsDownmixOffset = 0;
1521
156k
      for (i = 0; i < nDownmixCoeffs; i++) {
1522
        /* LFE downmix coefficients are not supported. */
1523
27.1k
        pDown->downmixCoefficient[i] = downmixCoeff[FDKreadBits(hBs, 4)];
1524
27.1k
      }
1525
129k
    } else {
1526
26.5k
      pDown->bsDownmixOffset = FDKreadBits(hBs, 4);
1527
140k
      for (i = 0; i < nDownmixCoeffs; i++) {
1528
114k
        pDown->downmixCoefficient[i] = downmixCoeffV1[FDKreadBits(hBs, 5)];
1529
114k
      }
1530
26.5k
    }
1531
155k
  }
1532
1.87M
  return err;
1533
1.87M
}
1534
1535
static DRC_ERROR _readDrcExtensionV1(HANDLE_FDK_BITSTREAM hBs,
1536
59.0k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1537
59.0k
  DRC_ERROR err = DE_OK;
1538
59.0k
  int downmixInstructionsV1Present;
1539
59.0k
  int drcCoeffsAndInstructionsUniDrcV1Present;
1540
59.0k
  int loudEqInstructionsPresent, loudEqInstructionsCount;
1541
59.0k
  int eqPresent, eqInstructionsCount;
1542
59.0k
  int i, offset;
1543
59.0k
  int diff = hUniDrcConfig->diff;
1544
1545
59.0k
  downmixInstructionsV1Present = FDKreadBits(hBs, 1);
1546
59.0k
  if (downmixInstructionsV1Present == 1) {
1547
15.8k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1,
1548
15.8k
                        FDKreadBits(hBs, 7));
1549
15.8k
    offset = hUniDrcConfig->downmixInstructionsCountV0;
1550
15.8k
    hUniDrcConfig->downmixInstructionsCount = fMin(
1551
15.8k
        (UCHAR)(offset + hUniDrcConfig->downmixInstructionsCountV1), (UCHAR)6);
1552
210k
    for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV1; i++) {
1553
196k
      DOWNMIX_INSTRUCTIONS tmpDown;
1554
196k
      FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1555
196k
      err = _readDownmixInstructions(hBs, 1, &hUniDrcConfig->channelLayout,
1556
196k
                                     &tmpDown);
1557
196k
      if (err) return err;
1558
194k
      if ((offset + i) >= 6) continue;
1559
34.9k
      if (!diff)
1560
432
        diff |= (FDKmemcmp(&tmpDown,
1561
432
                           &(hUniDrcConfig->downmixInstructions[offset + i]),
1562
432
                           sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1563
34.9k
      hUniDrcConfig->downmixInstructions[offset + i] = tmpDown;
1564
34.9k
    }
1565
43.2k
  } else {
1566
43.2k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1, 0);
1567
43.2k
  }
1568
1569
57.7k
  drcCoeffsAndInstructionsUniDrcV1Present = FDKreadBits(hBs, 1);
1570
57.7k
  if (drcCoeffsAndInstructionsUniDrcV1Present == 1) {
1571
27.1k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1,
1572
27.1k
                        FDKreadBits(hBs, 3));
1573
27.1k
    offset = hUniDrcConfig->drcCoefficientsUniDrcCountV0;
1574
27.1k
    hUniDrcConfig->drcCoefficientsUniDrcCount =
1575
27.1k
        fMin((UCHAR)(offset + hUniDrcConfig->drcCoefficientsUniDrcCountV1),
1576
27.1k
             (UCHAR)2);
1577
79.7k
    for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV1; i++) {
1578
56.0k
      DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1579
56.0k
      FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1580
56.0k
      err = _readDrcCoefficientsUniDrc(hBs, 1, &tmpCoef);
1581
56.0k
      if (err) return err;
1582
52.5k
      if ((offset + i) >= 2) continue;
1583
18.9k
      if (!diff)
1584
540
        diff |= (FDKmemcmp(&tmpCoef,
1585
540
                           &(hUniDrcConfig->drcCoefficientsUniDrc[offset + i]),
1586
540
                           sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1587
18.9k
      hUniDrcConfig->drcCoefficientsUniDrc[offset + i] = tmpCoef;
1588
18.9k
    }
1589
1590
23.6k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1,
1591
23.6k
                        FDKreadBits(hBs, 6));
1592
23.6k
    offset = hUniDrcConfig->drcInstructionsUniDrcCount;
1593
23.6k
    hUniDrcConfig->drcInstructionsUniDrcCount =
1594
23.6k
        fMin((UCHAR)(offset + hUniDrcConfig->drcInstructionsUniDrcCountV1),
1595
23.6k
             (UCHAR)12);
1596
132k
    for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCount; i++) {
1597
112k
      DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1598
112k
      FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1599
112k
      err = _readDrcInstructionsUniDrc(hBs, 1, hUniDrcConfig, &tmpInst);
1600
112k
      if (err) return err;
1601
108k
      if ((offset + i) >= 12) continue;
1602
58.7k
      if (!diff)
1603
750
        diff |= (FDKmemcmp(&tmpInst,
1604
750
                           &(hUniDrcConfig->drcInstructionsUniDrc[offset + i]),
1605
750
                           sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1606
58.7k
      hUniDrcConfig->drcInstructionsUniDrc[offset + i] = tmpInst;
1607
58.7k
    }
1608
30.5k
  } else {
1609
30.5k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1, 0);
1610
30.5k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1, 0);
1611
30.5k
  }
1612
1613
50.9k
  loudEqInstructionsPresent = FDKreadBits(hBs, 1);
1614
50.9k
  if (loudEqInstructionsPresent == 1) {
1615
13.8k
    loudEqInstructionsCount = FDKreadBits(hBs, 4);
1616
72.2k
    for (i = 0; i < loudEqInstructionsCount; i++) {
1617
58.3k
      _skipLoudEqInstructions(hBs);
1618
58.3k
    }
1619
13.8k
  }
1620
1621
50.9k
  eqPresent = FDKreadBits(hBs, 1);
1622
50.9k
  if (eqPresent == 1) {
1623
21.5k
    _skipEqCoefficients(hBs);
1624
21.5k
    eqInstructionsCount = FDKreadBits(hBs, 4);
1625
120k
    for (i = 0; i < eqInstructionsCount; i++) {
1626
99.1k
      _skipEqInstructions(hBs, hUniDrcConfig);
1627
99.1k
    }
1628
21.5k
  }
1629
1630
50.9k
  hUniDrcConfig->diff = diff;
1631
1632
50.9k
  return err;
1633
57.7k
}
1634
1635
static DRC_ERROR _readUniDrcConfigExtension(
1636
61.5k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1637
61.5k
  DRC_ERROR err = DE_OK;
1638
61.5k
  int k, bitSizeLen, extSizeBits, bitSize;
1639
61.5k
  INT nBitsRemaining;
1640
61.5k
  UNI_DRC_CONFIG_EXTENSION* pExt = &(hUniDrcConfig->uniDrcConfigExt);
1641
1642
61.5k
  k = 0;
1643
61.5k
  pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1644
99.4k
  while (pExt->uniDrcConfigExtType[k] != UNIDRCCONFEXT_TERM) {
1645
93.6k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1646
92.1k
    bitSizeLen = FDKreadBits(hBs, 4);
1647
92.1k
    extSizeBits = bitSizeLen + 4;
1648
1649
92.1k
    bitSize = FDKreadBits(hBs, extSizeBits);
1650
92.1k
    pExt->extBitSize[k] = bitSize + 1;
1651
92.1k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1652
1653
92.1k
    switch (pExt->uniDrcConfigExtType[k]) {
1654
59.0k
      case UNIDRCCONFEXT_V1:
1655
59.0k
        err = _readDrcExtensionV1(hBs, hUniDrcConfig);
1656
59.0k
        if (err) return err;
1657
50.9k
        if (nBitsRemaining !=
1658
50.9k
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1659
46.3k
          return DE_NOT_OK;
1660
4.63k
        break;
1661
4.70k
      case UNIDRCCONFEXT_PARAM_DRC:
1662
      /* add future extensions here */
1663
33.1k
      default:
1664
33.1k
        FDKpushFor(hBs, pExt->extBitSize[k]);
1665
33.1k
        break;
1666
92.1k
    }
1667
37.8k
    k++;
1668
37.8k
    pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1669
37.8k
  }
1670
1671
5.73k
  return err;
1672
61.5k
}
1673
1674
DRC_ERROR
1675
drcDec_readUniDrcConfig(HANDLE_FDK_BITSTREAM hBs,
1676
432k
                        HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1677
432k
  DRC_ERROR err = DE_OK;
1678
432k
  int i, diff = 0;
1679
432k
  int drcDescriptionBasicPresent, drcCoefficientsBasicCount,
1680
432k
      drcInstructionsBasicCount;
1681
432k
  CHANNEL_LAYOUT tmpChan;
1682
432k
  FDKmemclear(&tmpChan, sizeof(CHANNEL_LAYOUT));
1683
432k
  if (hUniDrcConfig == NULL) return DE_NOT_OK;
1684
1685
432k
  diff |= _compAssign(&hUniDrcConfig->sampleRatePresent, FDKreadBits(hBs, 1));
1686
1687
432k
  if (hUniDrcConfig->sampleRatePresent == 1) {
1688
7.62k
    diff |=
1689
7.62k
        _compAssign(&hUniDrcConfig->sampleRate, FDKreadBits(hBs, 18) + 1000);
1690
7.62k
  }
1691
1692
432k
  diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV0,
1693
432k
                      FDKreadBits(hBs, 7));
1694
1695
432k
  drcDescriptionBasicPresent = FDKreadBits(hBs, 1);
1696
432k
  if (drcDescriptionBasicPresent == 1) {
1697
13.4k
    drcCoefficientsBasicCount = FDKreadBits(hBs, 3);
1698
13.4k
    drcInstructionsBasicCount = FDKreadBits(hBs, 4);
1699
419k
  } else {
1700
419k
    drcCoefficientsBasicCount = 0;
1701
419k
    drcInstructionsBasicCount = 0;
1702
419k
  }
1703
1704
432k
  diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV0,
1705
432k
                      FDKreadBits(hBs, 3));
1706
432k
  diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV0,
1707
432k
                      FDKreadBits(hBs, 6));
1708
1709
432k
  err = _readChannelLayout(hBs, &tmpChan);
1710
432k
  if (err) return err;
1711
1712
417k
  if (!diff)
1713
375k
    diff |= (FDKmemcmp(&tmpChan, &hUniDrcConfig->channelLayout,
1714
375k
                       sizeof(CHANNEL_LAYOUT)) != 0);
1715
417k
  hUniDrcConfig->channelLayout = tmpChan;
1716
1717
417k
  hUniDrcConfig->downmixInstructionsCount =
1718
417k
      fMin(hUniDrcConfig->downmixInstructionsCountV0, (UCHAR)6);
1719
2.09M
  for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV0; i++) {
1720
1.67M
    DOWNMIX_INSTRUCTIONS tmpDown;
1721
1.67M
    FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1722
1.67M
    err = _readDownmixInstructions(hBs, 0, &hUniDrcConfig->channelLayout,
1723
1.67M
                                   &tmpDown);
1724
1.67M
    if (err) return err;
1725
1.67M
    if (i >= 6) continue;
1726
153k
    if (!diff)
1727
43.1k
      diff |= (FDKmemcmp(&tmpDown, &(hUniDrcConfig->downmixInstructions[i]),
1728
43.1k
                         sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1729
153k
    hUniDrcConfig->downmixInstructions[i] = tmpDown;
1730
153k
  }
1731
1732
439k
  for (i = 0; i < drcCoefficientsBasicCount; i++) {
1733
22.6k
    _skipDrcCoefficientsBasic(hBs);
1734
22.6k
  }
1735
486k
  for (i = 0; i < drcInstructionsBasicCount; i++) {
1736
69.2k
    _skipDrcInstructionsBasic(hBs);
1737
69.2k
  }
1738
1739
416k
  hUniDrcConfig->drcCoefficientsUniDrcCount =
1740
416k
      fMin(hUniDrcConfig->drcCoefficientsUniDrcCountV0, (UCHAR)2);
1741
510k
  for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV0; i++) {
1742
96.3k
    DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1743
96.3k
    FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1744
96.3k
    err = _readDrcCoefficientsUniDrc(hBs, 0, &tmpCoef);
1745
96.3k
    if (err) return err;
1746
93.3k
    if (i >= 2) continue;
1747
55.6k
    if (!diff)
1748
14.2k
      diff |= (FDKmemcmp(&tmpCoef, &(hUniDrcConfig->drcCoefficientsUniDrc[i]),
1749
14.2k
                         sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1750
55.6k
    hUniDrcConfig->drcCoefficientsUniDrc[i] = tmpCoef;
1751
55.6k
  }
1752
1753
413k
  hUniDrcConfig->drcInstructionsUniDrcCount =
1754
413k
      fMin(hUniDrcConfig->drcInstructionsUniDrcCountV0, (UCHAR)12);
1755
1.35M
  for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCountV0; i++) {
1756
952k
    DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1757
952k
    FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1758
952k
    err = _readDrcInstructionsUniDrc(hBs, 0, hUniDrcConfig, &tmpInst);
1759
952k
    if (err) return err;
1760
945k
    if (i >= 12) continue;
1761
296k
    if (!diff)
1762
95.3k
      diff |= (FDKmemcmp(&tmpInst, &(hUniDrcConfig->drcInstructionsUniDrc[i]),
1763
95.3k
                         sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1764
296k
    hUniDrcConfig->drcInstructionsUniDrc[i] = tmpInst;
1765
296k
  }
1766
1767
406k
  diff |=
1768
406k
      _compAssign(&hUniDrcConfig->uniDrcConfigExtPresent, FDKreadBits(hBs, 1));
1769
406k
  hUniDrcConfig->diff = diff;
1770
1771
406k
  if (hUniDrcConfig->uniDrcConfigExtPresent == 1) {
1772
61.5k
    err = _readUniDrcConfigExtension(hBs, hUniDrcConfig);
1773
61.5k
    if (err) return err;
1774
61.5k
  }
1775
1776
350k
  return err;
1777
406k
}
1778
1779
/*******************/
1780
/* loudnessInfoSet */
1781
/*******************/
1782
1783
static DRC_ERROR _decodeMethodValue(HANDLE_FDK_BITSTREAM hBs,
1784
                                    const UCHAR methodDefinition,
1785
116k
                                    FIXP_DBL* methodValue, INT isBox) {
1786
116k
  int tmp;
1787
116k
  FIXP_DBL val;
1788
116k
  switch (methodDefinition) {
1789
29.1k
    case MD_UNKNOWN_OTHER:
1790
39.8k
    case MD_PROGRAM_LOUDNESS:
1791
44.3k
    case MD_ANCHOR_LOUDNESS:
1792
45.6k
    case MD_MAX_OF_LOUDNESS_RANGE:
1793
56.8k
    case MD_MOMENTARY_LOUDNESS_MAX:
1794
64.6k
    case MD_SHORT_TERM_LOUDNESS_MAX:
1795
64.6k
      tmp = FDKreadBits(hBs, 8);
1796
64.6k
      val = FL2FXCONST_DBL(-57.75f / (float)(1 << 7)) +
1797
64.6k
            (FIXP_DBL)(
1798
64.6k
                tmp << (DFRACT_BITS - 1 - 2 - 7)); /* -57.75 + tmp * 0.25; */
1799
64.6k
      break;
1800
8.09k
    case MD_LOUDNESS_RANGE:
1801
8.09k
      tmp = FDKreadBits(hBs, 8);
1802
8.09k
      if (tmp == 0)
1803
2.28k
        val = (FIXP_DBL)0;
1804
5.81k
      else if (tmp <= 128)
1805
4.70k
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 2 - 7)); /* tmp * 0.25; */
1806
1.11k
      else if (tmp <= 204) {
1807
397
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 1 - 7)) -
1808
397
              FL2FXCONST_DBL(32.0f / (float)(1 << 7)); /* 0.5 * tmp - 32.0f; */
1809
717
      } else {
1810
        /* downscale by 1 more bit to prevent overflow at intermediate result */
1811
717
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 8)) -
1812
717
              FL2FXCONST_DBL(134.0f / (float)(1 << 8)); /* tmp - 134.0; */
1813
717
        val <<= 1;
1814
717
      }
1815
8.09k
      break;
1816
9.99k
    case MD_MIXING_LEVEL:
1817
9.99k
      tmp = FDKreadBits(hBs, isBox ? 8 : 5);
1818
9.99k
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)) +
1819
9.99k
            FL2FXCONST_DBL(80.0f / (float)(1 << 7)); /* tmp + 80.0; */
1820
9.99k
      break;
1821
8.55k
    case MD_ROOM_TYPE:
1822
8.55k
      tmp = FDKreadBits(hBs, isBox ? 8 : 2);
1823
8.55k
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)); /* tmp; */
1824
8.55k
      break;
1825
3.84k
    case MD_SHORT_TERM_LOUDNESS:
1826
3.84k
      tmp = FDKreadBits(hBs, 8);
1827
3.84k
      val = FL2FXCONST_DBL(-116.0f / (float)(1 << 7)) +
1828
3.84k
            (FIXP_DBL)(
1829
3.84k
                tmp << (DFRACT_BITS - 1 - 1 - 7)); /* -116.0 + tmp * 0.5; */
1830
3.84k
      break;
1831
21.3k
    default:
1832
21.3k
      return DE_NOT_OK; /* invalid methodDefinition value */
1833
116k
  }
1834
95.1k
  *methodValue = val;
1835
95.1k
  return DE_OK;
1836
116k
}
1837
1838
static DRC_ERROR _readLoudnessMeasurement(HANDLE_FDK_BITSTREAM hBs,
1839
116k
                                          LOUDNESS_MEASUREMENT* pMeas) {
1840
116k
  DRC_ERROR err = DE_OK;
1841
1842
116k
  pMeas->methodDefinition = FDKreadBits(hBs, 4);
1843
116k
  err =
1844
116k
      _decodeMethodValue(hBs, pMeas->methodDefinition, &pMeas->methodValue, 0);
1845
116k
  if (err) return err;
1846
95.1k
  pMeas->measurementSystem = FDKreadBits(hBs, 4);
1847
95.1k
  pMeas->reliability = FDKreadBits(hBs, 2);
1848
1849
95.1k
  return err;
1850
116k
}
1851
1852
static DRC_ERROR _readLoudnessInfo(HANDLE_FDK_BITSTREAM hBs, const int version,
1853
125k
                                   LOUDNESS_INFO* loudnessInfo) {
1854
125k
  DRC_ERROR err = DE_OK;
1855
125k
  int bsSamplePeakLevel, bsTruePeakLevel, i;
1856
125k
  int measurementCount;
1857
1858
125k
  loudnessInfo->drcSetId = FDKreadBits(hBs, 6);
1859
125k
  if (version >= 1) {
1860
17.9k
    loudnessInfo->eqSetId = FDKreadBits(hBs, 6);
1861
107k
  } else {
1862
107k
    loudnessInfo->eqSetId = 0;
1863
107k
  }
1864
125k
  loudnessInfo->downmixId = FDKreadBits(hBs, 7);
1865
1866
125k
  loudnessInfo->samplePeakLevelPresent = FDKreadBits(hBs, 1);
1867
125k
  if (loudnessInfo->samplePeakLevelPresent) {
1868
24.2k
    bsSamplePeakLevel = FDKreadBits(hBs, 12);
1869
24.2k
    if (bsSamplePeakLevel == 0) {
1870
565
      loudnessInfo->samplePeakLevelPresent = 0;
1871
565
      loudnessInfo->samplePeakLevel = (FIXP_DBL)0;
1872
23.7k
    } else { /* 20.0 - bsSamplePeakLevel * 0.03125; */
1873
23.7k
      loudnessInfo->samplePeakLevel =
1874
23.7k
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1875
23.7k
          (FIXP_DBL)(bsSamplePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1876
23.7k
    }
1877
24.2k
  }
1878
1879
125k
  loudnessInfo->truePeakLevelPresent = FDKreadBits(hBs, 1);
1880
125k
  if (loudnessInfo->truePeakLevelPresent) {
1881
22.9k
    bsTruePeakLevel = FDKreadBits(hBs, 12);
1882
22.9k
    if (bsTruePeakLevel == 0) {
1883
1.12k
      loudnessInfo->truePeakLevelPresent = 0;
1884
1.12k
      loudnessInfo->truePeakLevel = (FIXP_DBL)0;
1885
21.8k
    } else {
1886
21.8k
      loudnessInfo->truePeakLevel =
1887
21.8k
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1888
21.8k
          (FIXP_DBL)(bsTruePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1889
21.8k
    }
1890
22.9k
    loudnessInfo->truePeakLevelMeasurementSystem = FDKreadBits(hBs, 4);
1891
22.9k
    loudnessInfo->truePeakLevelReliability = FDKreadBits(hBs, 2);
1892
22.9k
  }
1893
1894
125k
  measurementCount = FDKreadBits(hBs, 4);
1895
125k
  loudnessInfo->measurementCount = fMin(measurementCount, 8);
1896
220k
  for (i = 0; i < measurementCount; i++) {
1897
116k
    LOUDNESS_MEASUREMENT tmpMeas;
1898
116k
    FDKmemclear(&tmpMeas, sizeof(LOUDNESS_MEASUREMENT));
1899
116k
    err = _readLoudnessMeasurement(hBs, &tmpMeas);
1900
116k
    if (err) return err;
1901
95.1k
    if (i >= 8) continue;
1902
92.8k
    loudnessInfo->loudnessMeasurement[i] = tmpMeas;
1903
92.8k
  }
1904
1905
104k
  return err;
1906
125k
}
1907
1908
static DRC_ERROR _readLoudnessInfoSetExtEq(
1909
4.62k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1910
4.62k
  DRC_ERROR err = DE_OK;
1911
4.62k
  int i, offset;
1912
4.62k
  int diff = hLoudnessInfoSet->diff;
1913
1914
4.62k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV1,
1915
4.62k
                      FDKreadBits(hBs, 6));
1916
4.62k
  diff |=
1917
4.62k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV1, FDKreadBits(hBs, 6));
1918
1919
4.62k
  offset = hLoudnessInfoSet->loudnessInfoAlbumCountV0;
1920
4.62k
  hLoudnessInfoSet->loudnessInfoAlbumCount = fMin(
1921
4.62k
      (UCHAR)(offset + hLoudnessInfoSet->loudnessInfoAlbumCountV1), (UCHAR)12);
1922
13.8k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV1; i++) {
1923
10.7k
    LOUDNESS_INFO tmpLoud;
1924
10.7k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1925
10.7k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1926
10.7k
    if (err) return err;
1927
9.20k
    if ((offset + i) >= 12) continue;
1928
4.00k
    if (!diff)
1929
447
      diff |= (FDKmemcmp(&tmpLoud,
1930
447
                         &(hLoudnessInfoSet->loudnessInfoAlbum[offset + i]),
1931
447
                         sizeof(LOUDNESS_INFO)) != 0);
1932
4.00k
    hLoudnessInfoSet->loudnessInfoAlbum[offset + i] = tmpLoud;
1933
4.00k
  }
1934
1935
3.12k
  offset = hLoudnessInfoSet->loudnessInfoCountV0;
1936
3.12k
  hLoudnessInfoSet->loudnessInfoCount =
1937
3.12k
      fMin((UCHAR)(offset + hLoudnessInfoSet->loudnessInfoCountV1), (UCHAR)12);
1938
9.55k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV1; i++) {
1939
7.20k
    LOUDNESS_INFO tmpLoud;
1940
7.20k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1941
7.20k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1942
7.20k
    if (err) return err;
1943
6.43k
    if ((offset + i) >= 12) continue;
1944
2.95k
    if (!diff)
1945
547
      diff |=
1946
547
          (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[offset + i]),
1947
547
                     sizeof(LOUDNESS_INFO)) != 0);
1948
2.95k
    hLoudnessInfoSet->loudnessInfo[offset + i] = tmpLoud;
1949
2.95k
  }
1950
2.34k
  hLoudnessInfoSet->diff = diff;
1951
2.34k
  return err;
1952
3.12k
}
1953
1954
static DRC_ERROR _readLoudnessInfoSetExtension(
1955
6.35k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1956
6.35k
  DRC_ERROR err = DE_OK;
1957
6.35k
  int k, bitSizeLen, extSizeBits, bitSize;
1958
6.35k
  INT nBitsRemaining;
1959
6.35k
  LOUDNESS_INFO_SET_EXTENSION* pExt = &(hLoudnessInfoSet->loudnessInfoSetExt);
1960
1961
6.35k
  k = 0;
1962
6.35k
  pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1963
12.1k
  while (pExt->loudnessInfoSetExtType[k] != UNIDRCLOUDEXT_TERM) {
1964
9.14k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1965
8.84k
    bitSizeLen = FDKreadBits(hBs, 4);
1966
8.84k
    extSizeBits = bitSizeLen + 4;
1967
1968
8.84k
    bitSize = FDKreadBits(hBs, extSizeBits);
1969
8.84k
    pExt->extBitSize[k] = bitSize + 1;
1970
8.84k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1971
1972
8.84k
    switch (pExt->loudnessInfoSetExtType[k]) {
1973
4.62k
      case UNIDRCLOUDEXT_EQ:
1974
4.62k
        err = _readLoudnessInfoSetExtEq(hBs, hLoudnessInfoSet);
1975
4.62k
        if (err) return err;
1976
2.34k
        if (nBitsRemaining !=
1977
2.34k
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1978
761
          return DE_NOT_OK;
1979
1.58k
        break;
1980
      /* add future extensions here */
1981
4.21k
      default:
1982
4.21k
        FDKpushFor(hBs, pExt->extBitSize[k]);
1983
4.21k
        break;
1984
8.84k
    }
1985
5.80k
    k++;
1986
5.80k
    pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1987
5.80k
  }
1988
1989
3.01k
  return err;
1990
6.35k
}
1991
1992
/* Parser for loundessInfoSet() */
1993
DRC_ERROR
1994
drcDec_readLoudnessInfoSet(HANDLE_FDK_BITSTREAM hBs,
1995
258k
                           HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1996
258k
  DRC_ERROR err = DE_OK;
1997
258k
  int i, diff = 0;
1998
258k
  if (hLoudnessInfoSet == NULL) return DE_NOT_OK;
1999
2000
258k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV0,
2001
258k
                      FDKreadBits(hBs, 6));
2002
258k
  diff |=
2003
258k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV0, FDKreadBits(hBs, 6));
2004
2005
258k
  hLoudnessInfoSet->loudnessInfoAlbumCount =
2006
258k
      fMin(hLoudnessInfoSet->loudnessInfoAlbumCountV0, (UCHAR)12);
2007
318k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV0; i++) {
2008
77.2k
    LOUDNESS_INFO tmpLoud;
2009
77.2k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2010
77.2k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2011
77.2k
    if (err) return err;
2012
59.4k
    if (i >= 12) continue;
2013
37.8k
    if (!diff)
2014
1.09k
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfoAlbum[i]),
2015
1.09k
                         sizeof(LOUDNESS_INFO)) != 0);
2016
37.8k
    hLoudnessInfoSet->loudnessInfoAlbum[i] = tmpLoud;
2017
37.8k
  }
2018
2019
240k
  hLoudnessInfoSet->loudnessInfoCount =
2020
240k
      fMin(hLoudnessInfoSet->loudnessInfoCountV0, (UCHAR)12);
2021
270k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV0; i++) {
2022
30.3k
    LOUDNESS_INFO tmpLoud;
2023
30.3k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2024
30.3k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2025
30.3k
    if (err) return err;
2026
29.1k
    if (i >= 12) continue;
2027
20.6k
    if (!diff)
2028
4.21k
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[i]),
2029
4.21k
                         sizeof(LOUDNESS_INFO)) != 0);
2030
20.6k
    hLoudnessInfoSet->loudnessInfo[i] = tmpLoud;
2031
20.6k
  }
2032
2033
239k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoSetExtPresent,
2034
239k
                      FDKreadBits(hBs, 1));
2035
239k
  hLoudnessInfoSet->diff = diff;
2036
2037
239k
  if (hLoudnessInfoSet->loudnessInfoSetExtPresent) {
2038
6.35k
    err = _readLoudnessInfoSetExtension(hBs, hLoudnessInfoSet);
2039
6.35k
    if (err) return err;
2040
6.35k
  }
2041
2042
236k
  return err;
2043
239k
}