• 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 "ixheaac_type_def.h"
23 
24 #include "ixheaac_constants.h"
25 #include "ixheaac_basic_ops32.h"
26 #include "ixheaac_basic_ops16.h"
27 #include "ixheaac_basic_ops40.h"
28 #include "ixheaac_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 "ixheaac_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[CHANNELS],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[CHANNELS], WORD32 num_ch,
604     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels, WORD32 object_type,
605     WORD32 aac_spect_data_resil_flag, WORD32 aac_sf_data_resil_flag, WORD32 *in_data,
606     WORD32 *out_data, void *self_ptr) {
607   WORD32 channel;
608   IA_ERRORCODE err = IA_NO_ERROR;
609   ia_aac_decoder_struct *self = self_ptr;
610   if (aac_spect_data_resil_flag &&
611       ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_ER_AAC_ELD) ||
612       (object_type == AOT_ER_AAC_LC))) {
613     for (channel = 0; channel < num_ch; channel++) {
614       err = ixheaacd_cblock_inv_quant_spect_data(
615           ptr_aac_dec_channel_info[channel], ptr_aac_tables);
616       if (err) return err;
617       ixheaacd_cblock_scale_spect_data(ptr_aac_dec_channel_info[channel],
618                                        ptr_aac_tables, num_ch, object_type,
619                                        aac_sf_data_resil_flag);
620     }
621   }
622 
623   if (num_ch > 1) {
624     if (ptr_aac_dec_channel_info[LEFT]->common_window) {
625       if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_active ||
626           ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_active) {
627         ixheaacd_map_ms_mask_pns(ptr_aac_dec_channel_info);
628       }
629 
630       ixheaacd_ms_stereo_process(ptr_aac_dec_channel_info, ptr_aac_tables);
631     }
632 
633     ixheaacd_intensity_stereo_process(
634         ptr_aac_dec_channel_info, ptr_aac_tables, object_type,
635         aac_sf_data_resil_flag,
636         ptr_aac_dec_channel_info[LEFT]->str_ics_info.frame_length);
637   }
638 
639   for (channel = 0; channel < num_ch; channel++) {
640     WORD32 *p_spectrum = ptr_aac_dec_channel_info[channel]->ptr_spec_coeff;
641 
642     if (total_channels > 2) {
643       if (ptr_aac_dec_channel_info[channel]->str_ics_info.window_sequence !=
644           EIGHT_SHORT_SEQUENCE) {
645         WORD16 *band_offsets = ptr_aac_tables->sfb_long_table;
646         WORD32 no_spec_coeff = band_offsets[ptr_aac_dec_channel_info[channel]
647                                                 ->str_ics_info.max_sfb];
648         ixheaacd_right_shift_block(p_spectrum, no_spec_coeff, 3);
649       } else {
650         ixheaacd_right_shift_block(p_spectrum, 1024, 3);
651       }
652     }
653 
654     ixheaacd_pns_process(ptr_aac_dec_channel_info, channel, ptr_aac_tables);
655 
656     if ((object_type == AOT_ER_AAC_LD) || (object_type == AOT_AAC_LTP)) {
657       {
658         if (channel == 0) {
659           ltp_info *ltp1 =
660               &(ptr_aac_dec_channel_info[channel]->str_ics_info.ltp);
661           ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp1,
662                                  p_spectrum, ptr_aac_tables,
663                                  self->ptr_aac_dec_static_channel_info[LEFT]
664                                      ->overlap_add_data.win_shape,
665                                  self->sampling_rate_index, object_type,
666                                  self->samples_per_frame, in_data, out_data);
667 
668         } else {
669           ltp_info *ltp2 =
670               (self->pstr_aac_dec_ch_info[0]->common_window)
671                   ? &(ptr_aac_dec_channel_info[0]->str_ics_info.ltp2)
672                   :
673 
674                   &(ptr_aac_dec_channel_info[1]->str_ics_info.ltp);
675           ixheaacd_lt_prediction(ptr_aac_dec_channel_info[channel], ltp2,
676                                  p_spectrum, ptr_aac_tables,
677                                  self->ptr_aac_dec_static_channel_info[RIGHT]
678                                      ->overlap_add_data.win_shape,
679                                  self->sampling_rate_index, object_type,
680                                  self->samples_per_frame, in_data, out_data);
681         }
682       }
683     }
684 
685     if (ptr_aac_dec_channel_info[channel]->str_tns_info.tns_data_present) {
686       ixheaacd_aac_tns_process(ptr_aac_dec_channel_info[channel],
687                                total_channels, ptr_aac_tables, object_type, 1,
688                                NULL);
689     }
690   }
691   return err;
692 }
693 
ixheaacd_set_corr_info(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info,WORD16 pns_band)694 VOID ixheaacd_set_corr_info(
695     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info, WORD16 pns_band) {
696   ia_pns_correlation_info_struct *ptr_corr_info =
697       ptr_aac_dec_channel_info->pstr_pns_corr_info;
698   ptr_corr_info->correlated[(pns_band >> PNS_BAND_FLAGS_SHIFT)] |=
699       (1 << (pns_band & PNS_BAND_FLAGS_MASK));
700 }
701 
ixheaacd_map_ms_mask_pns(ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info[CHANNELS])702 VOID ixheaacd_map_ms_mask_pns(
703     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info[CHANNELS]) {
704   WORD32 num_win_group, sfb;
705 
706   for (num_win_group = 0;
707        num_win_group <
708        ptr_aac_dec_channel_info[LEFT]->str_ics_info.num_window_groups;
709        num_win_group++) {
710     for (sfb = 0; sfb < ptr_aac_dec_channel_info[LEFT]->str_ics_info.max_sfb;
711          sfb++) {
712       if (ptr_aac_dec_channel_info[LEFT]
713               ->pstr_stereo_info->ms_used[num_win_group][sfb]) {
714         WORD16 pns_band = (num_win_group << 4) + sfb;
715         ixheaacd_set_corr_info(ptr_aac_dec_channel_info[LEFT], pns_band);
716 
717         if (ptr_aac_dec_channel_info[LEFT]->str_pns_info.pns_used[pns_band] &&
718             ptr_aac_dec_channel_info[RIGHT]->str_pns_info.pns_used[pns_band]) {
719           ptr_aac_dec_channel_info[LEFT]
720               ->pstr_stereo_info->ms_used[num_win_group][sfb] ^= 1;
721         }
722       }
723     }
724   }
725 }
726 
ixheaacd_pulse_data_apply(ia_pulse_info_struct * ptr_pulse_info,WORD8 * pulse_scratch,const WORD16 * ptr_swb_offset,WORD object_type)727 VOID ixheaacd_pulse_data_apply(ia_pulse_info_struct *ptr_pulse_info,
728                                WORD8 *pulse_scratch,
729                                const WORD16 *ptr_swb_offset, WORD object_type) {
730   WORD i;
731   WORD32 k;
732 
733   memset(pulse_scratch, 0, sizeof(WORD32) * 256);
734 
735   if (object_type != AOT_ER_AAC_ELD) {
736     if (ptr_pulse_info->pulse_data_present) {
737       k = ptr_swb_offset[ptr_pulse_info->pulse_start_band];
738 
739       for (i = 0; i <= ptr_pulse_info->number_pulse; i++) {
740         k = k + ptr_pulse_info->pulse_offset[i];
741         pulse_scratch[k] = ptr_pulse_info->pulse_amp[i];
742       }
743     }
744   } else {
745     ptr_pulse_info->pulse_data_present = 0;
746   }
747 }
748 
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)749 IA_ERRORCODE ixheaacd_read_spectral_data(
750     ia_bit_buf_struct *it_bit_buff,
751     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
752     ia_aac_dec_tables_struct *ptr_aac_tables, WORD32 total_channels,
753     WORD32 frame_size, WORD32 object_type, WORD32 aac_spect_data_resil_flag,
754     WORD32 aac_sf_data_resil_flag) {
755   WORD sfb, max_sfb;
756   WORD num_win_grp, group_len, grp_offset;
757 
758   WORD index;
759   WORD8 *ptr_code_book;
760   WORD16 *ptr_scale_factor;
761   WORD32 *ptr_spec_coef;
762   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
763   WORD16 *swb_offset;
764 
765   WORD32 maximum_bins_short = ptr_ics_info->frame_length >> 3;
766 
767   WORD32 *ptr_spec_coef_out;
768 
769   ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
770   ptr_scale_factor = ptr_aac_dec_channel_info->ptr_scale_factor;
771   ptr_spec_coef = ptr_aac_dec_channel_info->ptr_spec_coeff;
772   max_sfb = ptr_ics_info->max_sfb;
773 
774   swb_offset =
775       ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence].sfb_index;
776 
777   if (!aac_spect_data_resil_flag) {
778     if (ptr_aac_dec_channel_info->str_ics_info.window_sequence !=
779         EIGHT_SHORT_SEQUENCE) {
780       WORD8 *ptr_scratch;
781 
782       if (object_type == AOT_ER_AAC_ELD)
783         ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->pulse_scratch;
784       else
785         ptr_scratch = (WORD8 *)ptr_aac_dec_channel_info->scratch_buf_ptr;
786 
787       memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
788 
789       ixheaacd_pulse_data_apply(&ptr_aac_dec_channel_info->str_pulse_info,
790                                 ptr_scratch, swb_offset, object_type);
791 
792       ptr_spec_coef_out = &ptr_spec_coef[0];
793       for (sfb = 0; sfb < max_sfb;) {
794         WORD ret_val;
795         WORD32 sfb_width;
796         WORD32 sect_cb = ptr_code_book[sfb];
797         WORD start = sfb;
798         if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
799             || (object_type == AOT_ER_AAC_LC)) {
800           if ((sect_cb >= 16) && (sect_cb <= 31)) {
801             ptr_code_book[sfb] = sect_cb = 11;
802           }
803         }
804         for (; sfb < max_sfb && (ptr_code_book[sfb] == sect_cb); sfb++)
805           ;
806 
807         sfb_width = swb_offset[sfb] - swb_offset[start];
808 
809         if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
810           ret_val = ixheaacd_huffman_dec_word2(it_bit_buff, sect_cb, sfb_width,
811                                                ptr_aac_tables,
812                                                ptr_spec_coef_out, ptr_scratch);
813 
814           if (ret_val != 0) {
815             return (WORD16)(
816                 (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
817           }
818         }
819 
820         else {
821           if (ptr_aac_dec_channel_info->str_pulse_info.pulse_data_present) {
822             ixheaacd_inverse_quantize(
823                 ptr_spec_coef_out, sfb_width,
824                 (WORD32 *)
825                     ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13,
826                 ptr_scratch);
827           }
828         }
829         ptr_scratch += sfb_width;
830         ptr_spec_coef_out += sfb_width;
831       }
832 
833       if ((object_type != AOT_ER_AAC_ELD) && (object_type != AOT_ER_AAC_LD))
834         index = 1024 - swb_offset[max_sfb];
835       else
836         index = frame_size - swb_offset[max_sfb];
837 
838       if (index < 0) return -1;
839 
840     } else {
841       memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
842 
843       grp_offset = 0;
844 
845       for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
846            num_win_grp++) {
847         WORD grp_len = ptr_ics_info->window_group_length[num_win_grp];
848 
849         if (maximum_bins_short == 120)
850           ptr_spec_coef_out = &ptr_spec_coef[grp_offset * maximum_bins_short];
851         else
852           ptr_spec_coef_out = &ptr_spec_coef[grp_offset * MAX_BINS_SHORT];
853 
854         WORD bands = num_win_grp * MAX_SCALE_FACTOR_BANDS_SHORT;
855         for (sfb = 0; sfb < max_sfb;) {
856           WORD sect_cb = ptr_code_book[bands];
857           WORD start = sfb;
858           WORD ret_val;
859 
860           if ((object_type == AOT_ER_AAC_ELD) || (object_type == AOT_ER_AAC_LD)
861               || (object_type == AOT_ER_AAC_LC)) {
862             if ((sect_cb >= 16) && (sect_cb <= 31)) {
863               ptr_code_book[bands] = sect_cb = 11;
864             }
865           }
866 
867           for (; sfb < max_sfb && (ptr_code_book[bands] == sect_cb);
868                sfb++, bands++)
869             ;
870 
871           if (sect_cb > ZERO_HCB && (sect_cb < NOISE_HCB)) {
872             ret_val = ixheaacd_decode_huffman(
873                 it_bit_buff, sect_cb, ptr_spec_coef_out, (WORD16 *)swb_offset,
874                 start, sfb, grp_len, ptr_aac_tables, maximum_bins_short);
875 
876             if (ret_val != 0) {
877               return (WORD16)(
878                   (WORD32)
879                       IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL);
880             }
881           }
882         }
883         grp_offset = (grp_offset + grp_len);
884       }
885     }
886     {
887       WORD32 *ptr_scale_table;
888 
889       if (maximum_bins_short != 120)
890         ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table;
891       else
892         ptr_scale_table = ptr_aac_tables->pstr_block_tables->scale_table_960;
893 
894       WORD8 *ptr_sfb_width =
895           ptr_aac_tables->str_aac_sfb_info[ptr_ics_info->window_sequence]
896               .sfb_width;
897 
898       for (num_win_grp = 0; num_win_grp < ptr_ics_info->num_window_groups;
899            num_win_grp++) {
900         for (group_len = 0;
901              group_len < ptr_ics_info->window_group_length[num_win_grp];
902              group_len++) {
903           (*ixheaacd_scale_factor_process)(
904               &ptr_spec_coef[0], &ptr_scale_factor[0], max_sfb,
905               (WORD8 *)ptr_sfb_width, ptr_scale_table, total_channels,
906               object_type, aac_sf_data_resil_flag);
907 
908           if (maximum_bins_short == 120)
909             ptr_spec_coef += maximum_bins_short;
910           else
911             ptr_spec_coef += MAX_BINS_SHORT;
912         }
913 
914         ptr_scale_factor += MAX_SCALE_FACTOR_BANDS_SHORT;
915       }
916     }
917   } else {
918     ia_hcr_info_struct *pstr_hcr_info = &ptr_aac_dec_channel_info->str_hcr_info;
919     WORD32 error = 0;
920 
921     memset(ptr_spec_coef, 0, sizeof(WORD32) * 1024);
922 
923     if (ptr_aac_dec_channel_info->reorder_spect_data_len != 0) {
924       error = ixheaacd_huff_code_reorder_init(
925           pstr_hcr_info, ptr_aac_dec_channel_info, ptr_aac_tables, it_bit_buff);
926 
927       if (error != 0) {
928         return IA_XHEAAC_DEC_EXE_NONFATAL_DECODE_FRAME_ERROR;
929       }
930       error = ixheaacd_hcr_decoder(pstr_hcr_info, ptr_aac_dec_channel_info,
931                                    ptr_aac_tables, it_bit_buff);
932 
933       if (error != 0) {
934         ixheaacd_huff_mute_erroneous_lines(pstr_hcr_info);
935       }
936 
937       if (it_bit_buff->cnt_bits <
938           ptr_aac_dec_channel_info->reorder_spect_data_len) {
939         longjmp(*(it_bit_buff->xaac_jmp_buf),
940                 IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
941       }
942 
943       it_bit_buff->cnt_bits +=
944           -ptr_aac_dec_channel_info->reorder_spect_data_len;
945       it_bit_buff->ptr_read_next =
946           it_bit_buff->ptr_bit_buf_base +
947           ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
948       it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
949 
950     } else {
951       it_bit_buff->ptr_read_next =
952           it_bit_buff->ptr_bit_buf_base +
953           ((it_bit_buff->size - it_bit_buff->cnt_bits) >> 3);
954       it_bit_buff->bit_pos = (it_bit_buff->size - it_bit_buff->cnt_bits) & 7;
955     }
956   }
957 
958   return AAC_DEC_OK;
959 }
960 
ixheaacd_read_tns_data(ia_bit_buf_struct * it_bit_buff,ia_aac_dec_channel_info_struct * ptr_aac_dec_channel_info)961 WORD16 ixheaacd_read_tns_data(
962     ia_bit_buf_struct *it_bit_buff,
963     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info) {
964   WORD win_size, window_per_frame;
965   WORD n_filt_bits, start_band_bits, order_bits;
966   WORD32 bottom;
967 
968   ia_ics_info_struct *ptr_ics_info = &ptr_aac_dec_channel_info->str_ics_info;
969   ia_tns_info_aac_struct *ptr_tns_info =
970       &ptr_aac_dec_channel_info->str_tns_info;
971 
972   if (ptr_ics_info->window_sequence != EIGHT_SHORT_SEQUENCE) {
973     n_filt_bits = 2;
974     start_band_bits = 6;
975     order_bits = 5;
976     window_per_frame = 1;
977 
978   } else {
979     n_filt_bits = 1;
980     start_band_bits = 4;
981     order_bits = 3;
982     window_per_frame = 8;
983   }
984 
985   bottom = ptr_ics_info->num_swb_window;
986 
987   for (win_size = 0; win_size < window_per_frame; win_size++) {
988     WORD n_filt;
989     WORD start_band, coef_res;
990     ptr_tns_info->n_filt[win_size] = n_filt =
991         (WORD16)ixheaacd_read_bits_buf(it_bit_buff, n_filt_bits);
992 
993     if (n_filt) {
994       WORD filt;
995       WORD top;
996 
997       coef_res = ixheaacd_read_bits_buf(it_bit_buff, 1);
998 
999       top = bottom;
1000       for (filt = 0; filt < n_filt; filt++) {
1001         WORD order;
1002         ia_filter_info_struct *filter =
1003             &ptr_tns_info->str_filter[win_size][filt];
1004 
1005         start_band = ixheaacd_read_bits_buf(it_bit_buff, start_band_bits);
1006 
1007         if (top < start_band) {
1008           top = start_band;
1009         }
1010         filter->start_band = top - start_band;
1011         filter->stop_band = top;
1012 
1013         top = filter->start_band;
1014 
1015         if (filter->start_band < 0) {
1016           filter->order = -1;
1017           return (WORD16)((WORD32)IA_XHEAAC_DEC_EXE_FATAL_TNS_RANGE_ERROR);
1018         }
1019 
1020         filter->order = order = ixheaacd_read_bits_buf(it_bit_buff, order_bits);
1021 
1022         if ((order - MAX_ORDER_LONG) > 0) {
1023           return (WORD16)(
1024               (WORD32)IA_XHEAAC_DEC_EXE_NONFATAL_TNS_ORDER_ERROR);
1025         }
1026 
1027         if (order) {
1028           WORD i;
1029           WORD32 coef, coef_compress;
1030           WORD resolution, shift;
1031 
1032           filter->direction =
1033               (WORD8)(ixheaacd_read_bits_buf(it_bit_buff, 1) ? -1 : 1);
1034 
1035           coef_compress = ixheaacd_read_bits_buf(it_bit_buff, 1);
1036 
1037           filter->resolution = coef_res;
1038 
1039           resolution = coef_res + 3 - coef_compress;
1040 
1041           shift = 32 - resolution;
1042 
1043           for (i = 0; i < order; i++) {
1044             coef = ixheaacd_read_bits_buf(it_bit_buff, resolution);
1045             coef = coef << shift;
1046             filter->coef[i] = (WORD8)(coef >> shift);
1047           }
1048         }
1049       }
1050     }
1051   }
1052   return AAC_DEC_OK;
1053 }
1054 
ixheaacd_inv_quant(WORD32 * px_quant,WORD32 * ixheaacd_pow_table_Q13)1055 WORD32 ixheaacd_inv_quant(WORD32 *px_quant, WORD32 *ixheaacd_pow_table_Q13)
1056 
1057 {
1058   WORD32 q1;
1059   WORD32 temp;
1060   WORD32 q_abs;
1061   WORD16 interp;
1062   WORD32 shift;
1063 
1064   q_abs = *px_quant;
1065 
1066   if (q_abs > (8191 + 32))
1067     return IA_XHEAAC_DEC_EXE_NONFATAL_EXCEEDS_MAX_HUFFDEC_VAL;
1068 
1069   if (q_abs < 1024) {
1070     shift = 3;
1071   } else {
1072     shift = 6;
1073   }
1074 
1075   q1 = (q_abs) >> shift;
1076 
1077   interp = q_abs - (q1 << shift);
1078 
1079   temp = ixheaacd_pow_table_Q13[q1 + 1] - ixheaacd_pow_table_Q13[q1];
1080 
1081   temp = (WORD32)(temp * (WORD32)interp);
1082 
1083   temp = temp + (ixheaacd_pow_table_Q13[q1] << shift);
1084 
1085   if (shift == 3)
1086     temp = temp << 1;
1087   else
1088     temp = temp << 2;
1089 
1090   *px_quant = temp;
1091 
1092   return 0;
1093 }
1094 
ixheaacd_scale_value_in_place(WORD32 * value,WORD32 scalefactor)1095 void ixheaacd_scale_value_in_place(WORD32 *value, WORD32 scalefactor) {
1096   WORD32 newscale;
1097 
1098   if ((newscale = (scalefactor)) >= 0) {
1099     *(value) <<= newscale;
1100   } else {
1101     *(value) >>= -newscale;
1102   }
1103 }
1104 
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)1105 WORD32 ixheaacd_cblock_inv_quant_spect_data(
1106     ia_aac_dec_channel_info_struct *ptr_aac_dec_channel_info,
1107     ia_aac_dec_tables_struct *ptr_aac_tables) {
1108   int window, group, grp_win, band;
1109   IA_ERRORCODE err = IA_NO_ERROR;
1110   int sf_bands_transmitted = ptr_aac_dec_channel_info->str_ics_info.max_sfb;
1111   WORD8 *ptr_code_book = ptr_aac_dec_channel_info->ptr_code_book;
1112   const WORD16 *band_offsets = (WORD16 *)ixheaacd_getscalefactorbandoffsets(
1113       &(ptr_aac_dec_channel_info->str_ics_info), ptr_aac_tables);
1114   WORD32 *ptr_pow_table_Q13 =
1115       (WORD32 *)ptr_aac_tables->pstr_block_tables->ixheaacd_pow_table_Q13;
1116 
1117   for (window = 0, group = 0;
1118        group < ptr_aac_dec_channel_info->str_ics_info.num_window_groups;
1119        group++) {
1120     for (grp_win = 0;
1121          grp_win <
1122          ptr_aac_dec_channel_info->str_ics_info.window_group_length[group];
1123          grp_win++, window++) {
1124       for (band = 0; band < sf_bands_transmitted; band++) {
1125         WORD32 *ptr_spec_coef =
1126             SPEC(ptr_aac_dec_channel_info->ptr_spec_coeff, window,
1127                  ptr_aac_dec_channel_info->granule_len) +
1128             band_offsets[band];
1129         int num_lines = band_offsets[band + 1] - band_offsets[band];
1130         int bnds = group * 16 + band;
1131         int i;
1132 
1133         if ((ptr_code_book[bnds] == ZERO_HCB) ||
1134             (ptr_code_book[bnds] == INTENSITY_HCB) ||
1135             (ptr_code_book[bnds] == INTENSITY_HCB2))
1136           continue;
1137 
1138         if (ptr_code_book[bnds] == NOISE_HCB) {
1139           continue;
1140         }
1141 
1142         for (i = 0; i < num_lines; i++) {
1143           WORD8 temp = 0;
1144           WORD32 out1 = ptr_spec_coef[i];
1145           if (out1 <= 0) {
1146             out1 = sub_d(temp, out1);
1147             if (out1 > 127) {
1148               err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1149               if (err) return err;
1150             } else
1151               out1 = ptr_pow_table_Q13[out1];
1152             ptr_spec_coef[i] = -out1;
1153 
1154           } else {
1155             if (out1 > 127) {
1156               err = ixheaacd_inv_quant(&out1, ptr_pow_table_Q13);
1157               if (err) return err;
1158             } else
1159               out1 = ptr_pow_table_Q13[out1];
1160 
1161             ptr_spec_coef[i] = out1;
1162           }
1163         }
1164       }
1165     }
1166   }
1167 
1168   return AAC_DEC_OK;
1169 }
1170 
ixheaacd_init_ltp_object(ltp_info * ltp)1171 void ixheaacd_init_ltp_object(ltp_info *ltp) {
1172   ltp->data_present = 0;
1173   ltp->last_band = 0;
1174 
1175   ltp->lag_update = 0;
1176   ltp->coef = 0;
1177 }
1178 
ixheaacd_ltp_data(WORD32 object_type,ia_ics_info_struct * ics,ltp_info * ltp,ia_bit_buf_struct * it_bit_buf,WORD32 frame_len)1179 WORD32 ixheaacd_ltp_data(WORD32 object_type, ia_ics_info_struct *ics,
1180                          ltp_info *ltp, ia_bit_buf_struct *it_bit_buf,
1181                          WORD32 frame_len) {
1182   UWORD8 sfb, w;
1183 
1184   if (object_type == AOT_ER_AAC_LD) {
1185     ltp->lag_update = ixheaacd_read_bits_buf(it_bit_buf, 1);
1186 
1187     if (ltp->lag_update) {
1188       ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 10);
1189     }
1190   } else {
1191     ltp->lag = (UWORD16)ixheaacd_read_bits_buf(it_bit_buf, 11);
1192   }
1193 
1194   if (ltp->lag > (frame_len << 1)) return -1;
1195 
1196   ltp->coef = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 3);
1197 
1198   if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1199     for (w = 0; w < 8; w++) {
1200       if ((ltp->short_used[w] = ixheaacd_read_bits_buf(it_bit_buf, 1)) & 1) {
1201         ltp->short_lag_present[w] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1202         if (ltp->short_lag_present[w]) {
1203           ltp->short_lag[w] = (UWORD8)ixheaacd_read_bits_buf(it_bit_buf, 4);
1204         }
1205       }
1206     }
1207   } else {
1208     ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1209 
1210     for (sfb = 0; sfb < ltp->last_band; sfb++) {
1211       ltp->long_used[sfb] = ixheaacd_read_bits_buf(it_bit_buf, 1);
1212     }
1213   }
1214   if (ics->frame_length == 480) {
1215     if ((ics->sampling_rate_index > 5) &&
1216         (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_480))
1217       ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_480;
1218     else if ((ics->sampling_rate_index == 5) &&
1219              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_480))
1220       ltp->last_band = MAX_LTP_SFB_SR_FIVE_480;
1221     else if ((ics->sampling_rate_index < 5) &&
1222              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_480))
1223       ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_480;
1224   } else if (ics->frame_length == 512) {
1225     if ((ics->sampling_rate_index > 5) &&
1226         (ltp->last_band > MAX_LTP_SFB_SR_FIVE_PLUS_512))
1227       ltp->last_band = MAX_LTP_SFB_SR_FIVE_PLUS_512;
1228     else if ((ics->sampling_rate_index == 5) &&
1229              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_512))
1230       ltp->last_band = MAX_LTP_SFB_SR_FIVE_512;
1231     else if ((ics->sampling_rate_index < 5) &&
1232              (ltp->last_band > MAX_LTP_SFB_SR_FIVE_LESS_512))
1233       ltp->last_band = MAX_LTP_SFB_SR_FIVE_LESS_512;
1234   }
1235   return 0;
1236 }
1237