Coverage Report

Created: 2026-07-25 07:52

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