Libav 0.7.1
|
00001 /* 00002 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * Libav is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00026 #ifndef AVCODEC_GET_BITS_H 00027 #define AVCODEC_GET_BITS_H 00028 00029 #include <stdint.h> 00030 #include <stdlib.h> 00031 #include <assert.h> 00032 #include "libavutil/bswap.h" 00033 #include "libavutil/common.h" 00034 #include "libavutil/intreadwrite.h" 00035 #include "libavutil/log.h" 00036 #include "mathops.h" 00037 00038 #if defined(ALT_BITSTREAM_READER_LE) && !defined(ALT_BITSTREAM_READER) 00039 # define ALT_BITSTREAM_READER 00040 #endif 00041 00042 #if !defined(A32_BITSTREAM_READER) && !defined(ALT_BITSTREAM_READER) 00043 # if ARCH_ARM && !HAVE_FAST_UNALIGNED 00044 # define A32_BITSTREAM_READER 00045 # else 00046 # define ALT_BITSTREAM_READER 00047 //#define A32_BITSTREAM_READER 00048 # endif 00049 #endif 00050 00051 /* bit input */ 00052 /* buffer, buffer_end and size_in_bits must be present and used by every reader */ 00053 typedef struct GetBitContext { 00054 const uint8_t *buffer, *buffer_end; 00055 #ifdef ALT_BITSTREAM_READER 00056 int index; 00057 #elif defined A32_BITSTREAM_READER 00058 uint32_t *buffer_ptr; 00059 uint32_t cache0; 00060 uint32_t cache1; 00061 int bit_count; 00062 #endif 00063 int size_in_bits; 00064 } GetBitContext; 00065 00066 #define VLC_TYPE int16_t 00067 00068 typedef struct VLC { 00069 int bits; 00070 VLC_TYPE (*table)[2]; 00071 int table_size, table_allocated; 00072 } VLC; 00073 00074 typedef struct RL_VLC_ELEM { 00075 int16_t level; 00076 int8_t len; 00077 uint8_t run; 00078 } RL_VLC_ELEM; 00079 00080 /* Bitstream reader API docs: 00081 name 00082 arbitrary name which is used as prefix for the internal variables 00083 00084 gb 00085 getbitcontext 00086 00087 OPEN_READER(name, gb) 00088 loads gb into local variables 00089 00090 CLOSE_READER(name, gb) 00091 stores local vars in gb 00092 00093 UPDATE_CACHE(name, gb) 00094 refills the internal cache from the bitstream 00095 after this call at least MIN_CACHE_BITS will be available, 00096 00097 GET_CACHE(name, gb) 00098 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit) 00099 00100 SHOW_UBITS(name, gb, num) 00101 will return the next num bits 00102 00103 SHOW_SBITS(name, gb, num) 00104 will return the next num bits and do sign extension 00105 00106 SKIP_BITS(name, gb, num) 00107 will skip over the next num bits 00108 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER 00109 00110 SKIP_CACHE(name, gb, num) 00111 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER) 00112 00113 SKIP_COUNTER(name, gb, num) 00114 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS) 00115 00116 LAST_SKIP_CACHE(name, gb, num) 00117 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing 00118 00119 LAST_SKIP_BITS(name, gb, num) 00120 is equivalent to LAST_SKIP_CACHE; SKIP_COUNTER 00121 00122 for examples see get_bits, show_bits, skip_bits, get_vlc 00123 */ 00124 00125 #ifdef ALT_BITSTREAM_READER 00126 # define MIN_CACHE_BITS 25 00127 00128 # define OPEN_READER(name, gb) \ 00129 unsigned int name##_index = (gb)->index; \ 00130 unsigned int av_unused name##_cache = 0 00131 00132 # define CLOSE_READER(name, gb) (gb)->index = name##_index 00133 00134 # ifdef ALT_BITSTREAM_READER_LE 00135 # define UPDATE_CACHE(name, gb) \ 00136 name##_cache = AV_RL32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) >> (name##_index&0x07) 00137 00138 # define SKIP_CACHE(name, gb, num) name##_cache >>= (num) 00139 # else 00140 # define UPDATE_CACHE(name, gb) \ 00141 name##_cache = AV_RB32(((const uint8_t *)(gb)->buffer)+(name##_index>>3)) << (name##_index&0x07) 00142 00143 # define SKIP_CACHE(name, gb, num) name##_cache <<= (num) 00144 # endif 00145 00146 // FIXME name? 00147 # define SKIP_COUNTER(name, gb, num) name##_index += (num) 00148 00149 # define SKIP_BITS(name, gb, num) do { \ 00150 SKIP_CACHE(name, gb, num); \ 00151 SKIP_COUNTER(name, gb, num); \ 00152 } while (0) 00153 00154 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) 00155 # define LAST_SKIP_CACHE(name, gb, num) 00156 00157 # ifdef ALT_BITSTREAM_READER_LE 00158 # define SHOW_UBITS(name, gb, num) zero_extend(name##_cache, num) 00159 00160 # define SHOW_SBITS(name, gb, num) sign_extend(name##_cache, num) 00161 # else 00162 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache, num) 00163 00164 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache, num) 00165 # endif 00166 00167 # define GET_CACHE(name, gb) ((uint32_t)name##_cache) 00168 00169 static inline int get_bits_count(const GetBitContext *s){ 00170 return s->index; 00171 } 00172 00173 static inline void skip_bits_long(GetBitContext *s, int n){ 00174 s->index += n; 00175 } 00176 00177 #elif defined A32_BITSTREAM_READER 00178 00179 # define MIN_CACHE_BITS 32 00180 00181 # define OPEN_READER(name, gb) \ 00182 int name##_bit_count = (gb)->bit_count; \ 00183 uint32_t name##_cache0 = (gb)->cache0; \ 00184 uint32_t name##_cache1 = (gb)->cache1; \ 00185 uint32_t *name##_buffer_ptr = (gb)->buffer_ptr 00186 00187 # define CLOSE_READER(name, gb) do { \ 00188 (gb)->bit_count = name##_bit_count; \ 00189 (gb)->cache0 = name##_cache0; \ 00190 (gb)->cache1 = name##_cache1; \ 00191 (gb)->buffer_ptr = name##_buffer_ptr; \ 00192 } while (0) 00193 00194 # define UPDATE_CACHE(name, gb) do { \ 00195 if(name##_bit_count > 0){ \ 00196 const uint32_t next = av_be2ne32(*name##_buffer_ptr); \ 00197 name##_cache0 |= NEG_USR32(next, name##_bit_count); \ 00198 name##_cache1 |= next << name##_bit_count; \ 00199 name##_buffer_ptr++; \ 00200 name##_bit_count -= 32; \ 00201 } \ 00202 } while (0) 00203 00204 #if ARCH_X86 00205 # define SKIP_CACHE(name, gb, num) \ 00206 __asm__("shldl %2, %1, %0 \n\t" \ 00207 "shll %2, %1 \n\t" \ 00208 : "+r" (name##_cache0), "+r" (name##_cache1) \ 00209 : "Ic" ((uint8_t)(num))) 00210 #else 00211 # define SKIP_CACHE(name, gb, num) do { \ 00212 name##_cache0 <<= (num); \ 00213 name##_cache0 |= NEG_USR32(name##_cache1,num); \ 00214 name##_cache1 <<= (num); \ 00215 } while (0) 00216 #endif 00217 00218 # define SKIP_COUNTER(name, gb, num) name##_bit_count += (num) 00219 00220 # define SKIP_BITS(name, gb, num) do { \ 00221 SKIP_CACHE(name, gb, num); \ 00222 SKIP_COUNTER(name, gb, num); \ 00223 } while (0) 00224 00225 # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) 00226 # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) 00227 00228 # define SHOW_UBITS(name, gb, num) NEG_USR32(name##_cache0, num) 00229 00230 # define SHOW_SBITS(name, gb, num) NEG_SSR32(name##_cache0, num) 00231 00232 # define GET_CACHE(name, gb) name##_cache0 00233 00234 static inline int get_bits_count(const GetBitContext *s) { 00235 return ((uint8_t*)s->buffer_ptr - s->buffer)*8 - 32 + s->bit_count; 00236 } 00237 00238 static inline void skip_bits_long(GetBitContext *s, int n){ 00239 OPEN_READER(re, s); 00240 re_bit_count += n; 00241 re_buffer_ptr += re_bit_count>>5; 00242 re_bit_count &= 31; 00243 re_cache0 = av_be2ne32(re_buffer_ptr[-1]) << re_bit_count; 00244 re_cache1 = 0; 00245 UPDATE_CACHE(re, s); 00246 CLOSE_READER(re, s); 00247 } 00248 00249 #endif 00250 00257 static inline int get_xbits(GetBitContext *s, int n){ 00258 register int sign; 00259 register int32_t cache; 00260 OPEN_READER(re, s); 00261 UPDATE_CACHE(re, s); 00262 cache = GET_CACHE(re, s); 00263 sign = ~cache >> 31; 00264 LAST_SKIP_BITS(re, s, n); 00265 CLOSE_READER(re, s); 00266 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; 00267 } 00268 00269 static inline int get_sbits(GetBitContext *s, int n){ 00270 register int tmp; 00271 OPEN_READER(re, s); 00272 UPDATE_CACHE(re, s); 00273 tmp = SHOW_SBITS(re, s, n); 00274 LAST_SKIP_BITS(re, s, n); 00275 CLOSE_READER(re, s); 00276 return tmp; 00277 } 00278 00282 static inline unsigned int get_bits(GetBitContext *s, int n){ 00283 register int tmp; 00284 OPEN_READER(re, s); 00285 UPDATE_CACHE(re, s); 00286 tmp = SHOW_UBITS(re, s, n); 00287 LAST_SKIP_BITS(re, s, n); 00288 CLOSE_READER(re, s); 00289 return tmp; 00290 } 00291 00295 static inline unsigned int show_bits(GetBitContext *s, int n){ 00296 register int tmp; 00297 OPEN_READER(re, s); 00298 UPDATE_CACHE(re, s); 00299 tmp = SHOW_UBITS(re, s, n); 00300 return tmp; 00301 } 00302 00303 static inline void skip_bits(GetBitContext *s, int n){ 00304 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :)) 00305 OPEN_READER(re, s); 00306 UPDATE_CACHE(re, s); 00307 LAST_SKIP_BITS(re, s, n); 00308 CLOSE_READER(re, s); 00309 } 00310 00311 static inline unsigned int get_bits1(GetBitContext *s){ 00312 #ifdef ALT_BITSTREAM_READER 00313 unsigned int index = s->index; 00314 uint8_t result = s->buffer[index>>3]; 00315 #ifdef ALT_BITSTREAM_READER_LE 00316 result >>= index & 7; 00317 result &= 1; 00318 #else 00319 result <<= index & 7; 00320 result >>= 8 - 1; 00321 #endif 00322 index++; 00323 s->index = index; 00324 00325 return result; 00326 #else 00327 return get_bits(s, 1); 00328 #endif 00329 } 00330 00331 static inline unsigned int show_bits1(GetBitContext *s){ 00332 return show_bits(s, 1); 00333 } 00334 00335 static inline void skip_bits1(GetBitContext *s){ 00336 skip_bits(s, 1); 00337 } 00338 00342 static inline unsigned int get_bits_long(GetBitContext *s, int n){ 00343 if (n <= MIN_CACHE_BITS) return get_bits(s, n); 00344 else { 00345 #ifdef ALT_BITSTREAM_READER_LE 00346 int ret = get_bits(s, 16); 00347 return ret | (get_bits(s, n-16) << 16); 00348 #else 00349 int ret = get_bits(s, 16) << (n-16); 00350 return ret | get_bits(s, n-16); 00351 #endif 00352 } 00353 } 00354 00358 static inline int get_sbits_long(GetBitContext *s, int n) { 00359 return sign_extend(get_bits_long(s, n), n); 00360 } 00361 00365 static inline unsigned int show_bits_long(GetBitContext *s, int n){ 00366 if (n <= MIN_CACHE_BITS) return show_bits(s, n); 00367 else { 00368 GetBitContext gb = *s; 00369 return get_bits_long(&gb, n); 00370 } 00371 } 00372 00373 static inline int check_marker(GetBitContext *s, const char *msg) 00374 { 00375 int bit = get_bits1(s); 00376 if (!bit) 00377 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg); 00378 00379 return bit; 00380 } 00381 00391 static inline void init_get_bits(GetBitContext *s, 00392 const uint8_t *buffer, int bit_size) 00393 { 00394 int buffer_size = (bit_size+7)>>3; 00395 if (buffer_size < 0 || bit_size < 0) { 00396 buffer_size = bit_size = 0; 00397 buffer = NULL; 00398 } 00399 00400 s->buffer = buffer; 00401 s->size_in_bits = bit_size; 00402 s->buffer_end = buffer + buffer_size; 00403 #ifdef ALT_BITSTREAM_READER 00404 s->index = 0; 00405 #elif defined A32_BITSTREAM_READER 00406 s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3); 00407 s->bit_count = 32 + 8*((intptr_t)buffer & 3); 00408 skip_bits_long(s, 0); 00409 #endif 00410 } 00411 00412 static inline void align_get_bits(GetBitContext *s) 00413 { 00414 int n = -get_bits_count(s) & 7; 00415 if (n) skip_bits(s, n); 00416 } 00417 00418 #define init_vlc(vlc, nb_bits, nb_codes, \ 00419 bits, bits_wrap, bits_size, \ 00420 codes, codes_wrap, codes_size, \ 00421 flags) \ 00422 init_vlc_sparse(vlc, nb_bits, nb_codes, \ 00423 bits, bits_wrap, bits_size, \ 00424 codes, codes_wrap, codes_size, \ 00425 NULL, 0, 0, flags) 00426 00427 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, 00428 const void *bits, int bits_wrap, int bits_size, 00429 const void *codes, int codes_wrap, int codes_size, 00430 const void *symbols, int symbols_wrap, int symbols_size, 00431 int flags); 00432 #define INIT_VLC_LE 2 00433 #define INIT_VLC_USE_NEW_STATIC 4 00434 void free_vlc(VLC *vlc); 00435 00436 #define INIT_VLC_STATIC(vlc, bits, a,b,c,d,e,f,g, static_size) do { \ 00437 static VLC_TYPE table[static_size][2]; \ 00438 (vlc)->table = table; \ 00439 (vlc)->table_allocated = static_size; \ 00440 init_vlc(vlc, bits, a,b,c,d,e,f,g, INIT_VLC_USE_NEW_STATIC); \ 00441 } while (0) 00442 00443 00450 #define GET_VLC(code, name, gb, table, bits, max_depth) do { \ 00451 int n, nb_bits; \ 00452 unsigned int index; \ 00453 \ 00454 index = SHOW_UBITS(name, gb, bits); \ 00455 code = table[index][0]; \ 00456 n = table[index][1]; \ 00457 \ 00458 if (max_depth > 1 && n < 0) { \ 00459 LAST_SKIP_BITS(name, gb, bits); \ 00460 UPDATE_CACHE(name, gb); \ 00461 \ 00462 nb_bits = -n; \ 00463 \ 00464 index = SHOW_UBITS(name, gb, nb_bits) + code; \ 00465 code = table[index][0]; \ 00466 n = table[index][1]; \ 00467 if (max_depth > 2 && n < 0) { \ 00468 LAST_SKIP_BITS(name, gb, nb_bits); \ 00469 UPDATE_CACHE(name, gb); \ 00470 \ 00471 nb_bits = -n; \ 00472 \ 00473 index = SHOW_UBITS(name, gb, nb_bits) + code; \ 00474 code = table[index][0]; \ 00475 n = table[index][1]; \ 00476 } \ 00477 } \ 00478 SKIP_BITS(name, gb, n); \ 00479 } while (0) 00480 00481 #define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update) do { \ 00482 int n, nb_bits; \ 00483 unsigned int index; \ 00484 \ 00485 index = SHOW_UBITS(name, gb, bits); \ 00486 level = table[index].level; \ 00487 n = table[index].len; \ 00488 \ 00489 if (max_depth > 1 && n < 0) { \ 00490 SKIP_BITS(name, gb, bits); \ 00491 if (need_update) { \ 00492 UPDATE_CACHE(name, gb); \ 00493 } \ 00494 \ 00495 nb_bits = -n; \ 00496 \ 00497 index = SHOW_UBITS(name, gb, nb_bits) + level; \ 00498 level = table[index].level; \ 00499 n = table[index].len; \ 00500 } \ 00501 run = table[index].run; \ 00502 SKIP_BITS(name, gb, n); \ 00503 } while (0) 00504 00505 00514 static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2], 00515 int bits, int max_depth) 00516 { 00517 int code; 00518 00519 OPEN_READER(re, s); 00520 UPDATE_CACHE(re, s); 00521 00522 GET_VLC(code, re, s, table, bits, max_depth); 00523 00524 CLOSE_READER(re, s); 00525 return code; 00526 } 00527 00528 static inline int decode012(GetBitContext *gb){ 00529 int n; 00530 n = get_bits1(gb); 00531 if (n == 0) 00532 return 0; 00533 else 00534 return get_bits1(gb) + 1; 00535 } 00536 00537 static inline int decode210(GetBitContext *gb){ 00538 if (get_bits1(gb)) 00539 return 0; 00540 else 00541 return 2 - get_bits1(gb); 00542 } 00543 00544 static inline int get_bits_left(GetBitContext *gb) 00545 { 00546 return gb->size_in_bits - get_bits_count(gb); 00547 } 00548 00549 //#define TRACE 00550 00551 #ifdef TRACE 00552 static inline void print_bin(int bits, int n){ 00553 int i; 00554 00555 for (i = n-1; i >= 0; i--) { 00556 av_log(NULL, AV_LOG_DEBUG, "%d", (bits>>i)&1); 00557 } 00558 for (i = n; i < 24; i++) 00559 av_log(NULL, AV_LOG_DEBUG, " "); 00560 } 00561 00562 static inline int get_bits_trace(GetBitContext *s, int n, char *file, 00563 const char *func, int line){ 00564 int r = get_bits(s, n); 00565 00566 print_bin(r, n); 00567 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d bit @%5d in %s %s:%d\n", 00568 r, n, r, get_bits_count(s)-n, file, func, line); 00569 return r; 00570 } 00571 static inline int get_vlc_trace(GetBitContext *s, VLC_TYPE (*table)[2], 00572 int bits, int max_depth, char *file, 00573 const char *func, int line){ 00574 int show = show_bits(s, 24); 00575 int pos = get_bits_count(s); 00576 int r = get_vlc2(s, table, bits, max_depth); 00577 int len = get_bits_count(s) - pos; 00578 int bits2 = show >> (24-len); 00579 00580 print_bin(bits2, len); 00581 00582 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d vlc @%5d in %s %s:%d\n", 00583 bits2, len, r, pos, file, func, line); 00584 return r; 00585 } 00586 static inline int get_xbits_trace(GetBitContext *s, int n, char *file, 00587 const char *func, int line){ 00588 int show = show_bits(s, n); 00589 int r = get_xbits(s, n); 00590 00591 print_bin(show, n); 00592 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d xbt @%5d in %s %s:%d\n", 00593 show, n, r, get_bits_count(s)-n, file, func, line); 00594 return r; 00595 } 00596 00597 #define get_bits(s, n) get_bits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00598 #define get_bits1(s) get_bits_trace(s, 1, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00599 #define get_xbits(s, n) get_xbits_trace(s, n, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00600 #define get_vlc(s, vlc) get_vlc_trace(s, (vlc)->table, (vlc)->bits, 3, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00601 #define get_vlc2(s, tab, bits, max) get_vlc_trace(s, tab, bits, max, __FILE__, __PRETTY_FUNCTION__, __LINE__) 00602 00603 #define tprintf(p, ...) av_log(p, AV_LOG_DEBUG, __VA_ARGS__) 00604 00605 #else //TRACE 00606 #define tprintf(p, ...) {} 00607 #endif 00608 00609 #endif /* AVCODEC_GET_BITS_H */