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_bitbuffer.h"
29 #include "ixheaacd_defines.h"
30 #include "ixheaacd_aac_rom.h"
31 #include "ixheaacd_pulsedata.h"
32 
33 #include "ixheaacd_pns.h"
34 #include "ixheaacd_drc_data_struct.h"
35 
36 #include "ixheaacd_lt_predict.h"
37 
38 #include "ixheaacd_channelinfo.h"
39 #include "ixheaacd_drc_dec.h"
40 #include "ixheaacd_sbrdecoder.h"
41 
42 #include "ixheaacd_audioobjtypes.h"
43 #include "ixheaacd_sbrdecsettings.h"
44 #include "ixheaacd_memory_standards.h"
45 #include "ixheaacd_error_codes.h"
46 
47 #include "ixheaacd_defines.h"
48 
49 #include "ixheaacd_sbr_scale.h"
50 #include "ixheaacd_lpp_tran.h"
51 #include "ixheaacd_env_extr_part.h"
52 #include "ixheaacd_sbr_rom.h"
53 
54 #include "ixheaacd_hybrid.h"
55 #include "ixheaacd_ps_dec.h"
56 #include "ixheaacd_ps_bitdec.h"
57 
58 #include "ixheaacd_pulsedata.h"
59 
60 #include "ixheaacd_env_extr.h"
61 #include "ixheaacd_common_rom.h"
62 #include "ixheaacd_block.h"
63 #include "ixheaacd_channel.h"
64 
65 #include "ixheaacd_audioobjtypes.h"
66 #include "ixheaacd_latmdemux.h"
67 
68 #include "ixheaacd_aacdec.h"
69 #include "ixheaacd_mps_polyphase.h"
70 #include "ixheaacd_config.h"
71 #include "ixheaacd_mps_dec.h"
72 #include "ixheaacd_struct_def.h"
73 #include "ixheaacd_headerdecode.h"
74 
75 #include "ixheaacd_multichannel.h"
76 
77 #include "ixheaacd_basic_op.h"
78 #include "ixheaacd_adts_crc_check.h"
79 #include "ixheaacd_function_selector.h"
80 
ixheaacd_allocate_mem_persistent(ia_exhaacplus_dec_api_struct * p_obj_enhaacplus_dec,ia_aac_dec_state_struct * p_state_enhaacplus_dec,WORD channels,WORD * persistent_used_total,WORD * sbr_persistent_start,WORD ps_enable)81 VOID ixheaacd_allocate_mem_persistent(
82     ia_exhaacplus_dec_api_struct *p_obj_enhaacplus_dec,
83     ia_aac_dec_state_struct *p_state_enhaacplus_dec, WORD channels,
84     WORD *persistent_used_total, WORD *sbr_persistent_start, WORD ps_enable) {
85   WORD persistent_used;
86   WORD8 **temp_persistent =
87       (WORD8 **)&(p_state_enhaacplus_dec->aac_persistent_mem_v);
88   *temp_persistent += *persistent_used_total;
89 
90   persistent_used = ixheaacd_set_aac_persistent_buffers(
91       p_state_enhaacplus_dec->aac_persistent_mem_v, channels);
92 
93   *persistent_used_total += persistent_used;
94 
95   *sbr_persistent_start = *persistent_used_total;
96 
97   p_state_enhaacplus_dec->sbr_persistent_mem_v =
98       (pVOID)((SIZE_T)((pWORD8)p_state_enhaacplus_dec->aac_persistent_mem_v +
99                        persistent_used + sizeof(SIZE_T) - 1) &
100               (SIZE_T)(~(sizeof(SIZE_T) - 1)));
101 
102   persistent_used = ixheaacd_getsize_sbr_persistent();
103 
104   ixheaacd_set_sbr_persistent_buffers(
105       p_state_enhaacplus_dec->sbr_persistent_mem_v, &persistent_used, channels,
106       ps_enable);
107 
108   *persistent_used_total += persistent_used;
109 
110   {
111     struct ia_aac_persistent_struct *aac_persistent_mem =
112         (struct ia_aac_persistent_struct *)
113             p_obj_enhaacplus_dec->p_state_aac->aac_persistent_mem_v;
114     aac_persistent_mem->str_aac_decoder.pstr_aac_tables =
115         &p_obj_enhaacplus_dec->aac_tables;
116     aac_persistent_mem->str_aac_decoder.pstr_common_tables =
117         p_obj_enhaacplus_dec->common_tables;
118   }
119 
120   ixheaacd_set_sbr_persistent_table_pointer(
121       p_obj_enhaacplus_dec->p_state_aac->sbr_persistent_mem_v,
122       &p_obj_enhaacplus_dec->str_sbr_tables,
123       p_obj_enhaacplus_dec->common_tables);
124 }
125 
ixheaacd_create_bit_buf(ia_bit_buf_struct * it_bit_buff,UWORD8 * ptr_bit_buf_base,WORD32 bit_buf_size)126 ia_bit_buf_struct *ixheaacd_create_bit_buf(ia_bit_buf_struct *it_bit_buff,
127                                            UWORD8 *ptr_bit_buf_base,
128                                            WORD32 bit_buf_size) {
129   it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base;
130   it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1;
131 
132   it_bit_buff->ptr_read_next = ptr_bit_buf_base;
133   it_bit_buff->bit_pos = 7;
134 
135   it_bit_buff->cnt_bits = 0;
136   it_bit_buff->size = bit_buf_size << 3;
137 
138   it_bit_buff->adts_header_present = 0;
139   it_bit_buff->protection_absent = 0;
140   it_bit_buff->pstr_adts_crc_info = &it_bit_buff->str_adts_crc_info;
141 
142   it_bit_buff->max_size = it_bit_buff->size;
143 
144   ixheaacd_adts_crc_open(it_bit_buff->pstr_adts_crc_info);
145 
146   return it_bit_buff;
147 }
148 
ixheaacd_create_init_bit_buf(ia_bit_buf_struct * it_bit_buff,UWORD8 * ptr_bit_buf_base,WORD32 bit_buf_size)149 VOID ixheaacd_create_init_bit_buf(ia_bit_buf_struct *it_bit_buff,
150                                   UWORD8 *ptr_bit_buf_base,
151                                   WORD32 bit_buf_size) {
152   ixheaacd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size);
153   it_bit_buff->cnt_bits = (bit_buf_size << 3);
154   return;
155 }
156 
ixheaacd_read_bidirection(ia_bit_buf_struct * it_bit_buff,WORD32 ixheaacd_drc_offset)157 VOID ixheaacd_read_bidirection(ia_bit_buf_struct *it_bit_buff,
158                                WORD32 ixheaacd_drc_offset) {
159   if (ixheaacd_drc_offset != 0) {
160     WORD32 byte_offset;
161     if ((it_bit_buff->cnt_bits < 0) ||
162         (it_bit_buff->cnt_bits - ixheaacd_drc_offset < 0) ||
163         (it_bit_buff->cnt_bits - ixheaacd_drc_offset > it_bit_buff->size)) {
164       longjmp(*(it_bit_buff->xaac_jmp_buf),
165               IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
166     }
167     it_bit_buff->cnt_bits = it_bit_buff->cnt_bits - ixheaacd_drc_offset;
168     it_bit_buff->bit_pos = it_bit_buff->bit_pos - ixheaacd_drc_offset;
169     byte_offset = it_bit_buff->bit_pos >> 3;
170     it_bit_buff->bit_pos = it_bit_buff->bit_pos - (byte_offset << 3);
171 
172     if (byte_offset) {
173       UWORD8 *ptr_read_next;
174 
175       ptr_read_next = it_bit_buff->ptr_read_next;
176 
177       ptr_read_next = ptr_read_next - (byte_offset);
178 
179       it_bit_buff->ptr_read_next = ptr_read_next;
180     }
181   }
182 }
183