Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
comparison libavcodec/get_bits.h @ 9:ea1ba68cf0ed
update to match api changes + add sscc produced source
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 05 Jun 2013 14:43:26 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c05a98ab0c56 |
|---|---|
| 1 /* | |
| 2 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
| 3 * | |
| 4 * This file is part of FFmpeg. | |
| 5 * | |
| 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. | |
| 10 * | |
| 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. | |
| 15 * | |
| 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 | |
| 19 */ | |
| 20 | |
| 21 /** | |
| 22 * @file | |
| 23 * bitstream reader API header. | |
| 24 */ | |
| 25 | |
| 26 #ifndef AVCODEC_GET_BITS_H | |
| 27 #define AVCODEC_GET_BITS_H | |
| 28 | |
| 29 #include <stdint.h> | |
| 30 #include <stdlib.h> | |
| 31 #include <assert.h> | |
| 32 #include "libavutil/bswap.h" | |
| 33 #include "libavutil/common.h" | |
| 34 #include "libavutil/intreadwrite.h" | |
| 35 #include "libavutil/log.h" | |
| 36 #include "mathops.h" | |
| 37 | |
| 38 | |
| 39 typedef struct GetBitContext { | |
| 40 uint8_t *rbsp; | |
| 41 unsigned int rbsp_size; | |
| 42 uint8_t *raw; | |
| 43 const uint8_t *buffer, *buffer_end; | |
| 44 unsigned int alloc_size; | |
| 45 unsigned int buf_size; | |
| 46 uint32_t *buffer_ptr; | |
| 47 uint32_t cache0; | |
| 48 uint32_t cache1; | |
| 49 int bit_count; | |
| 50 int size_in_bits; | |
| 51 } GetBitContext; | |
| 52 | |
| 53 /* Bitstream reader API docs: | |
| 54 name | |
| 55 arbitrary name which is used as prefix for the internal variables | |
| 56 | |
| 57 gb | |
| 58 getbitcontext | |
| 59 | |
| 60 OPEN_READER(name, gb) | |
| 61 loads gb into local variables | |
| 62 | |
| 63 CLOSE_READER(name, gb) | |
| 64 stores local vars in gb | |
| 65 | |
| 66 UPDATE_CACHE(name, gb) | |
| 67 refills the internal cache from the bitstream | |
| 68 after this call at least MIN_CACHE_BITS will be available, | |
| 69 | |
| 70 GET_CACHE(name, gb) | |
| 71 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) | |
| 72 | |
| 73 SHOW_UBITS(name, gb, num) | |
| 74 will return the next num bits | |
| 75 | |
| 76 SHOW_SBITS(name, gb, num) | |
| 77 will return the next num bits and do sign extension | |
| 78 | |
| 79 SKIP_BITS(name, gb, num) | |
| 80 will skip over the next num bits | |
| 81 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER | |
| 82 | |
| 83 SKIP_CACHE(name, gb, num) | |
| 84 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) | |
| 85 | |
| 86 SKIP_COUNTER(name, gb, num) | |
| 87 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) | |
| 88 | |
| 89 LAST_SKIP_CACHE(name, gb, num) | |
| 90 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing | |
| 91 | |
| 92 LAST_SKIP_BITS(name, gb, num) | |
| 93 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER | |
| 94 | |
| 95 for examples see get_bits, show_bits, skip_bits, get_vlc | |
| 96 */ | |
| 97 | |
| 98 #define MIN_CACHE_BITS 32 | |
| 99 | |
| 100 #define OPEN_READER(name, gb)\ | |
| 101 int name##_bit_count=(gb)->bit_count;\ | |
| 102 uint32_t name##_cache0= (gb)->cache0;\ | |
| 103 uint32_t name##_cache1= (gb)->cache1;\ | |
| 104 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | |
| 105 | |
| 106 #define CLOSE_READER(name, gb)\ | |
| 107 (gb)->bit_count= name##_bit_count;\ | |
| 108 (gb)->cache0= name##_cache0;\ | |
| 109 (gb)->cache1= name##_cache1;\ | |
| 110 (gb)->buffer_ptr= name##_buffer_ptr;\ | |
| 111 | |
| 112 #define UPDATE_CACHE(name, gb)\ | |
| 113 if(name##_bit_count > 0){\ | |
| 114 const uint32_t next= be2me_32( *name##_buffer_ptr );\ | |
| 115 name##_cache0 |= NEG_USR32(next,name##_bit_count);\ | |
| 116 name##_cache1 |= next<<name##_bit_count;\ | |
| 117 name##_buffer_ptr++;\ | |
| 118 name##_bit_count-= 32;\ | |
| 119 }\ | |
| 120 | |
| 121 #if ARCH_X86 | |
| 122 # define SKIP_CACHE(name, gb, num)\ | |
| 123 __asm__(\ | |
| 124 "shldl %2, %1, %0 \n\t"\ | |
| 125 "shll %2, %1 \n\t"\ | |
| 126 : "+r" (name##_cache0), "+r" (name##_cache1)\ | |
| 127 : "Ic" ((uint8_t)(num))\ | |
| 128 ); | |
| 129 #else | |
| 130 # define SKIP_CACHE(name, gb, num)\ | |
| 131 name##_cache0 <<= (num);\ | |
| 132 name##_cache0 |= NEG_USR32(name##_cache1,num);\ | |
| 133 name##_cache1 <<= (num); | |
| 134 #endif | |
| 135 | |
| 136 #define SKIP_COUNTER(name, gb, num)\ | |
| 137 name##_bit_count += (num);\ | |
| 138 | |
| 139 #define SKIP_BITS(name, gb, num)\ | |
| 140 {\ | |
| 141 SKIP_CACHE(name, gb, num)\ | |
| 142 SKIP_COUNTER(name, gb, num)\ | |
| 143 }\ | |
| 144 | |
| 145 #define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | |
| 146 #define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | |
| 147 | |
| 148 #define SHOW_UBITS(name, gb, num)\ | |
| 149 NEG_USR32(name##_cache0, num) | |
| 150 | |
| 151 #define SHOW_SBITS(name, gb, num)\ | |
| 152 NEG_SSR32(name##_cache0, num) | |
| 153 | |
| 154 #define GET_CACHE(name, gb)\ | |
| 155 (name##_cache0) | |
| 156 | |
| 157 static inline int get_bits_count(const GetBitContext *s){ | |
| 158 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; | |
| 159 } | |
| 160 | |
| 161 static inline void skip_bits_long(GetBitContext *s, int n){ | |
| 162 OPEN_READER(re, s) | |
| 163 re_bit_count += n; | |
| 164 re_buffer_ptr += re_bit_count>>5; | |
| 165 re_bit_count &= 31; | |
| 166 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count; | |
| 167 re_cache1 = 0; | |
| 168 UPDATE_CACHE(re, s) | |
| 169 CLOSE_READER(re, s) | |
| 170 } | |
| 171 | |
| 172 /** | |
| 173 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). | |
| 174 * if MSB not set it is negative | |
| 175 * @param n length in bits | |
| 176 * @author BERO | |
| 177 */ | |
| 178 static inline int get_xbits(GetBitContext *s, int n){ | |
| 179 register int sign; | |
| 180 register int32_t cache; | |
| 181 OPEN_READER(re, s) | |
| 182 UPDATE_CACHE(re, s) | |
| 183 cache = GET_CACHE(re,s); | |
| 184 sign=(~cache)>>31; | |
| 185 LAST_SKIP_BITS(re, s, n) | |
| 186 CLOSE_READER(re, s) | |
| 187 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | |
| 188 } | |
| 189 | |
| 190 static inline int get_sbits(GetBitContext *s, int n){ | |
| 191 register int tmp; | |
| 192 OPEN_READER(re, s) | |
| 193 UPDATE_CACHE(re, s) | |
| 194 tmp= SHOW_SBITS(re, s, n); | |
| 195 LAST_SKIP_BITS(re, s, n) | |
| 196 CLOSE_READER(re, s) | |
| 197 return tmp; | |
| 198 } | |
| 199 | |
| 200 /** | |
| 201 * reads 1-17 bits. | |
| 202 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | |
| 203 */ | |
| 204 static inline unsigned int get_bits(GetBitContext *s, int n){ | |
| 205 register int tmp; | |
| 206 OPEN_READER(re, s) | |
| 207 UPDATE_CACHE(re, s) | |
| 208 tmp= SHOW_UBITS(re, s, n); | |
| 209 LAST_SKIP_BITS(re, s, n) | |
| 210 CLOSE_READER(re, s) | |
| 211 return tmp; | |
| 212 } | |
| 213 | |
| 214 /** | |
| 215 * shows 1-17 bits. | |
| 216 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | |
| 217 */ | |
| 218 static inline unsigned int show_bits(GetBitContext *s, int n){ | |
| 219 register int tmp; | |
| 220 OPEN_READER(re, s) | |
| 221 UPDATE_CACHE(re, s) | |
| 222 tmp= SHOW_UBITS(re, s, n); | |
| 223 // CLOSE_READER(re, s) | |
| 224 return tmp; | |
| 225 } | |
| 226 | |
| 227 static inline void skip_bits(GetBitContext *s, int n){ | |
| 228 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) | |
| 229 OPEN_READER(re, s) | |
| 230 UPDATE_CACHE(re, s) | |
| 231 LAST_SKIP_BITS(re, s, n) | |
| 232 CLOSE_READER(re, s) | |
| 233 } | |
| 234 | |
| 235 static inline unsigned int get_bits1(GetBitContext *s){ | |
| 236 return get_bits(s, 1); | |
| 237 } | |
| 238 | |
| 239 static inline unsigned int show_bits1(GetBitContext *s){ | |
| 240 return show_bits(s, 1); | |
| 241 } | |
| 242 | |
| 243 static inline void skip_bits1(GetBitContext *s){ | |
| 244 skip_bits(s, 1); | |
| 245 } | |
| 246 | |
| 247 /** | |
| 248 * reads 0-32 bits. | |
| 249 */ | |
| 250 static inline unsigned int get_bits_long(GetBitContext *s, int n){ | |
| 251 if(n<=MIN_CACHE_BITS) return get_bits(s, n); | |
| 252 else{ | |
| 253 int ret= get_bits(s, 16) << (n-16); | |
| 254 return ret | get_bits(s, n-16); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 /** | |
| 259 * reads 0-32 bits as a signed integer. | |
| 260 */ | |
| 261 static inline int get_sbits_long(GetBitContext *s, int n) { | |
| 262 return sign_extend(get_bits_long(s, n), n); | |
| 263 } | |
| 264 | |
| 265 /** | |
| 266 * shows 0-32 bits. | |
| 267 */ | |
| 268 static inline unsigned int show_bits_long(GetBitContext *s, int n){ | |
| 269 if(n<=MIN_CACHE_BITS) return show_bits(s, n); | |
| 270 else{ | |
| 271 GetBitContext gb= *s; | |
| 272 return get_bits_long(&gb, n); | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 static inline int check_marker(GetBitContext *s, const char *msg) | |
| 277 { | |
| 278 int bit= get_bits1(s); | |
| 279 if(!bit) | |
| 280 av_log(AV_LOG_INFO, "Marker bit missing %s\n", msg); | |
| 281 | |
| 282 return bit; | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * init GetBitContext. | |
| 287 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits | |
| 288 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end | |
| 289 * @param bit_size the size of the buffer in bits | |
| 290 * | |
| 291 * While GetBitContext stores the buffer size, for performance reasons you are | |
| 292 * responsible for checking for the buffer end yourself (take advantage of the padding)! | |
| 293 */ | |
| 294 static inline void init_get_bits(GetBitContext *s, | |
| 295 const uint8_t *buffer, int bit_size) | |
| 296 { | |
| 297 int buffer_size= (bit_size+7)>>3; | |
| 298 if(buffer_size < 0 || bit_size < 0) { | |
| 299 buffer_size = bit_size = 0; | |
| 300 buffer = NULL; | |
| 301 } | |
| 302 | |
| 303 s->buffer= buffer; | |
| 304 s->size_in_bits= bit_size; | |
| 305 s->buffer_end= buffer + buffer_size; | |
| 306 | |
| 307 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3)); | |
| 308 s->bit_count = 32 + 8*((intptr_t)buffer&3); | |
| 309 skip_bits_long(s, 0); | |
| 310 } | |
| 311 | |
| 312 static inline void align_get_bits(GetBitContext *s) | |
| 313 { | |
| 314 int n= (-get_bits_count(s)) & 7; | |
| 315 if(n) skip_bits(s, n); | |
| 316 } | |
| 317 | |
| 318 #define tprintf(p, ...) {} | |
| 319 | |
| 320 static inline int get_bits_left(GetBitContext *gb) | |
| 321 { | |
| 322 return gb->size_in_bits - get_bits_count(gb); | |
| 323 } | |
| 324 | |
| 325 #endif /* AVCODEC_GET_BITS_H */ |
