Coverage Report

Created: 2026-01-10 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aac/libDRCdec/src/drcDec_reader.cpp
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4
© Copyright  1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten
5
Forschung e.V. All rights reserved.
6
7
 1.    INTRODUCTION
8
The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9
that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10
scheme for digital audio. This FDK AAC Codec software is intended to be used on
11
a wide variety of Android devices.
12
13
AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14
general perceptual audio codecs. AAC-ELD is considered the best-performing
15
full-bandwidth communications codec by independent studies and is widely
16
deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17
specifications.
18
19
Patent licenses for necessary patent claims for the FDK AAC Codec (including
20
those of Fraunhofer) may be obtained through Via Licensing
21
(www.vialicensing.com) or through the respective patent owners individually for
22
the purpose of encoding or decoding bit streams in products that are compliant
23
with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24
Android devices already license these patent claims through Via Licensing or
25
directly from the patent owners, and therefore FDK AAC Codec software may
26
already be covered under those patent licenses when it is used for those
27
licensed purposes only.
28
29
Commercially-licensed AAC software libraries, including floating-point versions
30
with enhanced sound quality, are also available from Fraunhofer. Users are
31
encouraged to check the Fraunhofer website for additional applications
32
information and documentation.
33
34
2.    COPYRIGHT LICENSE
35
36
Redistribution and use in source and binary forms, with or without modification,
37
are permitted without payment of copyright license fees provided that you
38
satisfy the following conditions:
39
40
You must retain the complete text of this software license in redistributions of
41
the FDK AAC Codec or your modifications thereto in source code form.
42
43
You must retain the complete text of this software license in the documentation
44
and/or other materials provided with redistributions of the FDK AAC Codec or
45
your modifications thereto in binary form. You must make available free of
46
charge copies of the complete source code of the FDK AAC Codec and your
47
modifications thereto to recipients of copies in binary form.
48
49
The name of Fraunhofer may not be used to endorse or promote products derived
50
from this library without prior written permission.
51
52
You may not charge copyright license fees for anyone to use, copy or distribute
53
the FDK AAC Codec software or your modifications thereto.
54
55
Your modified versions of the FDK AAC Codec must carry prominent notices stating
56
that you changed the software and the date of any change. For modified versions
57
of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58
must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59
AAC Codec Library for Android."
60
61
3.    NO PATENT LICENSE
62
63
NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64
limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65
Fraunhofer provides no warranty of patent non-infringement with respect to this
66
software.
67
68
You may use this FDK AAC Codec software or modifications thereto only for
69
purposes that are authorized by appropriate patent licenses.
70
71
4.    DISCLAIMER
72
73
This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74
holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75
including but not limited to the implied warranties of merchantability and
76
fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77
CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78
or consequential damages, including but not limited to procurement of substitute
79
goods or services; loss of use, data, or profits, or business interruption,
80
however caused and on any theory of liability, whether in contract, strict
81
liability, or tort (including negligence), arising in any way out of the use of
82
this software, even if advised of the possibility of such damage.
83
84
5.    CONTACT INFORMATION
85
86
Fraunhofer Institute for Integrated Circuits IIS
87
Attention: Audio and Multimedia Departments - FDK AAC LL
88
Am Wolfsmantel 33
89
91058 Erlangen, Germany
90
91
www.iis.fraunhofer.de/amm
92
amm-info@iis.fraunhofer.de
93
----------------------------------------------------------------------------- */
94
95
/************************* MPEG-D DRC decoder library **************************
96
97
   Author(s):
98
99
   Description:
100
101
*******************************************************************************/
102
103
#include "fixpoint_math.h"
104
#include "drcDec_reader.h"
105
#include "drcDec_tools.h"
106
#include "drcDec_rom.h"
107
#include "drcDecoder.h"
108
109
/* MPEG-D DRC AMD 1 */
110
111
934
#define UNIDRCCONFEXT_PARAM_DRC 0x1
112
21.7k
#define UNIDRCCONFEXT_V1 0x2
113
637
#define UNIDRCLOUDEXT_EQ 0x1
114
115
0
#define UNIDRCGAINEXT_TERM 0x0
116
1.89k
#define UNIDRCLOUDEXT_TERM 0x0
117
38.6k
#define UNIDRCCONFEXT_TERM 0x0
118
119
0
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
0
  int Z = 1;
124
0
  while ((1 << Z) < (2 * nNodesMax)) {
125
0
    Z++;
126
0
  }
127
0
  return Z;
128
0
}
129
130
0
static int _getTimeDeltaMin(const GAIN_SET* pGset, const int deltaTminDefault) {
131
0
  if (pGset->timeDeltaMinPresent) {
132
0
    return pGset->timeDeltaMin;
133
0
  } else {
134
0
    return deltaTminDefault;
135
0
  }
136
0
}
137
138
/* compare and assign */
139
242k
static inline int _compAssign(UCHAR* dest, const UCHAR src) {
140
242k
  int diff = 0;
141
242k
  if (*dest != src) diff = 1;
142
242k
  *dest = src;
143
242k
  return diff;
144
242k
}
145
146
1.07k
static inline int _compAssign(ULONG* dest, const ULONG src) {
147
1.07k
  int diff = 0;
148
1.07k
  if (*dest != src) diff = 1;
149
1.07k
  *dest = src;
150
1.07k
  return diff;
151
1.07k
}
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
0
{
158
0
  SCHAR index = 0;
159
0
  int value, bit;
160
161
0
  while (index >= 0) {
162
0
    bit = FDKreadBits(hBs, 1);
163
0
    index = h[index][bit];
164
0
  }
165
166
0
  value = index + 64; /* Add offset */
167
168
0
  return value;
169
0
}
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
0
    HANDLE_FDK_BITSTREAM hBs, const GAIN_CODING_PROFILE gainCodingProfile) {
214
0
  int sign, magn;
215
0
  FIXP_SGL gainInitial = (FIXP_SGL)0;
216
0
  switch (gainCodingProfile) {
217
0
    case GCP_REGULAR:
218
0
      sign = FDKreadBits(hBs, 1);
219
0
      magn = FDKreadBits(hBs, 8);
220
221
0
      gainInitial =
222
0
          (FIXP_SGL)(magn << (FRACT_BITS - 1 - 3 - 7)); /* magn * 0.125; */
223
0
      if (sign) gainInitial = -gainInitial;
224
0
      break;
225
0
    case GCP_FADING:
226
0
      sign = FDKreadBits(hBs, 1);
227
0
      if (sign == 0)
228
0
        gainInitial = (FIXP_SGL)0;
229
0
      else {
230
0
        magn = FDKreadBits(hBs, 10);
231
0
        gainInitial = -(FIXP_SGL)(
232
0
            (magn + 1) << (FRACT_BITS - 1 - 3 - 7)); /* - (magn + 1) * 0.125; */
233
0
      }
234
0
      break;
235
0
    case GCP_CLIPPING_DUCKING:
236
0
      sign = FDKreadBits(hBs, 1);
237
0
      if (sign == 0)
238
0
        gainInitial = (FIXP_SGL)0;
239
0
      else {
240
0
        magn = FDKreadBits(hBs, 8);
241
0
        gainInitial = -(FIXP_SGL)(
242
0
            (magn + 1) << (FRACT_BITS - 1 - 3 - 7)); /* - (magn + 1) * 0.125; */
243
0
      }
244
0
      break;
245
0
    case GCP_CONSTANT:
246
0
      break;
247
0
  }
248
0
  return gainInitial;
249
0
}
250
251
0
static int _decodeNNodes(HANDLE_FDK_BITSTREAM hBs) {
252
0
  int nNodes = 0, endMarker = 0;
253
254
  /* decode number of nodes */
255
0
  while (endMarker != 1) {
256
0
    nNodes++;
257
0
    if (nNodes >= 128) break;
258
0
    endMarker = FDKreadBits(hBs, 1);
259
0
  }
260
0
  return nNodes;
261
0
}
262
263
static void _decodeGains(HANDLE_FDK_BITSTREAM hBs,
264
                         const GAIN_CODING_PROFILE gainCodingProfile,
265
0
                         const int nNodes, GAIN_NODE* pNodes) {
266
0
  int k, deltaGain;
267
0
  Huffman deltaGainCodebook;
268
269
0
  pNodes[0].gainDb = _decodeGainInitial(hBs, gainCodingProfile);
270
271
0
  if (gainCodingProfile == GCP_CLIPPING_DUCKING) {
272
0
    deltaGainCodebook = (Huffman)&deltaGain_codingProfile_2_huffman;
273
0
  } else {
274
0
    deltaGainCodebook = (Huffman)&deltaGain_codingProfile_0_1_huffman;
275
0
  }
276
277
0
  for (k = 1; k < nNodes; k++) {
278
0
    deltaGain = _decodeHuffmanCW(deltaGainCodebook, hBs);
279
0
    if (k >= 16) continue;
280
    /* gain_dB_e = 7 */
281
0
    pNodes[k].gainDb =
282
0
        pNodes[k - 1].gainDb +
283
0
        (FIXP_SGL)(deltaGain << (FRACT_BITS - 1 - 7 -
284
0
                                 3)); /* pNodes[k-1].gainDb + 0.125*deltaGain */
285
0
  }
286
0
}
287
288
static void _decodeSlopes(HANDLE_FDK_BITSTREAM hBs,
289
                          const GAIN_INTERPOLATION_TYPE gainInterpolationType,
290
0
                          const int nNodes, GAIN_NODE* pNodes) {
291
0
  int k = 0;
292
293
0
  if (gainInterpolationType == GIT_SPLINE) {
294
    /* decode slope steepness */
295
0
    for (k = 0; k < nNodes; k++) {
296
0
      _decodeHuffmanCW((Huffman)&slopeSteepness_huffman, hBs);
297
0
    }
298
0
  }
299
0
}
300
301
0
static int _decodeTimeDelta(HANDLE_FDK_BITSTREAM hBs, const int Z) {
302
0
  int prefix, mu;
303
304
0
  prefix = FDKreadBits(hBs, 2);
305
0
  switch (prefix) {
306
0
    case 0x0:
307
0
      return 1;
308
0
    case 0x1:
309
0
      mu = FDKreadBits(hBs, 2);
310
0
      return mu + 2;
311
0
    case 0x2:
312
0
      mu = FDKreadBits(hBs, 3);
313
0
      return mu + 6;
314
0
    case 0x3:
315
0
      mu = FDKreadBits(hBs, Z);
316
0
      return mu + 14;
317
0
    default:
318
0
      return 0;
319
0
  }
320
0
}
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
0
                         GAIN_NODE* pNodes) {
326
0
  int timeDelta, k;
327
0
  int timeOffs = timeOffset;
328
0
  int frameEndFlag, nodeTimeTmp, nodeResFlag;
329
330
0
  if (fullFrame == 0) {
331
0
    frameEndFlag = FDKreadBits(hBs, 1);
332
0
  } else {
333
0
    frameEndFlag = 1;
334
0
  }
335
336
0
  if (frameEndFlag ==
337
0
      1) { /* frameEndFlag == 1 signals that the last node is at the end of the
338
              DRC frame */
339
0
    nodeResFlag = 0;
340
0
    for (k = 0; k < nNodes - 1; k++) {
341
      /* decode a delta time value */
342
0
      timeDelta = _decodeTimeDelta(hBs, Z);
343
0
      if (k >= (16 - 1)) continue;
344
      /* frameEndFlag == 1 needs special handling for last node with node
345
       * reservoir */
346
0
      nodeTimeTmp = timeOffs + timeDelta * deltaTmin;
347
0
      if (nodeTimeTmp > frameSize + timeOffset) {
348
0
        if (nodeResFlag == 0) {
349
0
          pNodes[k].time = frameSize + timeOffset;
350
0
          nodeResFlag = 1;
351
0
        }
352
0
        pNodes[k + 1].time = nodeTimeTmp;
353
0
      } else {
354
0
        pNodes[k].time = nodeTimeTmp;
355
0
      }
356
0
      timeOffs = nodeTimeTmp;
357
0
    }
358
0
    if (nodeResFlag == 0) {
359
0
      k = fMin(k, 16 - 1);
360
0
      pNodes[k].time = frameSize + timeOffset;
361
0
    }
362
0
  } else {
363
0
    for (k = 0; k < nNodes; k++) {
364
      /* decode a delta time value */
365
0
      timeDelta = _decodeTimeDelta(hBs, Z);
366
0
      if (k >= 16) continue;
367
0
      pNodes[k].time = timeOffs + timeDelta * deltaTmin;
368
0
      timeOffs = pNodes[k].time;
369
0
    }
370
0
  }
371
0
}
372
373
static void _readNodes(HANDLE_FDK_BITSTREAM hBs, GAIN_SET* gainSet,
374
                       const int frameSize, const int timeDeltaMin,
375
0
                       UCHAR* pNNodes, GAIN_NODE* pNodes) {
376
0
  int timeOffset, drcGainCodingMode, nNodes;
377
0
  int Z = _getZ(frameSize / timeDeltaMin);
378
0
  if (gainSet->timeAlignment == 0) {
379
0
    timeOffset = -1;
380
0
  } else {
381
0
    timeOffset = -timeDeltaMin +
382
0
                 (timeDeltaMin - 1) /
383
0
                     2; /* timeOffset = - deltaTmin + floor((deltaTmin-1)/2); */
384
0
  }
385
386
0
  drcGainCodingMode = FDKreadBits(hBs, 1);
387
0
  if (drcGainCodingMode == 0) {
388
    /* "simple" mode: only one node at the end of the frame with slope = 0 */
389
0
    nNodes = 1;
390
0
    pNodes[0].gainDb = _decodeGainInitial(
391
0
        hBs, (GAIN_CODING_PROFILE)gainSet->gainCodingProfile);
392
0
    pNodes[0].time = frameSize + timeOffset;
393
0
  } else {
394
0
    nNodes = _decodeNNodes(hBs);
395
396
0
    _decodeSlopes(hBs, (GAIN_INTERPOLATION_TYPE)gainSet->gainInterpolationType,
397
0
                  nNodes, pNodes);
398
399
0
    _decodeTimes(hBs, timeDeltaMin, frameSize, gainSet->fullFrame, timeOffset,
400
0
                 Z, nNodes, pNodes);
401
402
0
    _decodeGains(hBs, (GAIN_CODING_PROFILE)gainSet->gainCodingProfile, nNodes,
403
0
                 pNodes);
404
0
  }
405
0
  *pNNodes = (UCHAR)nNodes;
406
0
}
407
408
static void _readDrcGainSequence(HANDLE_FDK_BITSTREAM hBs, GAIN_SET* gainSet,
409
                                 const int frameSize, const int timeDeltaMin,
410
0
                                 UCHAR* pNNodes, GAIN_NODE pNodes[16]) {
411
0
  SHORT timeBufPrevFrame[16], timeBufCurFrame[16];
412
0
  int nNodesNodeRes, nNodesCur, k, m;
413
414
0
  if (gainSet->gainCodingProfile == GCP_CONSTANT) {
415
0
    *pNNodes = 1;
416
0
    pNodes[0].time = frameSize - 1;
417
0
    pNodes[0].gainDb = (FIXP_SGL)0;
418
0
  } else {
419
0
    _readNodes(hBs, gainSet, frameSize, timeDeltaMin, pNNodes, pNodes);
420
421
    /* count number of nodes in node reservoir */
422
0
    nNodesNodeRes = 0;
423
0
    nNodesCur = 0;
424
    /* count and buffer nodes from node reservoir */
425
0
    for (k = 0; k < *pNNodes; k++) {
426
0
      if (k >= 16) continue;
427
0
      if (pNodes[k].time >= frameSize) {
428
        /* write node reservoir times into buffer */
429
0
        timeBufPrevFrame[nNodesNodeRes] = pNodes[k].time;
430
0
        nNodesNodeRes++;
431
0
      } else { /* times from current frame */
432
0
        timeBufCurFrame[nNodesCur] = pNodes[k].time;
433
0
        nNodesCur++;
434
0
      }
435
0
    }
436
    /* compose right time order (bit reservoir first) */
437
0
    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
0
      pNodes[k].time = timeBufPrevFrame[k] - 2 * frameSize;
442
0
    }
443
    /* ...and times from current frame */
444
0
    for (m = 0; m < nNodesCur; m++, k++) {
445
0
      pNodes[k].time = timeBufCurFrame[m];
446
0
    }
447
0
  }
448
0
}
449
450
static DRC_ERROR _readUniDrcGainExtension(HANDLE_FDK_BITSTREAM hBs,
451
0
                                          UNI_DRC_GAIN_EXTENSION* pExt) {
452
0
  DRC_ERROR err = DE_OK;
453
0
  int k, bitSizeLen, extSizeBits, bitSize;
454
455
0
  k = 0;
456
0
  pExt->uniDrcGainExtType[k] = FDKreadBits(hBs, 4);
457
0
  while (pExt->uniDrcGainExtType[k] != UNIDRCGAINEXT_TERM) {
458
0
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
459
0
    bitSizeLen = FDKreadBits(hBs, 3);
460
0
    extSizeBits = bitSizeLen + 4;
461
462
0
    bitSize = FDKreadBits(hBs, extSizeBits);
463
0
    pExt->extBitSize[k] = bitSize + 1;
464
465
0
    switch (pExt->uniDrcGainExtType[k]) {
466
      /* add future extensions here */
467
0
      default:
468
0
        FDKpushFor(hBs, pExt->extBitSize[k]);
469
0
        break;
470
0
    }
471
0
    k++;
472
0
    pExt->uniDrcGainExtType[k] = FDKreadBits(hBs, 4);
473
0
  }
474
475
0
  return err;
476
0
}
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
0
                      HANDLE_UNI_DRC_GAIN hUniDrcGain) {
483
0
  DRC_ERROR err = DE_OK;
484
0
  int seq, gainSequenceCount;
485
0
  DRC_COEFFICIENTS_UNI_DRC* pCoef =
486
0
      selectDrcCoefficients(hUniDrcConfig, LOCATION_SELECTED);
487
0
  if (hUniDrcGain == NULL) return DE_NOT_OK;
488
0
  hUniDrcGain->status = 0;
489
0
  if (pCoef) {
490
0
    gainSequenceCount = fMin(pCoef->gainSequenceCount, (UCHAR)12);
491
0
  } else {
492
0
    gainSequenceCount = 0;
493
0
  }
494
495
0
  for (seq = 0; seq < gainSequenceCount; seq++) {
496
0
    UCHAR index = pCoef->gainSetIndexForGainSequence[seq];
497
0
    GAIN_SET* gainSet;
498
0
    int timeDeltaMin;
499
0
    UCHAR tmpNNodes = 0;
500
0
    GAIN_NODE tmpNodes[16];
501
502
0
    if ((index >= pCoef->gainSetCount) || (index >= 12)) return DE_NOT_OK;
503
0
    gainSet = &(pCoef->gainSet[index]);
504
505
0
    timeDeltaMin = _getTimeDeltaMin(gainSet, deltaTminDefault);
506
507
0
    _readDrcGainSequence(hBs, gainSet, frameSize, timeDeltaMin, &tmpNNodes,
508
0
                         tmpNodes);
509
510
0
    hUniDrcGain->nNodes[seq] = tmpNNodes;
511
0
    FDKmemcpy(hUniDrcGain->gainNode[seq], tmpNodes,
512
0
              fMin(tmpNNodes, (UCHAR)16) * sizeof(GAIN_NODE));
513
0
  }
514
515
0
  if (pCoef && (gainSequenceCount ==
516
0
                pCoef->gainSequenceCount)) { /* all sequences have been read */
517
0
    hUniDrcGain->uniDrcGainExtPresent = FDKreadBits(hBs, 1);
518
0
    if (hUniDrcGain->uniDrcGainExtPresent == 1) {
519
0
      err = _readUniDrcGainExtension(hBs, &(hUniDrcGain->uniDrcGainExtension));
520
0
      if (err) return err;
521
0
    }
522
0
  }
523
524
0
  if (err == DE_OK && gainSequenceCount > 0) {
525
0
    hUniDrcGain->status = 1;
526
0
  }
527
0
  return err;
528
0
}
529
530
/****************/
531
/* uniDrcConfig */
532
/****************/
533
534
static void _decodeDuckingModification(HANDLE_FDK_BITSTREAM hBs,
535
14.9k
                                       DUCKING_MODIFICATION* pDMod, int isBox) {
536
14.9k
  int bsDuckingScaling, sigma, mu;
537
538
14.9k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
539
14.9k
  pDMod->duckingScalingPresent = FDKreadBits(hBs, 1);
540
541
14.9k
  if (pDMod->duckingScalingPresent) {
542
3.38k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
543
3.38k
    bsDuckingScaling = FDKreadBits(hBs, 4);
544
3.38k
    sigma = bsDuckingScaling >> 3;
545
3.38k
    mu = bsDuckingScaling & 0x7;
546
547
3.38k
    if (sigma) {
548
1.81k
      pDMod->duckingScaling = (FIXP_SGL)(
549
1.81k
          (7 - mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 - 0.125 * (1 + mu); */
550
1.81k
    } else {
551
1.57k
      pDMod->duckingScaling = (FIXP_SGL)(
552
1.57k
          (9 + mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 + 0.125 * (1 + mu); */
553
1.57k
    }
554
11.5k
  } else {
555
11.5k
    pDMod->duckingScaling = (FIXP_SGL)(1 << (FRACT_BITS - 1 - 2)); /* 1.0 */
556
11.5k
  }
557
14.9k
}
558
559
static void _decodeGainModification(HANDLE_FDK_BITSTREAM hBs, const int version,
560
                                    int bandCount, GAIN_MODIFICATION* pGMod,
561
33.9k
                                    int isBox) {
562
33.9k
  int sign, bsGainOffset, bsAttenuationScaling, bsAmplificationScaling;
563
564
33.9k
  if (version > 0) {
565
7.32k
    int b, shapeFilterPresent;
566
567
7.32k
    if (isBox) {
568
0
      FDKpushFor(hBs, 4); /* reserved */
569
0
      bandCount = FDKreadBits(hBs, 4);
570
0
    }
571
572
14.6k
    for (b = 0; b < bandCount; b++) {
573
7.34k
      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
7.34k
      if (!isBox)
582
7.34k
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
583
7.34k
      if (pGMod[b].targetCharacteristicLeftPresent) {
584
1.79k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
585
1.79k
        pGMod[b].targetCharacteristicLeftIndex = FDKreadBits(hBs, 4);
586
1.79k
      }
587
7.34k
      if (!isBox)
588
7.34k
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
589
7.34k
      if (pGMod[b].targetCharacteristicRightPresent) {
590
1.96k
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
591
1.96k
        pGMod[b].targetCharacteristicRightIndex = FDKreadBits(hBs, 4);
592
1.96k
      }
593
7.34k
      if (!isBox) pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
594
7.34k
      if (pGMod[b].gainScalingPresent) {
595
2.12k
        bsAttenuationScaling = FDKreadBits(hBs, 4);
596
2.12k
        pGMod[b].attenuationScaling = (FIXP_SGL)(
597
2.12k
            bsAttenuationScaling
598
2.12k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
599
2.12k
        bsAmplificationScaling = FDKreadBits(hBs, 4);
600
2.12k
        pGMod[b].amplificationScaling = (FIXP_SGL)(
601
2.12k
            bsAmplificationScaling
602
2.12k
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
603
2.12k
      }
604
7.34k
      if (!isBox) pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
605
7.34k
      if (pGMod[b].gainOffsetPresent) {
606
2.04k
        if (isBox) FDKpushFor(hBs, 2); /* reserved */
607
2.04k
        sign = FDKreadBits(hBs, 1);
608
2.04k
        bsGainOffset = FDKreadBits(hBs, 5);
609
2.04k
        pGMod[b].gainOffset = (FIXP_SGL)(
610
2.04k
            (1 + bsGainOffset)
611
2.04k
            << (FRACT_BITS - 1 - 2 - 4)); /* (1+bsGainOffset) * 0.25; */
612
2.04k
        if (sign) {
613
1.24k
          pGMod[b].gainOffset = -pGMod[b].gainOffset;
614
1.24k
        }
615
2.04k
      }
616
7.34k
    }
617
7.32k
    if (bandCount == 1) {
618
6.98k
      shapeFilterPresent = FDKreadBits(hBs, 1);
619
6.98k
      if (shapeFilterPresent) {
620
1.72k
        if (isBox) FDKpushFor(hBs, 3); /* reserved */
621
1.72k
        FDKpushFor(hBs, 4);            /* pGMod->shapeFilterIndex */
622
5.25k
      } else {
623
5.25k
        if (isBox) FDKpushFor(hBs, 7); /* reserved */
624
5.25k
      }
625
6.98k
    }
626
26.6k
  } else {
627
26.6k
    int b, gainScalingPresent, gainOffsetPresent;
628
26.6k
    FIXP_SGL attenuationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
629
26.6k
             amplificationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
630
26.6k
             gainOffset = (FIXP_SGL)0;
631
26.6k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
632
26.6k
    gainScalingPresent = FDKreadBits(hBs, 1);
633
26.6k
    if (gainScalingPresent) {
634
15.9k
      bsAttenuationScaling = FDKreadBits(hBs, 4);
635
15.9k
      attenuationScaling = (FIXP_SGL)(
636
15.9k
          bsAttenuationScaling
637
15.9k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
638
15.9k
      bsAmplificationScaling = FDKreadBits(hBs, 4);
639
15.9k
      amplificationScaling = (FIXP_SGL)(
640
15.9k
          bsAmplificationScaling
641
15.9k
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
642
15.9k
    }
643
26.6k
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
644
26.6k
    gainOffsetPresent = FDKreadBits(hBs, 1);
645
26.6k
    if (gainOffsetPresent) {
646
13.8k
      if (isBox) FDKpushFor(hBs, 2); /* reserved */
647
13.8k
      sign = FDKreadBits(hBs, 1);
648
13.8k
      bsGainOffset = FDKreadBits(hBs, 5);
649
13.8k
      gainOffset =
650
13.8k
          (FIXP_SGL)((1 + bsGainOffset) << (FRACT_BITS - 1 - 2 -
651
13.8k
                                            4)); /* (1+bsGainOffset) * 0.25; */
652
13.8k
      if (sign) {
653
8.26k
        gainOffset = -gainOffset;
654
8.26k
      }
655
13.8k
    }
656
133k
    for (b = 0; b < 4; b++) {
657
106k
      pGMod[b].targetCharacteristicLeftPresent = 0;
658
106k
      pGMod[b].targetCharacteristicRightPresent = 0;
659
106k
      pGMod[b].gainScalingPresent = gainScalingPresent;
660
106k
      pGMod[b].attenuationScaling = attenuationScaling;
661
106k
      pGMod[b].amplificationScaling = amplificationScaling;
662
106k
      pGMod[b].gainOffsetPresent = gainOffsetPresent;
663
106k
      pGMod[b].gainOffset = gainOffset;
664
106k
    }
665
26.6k
  }
666
33.9k
}
667
668
static void _readDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs, const int version,
669
10.4k
                                   DRC_CHARACTERISTIC* pDChar, int isBox) {
670
10.4k
  if (version == 0) {
671
1.87k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
672
1.87k
    pDChar->cicpIndex = FDKreadBits(hBs, 7);
673
1.87k
    if (pDChar->cicpIndex > 0) {
674
802
      pDChar->present = 1;
675
802
      pDChar->isCICP = 1;
676
1.07k
    } else {
677
1.07k
      pDChar->present = 0;
678
1.07k
    }
679
8.59k
  } else {
680
8.59k
    pDChar->present = FDKreadBits(hBs, 1);
681
8.59k
    if (isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
682
8.59k
    if (pDChar->present) {
683
2.94k
      if (!isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
684
2.94k
      if (pDChar->isCICP) {
685
1.04k
        if (isBox) FDKpushFor(hBs, 1); /* reserved */
686
1.04k
        pDChar->cicpIndex = FDKreadBits(hBs, 7);
687
1.89k
      } else {
688
1.89k
        pDChar->custom.left = FDKreadBits(hBs, 4);
689
1.89k
        pDChar->custom.right = FDKreadBits(hBs, 4);
690
1.89k
      }
691
2.94k
    }
692
8.59k
  }
693
10.4k
}
694
695
static void _readBandBorder(HANDLE_FDK_BITSTREAM hBs, BAND_BORDER* pBBord,
696
6.40k
                            int drcBandType, int isBox) {
697
6.40k
  if (drcBandType) {
698
3.46k
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
699
3.46k
    pBBord->crossoverFreqIndex = FDKreadBits(hBs, 4);
700
3.46k
  } else {
701
2.93k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
702
2.93k
    pBBord->startSubBandIndex = FDKreadBits(hBs, 10);
703
2.93k
  }
704
6.40k
}
705
706
static DRC_ERROR _readGainSet(HANDLE_FDK_BITSTREAM hBs, const int version,
707
                              int* gainSequenceIndex, GAIN_SET* pGSet,
708
88.4k
                              int isBox) {
709
88.4k
  if (isBox) FDKpushFor(hBs, 2); /* reserved */
710
88.4k
  pGSet->gainCodingProfile = FDKreadBits(hBs, 2);
711
88.4k
  pGSet->gainInterpolationType = FDKreadBits(hBs, 1);
712
88.4k
  pGSet->fullFrame = FDKreadBits(hBs, 1);
713
88.4k
  pGSet->timeAlignment = FDKreadBits(hBs, 1);
714
88.4k
  pGSet->timeDeltaMinPresent = FDKreadBits(hBs, 1);
715
716
88.4k
  if (pGSet->timeDeltaMinPresent) {
717
2.02k
    int bsTimeDeltaMin;
718
2.02k
    if (isBox) FDKpushFor(hBs, 5); /* reserved */
719
2.02k
    bsTimeDeltaMin = FDKreadBits(hBs, 11);
720
2.02k
    pGSet->timeDeltaMin = bsTimeDeltaMin + 1;
721
2.02k
  }
722
723
88.4k
  if (pGSet->gainCodingProfile != GCP_CONSTANT) {
724
83.8k
    int i;
725
83.8k
    if (isBox) FDKpushFor(hBs, 3); /* reserved */
726
83.8k
    pGSet->bandCount = FDKreadBits(hBs, 4);
727
83.8k
    if (pGSet->bandCount > 4) return DE_MEMORY_ERROR;
728
729
83.6k
    if ((pGSet->bandCount > 1) || isBox) {
730
2.82k
      pGSet->drcBandType = FDKreadBits(hBs, 1);
731
2.82k
    }
732
733
94.1k
    for (i = 0; i < pGSet->bandCount; i++) {
734
10.4k
      if (version == 0) {
735
1.87k
        *gainSequenceIndex = (*gainSequenceIndex) + 1;
736
8.59k
      } else {
737
8.59k
        int indexPresent;
738
8.59k
        indexPresent = (isBox) ? 1 : FDKreadBits(hBs, 1);
739
8.59k
        if (indexPresent) {
740
2.23k
          int bsIndex;
741
2.23k
          bsIndex = FDKreadBits(hBs, 6);
742
2.23k
          *gainSequenceIndex = bsIndex;
743
6.35k
        } else {
744
6.35k
          *gainSequenceIndex = (*gainSequenceIndex) + 1;
745
6.35k
        }
746
8.59k
      }
747
10.4k
      pGSet->gainSequenceIndex[i] = *gainSequenceIndex;
748
10.4k
      _readDrcCharacteristic(hBs, version, &(pGSet->drcCharacteristic[i]),
749
10.4k
                             isBox);
750
10.4k
    }
751
90.0k
    for (i = 1; i < pGSet->bandCount; i++) {
752
6.40k
      _readBandBorder(hBs, &(pGSet->bandBorder[i]), pGSet->drcBandType, isBox);
753
6.40k
    }
754
83.6k
  } else {
755
4.55k
    pGSet->bandCount = 1;
756
4.55k
    *gainSequenceIndex = (*gainSequenceIndex) + 1;
757
4.55k
    pGSet->gainSequenceIndex[0] = *gainSequenceIndex;
758
4.55k
  }
759
760
88.2k
  return DE_OK;
761
88.4k
}
762
763
static DRC_ERROR _readCustomDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs,
764
                                              const CHARACTERISTIC_SIDE side,
765
                                              UCHAR* pCharacteristicFormat,
766
                                              CUSTOM_DRC_CHAR* pCChar,
767
10.4k
                                              int isBox) {
768
10.4k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
769
10.4k
  *pCharacteristicFormat = FDKreadBits(hBs, 1);
770
10.4k
  if (*pCharacteristicFormat == CF_SIGMOID) {
771
8.68k
    int bsGain, bsIoRatio, bsExp;
772
8.68k
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
773
8.68k
    bsGain = FDKreadBits(hBs, 6);
774
8.68k
    if (side == CS_LEFT) {
775
4.63k
      pCChar->sigmoid.gain = (FIXP_SGL)(bsGain << (FRACT_BITS - 1 - 6));
776
4.63k
    } else {
777
4.05k
      pCChar->sigmoid.gain = (FIXP_SGL)(-bsGain << (FRACT_BITS - 1 - 6));
778
4.05k
    }
779
8.68k
    bsIoRatio = FDKreadBits(hBs, 4);
780
    /* pCChar->sigmoid.ioRatio = 0.05 + 0.15 * bsIoRatio; */
781
8.68k
    pCChar->sigmoid.ioRatio =
782
8.68k
        FL2FXCONST_SGL(0.05f / (float)(1 << 2)) +
783
8.68k
        (FIXP_SGL)((((3 * bsIoRatio) << (FRACT_BITS - 1)) / 5) >> 4);
784
8.68k
    bsExp = FDKreadBits(hBs, 4);
785
8.68k
    if (bsExp < 15) {
786
8.30k
      pCChar->sigmoid.exp = (FIXP_SGL)((1 + 2 * bsExp) << (FRACT_BITS - 1 - 5));
787
8.30k
    } else {
788
383
      pCChar->sigmoid.exp = (FIXP_SGL)MAXVAL_SGL; /* represents infinity */
789
383
    }
790
8.68k
    pCChar->sigmoid.flipSign = FDKreadBits(hBs, 1);
791
8.68k
  } else { /* CF_NODES */
792
1.77k
    int i, bsCharacteristicNodeCount, bsNodeLevelDelta, bsNodeGain;
793
1.77k
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
794
1.77k
    bsCharacteristicNodeCount = FDKreadBits(hBs, 2);
795
1.77k
    pCChar->nodes.characteristicNodeCount = bsCharacteristicNodeCount + 1;
796
1.77k
    if (pCChar->nodes.characteristicNodeCount > 4) return DE_MEMORY_ERROR;
797
1.77k
    pCChar->nodes.nodeLevel[0] = DRC_INPUT_LOUDNESS_TARGET_SGL;
798
1.77k
    pCChar->nodes.nodeGain[0] = (FIXP_SGL)0;
799
5.70k
    for (i = 0; i < pCChar->nodes.characteristicNodeCount; i++) {
800
3.93k
      if (isBox) FDKpushFor(hBs, 3); /* reserved */
801
3.93k
      bsNodeLevelDelta = FDKreadBits(hBs, 5);
802
3.93k
      if (side == CS_LEFT) {
803
2.96k
        pCChar->nodes.nodeLevel[i + 1] =
804
2.96k
            pCChar->nodes.nodeLevel[i] -
805
2.96k
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
806
2.96k
      } else {
807
975
        pCChar->nodes.nodeLevel[i + 1] =
808
975
            pCChar->nodes.nodeLevel[i] +
809
975
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
810
975
      }
811
3.93k
      bsNodeGain = FDKreadBits(hBs, 8);
812
3.93k
      pCChar->nodes.nodeGain[i + 1] = (FIXP_SGL)(
813
3.93k
          (bsNodeGain - 128)
814
3.93k
          << (FRACT_BITS - 1 - 1 - 7)); /* 0.5f * bsNodeGain - 64.0f; */
815
3.93k
    }
816
1.77k
  }
817
10.4k
  return DE_OK;
818
10.4k
}
819
820
20.3k
static void _skipLoudEqInstructions(HANDLE_FDK_BITSTREAM hBs) {
821
20.3k
  int i;
822
20.3k
  int downmixIdPresent, additionalDownmixIdPresent,
823
20.3k
      additionalDownmixIdCount = 0;
824
20.3k
  int drcSetIdPresent, additionalDrcSetIdPresent, additionalDrcSetIdCount = 0;
825
20.3k
  int eqSetIdPresent, additionalEqSetIdPresent, additionalEqSetIdCount = 0;
826
20.3k
  int loudEqGainSequenceCount, drcCharacteristicFormatIsCICP;
827
828
20.3k
  FDKpushFor(hBs, 4); /* loudEqSetId */
829
20.3k
  FDKpushFor(hBs, 4); /* drcLocation */
830
20.3k
  downmixIdPresent = FDKreadBits(hBs, 1);
831
20.3k
  if (downmixIdPresent) {
832
1.92k
    FDKpushFor(hBs, 7); /* downmixId */
833
1.92k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
834
1.92k
    if (additionalDownmixIdPresent) {
835
823
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
836
14.9k
      for (i = 0; i < additionalDownmixIdCount; i++) {
837
14.1k
        FDKpushFor(hBs, 7); /* additionalDownmixId */
838
14.1k
      }
839
823
    }
840
1.92k
  }
841
842
20.3k
  drcSetIdPresent = FDKreadBits(hBs, 1);
843
20.3k
  if (drcSetIdPresent) {
844
2.25k
    FDKpushFor(hBs, 6); /* drcSetId */
845
2.25k
    additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
846
2.25k
    if (additionalDrcSetIdPresent) {
847
933
      additionalDrcSetIdCount = FDKreadBits(hBs, 6);
848
5.41k
      for (i = 0; i < additionalDrcSetIdCount; i++) {
849
4.48k
        FDKpushFor(hBs, 6); /* additionalDrcSetId; */
850
4.48k
      }
851
933
    }
852
2.25k
  }
853
854
20.3k
  eqSetIdPresent = FDKreadBits(hBs, 1);
855
20.3k
  if (eqSetIdPresent) {
856
1.48k
    FDKpushFor(hBs, 6); /* eqSetId */
857
1.48k
    additionalEqSetIdPresent = FDKreadBits(hBs, 1);
858
1.48k
    if (additionalEqSetIdPresent) {
859
854
      additionalEqSetIdCount = FDKreadBits(hBs, 6);
860
5.36k
      for (i = 0; i < additionalEqSetIdCount; i++) {
861
4.50k
        FDKpushFor(hBs, 6); /* additionalEqSetId; */
862
4.50k
      }
863
854
    }
864
1.48k
  }
865
866
20.3k
  FDKpushFor(hBs, 1); /* loudnessAfterDrc */
867
20.3k
  FDKpushFor(hBs, 1); /* loudnessAfterEq */
868
20.3k
  loudEqGainSequenceCount = FDKreadBits(hBs, 6);
869
31.5k
  for (i = 0; i < loudEqGainSequenceCount; i++) {
870
11.2k
    FDKpushFor(hBs, 6); /* gainSequenceIndex */
871
11.2k
    drcCharacteristicFormatIsCICP = FDKreadBits(hBs, 1);
872
11.2k
    if (drcCharacteristicFormatIsCICP) {
873
772
      FDKpushFor(hBs, 7); /* drcCharacteristic */
874
10.4k
    } else {
875
10.4k
      FDKpushFor(hBs, 4); /* drcCharacteristicLeftIndex */
876
10.4k
      FDKpushFor(hBs, 4); /* drcCharacteristicRightIndex */
877
10.4k
    }
878
11.2k
    FDKpushFor(hBs, 6); /* frequencyRangeIndex */
879
11.2k
    FDKpushFor(hBs, 3); /* bsLoudEqScaling */
880
11.2k
    FDKpushFor(hBs, 5); /* bsLoudEqOffset */
881
11.2k
  }
882
20.3k
}
883
884
25.4k
static void _skipEqSubbandGainSpline(HANDLE_FDK_BITSTREAM hBs) {
885
25.4k
  int nEqNodes, k, bits;
886
25.4k
  nEqNodes = FDKreadBits(hBs, 5);
887
25.4k
  nEqNodes += 2;
888
78.3k
  for (k = 0; k < nEqNodes; k++) {
889
52.9k
    bits = FDKreadBits(hBs, 1);
890
52.9k
    if (!bits) {
891
50.2k
      FDKpushFor(hBs, 4);
892
50.2k
    }
893
52.9k
  }
894
25.4k
  FDKpushFor(hBs, 4 * (nEqNodes - 1));
895
25.4k
  bits = FDKreadBits(hBs, 2);
896
25.4k
  switch (bits) {
897
23.7k
    case 0:
898
23.7k
      FDKpushFor(hBs, 5);
899
23.7k
      break;
900
398
    case 1:
901
1.17k
    case 2:
902
1.17k
      FDKpushFor(hBs, 4);
903
1.17k
      break;
904
494
    case 3:
905
494
      FDKpushFor(hBs, 3);
906
494
      break;
907
25.4k
  }
908
25.4k
  FDKpushFor(hBs, 5 * (nEqNodes - 1));
909
25.4k
}
910
911
6.89k
static void _skipEqCoefficients(HANDLE_FDK_BITSTREAM hBs) {
912
6.89k
  int j, k;
913
6.89k
  int eqDelayMaxPresent;
914
6.89k
  int uniqueFilterBlockCount, filterElementCount, filterElementGainPresent;
915
6.89k
  int uniqueTdFilterElementCount, eqFilterFormat, bsRealZeroRadiusOneCount,
916
6.89k
      realZeroCount, genericZeroCount, realPoleCount, complexPoleCount,
917
6.89k
      firFilterOrder;
918
6.89k
  int uniqueEqSubbandGainsCount, eqSubbandGainRepresentation,
919
6.89k
      eqSubbandGainCount;
920
6.89k
  int eqSubbandGainFormat;
921
922
6.89k
  eqDelayMaxPresent = FDKreadBits(hBs, 1);
923
6.89k
  if (eqDelayMaxPresent) {
924
1.33k
    FDKpushFor(hBs, 8); /* bsEqDelayMax */
925
1.33k
  }
926
927
6.89k
  uniqueFilterBlockCount = FDKreadBits(hBs, 6);
928
25.8k
  for (j = 0; j < uniqueFilterBlockCount; j++) {
929
18.9k
    filterElementCount = FDKreadBits(hBs, 6);
930
29.8k
    for (k = 0; k < filterElementCount; k++) {
931
10.9k
      FDKpushFor(hBs, 6); /* filterElementIndex */
932
10.9k
      filterElementGainPresent = FDKreadBits(hBs, 1);
933
10.9k
      if (filterElementGainPresent) {
934
675
        FDKpushFor(hBs, 10); /* bsFilterElementGain */
935
675
      }
936
10.9k
    }
937
18.9k
  }
938
6.89k
  uniqueTdFilterElementCount = FDKreadBits(hBs, 6);
939
25.2k
  for (j = 0; j < uniqueTdFilterElementCount; j++) {
940
18.3k
    eqFilterFormat = FDKreadBits(hBs, 1);
941
18.3k
    if (eqFilterFormat == 0) { /* pole/zero */
942
17.2k
      bsRealZeroRadiusOneCount = FDKreadBits(hBs, 3);
943
17.2k
      realZeroCount = FDKreadBits(hBs, 6);
944
17.2k
      genericZeroCount = FDKreadBits(hBs, 6);
945
17.2k
      realPoleCount = FDKreadBits(hBs, 4);
946
17.2k
      complexPoleCount = FDKreadBits(hBs, 4);
947
17.2k
      FDKpushFor(hBs, 2 * bsRealZeroRadiusOneCount * 1);
948
17.2k
      FDKpushFor(hBs, realZeroCount * 8);
949
17.2k
      FDKpushFor(hBs, genericZeroCount * 14);
950
17.2k
      FDKpushFor(hBs, realPoleCount * 8);
951
17.2k
      FDKpushFor(hBs, complexPoleCount * 14);
952
17.2k
    } else { /* FIR coefficients */
953
1.09k
      firFilterOrder = FDKreadBits(hBs, 7);
954
1.09k
      FDKpushFor(hBs, 1);
955
1.09k
      FDKpushFor(hBs, (firFilterOrder / 2 + 1) * 11);
956
1.09k
    }
957
18.3k
  }
958
6.89k
  uniqueEqSubbandGainsCount = FDKreadBits(hBs, 6);
959
6.89k
  if (uniqueEqSubbandGainsCount > 0) {
960
4.27k
    eqSubbandGainRepresentation = FDKreadBits(hBs, 1);
961
4.27k
    eqSubbandGainFormat = FDKreadBits(hBs, 4);
962
4.27k
    switch (eqSubbandGainFormat) {
963
345
      case GF_QMF32:
964
345
        eqSubbandGainCount = 32;
965
345
        break;
966
76
      case GF_QMFHYBRID39:
967
76
        eqSubbandGainCount = 39;
968
76
        break;
969
161
      case GF_QMF64:
970
161
        eqSubbandGainCount = 64;
971
161
        break;
972
416
      case GF_QMFHYBRID71:
973
416
        eqSubbandGainCount = 71;
974
416
        break;
975
69
      case GF_QMF128:
976
69
        eqSubbandGainCount = 128;
977
69
        break;
978
165
      case GF_QMFHYBRID135:
979
165
        eqSubbandGainCount = 135;
980
165
        break;
981
481
      case GF_UNIFORM:
982
3.04k
      default:
983
3.04k
        eqSubbandGainCount = FDKreadBits(hBs, 8);
984
3.04k
        eqSubbandGainCount++;
985
3.04k
        break;
986
4.27k
    }
987
130k
    for (k = 0; k < uniqueEqSubbandGainsCount; k++) {
988
126k
      if (eqSubbandGainRepresentation == 1) {
989
25.4k
        _skipEqSubbandGainSpline(hBs);
990
101k
      } else {
991
101k
        FDKpushFor(hBs, eqSubbandGainCount * 9);
992
101k
      }
993
126k
    }
994
4.27k
  }
995
6.89k
}
996
997
static void _skipTdFilterCascade(HANDLE_FDK_BITSTREAM hBs,
998
3.23k
                                 const int eqChannelGroupCount) {
999
3.23k
  int i, eqCascadeGainPresent, filterBlockCount, eqPhaseAlignmentPresent;
1000
6.93k
  for (i = 0; i < eqChannelGroupCount; i++) {
1001
3.70k
    eqCascadeGainPresent = FDKreadBits(hBs, 1);
1002
3.70k
    if (eqCascadeGainPresent) {
1003
1.39k
      FDKpushFor(hBs, 10); /* bsEqCascadeGain */
1004
1.39k
    }
1005
3.70k
    filterBlockCount = FDKreadBits(hBs, 4);
1006
3.70k
    FDKpushFor(hBs, filterBlockCount * 7); /* filterBlockIndex */
1007
3.70k
  }
1008
3.23k
  eqPhaseAlignmentPresent = FDKreadBits(hBs, 1);
1009
3.23k
  {
1010
3.23k
    if (eqPhaseAlignmentPresent) {
1011
3.13k
      for (i = 0; i < eqChannelGroupCount; i++) {
1012
1.13k
        FDKpushFor(hBs, (eqChannelGroupCount - i - 1) * 1);
1013
1.13k
      }
1014
1.99k
    }
1015
3.23k
  }
1016
3.23k
}
1017
1018
static DRC_ERROR _skipEqInstructions(HANDLE_FDK_BITSTREAM hBs,
1019
32.0k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1020
32.0k
  DRC_ERROR err = DE_OK;
1021
32.0k
  int c, i, k, channelCount;
1022
32.0k
  int downmixIdPresent, downmixId, eqApplyToDownmix, additionalDownmixIdPresent,
1023
32.0k
      additionalDownmixIdCount = 0;
1024
32.0k
  int additionalDrcSetIdPresent, additionalDrcSetIdCount;
1025
32.0k
  int dependsOnEqSetPresent, eqChannelGroupCount, tdFilterCascadePresent,
1026
32.0k
      subbandGainsPresent, eqTransitionDurationPresent;
1027
32.0k
  UCHAR eqChannelGroupForChannel[8];
1028
1029
32.0k
  FDKpushFor(hBs, 6); /* eqSetId */
1030
32.0k
  FDKpushFor(hBs, 4); /* eqSetComplexityLevel */
1031
32.0k
  downmixIdPresent = FDKreadBits(hBs, 1);
1032
32.0k
  if (downmixIdPresent) {
1033
4.96k
    downmixId = FDKreadBits(hBs, 7);
1034
4.96k
    eqApplyToDownmix = FDKreadBits(hBs, 1);
1035
4.96k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1036
4.96k
    if (additionalDownmixIdPresent) {
1037
1.42k
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
1038
1.42k
      FDKpushFor(hBs, additionalDownmixIdCount * 7); /* additionalDownmixId */
1039
1.42k
    }
1040
27.0k
  } else {
1041
27.0k
    downmixId = 0;
1042
27.0k
    eqApplyToDownmix = 0;
1043
27.0k
  }
1044
32.0k
  FDKpushFor(hBs, 6); /* drcSetId */
1045
32.0k
  additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
1046
32.0k
  if (additionalDrcSetIdPresent) {
1047
2.63k
    additionalDrcSetIdCount = FDKreadBits(hBs, 6);
1048
17.8k
    for (i = 0; i < additionalDrcSetIdCount; i++) {
1049
15.1k
      FDKpushFor(hBs, 6); /* additionalDrcSetId */
1050
15.1k
    }
1051
2.63k
  }
1052
32.0k
  FDKpushFor(hBs, 16); /* eqSetPurpose */
1053
32.0k
  dependsOnEqSetPresent = FDKreadBits(hBs, 1);
1054
32.0k
  if (dependsOnEqSetPresent) {
1055
3.29k
    FDKpushFor(hBs, 6); /* dependsOnEqSet */
1056
28.7k
  } else {
1057
28.7k
    FDKpushFor(hBs, 1); /* noIndependentEqUse */
1058
28.7k
  }
1059
1060
32.0k
  channelCount = hUniDrcConfig->channelLayout.baseChannelCount;
1061
32.0k
  if ((downmixIdPresent == 1) && (eqApplyToDownmix == 1) && (downmixId != 0) &&
1062
2.23k
      (downmixId != DOWNMIX_ID_ANY_DOWNMIX) &&
1063
1.92k
      (additionalDownmixIdCount == 0)) {
1064
1.56k
    DOWNMIX_INSTRUCTIONS* pDown =
1065
1.56k
        selectDownmixInstructions(hUniDrcConfig, downmixId);
1066
1.56k
    if (pDown == NULL) return DE_NOT_OK;
1067
1068
321
    channelCount =
1069
321
        pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1070
30.4k
  } else if ((downmixId == DOWNMIX_ID_ANY_DOWNMIX) ||
1071
30.1k
             (additionalDownmixIdCount > 1)) {
1072
848
    channelCount = 1;
1073
848
  }
1074
1075
30.7k
  eqChannelGroupCount = 0;
1076
105k
  for (c = 0; c < channelCount; c++) {
1077
75.0k
    int newGroup = 1;
1078
75.0k
    if (c >= 8) return DE_MEMORY_ERROR;
1079
74.8k
    eqChannelGroupForChannel[c] = FDKreadBits(hBs, 7);
1080
271k
    for (k = 0; k < c; k++) {
1081
196k
      if (eqChannelGroupForChannel[c] == eqChannelGroupForChannel[k]) {
1082
172k
        newGroup = 0;
1083
172k
      }
1084
196k
    }
1085
74.8k
    if (newGroup == 1) {
1086
25.1k
      eqChannelGroupCount += 1;
1087
25.1k
    }
1088
74.8k
  }
1089
30.5k
  tdFilterCascadePresent = FDKreadBits(hBs, 1);
1090
30.5k
  if (tdFilterCascadePresent) {
1091
3.23k
    _skipTdFilterCascade(hBs, eqChannelGroupCount);
1092
3.23k
  }
1093
30.5k
  subbandGainsPresent = FDKreadBits(hBs, 1);
1094
30.5k
  if (subbandGainsPresent) {
1095
2.67k
    FDKpushFor(hBs, eqChannelGroupCount * 6); /* subbandGainsIndex */
1096
2.67k
  }
1097
30.5k
  eqTransitionDurationPresent = FDKreadBits(hBs, 1);
1098
30.5k
  if (eqTransitionDurationPresent) {
1099
2.66k
    FDKpushFor(hBs, 5); /* bsEqTransitionDuration */
1100
2.66k
  }
1101
30.5k
  return err;
1102
30.7k
}
1103
1104
4.57k
static void _skipDrcCoefficientsBasic(HANDLE_FDK_BITSTREAM hBs) {
1105
4.57k
  FDKpushFor(hBs, 4); /* drcLocation */
1106
4.57k
  FDKpushFor(hBs, 7); /* drcCharacteristic */
1107
4.57k
}
1108
1109
static DRC_ERROR _readDrcCoefficientsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1110
                                            const int version,
1111
76.5k
                                            DRC_COEFFICIENTS_UNI_DRC* pCoef) {
1112
76.5k
  DRC_ERROR err = DE_OK;
1113
76.5k
  int i, bsDrcFrameSize;
1114
76.5k
  int gainSequenceIndex = -1;
1115
1116
76.5k
  pCoef->drcLocation = FDKreadBits(hBs, 4);
1117
76.5k
  pCoef->drcFrameSizePresent = FDKreadBits(hBs, 1);
1118
1119
76.5k
  if (pCoef->drcFrameSizePresent == 1) {
1120
4.23k
    bsDrcFrameSize = FDKreadBits(hBs, 15);
1121
4.23k
    pCoef->drcFrameSize = bsDrcFrameSize + 1;
1122
4.23k
  }
1123
76.5k
  if (version == 0) {
1124
48.9k
    int gainSequenceCount = 0, gainSetCount;
1125
48.9k
    pCoef->characteristicLeftCount = 0;
1126
48.9k
    pCoef->characteristicRightCount = 0;
1127
48.9k
    gainSetCount = FDKreadBits(hBs, 6);
1128
48.9k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1129
76.5k
    for (i = 0; i < gainSetCount; i++) {
1130
27.6k
      GAIN_SET tmpGset;
1131
27.6k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1132
27.6k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1133
27.6k
      if (err) return err;
1134
27.5k
      gainSequenceCount += tmpGset.bandCount;
1135
1136
27.5k
      if (i >= 12) continue;
1137
10.6k
      pCoef->gainSet[i] = tmpGset;
1138
10.6k
    }
1139
48.8k
    pCoef->gainSequenceCount = gainSequenceCount;
1140
48.8k
  } else { /* (version == 1) */
1141
27.5k
    UCHAR drcCharacteristicLeftPresent, drcCharacteristicRightPresent;
1142
27.5k
    UCHAR shapeFiltersPresent, shapeFilterCount, tmpPresent;
1143
27.5k
    int gainSetCount;
1144
27.5k
    drcCharacteristicLeftPresent = FDKreadBits(hBs, 1);
1145
27.5k
    if (drcCharacteristicLeftPresent) {
1146
1.88k
      pCoef->characteristicLeftCount = FDKreadBits(hBs, 4);
1147
1.88k
      if ((pCoef->characteristicLeftCount + 1) > 16) return DE_MEMORY_ERROR;
1148
7.85k
      for (i = 0; i < pCoef->characteristicLeftCount; i++) {
1149
5.97k
        err = _readCustomDrcCharacteristic(
1150
5.97k
            hBs, CS_LEFT, &(pCoef->characteristicLeftFormat[i + 1]),
1151
5.97k
            &(pCoef->customCharacteristicLeft[i + 1]), 0);
1152
5.97k
        if (err) return err;
1153
5.97k
      }
1154
1.88k
    }
1155
27.5k
    drcCharacteristicRightPresent = FDKreadBits(hBs, 1);
1156
27.5k
    if (drcCharacteristicRightPresent) {
1157
1.84k
      pCoef->characteristicRightCount = FDKreadBits(hBs, 4);
1158
1.84k
      if ((pCoef->characteristicRightCount + 1) > 16) return DE_MEMORY_ERROR;
1159
6.32k
      for (i = 0; i < pCoef->characteristicRightCount; i++) {
1160
4.48k
        err = _readCustomDrcCharacteristic(
1161
4.48k
            hBs, CS_RIGHT, &(pCoef->characteristicRightFormat[i + 1]),
1162
4.48k
            &(pCoef->customCharacteristicRight[i + 1]), 0);
1163
4.48k
        if (err) return err;
1164
4.48k
      }
1165
1.84k
    }
1166
27.5k
    shapeFiltersPresent = FDKreadBits(hBs, 1);
1167
27.5k
    if (shapeFiltersPresent) {
1168
3.37k
      shapeFilterCount = FDKreadBits(hBs, 4);
1169
26.5k
      for (i = 0; i < shapeFilterCount; i++) {
1170
23.2k
        tmpPresent = FDKreadBits(hBs, 1);
1171
23.2k
        if (tmpPresent) /* lfCutParams */
1172
6.17k
          FDKpushFor(hBs, 5);
1173
1174
23.2k
        tmpPresent = FDKreadBits(hBs, 1);
1175
23.2k
        if (tmpPresent) /* lfBoostParams */
1176
6.06k
          FDKpushFor(hBs, 5);
1177
1178
23.2k
        tmpPresent = FDKreadBits(hBs, 1);
1179
23.2k
        if (tmpPresent) /* hfCutParams */
1180
5.88k
          FDKpushFor(hBs, 5);
1181
1182
23.2k
        tmpPresent = FDKreadBits(hBs, 1);
1183
23.2k
        if (tmpPresent) /* hfBoostParams */
1184
4.47k
          FDKpushFor(hBs, 5);
1185
23.2k
      }
1186
3.37k
    }
1187
27.5k
    pCoef->gainSequenceCount = FDKreadBits(hBs, 6);
1188
27.5k
    gainSetCount = FDKreadBits(hBs, 6);
1189
27.5k
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1190
88.2k
    for (i = 0; i < gainSetCount; i++) {
1191
60.7k
      GAIN_SET tmpGset;
1192
60.7k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1193
60.7k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1194
60.7k
      if (err) return err;
1195
1196
60.6k
      if (i >= 12) continue;
1197
26.2k
      pCoef->gainSet[i] = tmpGset;
1198
26.2k
    }
1199
27.5k
  }
1200
992k
  for (i = 0; i < 12; i++) {
1201
916k
    pCoef->gainSetIndexForGainSequence[i] = 255;
1202
916k
  }
1203
113k
  for (i = 0; i < pCoef->gainSetCount; i++) {
1204
36.6k
    int b;
1205
51.5k
    for (b = 0; b < pCoef->gainSet[i].bandCount; b++) {
1206
14.8k
      if (pCoef->gainSet[i].gainSequenceIndex[b] >= 12) continue;
1207
10.7k
      pCoef->gainSetIndexForGainSequence[pCoef->gainSet[i]
1208
10.7k
                                             .gainSequenceIndex[b]] = i;
1209
10.7k
    }
1210
36.6k
  }
1211
1212
76.3k
  return err;
1213
76.5k
}
1214
1215
19.4k
static void _skipDrcInstructionsBasic(HANDLE_FDK_BITSTREAM hBs) {
1216
19.4k
  int drcSetEffect;
1217
19.4k
  int additionalDownmixIdPresent, additionalDownmixIdCount,
1218
19.4k
      limiterPeakTargetPresent;
1219
19.4k
  int drcSetTargetLoudnessPresent, drcSetTargetLoudnessValueLowerPresent;
1220
1221
19.4k
  FDKpushFor(hBs, 6); /* drcSetId */
1222
19.4k
  FDKpushFor(hBs, 4); /* drcLocation */
1223
19.4k
  FDKpushFor(hBs, 7); /* downmixId */
1224
19.4k
  additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1225
19.4k
  if (additionalDownmixIdPresent) {
1226
1.86k
    additionalDownmixIdCount = FDKreadBits(hBs, 3);
1227
1.86k
    FDKpushFor(hBs, 7 * additionalDownmixIdCount); /* additionalDownmixId */
1228
1.86k
  }
1229
1230
19.4k
  drcSetEffect = FDKreadBits(hBs, 16);
1231
19.4k
  if (!(drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF))) {
1232
18.0k
    limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1233
18.0k
    if (limiterPeakTargetPresent) {
1234
1.29k
      FDKpushFor(hBs, 8); /* bsLimiterPeakTarget */
1235
1.29k
    }
1236
18.0k
  }
1237
1238
19.4k
  drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1239
19.4k
  if (drcSetTargetLoudnessPresent) {
1240
1.52k
    FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueUpper */
1241
1.52k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1242
1.52k
    if (drcSetTargetLoudnessValueLowerPresent) {
1243
712
      FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueLower */
1244
712
    }
1245
1.52k
  }
1246
19.4k
}
1247
1248
static DRC_ERROR _readDrcInstructionsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1249
                                            const int version,
1250
                                            HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
1251
769k
                                            DRC_INSTRUCTIONS_UNI_DRC* pInst) {
1252
769k
  DRC_ERROR err = DE_OK;
1253
769k
  int i, g, c;
1254
769k
  int downmixIdPresent, additionalDownmixIdPresent, additionalDownmixIdCount;
1255
769k
  int bsLimiterPeakTarget, channelCount;
1256
769k
  DRC_COEFFICIENTS_UNI_DRC* pCoef = NULL;
1257
769k
  int repeatParameters, bsRepeatParametersCount;
1258
769k
  int repeatSequenceIndex, bsRepeatSequenceCount;
1259
769k
  SCHAR* gainSetIndex = pInst->gainSetIndex;
1260
769k
  SCHAR channelGroupForChannel[8];
1261
769k
  DUCKING_MODIFICATION duckingModificationForChannelGroup[8];
1262
1263
769k
  pInst->drcSetId = FDKreadBits(hBs, 6);
1264
769k
  if (version == 0) {
1265
    /* Assume all v0 DRC sets to be manageable in terms of complexity */
1266
698k
    pInst->drcSetComplexityLevel = 2;
1267
698k
  } else {
1268
71.0k
    pInst->drcSetComplexityLevel = FDKreadBits(hBs, 4);
1269
71.0k
  }
1270
769k
  pInst->drcLocation = FDKreadBits(hBs, 4);
1271
769k
  if (version == 0) {
1272
698k
    downmixIdPresent = 1;
1273
698k
  } else {
1274
71.0k
    downmixIdPresent = FDKreadBits(hBs, 1);
1275
71.0k
  }
1276
769k
  if (downmixIdPresent) {
1277
700k
    pInst->downmixId[0] = FDKreadBits(hBs, 7);
1278
700k
    if (version == 0) {
1279
698k
      if (pInst->downmixId[0] == 0)
1280
688k
        pInst->drcApplyToDownmix = 0;
1281
9.55k
      else
1282
9.55k
        pInst->drcApplyToDownmix = 1;
1283
698k
    } else {
1284
2.20k
      pInst->drcApplyToDownmix = FDKreadBits(hBs, 1);
1285
2.20k
    }
1286
1287
700k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1288
700k
    if (additionalDownmixIdPresent) {
1289
7.15k
      additionalDownmixIdCount = FDKreadBits(hBs, 3);
1290
7.15k
      if ((1 + additionalDownmixIdCount) > 8) return DE_MEMORY_ERROR;
1291
27.0k
      for (i = 0; i < additionalDownmixIdCount; i++) {
1292
19.9k
        pInst->downmixId[i + 1] = FDKreadBits(hBs, 7);
1293
19.9k
      }
1294
7.15k
      pInst->downmixIdCount = 1 + additionalDownmixIdCount;
1295
693k
    } else {
1296
693k
      pInst->downmixIdCount = 1;
1297
693k
    }
1298
700k
  } else {
1299
68.8k
    pInst->downmixId[0] = 0;
1300
68.8k
    pInst->downmixIdCount = 1;
1301
68.8k
  }
1302
1303
769k
  pInst->drcSetEffect = FDKreadBits(hBs, 16);
1304
1305
769k
  if ((pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) == 0) {
1306
763k
    pInst->limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1307
763k
    if (pInst->limiterPeakTargetPresent) {
1308
1.78k
      bsLimiterPeakTarget = FDKreadBits(hBs, 8);
1309
1.78k
      pInst->limiterPeakTarget = -(FIXP_SGL)(
1310
1.78k
          bsLimiterPeakTarget
1311
1.78k
          << (FRACT_BITS - 1 - 3 - 5)); /* - bsLimiterPeakTarget * 0.125; */
1312
1.78k
    }
1313
763k
  }
1314
1315
769k
  pInst->drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1316
1317
  /* set default values */
1318
769k
  pInst->drcSetTargetLoudnessValueUpper = 0;
1319
769k
  pInst->drcSetTargetLoudnessValueLower = -63;
1320
1321
769k
  if (pInst->drcSetTargetLoudnessPresent == 1) {
1322
4.93k
    int bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1323
4.93k
    int drcSetTargetLoudnessValueLowerPresent;
1324
4.93k
    bsDrcSetTargetLoudnessValueUpper = FDKreadBits(hBs, 6);
1325
4.93k
    pInst->drcSetTargetLoudnessValueUpper =
1326
4.93k
        bsDrcSetTargetLoudnessValueUpper - 63;
1327
4.93k
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1328
4.93k
    if (drcSetTargetLoudnessValueLowerPresent == 1) {
1329
1.80k
      bsDrcSetTargetLoudnessValueLower = FDKreadBits(hBs, 6);
1330
1.80k
      pInst->drcSetTargetLoudnessValueLower =
1331
1.80k
          bsDrcSetTargetLoudnessValueLower - 63;
1332
1.80k
    }
1333
4.93k
  }
1334
1335
769k
  pInst->dependsOnDrcSetPresent = FDKreadBits(hBs, 1);
1336
1337
769k
  pInst->noIndependentUse = 0;
1338
769k
  if (pInst->dependsOnDrcSetPresent) {
1339
4.44k
    pInst->dependsOnDrcSet = FDKreadBits(hBs, 6);
1340
765k
  } else {
1341
765k
    pInst->noIndependentUse = FDKreadBits(hBs, 1);
1342
765k
  }
1343
1344
769k
  if (version == 0) {
1345
698k
    pInst->requiresEq = 0;
1346
698k
  } else {
1347
71.0k
    pInst->requiresEq = FDKreadBits(hBs, 1);
1348
71.0k
  }
1349
1350
769k
  pCoef = selectDrcCoefficients(hUniDrcConfig, pInst->drcLocation);
1351
1352
769k
  pInst->drcChannelCount = channelCount =
1353
769k
      hUniDrcConfig->channelLayout.baseChannelCount;
1354
1355
769k
  if (pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) {
1356
6.46k
    DUCKING_MODIFICATION* pDModForChannel =
1357
6.46k
        pInst->duckingModificationForChannel;
1358
6.46k
    c = 0;
1359
21.1k
    while (c < channelCount) {
1360
14.9k
      int bsGainSetIndex;
1361
14.9k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1362
14.9k
      if (c >= 8) return DE_MEMORY_ERROR;
1363
14.9k
      gainSetIndex[c] = bsGainSetIndex - 1;
1364
14.9k
      _decodeDuckingModification(hBs, &(pDModForChannel[c]), 0);
1365
1366
14.9k
      c++;
1367
14.9k
      repeatParameters = FDKreadBits(hBs, 1);
1368
14.9k
      if (repeatParameters == 1) {
1369
1.31k
        bsRepeatParametersCount = FDKreadBits(hBs, 5);
1370
1.31k
        bsRepeatParametersCount += 1;
1371
6.76k
        for (i = 0; i < bsRepeatParametersCount; i++) {
1372
5.71k
          if (c >= 8) return DE_MEMORY_ERROR;
1373
5.44k
          gainSetIndex[c] = gainSetIndex[c - 1];
1374
5.44k
          pDModForChannel[c] = pDModForChannel[c - 1];
1375
5.44k
          c++;
1376
5.44k
        }
1377
1.31k
      }
1378
14.9k
    }
1379
6.19k
    if (c > channelCount) {
1380
85
      return DE_NOT_OK;
1381
85
    }
1382
1383
6.11k
    err = deriveDrcChannelGroups(
1384
6.11k
        pInst->drcSetEffect, pInst->drcChannelCount, gainSetIndex,
1385
6.11k
        pDModForChannel, &pInst->nDrcChannelGroups,
1386
6.11k
        pInst->gainSetIndexForChannelGroup, channelGroupForChannel,
1387
6.11k
        duckingModificationForChannelGroup);
1388
6.11k
    if (err) return (err);
1389
763k
  } else {
1390
763k
    int deriveChannelCount = 0;
1391
763k
    if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1392
694k
        (pInst->downmixId[0] != DOWNMIX_ID_BASE_LAYOUT) &&
1393
6.53k
        (pInst->downmixId[0] != DOWNMIX_ID_ANY_DOWNMIX) &&
1394
5.94k
        (pInst->downmixIdCount == 1)) {
1395
2.37k
      if (hUniDrcConfig->downmixInstructionsCount != 0) {
1396
735
        DOWNMIX_INSTRUCTIONS* pDown =
1397
735
            selectDownmixInstructions(hUniDrcConfig, pInst->downmixId[0]);
1398
735
        if (pDown == NULL) return DE_NOT_OK;
1399
484
        pInst->drcChannelCount = channelCount =
1400
484
            pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1401
1.63k
      } else {
1402
1.63k
        deriveChannelCount = 1;
1403
1.63k
        channelCount = 1;
1404
1.63k
      }
1405
760k
    } else if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1406
691k
               ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1407
691k
                (pInst->downmixIdCount > 1))) {
1408
      /* Set maximum channel count as upper border. The effective channel count
1409
       * is set at the process function. */
1410
4.18k
      pInst->drcChannelCount = 8;
1411
4.18k
      channelCount = 1;
1412
4.18k
    }
1413
1414
762k
    c = 0;
1415
6.12M
    while (c < channelCount) {
1416
5.36M
      int bsGainSetIndex;
1417
5.36M
      bsGainSetIndex = FDKreadBits(hBs, 6);
1418
5.36M
      if (c >= 8) return DE_MEMORY_ERROR;
1419
5.36M
      gainSetIndex[c] = bsGainSetIndex - 1;
1420
5.36M
      c++;
1421
5.36M
      repeatSequenceIndex = FDKreadBits(hBs, 1);
1422
1423
5.36M
      if (repeatSequenceIndex == 1) {
1424
5.17k
        bsRepeatSequenceCount = FDKreadBits(hBs, 5);
1425
5.17k
        bsRepeatSequenceCount += 1;
1426
5.17k
        if (deriveChannelCount) {
1427
649
          channelCount = 1 + bsRepeatSequenceCount;
1428
649
        }
1429
24.4k
        for (i = 0; i < bsRepeatSequenceCount; i++) {
1430
19.5k
          if (c >= 8) return DE_MEMORY_ERROR;
1431
19.2k
          gainSetIndex[c] = bsGainSetIndex - 1;
1432
19.2k
          c++;
1433
19.2k
        }
1434
5.17k
      }
1435
5.36M
    }
1436
762k
    if (c > channelCount) {
1437
82
      return DE_NOT_OK;
1438
82
    }
1439
762k
    if (deriveChannelCount) {
1440
1.58k
      pInst->drcChannelCount = channelCount;
1441
1.58k
    }
1442
1443
    /* DOWNMIX_ID_ANY_DOWNMIX: channelCount is 1. Distribute gainSetIndex to all
1444
     * channels. */
1445
762k
    if ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1446
761k
        (pInst->downmixIdCount > 1)) {
1447
33.3k
      for (c = 1; c < pInst->drcChannelCount; c++) {
1448
28.8k
        gainSetIndex[c] = gainSetIndex[0];
1449
28.8k
      }
1450
4.53k
    }
1451
1452
762k
    err = deriveDrcChannelGroups(pInst->drcSetEffect, pInst->drcChannelCount,
1453
762k
                                 gainSetIndex, NULL, &pInst->nDrcChannelGroups,
1454
762k
                                 pInst->gainSetIndexForChannelGroup,
1455
762k
                                 channelGroupForChannel, NULL);
1456
762k
    if (err) return (err);
1457
1458
796k
    for (g = 0; g < pInst->nDrcChannelGroups; g++) {
1459
33.9k
      int set, bandCount;
1460
33.9k
      set = pInst->gainSetIndexForChannelGroup[g];
1461
1462
      /* get bandCount */
1463
33.9k
      if (pCoef != NULL && set < pCoef->gainSetCount) {
1464
1.13k
        bandCount = pCoef->gainSet[set].bandCount;
1465
32.8k
      } else {
1466
32.8k
        bandCount = 1;
1467
32.8k
      }
1468
1469
33.9k
      _decodeGainModification(hBs, version, bandCount,
1470
33.9k
                              pInst->gainModificationForChannelGroup[g], 0);
1471
33.9k
    }
1472
762k
  }
1473
1474
767k
  return err;
1475
769k
}
1476
1477
static DRC_ERROR _readChannelLayout(HANDLE_FDK_BITSTREAM hBs,
1478
32.5k
                                    CHANNEL_LAYOUT* pChan) {
1479
32.5k
  DRC_ERROR err = DE_OK;
1480
1481
32.5k
  pChan->baseChannelCount = FDKreadBits(hBs, 7);
1482
1483
32.5k
  if (pChan->baseChannelCount > 8) return DE_NOT_OK;
1484
1485
32.3k
  pChan->layoutSignalingPresent = FDKreadBits(hBs, 1);
1486
1487
32.3k
  if (pChan->layoutSignalingPresent) {
1488
6.99k
    pChan->definedLayout = FDKreadBits(hBs, 8);
1489
1490
6.99k
    if (pChan->definedLayout == 0) {
1491
486
      int i;
1492
2.55k
      for (i = 0; i < pChan->baseChannelCount; i++) {
1493
2.06k
        if (i < 8) {
1494
2.06k
          pChan->speakerPosition[i] = FDKreadBits(hBs, 7);
1495
2.06k
        } else {
1496
0
          FDKpushFor(hBs, 7);
1497
0
        }
1498
2.06k
      }
1499
486
    }
1500
6.99k
  }
1501
32.3k
  return err;
1502
32.5k
}
1503
1504
static DRC_ERROR _readDownmixInstructions(HANDLE_FDK_BITSTREAM hBs,
1505
                                          const int version,
1506
                                          CHANNEL_LAYOUT* pChan,
1507
1.24M
                                          DOWNMIX_INSTRUCTIONS* pDown) {
1508
1.24M
  DRC_ERROR err = DE_OK;
1509
1510
1.24M
  pDown->downmixId = FDKreadBits(hBs, 7);
1511
1.24M
  pDown->targetChannelCount = FDKreadBits(hBs, 7);
1512
1.24M
  pDown->targetLayout = FDKreadBits(hBs, 8);
1513
1.24M
  pDown->downmixCoefficientsPresent = FDKreadBits(hBs, 1);
1514
1515
1.24M
  if (pDown->downmixCoefficientsPresent) {
1516
4.63k
    int nDownmixCoeffs = pDown->targetChannelCount * pChan->baseChannelCount;
1517
4.63k
    int i;
1518
4.63k
    if (nDownmixCoeffs > 8 * 8) return DE_NOT_OK;
1519
4.43k
    if (version == 0) {
1520
2.53k
      pDown->bsDownmixOffset = 0;
1521
24.3k
      for (i = 0; i < nDownmixCoeffs; i++) {
1522
        /* LFE downmix coefficients are not supported. */
1523
21.8k
        pDown->downmixCoefficient[i] = downmixCoeff[FDKreadBits(hBs, 4)];
1524
21.8k
      }
1525
2.53k
    } else {
1526
1.90k
      pDown->bsDownmixOffset = FDKreadBits(hBs, 4);
1527
6.00k
      for (i = 0; i < nDownmixCoeffs; i++) {
1528
4.10k
        pDown->downmixCoefficient[i] = downmixCoeffV1[FDKreadBits(hBs, 5)];
1529
4.10k
      }
1530
1.90k
    }
1531
4.43k
  }
1532
1.24M
  return err;
1533
1.24M
}
1534
1535
static DRC_ERROR _readDrcExtensionV1(HANDLE_FDK_BITSTREAM hBs,
1536
21.7k
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1537
21.7k
  DRC_ERROR err = DE_OK;
1538
21.7k
  int downmixInstructionsV1Present;
1539
21.7k
  int drcCoeffsAndInstructionsUniDrcV1Present;
1540
21.7k
  int loudEqInstructionsPresent, loudEqInstructionsCount;
1541
21.7k
  int eqPresent, eqInstructionsCount;
1542
21.7k
  int i, offset;
1543
21.7k
  int diff = hUniDrcConfig->diff;
1544
1545
21.7k
  downmixInstructionsV1Present = FDKreadBits(hBs, 1);
1546
21.7k
  if (downmixInstructionsV1Present == 1) {
1547
3.35k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1,
1548
3.35k
                        FDKreadBits(hBs, 7));
1549
3.35k
    offset = hUniDrcConfig->downmixInstructionsCountV0;
1550
3.35k
    hUniDrcConfig->downmixInstructionsCount = fMin(
1551
3.35k
        (UCHAR)(offset + hUniDrcConfig->downmixInstructionsCountV1), (UCHAR)6);
1552
45.8k
    for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV1; i++) {
1553
42.5k
      DOWNMIX_INSTRUCTIONS tmpDown;
1554
42.5k
      FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1555
42.5k
      err = _readDownmixInstructions(hBs, 1, &hUniDrcConfig->channelLayout,
1556
42.5k
                                     &tmpDown);
1557
42.5k
      if (err) return err;
1558
42.4k
      if ((offset + i) >= 6) continue;
1559
8.41k
      if (!diff)
1560
236
        diff |= (FDKmemcmp(&tmpDown,
1561
236
                           &(hUniDrcConfig->downmixInstructions[offset + i]),
1562
236
                           sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1563
8.41k
      hUniDrcConfig->downmixInstructions[offset + i] = tmpDown;
1564
8.41k
    }
1565
18.3k
  } else {
1566
18.3k
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1, 0);
1567
18.3k
  }
1568
1569
21.6k
  drcCoeffsAndInstructionsUniDrcV1Present = FDKreadBits(hBs, 1);
1570
21.6k
  if (drcCoeffsAndInstructionsUniDrcV1Present == 1) {
1571
10.6k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1,
1572
10.6k
                        FDKreadBits(hBs, 3));
1573
10.6k
    offset = hUniDrcConfig->drcCoefficientsUniDrcCountV0;
1574
10.6k
    hUniDrcConfig->drcCoefficientsUniDrcCount =
1575
10.6k
        fMin((UCHAR)(offset + hUniDrcConfig->drcCoefficientsUniDrcCountV1),
1576
10.6k
             (UCHAR)2);
1577
38.1k
    for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV1; i++) {
1578
27.5k
      DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1579
27.5k
      FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1580
27.5k
      err = _readDrcCoefficientsUniDrc(hBs, 1, &tmpCoef);
1581
27.5k
      if (err) return err;
1582
27.4k
      if ((offset + i) >= 2) continue;
1583
6.94k
      if (!diff)
1584
289
        diff |= (FDKmemcmp(&tmpCoef,
1585
289
                           &(hUniDrcConfig->drcCoefficientsUniDrc[offset + i]),
1586
289
                           sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1587
6.94k
      hUniDrcConfig->drcCoefficientsUniDrc[offset + i] = tmpCoef;
1588
6.94k
    }
1589
1590
10.5k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1,
1591
10.5k
                        FDKreadBits(hBs, 6));
1592
10.5k
    offset = hUniDrcConfig->drcInstructionsUniDrcCount;
1593
10.5k
    hUniDrcConfig->drcInstructionsUniDrcCount =
1594
10.5k
        fMin((UCHAR)(offset + hUniDrcConfig->drcInstructionsUniDrcCountV1),
1595
10.5k
             (UCHAR)12);
1596
81.2k
    for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCount; i++) {
1597
71.0k
      DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1598
71.0k
      FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1599
71.0k
      err = _readDrcInstructionsUniDrc(hBs, 1, hUniDrcConfig, &tmpInst);
1600
71.0k
      if (err) return err;
1601
70.7k
      if ((offset + i) >= 12) continue;
1602
24.0k
      if (!diff)
1603
501
        diff |= (FDKmemcmp(&tmpInst,
1604
501
                           &(hUniDrcConfig->drcInstructionsUniDrc[offset + i]),
1605
501
                           sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1606
24.0k
      hUniDrcConfig->drcInstructionsUniDrc[offset + i] = tmpInst;
1607
24.0k
    }
1608
10.9k
  } else {
1609
10.9k
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1, 0);
1610
10.9k
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1, 0);
1611
10.9k
  }
1612
1613
21.1k
  loudEqInstructionsPresent = FDKreadBits(hBs, 1);
1614
21.1k
  if (loudEqInstructionsPresent == 1) {
1615
4.21k
    loudEqInstructionsCount = FDKreadBits(hBs, 4);
1616
24.5k
    for (i = 0; i < loudEqInstructionsCount; i++) {
1617
20.3k
      _skipLoudEqInstructions(hBs);
1618
20.3k
    }
1619
4.21k
  }
1620
1621
21.1k
  eqPresent = FDKreadBits(hBs, 1);
1622
21.1k
  if (eqPresent == 1) {
1623
6.89k
    _skipEqCoefficients(hBs);
1624
6.89k
    eqInstructionsCount = FDKreadBits(hBs, 4);
1625
38.9k
    for (i = 0; i < eqInstructionsCount; i++) {
1626
32.0k
      _skipEqInstructions(hBs, hUniDrcConfig);
1627
32.0k
    }
1628
6.89k
  }
1629
1630
21.1k
  hUniDrcConfig->diff = diff;
1631
1632
21.1k
  return err;
1633
21.6k
}
1634
1635
static DRC_ERROR _readUniDrcConfigExtension(
1636
19.5k
    HANDLE_FDK_BITSTREAM hBs, HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1637
19.5k
  DRC_ERROR err = DE_OK;
1638
19.5k
  int k, bitSizeLen, extSizeBits, bitSize;
1639
19.5k
  INT nBitsRemaining;
1640
19.5k
  UNI_DRC_CONFIG_EXTENSION* pExt = &(hUniDrcConfig->uniDrcConfigExt);
1641
1642
19.5k
  k = 0;
1643
19.5k
  pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1644
38.6k
  while (pExt->uniDrcConfigExtType[k] != UNIDRCCONFEXT_TERM) {
1645
37.7k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1646
37.0k
    bitSizeLen = FDKreadBits(hBs, 4);
1647
37.0k
    extSizeBits = bitSizeLen + 4;
1648
1649
37.0k
    bitSize = FDKreadBits(hBs, extSizeBits);
1650
37.0k
    pExt->extBitSize[k] = bitSize + 1;
1651
37.0k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1652
1653
37.0k
    switch (pExt->uniDrcConfigExtType[k]) {
1654
21.7k
      case UNIDRCCONFEXT_V1:
1655
21.7k
        err = _readDrcExtensionV1(hBs, hUniDrcConfig);
1656
21.7k
        if (err) return err;
1657
21.1k
        if (nBitsRemaining !=
1658
21.1k
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1659
17.3k
          return DE_NOT_OK;
1660
3.79k
        break;
1661
3.79k
      case UNIDRCCONFEXT_PARAM_DRC:
1662
      /* add future extensions here */
1663
15.3k
      default:
1664
15.3k
        FDKpushFor(hBs, pExt->extBitSize[k]);
1665
15.3k
        break;
1666
37.0k
    }
1667
19.1k
    k++;
1668
19.1k
    pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1669
19.1k
  }
1670
1671
963
  return err;
1672
19.5k
}
1673
1674
DRC_ERROR
1675
drcDec_readUniDrcConfig(HANDLE_FDK_BITSTREAM hBs,
1676
32.5k
                        HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1677
32.5k
  DRC_ERROR err = DE_OK;
1678
32.5k
  int i, diff = 0;
1679
32.5k
  int drcDescriptionBasicPresent, drcCoefficientsBasicCount,
1680
32.5k
      drcInstructionsBasicCount;
1681
32.5k
  CHANNEL_LAYOUT tmpChan;
1682
32.5k
  FDKmemclear(&tmpChan, sizeof(CHANNEL_LAYOUT));
1683
32.5k
  if (hUniDrcConfig == NULL) return DE_NOT_OK;
1684
1685
32.5k
  diff |= _compAssign(&hUniDrcConfig->sampleRatePresent, FDKreadBits(hBs, 1));
1686
1687
32.5k
  if (hUniDrcConfig->sampleRatePresent == 1) {
1688
1.07k
    diff |=
1689
1.07k
        _compAssign(&hUniDrcConfig->sampleRate, FDKreadBits(hBs, 18) + 1000);
1690
1.07k
  }
1691
1692
32.5k
  diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV0,
1693
32.5k
                      FDKreadBits(hBs, 7));
1694
1695
32.5k
  drcDescriptionBasicPresent = FDKreadBits(hBs, 1);
1696
32.5k
  if (drcDescriptionBasicPresent == 1) {
1697
2.46k
    drcCoefficientsBasicCount = FDKreadBits(hBs, 3);
1698
2.46k
    drcInstructionsBasicCount = FDKreadBits(hBs, 4);
1699
30.1k
  } else {
1700
30.1k
    drcCoefficientsBasicCount = 0;
1701
30.1k
    drcInstructionsBasicCount = 0;
1702
30.1k
  }
1703
1704
32.5k
  diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV0,
1705
32.5k
                      FDKreadBits(hBs, 3));
1706
32.5k
  diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV0,
1707
32.5k
                      FDKreadBits(hBs, 6));
1708
1709
32.5k
  err = _readChannelLayout(hBs, &tmpChan);
1710
32.5k
  if (err) return err;
1711
1712
32.3k
  if (!diff)
1713
16.8k
    diff |= (FDKmemcmp(&tmpChan, &hUniDrcConfig->channelLayout,
1714
16.8k
                       sizeof(CHANNEL_LAYOUT)) != 0);
1715
32.3k
  hUniDrcConfig->channelLayout = tmpChan;
1716
1717
32.3k
  hUniDrcConfig->downmixInstructionsCount =
1718
32.3k
      fMin(hUniDrcConfig->downmixInstructionsCountV0, (UCHAR)6);
1719
1.23M
  for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV0; i++) {
1720
1.20M
    DOWNMIX_INSTRUCTIONS tmpDown;
1721
1.20M
    FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1722
1.20M
    err = _readDownmixInstructions(hBs, 0, &hUniDrcConfig->channelLayout,
1723
1.20M
                                   &tmpDown);
1724
1.20M
    if (err) return err;
1725
1.20M
    if (i >= 6) continue;
1726
71.3k
    if (!diff)
1727
12.2k
      diff |= (FDKmemcmp(&tmpDown, &(hUniDrcConfig->downmixInstructions[i]),
1728
12.2k
                         sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1729
71.3k
    hUniDrcConfig->downmixInstructions[i] = tmpDown;
1730
71.3k
  }
1731
1732
36.8k
  for (i = 0; i < drcCoefficientsBasicCount; i++) {
1733
4.57k
    _skipDrcCoefficientsBasic(hBs);
1734
4.57k
  }
1735
51.7k
  for (i = 0; i < drcInstructionsBasicCount; i++) {
1736
19.4k
    _skipDrcInstructionsBasic(hBs);
1737
19.4k
  }
1738
1739
32.2k
  hUniDrcConfig->drcCoefficientsUniDrcCount =
1740
32.2k
      fMin(hUniDrcConfig->drcCoefficientsUniDrcCountV0, (UCHAR)2);
1741
81.1k
  for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV0; i++) {
1742
48.9k
    DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1743
48.9k
    FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1744
48.9k
    err = _readDrcCoefficientsUniDrc(hBs, 0, &tmpCoef);
1745
48.9k
    if (err) return err;
1746
48.8k
    if (i >= 2) continue;
1747
20.1k
    if (!diff)
1748
5.37k
      diff |= (FDKmemcmp(&tmpCoef, &(hUniDrcConfig->drcCoefficientsUniDrc[i]),
1749
5.37k
                         sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1750
20.1k
    hUniDrcConfig->drcCoefficientsUniDrc[i] = tmpCoef;
1751
20.1k
  }
1752
1753
32.1k
  hUniDrcConfig->drcInstructionsUniDrcCount =
1754
32.1k
      fMin(hUniDrcConfig->drcInstructionsUniDrcCountV0, (UCHAR)12);
1755
728k
  for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCountV0; i++) {
1756
698k
    DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1757
698k
    FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1758
698k
    err = _readDrcInstructionsUniDrc(hBs, 0, hUniDrcConfig, &tmpInst);
1759
698k
    if (err) return err;
1760
696k
    if (i >= 12) continue;
1761
170k
    if (!diff)
1762
38.8k
      diff |= (FDKmemcmp(&tmpInst, &(hUniDrcConfig->drcInstructionsUniDrc[i]),
1763
38.8k
                         sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1764
170k
    hUniDrcConfig->drcInstructionsUniDrc[i] = tmpInst;
1765
170k
  }
1766
1767
30.4k
  diff |=
1768
30.4k
      _compAssign(&hUniDrcConfig->uniDrcConfigExtPresent, FDKreadBits(hBs, 1));
1769
30.4k
  hUniDrcConfig->diff = diff;
1770
1771
30.4k
  if (hUniDrcConfig->uniDrcConfigExtPresent == 1) {
1772
19.5k
    err = _readUniDrcConfigExtension(hBs, hUniDrcConfig);
1773
19.5k
    if (err) return err;
1774
19.5k
  }
1775
1776
11.8k
  return err;
1777
30.4k
}
1778
1779
/*******************/
1780
/* loudnessInfoSet */
1781
/*******************/
1782
1783
static DRC_ERROR _decodeMethodValue(HANDLE_FDK_BITSTREAM hBs,
1784
                                    const UCHAR methodDefinition,
1785
6.45k
                                    FIXP_DBL* methodValue, INT isBox) {
1786
6.45k
  int tmp;
1787
6.45k
  FIXP_DBL val;
1788
6.45k
  switch (methodDefinition) {
1789
1.73k
    case MD_UNKNOWN_OTHER:
1790
2.34k
    case MD_PROGRAM_LOUDNESS:
1791
2.85k
    case MD_ANCHOR_LOUDNESS:
1792
3.02k
    case MD_MAX_OF_LOUDNESS_RANGE:
1793
3.32k
    case MD_MOMENTARY_LOUDNESS_MAX:
1794
3.53k
    case MD_SHORT_TERM_LOUDNESS_MAX:
1795
3.53k
      tmp = FDKreadBits(hBs, 8);
1796
3.53k
      val = FL2FXCONST_DBL(-57.75f / (float)(1 << 7)) +
1797
3.53k
            (FIXP_DBL)(
1798
3.53k
                tmp << (DFRACT_BITS - 1 - 2 - 7)); /* -57.75 + tmp * 0.25; */
1799
3.53k
      break;
1800
588
    case MD_LOUDNESS_RANGE:
1801
588
      tmp = FDKreadBits(hBs, 8);
1802
588
      if (tmp == 0)
1803
80
        val = (FIXP_DBL)0;
1804
508
      else if (tmp <= 128)
1805
267
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 2 - 7)); /* tmp * 0.25; */
1806
241
      else if (tmp <= 204) {
1807
134
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 1 - 7)) -
1808
134
              FL2FXCONST_DBL(32.0f / (float)(1 << 7)); /* 0.5 * tmp - 32.0f; */
1809
134
      } else {
1810
        /* downscale by 1 more bit to prevent overflow at intermediate result */
1811
107
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 8)) -
1812
107
              FL2FXCONST_DBL(134.0f / (float)(1 << 8)); /* tmp - 134.0; */
1813
107
        val <<= 1;
1814
107
      }
1815
588
      break;
1816
632
    case MD_MIXING_LEVEL:
1817
632
      tmp = FDKreadBits(hBs, isBox ? 8 : 5);
1818
632
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)) +
1819
632
            FL2FXCONST_DBL(80.0f / (float)(1 << 7)); /* tmp + 80.0; */
1820
632
      break;
1821
736
    case MD_ROOM_TYPE:
1822
736
      tmp = FDKreadBits(hBs, isBox ? 8 : 2);
1823
736
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)); /* tmp; */
1824
736
      break;
1825
289
    case MD_SHORT_TERM_LOUDNESS:
1826
289
      tmp = FDKreadBits(hBs, 8);
1827
289
      val = FL2FXCONST_DBL(-116.0f / (float)(1 << 7)) +
1828
289
            (FIXP_DBL)(
1829
289
                tmp << (DFRACT_BITS - 1 - 1 - 7)); /* -116.0 + tmp * 0.5; */
1830
289
      break;
1831
671
    default:
1832
671
      return DE_NOT_OK; /* invalid methodDefinition value */
1833
6.45k
  }
1834
5.78k
  *methodValue = val;
1835
5.78k
  return DE_OK;
1836
6.45k
}
1837
1838
static DRC_ERROR _readLoudnessMeasurement(HANDLE_FDK_BITSTREAM hBs,
1839
6.45k
                                          LOUDNESS_MEASUREMENT* pMeas) {
1840
6.45k
  DRC_ERROR err = DE_OK;
1841
1842
6.45k
  pMeas->methodDefinition = FDKreadBits(hBs, 4);
1843
6.45k
  err =
1844
6.45k
      _decodeMethodValue(hBs, pMeas->methodDefinition, &pMeas->methodValue, 0);
1845
6.45k
  if (err) return err;
1846
5.78k
  pMeas->measurementSystem = FDKreadBits(hBs, 4);
1847
5.78k
  pMeas->reliability = FDKreadBits(hBs, 2);
1848
1849
5.78k
  return err;
1850
6.45k
}
1851
1852
static DRC_ERROR _readLoudnessInfo(HANDLE_FDK_BITSTREAM hBs, const int version,
1853
17.8k
                                   LOUDNESS_INFO* loudnessInfo) {
1854
17.8k
  DRC_ERROR err = DE_OK;
1855
17.8k
  int bsSamplePeakLevel, bsTruePeakLevel, i;
1856
17.8k
  int measurementCount;
1857
1858
17.8k
  loudnessInfo->drcSetId = FDKreadBits(hBs, 6);
1859
17.8k
  if (version >= 1) {
1860
3.32k
    loudnessInfo->eqSetId = FDKreadBits(hBs, 6);
1861
14.4k
  } else {
1862
14.4k
    loudnessInfo->eqSetId = 0;
1863
14.4k
  }
1864
17.8k
  loudnessInfo->downmixId = FDKreadBits(hBs, 7);
1865
1866
17.8k
  loudnessInfo->samplePeakLevelPresent = FDKreadBits(hBs, 1);
1867
17.8k
  if (loudnessInfo->samplePeakLevelPresent) {
1868
1.61k
    bsSamplePeakLevel = FDKreadBits(hBs, 12);
1869
1.61k
    if (bsSamplePeakLevel == 0) {
1870
114
      loudnessInfo->samplePeakLevelPresent = 0;
1871
114
      loudnessInfo->samplePeakLevel = (FIXP_DBL)0;
1872
1.50k
    } else { /* 20.0 - bsSamplePeakLevel * 0.03125; */
1873
1.50k
      loudnessInfo->samplePeakLevel =
1874
1.50k
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1875
1.50k
          (FIXP_DBL)(bsSamplePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1876
1.50k
    }
1877
1.61k
  }
1878
1879
17.8k
  loudnessInfo->truePeakLevelPresent = FDKreadBits(hBs, 1);
1880
17.8k
  if (loudnessInfo->truePeakLevelPresent) {
1881
959
    bsTruePeakLevel = FDKreadBits(hBs, 12);
1882
959
    if (bsTruePeakLevel == 0) {
1883
122
      loudnessInfo->truePeakLevelPresent = 0;
1884
122
      loudnessInfo->truePeakLevel = (FIXP_DBL)0;
1885
837
    } else {
1886
837
      loudnessInfo->truePeakLevel =
1887
837
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1888
837
          (FIXP_DBL)(bsTruePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1889
837
    }
1890
959
    loudnessInfo->truePeakLevelMeasurementSystem = FDKreadBits(hBs, 4);
1891
959
    loudnessInfo->truePeakLevelReliability = FDKreadBits(hBs, 2);
1892
959
  }
1893
1894
17.8k
  measurementCount = FDKreadBits(hBs, 4);
1895
17.8k
  loudnessInfo->measurementCount = fMin(measurementCount, 8);
1896
23.5k
  for (i = 0; i < measurementCount; i++) {
1897
6.45k
    LOUDNESS_MEASUREMENT tmpMeas;
1898
6.45k
    FDKmemclear(&tmpMeas, sizeof(LOUDNESS_MEASUREMENT));
1899
6.45k
    err = _readLoudnessMeasurement(hBs, &tmpMeas);
1900
6.45k
    if (err) return err;
1901
5.78k
    if (i >= 8) continue;
1902
5.27k
    loudnessInfo->loudnessMeasurement[i] = tmpMeas;
1903
5.27k
  }
1904
1905
17.1k
  return err;
1906
17.8k
}
1907
1908
static DRC_ERROR _readLoudnessInfoSetExtEq(
1909
637
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1910
637
  DRC_ERROR err = DE_OK;
1911
637
  int i, offset;
1912
637
  int diff = hLoudnessInfoSet->diff;
1913
1914
637
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV1,
1915
637
                      FDKreadBits(hBs, 6));
1916
637
  diff |=
1917
637
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV1, FDKreadBits(hBs, 6));
1918
1919
637
  offset = hLoudnessInfoSet->loudnessInfoAlbumCountV0;
1920
637
  hLoudnessInfoSet->loudnessInfoAlbumCount = fMin(
1921
637
      (UCHAR)(offset + hLoudnessInfoSet->loudnessInfoAlbumCountV1), (UCHAR)12);
1922
2.42k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV1; i++) {
1923
1.88k
    LOUDNESS_INFO tmpLoud;
1924
1.88k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1925
1.88k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1926
1.88k
    if (err) return err;
1927
1.79k
    if ((offset + i) >= 12) continue;
1928
786
    if (!diff)
1929
137
      diff |= (FDKmemcmp(&tmpLoud,
1930
137
                         &(hLoudnessInfoSet->loudnessInfoAlbum[offset + i]),
1931
137
                         sizeof(LOUDNESS_INFO)) != 0);
1932
786
    hLoudnessInfoSet->loudnessInfoAlbum[offset + i] = tmpLoud;
1933
786
  }
1934
1935
538
  offset = hLoudnessInfoSet->loudnessInfoCountV0;
1936
538
  hLoudnessInfoSet->loudnessInfoCount =
1937
538
      fMin((UCHAR)(offset + hLoudnessInfoSet->loudnessInfoCountV1), (UCHAR)12);
1938
1.93k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV1; i++) {
1939
1.43k
    LOUDNESS_INFO tmpLoud;
1940
1.43k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1941
1.43k
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1942
1.43k
    if (err) return err;
1943
1.39k
    if ((offset + i) >= 12) continue;
1944
605
    if (!diff)
1945
119
      diff |=
1946
119
          (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[offset + i]),
1947
119
                     sizeof(LOUDNESS_INFO)) != 0);
1948
605
    hLoudnessInfoSet->loudnessInfo[offset + i] = tmpLoud;
1949
605
  }
1950
498
  hLoudnessInfoSet->diff = diff;
1951
498
  return err;
1952
538
}
1953
1954
static DRC_ERROR _readLoudnessInfoSetExtension(
1955
965
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1956
965
  DRC_ERROR err = DE_OK;
1957
965
  int k, bitSizeLen, extSizeBits, bitSize;
1958
965
  INT nBitsRemaining;
1959
965
  LOUDNESS_INFO_SET_EXTENSION* pExt = &(hLoudnessInfoSet->loudnessInfoSetExt);
1960
1961
965
  k = 0;
1962
965
  pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1963
1.89k
  while (pExt->loudnessInfoSetExtType[k] != UNIDRCLOUDEXT_TERM) {
1964
1.34k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1965
1.28k
    bitSizeLen = FDKreadBits(hBs, 4);
1966
1.28k
    extSizeBits = bitSizeLen + 4;
1967
1968
1.28k
    bitSize = FDKreadBits(hBs, extSizeBits);
1969
1.28k
    pExt->extBitSize[k] = bitSize + 1;
1970
1.28k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1971
1972
1.28k
    switch (pExt->loudnessInfoSetExtType[k]) {
1973
637
      case UNIDRCLOUDEXT_EQ:
1974
637
        err = _readLoudnessInfoSetExtEq(hBs, hLoudnessInfoSet);
1975
637
        if (err) return err;
1976
498
        if (nBitsRemaining !=
1977
498
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1978
218
          return DE_NOT_OK;
1979
280
        break;
1980
      /* add future extensions here */
1981
646
      default:
1982
646
        FDKpushFor(hBs, pExt->extBitSize[k]);
1983
646
        break;
1984
1.28k
    }
1985
926
    k++;
1986
926
    pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1987
926
  }
1988
1989
544
  return err;
1990
965
}
1991
1992
/* Parser for loundessInfoSet() */
1993
DRC_ERROR
1994
drcDec_readLoudnessInfoSet(HANDLE_FDK_BITSTREAM hBs,
1995
5.42k
                           HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1996
5.42k
  DRC_ERROR err = DE_OK;
1997
5.42k
  int i, diff = 0;
1998
5.42k
  if (hLoudnessInfoSet == NULL) return DE_NOT_OK;
1999
2000
5.42k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV0,
2001
5.42k
                      FDKreadBits(hBs, 6));
2002
5.42k
  diff |=
2003
5.42k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV0, FDKreadBits(hBs, 6));
2004
2005
5.42k
  hLoudnessInfoSet->loudnessInfoAlbumCount =
2006
5.42k
      fMin(hLoudnessInfoSet->loudnessInfoAlbumCountV0, (UCHAR)12);
2007
11.1k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV0; i++) {
2008
6.14k
    LOUDNESS_INFO tmpLoud;
2009
6.14k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2010
6.14k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2011
6.14k
    if (err) return err;
2012
5.72k
    if (i >= 12) continue;
2013
2.80k
    if (!diff)
2014
289
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfoAlbum[i]),
2015
289
                         sizeof(LOUDNESS_INFO)) != 0);
2016
2.80k
    hLoudnessInfoSet->loudnessInfoAlbum[i] = tmpLoud;
2017
2.80k
  }
2018
2019
5.00k
  hLoudnessInfoSet->loudnessInfoCount =
2020
5.00k
      fMin(hLoudnessInfoSet->loudnessInfoCountV0, (UCHAR)12);
2021
13.2k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV0; i++) {
2022
8.33k
    LOUDNESS_INFO tmpLoud;
2023
8.33k
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2024
8.33k
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2025
8.33k
    if (err) return err;
2026
8.22k
    if (i >= 12) continue;
2027
5.11k
    if (!diff)
2028
1.76k
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[i]),
2029
1.76k
                         sizeof(LOUDNESS_INFO)) != 0);
2030
5.11k
    hLoudnessInfoSet->loudnessInfo[i] = tmpLoud;
2031
5.11k
  }
2032
2033
4.89k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoSetExtPresent,
2034
4.89k
                      FDKreadBits(hBs, 1));
2035
4.89k
  hLoudnessInfoSet->diff = diff;
2036
2037
4.89k
  if (hLoudnessInfoSet->loudnessInfoSetExtPresent) {
2038
965
    err = _readLoudnessInfoSetExtension(hBs, hLoudnessInfoSet);
2039
965
    if (err) return err;
2040
965
  }
2041
2042
4.47k
  return err;
2043
4.89k
}