Coverage Report

Created: 2026-06-13 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxaac/decoder/ixheaacd_bitbuffer.c
Line
Count
Source
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
38
VOID ixheaacd_byte_align(ia_bit_buf_struct *it_bit_buff,
39
123k
                         WORD32 *align_bits_cnt) {
40
123k
  WORD alignment;
41
123k
  alignment = (WORD)((*align_bits_cnt - it_bit_buff->cnt_bits) & 0x07);
42
43
123k
  if (alignment) {
44
47.5k
    ixheaacd_read_bits_buf(it_bit_buff, (8 - alignment));
45
47.5k
  }
46
47
123k
  *align_bits_cnt = it_bit_buff->cnt_bits;
48
123k
}
49
50
4.44G
WORD32 ixheaacd_skip_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
51
4.44G
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
52
4.44G
  WORD bit_pos = it_bit_buff->bit_pos;
53
54
4.44G
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0)
55
102
    longjmp(*(it_bit_buff->xaac_jmp_buf),
56
102
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
57
4.44G
  it_bit_buff->cnt_bits -= no_of_bits;
58
59
4.44G
  ptr_read_next += no_of_bits / 8;
60
4.44G
  bit_pos -= (no_of_bits % 8);
61
4.44G
  if (bit_pos < 0) {
62
555M
    bit_pos += 8;
63
555M
    ptr_read_next++;
64
555M
  }
65
4.44G
  assert(bit_pos >= 0 && bit_pos <= 7);
66
67
4.44G
  it_bit_buff->ptr_read_next = ptr_read_next;
68
4.44G
  it_bit_buff->bit_pos = (WORD16)bit_pos;
69
4.44G
  return no_of_bits;
70
4.44G
}
71
72
4.42M
WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
73
4.42M
  UWORD32 ret_val;
74
4.42M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
75
4.42M
  WORD bit_pos = it_bit_buff->bit_pos;
76
77
4.42M
  if (no_of_bits == 0) {
78
106
    return 0;
79
106
  }
80
81
4.42M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
82
4.42M
      no_of_bits > 25) {
83
3
    longjmp(*(it_bit_buff->xaac_jmp_buf),
84
3
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
85
3
  }
86
87
4.42M
  ret_val = (UWORD32)*ptr_read_next;
88
89
4.42M
  bit_pos -= no_of_bits;
90
15.1M
  while (bit_pos < -1) {
91
10.7M
    bit_pos += 8;
92
10.7M
    ptr_read_next++;
93
94
10.7M
    ret_val <<= 8;
95
96
10.7M
    ret_val |= (UWORD32)*ptr_read_next;
97
10.7M
  }
98
99
4.42M
  if (bit_pos == -1) {
100
555k
    bit_pos += 8;
101
555k
    ret_val <<= 8;
102
555k
    ptr_read_next++;
103
555k
  }
104
105
4.42M
  ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
106
107
4.42M
  return ret_val;
108
4.42M
}
109
110
345M
WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
111
345M
  UWORD32 ret_val;
112
345M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
113
345M
  WORD bit_pos = it_bit_buff->bit_pos;
114
115
345M
  if (no_of_bits == 0) {
116
25.5M
    return 0;
117
25.5M
  }
118
119
319M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
120
319M
      no_of_bits > 25) {
121
3.13k
    longjmp(*(it_bit_buff->xaac_jmp_buf),
122
3.13k
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
123
3.13k
  }
124
125
319M
  it_bit_buff->cnt_bits -= no_of_bits;
126
319M
  ret_val = (UWORD32)*ptr_read_next;
127
128
319M
  bit_pos -= no_of_bits;
129
367M
  while (bit_pos < -1) {
130
48.1M
    bit_pos += 8;
131
48.1M
    ptr_read_next++;
132
133
48.1M
    ret_val <<= 8;
134
135
48.1M
    ret_val |= (UWORD32)*ptr_read_next;
136
48.1M
  }
137
138
319M
  if (bit_pos == -1) {
139
43.7M
    bit_pos += 8;
140
43.7M
    ret_val <<= 8;
141
43.7M
    ptr_read_next++;
142
43.7M
  }
143
144
319M
  ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
145
319M
  it_bit_buff->ptr_read_next = ptr_read_next;
146
319M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
147
319M
  return ret_val;
148
319M
}
149
150
VOID ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos,
151
2.76k
                            WORD32 *readword) {
152
2.76k
  UWORD8 *v = *ptr_read_next;
153
2.76k
  WORD32 bits_consumed = *bit_pos;
154
155
2.76k
  if ((bits_consumed -= 8) >= 0) {
156
593
    *readword = (*readword << 8) | *v;
157
593
    v++;
158
2.17k
  } else {
159
2.17k
    bits_consumed += 8;
160
2.17k
  }
161
2.76k
  *bit_pos = bits_consumed;
162
2.76k
  *ptr_read_next = v;
163
2.76k
  return;
164
2.76k
}
165
166
VOID ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
167
7.07M
                                  WORD32 *readword, UWORD8 *p_bit_buf_end) {
168
7.07M
  UWORD8 *v = *ptr_read_next;
169
7.07M
  WORD32 bits_consumed = *ptr_bit_pos;
170
7.07M
  WORD32 temp_bit_count = 0;
171
172
9.69M
  while (bits_consumed >= 8) {
173
2.61M
    bits_consumed -= 8;
174
2.61M
    if ((p_bit_buf_end < v) && (p_bit_buf_end != 0))
175
38.8k
      temp_bit_count += 8;
176
2.58M
    else {
177
2.58M
      *readword = (*readword << 8) | *v;
178
2.58M
      v++;
179
2.58M
    }
180
2.61M
  }
181
182
7.07M
  if (bits_consumed > (31 - temp_bit_count)) {
183
5.15k
    if ((p_bit_buf_end != NULL) && (p_bit_buf_end < v)) {
184
5.15k
      bits_consumed = 31 - temp_bit_count;
185
5.15k
    }
186
5.15k
  }
187
188
7.07M
  *ptr_bit_pos = bits_consumed + temp_bit_count;
189
7.07M
  *ptr_read_next = v;
190
7.07M
  return;
191
7.07M
}
192
193
VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
194
24.1M
                                 WORD32 *readword, UWORD8 *p_bit_buf_end) {
195
24.1M
  UWORD8 *v = *ptr_read_next;
196
24.1M
  WORD32 bits_consumed = *ptr_bit_pos;
197
198
24.1M
  if ((bits_consumed -= 8) >= 0) {
199
6.27M
    if (p_bit_buf_end < v)
200
255k
      bits_consumed += 8;
201
6.02M
    else {
202
6.02M
      *readword = (*readword << 8) | *v;
203
6.02M
      v++;
204
6.02M
    }
205
17.8M
  } else {
206
17.8M
    bits_consumed += 8;
207
17.8M
  }
208
209
24.1M
  if (bits_consumed > 31) {
210
164k
    if (p_bit_buf_end < v) {
211
164k
      bits_consumed = 31;
212
164k
    }
213
164k
  }
214
215
24.1M
  *ptr_bit_pos = bits_consumed;
216
24.1M
  *ptr_read_next = v;
217
24.1M
  return;
218
24.1M
}
219
220
10.3M
WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) {
221
10.3M
  UWORD8 ret_val;
222
10.3M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
223
10.3M
  WORD bit_pos = it_bit_buff->bit_pos;
224
10.3M
  UWORD32 temp;
225
10.3M
  WORD no_of_bits = 1;
226
227
10.3M
  if (bit_pos < 0) {
228
1.06M
    bit_pos = 7;
229
1.06M
    ptr_read_next--;
230
1.06M
  }
231
232
10.3M
  if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) {
233
155
    longjmp(*(it_bit_buff->xaac_jmp_buf),
234
155
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
235
155
  }
236
237
10.3M
  it_bit_buff->cnt_bits += no_of_bits;
238
10.3M
  ret_val = *ptr_read_next;
239
10.3M
  bit_pos -= no_of_bits;
240
241
10.3M
  temp = (ret_val << 24) << (bit_pos + no_of_bits);
242
10.3M
  it_bit_buff->ptr_read_next = ptr_read_next;
243
10.3M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
244
245
10.3M
  return temp >> (32 - no_of_bits);
246
10.3M
}
247
248
8.73M
WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) {
249
8.73M
  UWORD8 ret_val;
250
8.73M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
251
8.73M
  WORD bit_pos = it_bit_buff->bit_pos;
252
8.73M
  UWORD32 temp;
253
8.73M
  WORD no_of_bits = 1;
254
255
8.73M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) {
256
56
    longjmp(*(it_bit_buff->xaac_jmp_buf),
257
56
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
258
56
  }
259
260
8.73M
  if (bit_pos >= 8) {
261
447k
    bit_pos -= 8;
262
447k
    ptr_read_next++;
263
447k
  }
264
265
8.73M
  it_bit_buff->cnt_bits -= no_of_bits;
266
8.73M
  ret_val = *ptr_read_next;
267
8.73M
  bit_pos += no_of_bits;
268
269
8.73M
  temp = (ret_val << 24) << (bit_pos - no_of_bits);
270
8.73M
  it_bit_buff->ptr_read_next = ptr_read_next;
271
8.73M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
272
273
8.73M
  return temp >> (32 - no_of_bits);
274
8.73M
}
275
276
VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
277
                        WORD32 no_of_bits)
278
279
14.5M
{
280
14.5M
  WORD32 mask;
281
282
14.5M
  if (no_of_bits == 0) return;
283
284
14.2M
  mask = 0x1;
285
14.2M
  mask <<= no_of_bits - 1;
286
287
14.2M
  it_bit_buff->bit_count += no_of_bits;
288
289
30.6M
  while (no_of_bits > 0) {
290
51.1M
    while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) {
291
34.8M
      it_bit_buff->byte <<= 1;
292
34.8M
      if (value & mask) it_bit_buff->byte |= 0x1;
293
34.8M
      value <<= 1;
294
34.8M
      no_of_bits--;
295
34.8M
      it_bit_buff->valid_bits++;
296
34.8M
    }
297
16.3M
    if (it_bit_buff->valid_bits == 8) {
298
4.35M
      *it_bit_buff->byte_ptr++ = it_bit_buff->byte;
299
4.35M
      it_bit_buff->byte = 0;
300
4.35M
      it_bit_buff->valid_bits = 0;
301
4.35M
    }
302
16.3M
  }
303
14.2M
}
304
305
477k
WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) {
306
477k
  UWORD32 ret_val;
307
477k
  UWORD8 *ptr_read_next = it_bit_buff->byte_ptr;
308
309
477k
  if (no_of_bits == 0) {
310
227k
    return 0;
311
227k
  }
312
313
249k
  ret_val =
314
249k
      ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL);
315
249k
  it_bit_buff->byte_ptr += (no_of_bits >> 3);
316
317
249k
  if (it_bit_buff->valid_bits != 8) {
318
11.3k
    UWORD8 *v = it_bit_buff->byte_ptr;
319
11.3k
    ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) |
320
11.3k
              (*v >> it_bit_buff->valid_bits);
321
11.3k
  }
322
323
249k
  it_bit_buff->valid_bits -= (no_of_bits % 8);
324
325
249k
  ret_val = ret_val >> (32 - no_of_bits);
326
327
249k
  return ret_val;
328
477k
}