@ -32,8 +32,14 @@
# include "libavutil/intreadwrite.h"
# include "libavutil/avassert.h"
typedef uint32_t BitBuf ;
# define AV_WBBUF AV_WB32
# define AV_WLBUF AV_WL32
static const int BUF_BITS = 8 * sizeof ( BitBuf ) ;
typedef struct PutBitContext {
uint32_t bit_buf ;
BitBuf bit_buf ;
int bit_left ;
uint8_t * buf , * buf_ptr , * buf_end ;
int size_in_bits ;
@ -57,7 +63,7 @@ static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
s - > buf = buffer ;
s - > buf_end = s - > buf + buffer_size ;
s - > buf_ptr = s - > buf ;
s - > bit_left = 32 ;
s - > bit_left = BUF_BITS ;
s - > bit_buf = 0 ;
}
@ -66,7 +72,7 @@ static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
*/
static inline int put_bits_count ( PutBitContext * s )
{
return ( s - > buf_ptr - s - > buf ) * 8 + 32 - s - > bit_left ;
return ( s - > buf_ptr - s - > buf ) * 8 + BUF_BITS - s - > bit_left ;
}
/**
@ -92,7 +98,7 @@ static inline void rebase_put_bits(PutBitContext *s, uint8_t *buffer,
*/
static inline int put_bits_left ( PutBitContext * s )
{
return ( s - > buf_end - s - > buf_ptr ) * 8 - 32 + s - > bit_left ;
return ( s - > buf_end - s - > buf_ptr ) * 8 - BUF_BITS + s - > bit_left ;
}
/**
@ -101,33 +107,33 @@ static inline int put_bits_left(PutBitContext* s)
static inline void flush_put_bits ( PutBitContext * s )
{
# ifndef BITSTREAM_WRITER_LE
if ( s - > bit_left < 32 )
if ( s - > bit_left < BUF_BITS )
s - > bit_buf < < = s - > bit_left ;
# endif
while ( s - > bit_left < 32 ) {
while ( s - > bit_left < BUF_BITS ) {
av_assert0 ( s - > buf_ptr < s - > buf_end ) ;
# ifdef BITSTREAM_WRITER_LE
* s - > buf_ptr + + = s - > bit_buf ;
s - > bit_buf > > = 8 ;
# else
* s - > buf_ptr + + = s - > bit_buf > > 24 ;
* s - > buf_ptr + + = s - > bit_buf > > ( BUF_BITS - 8 ) ;
s - > bit_buf < < = 8 ;
# endif
s - > bit_left + = 8 ;
}
s - > bit_left = 32 ;
s - > bit_left = BUF_BITS ;
s - > bit_buf = 0 ;
}
static inline void flush_put_bits_le ( PutBitContext * s )
{
while ( s - > bit_left < 32 ) {
while ( s - > bit_left < BUF_BITS ) {
av_assert0 ( s - > buf_ptr < s - > buf_end ) ;
* s - > buf_ptr + + = s - > bit_buf ;
s - > bit_buf > > = 8 ;
s - > bit_left + = 8 ;
}
s - > bit_left = 32 ;
s - > bit_left = BUF_BITS ;
s - > bit_buf = 0 ;
}
@ -161,29 +167,29 @@ void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
* Write up to 31 bits into a bitstream .
* Use put_bits32 to write 32 bits .
*/
static inline void put_bits ( PutBitContext * s , int n , unsigned in t value )
static inline void put_bits ( PutBitContext * s , int n , B itBuf value )
{
unsigned in t bit_buf ;
B itBuf bit_buf ;
int bit_left ;
av_assert2 ( n < = 31 & & value < ( 1U < < n ) ) ;
av_assert2 ( n < = 31 & & value < ( 1UL < < n ) ) ;
bit_buf = s - > bit_buf ;
bit_left = s - > bit_left ;
/* XXX: optimize */
# ifdef BITSTREAM_WRITER_LE
bit_buf | = value < < ( 32 - bit_left ) ;
bit_buf | = value < < ( BUF_BITS - bit_left ) ;
if ( n > = bit_left ) {
if ( 3 < s - > buf_end - s - > buf_ptr ) {
AV_WL32 ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = 4 ;
if ( s - > buf_end - s - > buf_ptr > = sizeof ( BitBuf ) ) {
AV_WLBUF ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = sizeof ( BitBuf ) ;
} else {
av_log ( NULL , AV_LOG_ERROR , " Internal error, put_bits buffer too small \n " ) ;
av_assert2 ( 0 ) ;
}
bit_buf = value > > bit_left ;
bit_left + = 32 ;
bit_left + = BUF_BITS ;
}
bit_left - = n ;
# else
@ -193,14 +199,14 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
} else {
bit_buf < < = bit_left ;
bit_buf | = value > > ( n - bit_left ) ;
if ( 3 < s - > buf_end - s - > buf_ptr ) {
AV_WB32 ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = 4 ;
if ( s - > buf_end - s - > buf_ptr > = sizeof ( BitBuf ) ) {
AV_WBBUF ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = sizeof ( BitBuf ) ;
} else {
av_log ( NULL , AV_LOG_ERROR , " Internal error, put_bits buffer too small \n " ) ;
av_assert2 ( 0 ) ;
}
bit_left + = 32 - n ;
bit_left + = BUF_BITS - n ;
bit_buf = value ;
}
# endif
@ -209,27 +215,27 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
s - > bit_left = bit_left ;
}
static inline void put_bits_le ( PutBitContext * s , int n , unsigned in t value )
static inline void put_bits_le ( PutBitContext * s , int n , B itBuf value )
{
unsigned in t bit_buf ;
B itBuf bit_buf ;
int bit_left ;
av_assert2 ( n < = 31 & & value < ( 1U < < n ) ) ;
av_assert2 ( n < = 31 & & value < ( 1UL < < n ) ) ;
bit_buf = s - > bit_buf ;
bit_left = s - > bit_left ;
bit_buf | = value < < ( 32 - bit_left ) ;
bit_buf | = value < < ( BUF_BITS - bit_left ) ;
if ( n > = bit_left ) {
if ( 3 < s - > buf_end - s - > buf_ptr ) {
AV_WL32 ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = 4 ;
if ( s - > buf_end - s - > buf_ptr > = sizeof ( BitBuf ) ) {
AV_WLBUF ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = sizeof ( BitBuf ) ;
} else {
av_log ( NULL , AV_LOG_ERROR , " Internal error, put_bits buffer too small \n " ) ;
av_assert2 ( 0 ) ;
}
bit_buf = value > > bit_left ;
bit_left + = 32 ;
bit_left + = BUF_BITS ;
}
bit_left - = n ;
@ -249,17 +255,17 @@ static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
*/
static void av_unused put_bits32 ( PutBitContext * s , uint32_t value )
{
unsigned in t bit_buf ;
B itBuf bit_buf ;
int bit_left ;
bit_buf = s - > bit_buf ;
bit_left = s - > bit_left ;
# ifdef BITSTREAM_WRITER_LE
bit_buf | = value < < ( 32 - bit_left ) ;
if ( 3 < s - > buf_end - s - > buf_ptr ) {
AV_WL32 ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = 4 ;
bit_buf | = ( BitBuf ) value < < ( BUF_BITS - bit_left ) ;
if ( s - > buf_end - s - > buf_ptr > = sizeof ( BitBuf ) ) {
AV_WLBUF ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = sizeof ( BitBuf ) ;
} else {
av_log ( NULL , AV_LOG_ERROR , " Internal error, put_bits buffer too small \n " ) ;
av_assert2 ( 0 ) ;
@ -267,10 +273,10 @@ static void av_unused put_bits32(PutBitContext *s, uint32_t value)
bit_buf = ( uint64_t ) value > > bit_left ;
# else
bit_buf = ( uint64_t ) bit_buf < < bit_left ;
bit_buf | = value > > ( 32 - bit_left ) ;
if ( 3 < s - > buf_end - s - > buf_ptr ) {
AV_WB32 ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = 4 ;
bit_buf | = ( BitBuf ) value > > ( BUF_BITS - bit_left ) ;
if ( s - > buf_end - s - > buf_ptr > = sizeof ( BitBuf ) ) {
AV_WBBUF ( s - > buf_ptr , bit_buf ) ;
s - > buf_ptr + = sizeof ( BitBuf ) ;
} else {
av_log ( NULL , AV_LOG_ERROR , " Internal error, put_bits buffer too small \n " ) ;
av_assert2 ( 0 ) ;
@ -333,7 +339,7 @@ static inline uint8_t *put_bits_ptr(PutBitContext *s)
static inline void skip_put_bytes ( PutBitContext * s , int n )
{
av_assert2 ( ( put_bits_count ( s ) & 7 ) = = 0 ) ;
av_assert2 ( s - > bit_left = = 32 ) ;
av_assert2 ( s - > bit_left = = BUF_BITS ) ;
av_assert0 ( n < = s - > buf_end - s - > buf_ptr ) ;
s - > buf_ptr + = n ;
}
@ -346,8 +352,8 @@ static inline void skip_put_bytes(PutBitContext *s, int n)
static inline void skip_put_bits ( PutBitContext * s , int n )
{
s - > bit_left - = n ;
s - > buf_ptr - = 4 * ( s - > bit_left > > 5 ) ;
s - > bit_left & = 3 1;
s - > buf_ptr - = sizeof ( BitBuf ) * ( ( unsigned ) s - > bit_left / BUF_BITS ) ;
s - > bit_left & = ( BUF_BITS - 1 ) ;
}
/**
@ -357,9 +363,12 @@ static inline void skip_put_bits(PutBitContext *s, int n)
*/
static inline void set_put_bits_buffer_size ( PutBitContext * s , int size )
{
av_assert0 ( size < = INT_MAX / 8 - 32 ) ;
av_assert0 ( size < = INT_MAX / 8 - BUF_BITS ) ;
s - > buf_end = s - > buf + size ;
s - > size_in_bits = 8 * size ;
}
# undef AV_WBBUF
# undef AV_WLBUF
# endif /* AVCODEC_PUT_BITS_H */