nengel@2: /* nengel@2: * copyright (c) 2004 Michael Niedermayer nengel@2: * nengel@2: * This file is part of FFmpeg. nengel@2: * nengel@2: * FFmpeg is free software; you can redistribute it and/or nengel@2: * modify it under the terms of the GNU Lesser General Public nengel@2: * License as published by the Free Software Foundation; either nengel@2: * version 2.1 of the License, or (at your option) any later version. nengel@2: * nengel@2: * FFmpeg is distributed in the hope that it will be useful, nengel@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of nengel@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nengel@2: * Lesser General Public License for more details. nengel@2: * nengel@2: * You should have received a copy of the GNU Lesser General Public nengel@2: * License along with FFmpeg; if not, write to the Free Software nengel@2: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA nengel@2: */ nengel@2: nengel@2: /** nengel@2: * @file nengel@2: * bitstream reader API header. nengel@2: */ nengel@2: nengel@2: #ifndef AVCODEC_GET_BITS_H nengel@2: #define AVCODEC_GET_BITS_H nengel@2: nengel@2: #include nengel@2: #include nengel@2: #include nengel@2: #include "libavutil/bswap.h" nengel@2: #include "libavutil/common.h" nengel@2: #include "libavutil/intreadwrite.h" nengel@2: #include "libavutil/log.h" nengel@2: #include "mathops.h" nengel@2: nengel@2: nengel@2: typedef struct GetBitContext { nengel@2: uint8_t *rbsp; nengel@2: unsigned int rbsp_size; nengel@2: uint8_t *raw; nengel@2: const uint8_t *buffer, *buffer_end; nengel@2: unsigned int alloc_size; nengel@2: unsigned int buf_size; nengel@2: uint32_t *buffer_ptr; nengel@2: uint32_t cache0; nengel@2: uint32_t cache1; nengel@2: int bit_count; nengel@2: int size_in_bits; nengel@2: } GetBitContext; nengel@2: nengel@2: /* Bitstream reader API docs: nengel@2: name nengel@2: arbitrary name which is used as prefix for the internal variables nengel@2: nengel@2: gb nengel@2: getbitcontext nengel@2: nengel@2: OPEN_READER(name, gb) nengel@2: loads gb into local variables nengel@2: nengel@2: CLOSE_READER(name, gb) nengel@2: stores local vars in gb nengel@2: nengel@2: UPDATE_CACHE(name, gb) nengel@2: refills the internal cache from the bitstream nengel@2: after this call at least MIN_CACHE_BITS will be available, nengel@2: nengel@2: GET_CACHE(name, gb) nengel@2: will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) nengel@2: nengel@2: SHOW_UBITS(name, gb, num) nengel@2: will return the next num bits nengel@2: nengel@2: SHOW_SBITS(name, gb, num) nengel@2: will return the next num bits and do sign extension nengel@2: nengel@2: SKIP_BITS(name, gb, num) nengel@2: will skip over the next num bits nengel@2: note, this is equivalent to SKIP_CACHE; SKIP_COUNTER nengel@2: nengel@2: SKIP_CACHE(name, gb, num) nengel@2: will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) nengel@2: nengel@2: SKIP_COUNTER(name, gb, num) nengel@2: will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) nengel@2: nengel@2: LAST_SKIP_CACHE(name, gb, num) nengel@2: will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing nengel@2: nengel@2: LAST_SKIP_BITS(name, gb, num) nengel@2: is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER nengel@2: nengel@2: for examples see get_bits, show_bits, skip_bits, get_vlc nengel@2: */ nengel@2: nengel@2: #define MIN_CACHE_BITS 32 nengel@2: nengel@2: #define OPEN_READER(name, gb)\ nengel@2: int name##_bit_count=(gb)->bit_count;\ nengel@2: uint32_t name##_cache0= (gb)->cache0;\ nengel@2: uint32_t name##_cache1= (gb)->cache1;\ nengel@2: uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ nengel@2: nengel@2: #define CLOSE_READER(name, gb)\ nengel@2: (gb)->bit_count= name##_bit_count;\ nengel@2: (gb)->cache0= name##_cache0;\ nengel@2: (gb)->cache1= name##_cache1;\ nengel@2: (gb)->buffer_ptr= name##_buffer_ptr;\ nengel@2: nengel@2: #define UPDATE_CACHE(name, gb)\ nengel@2: if(name##_bit_count > 0){\ nengel@2: const uint32_t next= be2me_32( *name##_buffer_ptr );\ nengel@2: name##_cache0 |= NEG_USR32(next,name##_bit_count);\ nengel@2: name##_cache1 |= next<buffer_ptr - s->buffer)*8 - 32 + s->bit_count; nengel@2: } nengel@2: nengel@2: static inline void skip_bits_long(GetBitContext *s, int n){ nengel@2: OPEN_READER(re, s) nengel@2: re_bit_count += n; nengel@2: re_buffer_ptr += re_bit_count>>5; nengel@2: re_bit_count &= 31; nengel@2: re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count; nengel@2: re_cache1 = 0; nengel@2: UPDATE_CACHE(re, s) nengel@2: CLOSE_READER(re, s) nengel@2: } nengel@2: nengel@2: /** nengel@2: * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). nengel@2: * if MSB not set it is negative nengel@2: * @param n length in bits nengel@2: * @author BERO nengel@2: */ nengel@2: static inline int get_xbits(GetBitContext *s, int n){ nengel@2: register int sign; nengel@2: register int32_t cache; nengel@2: OPEN_READER(re, s) nengel@2: UPDATE_CACHE(re, s) nengel@2: cache = GET_CACHE(re,s); nengel@2: sign=(~cache)>>31; nengel@2: LAST_SKIP_BITS(re, s, n) nengel@2: CLOSE_READER(re, s) nengel@2: return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; nengel@2: } nengel@2: nengel@2: static inline int get_sbits(GetBitContext *s, int n){ nengel@2: register int tmp; nengel@2: OPEN_READER(re, s) nengel@2: UPDATE_CACHE(re, s) nengel@2: tmp= SHOW_SBITS(re, s, n); nengel@2: LAST_SKIP_BITS(re, s, n) nengel@2: CLOSE_READER(re, s) nengel@2: return tmp; nengel@2: } nengel@2: nengel@2: /** nengel@2: * reads 1-17 bits. nengel@2: * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't nengel@2: */ nengel@2: static inline unsigned int get_bits(GetBitContext *s, int n){ nengel@2: register int tmp; nengel@2: OPEN_READER(re, s) nengel@2: UPDATE_CACHE(re, s) nengel@2: tmp= SHOW_UBITS(re, s, n); nengel@2: LAST_SKIP_BITS(re, s, n) nengel@2: CLOSE_READER(re, s) nengel@2: return tmp; nengel@2: } nengel@2: nengel@2: /** nengel@2: * shows 1-17 bits. nengel@2: * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't nengel@2: */ nengel@2: static inline unsigned int show_bits(GetBitContext *s, int n){ nengel@2: register int tmp; nengel@2: OPEN_READER(re, s) nengel@2: UPDATE_CACHE(re, s) nengel@2: tmp= SHOW_UBITS(re, s, n); nengel@2: // CLOSE_READER(re, s) nengel@2: return tmp; nengel@2: } nengel@2: nengel@2: static inline void skip_bits(GetBitContext *s, int n){ nengel@2: //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) nengel@2: OPEN_READER(re, s) nengel@2: UPDATE_CACHE(re, s) nengel@2: LAST_SKIP_BITS(re, s, n) nengel@2: CLOSE_READER(re, s) nengel@2: } nengel@2: nengel@2: static inline unsigned int get_bits1(GetBitContext *s){ nengel@2: return get_bits(s, 1); nengel@2: } nengel@2: nengel@2: static inline unsigned int show_bits1(GetBitContext *s){ nengel@2: return show_bits(s, 1); nengel@2: } nengel@2: nengel@2: static inline void skip_bits1(GetBitContext *s){ nengel@2: skip_bits(s, 1); nengel@2: } nengel@2: nengel@2: /** nengel@2: * reads 0-32 bits. nengel@2: */ nengel@2: static inline unsigned int get_bits_long(GetBitContext *s, int n){ nengel@2: if(n<=MIN_CACHE_BITS) return get_bits(s, n); nengel@2: else{ nengel@2: int ret= get_bits(s, 16) << (n-16); nengel@2: return ret | get_bits(s, n-16); nengel@2: } nengel@2: } nengel@2: nengel@2: /** nengel@2: * reads 0-32 bits as a signed integer. nengel@2: */ nengel@2: static inline int get_sbits_long(GetBitContext *s, int n) { nengel@2: return sign_extend(get_bits_long(s, n), n); nengel@2: } nengel@2: nengel@2: /** nengel@2: * shows 0-32 bits. nengel@2: */ nengel@2: static inline unsigned int show_bits_long(GetBitContext *s, int n){ nengel@2: if(n<=MIN_CACHE_BITS) return show_bits(s, n); nengel@2: else{ nengel@2: GetBitContext gb= *s; nengel@2: return get_bits_long(&gb, n); nengel@2: } nengel@2: } nengel@2: nengel@2: static inline int check_marker(GetBitContext *s, const char *msg) nengel@2: { nengel@2: int bit= get_bits1(s); nengel@2: if(!bit) nengel@2: av_log(AV_LOG_INFO, "Marker bit missing %s\n", msg); nengel@2: nengel@2: return bit; nengel@2: } nengel@2: nengel@2: /** nengel@2: * init GetBitContext. nengel@2: * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits nengel@2: * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end nengel@2: * @param bit_size the size of the buffer in bits nengel@2: * nengel@2: * While GetBitContext stores the buffer size, for performance reasons you are nengel@2: * responsible for checking for the buffer end yourself (take advantage of the padding)! nengel@2: */ nengel@2: static inline void init_get_bits(GetBitContext *s, nengel@2: const uint8_t *buffer, int bit_size) nengel@2: { nengel@2: int buffer_size= (bit_size+7)>>3; nengel@2: if(buffer_size < 0 || bit_size < 0) { nengel@2: buffer_size = bit_size = 0; nengel@2: buffer = NULL; nengel@2: } nengel@2: nengel@2: s->buffer= buffer; nengel@2: s->size_in_bits= bit_size; nengel@2: s->buffer_end= buffer + buffer_size; nengel@2: nengel@2: s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3)); nengel@2: s->bit_count = 32 + 8*((intptr_t)buffer&3); nengel@2: skip_bits_long(s, 0); nengel@2: } nengel@2: nengel@2: static inline void align_get_bits(GetBitContext *s) nengel@2: { nengel@2: int n= (-get_bits_count(s)) & 7; nengel@2: if(n) skip_bits(s, n); nengel@2: } nengel@2: nengel@2: #define tprintf(p, ...) {} nengel@2: nengel@2: static inline int get_bits_left(GetBitContext *gb) nengel@2: { nengel@2: return gb->size_in_bits - get_bits_count(gb); nengel@2: } nengel@2: nengel@2: #endif /* AVCODEC_GET_BITS_H */