• 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 #include <string.h>
21 #include "ixheaacd_type_def.h"
22 
23 #include "ixheaacd_acelp_com.h"
24 #include "ixheaacd_bitbuffer.h"
25 
26 #include "ixheaacd_interface.h"
27 #include "ixheaacd_cnst.h"
28 #include "ixheaacd_info.h"
29 #include "ixheaacd_sbr_common.h"
30 #include "ixheaacd_drc_data_struct.h"
31 #include "ixheaacd_defines.h"
32 #include "ixheaacd_aac_rom.h"
33 #include "ixheaacd_pulsedata.h"
34 #include "ixheaacd_pns.h"
35 #include "ixheaacd_constants.h"
36 #include "ixheaacd_sbrdecsettings.h"
37 #include "ixheaacd_sbrdecoder.h"
38 #include "ixheaacd_acelp_info.h"
39 #include "ixheaacd_tns_usac.h"
40 #include "ixheaacd_ec_defines.h"
41 #include "ixheaacd_ec_struct_def.h"
42 #include "ixheaacd_main.h"
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_ec.h"
45 #include "ixheaacd_arith_dec.h"
46 #include "ixheaacd_func_def.h"
47 #include "ixheaacd_basic_ops32.h"
48 #include "ixheaacd_error_standards.h"
49 #include "ixheaacd_error_codes.h"
50 
51 extern const ia_usac_samp_rate_info ixheaacd_samp_rate_info[];
52 
ixheaacd_get_mode_lpc(WORD32 lpc_set,ia_bit_buf_struct * it_bit_buff,WORD32 * nk_mode)53 WORD32 ixheaacd_get_mode_lpc(WORD32 lpc_set, ia_bit_buf_struct *it_bit_buff, WORD32 *nk_mode) {
54   WORD32 mode_lpc = 0;
55   switch (lpc_set) {
56     case 4:
57       mode_lpc = 0;
58       break;
59     case 0:
60     case 2:
61       mode_lpc = ixheaacd_read_bits_buf(it_bit_buff, 1);
62       if (mode_lpc == 1) *nk_mode = 3;
63       break;
64     case 1:
65       if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0)
66         mode_lpc = *nk_mode = 2;
67       else {
68         if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0)
69           mode_lpc = *nk_mode = 0;
70         else
71           mode_lpc = *nk_mode = 1;
72       }
73       break;
74     case 3:
75       if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0)
76         mode_lpc = *nk_mode = 1;
77       else {
78         if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0)
79           mode_lpc = *nk_mode = 0;
80         else {
81           if (ixheaacd_read_bits_buf(it_bit_buff, 1) == 0)
82             mode_lpc = 2;
83           else
84             mode_lpc = 3;
85           *nk_mode = 2;
86         }
87       }
88       break;
89   }
90   return mode_lpc;
91 }
92 
ixheaacd_qn_data(WORD32 nk_mode,WORD32 * qn,ia_bit_buf_struct * it_bit_buff)93 VOID ixheaacd_qn_data(WORD32 nk_mode, WORD32 *qn, ia_bit_buf_struct *it_bit_buff) {
94   WORD32 k;
95   switch (nk_mode) {
96     case 1:
97       for (k = 0; k < 2; k++) {
98         while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) {
99           qn[k] += 1;
100         }
101         if (qn[k] > 0) qn[k] += 1;
102       }
103       break;
104     case 0:
105     case 2:
106     case 3:
107       for (k = 0; k < 2; k++) qn[k] = 2 + ixheaacd_read_bits_buf(it_bit_buff, 2);
108 
109       if (nk_mode == 2) {
110         for (k = 0; k < 2; k++) {
111           if (qn[k] > 4) {
112             qn[k] = 0;
113 
114             while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) qn[k] += 1;
115 
116             if (qn[k] > 0) qn[k] += 4;
117           }
118         }
119       } else {
120         for (k = 0; k < 2; k++) {
121           if (qn[k] > 4) {
122             WORD32 qn_ext = 0;
123             while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) qn_ext += 1;
124 
125             switch (qn_ext) {
126               case 0:
127                 qn[k] = 5;
128                 break;
129               case 1:
130                 qn[k] = 6;
131                 break;
132               case 2:
133                 qn[k] = 0;
134                 break;
135               default:
136                 qn[k] = qn_ext + 4;
137                 break;
138             }
139           }
140         }
141       }
142       break;
143   }
144   return;
145 }
146 
ixheaacd_code_book_indices(ia_td_frame_data_struct * pstr_td_frame_data,WORD32 nk_mode,WORD32 * pos,ia_bit_buf_struct * it_bit_buff)147 VOID ixheaacd_code_book_indices(ia_td_frame_data_struct *pstr_td_frame_data, WORD32 nk_mode,
148                                 WORD32 *pos, ia_bit_buf_struct *it_bit_buff) {
149   WORD32 k, qn[2] = {0, 0}, nk, n, i;
150 
151   ixheaacd_qn_data(nk_mode, &qn[0], it_bit_buff);
152 
153   pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = qn[0];
154   pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] = qn[1];
155 
156   for (k = 0; k < 2; k++) {
157     if (qn[k] > 0) {
158       if (qn[k] > 4) {
159         nk = (qn[k] - 3) / 2;
160         n = qn[k] - nk * 2;
161       } else {
162         nk = 0;
163         n = qn[k];
164       }
165       pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] =
166           ixheaacd_read_bits_buf(it_bit_buff, 4 * n);
167 
168       for (i = 0; i < 8; i++)
169         pstr_td_frame_data->lpc_first_approx_idx[(*pos)++] =
170             ixheaacd_read_bits_buf(it_bit_buff, nk);
171     }
172   }
173   return;
174 }
175 
ixheaacd_lpc_data(WORD32 first_lpd_flag,WORD32 mod[],ia_td_frame_data_struct * pstr_td_frame_data,ia_bit_buf_struct * it_bit_buff)176 VOID ixheaacd_lpc_data(WORD32 first_lpd_flag, WORD32 mod[],
177                        ia_td_frame_data_struct *pstr_td_frame_data,
178                        ia_bit_buf_struct *it_bit_buff) {
179   WORD32 mode_lpc, nk_mode = 0, j = 0;
180 
181   mode_lpc = ixheaacd_get_mode_lpc(4, it_bit_buff, &nk_mode);
182 
183   pstr_td_frame_data->lpc_first_approx_idx[j++] = ixheaacd_read_bits_buf(it_bit_buff, 8);
184 
185   ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff);
186   if (first_lpd_flag) {
187     mode_lpc = ixheaacd_get_mode_lpc(0, it_bit_buff, &nk_mode);
188     pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc;
189 
190     if (mode_lpc == 0)
191       pstr_td_frame_data->lpc_first_approx_idx[j++] = ixheaacd_read_bits_buf(it_bit_buff, 8);
192 
193     ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff);
194   }
195   if (mod[0] < 3) {
196     mode_lpc = ixheaacd_get_mode_lpc(2, it_bit_buff, &nk_mode);
197     pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc;
198 
199     if (mode_lpc == 0)
200       pstr_td_frame_data->lpc_first_approx_idx[j++] = ixheaacd_read_bits_buf(it_bit_buff, 8);
201 
202     ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff);
203   }
204   if (mod[0] < 2) {
205     mode_lpc = ixheaacd_get_mode_lpc(1, it_bit_buff, &nk_mode);
206     pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc;
207 
208     if (mode_lpc == 0)
209       pstr_td_frame_data->lpc_first_approx_idx[j++] = ixheaacd_read_bits_buf(it_bit_buff, 8);
210 
211     if (mode_lpc != 1) ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff);
212   }
213   if (mod[2] < 2) {
214     mode_lpc = ixheaacd_get_mode_lpc(3, it_bit_buff, &nk_mode);
215     pstr_td_frame_data->lpc_first_approx_idx[j++] = mode_lpc;
216 
217     if (mode_lpc == 0)
218       pstr_td_frame_data->lpc_first_approx_idx[j++] = ixheaacd_read_bits_buf(it_bit_buff, 8);
219 
220     ixheaacd_code_book_indices(pstr_td_frame_data, nk_mode, &j, it_bit_buff);
221   }
222   return;
223 }
224 
ixheaacd_fac_decoding(WORD32 fac_length,WORD32 k,WORD32 * fac_prm,ia_bit_buf_struct * it_bit_buff)225 VOID ixheaacd_fac_decoding(WORD32 fac_length, WORD32 k, WORD32 *fac_prm,
226                            ia_bit_buf_struct *it_bit_buff) {
227   WORD32 i, j, n, qn, nk, kv[8];
228   long code_book_index;
229 
230   for (i = 0; i < fac_length; i += 8) {
231     qn = 0;
232     while (ixheaacd_read_bits_buf(it_bit_buff, 1) == 1) {
233       qn += 1;
234     }
235     if (qn != 0) qn += 1;
236 
237     nk = 0;
238     n = qn;
239     if (qn > 4) {
240       nk = (qn - 3) >> 1;
241       n = qn - nk * 2;
242     }
243 
244     code_book_index = ixheaacd_read_bits_buf(it_bit_buff, 4 * n);
245 
246     for (j = 0; j < 8; j++) {
247       kv[j] = ixheaacd_read_bits_buf(it_bit_buff, nk);
248     }
249 
250     ixheaacd_rotated_gosset_mtx_dec(qn, code_book_index, kv, &fac_prm[k * FAC_LENGTH + i]);
251   }
252 }
253 
254 const UWORD8 ixheaacd_num_bites_celp_coding[8][4] = {
255     {5, 5, 5, 5},     {9, 9, 5, 5},     {9, 9, 9, 9}, {13, 13, 9, 9},
256     {13, 13, 13, 13}, {16, 16, 16, 16}, {1, 5, 1, 5}, {1, 5, 5, 5}};
257 
ixheaacd_acelp_decoding(WORD32 k,ia_usac_data_struct * usac_data,ia_td_frame_data_struct * pstr_td_frame_data,ia_bit_buf_struct * it_bit_buff,WORD32 chan)258 VOID ixheaacd_acelp_decoding(WORD32 k, ia_usac_data_struct *usac_data,
259                              ia_td_frame_data_struct *pstr_td_frame_data,
260                              ia_bit_buf_struct *it_bit_buff, WORD32 chan) {
261   WORD32 sfr, kk;
262   WORD32 nb_subfr = usac_data->num_subfrm;
263   const UWORD8 *ptr_num_bits =
264       &ixheaacd_num_bites_celp_coding[pstr_td_frame_data->acelp_core_mode][0];
265 
266   chan = 0;
267   pstr_td_frame_data->mean_energy[k] = ixheaacd_read_bits_buf(it_bit_buff, 2);
268 
269   for (sfr = 0; sfr < nb_subfr; sfr++) {
270     kk = k * 4 + sfr;
271 
272     if ((sfr == 0) || ((nb_subfr == 4) && (sfr == 2)))
273       pstr_td_frame_data->acb_index[kk] = ixheaacd_read_bits_buf(it_bit_buff, 9);
274 
275     else
276       pstr_td_frame_data->acb_index[kk] = ixheaacd_read_bits_buf(it_bit_buff, 6);
277 
278     pstr_td_frame_data->ltp_filtering_flag[kk] = ixheaacd_read_bits_buf(it_bit_buff, 1);
279 
280     if (pstr_td_frame_data->acelp_core_mode == 5) {
281       pstr_td_frame_data->icb_index[kk][0] =
282           ixheaacd_read_bits_buf(it_bit_buff, 2);
283       pstr_td_frame_data->icb_index[kk][1] =
284           ixheaacd_read_bits_buf(it_bit_buff, 2);
285       pstr_td_frame_data->icb_index[kk][2] =
286           ixheaacd_read_bits_buf(it_bit_buff, 2);
287       pstr_td_frame_data->icb_index[kk][3] =
288           ixheaacd_read_bits_buf(it_bit_buff, 2);
289       pstr_td_frame_data->icb_index[kk][4] =
290           ixheaacd_read_bits_buf(it_bit_buff, 14);
291       pstr_td_frame_data->icb_index[kk][5] =
292           ixheaacd_read_bits_buf(it_bit_buff, 14);
293       pstr_td_frame_data->icb_index[kk][6] =
294           ixheaacd_read_bits_buf(it_bit_buff, 14);
295       pstr_td_frame_data->icb_index[kk][7] =
296           ixheaacd_read_bits_buf(it_bit_buff, 14);
297     } else {
298       pstr_td_frame_data->icb_index[kk][0] =
299           ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[0]);
300       pstr_td_frame_data->icb_index[kk][1] =
301           ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[1]);
302       pstr_td_frame_data->icb_index[kk][2] =
303           ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[2]);
304       pstr_td_frame_data->icb_index[kk][3] =
305           ixheaacd_read_bits_buf(it_bit_buff, ptr_num_bits[3]);
306     }
307 
308     pstr_td_frame_data->gains[kk] = ixheaacd_read_bits_buf(it_bit_buff, 7);
309   }
310 }
311 
ixheaacd_tcx_coding(ia_usac_data_struct * usac_data,pWORD32 quant,WORD32 k,WORD32 first_tcx_flag,ia_td_frame_data_struct * pstr_td_frame_data,ia_bit_buf_struct * it_bit_buff)312 VOID ixheaacd_tcx_coding(ia_usac_data_struct *usac_data, pWORD32 quant, WORD32 k,
313                          WORD32 first_tcx_flag, ia_td_frame_data_struct *pstr_td_frame_data,
314                          ia_bit_buf_struct *it_bit_buff) {
315   pstr_td_frame_data->noise_factor[k] = ixheaacd_read_bits_buf(it_bit_buff, 3);
316 
317   pstr_td_frame_data->global_gain[k] = ixheaacd_read_bits_buf(it_bit_buff, 7);
318 
319   switch (pstr_td_frame_data->mod[k]) {
320     case 1:
321       pstr_td_frame_data->tcx_lg[k] = usac_data->len_subfrm;
322       break;
323     case 2:
324       pstr_td_frame_data->tcx_lg[k] = 2 * (usac_data->len_subfrm);
325       break;
326     case 3:
327       pstr_td_frame_data->tcx_lg[k] = 4 * (usac_data->len_subfrm);
328       break;
329   }
330 
331   if (first_tcx_flag) {
332     if (usac_data->usac_independency_flg) {
333       pstr_td_frame_data->arith_reset_flag = 1;
334     } else {
335       pstr_td_frame_data->arith_reset_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
336     }
337   }
338 
339   ixheaacd_arith_data(pstr_td_frame_data, quant, usac_data, it_bit_buff, (first_tcx_flag), k);
340 
341   return;
342 }
343 
ixheaacd_lpd_channel_stream(ia_usac_data_struct * usac_data,ia_td_frame_data_struct * pstr_td_frame_data,ia_bit_buf_struct * it_bit_buff,FLOAT32 * synth)344 WORD32 ixheaacd_lpd_channel_stream(ia_usac_data_struct *usac_data,
345                                    ia_td_frame_data_struct *pstr_td_frame_data,
346                                    ia_bit_buf_struct *it_bit_buff, FLOAT32 *synth) {
347   WORD32 lpd_mode, k, cnt, ii;
348   WORD32 first_tcx_flag;
349   WORD32 *quant;
350   WORD32 core_mode_last = 0, fac_data_present;
351   WORD32 *fac_data;
352   WORD32 first_lpd_flag = 0;
353   WORD32 short_fac_flag;
354   WORD32 bpf_control_info = 0;
355   WORD32 chan = usac_data->present_chan;
356   WORD32 last_lpd_mode = usac_data->str_tddec[chan]->mode_prev;
357   WORD32 err = 0;
358   short_fac_flag = 0;
359 
360   jmp_buf local;
361 
362   if (usac_data->ec_flag) {
363     err = setjmp(local);
364   }
365 
366   if (usac_data->sampling_rate_idx <= 5 && usac_data->ec_flag) {
367     usac_data->frame_ok = 0;
368   }
369 
370   if (err == 0 && usac_data->frame_ok == 1) {
371     if (usac_data->ec_flag) {
372       it_bit_buff->xaac_jmp_buf = &local;
373     }
374     pstr_td_frame_data->acelp_core_mode = ixheaacd_read_bits_buf(it_bit_buff, 3);
375 
376     lpd_mode = ixheaacd_read_bits_buf(it_bit_buff, 5);
377 
378     if ((lpd_mode > 25 || lpd_mode < 0)) {
379       if (usac_data->ec_flag) {
380         longjmp(*(it_bit_buff->xaac_jmp_buf),
381                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
382       } else {
383         return IA_FATAL_ERROR;
384       }
385     }
386 
387     if (lpd_mode == 25) {
388       pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = pstr_td_frame_data->mod[2] =
389           pstr_td_frame_data->mod[3] = 3;
390     } else if (lpd_mode == 24) {
391       pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = pstr_td_frame_data->mod[2] =
392           pstr_td_frame_data->mod[3] = 2;
393     } else {
394       if (lpd_mode >= 20) {
395         pstr_td_frame_data->mod[0] = lpd_mode & 1;
396         pstr_td_frame_data->mod[1] = (lpd_mode >> 1) & 1;
397         pstr_td_frame_data->mod[2] = pstr_td_frame_data->mod[3] = 2;
398       } else if (lpd_mode >= 16) {
399         pstr_td_frame_data->mod[0] = pstr_td_frame_data->mod[1] = 2;
400         pstr_td_frame_data->mod[2] = lpd_mode & 1;
401         pstr_td_frame_data->mod[3] = (lpd_mode >> 1) & 1;
402       } else {
403         pstr_td_frame_data->mod[0] = lpd_mode & 1;
404         pstr_td_frame_data->mod[1] = (lpd_mode >> 1) & 1;
405         pstr_td_frame_data->mod[2] = (lpd_mode >> 2) & 1;
406         pstr_td_frame_data->mod[3] = (lpd_mode >> 3) & 1;
407       }
408     }
409 
410     bpf_control_info = ixheaacd_read_bits_buf(it_bit_buff, 1);
411 
412     core_mode_last = ixheaacd_read_bits_buf(it_bit_buff, 1);
413 
414     fac_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
415 
416     first_lpd_flag = (core_mode_last == 0) ? 1 : 0;
417 
418     quant = pstr_td_frame_data->x_tcx_invquant;
419     first_tcx_flag = 1;
420     k = 0;
421     while (k < 4) {
422       if (k == 0) {
423         if ((core_mode_last == 1) && (fac_data_present == 1))
424           ixheaacd_fac_decoding((usac_data->len_subfrm) / 2, k, pstr_td_frame_data->fac,
425                                 it_bit_buff);
426       } else {
427         if (((last_lpd_mode == 0) && (pstr_td_frame_data->mod[k] > 0)) ||
428             ((last_lpd_mode > 0) && (pstr_td_frame_data->mod[k] == 0)))
429           ixheaacd_fac_decoding((usac_data->len_subfrm) / 2, k, pstr_td_frame_data->fac,
430                                 it_bit_buff);
431       }
432 
433       if (pstr_td_frame_data->mod[k] == 0) {
434         ixheaacd_acelp_decoding(k, usac_data, pstr_td_frame_data, it_bit_buff, chan);
435         last_lpd_mode = 0;
436         pstr_td_frame_data->tcx_lg[k] = 0;
437         k += 1;
438       } else {
439         ixheaacd_tcx_coding(usac_data, quant, k, first_tcx_flag, pstr_td_frame_data, it_bit_buff);
440         last_lpd_mode = pstr_td_frame_data->mod[k];
441         quant += pstr_td_frame_data->tcx_lg[k];
442 
443         cnt = 1 << (pstr_td_frame_data->mod[k] - 1);
444 
445         for (ii = 0; ii < cnt - 1; ii++) pstr_td_frame_data->tcx_lg[k + 1 + ii] = 0;
446 
447         k += cnt;
448         first_tcx_flag = 0;
449       }
450     }
451 
452     ixheaacd_lpc_data(first_lpd_flag, pstr_td_frame_data->mod, pstr_td_frame_data, it_bit_buff);
453 
454     if ((core_mode_last == 0) && (fac_data_present == 1)) {
455       WORD32 fac_length;
456       short_fac_flag = ixheaacd_read_bits_buf(it_bit_buff, 1);
457 
458       fac_length = (short_fac_flag) ? ((usac_data->ccfl) / 16) : ((usac_data->ccfl) / 8);
459 
460       fac_data = pstr_td_frame_data->fac_data;
461       fac_data[0] = ixheaacd_read_bits_buf(it_bit_buff, 7);
462       if ((pstr_td_frame_data->fac_data[0] < 0) || (pstr_td_frame_data->fac_data[0] > 128)) {
463         if (usac_data->ec_flag) {
464           longjmp(*(it_bit_buff->xaac_jmp_buf),
465                   IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
466         } else {
467           return IA_FATAL_ERROR;
468         }
469       }
470       ixheaacd_fac_decoding(fac_length, 0, &fac_data[1], it_bit_buff);
471     }
472     if (fac_data_present == 0) {
473       memset(&pstr_td_frame_data->fac_data[0], 0, sizeof(pstr_td_frame_data->fac_data));
474     }
475     if (usac_data->ec_flag && usac_data->frame_ok) {
476       usac_data->bpf_control_info = bpf_control_info;
477       usac_data->core_mode_last = core_mode_last;
478       usac_data->first_lpd_flag = first_lpd_flag;
479     }
480   } else {
481     usac_data->frame_ok = 0;
482   }
483 
484   if (usac_data->ec_flag) {
485     usac_data->str_error_concealment[chan].pstr_ec_scratch =
486         (ia_ec_scratch_str *)&usac_data->str_error_concealment[chan].str_ec_scratch;
487 
488     ixheaacd_usac_apply_ec(usac_data, &ixheaacd_samp_rate_info[0], chan);
489 
490     if (usac_data->frame_ok == 0) {
491       bpf_control_info = usac_data->bpf_control_info;
492       core_mode_last = usac_data->core_mode_last;
493       first_lpd_flag = usac_data->first_lpd_flag;
494     }
495   }
496   if (usac_data->frame_ok) {
497     err = ixheaacd_lpd_dec(usac_data, usac_data->str_tddec[chan], pstr_td_frame_data, synth,
498                            first_lpd_flag, short_fac_flag, bpf_control_info);
499   } else {
500     if (usac_data->ec_flag) {
501       usac_data->fac_data_present[chan] = 0;
502       err = ixheaacd_lpd_dec(usac_data, usac_data->str_tddec[chan],
503                              &usac_data->td_frame_data_prev[chan], synth, first_lpd_flag,
504                              short_fac_flag, bpf_control_info);
505     }
506   }
507 
508   return (err);
509 }
510 
ixheaacd_tw_buff_update(ia_usac_data_struct * usac_data,WORD32 i,ia_usac_lpd_decoder_handle st)511 WORD32 ixheaacd_tw_buff_update(ia_usac_data_struct *usac_data, WORD32 i,
512                                ia_usac_lpd_decoder_handle st) {
513   WORD32 *p_ioverlap = usac_data->overlap_data_ptr[i];
514   WORD32 td_frame_prev = usac_data->td_frame_prev[i];
515   WORD32 window_sequence_last = usac_data->window_sequence_last[i];
516   WORD32 tw_mdct = usac_data->tw_mdct[0];
517 
518   if (!td_frame_prev) {
519     if (tw_mdct) {
520       if (usac_data->ec_flag == 0) {
521         return -1;
522       } else {
523         tw_mdct = 0;
524       }
525     } else {
526       ixheaacd_reset_acelp_data_fix(usac_data, st, p_ioverlap,
527                                     (window_sequence_last == EIGHT_SHORT_SEQUENCE), 0);
528     }
529   }
530   return 0;
531 }
532 
ixheaacd_td_frm_dec(ia_usac_data_struct * usac_data,WORD32 k,WORD32 mod0)533 VOID ixheaacd_td_frm_dec(ia_usac_data_struct *usac_data, WORD32 k, WORD32 mod0) {
534   WORD32 i;
535   WORD32 lfac = 0;
536 
537   WORD32 *p_out_idata = usac_data->output_data_ptr[k];
538   WORD32 *p_ioverlap = usac_data->overlap_data_ptr[k];
539   WORD32 nlong = usac_data->ccfl;
540   WORD32 window_sequence_last = usac_data->window_sequence_last[k];
541   WORD32 td_frame_prev = usac_data->td_frame_prev[k];
542   WORD32 tw_mdct = usac_data->tw_mdct[0];
543   WORD32 nshort = nlong / 8;
544   WORD32 *p_in_idata = p_out_idata;
545 
546   if (!td_frame_prev) {
547     if (window_sequence_last == EIGHT_SHORT_SEQUENCE) {
548       lfac = nshort / 2;
549     } else {
550       lfac = nshort;
551     }
552   }
553 
554   if (!td_frame_prev && (mod0 == 0)) {
555     for (i = 0; i < (nlong / 2) - lfac - (LEN_SUBFR); i++) {
556       p_in_idata[i] = 0;
557     }
558   } else if (!td_frame_prev && (mod0 > 0)) {
559     for (i = 0; i < (nlong / 2) - lfac; i++) {
560       p_in_idata[i] = 0;
561     }
562   }
563 
564   if (tw_mdct) {
565     if (!td_frame_prev && (mod0 == 0)) {
566       for (i = (nlong / 2) - lfac - (LEN_SUBFR); i < nlong / 2; i++) {
567         p_ioverlap[i + (nlong / 2)] = 0;
568       }
569     }
570     for (i = 0; i < nlong / 2; i++) {
571       p_out_idata[i] = p_ioverlap[i] << 1;
572       p_out_idata[i + nlong / 2] =
573           ixheaacd_add32_sat(p_ioverlap[i + nlong / 2] << 1, p_in_idata[i]);
574       p_ioverlap[i] = ixheaacd_add32_sat(p_in_idata[i + (nlong / 2)] >> 1, p_ioverlap[i + nlong]);
575       p_ioverlap[i + (nlong / 2)] = 0;
576       p_ioverlap[i + nlong] = 0;
577       p_ioverlap[i + nlong + (nlong / 2)] = 0;
578     }
579   } else {
580     if (!td_frame_prev && (mod0 == 0)) {
581       for (i = (nlong / 2) - lfac - (LEN_SUBFR); i < nlong / 2; i++) {
582         p_ioverlap[i] = 0;
583       }
584     } else if (!td_frame_prev) {
585       for (i = (nlong / 2) - lfac; i < nlong; i++) {
586         p_ioverlap[i] = 0;
587       }
588     }
589     for (i = 0; i < nlong; i++) {
590       p_out_idata[i] = ixheaacd_add32_sat(p_ioverlap[i] << 1, p_in_idata[i]);
591       p_ioverlap[i] = 0;
592     }
593   }
594 }
595