xref: /aosp_15_r20/external/libopenapv/src/oapv_bs.h (revision abb65b4b03b69e1d508d4d9a44dcf199df16e7c3)
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the copyright owner, nor the names of its contributors
16  *   may be used to endorse or promote products derived from this software
17  *   without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _OAPV_BSR_H_
33 #define _OAPV_BSR_H_
34 
35 #include "oapv_port.h"
36 
37 typedef struct oapv_bs oapv_bs_t;
38 typedef int (*oapv_bs_fn_flush_t)(oapv_bs_t *bs, int byte);
39 
40 struct oapv_bs {
41     u32                code;     // intermediate code buffer
42     int                leftbits; // left bits count in code
43     u8                *cur;      // address of current bitstream position
44     u8                *end;      // address of bitstream end
45     u8                *beg;      // address of bitstream begin
46     u32                size;     // size of input bitstream in byte
47     oapv_bs_fn_flush_t fn_flush; // function pointer for flush operation
48     int                ndata[4]; // arbitrary data, if needs
49     void              *pdata[4]; // arbitrary address, if needs
50     char               is_bin_count;
51     u32                bin_count;
52 };
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 // start of encoder code
56 #if ENABLE_ENCODER
57 ///////////////////////////////////////////////////////////////////////////////
58 
bsw_is_align8(oapv_bs_t * bs)59 static inline bool bsw_is_align8(oapv_bs_t *bs)
60 {
61     return (bool)(!((bs)->leftbits & 0x7));
62 }
63 
bsw_get_write_byte(oapv_bs_t * bs)64 static inline int bsw_get_write_byte(oapv_bs_t *bs)
65 {
66     return (int)((u8 *)(bs->cur) - (u8 *)(bs->beg));
67 }
68 
69 void oapv_bsw_init(oapv_bs_t *bs, u8 *buf, int size, oapv_bs_fn_flush_t fn_flush);
70 void oapv_bsw_deinit(oapv_bs_t *bs);
71 void *oapv_bsw_sink(oapv_bs_t *bs);
72 int oapv_bsw_write_direct(void *bits, u32 val, int len);
73 int oapv_bsw_write1(oapv_bs_t *bs, int val);
74 int oapv_bsw_write(oapv_bs_t *bs, u32 val, int len);
75 ///////////////////////////////////////////////////////////////////////////////
76 // end of encoder code
77 #endif // ENABLE_ENCODER
78 ///////////////////////////////////////////////////////////////////////////////
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 // start of decoder code
82 #if ENABLE_DECODER
83 ///////////////////////////////////////////////////////////////////////////////
84 /*! is bitstream byte aligned? */
bsr_is_align8(oapv_bs_t * bs)85 static bool inline bsr_is_align8(oapv_bs_t *bs)
86 {
87     return ((bs->leftbits & 0x7) == 0) ? true : false;
88 }
89 
90 /* get number of byte consumed */
bsr_get_read_byte(oapv_bs_t * bs)91 static int inline bsr_get_read_byte(oapv_bs_t *bs)
92 {
93     return ((int)((bs)->cur - (bs)->beg) - ((bs)->leftbits >> 3));
94 }
95 
bsr_get_remained_byte(oapv_bs_t * bs)96 static int inline bsr_get_remained_byte(oapv_bs_t *bs)
97 {
98     return (bs->size - bsr_get_read_byte(bs));
99 }
100 
101 void oapv_bsr_init(oapv_bs_t *bs, u8 *buf, int size, oapv_bs_fn_flush_t fn_flush);
102 int oapv_bsr_clz_in_code(u32 code);
103 void oapv_bsr_align8(oapv_bs_t *bs);
104 void oapv_bsr_skip(oapv_bs_t *bs, int size);
105 void oapv_bsr_peek(oapv_bs_t *bs, u32 *val, int size);
106 void *oapv_bsr_sink(oapv_bs_t *bs);
107 void oapv_bsr_move(oapv_bs_t *bs, u8 *pos);
108 u32 oapv_bsr_read(oapv_bs_t *bs, int size);
109 int oapv_bsr_read1(oapv_bs_t *bs);
110 
111 ///////////////////////////////////////////////////////////////////////////////
112 // end of decoder code
113 #endif // ENABLE_DECODER
114 ///////////////////////////////////////////////////////////////////////////////
115 
116 #endif /* _OAPV_BSR_H_ */
117