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