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