github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/src/utils/bit_reader.h (about)

     1  // Copyright 2010 Google Inc. All Rights Reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the COPYING file in the root of the source
     5  // tree. An additional intellectual property rights grant can be found
     6  // in the file PATENTS. All contributing project authors may
     7  // be found in the AUTHORS file in the root of the source tree.
     8  // -----------------------------------------------------------------------------
     9  //
    10  // Boolean decoder
    11  //
    12  // Author: Skal (pascal.massimino@gmail.com)
    13  //         Vikas Arora (vikaas.arora@gmail.com)
    14  
    15  #ifndef WEBP_UTILS_BIT_READER_H_
    16  #define WEBP_UTILS_BIT_READER_H_
    17  
    18  #include <assert.h>
    19  #ifdef _MSC_VER
    20  #include <stdlib.h>  // _byteswap_ulong
    21  #endif
    22  #include "../webp/types.h"
    23  
    24  #ifdef __cplusplus
    25  extern "C" {
    26  #endif
    27  
    28  // The Boolean decoder needs to maintain infinite precision on the value_ field.
    29  // However, since range_ is only 8bit, we only need an active window of 8 bits
    30  // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls
    31  // below 128, range_ is updated, and fresh bits read from the bitstream are
    32  // brought in as LSB.
    33  // To avoid reading the fresh bits one by one (slow), we cache a few of them
    34  // ahead (actually, we cache BITS of them ahead. See below). There's two
    35  // strategies regarding how to shift these looked-ahead fresh bits into the
    36  // 8bit window of value_: either we shift them in, while keeping the position of
    37  // the window fixed. Or we slide the window to the right while keeping the cache
    38  // bits at a fixed, right-justified, position.
    39  //
    40  //  Example, for BITS=16: here is the content of value_ for both strategies:
    41  //
    42  //          !USE_RIGHT_JUSTIFY            ||        USE_RIGHT_JUSTIFY
    43  //                                        ||
    44  //   <- 8b -><- 8b -><- BITS bits  ->     ||  <- 8b+3b -><- 8b -><- 13 bits ->
    45  //   [unused][value_][cached bits][0]     ||  [unused...][value_][cached bits]
    46  //  [........00vvvvvvBBBBBBBBBBBBB000]LSB || [...........00vvvvvvBBBBBBBBBBBBB]
    47  //                                        ||
    48  // After calling VP8Shift(), where we need to shift away two zeros:
    49  //  [........vvvvvvvvBBBBBBBBBBB00000]LSB || [.............vvvvvvvvBBBBBBBBBBB]
    50  //                                        ||
    51  // Just before we need to call VP8LoadNewBytes(), the situation is:
    52  //  [........vvvvvv000000000000000000]LSB || [..........................vvvvvv]
    53  //                                        ||
    54  // And just after calling VP8LoadNewBytes():
    55  //  [........vvvvvvvvBBBBBBBBBBBBBBBB]LSB || [........vvvvvvvvBBBBBBBBBBBBBBBB]
    56  //
    57  // -> we're back to eight active 'value_' bits (marked 'v') and BITS cached
    58  // bits (marked 'B')
    59  //
    60  // The right-justify strategy tends to use less shifts and is often faster.
    61  
    62  //------------------------------------------------------------------------------
    63  // BITS can be any multiple of 8 from 8 to 56 (inclusive).
    64  // Pick values that fit natural register size.
    65  
    66  #if !defined(WEBP_REFERENCE_IMPLEMENTATION)
    67  
    68  #define USE_RIGHT_JUSTIFY
    69  
    70  #if defined(__i386__) || defined(_M_IX86)      // x86 32bit
    71  #define BITS 16
    72  #elif defined(__x86_64__) || defined(_M_X64)   // x86 64bit
    73  #define BITS 56
    74  #elif defined(__arm__) || defined(_M_ARM)      // ARM
    75  #define BITS 24
    76  #else                      // reasonable default
    77  #define BITS 24
    78  #endif
    79  
    80  #else     // reference choices
    81  
    82  #define USE_RIGHT_JUSTIFY
    83  #define BITS 8
    84  
    85  #endif
    86  
    87  //------------------------------------------------------------------------------
    88  // Derived types and constants
    89  
    90  // bit_t = natural register type
    91  // lbit_t = natural type for memory I/O
    92  
    93  #if (BITS > 32)
    94  typedef uint64_t bit_t;
    95  typedef uint64_t lbit_t;
    96  #elif (BITS == 32)
    97  typedef uint64_t bit_t;
    98  typedef uint32_t lbit_t;
    99  #elif (BITS == 24)
   100  typedef uint32_t bit_t;
   101  typedef uint32_t lbit_t;
   102  #elif (BITS == 16)
   103  typedef uint32_t bit_t;
   104  typedef uint16_t lbit_t;
   105  #else
   106  typedef uint32_t bit_t;
   107  typedef uint8_t lbit_t;
   108  #endif
   109  
   110  #ifndef USE_RIGHT_JUSTIFY
   111  typedef bit_t range_t;     // type for storing range_
   112  #define MASK ((((bit_t)1) << (BITS)) - 1)
   113  #else
   114  typedef uint32_t range_t;  // range_ only uses 8bits here. No need for bit_t.
   115  #endif
   116  
   117  //------------------------------------------------------------------------------
   118  // Bitreader
   119  
   120  typedef struct VP8BitReader VP8BitReader;
   121  struct VP8BitReader {
   122    const uint8_t* buf_;        // next byte to be read
   123    const uint8_t* buf_end_;    // end of read buffer
   124    int eof_;                   // true if input is exhausted
   125  
   126    // boolean decoder
   127    range_t range_;            // current range minus 1. In [127, 254] interval.
   128    bit_t value_;              // current value
   129    int bits_;                 // number of valid bits left
   130  };
   131  
   132  // Initialize the bit reader and the boolean decoder.
   133  void VP8InitBitReader(VP8BitReader* const br,
   134                        const uint8_t* const start, const uint8_t* const end);
   135  
   136  // return the next value made of 'num_bits' bits
   137  uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
   138  static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {
   139    return VP8GetValue(br, 1);
   140  }
   141  
   142  // return the next value with sign-extension.
   143  int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);
   144  
   145  // Read a bit with proba 'prob'. Speed-critical function!
   146  extern const uint8_t kVP8Log2Range[128];
   147  extern const range_t kVP8NewRange[128];
   148  
   149  void VP8LoadFinalBytes(VP8BitReader* const br);    // special case for the tail
   150  
   151  static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
   152    assert(br != NULL && br->buf_ != NULL);
   153    // Read 'BITS' bits at a time if possible.
   154    if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
   155      // convert memory type to register type (with some zero'ing!)
   156      bit_t bits;
   157      const lbit_t in_bits = *(const lbit_t*)br->buf_;
   158      br->buf_ += (BITS) >> 3;
   159  #if !defined(__BIG_ENDIAN__)
   160  #if (BITS > 32)
   161  // gcc 4.3 has builtin functions for swap32/swap64
   162  #if defined(__GNUC__) && \
   163             (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
   164      bits = (bit_t)__builtin_bswap64(in_bits);
   165  #elif defined(_MSC_VER)
   166      bits = (bit_t)_byteswap_uint64(in_bits);
   167  #elif defined(__x86_64__)
   168      __asm__ volatile("bswapq %0" : "=r"(bits) : "0"(in_bits));
   169  #else  // generic code for swapping 64-bit values (suggested by bdb@)
   170      bits = (bit_t)in_bits;
   171      bits = ((bits & 0xffffffff00000000ull) >> 32) |
   172             ((bits & 0x00000000ffffffffull) << 32);
   173      bits = ((bits & 0xffff0000ffff0000ull) >> 16) |
   174             ((bits & 0x0000ffff0000ffffull) << 16);
   175      bits = ((bits & 0xff00ff00ff00ff00ull) >> 8) |
   176             ((bits & 0x00ff00ff00ff00ffull) << 8);
   177  #endif
   178      bits >>= 64 - BITS;
   179  #elif (BITS >= 24)
   180  #if defined(__i386__) || defined(__x86_64__)
   181      {
   182        lbit_t swapped_in_bits;
   183        __asm__ volatile("bswap %k0" : "=r"(swapped_in_bits) : "0"(in_bits));
   184        bits = (bit_t)swapped_in_bits;   // 24b/32b -> 32b/64b zero-extension
   185      }
   186  #elif defined(_MSC_VER)
   187      bits = (bit_t)_byteswap_ulong(in_bits);
   188  #else
   189      bits = (bit_t)(in_bits >> 24) | ((in_bits >> 8) & 0xff00)
   190           | ((in_bits << 8) & 0xff0000)  | (in_bits << 24);
   191  #endif  // x86
   192      bits >>= (32 - BITS);
   193  #elif (BITS == 16)
   194      // gcc will recognize a 'rorw $8, ...' here:
   195      bits = (bit_t)(in_bits >> 8) | ((in_bits & 0xff) << 8);
   196  #else   // BITS == 8
   197      bits = (bit_t)in_bits;
   198  #endif
   199  #else    // BIG_ENDIAN
   200      bits = (bit_t)in_bits;
   201      if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
   202  #endif
   203  #ifndef USE_RIGHT_JUSTIFY
   204      br->value_ |= bits << (-br->bits_);
   205  #else
   206      br->value_ = bits | (br->value_ << (BITS));
   207  #endif
   208      br->bits_ += (BITS);
   209    } else {
   210      VP8LoadFinalBytes(br);    // no need to be inlined
   211    }
   212  }
   213  
   214  static WEBP_INLINE int VP8BitUpdate(VP8BitReader* const br, range_t split) {
   215    if (br->bits_ < 0) {  // Make sure we have a least BITS bits in 'value_'
   216      VP8LoadNewBytes(br);
   217    }
   218  #ifndef USE_RIGHT_JUSTIFY
   219    split |= (MASK);
   220    if (br->value_ > split) {
   221      br->range_ -= split + 1;
   222      br->value_ -= split + 1;
   223      return 1;
   224    } else {
   225      br->range_ = split;
   226      return 0;
   227    }
   228  #else
   229    {
   230      const int pos = br->bits_;
   231      const range_t value = (range_t)(br->value_ >> pos);
   232      if (value > split) {
   233        br->range_ -= split + 1;
   234        br->value_ -= (bit_t)(split + 1) << pos;
   235        return 1;
   236      } else {
   237        br->range_ = split;
   238        return 0;
   239      }
   240    }
   241  #endif
   242  }
   243  
   244  static WEBP_INLINE void VP8Shift(VP8BitReader* const br) {
   245  #ifndef USE_RIGHT_JUSTIFY
   246    // range_ is in [0..127] interval here.
   247    const bit_t idx = br->range_ >> (BITS);
   248    const int shift = kVP8Log2Range[idx];
   249    br->range_ = kVP8NewRange[idx];
   250    br->value_ <<= shift;
   251    br->bits_ -= shift;
   252  #else
   253    const int shift = kVP8Log2Range[br->range_];
   254    assert(br->range_ < (range_t)128);
   255    br->range_ = kVP8NewRange[br->range_];
   256    br->bits_ -= shift;
   257  #endif
   258  }
   259  
   260  static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
   261  #ifndef USE_RIGHT_JUSTIFY
   262    // It's important to avoid generating a 64bit x 64bit multiply here.
   263    // We just need an 8b x 8b after all.
   264    const range_t split =
   265        (range_t)((uint32_t)(br->range_ >> (BITS)) * prob) << ((BITS) - 8);
   266    const int bit = VP8BitUpdate(br, split);
   267    if (br->range_ <= (((range_t)0x7e << (BITS)) | (MASK))) {
   268      VP8Shift(br);
   269    }
   270    return bit;
   271  #else
   272    const range_t split = (br->range_ * prob) >> 8;
   273    const int bit = VP8BitUpdate(br, split);
   274    if (br->range_ <= (range_t)0x7e) {
   275      VP8Shift(br);
   276    }
   277    return bit;
   278  #endif
   279  }
   280  
   281  static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
   282    const range_t split = (br->range_ >> 1);
   283    const int bit = VP8BitUpdate(br, split);
   284    VP8Shift(br);
   285    return bit ? -v : v;
   286  }
   287  
   288  // -----------------------------------------------------------------------------
   289  // Bitreader for lossless format
   290  
   291  typedef uint64_t vp8l_val_t;  // right now, this bit-reader can only use 64bit.
   292  
   293  typedef struct {
   294    vp8l_val_t     val_;        // pre-fetched bits
   295    const uint8_t* buf_;        // input byte buffer
   296    size_t         len_;        // buffer length
   297    size_t         pos_;        // byte position in buf_
   298    int            bit_pos_;    // current bit-reading position in val_
   299    int            eos_;        // bitstream is finished
   300    int            error_;      // an error occurred (buffer overflow attempt...)
   301  } VP8LBitReader;
   302  
   303  void VP8LInitBitReader(VP8LBitReader* const br,
   304                         const uint8_t* const start,
   305                         size_t length);
   306  
   307  //  Sets a new data buffer.
   308  void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
   309                              const uint8_t* const buffer, size_t length);
   310  
   311  // Reads the specified number of bits from Read Buffer.
   312  // Flags an error in case end_of_stream or n_bits is more than allowed limit.
   313  // Flags eos if this read attempt is going to cross the read buffer.
   314  uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
   315  
   316  // Return the prefetched bits, so they can be looked up.
   317  static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) {
   318    return (uint32_t)(br->val_ >> br->bit_pos_);
   319  }
   320  
   321  // For jumping over a number of bits in the bit stream when accessed with
   322  // VP8LPrefetchBits and VP8LFillBitWindow.
   323  static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) {
   324    br->bit_pos_ = val;
   325  }
   326  
   327  // Advances the read buffer by 4 bytes to make room for reading next 32 bits.
   328  void VP8LFillBitWindow(VP8LBitReader* const br);
   329  
   330  #ifdef __cplusplus
   331  }    // extern "C"
   332  #endif
   333  
   334  #endif  /* WEBP_UTILS_BIT_READER_H_ */