1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*
19
20 Filename: calc_auto_corr.c
21
22
23------------------------------------------------------------------------------
24 REVISION HISTORY
25
26
27 Who:                                   Date: MM/DD/YYYY
28 Description:
29
30------------------------------------------------------------------------------
31 INPUT AND OUTPUT DEFINITIONS
32
33
34
35------------------------------------------------------------------------------
36 FUNCTION DESCRIPTION
37
38
39------------------------------------------------------------------------------
40 REQUIREMENTS
41
42
43------------------------------------------------------------------------------
44 REFERENCES
45
46SC 29 Software Copyright Licencing Disclaimer:
47
48This software module was originally developed by
49  Coding Technologies
50
51and edited by
52  -
53
54in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
55standards for reference purposes and its performance may not have been
56optimized. This software module is an implementation of one or more tools as
57specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
58ISO/IEC gives users free license to this software module or modifications
59thereof for use in products claiming conformance to audiovisual and
60image-coding related ITU Recommendations and/or ISO/IEC International
61Standards. ISO/IEC gives users the same free license to this software module or
62modifications thereof for research purposes and further ISO/IEC standardisation.
63Those intending to use this software module in products are advised that its
64use may infringe existing patents. ISO/IEC have no liability for use of this
65software module or modifications thereof. Copyright is not released for
66products that do not conform to audiovisual and image-coding related ITU
67Recommendations and/or ISO/IEC International Standards.
68The original developer retains full right to modify and use the code for its
69own purpose, assign or donate the code to a third party and to inhibit third
70parties from using the code for products that do not conform to audiovisual and
71image-coding related ITU Recommendations and/or ISO/IEC International Standards.
72This copyright notice must be included in all copies or derivative works.
73Copyright (c) ISO/IEC 2002.
74
75------------------------------------------------------------------------------
76 PSEUDO-CODE
77
78------------------------------------------------------------------------------
79*/
80
81
82/*----------------------------------------------------------------------------
83; INCLUDES
84----------------------------------------------------------------------------*/
85#ifdef AAC_PLUS
86
87
88#include    "calc_auto_corr.h"
89#include    "aac_mem_funcs.h"
90
91/*----------------------------------------------------------------------------
92; MACROS
93; Define module specific macros here
94----------------------------------------------------------------------------*/
95
96
97/*----------------------------------------------------------------------------
98; DEFINES
99; Include all pre-processor statements here. Include conditional
100; compile variables also.
101----------------------------------------------------------------------------*/
102#include    "fxp_mul32.h"
103#include    "pv_normalize.h"
104
105#define N   2
106
107/*----------------------------------------------------------------------------
108; LOCAL FUNCTION DEFINITIONS
109; Function Prototype declaration
110----------------------------------------------------------------------------*/
111
112/*----------------------------------------------------------------------------
113; LOCAL STORE/BUFFER/POINTER DEFINITIONS
114; Variable declaration - defined here and used outside this module
115----------------------------------------------------------------------------*/
116
117
118/*----------------------------------------------------------------------------
119; EXTERNAL FUNCTION REFERENCES
120; Declare functions defined elsewhere and referenced in this module
121----------------------------------------------------------------------------*/
122
123/*----------------------------------------------------------------------------
124; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
125; Declare variables used in this module but defined elsewhere
126----------------------------------------------------------------------------*/
127
128/*----------------------------------------------------------------------------
129; FUNCTION CODE
130----------------------------------------------------------------------------*/
131
132
133
134void calc_auto_corr_LC(struct ACORR_COEFS *ac,
135                       Int32  realBuf[][32],
136                       Int32  bd,
137                       Int32  len)
138{
139    Int32 j;
140    Int32 temp1;
141    Int32 temp3;
142    Int32 temp5;
143
144    int64_t temp_r01r;
145    int64_t temp_r02r;
146    int64_t temp_r11r;
147    int64_t temp_r12r;
148    int64_t temp_r22r;
149    int64_t max = 0;
150
151
152    temp1 = (realBuf[ 0][bd]) >> N;
153    temp3 = (realBuf[-1][bd]) >> N;
154    temp5 = (realBuf[-2][bd]) >> N;
155
156
157    temp_r11r = fxp_mac64_Q31(0, temp3, temp3);   /* [j-1]*[j-1]  */
158    temp_r12r = fxp_mac64_Q31(0, temp3, temp5);   /* [j-1]*[j-2]  */
159    temp_r22r = fxp_mac64_Q31(0, temp5, temp5);   /* [j-2]*[j-2]  */
160
161    temp_r01r = 0;
162    temp_r02r = 0;
163
164    for (j = 1; j < len; j++)
165    {
166        temp_r01r = fxp_mac64_Q31(temp_r01r, temp1, temp3);    /* [j  ]*[j-1]  */
167        temp_r02r = fxp_mac64_Q31(temp_r02r, temp1, temp5);    /* [j  ]*[j-2]  */
168        temp_r11r = fxp_mac64_Q31(temp_r11r, temp1, temp1);    /* [j-1]*[j-1]  */
169
170        temp5 = temp3;
171        temp3 = temp1;
172        temp1 = (realBuf[j][bd]) >> N;
173    }
174
175
176    temp_r22r += temp_r11r;
177    temp_r12r += temp_r01r;          /* [j-1]*[j-2]  */
178
179    temp_r22r  = fxp_mac64_Q31(temp_r22r, -temp3, temp3);
180
181    temp_r01r = fxp_mac64_Q31(temp_r01r, temp1, temp3);
182    temp_r02r = fxp_mac64_Q31(temp_r02r, temp1, temp5);
183
184    max  |= temp_r01r ^(temp_r01r >> 63);
185    max  |= temp_r02r ^(temp_r02r >> 63);
186    max  |= temp_r11r;
187    max  |= temp_r12r ^(temp_r12r >> 63);
188    max  |= temp_r22r;
189
190    if (max)
191    {
192        temp1 = (UInt32)(max >> 32);
193        if (temp1)
194        {
195            temp3 = 33 - pv_normalize(temp1);
196            ac->r01r = (Int32)(temp_r01r >> temp3);
197            ac->r02r = (Int32)(temp_r02r >> temp3);
198            ac->r11r = (Int32)(temp_r11r >> temp3);
199            ac->r12r = (Int32)(temp_r12r >> temp3);
200            ac->r22r = (Int32)(temp_r22r >> temp3);
201
202        }
203        else
204        {
205            temp3 = pv_normalize(((UInt32)max) >> 1) - 2;
206
207            if (temp3 > 0)
208            {
209                ac->r01r = (Int32)(temp_r01r << temp3);
210                ac->r02r = (Int32)(temp_r02r << temp3);
211                ac->r11r = (Int32)(temp_r11r << temp3);
212                ac->r12r = (Int32)(temp_r12r << temp3);
213                ac->r22r = (Int32)(temp_r22r << temp3);
214            }
215            else
216            {
217                temp3 = -temp3;
218                ac->r01r = (Int32)(temp_r01r >> temp3);
219                ac->r02r = (Int32)(temp_r02r >> temp3);
220                ac->r11r = (Int32)(temp_r11r >> temp3);
221                ac->r12r = (Int32)(temp_r12r >> temp3);
222                ac->r22r = (Int32)(temp_r22r >> temp3);
223            }
224
225        }
226
227        /*
228         *  ac->det = ac->r11r*ac->r22r - rel*(ac->r12r*ac->r12r);
229         */
230        /* 1/(1 + 1e-6) == 1 - 1e-6 */
231        /* 2^-20 == 1e-6 */
232        ac->det  = fxp_mul32_Q30(ac->r12r, ac->r12r);
233
234        ac->det -= ac->det >> 20;
235
236        ac->det  = fxp_mul32_Q30(ac->r11r, ac->r22r) - ac->det;
237    }
238    else
239    {
240        pv_memset((void *)ac, 0, sizeof(struct ACORR_COEFS));
241    }
242
243
244}
245
246
247#ifdef HQ_SBR
248
249
250void calc_auto_corr(struct ACORR_COEFS *ac,
251                    Int32  realBuf[][32],
252                    Int32  imagBuf[][32],
253                    Int32  bd,
254                    Int32  len)
255{
256
257
258    Int32 j;
259    Int32 temp1;
260    Int32 temp2;
261    Int32 temp3;
262    Int32 temp4;
263    Int32 temp5;
264    Int32 temp6;
265
266    int64_t accu1 = 0;
267    int64_t accu2 = 0;
268    int64_t accu3 = 0;
269    int64_t accu4 = 0;
270    int64_t accu5 = 0;
271
272
273    int64_t temp_r12r;
274    int64_t temp_r12i;
275    int64_t temp_r22r;
276    int64_t max = 0;
277
278
279    temp1 = realBuf[0  ][bd] >> N;
280    temp2 = imagBuf[0  ][bd] >> N;
281    temp3 = realBuf[0-1][bd] >> N;
282    temp4 = imagBuf[0-1][bd] >> N;
283    temp5 = realBuf[0-2][bd] >> N;
284    temp6 = imagBuf[0-2][bd] >> N;
285
286    temp_r22r =  fxp_mac64_Q31(0, temp5, temp5);
287    temp_r22r =  fxp_mac64_Q31(temp_r22r, temp6, temp6);
288    temp_r12r =  fxp_mac64_Q31(0, temp3, temp5);
289    temp_r12r =  fxp_mac64_Q31(temp_r12r, temp4, temp6);
290    temp_r12i = -fxp_mac64_Q31(0, temp3, temp6);
291    temp_r12i =  fxp_mac64_Q31(temp_r12i, temp4, temp5);
292
293    for (j = 1; j < len; j++)
294    {
295        accu1  = fxp_mac64_Q31(accu1, temp3, temp3);
296        accu1  = fxp_mac64_Q31(accu1, temp4, temp4);
297        accu2  = fxp_mac64_Q31(accu2, temp1, temp3);
298        accu2  = fxp_mac64_Q31(accu2, temp2, temp4);
299        accu3  = fxp_mac64_Q31(accu3, temp2, temp3);
300        accu3  = fxp_mac64_Q31(accu3, -temp1, temp4);
301        accu4  = fxp_mac64_Q31(accu4, temp1, temp5);
302        accu4  = fxp_mac64_Q31(accu4, temp2, temp6);
303        accu5  = fxp_mac64_Q31(accu5, temp2, temp5);
304        accu5  = fxp_mac64_Q31(accu5, -temp1, temp6);
305
306        temp5 = temp3;
307        temp6 = temp4;
308        temp3 = temp1;
309        temp4 = temp2;
310        temp1 = realBuf[j][bd] >> N;
311        temp2 = imagBuf[j][bd] >> N;
312    }
313
314
315    temp_r22r += accu1;
316    temp_r12r += accu2;
317    temp_r12i += accu3;
318
319
320    accu1  = fxp_mac64_Q31(accu1, temp3, temp3);
321    accu1  = fxp_mac64_Q31(accu1, temp4, temp4);
322    accu2  = fxp_mac64_Q31(accu2, temp1, temp3);
323    accu2  = fxp_mac64_Q31(accu2, temp2, temp4);
324    accu3  = fxp_mac64_Q31(accu3, temp2, temp3);
325    accu3  = fxp_mac64_Q31(accu3, -temp1, temp4);
326    accu4  = fxp_mac64_Q31(accu4, temp1, temp5);
327    accu4  = fxp_mac64_Q31(accu4, temp2, temp6);
328    accu5  = fxp_mac64_Q31(accu5, temp2, temp5);
329    accu5  = fxp_mac64_Q31(accu5, -temp1, temp6);
330
331
332    max  |= accu5 ^(accu5 >> 63);
333    max  |= accu4 ^(accu4 >> 63);
334    max  |= accu3 ^(accu3 >> 63);
335    max  |= accu2 ^(accu2 >> 63);
336    max  |= accu1;
337    max  |= temp_r12r ^(temp_r12r >> 63);
338    max  |= temp_r12i ^(temp_r12i >> 63);
339    max  |= temp_r22r;
340
341    if (max)
342    {
343
344        temp1 = (UInt32)(max >> 32);
345        if (temp1)
346        {
347            temp1 = 34 - pv_normalize(temp1);
348            ac->r11r = (Int32)(accu1 >> temp1);
349            ac->r01r = (Int32)(accu2 >> temp1);
350            ac->r01i = (Int32)(accu3 >> temp1);
351            ac->r02r = (Int32)(accu4 >> temp1);
352            ac->r02i = (Int32)(accu5 >> temp1);
353            ac->r12r = (Int32)(temp_r12r >> temp1);
354            ac->r12i = (Int32)(temp_r12i >> temp1);
355            ac->r22r = (Int32)(temp_r22r >> temp1);
356        }
357        else
358        {
359            temp1 = pv_normalize(((UInt32)max) >> 1) - 3;
360
361            if (temp1 > 0)
362            {
363                ac->r11r = (Int32)(accu1 << temp1);
364                ac->r01r = (Int32)(accu2 << temp1);
365                ac->r01i = (Int32)(accu3 << temp1);
366                ac->r02r = (Int32)(accu4 << temp1);
367                ac->r02i = (Int32)(accu5 << temp1);
368                ac->r12r = (Int32)(temp_r12r << temp1);
369                ac->r12i = (Int32)(temp_r12i << temp1);
370                ac->r22r = (Int32)(temp_r22r << temp1);
371            }
372            else
373            {
374                temp1 = -temp1;
375                ac->r11r = (Int32)(accu1 >> temp1);
376                ac->r01r = (Int32)(accu2 >> temp1);
377                ac->r01i = (Int32)(accu3 >> temp1);
378                ac->r02r = (Int32)(accu4 >> temp1);
379                ac->r02i = (Int32)(accu5 >> temp1);
380                ac->r12r = (Int32)(temp_r12r >> temp1);
381                ac->r12i = (Int32)(temp_r12i >> temp1);
382                ac->r22r = (Int32)(temp_r22r >> temp1);
383            }
384
385        }
386
387        /*
388         *  ac->det = ac->r11r*ac->r22r - rel*(ac->r12r*ac->r12r);
389         */
390        /* 1/(1 + 1e-6) == 1 - 1e-6 */
391        /* 2^-20 == 1e-6 */
392
393        ac->det =   fxp_mul32_Q29(ac->r12i, ac->r12i);
394        ac->det =   fxp_mac32_Q29(ac->r12r, ac->r12r, ac->det);
395
396        ac->det -= ac->det >> 20;
397
398        ac->det =  -fxp_msu32_Q29(ac->r11r, ac->r22r, ac->det);
399
400    }
401    else
402    {
403        pv_memset((void *)ac, 0, sizeof(struct ACORR_COEFS));
404    }
405
406}
407
408#endif
409
410
411
412
413
414#endif
415
416
417