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