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
21 #include <string.h>
22 #include <assert.h>
23 #include "ixheaacd_sbr_common.h"
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_basic_ops.h"
30
31 #include "ixheaacd_basic_op.h"
32 #include "ixheaacd_intrinsics.h"
33 #include "ixheaacd_bitbuffer.h"
34
35 #include "ixheaacd_adts_crc_check.h"
36 #include "ixheaacd_error_codes.h"
37
ixheaacd_byte_align(ia_bit_buf_struct * it_bit_buff,WORD32 * align_bits_cnt)38 VOID ixheaacd_byte_align(ia_bit_buf_struct *it_bit_buff,
39 WORD32 *align_bits_cnt) {
40 WORD alignment;
41 alignment = (WORD)((*align_bits_cnt - it_bit_buff->cnt_bits) & 0x07);
42
43 if (alignment) {
44 ixheaacd_read_bits_buf(it_bit_buff, (8 - alignment));
45 }
46
47 *align_bits_cnt = it_bit_buff->cnt_bits;
48 }
49
ixheaacd_skip_bits_buf(ia_bit_buf_struct * it_bit_buff,WORD no_of_bits)50 WORD32 ixheaacd_skip_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
51 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
52 WORD bit_pos = it_bit_buff->bit_pos;
53
54 if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0)
55 longjmp(*(it_bit_buff->xaac_jmp_buf),
56 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
57 it_bit_buff->cnt_bits -= no_of_bits;
58
59 ptr_read_next += no_of_bits / 8;
60 bit_pos -= (no_of_bits % 8);
61 if (bit_pos < 0) {
62 bit_pos += 8;
63 ptr_read_next++;
64 }
65 assert(bit_pos >= 0 && bit_pos <= 7);
66
67 it_bit_buff->ptr_read_next = ptr_read_next;
68 it_bit_buff->bit_pos = (WORD16)bit_pos;
69 return no_of_bits;
70 }
71
ixheaacd_show_bits_buf(ia_bit_buf_struct * it_bit_buff,WORD no_of_bits)72 WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
73 UWORD32 ret_val;
74 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
75 WORD bit_pos = it_bit_buff->bit_pos;
76
77 if (no_of_bits == 0) {
78 return 0;
79 }
80
81 if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
82 no_of_bits > 25) {
83 longjmp(*(it_bit_buff->xaac_jmp_buf),
84 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
85 }
86
87 ret_val = (UWORD32)*ptr_read_next;
88
89 bit_pos -= no_of_bits;
90 while (bit_pos < -1) {
91 bit_pos += 8;
92 ptr_read_next++;
93
94 ret_val <<= 8;
95
96 ret_val |= (UWORD32)*ptr_read_next;
97 }
98
99 if (bit_pos == -1) {
100 bit_pos += 8;
101 ret_val <<= 8;
102 ptr_read_next++;
103 }
104
105 ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
106
107 return ret_val;
108 }
109
ixheaacd_read_bits_buf(ia_bit_buf_struct * it_bit_buff,WORD no_of_bits)110 WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
111 UWORD32 ret_val;
112 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
113 WORD bit_pos = it_bit_buff->bit_pos;
114
115 if (no_of_bits == 0) {
116 return 0;
117 }
118
119 if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
120 no_of_bits > 25) {
121 longjmp(*(it_bit_buff->xaac_jmp_buf),
122 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
123 }
124
125 it_bit_buff->cnt_bits -= no_of_bits;
126 ret_val = (UWORD32)*ptr_read_next;
127
128 bit_pos -= no_of_bits;
129 while (bit_pos < -1) {
130 bit_pos += 8;
131 ptr_read_next++;
132
133 ret_val <<= 8;
134
135 ret_val |= (UWORD32)*ptr_read_next;
136 }
137
138 if (bit_pos == -1) {
139 bit_pos += 8;
140 ret_val <<= 8;
141 ptr_read_next++;
142 }
143
144 ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
145 it_bit_buff->ptr_read_next = ptr_read_next;
146 it_bit_buff->bit_pos = (WORD16)bit_pos;
147 return ret_val;
148 }
149
ixheaacd_aac_read_byte(UWORD8 ** ptr_read_next,WORD32 * bit_pos,WORD32 * readword)150 VOID ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos,
151 WORD32 *readword) {
152 UWORD8 *v = *ptr_read_next;
153 WORD32 bits_consumed = *bit_pos;
154
155 if ((bits_consumed -= 8) >= 0) {
156 *readword = (*readword << 8) | *v;
157 v++;
158 } else {
159 bits_consumed += 8;
160 }
161 *bit_pos = bits_consumed;
162 *ptr_read_next = v;
163 return;
164 }
165
ixheaacd_aac_read_byte_corr1(UWORD8 ** ptr_read_next,WORD32 * ptr_bit_pos,WORD32 * readword,UWORD8 * p_bit_buf_end)166 VOID ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
167 WORD32 *readword, UWORD8 *p_bit_buf_end) {
168 UWORD8 *v = *ptr_read_next;
169 WORD32 bits_consumed = *ptr_bit_pos;
170 WORD32 temp_bit_count = 0;
171
172 while (bits_consumed >= 8) {
173 bits_consumed -= 8;
174 if ((p_bit_buf_end < v) && (p_bit_buf_end != 0))
175 temp_bit_count += 8;
176 else {
177 *readword = (*readword << 8) | *v;
178 v++;
179 }
180 }
181 *ptr_bit_pos = bits_consumed + temp_bit_count;
182 *ptr_read_next = v;
183 return;
184 }
185
ixheaacd_aac_read_byte_corr(UWORD8 ** ptr_read_next,WORD32 * ptr_bit_pos,WORD32 * readword,UWORD8 * p_bit_buf_end)186 VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
187 WORD32 *readword, UWORD8 *p_bit_buf_end) {
188 UWORD8 *v = *ptr_read_next;
189 WORD32 bits_consumed = *ptr_bit_pos;
190
191 if ((bits_consumed -= 8) >= 0) {
192 if (p_bit_buf_end < v)
193 bits_consumed += 8;
194 else {
195 *readword = (*readword << 8) | *v;
196 v++;
197 }
198 } else {
199 bits_consumed += 8;
200 }
201 *ptr_bit_pos = bits_consumed;
202 *ptr_read_next = v;
203 return;
204 }
205
ixheaacd_aac_read_bit(ia_bit_buf_struct * it_bit_buff)206 WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) {
207 UWORD8 ret_val;
208 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
209 WORD bit_pos = it_bit_buff->bit_pos;
210 UWORD32 temp;
211 WORD no_of_bits = 1;
212
213 if (bit_pos < 0) {
214 bit_pos = 7;
215 ptr_read_next--;
216 }
217
218 if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) {
219 longjmp(*(it_bit_buff->xaac_jmp_buf),
220 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
221 }
222
223 it_bit_buff->cnt_bits += no_of_bits;
224 ret_val = *ptr_read_next;
225 bit_pos -= no_of_bits;
226
227 temp = (ret_val << 24) << (bit_pos + no_of_bits);
228 it_bit_buff->ptr_read_next = ptr_read_next;
229 it_bit_buff->bit_pos = (WORD16)bit_pos;
230
231 return temp >> (32 - no_of_bits);
232 }
233
ixheaacd_aac_read_bit_rev(ia_bit_buf_struct * it_bit_buff)234 WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) {
235 UWORD8 ret_val;
236 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
237 WORD bit_pos = it_bit_buff->bit_pos;
238 UWORD32 temp;
239 WORD no_of_bits = 1;
240
241 if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) {
242 longjmp(*(it_bit_buff->xaac_jmp_buf),
243 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
244 }
245
246 if (bit_pos >= 8) {
247 bit_pos -= 8;
248 ptr_read_next++;
249 }
250
251 it_bit_buff->cnt_bits -= no_of_bits;
252 ret_val = *ptr_read_next;
253 bit_pos += no_of_bits;
254
255 temp = (ret_val << 24) << (bit_pos - no_of_bits);
256 it_bit_buff->ptr_read_next = ptr_read_next;
257 it_bit_buff->bit_pos = (WORD16)bit_pos;
258
259 return temp >> (32 - no_of_bits);
260 }
261
ixheaacd_write_bit(ia_bit_buf_struct * it_bit_buff,WORD32 value,WORD32 no_of_bits)262 VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
263 WORD32 no_of_bits)
264
265 {
266 WORD32 mask;
267
268 if (no_of_bits == 0) return;
269
270 mask = 0x1;
271 mask <<= no_of_bits - 1;
272
273 it_bit_buff->bit_count += no_of_bits;
274
275 while (no_of_bits > 0) {
276 while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) {
277 it_bit_buff->byte <<= 1;
278 if (value & mask) it_bit_buff->byte |= 0x1;
279 value <<= 1;
280 no_of_bits--;
281 it_bit_buff->valid_bits++;
282 }
283 if (it_bit_buff->valid_bits == 8) {
284 *it_bit_buff->byte_ptr++ = it_bit_buff->byte;
285 it_bit_buff->byte = 0;
286 it_bit_buff->valid_bits = 0;
287 }
288 }
289 }
290
ixheaacd_read_bit(ia_bit_buf_struct * it_bit_buff,WORD32 no_of_bits)291 WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) {
292 UWORD32 ret_val;
293 UWORD8 *ptr_read_next = it_bit_buff->byte_ptr;
294
295 if (no_of_bits == 0) {
296 return 0;
297 }
298
299 ret_val =
300 ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL);
301 it_bit_buff->byte_ptr += (no_of_bits >> 3);
302
303 if (it_bit_buff->valid_bits != 8) {
304 UWORD8 *v = it_bit_buff->byte_ptr;
305 ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) |
306 (*v >> it_bit_buff->valid_bits);
307 }
308
309 it_bit_buff->valid_bits -= (no_of_bits % 8);
310
311 ret_val = ret_val >> (32 - no_of_bits);
312
313 return ret_val;
314 }
315