xref: /rockchip-linux_mpp/mpp/base/mpp_bitread.c (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 
18 #include <stdlib.h>
19 #include <string.h>
20 #include "rk_type.h"
21 #include "mpp_mem.h"
22 #include "mpp_bitread.h"
23 
update_curbyte_default(BitReadCtx_t * bitctx)24 static MPP_RET update_curbyte_default(BitReadCtx_t *bitctx)
25 {
26     if (bitctx->bytes_left_ < 1)
27         return  MPP_ERR_READ_BIT;
28 
29     // Load a new byte and advance pointers.
30     bitctx->curr_byte_ = *bitctx->data_++ & 0xff;
31     --bitctx->bytes_left_;
32     bitctx->num_remaining_bits_in_curr_byte_ = 8;
33     bitctx->prev_two_bytes_ = (bitctx->prev_two_bytes_ << 8) | bitctx->curr_byte_;
34 
35     return MPP_OK;
36 }
37 
update_curbyte_h264(BitReadCtx_t * bitctx)38 static MPP_RET update_curbyte_h264(BitReadCtx_t *bitctx)
39 {
40     if (bitctx->bytes_left_ < 1)
41         return  MPP_ERR_READ_BIT;
42 
43     // Emulation prevention three-byte detection.
44     // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
45     if ((*bitctx->data_ == 0x03)
46         && ((bitctx->prev_two_bytes_ & 0xffff) == 0)) {
47         // Detected 0x000003, skip last byte.
48         ++bitctx->data_;
49         --bitctx->bytes_left_;
50         ++bitctx->emulation_prevention_bytes_;
51         bitctx->used_bits += 8;
52         // Need another full three bytes before we can detect the sequence again.
53         bitctx->prev_two_bytes_ = 0xffff;
54         if (bitctx->bytes_left_ < 1)
55             return  MPP_ERR_READ_BIT;
56     }
57     // Load a new byte and advance pointers.
58     bitctx->curr_byte_ = *bitctx->data_++ & 0xff;
59     --bitctx->bytes_left_;
60     bitctx->num_remaining_bits_in_curr_byte_ = 8;
61     bitctx->prev_two_bytes_ = (bitctx->prev_two_bytes_ << 8) | bitctx->curr_byte_;
62 
63     return MPP_OK;
64 }
65 
update_curbyte_h2645_sei(BitReadCtx_t * bitctx)66 static MPP_RET update_curbyte_h2645_sei(BitReadCtx_t *bitctx)
67 {
68     if (bitctx->bytes_left_ < 1)
69         return  MPP_ERR_READ_BIT;
70 
71     // Emulation prevention three-byte detection.
72     // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
73     if ((*bitctx->data_ == 0x03)
74         && ((bitctx->prev_two_bytes_ & 0xffff) == 0)) {
75         // Detected 0x000003, skip last byte.
76         ++bitctx->data_;
77         // Need another full three bytes before we can detect the sequence again.
78         bitctx->prev_two_bytes_ = 0xffff;
79     }
80     // Load a new byte and advance pointers.
81     bitctx->curr_byte_ = *bitctx->data_++ & 0xff;
82     --bitctx->bytes_left_;
83     bitctx->num_remaining_bits_in_curr_byte_ = 8;
84     bitctx->prev_two_bytes_ = (bitctx->prev_two_bytes_ << 8) | bitctx->curr_byte_;
85 
86     return MPP_OK;
87 }
88 
update_curbyte_avs2(BitReadCtx_t * bitctx)89 static MPP_RET update_curbyte_avs2(BitReadCtx_t *bitctx)
90 {
91     if (bitctx->bytes_left_ < 1)
92         return MPP_ERR_READ_BIT;
93 
94     if (*bitctx->data_ == 0x02 && ((bitctx->prev_two_bytes_ & 0xffff) == 0)) {
95         // Detected 0x000002, get 2 bits from next byte
96         bitctx->curr_byte_ = 0;
97         bitctx->num_remaining_bits_in_curr_byte_ = 6;
98         ++bitctx->data_;
99     } else {
100         bitctx->curr_byte_ = *bitctx->data_++ & 0xff;
101         bitctx->num_remaining_bits_in_curr_byte_ = 8;
102     }
103     // Load a new byte and advance pointers.
104     bitctx->prev_two_bytes_ = (bitctx->prev_two_bytes_ << 8) | bitctx->curr_byte_;
105     --bitctx->bytes_left_;
106 
107     return MPP_OK;
108 }
109 
110 /*!
111 ***********************************************************************
112 * \brief
113 *   Read |num_bits| (1 to 31 inclusive) from the stream and return them
114 *   in |out|, with first bit in the stream as MSB in |out| at position
115 *   (|num_bits| - 1)
116 ***********************************************************************
117 */
mpp_read_bits(BitReadCtx_t * bitctx,RK_S32 num_bits,RK_S32 * out)118 MPP_RET mpp_read_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out)
119 {
120     RK_S32 bits_left = num_bits;
121     *out = 0;
122     if (num_bits > 31) {
123         return  MPP_ERR_READ_BIT;
124     }
125     while (bitctx->num_remaining_bits_in_curr_byte_ < bits_left) {
126         // Take all that's left in current byte, shift to make space for the rest.
127         *out |= (bitctx->curr_byte_ << (bits_left - bitctx->num_remaining_bits_in_curr_byte_));
128         bits_left -= bitctx->num_remaining_bits_in_curr_byte_;
129         if (bitctx->update_curbyte(bitctx)) {
130             return  MPP_ERR_READ_BIT;
131         }
132     }
133     *out |= (bitctx->curr_byte_ >> (bitctx->num_remaining_bits_in_curr_byte_ - bits_left));
134     *out &= ((1 << num_bits) - 1);
135     bitctx->num_remaining_bits_in_curr_byte_ -= bits_left;
136     bitctx->used_bits += num_bits;
137 
138     return MPP_OK;
139 }
140 /*!
141 ***********************************************************************
142 * \brief
143 *   read more than 32 bits data
144 ***********************************************************************
145 */
mpp_read_longbits(BitReadCtx_t * bitctx,RK_S32 num_bits,RK_U32 * out)146 MPP_RET mpp_read_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out)
147 {
148     RK_S32 val = 0, val1 = 0;
149 
150     if (num_bits < 32)
151         return mpp_read_bits(bitctx, num_bits, (RK_S32 *)out);
152 
153     if (mpp_read_bits(bitctx, 16, &val)) {
154         return  MPP_ERR_READ_BIT;
155     }
156     if (mpp_read_bits(bitctx, (num_bits - 16), &val1)) {
157         return  MPP_ERR_READ_BIT;
158     }
159 
160     *out = (RK_U32)((val << 16) | val1);
161 
162     return MPP_OK;
163 }
164 /*!
165 ***********************************************************************
166 * \brief
167 *   skip bits (0 - 31)
168 ***********************************************************************
169 */
mpp_skip_bits(BitReadCtx_t * bitctx,RK_S32 num_bits)170 MPP_RET mpp_skip_bits(BitReadCtx_t *bitctx, RK_S32 num_bits)
171 {
172     RK_S32 bits_left = num_bits;
173 
174     while (bitctx->num_remaining_bits_in_curr_byte_ < bits_left) {
175         // Take all that's left in current byte, shift to make space for the rest.
176         bits_left -= bitctx->num_remaining_bits_in_curr_byte_;
177         if (bitctx->update_curbyte(bitctx)) {
178             return  MPP_ERR_READ_BIT;
179         }
180     }
181     bitctx->num_remaining_bits_in_curr_byte_ -= bits_left;
182     bitctx->used_bits += num_bits;
183 
184     return MPP_OK;
185 }
186 /*!
187 ***********************************************************************
188 * \brief
189 *   skip bits long (0 - 32)
190 ***********************************************************************
191 */
mpp_skip_longbits(BitReadCtx_t * bitctx,RK_S32 num_bits)192 MPP_RET mpp_skip_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits)
193 {
194     if (num_bits < 32)
195         return mpp_skip_bits(bitctx, num_bits);
196 
197     if (mpp_skip_bits(bitctx, 16)) {
198         return  MPP_ERR_READ_BIT;
199     }
200     if (mpp_skip_bits(bitctx, (num_bits - 16))) {
201         return  MPP_ERR_READ_BIT;
202     }
203     return MPP_OK;
204 }
205 /*!
206 ***********************************************************************
207 * \brief
208 *   show bits (0 - 31)
209 ***********************************************************************
210 */
mpp_show_bits(BitReadCtx_t * bitctx,RK_S32 num_bits,RK_S32 * out)211 MPP_RET mpp_show_bits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_S32 *out)
212 {
213     MPP_RET ret = MPP_ERR_UNKNOW;
214     BitReadCtx_t tmp_ctx = *bitctx;
215 
216     if (num_bits < 32)
217         ret = mpp_read_bits(&tmp_ctx, num_bits, out);
218     else
219         ret = mpp_read_longbits(&tmp_ctx, num_bits, (RK_U32 *)out);
220 
221     return ret;
222 }
223 /*!
224 ***********************************************************************
225 * \brief
226 *   show long bits (0 - 32)
227 ***********************************************************************
228 */
mpp_show_longbits(BitReadCtx_t * bitctx,RK_S32 num_bits,RK_U32 * out)229 MPP_RET mpp_show_longbits(BitReadCtx_t *bitctx, RK_S32 num_bits, RK_U32 *out)
230 {
231     MPP_RET ret = MPP_ERR_UNKNOW;
232     BitReadCtx_t tmp_ctx = *bitctx;
233 
234     ret = mpp_read_longbits(&tmp_ctx, num_bits, out);
235 
236     return ret;
237 }
238 /*!
239 ***********************************************************************
240 * \brief
241 *   read unsigned data
242 ***********************************************************************
243 */
mpp_read_ue(BitReadCtx_t * bitctx,RK_U32 * val)244 MPP_RET mpp_read_ue(BitReadCtx_t *bitctx, RK_U32 *val)
245 {
246     RK_S32 num_bits = -1;
247     RK_S32 bit;
248     RK_S32 rest;
249     // Count the number of contiguous zero bits.
250     do {
251         if (mpp_read_bits(bitctx, 1, &bit)) {
252             return  MPP_ERR_READ_BIT;
253         }
254         num_bits++;
255     } while (bit == 0);
256     if (num_bits > 31) {
257         return  MPP_ERR_READ_BIT;
258     }
259     // Calculate exp-Golomb code value of size num_bits.
260     *val = (1 << num_bits) - 1;
261     if (num_bits > 0) {
262         if (mpp_read_bits(bitctx, num_bits, &rest)) {
263             return  MPP_ERR_READ_BIT;
264         }
265         *val += rest;
266     }
267 
268     return MPP_OK;
269 }
270 /*!
271 ***********************************************************************
272 * \brief
273 *   read signed data
274 ***********************************************************************
275 */
mpp_read_se(BitReadCtx_t * bitctx,RK_S32 * val)276 MPP_RET mpp_read_se(BitReadCtx_t *bitctx, RK_S32 *val)
277 {
278     RK_U32 ue;
279 
280     if (mpp_read_ue(bitctx, &ue)) {
281         return  MPP_ERR_READ_BIT;
282     }
283     if (ue % 2 == 0) { // odd
284         *val = -(RK_S32)(ue >> 1);
285     } else {
286         *val = (RK_S32)((ue >> 1) + 1);
287     }
288     return MPP_OK;
289 }
290 
291 /*!
292 ***********************************************************************
293 * \brief
294 *   check whether has more rbsp data
295 ***********************************************************************
296 */
mpp_has_more_rbsp_data(BitReadCtx_t * bitctx)297 RK_U32 mpp_has_more_rbsp_data(BitReadCtx_t *bitctx)
298 {
299     // remove tail byte which equal zero
300     while (bitctx->bytes_left_ &&
301            bitctx->data_[bitctx->bytes_left_ - 1] == 0)
302         bitctx->bytes_left_--;
303 
304     // Make sure we have more bits, if we are at 0 bits in current byte
305     // and updating current byte fails, we don't have more data anyway.
306     if (bitctx->num_remaining_bits_in_curr_byte_ == 0 && bitctx->update_curbyte(bitctx))
307         return 0;
308     // On last byte?
309     if (bitctx->bytes_left_)
310         return 1;
311     // Last byte, look for stop bit;
312     // We have more RBSP data if the last non-zero bit we find is not the
313     // first available bit.
314     if (bitctx->num_remaining_bits_in_curr_byte_)
315         return (bitctx->curr_byte_ &
316                 ((1 << (bitctx->num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
317     else
318         return 0;
319 }
320 /*!
321 ***********************************************************************
322 * \brief
323 *   initialize bit read context
324 ***********************************************************************
325 */
mpp_set_bitread_ctx(BitReadCtx_t * bitctx,RK_U8 * data,RK_S32 size)326 void mpp_set_bitread_ctx(BitReadCtx_t *bitctx, RK_U8 *data, RK_S32 size)
327 {
328     memset(bitctx, 0, sizeof(BitReadCtx_t));
329     bitctx->data_ = data;
330     bitctx->bytes_left_ = size;
331     bitctx->num_remaining_bits_in_curr_byte_ = 0;
332     // Initially set to 0xffff to accept all initial two-byte sequences.
333     bitctx->prev_two_bytes_ = 0xffff;
334     bitctx->emulation_prevention_bytes_ = 0;
335     // add
336     bitctx->buf = data;
337     bitctx->buf_len = size;
338     bitctx->used_bits = 0;
339     mpp_set_bitread_pseudo_code_type(bitctx, PSEUDO_CODE_NONE);
340 }
341 
mpp_set_bitread_pseudo_code_type(BitReadCtx_t * bitctx,PseudoCodeType type)342 void mpp_set_bitread_pseudo_code_type(BitReadCtx_t *bitctx, PseudoCodeType type)
343 {
344     bitctx->prevention_type = type;
345     switch (type) {
346     case PSEUDO_CODE_H264_H265:
347         bitctx->update_curbyte = update_curbyte_h264;
348         break;
349     case PSEUDO_CODE_H264_H265_SEI:
350         bitctx->update_curbyte = update_curbyte_h2645_sei;
351         break;
352     case PSEUDO_CODE_AVS2:
353         bitctx->update_curbyte = update_curbyte_avs2;
354         break;
355     default:
356         bitctx->update_curbyte = update_curbyte_default;
357         break;
358     }
359 }
360 
361 /*!
362 ***********************************************************************
363 * \brief
364 *   align data and get current point
365 ***********************************************************************
366 */
mpp_align_get_bits(BitReadCtx_t * bitctx)367 RK_U8 *mpp_align_get_bits(BitReadCtx_t *bitctx)
368 {
369     int n = bitctx->num_remaining_bits_in_curr_byte_;
370     if (n)
371         mpp_skip_bits(bitctx, n);
372     return bitctx->data_;
373 }
374 
mpp_get_bits_left(BitReadCtx_t * bitctx)375 RK_S32 mpp_get_bits_left(BitReadCtx_t *bitctx)
376 {
377     return  bitctx->bytes_left_ * 8 + bitctx->num_remaining_bits_in_curr_byte_;
378 }
379 
mpp_get_bits_count(BitReadCtx_t * bitctx)380 RK_S32 mpp_get_bits_count(BitReadCtx_t *bitctx)
381 {
382     return bitctx->used_bits;
383 }
384