2 * Copyright (c) 2016 Alexandra Hájková
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef BITSTREAM_TEMPLATE_LE
22 # define BS_SUFFIX_LOWER _le
23 # define BS_SUFFIX_UPPER LE
25 # define BS_SUFFIX_LOWER _be
26 # define BS_SUFFIX_UPPER BE
29 #define BS_JOIN(x, y, z) x ## y ## z
30 #define BS_JOIN3(x, y, z) BS_JOIN(x, y, z)
31 #define BS_FUNC(x) BS_JOIN3(bits_, x, BS_SUFFIX_LOWER)
33 #define BSCTX BS_JOIN3(Bitstream, Context, BS_SUFFIX_UPPER)
35 typedef struct BSCTX
{
36 uint64_t bits
; // stores bits read from the buffer
37 const uint8_t *buffer
, *buffer_end
;
38 const uint8_t *ptr
; // pointer to the position inside a buffer
39 unsigned bits_valid
; // number of bits left in bits field
40 unsigned size_in_bits
;
45 * - 0 on successful refill
46 * - a negative number when bitstream end is hit
48 * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled.
50 static inline int BS_FUNC(priv_refill_64
)(BSCTX
*bc
)
52 #if !UNCHECKED_BITSTREAM_READER
53 if (bc
->ptr
>= bc
->buffer_end
)
57 #ifdef BITSTREAM_TEMPLATE_LE
58 bc
->bits
= AV_RL64(bc
->ptr
);
60 bc
->bits
= AV_RB64(bc
->ptr
);
70 * - 0 on successful refill
71 * - a negative number when bitstream end is hit
73 * Always succeeds when UNCHECKED_BITSTREAM_READER is enabled.
75 static inline int BS_FUNC(priv_refill_32
)(BSCTX
*bc
)
77 #if !UNCHECKED_BITSTREAM_READER
78 if (bc
->ptr
>= bc
->buffer_end
)
82 #ifdef BITSTREAM_TEMPLATE_LE
83 bc
->bits
|= (uint64_t)AV_RL32(bc
->ptr
) << bc
->bits_valid
;
85 bc
->bits
|= (uint64_t)AV_RB32(bc
->ptr
) << (32 - bc
->bits_valid
);
94 * Initialize BitstreamContext.
95 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
96 * larger than the actual read bits because some optimized bitstream
97 * readers read 32 or 64 bits at once and could read over the end
98 * @param bit_size the size of the buffer in bits
99 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
101 static inline int BS_FUNC(init
)(BSCTX
*bc
, const uint8_t *buffer
,
102 unsigned int bit_size
)
104 unsigned int buffer_size
;
106 if (bit_size
> INT_MAX
- 7 || !buffer
) {
110 return AVERROR_INVALIDDATA
;
113 buffer_size
= (bit_size
+ 7) >> 3;
116 bc
->buffer_end
= buffer
+ buffer_size
;
117 bc
->ptr
= bc
->buffer
;
118 bc
->size_in_bits
= bit_size
;
122 BS_FUNC(priv_refill_64
)(bc
);
128 * Initialize BitstreamContext.
129 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
130 * larger than the actual read bits because some optimized bitstream
131 * readers read 32 or 64 bits at once and could read over the end
132 * @param byte_size the size of the buffer in bytes
133 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow
135 static inline int BS_FUNC(init8
)(BSCTX
*bc
, const uint8_t *buffer
,
136 unsigned int byte_size
)
138 if (byte_size
> INT_MAX
/ 8)
139 return AVERROR_INVALIDDATA
;
140 return BS_FUNC(init
)(bc
, buffer
, byte_size
* 8);
144 * Return number of bits already read.
146 static inline int BS_FUNC(tell
)(const BSCTX
*bc
)
148 return (bc
->ptr
- bc
->buffer
) * 8 - bc
->bits_valid
;
152 * Return buffer size in bits.
154 static inline int BS_FUNC(size
)(const BSCTX
*bc
)
156 return bc
->size_in_bits
;
160 * Return the number of the bits left in a buffer.
162 static inline int BS_FUNC(left
)(const BSCTX
*bc
)
164 return (bc
->buffer
- bc
->ptr
) * 8 + bc
->size_in_bits
+ bc
->bits_valid
;
167 static inline uint64_t BS_FUNC(priv_val_show
)(BSCTX
*bc
, unsigned int n
)
169 av_assert2(n
> 0 && n
<= 64);
171 #ifdef BITSTREAM_TEMPLATE_LE
172 return bc
->bits
& (UINT64_MAX
>> (64 - n
));
174 return bc
->bits
>> (64 - n
);
178 static inline void BS_FUNC(priv_skip_remaining
)(BSCTX
*bc
, unsigned int n
)
180 #ifdef BITSTREAM_TEMPLATE_LE
188 static inline uint64_t BS_FUNC(priv_val_get
)(BSCTX
*bc
, unsigned int n
)
192 av_assert2(n
> 0 && n
< 64);
194 ret
= BS_FUNC(priv_val_show
)(bc
, n
);
195 BS_FUNC(priv_skip_remaining
)(bc
, n
);
201 * Return one bit from the buffer.
203 static inline unsigned int BS_FUNC(read_bit
)(BSCTX
*bc
)
205 if (!bc
->bits_valid
&& BS_FUNC(priv_refill_64
)(bc
) < 0)
208 return BS_FUNC(priv_val_get
)(bc
, 1);
212 * Return n bits from the buffer, n has to be in the 1-32 range.
213 * May be faster than bits_read() when n is not a compile-time constant and is
214 * known to be non-zero;
216 static inline uint32_t BS_FUNC(read_nz
)(BSCTX
*bc
, unsigned int n
)
218 av_assert2(n
> 0 && n
<= 32);
220 if (n
> bc
->bits_valid
) {
221 if (BS_FUNC(priv_refill_32
)(bc
) < 0)
225 return BS_FUNC(priv_val_get
)(bc
, n
);
229 * Return n bits from the buffer, n has to be in the 0-32 range.
231 static inline uint32_t BS_FUNC(read
)(BSCTX
*bc
, unsigned int n
)
238 return BS_FUNC(read_nz
)(bc
, n
);
242 * Return n bits from the buffer, n has to be in the 0-63 range.
244 static inline uint64_t BS_FUNC(read_63
)(BSCTX
*bc
, unsigned int n
)
254 if (n
> bc
->bits_valid
) {
255 left
= bc
->bits_valid
;
259 ret
= BS_FUNC(priv_val_get
)(bc
, left
);
261 if (BS_FUNC(priv_refill_64
)(bc
) < 0)
266 #ifdef BITSTREAM_TEMPLATE_LE
267 ret
= BS_FUNC(priv_val_get
)(bc
, n
) << left
| ret
;
269 ret
= BS_FUNC(priv_val_get
)(bc
, n
) | ret
<< n
;
276 * Return n bits from the buffer, n has to be in the 0-64 range.
278 static inline uint64_t BS_FUNC(read_64
)(BSCTX
*bc
, unsigned int n
)
283 uint64_t ret
= BS_FUNC(read_63
)(bc
, 63);
284 #ifdef BITSTREAM_TEMPLATE_LE
285 return ret
| ((uint64_t)BS_FUNC(read_bit
)(bc
) << 63);
287 return (ret
<< 1) | (uint64_t)BS_FUNC(read_bit
)(bc
);
290 return BS_FUNC(read_63
)(bc
, n
);
294 * Return n bits from the buffer as a signed integer, n has to be in the 1-32
295 * range. May be faster than bits_read_signed() when n is not a compile-time
296 * constant and is known to be non-zero;
298 static inline int32_t BS_FUNC(read_signed_nz
)(BSCTX
*bc
, unsigned int n
)
300 av_assert2(n
> 0 && n
<= 32);
301 return sign_extend(BS_FUNC(read_nz
)(bc
, n
), n
);
305 * Return n bits from the buffer as a signed integer.
306 * n has to be in the 0-32 range.
308 static inline int32_t BS_FUNC(read_signed
)(BSCTX
*bc
, unsigned int n
)
315 return BS_FUNC(read_signed_nz
)(bc
, n
);
319 * Return n bits from the buffer but do not change the buffer state.
320 * n has to be in the 1-32 range. May
322 static inline uint32_t BS_FUNC(peek_nz
)(BSCTX
*bc
, unsigned int n
)
324 av_assert2(n
> 0 && n
<= 32);
326 if (n
> bc
->bits_valid
)
327 BS_FUNC(priv_refill_32
)(bc
);
329 return BS_FUNC(priv_val_show
)(bc
, n
);
333 * Return n bits from the buffer but do not change the buffer state.
334 * n has to be in the 0-32 range.
336 static inline uint32_t BS_FUNC(peek
)(BSCTX
*bc
, unsigned int n
)
343 return BS_FUNC(peek_nz
)(bc
, n
);
347 * Return n bits from the buffer as a signed integer, do not change the buffer
348 * state. n has to be in the 1-32 range. May be faster than bits_peek_signed()
349 * when n is not a compile-time constant and is known to be non-zero;
351 static inline int BS_FUNC(peek_signed_nz
)(BSCTX
*bc
, unsigned int n
)
353 av_assert2(n
> 0 && n
<= 32);
354 return sign_extend(BS_FUNC(peek_nz
)(bc
, n
), n
);
358 * Return n bits from the buffer as a signed integer,
359 * do not change the buffer state.
360 * n has to be in the 0-32 range.
362 static inline int BS_FUNC(peek_signed
)(BSCTX
*bc
, unsigned int n
)
369 return BS_FUNC(peek_signed_nz
)(bc
, n
);
373 * Skip n bits in the buffer.
375 static inline void BS_FUNC(skip
)(BSCTX
*bc
, unsigned int n
)
377 if (n
< bc
->bits_valid
)
378 BS_FUNC(priv_skip_remaining
)(bc
, n
);
385 unsigned int skip
= n
/ 8;
390 BS_FUNC(priv_refill_64
)(bc
);
392 BS_FUNC(priv_skip_remaining
)(bc
, n
);
397 * Seek to the given bit position.
399 static inline void BS_FUNC(seek
)(BSCTX
*bc
, unsigned pos
)
401 bc
->ptr
= bc
->buffer
;
405 BS_FUNC(skip
)(bc
, pos
);
409 * Skip bits to a byte boundary.
411 static inline const uint8_t *BS_FUNC(align
)(BSCTX
*bc
)
413 unsigned int n
= -BS_FUNC(tell
)(bc
) & 7;
415 BS_FUNC(skip
)(bc
, n
);
416 return bc
->buffer
+ (BS_FUNC(tell
)(bc
) >> 3);
420 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
421 * If MSB not set it is negative.
422 * @param n length in bits
424 static inline int BS_FUNC(read_xbits
)(BSCTX
*bc
, unsigned int n
)
426 int32_t cache
= BS_FUNC(peek
)(bc
, 32);
427 int sign
= ~cache
>> 31;
428 BS_FUNC(priv_skip_remaining
)(bc
, n
);
430 return ((((uint32_t)(sign
^ cache
)) >> (32 - n
)) ^ sign
) - sign
;
434 * Return decoded truncated unary code for the values 0, 1, 2.
436 static inline int BS_FUNC(decode012
)(BSCTX
*bc
)
438 if (!BS_FUNC(read_bit
)(bc
))
441 return BS_FUNC(read_bit
)(bc
) + 1;
445 * Return decoded truncated unary code for the values 2, 1, 0.
447 static inline int BS_FUNC(decode210
)(BSCTX
*bc
)
449 if (BS_FUNC(read_bit
)(bc
))
452 return 2 - BS_FUNC(read_bit
)(bc
);
455 /* Read sign bit and flip the sign of the provided value accordingly. */
456 static inline int BS_FUNC(apply_sign
)(BSCTX
*bc
, int val
)
458 int sign
= BS_FUNC(read_signed
)(bc
, 1);
459 return (val
^ sign
) - sign
;
462 static inline int BS_FUNC(skip_1stop_8data
)(BSCTX
*s
)
464 if (BS_FUNC(left
)(s
) <= 0)
465 return AVERROR_INVALIDDATA
;
467 while (BS_FUNC(read_bit
)(s
)) {
469 if (BS_FUNC(left
)(s
) <= 0)
470 return AVERROR_INVALIDDATA
;
477 * Return the LUT element for the given bitstream configuration.
479 static inline int BS_FUNC(priv_set_idx
)(BSCTX
*bc
, int code
, int *n
,
480 int *nb_bits
, const VLCElem
*table
)
485 idx
= BS_FUNC(peek
)(bc
, *nb_bits
) + code
;
488 return table
[idx
].sym
;
493 * @param bits is the number of bits which will be read at once, must be
494 * identical to nb_bits in vlc_init()
495 * @param max_depth is the number of times bits bits must be read to completely
496 * read the longest vlc code
497 * = (max_vlc_length + bits - 1) / bits
498 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
499 * If the vlc code is invalid and max_depth>1, then the number of bits removed
502 static inline int BS_FUNC(read_vlc
)(BSCTX
*bc
, const VLCElem
*table
,
503 int bits
, int max_depth
)
506 unsigned idx
= BS_FUNC(peek
)(bc
, bits
);
507 int code
= table
[idx
].sym
;
508 int n
= table
[idx
].len
;
510 if (max_depth
> 1 && n
< 0) {
511 BS_FUNC(priv_skip_remaining
)(bc
, bits
);
512 code
= BS_FUNC(priv_set_idx
)(bc
, code
, &n
, &nb_bits
, table
);
513 if (max_depth
> 2 && n
< 0) {
514 BS_FUNC(priv_skip_remaining
)(bc
, nb_bits
);
515 code
= BS_FUNC(priv_set_idx
)(bc
, code
, &n
, &nb_bits
, table
);
518 BS_FUNC(priv_skip_remaining
)(bc
, n
);
524 * Parse a vlc / vlc_multi code.
525 * @param bits is the number of bits which will be read at once, must be
526 * identical to nb_bits in vlc_init()
527 * @param max_depth is the number of times bits bits must be read to completely
528 * read the longest vlc code
529 * = (max_vlc_length + bits - 1) / bits
530 * @param dst the parsed symbol(s) will be stored here. Up to 8 bytes are written
531 * @returns number of symbols parsed
532 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
533 * If the vlc code is invalid and max_depth>1, then the number of bits removed
536 static inline int BS_FUNC(read_vlc_multi
)(BSCTX
*bc
, uint8_t dst
[8],
537 const VLC_MULTI_ELEM
*const Jtable
,
538 const VLCElem
*const table
,
539 const int bits
, const int max_depth
,
540 const int symbols_size
)
542 unsigned idx
= BS_FUNC(peek
)(bc
, bits
);
543 int ret
, nb_bits
, code
, n
= Jtable
[idx
].len
;
544 if (Jtable
[idx
].num
) {
545 AV_COPY64U(dst
, Jtable
[idx
].val8
);
546 ret
= Jtable
[idx
].num
;
548 code
= table
[idx
].sym
;
550 if (max_depth
> 1 && n
< 0) {
551 BS_FUNC(priv_skip_remaining
)(bc
, bits
);
552 code
= BS_FUNC(priv_set_idx
)(bc
, code
, &n
, &nb_bits
, table
);
553 if (max_depth
> 2 && n
< 0) {
554 BS_FUNC(priv_skip_remaining
)(bc
, nb_bits
);
555 code
= BS_FUNC(priv_set_idx
)(bc
, code
, &n
, &nb_bits
, table
);
558 if (symbols_size
== 1)
564 BS_FUNC(priv_skip_remaining
)(bc
, n
);
573 #undef BS_SUFFIX_UPPER
574 #undef BS_SUFFIX_LOWER