xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_bitbuffer.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 <stdlib.h>
22 
23 #include "ixheaac_type_def.h"
24 #include "ixheaac_constants.h"
25 #include "ixheaace_aac_constants.h"
26 #include "ixheaace_bitbuffer.h"
27 
ia_enhaacplus_enc_update_bitbuf_word_ptr(ixheaace_bit_buf_handle pstr_bit_buf,UWORD8 ** p_bitbuf_word,WORD32 cnt)28 static VOID ia_enhaacplus_enc_update_bitbuf_word_ptr(ixheaace_bit_buf_handle pstr_bit_buf,
29                                                      UWORD8 **p_bitbuf_word, WORD32 cnt) {
30   *p_bitbuf_word += cnt;
31 
32   if (*p_bitbuf_word > pstr_bit_buf->ptr_bit_buf_end) {
33     *p_bitbuf_word -= (pstr_bit_buf->ptr_bit_buf_end - pstr_bit_buf->ptr_bit_buf_base + 1);
34   }
35 
36   if (*p_bitbuf_word < pstr_bit_buf->ptr_bit_buf_base) {
37     *p_bitbuf_word += (pstr_bit_buf->ptr_bit_buf_end - pstr_bit_buf->ptr_bit_buf_base + 1);
38   }
39 }
40 
ia_enhaacplus_enc_create_bitbuffer(ixheaace_bit_buf_handle pstr_bit_buf,UWORD8 * ptr_bit_buf_base,UWORD32 bitbuf_size)41 ixheaace_bit_buf_handle ia_enhaacplus_enc_create_bitbuffer(ixheaace_bit_buf_handle pstr_bit_buf,
42                                                            UWORD8 *ptr_bit_buf_base,
43                                                            UWORD32 bitbuf_size) {
44   pstr_bit_buf->ptr_bit_buf_base = ptr_bit_buf_base;
45 
46   pstr_bit_buf->ptr_bit_buf_end = ptr_bit_buf_base + bitbuf_size - 1;
47 
48   pstr_bit_buf->ptr_read_next = ptr_bit_buf_base;
49   pstr_bit_buf->ptr_write_next = ptr_bit_buf_base;
50 
51   pstr_bit_buf->write_position = 7;
52   pstr_bit_buf->read_position = 7;
53 
54   pstr_bit_buf->cnt_bits = 0;
55 
56   pstr_bit_buf->size = bitbuf_size * 8;
57 
58   return pstr_bit_buf;
59 }
60 
ia_enhaacplus_enc_delete_bitbuffer(ixheaace_bit_buf_handle pstr_bit_buf)61 VOID ia_enhaacplus_enc_delete_bitbuffer(ixheaace_bit_buf_handle pstr_bit_buf) {
62   pstr_bit_buf = NULL;
63 }
64 
ixheaace_reset_bitbuf(ixheaace_bit_buf_handle pstr_bit_buf,UWORD8 * ptr_bit_buf_base,UWORD32 bitbuf_size)65 VOID ixheaace_reset_bitbuf(ixheaace_bit_buf_handle pstr_bit_buf, UWORD8 *ptr_bit_buf_base,
66                            UWORD32 bitbuf_size) {
67   pstr_bit_buf->ptr_bit_buf_base = ptr_bit_buf_base;
68 
69   pstr_bit_buf->ptr_bit_buf_end = ptr_bit_buf_base + bitbuf_size - 1;
70 
71   pstr_bit_buf->ptr_read_next = ptr_bit_buf_base;
72   pstr_bit_buf->ptr_write_next = ptr_bit_buf_base;
73 
74   pstr_bit_buf->read_position = 7;
75   pstr_bit_buf->write_position = 7;
76 
77   pstr_bit_buf->cnt_bits = 0;
78 }
ixheaace_copy_bitbuf_to_and_fro(ixheaace_bit_buf_handle h_bitbuf_src,ixheaace_bit_buf_handle h_bitbuf_dst)79 VOID ixheaace_copy_bitbuf_to_and_fro(ixheaace_bit_buf_handle h_bitbuf_src,
80                                      ixheaace_bit_buf_handle h_bitbuf_dst)
81 
82 {
83   WORD32 i;
84   WORD32 bytes_to_go_src =
85       (WORD32)(h_bitbuf_src->ptr_bit_buf_end - h_bitbuf_src->ptr_bit_buf_base);
86 
87   UWORD8 *dst = &h_bitbuf_dst->ptr_bit_buf_base[0];
88   UWORD8 *src = &h_bitbuf_src->ptr_bit_buf_base[0];
89   WORD32 temp321, temp322;
90   UWORD8 temp1, temp2, temp3;
91   WORD32 count;
92   WORD32 remaining;
93 
94   count = bytes_to_go_src >> 1;
95   remaining = bytes_to_go_src - (count << 1);
96 
97   for (i = count - 1; i >= 0; i--) {
98     temp1 = *src;
99     temp2 = *dst;
100     temp3 = *(src + 1);
101     *dst++ = temp1;
102     temp1 = *dst;
103     *src++ = temp2;
104     *dst++ = temp3;
105     *src++ = temp1;
106   }
107   if (remaining)
108     for (i = remaining - 1; i >= 0; i--) {
109       temp1 = *src;
110       temp2 = *dst;
111       *dst++ = temp1;
112       *src++ = temp2;
113     }
114 
115   src = h_bitbuf_src->ptr_read_next;
116   dst = h_bitbuf_dst->ptr_read_next;
117 
118   h_bitbuf_dst->ptr_read_next = src;
119   h_bitbuf_src->ptr_read_next = dst;
120 
121   src = h_bitbuf_src->ptr_write_next;
122   dst = h_bitbuf_dst->ptr_write_next;
123 
124   h_bitbuf_dst->ptr_write_next = src;
125   h_bitbuf_src->ptr_write_next = dst;
126 
127   temp321 = h_bitbuf_dst->read_position;
128   temp322 = h_bitbuf_src->read_position;
129 
130   h_bitbuf_dst->read_position = temp322;
131   h_bitbuf_src->read_position = temp321;
132 
133   temp321 = h_bitbuf_dst->write_position;
134   temp322 = h_bitbuf_src->write_position;
135 
136   h_bitbuf_dst->write_position = temp322;
137   h_bitbuf_src->write_position = temp321;
138 
139   temp321 = h_bitbuf_dst->cnt_bits;
140   temp322 = h_bitbuf_src->cnt_bits;
141 
142   h_bitbuf_dst->cnt_bits = temp322;
143   h_bitbuf_src->cnt_bits = temp321;
144 }
145 
ia_enhaacplus_enc_copy_bitbuf(ixheaace_bit_buf_handle h_bitbuf_src,ixheaace_bit_buf_handle h_bitbuf_dst)146 VOID ia_enhaacplus_enc_copy_bitbuf(ixheaace_bit_buf_handle h_bitbuf_src,
147                                    ixheaace_bit_buf_handle h_bitbuf_dst) {
148   WORD32 i;
149   WORD32 bytes_to_go_src =
150       (WORD32)(h_bitbuf_src->ptr_bit_buf_end - h_bitbuf_src->ptr_bit_buf_base);
151 
152   for (i = 0; i < bytes_to_go_src; i++) {
153     h_bitbuf_dst->ptr_bit_buf_base[i] = h_bitbuf_src->ptr_bit_buf_base[i];
154   }
155 
156   h_bitbuf_dst->ptr_read_next = h_bitbuf_src->ptr_read_next;
157   h_bitbuf_dst->ptr_write_next = h_bitbuf_src->ptr_write_next;
158 
159   h_bitbuf_dst->read_position = h_bitbuf_src->read_position;
160   h_bitbuf_dst->write_position = h_bitbuf_src->write_position;
161 
162   h_bitbuf_dst->cnt_bits = h_bitbuf_src->cnt_bits;
163 }
164 
ia_enhaacplus_enc_get_bits_available(ixheaace_bit_buf_handle pstr_bit_buf_handle)165 WORD32 ia_enhaacplus_enc_get_bits_available(ixheaace_bit_buf_handle pstr_bit_buf_handle) {
166   return pstr_bit_buf_handle->cnt_bits;
167 }
168 UWORD32
ixheaace_readbits(ixheaace_bit_buf_handle pstr_bit_buf,UWORD8 no_bits_to_read)169 ixheaace_readbits(ixheaace_bit_buf_handle pstr_bit_buf, UWORD8 no_bits_to_read) {
170   UWORD32 return_value;
171 
172   if (no_bits_to_read >= 25) {
173     return 0;
174   }
175 
176   pstr_bit_buf->cnt_bits -= no_bits_to_read;
177   pstr_bit_buf->read_position -= no_bits_to_read;
178 
179   return_value = (UWORD32)*pstr_bit_buf->ptr_read_next;
180 
181   while (pstr_bit_buf->read_position < 0) {
182     pstr_bit_buf->read_position += 8;
183     pstr_bit_buf->ptr_read_next++;
184 
185     if (pstr_bit_buf->ptr_read_next > pstr_bit_buf->ptr_bit_buf_end) {
186       pstr_bit_buf->ptr_read_next = pstr_bit_buf->ptr_bit_buf_base;
187     }
188 
189     return_value <<= 8;
190 
191     return_value |= (UWORD32)*pstr_bit_buf->ptr_read_next;
192   }
193   return_value = return_value << (31 - no_bits_to_read - pstr_bit_buf->read_position) >>
194                  (32 - no_bits_to_read);
195 
196   return (return_value);
197 }
198 
ia_enhaacplus_enc_wind_bitbuffer_bidirectional(ixheaace_bit_buf_handle pstr_bit_buf,WORD32 offset)199 VOID ia_enhaacplus_enc_wind_bitbuffer_bidirectional(ixheaace_bit_buf_handle pstr_bit_buf,
200                                                     WORD32 offset) {
201   if (offset != 0) {
202     WORD32 buff_offset;
203     pstr_bit_buf->read_position -= offset;
204     buff_offset = (pstr_bit_buf->read_position) >> 3;
205     pstr_bit_buf->read_position -= buff_offset << 3;
206 
207     if (buff_offset) {
208       ia_enhaacplus_enc_update_bitbuf_word_ptr(pstr_bit_buf, &pstr_bit_buf->ptr_read_next,
209                                                -buff_offset);
210     }
211 
212     pstr_bit_buf->cnt_bits -= offset;
213   }
214 }
215