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