• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define DEBUG
21 
22 #include <math.h>
23 #include <ixheaacd_type_def.h>
24 #include "ixheaacd_bitbuffer.h"
25 #include "ixheaacd_interface.h"
26 #include "ixheaacd_tns_usac.h"
27 #include "ixheaacd_cnst.h"
28 #include "ixheaacd_acelp_info.h"
29 #include "ixheaacd_sbrdecsettings.h"
30 #include "ixheaacd_info.h"
31 #include "ixheaacd_sbr_common.h"
32 #include "ixheaacd_drc_data_struct.h"
33 #include "ixheaacd_drc_dec.h"
34 #include "ixheaacd_sbrdecoder.h"
35 #include "ixheaacd_mps_polyphase.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 <ixheaacd_basic_op.h>
42 #include "ixheaacd_constants.h"
43 #include <ixheaacd_basic_ops32.h>
44 #include <ixheaacd_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 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] / (FREQ_DIV / sqrt(d[i] * d[i + 1])));
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, n, qn, nk, 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     nk = 0;
80     n = qn;
81     if (qn > 4) {
82       nk = (qn - 3) >> 1;
83       n = qn - nk * 2;
84     }
85 
86     if (qn > 0) {
87       code_book_idx = read_arr[position++];
88 
89       ptr_kv = &read_arr[position];
90       position += 8;
91 
92     } else {
93       code_book_idx = 0;
94       for (i = 0; i < 8; i++) ptr_kv = &kv[0];
95     }
96 
97     ixheaacd_rotated_gosset_mtx_dec(qn, code_book_idx, ptr_kv, &nvecq[k * 8]);
98   }
99 
100   return position;
101 }
102 
ixheaacd_avq_first_approx_abs(FLOAT32 * lsf,WORD32 * indx)103 static WORD32 ixheaacd_avq_first_approx_abs(FLOAT32 *lsf, WORD32 *indx) {
104   WORD32 i;
105   extern FLOAT32 ixheaacd_dico_lsf_abs_8b_flt[];
106   extern FLOAT32 ixheaacd_weight_table_avq[];
107   WORD32 position = 0;
108   WORD32 avq[ORDER];
109   FLOAT32 d[ORDER + 1], lsf_min;
110   FLOAT32 *ptr_w;
111 
112   ptr_w = &ixheaacd_weight_table_avq[(indx[0] * ORDER)];
113 
114   position++;
115 
116   for (i = 0; i < ORDER; i++) {
117     lsf[i] = ixheaacd_dico_lsf_abs_8b_flt[indx[0] * ORDER + i];
118   }
119 
120   position += ixheaacd_decoding_avq_tool(&indx[position], avq);
121 
122   d[0] = lsf[0];
123   d[ORDER] = FREQ_MAX - lsf[ORDER - 1];
124   for (i = 1; i < ORDER; i++) {
125     d[i] = lsf[i] - lsf[i - 1];
126   }
127 
128   lsf_min = LSF_GAP;
129   for (i = 0; i < ORDER; i++) {
130     lsf[i] += (ptr_w[i] * avq[i]);
131 
132     if (lsf[i] < lsf_min) lsf[i] = lsf_min;
133 
134     lsf_min = lsf[i] + LSF_GAP;
135   }
136 
137   return position;
138 }
139 
ixheaacd_avq_first_approx_rel(FLOAT32 * lsf,WORD32 * indx,WORD32 mode)140 WORD32 ixheaacd_avq_first_approx_rel(FLOAT32 *lsf, WORD32 *indx, WORD32 mode) {
141   WORD32 i;
142   FLOAT32 w[ORDER];
143   WORD32 avq[ORDER];
144   WORD32 position = 0;
145   FLOAT32 lsf_min;
146 
147   ixheaacd_lsf_weight_2st_flt(lsf, w, mode);
148 
149   position = ixheaacd_decoding_avq_tool(indx, avq);
150 
151   lsf_min = LSF_GAP;
152 
153   for (i = 0; i < ORDER; i++) {
154     lsf[i] += (w[i] * avq[i]);
155 
156     if (lsf[i] < lsf_min) lsf[i] = lsf_min;
157 
158     lsf_min = lsf[i] + LSF_GAP;
159   }
160 
161   return position;
162 }
163 
ixheaacd_alg_vec_dequant(ia_td_frame_data_struct * pstr_td_frame_data,WORD32 first_lpd_flag,FLOAT32 * lsf,WORD32 mod[])164 VOID ixheaacd_alg_vec_dequant(ia_td_frame_data_struct *pstr_td_frame_data,
165                               WORD32 first_lpd_flag, FLOAT32 *lsf,
166                               WORD32 mod[]) {
167   WORD32 i;
168   WORD32 *lpc_index, mode_lpc, pos = 0;
169 
170   lpc_index = pstr_td_frame_data->lpc_first_approx_idx;
171 
172   pos = ixheaacd_avq_first_approx_abs(&lsf[4 * ORDER], &lpc_index[0]);
173 
174   lpc_index += pos;
175 
176   if (first_lpd_flag) {
177     mode_lpc = lpc_index[0];
178     lpc_index++;
179 
180     if (mode_lpc == 0) {
181       pos = ixheaacd_avq_first_approx_abs(&lsf[0], &lpc_index[0]);
182 
183     } else if (mode_lpc == 1) {
184       for (i = 0; i < ORDER; i++) lsf[i] = lsf[4 * ORDER + i];
185       pos = ixheaacd_avq_first_approx_rel(&lsf[0], &lpc_index[0], 3);
186     }
187 
188     lpc_index += pos;
189   }
190 
191   if (mod[0] < 3) {
192     mode_lpc = lpc_index[0];
193     lpc_index++;
194 
195     if (mode_lpc == 0) {
196       pos = ixheaacd_avq_first_approx_abs(&lsf[2 * ORDER], &lpc_index[0]);
197     } else if (mode_lpc == 1) {
198       for (i = 0; i < ORDER; i++) lsf[2 * ORDER + i] = lsf[4 * ORDER + i];
199       pos = ixheaacd_avq_first_approx_rel(&lsf[2 * ORDER], &lpc_index[0], 3);
200     }
201 
202     lpc_index += pos;
203   }
204 
205   if (mod[0] < 2) {
206     mode_lpc = lpc_index[0];
207     lpc_index++;
208 
209     if (mode_lpc == 1) {
210       for (i = 0; i < ORDER; i++)
211         lsf[ORDER + i] = 0.5f * (lsf[i] + lsf[2 * ORDER + i]);
212     } else {
213       if (mode_lpc == 0) {
214         pos = ixheaacd_avq_first_approx_abs(&lsf[ORDER], &lpc_index[0]);
215       } else if (mode_lpc == 2) {
216         for (i = 0; i < ORDER; i++) lsf[ORDER + i] = lsf[2 * ORDER + i];
217         pos = ixheaacd_avq_first_approx_rel(&lsf[ORDER], &lpc_index[0], 2);
218       }
219 
220       lpc_index += pos;
221     }
222   }
223 
224   if (mod[2] < 2) {
225     mode_lpc = lpc_index[0];
226     lpc_index++;
227 
228     if (mode_lpc == 0) {
229       pos = ixheaacd_avq_first_approx_abs(&lsf[3 * ORDER], &lpc_index[0]);
230     } else if (mode_lpc == 1) {
231       for (i = 0; i < ORDER; i++)
232         lsf[3 * ORDER + i] = 0.5f * (lsf[2 * ORDER + i] + lsf[4 * ORDER + i]);
233       pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 1);
234     } else if (mode_lpc == 2) {
235       for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[2 * ORDER + i];
236       pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2);
237     } else if (mode_lpc == 3) {
238       for (i = 0; i < ORDER; i++) lsf[3 * ORDER + i] = lsf[4 * ORDER + i];
239       pos = ixheaacd_avq_first_approx_rel(&lsf[3 * ORDER], &lpc_index[0], 2);
240     }
241 
242     lpc_index += pos;
243   }
244 }
245