/src/aac/libFDK/src/autocorr2nd.cpp
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2020 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /******************* Library for basic calculation routines ******************** |
96 | | |
97 | | Author(s): M. Lohwasser |
98 | | |
99 | | Description: auto-correlation functions |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "autocorr2nd.h" |
104 | | |
105 | | /*! |
106 | | * |
107 | | * \brief Calculate second order autocorrelation using 2 accumulators |
108 | | * |
109 | | */ |
110 | | #if !defined(FUNCTION_autoCorr2nd_real) |
111 | | INT autoCorr2nd_real( |
112 | | ACORR_COEFS *ac, /*!< Pointer to autocorrelation coeffs */ |
113 | | const FIXP_DBL *reBuffer, /*!< Pointer to to real part of input samples */ |
114 | | const int len /*!< Number input samples */ |
115 | 1.73M | ) { |
116 | 1.73M | int j, autoCorrScaling, mScale; |
117 | | |
118 | 1.73M | FIXP_DBL accu1, accu2, accu3, accu4, accu5; |
119 | | |
120 | 1.73M | const FIXP_DBL *pReBuf; |
121 | | |
122 | 1.73M | const FIXP_DBL *realBuf = reBuffer; |
123 | | |
124 | 1.73M | const int len_scale = fMax(DFRACT_BITS - fNormz((FIXP_DBL)(len / 2)), 1); |
125 | | /* |
126 | | r11r,r22r |
127 | | r01r,r12r |
128 | | r02r |
129 | | */ |
130 | 1.73M | pReBuf = realBuf - 2; |
131 | 1.73M | accu5 = |
132 | 1.73M | ((fMultDiv2(pReBuf[0], pReBuf[2]) + fMultDiv2(pReBuf[1], pReBuf[3])) >> |
133 | 1.73M | len_scale); |
134 | 1.73M | pReBuf++; |
135 | | |
136 | | /* len must be even */ |
137 | 1.73M | accu1 = fPow2Div2(pReBuf[0]) >> len_scale; |
138 | 1.73M | accu3 = fMultDiv2(pReBuf[0], pReBuf[1]) >> len_scale; |
139 | 1.73M | pReBuf++; |
140 | | |
141 | 30.4M | for (j = (len - 2) >> 1; j != 0; j--, pReBuf += 2) { |
142 | 28.7M | accu1 += ((fPow2Div2(pReBuf[0]) + fPow2Div2(pReBuf[1])) >> len_scale); |
143 | | |
144 | 28.7M | accu3 += |
145 | 28.7M | ((fMultDiv2(pReBuf[0], pReBuf[1]) + fMultDiv2(pReBuf[1], pReBuf[2])) >> |
146 | 28.7M | len_scale); |
147 | | |
148 | 28.7M | accu5 += |
149 | 28.7M | ((fMultDiv2(pReBuf[0], pReBuf[2]) + fMultDiv2(pReBuf[1], pReBuf[3])) >> |
150 | 28.7M | len_scale); |
151 | 28.7M | } |
152 | | |
153 | 1.73M | accu2 = (fPow2Div2(realBuf[-2]) >> len_scale); |
154 | 1.73M | accu2 += accu1; |
155 | | |
156 | 1.73M | accu1 += (fPow2Div2(realBuf[len - 2]) >> len_scale); |
157 | | |
158 | 1.73M | accu4 = (fMultDiv2(realBuf[-1], realBuf[-2]) >> len_scale); |
159 | 1.73M | accu4 += accu3; |
160 | | |
161 | 1.73M | accu3 += (fMultDiv2(realBuf[len - 1], realBuf[len - 2]) >> len_scale); |
162 | | |
163 | 1.73M | mScale = CntLeadingZeros( |
164 | 1.73M | (accu1 | accu2 | fAbs(accu3) | fAbs(accu4) | fAbs(accu5))) - |
165 | 1.73M | 1; |
166 | 1.73M | autoCorrScaling = mScale - 1 - len_scale; /* -1 because of fMultDiv2*/ |
167 | | |
168 | | /* Scale to common scale factor */ |
169 | 1.73M | ac->r11r = accu1 << mScale; |
170 | 1.73M | ac->r22r = accu2 << mScale; |
171 | 1.73M | ac->r01r = accu3 << mScale; |
172 | 1.73M | ac->r12r = accu4 << mScale; |
173 | 1.73M | ac->r02r = accu5 << mScale; |
174 | | |
175 | 1.73M | ac->det = (fMultDiv2(ac->r11r, ac->r22r) - fMultDiv2(ac->r12r, ac->r12r)); |
176 | 1.73M | mScale = CountLeadingBits(fAbs(ac->det)); |
177 | | |
178 | 1.73M | ac->det <<= mScale; |
179 | 1.73M | ac->det_scale = mScale - 1; |
180 | | |
181 | 1.73M | return autoCorrScaling; |
182 | 1.73M | } |
183 | | #endif |
184 | | |
185 | | #if !defined(FUNCTION_autoCorr2nd_cplx) |
186 | | INT autoCorr2nd_cplx( |
187 | | ACORR_COEFS *ac, /*!< Pointer to autocorrelation coeffs */ |
188 | | const FIXP_DBL *reBuffer, /*!< Pointer to real part of input samples */ |
189 | | const FIXP_DBL *imBuffer, /*!< Pointer to imag part of input samples */ |
190 | | const int len /*!< Number of input samples (should be smaller than 128) */ |
191 | 4.67M | ) { |
192 | 4.67M | int j, autoCorrScaling, mScale; |
193 | | |
194 | 4.67M | FIXP_DBL accu0, accu1, accu2, accu3, accu4, accu5, accu6, accu7, accu8; |
195 | | |
196 | 4.67M | const FIXP_DBL *pReBuf, *pImBuf; |
197 | | |
198 | 4.67M | const FIXP_DBL *realBuf = reBuffer; |
199 | 4.67M | const FIXP_DBL *imagBuf = imBuffer; |
200 | | |
201 | 4.67M | const int len_scale = fMax(DFRACT_BITS - fNormz((FIXP_DBL)len), 1); |
202 | | /* |
203 | | r00r, |
204 | | r11r,r22r |
205 | | r01r,r12r |
206 | | r01i,r12i |
207 | | r02r,r02i |
208 | | */ |
209 | 4.67M | accu1 = accu3 = accu5 = accu7 = accu8 = FL2FXCONST_DBL(0.0f); |
210 | | |
211 | 4.67M | pReBuf = realBuf - 2, pImBuf = imagBuf - 2; |
212 | 4.67M | accu7 += |
213 | 4.67M | ((fMultDiv2(pReBuf[2], pReBuf[0]) + fMultDiv2(pImBuf[2], pImBuf[0])) >> |
214 | 4.67M | len_scale); |
215 | 4.67M | accu8 += |
216 | 4.67M | ((fMultDiv2(pImBuf[2], pReBuf[0]) - fMultDiv2(pReBuf[2], pImBuf[0])) >> |
217 | 4.67M | len_scale); |
218 | | |
219 | 4.67M | pReBuf = realBuf - 1, pImBuf = imagBuf - 1; |
220 | 205M | for (j = (len - 1); j != 0; j--, pReBuf++, pImBuf++) { |
221 | 200M | accu1 += ((fPow2Div2(pReBuf[0]) + fPow2Div2(pImBuf[0])) >> len_scale); |
222 | 200M | accu3 += |
223 | 200M | ((fMultDiv2(pReBuf[0], pReBuf[1]) + fMultDiv2(pImBuf[0], pImBuf[1])) >> |
224 | 200M | len_scale); |
225 | 200M | accu5 += |
226 | 200M | ((fMultDiv2(pImBuf[1], pReBuf[0]) - fMultDiv2(pReBuf[1], pImBuf[0])) >> |
227 | 200M | len_scale); |
228 | 200M | accu7 += |
229 | 200M | ((fMultDiv2(pReBuf[2], pReBuf[0]) + fMultDiv2(pImBuf[2], pImBuf[0])) >> |
230 | 200M | len_scale); |
231 | 200M | accu8 += |
232 | 200M | ((fMultDiv2(pImBuf[2], pReBuf[0]) - fMultDiv2(pReBuf[2], pImBuf[0])) >> |
233 | 200M | len_scale); |
234 | 200M | } |
235 | | |
236 | 4.67M | accu2 = ((fPow2Div2(realBuf[-2]) + fPow2Div2(imagBuf[-2])) >> len_scale); |
237 | 4.67M | accu2 += accu1; |
238 | | |
239 | 4.67M | accu1 += ((fPow2Div2(realBuf[len - 2]) + fPow2Div2(imagBuf[len - 2])) >> |
240 | 4.67M | len_scale); |
241 | 4.67M | accu0 = ((fPow2Div2(realBuf[len - 1]) + fPow2Div2(imagBuf[len - 1])) >> |
242 | 4.67M | len_scale) - |
243 | 4.67M | ((fPow2Div2(realBuf[-1]) + fPow2Div2(imagBuf[-1])) >> len_scale); |
244 | 4.67M | accu0 += accu1; |
245 | | |
246 | 4.67M | accu4 = ((fMultDiv2(realBuf[-1], realBuf[-2]) + |
247 | 4.67M | fMultDiv2(imagBuf[-1], imagBuf[-2])) >> |
248 | 4.67M | len_scale); |
249 | 4.67M | accu4 += accu3; |
250 | | |
251 | 4.67M | accu3 += ((fMultDiv2(realBuf[len - 1], realBuf[len - 2]) + |
252 | 4.67M | fMultDiv2(imagBuf[len - 1], imagBuf[len - 2])) >> |
253 | 4.67M | len_scale); |
254 | | |
255 | 4.67M | accu6 = ((fMultDiv2(imagBuf[-1], realBuf[-2]) - |
256 | 4.67M | fMultDiv2(realBuf[-1], imagBuf[-2])) >> |
257 | 4.67M | len_scale); |
258 | 4.67M | accu6 += accu5; |
259 | | |
260 | 4.67M | accu5 += ((fMultDiv2(imagBuf[len - 1], realBuf[len - 2]) - |
261 | 4.67M | fMultDiv2(realBuf[len - 1], imagBuf[len - 2])) >> |
262 | 4.67M | len_scale); |
263 | | |
264 | 4.67M | mScale = |
265 | 4.67M | CntLeadingZeros((accu0 | accu1 | accu2 | fAbs(accu3) | fAbs(accu4) | |
266 | 4.67M | fAbs(accu5) | fAbs(accu6) | fAbs(accu7) | fAbs(accu8))) - |
267 | 4.67M | 1; |
268 | 4.67M | autoCorrScaling = mScale - 1 - len_scale; /* -1 because of fMultDiv2*/ |
269 | | |
270 | | /* Scale to common scale factor */ |
271 | 4.67M | ac->r00r = (FIXP_DBL)accu0 << mScale; |
272 | 4.67M | ac->r11r = (FIXP_DBL)accu1 << mScale; |
273 | 4.67M | ac->r22r = (FIXP_DBL)accu2 << mScale; |
274 | 4.67M | ac->r01r = (FIXP_DBL)accu3 << mScale; |
275 | 4.67M | ac->r12r = (FIXP_DBL)accu4 << mScale; |
276 | 4.67M | ac->r01i = (FIXP_DBL)accu5 << mScale; |
277 | 4.67M | ac->r12i = (FIXP_DBL)accu6 << mScale; |
278 | 4.67M | ac->r02r = (FIXP_DBL)accu7 << mScale; |
279 | 4.67M | ac->r02i = (FIXP_DBL)accu8 << mScale; |
280 | | |
281 | 4.67M | ac->det = |
282 | 4.67M | (fMultDiv2(ac->r11r, ac->r22r) >> 1) - |
283 | 4.67M | ((fMultDiv2(ac->r12r, ac->r12r) + fMultDiv2(ac->r12i, ac->r12i)) >> 1); |
284 | 4.67M | mScale = CntLeadingZeros(fAbs(ac->det)) - 1; |
285 | | |
286 | 4.67M | ac->det <<= mScale; |
287 | 4.67M | ac->det_scale = mScale - 2; |
288 | | |
289 | 4.67M | return autoCorrScaling; |
290 | 4.67M | } |
291 | | |
292 | | #endif /* FUNCTION_autoCorr2nd_cplx */ |