• 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 <stdio.h>
22 #include <stdlib.h>
23 #include "ixheaacd_sbr_common.h"
24 #include "ixheaacd_type_def.h"
25 #include "ixheaacd_constants.h"
26 #include "ixheaacd_basic_ops32.h"
27 #include "ixheaacd_basic_ops16.h"
28 #include "ixheaacd_basic_ops40.h"
29 #include "ixheaacd_basic_ops.h"
30 
31 #include "ixheaacd_bitbuffer.h"
32 
33 #include "ixheaacd_error_codes.h"
34 #include "ixheaacd_defines.h"
35 #include "ixheaacd_aac_rom.h"
36 #include "ixheaacd_common_rom.h"
37 #include "ixheaacd_basic_funcs.h"
38 #include "ixheaacd_aac_imdct.h"
39 #include "ixheaacd_basic_op.h"
40 #include "ixheaacd_intrinsics.h"
41 
42 #include "ixheaacd_pulsedata.h"
43 
44 #include "ixheaacd_pns.h"
45 #include "ixheaacd_drc_data_struct.h"
46 
47 #include "ixheaacd_lt_predict.h"
48 #include "ixheaacd_cnst.h"
49 #include "ixheaacd_ec_defines.h"
50 #include "ixheaacd_ec_struct_def.h"
51 #include "ixheaacd_channelinfo.h"
52 #include "ixheaacd_drc_dec.h"
53 #include "ixheaacd_sbrdecoder.h"
54 #include "ixheaacd_block.h"
55 
56 #include "ixheaacd_channel.h"
57 
58 #include "ixheaacd_audioobjtypes.h"
59 #include "ixheaacd_latmdemux.h"
60 #include "ixheaacd_aacdec.h"
61 #include "ixheaacd_tns.h"
62 #include "ixheaacd_function_selector.h"
63 
ixheaacd_is_correlation(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,WORD16 pns_band)64 static PLATFORM_INLINE WORD16 ixheaacd_is_correlation(
65     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD16 pns_band) {
66   ia_pns_correlation_info_struct *ptr_corr_info =
67       ptr_aac_dec_channel_info->pstr_pns_corr_info;
68 
69   return ((ptr_corr_info->correlated[(pns_band >> PNS_BAND_FLAGS_SHIFT)] >>
70            (pns_band & PNS_BAND_FLAGS_MASK)) &
71           1);
72 }
73 
ixheaacd_gen_rand_vec(WORD32 scale,WORD shift,WORD32 * ptr_spec_coef,WORD32 sfb_width,WORD32 * seed)74 VOID ixheaacd_gen_rand_vec(WORD32 scale, WORD shift, WORD32 *ptr_spec_coef,
75                            WORD32 sfb_width, WORD32 *seed) {
76   WORD nrg_scale;
77   WORD32 nrg = 0;
78   WORD32 *spec = ptr_spec_coef;
79   WORD32 sfb;
80 
81   for (sfb = 0; sfb <= sfb_width; sfb++) {
82     *seed = (WORD32)(((WORD64)1664525 * (WORD64)(*seed)) + (WORD64)1013904223);
83 
84     *spec = (*seed >> 3);
85 
86     nrg = ixheaacd_add32_sat(nrg, ixheaacd_mult32_shl_sat(*spec, *spec));
87 
88     spec++;
89   }
90 
91   nrg_scale = ixheaacd_norm32(nrg);
92 
93   if (nrg_scale > 0) {
94     nrg_scale &= ~1;
95     nrg = ixheaacd_shl32_sat(nrg, nrg_scale);
96     shift = shift - (nrg_scale >> 1);
97   }
98 
99   nrg = ixheaacd_sqrt(nrg);
100   scale = ixheaacd_div32_pos_normb(scale, nrg);
101 
102   spec = ptr_spec_coef;
103 
104   for (sfb = 0; sfb <= sfb_width; sfb++) {
105     *spec = ixheaacd_shr32_dir_sat_limit(ixheaacd_mult32_shl_sat(*spec, scale),
106                                          shift);
107     spec++;
108   }
109 }
110 
ixheaacd_pns_process(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info[],WORD32 channel,ia_aac_dec_tables_struct * ptr_aac_tables)111 VOID ixheaacd_pns_process(
112     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[], WORD32 channel,
113     ia_aac_dec_tables_struct *ptr_aac_tables) {
114   ia_pns_info_struct *ptr_pns_info =
115       &ptr_aac_dec_channel_info[channel]->str_pns_info;
116   ia_ics_info_struct *ptr_ics_info =
117       &ptr_aac_dec_channel_info[channel]->str_ics_info;
118   WORD16 maximum_bins_short = ptr_ics_info->frame_length >> 3;
119   WORD32 *ptr_scale_mant_tab =
120       ptr_aac_tables->pstr_block_tables->scale_mant_tab;
121 
122   if (ptr_pns_info->pns_active) {
123     const WORD16 *swb_offset =
124         ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
125             .sfb_index;
126 
127     WORD num_win_group, grp_len, sfb;
128     WORD32 *spec = &ptr_aac_dec_channel_info[channel]->ptr_spec_coeff[0];
129 
130     for (num_win_group = 0; num_win_group < ptr_ics_info->num_window_groups;
131          num_win_group++) {
132       grp_len = ptr_ics_info->window_group_length[num_win_group];
133 
134       for (grp_len = 0;
135            grp_len < ptr_ics_info->window_group_length[num_win_group];
136            grp_len++) {
137         for (sfb = 0; sfb < ptr_ics_info->max_sfb; sfb++) {
138           WORD16 pns_band = ((num_win_group << 4) + sfb);
139 
140           if (ptr_aac_dec_channel_info[channel]
141                   ->str_pns_info.pns_used[pns_band]) {
142             WORD32 scale_mant;
143             WORD32 scale_exp;
144             WORD32 sfb_width = swb_offset[sfb + 1] - swb_offset[sfb] - 1;
145             WORD32 *ptr_spec = &spec[swb_offset[sfb]];
146 
147             scale_mant = ptr_scale_mant_tab[ptr_aac_dec_channel_info[channel]
148                                                 ->ptr_scale_factor[pns_band] &
149                                             PNS_SCALE_MANT_TAB_MASK];
150             scale_exp = add_d(sub_d(31, (ptr_aac_dec_channel_info[channel]
151                                              ->ptr_scale_factor[pns_band] >>
152                                          PNS_SCALEFACTOR_SCALING)),
153                               PNS_SCALE_MANT_TAB_SCALING);
154 
155             if (ixheaacd_is_correlation(ptr_aac_dec_channel_info[LEFT],
156                                         pns_band)) {
157               if (channel == 0) {
158                 ptr_aac_dec_channel_info[LEFT]
159                     ->pstr_pns_corr_info->random_vector[pns_band] =
160                     ptr_aac_dec_channel_info[LEFT]
161                         ->pstr_pns_rand_vec_data->current_seed;
162 
163                 ixheaacd_gen_rand_vec(
164                     scale_mant, scale_exp, ptr_spec, sfb_width,
165                     &(ptr_aac_dec_channel_info[LEFT]
166                           ->pstr_pns_rand_vec_data->current_seed));
167               }
168 
169               else {
170                 ixheaacd_gen_rand_vec(
171                     scale_mant, scale_exp, ptr_spec, sfb_width,
172                     &(ptr_aac_dec_channel_info[LEFT]
173                           ->pstr_pns_corr_info->random_vector[pns_band]));
174               }
175 
176             }
177 
178             else {
179               ixheaacd_gen_rand_vec(
180                   scale_mant, scale_exp, ptr_spec, sfb_width,
181                   &(ptr_aac_dec_channel_info[LEFT]
182                         ->pstr_pns_rand_vec_data->current_seed));
183             }
184           }
185         }
186 
187         if (maximum_bins_short == 120)
188           spec += maximum_bins_short;
189         else
190           spec += 128;
191       }
192     }
193   }
194 
195   if (channel == 0) {
196     ptr_aac_dec_channel_info[0]->pstr_pns_rand_vec_data->pns_frame_number++;
197   }
198 }
199 
ixheaacd_tns_decode_coef(const ia_filter_info_struct * filter,WORD16 * parcor_coef,ia_aac_dec_tables_struct * ptr_aac_tables)200 VOID ixheaacd_tns_decode_coef(const ia_filter_info_struct *filter,
201                               WORD16 *parcor_coef,
202                               ia_aac_dec_tables_struct *ptr_aac_tables) {
203   WORD order, resolution;
204   WORD16 *ptr_par_coef = parcor_coef;
205   WORD16 *tns_coeff_ptr;
206   WORD8 ixheaacd_drc_offset = 4;
207   WORD8 *ptr_coef = (WORD8 *)filter->coef;
208 
209   resolution = filter->resolution;
210   tns_coeff_ptr = ptr_aac_tables->pstr_block_tables->tns_coeff3_16;
211 
212   if (resolution) {
213     tns_coeff_ptr = ptr_aac_tables->pstr_block_tables->tns_coeff4_16;
214     ixheaacd_drc_offset = ixheaacd_drc_offset << 1;
215   }
216 
217   for (order = 0; order < filter->order; order++) {
218     WORD8 temp = *ptr_coef++;
219     *ptr_par_coef++ = tns_coeff_ptr[temp + ixheaacd_drc_offset];
220   }
221 }
222 
ixheaacd_tns_decode_coef_ld(const ia_filter_info_struct * filter,WORD32 * parcor_coef,ia_aac_dec_tables_struct * ptr_aac_tables)223 VOID ixheaacd_tns_decode_coef_ld(const ia_filter_info_struct *filter,
224                                  WORD32 *parcor_coef,
225                                  ia_aac_dec_tables_struct *ptr_aac_tables) {
226   WORD order, resolution;
227   WORD32 *ptr_par_coef = parcor_coef;
228   WORD32 *tns_coeff_ptr;
229   WORD8 offset = 4;
230   WORD8 *ptr_coef = (WORD8 *)filter->coef;
231 
232   resolution = filter->resolution;
233   tns_coeff_ptr = ptr_aac_tables->pstr_block_tables->tns_coeff3;
234 
235   if (resolution) {
236     tns_coeff_ptr = ptr_aac_tables->pstr_block_tables->tns_coeff4;
237     offset = offset << 1;
238   }
239 
240   for (order = 0; order < filter->order; order++) {
241     WORD8 temp = *ptr_coef++;
242     *ptr_par_coef++ = tns_coeff_ptr[temp + offset];
243   }
244 }
245 
ixheaacd_aac_tns_process(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,WORD32 num_ch,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 object_type,WORD32 ar_flag,WORD32 * predicted_spectrum)246 VOID ixheaacd_aac_tns_process(
247     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD32 num_ch,
248     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 object_type,
249     WORD32 ar_flag, WORD32 *predicted_spectrum) {
250   WORD i;
251   WORD16 scale_lpc;
252 
253   ia_tns_info_aac_struct *ptr_tns_info =
254       &ptr_aac_dec_channel_info->str_tns_info;
255   WORD32 *spec = ptr_aac_dec_channel_info->ptr_spec_coeff;
256   WORD32 *scratch_buf = ptr_aac_dec_channel_info->scratch_buf_ptr;
257 
258   WORD win, filt, start, stop, size, scale_spec;
259   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
260   WORD num_window, tns_max_bands, win_seq;
261   WORD16 maximum_bins_short = ptr_ics_info->frame_length >> 3;
262   WORD position;
263 
264   WORD32 parcor_coef[MAX_ORDER + 1];
265   WORD16 parcor_coef_16[MAX_ORDER + 1];
266 
267   WORD32 lpc_coef[MAX_ORDER + 1];
268   WORD16 lpc_coef_16[MAX_ORDER + 1];
269 
270   const WORD16 *ptr_sfb_table;
271 
272   WORD16 max_bin_long = ptr_ics_info->frame_length;
273 
274   win_seq = ptr_ics_info->window_sequence == 0
275                 ? 0
276                 : (ptr_ics_info->window_sequence % 2 == 0);
277 
278   if (ar_flag)
279     spec = ptr_aac_dec_channel_info->ptr_spec_coeff;
280   else {
281     spec = predicted_spectrum;
282   }
283 
284   if (object_type == AOT_ER_AAC_ELD || object_type == AOT_ER_AAC_LD ||
285       object_type == AOT_AAC_LTP) {
286     if (512 == ptr_ics_info->frame_length) {
287       tns_max_bands =
288           ptr_aac_tables->pstr_block_tables
289               ->tns_max_bands_tbl_ld[ptr_ics_info->sampling_rate_index];
290       win_seq = 1;
291       num_window = win_seq;
292     } else if (480 == ptr_ics_info->frame_length) {
293       tns_max_bands =
294           ptr_aac_tables->pstr_block_tables
295               ->tns_max_bands_tbl_480[ptr_ics_info->sampling_rate_index];
296       win_seq = 1;
297       num_window = win_seq;
298     } else {
299       tns_max_bands =
300           ptr_aac_tables->pstr_block_tables
301               ->tns_max_bands_tbl[ptr_ics_info->sampling_rate_index][win_seq];
302 
303       num_window = win_seq ? 8 : 1;
304     }
305   } else {
306     tns_max_bands =
307         ptr_aac_tables->pstr_block_tables
308             ->tns_max_bands_tbl[ptr_ics_info->sampling_rate_index][win_seq];
309 
310     num_window = win_seq ? 8 : 1;
311   }
312 
313   ptr_sfb_table =
314       ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence].sfb_index;
315 
316   for (win = 0; win < num_window; win++) {
317     WORD n_filt = ptr_tns_info->n_filt[win];
318 
319     for (filt = 0; filt < n_filt; filt++) {
320       ia_filter_info_struct *filter = &ptr_tns_info->str_filter[win][filt];
321 
322       if (filter->order <= 0) {
323         continue;
324       }
325 
326       if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP) ||
327           (num_ch > 2)) {
328         ixheaacd_tns_decode_coefficients(filter, parcor_coef, ptr_aac_tables);
329 
330       } else {
331         ixheaacd_tns_decode_coef(filter, parcor_coef_16, ptr_aac_tables);
332       }
333 
334       start = ixheaacd_min32(ixheaacd_min32(filter->start_band, tns_max_bands),
335                              ptr_ics_info->max_sfb);
336 
337       start = ptr_sfb_table[start];
338 
339       stop = ixheaacd_min32(ixheaacd_min32(filter->stop_band, tns_max_bands),
340                             ptr_ics_info->max_sfb);
341 
342       stop = ptr_sfb_table[stop];
343 
344       size = (stop - start);
345 
346       if (size <= 0) {
347         continue;
348       }
349       if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP) ||
350           (num_ch > 2)) {
351         ixheaacd_tns_parcor_to_lpc(parcor_coef, lpc_coef, &scale_lpc,
352                                    filter->order);
353 
354       } else {
355         (*ixheaacd_tns_parcor_lpc_convert)(parcor_coef_16, lpc_coef_16,
356                                            &scale_lpc, filter->order);
357       }
358 
359       {
360         WORD32 *ptr_tmp;
361 
362         if (maximum_bins_short == 120)
363           ptr_tmp = spec + (win * maximum_bins_short) + start;
364         else
365           ptr_tmp = spec + (win << 7) + start;
366 
367         scale_spec = (*ixheaacd_calc_max_spectral_line)(ptr_tmp, size);
368       }
369 
370       if (filter->direction == -1) {
371         position = stop - 1;
372 
373         if (maximum_bins_short == 120) {
374           if (((win * maximum_bins_short) + position) < filter->order) continue;
375         } else {
376           if (((win << 7) + position) < filter->order) continue;
377         }
378 
379       } else {
380         position = start;
381         if (maximum_bins_short == 120) {
382           if ((((win * maximum_bins_short) + position) + filter->order) > max_bin_long)
383             continue;
384         } else {
385           if ((((win << 7) + position) + filter->order) > MAX_BINS_LONG) continue;
386         }
387       }
388 
389       if ((num_ch <= 2) &&
390           ((object_type != AOT_ER_AAC_LD) && (object_type != AOT_AAC_LTP)))
391         scale_spec = ((scale_spec - 4) - scale_lpc);
392       else {
393         if (scale_spec > 17)
394           scale_spec = ((scale_spec - 6) - scale_lpc);
395         else if (scale_spec > 11)
396           scale_spec = ((scale_spec - 5) - scale_lpc);
397         else
398           scale_spec = ((scale_spec - 4) - scale_lpc);
399       }
400 
401       if (scale_spec > 0) {
402         scale_spec = ixheaacd_min32(scale_spec, 31);
403 
404         if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP) ||
405             (num_ch > 2)) {
406           if (ar_flag)
407           {
408             if (maximum_bins_short == 120) {
409               (*ixheaacd_tns_ar_filter_fixed)(&spec[(win * maximum_bins_short) + position],
410                                               size, filter->direction,
411                                               (WORD32 *)lpc_coef, filter->order,
412                                               (WORD32)scale_lpc, scale_spec);
413             } else {
414               (*ixheaacd_tns_ar_filter_fixed)(&spec[(win << 7) + position], size,
415                                               filter->direction,
416                                               (WORD32 *)lpc_coef, filter->order,
417                                               (WORD32)scale_lpc, scale_spec);
418             }
419           } else {
420             if (maximum_bins_short == 120) {
421               ixheaacd_tns_ma_filter_fixed_ld(&spec[(win * maximum_bins_short) + position],
422                                               size, filter->direction, lpc_coef,
423                                               filter->order, scale_lpc);
424             } else {
425               ixheaacd_tns_ma_filter_fixed_ld(&spec[(win << 7) + position], size,
426                                               filter->direction, lpc_coef,
427                                               filter->order, scale_lpc);
428             }
429           }
430         } else {
431           if (object_type == AOT_ER_AAC_ELD) scale_spec = scale_spec - 1;
432           if (maximum_bins_short == 120) {
433             (*ixheaacd_tns_ar_filter)(&spec[(win * maximum_bins_short) + position], size,
434                                       filter->direction, lpc_coef_16,
435                                       filter->order, (WORD32)scale_lpc,
436                                       scale_spec, scratch_buf);
437           } else {
438             (*ixheaacd_tns_ar_filter)(&spec[(win << 7) + position], size,
439                                       filter->direction, lpc_coef_16,
440                                       filter->order, (WORD32)scale_lpc,
441                                       scale_spec, scratch_buf);
442           }
443         }
444       }
445 
446       else {
447         WORD32 *ptr_tmp;
448 
449         if (maximum_bins_short == 120)
450           ptr_tmp = spec + (win * maximum_bins_short) + start;
451         else
452           ptr_tmp = spec + (win >> 7) + start;
453 
454         scale_spec = -scale_spec;
455         scale_spec = ixheaacd_min32(scale_spec, 31);
456 
457         for (i = size; i != 0; i--) {
458           *ptr_tmp = (*ptr_tmp >> scale_spec);
459           ptr_tmp++;
460         }
461 
462         if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP) ||
463             num_ch > 2) {
464           if (ar_flag) {
465             if (maximum_bins_short == 120) {
466               (*ixheaacd_tns_ar_filter_fixed)(
467                   &spec[(win * maximum_bins_short) + position], size, filter->direction,
468                   (WORD32 *)lpc_coef, filter->order, scale_lpc, 0);
469             } else {
470               (*ixheaacd_tns_ar_filter_fixed)(
471                   &spec[(win << 7) + position], size, filter->direction,
472                   (WORD32 *)lpc_coef, filter->order, scale_lpc, 0);
473             }
474           } else {
475             if (maximum_bins_short == 120) {
476               ixheaacd_tns_ma_filter_fixed_ld(&spec[(win * maximum_bins_short) + position],
477                                               size, filter->direction, lpc_coef,
478                                               filter->order, scale_lpc);
479             } else {
480               ixheaacd_tns_ma_filter_fixed_ld(&spec[(win << 7) + position], size,
481                                               filter->direction, lpc_coef,
482                                               filter->order, scale_lpc);
483             }
484           }
485         } else {
486           if (object_type == AOT_ER_AAC_ELD) {
487             scale_lpc = scale_lpc - 1;
488           }
489 
490           if (maximum_bins_short == 120) {
491             (*ixheaacd_tns_ar_filter)(&spec[(win * maximum_bins_short) + position], size,
492                                       filter->direction, lpc_coef_16,
493                                       filter->order, scale_lpc, 0, scratch_buf);
494           } else {
495             (*ixheaacd_tns_ar_filter)(&spec[(win << 7) + position], size,
496                                       filter->direction, lpc_coef_16,
497                                       filter->order, scale_lpc, 0, scratch_buf);
498           }
499         }
500 
501         if (maximum_bins_short == 120)
502           ptr_tmp = spec + (win * maximum_bins_short) + start;
503         else
504           ptr_tmp = spec + (win << 7) + start;
505 
506         for (i = size; i != 0; i--) {
507           *ptr_tmp = (*ptr_tmp << scale_spec);
508           ptr_tmp++;
509         }
510       }
511     }
512   }
513 }