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 #if 0
85 #if defined(X86F) || defined(ARMV8N_64)
86 /* on X86 machine, 32-bit shift means remaining of original value, so we
87 should set zero in that case. */
88 #define BSR_SKIP_CODE(bs, size) \
89 oapv_assert((bs)->leftbits >= (size)); \
90 if((size) == 32) {(bs)->code = 0; (bs)->leftbits = 0;} \
91 else {(bs)->code <<= (size); (bs)->leftbits -= (size);}
92 #else
93 #define BSR_SKIP_CODE(bs, size) \
94 oapv_assert((bs)->leftbits >= (size)); \
95 (bs)->code <<= (size); (bs)->leftbits -= (size);
96 #endif
97 #else
98 #define BSR_SKIP_CODE(bs, size) \
99 oapv_assert((bs)->leftbits >= (size) && (size) <= 32); \
100 (bs)->code <<= (size); (bs)->leftbits -= (size);
101 #endif
102
103 /*! Is end of bitstream ? */
104 #define BSR_IS_EOB(bs) (((bs)->cur > (bs)->end && (bs)->leftbits==0)? 1: 0)
105
106 /*! Is bitstream byte aligned? */
107 #define BSR_IS_BYTE_ALIGN(bs) ((((bs)->leftbits & 0x7) == 0)? 1: 0)
108
109 /*! Is last byte of bitsteam? */
110 #define BSR_IS_LAST_BYTE(bs) \
111 (((bs)->cur > (bs)->end && bs->leftbits > 0 && (bs)->leftbits <= 8)? 1: 0)
112
113 /* get left byte count in BS */
114 #define BSR_GET_LEFT_BYTE(bs) \
115 ((int)((bs)->end - (bs)->cur) + 1 + ((bs)->leftbits >> 3))
116 /* get number of byte consumed */
117 #define BSR_GET_READ_BYTE(bs) \
118 ((int)((bs)->cur - (bs)->beg) - ((bs)->leftbits >> 3))
119 /* get number of bit consumed */
120 #define BSR_GET_READ_BIT(bs) \
121 (((int)((bs)->cur - (bs)->beg) << 3) - ((bs)->leftbits))
122
123 /* get address of current reading */
124 #define BSR_GET_CUR(bs) ((bs)->cur - (((bs)->leftbits + 7) >> 3))
125
126 /* move to # bytes align position */
127 #define BSR_MOVE_BYTE_ALIGN(bs, byte) \
128 (bs)->cur += (byte) - ((bs)->leftbits >> 3); \
129 (bs)->code = 0; \
130 (bs)->leftbits = 0;
131
132 void oapv_bsr_init(oapv_bs_t *bs, u8 *buf, u32 size, oapv_bs_fn_flush_t fn_flush);
133 int oapv_bsr_clz_in_code(u32 code);
134 int oapv_bsr_clz(oapv_bs_t *bs);
135 void oapv_bsr_align8(oapv_bs_t *bs);
136 void oapv_bsr_skip(oapv_bs_t *bs, int size);
137 u32 oapv_bsr_peek(oapv_bs_t *bs, int size);
138 void *oapv_bsr_sink(oapv_bs_t *bs);
139 void oapv_bsr_move(oapv_bs_t *bs, u8 *pos);
140 u32 oapv_bsr_read(oapv_bs_t *bs, int size);
141 int oapv_bsr_read1(oapv_bs_t *bs);
142 u32 oapv_bsr_read_direct(void *addr, int len);
143
144 ///////////////////////////////////////////////////////////////////////////////
145 // end of decoder code
146 #endif // ENABLE_DECODER
147 ///////////////////////////////////////////////////////////////////////////////
148
149 #endif /* _OAPV_BSR_H_ */
150