• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <string.h>
21 #include "ixheaacd_sbr_common.h"
22 #include "ixheaacd_type_def.h"
23 
24 #include "ixheaacd_constants.h"
25 #include "ixheaacd_basic_ops32.h"
26 #include "ixheaacd_basic_ops16.h"
27 #include "ixheaacd_basic_ops40.h"
28 #include "ixheaacd_basic_ops.h"
29 
30 #include "ixheaacd_bitbuffer.h"
31 #include "ixheaacd_error_codes.h"
32 #include "ixheaacd_defines.h"
33 #include "ixheaacd_aac_rom.h"
34 #include "ixheaacd_pulsedata.h"
35 
36 #include "ixheaacd_pns.h"
37 #include "ixheaacd_drc_data_struct.h"
38 
39 #include "ixheaacd_lt_predict.h"
40 #include "ixheaacd_cnst.h"
41 #include "ixheaacd_ec_defines.h"
42 #include "ixheaacd_ec_struct_def.h"
43 #include "ixheaacd_channelinfo.h"
44 #include "ixheaacd_drc_dec.h"
45 #include "ixheaacd_sbrdecoder.h"
46 
47 #include "ixheaacd_block.h"
48 #include "ixheaacd_channel.h"
49 
50 #include "ixheaacd_common_rom.h"
51 #include "ixheaacd_basic_funcs.h"
52 #include "ixheaacd_basic_op.h"
53 #include "ixheaacd_intrinsics.h"
54 #include "ixheaacd_stereo.h"
55 
56 #include "ixheaacd_tns.h"
57 
58 #include "ixheaacd_audioobjtypes.h"
59 #include "ixheaacd_latmdemux.h"
60 
61 #include "ixheaacd_aacdec.h"
62 #include "ixheaacd_definitions.h"
63 
64 #include "ixheaacd_error_codes.h"
65 
66 #include "ixheaacd_pulsedata.h"
67 
68 #include "ixheaacd_sbrdecsettings.h"
69 #include "ixheaacd_sbr_scale.h"
70 #include "ixheaacd_env_extr_part.h"
71 #include "ixheaacd_sbr_rom.h"
72 #include "ixheaacd_audioobjtypes.h"
73 #include "ixheaacd_sbrdecoder.h"
74 #include "ixheaacd_memory_standards.h"
75 #include "ixheaacd_hybrid.h"
76 #include "ixheaacd_ps_dec.h"
77 #include "ixheaacd_mps_polyphase.h"
78 #include "ixheaacd_config.h"
79 #include "ixheaacd_qmf_dec.h"
80 #include "ixheaacd_mps_macro_def.h"
81 #include "ixheaacd_mps_struct_def.h"
82 #include "ixheaacd_mps_res_rom.h"
83 #include "ixheaacd_mps_aac_struct.h"
84 #include "ixheaacd_mps_dec.h"
85 #include "ixheaacd_struct_def.h"
86 #include "ixheaacd_adts_crc_check.h"
87 #include "ixheaacd_rvlc.h"
88 #include "ixheaacd_hcr.h"
89 #include "ixheaacd_function_selector.h"
90 
91 #define SPEC(ptr, w, gl) ((ptr) + ((w) * (gl)))
92 
ixheaacd_aac_showbits_32(UWORD8 * ptr_read_next,WORD32 cnt_bits,WORD32 * increment)93 UWORD32 ixheaacd_aac_showbits_32(UWORD8 *ptr_read_next, WORD32 cnt_bits,
94                                  WORD32 *increment) {
95   UWORD8 *v = ptr_read_next;
96   UWORD32 b = 0;
97   WORD32 i;
98   WORD32 bumped = 0;
99 
100   for (i = 0; i < 4; i++) {
101     b = b << 8;
102     if (cnt_bits > 0) {
103       b = b | *v;
104       v++;
105       bumped++;
106     }
107     cnt_bits -= 8;
108   }
109   if (increment != NULL) {
110     *increment = bumped;
111   }
112   return b;
113 }
114 
ixheaacd_getscalefactorbandoffsets(ia_ics_info_struct * ptr_ics_info,ia_aac_dec_tables_struct * ptr_aac_tables)115 WORD16 *ixheaacd_getscalefactorbandoffsets(
116     ia_ics_info_struct *ptr_ics_info,
117     ia_aac_dec_tables_struct *ptr_aac_tables) {
118   if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
119     return ptr_aac_tables->sfb_long_table;
120   } else {
121     return ptr_aac_tables->sfb_short_table;
122   }
123 }
124 
ixheaacd_getscalefactorbandwidth(ia_ics_info_struct * ptr_ics_info,ia_aac_dec_tables_struct * ptr_aac_tables)125 WORD8 *ixheaacd_getscalefactorbandwidth(
126     ia_ics_info_struct *ptr_ics_info,
127     ia_aac_dec_tables_struct *ptr_aac_tables) {
128   if (ptr_ics_info->frame_length == 512) {
129     return (
130         WORD8 *)(&ptr_aac_tables
131                       ->scale_fac_bands_512[ptr_ics_info->sampling_rate_index]
132                                            [0]);
133   } else if (ptr_ics_info->frame_length == 1024) {
134     return (
135         WORD8 *)(&ptr_aac_tables
136                       ->scale_factor_bands_long[ptr_ics_info->sampling_rate_index]
137                                                [0]);
138   } else {
139     return (
140         WORD8 *)(&ptr_aac_tables
141                       ->scale_fac_bands_480[ptr_ics_info->sampling_rate_index]
142                                            [0]);
143   }
144 }
145 
146 WORD32 ixheaacd_cblock_inv_quant_spect_data(
147     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
148     ia_aac_dec_tables_struct *ptr_aac_tables);
149 
ixheaacd_cblock_scale_spect_data(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 total_channels,WORD32 object_type,WORD32 aac_sf_data_resil_flag)150 void ixheaacd_cblock_scale_spect_data(
151     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
152     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
153     WORD32 object_type, WORD32 aac_sf_data_resil_flag) {
154   WORD32 *ptr_spect_coeff = ptr_aac_dec_channel_info->ptr_spec_coeff;
155   WORD8 *ptr_sfb_width = (WORD8 *)(ixheaacd_getscalefactorbandwidth(
156       &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables));
157   WORD16 *ptr_scale_fac = ptr_aac_dec_channel_info->ptr_scale_factor;
158   WORD tot_bands = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
159 
160   WORD num_win_grp, group_len;
161   WORD32 *ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
162   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
163 
164   if (object_type == AOT_ER_AAC_LC)
165     ptr_sfb_width = ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
166       .sfb_width;
167   for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
168       num_win_grp++) {
169     for (group_len = 0;
170         group_len < ptr_ics_info->window_group_length[num_win_grp];
171         group_len++) {
172       (*ixheaacd_scale_factor_process)(
173           &ptr_spect_coeff[0], &ptr_scale_fac[0], tot_bands,
174           (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
175           object_type, aac_sf_data_resil_flag);
176 
177       ptr_spect_coeff += MAX_BINS_SHORT;
178     }
179     ptr_scale_fac += MAX_SCALE_FACTOR_BANDS_SHORT;
180   }
181 }
182 
ixheaacd_read_pulse_data(ia_bit_buf_struct * it_bit_buff,ia_pulse_info_struct * ptr_pulse_info,ia_aac_dec_tables_struct * ptr_aac_tables)183 WORD32 ixheaacd_read_pulse_data(ia_bit_buf_struct *it_bit_buff,
184                                 ia_pulse_info_struct *ptr_pulse_info,
185                                 ia_aac_dec_tables_struct *ptr_aac_tables) {
186   WORD32 i, total_offset;
187   WORD32 error_code = 0;
188 
189   WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 8);
190   ptr_pulse_info->number_pulse = value >> 6;
191   ptr_pulse_info->pulse_start_band = value & 0x3F;
192 
193   if (ptr_pulse_info->pulse_start_band >= 52) {
194     return (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
195   }
196 
197   total_offset = ptr_aac_tables->str_aac_sfb_info[0]
198                      .sfb_index[ptr_pulse_info->pulse_start_band];
199 
200   for (i = 0; i < ptr_pulse_info->number_pulse + 1; i++) {
201     WORD32 value = ixheaacd_read_bits_buf(it_bit_buff, 9);
202     ptr_pulse_info->pulse_offset[i] = value >> 4;
203     ptr_pulse_info->pulse_amp[i] = value & 0xF;
204     total_offset += ptr_pulse_info->pulse_offset[i];
205 
206     if (total_offset >= 1024) {
207       error_code = (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PULSEDATA_ERROR;
208     }
209   }
210 
211   return error_code;
212 }
213 
ixheaacd_read_block_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 total_channels,WORD32 frame_size,WORD32 object_type,WORD32 aac_spect_data_resil_flag,WORD32 aac_sect_data_resil_flag,WORD32 aac_sf_data_resil_flag,WORD32 ele_type,ia_aac_dec_overlap_info * ptr_aac_dec_static_channel_info)214 static IA_ERRORCODE ixheaacd_read_block_data(
215     ia_bit_buf_struct *it_bit_buff,
216     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
217     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
218     WORD32 frame_size, WORD32 object_type, WORD32 aac_spect_data_resil_flag,
219     WORD32 aac_sect_data_resil_flag, WORD32 aac_sf_data_resil_flag,
220     WORD32 ele_type, ia_aac_dec_overlap_info *ptr_aac_dec_static_channel_info)
221 
222 {
223   FLAG gain_control_data_present;
224   WORD16 error_code = AAC_DEC_OK;
225 
226   if (ptr_aac_dec_channel_info->str_ics_info.window_sequence ==
227       EIGHT_SHORT_SEQUENCE) {
228     memset(ptr_aac_dec_channel_info->ptr_scale_factor, 0,
229            MAX_WINDOWS * MAX_SCALE_FACTOR_BANDS_SHORT * 3);
230   }
231 
232   error_code = ixheaacd_read_section_data(
233       it_bit_buff, ptr_aac_dec_channel_info, aac_spect_data_resil_flag,
234       aac_sect_data_resil_flag, ptr_aac_tables);
235 
236   if (error_code) {
237     return error_code;
238   }
239   if (aac_sf_data_resil_flag &&
240       ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)))
241     ixheaacd_rvlc_read(it_bit_buff, ptr_aac_dec_channel_info);
242   else
243     ixheaacd_read_scale_factor_data(it_bit_buff, ptr_aac_dec_channel_info,
244                                     ptr_aac_tables, object_type);
245 
246   error_code = 0;
247   if (object_type != AOT_ER_AAC_ELD) {
248     ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present =
249         ixheaacd_read_bits_buf(it_bit_buff, 1);
250     if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
251       error_code = ixheaacd_read_pulse_data(
252           it_bit_buff, &ptr_aac_dec_channel_info->str_pulse_info,
253           ptr_aac_tables);
254     }
255 
256     if (error_code) {
257       return error_code;
258     }
259   }
260 
261   ptr_aac_dec_channel_info->str_tns_info.tns_data_present =
262       (FLAG)ixheaacd_read_bits_buf(it_bit_buff, 1);
263 
264   if (object_type < ER_OBJECT_START) {
265     error_code = 0;
266     if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present) {
267       error_code =
268           ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
269     }
270 
271     if (error_code) {
272       return error_code;
273     }
274   }
275 
276   if (object_type != AOT_ER_AAC_ELD) {
277     gain_control_data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
278 
279     if (gain_control_data_present) {
280       return (WORD16)(
281           (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_GAIN_CONTROL_DATA_PRESENT);
282     }
283   }
284 
285   if (object_type == AOT_ER_AAC_ELD) {
286     if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
287       error_code =
288           ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
289     if (error_code) return error_code;
290   }
291 
292   if (aac_spect_data_resil_flag &&
293       ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
294       || (object_type == AOT_ER_AAC_LC)))
295     ixheaacd_hcr_read(it_bit_buff, ptr_aac_dec_channel_info, ele_type);
296 
297   if (aac_sf_data_resil_flag &&
298       ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD))) {
299     error_code = ixheaacd_rvlc_dec(
300         ptr_aac_dec_channel_info, ptr_aac_dec_static_channel_info, it_bit_buff);
301     if (error_code) return error_code;
302 
303     it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos;
304   }
305 
306   if (object_type == AOT_ER_AAC_LD || object_type == AOT_ER_AAC_LC) {
307     if (ptr_aac_dec_channel_info->str_tns_info.tns_data_present)
308       error_code =
309           ixheaacd_read_tns_data(it_bit_buff, ptr_aac_dec_channel_info);
310     if (error_code) return error_code;
311   }
312 
313   { it_bit_buff->bit_pos = 7 - it_bit_buff->bit_pos; }
314 
315   error_code = ixheaacd_read_spectral_data(
316       it_bit_buff, ptr_aac_dec_channel_info, ptr_aac_tables, total_channels,
317       frame_size, object_type, aac_spect_data_resil_flag,
318       aac_sf_data_resil_flag);
319 
320   it_bit_buff->bit_pos = (7 - it_bit_buff->bit_pos);
321 
322   return error_code;
323 }
324 
ixheaacd_ltp_decode(ia_bit_buf_struct * it_bit_buff,ia_ics_info_struct * ptr_ics_info,WORD32 object_type,WORD32 frame_size,WORD32 ch)325 IA_ERRORCODE ixheaacd_ltp_decode(ia_bit_buf_struct *it_bit_buff,
326                                  ia_ics_info_struct *ptr_ics_info,
327                                  WORD32 object_type, WORD32 frame_size,
328                                  WORD32 ch) {
329   IA_ERRORCODE retval = AAC_DEC_OK;
330 
331   if (ptr_ics_info->predictor_data_present) {
332     if (ch == 0) {
333       ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
334       ptr_ics_info->ltp.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
335       if (ptr_ics_info->ltp.data_present) {
336         if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
337                                         &(ptr_ics_info->ltp), it_bit_buff,
338                                         frame_size)) > 0) {
339           return retval;
340         }
341       }
342     } else {
343       ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
344       ptr_ics_info->ltp2.data_present = ixheaacd_read_bits_buf(it_bit_buff, 1);
345       if (ptr_ics_info->ltp2.data_present) {
346         if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
347                                         &(ptr_ics_info->ltp2), it_bit_buff,
348                                         frame_size)) > 0) {
349           return retval;
350         }
351       }
352     }
353   }
354   return retval;
355 }
ixheaacd_ics_read(ia_bit_buf_struct * it_bit_buff,ia_ics_info_struct * ptr_ics_info,WORD8 num_swb_window[2],WORD32 object_type,WORD32 common_window,WORD32 frame_size)356 WORD16 ixheaacd_ics_read(ia_bit_buf_struct *it_bit_buff,
357                          ia_ics_info_struct *ptr_ics_info,
358                          WORD8 num_swb_window[2], WORD32 object_type,
359                          WORD32 common_window, WORD32 frame_size) {
360   WORD i;
361   WORD mask;
362   WORD value = 0;
363 
364   if (object_type == AOT_ER_AAC_ELD) {
365     ptr_ics_info->window_sequence = 0;
366     ptr_ics_info->window_shape = 1;
367   } else {
368     if (object_type != AOT_ER_AAC_LD)
369     {
370       if (frame_size == 960)
371         ptr_ics_info->frame_length = 960;
372       else
373         ptr_ics_info->frame_length = 1024;
374     }
375     value = ixheaacd_read_bits_buf(it_bit_buff, 4);
376     ptr_ics_info->window_sequence = (WORD16)((value & 0x6) >> 1);
377     ptr_ics_info->window_shape = (WORD16)((value & 0x1));
378   }
379 
380   if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
381     ptr_ics_info->num_swb_window = num_swb_window[0];
382 
383     ptr_ics_info->num_window_groups = 1;
384     ptr_ics_info->window_group_length[0] = 1;
385 
386     if (object_type == AOT_ER_AAC_ELD) {
387       ptr_ics_info->max_sfb = ixheaacd_read_bits_buf(it_bit_buff, 6);
388       if (ptr_ics_info->max_sfb == 0) ptr_ics_info->num_swb_window = 0;
389     } else {
390       value = ixheaacd_read_bits_buf(it_bit_buff, 7);
391       ptr_ics_info->max_sfb = (value & 0x7E) >> 1;
392     }
393 
394     if ((object_type != AOT_ER_AAC_LD) && (object_type != AOT_AAC_LTP)) {
395       if (value & 1) {
396         return (WORD16)(
397             (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_PREDICTION_DATA_PRESENT);
398       }
399 
400     } else {
401       ptr_ics_info->predictor_data_present = value & 1;
402 
403       if (ptr_ics_info->predictor_data_present) {
404         WORD32 retval = AAC_DEC_OK;
405 
406         ixheaacd_init_ltp_object(&(ptr_ics_info->ltp));
407         if (object_type < ER_OBJECT_START) {
408           if ((ptr_ics_info->ltp.data_present =
409                    ixheaacd_read_bits_buf(it_bit_buff, 1)) &
410               1) {
411             if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
412                                             &(ptr_ics_info->ltp), it_bit_buff,
413                                             frame_size)) > 0) {
414               return retval;
415             }
416           }
417           if (common_window) {
418             ixheaacd_init_ltp_object(&(ptr_ics_info->ltp2));
419             if ((ptr_ics_info->ltp2.data_present =
420                      ixheaacd_read_bits_buf(it_bit_buff, 1)) &
421                 1) {
422               if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
423                                               &(ptr_ics_info->ltp2),
424                                               it_bit_buff, frame_size)) > 0) {
425                 return retval;
426               }
427             }
428           }
429         }
430         if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)) {
431           if (!common_window && (object_type >= ER_OBJECT_START)) {
432             if ((ptr_ics_info->ltp.data_present =
433                      ixheaacd_read_bits_buf(it_bit_buff, 1)) &
434                 1) {
435               if ((retval = ixheaacd_ltp_data(object_type, ptr_ics_info,
436                                               &(ptr_ics_info->ltp), it_bit_buff,
437                                               frame_size)) < 0) {
438                 return retval;
439               }
440             }
441           }
442         }
443       }
444     }
445 
446   } else {
447     WORD32 num_groups = 0, scale_factor_grouping;
448     ptr_ics_info->num_swb_window = num_swb_window[1];
449 
450     value = ixheaacd_read_bits_buf(it_bit_buff, 11);
451     ptr_ics_info->max_sfb = (value & 0x780) >> 7;
452 
453     scale_factor_grouping = (value & 0x7F);
454 
455     mask = 0x40;
456     for (i = 0; i < 7; i++) {
457       ptr_ics_info->window_group_length[i] = 1;
458 
459       if (scale_factor_grouping & mask) {
460         ptr_ics_info->window_group_length[num_groups] =
461             ptr_ics_info->window_group_length[num_groups] + 1;
462 
463       } else {
464         num_groups = num_groups + 1;
465       }
466 
467       mask = mask >> 1;
468     }
469 
470     ptr_ics_info->window_group_length[7] = 1;
471     ptr_ics_info->num_window_groups = num_groups + 1;
472   }
473 
474   if (ptr_ics_info->max_sfb > ptr_ics_info->num_swb_window) {
475     return (WORD16)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_SFB_TRANSMITTED;
476   }
477 
478   return AAC_DEC_OK;
479 }
480 
ixheaacd_individual_ch_stream(ia_bit_buf_struct * it_bit_buff,ia_aac_decoder_struct * aac_dec_handle,WORD32 num_ch,WORD32 frame_size,WORD32 total_channels,WORD32 object_type,ia_eld_specific_config_struct eld_specific_config,WORD32 ele_type)481 WORD16 ixheaacd_individual_ch_stream(
482     ia_bit_buf_struct *it_bit_buff, ia_aac_decoder_struct *aac_dec_handle,
483     WORD32 num_ch, WORD32 frame_size, WORD32 total_channels, WORD32 object_type,
484     ia_eld_specific_config_struct eld_specific_config, WORD32 ele_type) {
485   WORD16 error_code = AAC_DEC_OK;
486   WORD32 ch;
487   WORD32 crc_reg = 0;
488 
489   for (ch = 0; ch < num_ch; ch++) {
490     ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info =
491         aac_dec_handle->pstr_aac_dec_ch_info[ch];
492     ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_ch_info->str_ics_info;
493 
494     if (ch == 1) {
495       if (it_bit_buff->pstr_adts_crc_info->crc_active == 1 &&
496           (it_bit_buff->pstr_adts_crc_info->no_reg < 7)) {
497         crc_reg =
498             ixheaacd_adts_crc_start_reg(it_bit_buff->pstr_adts_crc_info,
499                                         it_bit_buff, CRC_ADTS_RAW_IIND_ICS);
500       }
501     }
502     ptr_aac_dec_ch_info->global_gain =
503         (WORD16)ixheaacd_read_bits_buf(it_bit_buff, 8);
504 
505     if (!(aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window)) {
506       error_code = ixheaacd_ics_read(
507           it_bit_buff, ptr_ics_info, aac_dec_handle->num_swb_window,
508           object_type,
509           aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window,
510           aac_dec_handle->samples_per_frame);
511       if (ch == 1)
512         aac_dec_handle->pstr_aac_dec_ch_info[ch - 1]->str_ics_info.ltp2.lag =
513             ptr_ics_info->ltp.lag;
514 
515       if (error_code) {
516         if (it_bit_buff->cnt_bits < 0) {
517           error_code = (WORD16)(
518               (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
519         }
520         return error_code;
521       }
522     }
523 
524     error_code = ixheaacd_read_block_data(
525         it_bit_buff, ptr_aac_dec_ch_info, aac_dec_handle->pstr_aac_tables,
526         total_channels, frame_size, object_type,
527         eld_specific_config.aac_spect_data_resil_flag,
528         eld_specific_config.aac_sect_data_resil_flag,
529         eld_specific_config.aac_sf_data_resil_flag, ele_type,
530         aac_dec_handle->pstr_aac_dec_overlap_info[ch]);
531     if (error_code) {
532       if (it_bit_buff->cnt_bits < 0) {
533         error_code = (WORD16)(
534             (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
535       }
536 
537       return error_code;
538     }
539 
540     if (ch == 0) {
541       if ((object_type == AOT_ER_AAC_LD) &&
542           (aac_dec_handle->pstr_aac_dec_ch_info[LEFT]->common_window) &&
543           (ele_type == ID_CPE)) {
544         IA_ERRORCODE temp =
545             ixheaacd_ltp_decode(it_bit_buff, ptr_ics_info, object_type,
546                                 aac_dec_handle->samples_per_frame, 1);
547 
548         if (temp != 0) {
549           return temp;
550         }
551         aac_dec_handle->pstr_aac_dec_ch_info[ch + 1]->str_ics_info.ltp.lag =
552             ptr_ics_info->ltp2.lag;
553       }
554     }
555     if (ch == 1) {
556       if (it_bit_buff->pstr_adts_crc_info->crc_active == 1) {
557         ixheaacd_adts_crc_end_reg(it_bit_buff->pstr_adts_crc_info, it_bit_buff,
558                                   crc_reg);
559       }
560     }
561   }
562 
563   return error_code;
564 }
565 
ixheaacd_read_ms_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_ch_info)566 VOID ixheaacd_read_ms_data(
567     ia_bit_buf_struct *it_bit_buff,
568     ia_aac_dec_channel_info_struct *ptr_aac_dec_ch_info) {
569   WORD32 num_win_group, sfb;
570   WORD32 ms_mask_present;
571   UWORD8 *ptr_ms_used = &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[0][0];
572   WORD32 num_window_groups =
573       ptr_aac_dec_ch_info->str_ics_info.num_window_groups;
574   WORD16 max_sfb = ptr_aac_dec_ch_info->str_ics_info.max_sfb;
575 
576   ms_mask_present = ixheaacd_read_bits_buf(it_bit_buff, 2);
577 
578   if (ms_mask_present < 1) {
579     memset(ptr_ms_used, 0,
580            sizeof(UWORD8) * JOINT_STEREO_MAX_BANDS * JOINT_STEREO_MAX_GROUPS);
581   }
582 
583   else if (ms_mask_present == 1) {
584     for (num_win_group = 0; num_win_group < num_window_groups;
585          num_win_group++) {
586       for (sfb = 0; sfb < max_sfb; sfb++) {
587         ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][sfb] =
588             (UWORD8)ixheaacd_read_bits_buf(it_bit_buff, 1);
589       }
590     }
591 
592   } else {
593     for (num_win_group = 0; num_win_group < num_window_groups;
594          num_win_group++) {
595       ptr_ms_used =
596           &ptr_aac_dec_ch_info->pstr_stereo_info->ms_used[num_win_group][0];
597       memset(ptr_ms_used, 1, (max_sfb) * sizeof(UWORD8));
598     }
599   }
600 }
601 
ixheaacd_channel_pair_process(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info[],WORD32 num_ch,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 total_channels,WORD32 object_type,WORD32 aac_spect_data_resil_flag,WORD32 aac_sf_data_resil_flag,WORD32 * in_data,WORD32 * out_data,void * self_ptr)602 IA_ERRORCODE ixheaacd_channel_pair_process(
603     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[], WORD32 num_ch,
604     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
605     WORD32 object_type, WORD32 aac_spect_data_resil_flag,
606     WORD32 aac_sf_data_resil_flag, WORD32 *in_data, WORD32 *out_data,
607     void *self_ptr) {
608   WORD32 channel;
609   IA_ERRORCODE err = IA_NO_ERROR;
610   ia_aac_decoder_struct *self = self_ptr;
611   if (aac_spect_data_resil_flag &&
612       ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_ER_AAC_ELD) ||
613       (object_type == AOT_ER_AAC_LC))) {
614     for (channel = 0; channel < num_ch; channel++) {
615       err = ixheaacd_cblock_inv_quant_spect_data(
616           ptr_aac_dec_channel_info[channel], ptr_aac_tables);
617       if (err) return err;
618       ixheaacd_cblock_scale_spect_data(ptr_aac_dec_channel_info[channel],
619                                        ptr_aac_tables, num_ch, object_type,
620                                        aac_sf_data_resil_flag);
621     }
622   }
623 
624   if (num_ch > 1) {
625     if (ptr_aac_dec_channel_info[LEFT]->common_window) {
626       if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_active ||
627           ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_active) {
628         ixheaacd_map_ms_mask_pns(ptr_aac_dec_channel_info);
629       }
630 
631       ixheaacd_ms_stereo_process(ptr_aac_dec_channel_info, ptr_aac_tables);
632     }
633 
634     ixheaacd_intensity_stereo_process(
635         ptr_aac_dec_channel_info, ptr_aac_tables, object_type,
636         aac_sf_data_resil_flag,
637         ptr_aac_dec_channel_info[LEFT]->str_ics_info.frame_length);
638   }
639 
640   for (channel = 0; channel < num_ch; channel++) {
641     WORD32 *p_spectrum = ptr_aac_dec_channel_info[channel]->ptr_spec_coeff;
642 
643     if (total_channels > 2) {
644       if (ptr_aac_dec_channel_info[channel]->str_ics_info.window_sequence !=
645           EIGHT_SHORT_SEQUENCE) {
646         WORD16 *band_offsets = ptr_aac_tables->sfb_long_table;
647         WORD32 no_spec_coeff = band_offsets[ptr_aac_dec_channel_info[channel]
648                                                 ->str_ics_info.max_sfb];
649         ixheaacd_right_shift_block(p_spectrum, no_spec_coeff, 3);
650       } else {
651         ixheaacd_right_shift_block(p_spectrum, 1024, 3);
652       }
653     }
654 
655     ixheaacd_pns_process(ptr_aac_dec_channel_info, channel, ptr_aac_tables);
656 
657     if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
658       {
659         if (channel == 0) {
660           ltp_info *ltp1 =
661               &(ptr_aac_dec_channel_info[channel]->str_ics_info.ltp);
662           ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp1,
663                                  p_spectrum, ptr_aac_tables,
664                                  self->ptr_aac_dec_static_channel_info[LEFT]
665                                      ->overlap_add_data.win_shape,
666                                  self->sampling_rate_index, object_type,
667                                  self->samples_per_frame, in_data, out_data);
668 
669         } else {
670           ltp_info *ltp2 =
671               (self->pstr_aac_dec_ch_info[0]->common_window)
672                   ? &(ptr_aac_dec_channel_info[0]->str_ics_info.ltp2)
673                   :
674 
675                   &(ptr_aac_dec_channel_info[1]->str_ics_info.ltp);
676           ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp2,
677                                  p_spectrum, ptr_aac_tables,
678                                  self->ptr_aac_dec_static_channel_info[RIGHT]
679                                      ->overlap_add_data.win_shape,
680                                  self->sampling_rate_index, object_type,
681                                  self->samples_per_frame, in_data, out_data);
682         }
683       }
684     }
685 
686     if (ptr_aac_dec_channel_info[channel]->str_tns_info.tns_data_present) {
687       ixheaacd_aac_tns_process(ptr_aac_dec_channel_info[channel],
688                                total_channels, ptr_aac_tables, object_type, 1,
689                                NULL);
690     }
691   }
692   return err;
693 }
694 
ixheaacd_set_corr_info(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,WORD16 pns_band)695 VOID ixheaacd_set_corr_info(
696     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD16 pns_band) {
697   ia_pns_correlation_info_struct *ptr_corr_info =
698       ptr_aac_dec_channel_info->pstr_pns_corr_info;
699   ptr_corr_info->correlated[(pns_band >> PNS_BAND_FLAGS_SHIFT)] |=
700       (1 << (pns_band & PNS_BAND_FLAGS_MASK));
701 }
702 
ixheaacd_map_ms_mask_pns(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info[CHANNELS])703 VOID ixheaacd_map_ms_mask_pns(
704     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[CHANNELS]) {
705   WORD32 num_win_group, sfb;
706 
707   for (num_win_group = 0;
708        num_win_group <
709        ptr_aac_dec_channel_info[LEFT]->str_ics_info.num_window_groups;
710        num_win_group++) {
711     for (sfb = 0; sfb < ptr_aac_dec_channel_info[LEFT]->str_ics_info.max_sfb;
712          sfb++) {
713       if (ptr_aac_dec_channel_info[LEFT]
714               ->pstr_stereo_info->ms_used[num_win_group][sfb]) {
715         WORD16 pns_band = (num_win_group << 4) + sfb;
716         ixheaacd_set_corr_info(ptr_aac_dec_channel_info[LEFT], pns_band);
717 
718         if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_used[pns_band] &&
719             ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_used[pns_band]) {
720           ptr_aac_dec_channel_info[LEFT]
721               ->pstr_stereo_info->ms_used[num_win_group][sfb] ^= 1;
722         }
723       }
724     }
725   }
726 }
727 
ixheaacd_pulse_data_apply(ia_pulse_info_struct * ptr_pulse_info,WORD8 * pulse_scratch,const WORD16 * ptr_swb_offset,WORD object_type)728 VOID ixheaacd_pulse_data_apply(ia_pulse_info_struct *ptr_pulse_info,
729                                WORD8 *pulse_scratch,
730                                const WORD16 *ptr_swb_offset, WORD object_type) {
731   WORD i;
732   WORD32 k;
733 
734   memset(pulse_scratch, 0, sizeof(WORD32) * 256);
735 
736   if (object_type != AOT_ER_AAC_ELD) {
737     if (ptr_pulse_info->pulse_data_present) {
738       k = ptr_swb_offset[ptr_pulse_info->pulse_start_band];
739 
740       for (i = 0; i <= ptr_pulse_info->number_pulse; i++) {
741         k = k + ptr_pulse_info->pulse_offset[i];
742         pulse_scratch[k] = ptr_pulse_info->pulse_amp[i];
743       }
744     }
745   } else {
746     ptr_pulse_info->pulse_data_present = 0;
747   }
748 }
749 
ixheaacd_read_spectral_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables,WORD32 total_channels,WORD32 frame_size,WORD32 object_type,WORD32 aac_spect_data_resil_flag,WORD32 aac_sf_data_resil_flag)750 IA_ERRORCODE ixheaacd_read_spectral_data(
751     ia_bit_buf_struct *it_bit_buff,
752     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
753     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
754     WORD32 frame_size, WORD32 object_type, WORD32 aac_spect_data_resil_flag,
755     WORD32 aac_sf_data_resil_flag) {
756   WORD sfb, max_sfb;
757   WORD num_win_grp, group_len, grp_offset;
758 
759   WORD index;
760   WORD8 *ptr_code_book;
761   WORD16 *ptr_scale_factor;
762   WORD32 *ptr_spec_coef;
763   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
764   WORD16 *swb_offset;
765 
766   WORD32 maximum_bins_short = ptr_ics_info->frame_length >> 3;
767 
768   WORD32 *ptr_spec_coef_out;
769 
770   ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
771   ptr_scale_factor = ptr_aac_dec_channel_info->ptr_scale_factor;
772   ptr_spec_coef = ptr_aac_dec_channel_info->ptr_spec_coeff;
773   max_sfb = ptr_ics_info->max_sfb;
774 
775   swb_offset =
776       ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence].sfb_index;
777 
778   if (!aac_spect_data_resil_flag) {
779     if (ptr_aac_dec_channel_info->str_ics_info.window_sequence !=
780         EIGHT_SHORT_SEQUENCE) {
781       WORD8 *ptr_scratch;
782 
783       if (object_type == AOT_ER_AAC_ELD)
784         ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->pulse_scratch;
785       else
786         ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
787 
788       memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
789 
790       ixheaacd_pulse_data_apply(&ptr_aac_dec_channel_info->str_pulse_info,
791                                 ptr_scratch, swb_offset, object_type);
792 
793       ptr_spec_coef_out = &ptr_spec_coef[0];
794       for (sfb = 0; sfb < max_sfb;) {
795         WORD ret_val;
796         WORD32 sfb_width;
797         WORD32 sect_cb = ptr_code_book[sfb];
798         WORD start = sfb;
799         if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
800             || (object_type == AOT_ER_AAC_LC)) {
801           if ((sect_cb >= 16) && (sect_cb <= 31)) {
802             ptr_code_book[sfb] = sect_cb = 11;
803           }
804         }
805         for (; sfb < max_sfb && (ptr_code_book[sfb] == sect_cb); sfb++)
806           ;
807 
808         sfb_width = swb_offset[sfb] - swb_offset[start];
809 
810         if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
811           ret_val = ixheaacd_huffman_dec_word2(it_bit_buff, sect_cb, sfb_width,
812                                                ptr_aac_tables,
813                                                ptr_spec_coef_out, ptr_scratch);
814 
815           if (ret_val != 0) {
816             return (WORD16)(
817                 (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
818           }
819         }
820 
821         else {
822           if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
823             ixheaacd_inverse_quantize(
824                 ptr_spec_coef_out, sfb_width,
825                 (WORD32 *)
826                     ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13,
827                 ptr_scratch);
828           }
829         }
830         ptr_scratch += sfb_width;
831         ptr_spec_coef_out += sfb_width;
832       }
833 
834       if ((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD))
835         index = 1024 - swb_offset[max_sfb];
836       else
837         index = frame_size - swb_offset[max_sfb];
838 
839       if (index < 0) return -1;
840 
841     } else {
842       memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
843 
844       grp_offset = 0;
845 
846       for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
847            num_win_grp++) {
848         WORD grp_len = ptr_ics_info->window_group_length[num_win_grp];
849 
850         if (maximum_bins_short == 120)
851           ptr_spec_coef_out = &ptr_spec_coef[grp_offset * maximum_bins_short];
852         else
853           ptr_spec_coef_out = &ptr_spec_coef[grp_offset * MAX_BINS_SHORT];
854 
855         WORD bands = num_win_grp * MAX_SCALE_FACTOR_BANDS_SHORT;
856         for (sfb = 0; sfb < max_sfb;) {
857           WORD sect_cb = ptr_code_book[bands];
858           WORD start = sfb;
859           WORD ret_val;
860 
861           if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
862               || (object_type == AOT_ER_AAC_LC)) {
863             if ((sect_cb >= 16) && (sect_cb <= 31)) {
864               ptr_code_book[bands] = sect_cb = 11;
865             }
866           }
867 
868           for (; sfb < max_sfb && (ptr_code_book[bands] == sect_cb);
869                sfb++, bands++)
870             ;
871 
872           if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
873             ret_val = ixheaacd_decode_huffman(
874                 it_bit_buff, sect_cb, ptr_spec_coef_out, (WORD16 *)swb_offset,
875                 start, sfb, grp_len, ptr_aac_tables, maximum_bins_short);
876 
877             if (ret_val != 0) {
878               return (WORD16)(
879                   (WORD32)
880                       IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
881             }
882           }
883         }
884         grp_offset = (grp_offset + grp_len);
885       }
886     }
887     {
888       WORD32 *ptr_scale_table;
889 
890       if (maximum_bins_short != 120)
891         ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
892       else
893         ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table_960;
894 
895       WORD8 *ptr_sfb_width =
896           ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
897               .sfb_width;
898 
899       for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
900            num_win_grp++) {
901         for (group_len = 0;
902              group_len < ptr_ics_info->window_group_length[num_win_grp];
903              group_len++) {
904           (*ixheaacd_scale_factor_process)(
905               &ptr_spec_coef[0], &ptr_scale_factor[0], max_sfb,
906               (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
907               object_type, aac_sf_data_resil_flag);
908 
909           if (maximum_bins_short == 120)
910             ptr_spec_coef += maximum_bins_short;
911           else
912             ptr_spec_coef += MAX_BINS_SHORT;
913         }
914 
915         ptr_scale_factor += MAX_SCALE_FACTOR_BANDS_SHORT;
916       }
917     }
918   } else {
919     ia_hcr_info_struct *pstr_hcr_info = &ptr_aac_dec_channel_info->str_hcr_info;
920     WORD32 error = 0;
921 
922     memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
923 
924     if (ptr_aac_dec_channel_info->reorder_spect_data_len != 0) {
925       error = ixheaacd_huff_code_reorder_init(
926           pstr_hcr_info, ptr_aac_dec_channel_info, ptr_aac_tables, it_bit_buff);
927 
928       if (error != 0) {
929         return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
930       }
931       error = ixheaacd_hcr_decoder(pstr_hcr_info, ptr_aac_dec_channel_info,
932                                    ptr_aac_tables, it_bit_buff);
933 
934       if (error != 0) {
935         ixheaacd_huff_mute_erroneous_lines(pstr_hcr_info);
936       }
937 
938       if (it_bit_buff->cnt_bits <
939           ptr_aac_dec_channel_info->reorder_spect_data_len) {
940         longjmp(*(it_bit_buff->xaac_jmp_buf),
941                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
942       }
943 
944       it_bit_buff->cnt_bits +=
945           -ptr_aac_dec_channel_info->reorder_spect_data_len;
946       it_bit_buff->ptr_read_next =
947           it_bit_buff->ptr_bit_buf_base +
948           ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
949       it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
950 
951     } else {
952       it_bit_buff->ptr_read_next =
953           it_bit_buff->ptr_bit_buf_base +
954           ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
955       it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
956     }
957   }
958 
959   return AAC_DEC_OK;
960 }
961 
ixheaacd_read_tns_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info)962 WORD16 ixheaacd_read_tns_data(
963     ia_bit_buf_struct *it_bit_buff,
964     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
965   WORD win_size, window_per_frame;
966   WORD n_filt_bits, start_band_bits, order_bits;
967   WORD32 bottom;
968 
969   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
970   ia_tns_info_aac_struct *ptr_tns_info =
971       &ptr_aac_dec_channel_info->str_tns_info;
972 
973   if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
974     n_filt_bits = 2;
975     start_band_bits = 6;
976     order_bits = 5;
977     window_per_frame = 1;
978 
979   } else {
980     n_filt_bits = 1;
981     start_band_bits = 4;
982     order_bits = 3;
983     window_per_frame = 8;
984   }
985 
986   bottom = ptr_ics_info->num_swb_window;
987 
988   for (win_size = 0; win_size < window_per_frame; win_size++) {
989     WORD n_filt;
990     WORD start_band, coef_res;
991     ptr_tns_info->n_filt[win_size] = n_filt =
992         (WORD16)ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits);
993 
994     if (n_filt) {
995       WORD filt;
996       WORD top;
997 
998       coef_res = ixheaacd_read_bits_buf(it_bit_buff, 1);
999 
1000       top = bottom;
1001       for (filt = 0; filt < n_filt; filt++) {
1002         WORD order;
1003         ia_filter_info_struct *filter =
1004             &ptr_tns_info->str_filter[win_size][filt];
1005 
1006         start_band = ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
1007 
1008         if (top < start_band) {
1009           top = start_band;
1010         }
1011         filter->start_band = top - start_band;
1012         filter->stop_band = top;
1013 
1014         top = filter->start_band;
1015 
1016         if (filter->start_band < 0) {
1017           filter->order = -1;
1018           return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_FATAL_TNS_RANGE_ERROR);
1019         }
1020 
1021         filter->order = order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
1022 
1023         if ((order - MAX_ORDER_LONG) > 0) {
1024           return (WORD16)(
1025               (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_TNS_ORDER_ERROR);
1026         }
1027 
1028         if (order) {
1029           WORD i;
1030           WORD32 coef, coef_compress;
1031           WORD resolution, shift;
1032 
1033           filter->direction =
1034               (WORD8)(ixheaacd_read_bits_buf(it_bit_buff, 1) ? -1 : 1);
1035 
1036           coef_compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
1037 
1038           filter->resolution = coef_res;
1039 
1040           resolution = coef_res + 3 - coef_compress;
1041 
1042           shift = 32 - resolution;
1043 
1044           for (i = 0; i < order; i++) {
1045             coef = ixheaacd_read_bits_buf(it_bit_buff, resolution);
1046             coef = coef << shift;
1047             filter->coef[i] = (WORD8)(coef >> shift);
1048           }
1049         }
1050       }
1051     }
1052   }
1053   return AAC_DEC_OK;
1054 }
1055 
ixheaacd_inv_quant(WORD32 * px_quant,WORD32 * ixheaacd_pow_table_Q13)1056 WORD32 ixheaacd_inv_quant(WORD32 *px_quant, WORD32 *ixheaacd_pow_table_Q13)
1057 
1058 {
1059   WORD32 q1;
1060   WORD32 temp;
1061   WORD32 q_abs;
1062   WORD16 interp;
1063   WORD32 shift;
1064 
1065   q_abs = *px_quant;
1066 
1067   if (q_abs > (8191 + 32))
1068     return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
1069 
1070   if (q_abs < 1024) {
1071     shift = 3;
1072   } else {
1073     shift = 6;
1074   }
1075 
1076   q1 = (q_abs) >> shift;
1077 
1078   interp = q_abs - (q1 << shift);
1079 
1080   temp = ixheaacd_pow_table_Q13[q1 + 1] - ixheaacd_pow_table_Q13[q1];
1081 
1082   temp = (WORD32)(temp * (WORD32)interp);
1083 
1084   temp = temp + (ixheaacd_pow_table_Q13[q1] << shift);
1085 
1086   if (shift == 3)
1087     temp = temp << 1;
1088   else
1089     temp = temp << 2;
1090 
1091   *px_quant = temp;
1092 
1093   return 0;
1094 }
1095 
ixheaacd_scale_value_in_place(WORD32 * value,WORD32 scalefactor)1096 void ixheaacd_scale_value_in_place(WORD32 *value, WORD32 scalefactor) {
1097   WORD32 newscale;
1098 
1099   if ((newscale = (scalefactor)) >= 0) {
1100     *(value) <<= newscale;
1101   } else {
1102     *(value) >>= -newscale;
1103   }
1104 }
1105 
ixheaacd_cblock_inv_quant_spect_data(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,ia_aac_dec_tables_struct * ptr_aac_tables)1106 WORD32 ixheaacd_cblock_inv_quant_spect_data(
1107     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
1108     ia_aac_dec_tables_struct *ptr_aac_tables) {
1109   int window, group, grp_win, band;
1110   IA_ERRORCODE err = IA_NO_ERROR;
1111   int sf_bands_transmitted = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
1112   WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
1113   const WORD16 *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
1114       &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
1115   WORD32 *ptr_pow_table_Q13 =
1116       (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1117 
1118   for (window = 0, group = 0;
1119        group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
1120        group++) {
1121     for (grp_win = 0;
1122          grp_win <
1123          ptr_aac_dec_channel_info->str_ics_info.window_group_length[group];
1124          grp_win++, window++) {
1125       for (band = 0; band < sf_bands_transmitted; band++) {
1126         WORD32 *ptr_spec_coef =
1127             SPEC(ptr_aac_dec_channel_info->ptr_spec_coeff, window,
1128                  ptr_aac_dec_channel_info->granule_len) +
1129             band_offsets[band];
1130         int num_lines = band_offsets[band + 1] - band_offsets[band];
1131         int bnds = group * 16 + band;
1132         int i;
1133 
1134         if ((ptr_code_book[bnds] == ZERO_HCB) ||
1135             (ptr_code_book[bnds] == INTENSITY_HCB) ||
1136             (ptr_code_book[bnds] == INTENSITY_HCB2))
1137           continue;
1138 
1139         if (ptr_code_book[bnds] == NOISE_HCB) {
1140           continue;
1141         }
1142 
1143         for (i = 0; i < num_lines; i++) {
1144           WORD8 temp = 0;
1145           WORD32 out1 = ptr_spec_coef[i];
1146           if (out1 <= 0) {
1147             out1 = sub_d(temp, out1);
1148             if (out1 > 127) {
1149               err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1150               if (err) return err;
1151             } else
1152               out1 = ptr_pow_table_Q13[out1];
1153             ptr_spec_coef[i] = -out1;
1154 
1155           } else {
1156             if (out1 > 127) {
1157               err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1158               if (err) return err;
1159             } else
1160               out1 = ptr_pow_table_Q13[out1];
1161 
1162             ptr_spec_coef[i] = out1;
1163           }
1164         }
1165       }
1166     }
1167   }
1168 
1169   return AAC_DEC_OK;
1170 }
1171 
ixheaacd_init_ltp_object(ltp_info * ltp)1172 void ixheaacd_init_ltp_object(ltp_info *ltp) {
1173   ltp->data_present = 0;
1174   ltp->last_band = 0;
1175 
1176   ltp->lag_update = 0;
1177   ltp->coef = 0;
1178 }
1179 
ixheaacd_ltp_data(WORD32 object_type,ia_ics_info_struct * ics,ltp_info * ltp,ia_bit_buf_struct * it_bit_buf,WORD32 frame_len)1180 WORD32 ixheaacd_ltp_data(WORD32 object_type, ia_ics_info_struct *ics,
1181                          ltp_info *ltp, ia_bit_buf_struct *it_bit_buf,
1182                          WORD32 frame_len) {
1183   UWORD8 sfb, w;
1184 
1185   if (object_type == AOT_ER_AAC_LD) {
1186     ltp->lag_update = ixheaacd_read_bits_buf(it_bit_buf, 1);
1187 
1188     if (ltp->lag_update) {
1189       ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 10);
1190     }
1191   } else {
1192     ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 11);
1193   }
1194 
1195   if (ltp->lag > (frame_len << 1)) return -1;
1196 
1197   ltp->coef = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 3);
1198 
1199   if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1200     for (w = 0; w < 8; w++) {
1201       if ((ltp->short_used[w] = ixheaacd_read_bits_buf(it_bit_buf, 1)) & 1) {
1202         ltp->short_lag_present[w] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1203         if (ltp->short_lag_present[w]) {
1204           ltp->short_lag[w] = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 4);
1205         }
1206       }
1207     }
1208   } else {
1209     ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1210 
1211     for (sfb = 0; sfb < ltp->last_band; sfb++) {
1212       ltp->long_used[sfb] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1213     }
1214   }
1215   if (ics->frame_length == 480) {
1216     if ((ics->sampling_rate_index > 5) &&
1217         (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_480))
1218       ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_480;
1219     else if ((ics->sampling_rate_index == 5) &&
1220              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_480))
1221       ltp->last_band = MAX_LTP_SFB_SR_FIVE_480;
1222     else if ((ics->sampling_rate_index < 5) &&
1223              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_480))
1224       ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_480;
1225   } else if (ics->frame_length == 512) {
1226     if ((ics->sampling_rate_index > 5) &&
1227         (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_512))
1228       ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_512;
1229     else if ((ics->sampling_rate_index == 5) &&
1230              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_512))
1231       ltp->last_band = MAX_LTP_SFB_SR_FIVE_512;
1232     else if ((ics->sampling_rate_index < 5) &&
1233              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_512))
1234       ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_512;
1235   }
1236   return 0;
1237 }
1238