diff libavcodec/get_bits.h @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libavcodec/get_bits.h	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
     1.6 + *
     1.7 + * This file is part of FFmpeg.
     1.8 + *
     1.9 + * FFmpeg is free software; you can redistribute it and/or
    1.10 + * modify it under the terms of the GNU Lesser General Public
    1.11 + * License as published by the Free Software Foundation; either
    1.12 + * version 2.1 of the License, or (at your option) any later version.
    1.13 + *
    1.14 + * FFmpeg is distributed in the hope that it will be useful,
    1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 + * Lesser General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU Lesser General Public
    1.20 + * License along with FFmpeg; if not, write to the Free Software
    1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    1.22 + */
    1.23 +
    1.24 +/**
    1.25 + * @file
    1.26 + * bitstream reader API header.
    1.27 + */
    1.28 +
    1.29 +#ifndef AVCODEC_GET_BITS_H
    1.30 +#define AVCODEC_GET_BITS_H
    1.31 +
    1.32 +#include <stdint.h>
    1.33 +#include <stdlib.h>
    1.34 +#include <assert.h>
    1.35 +#include "libavutil/bswap.h"
    1.36 +#include "libavutil/common.h"
    1.37 +#include "libavutil/intreadwrite.h"
    1.38 +#include "libavutil/log.h"
    1.39 +#include "mathops.h"
    1.40 +
    1.41 +
    1.42 +typedef struct GetBitContext {
    1.43 +    uint8_t *rbsp;
    1.44 +    unsigned int rbsp_size;
    1.45 +    uint8_t *raw;
    1.46 +    const uint8_t *buffer, *buffer_end;
    1.47 +    unsigned int alloc_size;
    1.48 +    unsigned int buf_size;
    1.49 +    uint32_t *buffer_ptr;
    1.50 +    uint32_t cache0;
    1.51 +    uint32_t cache1;
    1.52 +    int bit_count;
    1.53 +    int size_in_bits;
    1.54 +} GetBitContext;
    1.55 +
    1.56 +/* Bitstream reader API docs:
    1.57 +name
    1.58 +    arbitrary name which is used as prefix for the internal variables
    1.59 +
    1.60 +gb
    1.61 +    getbitcontext
    1.62 +
    1.63 +OPEN_READER(name, gb)
    1.64 +    loads gb into local variables
    1.65 +
    1.66 +CLOSE_READER(name, gb)
    1.67 +    stores local vars in gb
    1.68 +
    1.69 +UPDATE_CACHE(name, gb)
    1.70 +    refills the internal cache from the bitstream
    1.71 +    after this call at least MIN_CACHE_BITS will be available,
    1.72 +
    1.73 +GET_CACHE(name, gb)
    1.74 +    will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
    1.75 +
    1.76 +SHOW_UBITS(name, gb, num)
    1.77 +    will return the next num bits
    1.78 +
    1.79 +SHOW_SBITS(name, gb, num)
    1.80 +    will return the next num bits and do sign extension
    1.81 +
    1.82 +SKIP_BITS(name, gb, num)
    1.83 +    will skip over the next num bits
    1.84 +    note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
    1.85 +
    1.86 +SKIP_CACHE(name, gb, num)
    1.87 +    will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
    1.88 +
    1.89 +SKIP_COUNTER(name, gb, num)
    1.90 +    will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
    1.91 +
    1.92 +LAST_SKIP_CACHE(name, gb, num)
    1.93 +    will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
    1.94 +
    1.95 +LAST_SKIP_BITS(name, gb, num)
    1.96 +    is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER
    1.97 +
    1.98 +for examples see get_bits, show_bits, skip_bits, get_vlc
    1.99 +*/
   1.100 +
   1.101 +#define MIN_CACHE_BITS 32
   1.102 +
   1.103 +#define OPEN_READER(name, gb)\
   1.104 +	int name##_bit_count=(gb)->bit_count;\
   1.105 +	uint32_t name##_cache0= (gb)->cache0;\
   1.106 +	uint32_t name##_cache1= (gb)->cache1;\
   1.107 +	uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\
   1.108 +
   1.109 +#define CLOSE_READER(name, gb)\
   1.110 +	(gb)->bit_count= name##_bit_count;\
   1.111 +	(gb)->cache0= name##_cache0;\
   1.112 +	(gb)->cache1= name##_cache1;\
   1.113 +	(gb)->buffer_ptr= name##_buffer_ptr;\
   1.114 +
   1.115 +#define UPDATE_CACHE(name, gb)\
   1.116 +	if(name##_bit_count > 0){\
   1.117 +		const uint32_t next= be2me_32( *name##_buffer_ptr );\
   1.118 +		name##_cache0 |= NEG_USR32(next,name##_bit_count);\
   1.119 +		name##_cache1 |= next<<name##_bit_count;\
   1.120 +		name##_buffer_ptr++;\
   1.121 +		name##_bit_count-= 32;\
   1.122 +	}\
   1.123 +
   1.124 +#if ARCH_X86
   1.125 +#   define SKIP_CACHE(name, gb, num)\
   1.126 +        __asm__(\
   1.127 +            "shldl %2, %1, %0          \n\t"\
   1.128 +            "shll %2, %1               \n\t"\
   1.129 +            : "+r" (name##_cache0), "+r" (name##_cache1)\
   1.130 +            : "Ic" ((uint8_t)(num))\
   1.131 +           );
   1.132 +#else
   1.133 +#   define SKIP_CACHE(name, gb, num)\
   1.134 +        name##_cache0 <<= (num);\
   1.135 +        name##_cache0 |= NEG_USR32(name##_cache1,num);\
   1.136 +        name##_cache1 <<= (num);
   1.137 +#endif
   1.138 +
   1.139 +#define SKIP_COUNTER(name, gb, num)\
   1.140 +	name##_bit_count += (num);\
   1.141 +
   1.142 +#define SKIP_BITS(name, gb, num)\
   1.143 +	{\
   1.144 +		SKIP_CACHE(name, gb, num)\
   1.145 +		SKIP_COUNTER(name, gb, num)\
   1.146 +	}\
   1.147 +
   1.148 +#define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num)
   1.149 +#define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num)
   1.150 +
   1.151 +#define SHOW_UBITS(name, gb, num)\
   1.152 +	NEG_USR32(name##_cache0, num)
   1.153 +
   1.154 +#define SHOW_SBITS(name, gb, num)\
   1.155 +        NEG_SSR32(name##_cache0, num)
   1.156 +
   1.157 +#define GET_CACHE(name, gb)\
   1.158 +	(name##_cache0)
   1.159 +
   1.160 +static inline int get_bits_count(const GetBitContext *s){
   1.161 +    return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count;
   1.162 +}
   1.163 +
   1.164 +static inline void skip_bits_long(GetBitContext *s, int n){
   1.165 +    OPEN_READER(re, s)
   1.166 +    re_bit_count += n;
   1.167 +    re_buffer_ptr += re_bit_count>>5;
   1.168 +    re_bit_count &= 31;
   1.169 +    re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count;
   1.170 +    re_cache1 = 0;
   1.171 +    UPDATE_CACHE(re, s)
   1.172 +    CLOSE_READER(re, s)
   1.173 +}
   1.174 +
   1.175 +/**
   1.176 + * read mpeg1 dc style vlc (sign bit + mantisse with no MSB).
   1.177 + * if MSB not set it is negative
   1.178 + * @param n length in bits
   1.179 + * @author BERO
   1.180 + */
   1.181 +static inline int get_xbits(GetBitContext *s, int n){
   1.182 +    register int sign;
   1.183 +    register int32_t cache;
   1.184 +    OPEN_READER(re, s)
   1.185 +    UPDATE_CACHE(re, s)
   1.186 +    cache = GET_CACHE(re,s);
   1.187 +    sign=(~cache)>>31;
   1.188 +    LAST_SKIP_BITS(re, s, n)
   1.189 +    CLOSE_READER(re, s)
   1.190 +    return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
   1.191 +}
   1.192 +
   1.193 +static inline int get_sbits(GetBitContext *s, int n){
   1.194 +    register int tmp;
   1.195 +    OPEN_READER(re, s)
   1.196 +    UPDATE_CACHE(re, s)
   1.197 +    tmp= SHOW_SBITS(re, s, n);
   1.198 +    LAST_SKIP_BITS(re, s, n)
   1.199 +    CLOSE_READER(re, s)
   1.200 +    return tmp;
   1.201 +}
   1.202 +
   1.203 +/**
   1.204 + * reads 1-17 bits.
   1.205 + * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
   1.206 + */
   1.207 +static inline unsigned int get_bits(GetBitContext *s, int n){
   1.208 +    register int tmp;
   1.209 +    OPEN_READER(re, s)
   1.210 +    UPDATE_CACHE(re, s)
   1.211 +    tmp= SHOW_UBITS(re, s, n);
   1.212 +    LAST_SKIP_BITS(re, s, n)
   1.213 +    CLOSE_READER(re, s)
   1.214 +    return tmp;
   1.215 +}
   1.216 +
   1.217 +/**
   1.218 + * shows 1-17 bits.
   1.219 + * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
   1.220 + */
   1.221 +static inline unsigned int show_bits(GetBitContext *s, int n){
   1.222 +    register int tmp;
   1.223 +    OPEN_READER(re, s)
   1.224 +    UPDATE_CACHE(re, s)
   1.225 +    tmp= SHOW_UBITS(re, s, n);
   1.226 +//    CLOSE_READER(re, s)
   1.227 +    return tmp;
   1.228 +}
   1.229 +
   1.230 +static inline void skip_bits(GetBitContext *s, int n){
   1.231 + //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
   1.232 +    OPEN_READER(re, s)
   1.233 +    UPDATE_CACHE(re, s)
   1.234 +    LAST_SKIP_BITS(re, s, n)
   1.235 +    CLOSE_READER(re, s)
   1.236 +}
   1.237 +
   1.238 +static inline unsigned int get_bits1(GetBitContext *s){
   1.239 +    return get_bits(s, 1);
   1.240 +}
   1.241 +
   1.242 +static inline unsigned int show_bits1(GetBitContext *s){
   1.243 +    return show_bits(s, 1);
   1.244 +}
   1.245 +
   1.246 +static inline void skip_bits1(GetBitContext *s){
   1.247 +    skip_bits(s, 1);
   1.248 +}
   1.249 +
   1.250 +/**
   1.251 + * reads 0-32 bits.
   1.252 + */
   1.253 +static inline unsigned int get_bits_long(GetBitContext *s, int n){
   1.254 +    if(n<=MIN_CACHE_BITS) return get_bits(s, n);
   1.255 +    else{
   1.256 +        int ret= get_bits(s, 16) << (n-16);
   1.257 +        return ret | get_bits(s, n-16);
   1.258 +    }
   1.259 +}
   1.260 +
   1.261 +/**
   1.262 + * reads 0-32 bits as a signed integer.
   1.263 + */
   1.264 +static inline int get_sbits_long(GetBitContext *s, int n) {
   1.265 +    return sign_extend(get_bits_long(s, n), n);
   1.266 +}
   1.267 +
   1.268 +/**
   1.269 + * shows 0-32 bits.
   1.270 + */
   1.271 +static inline unsigned int show_bits_long(GetBitContext *s, int n){
   1.272 +    if(n<=MIN_CACHE_BITS) return show_bits(s, n);
   1.273 +    else{
   1.274 +        GetBitContext gb= *s;
   1.275 +        return get_bits_long(&gb, n);
   1.276 +    }
   1.277 +}
   1.278 +
   1.279 +static inline int check_marker(GetBitContext *s, const char *msg)
   1.280 +{
   1.281 +    int bit= get_bits1(s);
   1.282 +    if(!bit)
   1.283 +        av_log(AV_LOG_INFO, "Marker bit missing %s\n", msg);
   1.284 +
   1.285 +    return bit;
   1.286 +}
   1.287 +
   1.288 +/**
   1.289 + * init GetBitContext.
   1.290 + * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
   1.291 + * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
   1.292 + * @param bit_size the size of the buffer in bits
   1.293 + *
   1.294 + * While GetBitContext stores the buffer size, for performance reasons you are
   1.295 + * responsible for checking for the buffer end yourself (take advantage of the padding)!
   1.296 + */
   1.297 +static inline void init_get_bits(GetBitContext *s,
   1.298 +                   const uint8_t *buffer, int bit_size)
   1.299 +{
   1.300 +    int buffer_size= (bit_size+7)>>3;
   1.301 +    if(buffer_size < 0 || bit_size < 0) {
   1.302 +        buffer_size = bit_size = 0;
   1.303 +        buffer = NULL;
   1.304 +    }
   1.305 +
   1.306 +    s->buffer= buffer;
   1.307 +    s->size_in_bits= bit_size;
   1.308 +    s->buffer_end= buffer + buffer_size;
   1.309 +
   1.310 +    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));
   1.311 +    s->bit_count = 32 + 8*((intptr_t)buffer&3);
   1.312 +    skip_bits_long(s, 0);
   1.313 +}
   1.314 +
   1.315 +static inline void align_get_bits(GetBitContext *s)
   1.316 +{
   1.317 +    int n= (-get_bits_count(s)) & 7;
   1.318 +    if(n) skip_bits(s, n);
   1.319 +}
   1.320 +
   1.321 +#define tprintf(p, ...) {}
   1.322 +
   1.323 +static inline int get_bits_left(GetBitContext *gb)
   1.324 +{
   1.325 +    return gb->size_in_bits - get_bits_count(gb);
   1.326 +}
   1.327 +
   1.328 +#endif /* AVCODEC_GET_BITS_H */