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