• 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 <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 
24 #include "ixheaacd_type_def.h"
25 
26 #include "ixheaacd_cnst.h"
27 
28 #include "ixheaacd_bitbuffer.h"
29 
30 #include "ixheaacd_interface.h"
31 #include "ixheaacd_acelp_info.h"
32 
33 #include "ixheaacd_tns_usac.h"
34 #include "ixheaacd_acelp_info.h"
35 
36 #include "ixheaacd_sbrdecsettings.h"
37 #include "ixheaacd_info.h"
38 #include "ixheaacd_sbr_common.h"
39 #include "ixheaacd_drc_data_struct.h"
40 #include "ixheaacd_drc_dec.h"
41 
42 #include "ixheaacd_sbrdecoder.h"
43 #include "ixheaacd_mps_polyphase.h"
44 #include "ixheaacd_sbr_const.h"
45 
46 #include "ixheaacd_main.h"
47 #include "ixheaacd_arith_dec.h"
48 #include "ixheaacd_tns_usac.h"
49 
50 #include "ixheaacd_bit_extract.h"
51 
52 #include "ixheaacd_constants.h"
53 #include "ixheaacd_basic_ops32.h"
54 #include "ixheaacd_basic_ops40.h"
55 
56 #include "ixheaacd_func_def.h"
57 
58 #include "ixheaacd_defines.h"
59 #include "ixheaacd_windows.h"
60 
61 #include "ixheaacd_vec_baisc_ops.h"
62 #include "ixheaacd_config.h"
63 
64 const WORD16 ixheaacd_mdst_fcoeff_long_sin[] = {0, 0, -16384, 0, 16384, 0, 0};
65 const WORD16 ixheaacd_mdst_fcoeff_long_kbd[] = {-2998, 0, -19052, 0,
66                                                 19052, 0, 2998};
67 const WORD16 ixheaacd_mdst_fcoeff_long_sin_kbd[] = {-1499, -1876, -17718, 0,
68                                                     17718, 1876,  1499};
69 const WORD16 ixheaacd_mdst_fcoeff_long_kbd_sin[] = {-1499, 1876,  -17718, 0,
70                                                     17718, -1876, 1499};
71 
72 const WORD16 *const ixheaacd_mdst_fcoeff_longshort_curr[2][2] = {
73     {ixheaacd_mdst_fcoeff_long_sin, ixheaacd_mdst_fcoeff_long_sin_kbd},
74     {ixheaacd_mdst_fcoeff_long_kbd_sin, ixheaacd_mdst_fcoeff_long_kbd}};
75 
76 const WORD16 ixheaacd_mdst_fcoeff_start_sin[] = {-3364, -3401, -18584, 0,
77                                                  18584, 3401,  3364};
78 const WORD16 ixheaacd_mdst_fcoeff_start_kbd[] = {-4932, -1572, -19942, 0,
79                                                  19942, 1572,  4932};
80 const WORD16 ixheaacd_mdst_fcoeff_start_sin_kbd[] = {-3433, -3447, -18608, 0,
81                                                      18608, 3447,  3433};
82 const WORD16 ixheaacd_mdst_fcoeff_start_kbd_sin[] = {-4863, -1525, -19918, 0,
83                                                      19918, 1525,  4863};
84 
85 const WORD16 *const ixheaacd_mdst_fcoeff_start_curr[2][2] = {
86     {ixheaacd_mdst_fcoeff_start_sin, ixheaacd_mdst_fcoeff_start_sin_kbd},
87     {ixheaacd_mdst_fcoeff_start_kbd_sin, ixheaacd_mdst_fcoeff_start_kbd}};
88 
89 const WORD16 ixheaacd_mdst_fcoeff_stop_sin[] = {-3364, 3401,  -18584, 0,
90                                                 18584, -3401, 3364};
91 const WORD16 ixheaacd_mdst_fcoeff_stop_kbd[] = {-4932, 1572,  -19942, 0,
92                                                 19942, -1572, 4932};
93 const WORD16 ixheaacd_mdst_fcoeff_stop_sin_kbd[] = {-4863, 1525,  -19918, 0,
94                                                     19918, -1525, 4863};
95 const WORD16 ixheaacd_mdst_fcoeff_stop_kbd_sin[] = {-3433, 3447,  -18608, 0,
96                                                     18608, -3447, 3433};
97 
98 const WORD16 *const ixheaacd_mdst_fcoeff_stop_cur[2][2] = {
99     {ixheaacd_mdst_fcoeff_stop_sin, ixheaacd_mdst_fcoeff_stop_sin_kbd},
100     {ixheaacd_mdst_fcoeff_stop_kbd_sin, ixheaacd_mdst_fcoeff_stop_kbd}};
101 
102 const WORD16 ixheaacd_mdst_fcoeff_stopstart_sin[] = {-6728, 0, -20785, 0,
103                                                      20785, 0, 6728};
104 const WORD16 ixheaacd_mdst_fcoeff_stopstart_kbd[] = {-6866, -0, -20831, 0,
105                                                      20831, 0,  6866};
106 const WORD16 ixheaacd_mdst_fcoeff_stopstart_sin_kbd[] = {-6797, -46, -20808, 0,
107                                                          20808, 46,  6797};
108 const WORD16 ixheaacd_mdst_fcoeff_stopstart_kbd_sin[] = {-6797, 46, -20808, 0,
109                                                          20808, 46, 6797};
110 
111 const WORD16 *const ixheaacd_mdst_fcoeff_stopstart_cur[2][2] = {
112     {ixheaacd_mdst_fcoeff_stopstart_sin,
113      ixheaacd_mdst_fcoeff_stopstart_sin_kbd},
114     {ixheaacd_mdst_fcoeff_stopstart_kbd_sin,
115      ixheaacd_mdst_fcoeff_stopstart_kbd}};
116 
117 const WORD16 ixheaacd_mdst_fcoeff_l_s_start_left_sin[] = {
118     -0, 3477, 8192, 10430, 8192, 3477, -0};
119 const WORD16 ixheaacd_mdst_fcoeff_l_s_start_left_kbd[] = {
120     1950, 4054, 6114, 6982, 6114, 4054, 1950};
121 
122 const WORD16 ixheaacd_mdst_fcoeff_stop_stopstart_left_sin[] = {
123     1262, 1285, 1299, 1304, 1299, 1285, 1262};
124 const WORD16 ixheaacd_mdst_fcoeff_stop_stopstart_left_kbd[] = {
125     857, 866, 871, 873, 871, 866, 857};
126 
127 const WORD16 *const ixheaacd_mdst_fcoeff_l_s_start_left_prev[2] = {
128     ixheaacd_mdst_fcoeff_l_s_start_left_sin,
129     ixheaacd_mdst_fcoeff_l_s_start_left_kbd};
130 const WORD16 *const ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[2] = {
131     ixheaacd_mdst_fcoeff_stop_stopstart_left_sin,
132     ixheaacd_mdst_fcoeff_stop_stopstart_left_kbd};
133 
134 #define ONE_BY_TWO_POW_14 0.00006103515625
135 #define ONE_BY_TWO_POW_15 0.000030517578125
136 
ixheaacd_usac_cplx_save_prev(ia_sfb_info_struct * info,WORD32 * l_spec,WORD32 * r_spec,WORD32 * l_spec_prev,WORD32 * r_spec_prev)137 void ixheaacd_usac_cplx_save_prev(ia_sfb_info_struct *info, WORD32 *l_spec,
138                                   WORD32 *r_spec, WORD32 *l_spec_prev,
139                                   WORD32 *r_spec_prev) {
140   WORD32 ixheaacd_drc_offset;
141 
142   ixheaacd_drc_offset = info->samp_per_bk - info->bins_per_sbk;
143 
144   memcpy(l_spec_prev + ixheaacd_drc_offset, l_spec + ixheaacd_drc_offset,
145          sizeof(WORD32) * info->bins_per_sbk);
146   memcpy(r_spec_prev + ixheaacd_drc_offset, r_spec + ixheaacd_drc_offset,
147          sizeof(WORD32) * info->bins_per_sbk);
148 }
149 
ixheaacd_cplx_pred_data(ia_usac_data_struct * usac_data,ia_usac_tmp_core_coder_struct * pstr_core_coder,WORD32 num_window_groups,ia_bit_buf_struct * it_bit_buff)150 static VOID ixheaacd_cplx_pred_data(
151     ia_usac_data_struct *usac_data,
152     ia_usac_tmp_core_coder_struct *pstr_core_coder, WORD32 num_window_groups,
153     ia_bit_buf_struct *it_bit_buff) {
154   ia_huff_code_book_struct *ptr_huff_code_book = &ixheaacd_book;
155   const ia_huff_code_word_struct *ptr_huff_code_word =
156       ptr_huff_code_book->pstr_huff_code_word;
157   WORD32 cplx_pred_all;
158   WORD32 delta_code_time;
159   WORD32 g, sfb;
160   WORD32 dpcm_alpha, last_alpha_q_re, last_alpha_q_im;
161   UWORD8 max_sfb_ste = pstr_core_coder->max_sfb_ste;
162 
163   WORD32(*alpha_q_re)[SFB_NUM_MAX] = usac_data->alpha_q_re;
164   WORD32(*alpha_q_im)[SFB_NUM_MAX] = usac_data->alpha_q_im;
165   WORD32 *alpha_q_re_prev = usac_data->alpha_q_re_prev;
166   WORD32 *alpha_q_im_prev = usac_data->alpha_q_im_prev;
167   UWORD8(*cplx_pred_used)[SFB_NUM_MAX] = usac_data->cplx_pred_used;
168 
169   cplx_pred_all = ixheaacd_read_bits_buf(it_bit_buff, 1);
170 
171   if (cplx_pred_all == 0) {
172     for (g = 0; g < num_window_groups; g++) {
173       for (sfb = 0; sfb < max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
174         cplx_pred_used[g][sfb] = ixheaacd_read_bits_buf(it_bit_buff, 1);
175 
176         if (sfb + 1 < max_sfb_ste)
177           cplx_pred_used[g][sfb + 1] = cplx_pred_used[g][sfb];
178       }
179       for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++)
180         cplx_pred_used[g][sfb] = 0;
181     }
182   } else {
183     for (g = 0; g < num_window_groups; g++) {
184       for (sfb = 0; sfb < max_sfb_ste; sfb++) cplx_pred_used[g][sfb] = 1;
185 
186       for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++)
187         cplx_pred_used[g][sfb] = 0;
188     }
189   }
190 
191   pstr_core_coder->pred_dir = ixheaacd_read_bits_buf(it_bit_buff, 1);
192 
193   pstr_core_coder->complex_coef = ixheaacd_read_bits_buf(it_bit_buff, 1);
194 
195   if (pstr_core_coder->complex_coef) {
196     if (usac_data->usac_independency_flg)
197       pstr_core_coder->use_prev_frame = 0;
198     else
199       pstr_core_coder->use_prev_frame = ixheaacd_read_bits_buf(it_bit_buff, 1);
200   }
201 
202   if (usac_data->usac_independency_flg)
203     delta_code_time = 0;
204   else
205     delta_code_time = ixheaacd_read_bits_buf(it_bit_buff, 1);
206 
207   for (g = 0; g < num_window_groups; g++) {
208     for (sfb = 0; sfb < max_sfb_ste; sfb += SFB_PER_PRED_BAND) {
209       if (delta_code_time == 1) {
210         last_alpha_q_re = alpha_q_re_prev[sfb];
211         last_alpha_q_im = alpha_q_im_prev[sfb];
212       } else {
213         if (sfb > 0) {
214           last_alpha_q_re = alpha_q_re[g][sfb - 1];
215           last_alpha_q_im = alpha_q_im[g][sfb - 1];
216         } else {
217           last_alpha_q_re = last_alpha_q_im = 0;
218         }
219       }
220 
221       if (cplx_pred_used[g][sfb] == 1) {
222         dpcm_alpha =
223             -ixheaacd_huff_codeword(ptr_huff_code_word, 0, it_bit_buff) + 60;
224         alpha_q_re[g][sfb] = dpcm_alpha + last_alpha_q_re;
225 
226         if (pstr_core_coder->complex_coef) {
227           dpcm_alpha =
228               -ixheaacd_huff_codeword(ptr_huff_code_word, 0, it_bit_buff) + 60;
229           alpha_q_im[g][sfb] = dpcm_alpha + last_alpha_q_im;
230         } else {
231           alpha_q_im[g][sfb] = 0;
232         }
233       } else {
234         alpha_q_re[g][sfb] = 0;
235         alpha_q_im[g][sfb] = 0;
236       }
237 
238       if ((sfb + 1) < max_sfb_ste) {
239         alpha_q_re[g][sfb + 1] = alpha_q_re[g][sfb];
240         alpha_q_im[g][sfb + 1] = alpha_q_im[g][sfb];
241       }
242 
243       alpha_q_re_prev[sfb] = alpha_q_re[g][sfb];
244       alpha_q_im_prev[sfb] = alpha_q_im[g][sfb];
245     }
246     for (sfb = max_sfb_ste; sfb < SFB_NUM_MAX; sfb++) {
247       alpha_q_re[g][sfb] = 0;
248       alpha_q_im[g][sfb] = 0;
249       alpha_q_re_prev[sfb] = 0;
250       alpha_q_im_prev[sfb] = 0;
251     }
252   }
253 
254   return;
255 }
256 
ixheaacd_read_ms_mask(ia_usac_data_struct * usac_data,ia_usac_tmp_core_coder_struct * pstr_core_coder,ia_bit_buf_struct * it_bit_buff,WORD32 chn)257 static WORD32 ixheaacd_read_ms_mask(
258     ia_usac_data_struct *usac_data,
259     ia_usac_tmp_core_coder_struct *pstr_core_coder,
260     ia_bit_buf_struct *it_bit_buff, WORD32 chn) {
261   WORD32 g, sfb;
262   WORD32 ms_mask_present;
263 
264   UWORD8 *sfb_group = usac_data->group_dis[chn];
265   UWORD8 max_sfb = pstr_core_coder->max_sfb_ste;
266   UWORD8 *ms_used = usac_data->ms_used[chn];
267   ia_sfb_info_struct *info = usac_data->pstr_sfb_info[chn];
268 
269   ms_mask_present = ixheaacd_read_bits_buf(it_bit_buff, 2);
270 
271   switch (ms_mask_present) {
272     case 0:
273 
274       break;
275 
276     case 1:
277       for (g = 0; g < info->max_win_len; g = *sfb_group++) {
278         for (sfb = 0; sfb < max_sfb; sfb++) {
279           *ms_used = ixheaacd_read_bits_buf(it_bit_buff, 1);
280           ms_used++;
281         }
282         for (; sfb < info->sfb_per_sbk; sfb++) {
283           *ms_used = 0;
284           ms_used++;
285         }
286       }
287 
288       break;
289     case 2:
290       for (g = 0; g < info->max_win_len; g = *sfb_group++)
291         for (sfb = 0; sfb < info->sfb_per_sbk; sfb++) *ms_used++ = 1;
292       break;
293 
294     case 3:
295 
296       ixheaacd_cplx_pred_data(usac_data, pstr_core_coder, info->num_groups,
297                               it_bit_buff);
298       return 3;
299   }
300 
301   for (sfb = 0; sfb < SFB_NUM_MAX; sfb++) {
302     usac_data->alpha_q_re_prev[sfb] = 0;
303     usac_data->alpha_q_im_prev[sfb] = 0;
304   }
305   return ms_mask_present;
306 }
307 
ixheaacd_ms_stereo(ia_usac_data_struct * usac_data,WORD32 * r_spec,WORD32 * l_spec,WORD32 chn,WORD32 nband)308 VOID ixheaacd_ms_stereo(ia_usac_data_struct *usac_data, WORD32 *r_spec,
309                         WORD32 *l_spec, WORD32 chn, WORD32 nband) {
310   WORD32 temp_r, temp_l;
311   WORD32 sfb, k, grp, grp_len;
312   ia_sfb_info_struct *ptr_sfb_info = usac_data->pstr_sfb_info[chn];
313   UWORD8 *ms_used = usac_data->ms_used[chn];
314   WORD32 ixheaacd_drc_offset = 0;
315 
316   for (grp = 0; grp < ptr_sfb_info->num_groups; grp++) {
317     for (grp_len = 0; grp_len < ptr_sfb_info->group_len[grp]; grp_len++) {
318       ixheaacd_drc_offset = 0;
319       for (sfb = 0; sfb < nband; sfb++) {
320         ixheaacd_drc_offset += ptr_sfb_info->sfb_width[sfb];
321         if (ms_used[sfb]) {
322           for (k = 0; k < ptr_sfb_info->sfb_width[sfb]; k++) {
323             temp_r = *r_spec;
324             temp_l = *l_spec;
325             *l_spec = ixheaacd_add32_sat(temp_r, temp_l);
326             *r_spec = ixheaacd_sub32_sat(temp_l, temp_r);
327             r_spec++;
328             l_spec++;
329           }
330         } else {
331           r_spec += ptr_sfb_info->sfb_width[sfb];
332           l_spec += ptr_sfb_info->sfb_width[sfb];
333         }
334       }
335 
336       l_spec = l_spec + ptr_sfb_info->bins_per_sbk - ixheaacd_drc_offset;
337       r_spec = r_spec + ptr_sfb_info->bins_per_sbk - ixheaacd_drc_offset;
338     }
339 
340     ms_used += ptr_sfb_info->sfb_per_sbk;
341   }
342 }
343 
ixheaacd_filter_and_add(const WORD32 * in,const WORD32 length,const WORD16 * filter,WORD32 * out,const WORD32 factor_even,const WORD32 factor_odd)344 static VOID ixheaacd_filter_and_add(const WORD32 *in, const WORD32 length,
345                                     const WORD16 *filter, WORD32 *out,
346                                     const WORD32 factor_even,
347                                     const WORD32 factor_odd) {
348   WORD32 i;
349   WORD64 sum;
350 
351   sum = ixheaacd_mult32x32in64(in[2], filter[0]);
352   sum = ixheaacd_mac32x32in64(sum, in[1], filter[1]);
353   sum = ixheaacd_mac32x32in64(sum, in[0], filter[2]);
354   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[3], 4);
355   *out = ixheaacd_add32_sat(
356       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
357   out++;
358 
359   sum = ixheaacd_mult32x32in64(in[1], filter[0]);
360   sum = ixheaacd_mac32x32in64(sum, in[0], filter[1]);
361   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[2], 5);
362   *out = ixheaacd_add32_sat(
363       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
364   out++;
365 
366   sum = ixheaacd_mult32x32in64(in[0], filter[0]);
367   sum = ixheaacd_mac32x32in64_n(sum, &in[0], &filter[1], 6);
368   *out = ixheaacd_add32_sat(
369       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
370   out++;
371 
372   for (i = 3; i < length - 4; i += 2) {
373     sum = ixheaacd_mac32x32in64_7(&in[i - 3], filter);
374     *out = ixheaacd_add32_sat(
375         *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
376     out++;
377 
378     sum = ixheaacd_mac32x32in64_7(&in[i - 2], filter);
379     *out = ixheaacd_add32_sat(
380         *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
381     out++;
382   }
383   i = length - 3;
384   sum = 0;
385   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 6);
386   sum = ixheaacd_mac32x32in64(sum, in[i + 2], filter[6]);
387   *out = ixheaacd_add32_sat(
388       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
389 
390   out++;
391   i = length - 2;
392   sum = 0;
393   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 5);
394   sum = ixheaacd_mac32x32in64(sum, in[i + 1], filter[5]);
395   sum = ixheaacd_mac32x32in64(sum, in[i], filter[6]);
396 
397   *out = ixheaacd_add32_sat(
398       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_even) >> 15)));
399   out++;
400 
401   i = length - 1;
402   sum = 0;
403   sum = ixheaacd_mac32x32in64_n(sum, &in[i - 3], filter, 4);
404   sum = ixheaacd_mac32x32in64(sum, in[i], filter[4]);
405   sum = ixheaacd_mac32x32in64(sum, in[i - 1], filter[5]);
406   sum = ixheaacd_mac32x32in64(sum, in[i - 2], filter[6]);
407 
408   *out = ixheaacd_add32_sat(
409       *out, ixheaacd_sat64_32((((WORD64)sum * (WORD64)factor_odd) >> 15)));
410 }
411 
ixheaacd_estimate_dmx_im(const WORD32 * dmx_re,const WORD32 * dmx_re_prev,WORD32 * dmx_im,ia_sfb_info_struct * pstr_sfb_info,WORD32 window,const WORD32 w_shape,const WORD32 prev_w_shape)412 static VOID ixheaacd_estimate_dmx_im(const WORD32 *dmx_re,
413                                      const WORD32 *dmx_re_prev, WORD32 *dmx_im,
414                                      ia_sfb_info_struct *pstr_sfb_info,
415                                      WORD32 window, const WORD32 w_shape,
416                                      const WORD32 prev_w_shape) {
417   WORD32 i;
418   const WORD16 *mdst_fcoeff_curr, *mdst_fcoeff_prev;
419 
420   switch (window) {
421     case ONLY_LONG_SEQUENCE:
422     case EIGHT_SHORT_SEQUENCE:
423       mdst_fcoeff_curr =
424           ixheaacd_mdst_fcoeff_longshort_curr[prev_w_shape][w_shape];
425       mdst_fcoeff_prev = ixheaacd_mdst_fcoeff_l_s_start_left_prev[prev_w_shape];
426       break;
427     case LONG_START_SEQUENCE:
428       mdst_fcoeff_curr = ixheaacd_mdst_fcoeff_start_curr[prev_w_shape][w_shape];
429       mdst_fcoeff_prev = ixheaacd_mdst_fcoeff_l_s_start_left_prev[prev_w_shape];
430       break;
431     case LONG_STOP_SEQUENCE:
432       mdst_fcoeff_curr = ixheaacd_mdst_fcoeff_stop_cur[prev_w_shape][w_shape];
433       mdst_fcoeff_prev =
434           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
435       break;
436     case STOP_START_SEQUENCE:
437       mdst_fcoeff_curr =
438           ixheaacd_mdst_fcoeff_stopstart_cur[prev_w_shape][w_shape];
439       mdst_fcoeff_prev =
440           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
441       break;
442     default:
443       mdst_fcoeff_curr =
444           ixheaacd_mdst_fcoeff_stopstart_cur[prev_w_shape][w_shape];
445       mdst_fcoeff_prev =
446           ixheaacd_mdst_fcoeff_stop_stopstart_left_prev[prev_w_shape];
447       break;
448   }
449 
450   for (i = 0; i < pstr_sfb_info->max_win_len; i++) {
451     ixheaacd_filter_and_add(dmx_re, pstr_sfb_info->bins_per_sbk,
452                             mdst_fcoeff_curr, dmx_im, 1, 1);
453 
454     if (dmx_re_prev)
455       ixheaacd_filter_and_add(dmx_re_prev, pstr_sfb_info->bins_per_sbk,
456                               mdst_fcoeff_prev, dmx_im, -1, 1);
457 
458     dmx_re_prev = dmx_re;
459     dmx_re += pstr_sfb_info->bins_per_sbk;
460     dmx_im += pstr_sfb_info->bins_per_sbk;
461   }
462   return;
463 }
464 
ixheaacd_cplx_pred_upmixing(ia_usac_data_struct * usac_data,WORD32 * l_spec,WORD32 * r_spec,ia_usac_tmp_core_coder_struct * pstr_core_coder,WORD32 chn)465 static VOID ixheaacd_cplx_pred_upmixing(
466     ia_usac_data_struct *usac_data, WORD32 *l_spec, WORD32 *r_spec,
467     ia_usac_tmp_core_coder_struct *pstr_core_coder, WORD32 chn) {
468   ia_sfb_info_struct *pstr_sfb_info = usac_data->pstr_sfb_info[chn];
469   WORD32 *dmx_re = &usac_data->scratch_buffer[0];
470   WORD32 *dmx_im = &usac_data->x_ac_dec[0];
471 
472   WORD32 grp, sfb, grp_len, i = 0, k;
473   WORD32 *dmx_re_prev = usac_data->dmx_re_prev;
474   WORD32(*alpha_q_re)[SFB_NUM_MAX] = usac_data->alpha_q_re;
475   WORD32(*alpha_q_im)[SFB_NUM_MAX] = usac_data->alpha_q_im;
476 
477   UWORD8(*cplx_pred_used)[SFB_NUM_MAX] = usac_data->cplx_pred_used;
478 
479   WORD32 alpha_q_re_temp;
480   WORD32 alpha_q_im_temp;
481   WORD32 factor = 1;
482 
483   if (pstr_core_coder->pred_dir) factor = -1;
484 
485   for (grp = 0; grp < pstr_sfb_info->num_groups; grp++) {
486     for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
487       for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
488         if (cplx_pred_used[grp][sfb] == 1) {
489           memcpy(&dmx_re[i], &l_spec[i],
490                  pstr_sfb_info->sfb_width[sfb] * sizeof(WORD32));
491           i += pstr_sfb_info->sfb_width[sfb];
492         }
493 
494         else {
495           for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
496             dmx_re[i] = (WORD32)(
497                 ((WORD64)l_spec[i] + ((WORD64)factor * (WORD64)r_spec[i])) >>
498                 1);
499           }
500         }
501       }
502     }
503   }
504 
505   memset(dmx_im, 0, sizeof(WORD32) * BLOCK_LEN_LONG);
506 
507   if (pstr_core_coder->complex_coef) {
508     WORD32 *p_dmx_re_prev =
509         pstr_core_coder->use_prev_frame ? dmx_re_prev : NULL;
510     ixheaacd_estimate_dmx_im(dmx_re, p_dmx_re_prev, dmx_im, pstr_sfb_info,
511                              usac_data->window_sequence[chn],
512                              usac_data->window_shape[chn],
513                              usac_data->window_shape_prev[chn]);
514 
515     for (grp = 0, i = 0; grp < pstr_sfb_info->num_groups; grp++) {
516       for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
517         for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
518           alpha_q_re_temp = alpha_q_re[grp][sfb] * 1677722;
519           alpha_q_im_temp = alpha_q_im[grp][sfb] * 1677722;
520           if (cplx_pred_used[grp][sfb]) {
521             for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
522               WORD32 mid_side = ixheaacd_sub32_sat(
523                   ixheaacd_sub32_sat(r_spec[i],
524                                      (WORD32)((WORD64)ixheaacd_mult32x32in64(
525                                                   alpha_q_re_temp, l_spec[i]) >>
526                                               24)),
527                   (WORD32)((WORD64)ixheaacd_mult32x32in64(alpha_q_im_temp,
528                                                           dmx_im[i]) >>
529                            24));
530               r_spec[i] = ixheaacd_sat64_32((WORD64)factor) *
531                           (WORD64)(ixheaacd_sub32_sat(l_spec[i], mid_side));
532               l_spec[i] = ixheaacd_add32_sat(l_spec[i], mid_side);
533             }
534 
535           } else {
536             i += pstr_sfb_info->sfb_width[sfb];
537           }
538         }
539       }
540     }
541   } else {
542     for (grp = 0, i = 0; grp < pstr_sfb_info->num_groups; grp++) {
543       for (grp_len = 0; grp_len < pstr_sfb_info->group_len[grp]; grp_len++) {
544         for (sfb = 0; sfb < pstr_sfb_info->sfb_per_sbk; sfb++) {
545           alpha_q_re_temp = alpha_q_re[grp][sfb] * 1677722;
546           if (cplx_pred_used[grp][sfb]) {
547             for (k = 0; k < pstr_sfb_info->sfb_width[sfb]; k++, i++) {
548               WORD32 mid_side = ixheaacd_sub32_sat(
549                   r_spec[i], (WORD32)((WORD64)ixheaacd_mult32x32in64(
550                                           alpha_q_re_temp, l_spec[i]) >>
551                                       24));
552 
553               r_spec[i] = ixheaacd_sat64_32((WORD64)factor) *
554                           (WORD64)(ixheaacd_sub32_sat(l_spec[i], mid_side));
555               l_spec[i] = ixheaacd_add32_sat(l_spec[i], mid_side);
556             }
557 
558           } else {
559             i += pstr_sfb_info->sfb_width[sfb];
560           }
561         }
562       }
563     }
564   }
565 
566   return;
567 }
568 
ixheaacd_cplx_prev_mdct_dmx(ia_sfb_info_struct * pstr_sfb_info,WORD32 * l_spec,WORD32 * r_spec,WORD32 * dmx_re_prev,WORD32 pred_dir)569 static VOID ixheaacd_cplx_prev_mdct_dmx(ia_sfb_info_struct *pstr_sfb_info,
570                                         WORD32 *l_spec, WORD32 *r_spec,
571                                         WORD32 *dmx_re_prev, WORD32 pred_dir) {
572   WORD32 offs, i;
573   WORD32 factor = 1;
574   if (pred_dir) factor = -1;
575 
576   offs = pstr_sfb_info->samp_per_bk - pstr_sfb_info->bins_per_sbk;
577 
578   for (i = 0; i < pstr_sfb_info->bins_per_sbk; i++)
579     dmx_re_prev[i] = (WORD32)(((WORD64)l_spec[i + offs] +
580                                ((WORD64)factor * (WORD64)r_spec[i + offs])) >>
581                               1);
582 }
583 
ixheaacd_ics_info(ia_usac_data_struct * usac_data,WORD32 chn,UWORD8 * max_sfb,ia_bit_buf_struct * it_bit_buff,WORD32 window_sequence_last)584 WORD32 ixheaacd_ics_info(ia_usac_data_struct *usac_data, WORD32 chn,
585                          UWORD8 *max_sfb, ia_bit_buf_struct *it_bit_buff,
586                          WORD32 window_sequence_last
587 
588                          )
589 
590 {
591   WORD32 win;
592   WORD32 mask = 0x40;
593 
594   UWORD8 *scf_group_ptr = usac_data->group_dis[chn];
595 
596   win = ixheaacd_read_bits_buf(it_bit_buff, 2);
597 
598   win = usac_data->window_sequence[chn] =
599       ixheaacd_win_seq_select(win, window_sequence_last);
600   if (win == -1) return -1;
601 
602   usac_data->pstr_sfb_info[chn] =
603       usac_data->pstr_usac_winmap[usac_data->window_sequence[chn]];
604 
605   usac_data->window_shape[chn] = (WORD32)ixheaacd_read_bits_buf(it_bit_buff, 1);
606 
607   if (usac_data->pstr_usac_winmap[win]->islong) {
608     *max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 6);
609     *scf_group_ptr = 1;
610 
611   } else {
612     WORD32 i, scale_factor_grouping;
613 
614     *max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 4);
615 
616     scale_factor_grouping = ixheaacd_read_bits_buf(it_bit_buff, 7);
617 
618     for (i = 1; i < 8; i++) {
619       if (!(scale_factor_grouping & mask)) *scf_group_ptr++ = i;
620 
621       mask = mask >> 1;
622     }
623     *scf_group_ptr++ = i;
624 
625     ixheaacd_calc_grp_offset(usac_data->pstr_usac_winmap[win],
626                              &usac_data->group_dis[chn][0]);
627   }
628 
629   if (*max_sfb > usac_data->pstr_sfb_info[chn]->sfb_per_sbk) {
630     *max_sfb = usac_data->pstr_sfb_info[chn]->sfb_per_sbk;
631     return 0;
632   }
633 
634   return 0;
635 }
636 
ixheaacd_core_coder_data(WORD32 id,ia_usac_data_struct * usac_data,WORD32 elem_idx,WORD32 chan_offset,ia_bit_buf_struct * it_bit_buff,WORD32 nr_core_coder_channels)637 WORD32 ixheaacd_core_coder_data(WORD32 id, ia_usac_data_struct *usac_data,
638                                 WORD32 elem_idx, WORD32 chan_offset,
639                                 ia_bit_buf_struct *it_bit_buff,
640                                 WORD32 nr_core_coder_channels) {
641   WORD32 err_code = 0;
642   WORD32 k = 0, ch = 0, chn, left = 0, right = 0;
643 
644   ia_usac_tmp_core_coder_struct str_tmp_core_coder = {0};
645   ia_usac_tmp_core_coder_struct *pstr_core_coder = &str_tmp_core_coder;
646   ia_td_frame_data_struct td_frame;
647 
648   memset(&td_frame, 0, sizeof(td_frame));
649   pstr_core_coder->tns_on_lr = 0;
650   pstr_core_coder->pred_dir = 0;
651   if (id != ID_USAC_LFE) {
652     for (ch = 0; ch < nr_core_coder_channels; ch++)
653       pstr_core_coder->core_mode[ch] = ixheaacd_read_bits_buf(it_bit_buff, 1);
654   } else {
655     for (ch = 0; ch < nr_core_coder_channels; ch++)
656       pstr_core_coder->core_mode[ch] = 0;
657   }
658 
659   if (nr_core_coder_channels == 2 && pstr_core_coder->core_mode[0] == 0 &&
660       pstr_core_coder->core_mode[1] == 0) {
661     pstr_core_coder->tns_active = ixheaacd_read_bits_buf(it_bit_buff, 1);
662     pstr_core_coder->common_window = ixheaacd_read_bits_buf(it_bit_buff, 1);
663 
664     if (pstr_core_coder->common_window) {
665       left = chan_offset;
666       right = chan_offset + 1;
667 
668       err_code =
669           ixheaacd_ics_info(usac_data, left, &pstr_core_coder->max_sfb[left],
670                             it_bit_buff, usac_data->window_sequence_last[left]);
671 
672       if (err_code == -1) return err_code;
673 
674       pstr_core_coder->common_max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 1);
675 
676       if (pstr_core_coder->common_max_sfb == 0) {
677         if (usac_data->window_sequence[left] == EIGHT_SHORT_SEQUENCE)
678           pstr_core_coder->max_sfb[right] =
679               ixheaacd_read_bits_buf(it_bit_buff, 4);
680         else
681           pstr_core_coder->max_sfb[right] =
682               ixheaacd_read_bits_buf(it_bit_buff, 6);
683       } else {
684         pstr_core_coder->max_sfb[right] = pstr_core_coder->max_sfb[left];
685       }
686 
687       pstr_core_coder->max_sfb_ste =
688           max(pstr_core_coder->max_sfb[left], pstr_core_coder->max_sfb[right]);
689 
690       usac_data->window_sequence[right] = usac_data->window_sequence[left];
691       usac_data->window_shape[right] = usac_data->window_shape[left];
692       memcpy(&usac_data->group_dis[right][0], &usac_data->group_dis[left][0],
693              8);
694       usac_data->pstr_sfb_info[right] = usac_data->pstr_sfb_info[left];
695       if (pstr_core_coder->max_sfb[right] >
696           usac_data->pstr_sfb_info[right]->sfb_per_sbk)
697         pstr_core_coder->max_sfb[right] =
698             usac_data->pstr_sfb_info[right]->sfb_per_sbk;
699 
700       pstr_core_coder->ms_mask_present[0] =
701           ixheaacd_read_ms_mask(usac_data, pstr_core_coder, it_bit_buff, left);
702     } else {
703       left = chan_offset;
704       right = chan_offset + 1;
705 
706       pstr_core_coder->ms_mask_present[0] = 0;
707       pstr_core_coder->ms_mask_present[1] = 0;
708 
709       for (k = 0; k < SFB_NUM_MAX; k++) {
710         usac_data->alpha_q_re_prev[k] = 0;
711         usac_data->alpha_q_im_prev[k] = 0;
712       }
713     }
714 
715     if (usac_data->tw_mdct[elem_idx] == 1) {
716       pstr_core_coder->common_tw = ixheaacd_read_bits_buf(it_bit_buff, 1);
717 
718       if (pstr_core_coder->common_tw == 1) {
719         usac_data->tw_data_present[left] =
720             ixheaacd_read_bits_buf(it_bit_buff, 1);
721         usac_data->tw_data_present[right] = usac_data->tw_data_present[left];
722         if (usac_data->tw_data_present[left]) {
723           for (k = 0; k < NUM_TW_NODES; k++) {
724             usac_data->tw_ratio[left][k] =
725                 ixheaacd_read_bits_buf(it_bit_buff, 3);
726             usac_data->tw_ratio[right][k] = usac_data->tw_ratio[left][k];
727           }
728         }
729       }
730     }
731 
732     if (pstr_core_coder->tns_active) {
733       if (pstr_core_coder->common_window) {
734         pstr_core_coder->common_tns = ixheaacd_read_bits_buf(it_bit_buff, 1);
735 
736       } else {
737         pstr_core_coder->common_tns = 0;
738       }
739 
740       pstr_core_coder->tns_on_lr = ixheaacd_read_bits_buf(it_bit_buff, 1);
741 
742       if (pstr_core_coder->common_tns) {
743         ixheaacd_read_tns_u(usac_data->pstr_sfb_info[0],
744                             &usac_data->pstr_tns[left][0], it_bit_buff);
745         memcpy(&usac_data->pstr_tns[right][0], &usac_data->pstr_tns[left][0],
746                sizeof(ia_tns_frame_info_struct));
747 
748         pstr_core_coder->tns_data_present[0] = 2;
749         pstr_core_coder->tns_data_present[1] = 2;
750       } else {
751         pstr_core_coder->tns_present_both =
752             ixheaacd_read_bits_buf(it_bit_buff, 1);
753 
754         if (pstr_core_coder->tns_present_both) {
755           pstr_core_coder->tns_data_present[0] = 1;
756           pstr_core_coder->tns_data_present[1] = 1;
757         } else {
758           pstr_core_coder->tns_data_present[1] =
759               ixheaacd_read_bits_buf(it_bit_buff, 1);
760           pstr_core_coder->tns_data_present[0] =
761               1 - pstr_core_coder->tns_data_present[1];
762         }
763       }
764     } else {
765       pstr_core_coder->common_tns = 0;
766       pstr_core_coder->tns_data_present[0] = 0;
767       pstr_core_coder->tns_data_present[1] = 0;
768     }
769 
770   } else {
771     pstr_core_coder->common_window = 0;
772     pstr_core_coder->common_tw = 0;
773     left = chan_offset;
774     right = chan_offset;
775     if (nr_core_coder_channels == 2) right = chan_offset + 1;
776   }
777 
778   for (ch = 0, chn = chan_offset; ch < nr_core_coder_channels; ch++, chn++) {
779     if (pstr_core_coder->core_mode[ch] == 1) {
780       err_code =
781           ixheaacd_tw_buff_update(usac_data, chn, usac_data->str_tddec[chn]);
782       if (err_code == -1) return err_code;
783 
784       if (!usac_data->td_frame_prev[chn]) {
785         ixheaacd_fix2flt_data(usac_data, usac_data->str_tddec[chn], chn);
786       }
787 
788       for (k = 0; k < usac_data->ccfl; k++) {
789         usac_data->time_sample_vector[chn][k] =
790             (FLOAT32)((FLOAT32)usac_data->output_data_ptr[chn][k] *
791                       (FLOAT32)(ONE_BY_TWO_POW_15));
792       }
793       usac_data->present_chan = chn;
794       err_code =
795           ixheaacd_lpd_channel_stream(usac_data, &td_frame, it_bit_buff,
796                                       usac_data->time_sample_vector[chn]);
797       if (err_code == -1) return err_code;
798 
799       for (k = 0; k < usac_data->ccfl; k++) {
800         usac_data->output_data_ptr[chn][k] = (WORD32)(
801             usac_data->time_sample_vector[chn][k] * (FLOAT32)((WORD64)1 << 15));
802       }
803 
804       usac_data->window_shape[chn] = WIN_SEL_0;
805 
806       ixheaacd_td_frm_dec(usac_data, chn, td_frame.mod[0]);
807 
808       for (k = 0; k < usac_data->ccfl; k++) {
809         usac_data->time_sample_vector[chn][k] =
810             (FLOAT32)((FLOAT32)usac_data->output_data_ptr[chn][k] *
811                       (FLOAT32)(ONE_BY_TWO_POW_15));
812       }
813 
814       usac_data->window_shape_prev[chn] = usac_data->window_shape[chn];
815       usac_data->window_sequence_last[chn] = EIGHT_SHORT_SEQUENCE;
816 
817     } else {
818       memset(usac_data->coef_fix[chn], 0,
819              LN2 * sizeof(*usac_data->coef_fix[0]));
820 
821       if (usac_data->str_tddec[chn] && usac_data->td_frame_prev[chn]) {
822         ixheaacd_lpd_dec_update(usac_data->str_tddec[chn], usac_data, chn);
823       }
824 
825       if (id != ID_USAC_LFE) {
826         if ((nr_core_coder_channels == 1) ||
827             (pstr_core_coder->core_mode[0] != pstr_core_coder->core_mode[1]))
828           pstr_core_coder->tns_data_present[ch] =
829               ixheaacd_read_bits_buf(it_bit_buff, 1);
830       }
831 
832       err_code = ixheaacd_fd_channel_stream(
833           usac_data, pstr_core_coder, &pstr_core_coder->max_sfb[ch],
834           usac_data->window_sequence_last[chn], chn,
835           usac_data->noise_filling_config[elem_idx], ch, it_bit_buff);
836       if (err_code == -1) return err_code;
837     }
838   }
839 
840   if (pstr_core_coder->core_mode[0] == CORE_MODE_FD &&
841       pstr_core_coder->core_mode[1] == CORE_MODE_FD &&
842       nr_core_coder_channels == 2) {
843     ixheaacd_cplx_prev_mdct_dmx(
844         usac_data->pstr_sfb_info[left], usac_data->coef_save[left],
845         usac_data->coef_save[right], usac_data->dmx_re_prev,
846         pstr_core_coder->pred_dir);
847   }
848 
849   if (pstr_core_coder->tns_on_lr == 0 && (id != ID_USAC_LFE)) {
850     for (ch = 0, chn = left; chn <= right; ch++, chn++) {
851       if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
852         err_code = ixheaacd_tns_apply(
853             usac_data, usac_data->coef_fix[chn], pstr_core_coder->max_sfb[ch],
854             usac_data->pstr_sfb_info[chn], usac_data->pstr_tns[chn]);
855         if (err_code) return err_code;
856       }
857     }
858   }
859 
860   if (nr_core_coder_channels == 2 && pstr_core_coder->core_mode[0] == 0 &&
861       pstr_core_coder->core_mode[1] == 0) {
862     if (pstr_core_coder->ms_mask_present[0] == 3) {
863       ixheaacd_cplx_pred_upmixing(usac_data, usac_data->coef_fix[left],
864                                   usac_data->coef_fix[right], pstr_core_coder,
865                                   left);
866 
867     } else if (pstr_core_coder->ms_mask_present[0] > 0) {
868       ixheaacd_ms_stereo(
869           usac_data, usac_data->coef_fix[right], usac_data->coef_fix[left],
870           left, pstr_core_coder->max_sfb[right] > pstr_core_coder->max_sfb[left]
871                     ? pstr_core_coder->max_sfb[right]
872                     : pstr_core_coder->max_sfb[left]);
873     }
874 
875     if (pstr_core_coder->tns_on_lr) {
876       for (ch = 0, chn = left; chn <= right; ch++, chn++) {
877         if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
878           err_code = ixheaacd_tns_apply(
879               usac_data, usac_data->coef_fix[chn], pstr_core_coder->max_sfb[ch],
880               usac_data->pstr_sfb_info[chn], usac_data->pstr_tns[chn]);
881           if (err_code) return err_code;
882         }
883       }
884     }
885 
886     ixheaacd_usac_cplx_save_prev(
887         usac_data->pstr_sfb_info[left], usac_data->coef_fix[left],
888         usac_data->coef_fix[right], usac_data->coef_save[left],
889         usac_data->coef_save[right]);
890   }
891 
892   for (ch = left; ch <= right; ch++) {
893     if (pstr_core_coder->core_mode[ch] == CORE_MODE_FD) {
894       if (usac_data->tw_mdct[elem_idx]) {
895         err_code = -1;
896         return err_code;
897 
898       } else {
899         err_code = ixheaacd_fd_frm_dec(usac_data, ch);
900         if (err_code == -1) return err_code;
901 
902         for (k = 0; k < usac_data->ccfl; k++) {
903           usac_data->time_sample_vector[ch][k] =
904               (FLOAT32)((FLOAT32)usac_data->output_data_ptr[ch][k] *
905                         (FLOAT32)(ONE_BY_TWO_POW_15));
906         }
907       }
908 
909       usac_data->window_shape_prev[ch] = usac_data->window_shape[ch];
910       usac_data->window_sequence_last[ch] = usac_data->window_sequence[ch];
911     }
912   }
913 
914   for (ch = 0, chn = left; chn <= right; chn++, ch++)
915     usac_data->td_frame_prev[chn] = pstr_core_coder->core_mode[ch];
916 
917   return 0;
918 }
919