xref: /aosp_15_r20/external/libxaac/decoder/ixheaacd_bitbuffer.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 "ixheaac_type_def.h"
25 #include "ixheaac_constants.h"
26 #include "ixheaac_basic_ops32.h"
27 #include "ixheaac_basic_ops16.h"
28 #include "ixheaac_basic_ops40.h"
29 #include "ixheaac_basic_ops.h"
30 
31 #include "ixheaac_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_XHEAAC_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_XHEAAC_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_XHEAAC_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 
182   if (bits_consumed > (31 - temp_bit_count)) {
183     if ((p_bit_buf_end != NULL) && (p_bit_buf_end < v)) {
184       bits_consumed = 31 - temp_bit_count;
185     }
186   }
187 
188   *ptr_bit_pos = bits_consumed + temp_bit_count;
189   *ptr_read_next = v;
190   return;
191 }
192 
ixheaacd_aac_read_byte_corr(UWORD8 ** ptr_read_next,WORD32 * ptr_bit_pos,WORD32 * readword,UWORD8 * p_bit_buf_end)193 VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
194                                  WORD32 *readword, UWORD8 *p_bit_buf_end) {
195   UWORD8 *v = *ptr_read_next;
196   WORD32 bits_consumed = *ptr_bit_pos;
197 
198   if ((bits_consumed -= 8) >= 0) {
199     if (p_bit_buf_end < v)
200       bits_consumed += 8;
201     else {
202       *readword = (*readword << 8) | *v;
203       v++;
204     }
205   } else {
206     bits_consumed += 8;
207   }
208 
209   if (bits_consumed > 31) {
210     if (p_bit_buf_end < v) {
211       bits_consumed = 31;
212     }
213   }
214 
215   *ptr_bit_pos = bits_consumed;
216   *ptr_read_next = v;
217   return;
218 }
219 
ixheaacd_aac_read_bit(ia_bit_buf_struct * it_bit_buff)220 WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) {
221   UWORD8 ret_val;
222   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
223   WORD bit_pos = it_bit_buff->bit_pos;
224   UWORD32 temp;
225   WORD no_of_bits = 1;
226 
227   if (bit_pos < 0) {
228     bit_pos = 7;
229     ptr_read_next--;
230   }
231 
232   if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) {
233     longjmp(*(it_bit_buff->xaac_jmp_buf),
234             IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
235   }
236 
237   it_bit_buff->cnt_bits += no_of_bits;
238   ret_val = *ptr_read_next;
239   bit_pos -= no_of_bits;
240 
241   temp = (ret_val << 24) << (bit_pos + no_of_bits);
242   it_bit_buff->ptr_read_next = ptr_read_next;
243   it_bit_buff->bit_pos = (WORD16)bit_pos;
244 
245   return temp >> (32 - no_of_bits);
246 }
247 
ixheaacd_aac_read_bit_rev(ia_bit_buf_struct * it_bit_buff)248 WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) {
249   UWORD8 ret_val;
250   UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
251   WORD bit_pos = it_bit_buff->bit_pos;
252   UWORD32 temp;
253   WORD no_of_bits = 1;
254 
255   if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) {
256     longjmp(*(it_bit_buff->xaac_jmp_buf),
257             IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
258   }
259 
260   if (bit_pos >= 8) {
261     bit_pos -= 8;
262     ptr_read_next++;
263   }
264 
265   it_bit_buff->cnt_bits -= no_of_bits;
266   ret_val = *ptr_read_next;
267   bit_pos += no_of_bits;
268 
269   temp = (ret_val << 24) << (bit_pos - no_of_bits);
270   it_bit_buff->ptr_read_next = ptr_read_next;
271   it_bit_buff->bit_pos = (WORD16)bit_pos;
272 
273   return temp >> (32 - no_of_bits);
274 }
275 
ixheaacd_write_bit(ia_bit_buf_struct * it_bit_buff,WORD32 value,WORD32 no_of_bits)276 VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
277                         WORD32 no_of_bits)
278 
279 {
280   WORD32 mask;
281 
282   if (no_of_bits == 0) return;
283 
284   mask = 0x1;
285   mask <<= no_of_bits - 1;
286 
287   it_bit_buff->bit_count += no_of_bits;
288 
289   while (no_of_bits > 0) {
290     while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) {
291       it_bit_buff->byte <<= 1;
292       if (value & mask) it_bit_buff->byte |= 0x1;
293       value <<= 1;
294       no_of_bits--;
295       it_bit_buff->valid_bits++;
296     }
297     if (it_bit_buff->valid_bits == 8) {
298       *it_bit_buff->byte_ptr++ = it_bit_buff->byte;
299       it_bit_buff->byte = 0;
300       it_bit_buff->valid_bits = 0;
301     }
302   }
303 }
304 
ixheaacd_read_bit(ia_bit_buf_struct * it_bit_buff,WORD32 no_of_bits)305 WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) {
306   UWORD32 ret_val;
307   UWORD8 *ptr_read_next = it_bit_buff->byte_ptr;
308 
309   if (no_of_bits == 0) {
310     return 0;
311   }
312 
313   ret_val =
314       ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL);
315   it_bit_buff->byte_ptr += (no_of_bits >> 3);
316 
317   if (it_bit_buff->valid_bits != 8) {
318     UWORD8 *v = it_bit_buff->byte_ptr;
319     ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) |
320               (*v >> it_bit_buff->valid_bits);
321   }
322 
323   it_bit_buff->valid_bits -= (no_of_bits % 8);
324 
325   ret_val = ret_val >> (32 - no_of_bits);
326 
327   return ret_val;
328 }
329