Coverage Report

Created: 2026-01-09 06:51

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