Coverage Report

Created: 2026-09-14 08:00

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