xref: /aosp_15_r20/external/libxaac/decoder/ixheaacd_acelp_tools.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <math.h>
21 #include <memory.h>
22 #include "ixheaac_type_def.h"
23 
24 #include "ixheaacd_cnst.h"
25 #include "ixheaac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops40.h"
28 
29 const FLOAT32 ixheaacd_gamma_table[17] = {
30     1.0f,      0.92f,     0.8464f,   0.778688f, 0.716393f, 0.659082f,
31     0.606355f, 0.557847f, 0.513219f, 0.472161f, 0.434389f, 0.399637f,
32     0.367666f, 0.338253f, 0.311193f, 0.286298f, 0.263394f};
33 
ixheaacd_rand_gen(WORD16 * seed)34 WORD16 ixheaacd_rand_gen(WORD16 *seed) {
35   *seed = (WORD16)(*seed * 31821L + 13849L);
36   return (*seed);
37 }
38 
ixheaacd_preemphsis_tool(WORD32 * signal,WORD32 mu,WORD32 len,WORD32 mem)39 VOID ixheaacd_preemphsis_tool(WORD32 *signal, WORD32 mu, WORD32 len,
40                               WORD32 mem) {
41   WORD32 i;
42   for (i = len - 1; i > 0; i--) {
43     signal[i] -= (WORD32)ixheaac_mul32_sh(mu, signal[i - 1], 16);
44   }
45   signal[0] -= (WORD32)ixheaac_mul32_sh(mu, mem, 16);
46   return;
47 }
48 
ixheaacd_preemphsis_tool_float(FLOAT32 * signal,FLOAT32 mu,WORD32 len,FLOAT32 mem)49 VOID ixheaacd_preemphsis_tool_float(FLOAT32 *signal, FLOAT32 mu, WORD32 len,
50                                     FLOAT32 mem) {
51   WORD32 i;
52   for (i = len - 1; i > 0; i--) {
53     signal[i] = signal[i] - mu * signal[i - 1];
54   }
55   signal[0] -= mu * mem;
56   return;
57 }
58 
ixheaacd_deemphsis_tool(FLOAT32 * signal,WORD32 len,FLOAT32 mem)59 VOID ixheaacd_deemphsis_tool(FLOAT32 *signal, WORD32 len, FLOAT32 mem) {
60   WORD32 i;
61   signal[0] = signal[0] + PREEMPH_FILT_FAC * mem;
62   for (i = 1; i < len; i++) {
63     signal[i] = signal[i] + PREEMPH_FILT_FAC * signal[i - 1];
64   }
65   return;
66 }
67 
ixheaacd_lpc_wt_synthesis_tool(FLOAT32 a[],FLOAT32 x[],WORD32 l)68 VOID ixheaacd_lpc_wt_synthesis_tool(FLOAT32 a[], FLOAT32 x[], WORD32 l) {
69   FLOAT32 s;
70   WORD32 i, j;
71 
72   for (i = 0; i < l; i++) {
73     s = x[i];
74     for (j = 1; j <= ORDER; j += 4) {
75       s -= (a[j] * ixheaacd_gamma_table[j]) * x[i - j];
76       s -= (a[j + 1] * ixheaacd_gamma_table[j + 1]) * x[i - (j + 1)];
77       s -= (a[j + 2] * ixheaacd_gamma_table[j + 2]) * x[i - (j + 2)];
78       s -= (a[j + 3] * ixheaacd_gamma_table[j + 3]) * x[i - (j + 3)];
79     }
80     x[i] = s;
81   }
82 
83   return;
84 }
85 
ixheaacd_synthesis_tool_float(FLOAT32 a[],FLOAT32 x[],FLOAT32 y[],WORD32 l,FLOAT32 mem[])86 VOID ixheaacd_synthesis_tool_float(FLOAT32 a[], FLOAT32 x[], FLOAT32 y[],
87                                    WORD32 l, FLOAT32 mem[]) {
88   FLOAT32 buf[LEN_FRAME * 2];
89   FLOAT32 s;
90   FLOAT32 *yy;
91   WORD32 i, j;
92   memcpy(buf, mem, ORDER * sizeof(FLOAT32));
93   yy = &buf[ORDER];
94   for (i = 0; i < l; i++) {
95     s = x[i];
96     for (j = 1; j <= ORDER; j += 4) {
97       s -= a[j] * yy[i - j];
98       s -= a[j + 1] * yy[i - (j + 1)];
99       s -= a[j + 2] * yy[i - (j + 2)];
100       s -= a[j + 3] * yy[i - (j + 3)];
101     }
102     yy[i] = s;
103     y[i] = s;
104   }
105 
106   return;
107 }
108 
ixheaacd_synthesis_tool_float1(FLOAT32 a[],FLOAT32 x[],WORD32 l)109 VOID ixheaacd_synthesis_tool_float1(FLOAT32 a[], FLOAT32 x[], WORD32 l) {
110   FLOAT32 s;
111   WORD32 i, j;
112   for (i = 0; i < l; i++) {
113     s = x[i];
114     for (j = 1; j <= ORDER; j += 4) {
115       s -= a[j] * x[i - j];
116       s -= a[j + 1] * x[i - (j + 1)];
117       s -= a[j + 2] * x[i - (j + 2)];
118       s -= a[j + 3] * x[i - (j + 3)];
119     }
120     x[i] = s;
121   }
122 
123   return;
124 }
125 
ixheaacd_residual_tool(WORD32 * a,WORD32 * x,WORD32 * y,WORD32 l,WORD32 count)126 VOID ixheaacd_residual_tool(WORD32 *a, WORD32 *x, WORD32 *y, WORD32 l,
127                             WORD32 count) {
128   WORD32 s;
129   WORD32 i, j;
130   WORD32 n = l * count;
131 
132   for (i = 0; i < n; i++) {
133     s = x[i];
134     for (j = 1; j <= 16; j++)
135       s += (WORD32)ixheaac_mul32_sh(a[j], x[i - j], 24);
136     y[i] = s;
137   }
138 
139   return;
140 }
141 
ixheaacd_residual_tool_float(FLOAT32 * a,FLOAT32 * x,FLOAT32 * y,WORD32 l,WORD32 loop_count)142 VOID ixheaacd_residual_tool_float(FLOAT32 *a, FLOAT32 *x, FLOAT32 *y, WORD32 l,
143                                   WORD32 loop_count) {
144   FLOAT32 s;
145   WORD32 i, j;
146   for (j = 0; j < loop_count; j++) {
147     for (i = 0; i < l; i++) {
148       s = x[i];
149       s += a[1] * x[i - 1];
150       s += a[2] * x[i - 2];
151       s += a[3] * x[i - 3];
152       s += a[4] * x[i - 4];
153       s += a[5] * x[i - 5];
154       s += a[6] * x[i - 6];
155       s += a[7] * x[i - 7];
156       s += a[8] * x[i - 8];
157       s += a[9] * x[i - 9];
158       s += a[10] * x[i - 10];
159       s += a[11] * x[i - 11];
160       s += a[12] * x[i - 12];
161       s += a[13] * x[i - 13];
162       s += a[14] * x[i - 14];
163       s += a[15] * x[i - 15];
164       s += a[16] * x[i - 16];
165       y[i] = s;
166     }
167     a += 17;
168     x += l;
169     y += l;
170   }
171   return;
172 }
173 
ixheaacd_residual_tool_float1(FLOAT32 * a,FLOAT32 * x,FLOAT32 * y,WORD32 l,WORD32 loop_count)174 VOID ixheaacd_residual_tool_float1(FLOAT32 *a, FLOAT32 *x, FLOAT32 *y, WORD32 l,
175                                    WORD32 loop_count) {
176   FLOAT32 s;
177   WORD32 i, j;
178   for (j = 0; j < loop_count; j++) {
179     for (i = 0; i < l; i++) {
180       s = x[i];
181       s += a[1] * x[i - 1];
182       s += a[2] * x[i - 2];
183       s += a[3] * x[i - 3];
184       s += a[4] * x[i - 4];
185       s += a[5] * x[i - 5];
186       s += a[6] * x[i - 6];
187       s += a[7] * x[i - 7];
188       s += a[8] * x[i - 8];
189       s += a[9] * x[i - 9];
190       s += a[10] * x[i - 10];
191       s += a[11] * x[i - 11];
192       s += a[12] * x[i - 12];
193       s += a[13] * x[i - 13];
194       s += a[14] * x[i - 14];
195       s += a[15] * x[i - 15];
196       s += a[16] * x[i - 16];
197       y[i] = s;
198     }
199     x += l;
200     y += l;
201   }
202   return;
203 }
204