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