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