Coverage Report

Created: 2026-03-21 07:02

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