Coverage Report

Created: 2025-08-24 07:17

/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
421k
                                            WORD32 init) {
32
421k
  it_bit_buf->ptr_bit_buf_base = ptr_bit_buf_base;
33
421k
  it_bit_buf->ptr_bit_buf_end = ptr_bit_buf_base + bit_buffer_size - 1;
34
421k
  it_bit_buf->ptr_read_next = ptr_bit_buf_base;
35
421k
  it_bit_buf->ptr_write_next = ptr_bit_buf_base;
36
37
421k
  if (init) {
38
421k
    it_bit_buf->write_position = 7;
39
421k
    it_bit_buf->read_position = 7;
40
421k
    it_bit_buf->cnt_bits = 0;
41
421k
    it_bit_buf->size = bit_buffer_size * 8;
42
421k
  }
43
44
421k
  return (it_bit_buf);
45
421k
}
46
47
45.7k
VOID iusace_reset_bit_buffer(ia_bit_buf_struct *it_bit_buf) {
48
45.7k
  it_bit_buf->ptr_read_next = it_bit_buf->ptr_bit_buf_base;
49
45.7k
  it_bit_buf->ptr_write_next = it_bit_buf->ptr_bit_buf_base;
50
51
45.7k
  it_bit_buf->write_position = 7;
52
45.7k
  it_bit_buf->read_position = 7;
53
45.7k
  it_bit_buf->cnt_bits = 0;
54
55
45.7k
  return;
56
45.7k
}
57
58
2.76G
UWORD8 iusace_write_bits_buf(ia_bit_buf_struct *it_bit_buf, UWORD32 write_val, UWORD8 num_bits) {
59
2.76G
  WORD8 bits_to_write;
60
2.76G
  WORD32 write_position;
61
2.76G
  UWORD8 *ptr_write_next;
62
2.76G
  UWORD8 *ptr_bit_buf_end;
63
2.76G
  UWORD8 *ptr_bit_buf_base;
64
2.76G
  UWORD8 bits_written = num_bits;
65
2.76G
  if (it_bit_buf) {
66
1.54G
    it_bit_buf->cnt_bits += num_bits;
67
68
1.54G
    write_position = it_bit_buf->write_position;
69
1.54G
    ptr_write_next = it_bit_buf->ptr_write_next;
70
1.54G
    ptr_bit_buf_end = it_bit_buf->ptr_bit_buf_end;
71
1.54G
    ptr_bit_buf_base = it_bit_buf->ptr_bit_buf_base;
72
3.21G
    while (num_bits) {
73
1.66G
      UWORD8 tmp, msk;
74
75
1.66G
      bits_to_write = (WORD8)MIN(write_position + 1, num_bits);
76
77
1.66G
      tmp = (UWORD8)(write_val << (32 - num_bits) >> (32 - bits_to_write)
78
1.66G
                                                         << (write_position + 1 - bits_to_write));
79
80
1.66G
      msk = ~(((1 << bits_to_write) - 1) << (write_position + 1 - bits_to_write));
81
82
1.66G
      *ptr_write_next &= msk;
83
1.66G
      *ptr_write_next |= tmp;
84
85
1.66G
      write_position -= bits_to_write;
86
87
1.66G
      num_bits -= bits_to_write;
88
89
1.66G
      if (write_position < 0) {
90
314M
        write_position += 8;
91
314M
        ptr_write_next++;
92
93
314M
        if (ptr_write_next > ptr_bit_buf_end) {
94
0
          ptr_write_next = ptr_bit_buf_base;
95
0
        }
96
314M
      }
97
1.66G
    }
98
99
1.54G
    it_bit_buf->write_position = write_position;
100
1.54G
    it_bit_buf->ptr_write_next = ptr_write_next;
101
1.54G
  }
102
103
2.76G
  return (bits_written);
104
2.76G
}
105
106
WORD32 iusace_write_escape_value(ia_bit_buf_struct *pstr_it_bit_buff, UWORD32 value,
107
209k
                                 UWORD8 no_bits1, UWORD8 no_bits2, UWORD8 no_bits3) {
108
209k
  WORD32 bit_cnt = 0;
109
209k
  UWORD32 esc_val = 0;
110
209k
  UWORD32 max_val1 = (1 << no_bits1) - 1;
111
209k
  UWORD32 max_val2 = (1 << no_bits2) - 1;
112
209k
  UWORD32 max_val3 = (1 << no_bits3) - 1;
113
114
209k
  esc_val = MIN(value, max_val1);
115
209k
  bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits1);
116
117
209k
  if (esc_val == max_val1) {
118
20.5k
    value = value - esc_val;
119
120
20.5k
    esc_val = MIN(value, max_val2);
121
20.5k
    bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits2);
122
123
20.5k
    if (esc_val == max_val2) {
124
985
      value = value - esc_val;
125
126
985
      esc_val = MIN(value, max_val3);
127
985
      bit_cnt += iusace_write_bits_buf(pstr_it_bit_buff, esc_val, no_bits3);
128
985
    }
129
20.5k
  }
130
131
209k
  return bit_cnt;
132
209k
}