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