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 
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 }
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 
116 VOID 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 
129   size_n = 2 * lg;
130   avg_fac = PI / (FLOAT32)(size_n);
131 
132   for (i = 0; i < order + 1; i++) {
133     tmp = (FLOAT32)(((FLOAT32)i) * avg_fac);
134     data_r[i] = (FLOAT32)(coeff[i] * cos(tmp));
135     data_i[i] = (FLOAT32)(-coeff[i] * sin(tmp));
136   }
137   for (; i < size_n; i++) {
138     data_r[i] = 0.f;
139     data_i[i] = 0.f;
140   }
141 
142   for (i = 0; i < size_n; i++) {
143     if (ABS(data_r[i]) > ftemp) ftemp = ABS(data_r[i]);
144     if (ABS(data_i[i]) > ftemp) ftemp = ABS(data_i[i]);
145   }
146 
147   itemp = (WORD32)ftemp;
148   qshift = ixheaacd_norm32(itemp);
149 
150   for (i = 0; i < size_n; i++) {
151     idata_r[i] = (WORD32)(data_r[i] * ((WORD64)1 << qshift));
152     idata_i[i] = (WORD32)(data_i[i] * ((WORD64)1 << qshift));
153   }
154 
155   ixheaacd_complex_fft(idata_r, idata_i, size_n, -1, &preshift);
156 
157   qfac = 1.0f / ((FLOAT32)((WORD64)1 << (qshift - preshift)));
158 
159   for (i = 0; i < size_n; i++) {
160     data_r[i] = (FLOAT32)((FLOAT32)idata_r[i] * qfac);
161     data_i[i] = (FLOAT32)((FLOAT32)idata_i[i] * qfac);
162   }
163 
164   for (i = 0; i < size_n / 2; i++) {
165     gains[i] =
166         (FLOAT32)(1.0f / sqrt(data_r[i] * data_r[i] + data_i[i] * data_i[i]));
167   }
168 
169   return;
170 }
171 
172 VOID ixheaacd_noise_shaping(FLOAT32 r[], WORD32 lg, WORD32 M, FLOAT32 g1[],
173                             FLOAT32 g2[]) {
174   WORD32 i, k;
175   FLOAT32 rr_prev, a = 0, b = 0;
176   FLOAT32 rr[1024];
177 
178   k = lg / M;
179 
180   rr_prev = 0;
181 
182   memcpy(&rr, r, lg * sizeof(FLOAT32));
183 
184   for (i = 0; i < lg; i++) {
185     if ((i % k) == 0) {
186       a = 2.0f * g1[i / k] * g2[i / k] / (g1[i / k] + g2[i / k]);
187       b = (g2[i / k] - g1[i / k]) / (g1[i / k] + g2[i / k]);
188     }
189 
190     rr[i] = a * rr[i] + b * rr_prev;
191     rr_prev = rr[i];
192   }
193 
194   for (i = 0; i < lg / 2; i++) {
195     r[i] = rr[2 * i];
196     r[lg / 2 + i] = rr[lg - 2 * i - 1];
197   }
198   return;
199 }
200 
201 VOID ixheaacd_lpc_coef_gen(FLOAT32 lsf_old[], FLOAT32 lsf_new[], FLOAT32 a[],
202                            WORD32 nb_subfr, WORD32 m) {
203   FLOAT32 lsf[ORDER], *ptr_a;
204   FLOAT32 inc, fnew, fold;
205   WORD32 i;
206 
207   ptr_a = a;
208 
209   inc = 1.0f / (FLOAT32)nb_subfr;
210   fnew = 0.5f - (0.5f * inc);
211   fold = 1.0f - fnew;
212 
213   for (i = 0; i < m; i++) {
214     lsf[i] = (lsf_old[i] * fold) + (lsf_new[i] * fnew);
215   }
216   ixheaacd_lsp_to_lp_conversion(lsf, ptr_a);
217   ptr_a += (m + 1);
218   ixheaacd_lsp_to_lp_conversion(lsf_old, ptr_a);
219   ptr_a += (m + 1);
220   ixheaacd_lsp_to_lp_conversion(lsf_new, ptr_a);
221   ptr_a += (m + 1);
222 
223   return;
224 }
225 
226 VOID ixheaacd_interpolation_lsp_params(FLOAT32 lsp_old[], FLOAT32 lsp_new[],
227                                        FLOAT32 lp_flt_coff_a[],
228                                        WORD32 nb_subfr) {
229   FLOAT32 lsp[ORDER];
230   FLOAT32 factor;
231   WORD32 i, k;
232   FLOAT32 x_plus_y, x_minus_y;
233 
234   factor = 1.0f / (FLOAT32)nb_subfr;
235 
236   x_plus_y = 0.5f * factor;
237 
238   for (k = 0; k < nb_subfr; k++) {
239     x_minus_y = 1.0f - x_plus_y;
240     for (i = 0; i < ORDER; i++) {
241       lsp[i] = (lsp_old[i] * x_minus_y) + (lsp_new[i] * x_plus_y);
242     }
243     x_plus_y += factor;
244 
245     ixheaacd_lsp_to_lp_conversion(lsp, lp_flt_coff_a);
246 
247     lp_flt_coff_a += (ORDER + 1);
248   }
249 
250   ixheaacd_lsp_to_lp_conversion(lsp_new, lp_flt_coff_a);
251 
252   return;
253 }
254