/src/aac/libAACdec/src/aacdec_tns.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 | | /**************************** AAC decoder library ****************************** |
96 | | |
97 | | Author(s): Josef Hoepfl |
98 | | |
99 | | Description: temporal noise shaping tool |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "aacdec_tns.h" |
104 | | #include "aac_rom.h" |
105 | | #include "FDK_bitstream.h" |
106 | | #include "channelinfo.h" |
107 | | |
108 | | #include "FDK_lpc.h" |
109 | | |
110 | | #define TNS_MAXIMUM_ORDER_AAC 12 |
111 | | |
112 | | /*! |
113 | | \brief Reset tns data |
114 | | |
115 | | The function resets the tns data |
116 | | |
117 | | \return none |
118 | | */ |
119 | 0 | void CTns_Reset(CTnsData *pTnsData) { |
120 | | /* Note: the following FDKmemclear should not be required. */ |
121 | 0 | FDKmemclear(pTnsData->Filter, |
122 | 0 | TNS_MAX_WINDOWS * TNS_MAXIMUM_FILTERS * sizeof(CFilter)); |
123 | 0 | FDKmemclear(pTnsData->NumberOfFilters, TNS_MAX_WINDOWS * sizeof(UCHAR)); |
124 | 0 | pTnsData->DataPresent = 0; |
125 | 0 | pTnsData->Active = 0; |
126 | 0 | } |
127 | | |
128 | | void CTns_ReadDataPresentFlag( |
129 | | HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */ |
130 | | CTnsData *pTnsData) /*!< pointer to aac decoder channel info */ |
131 | 0 | { |
132 | 0 | pTnsData->DataPresent = (UCHAR)FDKreadBits(bs, 1); |
133 | 0 | } |
134 | | |
135 | | /*! |
136 | | \brief Read tns data from bitstream |
137 | | |
138 | | The function reads the elements for tns from |
139 | | the bitstream. |
140 | | |
141 | | \return none |
142 | | */ |
143 | | AAC_DECODER_ERROR CTns_Read(HANDLE_FDK_BITSTREAM bs, CTnsData *pTnsData, |
144 | 0 | const CIcsInfo *pIcsInfo, const UINT flags) { |
145 | 0 | UCHAR n_filt, order; |
146 | 0 | UCHAR length, coef_res, coef_compress; |
147 | 0 | UCHAR window; |
148 | 0 | UCHAR wins_per_frame; |
149 | 0 | UCHAR isLongFlag; |
150 | 0 | UCHAR start_window; |
151 | 0 | AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK; |
152 | |
|
153 | 0 | if (!pTnsData->DataPresent) { |
154 | 0 | return ErrorStatus; |
155 | 0 | } |
156 | | |
157 | 0 | { |
158 | 0 | start_window = 0; |
159 | 0 | wins_per_frame = GetWindowsPerFrame(pIcsInfo); |
160 | 0 | isLongFlag = IsLongBlock(pIcsInfo); |
161 | 0 | } |
162 | |
|
163 | 0 | pTnsData->GainLd = 0; |
164 | |
|
165 | 0 | for (window = start_window; window < wins_per_frame; window++) { |
166 | 0 | pTnsData->NumberOfFilters[window] = n_filt = |
167 | 0 | (UCHAR)FDKreadBits(bs, isLongFlag ? 2 : 1); |
168 | |
|
169 | 0 | if (n_filt) { |
170 | 0 | int index; |
171 | 0 | UCHAR nextstopband; |
172 | |
|
173 | 0 | coef_res = (UCHAR)FDKreadBits(bs, 1); |
174 | |
|
175 | 0 | nextstopband = GetScaleFactorBandsTotal(pIcsInfo); |
176 | |
|
177 | 0 | for (index = 0; index < n_filt; index++) { |
178 | 0 | CFilter *filter = &pTnsData->Filter[window][index]; |
179 | |
|
180 | 0 | length = (UCHAR)FDKreadBits(bs, isLongFlag ? 6 : 4); |
181 | |
|
182 | 0 | if (length > nextstopband) { |
183 | 0 | length = nextstopband; |
184 | 0 | } |
185 | |
|
186 | 0 | filter->StartBand = nextstopband - length; |
187 | 0 | filter->StopBand = nextstopband; |
188 | 0 | nextstopband = filter->StartBand; |
189 | |
|
190 | 0 | if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) { |
191 | | /* max(Order) = 15 (long), 7 (short) */ |
192 | 0 | filter->Order = order = (UCHAR)FDKreadBits(bs, isLongFlag ? 4 : 3); |
193 | 0 | } else { |
194 | 0 | filter->Order = order = (UCHAR)FDKreadBits(bs, isLongFlag ? 5 : 3); |
195 | |
|
196 | 0 | if (filter->Order > TNS_MAXIMUM_ORDER) { |
197 | 0 | ErrorStatus = AAC_DEC_TNS_READ_ERROR; |
198 | 0 | return ErrorStatus; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | 0 | FDK_ASSERT(order <= |
203 | 0 | TNS_MAXIMUM_ORDER); /* avoid illegal memory access */ |
204 | 0 | if (order) { |
205 | 0 | UCHAR coef, s_mask; |
206 | 0 | UCHAR i; |
207 | 0 | SCHAR n_mask; |
208 | |
|
209 | 0 | static const UCHAR sgn_mask[] = {0x2, 0x4, 0x8}; |
210 | 0 | static const SCHAR neg_mask[] = {~0x3, ~0x7, ~0xF}; |
211 | |
|
212 | 0 | filter->Direction = FDKreadBits(bs, 1) ? -1 : 1; |
213 | |
|
214 | 0 | coef_compress = (UCHAR)FDKreadBits(bs, 1); |
215 | |
|
216 | 0 | filter->Resolution = coef_res + 3; |
217 | |
|
218 | 0 | s_mask = sgn_mask[coef_res + 1 - coef_compress]; |
219 | 0 | n_mask = neg_mask[coef_res + 1 - coef_compress]; |
220 | |
|
221 | 0 | for (i = 0; i < order; i++) { |
222 | 0 | coef = (UCHAR)FDKreadBits(bs, filter->Resolution - coef_compress); |
223 | 0 | filter->Coeff[i] = (coef & s_mask) ? (coef | n_mask) : coef; |
224 | 0 | } |
225 | 0 | pTnsData->GainLd = 4; |
226 | 0 | } |
227 | 0 | } |
228 | 0 | } |
229 | 0 | } |
230 | | |
231 | 0 | pTnsData->Active = 1; |
232 | |
|
233 | 0 | return ErrorStatus; |
234 | 0 | } |
235 | | |
236 | | void CTns_ReadDataPresentUsac(HANDLE_FDK_BITSTREAM hBs, CTnsData *pTnsData0, |
237 | | CTnsData *pTnsData1, UCHAR *ptns_on_lr, |
238 | | const CIcsInfo *pIcsInfo, const UINT flags, |
239 | 0 | const UINT elFlags, const int fCommonWindow) { |
240 | 0 | int common_tns = 0; |
241 | |
|
242 | 0 | if (fCommonWindow) { |
243 | 0 | common_tns = FDKreadBit(hBs); |
244 | 0 | } |
245 | 0 | { *ptns_on_lr = FDKreadBit(hBs); } |
246 | 0 | if (common_tns) { |
247 | 0 | pTnsData0->DataPresent = 1; |
248 | 0 | CTns_Read(hBs, pTnsData0, pIcsInfo, flags); |
249 | |
|
250 | 0 | pTnsData0->DataPresent = 0; |
251 | 0 | pTnsData0->Active = 1; |
252 | 0 | *pTnsData1 = *pTnsData0; |
253 | 0 | } else { |
254 | 0 | int tns_present_both; |
255 | |
|
256 | 0 | tns_present_both = FDKreadBit(hBs); |
257 | 0 | if (tns_present_both) { |
258 | 0 | pTnsData0->DataPresent = 1; |
259 | 0 | pTnsData1->DataPresent = 1; |
260 | 0 | } else { |
261 | 0 | pTnsData1->DataPresent = FDKreadBit(hBs); |
262 | 0 | pTnsData0->DataPresent = !pTnsData1->DataPresent; |
263 | 0 | } |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | | /*! |
268 | | \brief Apply tns to spectral lines |
269 | | |
270 | | The function applies the tns to the spectrum, |
271 | | |
272 | | \return none |
273 | | */ |
274 | | void CTns_Apply(CTnsData *RESTRICT pTnsData, /*!< pointer to aac decoder info */ |
275 | | const CIcsInfo *pIcsInfo, SPECTRAL_PTR pSpectralCoefficient, |
276 | | const SamplingRateInfo *pSamplingRateInfo, |
277 | | const INT granuleLength, const UCHAR nbands, |
278 | 0 | const UCHAR igf_active, const UINT flags) { |
279 | 0 | int window, index, start, stop, size, start_window, wins_per_frame; |
280 | |
|
281 | 0 | if (pTnsData->Active) { |
282 | 0 | C_AALLOC_SCRATCH_START(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER) |
283 | |
|
284 | 0 | { |
285 | 0 | start_window = 0; |
286 | 0 | wins_per_frame = GetWindowsPerFrame(pIcsInfo); |
287 | 0 | } |
288 | |
|
289 | 0 | for (window = start_window; window < wins_per_frame; window++) { |
290 | 0 | FIXP_DBL *pSpectrum; |
291 | |
|
292 | 0 | { pSpectrum = SPEC(pSpectralCoefficient, window, granuleLength); } |
293 | |
|
294 | 0 | for (index = 0; index < pTnsData->NumberOfFilters[window]; index++) { |
295 | 0 | CFilter *filter = &pTnsData->Filter[window][index]; |
296 | |
|
297 | 0 | if (filter->Order > 0) { |
298 | 0 | FIXP_TCC *pCoeff; |
299 | 0 | UCHAR tns_max_bands; |
300 | |
|
301 | 0 | pCoeff = coeff; |
302 | 0 | if (filter->Resolution == 3) { |
303 | 0 | int i; |
304 | 0 | for (i = 0; i < filter->Order; i++) |
305 | 0 | *pCoeff++ = FDKaacDec_tnsCoeff3[filter->Coeff[i] + 4]; |
306 | 0 | } else { |
307 | 0 | int i; |
308 | 0 | for (i = 0; i < filter->Order; i++) |
309 | 0 | *pCoeff++ = FDKaacDec_tnsCoeff4[filter->Coeff[i] + 8]; |
310 | 0 | } |
311 | |
|
312 | 0 | switch (granuleLength) { |
313 | 0 | case 480: |
314 | 0 | tns_max_bands = |
315 | 0 | tns_max_bands_tbl_480[pSamplingRateInfo->samplingRateIndex]; |
316 | 0 | break; |
317 | 0 | case 512: |
318 | 0 | tns_max_bands = |
319 | 0 | tns_max_bands_tbl_512[pSamplingRateInfo->samplingRateIndex]; |
320 | 0 | break; |
321 | 0 | default: |
322 | 0 | tns_max_bands = GetMaximumTnsBands( |
323 | 0 | pIcsInfo, pSamplingRateInfo->samplingRateIndex); |
324 | | /* See redefinition of TNS_MAX_BANDS table */ |
325 | 0 | if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) && |
326 | 0 | (pSamplingRateInfo->samplingRateIndex > 5)) { |
327 | 0 | tns_max_bands += 1; |
328 | 0 | } |
329 | 0 | break; |
330 | 0 | } |
331 | | |
332 | 0 | start = fixMin(fixMin(filter->StartBand, tns_max_bands), nbands); |
333 | |
|
334 | 0 | start = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[start]; |
335 | |
|
336 | 0 | if (igf_active) { |
337 | 0 | stop = fixMin(filter->StopBand, nbands); |
338 | 0 | } else { |
339 | 0 | stop = fixMin(fixMin(filter->StopBand, tns_max_bands), nbands); |
340 | 0 | } |
341 | |
|
342 | 0 | stop = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[stop]; |
343 | |
|
344 | 0 | size = stop - start; |
345 | |
|
346 | 0 | if (size) { |
347 | 0 | C_ALLOC_SCRATCH_START(state, FIXP_DBL, TNS_MAXIMUM_ORDER) |
348 | |
|
349 | 0 | FDKmemclear(state, TNS_MAXIMUM_ORDER * sizeof(FIXP_DBL)); |
350 | 0 | CLpc_SynthesisLattice(pSpectrum + start, size, 0, 0, |
351 | 0 | filter->Direction, coeff, filter->Order, |
352 | 0 | state); |
353 | |
|
354 | 0 | C_ALLOC_SCRATCH_END(state, FIXP_DBL, TNS_MAXIMUM_ORDER) |
355 | 0 | } |
356 | 0 | } |
357 | 0 | } |
358 | 0 | } |
359 | 0 | C_AALLOC_SCRATCH_END(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER) |
360 | 0 | } |
361 | 0 | } |