/src/aac/libAACdec/src/block.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 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /**************************** AAC decoder library ****************************** |
96 | | |
97 | | Author(s): Josef Hoepfl |
98 | | |
99 | | Description: long/short-block decoding |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "block.h" |
104 | | |
105 | | #include "aac_rom.h" |
106 | | #include "FDK_bitstream.h" |
107 | | #include "scale.h" |
108 | | #include "FDK_tools_rom.h" |
109 | | |
110 | | #include "usacdec_fac.h" |
111 | | #include "usacdec_lpd.h" |
112 | | #include "usacdec_lpc.h" |
113 | | #include "FDK_trigFcts.h" |
114 | | |
115 | | #include "ac_arith_coder.h" |
116 | | |
117 | | #include "aacdec_hcr.h" |
118 | | #include "rvlc.h" |
119 | | |
120 | | #if defined(__arm__) |
121 | | #include "arm/block_arm.cpp" |
122 | | #endif |
123 | | |
124 | | /*! |
125 | | \brief Read escape sequence of codeword |
126 | | |
127 | | The function reads the escape sequence from the bitstream, |
128 | | if the absolute value of the quantized coefficient has the |
129 | | value 16. |
130 | | A limitation is implemented to maximal 21 bits according to |
131 | | ISO/IEC 14496-3:2009(E) 4.6.3.3. |
132 | | This limits the escape prefix to a maximum of eight 1's. |
133 | | If more than eight 1's are read, MAX_QUANTIZED_VALUE + 1 is |
134 | | returned, independent of the sign of parameter q. |
135 | | |
136 | | \return quantized coefficient |
137 | | */ |
138 | | LONG CBlock_GetEscape(HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */ |
139 | | const LONG q) /*!< quantized coefficient */ |
140 | 0 | { |
141 | 0 | if (fAbs(q) != 16) return (q); |
142 | | |
143 | 0 | LONG i, off; |
144 | 0 | for (i = 4; i < 13; i++) { |
145 | 0 | if (FDKreadBit(bs) == 0) break; |
146 | 0 | } |
147 | |
|
148 | 0 | if (i == 13) return (MAX_QUANTIZED_VALUE + 1); |
149 | | |
150 | 0 | off = FDKreadBits(bs, i); |
151 | 0 | i = off + (1 << i); |
152 | |
|
153 | 0 | if (q < 0) i = -i; |
154 | |
|
155 | 0 | return i; |
156 | 0 | } |
157 | | |
158 | | AAC_DECODER_ERROR CBlock_ReadScaleFactorData( |
159 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, HANDLE_FDK_BITSTREAM bs, |
160 | 0 | UINT flags) { |
161 | 0 | int temp; |
162 | 0 | int band; |
163 | 0 | int group; |
164 | 0 | int position = 0; /* accu for intensity delta coding */ |
165 | 0 | int factor = pAacDecoderChannelInfo->pDynData->RawDataInfo |
166 | 0 | .GlobalGain; /* accu for scale factor delta coding */ |
167 | 0 | UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook; |
168 | 0 | SHORT *pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor; |
169 | 0 | const CodeBookDescription *hcb = &AACcodeBookDescriptionTable[BOOKSCL]; |
170 | |
|
171 | 0 | const USHORT(*CodeBook)[HuffmanEntries] = hcb->CodeBook; |
172 | |
|
173 | 0 | int ScaleFactorBandsTransmitted = |
174 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo); |
175 | 0 | for (group = 0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); |
176 | 0 | group++) { |
177 | 0 | for (band = 0; band < ScaleFactorBandsTransmitted; band++) { |
178 | 0 | switch (pCodeBook[band]) { |
179 | 0 | case ZERO_HCB: /* zero book */ |
180 | 0 | pScaleFactor[band] = 0; |
181 | 0 | break; |
182 | | |
183 | 0 | default: /* decode scale factor */ |
184 | 0 | if (!((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) && band == 0 && |
185 | 0 | group == 0)) { |
186 | 0 | temp = CBlock_DecodeHuffmanWordCB(bs, CodeBook); |
187 | 0 | factor += temp - 60; /* MIDFAC 1.5 dB */ |
188 | 0 | } |
189 | 0 | pScaleFactor[band] = factor - 100; |
190 | 0 | break; |
191 | | |
192 | 0 | case INTENSITY_HCB: /* intensity steering */ |
193 | 0 | case INTENSITY_HCB2: |
194 | 0 | temp = CBlock_DecodeHuffmanWordCB(bs, CodeBook); |
195 | 0 | position += temp - 60; |
196 | 0 | pScaleFactor[band] = position - 100; |
197 | 0 | break; |
198 | | |
199 | 0 | case NOISE_HCB: /* PNS */ |
200 | 0 | if (flags & (AC_MPEGD_RES | AC_USAC | AC_RSVD50 | AC_RSV603DA)) { |
201 | 0 | return AAC_DEC_PARSE_ERROR; |
202 | 0 | } |
203 | 0 | CPns_Read(&pAacDecoderChannelInfo->data.aac.PnsData, bs, hcb, |
204 | 0 | pAacDecoderChannelInfo->pDynData->aScaleFactor, |
205 | 0 | pAacDecoderChannelInfo->pDynData->RawDataInfo.GlobalGain, |
206 | 0 | band, group); |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | pCodeBook += 16; |
211 | 0 | pScaleFactor += 16; |
212 | 0 | } |
213 | | |
214 | 0 | return AAC_DEC_OK; |
215 | 0 | } |
216 | | |
217 | | void CBlock_ScaleSpectralData(CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
218 | | UCHAR maxSfbs, |
219 | 0 | SamplingRateInfo *pSamplingRateInfo) { |
220 | 0 | int band; |
221 | 0 | int window; |
222 | 0 | const SHORT *RESTRICT pSfbScale = pAacDecoderChannelInfo->pDynData->aSfbScale; |
223 | 0 | SHORT *RESTRICT pSpecScale = pAacDecoderChannelInfo->specScale; |
224 | 0 | int groupwin, group; |
225 | 0 | const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets( |
226 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo); |
227 | 0 | SPECTRAL_PTR RESTRICT pSpectralCoefficient = |
228 | 0 | pAacDecoderChannelInfo->pSpectralCoefficient; |
229 | |
|
230 | 0 | FDKmemclear(pSpecScale, 8 * sizeof(SHORT)); |
231 | |
|
232 | 0 | for (window = 0, group = 0; |
233 | 0 | group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++) { |
234 | 0 | for (groupwin = 0; groupwin < GetWindowGroupLength( |
235 | 0 | &pAacDecoderChannelInfo->icsInfo, group); |
236 | 0 | groupwin++, window++) { |
237 | 0 | int SpecScale_window = pSpecScale[window]; |
238 | 0 | FIXP_DBL *pSpectrum = SPEC(pSpectralCoefficient, window, |
239 | 0 | pAacDecoderChannelInfo->granuleLength); |
240 | | |
241 | | /* find scaling for current window */ |
242 | 0 | for (band = 0; band < maxSfbs; band++) { |
243 | 0 | SpecScale_window = |
244 | 0 | fMax(SpecScale_window, (int)pSfbScale[window * 16 + band]); |
245 | 0 | } |
246 | |
|
247 | 0 | if (pAacDecoderChannelInfo->pDynData->TnsData.Active && |
248 | 0 | pAacDecoderChannelInfo->pDynData->TnsData.NumberOfFilters[window] > |
249 | 0 | 0) { |
250 | 0 | int filter_index, SpecScale_window_tns; |
251 | 0 | int tns_start, tns_stop; |
252 | | |
253 | | /* Find max scale of TNS bands */ |
254 | 0 | SpecScale_window_tns = 0; |
255 | 0 | tns_start = GetMaximumTnsBands(&pAacDecoderChannelInfo->icsInfo, |
256 | 0 | pSamplingRateInfo->samplingRateIndex); |
257 | 0 | tns_stop = 0; |
258 | 0 | for (filter_index = 0; |
259 | 0 | filter_index < (int)pAacDecoderChannelInfo->pDynData->TnsData |
260 | 0 | .NumberOfFilters[window]; |
261 | 0 | filter_index++) { |
262 | 0 | for (band = pAacDecoderChannelInfo->pDynData->TnsData |
263 | 0 | .Filter[window][filter_index] |
264 | 0 | .StartBand; |
265 | 0 | band < pAacDecoderChannelInfo->pDynData->TnsData |
266 | 0 | .Filter[window][filter_index] |
267 | 0 | .StopBand; |
268 | 0 | band++) { |
269 | 0 | SpecScale_window_tns = |
270 | 0 | fMax(SpecScale_window_tns, (int)pSfbScale[window * 16 + band]); |
271 | 0 | } |
272 | | /* Find TNS line boundaries for all TNS filters */ |
273 | 0 | tns_start = |
274 | 0 | fMin(tns_start, (int)pAacDecoderChannelInfo->pDynData->TnsData |
275 | 0 | .Filter[window][filter_index] |
276 | 0 | .StartBand); |
277 | 0 | tns_stop = |
278 | 0 | fMax(tns_stop, (int)pAacDecoderChannelInfo->pDynData->TnsData |
279 | 0 | .Filter[window][filter_index] |
280 | 0 | .StopBand); |
281 | 0 | } |
282 | 0 | SpecScale_window_tns = SpecScale_window_tns + |
283 | 0 | pAacDecoderChannelInfo->pDynData->TnsData.GainLd; |
284 | 0 | FDK_ASSERT(tns_stop >= tns_start); |
285 | | /* Consider existing headroom of all MDCT lines inside the TNS bands. */ |
286 | 0 | SpecScale_window_tns -= |
287 | 0 | getScalefactor(pSpectrum + BandOffsets[tns_start], |
288 | 0 | BandOffsets[tns_stop] - BandOffsets[tns_start]); |
289 | 0 | if (SpecScale_window <= 17) { |
290 | 0 | SpecScale_window_tns++; |
291 | 0 | } |
292 | | /* Add enough mantissa head room such that the spectrum is still |
293 | | representable after applying TNS. */ |
294 | 0 | SpecScale_window = fMax(SpecScale_window, SpecScale_window_tns); |
295 | 0 | } |
296 | | |
297 | | /* store scaling of current window */ |
298 | 0 | pSpecScale[window] = SpecScale_window; |
299 | |
|
300 | | #ifdef FUNCTION_CBlock_ScaleSpectralData_func1 |
301 | | |
302 | | CBlock_ScaleSpectralData_func1(pSpectrum, maxSfbs, BandOffsets, |
303 | | SpecScale_window, pSfbScale, window); |
304 | | |
305 | | #else /* FUNCTION_CBlock_ScaleSpectralData_func1 */ |
306 | 0 | for (band = 0; band < maxSfbs; band++) { |
307 | 0 | int scale = fMin(DFRACT_BITS - 1, |
308 | 0 | SpecScale_window - pSfbScale[window * 16 + band]); |
309 | 0 | if (scale) { |
310 | 0 | FDK_ASSERT(scale > 0); |
311 | | |
312 | | /* following relation can be used for optimizations: |
313 | | * (BandOffsets[i]%4) == 0 for all i */ |
314 | 0 | int max_index = BandOffsets[band + 1]; |
315 | 0 | DWORD_ALIGNED(pSpectrum); |
316 | 0 | for (int index = BandOffsets[band]; index < max_index; index++) { |
317 | 0 | pSpectrum[index] >>= scale; |
318 | 0 | } |
319 | 0 | } |
320 | 0 | } |
321 | 0 | #endif /* FUNCTION_CBlock_ScaleSpectralData_func1 */ |
322 | 0 | } |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | AAC_DECODER_ERROR CBlock_ReadSectionData( |
327 | | HANDLE_FDK_BITSTREAM bs, CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
328 | 0 | const SamplingRateInfo *pSamplingRateInfo, const UINT flags) { |
329 | 0 | int top, band; |
330 | 0 | int sect_len, sect_len_incr; |
331 | 0 | int group; |
332 | 0 | UCHAR sect_cb; |
333 | 0 | UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook; |
334 | | /* HCR input (long) */ |
335 | 0 | SHORT *pNumLinesInSec = |
336 | 0 | pAacDecoderChannelInfo->pDynData->specificTo.aac.aNumLineInSec4Hcr; |
337 | 0 | int numLinesInSecIdx = 0; |
338 | 0 | UCHAR *pHcrCodeBook = |
339 | 0 | pAacDecoderChannelInfo->pDynData->specificTo.aac.aCodeBooks4Hcr; |
340 | 0 | const SHORT *BandOffsets = GetScaleFactorBandOffsets( |
341 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo); |
342 | 0 | pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection = 0; |
343 | 0 | AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK; |
344 | |
|
345 | 0 | FDKmemclear(pCodeBook, sizeof(UCHAR) * (8 * 16)); |
346 | |
|
347 | 0 | const int nbits = |
348 | 0 | (IsLongBlock(&pAacDecoderChannelInfo->icsInfo) == 1) ? 5 : 3; |
349 | |
|
350 | 0 | int sect_esc_val = (1 << nbits) - 1; |
351 | |
|
352 | 0 | UCHAR ScaleFactorBandsTransmitted = |
353 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo); |
354 | 0 | for (group = 0; group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); |
355 | 0 | group++) { |
356 | 0 | for (band = 0; band < ScaleFactorBandsTransmitted;) { |
357 | 0 | sect_len = 0; |
358 | 0 | if (flags & AC_ER_VCB11) { |
359 | 0 | sect_cb = (UCHAR)FDKreadBits(bs, 5); |
360 | 0 | } else |
361 | 0 | sect_cb = (UCHAR)FDKreadBits(bs, 4); |
362 | |
|
363 | 0 | if (((flags & AC_ER_VCB11) == 0) || (sect_cb < 11) || |
364 | 0 | ((sect_cb > 11) && (sect_cb < 16))) { |
365 | 0 | sect_len_incr = FDKreadBits(bs, nbits); |
366 | 0 | while (sect_len_incr == sect_esc_val) { |
367 | 0 | sect_len += sect_esc_val; |
368 | 0 | sect_len_incr = FDKreadBits(bs, nbits); |
369 | 0 | } |
370 | 0 | } else { |
371 | 0 | sect_len_incr = 1; |
372 | 0 | } |
373 | |
|
374 | 0 | sect_len += sect_len_incr; |
375 | |
|
376 | 0 | top = band + sect_len; |
377 | |
|
378 | 0 | if (flags & AC_ER_HCR) { |
379 | | /* HCR input (long) -- collecting sideinfo (for HCR-_long_ only) */ |
380 | 0 | if (numLinesInSecIdx >= MAX_SFB_HCR) { |
381 | 0 | return AAC_DEC_PARSE_ERROR; |
382 | 0 | } |
383 | 0 | if (top > (int)GetNumberOfScaleFactorBands( |
384 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo)) { |
385 | 0 | return AAC_DEC_PARSE_ERROR; |
386 | 0 | } |
387 | 0 | pNumLinesInSec[numLinesInSecIdx] = BandOffsets[top] - BandOffsets[band]; |
388 | 0 | numLinesInSecIdx++; |
389 | 0 | if (sect_cb == BOOKSCL) { |
390 | 0 | return AAC_DEC_INVALID_CODE_BOOK; |
391 | 0 | } else { |
392 | 0 | *pHcrCodeBook++ = sect_cb; |
393 | 0 | } |
394 | 0 | pAacDecoderChannelInfo->pDynData->specificTo.aac.numberSection++; |
395 | 0 | } |
396 | | |
397 | | /* Check spectral line limits */ |
398 | 0 | if (IsLongBlock(&(pAacDecoderChannelInfo->icsInfo))) { |
399 | 0 | if (top > 64) { |
400 | 0 | return AAC_DEC_DECODE_FRAME_ERROR; |
401 | 0 | } |
402 | 0 | } else { /* short block */ |
403 | 0 | if (top + group * 16 > (8 * 16)) { |
404 | 0 | return AAC_DEC_DECODE_FRAME_ERROR; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | | /* Check if decoded codebook index is feasible */ |
409 | 0 | if ((sect_cb == BOOKSCL) || |
410 | 0 | ((sect_cb == INTENSITY_HCB || sect_cb == INTENSITY_HCB2) && |
411 | 0 | pAacDecoderChannelInfo->pDynData->RawDataInfo.CommonWindow == 0)) { |
412 | 0 | return AAC_DEC_INVALID_CODE_BOOK; |
413 | 0 | } |
414 | | |
415 | | /* Store codebook index */ |
416 | 0 | for (; band < top; band++) { |
417 | 0 | pCodeBook[group * 16 + band] = sect_cb; |
418 | 0 | } |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | 0 | return ErrorStatus; |
423 | 0 | } |
424 | | |
425 | | /* mso: provides a faster way to i-quantize a whole band in one go */ |
426 | | |
427 | | /** |
428 | | * \brief inverse quantize one sfb. Each value of the sfb is processed according |
429 | | * to the formula: spectrum[i] = Sign(spectrum[i]) * Matissa(spectrum[i])^(4/3) |
430 | | * * 2^(lsb/4). |
431 | | * \param spectrum pointer to first line of the sfb to be inverse quantized. |
432 | | * \param noLines number of lines belonging to the sfb. |
433 | | * \param lsb last 2 bits of the scale factor of the sfb. |
434 | | * \param scale max allowed shift scale for the sfb. |
435 | | */ |
436 | | static inline void InverseQuantizeBand( |
437 | | FIXP_DBL *RESTRICT spectrum, const FIXP_DBL *RESTRICT InverseQuantTabler, |
438 | | const FIXP_DBL *RESTRICT MantissaTabler, |
439 | 0 | const SCHAR *RESTRICT ExponentTabler, INT noLines, INT scale) { |
440 | 0 | scale = scale + 1; /* +1 to compensate fMultDiv2 shift-right in loop */ |
441 | |
|
442 | 0 | FIXP_DBL *RESTRICT ptr = spectrum; |
443 | 0 | FIXP_DBL signedValue; |
444 | |
|
445 | 0 | for (INT i = noLines; i--;) { |
446 | 0 | if ((signedValue = *ptr++) != FL2FXCONST_DBL(0)) { |
447 | 0 | FIXP_DBL value = fAbs(signedValue); |
448 | 0 | UINT freeBits = CntLeadingZeros(value); |
449 | 0 | UINT exponent = 32 - freeBits; |
450 | |
|
451 | 0 | UINT x = (UINT)(LONG)value << (INT)freeBits; |
452 | 0 | x <<= 1; /* shift out sign bit to avoid masking later on */ |
453 | 0 | UINT tableIndex = x >> 24; |
454 | 0 | x = (x >> 20) & 0x0F; |
455 | |
|
456 | 0 | UINT r0 = (UINT)(LONG)InverseQuantTabler[tableIndex + 0]; |
457 | 0 | UINT r1 = (UINT)(LONG)InverseQuantTabler[tableIndex + 1]; |
458 | 0 | UINT temp = (r1 - r0) * x + (r0 << 4); |
459 | |
|
460 | 0 | value = fMultDiv2((FIXP_DBL)temp, MantissaTabler[exponent]); |
461 | | |
462 | | /* + 1 compensates fMultDiv2() */ |
463 | 0 | scaleValueInPlace(&value, scale + ExponentTabler[exponent]); |
464 | |
|
465 | 0 | signedValue = (signedValue < (FIXP_DBL)0) ? -value : value; |
466 | 0 | ptr[-1] = signedValue; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | | static inline FIXP_DBL maxabs_D(const FIXP_DBL *pSpectralCoefficient, |
472 | 0 | const int noLines) { |
473 | | /* Find max spectral line value of the current sfb */ |
474 | 0 | FIXP_DBL locMax = (FIXP_DBL)0; |
475 | 0 | int i; |
476 | |
|
477 | 0 | DWORD_ALIGNED(pSpectralCoefficient); |
478 | |
|
479 | 0 | for (i = noLines; i-- > 0;) { |
480 | | /* Expensive memory access */ |
481 | 0 | locMax = fMax(fixp_abs(pSpectralCoefficient[i]), locMax); |
482 | 0 | } |
483 | |
|
484 | 0 | return locMax; |
485 | 0 | } |
486 | | |
487 | | AAC_DECODER_ERROR CBlock_InverseQuantizeSpectralData( |
488 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
489 | | SamplingRateInfo *pSamplingRateInfo, UCHAR *band_is_noise, |
490 | 0 | UCHAR active_band_search) { |
491 | 0 | int window, group, groupwin, band; |
492 | 0 | int ScaleFactorBandsTransmitted = |
493 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo); |
494 | 0 | UCHAR *RESTRICT pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook; |
495 | 0 | SHORT *RESTRICT pSfbScale = pAacDecoderChannelInfo->pDynData->aSfbScale; |
496 | 0 | SHORT *RESTRICT pScaleFactor = pAacDecoderChannelInfo->pDynData->aScaleFactor; |
497 | 0 | const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets( |
498 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo); |
499 | 0 | const SHORT total_bands = |
500 | 0 | GetScaleFactorBandsTotal(&pAacDecoderChannelInfo->icsInfo); |
501 | |
|
502 | 0 | FDKmemclear(pAacDecoderChannelInfo->pDynData->aSfbScale, |
503 | 0 | (8 * 16) * sizeof(SHORT)); |
504 | |
|
505 | 0 | for (window = 0, group = 0; |
506 | 0 | group < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++) { |
507 | 0 | for (groupwin = 0; groupwin < GetWindowGroupLength( |
508 | 0 | &pAacDecoderChannelInfo->icsInfo, group); |
509 | 0 | groupwin++, window++) { |
510 | | /* inverse quantization */ |
511 | 0 | for (band = 0; band < ScaleFactorBandsTransmitted; band++) { |
512 | 0 | FIXP_DBL *pSpectralCoefficient = |
513 | 0 | SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, window, |
514 | 0 | pAacDecoderChannelInfo->granuleLength) + |
515 | 0 | BandOffsets[band]; |
516 | 0 | FIXP_DBL locMax; |
517 | |
|
518 | 0 | const int noLines = BandOffsets[band + 1] - BandOffsets[band]; |
519 | 0 | const int bnds = group * 16 + band; |
520 | |
|
521 | 0 | if ((pCodeBook[bnds] == ZERO_HCB) || |
522 | 0 | (pCodeBook[bnds] == INTENSITY_HCB) || |
523 | 0 | (pCodeBook[bnds] == INTENSITY_HCB2)) |
524 | 0 | continue; |
525 | | |
526 | 0 | if (pCodeBook[bnds] == NOISE_HCB) { |
527 | | /* Leave headroom for PNS values. + 1 because ceil(log2(2^(0.25*3))) = |
528 | | 1, worst case of additional headroom required because of the |
529 | | scalefactor. */ |
530 | 0 | pSfbScale[window * 16 + band] = (pScaleFactor[bnds] >> 2) + 1; |
531 | 0 | continue; |
532 | 0 | } |
533 | | |
534 | 0 | locMax = maxabs_D(pSpectralCoefficient, noLines); |
535 | |
|
536 | 0 | if (active_band_search) { |
537 | 0 | if (locMax != FIXP_DBL(0)) { |
538 | 0 | band_is_noise[group * 16 + band] = 0; |
539 | 0 | } |
540 | 0 | } |
541 | | |
542 | | /* Cheap robustness improvement - Do not remove!!! */ |
543 | 0 | if (fixp_abs(locMax) > (FIXP_DBL)MAX_QUANTIZED_VALUE) { |
544 | 0 | return AAC_DEC_PARSE_ERROR; |
545 | 0 | } |
546 | | |
547 | | /* Added by Youliy Ninov: |
548 | | The inverse quantization operation is given by (ISO/IEC 14496-3:2009(E)) |
549 | | by: |
550 | | |
551 | | x_invquant=Sign(x_quant). abs(x_quant)^(4/3) |
552 | | |
553 | | We apply a gain, derived from the scale factor for the particular sfb, |
554 | | according to the following function: |
555 | | |
556 | | gain=2^(0.25*ScaleFactor) |
557 | | |
558 | | So, after scaling we have: |
559 | | |
560 | | x_rescale=gain*x_invquant=Sign(x_quant)*2^(0.25*ScaleFactor)*abs(s_quant)^(4/3) |
561 | | |
562 | | We could represent the ScaleFactor as: |
563 | | |
564 | | ScaleFactor= (ScaleFactor >> 2)*4 + ScaleFactor %4 |
565 | | |
566 | | When we substitute it we get: |
567 | | |
568 | | x_rescale=Sign(x_quant)*2^(ScaleFactor>>2)* ( |
569 | | 2^(0.25*(ScaleFactor%4))*abs(s_quant)^(4/3)) |
570 | | |
571 | | When we set: msb=(ScaleFactor>>2) and lsb=(ScaleFactor%4), we obtain: |
572 | | |
573 | | x_rescale=Sign(x_quant)*(2^msb)* ( 2^(lsb/4)*abs(s_quant)^(4/3)) |
574 | | |
575 | | The rescaled output can be represented by: |
576 | | mantissa : Sign(x_quant)*( 2^(lsb/4)*abs(s_quant)^(4/3)) |
577 | | exponent :(2^msb) |
578 | | |
579 | | */ |
580 | | |
581 | 0 | int msb = pScaleFactor[bnds] >> 2; |
582 | | |
583 | | /* Inverse quantize band only if it is not empty */ |
584 | 0 | if (locMax != FIXP_DBL(0)) { |
585 | 0 | int lsb = pScaleFactor[bnds] & 0x03; |
586 | |
|
587 | 0 | int scale = EvaluatePower43(&locMax, lsb); |
588 | |
|
589 | 0 | scale = CntLeadingZeros(locMax) - scale - 2; |
590 | |
|
591 | 0 | pSfbScale[window * 16 + band] = msb - scale; |
592 | 0 | InverseQuantizeBand(pSpectralCoefficient, InverseQuantTable, |
593 | 0 | MantissaTable[lsb], ExponentTable[lsb], noLines, |
594 | 0 | scale); |
595 | 0 | } else { |
596 | 0 | pSfbScale[window * 16 + band] = msb; |
597 | 0 | } |
598 | |
|
599 | 0 | } /* for (band=0; band < ScaleFactorBandsTransmitted; band++) */ |
600 | | |
601 | | /* Make sure the array is cleared to the end */ |
602 | 0 | SHORT start_clear = BandOffsets[ScaleFactorBandsTransmitted]; |
603 | 0 | SHORT end_clear = BandOffsets[total_bands]; |
604 | 0 | int diff_clear = (int)(end_clear - start_clear); |
605 | 0 | FIXP_DBL *pSpectralCoefficient = |
606 | 0 | SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, window, |
607 | 0 | pAacDecoderChannelInfo->granuleLength) + |
608 | 0 | start_clear; |
609 | 0 | FDKmemclear(pSpectralCoefficient, diff_clear * sizeof(FIXP_DBL)); |
610 | |
|
611 | 0 | } /* for (groupwin=0; groupwin < |
612 | | GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo,group); |
613 | | groupwin++, window++) */ |
614 | 0 | } /* for (window=0, group=0; group < |
615 | | GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); group++)*/ |
616 | | |
617 | 0 | return AAC_DEC_OK; |
618 | 0 | } |
619 | | |
620 | | AAC_DECODER_ERROR CBlock_ReadSpectralData( |
621 | | HANDLE_FDK_BITSTREAM bs, CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
622 | 0 | const SamplingRateInfo *pSamplingRateInfo, const UINT flags) { |
623 | 0 | int index, i; |
624 | 0 | const SHORT *RESTRICT BandOffsets = GetScaleFactorBandOffsets( |
625 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo); |
626 | |
|
627 | 0 | SPECTRAL_PTR pSpectralCoefficient = |
628 | 0 | pAacDecoderChannelInfo->pSpectralCoefficient; |
629 | |
|
630 | 0 | FDK_ASSERT(BandOffsets != NULL); |
631 | | |
632 | 0 | FDKmemclear(pSpectralCoefficient, sizeof(SPECTRUM)); |
633 | |
|
634 | 0 | if ((flags & AC_ER_HCR) == 0) { |
635 | 0 | int group; |
636 | 0 | int groupoffset; |
637 | 0 | UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook; |
638 | 0 | int ScaleFactorBandsTransmitted = |
639 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo); |
640 | 0 | int granuleLength = pAacDecoderChannelInfo->granuleLength; |
641 | |
|
642 | 0 | groupoffset = 0; |
643 | | |
644 | | /* plain huffman decoder short */ |
645 | 0 | int max_group = GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); |
646 | |
|
647 | 0 | for (group = 0; group < max_group; group++) { |
648 | 0 | int max_groupwin = |
649 | 0 | GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo, group); |
650 | 0 | int band; |
651 | |
|
652 | 0 | int bnds = group * 16; |
653 | |
|
654 | 0 | int bandOffset1 = BandOffsets[0]; |
655 | 0 | for (band = 0; band < ScaleFactorBandsTransmitted; band++, bnds++) { |
656 | 0 | UCHAR currentCB = pCodeBook[bnds]; |
657 | 0 | int bandOffset0 = bandOffset1; |
658 | 0 | bandOffset1 = BandOffsets[band + 1]; |
659 | | |
660 | | /* patch to run plain-huffman-decoder with vcb11 input codebooks |
661 | | * (LAV-checking might be possible below using the virtual cb and a |
662 | | * LAV-table) */ |
663 | 0 | if ((currentCB >= 16) && (currentCB <= 31)) { |
664 | 0 | pCodeBook[bnds] = currentCB = 11; |
665 | 0 | } |
666 | 0 | if (((currentCB != ZERO_HCB) && (currentCB != NOISE_HCB) && |
667 | 0 | (currentCB != INTENSITY_HCB) && (currentCB != INTENSITY_HCB2))) { |
668 | 0 | const CodeBookDescription *hcb = |
669 | 0 | &AACcodeBookDescriptionTable[currentCB]; |
670 | 0 | int step = hcb->Dimension; |
671 | 0 | int offset = hcb->Offset; |
672 | 0 | int bits = hcb->numBits; |
673 | 0 | int mask = (1 << bits) - 1; |
674 | 0 | const USHORT(*CodeBook)[HuffmanEntries] = hcb->CodeBook; |
675 | 0 | int groupwin; |
676 | |
|
677 | 0 | FIXP_DBL *mdctSpectrum = |
678 | 0 | &pSpectralCoefficient[groupoffset * granuleLength]; |
679 | |
|
680 | 0 | if (offset == 0) { |
681 | 0 | for (groupwin = 0; groupwin < max_groupwin; groupwin++) { |
682 | 0 | for (index = bandOffset0; index < bandOffset1; index += step) { |
683 | 0 | int idx = CBlock_DecodeHuffmanWordCB(bs, CodeBook); |
684 | 0 | for (i = 0; i < step; i++, idx >>= bits) { |
685 | 0 | FIXP_DBL tmp = (FIXP_DBL)((idx & mask) - offset); |
686 | 0 | if (tmp != FIXP_DBL(0)) tmp = (FDKreadBit(bs)) ? -tmp : tmp; |
687 | 0 | mdctSpectrum[index + i] = tmp; |
688 | 0 | } |
689 | |
|
690 | 0 | if (currentCB == ESCBOOK) { |
691 | 0 | for (int j = 0; j < 2; j++) |
692 | 0 | mdctSpectrum[index + j] = (FIXP_DBL)CBlock_GetEscape( |
693 | 0 | bs, (LONG)mdctSpectrum[index + j]); |
694 | 0 | } |
695 | 0 | } |
696 | 0 | mdctSpectrum += granuleLength; |
697 | 0 | } |
698 | 0 | } else { |
699 | 0 | for (groupwin = 0; groupwin < max_groupwin; groupwin++) { |
700 | 0 | for (index = bandOffset0; index < bandOffset1; index += step) { |
701 | 0 | int idx = CBlock_DecodeHuffmanWordCB(bs, CodeBook); |
702 | 0 | for (i = 0; i < step; i++, idx >>= bits) { |
703 | 0 | mdctSpectrum[index + i] = (FIXP_DBL)((idx & mask) - offset); |
704 | 0 | } |
705 | 0 | if (currentCB == ESCBOOK) { |
706 | 0 | for (int j = 0; j < 2; j++) |
707 | 0 | mdctSpectrum[index + j] = (FIXP_DBL)CBlock_GetEscape( |
708 | 0 | bs, (LONG)mdctSpectrum[index + j]); |
709 | 0 | } |
710 | 0 | } |
711 | 0 | mdctSpectrum += granuleLength; |
712 | 0 | } |
713 | 0 | } |
714 | 0 | } |
715 | 0 | } |
716 | 0 | groupoffset += max_groupwin; |
717 | 0 | } |
718 | | /* plain huffman decoding (short) finished */ |
719 | 0 | } |
720 | | |
721 | | /* HCR - Huffman Codeword Reordering short */ |
722 | 0 | else /* if ( flags & AC_ER_HCR ) */ |
723 | | |
724 | 0 | { |
725 | 0 | H_HCR_INFO hHcr = &pAacDecoderChannelInfo->pComData->overlay.aac.erHcrInfo; |
726 | |
|
727 | 0 | int hcrStatus = 0; |
728 | | |
729 | | /* advanced Huffman decoding starts here (HCR decoding :) */ |
730 | 0 | if (pAacDecoderChannelInfo->pDynData->specificTo.aac |
731 | 0 | .lenOfReorderedSpectralData != 0) { |
732 | | /* HCR initialization short */ |
733 | 0 | hcrStatus = HcrInit(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs); |
734 | |
|
735 | 0 | if (hcrStatus != 0) { |
736 | 0 | return AAC_DEC_DECODE_FRAME_ERROR; |
737 | 0 | } |
738 | | |
739 | | /* HCR decoding short */ |
740 | 0 | hcrStatus = |
741 | 0 | HcrDecoder(hHcr, pAacDecoderChannelInfo, pSamplingRateInfo, bs); |
742 | |
|
743 | 0 | if (hcrStatus != 0) { |
744 | 0 | #if HCR_ERROR_CONCEALMENT |
745 | 0 | HcrMuteErroneousLines(hHcr); |
746 | | #else |
747 | | return AAC_DEC_DECODE_FRAME_ERROR; |
748 | | #endif /* HCR_ERROR_CONCEALMENT */ |
749 | 0 | } |
750 | |
|
751 | 0 | FDKpushFor(bs, pAacDecoderChannelInfo->pDynData->specificTo.aac |
752 | 0 | .lenOfReorderedSpectralData); |
753 | 0 | } |
754 | 0 | } |
755 | | /* HCR - Huffman Codeword Reordering short finished */ |
756 | | |
757 | 0 | if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo) && |
758 | 0 | !(flags & (AC_ELD | AC_SCALABLE))) { |
759 | | /* apply pulse data */ |
760 | 0 | CPulseData_Apply( |
761 | 0 | &pAacDecoderChannelInfo->pDynData->specificTo.aac.PulseData, |
762 | 0 | GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, |
763 | 0 | pSamplingRateInfo), |
764 | 0 | SPEC_LONG(pSpectralCoefficient)); |
765 | 0 | } |
766 | |
|
767 | 0 | return AAC_DEC_OK; |
768 | 0 | } |
769 | | |
770 | | static const FIXP_SGL noise_level_tab[8] = { |
771 | | /* FDKpow(2, (float)(noise_level-14)/3.0f) * 2; (*2 to compensate for |
772 | | fMultDiv2) noise_level_tab(noise_level==0) == 0 by definition |
773 | | */ |
774 | | FX_DBL2FXCONST_SGL(0x00000000 /*0x0a145173*/), |
775 | | FX_DBL2FXCONST_SGL(0x0cb2ff5e), |
776 | | FX_DBL2FXCONST_SGL(0x10000000), |
777 | | FX_DBL2FXCONST_SGL(0x1428a2e7), |
778 | | FX_DBL2FXCONST_SGL(0x1965febd), |
779 | | FX_DBL2FXCONST_SGL(0x20000000), |
780 | | FX_DBL2FXCONST_SGL(0x28514606), |
781 | | FX_DBL2FXCONST_SGL(0x32cbfd33)}; |
782 | | |
783 | | void CBlock_ApplyNoise(CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
784 | | SamplingRateInfo *pSamplingRateInfo, ULONG *nfRandomSeed, |
785 | 0 | UCHAR *band_is_noise) { |
786 | 0 | const SHORT *swb_offset = GetScaleFactorBandOffsets( |
787 | 0 | &pAacDecoderChannelInfo->icsInfo, pSamplingRateInfo); |
788 | 0 | int g, win, gwin, sfb, noiseFillingStartOffset, nfStartOffset_sfb; |
789 | | |
790 | | /* Obtain noise level and scale factor offset. */ |
791 | 0 | int noise_level = pAacDecoderChannelInfo->pDynData->specificTo.usac |
792 | 0 | .fd_noise_level_and_offset >> |
793 | 0 | 5; |
794 | 0 | const FIXP_SGL noiseVal_pos = noise_level_tab[noise_level]; |
795 | | |
796 | | /* noise_offset can change even when noise_level=0. Neccesary for IGF stereo |
797 | | * filling */ |
798 | 0 | const int noise_offset = (pAacDecoderChannelInfo->pDynData->specificTo.usac |
799 | 0 | .fd_noise_level_and_offset & |
800 | 0 | 0x1f) - |
801 | 0 | 16; |
802 | |
|
803 | 0 | int max_sfb = |
804 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo); |
805 | |
|
806 | 0 | noiseFillingStartOffset = |
807 | 0 | (GetWindowSequence(&pAacDecoderChannelInfo->icsInfo) == BLOCK_SHORT) |
808 | 0 | ? 20 |
809 | 0 | : 160; |
810 | 0 | if (pAacDecoderChannelInfo->granuleLength == 96) { |
811 | 0 | noiseFillingStartOffset = |
812 | 0 | (3 * noiseFillingStartOffset) / |
813 | 0 | 4; /* scale offset with 3/4 for coreCoderFrameLength == 768 */ |
814 | 0 | } |
815 | | |
816 | | /* determine sfb from where on noise filling is applied */ |
817 | 0 | for (sfb = 0; swb_offset[sfb] < noiseFillingStartOffset; sfb++) |
818 | 0 | ; |
819 | 0 | nfStartOffset_sfb = sfb; |
820 | | |
821 | | /* if (noise_level!=0) */ |
822 | 0 | { |
823 | 0 | for (g = 0, win = 0; g < GetWindowGroups(&pAacDecoderChannelInfo->icsInfo); |
824 | 0 | g++) { |
825 | 0 | int windowGroupLength = |
826 | 0 | GetWindowGroupLength(&pAacDecoderChannelInfo->icsInfo, g); |
827 | 0 | for (sfb = nfStartOffset_sfb; sfb < max_sfb; sfb++) { |
828 | 0 | int bin_start = swb_offset[sfb]; |
829 | 0 | int bin_stop = swb_offset[sfb + 1]; |
830 | |
|
831 | 0 | int flagN = band_is_noise[g * 16 + sfb]; |
832 | | |
833 | | /* if all bins of one sfb in one window group are zero modify the scale |
834 | | * factor by noise_offset */ |
835 | 0 | if (flagN) { |
836 | | /* Change scaling factors for empty signal bands */ |
837 | 0 | pAacDecoderChannelInfo->pDynData->aScaleFactor[g * 16 + sfb] += |
838 | 0 | noise_offset; |
839 | | /* scale factor "sf" implied gain "g" is g = 2^(sf/4) */ |
840 | 0 | for (gwin = 0; gwin < windowGroupLength; gwin++) { |
841 | 0 | pAacDecoderChannelInfo->pDynData |
842 | 0 | ->aSfbScale[(win + gwin) * 16 + sfb] += (noise_offset >> 2); |
843 | 0 | } |
844 | 0 | } |
845 | |
|
846 | 0 | ULONG seed = *nfRandomSeed; |
847 | | /* + 1 because exponent of MantissaTable[lsb][0] is always 1. */ |
848 | 0 | int scale = |
849 | 0 | (pAacDecoderChannelInfo->pDynData->aScaleFactor[g * 16 + sfb] >> |
850 | 0 | 2) + |
851 | 0 | 1; |
852 | 0 | int lsb = |
853 | 0 | pAacDecoderChannelInfo->pDynData->aScaleFactor[g * 16 + sfb] & 3; |
854 | 0 | FIXP_DBL mantissa = MantissaTable[lsb][0]; |
855 | |
|
856 | 0 | for (gwin = 0; gwin < windowGroupLength; gwin++) { |
857 | 0 | FIXP_DBL *pSpec = |
858 | 0 | SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, win + gwin, |
859 | 0 | pAacDecoderChannelInfo->granuleLength); |
860 | |
|
861 | 0 | int scale1 = scale - pAacDecoderChannelInfo->pDynData |
862 | 0 | ->aSfbScale[(win + gwin) * 16 + sfb]; |
863 | 0 | FIXP_DBL scaled_noiseVal_pos = |
864 | 0 | scaleValue(fMultDiv2(noiseVal_pos, mantissa), scale1); |
865 | 0 | FIXP_DBL scaled_noiseVal_neg = -scaled_noiseVal_pos; |
866 | | |
867 | | /* If the whole band is zero, just fill without checking */ |
868 | 0 | if (flagN) { |
869 | 0 | for (int bin = bin_start; bin < bin_stop; bin++) { |
870 | 0 | seed = (ULONG)( |
871 | 0 | (UINT64)seed * 69069 + |
872 | 0 | 5); /* Inlined: UsacRandomSign - origin in usacdec_lpd.h */ |
873 | 0 | pSpec[bin] = |
874 | 0 | (seed & 0x10000) ? scaled_noiseVal_neg : scaled_noiseVal_pos; |
875 | 0 | } /* for (bin...) */ |
876 | 0 | } |
877 | | /*If band is sparsely filled, check for 0 and fill */ |
878 | 0 | else { |
879 | 0 | for (int bin = bin_start; bin < bin_stop; bin++) { |
880 | 0 | if (pSpec[bin] == (FIXP_DBL)0) { |
881 | 0 | seed = (ULONG)( |
882 | 0 | (UINT64)seed * 69069 + |
883 | 0 | 5); /* Inlined: UsacRandomSign - origin in usacdec_lpd.h */ |
884 | 0 | pSpec[bin] = (seed & 0x10000) ? scaled_noiseVal_neg |
885 | 0 | : scaled_noiseVal_pos; |
886 | 0 | } |
887 | 0 | } /* for (bin...) */ |
888 | 0 | } |
889 | |
|
890 | 0 | } /* for (gwin...) */ |
891 | 0 | *nfRandomSeed = seed; |
892 | 0 | } /* for (sfb...) */ |
893 | 0 | win += windowGroupLength; |
894 | 0 | } /* for (g...) */ |
895 | |
|
896 | 0 | } /* ... */ |
897 | 0 | } |
898 | | |
899 | | AAC_DECODER_ERROR CBlock_ReadAcSpectralData( |
900 | | HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo, |
901 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
902 | | const SamplingRateInfo *pSamplingRateInfo, const UINT frame_length, |
903 | 0 | const UINT flags) { |
904 | 0 | AAC_DECODER_ERROR errorAAC = AAC_DEC_OK; |
905 | 0 | ARITH_CODING_ERROR error = ARITH_CODER_OK; |
906 | 0 | int arith_reset_flag, lg, numWin, win, winLen; |
907 | 0 | const SHORT *RESTRICT BandOffsets; |
908 | | |
909 | | /* number of transmitted spectral coefficients */ |
910 | 0 | BandOffsets = GetScaleFactorBandOffsets(&pAacDecoderChannelInfo->icsInfo, |
911 | 0 | pSamplingRateInfo); |
912 | 0 | lg = BandOffsets[GetScaleFactorBandsTransmitted( |
913 | 0 | &pAacDecoderChannelInfo->icsInfo)]; |
914 | |
|
915 | 0 | numWin = GetWindowsPerFrame(&pAacDecoderChannelInfo->icsInfo); |
916 | 0 | winLen = (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) |
917 | 0 | ? (int)frame_length |
918 | 0 | : (int)frame_length / numWin; |
919 | |
|
920 | 0 | if (flags & AC_INDEP) { |
921 | 0 | arith_reset_flag = 1; |
922 | 0 | } else { |
923 | 0 | arith_reset_flag = (USHORT)FDKreadBits(hBs, 1); |
924 | 0 | } |
925 | |
|
926 | 0 | for (win = 0; win < numWin; win++) { |
927 | 0 | error = |
928 | 0 | CArco_DecodeArithData(pAacDecoderStaticChannelInfo->hArCo, hBs, |
929 | 0 | SPEC(pAacDecoderChannelInfo->pSpectralCoefficient, |
930 | 0 | win, pAacDecoderChannelInfo->granuleLength), |
931 | 0 | lg, winLen, arith_reset_flag && (win == 0)); |
932 | 0 | if (error != ARITH_CODER_OK) { |
933 | 0 | goto bail; |
934 | 0 | } |
935 | 0 | } |
936 | | |
937 | 0 | bail: |
938 | 0 | if (error == ARITH_CODER_ERROR) { |
939 | 0 | errorAAC = AAC_DEC_PARSE_ERROR; |
940 | 0 | } |
941 | |
|
942 | 0 | return errorAAC; |
943 | 0 | } |
944 | | |
945 | | void ApplyTools(CAacDecoderChannelInfo *pAacDecoderChannelInfo[], |
946 | | const SamplingRateInfo *pSamplingRateInfo, const UINT flags, |
947 | | const UINT elFlags, const int channel, |
948 | 0 | const int common_window) { |
949 | 0 | if (!(flags & (AC_USAC | AC_RSVD50 | AC_MPEGD_RES | AC_RSV603DA))) { |
950 | 0 | CPns_Apply(&pAacDecoderChannelInfo[channel]->data.aac.PnsData, |
951 | 0 | &pAacDecoderChannelInfo[channel]->icsInfo, |
952 | 0 | pAacDecoderChannelInfo[channel]->pSpectralCoefficient, |
953 | 0 | pAacDecoderChannelInfo[channel]->specScale, |
954 | 0 | pAacDecoderChannelInfo[channel]->pDynData->aScaleFactor, |
955 | 0 | pSamplingRateInfo, |
956 | 0 | pAacDecoderChannelInfo[channel]->granuleLength, channel); |
957 | 0 | } |
958 | |
|
959 | 0 | UCHAR nbands = |
960 | 0 | GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[channel]->icsInfo); |
961 | |
|
962 | 0 | CTns_Apply(&pAacDecoderChannelInfo[channel]->pDynData->TnsData, |
963 | 0 | &pAacDecoderChannelInfo[channel]->icsInfo, |
964 | 0 | pAacDecoderChannelInfo[channel]->pSpectralCoefficient, |
965 | 0 | pSamplingRateInfo, pAacDecoderChannelInfo[channel]->granuleLength, |
966 | 0 | nbands, (elFlags & AC_EL_ENHANCED_NOISE) ? 1 : 0, flags); |
967 | 0 | } |
968 | | |
969 | 0 | static int getWindow2Nr(int length, int shape) { |
970 | 0 | int nr = 0; |
971 | |
|
972 | 0 | if (shape == 2) { |
973 | | /* Low Overlap, 3/4 zeroed */ |
974 | 0 | nr = (length * 3) >> 2; |
975 | 0 | } |
976 | |
|
977 | 0 | return nr; |
978 | 0 | } |
979 | | |
980 | 0 | FIXP_DBL get_gain(const FIXP_DBL *x, const FIXP_DBL *y, int n) { |
981 | 0 | FIXP_DBL corr = (FIXP_DBL)0; |
982 | 0 | FIXP_DBL ener = (FIXP_DBL)1; |
983 | |
|
984 | 0 | int headroom_x = getScalefactor(x, n); |
985 | 0 | int headroom_y = getScalefactor(y, n); |
986 | | |
987 | | /*Calculate the normalization necessary due to addition*/ |
988 | | /* Check for power of two /special case */ |
989 | 0 | INT width_shift = (INT)(fNormz((FIXP_DBL)n)); |
990 | | /* Get the number of bits necessary minus one, because we need one sign bit |
991 | | * only */ |
992 | 0 | width_shift = 31 - width_shift; |
993 | |
|
994 | 0 | for (int i = 0; i < n; i++) { |
995 | 0 | corr += |
996 | 0 | fMultDiv2((x[i] << headroom_x), (y[i] << headroom_y)) >> width_shift; |
997 | 0 | ener += fPow2Div2((y[i] << headroom_y)) >> width_shift; |
998 | 0 | } |
999 | |
|
1000 | 0 | int exp_corr = (17 - headroom_x) + (17 - headroom_y) + width_shift + 1; |
1001 | 0 | int exp_ener = ((17 - headroom_y) << 1) + width_shift + 1; |
1002 | |
|
1003 | 0 | int temp_exp = 0; |
1004 | 0 | FIXP_DBL output = fDivNormSigned(corr, ener, &temp_exp); |
1005 | |
|
1006 | 0 | int output_exp = (exp_corr - exp_ener) + temp_exp; |
1007 | |
|
1008 | 0 | INT output_shift = 17 - output_exp; |
1009 | 0 | output_shift = fMin(output_shift, 31); |
1010 | |
|
1011 | 0 | output = scaleValue(output, -output_shift); |
1012 | |
|
1013 | 0 | return output; |
1014 | 0 | } |
1015 | | |
1016 | | void CBlock_FrequencyToTime( |
1017 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
1018 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, PCM_DEC outSamples[], |
1019 | | const SHORT frameLen, const int frameOk, FIXP_DBL *pWorkBuffer1, |
1020 | 0 | const INT aacOutDataHeadroom, UINT elFlags, INT elCh) { |
1021 | 0 | int fr, fl, tl, nSpec; |
1022 | |
|
1023 | 0 | #if defined(FDK_ASSERT_ENABLE) |
1024 | 0 | LONG nSamples; |
1025 | 0 | #endif |
1026 | | |
1027 | | /* Determine left slope length (fl), right slope length (fr) and transform |
1028 | | length (tl). USAC: The slope length may mismatch with the previous frame in |
1029 | | case of LPD / FD transitions. The adjustment is handled by the imdct |
1030 | | implementation. |
1031 | | */ |
1032 | 0 | tl = frameLen; |
1033 | 0 | nSpec = 1; |
1034 | |
|
1035 | 0 | switch (pAacDecoderChannelInfo->icsInfo.WindowSequence) { |
1036 | 0 | default: |
1037 | 0 | case BLOCK_LONG: |
1038 | 0 | fl = frameLen; |
1039 | 0 | fr = frameLen - |
1040 | 0 | getWindow2Nr(frameLen, |
1041 | 0 | GetWindowShape(&pAacDecoderChannelInfo->icsInfo)); |
1042 | | /* New startup needs differentiation between sine shape and low overlap |
1043 | | shape. This is a special case for the LD-AAC transformation windows, |
1044 | | because the slope length can be different while using the same window |
1045 | | sequence. */ |
1046 | 0 | if (pAacDecoderStaticChannelInfo->IMdct.prev_tl == 0) { |
1047 | 0 | fl = fr; |
1048 | 0 | } |
1049 | 0 | break; |
1050 | 0 | case BLOCK_STOP: |
1051 | 0 | fl = frameLen >> 3; |
1052 | 0 | fr = frameLen; |
1053 | 0 | break; |
1054 | 0 | case BLOCK_START: /* or StopStartSequence */ |
1055 | 0 | fl = frameLen; |
1056 | 0 | fr = frameLen >> 3; |
1057 | 0 | break; |
1058 | 0 | case BLOCK_SHORT: |
1059 | 0 | fl = fr = frameLen >> 3; |
1060 | 0 | tl >>= 3; |
1061 | 0 | nSpec = 8; |
1062 | 0 | break; |
1063 | 0 | } |
1064 | | |
1065 | 0 | { |
1066 | 0 | int last_frame_lost = pAacDecoderStaticChannelInfo->last_lpc_lost; |
1067 | |
|
1068 | 0 | if (pAacDecoderStaticChannelInfo->last_core_mode == LPD) { |
1069 | 0 | INT fac_FB = 1; |
1070 | 0 | if (elFlags & AC_EL_FULLBANDLPD) { |
1071 | 0 | fac_FB = 2; |
1072 | 0 | } |
1073 | |
|
1074 | 0 | FIXP_DBL *synth; |
1075 | | |
1076 | | /* Keep some free space at the beginning of the buffer. To be used for |
1077 | | * past data */ |
1078 | 0 | if (!(elFlags & AC_EL_LPDSTEREOIDX)) { |
1079 | 0 | synth = pWorkBuffer1 + ((PIT_MAX_MAX - (1 * L_SUBFR)) * fac_FB); |
1080 | 0 | } else { |
1081 | 0 | synth = pWorkBuffer1 + PIT_MAX_MAX * fac_FB; |
1082 | 0 | } |
1083 | |
|
1084 | 0 | int fac_length = |
1085 | 0 | (pAacDecoderChannelInfo->icsInfo.WindowSequence == BLOCK_SHORT) |
1086 | 0 | ? (frameLen >> 4) |
1087 | 0 | : (frameLen >> 3); |
1088 | |
|
1089 | 0 | INT pitch[NB_SUBFR_SUPERFR + SYN_SFD]; |
1090 | 0 | FIXP_DBL pit_gain[NB_SUBFR_SUPERFR + SYN_SFD]; |
1091 | |
|
1092 | 0 | int nbDiv = (elFlags & AC_EL_FULLBANDLPD) ? 2 : 4; |
1093 | 0 | int lFrame = (elFlags & AC_EL_FULLBANDLPD) ? frameLen / 2 : frameLen; |
1094 | 0 | int nbSubfr = |
1095 | 0 | lFrame / (nbDiv * L_SUBFR); /* number of subframes per division */ |
1096 | 0 | int LpdSfd = (nbDiv * nbSubfr) >> 1; |
1097 | 0 | int SynSfd = LpdSfd - BPF_SFD; |
1098 | |
|
1099 | 0 | FDKmemclear( |
1100 | 0 | pitch, |
1101 | 0 | sizeof( |
1102 | 0 | pitch)); // added to prevent ferret errors in bass_pf_1sf_delay |
1103 | 0 | FDKmemclear(pit_gain, sizeof(pit_gain)); |
1104 | | |
1105 | | /* FAC case */ |
1106 | 0 | if (pAacDecoderStaticChannelInfo->last_lpd_mode == 0 || |
1107 | 0 | pAacDecoderStaticChannelInfo->last_lpd_mode == 4) { |
1108 | 0 | FIXP_DBL fac_buf[LFAC]; |
1109 | 0 | FIXP_LPC *A = pAacDecoderChannelInfo->data.usac.lp_coeff[0]; |
1110 | |
|
1111 | 0 | if (!frameOk || last_frame_lost || |
1112 | 0 | (pAacDecoderChannelInfo->data.usac.fac_data[0] == NULL)) { |
1113 | 0 | FDKmemclear(fac_buf, |
1114 | 0 | pAacDecoderChannelInfo->granuleLength * sizeof(FIXP_DBL)); |
1115 | 0 | pAacDecoderChannelInfo->data.usac.fac_data[0] = fac_buf; |
1116 | 0 | pAacDecoderChannelInfo->data.usac.fac_data_e[0] = 0; |
1117 | 0 | } |
1118 | |
|
1119 | 0 | INT A_exp; /* linear prediction coefficients exponent */ |
1120 | 0 | { |
1121 | 0 | for (int i = 0; i < M_LP_FILTER_ORDER; i++) { |
1122 | 0 | A[i] = FX_DBL2FX_LPC(fixp_cos( |
1123 | 0 | fMult(pAacDecoderStaticChannelInfo->lpc4_lsf[i], |
1124 | 0 | FL2FXCONST_SGL((1 << LSPARG_SCALE) * M_PI / 6400.0)), |
1125 | 0 | LSF_SCALE - LSPARG_SCALE)); |
1126 | 0 | } |
1127 | |
|
1128 | 0 | E_LPC_f_lsp_a_conversion(A, A, &A_exp); |
1129 | 0 | } |
1130 | |
|
1131 | 0 | #if defined(FDK_ASSERT_ENABLE) |
1132 | 0 | nSamples = |
1133 | 0 | #endif |
1134 | 0 | CLpd_FAC_Acelp2Mdct( |
1135 | 0 | &pAacDecoderStaticChannelInfo->IMdct, synth, |
1136 | 0 | SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient), |
1137 | 0 | pAacDecoderChannelInfo->specScale, nSpec, |
1138 | 0 | pAacDecoderChannelInfo->data.usac.fac_data[0], |
1139 | 0 | pAacDecoderChannelInfo->data.usac.fac_data_e[0], fac_length, |
1140 | 0 | frameLen, tl, |
1141 | 0 | FDKgetWindowSlope( |
1142 | 0 | fr, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1143 | 0 | fr, A, A_exp, &pAacDecoderStaticChannelInfo->acelp, |
1144 | 0 | (FIXP_DBL)0, /* FAC gain has already been applied. */ |
1145 | 0 | (last_frame_lost || !frameOk), 1, |
1146 | 0 | pAacDecoderStaticChannelInfo->last_lpd_mode, 0, |
1147 | 0 | pAacDecoderChannelInfo->currAliasingSymmetry); |
1148 | |
|
1149 | 0 | } else { |
1150 | 0 | #if defined(FDK_ASSERT_ENABLE) |
1151 | 0 | nSamples = |
1152 | 0 | #endif |
1153 | 0 | imlt_block( |
1154 | 0 | &pAacDecoderStaticChannelInfo->IMdct, synth, |
1155 | 0 | SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient), |
1156 | 0 | pAacDecoderChannelInfo->specScale, nSpec, frameLen, tl, |
1157 | 0 | FDKgetWindowSlope( |
1158 | 0 | fl, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1159 | 0 | fl, |
1160 | 0 | FDKgetWindowSlope( |
1161 | 0 | fr, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1162 | 0 | fr, (FIXP_DBL)0, |
1163 | 0 | pAacDecoderChannelInfo->currAliasingSymmetry |
1164 | 0 | ? MLT_FLAG_CURR_ALIAS_SYMMETRY |
1165 | 0 | : 0); |
1166 | 0 | } |
1167 | 0 | FDK_ASSERT(nSamples == frameLen); |
1168 | | |
1169 | | /* The "if" clause is entered both for fullbandLpd mono and |
1170 | | * non-fullbandLpd*. The "else"-> just for fullbandLpd stereo*/ |
1171 | 0 | if (!(elFlags & AC_EL_LPDSTEREOIDX)) { |
1172 | 0 | FDKmemcpy(pitch, pAacDecoderStaticChannelInfo->old_T_pf, |
1173 | 0 | SynSfd * sizeof(INT)); |
1174 | 0 | FDKmemcpy(pit_gain, pAacDecoderStaticChannelInfo->old_gain_pf, |
1175 | 0 | SynSfd * sizeof(FIXP_DBL)); |
1176 | |
|
1177 | 0 | for (int i = SynSfd; i < LpdSfd + 3; i++) { |
1178 | 0 | pitch[i] = L_SUBFR; |
1179 | 0 | pit_gain[i] = (FIXP_DBL)0; |
1180 | 0 | } |
1181 | |
|
1182 | 0 | if (pAacDecoderStaticChannelInfo->last_lpd_mode == 0) { |
1183 | 0 | pitch[SynSfd] = pitch[SynSfd - 1]; |
1184 | 0 | pit_gain[SynSfd] = pit_gain[SynSfd - 1]; |
1185 | 0 | if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) { |
1186 | 0 | pitch[SynSfd + 1] = pitch[SynSfd]; |
1187 | 0 | pit_gain[SynSfd + 1] = pit_gain[SynSfd]; |
1188 | 0 | } |
1189 | 0 | } |
1190 | | |
1191 | | /* Copy old data to the beginning of the buffer */ |
1192 | 0 | { |
1193 | 0 | FDKmemcpy( |
1194 | 0 | pWorkBuffer1, pAacDecoderStaticChannelInfo->old_synth, |
1195 | 0 | ((PIT_MAX_MAX - (1 * L_SUBFR)) * fac_FB) * sizeof(FIXP_DBL)); |
1196 | 0 | } |
1197 | |
|
1198 | 0 | FIXP_DBL *p2_synth = pWorkBuffer1 + (PIT_MAX_MAX * fac_FB); |
1199 | | |
1200 | | /* recalculate pitch gain to allow postfilering on FAC area */ |
1201 | 0 | for (int i = 0; i < SynSfd + 2; i++) { |
1202 | 0 | int T = pitch[i]; |
1203 | 0 | FIXP_DBL gain = pit_gain[i]; |
1204 | |
|
1205 | 0 | if (gain > (FIXP_DBL)0) { |
1206 | 0 | gain = get_gain(&p2_synth[i * L_SUBFR * fac_FB], |
1207 | 0 | &p2_synth[(i * L_SUBFR * fac_FB) - fac_FB * T], |
1208 | 0 | L_SUBFR * fac_FB); |
1209 | 0 | pit_gain[i] = gain; |
1210 | 0 | } |
1211 | 0 | } |
1212 | |
|
1213 | 0 | bass_pf_1sf_delay(p2_synth, pitch, pit_gain, frameLen, |
1214 | 0 | (LpdSfd + 2) * L_SUBFR + BPF_SFD * L_SUBFR, |
1215 | 0 | frameLen - (LpdSfd + 4) * L_SUBFR, outSamples, |
1216 | 0 | aacOutDataHeadroom, |
1217 | 0 | pAacDecoderStaticChannelInfo->mem_bpf); |
1218 | 0 | } |
1219 | |
|
1220 | 0 | } else /* last_core_mode was not LPD */ |
1221 | 0 | { |
1222 | 0 | FIXP_DBL *tmp = |
1223 | 0 | pAacDecoderChannelInfo->pComStaticData->pWorkBufferCore1->mdctOutTemp; |
1224 | 0 | #if defined(FDK_ASSERT_ENABLE) |
1225 | 0 | nSamples = |
1226 | 0 | #endif |
1227 | 0 | imlt_block(&pAacDecoderStaticChannelInfo->IMdct, tmp, |
1228 | 0 | SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient), |
1229 | 0 | pAacDecoderChannelInfo->specScale, nSpec, frameLen, tl, |
1230 | 0 | FDKgetWindowSlope( |
1231 | 0 | fl, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1232 | 0 | fl, |
1233 | 0 | FDKgetWindowSlope( |
1234 | 0 | fr, GetWindowShape(&pAacDecoderChannelInfo->icsInfo)), |
1235 | 0 | fr, (FIXP_DBL)0, |
1236 | 0 | pAacDecoderChannelInfo->currAliasingSymmetry |
1237 | 0 | ? MLT_FLAG_CURR_ALIAS_SYMMETRY |
1238 | 0 | : 0); |
1239 | |
|
1240 | 0 | scaleValuesSaturate(outSamples, tmp, frameLen, |
1241 | 0 | MDCT_OUT_HEADROOM - aacOutDataHeadroom); |
1242 | 0 | } |
1243 | 0 | } |
1244 | | |
1245 | 0 | FDK_ASSERT(nSamples == frameLen); |
1246 | | |
1247 | 0 | pAacDecoderStaticChannelInfo->last_core_mode = |
1248 | 0 | (pAacDecoderChannelInfo->icsInfo.WindowSequence == BLOCK_SHORT) ? FD_SHORT |
1249 | 0 | : FD_LONG; |
1250 | 0 | pAacDecoderStaticChannelInfo->last_lpd_mode = 255; |
1251 | 0 | } |
1252 | | |
1253 | | #include "ldfiltbank.h" |
1254 | | void CBlock_FrequencyToTimeLowDelay( |
1255 | | CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo, |
1256 | | CAacDecoderChannelInfo *pAacDecoderChannelInfo, PCM_DEC outSamples[], |
1257 | 0 | const short frameLen) { |
1258 | 0 | InvMdctTransformLowDelay_fdk( |
1259 | 0 | SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient), |
1260 | 0 | pAacDecoderChannelInfo->specScale[0], outSamples, |
1261 | 0 | pAacDecoderStaticChannelInfo->pOverlapBuffer, frameLen); |
1262 | 0 | } |