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