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 <float.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <string.h>
25
26 #include "ixheaacd_cnst.h"
27 #include "ixheaacd_type_def.h"
28 #include "ixheaacd_bitbuffer.h"
29 #include "ixheaacd_acelp_com.h"
30
31 #include "ixheaacd_bitbuffer.h"
32 #include "ixheaacd_interface.h"
33
34 #include "ixheaacd_tns_usac.h"
35 #include "ixheaacd_cnst.h"
36
37 #include "ixheaacd_acelp_info.h"
38
39 #include "ixheaacd_td_mdct.h"
40
41 #include "ixheaacd_sbrdecsettings.h"
42 #include "ixheaacd_info.h"
43 #include "ixheaacd_sbr_common.h"
44 #include "ixheaacd_drc_data_struct.h"
45 #include "ixheaacd_drc_dec.h"
46 #include "ixheaacd_sbrdecoder.h"
47 #include "ixheaacd_mps_polyphase.h"
48 #include "ixheaacd_sbr_const.h"
49
50 #include "ixheaacd_constants.h"
51 #include "ixheaacd_basic_ops32.h"
52 #include "ixheaacd_basic_ops40.h"
53 #include "ixheaacd_main.h"
54 #include "ixheaacd_arith_dec.h"
55
56 #define FREQ_MAX 6400.0f
57
58 #define ABS(A) ((A) < 0 ? (-A) : (A))
59
ixheaacd_compute_coeff_poly_f(FLOAT32 lsp[],FLOAT32 * f1,FLOAT32 * f2)60 static VOID ixheaacd_compute_coeff_poly_f(FLOAT32 lsp[], FLOAT32 *f1,
61 FLOAT32 *f2) {
62 FLOAT32 b1, b2;
63 FLOAT32 *ptr_lsp;
64 WORD32 i, j;
65
66 ptr_lsp = lsp;
67 f1[0] = f2[0] = 1.0f;
68
69 for (i = 1; i <= ORDER_BY_2; i++) {
70 b1 = -2.0f * (*ptr_lsp++);
71 b2 = -2.0f * (*ptr_lsp++);
72 f1[i] = (b1 * f1[i - 1]) + (2.0f * f1[i - 2]);
73 f2[i] = (b2 * f2[i - 1]) + (2.0f * f2[i - 2]);
74 for (j = i - 1; j > 0; j--) {
75 f1[j] += (b1 * f1[j - 1]) + f1[j - 2];
76 f2[j] += (b2 * f2[j - 1]) + f2[j - 2];
77 }
78 }
79
80 return;
81 }
ixheaacd_lsp_to_lp_conversion(FLOAT32 * lsp,FLOAT32 * lp_flt_coff_a)82 VOID ixheaacd_lsp_to_lp_conversion(FLOAT32 *lsp, FLOAT32 *lp_flt_coff_a) {
83 WORD32 i;
84 FLOAT32 *ppoly_f1, *ppoly_f2;
85 FLOAT32 *plp_flt_coff_a_bott, *plp_flt_coff_a_top;
86 FLOAT32 poly1[ORDER_BY_2 + 2], poly2[ORDER_BY_2 + 2];
87
88 poly1[0] = 0.0f;
89 poly2[0] = 0.0f;
90
91 ixheaacd_compute_coeff_poly_f(lsp, &poly1[1], &poly2[1]);
92
93 ppoly_f1 = poly1 + ORDER_BY_2 + 1;
94 ppoly_f2 = poly2 + ORDER_BY_2 + 1;
95
96 for (i = 0; i < ORDER_BY_2; i++) {
97 ppoly_f1[0] += ppoly_f1[-1];
98 ppoly_f2[0] -= ppoly_f2[-1];
99 ppoly_f1--;
100 ppoly_f2--;
101 }
102
103 plp_flt_coff_a_bott = lp_flt_coff_a;
104 *plp_flt_coff_a_bott++ = 1.0f;
105 plp_flt_coff_a_top = lp_flt_coff_a + ORDER;
106 ppoly_f1 = poly1 + 2;
107 ppoly_f2 = poly2 + 2;
108 for (i = 0; i < ORDER_BY_2; i++) {
109 *plp_flt_coff_a_bott++ = 0.5f * (*ppoly_f1 + *ppoly_f2);
110 *plp_flt_coff_a_top-- = 0.5f * (*ppoly_f1++ - *ppoly_f2++);
111 }
112
113 return;
114 }
115
ixheaacd_lpc_to_td(float * coeff,WORD32 order,float * gains,WORD32 lg)116 WORD32 ixheaacd_lpc_to_td(float *coeff, WORD32 order, float *gains, WORD32 lg) {
117 FLOAT32 data_r[LEN_SUPERFRAME * 2];
118 FLOAT32 data_i[LEN_SUPERFRAME * 2];
119 FLOAT64 avg_fac;
120 WORD32 idata_r[LEN_SUPERFRAME * 2];
121 WORD32 idata_i[LEN_SUPERFRAME * 2];
122 WORD8 qshift;
123 WORD32 preshift = 0;
124 WORD32 itemp;
125 FLOAT32 ftemp = 0;
126 FLOAT32 tmp, qfac;
127 WORD32 i, size_n;
128 WORD32 err = 0;
129
130 size_n = 2 * lg;
131 avg_fac = PI / (FLOAT32)(size_n);
132
133 for (i = 0; i < order + 1; i++) {
134 tmp = (FLOAT32)(((FLOAT32)i) * avg_fac);
135 data_r[i] = (FLOAT32)(coeff[i] * cos(tmp));
136 data_i[i] = (FLOAT32)(-coeff[i] * sin(tmp));
137 }
138 for (; i < size_n; i++) {
139 data_r[i] = 0.f;
140 data_i[i] = 0.f;
141 }
142
143 for (i = 0; i < size_n; i++) {
144 if (ABS(data_r[i]) > ftemp) ftemp = ABS(data_r[i]);
145 if (ABS(data_i[i]) > ftemp) ftemp = ABS(data_i[i]);
146 }
147
148 itemp = (WORD32)ftemp;
149 qshift = ixheaacd_norm32(itemp);
150
151 for (i = 0; i < size_n; i++) {
152 idata_r[i] = (WORD32)(data_r[i] * ((WORD64)1 << qshift));
153 idata_i[i] = (WORD32)(data_i[i] * ((WORD64)1 << qshift));
154 }
155
156 err = ixheaacd_complex_fft(idata_r, idata_i, size_n, -1, &preshift);
157 if (err) return err;
158
159 qfac = 1.0f / ((FLOAT32)((WORD64)1 << (qshift - preshift)));
160
161 for (i = 0; i < size_n; i++) {
162 data_r[i] = (FLOAT32)((FLOAT32)idata_r[i] * qfac);
163 data_i[i] = (FLOAT32)((FLOAT32)idata_i[i] * qfac);
164 }
165
166 for (i = 0; i < size_n / 2; i++) {
167 gains[i] =
168 (FLOAT32)(1.0f / sqrt(data_r[i] * data_r[i] + data_i[i] * data_i[i]));
169 }
170
171 return err;
172 }
173
ixheaacd_noise_shaping(FLOAT32 r[],WORD32 lg,WORD32 M,FLOAT32 g1[],FLOAT32 g2[])174 VOID ixheaacd_noise_shaping(FLOAT32 r[], WORD32 lg, WORD32 M, FLOAT32 g1[],
175 FLOAT32 g2[]) {
176 WORD32 i, k;
177 FLOAT32 rr_prev, a = 0, b = 0;
178 FLOAT32 rr[1024];
179
180 k = lg / M;
181
182 rr_prev = 0;
183
184 memcpy(&rr, r, lg * sizeof(FLOAT32));
185
186 for (i = 0; i < lg; i++) {
187 if ((i % k) == 0) {
188 a = 2.0f * g1[i / k] * g2[i / k] / (g1[i / k] + g2[i / k]);
189 b = (g2[i / k] - g1[i / k]) / (g1[i / k] + g2[i / k]);
190 }
191
192 rr[i] = a * rr[i] + b * rr_prev;
193 rr_prev = rr[i];
194 }
195
196 for (i = 0; i < lg / 2; i++) {
197 r[i] = rr[2 * i];
198 r[lg / 2 + i] = rr[lg - 2 * i - 1];
199 }
200 return;
201 }
202
ixheaacd_lpc_coef_gen(FLOAT32 lsf_old[],FLOAT32 lsf_new[],FLOAT32 a[],WORD32 nb_subfr,WORD32 m)203 VOID ixheaacd_lpc_coef_gen(FLOAT32 lsf_old[], FLOAT32 lsf_new[], FLOAT32 a[],
204 WORD32 nb_subfr, WORD32 m) {
205 FLOAT32 lsf[ORDER], *ptr_a;
206 FLOAT32 inc, fnew, fold;
207 WORD32 i;
208
209 ptr_a = a;
210
211 inc = 1.0f / (FLOAT32)nb_subfr;
212 fnew = 0.5f - (0.5f * inc);
213 fold = 1.0f - fnew;
214
215 for (i = 0; i < m; i++) {
216 lsf[i] = (lsf_old[i] * fold) + (lsf_new[i] * fnew);
217 }
218 ixheaacd_lsp_to_lp_conversion(lsf, ptr_a);
219 ptr_a += (m + 1);
220 ixheaacd_lsp_to_lp_conversion(lsf_old, ptr_a);
221 ptr_a += (m + 1);
222 ixheaacd_lsp_to_lp_conversion(lsf_new, ptr_a);
223 ptr_a += (m + 1);
224
225 return;
226 }
227
ixheaacd_interpolation_lsp_params(FLOAT32 lsp_old[],FLOAT32 lsp_new[],FLOAT32 lp_flt_coff_a[],WORD32 nb_subfr)228 VOID ixheaacd_interpolation_lsp_params(FLOAT32 lsp_old[], FLOAT32 lsp_new[],
229 FLOAT32 lp_flt_coff_a[],
230 WORD32 nb_subfr) {
231 FLOAT32 lsp[ORDER];
232 FLOAT32 factor;
233 WORD32 i, k;
234 FLOAT32 x_plus_y, x_minus_y;
235
236 factor = 1.0f / (FLOAT32)nb_subfr;
237
238 x_plus_y = 0.5f * factor;
239
240 for (k = 0; k < nb_subfr; k++) {
241 x_minus_y = 1.0f - x_plus_y;
242 for (i = 0; i < ORDER; i++) {
243 lsp[i] = (lsp_old[i] * x_minus_y) + (lsp_new[i] * x_plus_y);
244 }
245 x_plus_y += factor;
246
247 ixheaacd_lsp_to_lp_conversion(lsp, lp_flt_coff_a);
248
249 lp_flt_coff_a += (ORDER + 1);
250 }
251
252 ixheaacd_lsp_to_lp_conversion(lsp_new, lp_flt_coff_a);
253
254 return;
255 }
256