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