Mercurial > libavcodec.hg
annotate get_bits.h @ 11630:02ad6bf427dc libavcodec
Add PIX_FMT_Y400A, 8bit gray, 8bit alpha
| author | andoma |
|---|---|
| date | Wed, 14 Apr 2010 20:15:19 +0000 |
| parents | 74f7086f532a |
| children | 7dd2a45249a9 |
| rev | line source |
|---|---|
| 9428 | 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 libavcodec/get_bits.h | |
| 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 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER) | |
| 39 # define ALT_BITSTREAM_READER | |
| 40 #endif | |
| 41 | |
| 42 #if !defined(LIBMPEG2_BITSTREAM_READER) && !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER) | |
|
10703
3be801dcc8a1
ARM: use ALT_BITSTREAM_READER on cores with fast unaligned access
mru
parents:
10504
diff
changeset
|
43 # if ARCH_ARM && !HAVE_FAST_UNALIGNED |
| 9428 | 44 # define A32_BITSTREAM_READER |
| 45 # else | |
| 46 # define ALT_BITSTREAM_READER | |
| 47 //#define LIBMPEG2_BITSTREAM_READER | |
| 48 //#define A32_BITSTREAM_READER | |
| 49 # endif | |
| 50 #endif | |
| 51 | |
| 52 /* bit input */ | |
| 53 /* buffer, buffer_end and size_in_bits must be present and used by every reader */ | |
| 54 typedef struct GetBitContext { | |
| 55 const uint8_t *buffer, *buffer_end; | |
| 56 #ifdef ALT_BITSTREAM_READER | |
| 57 int index; | |
| 58 #elif defined LIBMPEG2_BITSTREAM_READER | |
| 59 uint8_t *buffer_ptr; | |
| 60 uint32_t cache; | |
| 61 int bit_count; | |
| 62 #elif defined A32_BITSTREAM_READER | |
| 63 uint32_t *buffer_ptr; | |
| 64 uint32_t cache0; | |
| 65 uint32_t cache1; | |
| 66 int bit_count; | |
| 67 #endif | |
| 68 int size_in_bits; | |
| 69 } GetBitContext; | |
| 70 | |
| 71 #define VLC_TYPE int16_t | |
| 72 | |
| 73 typedef struct VLC { | |
| 74 int bits; | |
| 75 VLC_TYPE (*table)[2]; ///< code, bits | |
| 76 int table_size, table_allocated; | |
| 77 } VLC; | |
| 78 | |
| 79 typedef struct RL_VLC_ELEM { | |
| 80 int16_t level; | |
| 81 int8_t len; | |
| 82 uint8_t run; | |
| 83 } RL_VLC_ELEM; | |
| 84 | |
| 85 /* Bitstream reader API docs: | |
| 86 name | |
| 87 arbitrary name which is used as prefix for the internal variables | |
| 88 | |
| 89 gb | |
| 90 getbitcontext | |
| 91 | |
| 92 OPEN_READER(name, gb) | |
| 93 loads gb into local variables | |
| 94 | |
| 95 CLOSE_READER(name, gb) | |
| 96 stores local vars in gb | |
| 97 | |
| 98 UPDATE_CACHE(name, gb) | |
| 99 refills the internal cache from the bitstream | |
| 100 after this call at least MIN_CACHE_BITS will be available, | |
| 101 | |
| 102 GET_CACHE(name, gb) | |
| 103 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) | |
| 104 | |
| 105 SHOW_UBITS(name, gb, num) | |
| 106 will return the next num bits | |
| 107 | |
| 108 SHOW_SBITS(name, gb, num) | |
| 109 will return the next num bits and do sign extension | |
| 110 | |
| 111 SKIP_BITS(name, gb, num) | |
| 112 will skip over the next num bits | |
| 113 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER | |
| 114 | |
| 115 SKIP_CACHE(name, gb, num) | |
| 116 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) | |
| 117 | |
| 118 SKIP_COUNTER(name, gb, num) | |
| 119 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) | |
| 120 | |
| 121 LAST_SKIP_CACHE(name, gb, num) | |
| 122 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing | |
| 123 | |
| 124 LAST_SKIP_BITS(name, gb, num) | |
| 10163 | 125 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER |
| 9428 | 126 |
| 127 for examples see get_bits, show_bits, skip_bits, get_vlc | |
| 128 */ | |
| 129 | |
| 130 #ifdef ALT_BITSTREAM_READER | |
| 131 # define MIN_CACHE_BITS 25 | |
| 132 | |
| 133 # define OPEN_READER(name, gb)\ | |
|
10997
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
134 unsigned int name##_index= (gb)->index;\ |
| 9428 | 135 int name##_cache= 0;\ |
| 136 | |
| 137 # define CLOSE_READER(name, gb)\ | |
| 138 (gb)->index= name##_index;\ | |
| 139 | |
| 140 # ifdef ALT_BITSTREAM_READER_LE | |
| 141 # define UPDATE_CACHE(name, gb)\ | |
| 142 name##_cache= AV_RL32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\ | |
| 143 | |
| 144 # define SKIP_CACHE(name, gb, num)\ | |
| 145 name##_cache >>= (num); | |
| 146 # else | |
| 147 # define UPDATE_CACHE(name, gb)\ | |
| 148 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\ | |
| 149 | |
| 150 # define SKIP_CACHE(name, gb, num)\ | |
| 151 name##_cache <<= (num); | |
| 152 # endif | |
| 153 | |
| 154 // FIXME name? | |
| 155 # define SKIP_COUNTER(name, gb, num)\ | |
| 156 name##_index += (num);\ | |
| 157 | |
| 158 # define SKIP_BITS(name, gb, num)\ | |
| 159 {\ | |
| 160 SKIP_CACHE(name, gb, num)\ | |
| 161 SKIP_COUNTER(name, gb, num)\ | |
| 162 }\ | |
| 163 | |
| 164 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | |
| 165 # define LAST_SKIP_CACHE(name, gb, num) ; | |
| 166 | |
| 167 # ifdef ALT_BITSTREAM_READER_LE | |
| 168 # define SHOW_UBITS(name, gb, num)\ | |
|
11238
5401fc245167
Deobfuscate LE SHOW_[SU]BITS; these are simple sign/zero-extend
mru
parents:
11200
diff
changeset
|
169 zero_extend(name##_cache, num) |
| 9428 | 170 |
| 171 # define SHOW_SBITS(name, gb, num)\ | |
|
11238
5401fc245167
Deobfuscate LE SHOW_[SU]BITS; these are simple sign/zero-extend
mru
parents:
11200
diff
changeset
|
172 sign_extend(name##_cache, num) |
| 9428 | 173 # else |
| 174 # define SHOW_UBITS(name, gb, num)\ | |
| 175 NEG_USR32(name##_cache, num) | |
| 176 | |
| 177 # define SHOW_SBITS(name, gb, num)\ | |
| 178 NEG_SSR32(name##_cache, num) | |
| 179 # endif | |
| 180 | |
| 181 # define GET_CACHE(name, gb)\ | |
| 182 ((uint32_t)name##_cache) | |
| 183 | |
| 10986 | 184 static inline int get_bits_count(const GetBitContext *s){ |
| 9428 | 185 return s->index; |
| 186 } | |
| 187 | |
| 188 static inline void skip_bits_long(GetBitContext *s, int n){ | |
| 189 s->index += n; | |
| 190 } | |
| 191 | |
| 192 #elif defined LIBMPEG2_BITSTREAM_READER | |
| 193 //libmpeg2 like reader | |
| 194 | |
| 195 # define MIN_CACHE_BITS 17 | |
| 196 | |
| 197 # define OPEN_READER(name, gb)\ | |
| 198 int name##_bit_count=(gb)->bit_count;\ | |
| 199 int name##_cache= (gb)->cache;\ | |
| 200 uint8_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | |
| 201 | |
| 202 # define CLOSE_READER(name, gb)\ | |
| 203 (gb)->bit_count= name##_bit_count;\ | |
| 204 (gb)->cache= name##_cache;\ | |
| 205 (gb)->buffer_ptr= name##_buffer_ptr;\ | |
| 206 | |
| 207 # define UPDATE_CACHE(name, gb)\ | |
| 208 if(name##_bit_count >= 0){\ | |
| 209 name##_cache+= AV_RB16(name##_buffer_ptr) << name##_bit_count; \ | |
| 210 name##_buffer_ptr+=2;\ | |
| 211 name##_bit_count-= 16;\ | |
| 212 }\ | |
| 213 | |
| 214 # define SKIP_CACHE(name, gb, num)\ | |
| 215 name##_cache <<= (num);\ | |
| 216 | |
| 217 # define SKIP_COUNTER(name, gb, num)\ | |
| 218 name##_bit_count += (num);\ | |
| 219 | |
| 220 # define SKIP_BITS(name, gb, num)\ | |
| 221 {\ | |
| 222 SKIP_CACHE(name, gb, num)\ | |
| 223 SKIP_COUNTER(name, gb, num)\ | |
| 224 }\ | |
| 225 | |
| 226 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | |
| 227 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | |
| 228 | |
| 229 # define SHOW_UBITS(name, gb, num)\ | |
| 230 NEG_USR32(name##_cache, num) | |
| 231 | |
| 232 # define SHOW_SBITS(name, gb, num)\ | |
| 233 NEG_SSR32(name##_cache, num) | |
| 234 | |
| 235 # define GET_CACHE(name, gb)\ | |
| 236 ((uint32_t)name##_cache) | |
| 237 | |
| 10986 | 238 static inline int get_bits_count(const GetBitContext *s){ |
| 9428 | 239 return (s->buffer_ptr - s->buffer)*8 - 16 + s->bit_count; |
| 240 } | |
| 241 | |
| 242 static inline void skip_bits_long(GetBitContext *s, int n){ | |
| 243 OPEN_READER(re, s) | |
| 244 re_bit_count += n; | |
| 245 re_buffer_ptr += 2*(re_bit_count>>4); | |
| 246 re_bit_count &= 15; | |
| 247 re_cache = ((re_buffer_ptr[-2]<<8) + re_buffer_ptr[-1]) << (16+re_bit_count); | |
| 248 UPDATE_CACHE(re, s) | |
| 249 CLOSE_READER(re, s) | |
| 250 } | |
| 251 | |
| 252 #elif defined A32_BITSTREAM_READER | |
| 253 | |
| 254 # define MIN_CACHE_BITS 32 | |
| 255 | |
| 256 # define OPEN_READER(name, gb)\ | |
| 257 int name##_bit_count=(gb)->bit_count;\ | |
| 258 uint32_t name##_cache0= (gb)->cache0;\ | |
| 259 uint32_t name##_cache1= (gb)->cache1;\ | |
| 260 uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ | |
| 261 | |
| 262 # define CLOSE_READER(name, gb)\ | |
| 263 (gb)->bit_count= name##_bit_count;\ | |
| 264 (gb)->cache0= name##_cache0;\ | |
| 265 (gb)->cache1= name##_cache1;\ | |
| 266 (gb)->buffer_ptr= name##_buffer_ptr;\ | |
| 267 | |
| 268 # define UPDATE_CACHE(name, gb)\ | |
| 269 if(name##_bit_count > 0){\ | |
| 270 const uint32_t next= be2me_32( *name##_buffer_ptr );\ | |
| 271 name##_cache0 |= NEG_USR32(next,name##_bit_count);\ | |
| 272 name##_cache1 |= next<<name##_bit_count;\ | |
| 273 name##_buffer_ptr++;\ | |
| 274 name##_bit_count-= 32;\ | |
| 275 }\ | |
| 276 | |
| 277 #if ARCH_X86 | |
| 278 # define SKIP_CACHE(name, gb, num)\ | |
| 279 __asm__(\ | |
| 280 "shldl %2, %1, %0 \n\t"\ | |
| 281 "shll %2, %1 \n\t"\ | |
| 282 : "+r" (name##_cache0), "+r" (name##_cache1)\ | |
| 283 : "Ic" ((uint8_t)(num))\ | |
| 284 ); | |
| 285 #else | |
| 286 # define SKIP_CACHE(name, gb, num)\ | |
| 287 name##_cache0 <<= (num);\ | |
| 288 name##_cache0 |= NEG_USR32(name##_cache1,num);\ | |
| 289 name##_cache1 <<= (num); | |
| 290 #endif | |
| 291 | |
| 292 # define SKIP_COUNTER(name, gb, num)\ | |
| 293 name##_bit_count += (num);\ | |
| 294 | |
| 295 # define SKIP_BITS(name, gb, num)\ | |
| 296 {\ | |
| 297 SKIP_CACHE(name, gb, num)\ | |
| 298 SKIP_COUNTER(name, gb, num)\ | |
| 299 }\ | |
| 300 | |
| 301 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) | |
| 302 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) | |
| 303 | |
| 304 # define SHOW_UBITS(name, gb, num)\ | |
| 305 NEG_USR32(name##_cache0, num) | |
| 306 | |
| 307 # define SHOW_SBITS(name, gb, num)\ | |
| 308 NEG_SSR32(name##_cache0, num) | |
| 309 | |
| 310 # define GET_CACHE(name, gb)\ | |
| 311 (name##_cache0) | |
| 312 | |
| 10986 | 313 static inline int get_bits_count(const GetBitContext *s){ |
| 9428 | 314 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; |
| 315 } | |
| 316 | |
| 317 static inline void skip_bits_long(GetBitContext *s, int n){ | |
| 318 OPEN_READER(re, s) | |
| 319 re_bit_count += n; | |
| 320 re_buffer_ptr += re_bit_count>>5; | |
| 321 re_bit_count &= 31; | |
| 322 re_cache0 = be2me_32( re_buffer_ptr[-1] ) << re_bit_count; | |
| 323 re_cache1 = 0; | |
| 324 UPDATE_CACHE(re, s) | |
| 325 CLOSE_READER(re, s) | |
| 326 } | |
| 327 | |
| 328 #endif | |
| 329 | |
| 330 /** | |
| 331 * read mpeg1 dc style vlc (sign bit + mantisse with no MSB). | |
| 332 * if MSB not set it is negative | |
| 333 * @param n length in bits | |
| 334 * @author BERO | |
| 335 */ | |
| 336 static inline int get_xbits(GetBitContext *s, int n){ | |
| 337 register int sign; | |
| 338 register int32_t cache; | |
| 339 OPEN_READER(re, s) | |
| 340 UPDATE_CACHE(re, s) | |
| 341 cache = GET_CACHE(re,s); | |
| 342 sign=(~cache)>>31; | |
| 343 LAST_SKIP_BITS(re, s, n) | |
| 344 CLOSE_READER(re, s) | |
| 345 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | |
| 346 } | |
| 347 | |
| 348 static inline int get_sbits(GetBitContext *s, int n){ | |
| 349 register int tmp; | |
| 350 OPEN_READER(re, s) | |
| 351 UPDATE_CACHE(re, s) | |
| 352 tmp= SHOW_SBITS(re, s, n); | |
| 353 LAST_SKIP_BITS(re, s, n) | |
| 354 CLOSE_READER(re, s) | |
| 355 return tmp; | |
| 356 } | |
| 357 | |
| 358 /** | |
| 359 * reads 1-17 bits. | |
| 360 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | |
| 361 */ | |
| 362 static inline unsigned int get_bits(GetBitContext *s, int n){ | |
| 363 register int tmp; | |
| 364 OPEN_READER(re, s) | |
| 365 UPDATE_CACHE(re, s) | |
| 366 tmp= SHOW_UBITS(re, s, n); | |
| 367 LAST_SKIP_BITS(re, s, n) | |
| 368 CLOSE_READER(re, s) | |
| 369 return tmp; | |
| 370 } | |
| 371 | |
| 372 /** | |
| 373 * shows 1-17 bits. | |
| 374 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't | |
| 375 */ | |
| 376 static inline unsigned int show_bits(GetBitContext *s, int n){ | |
| 377 register int tmp; | |
| 378 OPEN_READER(re, s) | |
| 379 UPDATE_CACHE(re, s) | |
| 380 tmp= SHOW_UBITS(re, s, n); | |
| 381 // CLOSE_READER(re, s) | |
| 382 return tmp; | |
| 383 } | |
| 384 | |
| 385 static inline void skip_bits(GetBitContext *s, int n){ | |
| 386 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) | |
| 387 OPEN_READER(re, s) | |
| 388 UPDATE_CACHE(re, s) | |
| 389 LAST_SKIP_BITS(re, s, n) | |
| 390 CLOSE_READER(re, s) | |
| 391 } | |
| 392 | |
| 393 static inline unsigned int get_bits1(GetBitContext *s){ | |
| 394 #ifdef ALT_BITSTREAM_READER | |
|
10997
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
395 unsigned int index= s->index; |
| 9428 | 396 uint8_t result= s->buffer[ index>>3 ]; |
| 397 #ifdef ALT_BITSTREAM_READER_LE | |
| 398 result>>= (index&0x07); | |
| 399 result&= 1; | |
| 400 #else | |
| 401 result<<= (index&0x07); | |
| 402 result>>= 8 - 1; | |
| 403 #endif | |
| 404 index++; | |
| 405 s->index= index; | |
| 406 | |
| 407 return result; | |
| 408 #else | |
| 409 return get_bits(s, 1); | |
| 410 #endif | |
| 411 } | |
| 412 | |
| 413 static inline unsigned int show_bits1(GetBitContext *s){ | |
| 414 return show_bits(s, 1); | |
| 415 } | |
| 416 | |
| 417 static inline void skip_bits1(GetBitContext *s){ | |
| 418 skip_bits(s, 1); | |
| 419 } | |
| 420 | |
| 421 /** | |
| 422 * reads 0-32 bits. | |
| 423 */ | |
| 424 static inline unsigned int get_bits_long(GetBitContext *s, int n){ | |
| 11240 | 425 if(n<=MIN_CACHE_BITS) return get_bits(s, n); |
| 9428 | 426 else{ |
| 427 #ifdef ALT_BITSTREAM_READER_LE | |
| 428 int ret= get_bits(s, 16); | |
| 429 return ret | (get_bits(s, n-16) << 16); | |
| 430 #else | |
| 431 int ret= get_bits(s, 16) << (n-16); | |
| 432 return ret | get_bits(s, n-16); | |
| 433 #endif | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 /** | |
| 438 * reads 0-32 bits as a signed integer. | |
| 439 */ | |
| 440 static inline int get_sbits_long(GetBitContext *s, int n) { | |
| 441 return sign_extend(get_bits_long(s, n), n); | |
| 442 } | |
| 443 | |
| 444 /** | |
| 445 * shows 0-32 bits. | |
| 446 */ | |
| 447 static inline unsigned int show_bits_long(GetBitContext *s, int n){ | |
| 11240 | 448 if(n<=MIN_CACHE_BITS) return show_bits(s, n); |
| 9428 | 449 else{ |
| 450 GetBitContext gb= *s; | |
| 451 return get_bits_long(&gb, n); | |
| 452 } | |
| 453 } | |
| 454 | |
| 455 static inline int check_marker(GetBitContext *s, const char *msg) | |
| 456 { | |
| 457 int bit= get_bits1(s); | |
| 458 if(!bit) | |
| 459 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg); | |
| 460 | |
| 461 return bit; | |
| 462 } | |
| 463 | |
| 464 /** | |
| 465 * init GetBitContext. | |
| 466 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits | |
| 467 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end | |
| 468 * @param bit_size the size of the buffer in bits | |
|
10148
732dfbf67524
Try to clarify that anyone using get_bits must check for buffer overrun
reimar
parents:
9428
diff
changeset
|
469 * |
|
732dfbf67524
Try to clarify that anyone using get_bits must check for buffer overrun
reimar
parents:
9428
diff
changeset
|
470 * While GetBitContext stores the buffer size, for performance reasons you are |
|
732dfbf67524
Try to clarify that anyone using get_bits must check for buffer overrun
reimar
parents:
9428
diff
changeset
|
471 * responsible for checking for the buffer end yourself (take advantage of the padding)! |
| 9428 | 472 */ |
| 473 static inline void init_get_bits(GetBitContext *s, | |
| 474 const uint8_t *buffer, int bit_size) | |
| 475 { | |
| 476 int buffer_size= (bit_size+7)>>3; | |
| 477 if(buffer_size < 0 || bit_size < 0) { | |
| 478 buffer_size = bit_size = 0; | |
| 479 buffer = NULL; | |
| 480 } | |
| 481 | |
| 482 s->buffer= buffer; | |
| 483 s->size_in_bits= bit_size; | |
| 484 s->buffer_end= buffer + buffer_size; | |
| 485 #ifdef ALT_BITSTREAM_READER | |
| 486 s->index=0; | |
| 487 #elif defined LIBMPEG2_BITSTREAM_READER | |
| 488 s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1)); | |
| 489 s->bit_count = 16 + 8*((intptr_t)buffer&1); | |
| 490 skip_bits_long(s, 0); | |
| 491 #elif defined A32_BITSTREAM_READER | |
| 492 s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3)); | |
| 493 s->bit_count = 32 + 8*((intptr_t)buffer&3); | |
| 494 skip_bits_long(s, 0); | |
| 495 #endif | |
| 496 } | |
| 497 | |
| 498 static inline void align_get_bits(GetBitContext *s) | |
| 499 { | |
| 500 int n= (-get_bits_count(s)) & 7; | |
| 501 if(n) skip_bits(s, n); | |
| 502 } | |
| 503 | |
| 504 #define init_vlc(vlc, nb_bits, nb_codes,\ | |
| 505 bits, bits_wrap, bits_size,\ | |
| 506 codes, codes_wrap, codes_size,\ | |
| 507 flags)\ | |
| 508 init_vlc_sparse(vlc, nb_bits, nb_codes,\ | |
| 509 bits, bits_wrap, bits_size,\ | |
| 510 codes, codes_wrap, codes_size,\ | |
| 511 NULL, 0, 0, flags) | |
| 512 | |
| 513 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, | |
| 514 const void *bits, int bits_wrap, int bits_size, | |
| 515 const void *codes, int codes_wrap, int codes_size, | |
| 516 const void *symbols, int symbols_wrap, int symbols_size, | |
| 517 int flags); | |
| 518 #define INIT_VLC_LE 2 | |
| 519 #define INIT_VLC_USE_NEW_STATIC 4 | |
| 520 void free_vlc(VLC *vlc); | |
| 521 | |
| 522 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size)\ | |
| 523 {\ | |
| 524 static VLC_TYPE table[static_size][2];\ | |
| 525 (vlc)->table= table;\ | |
| 526 (vlc)->table_allocated= static_size;\ | |
| 527 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC);\ | |
| 528 } | |
| 529 | |
| 530 | |
| 531 /** | |
| 532 * | |
|
11197
87bcae769ee7
get_bits: Fix spelling and grammar in GET_VLC() comment.
alexc
parents:
10997
diff
changeset
|
533 * If the vlc code is invalid and max_depth=1, then no bits will be removed. |
|
87bcae769ee7
get_bits: Fix spelling and grammar in GET_VLC() comment.
alexc
parents:
10997
diff
changeset
|
534 * If the vlc code is invalid and max_depth>1, then the number of bits removed |
|
87bcae769ee7
get_bits: Fix spelling and grammar in GET_VLC() comment.
alexc
parents:
10997
diff
changeset
|
535 * is undefined. |
| 9428 | 536 */ |
| 537 #define GET_VLC(code, name, gb, table, bits, max_depth)\ | |
| 538 {\ | |
|
10997
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
539 int n, nb_bits;\ |
|
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
540 unsigned int index;\ |
| 9428 | 541 \ |
| 542 index= SHOW_UBITS(name, gb, bits);\ | |
| 543 code = table[index][0];\ | |
| 544 n = table[index][1];\ | |
| 545 \ | |
| 546 if(max_depth > 1 && n < 0){\ | |
| 547 LAST_SKIP_BITS(name, gb, bits)\ | |
| 548 UPDATE_CACHE(name, gb)\ | |
| 549 \ | |
| 550 nb_bits = -n;\ | |
| 551 \ | |
| 552 index= SHOW_UBITS(name, gb, nb_bits) + code;\ | |
| 553 code = table[index][0];\ | |
| 554 n = table[index][1];\ | |
| 555 if(max_depth > 2 && n < 0){\ | |
| 556 LAST_SKIP_BITS(name, gb, nb_bits)\ | |
| 557 UPDATE_CACHE(name, gb)\ | |
| 558 \ | |
| 559 nb_bits = -n;\ | |
| 560 \ | |
| 561 index= SHOW_UBITS(name, gb, nb_bits) + code;\ | |
| 562 code = table[index][0];\ | |
| 563 n = table[index][1];\ | |
| 564 }\ | |
| 565 }\ | |
| 566 SKIP_BITS(name, gb, n)\ | |
| 567 } | |
| 568 | |
| 569 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)\ | |
| 570 {\ | |
|
10997
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
571 int n, nb_bits;\ |
|
9bd48e510bef
Mark index as unsigend so gcc knows it doesnt need a sign extension on x86_64.
michael
parents:
10986
diff
changeset
|
572 unsigned int index;\ |
| 9428 | 573 \ |
| 574 index= SHOW_UBITS(name, gb, bits);\ | |
| 575 level = table[index].level;\ | |
| 576 n = table[index].len;\ | |
| 577 \ | |
| 578 if(max_depth > 1 && n < 0){\ | |
| 579 SKIP_BITS(name, gb, bits)\ | |
| 580 if(need_update){\ | |
| 581 UPDATE_CACHE(name, gb)\ | |
| 582 }\ | |
| 583 \ | |
| 584 nb_bits = -n;\ | |
| 585 \ | |
| 586 index= SHOW_UBITS(name, gb, nb_bits) + level;\ | |
| 587 level = table[index].level;\ | |
| 588 n = table[index].len;\ | |
| 589 }\ | |
| 590 run= table[index].run;\ | |
| 591 SKIP_BITS(name, gb, n)\ | |
| 592 } | |
| 593 | |
| 594 | |
| 595 /** | |
| 596 * parses a vlc code, faster then get_vlc() | |
| 597 * @param bits is the number of bits which will be read at once, must be | |
| 598 * identical to nb_bits in init_vlc() | |
| 599 * @param max_depth is the number of times bits bits must be read to completely | |
| 600 * read the longest vlc code | |
| 601 * = (max_vlc_length + bits - 1) / bits | |
| 602 */ | |
| 603 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], | |
| 604 int bits, int max_depth) | |
| 605 { | |
| 606 int code; | |
| 607 | |
| 608 OPEN_READER(re, s) | |
| 609 UPDATE_CACHE(re, s) | |
| 610 | |
| 611 GET_VLC(code, re, s, table, bits, max_depth) | |
| 612 | |
| 613 CLOSE_READER(re, s) | |
| 614 return code; | |
| 615 } | |
| 616 | |
| 617 //#define TRACE | |
| 618 | |
| 619 #ifdef TRACE | |
| 620 static inline void print_bin(int bits, int n){ | |
| 621 int i; | |
| 622 | |
| 623 for(i=n-1; i>=0; i--){ | |
| 624 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1); | |
| 625 } | |
| 626 for(i=n; i<24; i++) | |
| 627 av_log(NULL, AV_LOG_DEBUG, " "); | |
| 628 } | |
| 629 | |
| 630 static inline int get_bits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ | |
| 631 int r= get_bits(s, n); | |
| 632 | |
| 633 print_bin(r, n); | |
| 634 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", r, n, r, get_bits_count(s)-n, file, func, line); | |
| 635 return r; | |
| 636 } | |
| 637 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], int bits, int max_depth, char *file, const char *func, int line){ | |
| 638 int show= show_bits(s, 24); | |
| 639 int pos= get_bits_count(s); | |
| 640 int r= get_vlc2(s, table, bits, max_depth); | |
| 641 int len= get_bits_count(s) - pos; | |
| 642 int bits2= show>>(24-len); | |
| 643 | |
| 644 print_bin(bits2, len); | |
| 645 | |
| 646 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", bits2, len, r, pos, file, func, line); | |
| 647 return r; | |
| 648 } | |
| 649 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, const char *func, int line){ | |
| 650 int show= show_bits(s, n); | |
| 651 int r= get_xbits(s, n); | |
| 652 | |
| 653 print_bin(show, n); | |
| 654 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", show, n, r, get_bits_count(s)-n, file, func, line); | |
| 655 return r; | |
| 656 } | |
| 657 | |
| 658 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
| 659 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
| 660 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
| 661 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
| 662 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
| 663 | |
| 664 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__) | |
| 665 | |
| 666 #else //TRACE | |
| 667 #define tprintf(p, ...) {} | |
| 668 #endif | |
| 669 | |
| 670 static inline int decode012(GetBitContext *gb){ | |
| 671 int n; | |
| 672 n = get_bits1(gb); | |
| 673 if (n == 0) | |
| 674 return 0; | |
| 675 else | |
| 676 return get_bits1(gb) + 1; | |
| 677 } | |
| 678 | |
| 679 static inline int decode210(GetBitContext *gb){ | |
| 680 if (get_bits1(gb)) | |
| 681 return 0; | |
| 682 else | |
| 683 return 2 - get_bits1(gb); | |
| 684 } | |
| 685 | |
|
10504
9f16f997c41b
Make get_bits_left() available for use in libavcodec (was previously held
rbultje
parents:
10501
diff
changeset
|
686 static inline int get_bits_left(GetBitContext *gb) |
|
9f16f997c41b
Make get_bits_left() available for use in libavcodec (was previously held
rbultje
parents:
10501
diff
changeset
|
687 { |
|
9f16f997c41b
Make get_bits_left() available for use in libavcodec (was previously held
rbultje
parents:
10501
diff
changeset
|
688 return gb->size_in_bits - get_bits_count(gb); |
|
9f16f997c41b
Make get_bits_left() available for use in libavcodec (was previously held
rbultje
parents:
10501
diff
changeset
|
689 } |
|
9f16f997c41b
Make get_bits_left() available for use in libavcodec (was previously held
rbultje
parents:
10501
diff
changeset
|
690 |
| 9428 | 691 #endif /* AVCODEC_GET_BITS_H */ |
