xref: /aosp_15_r20/external/libdav1d/src/getbits.h (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1*c0909341SAndroid Build Coastguard Worker /*
2*c0909341SAndroid Build Coastguard Worker  * Copyright © 2018, VideoLAN and dav1d authors
3*c0909341SAndroid Build Coastguard Worker  * Copyright © 2018, Two Orioles, LLC
4*c0909341SAndroid Build Coastguard Worker  * All rights reserved.
5*c0909341SAndroid Build Coastguard Worker  *
6*c0909341SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
7*c0909341SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions are met:
8*c0909341SAndroid Build Coastguard Worker  *
9*c0909341SAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
10*c0909341SAndroid Build Coastguard Worker  *    list of conditions and the following disclaimer.
11*c0909341SAndroid Build Coastguard Worker  *
12*c0909341SAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
13*c0909341SAndroid Build Coastguard Worker  *    this list of conditions and the following disclaimer in the documentation
14*c0909341SAndroid Build Coastguard Worker  *    and/or other materials provided with the distribution.
15*c0909341SAndroid Build Coastguard Worker  *
16*c0909341SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17*c0909341SAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*c0909341SAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*c0909341SAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20*c0909341SAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*c0909341SAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*c0909341SAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23*c0909341SAndroid Build Coastguard Worker  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*c0909341SAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25*c0909341SAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*c0909341SAndroid Build Coastguard Worker  */
27*c0909341SAndroid Build Coastguard Worker 
28*c0909341SAndroid Build Coastguard Worker #ifndef DAV1D_SRC_GETBITS_H
29*c0909341SAndroid Build Coastguard Worker #define DAV1D_SRC_GETBITS_H
30*c0909341SAndroid Build Coastguard Worker 
31*c0909341SAndroid Build Coastguard Worker #include <stddef.h>
32*c0909341SAndroid Build Coastguard Worker #include <stdint.h>
33*c0909341SAndroid Build Coastguard Worker 
34*c0909341SAndroid Build Coastguard Worker typedef struct GetBits {
35*c0909341SAndroid Build Coastguard Worker     uint64_t state;
36*c0909341SAndroid Build Coastguard Worker     int bits_left, error;
37*c0909341SAndroid Build Coastguard Worker     const uint8_t *ptr, *ptr_start, *ptr_end;
38*c0909341SAndroid Build Coastguard Worker } GetBits;
39*c0909341SAndroid Build Coastguard Worker 
40*c0909341SAndroid Build Coastguard Worker void dav1d_init_get_bits(GetBits *c, const uint8_t *data, size_t sz);
41*c0909341SAndroid Build Coastguard Worker unsigned dav1d_get_bit(GetBits *c);
42*c0909341SAndroid Build Coastguard Worker unsigned dav1d_get_bits(GetBits *c, int n);
43*c0909341SAndroid Build Coastguard Worker int dav1d_get_sbits(GetBits *c, int n);
44*c0909341SAndroid Build Coastguard Worker unsigned dav1d_get_uleb128(GetBits *c);
45*c0909341SAndroid Build Coastguard Worker 
46*c0909341SAndroid Build Coastguard Worker // Output in range 0..max-1
47*c0909341SAndroid Build Coastguard Worker unsigned dav1d_get_uniform(GetBits *c, unsigned max);
48*c0909341SAndroid Build Coastguard Worker unsigned dav1d_get_vlc(GetBits *c);
49*c0909341SAndroid Build Coastguard Worker int dav1d_get_bits_subexp(GetBits *c, int ref, unsigned n);
50*c0909341SAndroid Build Coastguard Worker 
51*c0909341SAndroid Build Coastguard Worker // Discard bits from the buffer until we're next byte-aligned.
dav1d_bytealign_get_bits(GetBits * c)52*c0909341SAndroid Build Coastguard Worker static inline void dav1d_bytealign_get_bits(GetBits *c) {
53*c0909341SAndroid Build Coastguard Worker     // bits_left is never more than 7, because it is only incremented
54*c0909341SAndroid Build Coastguard Worker     // by refill(), called by dav1d_get_bits and that never reads more
55*c0909341SAndroid Build Coastguard Worker     // than 7 bits more than it needs.
56*c0909341SAndroid Build Coastguard Worker     //
57*c0909341SAndroid Build Coastguard Worker     // If this wasn't true, we would need to work out how many bits to
58*c0909341SAndroid Build Coastguard Worker     // discard (bits_left % 8), subtract that from bits_left and then
59*c0909341SAndroid Build Coastguard Worker     // shift state right by that amount.
60*c0909341SAndroid Build Coastguard Worker     assert(c->bits_left <= 7);
61*c0909341SAndroid Build Coastguard Worker 
62*c0909341SAndroid Build Coastguard Worker     c->bits_left = 0;
63*c0909341SAndroid Build Coastguard Worker     c->state = 0;
64*c0909341SAndroid Build Coastguard Worker }
65*c0909341SAndroid Build Coastguard Worker 
66*c0909341SAndroid Build Coastguard Worker // Return the current bit position relative to the start of the buffer.
dav1d_get_bits_pos(const GetBits * c)67*c0909341SAndroid Build Coastguard Worker static inline unsigned dav1d_get_bits_pos(const GetBits *c) {
68*c0909341SAndroid Build Coastguard Worker     return (unsigned) (c->ptr - c->ptr_start) * 8 - c->bits_left;
69*c0909341SAndroid Build Coastguard Worker }
70*c0909341SAndroid Build Coastguard Worker 
71*c0909341SAndroid Build Coastguard Worker #endif /* DAV1D_SRC_GETBITS_H */
72