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