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