Coverage Report

Created: 2026-10-14 07:16

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
197k
                         WORD32 *align_bits_cnt) {
40
197k
  WORD alignment;
41
197k
  alignment = (WORD)((*align_bits_cnt - it_bit_buff->cnt_bits) & 0x07);
42
43
197k
  if (alignment) {
44
74.8k
    ixheaacd_read_bits_buf(it_bit_buff, (8 - alignment));
45
74.8k
  }
46
47
197k
  *align_bits_cnt = it_bit_buff->cnt_bits;
48
197k
}
49
50
5.86G
WORD32 ixheaacd_skip_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
51
5.86G
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
52
5.86G
  WORD bit_pos = it_bit_buff->bit_pos;
53
54
5.86G
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0)
55
230
    longjmp(*(it_bit_buff->xaac_jmp_buf),
56
230
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
57
5.86G
  it_bit_buff->cnt_bits -= no_of_bits;
58
59
5.86G
  ptr_read_next += no_of_bits / 8;
60
5.86G
  bit_pos -= (no_of_bits % 8);
61
5.86G
  if (bit_pos < 0) {
62
733M
    bit_pos += 8;
63
733M
    ptr_read_next++;
64
733M
  }
65
5.86G
  assert(bit_pos >= 0 && bit_pos <= 7);
66
67
5.86G
  it_bit_buff->ptr_read_next = ptr_read_next;
68
5.86G
  it_bit_buff->bit_pos = (WORD16)bit_pos;
69
5.86G
  return no_of_bits;
70
5.86G
}
71
72
5.51M
WORD32 ixheaacd_show_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
73
5.51M
  UWORD32 ret_val;
74
5.51M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
75
5.51M
  WORD bit_pos = it_bit_buff->bit_pos;
76
77
5.51M
  if (no_of_bits == 0) {
78
193
    return 0;
79
193
  }
80
81
5.51M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
82
5.51M
      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
5.51M
  ret_val = (UWORD32)*ptr_read_next;
88
89
5.51M
  bit_pos -= no_of_bits;
90
18.8M
  while (bit_pos < -1) {
91
13.3M
    bit_pos += 8;
92
13.3M
    ptr_read_next++;
93
94
13.3M
    ret_val <<= 8;
95
96
13.3M
    ret_val |= (UWORD32)*ptr_read_next;
97
13.3M
  }
98
99
5.51M
  if (bit_pos == -1) {
100
644k
    bit_pos += 8;
101
644k
    ret_val <<= 8;
102
644k
    ptr_read_next++;
103
644k
  }
104
105
5.51M
  ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
106
107
5.51M
  return ret_val;
108
5.51M
}
109
110
403M
WORD32 ixheaacd_read_bits_buf(ia_bit_buf_struct *it_bit_buff, WORD no_of_bits) {
111
403M
  UWORD32 ret_val;
112
403M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
113
403M
  WORD bit_pos = it_bit_buff->bit_pos;
114
115
403M
  if (no_of_bits == 0) {
116
34.6M
    return 0;
117
34.6M
  }
118
119
368M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0 ||
120
368M
      no_of_bits > 25) {
121
5.10k
    longjmp(*(it_bit_buff->xaac_jmp_buf),
122
5.10k
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
123
5.10k
  }
124
125
368M
  it_bit_buff->cnt_bits -= no_of_bits;
126
368M
  ret_val = (UWORD32)*ptr_read_next;
127
128
368M
  bit_pos -= no_of_bits;
129
429M
  while (bit_pos < -1) {
130
61.4M
    bit_pos += 8;
131
61.4M
    ptr_read_next++;
132
133
61.4M
    ret_val <<= 8;
134
135
61.4M
    ret_val |= (UWORD32)*ptr_read_next;
136
61.4M
  }
137
138
368M
  if (bit_pos == -1) {
139
48.0M
    bit_pos += 8;
140
48.0M
    ret_val <<= 8;
141
48.0M
    ptr_read_next++;
142
48.0M
  }
143
144
368M
  ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
145
368M
  it_bit_buff->ptr_read_next = ptr_read_next;
146
368M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
147
368M
  return ret_val;
148
368M
}
149
150
VOID ixheaacd_aac_read_byte(UWORD8 **ptr_read_next, WORD32 *bit_pos,
151
3.60k
                            WORD32 *readword) {
152
3.60k
  UWORD8 *v = *ptr_read_next;
153
3.60k
  WORD32 bits_consumed = *bit_pos;
154
155
3.60k
  if ((bits_consumed -= 8) >= 0) {
156
814
    *readword = (*readword << 8) | *v;
157
814
    v++;
158
2.79k
  } else {
159
2.79k
    bits_consumed += 8;
160
2.79k
  }
161
3.60k
  *bit_pos = bits_consumed;
162
3.60k
  *ptr_read_next = v;
163
3.60k
  return;
164
3.60k
}
165
166
VOID ixheaacd_aac_read_byte_corr1(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
167
9.96M
                                  WORD32 *readword, UWORD8 *p_bit_buf_end) {
168
9.96M
  UWORD8 *v = *ptr_read_next;
169
9.96M
  WORD32 bits_consumed = *ptr_bit_pos;
170
9.96M
  WORD32 temp_bit_count = 0;
171
172
13.8M
  while (bits_consumed >= 8) {
173
3.90M
    bits_consumed -= 8;
174
3.90M
    if ((p_bit_buf_end < v) && (p_bit_buf_end != 0))
175
80.0k
      temp_bit_count += 8;
176
3.82M
    else {
177
3.82M
      *readword = (*readword << 8) | *v;
178
3.82M
      v++;
179
3.82M
    }
180
3.90M
  }
181
182
9.96M
  if (bits_consumed > (31 - temp_bit_count)) {
183
12.1k
    if ((p_bit_buf_end != NULL) && (p_bit_buf_end < v)) {
184
12.1k
      bits_consumed = 31 - temp_bit_count;
185
12.1k
    }
186
12.1k
  }
187
188
9.96M
  *ptr_bit_pos = bits_consumed + temp_bit_count;
189
9.96M
  *ptr_read_next = v;
190
9.96M
  return;
191
9.96M
}
192
193
VOID ixheaacd_aac_read_byte_corr(UWORD8 **ptr_read_next, WORD32 *ptr_bit_pos,
194
36.0M
                                 WORD32 *readword, UWORD8 *p_bit_buf_end) {
195
36.0M
  UWORD8 *v = *ptr_read_next;
196
36.0M
  WORD32 bits_consumed = *ptr_bit_pos;
197
198
36.0M
  if ((bits_consumed -= 8) >= 0) {
199
8.97M
    if (p_bit_buf_end < v)
200
491k
      bits_consumed += 8;
201
8.48M
    else {
202
8.48M
      *readword = (*readword << 8) | *v;
203
8.48M
      v++;
204
8.48M
    }
205
27.0M
  } else {
206
27.0M
    bits_consumed += 8;
207
27.0M
  }
208
209
36.0M
  if (bits_consumed > 31) {
210
308k
    if (p_bit_buf_end < v) {
211
308k
      bits_consumed = 31;
212
308k
    }
213
308k
  }
214
215
36.0M
  *ptr_bit_pos = bits_consumed;
216
36.0M
  *ptr_read_next = v;
217
36.0M
  return;
218
36.0M
}
219
220
17.0M
WORD32 ixheaacd_aac_read_bit(ia_bit_buf_struct *it_bit_buff) {
221
17.0M
  UWORD8 ret_val;
222
17.0M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
223
17.0M
  WORD bit_pos = it_bit_buff->bit_pos;
224
17.0M
  UWORD32 temp;
225
17.0M
  WORD no_of_bits = 1;
226
227
17.0M
  if (bit_pos < 0) {
228
1.78M
    bit_pos = 7;
229
1.78M
    ptr_read_next--;
230
1.78M
  }
231
232
17.0M
  if (ptr_read_next < it_bit_buff->ptr_bit_buf_base) {
233
68
    longjmp(*(it_bit_buff->xaac_jmp_buf),
234
68
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
235
68
  }
236
237
17.0M
  it_bit_buff->cnt_bits += no_of_bits;
238
17.0M
  ret_val = *ptr_read_next;
239
17.0M
  bit_pos -= no_of_bits;
240
241
17.0M
  temp = (ret_val << 24) << (bit_pos + no_of_bits);
242
17.0M
  it_bit_buff->ptr_read_next = ptr_read_next;
243
17.0M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
244
245
17.0M
  return temp >> (32 - no_of_bits);
246
17.0M
}
247
248
12.8M
WORD32 ixheaacd_aac_read_bit_rev(ia_bit_buf_struct *it_bit_buff) {
249
12.8M
  UWORD8 ret_val;
250
12.8M
  UWORD8 *ptr_read_next = it_bit_buff->ptr_read_next;
251
12.8M
  WORD bit_pos = it_bit_buff->bit_pos;
252
12.8M
  UWORD32 temp;
253
12.8M
  WORD no_of_bits = 1;
254
255
12.8M
  if (it_bit_buff->cnt_bits < no_of_bits || it_bit_buff->cnt_bits < 0) {
256
111
    longjmp(*(it_bit_buff->xaac_jmp_buf),
257
111
            IA_XHEAAC_DEC_EXE_NONFATAL_INSUFFICIENT_INPUT_BYTES);
258
111
  }
259
260
12.8M
  if (bit_pos >= 8) {
261
741k
    bit_pos -= 8;
262
741k
    ptr_read_next++;
263
741k
  }
264
265
12.8M
  it_bit_buff->cnt_bits -= no_of_bits;
266
12.8M
  ret_val = *ptr_read_next;
267
12.8M
  bit_pos += no_of_bits;
268
269
12.8M
  temp = (ret_val << 24) << (bit_pos - no_of_bits);
270
12.8M
  it_bit_buff->ptr_read_next = ptr_read_next;
271
12.8M
  it_bit_buff->bit_pos = (WORD16)bit_pos;
272
273
12.8M
  return temp >> (32 - no_of_bits);
274
12.8M
}
275
276
VOID ixheaacd_write_bit(ia_bit_buf_struct *it_bit_buff, WORD32 value,
277
                        WORD32 no_of_bits)
278
279
24.2M
{
280
24.2M
  WORD32 mask;
281
282
24.2M
  if (no_of_bits == 0) return;
283
284
23.9M
  mask = 0x1;
285
23.9M
  mask <<= no_of_bits - 1;
286
287
23.9M
  it_bit_buff->bit_count += no_of_bits;
288
289
51.0M
  while (no_of_bits > 0) {
290
82.6M
    while (no_of_bits > 0 && it_bit_buff->valid_bits < 8) {
291
55.5M
      it_bit_buff->byte <<= 1;
292
55.5M
      if (value & mask) it_bit_buff->byte |= 0x1;
293
55.5M
      value <<= 1;
294
55.5M
      no_of_bits--;
295
55.5M
      it_bit_buff->valid_bits++;
296
55.5M
    }
297
27.0M
    if (it_bit_buff->valid_bits == 8) {
298
6.94M
      *it_bit_buff->byte_ptr++ = it_bit_buff->byte;
299
6.94M
      it_bit_buff->byte = 0;
300
6.94M
      it_bit_buff->valid_bits = 0;
301
6.94M
    }
302
27.0M
  }
303
23.9M
}
304
305
755k
WORD32 ixheaacd_read_bit(ia_bit_buf_struct *it_bit_buff, WORD32 no_of_bits) {
306
755k
  UWORD32 ret_val;
307
755k
  UWORD8 *ptr_read_next = it_bit_buff->byte_ptr;
308
309
755k
  if (no_of_bits == 0) {
310
363k
    return 0;
311
363k
  }
312
313
392k
  ret_val =
314
392k
      ixheaacd_aac_showbits_32(ptr_read_next, it_bit_buff->bit_count, NULL);
315
392k
  it_bit_buff->byte_ptr += (no_of_bits >> 3);
316
317
392k
  if (it_bit_buff->valid_bits != 8) {
318
14.1k
    UWORD8 *v = it_bit_buff->byte_ptr;
319
14.1k
    ret_val = (ret_val << (8 - it_bit_buff->valid_bits)) |
320
14.1k
              (*v >> it_bit_buff->valid_bits);
321
14.1k
  }
322
323
392k
  it_bit_buff->valid_bits -= (no_of_bits % 8);
324
325
392k
  ret_val = ret_val >> (32 - no_of_bits);
326
327
392k
  return ret_val;
328
755k
}