Coverage Report

Created: 2025-12-31 07:57

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
338
#define UNIDRCCONFEXT_PARAM_DRC 0x1
112
370
#define UNIDRCCONFEXT_V1 0x2
113
28
#define UNIDRCLOUDEXT_EQ 0x1
114
115
0
#define UNIDRCGAINEXT_TERM 0x0
116
119
#define UNIDRCLOUDEXT_TERM 0x0
117
1.24k
#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
73.5k
static inline int _compAssign(UCHAR* dest, const UCHAR src) {
140
73.5k
  int diff = 0;
141
73.5k
  if (*dest != src) diff = 1;
142
73.5k
  *dest = src;
143
73.5k
  return diff;
144
73.5k
}
145
146
257
static inline int _compAssign(ULONG* dest, const ULONG src) {
147
257
  int diff = 0;
148
257
  if (*dest != src) diff = 1;
149
257
  *dest = src;
150
257
  return diff;
151
257
}
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
126
                      HANDLE_UNI_DRC_GAIN hUniDrcGain) {
483
126
  DRC_ERROR err = DE_OK;
484
126
  int seq, gainSequenceCount;
485
126
  DRC_COEFFICIENTS_UNI_DRC* pCoef =
486
126
      selectDrcCoefficients(hUniDrcConfig, LOCATION_SELECTED);
487
126
  if (hUniDrcGain == NULL) return DE_NOT_OK;
488
126
  hUniDrcGain->status = 0;
489
126
  if (pCoef) {
490
0
    gainSequenceCount = fMin(pCoef->gainSequenceCount, (UCHAR)12);
491
126
  } else {
492
126
    gainSequenceCount = 0;
493
126
  }
494
495
126
  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
126
  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
126
  if (err == DE_OK && gainSequenceCount > 0) {
525
0
    hUniDrcGain->status = 1;
526
0
  }
527
126
  return err;
528
126
}
529
530
/****************/
531
/* uniDrcConfig */
532
/****************/
533
534
static void _decodeDuckingModification(HANDLE_FDK_BITSTREAM hBs,
535
6.15k
                                       DUCKING_MODIFICATION* pDMod, int isBox) {
536
6.15k
  int bsDuckingScaling, sigma, mu;
537
538
6.15k
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
539
6.15k
  pDMod->duckingScalingPresent = FDKreadBits(hBs, 1);
540
541
6.15k
  if (pDMod->duckingScalingPresent) {
542
96
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
543
96
    bsDuckingScaling = FDKreadBits(hBs, 4);
544
96
    sigma = bsDuckingScaling >> 3;
545
96
    mu = bsDuckingScaling & 0x7;
546
547
96
    if (sigma) {
548
54
      pDMod->duckingScaling = (FIXP_SGL)(
549
54
          (7 - mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 - 0.125 * (1 + mu); */
550
54
    } else {
551
42
      pDMod->duckingScaling = (FIXP_SGL)(
552
42
          (9 + mu) << (FRACT_BITS - 1 - 3 - 2)); /* 1.0 + 0.125 * (1 + mu); */
553
42
    }
554
6.05k
  } else {
555
6.05k
    pDMod->duckingScaling = (FIXP_SGL)(1 << (FRACT_BITS - 1 - 2)); /* 1.0 */
556
6.05k
  }
557
6.15k
}
558
559
static void _decodeGainModification(HANDLE_FDK_BITSTREAM hBs, const int version,
560
                                    int bandCount, GAIN_MODIFICATION* pGMod,
561
1.08k
                                    int isBox) {
562
1.08k
  int sign, bsGainOffset, bsAttenuationScaling, bsAmplificationScaling;
563
564
1.08k
  if (version > 0) {
565
286
    int b, shapeFilterPresent;
566
567
286
    if (isBox) {
568
0
      FDKpushFor(hBs, 4); /* reserved */
569
0
      bandCount = FDKreadBits(hBs, 4);
570
0
    }
571
572
581
    for (b = 0; b < bandCount; b++) {
573
295
      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
295
      if (!isBox)
582
295
        pGMod[b].targetCharacteristicLeftPresent = FDKreadBits(hBs, 1);
583
295
      if (pGMod[b].targetCharacteristicLeftPresent) {
584
81
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
585
81
        pGMod[b].targetCharacteristicLeftIndex = FDKreadBits(hBs, 4);
586
81
      }
587
295
      if (!isBox)
588
295
        pGMod[b].targetCharacteristicRightPresent = FDKreadBits(hBs, 1);
589
295
      if (pGMod[b].targetCharacteristicRightPresent) {
590
69
        if (isBox) FDKpushFor(hBs, 4); /* reserved */
591
69
        pGMod[b].targetCharacteristicRightIndex = FDKreadBits(hBs, 4);
592
69
      }
593
295
      if (!isBox) pGMod[b].gainScalingPresent = FDKreadBits(hBs, 1);
594
295
      if (pGMod[b].gainScalingPresent) {
595
91
        bsAttenuationScaling = FDKreadBits(hBs, 4);
596
91
        pGMod[b].attenuationScaling = (FIXP_SGL)(
597
91
            bsAttenuationScaling
598
91
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
599
91
        bsAmplificationScaling = FDKreadBits(hBs, 4);
600
91
        pGMod[b].amplificationScaling = (FIXP_SGL)(
601
91
            bsAmplificationScaling
602
91
            << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
603
91
      }
604
295
      if (!isBox) pGMod[b].gainOffsetPresent = FDKreadBits(hBs, 1);
605
295
      if (pGMod[b].gainOffsetPresent) {
606
109
        if (isBox) FDKpushFor(hBs, 2); /* reserved */
607
109
        sign = FDKreadBits(hBs, 1);
608
109
        bsGainOffset = FDKreadBits(hBs, 5);
609
109
        pGMod[b].gainOffset = (FIXP_SGL)(
610
109
            (1 + bsGainOffset)
611
109
            << (FRACT_BITS - 1 - 2 - 4)); /* (1+bsGainOffset) * 0.25; */
612
109
        if (sign) {
613
55
          pGMod[b].gainOffset = -pGMod[b].gainOffset;
614
55
        }
615
109
      }
616
295
    }
617
286
    if (bandCount == 1) {
618
280
      shapeFilterPresent = FDKreadBits(hBs, 1);
619
280
      if (shapeFilterPresent) {
620
87
        if (isBox) FDKpushFor(hBs, 3); /* reserved */
621
87
        FDKpushFor(hBs, 4);            /* pGMod->shapeFilterIndex */
622
193
      } else {
623
193
        if (isBox) FDKpushFor(hBs, 7); /* reserved */
624
193
      }
625
280
    }
626
803
  } else {
627
803
    int b, gainScalingPresent, gainOffsetPresent;
628
803
    FIXP_SGL attenuationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
629
803
             amplificationScaling = FL2FXCONST_SGL(1.0f / (float)(1 << 2)),
630
803
             gainOffset = (FIXP_SGL)0;
631
803
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
632
803
    gainScalingPresent = FDKreadBits(hBs, 1);
633
803
    if (gainScalingPresent) {
634
257
      bsAttenuationScaling = FDKreadBits(hBs, 4);
635
257
      attenuationScaling = (FIXP_SGL)(
636
257
          bsAttenuationScaling
637
257
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAttenuationScaling * 0.125; */
638
257
      bsAmplificationScaling = FDKreadBits(hBs, 4);
639
257
      amplificationScaling = (FIXP_SGL)(
640
257
          bsAmplificationScaling
641
257
          << (FRACT_BITS - 1 - 3 - 2)); /* bsAmplificationScaling * 0.125; */
642
257
    }
643
803
    if (isBox) FDKpushFor(hBs, 7); /* reserved */
644
803
    gainOffsetPresent = FDKreadBits(hBs, 1);
645
803
    if (gainOffsetPresent) {
646
273
      if (isBox) FDKpushFor(hBs, 2); /* reserved */
647
273
      sign = FDKreadBits(hBs, 1);
648
273
      bsGainOffset = FDKreadBits(hBs, 5);
649
273
      gainOffset =
650
273
          (FIXP_SGL)((1 + bsGainOffset) << (FRACT_BITS - 1 - 2 -
651
273
                                            4)); /* (1+bsGainOffset) * 0.25; */
652
273
      if (sign) {
653
42
        gainOffset = -gainOffset;
654
42
      }
655
273
    }
656
4.01k
    for (b = 0; b < 4; b++) {
657
3.21k
      pGMod[b].targetCharacteristicLeftPresent = 0;
658
3.21k
      pGMod[b].targetCharacteristicRightPresent = 0;
659
3.21k
      pGMod[b].gainScalingPresent = gainScalingPresent;
660
3.21k
      pGMod[b].attenuationScaling = attenuationScaling;
661
3.21k
      pGMod[b].amplificationScaling = amplificationScaling;
662
3.21k
      pGMod[b].gainOffsetPresent = gainOffsetPresent;
663
3.21k
      pGMod[b].gainOffset = gainOffset;
664
3.21k
    }
665
803
  }
666
1.08k
}
667
668
static void _readDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs, const int version,
669
835
                                   DRC_CHARACTERISTIC* pDChar, int isBox) {
670
835
  if (version == 0) {
671
359
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
672
359
    pDChar->cicpIndex = FDKreadBits(hBs, 7);
673
359
    if (pDChar->cicpIndex > 0) {
674
335
      pDChar->present = 1;
675
335
      pDChar->isCICP = 1;
676
335
    } else {
677
24
      pDChar->present = 0;
678
24
    }
679
476
  } else {
680
476
    pDChar->present = FDKreadBits(hBs, 1);
681
476
    if (isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
682
476
    if (pDChar->present) {
683
136
      if (!isBox) pDChar->isCICP = FDKreadBits(hBs, 1);
684
136
      if (pDChar->isCICP) {
685
86
        if (isBox) FDKpushFor(hBs, 1); /* reserved */
686
86
        pDChar->cicpIndex = FDKreadBits(hBs, 7);
687
86
      } else {
688
50
        pDChar->custom.left = FDKreadBits(hBs, 4);
689
50
        pDChar->custom.right = FDKreadBits(hBs, 4);
690
50
      }
691
136
    }
692
476
  }
693
835
}
694
695
static void _readBandBorder(HANDLE_FDK_BITSTREAM hBs, BAND_BORDER* pBBord,
696
492
                            int drcBandType, int isBox) {
697
492
  if (drcBandType) {
698
230
    if (isBox) FDKpushFor(hBs, 4); /* reserved */
699
230
    pBBord->crossoverFreqIndex = FDKreadBits(hBs, 4);
700
262
  } else {
701
262
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
702
262
    pBBord->startSubBandIndex = FDKreadBits(hBs, 10);
703
262
  }
704
492
}
705
706
static DRC_ERROR _readGainSet(HANDLE_FDK_BITSTREAM hBs, const int version,
707
                              int* gainSequenceIndex, GAIN_SET* pGSet,
708
2.75k
                              int isBox) {
709
2.75k
  if (isBox) FDKpushFor(hBs, 2); /* reserved */
710
2.75k
  pGSet->gainCodingProfile = FDKreadBits(hBs, 2);
711
2.75k
  pGSet->gainInterpolationType = FDKreadBits(hBs, 1);
712
2.75k
  pGSet->fullFrame = FDKreadBits(hBs, 1);
713
2.75k
  pGSet->timeAlignment = FDKreadBits(hBs, 1);
714
2.75k
  pGSet->timeDeltaMinPresent = FDKreadBits(hBs, 1);
715
716
2.75k
  if (pGSet->timeDeltaMinPresent) {
717
297
    int bsTimeDeltaMin;
718
297
    if (isBox) FDKpushFor(hBs, 5); /* reserved */
719
297
    bsTimeDeltaMin = FDKreadBits(hBs, 11);
720
297
    pGSet->timeDeltaMin = bsTimeDeltaMin + 1;
721
297
  }
722
723
2.75k
  if (pGSet->gainCodingProfile != GCP_CONSTANT) {
724
2.37k
    int i;
725
2.37k
    if (isBox) FDKpushFor(hBs, 3); /* reserved */
726
2.37k
    pGSet->bandCount = FDKreadBits(hBs, 4);
727
2.37k
    if (pGSet->bandCount > 4) return DE_MEMORY_ERROR;
728
729
2.32k
    if ((pGSet->bandCount > 1) || isBox) {
730
282
      pGSet->drcBandType = FDKreadBits(hBs, 1);
731
282
    }
732
733
3.16k
    for (i = 0; i < pGSet->bandCount; i++) {
734
835
      if (version == 0) {
735
359
        *gainSequenceIndex = (*gainSequenceIndex) + 1;
736
476
      } else {
737
476
        int indexPresent;
738
476
        indexPresent = (isBox) ? 1 : FDKreadBits(hBs, 1);
739
476
        if (indexPresent) {
740
192
          int bsIndex;
741
192
          bsIndex = FDKreadBits(hBs, 6);
742
192
          *gainSequenceIndex = bsIndex;
743
284
        } else {
744
284
          *gainSequenceIndex = (*gainSequenceIndex) + 1;
745
284
        }
746
476
      }
747
835
      pGSet->gainSequenceIndex[i] = *gainSequenceIndex;
748
835
      _readDrcCharacteristic(hBs, version, &(pGSet->drcCharacteristic[i]),
749
835
                             isBox);
750
835
    }
751
2.81k
    for (i = 1; i < pGSet->bandCount; i++) {
752
492
      _readBandBorder(hBs, &(pGSet->bandBorder[i]), pGSet->drcBandType, isBox);
753
492
    }
754
2.32k
  } else {
755
376
    pGSet->bandCount = 1;
756
376
    *gainSequenceIndex = (*gainSequenceIndex) + 1;
757
376
    pGSet->gainSequenceIndex[0] = *gainSequenceIndex;
758
376
  }
759
760
2.70k
  return DE_OK;
761
2.75k
}
762
763
static DRC_ERROR _readCustomDrcCharacteristic(HANDLE_FDK_BITSTREAM hBs,
764
                                              const CHARACTERISTIC_SIDE side,
765
                                              UCHAR* pCharacteristicFormat,
766
                                              CUSTOM_DRC_CHAR* pCChar,
767
470
                                              int isBox) {
768
470
  if (isBox) FDKpushFor(hBs, 7); /* reserved */
769
470
  *pCharacteristicFormat = FDKreadBits(hBs, 1);
770
470
  if (*pCharacteristicFormat == CF_SIGMOID) {
771
337
    int bsGain, bsIoRatio, bsExp;
772
337
    if (isBox) FDKpushFor(hBs, 1); /* reserved */
773
337
    bsGain = FDKreadBits(hBs, 6);
774
337
    if (side == CS_LEFT) {
775
167
      pCChar->sigmoid.gain = (FIXP_SGL)(bsGain << (FRACT_BITS - 1 - 6));
776
170
    } else {
777
170
      pCChar->sigmoid.gain = (FIXP_SGL)(-bsGain << (FRACT_BITS - 1 - 6));
778
170
    }
779
337
    bsIoRatio = FDKreadBits(hBs, 4);
780
    /* pCChar->sigmoid.ioRatio = 0.05 + 0.15 * bsIoRatio; */
781
337
    pCChar->sigmoid.ioRatio =
782
337
        FL2FXCONST_SGL(0.05f / (float)(1 << 2)) +
783
337
        (FIXP_SGL)((((3 * bsIoRatio) << (FRACT_BITS - 1)) / 5) >> 4);
784
337
    bsExp = FDKreadBits(hBs, 4);
785
337
    if (bsExp < 15) {
786
296
      pCChar->sigmoid.exp = (FIXP_SGL)((1 + 2 * bsExp) << (FRACT_BITS - 1 - 5));
787
296
    } else {
788
41
      pCChar->sigmoid.exp = (FIXP_SGL)MAXVAL_SGL; /* represents infinity */
789
41
    }
790
337
    pCChar->sigmoid.flipSign = FDKreadBits(hBs, 1);
791
337
  } else { /* CF_NODES */
792
133
    int i, bsCharacteristicNodeCount, bsNodeLevelDelta, bsNodeGain;
793
133
    if (isBox) FDKpushFor(hBs, 6); /* reserved */
794
133
    bsCharacteristicNodeCount = FDKreadBits(hBs, 2);
795
133
    pCChar->nodes.characteristicNodeCount = bsCharacteristicNodeCount + 1;
796
133
    if (pCChar->nodes.characteristicNodeCount > 4) return DE_MEMORY_ERROR;
797
133
    pCChar->nodes.nodeLevel[0] = DRC_INPUT_LOUDNESS_TARGET_SGL;
798
133
    pCChar->nodes.nodeGain[0] = (FIXP_SGL)0;
799
464
    for (i = 0; i < pCChar->nodes.characteristicNodeCount; i++) {
800
331
      if (isBox) FDKpushFor(hBs, 3); /* reserved */
801
331
      bsNodeLevelDelta = FDKreadBits(hBs, 5);
802
331
      if (side == CS_LEFT) {
803
215
        pCChar->nodes.nodeLevel[i + 1] =
804
215
            pCChar->nodes.nodeLevel[i] -
805
215
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
806
215
      } else {
807
116
        pCChar->nodes.nodeLevel[i + 1] =
808
116
            pCChar->nodes.nodeLevel[i] +
809
116
            (FIXP_SGL)((1 + bsNodeLevelDelta) << (FRACT_BITS - 1 - 7));
810
116
      }
811
331
      bsNodeGain = FDKreadBits(hBs, 8);
812
331
      pCChar->nodes.nodeGain[i + 1] = (FIXP_SGL)(
813
331
          (bsNodeGain - 128)
814
331
          << (FRACT_BITS - 1 - 1 - 7)); /* 0.5f * bsNodeGain - 64.0f; */
815
331
    }
816
133
  }
817
470
  return DE_OK;
818
470
}
819
820
417
static void _skipLoudEqInstructions(HANDLE_FDK_BITSTREAM hBs) {
821
417
  int i;
822
417
  int downmixIdPresent, additionalDownmixIdPresent,
823
417
      additionalDownmixIdCount = 0;
824
417
  int drcSetIdPresent, additionalDrcSetIdPresent, additionalDrcSetIdCount = 0;
825
417
  int eqSetIdPresent, additionalEqSetIdPresent, additionalEqSetIdCount = 0;
826
417
  int loudEqGainSequenceCount, drcCharacteristicFormatIsCICP;
827
828
417
  FDKpushFor(hBs, 4); /* loudEqSetId */
829
417
  FDKpushFor(hBs, 4); /* drcLocation */
830
417
  downmixIdPresent = FDKreadBits(hBs, 1);
831
417
  if (downmixIdPresent) {
832
128
    FDKpushFor(hBs, 7); /* downmixId */
833
128
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
834
128
    if (additionalDownmixIdPresent) {
835
73
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
836
667
      for (i = 0; i < additionalDownmixIdCount; i++) {
837
594
        FDKpushFor(hBs, 7); /* additionalDownmixId */
838
594
      }
839
73
    }
840
128
  }
841
842
417
  drcSetIdPresent = FDKreadBits(hBs, 1);
843
417
  if (drcSetIdPresent) {
844
110
    FDKpushFor(hBs, 6); /* drcSetId */
845
110
    additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
846
110
    if (additionalDrcSetIdPresent) {
847
26
      additionalDrcSetIdCount = FDKreadBits(hBs, 6);
848
269
      for (i = 0; i < additionalDrcSetIdCount; i++) {
849
243
        FDKpushFor(hBs, 6); /* additionalDrcSetId; */
850
243
      }
851
26
    }
852
110
  }
853
854
417
  eqSetIdPresent = FDKreadBits(hBs, 1);
855
417
  if (eqSetIdPresent) {
856
114
    FDKpushFor(hBs, 6); /* eqSetId */
857
114
    additionalEqSetIdPresent = FDKreadBits(hBs, 1);
858
114
    if (additionalEqSetIdPresent) {
859
44
      additionalEqSetIdCount = FDKreadBits(hBs, 6);
860
811
      for (i = 0; i < additionalEqSetIdCount; i++) {
861
767
        FDKpushFor(hBs, 6); /* additionalEqSetId; */
862
767
      }
863
44
    }
864
114
  }
865
866
417
  FDKpushFor(hBs, 1); /* loudnessAfterDrc */
867
417
  FDKpushFor(hBs, 1); /* loudnessAfterEq */
868
417
  loudEqGainSequenceCount = FDKreadBits(hBs, 6);
869
1.67k
  for (i = 0; i < loudEqGainSequenceCount; i++) {
870
1.26k
    FDKpushFor(hBs, 6); /* gainSequenceIndex */
871
1.26k
    drcCharacteristicFormatIsCICP = FDKreadBits(hBs, 1);
872
1.26k
    if (drcCharacteristicFormatIsCICP) {
873
168
      FDKpushFor(hBs, 7); /* drcCharacteristic */
874
1.09k
    } else {
875
1.09k
      FDKpushFor(hBs, 4); /* drcCharacteristicLeftIndex */
876
1.09k
      FDKpushFor(hBs, 4); /* drcCharacteristicRightIndex */
877
1.09k
    }
878
1.26k
    FDKpushFor(hBs, 6); /* frequencyRangeIndex */
879
1.26k
    FDKpushFor(hBs, 3); /* bsLoudEqScaling */
880
1.26k
    FDKpushFor(hBs, 5); /* bsLoudEqOffset */
881
1.26k
  }
882
417
}
883
884
530
static void _skipEqSubbandGainSpline(HANDLE_FDK_BITSTREAM hBs) {
885
530
  int nEqNodes, k, bits;
886
530
  nEqNodes = FDKreadBits(hBs, 5);
887
530
  nEqNodes += 2;
888
3.16k
  for (k = 0; k < nEqNodes; k++) {
889
2.63k
    bits = FDKreadBits(hBs, 1);
890
2.63k
    if (!bits) {
891
1.71k
      FDKpushFor(hBs, 4);
892
1.71k
    }
893
2.63k
  }
894
530
  FDKpushFor(hBs, 4 * (nEqNodes - 1));
895
530
  bits = FDKreadBits(hBs, 2);
896
530
  switch (bits) {
897
402
    case 0:
898
402
      FDKpushFor(hBs, 5);
899
402
      break;
900
16
    case 1:
901
62
    case 2:
902
62
      FDKpushFor(hBs, 4);
903
62
      break;
904
66
    case 3:
905
66
      FDKpushFor(hBs, 3);
906
66
      break;
907
530
  }
908
530
  FDKpushFor(hBs, 5 * (nEqNodes - 1));
909
530
}
910
911
152
static void _skipEqCoefficients(HANDLE_FDK_BITSTREAM hBs) {
912
152
  int j, k;
913
152
  int eqDelayMaxPresent;
914
152
  int uniqueFilterBlockCount, filterElementCount, filterElementGainPresent;
915
152
  int uniqueTdFilterElementCount, eqFilterFormat, bsRealZeroRadiusOneCount,
916
152
      realZeroCount, genericZeroCount, realPoleCount, complexPoleCount,
917
152
      firFilterOrder;
918
152
  int uniqueEqSubbandGainsCount, eqSubbandGainRepresentation,
919
152
      eqSubbandGainCount;
920
152
  int eqSubbandGainFormat;
921
922
152
  eqDelayMaxPresent = FDKreadBits(hBs, 1);
923
152
  if (eqDelayMaxPresent) {
924
130
    FDKpushFor(hBs, 8); /* bsEqDelayMax */
925
130
  }
926
927
152
  uniqueFilterBlockCount = FDKreadBits(hBs, 6);
928
643
  for (j = 0; j < uniqueFilterBlockCount; j++) {
929
491
    filterElementCount = FDKreadBits(hBs, 6);
930
1.57k
    for (k = 0; k < filterElementCount; k++) {
931
1.07k
      FDKpushFor(hBs, 6); /* filterElementIndex */
932
1.07k
      filterElementGainPresent = FDKreadBits(hBs, 1);
933
1.07k
      if (filterElementGainPresent) {
934
321
        FDKpushFor(hBs, 10); /* bsFilterElementGain */
935
321
      }
936
1.07k
    }
937
491
  }
938
152
  uniqueTdFilterElementCount = FDKreadBits(hBs, 6);
939
1.19k
  for (j = 0; j < uniqueTdFilterElementCount; j++) {
940
1.04k
    eqFilterFormat = FDKreadBits(hBs, 1);
941
1.04k
    if (eqFilterFormat == 0) { /* pole/zero */
942
1.00k
      bsRealZeroRadiusOneCount = FDKreadBits(hBs, 3);
943
1.00k
      realZeroCount = FDKreadBits(hBs, 6);
944
1.00k
      genericZeroCount = FDKreadBits(hBs, 6);
945
1.00k
      realPoleCount = FDKreadBits(hBs, 4);
946
1.00k
      complexPoleCount = FDKreadBits(hBs, 4);
947
1.00k
      FDKpushFor(hBs, 2 * bsRealZeroRadiusOneCount * 1);
948
1.00k
      FDKpushFor(hBs, realZeroCount * 8);
949
1.00k
      FDKpushFor(hBs, genericZeroCount * 14);
950
1.00k
      FDKpushFor(hBs, realPoleCount * 8);
951
1.00k
      FDKpushFor(hBs, complexPoleCount * 14);
952
1.00k
    } else { /* FIR coefficients */
953
41
      firFilterOrder = FDKreadBits(hBs, 7);
954
41
      FDKpushFor(hBs, 1);
955
41
      FDKpushFor(hBs, (firFilterOrder / 2 + 1) * 11);
956
41
    }
957
1.04k
  }
958
152
  uniqueEqSubbandGainsCount = FDKreadBits(hBs, 6);
959
152
  if (uniqueEqSubbandGainsCount > 0) {
960
59
    eqSubbandGainRepresentation = FDKreadBits(hBs, 1);
961
59
    eqSubbandGainFormat = FDKreadBits(hBs, 4);
962
59
    switch (eqSubbandGainFormat) {
963
2
      case GF_QMF32:
964
2
        eqSubbandGainCount = 32;
965
2
        break;
966
14
      case GF_QMFHYBRID39:
967
14
        eqSubbandGainCount = 39;
968
14
        break;
969
1
      case GF_QMF64:
970
1
        eqSubbandGainCount = 64;
971
1
        break;
972
1
      case GF_QMFHYBRID71:
973
1
        eqSubbandGainCount = 71;
974
1
        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
0
      case GF_UNIFORM:
982
39
      default:
983
39
        eqSubbandGainCount = FDKreadBits(hBs, 8);
984
39
        eqSubbandGainCount++;
985
39
        break;
986
59
    }
987
1.22k
    for (k = 0; k < uniqueEqSubbandGainsCount; k++) {
988
1.17k
      if (eqSubbandGainRepresentation == 1) {
989
530
        _skipEqSubbandGainSpline(hBs);
990
640
      } else {
991
640
        FDKpushFor(hBs, eqSubbandGainCount * 9);
992
640
      }
993
1.17k
    }
994
59
  }
995
152
}
996
997
static void _skipTdFilterCascade(HANDLE_FDK_BITSTREAM hBs,
998
111
                                 const int eqChannelGroupCount) {
999
111
  int i, eqCascadeGainPresent, filterBlockCount, eqPhaseAlignmentPresent;
1000
452
  for (i = 0; i < eqChannelGroupCount; i++) {
1001
341
    eqCascadeGainPresent = FDKreadBits(hBs, 1);
1002
341
    if (eqCascadeGainPresent) {
1003
145
      FDKpushFor(hBs, 10); /* bsEqCascadeGain */
1004
145
    }
1005
341
    filterBlockCount = FDKreadBits(hBs, 4);
1006
341
    FDKpushFor(hBs, filterBlockCount * 7); /* filterBlockIndex */
1007
341
  }
1008
111
  eqPhaseAlignmentPresent = FDKreadBits(hBs, 1);
1009
111
  {
1010
111
    if (eqPhaseAlignmentPresent) {
1011
182
      for (i = 0; i < eqChannelGroupCount; i++) {
1012
133
        FDKpushFor(hBs, (eqChannelGroupCount - i - 1) * 1);
1013
133
      }
1014
49
    }
1015
111
  }
1016
111
}
1017
1018
static DRC_ERROR _skipEqInstructions(HANDLE_FDK_BITSTREAM hBs,
1019
508
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1020
508
  DRC_ERROR err = DE_OK;
1021
508
  int c, i, k, channelCount;
1022
508
  int downmixIdPresent, downmixId, eqApplyToDownmix, additionalDownmixIdPresent,
1023
508
      additionalDownmixIdCount = 0;
1024
508
  int additionalDrcSetIdPresent, additionalDrcSetIdCount;
1025
508
  int dependsOnEqSetPresent, eqChannelGroupCount, tdFilterCascadePresent,
1026
508
      subbandGainsPresent, eqTransitionDurationPresent;
1027
508
  UCHAR eqChannelGroupForChannel[8];
1028
1029
508
  FDKpushFor(hBs, 6); /* eqSetId */
1030
508
  FDKpushFor(hBs, 4); /* eqSetComplexityLevel */
1031
508
  downmixIdPresent = FDKreadBits(hBs, 1);
1032
508
  if (downmixIdPresent) {
1033
144
    downmixId = FDKreadBits(hBs, 7);
1034
144
    eqApplyToDownmix = FDKreadBits(hBs, 1);
1035
144
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1036
144
    if (additionalDownmixIdPresent) {
1037
61
      additionalDownmixIdCount = FDKreadBits(hBs, 7);
1038
61
      FDKpushFor(hBs, additionalDownmixIdCount * 7); /* additionalDownmixId */
1039
61
    }
1040
364
  } else {
1041
364
    downmixId = 0;
1042
364
    eqApplyToDownmix = 0;
1043
364
  }
1044
508
  FDKpushFor(hBs, 6); /* drcSetId */
1045
508
  additionalDrcSetIdPresent = FDKreadBits(hBs, 1);
1046
508
  if (additionalDrcSetIdPresent) {
1047
117
    additionalDrcSetIdCount = FDKreadBits(hBs, 6);
1048
1.78k
    for (i = 0; i < additionalDrcSetIdCount; i++) {
1049
1.66k
      FDKpushFor(hBs, 6); /* additionalDrcSetId */
1050
1.66k
    }
1051
117
  }
1052
508
  FDKpushFor(hBs, 16); /* eqSetPurpose */
1053
508
  dependsOnEqSetPresent = FDKreadBits(hBs, 1);
1054
508
  if (dependsOnEqSetPresent) {
1055
117
    FDKpushFor(hBs, 6); /* dependsOnEqSet */
1056
391
  } else {
1057
391
    FDKpushFor(hBs, 1); /* noIndependentEqUse */
1058
391
  }
1059
1060
508
  channelCount = hUniDrcConfig->channelLayout.baseChannelCount;
1061
508
  if ((downmixIdPresent == 1) && (eqApplyToDownmix == 1) && (downmixId != 0) &&
1062
89
      (downmixId != DOWNMIX_ID_ANY_DOWNMIX) &&
1063
86
      (additionalDownmixIdCount == 0)) {
1064
56
    DOWNMIX_INSTRUCTIONS* pDown =
1065
56
        selectDownmixInstructions(hUniDrcConfig, downmixId);
1066
56
    if (pDown == NULL) return DE_NOT_OK;
1067
1068
6
    channelCount =
1069
6
        pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1070
452
  } else if ((downmixId == DOWNMIX_ID_ANY_DOWNMIX) ||
1071
445
             (additionalDownmixIdCount > 1)) {
1072
42
    channelCount = 1;
1073
42
  }
1074
1075
458
  eqChannelGroupCount = 0;
1076
1.99k
  for (c = 0; c < channelCount; c++) {
1077
1.53k
    int newGroup = 1;
1078
1.53k
    if (c >= 8) return DE_MEMORY_ERROR;
1079
1.53k
    eqChannelGroupForChannel[c] = FDKreadBits(hBs, 7);
1080
4.14k
    for (k = 0; k < c; k++) {
1081
2.61k
      if (eqChannelGroupForChannel[c] == eqChannelGroupForChannel[k]) {
1082
1.47k
        newGroup = 0;
1083
1.47k
      }
1084
2.61k
    }
1085
1.53k
    if (newGroup == 1) {
1086
878
      eqChannelGroupCount += 1;
1087
878
    }
1088
1.53k
  }
1089
458
  tdFilterCascadePresent = FDKreadBits(hBs, 1);
1090
458
  if (tdFilterCascadePresent) {
1091
111
    _skipTdFilterCascade(hBs, eqChannelGroupCount);
1092
111
  }
1093
458
  subbandGainsPresent = FDKreadBits(hBs, 1);
1094
458
  if (subbandGainsPresent) {
1095
62
    FDKpushFor(hBs, eqChannelGroupCount * 6); /* subbandGainsIndex */
1096
62
  }
1097
458
  eqTransitionDurationPresent = FDKreadBits(hBs, 1);
1098
458
  if (eqTransitionDurationPresent) {
1099
59
    FDKpushFor(hBs, 5); /* bsEqTransitionDuration */
1100
59
  }
1101
458
  return err;
1102
458
}
1103
1104
395
static void _skipDrcCoefficientsBasic(HANDLE_FDK_BITSTREAM hBs) {
1105
395
  FDKpushFor(hBs, 4); /* drcLocation */
1106
395
  FDKpushFor(hBs, 7); /* drcCharacteristic */
1107
395
}
1108
1109
static DRC_ERROR _readDrcCoefficientsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1110
                                            const int version,
1111
659
                                            DRC_COEFFICIENTS_UNI_DRC* pCoef) {
1112
659
  DRC_ERROR err = DE_OK;
1113
659
  int i, bsDrcFrameSize;
1114
659
  int gainSequenceIndex = -1;
1115
1116
659
  pCoef->drcLocation = FDKreadBits(hBs, 4);
1117
659
  pCoef->drcFrameSizePresent = FDKreadBits(hBs, 1);
1118
1119
659
  if (pCoef->drcFrameSizePresent == 1) {
1120
125
    bsDrcFrameSize = FDKreadBits(hBs, 15);
1121
125
    pCoef->drcFrameSize = bsDrcFrameSize + 1;
1122
125
  }
1123
659
  if (version == 0) {
1124
265
    int gainSequenceCount = 0, gainSetCount;
1125
265
    pCoef->characteristicLeftCount = 0;
1126
265
    pCoef->characteristicRightCount = 0;
1127
265
    gainSetCount = FDKreadBits(hBs, 6);
1128
265
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1129
1.43k
    for (i = 0; i < gainSetCount; i++) {
1130
1.19k
      GAIN_SET tmpGset;
1131
1.19k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1132
1.19k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1133
1.19k
      if (err) return err;
1134
1.17k
      gainSequenceCount += tmpGset.bandCount;
1135
1136
1.17k
      if (i >= 12) continue;
1137
566
      pCoef->gainSet[i] = tmpGset;
1138
566
    }
1139
240
    pCoef->gainSequenceCount = gainSequenceCount;
1140
394
  } else { /* (version == 1) */
1141
394
    UCHAR drcCharacteristicLeftPresent, drcCharacteristicRightPresent;
1142
394
    UCHAR shapeFiltersPresent, shapeFilterCount, tmpPresent;
1143
394
    int gainSetCount;
1144
394
    drcCharacteristicLeftPresent = FDKreadBits(hBs, 1);
1145
394
    if (drcCharacteristicLeftPresent) {
1146
36
      pCoef->characteristicLeftCount = FDKreadBits(hBs, 4);
1147
36
      if ((pCoef->characteristicLeftCount + 1) > 16) return DE_MEMORY_ERROR;
1148
292
      for (i = 0; i < pCoef->characteristicLeftCount; i++) {
1149
256
        err = _readCustomDrcCharacteristic(
1150
256
            hBs, CS_LEFT, &(pCoef->characteristicLeftFormat[i + 1]),
1151
256
            &(pCoef->customCharacteristicLeft[i + 1]), 0);
1152
256
        if (err) return err;
1153
256
      }
1154
36
    }
1155
394
    drcCharacteristicRightPresent = FDKreadBits(hBs, 1);
1156
394
    if (drcCharacteristicRightPresent) {
1157
38
      pCoef->characteristicRightCount = FDKreadBits(hBs, 4);
1158
38
      if ((pCoef->characteristicRightCount + 1) > 16) return DE_MEMORY_ERROR;
1159
252
      for (i = 0; i < pCoef->characteristicRightCount; i++) {
1160
214
        err = _readCustomDrcCharacteristic(
1161
214
            hBs, CS_RIGHT, &(pCoef->characteristicRightFormat[i + 1]),
1162
214
            &(pCoef->customCharacteristicRight[i + 1]), 0);
1163
214
        if (err) return err;
1164
214
      }
1165
38
    }
1166
394
    shapeFiltersPresent = FDKreadBits(hBs, 1);
1167
394
    if (shapeFiltersPresent) {
1168
70
      shapeFilterCount = FDKreadBits(hBs, 4);
1169
597
      for (i = 0; i < shapeFilterCount; i++) {
1170
527
        tmpPresent = FDKreadBits(hBs, 1);
1171
527
        if (tmpPresent) /* lfCutParams */
1172
164
          FDKpushFor(hBs, 5);
1173
1174
527
        tmpPresent = FDKreadBits(hBs, 1);
1175
527
        if (tmpPresent) /* lfBoostParams */
1176
145
          FDKpushFor(hBs, 5);
1177
1178
527
        tmpPresent = FDKreadBits(hBs, 1);
1179
527
        if (tmpPresent) /* hfCutParams */
1180
137
          FDKpushFor(hBs, 5);
1181
1182
527
        tmpPresent = FDKreadBits(hBs, 1);
1183
527
        if (tmpPresent) /* hfBoostParams */
1184
137
          FDKpushFor(hBs, 5);
1185
527
      }
1186
70
    }
1187
394
    pCoef->gainSequenceCount = FDKreadBits(hBs, 6);
1188
394
    gainSetCount = FDKreadBits(hBs, 6);
1189
394
    pCoef->gainSetCount = fMin(gainSetCount, 12);
1190
1.92k
    for (i = 0; i < gainSetCount; i++) {
1191
1.55k
      GAIN_SET tmpGset;
1192
1.55k
      FDKmemclear(&tmpGset, sizeof(GAIN_SET));
1193
1.55k
      err = _readGainSet(hBs, version, &gainSequenceIndex, &tmpGset, 0);
1194
1.55k
      if (err) return err;
1195
1196
1.53k
      if (i >= 12) continue;
1197
561
      pCoef->gainSet[i] = tmpGset;
1198
561
    }
1199
394
  }
1200
7.94k
  for (i = 0; i < 12; i++) {
1201
7.33k
    pCoef->gainSetIndexForGainSequence[i] = 255;
1202
7.33k
  }
1203
1.52k
  for (i = 0; i < pCoef->gainSetCount; i++) {
1204
912
    int b;
1205
1.65k
    for (b = 0; b < pCoef->gainSet[i].bandCount; b++) {
1206
742
      if (pCoef->gainSet[i].gainSequenceIndex[b] >= 12) continue;
1207
555
      pCoef->gainSetIndexForGainSequence[pCoef->gainSet[i]
1208
555
                                             .gainSequenceIndex[b]] = i;
1209
555
    }
1210
912
  }
1211
1212
611
  return err;
1213
659
}
1214
1215
577
static void _skipDrcInstructionsBasic(HANDLE_FDK_BITSTREAM hBs) {
1216
577
  int drcSetEffect;
1217
577
  int additionalDownmixIdPresent, additionalDownmixIdCount,
1218
577
      limiterPeakTargetPresent;
1219
577
  int drcSetTargetLoudnessPresent, drcSetTargetLoudnessValueLowerPresent;
1220
1221
577
  FDKpushFor(hBs, 6); /* drcSetId */
1222
577
  FDKpushFor(hBs, 4); /* drcLocation */
1223
577
  FDKpushFor(hBs, 7); /* downmixId */
1224
577
  additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1225
577
  if (additionalDownmixIdPresent) {
1226
140
    additionalDownmixIdCount = FDKreadBits(hBs, 3);
1227
140
    FDKpushFor(hBs, 7 * additionalDownmixIdCount); /* additionalDownmixId */
1228
140
  }
1229
1230
577
  drcSetEffect = FDKreadBits(hBs, 16);
1231
577
  if (!(drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF))) {
1232
427
    limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1233
427
    if (limiterPeakTargetPresent) {
1234
273
      FDKpushFor(hBs, 8); /* bsLimiterPeakTarget */
1235
273
    }
1236
427
  }
1237
1238
577
  drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1239
577
  if (drcSetTargetLoudnessPresent) {
1240
138
    FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueUpper */
1241
138
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1242
138
    if (drcSetTargetLoudnessValueLowerPresent) {
1243
74
      FDKpushFor(hBs, 6); /* bsDrcSetTargetLoudnessValueLower */
1244
74
    }
1245
138
  }
1246
577
}
1247
1248
static DRC_ERROR _readDrcInstructionsUniDrc(HANDLE_FDK_BITSTREAM hBs,
1249
                                            const int version,
1250
                                            HANDLE_UNI_DRC_CONFIG hUniDrcConfig,
1251
3.92k
                                            DRC_INSTRUCTIONS_UNI_DRC* pInst) {
1252
3.92k
  DRC_ERROR err = DE_OK;
1253
3.92k
  int i, g, c;
1254
3.92k
  int downmixIdPresent, additionalDownmixIdPresent, additionalDownmixIdCount;
1255
3.92k
  int bsLimiterPeakTarget, channelCount;
1256
3.92k
  DRC_COEFFICIENTS_UNI_DRC* pCoef = NULL;
1257
3.92k
  int repeatParameters, bsRepeatParametersCount;
1258
3.92k
  int repeatSequenceIndex, bsRepeatSequenceCount;
1259
3.92k
  SCHAR* gainSetIndex = pInst->gainSetIndex;
1260
3.92k
  SCHAR channelGroupForChannel[8];
1261
3.92k
  DUCKING_MODIFICATION duckingModificationForChannelGroup[8];
1262
1263
3.92k
  pInst->drcSetId = FDKreadBits(hBs, 6);
1264
3.92k
  if (version == 0) {
1265
    /* Assume all v0 DRC sets to be manageable in terms of complexity */
1266
3.27k
    pInst->drcSetComplexityLevel = 2;
1267
3.27k
  } else {
1268
650
    pInst->drcSetComplexityLevel = FDKreadBits(hBs, 4);
1269
650
  }
1270
3.92k
  pInst->drcLocation = FDKreadBits(hBs, 4);
1271
3.92k
  if (version == 0) {
1272
3.27k
    downmixIdPresent = 1;
1273
3.27k
  } else {
1274
650
    downmixIdPresent = FDKreadBits(hBs, 1);
1275
650
  }
1276
3.92k
  if (downmixIdPresent) {
1277
3.32k
    pInst->downmixId[0] = FDKreadBits(hBs, 7);
1278
3.32k
    if (version == 0) {
1279
3.27k
      if (pInst->downmixId[0] == 0)
1280
1.36k
        pInst->drcApplyToDownmix = 0;
1281
1.90k
      else
1282
1.90k
        pInst->drcApplyToDownmix = 1;
1283
3.27k
    } else {
1284
53
      pInst->drcApplyToDownmix = FDKreadBits(hBs, 1);
1285
53
    }
1286
1287
3.32k
    additionalDownmixIdPresent = FDKreadBits(hBs, 1);
1288
3.32k
    if (additionalDownmixIdPresent) {
1289
1.74k
      additionalDownmixIdCount = FDKreadBits(hBs, 3);
1290
1.74k
      if ((1 + additionalDownmixIdCount) > 8) return DE_MEMORY_ERROR;
1291
3.70k
      for (i = 0; i < additionalDownmixIdCount; i++) {
1292
1.96k
        pInst->downmixId[i + 1] = FDKreadBits(hBs, 7);
1293
1.96k
      }
1294
1.74k
      pInst->downmixIdCount = 1 + additionalDownmixIdCount;
1295
1.74k
    } else {
1296
1.58k
      pInst->downmixIdCount = 1;
1297
1.58k
    }
1298
3.32k
  } else {
1299
597
    pInst->downmixId[0] = 0;
1300
597
    pInst->downmixIdCount = 1;
1301
597
  }
1302
1303
3.92k
  pInst->drcSetEffect = FDKreadBits(hBs, 16);
1304
1305
3.92k
  if ((pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) == 0) {
1306
2.58k
    pInst->limiterPeakTargetPresent = FDKreadBits(hBs, 1);
1307
2.58k
    if (pInst->limiterPeakTargetPresent) {
1308
660
      bsLimiterPeakTarget = FDKreadBits(hBs, 8);
1309
660
      pInst->limiterPeakTarget = -(FIXP_SGL)(
1310
660
          bsLimiterPeakTarget
1311
660
          << (FRACT_BITS - 1 - 3 - 5)); /* - bsLimiterPeakTarget * 0.125; */
1312
660
    }
1313
2.58k
  }
1314
1315
3.92k
  pInst->drcSetTargetLoudnessPresent = FDKreadBits(hBs, 1);
1316
1317
  /* set default values */
1318
3.92k
  pInst->drcSetTargetLoudnessValueUpper = 0;
1319
3.92k
  pInst->drcSetTargetLoudnessValueLower = -63;
1320
1321
3.92k
  if (pInst->drcSetTargetLoudnessPresent == 1) {
1322
367
    int bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1323
367
    int drcSetTargetLoudnessValueLowerPresent;
1324
367
    bsDrcSetTargetLoudnessValueUpper = FDKreadBits(hBs, 6);
1325
367
    pInst->drcSetTargetLoudnessValueUpper =
1326
367
        bsDrcSetTargetLoudnessValueUpper - 63;
1327
367
    drcSetTargetLoudnessValueLowerPresent = FDKreadBits(hBs, 1);
1328
367
    if (drcSetTargetLoudnessValueLowerPresent == 1) {
1329
235
      bsDrcSetTargetLoudnessValueLower = FDKreadBits(hBs, 6);
1330
235
      pInst->drcSetTargetLoudnessValueLower =
1331
235
          bsDrcSetTargetLoudnessValueLower - 63;
1332
235
    }
1333
367
  }
1334
1335
3.92k
  pInst->dependsOnDrcSetPresent = FDKreadBits(hBs, 1);
1336
1337
3.92k
  pInst->noIndependentUse = 0;
1338
3.92k
  if (pInst->dependsOnDrcSetPresent) {
1339
213
    pInst->dependsOnDrcSet = FDKreadBits(hBs, 6);
1340
3.70k
  } else {
1341
3.70k
    pInst->noIndependentUse = FDKreadBits(hBs, 1);
1342
3.70k
  }
1343
1344
3.92k
  if (version == 0) {
1345
3.27k
    pInst->requiresEq = 0;
1346
3.27k
  } else {
1347
650
    pInst->requiresEq = FDKreadBits(hBs, 1);
1348
650
  }
1349
1350
3.92k
  pCoef = selectDrcCoefficients(hUniDrcConfig, pInst->drcLocation);
1351
1352
3.92k
  pInst->drcChannelCount = channelCount =
1353
3.92k
      hUniDrcConfig->channelLayout.baseChannelCount;
1354
1355
3.92k
  if (pInst->drcSetEffect & (EB_DUCK_OTHER | EB_DUCK_SELF)) {
1356
1.33k
    DUCKING_MODIFICATION* pDModForChannel =
1357
1.33k
        pInst->duckingModificationForChannel;
1358
1.33k
    c = 0;
1359
7.46k
    while (c < channelCount) {
1360
6.15k
      int bsGainSetIndex;
1361
6.15k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1362
6.15k
      if (c >= 8) return DE_MEMORY_ERROR;
1363
6.15k
      gainSetIndex[c] = bsGainSetIndex - 1;
1364
6.15k
      _decodeDuckingModification(hBs, &(pDModForChannel[c]), 0);
1365
1366
6.15k
      c++;
1367
6.15k
      repeatParameters = FDKreadBits(hBs, 1);
1368
6.15k
      if (repeatParameters == 1) {
1369
49
        bsRepeatParametersCount = FDKreadBits(hBs, 5);
1370
49
        bsRepeatParametersCount += 1;
1371
222
        for (i = 0; i < bsRepeatParametersCount; i++) {
1372
194
          if (c >= 8) return DE_MEMORY_ERROR;
1373
173
          gainSetIndex[c] = gainSetIndex[c - 1];
1374
173
          pDModForChannel[c] = pDModForChannel[c - 1];
1375
173
          c++;
1376
173
        }
1377
49
      }
1378
6.15k
    }
1379
1.31k
    if (c > channelCount) {
1380
3
      return DE_NOT_OK;
1381
3
    }
1382
1383
1.31k
    err = deriveDrcChannelGroups(
1384
1.31k
        pInst->drcSetEffect, pInst->drcChannelCount, gainSetIndex,
1385
1.31k
        pDModForChannel, &pInst->nDrcChannelGroups,
1386
1.31k
        pInst->gainSetIndexForChannelGroup, channelGroupForChannel,
1387
1.31k
        duckingModificationForChannelGroup);
1388
1.31k
    if (err) return (err);
1389
2.58k
  } else {
1390
2.58k
    int deriveChannelCount = 0;
1391
2.58k
    if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1392
1.99k
        (pInst->downmixId[0] != DOWNMIX_ID_BASE_LAYOUT) &&
1393
723
        (pInst->downmixId[0] != DOWNMIX_ID_ANY_DOWNMIX) &&
1394
714
        (pInst->downmixIdCount == 1)) {
1395
79
      if (hUniDrcConfig->downmixInstructionsCount != 0) {
1396
32
        DOWNMIX_INSTRUCTIONS* pDown =
1397
32
            selectDownmixInstructions(hUniDrcConfig, pInst->downmixId[0]);
1398
32
        if (pDown == NULL) return DE_NOT_OK;
1399
9
        pInst->drcChannelCount = channelCount =
1400
9
            pDown->targetChannelCount; /* targetChannelCountFromDownmixId*/
1401
47
      } else {
1402
47
        deriveChannelCount = 1;
1403
47
        channelCount = 1;
1404
47
      }
1405
2.50k
    } else if (((version == 0) || (pInst->drcApplyToDownmix != 0)) &&
1406
1.91k
               ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1407
1.90k
                (pInst->downmixIdCount > 1))) {
1408
      /* Set maximum channel count as upper border. The effective channel count
1409
       * is set at the process function. */
1410
646
      pInst->drcChannelCount = 8;
1411
646
      channelCount = 1;
1412
646
    }
1413
1414
2.56k
    c = 0;
1415
8.64k
    while (c < channelCount) {
1416
6.09k
      int bsGainSetIndex;
1417
6.09k
      bsGainSetIndex = FDKreadBits(hBs, 6);
1418
6.09k
      if (c >= 8) return DE_MEMORY_ERROR;
1419
6.09k
      gainSetIndex[c] = bsGainSetIndex - 1;
1420
6.09k
      c++;
1421
6.09k
      repeatSequenceIndex = FDKreadBits(hBs, 1);
1422
1423
6.09k
      if (repeatSequenceIndex == 1) {
1424
91
        bsRepeatSequenceCount = FDKreadBits(hBs, 5);
1425
91
        bsRepeatSequenceCount += 1;
1426
91
        if (deriveChannelCount) {
1427
6
          channelCount = 1 + bsRepeatSequenceCount;
1428
6
        }
1429
261
        for (i = 0; i < bsRepeatSequenceCount; i++) {
1430
183
          if (c >= 8) return DE_MEMORY_ERROR;
1431
170
          gainSetIndex[c] = bsGainSetIndex - 1;
1432
170
          c++;
1433
170
        }
1434
91
      }
1435
6.09k
    }
1436
2.54k
    if (c > channelCount) {
1437
4
      return DE_NOT_OK;
1438
4
    }
1439
2.54k
    if (deriveChannelCount) {
1440
46
      pInst->drcChannelCount = channelCount;
1441
46
    }
1442
1443
    /* DOWNMIX_ID_ANY_DOWNMIX: channelCount is 1. Distribute gainSetIndex to all
1444
     * channels. */
1445
2.54k
    if ((pInst->downmixId[0] == DOWNMIX_ID_ANY_DOWNMIX) ||
1446
2.53k
        (pInst->downmixIdCount > 1)) {
1447
5.14k
      for (c = 1; c < pInst->drcChannelCount; c++) {
1448
4.49k
        gainSetIndex[c] = gainSetIndex[0];
1449
4.49k
      }
1450
648
    }
1451
1452
2.54k
    err = deriveDrcChannelGroups(pInst->drcSetEffect, pInst->drcChannelCount,
1453
2.54k
                                 gainSetIndex, NULL, &pInst->nDrcChannelGroups,
1454
2.54k
                                 pInst->gainSetIndexForChannelGroup,
1455
2.54k
                                 channelGroupForChannel, NULL);
1456
2.54k
    if (err) return (err);
1457
1458
3.63k
    for (g = 0; g < pInst->nDrcChannelGroups; g++) {
1459
1.08k
      int set, bandCount;
1460
1.08k
      set = pInst->gainSetIndexForChannelGroup[g];
1461
1462
      /* get bandCount */
1463
1.08k
      if (pCoef != NULL && set < pCoef->gainSetCount) {
1464
9
        bandCount = pCoef->gainSet[set].bandCount;
1465
1.08k
      } else {
1466
1.08k
        bandCount = 1;
1467
1.08k
      }
1468
1469
1.08k
      _decodeGainModification(hBs, version, bandCount,
1470
1.08k
                              pInst->gainModificationForChannelGroup[g], 0);
1471
1.08k
    }
1472
2.54k
  }
1473
1474
3.83k
  return err;
1475
3.92k
}
1476
1477
static DRC_ERROR _readChannelLayout(HANDLE_FDK_BITSTREAM hBs,
1478
9.32k
                                    CHANNEL_LAYOUT* pChan) {
1479
9.32k
  DRC_ERROR err = DE_OK;
1480
1481
9.32k
  pChan->baseChannelCount = FDKreadBits(hBs, 7);
1482
1483
9.32k
  if (pChan->baseChannelCount > 8) return DE_NOT_OK;
1484
1485
9.23k
  pChan->layoutSignalingPresent = FDKreadBits(hBs, 1);
1486
1487
9.23k
  if (pChan->layoutSignalingPresent) {
1488
219
    pChan->definedLayout = FDKreadBits(hBs, 8);
1489
1490
219
    if (pChan->definedLayout == 0) {
1491
7
      int i;
1492
29
      for (i = 0; i < pChan->baseChannelCount; i++) {
1493
22
        if (i < 8) {
1494
22
          pChan->speakerPosition[i] = FDKreadBits(hBs, 7);
1495
22
        } else {
1496
0
          FDKpushFor(hBs, 7);
1497
0
        }
1498
22
      }
1499
7
    }
1500
219
  }
1501
9.23k
  return err;
1502
9.32k
}
1503
1504
static DRC_ERROR _readDownmixInstructions(HANDLE_FDK_BITSTREAM hBs,
1505
                                          const int version,
1506
                                          CHANNEL_LAYOUT* pChan,
1507
2.86k
                                          DOWNMIX_INSTRUCTIONS* pDown) {
1508
2.86k
  DRC_ERROR err = DE_OK;
1509
1510
2.86k
  pDown->downmixId = FDKreadBits(hBs, 7);
1511
2.86k
  pDown->targetChannelCount = FDKreadBits(hBs, 7);
1512
2.86k
  pDown->targetLayout = FDKreadBits(hBs, 8);
1513
2.86k
  pDown->downmixCoefficientsPresent = FDKreadBits(hBs, 1);
1514
1515
2.86k
  if (pDown->downmixCoefficientsPresent) {
1516
617
    int nDownmixCoeffs = pDown->targetChannelCount * pChan->baseChannelCount;
1517
617
    int i;
1518
617
    if (nDownmixCoeffs > 8 * 8) return DE_NOT_OK;
1519
606
    if (version == 0) {
1520
432
      pDown->bsDownmixOffset = 0;
1521
3.07k
      for (i = 0; i < nDownmixCoeffs; i++) {
1522
        /* LFE downmix coefficients are not supported. */
1523
2.64k
        pDown->downmixCoefficient[i] = downmixCoeff[FDKreadBits(hBs, 4)];
1524
2.64k
      }
1525
432
    } else {
1526
174
      pDown->bsDownmixOffset = FDKreadBits(hBs, 4);
1527
341
      for (i = 0; i < nDownmixCoeffs; i++) {
1528
167
        pDown->downmixCoefficient[i] = downmixCoeffV1[FDKreadBits(hBs, 5)];
1529
167
      }
1530
174
    }
1531
606
  }
1532
2.85k
  return err;
1533
2.86k
}
1534
1535
static DRC_ERROR _readDrcExtensionV1(HANDLE_FDK_BITSTREAM hBs,
1536
370
                                     HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1537
370
  DRC_ERROR err = DE_OK;
1538
370
  int downmixInstructionsV1Present;
1539
370
  int drcCoeffsAndInstructionsUniDrcV1Present;
1540
370
  int loudEqInstructionsPresent, loudEqInstructionsCount;
1541
370
  int eqPresent, eqInstructionsCount;
1542
370
  int i, offset;
1543
370
  int diff = hUniDrcConfig->diff;
1544
1545
370
  downmixInstructionsV1Present = FDKreadBits(hBs, 1);
1546
370
  if (downmixInstructionsV1Present == 1) {
1547
84
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1,
1548
84
                        FDKreadBits(hBs, 7));
1549
84
    offset = hUniDrcConfig->downmixInstructionsCountV0;
1550
84
    hUniDrcConfig->downmixInstructionsCount = fMin(
1551
84
        (UCHAR)(offset + hUniDrcConfig->downmixInstructionsCountV1), (UCHAR)6);
1552
1.10k
    for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV1; i++) {
1553
1.02k
      DOWNMIX_INSTRUCTIONS tmpDown;
1554
1.02k
      FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1555
1.02k
      err = _readDownmixInstructions(hBs, 1, &hUniDrcConfig->channelLayout,
1556
1.02k
                                     &tmpDown);
1557
1.02k
      if (err) return err;
1558
1.02k
      if ((offset + i) >= 6) continue;
1559
375
      if (!diff)
1560
0
        diff |= (FDKmemcmp(&tmpDown,
1561
0
                           &(hUniDrcConfig->downmixInstructions[offset + i]),
1562
0
                           sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1563
375
      hUniDrcConfig->downmixInstructions[offset + i] = tmpDown;
1564
375
    }
1565
286
  } else {
1566
286
    diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV1, 0);
1567
286
  }
1568
1569
366
  drcCoeffsAndInstructionsUniDrcV1Present = FDKreadBits(hBs, 1);
1570
366
  if (drcCoeffsAndInstructionsUniDrcV1Present == 1) {
1571
143
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1,
1572
143
                        FDKreadBits(hBs, 3));
1573
143
    offset = hUniDrcConfig->drcCoefficientsUniDrcCountV0;
1574
143
    hUniDrcConfig->drcCoefficientsUniDrcCount =
1575
143
        fMin((UCHAR)(offset + hUniDrcConfig->drcCoefficientsUniDrcCountV1),
1576
143
             (UCHAR)2);
1577
514
    for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV1; i++) {
1578
394
      DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1579
394
      FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1580
394
      err = _readDrcCoefficientsUniDrc(hBs, 1, &tmpCoef);
1581
394
      if (err) return err;
1582
371
      if ((offset + i) >= 2) continue;
1583
155
      if (!diff)
1584
0
        diff |= (FDKmemcmp(&tmpCoef,
1585
0
                           &(hUniDrcConfig->drcCoefficientsUniDrc[offset + i]),
1586
0
                           sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1587
155
      hUniDrcConfig->drcCoefficientsUniDrc[offset + i] = tmpCoef;
1588
155
    }
1589
1590
120
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1,
1591
120
                        FDKreadBits(hBs, 6));
1592
120
    offset = hUniDrcConfig->drcInstructionsUniDrcCount;
1593
120
    hUniDrcConfig->drcInstructionsUniDrcCount =
1594
120
        fMin((UCHAR)(offset + hUniDrcConfig->drcInstructionsUniDrcCountV1),
1595
120
             (UCHAR)12);
1596
743
    for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCount; i++) {
1597
650
      DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1598
650
      FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1599
650
      err = _readDrcInstructionsUniDrc(hBs, 1, hUniDrcConfig, &tmpInst);
1600
650
      if (err) return err;
1601
623
      if ((offset + i) >= 12) continue;
1602
228
      if (!diff)
1603
0
        diff |= (FDKmemcmp(&tmpInst,
1604
0
                           &(hUniDrcConfig->drcInstructionsUniDrc[offset + i]),
1605
0
                           sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1606
228
      hUniDrcConfig->drcInstructionsUniDrc[offset + i] = tmpInst;
1607
228
    }
1608
223
  } else {
1609
223
    diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV1, 0);
1610
223
    diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV1, 0);
1611
223
  }
1612
1613
316
  loudEqInstructionsPresent = FDKreadBits(hBs, 1);
1614
316
  if (loudEqInstructionsPresent == 1) {
1615
97
    loudEqInstructionsCount = FDKreadBits(hBs, 4);
1616
514
    for (i = 0; i < loudEqInstructionsCount; i++) {
1617
417
      _skipLoudEqInstructions(hBs);
1618
417
    }
1619
97
  }
1620
1621
316
  eqPresent = FDKreadBits(hBs, 1);
1622
316
  if (eqPresent == 1) {
1623
152
    _skipEqCoefficients(hBs);
1624
152
    eqInstructionsCount = FDKreadBits(hBs, 4);
1625
660
    for (i = 0; i < eqInstructionsCount; i++) {
1626
508
      _skipEqInstructions(hBs, hUniDrcConfig);
1627
508
    }
1628
152
  }
1629
1630
316
  hUniDrcConfig->diff = diff;
1631
1632
316
  return err;
1633
366
}
1634
1635
static DRC_ERROR _readUniDrcConfigExtension(
1636
388
    HANDLE_FDK_BITSTREAM hBs, HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1637
388
  DRC_ERROR err = DE_OK;
1638
388
  int k, bitSizeLen, extSizeBits, bitSize;
1639
388
  INT nBitsRemaining;
1640
388
  UNI_DRC_CONFIG_EXTENSION* pExt = &(hUniDrcConfig->uniDrcConfigExt);
1641
1642
388
  k = 0;
1643
388
  pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1644
1.24k
  while (pExt->uniDrcConfigExtType[k] != UNIDRCCONFEXT_TERM) {
1645
1.22k
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1646
1.22k
    bitSizeLen = FDKreadBits(hBs, 4);
1647
1.22k
    extSizeBits = bitSizeLen + 4;
1648
1649
1.22k
    bitSize = FDKreadBits(hBs, extSizeBits);
1650
1.22k
    pExt->extBitSize[k] = bitSize + 1;
1651
1.22k
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1652
1653
1.22k
    switch (pExt->uniDrcConfigExtType[k]) {
1654
370
      case UNIDRCCONFEXT_V1:
1655
370
        err = _readDrcExtensionV1(hBs, hUniDrcConfig);
1656
370
        if (err) return err;
1657
316
        if (nBitsRemaining !=
1658
316
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1659
313
          return DE_NOT_OK;
1660
3
        break;
1661
338
      case UNIDRCCONFEXT_PARAM_DRC:
1662
      /* add future extensions here */
1663
854
      default:
1664
854
        FDKpushFor(hBs, pExt->extBitSize[k]);
1665
854
        break;
1666
1.22k
    }
1667
857
    k++;
1668
857
    pExt->uniDrcConfigExtType[k] = FDKreadBits(hBs, 4);
1669
857
  }
1670
1671
20
  return err;
1672
388
}
1673
1674
DRC_ERROR
1675
drcDec_readUniDrcConfig(HANDLE_FDK_BITSTREAM hBs,
1676
9.32k
                        HANDLE_UNI_DRC_CONFIG hUniDrcConfig) {
1677
9.32k
  DRC_ERROR err = DE_OK;
1678
9.32k
  int i, diff = 0;
1679
9.32k
  int drcDescriptionBasicPresent, drcCoefficientsBasicCount,
1680
9.32k
      drcInstructionsBasicCount;
1681
9.32k
  CHANNEL_LAYOUT tmpChan;
1682
9.32k
  FDKmemclear(&tmpChan, sizeof(CHANNEL_LAYOUT));
1683
9.32k
  if (hUniDrcConfig == NULL) return DE_NOT_OK;
1684
1685
9.32k
  diff |= _compAssign(&hUniDrcConfig->sampleRatePresent, FDKreadBits(hBs, 1));
1686
1687
9.32k
  if (hUniDrcConfig->sampleRatePresent == 1) {
1688
257
    diff |=
1689
257
        _compAssign(&hUniDrcConfig->sampleRate, FDKreadBits(hBs, 18) + 1000);
1690
257
  }
1691
1692
9.32k
  diff |= _compAssign(&hUniDrcConfig->downmixInstructionsCountV0,
1693
9.32k
                      FDKreadBits(hBs, 7));
1694
1695
9.32k
  drcDescriptionBasicPresent = FDKreadBits(hBs, 1);
1696
9.32k
  if (drcDescriptionBasicPresent == 1) {
1697
286
    drcCoefficientsBasicCount = FDKreadBits(hBs, 3);
1698
286
    drcInstructionsBasicCount = FDKreadBits(hBs, 4);
1699
9.04k
  } else {
1700
9.04k
    drcCoefficientsBasicCount = 0;
1701
9.04k
    drcInstructionsBasicCount = 0;
1702
9.04k
  }
1703
1704
9.32k
  diff |= _compAssign(&hUniDrcConfig->drcCoefficientsUniDrcCountV0,
1705
9.32k
                      FDKreadBits(hBs, 3));
1706
9.32k
  diff |= _compAssign(&hUniDrcConfig->drcInstructionsUniDrcCountV0,
1707
9.32k
                      FDKreadBits(hBs, 6));
1708
1709
9.32k
  err = _readChannelLayout(hBs, &tmpChan);
1710
9.32k
  if (err) return err;
1711
1712
9.23k
  if (!diff)
1713
8.86k
    diff |= (FDKmemcmp(&tmpChan, &hUniDrcConfig->channelLayout,
1714
8.86k
                       sizeof(CHANNEL_LAYOUT)) != 0);
1715
9.23k
  hUniDrcConfig->channelLayout = tmpChan;
1716
1717
9.23k
  hUniDrcConfig->downmixInstructionsCount =
1718
9.23k
      fMin(hUniDrcConfig->downmixInstructionsCountV0, (UCHAR)6);
1719
11.0k
  for (i = 0; i < hUniDrcConfig->downmixInstructionsCountV0; i++) {
1720
1.84k
    DOWNMIX_INSTRUCTIONS tmpDown;
1721
1.84k
    FDKmemclear(&tmpDown, sizeof(DOWNMIX_INSTRUCTIONS));
1722
1.84k
    err = _readDownmixInstructions(hBs, 0, &hUniDrcConfig->channelLayout,
1723
1.84k
                                   &tmpDown);
1724
1.84k
    if (err) return err;
1725
1.83k
    if (i >= 6) continue;
1726
705
    if (!diff)
1727
30
      diff |= (FDKmemcmp(&tmpDown, &(hUniDrcConfig->downmixInstructions[i]),
1728
30
                         sizeof(DOWNMIX_INSTRUCTIONS)) != 0);
1729
705
    hUniDrcConfig->downmixInstructions[i] = tmpDown;
1730
705
  }
1731
1732
9.62k
  for (i = 0; i < drcCoefficientsBasicCount; i++) {
1733
395
    _skipDrcCoefficientsBasic(hBs);
1734
395
  }
1735
9.80k
  for (i = 0; i < drcInstructionsBasicCount; i++) {
1736
577
    _skipDrcInstructionsBasic(hBs);
1737
577
  }
1738
1739
9.22k
  hUniDrcConfig->drcCoefficientsUniDrcCount =
1740
9.22k
      fMin(hUniDrcConfig->drcCoefficientsUniDrcCountV0, (UCHAR)2);
1741
9.46k
  for (i = 0; i < hUniDrcConfig->drcCoefficientsUniDrcCountV0; i++) {
1742
265
    DRC_COEFFICIENTS_UNI_DRC tmpCoef;
1743
265
    FDKmemclear(&tmpCoef, sizeof(DRC_COEFFICIENTS_UNI_DRC));
1744
265
    err = _readDrcCoefficientsUniDrc(hBs, 0, &tmpCoef);
1745
265
    if (err) return err;
1746
240
    if (i >= 2) continue;
1747
117
    if (!diff)
1748
5
      diff |= (FDKmemcmp(&tmpCoef, &(hUniDrcConfig->drcCoefficientsUniDrc[i]),
1749
5
                         sizeof(DRC_COEFFICIENTS_UNI_DRC)) != 0);
1750
117
    hUniDrcConfig->drcCoefficientsUniDrc[i] = tmpCoef;
1751
117
  }
1752
1753
9.20k
  hUniDrcConfig->drcInstructionsUniDrcCount =
1754
9.20k
      fMin(hUniDrcConfig->drcInstructionsUniDrcCountV0, (UCHAR)12);
1755
12.4k
  for (i = 0; i < hUniDrcConfig->drcInstructionsUniDrcCountV0; i++) {
1756
3.27k
    DRC_INSTRUCTIONS_UNI_DRC tmpInst;
1757
3.27k
    FDKmemclear(&tmpInst, sizeof(DRC_INSTRUCTIONS_UNI_DRC));
1758
3.27k
    err = _readDrcInstructionsUniDrc(hBs, 0, hUniDrcConfig, &tmpInst);
1759
3.27k
    if (err) return err;
1760
3.20k
    if (i >= 12) continue;
1761
2.35k
    if (!diff)
1762
1
      diff |= (FDKmemcmp(&tmpInst, &(hUniDrcConfig->drcInstructionsUniDrc[i]),
1763
1
                         sizeof(DRC_INSTRUCTIONS_UNI_DRC)) != 0);
1764
2.35k
    hUniDrcConfig->drcInstructionsUniDrc[i] = tmpInst;
1765
2.35k
  }
1766
1767
9.13k
  diff |=
1768
9.13k
      _compAssign(&hUniDrcConfig->uniDrcConfigExtPresent, FDKreadBits(hBs, 1));
1769
9.13k
  hUniDrcConfig->diff = diff;
1770
1771
9.13k
  if (hUniDrcConfig->uniDrcConfigExtPresent == 1) {
1772
388
    err = _readUniDrcConfigExtension(hBs, hUniDrcConfig);
1773
388
    if (err) return err;
1774
388
  }
1775
1776
8.77k
  return err;
1777
9.13k
}
1778
1779
/*******************/
1780
/* loudnessInfoSet */
1781
/*******************/
1782
1783
static DRC_ERROR _decodeMethodValue(HANDLE_FDK_BITSTREAM hBs,
1784
                                    const UCHAR methodDefinition,
1785
1.46k
                                    FIXP_DBL* methodValue, INT isBox) {
1786
1.46k
  int tmp;
1787
1.46k
  FIXP_DBL val;
1788
1.46k
  switch (methodDefinition) {
1789
669
    case MD_UNKNOWN_OTHER:
1790
757
    case MD_PROGRAM_LOUDNESS:
1791
874
    case MD_ANCHOR_LOUDNESS:
1792
930
    case MD_MAX_OF_LOUDNESS_RANGE:
1793
1.00k
    case MD_MOMENTARY_LOUDNESS_MAX:
1794
1.06k
    case MD_SHORT_TERM_LOUDNESS_MAX:
1795
1.06k
      tmp = FDKreadBits(hBs, 8);
1796
1.06k
      val = FL2FXCONST_DBL(-57.75f / (float)(1 << 7)) +
1797
1.06k
            (FIXP_DBL)(
1798
1.06k
                tmp << (DFRACT_BITS - 1 - 2 - 7)); /* -57.75 + tmp * 0.25; */
1799
1.06k
      break;
1800
102
    case MD_LOUDNESS_RANGE:
1801
102
      tmp = FDKreadBits(hBs, 8);
1802
102
      if (tmp == 0)
1803
13
        val = (FIXP_DBL)0;
1804
89
      else if (tmp <= 128)
1805
75
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 2 - 7)); /* tmp * 0.25; */
1806
14
      else if (tmp <= 204) {
1807
11
        val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 1 - 7)) -
1808
11
              FL2FXCONST_DBL(32.0f / (float)(1 << 7)); /* 0.5 * tmp - 32.0f; */
1809
11
      } 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
102
      break;
1816
65
    case MD_MIXING_LEVEL:
1817
65
      tmp = FDKreadBits(hBs, isBox ? 8 : 5);
1818
65
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)) +
1819
65
            FL2FXCONST_DBL(80.0f / (float)(1 << 7)); /* tmp + 80.0; */
1820
65
      break;
1821
133
    case MD_ROOM_TYPE:
1822
133
      tmp = FDKreadBits(hBs, isBox ? 8 : 2);
1823
133
      val = (FIXP_DBL)(tmp << (DFRACT_BITS - 1 - 7)); /* tmp; */
1824
133
      break;
1825
36
    case MD_SHORT_TERM_LOUDNESS:
1826
36
      tmp = FDKreadBits(hBs, 8);
1827
36
      val = FL2FXCONST_DBL(-116.0f / (float)(1 << 7)) +
1828
36
            (FIXP_DBL)(
1829
36
                tmp << (DFRACT_BITS - 1 - 1 - 7)); /* -116.0 + tmp * 0.5; */
1830
36
      break;
1831
65
    default:
1832
65
      return DE_NOT_OK; /* invalid methodDefinition value */
1833
1.46k
  }
1834
1.40k
  *methodValue = val;
1835
1.40k
  return DE_OK;
1836
1.46k
}
1837
1838
static DRC_ERROR _readLoudnessMeasurement(HANDLE_FDK_BITSTREAM hBs,
1839
1.46k
                                          LOUDNESS_MEASUREMENT* pMeas) {
1840
1.46k
  DRC_ERROR err = DE_OK;
1841
1842
1.46k
  pMeas->methodDefinition = FDKreadBits(hBs, 4);
1843
1.46k
  err =
1844
1.46k
      _decodeMethodValue(hBs, pMeas->methodDefinition, &pMeas->methodValue, 0);
1845
1.46k
  if (err) return err;
1846
1.40k
  pMeas->measurementSystem = FDKreadBits(hBs, 4);
1847
1.40k
  pMeas->reliability = FDKreadBits(hBs, 2);
1848
1849
1.40k
  return err;
1850
1.46k
}
1851
1852
static DRC_ERROR _readLoudnessInfo(HANDLE_FDK_BITSTREAM hBs, const int version,
1853
1.66k
                                   LOUDNESS_INFO* loudnessInfo) {
1854
1.66k
  DRC_ERROR err = DE_OK;
1855
1.66k
  int bsSamplePeakLevel, bsTruePeakLevel, i;
1856
1.66k
  int measurementCount;
1857
1858
1.66k
  loudnessInfo->drcSetId = FDKreadBits(hBs, 6);
1859
1.66k
  if (version >= 1) {
1860
487
    loudnessInfo->eqSetId = FDKreadBits(hBs, 6);
1861
1.17k
  } else {
1862
1.17k
    loudnessInfo->eqSetId = 0;
1863
1.17k
  }
1864
1.66k
  loudnessInfo->downmixId = FDKreadBits(hBs, 7);
1865
1866
1.66k
  loudnessInfo->samplePeakLevelPresent = FDKreadBits(hBs, 1);
1867
1.66k
  if (loudnessInfo->samplePeakLevelPresent) {
1868
306
    bsSamplePeakLevel = FDKreadBits(hBs, 12);
1869
306
    if (bsSamplePeakLevel == 0) {
1870
24
      loudnessInfo->samplePeakLevelPresent = 0;
1871
24
      loudnessInfo->samplePeakLevel = (FIXP_DBL)0;
1872
282
    } else { /* 20.0 - bsSamplePeakLevel * 0.03125; */
1873
282
      loudnessInfo->samplePeakLevel =
1874
282
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1875
282
          (FIXP_DBL)(bsSamplePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1876
282
    }
1877
306
  }
1878
1879
1.66k
  loudnessInfo->truePeakLevelPresent = FDKreadBits(hBs, 1);
1880
1.66k
  if (loudnessInfo->truePeakLevelPresent) {
1881
280
    bsTruePeakLevel = FDKreadBits(hBs, 12);
1882
280
    if (bsTruePeakLevel == 0) {
1883
24
      loudnessInfo->truePeakLevelPresent = 0;
1884
24
      loudnessInfo->truePeakLevel = (FIXP_DBL)0;
1885
256
    } else {
1886
256
      loudnessInfo->truePeakLevel =
1887
256
          FL2FXCONST_DBL(20.0f / (float)(1 << 7)) -
1888
256
          (FIXP_DBL)(bsTruePeakLevel << (DFRACT_BITS - 1 - 5 - 7));
1889
256
    }
1890
280
    loudnessInfo->truePeakLevelMeasurementSystem = FDKreadBits(hBs, 4);
1891
280
    loudnessInfo->truePeakLevelReliability = FDKreadBits(hBs, 2);
1892
280
  }
1893
1894
1.66k
  measurementCount = FDKreadBits(hBs, 4);
1895
1.66k
  loudnessInfo->measurementCount = fMin(measurementCount, 8);
1896
3.06k
  for (i = 0; i < measurementCount; i++) {
1897
1.46k
    LOUDNESS_MEASUREMENT tmpMeas;
1898
1.46k
    FDKmemclear(&tmpMeas, sizeof(LOUDNESS_MEASUREMENT));
1899
1.46k
    err = _readLoudnessMeasurement(hBs, &tmpMeas);
1900
1.46k
    if (err) return err;
1901
1.40k
    if (i >= 8) continue;
1902
1.25k
    loudnessInfo->loudnessMeasurement[i] = tmpMeas;
1903
1.25k
  }
1904
1905
1.60k
  return err;
1906
1.66k
}
1907
1908
static DRC_ERROR _readLoudnessInfoSetExtEq(
1909
28
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1910
28
  DRC_ERROR err = DE_OK;
1911
28
  int i, offset;
1912
28
  int diff = hLoudnessInfoSet->diff;
1913
1914
28
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV1,
1915
28
                      FDKreadBits(hBs, 6));
1916
28
  diff |=
1917
28
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV1, FDKreadBits(hBs, 6));
1918
1919
28
  offset = hLoudnessInfoSet->loudnessInfoAlbumCountV0;
1920
28
  hLoudnessInfoSet->loudnessInfoAlbumCount = fMin(
1921
28
      (UCHAR)(offset + hLoudnessInfoSet->loudnessInfoAlbumCountV1), (UCHAR)12);
1922
236
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV1; i++) {
1923
218
    LOUDNESS_INFO tmpLoud;
1924
218
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1925
218
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1926
218
    if (err) return err;
1927
208
    if ((offset + i) >= 12) continue;
1928
122
    if (!diff)
1929
0
      diff |= (FDKmemcmp(&tmpLoud,
1930
0
                         &(hLoudnessInfoSet->loudnessInfoAlbum[offset + i]),
1931
0
                         sizeof(LOUDNESS_INFO)) != 0);
1932
122
    hLoudnessInfoSet->loudnessInfoAlbum[offset + i] = tmpLoud;
1933
122
  }
1934
1935
18
  offset = hLoudnessInfoSet->loudnessInfoCountV0;
1936
18
  hLoudnessInfoSet->loudnessInfoCount =
1937
18
      fMin((UCHAR)(offset + hLoudnessInfoSet->loudnessInfoCountV1), (UCHAR)12);
1938
280
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV1; i++) {
1939
269
    LOUDNESS_INFO tmpLoud;
1940
269
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
1941
269
    err = _readLoudnessInfo(hBs, 1, &tmpLoud);
1942
269
    if (err) return err;
1943
262
    if ((offset + i) >= 12) continue;
1944
107
    if (!diff)
1945
0
      diff |=
1946
0
          (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[offset + i]),
1947
0
                     sizeof(LOUDNESS_INFO)) != 0);
1948
107
    hLoudnessInfoSet->loudnessInfo[offset + i] = tmpLoud;
1949
107
  }
1950
11
  hLoudnessInfoSet->diff = diff;
1951
11
  return err;
1952
18
}
1953
1954
static DRC_ERROR _readLoudnessInfoSetExtension(
1955
43
    HANDLE_FDK_BITSTREAM hBs, HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1956
43
  DRC_ERROR err = DE_OK;
1957
43
  int k, bitSizeLen, extSizeBits, bitSize;
1958
43
  INT nBitsRemaining;
1959
43
  LOUDNESS_INFO_SET_EXTENSION* pExt = &(hLoudnessInfoSet->loudnessInfoSetExt);
1960
1961
43
  k = 0;
1962
43
  pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1963
119
  while (pExt->loudnessInfoSetExtType[k] != UNIDRCLOUDEXT_TERM) {
1964
105
    if (k >= (8 - 1)) return DE_MEMORY_ERROR;
1965
104
    bitSizeLen = FDKreadBits(hBs, 4);
1966
104
    extSizeBits = bitSizeLen + 4;
1967
1968
104
    bitSize = FDKreadBits(hBs, extSizeBits);
1969
104
    pExt->extBitSize[k] = bitSize + 1;
1970
104
    nBitsRemaining = (INT)FDKgetValidBits(hBs);
1971
1972
104
    switch (pExt->loudnessInfoSetExtType[k]) {
1973
28
      case UNIDRCLOUDEXT_EQ:
1974
28
        err = _readLoudnessInfoSetExtEq(hBs, hLoudnessInfoSet);
1975
28
        if (err) return err;
1976
11
        if (nBitsRemaining !=
1977
11
            ((INT)pExt->extBitSize[k] + (INT)FDKgetValidBits(hBs)))
1978
11
          return DE_NOT_OK;
1979
0
        break;
1980
      /* add future extensions here */
1981
76
      default:
1982
76
        FDKpushFor(hBs, pExt->extBitSize[k]);
1983
76
        break;
1984
104
    }
1985
76
    k++;
1986
76
    pExt->loudnessInfoSetExtType[k] = FDKreadBits(hBs, 4);
1987
76
  }
1988
1989
14
  return err;
1990
43
}
1991
1992
/* Parser for loundessInfoSet() */
1993
DRC_ERROR
1994
drcDec_readLoudnessInfoSet(HANDLE_FDK_BITSTREAM hBs,
1995
8.68k
                           HANDLE_LOUDNESS_INFO_SET hLoudnessInfoSet) {
1996
8.68k
  DRC_ERROR err = DE_OK;
1997
8.68k
  int i, diff = 0;
1998
8.68k
  if (hLoudnessInfoSet == NULL) return DE_NOT_OK;
1999
2000
8.68k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoAlbumCountV0,
2001
8.68k
                      FDKreadBits(hBs, 6));
2002
8.68k
  diff |=
2003
8.68k
      _compAssign(&hLoudnessInfoSet->loudnessInfoCountV0, FDKreadBits(hBs, 6));
2004
2005
8.68k
  hLoudnessInfoSet->loudnessInfoAlbumCount =
2006
8.68k
      fMin(hLoudnessInfoSet->loudnessInfoAlbumCountV0, (UCHAR)12);
2007
9.17k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoAlbumCountV0; i++) {
2008
520
    LOUDNESS_INFO tmpLoud;
2009
520
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2010
520
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2011
520
    if (err) return err;
2012
495
    if (i >= 12) continue;
2013
363
    if (!diff)
2014
11
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfoAlbum[i]),
2015
11
                         sizeof(LOUDNESS_INFO)) != 0);
2016
363
    hLoudnessInfoSet->loudnessInfoAlbum[i] = tmpLoud;
2017
363
  }
2018
2019
8.65k
  hLoudnessInfoSet->loudnessInfoCount =
2020
8.65k
      fMin(hLoudnessInfoSet->loudnessInfoCountV0, (UCHAR)12);
2021
9.29k
  for (i = 0; i < hLoudnessInfoSet->loudnessInfoCountV0; i++) {
2022
658
    LOUDNESS_INFO tmpLoud;
2023
658
    FDKmemclear(&tmpLoud, sizeof(LOUDNESS_INFO));
2024
658
    err = _readLoudnessInfo(hBs, 0, &tmpLoud);
2025
658
    if (err) return err;
2026
635
    if (i >= 12) continue;
2027
452
    if (!diff)
2028
41
      diff |= (FDKmemcmp(&tmpLoud, &(hLoudnessInfoSet->loudnessInfo[i]),
2029
41
                         sizeof(LOUDNESS_INFO)) != 0);
2030
452
    hLoudnessInfoSet->loudnessInfo[i] = tmpLoud;
2031
452
  }
2032
2033
8.63k
  diff |= _compAssign(&hLoudnessInfoSet->loudnessInfoSetExtPresent,
2034
8.63k
                      FDKreadBits(hBs, 1));
2035
8.63k
  hLoudnessInfoSet->diff = diff;
2036
2037
8.63k
  if (hLoudnessInfoSet->loudnessInfoSetExtPresent) {
2038
43
    err = _readLoudnessInfoSetExtension(hBs, hLoudnessInfoSet);
2039
43
    if (err) return err;
2040
43
  }
2041
2042
8.60k
  return err;
2043
8.63k
}