Coverage Report

Created: 2025-12-08 06:47

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