/src/aac/libSBRdec/src/psbitdec.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2018 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 | | /**************************** SBR decoder library ****************************** |
96 | | |
97 | | Author(s): |
98 | | |
99 | | Description: |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "psbitdec.h" |
104 | | |
105 | | #include "sbr_rom.h" |
106 | | #include "huff_dec.h" |
107 | | |
108 | | /* PS dec privat functions */ |
109 | | SBR_ERROR ResetPsDec(HANDLE_PS_DEC h_ps_d); |
110 | | |
111 | | /***************************************************************************/ |
112 | | /*! |
113 | | \brief huffman decoding by codebook table |
114 | | |
115 | | \return index of huffman codebook table |
116 | | |
117 | | ****************************************************************************/ |
118 | | static SCHAR decode_huff_cw( |
119 | | Huffman h, /*!< pointer to huffman codebook table */ |
120 | | HANDLE_FDK_BITSTREAM hBitBuf, /*!< Handle to Bitbuffer */ |
121 | | int *length) /*!< length of huffman codeword (or NULL) */ |
122 | 0 | { |
123 | 0 | UCHAR bit = 0; |
124 | 0 | SCHAR index = 0; |
125 | 0 | UCHAR bitCount = 0; |
126 | |
|
127 | 0 | while (index >= 0) { |
128 | 0 | bit = FDKreadBits(hBitBuf, 1); |
129 | 0 | bitCount++; |
130 | 0 | index = h[index][bit]; |
131 | 0 | } |
132 | 0 | if (length) { |
133 | 0 | *length = bitCount; |
134 | 0 | } |
135 | 0 | return (index + 64); /* Add offset */ |
136 | 0 | } |
137 | | |
138 | | /***************************************************************************/ |
139 | | /*! |
140 | | \brief helper function - limiting of value to min/max values |
141 | | |
142 | | \return limited value |
143 | | |
144 | | ****************************************************************************/ |
145 | | |
146 | 0 | static SCHAR limitMinMax(SCHAR i, SCHAR min, SCHAR max) { |
147 | 0 | if (i < min) |
148 | 0 | return min; |
149 | 0 | else if (i > max) |
150 | 0 | return max; |
151 | 0 | else |
152 | 0 | return i; |
153 | 0 | } |
154 | | |
155 | | /***************************************************************************/ |
156 | | /*! |
157 | | \brief Decodes delta values in-place and updates |
158 | | data buffers according to quantization classes. |
159 | | |
160 | | When delta coded in frequency the first element is deltacode from zero. |
161 | | aIndex buffer is decoded from delta values to actual values. |
162 | | |
163 | | \return none |
164 | | |
165 | | ****************************************************************************/ |
166 | | static void deltaDecodeArray( |
167 | | SCHAR enable, SCHAR *aIndex, /*!< ICC/IID parameters */ |
168 | | SCHAR *aPrevFrameIndex, /*!< ICC/IID parameters of previous frame */ |
169 | | SCHAR DtDf, UCHAR nrElements, /*!< as conveyed in bitstream */ |
170 | | /*!< output array size: nrElements*stride */ |
171 | | UCHAR stride, /*!< 1=dflt, 2=half freq. resolution */ |
172 | 0 | SCHAR minIdx, SCHAR maxIdx) { |
173 | 0 | int i; |
174 | | |
175 | | /* Delta decode */ |
176 | 0 | if (enable == 1) { |
177 | 0 | if (DtDf == 0) { /* Delta coded in freq */ |
178 | 0 | aIndex[0] = 0 + aIndex[0]; |
179 | 0 | aIndex[0] = limitMinMax(aIndex[0], minIdx, maxIdx); |
180 | 0 | for (i = 1; i < nrElements; i++) { |
181 | 0 | aIndex[i] = aIndex[i - 1] + aIndex[i]; |
182 | 0 | aIndex[i] = limitMinMax(aIndex[i], minIdx, maxIdx); |
183 | 0 | } |
184 | 0 | } else { /* Delta time */ |
185 | 0 | for (i = 0; i < nrElements; i++) { |
186 | 0 | aIndex[i] = aPrevFrameIndex[i * stride] + aIndex[i]; |
187 | 0 | aIndex[i] = limitMinMax(aIndex[i], minIdx, maxIdx); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | } else { /* No data is sent, set index to zero */ |
191 | 0 | for (i = 0; i < nrElements; i++) { |
192 | 0 | aIndex[i] = 0; |
193 | 0 | } |
194 | 0 | } |
195 | 0 | if (stride == 2) { |
196 | 0 | for (i = nrElements * stride - 1; i > 0; i--) { |
197 | 0 | aIndex[i] = aIndex[i >> 1]; |
198 | 0 | } |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | /***************************************************************************/ |
203 | | /*! |
204 | | \brief Mapping of ICC/IID parameters to 20 stereo bands |
205 | | |
206 | | \return none |
207 | | |
208 | | ****************************************************************************/ |
209 | | static void map34IndexTo20(SCHAR *aIndex, /*!< decoded ICC/IID parameters */ |
210 | | UCHAR noBins) /*!< number of stereo bands */ |
211 | 0 | { |
212 | 0 | aIndex[0] = (2 * aIndex[0] + aIndex[1]) / 3; |
213 | 0 | aIndex[1] = (aIndex[1] + 2 * aIndex[2]) / 3; |
214 | 0 | aIndex[2] = (2 * aIndex[3] + aIndex[4]) / 3; |
215 | 0 | aIndex[3] = (aIndex[4] + 2 * aIndex[5]) / 3; |
216 | 0 | aIndex[4] = (aIndex[6] + aIndex[7]) / 2; |
217 | 0 | aIndex[5] = (aIndex[8] + aIndex[9]) / 2; |
218 | 0 | aIndex[6] = aIndex[10]; |
219 | 0 | aIndex[7] = aIndex[11]; |
220 | 0 | aIndex[8] = (aIndex[12] + aIndex[13]) / 2; |
221 | 0 | aIndex[9] = (aIndex[14] + aIndex[15]) / 2; |
222 | 0 | aIndex[10] = aIndex[16]; |
223 | | /* For IPD/OPD it stops here */ |
224 | |
|
225 | 0 | if (noBins == NO_HI_RES_BINS) { |
226 | 0 | aIndex[11] = aIndex[17]; |
227 | 0 | aIndex[12] = aIndex[18]; |
228 | 0 | aIndex[13] = aIndex[19]; |
229 | 0 | aIndex[14] = (aIndex[20] + aIndex[21]) / 2; |
230 | 0 | aIndex[15] = (aIndex[22] + aIndex[23]) / 2; |
231 | 0 | aIndex[16] = (aIndex[24] + aIndex[25]) / 2; |
232 | 0 | aIndex[17] = (aIndex[26] + aIndex[27]) / 2; |
233 | 0 | aIndex[18] = (aIndex[28] + aIndex[29] + aIndex[30] + aIndex[31]) / 4; |
234 | 0 | aIndex[19] = (aIndex[32] + aIndex[33]) / 2; |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | /***************************************************************************/ |
239 | | /*! |
240 | | \brief Decodes delta coded IID, ICC, IPD and OPD indices |
241 | | |
242 | | \return PS processing flag. If set to 1 |
243 | | |
244 | | ****************************************************************************/ |
245 | | int DecodePs(struct PS_DEC *h_ps_d, /*!< PS handle */ |
246 | | const UCHAR frameError, /*!< Flag telling that frame had errors */ |
247 | 0 | PS_DEC_COEFFICIENTS *pScratch) { |
248 | 0 | MPEG_PS_BS_DATA *pBsData; |
249 | 0 | UCHAR gr, env; |
250 | 0 | int bPsHeaderValid, bPsDataAvail; |
251 | | |
252 | | /* Assign Scratch */ |
253 | 0 | h_ps_d->specificTo.mpeg.pCoef = pScratch; |
254 | | |
255 | | /* Shortcuts to avoid deferencing and keep the code readable */ |
256 | 0 | pBsData = &h_ps_d->bsData[h_ps_d->processSlot].mpeg; |
257 | 0 | bPsHeaderValid = pBsData->bPsHeaderValid; |
258 | 0 | bPsDataAvail = |
259 | 0 | (h_ps_d->bPsDataAvail[h_ps_d->processSlot] == ppt_mpeg) ? 1 : 0; |
260 | | |
261 | | /*************************************************************************************** |
262 | | * Decide whether to process or to conceal PS data or not. */ |
263 | |
|
264 | 0 | if ((h_ps_d->psDecodedPrv && !frameError && !bPsDataAvail) || |
265 | 0 | (!h_ps_d->psDecodedPrv && |
266 | 0 | (frameError || !bPsDataAvail || !bPsHeaderValid))) { |
267 | | /* Don't apply PS processing. |
268 | | * Declare current PS header and bitstream data invalid. */ |
269 | 0 | pBsData->bPsHeaderValid = 0; |
270 | 0 | h_ps_d->bPsDataAvail[h_ps_d->processSlot] = ppt_none; |
271 | 0 | return (0); |
272 | 0 | } |
273 | | |
274 | 0 | if (frameError || |
275 | 0 | !bPsHeaderValid) { /* no new PS data available (e.g. frame loss) */ |
276 | | /* => keep latest data constant (i.e. FIX with noEnv=0) */ |
277 | 0 | pBsData->noEnv = 0; |
278 | 0 | } |
279 | | |
280 | | /*************************************************************************************** |
281 | | * Decode bitstream payload or prepare parameter for concealment: |
282 | | */ |
283 | 0 | for (env = 0; env < pBsData->noEnv; env++) { |
284 | 0 | SCHAR *aPrevIidIndex; |
285 | 0 | SCHAR *aPrevIccIndex; |
286 | |
|
287 | 0 | UCHAR noIidSteps = pBsData->bFineIidQ ? NO_IID_STEPS_FINE : NO_IID_STEPS; |
288 | |
|
289 | 0 | if (env == 0) { |
290 | 0 | aPrevIidIndex = h_ps_d->specificTo.mpeg.aIidPrevFrameIndex; |
291 | 0 | aPrevIccIndex = h_ps_d->specificTo.mpeg.aIccPrevFrameIndex; |
292 | 0 | } else { |
293 | 0 | aPrevIidIndex = pBsData->aaIidIndex[env - 1]; |
294 | 0 | aPrevIccIndex = pBsData->aaIccIndex[env - 1]; |
295 | 0 | } |
296 | |
|
297 | 0 | deltaDecodeArray(pBsData->bEnableIid, pBsData->aaIidIndex[env], |
298 | 0 | aPrevIidIndex, pBsData->abIidDtFlag[env], |
299 | 0 | FDK_sbrDecoder_aNoIidBins[pBsData->freqResIid], |
300 | 0 | (pBsData->freqResIid) ? 1 : 2, -noIidSteps, noIidSteps); |
301 | |
|
302 | 0 | deltaDecodeArray(pBsData->bEnableIcc, pBsData->aaIccIndex[env], |
303 | 0 | aPrevIccIndex, pBsData->abIccDtFlag[env], |
304 | 0 | FDK_sbrDecoder_aNoIccBins[pBsData->freqResIcc], |
305 | 0 | (pBsData->freqResIcc) ? 1 : 2, 0, NO_ICC_STEPS - 1); |
306 | 0 | } /* for (env=0; env<pBsData->noEnv; env++) */ |
307 | | |
308 | | /* handling of FIX noEnv=0 */ |
309 | 0 | if (pBsData->noEnv == 0) { |
310 | | /* set noEnv=1, keep last parameters or force 0 if not enabled */ |
311 | 0 | pBsData->noEnv = 1; |
312 | |
|
313 | 0 | if (pBsData->bEnableIid) { |
314 | 0 | pBsData->bFineIidQ = h_ps_d->specificTo.mpeg.bPrevFrameFineIidQ; |
315 | 0 | pBsData->freqResIid = h_ps_d->specificTo.mpeg.prevFreqResIid; |
316 | 0 | for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) { |
317 | 0 | pBsData->aaIidIndex[pBsData->noEnv - 1][gr] = |
318 | 0 | h_ps_d->specificTo.mpeg.aIidPrevFrameIndex[gr]; |
319 | 0 | } |
320 | 0 | } else { |
321 | 0 | for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) { |
322 | 0 | pBsData->aaIidIndex[pBsData->noEnv - 1][gr] = 0; |
323 | 0 | } |
324 | 0 | } |
325 | |
|
326 | 0 | if (pBsData->bEnableIcc) { |
327 | 0 | pBsData->freqResIcc = h_ps_d->specificTo.mpeg.prevFreqResIcc; |
328 | 0 | for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) { |
329 | 0 | pBsData->aaIccIndex[pBsData->noEnv - 1][gr] = |
330 | 0 | h_ps_d->specificTo.mpeg.aIccPrevFrameIndex[gr]; |
331 | 0 | } |
332 | 0 | } else { |
333 | 0 | for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) { |
334 | 0 | pBsData->aaIccIndex[pBsData->noEnv - 1][gr] = 0; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | /* Update previous frame Iid quantization */ |
340 | 0 | h_ps_d->specificTo.mpeg.bPrevFrameFineIidQ = pBsData->bFineIidQ; |
341 | | |
342 | | /* Update previous frequency resolution for IID */ |
343 | 0 | h_ps_d->specificTo.mpeg.prevFreqResIid = pBsData->freqResIid; |
344 | | |
345 | | /* Update previous frequency resolution for ICC */ |
346 | 0 | h_ps_d->specificTo.mpeg.prevFreqResIcc = pBsData->freqResIcc; |
347 | | |
348 | | /* Update previous frame index buffers */ |
349 | 0 | for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) { |
350 | 0 | h_ps_d->specificTo.mpeg.aIidPrevFrameIndex[gr] = |
351 | 0 | pBsData->aaIidIndex[pBsData->noEnv - 1][gr]; |
352 | 0 | } |
353 | 0 | for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) { |
354 | 0 | h_ps_d->specificTo.mpeg.aIccPrevFrameIndex[gr] = |
355 | 0 | pBsData->aaIccIndex[pBsData->noEnv - 1][gr]; |
356 | 0 | } |
357 | | |
358 | | /* PS data from bitstream (if avail) was decoded now */ |
359 | 0 | h_ps_d->bPsDataAvail[h_ps_d->processSlot] = ppt_none; |
360 | | |
361 | | /* handling of env borders for FIX & VAR */ |
362 | 0 | if (pBsData->bFrameClass == 0) { |
363 | | /* FIX_BORDERS NoEnv=0,1,2,4 */ |
364 | 0 | pBsData->aEnvStartStop[0] = 0; |
365 | 0 | for (env = 1; env < pBsData->noEnv; env++) { |
366 | 0 | pBsData->aEnvStartStop[env] = |
367 | 0 | (env * h_ps_d->noSubSamples) / pBsData->noEnv; |
368 | 0 | } |
369 | 0 | pBsData->aEnvStartStop[pBsData->noEnv] = h_ps_d->noSubSamples; |
370 | | /* 1024 (32 slots) env borders: 0, 8, 16, 24, 32 */ |
371 | | /* 960 (30 slots) env borders: 0, 7, 15, 22, 30 */ |
372 | 0 | } else { /* if (h_ps_d->bFrameClass == 0) */ |
373 | | /* VAR_BORDERS NoEnv=1,2,3,4 */ |
374 | 0 | pBsData->aEnvStartStop[0] = 0; |
375 | | |
376 | | /* handle case aEnvStartStop[noEnv]<noSubSample for VAR_BORDERS by |
377 | | duplicating last PS parameters and incrementing noEnv */ |
378 | 0 | if (pBsData->aEnvStartStop[pBsData->noEnv] < h_ps_d->noSubSamples) { |
379 | 0 | for (gr = 0; gr < NO_HI_RES_IID_BINS; gr++) { |
380 | 0 | pBsData->aaIidIndex[pBsData->noEnv][gr] = |
381 | 0 | pBsData->aaIidIndex[pBsData->noEnv - 1][gr]; |
382 | 0 | } |
383 | 0 | for (gr = 0; gr < NO_HI_RES_ICC_BINS; gr++) { |
384 | 0 | pBsData->aaIccIndex[pBsData->noEnv][gr] = |
385 | 0 | pBsData->aaIccIndex[pBsData->noEnv - 1][gr]; |
386 | 0 | } |
387 | 0 | pBsData->noEnv++; |
388 | 0 | pBsData->aEnvStartStop[pBsData->noEnv] = h_ps_d->noSubSamples; |
389 | 0 | } |
390 | | |
391 | | /* enforce strictly monotonic increasing borders */ |
392 | 0 | for (env = 1; env < pBsData->noEnv; env++) { |
393 | 0 | UCHAR thr; |
394 | 0 | thr = (UCHAR)h_ps_d->noSubSamples - (pBsData->noEnv - env); |
395 | 0 | if (pBsData->aEnvStartStop[env] > thr) { |
396 | 0 | pBsData->aEnvStartStop[env] = thr; |
397 | 0 | } else { |
398 | 0 | thr = pBsData->aEnvStartStop[env - 1] + 1; |
399 | 0 | if (pBsData->aEnvStartStop[env] < thr) { |
400 | 0 | pBsData->aEnvStartStop[env] = thr; |
401 | 0 | } |
402 | 0 | } |
403 | 0 | } |
404 | 0 | } /* if (h_ps_d->bFrameClass == 0) ... else */ |
405 | | |
406 | | /* copy data prior to possible 20<->34 in-place mapping */ |
407 | 0 | for (env = 0; env < pBsData->noEnv; env++) { |
408 | 0 | UCHAR i; |
409 | 0 | for (i = 0; i < NO_HI_RES_IID_BINS; i++) { |
410 | 0 | h_ps_d->specificTo.mpeg.pCoef->aaIidIndexMapped[env][i] = |
411 | 0 | pBsData->aaIidIndex[env][i]; |
412 | 0 | } |
413 | 0 | for (i = 0; i < NO_HI_RES_ICC_BINS; i++) { |
414 | 0 | h_ps_d->specificTo.mpeg.pCoef->aaIccIndexMapped[env][i] = |
415 | 0 | pBsData->aaIccIndex[env][i]; |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | /* MPEG baseline PS */ |
420 | | /* Baseline version of PS always uses the hybrid filter structure with 20 |
421 | | * stereo bands. */ |
422 | | /* If ICC/IID parameters for 34 stereo bands are decoded they have to be |
423 | | * mapped to 20 */ |
424 | | /* stereo bands. */ |
425 | | /* Additionaly the IPD/OPD parameters won't be used. */ |
426 | |
|
427 | 0 | for (env = 0; env < pBsData->noEnv; env++) { |
428 | 0 | if (pBsData->freqResIid == 2) |
429 | 0 | map34IndexTo20(h_ps_d->specificTo.mpeg.pCoef->aaIidIndexMapped[env], |
430 | 0 | NO_HI_RES_IID_BINS); |
431 | 0 | if (pBsData->freqResIcc == 2) |
432 | 0 | map34IndexTo20(h_ps_d->specificTo.mpeg.pCoef->aaIccIndexMapped[env], |
433 | 0 | NO_HI_RES_ICC_BINS); |
434 | | |
435 | | /* IPD/OPD is disabled in baseline version and thus was removed here */ |
436 | 0 | } |
437 | |
|
438 | 0 | return (1); |
439 | 0 | } |
440 | | |
441 | | /***************************************************************************/ |
442 | | /*! |
443 | | |
444 | | \brief Reads parametric stereo data from bitstream |
445 | | |
446 | | \return |
447 | | |
448 | | ****************************************************************************/ |
449 | | unsigned int ReadPsData( |
450 | | HANDLE_PS_DEC h_ps_d, /*!< handle to struct PS_DEC */ |
451 | | HANDLE_FDK_BITSTREAM hBitBuf, /*!< handle to struct BIT_BUF */ |
452 | | int nBitsLeft /*!< max number of bits available */ |
453 | 0 | ) { |
454 | 0 | MPEG_PS_BS_DATA *pBsData; |
455 | |
|
456 | 0 | UCHAR gr, env; |
457 | 0 | SCHAR dtFlag; |
458 | 0 | INT startbits; |
459 | 0 | Huffman CurrentTable; |
460 | 0 | SCHAR bEnableHeader; |
461 | |
|
462 | 0 | if (!h_ps_d) return 0; |
463 | | |
464 | 0 | pBsData = &h_ps_d->bsData[h_ps_d->bsReadSlot].mpeg; |
465 | |
|
466 | 0 | if (h_ps_d->bsReadSlot != h_ps_d->bsLastSlot) { |
467 | | /* Copy last header data */ |
468 | 0 | FDKmemcpy(pBsData, &h_ps_d->bsData[h_ps_d->bsLastSlot].mpeg, |
469 | 0 | sizeof(MPEG_PS_BS_DATA)); |
470 | 0 | } |
471 | |
|
472 | 0 | startbits = (INT)FDKgetValidBits(hBitBuf); |
473 | |
|
474 | 0 | bEnableHeader = (SCHAR)FDKreadBits(hBitBuf, 1); |
475 | | |
476 | | /* Read header */ |
477 | 0 | if (bEnableHeader) { |
478 | 0 | pBsData->bPsHeaderValid = 1; |
479 | 0 | pBsData->bEnableIid = (UCHAR)FDKreadBits(hBitBuf, 1); |
480 | 0 | if (pBsData->bEnableIid) { |
481 | 0 | pBsData->modeIid = (UCHAR)FDKreadBits(hBitBuf, 3); |
482 | 0 | } |
483 | |
|
484 | 0 | pBsData->bEnableIcc = (UCHAR)FDKreadBits(hBitBuf, 1); |
485 | 0 | if (pBsData->bEnableIcc) { |
486 | 0 | pBsData->modeIcc = (UCHAR)FDKreadBits(hBitBuf, 3); |
487 | 0 | } |
488 | |
|
489 | 0 | pBsData->bEnableExt = (UCHAR)FDKreadBits(hBitBuf, 1); |
490 | 0 | } |
491 | |
|
492 | 0 | pBsData->bFrameClass = (UCHAR)FDKreadBits(hBitBuf, 1); |
493 | 0 | if (pBsData->bFrameClass == 0) { |
494 | | /* FIX_BORDERS NoEnv=0,1,2,4 */ |
495 | 0 | pBsData->noEnv = |
496 | 0 | FDK_sbrDecoder_aFixNoEnvDecode[(UCHAR)FDKreadBits(hBitBuf, 2)]; |
497 | | /* all additional handling of env borders is now in DecodePs() */ |
498 | 0 | } else { |
499 | | /* VAR_BORDERS NoEnv=1,2,3,4 */ |
500 | 0 | pBsData->noEnv = 1 + (UCHAR)FDKreadBits(hBitBuf, 2); |
501 | 0 | for (env = 1; env < pBsData->noEnv + 1; env++) |
502 | 0 | pBsData->aEnvStartStop[env] = ((UCHAR)FDKreadBits(hBitBuf, 5)) + 1; |
503 | | /* all additional handling of env borders is now in DecodePs() */ |
504 | 0 | } |
505 | | |
506 | | /* verify that IID & ICC modes (quant grid, freq res) are supported */ |
507 | 0 | if ((pBsData->modeIid > 5) || (pBsData->modeIcc > 5)) { |
508 | | /* no useful PS data could be read from bitstream */ |
509 | 0 | h_ps_d->bPsDataAvail[h_ps_d->bsReadSlot] = ppt_none; |
510 | | /* discard all remaining bits */ |
511 | 0 | nBitsLeft -= startbits - (INT)FDKgetValidBits(hBitBuf); |
512 | 0 | while (nBitsLeft > 0) { |
513 | 0 | int i = nBitsLeft; |
514 | 0 | if (i > 8) { |
515 | 0 | i = 8; |
516 | 0 | } |
517 | 0 | FDKreadBits(hBitBuf, i); |
518 | 0 | nBitsLeft -= i; |
519 | 0 | } |
520 | 0 | return (UINT)(startbits - (INT)FDKgetValidBits(hBitBuf)); |
521 | 0 | } |
522 | | |
523 | 0 | if (pBsData->modeIid > 2) { |
524 | 0 | pBsData->freqResIid = pBsData->modeIid - 3; |
525 | 0 | pBsData->bFineIidQ = 1; |
526 | 0 | } else { |
527 | 0 | pBsData->freqResIid = pBsData->modeIid; |
528 | 0 | pBsData->bFineIidQ = 0; |
529 | 0 | } |
530 | |
|
531 | 0 | if (pBsData->modeIcc > 2) { |
532 | 0 | pBsData->freqResIcc = pBsData->modeIcc - 3; |
533 | 0 | } else { |
534 | 0 | pBsData->freqResIcc = pBsData->modeIcc; |
535 | 0 | } |
536 | | |
537 | | /* Extract IID data */ |
538 | 0 | if (pBsData->bEnableIid) { |
539 | 0 | for (env = 0; env < pBsData->noEnv; env++) { |
540 | 0 | dtFlag = (SCHAR)FDKreadBits(hBitBuf, 1); |
541 | 0 | if (!dtFlag) { |
542 | 0 | if (pBsData->bFineIidQ) |
543 | 0 | CurrentTable = (Huffman)&aBookPsIidFineFreqDecode; |
544 | 0 | else |
545 | 0 | CurrentTable = (Huffman)&aBookPsIidFreqDecode; |
546 | 0 | } else { |
547 | 0 | if (pBsData->bFineIidQ) |
548 | 0 | CurrentTable = (Huffman)&aBookPsIidFineTimeDecode; |
549 | 0 | else |
550 | 0 | CurrentTable = (Huffman)&aBookPsIidTimeDecode; |
551 | 0 | } |
552 | |
|
553 | 0 | for (gr = 0; gr < FDK_sbrDecoder_aNoIidBins[pBsData->freqResIid]; gr++) |
554 | 0 | pBsData->aaIidIndex[env][gr] = |
555 | 0 | decode_huff_cw(CurrentTable, hBitBuf, NULL); |
556 | 0 | pBsData->abIidDtFlag[env] = dtFlag; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | /* Extract ICC data */ |
561 | 0 | if (pBsData->bEnableIcc) { |
562 | 0 | for (env = 0; env < pBsData->noEnv; env++) { |
563 | 0 | dtFlag = (SCHAR)FDKreadBits(hBitBuf, 1); |
564 | 0 | if (!dtFlag) |
565 | 0 | CurrentTable = (Huffman)&aBookPsIccFreqDecode; |
566 | 0 | else |
567 | 0 | CurrentTable = (Huffman)&aBookPsIccTimeDecode; |
568 | |
|
569 | 0 | for (gr = 0; gr < FDK_sbrDecoder_aNoIccBins[pBsData->freqResIcc]; gr++) |
570 | 0 | pBsData->aaIccIndex[env][gr] = |
571 | 0 | decode_huff_cw(CurrentTable, hBitBuf, NULL); |
572 | 0 | pBsData->abIccDtFlag[env] = dtFlag; |
573 | 0 | } |
574 | 0 | } |
575 | |
|
576 | 0 | if (pBsData->bEnableExt) { |
577 | | /*! |
578 | | Decoders that support only the baseline version of the PS tool are allowed |
579 | | to ignore the IPD/OPD data, but according header data has to be parsed. |
580 | | ISO/IEC 14496-3 Subpart 8 Annex 4 |
581 | | */ |
582 | |
|
583 | 0 | int cnt = FDKreadBits(hBitBuf, PS_EXTENSION_SIZE_BITS); |
584 | 0 | if (cnt == (1 << PS_EXTENSION_SIZE_BITS) - 1) { |
585 | 0 | cnt += FDKreadBits(hBitBuf, PS_EXTENSION_ESC_COUNT_BITS); |
586 | 0 | } |
587 | 0 | while (cnt--) FDKreadBits(hBitBuf, 8); |
588 | 0 | } |
589 | | |
590 | | /* new PS data was read from bitstream */ |
591 | 0 | h_ps_d->bPsDataAvail[h_ps_d->bsReadSlot] = ppt_mpeg; |
592 | |
|
593 | 0 | return (startbits - (INT)FDKgetValidBits(hBitBuf)); |
594 | 0 | } |