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 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 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 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 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 150 UWORD32 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 1; 164 } 165 166 UWORD32 ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, 167 WORD32 *ptr_bit_pos, WORD32 *readword, 168 UWORD8 *p_bit_buf_end) { 169 UWORD8 *v = *ptr_read_next; 170 WORD32 bits_consumed = *ptr_bit_pos; 171 WORD32 temp_bit_count = 0; 172 173 while (bits_consumed >= 8) { 174 bits_consumed -= 8; 175 if ((p_bit_buf_end < v) && (p_bit_buf_end != 0)) 176 temp_bit_count += 8; 177 else { 178 *readword = (*readword << 8) | *v; 179 v++; 180 } 181 } 182 *ptr_bit_pos = bits_consumed + temp_bit_count; 183 *ptr_read_next = v; 184 return 1; 185 } 186 187 UWORD32 ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos, 188 WORD32 *readword, UWORD8 *p_bit_buf_end) { 189 UWORD8 *v = *ptr_read_next; 190 WORD32 bits_consumed = *ptr_bit_pos; 191 192 if ((bits_consumed -= 8) >= 0) { 193 if (p_bit_buf_end < v) 194 bits_consumed += 8; 195 else { 196 *readword = (*readword << 8) | *v; 197 v++; 198 } 199 } else { 200 bits_consumed += 8; 201 } 202 *ptr_bit_pos = bits_consumed; 203 *ptr_read_next = v; 204 return 1; 205 } 206 207 WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) { 208 UWORD8 ret_val; 209 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; 210 WORD bit_pos = it_bit_buff->bit_pos; 211 UWORD32 temp; 212 WORD no_of_bits = 1; 213 214 if (bit_pos < 0) { 215 bit_pos = 7; 216 ptr_read_next--; 217 } 218 219 if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) { 220 longjmp(*(it_bit_buff->xaac_jmp_buf), 221 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); 222 } 223 224 it_bit_buff->cnt_bits += no_of_bits; 225 ret_val = *ptr_read_next; 226 bit_pos -= no_of_bits; 227 228 temp = (ret_val << 24) << (bit_pos + no_of_bits); 229 it_bit_buff->ptr_read_next = ptr_read_next; 230 it_bit_buff->bit_pos = (WORD16)bit_pos; 231 232 return temp >> (32 - no_of_bits); 233 } 234 235 WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) { 236 UWORD8 ret_val; 237 UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next; 238 WORD bit_pos = it_bit_buff->bit_pos; 239 UWORD32 temp; 240 WORD no_of_bits = 1; 241 242 if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) { 243 longjmp(*(it_bit_buff->xaac_jmp_buf), 244 IA_ENHAACPLUS_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES); 245 } 246 247 if (bit_pos >= 8) { 248 bit_pos -= 8; 249 ptr_read_next++; 250 } 251 252 it_bit_buff->cnt_bits -= no_of_bits; 253 ret_val = *ptr_read_next; 254 bit_pos += no_of_bits; 255 256 temp = (ret_val << 24) << (bit_pos - no_of_bits); 257 it_bit_buff->ptr_read_next = ptr_read_next; 258 it_bit_buff->bit_pos = (WORD16)bit_pos; 259 260 return temp >> (32 - no_of_bits); 261 } 262 263 VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value, 264 WORD32 no_of_bits) 265 266 { 267 WORD32 mask; 268 269 if (no_of_bits == 0) return; 270 271 mask = 0x1; 272 mask <<= no_of_bits - 1; 273 274 it_bit_buff->bit_count += no_of_bits; 275 276 while (no_of_bits > 0) { 277 while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) { 278 it_bit_buff->byte <<= 1; 279 if (value & mask) it_bit_buff->byte |= 0x1; 280 value <<= 1; 281 no_of_bits--; 282 it_bit_buff->valid_bits++; 283 } 284 if (it_bit_buff->valid_bits == 8) { 285 *it_bit_buff->byte_ptr++ = it_bit_buff->byte; 286 it_bit_buff->byte = 0; 287 it_bit_buff->valid_bits = 0; 288 } 289 } 290 } 291 292 WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) { 293 UWORD32 ret_val; 294 UWORD8 *ptr_read_next = it_bit_buff->byte_ptr; 295 296 if (no_of_bits == 0) { 297 return 0; 298 } 299 300 ret_val = 301 ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL); 302 it_bit_buff->byte_ptr += (no_of_bits >> 3); 303 304 if (it_bit_buff->valid_bits != 8) { 305 UWORD8 *v = it_bit_buff->byte_ptr; 306 ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) | 307 (*v >> it_bit_buff->valid_bits); 308 } 309 310 it_bit_buff->valid_bits -= (no_of_bits % 8); 311 312 ret_val = ret_val >> (32 - no_of_bits); 313 314 return ret_val; 315 } 316