Coverage Report

Created: 2025-07-12 07:02

/src/libxaac/encoder/iusace_bitbuffer.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *                                                                            *
3
 * Copyright (C) 2023 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 "ixheaac_type_def.h"
22
#include "ixheaac_constants.h"
23
#include "iusace_cnst.h"
24
#include "iusace_bitbuffer.h"
25
#include "ixheaac_basic_ops32.h"
26
#include "ixheaac_basic_ops40.h"
27
#include "ixheaac_basic_ops.h"
28
29
ia_bit_buf_struct *iusace_create_bit_buffer(ia_bit_buf_struct *it_bit_buf,
30
                                            UWORD8 *ptr_bit_buf_base, UWORD32 bit_buffer_size,
31
369k
                                            WORD32 init) {
32
369k
  it_bit_buf->ptr_bit_buf_base = ptr_bit_buf_base;
33
369k
  it_bit_buf->ptr_bit_buf_end = ptr_bit_buf_base + bit_buffer_size - 1;
34
369k
  it_bit_buf->ptr_read_next = ptr_bit_buf_base;
35
369k
  it_bit_buf->ptr_write_next = ptr_bit_buf_base;
36
37
369k
  if (init) {
38
369k
    it_bit_buf->write_position = 7;
39
369k
    it_bit_buf->read_position = 7;
40
369k
    it_bit_buf->cnt_bits = 0;
41
369k
    it_bit_buf->size = bit_buffer_size * 8;
42
369k
  }
43
44
369k
  return (it_bit_buf);
45
369k
}
46
47
266k
VOID iusace_reset_bit_buffer(ia_bit_buf_struct *it_bit_buf) {
48
266k
  it_bit_buf->ptr_read_next = it_bit_buf->ptr_bit_buf_base;
49
266k
  it_bit_buf->ptr_write_next = it_bit_buf->ptr_bit_buf_base;
50
51
266k
  it_bit_buf->write_position = 7;
52
266k
  it_bit_buf->read_position = 7;
53
266k
  it_bit_buf->cnt_bits = 0;
54
55
266k
  return;
56
266k
}
57
58
2.36G
UWORD8 iusace_write_bits_buf(ia_bit_buf_struct *it_bit_buf, UWORD32 write_val, UWORD8 num_bits) {
59
2.36G
  WORD8 bits_to_write;
60
2.36G
  WORD32 write_position;
61
2.36G
  UWORD8 *ptr_write_next;
62
2.36G
  UWORD8 *ptr_bit_buf_end;
63
2.36G
  UWORD8 *ptr_bit_buf_base;
64
2.36G
  UWORD8 bits_written = num_bits;
65
2.36G
  if (it_bit_buf) {
66
1.30G
    it_bit_buf->cnt_bits += num_bits;
67
68
1.30G
    write_position = it_bit_buf->write_position;
69
1.30G
    ptr_write_next = it_bit_buf->ptr_write_next;
70
1.30G
    ptr_bit_buf_end = it_bit_buf->ptr_bit_buf_end;
71
1.30G
    ptr_bit_buf_base = it_bit_buf->ptr_bit_buf_base;
72
2.69G
    while (num_bits) {
73
1.38G
      UWORD8 tmp, msk;
74
75
1.38G
      bits_to_write = (WORD8)MIN(write_position + 1, num_bits);
76
77
1.38G
      tmp = (UWORD8)(write_val << (32 - num_bits) >> (32 - bits_to_write)
78
1.38G
                                                         << (write_position + 1 - bits_to_write));
79
80
1.38G
      msk = ~(((1 << bits_to_write) - 1) << (write_position + 1 - bits_to_write));
81
82
1.38G
      *ptr_write_next &= msk;
83
1.38G
      *ptr_write_next |= tmp;
84
85
1.38G
      write_position -= bits_to_write;
86
87
1.38G
      num_bits -= bits_to_write;
88
89
1.38G
      if (write_position < 0) {
90
250M
        write_position += 8;
91
250M
        ptr_write_next++;
92
93
250M
        if (ptr_write_next > ptr_bit_buf_end) {
94
0
          ptr_write_next = ptr_bit_buf_base;
95
0
        }
96
250M
      }
97
1.38G
    }
98
99
1.30G
    it_bit_buf->write_position = write_position;
100
1.30G
    it_bit_buf->ptr_write_next = ptr_write_next;
101
1.30G
  }
102
103
2.36G
  return (bits_written);
104
2.36G
}
105
106
WORD32 iusace_write_escape_value(ia_bit_buf_struct *pstr_it_bit_buff, UWORD32 value,
107
203k
                                 UWORD8 no_bits1, UWORD8 no_bits2, UWORD8 no_bits3) {
108
203k
  WORD32 bit_cnt = 0;
109
203k
  UWORD32 esc_val = 0;
110
203k
  UWORD32 max_val1 = (1 << no_bits1) - 1;
111
203k
  UWORD32 max_val2 = (1 << no_bits2) - 1;
112
203k
  UWORD32 max_val3 = (1 << no_bits3) - 1;
113
114
203k
  esc_val = MIN(value, max_val1);
115
203k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits1);
116
117
203k
  if (esc_val == max_val1) {
118
16.1k
    value = value - esc_val;
119
120
16.1k
    esc_val = MIN(value, max_val2);
121
16.1k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits2);
122
123
16.1k
    if (esc_val == max_val2) {
124
683
      value = value - esc_val;
125
126
683
      esc_val = MIN(value, max_val3);
127
683
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits3);
128
683
    }
129
16.1k
  }
130
131
203k
  return bit_cnt;
132
203k
}