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 "ixheaac_type_def.h"
22 #include "ixheaacd_bitbuffer.h"
23 #include "ixheaacd_interface.h"
24 #include "ixheaacd_tns_usac.h"
25 #include "ixheaacd_cnst.h"
26 #include "ixheaacd_acelp_info.h"
27 #include "ixheaacd_sbrdecsettings.h"
28 #include "ixheaacd_info.h"
29 #include "ixheaacd_sbr_common.h"
30 #include "ixheaacd_drc_data_struct.h"
31 #include "ixheaacd_drc_dec.h"
32 #include "ixheaacd_sbrdecoder.h"
33 #include "ixheaacd_mps_polyphase.h"
34 #include "ixheaacd_ec_defines.h"
35 #include "ixheaacd_ec_struct_def.h"
36 #include "ixheaacd_main.h"
37 #include "ixheaacd_arith_dec.h"
38 #include "ixheaacd_func_def.h"
39 #include "ixheaacd_acelp_com.h"
40
41 #include "ixheaac_basic_op.h"
42 #include "ixheaac_constants.h"
43 #include "ixheaac_basic_ops32.h"
44 #include "ixheaac_basic_ops40.h"
45
46 #define LSF_GAP 50.0f
47 #define FREQ_MAX 6400.0f
48 #define FREQ_DIV 400.0f
49
50 static const FLOAT32 factor_table[4] = {60.0f, 65.0f, 64.0f, 63.0f};
51
ixheaacd_lsf_weight_2st_flt(FLOAT32 * lsfq,FLOAT32 * w,WORD32 mode)52 VOID ixheaacd_lsf_weight_2st_flt(FLOAT32 *lsfq, FLOAT32 *w, WORD32 mode) {
53 WORD32 i;
54 FLOAT32 d[ORDER + 1];
55
56 d[0] = lsfq[0];
57 d[ORDER] = FREQ_MAX - lsfq[ORDER - 1];
58 for (i = 1; i < ORDER; i++) {
59 d[i] = lsfq[i] - lsfq[i - 1];
60 }
61
62 for (i = 0; i < ORDER; i++) {
63 w[i] = (FLOAT32)((factor_table[mode] * sqrt(d[i] * d[i + 1])) / FREQ_DIV);
64 }
65
66 return;
67 }
68
ixheaacd_decoding_avq_tool(WORD32 * read_arr,WORD32 * nvecq)69 static WORD32 ixheaacd_decoding_avq_tool(WORD32 *read_arr, WORD32 *nvecq) {
70 WORD32 i, k, qn, kv[8] = {0};
71 WORD32 code_book_idx;
72 WORD32 *ptr_kv = &kv[0];
73
74 WORD32 position = 2;
75
76 for (k = 0; k < 2; k++) {
77 qn = read_arr[k];
78
79 if (qn > 0) {
80 code_book_idx = read_arr[position++];
81
82 ptr_kv = &read_arr[position];
83 position += 8;
84
85 } else {
86 code_book_idx = 0;
87 for (i = 0; i < 8; i++) ptr_kv = &kv[0];
88 }
89
90 ixheaacd_rotated_gosset_mtx_dec(qn, code_book_idx, ptr_kv, &nvecq[k * 8]);
91 }
92
93 return position;
94 }
95
ixheaacd_avq_first_approx_abs(FLOAT32 * lsf,WORD32 * indx)96 static WORD32 ixheaacd_avq_first_approx_abs(FLOAT32 *lsf, WORD32 *indx) {
97 WORD32 i;
98 extern const FLOAT32 ixheaacd_dico_lsf_abs_8b_flt[];
99 extern const FLOAT32 ixheaacd_weight_table_avq[];
100 WORD32 position = 0;
101 WORD32 avq[ORDER];
102 FLOAT32 lsf_min;
103 const FLOAT32 *ptr_w;
104
105 ptr_w = &ixheaacd_weight_table_avq[(indx[0] * ORDER)];
106
107 position++;
108
109 for (i = 0; i < ORDER; i++) {
110 lsf[i] = ixheaacd_dico_lsf_abs_8b_flt[indx[0] * ORDER + i];
111 }
112
113 position += ixheaacd_decoding_avq_tool(&indx[position], avq);
114
115 lsf_min = LSF_GAP;
116 for (i = 0; i < ORDER; i++) {
117 lsf[i] += (ptr_w[i] * avq[i]);
118
119 if (lsf[i] < lsf_min) lsf[i] = lsf_min;
120
121 lsf_min = lsf[i] + LSF_GAP;
122 }
123
124 lsf_min = FREQ_MAX - LSF_GAP;
125 for (i = ORDER - 1; i >= 0; i--) {
126 if (lsf[i] > lsf_min) {
127 lsf[i] = lsf_min;
128 }
129
130 lsf_min = lsf[i] - LSF_GAP;
131 }
132
133 return position;
134 }
135
ixheaacd_avq_first_approx_rel(FLOAT32 * lsf,WORD32 * indx,WORD32 mode)136 WORD32 ixheaacd_avq_first_approx_rel(FLOAT32 *lsf, WORD32 *indx, WORD32 mode) {
137 WORD32 i;
138 FLOAT32 w[ORDER];
139 WORD32 avq[ORDER];
140 WORD32 position = 0;
141 FLOAT32 lsf_min;
142
143 ixheaacd_lsf_weight_2st_flt(lsf, w, mode);
144
145 position = ixheaacd_decoding_avq_tool(indx, avq);
146
147 lsf_min = LSF_GAP;
148
149 for (i = 0; i < ORDER; i++) {
150 lsf[i] += (w[i] * avq[i]);
151
152 if (lsf[i] < lsf_min) lsf[i] = lsf_min;
153
154 lsf_min = lsf[i] + LSF_GAP;
155 }
156
157 return position;
158 }
159
ixheaacd_alg_vec_dequant(ia_td_frame_data_struct * pstr_td_frame_data,WORD32 first_lpd_flag,FLOAT32 * lsf,WORD32 mod[],WORD32 ec_flag)160 VOID ixheaacd_alg_vec_dequant(ia_td_frame_data_struct *pstr_td_frame_data, WORD32 first_lpd_flag,
161 FLOAT32 *lsf, WORD32 mod[], WORD32 ec_flag) {
162 WORD32 i;
163 WORD32 *lpc_index, mode_lpc, pos = 0;
164 WORD32 lpc_present[5] = {0, 0, 0, 0, 0};
165 lpc_index = pstr_td_frame_data->lpc_first_approx_idx;
166 lpc_present[4] = 1;
167 pos = ixheaacd_avq_first_approx_abs(&lsf[4 * ORDER], &lpc_index[0]);
168
169 lpc_index += pos;
170
171 if (first_lpd_flag) {
172 mode_lpc = lpc_index[0];
173 lpc_index++;
174
175 if (mode_lpc == 0) {
176 pos = ixheaacd_avq_first_approx_abs(&lsf[0], &lpc_index[0]);
177
178 } else if (mode_lpc == 1) {
179 for (i = 0; i < ORDER; i++) lsf[i] = lsf[4 * ORDER + i];
180 pos = ixheaacd_avq_first_approx_rel(&lsf[0], &lpc_index[0], 3);
181 }
182
183 lpc_index += pos;
184 }
185 lpc_present[0] = 1;
186
187 if (mod[0] < 3) {
188 mode_lpc = lpc_index[0];
189 lpc_index++;
190 lpc_present[2] = 1;
191
192 if (mode_lpc == 0) {
193 pos = ixheaacd_avq_first_approx_abs(&lsf[2 * ORDER], &lpc_index[0]);
194 } else if (mode_lpc == 1) {
195 for (i = 0; i < ORDER; i++) lsf[2 * ORDER + i] = lsf[4 * ORDER + i];
196 pos = ixheaacd_avq_first_approx_rel(&lsf[2 * ORDER], &lpc_index[0], 3);
197 }
198
199 lpc_index += pos;
200 }
201
202 if (mod[0] < 2) {
203 mode_lpc = lpc_index[0];
204 lpc_index++;
205 lpc_present[1] = 1;
206
207 if (mode_lpc == 1) {
208 for (i = 0; i < ORDER; i++)
209 lsf[ORDER + i] = 0.5f * (lsf[i] + lsf[2 * ORDER + i]);
210 } else {
211 if (mode_lpc == 0) {
212 pos = ixheaacd_avq_first_approx_abs(&lsf[ORDER], &lpc_index[0]);
213 } else if (mode_lpc == 2) {
214 for (i = 0; i < ORDER; i++) lsf[ORDER + i] = lsf[2 * ORDER + i];
215 pos = ixheaacd_avq_first_approx_rel(&lsf[ORDER], &lpc_index[0], 2);
216 }
217
218 lpc_index += pos;
219 }
220 }
221
222 if (mod[2] < 2) {
223 mode_lpc = lpc_index[0];
224 lpc_index++;
225 lpc_present[3] = 1;
226
227 if (mode_lpc == 0) {
228 pos = ixheaacd_avq_first_approx_abs(&lsf[3 * ORDER], &lpc_index[0]);
229 } else if (mode_lpc == 1) {
230 for (i = 0; i < ORDER; i++)
231 lsf[3 * ORDER + i] = 0.5f * (lsf[2 * ORDER + i] + lsf[4 * ORDER + i]);
232 pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 1);
233 } else if (mode_lpc == 2) {
234 for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[2 * ORDER + i];
235 pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2);
236 } else if (mode_lpc == 3) {
237 for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[4 * ORDER + i];
238 pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2);
239 }
240
241 lpc_index += pos;
242 }
243 if (ec_flag) {
244 WORD32 last, k;
245 WORD32 num_lpc = 0, num_div = 4;
246 FLOAT32 div_fac;
247 FLOAT32 *lsf4 = &lsf[4 * ORDER];
248 for (i = 0; i < ORDER; i++) {
249 pstr_td_frame_data->lpc4_lsf[i] = lsf4[i];
250 }
251 i = num_div;
252 do {
253 num_lpc += lpc_present[i--];
254 } while (i >= 0 && num_lpc < 3);
255
256 last = i;
257
258 switch (num_lpc) {
259 case 3:
260 div_fac = (1.0f / 3.0f);
261 break;
262 case 2:
263 div_fac = (1.0f / 2.0f);
264 break;
265 default:
266 div_fac = (1.0f);
267 break;
268 }
269 for (k = 0; k < ORDER; k++) {
270 FLOAT32 temp = 0;
271 for (i = 4; i > last; i--) {
272 if (lpc_present[i]) {
273 temp = temp + (lsf[i * ORDER + k] * div_fac);
274 }
275 }
276 pstr_td_frame_data->lsf_adaptive_mean_cand[k] = temp;
277 }
278 }
279 }
280