xref: /aosp_15_r20/external/liblc3/src/bits.c (revision 49fe348c0058011ee60b6957cdd9d52742df84bc)
1*49fe348cSAndroid Build Coastguard Worker /******************************************************************************
2*49fe348cSAndroid Build Coastguard Worker  *
3*49fe348cSAndroid Build Coastguard Worker  *  Copyright 2022 Google LLC
4*49fe348cSAndroid Build Coastguard Worker  *
5*49fe348cSAndroid Build Coastguard Worker  *  Licensed under the Apache License, Version 2.0 (the "License");
6*49fe348cSAndroid Build Coastguard Worker  *  you may not use this file except in compliance with the License.
7*49fe348cSAndroid Build Coastguard Worker  *  You may obtain a copy of the License at:
8*49fe348cSAndroid Build Coastguard Worker  *
9*49fe348cSAndroid Build Coastguard Worker  *  http://www.apache.org/licenses/LICENSE-2.0
10*49fe348cSAndroid Build Coastguard Worker  *
11*49fe348cSAndroid Build Coastguard Worker  *  Unless required by applicable law or agreed to in writing, software
12*49fe348cSAndroid Build Coastguard Worker  *  distributed under the License is distributed on an "AS IS" BASIS,
13*49fe348cSAndroid Build Coastguard Worker  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*49fe348cSAndroid Build Coastguard Worker  *  See the License for the specific language governing permissions and
15*49fe348cSAndroid Build Coastguard Worker  *  limitations under the License.
16*49fe348cSAndroid Build Coastguard Worker  *
17*49fe348cSAndroid Build Coastguard Worker  ******************************************************************************/
18*49fe348cSAndroid Build Coastguard Worker 
19*49fe348cSAndroid Build Coastguard Worker #include "bits.h"
20*49fe348cSAndroid Build Coastguard Worker #include "common.h"
21*49fe348cSAndroid Build Coastguard Worker 
22*49fe348cSAndroid Build Coastguard Worker 
23*49fe348cSAndroid Build Coastguard Worker /* ----------------------------------------------------------------------------
24*49fe348cSAndroid Build Coastguard Worker  *  Common
25*49fe348cSAndroid Build Coastguard Worker  * -------------------------------------------------------------------------- */
26*49fe348cSAndroid Build Coastguard Worker 
27*49fe348cSAndroid Build Coastguard Worker static inline int ac_get(struct lc3_bits_buffer *);
28*49fe348cSAndroid Build Coastguard Worker static inline void accu_load(struct lc3_bits_accu *, struct lc3_bits_buffer *);
29*49fe348cSAndroid Build Coastguard Worker 
30*49fe348cSAndroid Build Coastguard Worker /**
31*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder return range bits
32*49fe348cSAndroid Build Coastguard Worker  * ac              Arithmetic coder
33*49fe348cSAndroid Build Coastguard Worker  * return          1 + log2(ac->range)
34*49fe348cSAndroid Build Coastguard Worker  */
ac_get_range_bits(const struct lc3_bits_ac * ac)35*49fe348cSAndroid Build Coastguard Worker static int ac_get_range_bits(const struct lc3_bits_ac *ac)
36*49fe348cSAndroid Build Coastguard Worker {
37*49fe348cSAndroid Build Coastguard Worker     int nbits = 0;
38*49fe348cSAndroid Build Coastguard Worker 
39*49fe348cSAndroid Build Coastguard Worker     for (unsigned r = ac->range; r; r >>= 1, nbits++);
40*49fe348cSAndroid Build Coastguard Worker 
41*49fe348cSAndroid Build Coastguard Worker     return nbits;
42*49fe348cSAndroid Build Coastguard Worker }
43*49fe348cSAndroid Build Coastguard Worker 
44*49fe348cSAndroid Build Coastguard Worker /**
45*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder return pending bits
46*49fe348cSAndroid Build Coastguard Worker  * ac              Arithmetic coder
47*49fe348cSAndroid Build Coastguard Worker  * return          Pending bits
48*49fe348cSAndroid Build Coastguard Worker  */
ac_get_pending_bits(const struct lc3_bits_ac * ac)49*49fe348cSAndroid Build Coastguard Worker static int ac_get_pending_bits(const struct lc3_bits_ac *ac)
50*49fe348cSAndroid Build Coastguard Worker {
51*49fe348cSAndroid Build Coastguard Worker     return 26 - ac_get_range_bits(ac) +
52*49fe348cSAndroid Build Coastguard Worker         ((ac->cache >= 0) + ac->carry_count) * 8;
53*49fe348cSAndroid Build Coastguard Worker }
54*49fe348cSAndroid Build Coastguard Worker 
55*49fe348cSAndroid Build Coastguard Worker /**
56*49fe348cSAndroid Build Coastguard Worker  * Return number of bits left in the bitstream
57*49fe348cSAndroid Build Coastguard Worker  * bits            Bitstream context
58*49fe348cSAndroid Build Coastguard Worker  * return          >= 0: Number of bits left  < 0: Overflow
59*49fe348cSAndroid Build Coastguard Worker  */
get_bits_left(const struct lc3_bits * bits)60*49fe348cSAndroid Build Coastguard Worker static int get_bits_left(const struct lc3_bits *bits)
61*49fe348cSAndroid Build Coastguard Worker {
62*49fe348cSAndroid Build Coastguard Worker     const struct lc3_bits_buffer *buffer = &bits->buffer;
63*49fe348cSAndroid Build Coastguard Worker     const struct lc3_bits_accu *accu = &bits->accu;
64*49fe348cSAndroid Build Coastguard Worker     const struct lc3_bits_ac *ac = &bits->ac;
65*49fe348cSAndroid Build Coastguard Worker 
66*49fe348cSAndroid Build Coastguard Worker     uintptr_t end = (uintptr_t)buffer->p_bw +
67*49fe348cSAndroid Build Coastguard Worker         (bits->mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS/8 : 0);
68*49fe348cSAndroid Build Coastguard Worker 
69*49fe348cSAndroid Build Coastguard Worker     uintptr_t start = (uintptr_t)buffer->p_fw -
70*49fe348cSAndroid Build Coastguard Worker         (bits->mode == LC3_BITS_MODE_READ ? LC3_AC_BITS/8 : 0);
71*49fe348cSAndroid Build Coastguard Worker 
72*49fe348cSAndroid Build Coastguard Worker     int n = end > start ? (int)(end - start) : -(int)(start - end);
73*49fe348cSAndroid Build Coastguard Worker 
74*49fe348cSAndroid Build Coastguard Worker     return 8 * n - (accu->n + accu->nover + ac_get_pending_bits(ac));
75*49fe348cSAndroid Build Coastguard Worker }
76*49fe348cSAndroid Build Coastguard Worker 
77*49fe348cSAndroid Build Coastguard Worker /**
78*49fe348cSAndroid Build Coastguard Worker  * Setup bitstream writing
79*49fe348cSAndroid Build Coastguard Worker  */
lc3_setup_bits(struct lc3_bits * bits,enum lc3_bits_mode mode,void * buffer,int len)80*49fe348cSAndroid Build Coastguard Worker void lc3_setup_bits(struct lc3_bits *bits,
81*49fe348cSAndroid Build Coastguard Worker     enum lc3_bits_mode mode, void *buffer, int len)
82*49fe348cSAndroid Build Coastguard Worker {
83*49fe348cSAndroid Build Coastguard Worker     *bits = (struct lc3_bits){
84*49fe348cSAndroid Build Coastguard Worker         .mode = mode,
85*49fe348cSAndroid Build Coastguard Worker         .accu = {
86*49fe348cSAndroid Build Coastguard Worker             .n = mode == LC3_BITS_MODE_READ ? LC3_ACCU_BITS : 0,
87*49fe348cSAndroid Build Coastguard Worker         },
88*49fe348cSAndroid Build Coastguard Worker         .ac = {
89*49fe348cSAndroid Build Coastguard Worker             .range = 0xffffff,
90*49fe348cSAndroid Build Coastguard Worker             .cache = -1
91*49fe348cSAndroid Build Coastguard Worker         },
92*49fe348cSAndroid Build Coastguard Worker         .buffer = {
93*49fe348cSAndroid Build Coastguard Worker             .start = (uint8_t *)buffer, .end  = (uint8_t *)buffer + len,
94*49fe348cSAndroid Build Coastguard Worker             .p_fw  = (uint8_t *)buffer, .p_bw = (uint8_t *)buffer + len,
95*49fe348cSAndroid Build Coastguard Worker         }
96*49fe348cSAndroid Build Coastguard Worker     };
97*49fe348cSAndroid Build Coastguard Worker 
98*49fe348cSAndroid Build Coastguard Worker     if (mode == LC3_BITS_MODE_READ) {
99*49fe348cSAndroid Build Coastguard Worker         struct lc3_bits_ac *ac = &bits->ac;
100*49fe348cSAndroid Build Coastguard Worker         struct lc3_bits_accu *accu = &bits->accu;
101*49fe348cSAndroid Build Coastguard Worker         struct lc3_bits_buffer *buffer = &bits->buffer;
102*49fe348cSAndroid Build Coastguard Worker 
103*49fe348cSAndroid Build Coastguard Worker         ac->low  = ac_get(buffer) << 16;
104*49fe348cSAndroid Build Coastguard Worker         ac->low |= ac_get(buffer) <<  8;
105*49fe348cSAndroid Build Coastguard Worker         ac->low |= ac_get(buffer);
106*49fe348cSAndroid Build Coastguard Worker 
107*49fe348cSAndroid Build Coastguard Worker         accu_load(accu, buffer);
108*49fe348cSAndroid Build Coastguard Worker     }
109*49fe348cSAndroid Build Coastguard Worker }
110*49fe348cSAndroid Build Coastguard Worker 
111*49fe348cSAndroid Build Coastguard Worker /**
112*49fe348cSAndroid Build Coastguard Worker  * Return number of bits left in the bitstream
113*49fe348cSAndroid Build Coastguard Worker  */
lc3_get_bits_left(const struct lc3_bits * bits)114*49fe348cSAndroid Build Coastguard Worker int lc3_get_bits_left(const struct lc3_bits *bits)
115*49fe348cSAndroid Build Coastguard Worker {
116*49fe348cSAndroid Build Coastguard Worker     return LC3_MAX(get_bits_left(bits), 0);
117*49fe348cSAndroid Build Coastguard Worker }
118*49fe348cSAndroid Build Coastguard Worker 
119*49fe348cSAndroid Build Coastguard Worker /**
120*49fe348cSAndroid Build Coastguard Worker  * Return number of bits left in the bitstream
121*49fe348cSAndroid Build Coastguard Worker  */
lc3_check_bits(const struct lc3_bits * bits)122*49fe348cSAndroid Build Coastguard Worker int lc3_check_bits(const struct lc3_bits *bits)
123*49fe348cSAndroid Build Coastguard Worker {
124*49fe348cSAndroid Build Coastguard Worker     const struct lc3_bits_ac *ac = &bits->ac;
125*49fe348cSAndroid Build Coastguard Worker 
126*49fe348cSAndroid Build Coastguard Worker     return -(get_bits_left(bits) < 0 || ac->error);
127*49fe348cSAndroid Build Coastguard Worker }
128*49fe348cSAndroid Build Coastguard Worker 
129*49fe348cSAndroid Build Coastguard Worker 
130*49fe348cSAndroid Build Coastguard Worker /* ----------------------------------------------------------------------------
131*49fe348cSAndroid Build Coastguard Worker  *  Writing
132*49fe348cSAndroid Build Coastguard Worker  * -------------------------------------------------------------------------- */
133*49fe348cSAndroid Build Coastguard Worker 
134*49fe348cSAndroid Build Coastguard Worker /**
135*49fe348cSAndroid Build Coastguard Worker  * Flush the bits accumulator
136*49fe348cSAndroid Build Coastguard Worker  * accu            Bitstream accumulator
137*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
138*49fe348cSAndroid Build Coastguard Worker  */
accu_flush(struct lc3_bits_accu * accu,struct lc3_bits_buffer * buffer)139*49fe348cSAndroid Build Coastguard Worker static inline void accu_flush(
140*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_accu *accu, struct lc3_bits_buffer *buffer)
141*49fe348cSAndroid Build Coastguard Worker {
142*49fe348cSAndroid Build Coastguard Worker     int nbytes = LC3_MIN(accu->n >> 3,
143*49fe348cSAndroid Build Coastguard Worker         LC3_MAX(buffer->p_bw - buffer->p_fw, 0));
144*49fe348cSAndroid Build Coastguard Worker 
145*49fe348cSAndroid Build Coastguard Worker     accu->n -= 8 * nbytes;
146*49fe348cSAndroid Build Coastguard Worker 
147*49fe348cSAndroid Build Coastguard Worker     for ( ; nbytes; accu->v >>= 8, nbytes--)
148*49fe348cSAndroid Build Coastguard Worker         *(--buffer->p_bw) = accu->v & 0xff;
149*49fe348cSAndroid Build Coastguard Worker 
150*49fe348cSAndroid Build Coastguard Worker     if (accu->n >= 8)
151*49fe348cSAndroid Build Coastguard Worker         accu->n = 0;
152*49fe348cSAndroid Build Coastguard Worker }
153*49fe348cSAndroid Build Coastguard Worker 
154*49fe348cSAndroid Build Coastguard Worker /**
155*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder put byte
156*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
157*49fe348cSAndroid Build Coastguard Worker  * byte            Byte to output
158*49fe348cSAndroid Build Coastguard Worker  */
ac_put(struct lc3_bits_buffer * buffer,int byte)159*49fe348cSAndroid Build Coastguard Worker static inline void ac_put(struct lc3_bits_buffer *buffer, int byte)
160*49fe348cSAndroid Build Coastguard Worker {
161*49fe348cSAndroid Build Coastguard Worker     if (buffer->p_fw < buffer->end)
162*49fe348cSAndroid Build Coastguard Worker         *(buffer->p_fw++) = byte;
163*49fe348cSAndroid Build Coastguard Worker }
164*49fe348cSAndroid Build Coastguard Worker 
165*49fe348cSAndroid Build Coastguard Worker /**
166*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder range shift
167*49fe348cSAndroid Build Coastguard Worker  * ac              Arithmetic coder
168*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
169*49fe348cSAndroid Build Coastguard Worker  */
ac_shift(struct lc3_bits_ac * ac,struct lc3_bits_buffer * buffer)170*49fe348cSAndroid Build Coastguard Worker LC3_HOT static inline void ac_shift(
171*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_ac *ac, struct lc3_bits_buffer *buffer)
172*49fe348cSAndroid Build Coastguard Worker {
173*49fe348cSAndroid Build Coastguard Worker     if (ac->low < 0xff0000 || ac->carry)
174*49fe348cSAndroid Build Coastguard Worker     {
175*49fe348cSAndroid Build Coastguard Worker         if (ac->cache >= 0)
176*49fe348cSAndroid Build Coastguard Worker             ac_put(buffer, ac->cache + ac->carry);
177*49fe348cSAndroid Build Coastguard Worker 
178*49fe348cSAndroid Build Coastguard Worker         for ( ; ac->carry_count > 0; ac->carry_count--)
179*49fe348cSAndroid Build Coastguard Worker             ac_put(buffer, ac->carry ? 0x00 : 0xff);
180*49fe348cSAndroid Build Coastguard Worker 
181*49fe348cSAndroid Build Coastguard Worker          ac->cache = ac->low >> 16;
182*49fe348cSAndroid Build Coastguard Worker          ac->carry = 0;
183*49fe348cSAndroid Build Coastguard Worker     }
184*49fe348cSAndroid Build Coastguard Worker     else
185*49fe348cSAndroid Build Coastguard Worker          ac->carry_count++;
186*49fe348cSAndroid Build Coastguard Worker 
187*49fe348cSAndroid Build Coastguard Worker     ac->low = (ac->low << 8) & 0xffffff;
188*49fe348cSAndroid Build Coastguard Worker }
189*49fe348cSAndroid Build Coastguard Worker 
190*49fe348cSAndroid Build Coastguard Worker /**
191*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder termination
192*49fe348cSAndroid Build Coastguard Worker  * ac              Arithmetic coder
193*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
194*49fe348cSAndroid Build Coastguard Worker  * end_val/nbits   End value and count of bits to terminate (1 to 8)
195*49fe348cSAndroid Build Coastguard Worker  */
ac_terminate(struct lc3_bits_ac * ac,struct lc3_bits_buffer * buffer)196*49fe348cSAndroid Build Coastguard Worker static void ac_terminate(struct lc3_bits_ac *ac,
197*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_buffer *buffer)
198*49fe348cSAndroid Build Coastguard Worker {
199*49fe348cSAndroid Build Coastguard Worker     int nbits = 25 - ac_get_range_bits(ac);
200*49fe348cSAndroid Build Coastguard Worker     unsigned mask = 0xffffff >> nbits;
201*49fe348cSAndroid Build Coastguard Worker     unsigned val  = ac->low + mask;
202*49fe348cSAndroid Build Coastguard Worker     unsigned high = ac->low + ac->range;
203*49fe348cSAndroid Build Coastguard Worker 
204*49fe348cSAndroid Build Coastguard Worker     bool over_val  = val  >> 24;
205*49fe348cSAndroid Build Coastguard Worker     bool over_high = high >> 24;
206*49fe348cSAndroid Build Coastguard Worker 
207*49fe348cSAndroid Build Coastguard Worker     val  = (val  & 0xffffff) & ~mask;
208*49fe348cSAndroid Build Coastguard Worker     high = (high & 0xffffff);
209*49fe348cSAndroid Build Coastguard Worker 
210*49fe348cSAndroid Build Coastguard Worker     if (over_val == over_high) {
211*49fe348cSAndroid Build Coastguard Worker 
212*49fe348cSAndroid Build Coastguard Worker         if (val + mask >= high) {
213*49fe348cSAndroid Build Coastguard Worker             nbits++;
214*49fe348cSAndroid Build Coastguard Worker             mask >>= 1;
215*49fe348cSAndroid Build Coastguard Worker             val = ((ac->low + mask) & 0xffffff) & ~mask;
216*49fe348cSAndroid Build Coastguard Worker         }
217*49fe348cSAndroid Build Coastguard Worker 
218*49fe348cSAndroid Build Coastguard Worker         ac->carry |= val < ac->low;
219*49fe348cSAndroid Build Coastguard Worker     }
220*49fe348cSAndroid Build Coastguard Worker 
221*49fe348cSAndroid Build Coastguard Worker     ac->low = val;
222*49fe348cSAndroid Build Coastguard Worker 
223*49fe348cSAndroid Build Coastguard Worker     for (; nbits > 8; nbits -= 8)
224*49fe348cSAndroid Build Coastguard Worker         ac_shift(ac, buffer);
225*49fe348cSAndroid Build Coastguard Worker     ac_shift(ac, buffer);
226*49fe348cSAndroid Build Coastguard Worker 
227*49fe348cSAndroid Build Coastguard Worker     int end_val = ac->cache >> (8 - nbits);
228*49fe348cSAndroid Build Coastguard Worker 
229*49fe348cSAndroid Build Coastguard Worker     if (ac->carry_count) {
230*49fe348cSAndroid Build Coastguard Worker         ac_put(buffer, ac->cache);
231*49fe348cSAndroid Build Coastguard Worker         for ( ; ac->carry_count > 1; ac->carry_count--)
232*49fe348cSAndroid Build Coastguard Worker             ac_put(buffer, 0xff);
233*49fe348cSAndroid Build Coastguard Worker 
234*49fe348cSAndroid Build Coastguard Worker         end_val = nbits < 8 ? 0 : 0xff;
235*49fe348cSAndroid Build Coastguard Worker     }
236*49fe348cSAndroid Build Coastguard Worker 
237*49fe348cSAndroid Build Coastguard Worker     if (buffer->p_fw < buffer->end) {
238*49fe348cSAndroid Build Coastguard Worker         *buffer->p_fw &= 0xff >> nbits;
239*49fe348cSAndroid Build Coastguard Worker         *buffer->p_fw |= end_val << (8 - nbits);
240*49fe348cSAndroid Build Coastguard Worker     }
241*49fe348cSAndroid Build Coastguard Worker }
242*49fe348cSAndroid Build Coastguard Worker 
243*49fe348cSAndroid Build Coastguard Worker /**
244*49fe348cSAndroid Build Coastguard Worker  * Flush and terminate bitstream
245*49fe348cSAndroid Build Coastguard Worker  */
lc3_flush_bits(struct lc3_bits * bits)246*49fe348cSAndroid Build Coastguard Worker void lc3_flush_bits(struct lc3_bits *bits)
247*49fe348cSAndroid Build Coastguard Worker {
248*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_ac *ac = &bits->ac;
249*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_accu *accu = &bits->accu;
250*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_buffer *buffer = &bits->buffer;
251*49fe348cSAndroid Build Coastguard Worker 
252*49fe348cSAndroid Build Coastguard Worker     int nleft = buffer->p_bw - buffer->p_fw;
253*49fe348cSAndroid Build Coastguard Worker     for (int n = 8 * nleft - accu->n; n > 0; n -= 32)
254*49fe348cSAndroid Build Coastguard Worker         lc3_put_bits(bits, 0, LC3_MIN(n, 32));
255*49fe348cSAndroid Build Coastguard Worker 
256*49fe348cSAndroid Build Coastguard Worker     accu_flush(accu, buffer);
257*49fe348cSAndroid Build Coastguard Worker 
258*49fe348cSAndroid Build Coastguard Worker     ac_terminate(ac, buffer);
259*49fe348cSAndroid Build Coastguard Worker }
260*49fe348cSAndroid Build Coastguard Worker 
261*49fe348cSAndroid Build Coastguard Worker /**
262*49fe348cSAndroid Build Coastguard Worker  * Write from 1 to 32 bits,
263*49fe348cSAndroid Build Coastguard Worker  * exceeding the capacity of the accumulator
264*49fe348cSAndroid Build Coastguard Worker  */
lc3_put_bits_generic(struct lc3_bits * bits,unsigned v,int n)265*49fe348cSAndroid Build Coastguard Worker LC3_HOT void lc3_put_bits_generic(struct lc3_bits *bits, unsigned v, int n)
266*49fe348cSAndroid Build Coastguard Worker {
267*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_accu *accu = &bits->accu;
268*49fe348cSAndroid Build Coastguard Worker 
269*49fe348cSAndroid Build Coastguard Worker     /* --- Fulfill accumulator and flush -- */
270*49fe348cSAndroid Build Coastguard Worker 
271*49fe348cSAndroid Build Coastguard Worker     int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n);
272*49fe348cSAndroid Build Coastguard Worker     if (n1) {
273*49fe348cSAndroid Build Coastguard Worker         accu->v |= v << accu->n;
274*49fe348cSAndroid Build Coastguard Worker         accu->n = LC3_ACCU_BITS;
275*49fe348cSAndroid Build Coastguard Worker     }
276*49fe348cSAndroid Build Coastguard Worker 
277*49fe348cSAndroid Build Coastguard Worker     accu_flush(accu, &bits->buffer);
278*49fe348cSAndroid Build Coastguard Worker 
279*49fe348cSAndroid Build Coastguard Worker     /* --- Accumulate remaining bits -- */
280*49fe348cSAndroid Build Coastguard Worker 
281*49fe348cSAndroid Build Coastguard Worker     accu->v = v >> n1;
282*49fe348cSAndroid Build Coastguard Worker     accu->n = n - n1;
283*49fe348cSAndroid Build Coastguard Worker }
284*49fe348cSAndroid Build Coastguard Worker 
285*49fe348cSAndroid Build Coastguard Worker /**
286*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder renormalization
287*49fe348cSAndroid Build Coastguard Worker  */
lc3_ac_write_renorm(struct lc3_bits * bits)288*49fe348cSAndroid Build Coastguard Worker LC3_HOT void lc3_ac_write_renorm(struct lc3_bits *bits)
289*49fe348cSAndroid Build Coastguard Worker {
290*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_ac *ac = &bits->ac;
291*49fe348cSAndroid Build Coastguard Worker 
292*49fe348cSAndroid Build Coastguard Worker     for ( ; ac->range < 0x10000; ac->range <<= 8)
293*49fe348cSAndroid Build Coastguard Worker         ac_shift(ac, &bits->buffer);
294*49fe348cSAndroid Build Coastguard Worker }
295*49fe348cSAndroid Build Coastguard Worker 
296*49fe348cSAndroid Build Coastguard Worker 
297*49fe348cSAndroid Build Coastguard Worker /* ----------------------------------------------------------------------------
298*49fe348cSAndroid Build Coastguard Worker  *  Reading
299*49fe348cSAndroid Build Coastguard Worker  * -------------------------------------------------------------------------- */
300*49fe348cSAndroid Build Coastguard Worker 
301*49fe348cSAndroid Build Coastguard Worker /**
302*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder get byte
303*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
304*49fe348cSAndroid Build Coastguard Worker  * return          Byte read, 0 on overflow
305*49fe348cSAndroid Build Coastguard Worker  */
ac_get(struct lc3_bits_buffer * buffer)306*49fe348cSAndroid Build Coastguard Worker static inline int ac_get(struct lc3_bits_buffer *buffer)
307*49fe348cSAndroid Build Coastguard Worker {
308*49fe348cSAndroid Build Coastguard Worker     return buffer->p_fw < buffer->end ? *(buffer->p_fw++) : 0;
309*49fe348cSAndroid Build Coastguard Worker }
310*49fe348cSAndroid Build Coastguard Worker 
311*49fe348cSAndroid Build Coastguard Worker /**
312*49fe348cSAndroid Build Coastguard Worker  * Load the accumulator
313*49fe348cSAndroid Build Coastguard Worker  * accu            Bitstream accumulator
314*49fe348cSAndroid Build Coastguard Worker  * buffer          Bitstream buffer
315*49fe348cSAndroid Build Coastguard Worker  */
accu_load(struct lc3_bits_accu * accu,struct lc3_bits_buffer * buffer)316*49fe348cSAndroid Build Coastguard Worker static inline void accu_load(struct lc3_bits_accu *accu,
317*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_buffer *buffer)
318*49fe348cSAndroid Build Coastguard Worker {
319*49fe348cSAndroid Build Coastguard Worker     int nbytes = LC3_MIN(accu->n >> 3, buffer->p_bw - buffer->start);
320*49fe348cSAndroid Build Coastguard Worker 
321*49fe348cSAndroid Build Coastguard Worker     accu->n -= 8 * nbytes;
322*49fe348cSAndroid Build Coastguard Worker 
323*49fe348cSAndroid Build Coastguard Worker     for ( ; nbytes; nbytes--) {
324*49fe348cSAndroid Build Coastguard Worker         accu->v >>= 8;
325*49fe348cSAndroid Build Coastguard Worker         accu->v |= (unsigned)*(--buffer->p_bw) << (LC3_ACCU_BITS - 8);
326*49fe348cSAndroid Build Coastguard Worker     }
327*49fe348cSAndroid Build Coastguard Worker 
328*49fe348cSAndroid Build Coastguard Worker     if (accu->n >= 8) {
329*49fe348cSAndroid Build Coastguard Worker         accu->nover = LC3_MIN(accu->nover + accu->n, LC3_ACCU_BITS);
330*49fe348cSAndroid Build Coastguard Worker         accu->v >>= accu->n;
331*49fe348cSAndroid Build Coastguard Worker         accu->n = 0;
332*49fe348cSAndroid Build Coastguard Worker     }
333*49fe348cSAndroid Build Coastguard Worker }
334*49fe348cSAndroid Build Coastguard Worker 
335*49fe348cSAndroid Build Coastguard Worker /**
336*49fe348cSAndroid Build Coastguard Worker  * Read from 1 to 32 bits,
337*49fe348cSAndroid Build Coastguard Worker  * exceeding the capacity of the accumulator
338*49fe348cSAndroid Build Coastguard Worker  */
lc3_get_bits_generic(struct lc3_bits * bits,int n)339*49fe348cSAndroid Build Coastguard Worker LC3_HOT unsigned lc3_get_bits_generic(struct lc3_bits *bits, int n)
340*49fe348cSAndroid Build Coastguard Worker {
341*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_accu *accu = &bits->accu;
342*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_buffer *buffer = &bits->buffer;
343*49fe348cSAndroid Build Coastguard Worker 
344*49fe348cSAndroid Build Coastguard Worker     /* --- Fulfill accumulator and read -- */
345*49fe348cSAndroid Build Coastguard Worker 
346*49fe348cSAndroid Build Coastguard Worker     accu_load(accu, buffer);
347*49fe348cSAndroid Build Coastguard Worker 
348*49fe348cSAndroid Build Coastguard Worker     int n1 = LC3_MIN(LC3_ACCU_BITS - accu->n, n);
349*49fe348cSAndroid Build Coastguard Worker     unsigned v = (accu->v >> accu->n) & ((1u << n1) - 1);
350*49fe348cSAndroid Build Coastguard Worker     accu->n += n1;
351*49fe348cSAndroid Build Coastguard Worker 
352*49fe348cSAndroid Build Coastguard Worker     /* --- Second round --- */
353*49fe348cSAndroid Build Coastguard Worker 
354*49fe348cSAndroid Build Coastguard Worker     int n2 = n - n1;
355*49fe348cSAndroid Build Coastguard Worker 
356*49fe348cSAndroid Build Coastguard Worker     if (n2) {
357*49fe348cSAndroid Build Coastguard Worker         accu_load(accu, buffer);
358*49fe348cSAndroid Build Coastguard Worker 
359*49fe348cSAndroid Build Coastguard Worker         v |= ((accu->v >> accu->n) & ((1u << n2) - 1)) << n1;
360*49fe348cSAndroid Build Coastguard Worker         accu->n += n2;
361*49fe348cSAndroid Build Coastguard Worker     }
362*49fe348cSAndroid Build Coastguard Worker 
363*49fe348cSAndroid Build Coastguard Worker     return v;
364*49fe348cSAndroid Build Coastguard Worker }
365*49fe348cSAndroid Build Coastguard Worker 
366*49fe348cSAndroid Build Coastguard Worker /**
367*49fe348cSAndroid Build Coastguard Worker  * Arithmetic coder renormalization
368*49fe348cSAndroid Build Coastguard Worker  */
lc3_ac_read_renorm(struct lc3_bits * bits)369*49fe348cSAndroid Build Coastguard Worker LC3_HOT void lc3_ac_read_renorm(struct lc3_bits *bits)
370*49fe348cSAndroid Build Coastguard Worker {
371*49fe348cSAndroid Build Coastguard Worker     struct lc3_bits_ac *ac = &bits->ac;
372*49fe348cSAndroid Build Coastguard Worker 
373*49fe348cSAndroid Build Coastguard Worker     for ( ; ac->range < 0x10000; ac->range <<= 8)
374*49fe348cSAndroid Build Coastguard Worker         ac->low = ((ac->low << 8) | ac_get(&bits->buffer)) & 0xffffff;
375*49fe348cSAndroid Build Coastguard Worker }
376