/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 | 816k | void CTns_Reset(CTnsData *pTnsData) { |
120 | | /* Note: the following FDKmemclear should not be required. */ |
121 | 816k | FDKmemclear(pTnsData->Filter, |
122 | 816k | TNS_MAX_WINDOWS * TNS_MAXIMUM_FILTERS * sizeof(CFilter)); |
123 | 816k | FDKmemclear(pTnsData->NumberOfFilters, TNS_MAX_WINDOWS * sizeof(UCHAR)); |
124 | 816k | pTnsData->DataPresent = 0; |
125 | 816k | pTnsData->Active = 0; |
126 | 816k | } |
127 | | |
128 | | void CTns_ReadDataPresentFlag( |
129 | | HANDLE_FDK_BITSTREAM bs, /*!< pointer to bitstream */ |
130 | | CTnsData *pTnsData) /*!< pointer to aac decoder channel info */ |
131 | 613k | { |
132 | 613k | pTnsData->DataPresent = (UCHAR)FDKreadBits(bs, 1); |
133 | 613k | } |
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 | 797k | const CIcsInfo *pIcsInfo, const UINT flags) { |
145 | 797k | UCHAR n_filt, order; |
146 | 797k | UCHAR length, coef_res, coef_compress; |
147 | 797k | UCHAR window; |
148 | 797k | UCHAR wins_per_frame; |
149 | 797k | UCHAR isLongFlag; |
150 | 797k | UCHAR start_window; |
151 | 797k | AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK; |
152 | | |
153 | 797k | if (!pTnsData->DataPresent) { |
154 | 666k | return ErrorStatus; |
155 | 666k | } |
156 | | |
157 | 131k | { |
158 | 131k | start_window = 0; |
159 | 131k | wins_per_frame = GetWindowsPerFrame(pIcsInfo); |
160 | 131k | isLongFlag = IsLongBlock(pIcsInfo); |
161 | 131k | } |
162 | | |
163 | 131k | pTnsData->GainLd = 0; |
164 | | |
165 | 546k | for (window = start_window; window < wins_per_frame; window++) { |
166 | 415k | pTnsData->NumberOfFilters[window] = n_filt = |
167 | 415k | (UCHAR)FDKreadBits(bs, isLongFlag ? 2 : 1); |
168 | | |
169 | 415k | if (n_filt) { |
170 | 167k | int index; |
171 | 167k | UCHAR nextstopband; |
172 | | |
173 | 167k | coef_res = (UCHAR)FDKreadBits(bs, 1); |
174 | | |
175 | 167k | nextstopband = GetScaleFactorBandsTotal(pIcsInfo); |
176 | | |
177 | 359k | for (index = 0; index < n_filt; index++) { |
178 | 192k | CFilter *filter = &pTnsData->Filter[window][index]; |
179 | | |
180 | 192k | length = (UCHAR)FDKreadBits(bs, isLongFlag ? 6 : 4); |
181 | | |
182 | 192k | if (length > nextstopband) { |
183 | 45.1k | length = nextstopband; |
184 | 45.1k | } |
185 | | |
186 | 192k | filter->StartBand = nextstopband - length; |
187 | 192k | filter->StopBand = nextstopband; |
188 | 192k | nextstopband = filter->StartBand; |
189 | | |
190 | 192k | if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) { |
191 | | /* max(Order) = 15 (long), 7 (short) */ |
192 | 96.4k | filter->Order = order = (UCHAR)FDKreadBits(bs, isLongFlag ? 4 : 3); |
193 | 96.4k | } else { |
194 | 95.7k | filter->Order = order = (UCHAR)FDKreadBits(bs, isLongFlag ? 5 : 3); |
195 | | |
196 | 95.7k | if (filter->Order > TNS_MAXIMUM_ORDER) { |
197 | 97 | ErrorStatus = AAC_DEC_TNS_READ_ERROR; |
198 | 97 | return ErrorStatus; |
199 | 97 | } |
200 | 95.7k | } |
201 | | |
202 | 192k | FDK_ASSERT(order <= |
203 | 192k | TNS_MAXIMUM_ORDER); /* avoid illegal memory access */ |
204 | 192k | if (order) { |
205 | 108k | UCHAR coef, s_mask; |
206 | 108k | UCHAR i; |
207 | 108k | SCHAR n_mask; |
208 | | |
209 | 108k | static const UCHAR sgn_mask[] = {0x2, 0x4, 0x8}; |
210 | 108k | static const SCHAR neg_mask[] = {~0x3, ~0x7, ~0xF}; |
211 | | |
212 | 108k | filter->Direction = FDKreadBits(bs, 1) ? -1 : 1; |
213 | | |
214 | 108k | coef_compress = (UCHAR)FDKreadBits(bs, 1); |
215 | | |
216 | 108k | filter->Resolution = coef_res + 3; |
217 | | |
218 | 108k | s_mask = sgn_mask[coef_res + 1 - coef_compress]; |
219 | 108k | n_mask = neg_mask[coef_res + 1 - coef_compress]; |
220 | | |
221 | 752k | for (i = 0; i < order; i++) { |
222 | 644k | coef = (UCHAR)FDKreadBits(bs, filter->Resolution - coef_compress); |
223 | 644k | filter->Coeff[i] = (coef & s_mask) ? (coef | n_mask) : coef; |
224 | 644k | } |
225 | 108k | pTnsData->GainLd = 4; |
226 | 108k | } |
227 | 192k | } |
228 | 167k | } |
229 | 415k | } |
230 | | |
231 | 131k | pTnsData->Active = 1; |
232 | | |
233 | 131k | return ErrorStatus; |
234 | 131k | } |
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 | 44.1k | const UINT elFlags, const int fCommonWindow) { |
240 | 44.1k | int common_tns = 0; |
241 | | |
242 | 44.1k | if (fCommonWindow) { |
243 | 43.7k | common_tns = FDKreadBit(hBs); |
244 | 43.7k | } |
245 | 44.1k | { *ptns_on_lr = FDKreadBit(hBs); } |
246 | 44.1k | if (common_tns) { |
247 | 30.7k | pTnsData0->DataPresent = 1; |
248 | 30.7k | CTns_Read(hBs, pTnsData0, pIcsInfo, flags); |
249 | | |
250 | 30.7k | pTnsData0->DataPresent = 0; |
251 | 30.7k | pTnsData0->Active = 1; |
252 | 30.7k | *pTnsData1 = *pTnsData0; |
253 | 30.7k | } else { |
254 | 13.4k | int tns_present_both; |
255 | | |
256 | 13.4k | tns_present_both = FDKreadBit(hBs); |
257 | 13.4k | if (tns_present_both) { |
258 | 2.33k | pTnsData0->DataPresent = 1; |
259 | 2.33k | pTnsData1->DataPresent = 1; |
260 | 11.0k | } else { |
261 | 11.0k | pTnsData1->DataPresent = FDKreadBit(hBs); |
262 | 11.0k | pTnsData0->DataPresent = !pTnsData1->DataPresent; |
263 | 11.0k | } |
264 | 13.4k | } |
265 | 44.1k | } |
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 | 689k | const UCHAR igf_active, const UINT flags) { |
279 | 689k | int window, index, start, stop, size, start_window, wins_per_frame; |
280 | | |
281 | 689k | if (pTnsData->Active) { |
282 | 160k | C_AALLOC_SCRATCH_START(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER) |
283 | | |
284 | 160k | { |
285 | 160k | start_window = 0; |
286 | 160k | wins_per_frame = GetWindowsPerFrame(pIcsInfo); |
287 | 160k | } |
288 | | |
289 | 742k | for (window = start_window; window < wins_per_frame; window++) { |
290 | 581k | FIXP_DBL *pSpectrum; |
291 | | |
292 | 581k | { pSpectrum = SPEC(pSpectralCoefficient, window, granuleLength); } |
293 | | |
294 | 854k | for (index = 0; index < pTnsData->NumberOfFilters[window]; index++) { |
295 | 272k | CFilter *filter = &pTnsData->Filter[window][index]; |
296 | | |
297 | 272k | if (filter->Order > 0) { |
298 | 149k | FIXP_TCC *pCoeff; |
299 | 149k | UCHAR tns_max_bands; |
300 | | |
301 | 149k | pCoeff = coeff; |
302 | 149k | if (filter->Resolution == 3) { |
303 | 38.4k | int i; |
304 | 297k | for (i = 0; i < filter->Order; i++) |
305 | 258k | *pCoeff++ = FDKaacDec_tnsCoeff3[filter->Coeff[i] + 4]; |
306 | 111k | } else { |
307 | 111k | int i; |
308 | 666k | for (i = 0; i < filter->Order; i++) |
309 | 555k | *pCoeff++ = FDKaacDec_tnsCoeff4[filter->Coeff[i] + 8]; |
310 | 111k | } |
311 | | |
312 | 149k | switch (granuleLength) { |
313 | 24.6k | case 480: |
314 | 24.6k | tns_max_bands = |
315 | 24.6k | tns_max_bands_tbl_480[pSamplingRateInfo->samplingRateIndex]; |
316 | 24.6k | break; |
317 | 7.48k | case 512: |
318 | 7.48k | tns_max_bands = |
319 | 7.48k | tns_max_bands_tbl_512[pSamplingRateInfo->samplingRateIndex]; |
320 | 7.48k | break; |
321 | 117k | default: |
322 | 117k | tns_max_bands = GetMaximumTnsBands( |
323 | 117k | pIcsInfo, pSamplingRateInfo->samplingRateIndex); |
324 | | /* See redefinition of TNS_MAX_BANDS table */ |
325 | 117k | if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) && |
326 | 117k | (pSamplingRateInfo->samplingRateIndex > 5)) { |
327 | 89.9k | tns_max_bands += 1; |
328 | 89.9k | } |
329 | 117k | break; |
330 | 149k | } |
331 | | |
332 | 149k | start = fixMin(fixMin(filter->StartBand, tns_max_bands), nbands); |
333 | | |
334 | 149k | start = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[start]; |
335 | | |
336 | 149k | if (igf_active) { |
337 | 0 | stop = fixMin(filter->StopBand, nbands); |
338 | 149k | } else { |
339 | 149k | stop = fixMin(fixMin(filter->StopBand, tns_max_bands), nbands); |
340 | 149k | } |
341 | | |
342 | 149k | stop = GetScaleFactorBandOffsets(pIcsInfo, pSamplingRateInfo)[stop]; |
343 | | |
344 | 149k | size = stop - start; |
345 | | |
346 | 149k | if (size) { |
347 | 9.67k | C_ALLOC_SCRATCH_START(state, FIXP_DBL, TNS_MAXIMUM_ORDER) |
348 | | |
349 | 9.67k | FDKmemclear(state, TNS_MAXIMUM_ORDER * sizeof(FIXP_DBL)); |
350 | 9.67k | CLpc_SynthesisLattice(pSpectrum + start, size, 0, 0, |
351 | 9.67k | filter->Direction, coeff, filter->Order, |
352 | 9.67k | state); |
353 | | |
354 | 9.67k | C_ALLOC_SCRATCH_END(state, FIXP_DBL, TNS_MAXIMUM_ORDER) |
355 | 9.67k | } |
356 | 149k | } |
357 | 272k | } |
358 | 581k | } |
359 | 160k | C_AALLOC_SCRATCH_END(coeff, FIXP_TCC, TNS_MAXIMUM_ORDER) |
360 | 160k | } |
361 | 689k | } |