Coverage Report

Created: 2026-04-01 07:00

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