xref: /rockchip-linux_mpp/mpp/base/inc/mpp_bitread.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2 *
3 * Copyright 2015 Rockchip Electronics Co. LTD
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 #ifndef __MPP_BITREAD_H__
18 #define __MPP_BITREAD_H__
19 
20 #include <stdio.h>
21 #include <assert.h>
22 
23 #include "mpp_err.h"
24 #include "mpp_common.h"
25 
26 #define   __BITREAD_ERR   __bitread_error
27 
28 #define READ_ONEBIT(bitctx, out)\
29     do {\
30         RK_S32 _out; \
31         bitctx->ret = mpp_read_bits(bitctx, 1, &_out); \
32         if (!bitctx->ret) { *out = _out; }\
33         else { goto __BITREAD_ERR; }\
34     } while (0)
35 
36 #define READ_BITS(bitctx, num_bits, out)\
37     do {\
38         RK_S32 _out; \
39         bitctx->ret = mpp_read_bits(bitctx, num_bits, &_out); \
40         if (!bitctx->ret) { *out = _out; }\
41         else { goto __BITREAD_ERR; }\
42     } while (0)
43 
44 #define READ_BITS_LONG(bitctx, num_bits, out)\
45     do {\
46         RK_U32 _out; \
47         bitctx->ret = mpp_read_longbits(bitctx, num_bits, &_out); \
48         if (!bitctx->ret) { *out = _out; }\
49         else { goto __BITREAD_ERR; }\
50     } while (0)
51 
52 #define SHOW_BITS(bitctx, num_bits, out)\
53     do {\
54         RK_S32 _out; \
55         bitctx->ret = mpp_show_bits(bitctx, num_bits, &_out); \
56         if (!bitctx->ret) { *out = _out; }\
57         else { goto __BITREAD_ERR; }\
58     } while (0)
59 
60 #define SHOW_BITS_LONG(bitctx, num_bits, out)\
61     do {\
62         RK_U32 _out; \
63         bitctx->ret = mpp_show_longbits(bitctx, num_bits, &_out); \
64         if (!bitctx->ret) { *out = _out; }\
65         else { goto __BITREAD_ERR; }\
66     } while (0)
67 
68 #define SKIP_BITS(bitctx, num_bits)\
69     do {\
70         bitctx->ret = mpp_skip_longbits(bitctx, num_bits); \
71         if (bitctx->ret) { goto __BITREAD_ERR; }\
72     } while (0)
73 
74 #define SKIP_BITS_LONG(bitctx, num_bits)\
75     do {\
76         bitctx->ret = mpp_skip_longbits(bitctx, num_bits); \
77         if (bitctx->ret) { goto __BITREAD_ERR; }\
78     } while (0)
79 
80 #define READ_UE(bitctx, out)\
81     do {\
82         RK_U32 _out; \
83         bitctx->ret = mpp_read_ue(bitctx, &_out); \
84         if (!bitctx->ret) { *out = _out; }\
85         else { goto __BITREAD_ERR; }\
86     } while (0)
87 
88 #define READ_SE(bitctx, out)\
89     do {\
90         RK_S32 _out; \
91         bitctx->ret = mpp_read_se(bitctx, &_out); \
92         if (!bitctx->ret) { *out = _out; }\
93         else { goto __BITREAD_ERR; }\
94     } while (0)
95 
96 typedef enum  PseudoCodeType_e {
97     PSEUDO_CODE_NONE = 0,
98     PSEUDO_CODE_H264_H265,
99     PSEUDO_CODE_H264_H265_SEI,
100     PSEUDO_CODE_AVS2,
101     PSEUDO_CODE_BUT
102 } PseudoCodeType;
103 
104 typedef struct bitread_ctx_t {
105     // Pointer to the next unread (not in curr_byte_) byte in the stream.
106     RK_U8 *data_;
107     // Bytes left in the stream (without the curr_byte_).
108     RK_U32 bytes_left_;
109     // Contents of the current byte; first unread bit starting at position
110     // 8 - num_remaining_bits_in_curr_byte_ from MSB.
111     RK_S64 curr_byte_;
112     // Number of bits remaining in curr_byte_
113     RK_S32 num_remaining_bits_in_curr_byte_;
114     // Used in emulation prevention three byte detection (see spec).
115     // Initially set to 0xffff to accept all initial two-byte sequences.
116     RK_S64 prev_two_bytes_;
117     // Number of emulation presentation bytes (0x000003) we met.
118     RK_S64 emulation_prevention_bytes_;
119     // count PPS SPS SEI read bits
120     RK_S32 used_bits;
121     RK_U8  *buf;
122     RK_S32 buf_len;
123     // ctx
124     MPP_RET   ret;
125     PseudoCodeType prevention_type;
126     MPP_RET (*update_curbyte)(struct bitread_ctx_t *bitctx);
127 } BitReadCtx_t;
128 
129 
130 #ifdef  __cplusplus
131 extern "C" {
132 #endif
133 
134 //!< set bit read context
135 void    mpp_set_bitread_ctx(BitReadCtx_t *bitctx, RK_U8 *data, RK_S32 size);
136 
137 //!< Read bits (1-31)
138 MPP_RET mpp_read_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out);
139 
140 //!< Read bits (1-32)
141 MPP_RET mpp_read_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out);
142 
143 //!< Show bits (1-31)
144 MPP_RET mpp_show_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out);
145 
146 //!< Show bits (1-32)
147 MPP_RET mpp_show_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out);
148 
149 //!< skip bits(1-31)
150 MPP_RET mpp_skip_bits(BitReadCtx_t *bitctx, RK_S32 num_bits);
151 
152 //!< skip bits(1-32)
153 MPP_RET mpp_skip_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits);
154 
155 //!< read ue(1-32)
156 MPP_RET mpp_read_ue(BitReadCtx_t *bitctx, RK_U32* val);
157 
158 //!< read se(1-31)
159 MPP_RET mpp_read_se(BitReadCtx_t *bitctx, RK_S32* val);
160 
161 void mpp_set_bitread_pseudo_code_type(BitReadCtx_t *bitctx, PseudoCodeType type);
162 
163 //!< check whether has more rbsp data(used in h264)
164 RK_U32  mpp_has_more_rbsp_data(BitReadCtx_t * bitctx);
165 
166 //!< align bits and get current pointer
167 RK_U8  *mpp_align_get_bits(BitReadCtx_t *bitctx);
168 
169 RK_S32 mpp_get_bits_left(BitReadCtx_t *bitctx);
170 
171 RK_S32 mpp_get_bits_count(BitReadCtx_t *bitctx);
172 
173 #ifdef  __cplusplus
174 }
175 #endif
176 
177 
178 #endif /* __MPP_BITREAD_H__ */
179