Home | History | Annotate | Download | only in utils
      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 non-inlined methods
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #ifdef HAVE_CONFIG_H
     15 #include "webp/config.h"
     16 #endif
     17 
     18 #include "./bit_reader_inl.h"
     19 
     20 //------------------------------------------------------------------------------
     21 // VP8BitReader
     22 
     23 void VP8InitBitReader(VP8BitReader* const br,
     24                       const uint8_t* const start, const uint8_t* const end) {
     25   assert(br != NULL);
     26   assert(start != NULL);
     27   assert(start <= end);
     28   br->range_   = 255 - 1;
     29   br->buf_     = start;
     30   br->buf_end_ = end;
     31   br->value_   = 0;
     32   br->bits_    = -8;   // to load the very first 8bits
     33   br->eof_     = 0;
     34   VP8LoadNewBytes(br);
     35 }
     36 
     37 void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
     38   if (br->buf_ != NULL) {
     39     br->buf_ += offset;
     40     br->buf_end_ += offset;
     41   }
     42 }
     43 
     44 const uint8_t kVP8Log2Range[128] = {
     45      7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
     46   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
     47   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
     48   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
     49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     50   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     51   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     52   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     53   0
     54 };
     55 
     56 // range = ((range - 1) << kVP8Log2Range[range]) + 1
     57 const range_t kVP8NewRange[128] = {
     58   127, 127, 191, 127, 159, 191, 223, 127,
     59   143, 159, 175, 191, 207, 223, 239, 127,
     60   135, 143, 151, 159, 167, 175, 183, 191,
     61   199, 207, 215, 223, 231, 239, 247, 127,
     62   131, 135, 139, 143, 147, 151, 155, 159,
     63   163, 167, 171, 175, 179, 183, 187, 191,
     64   195, 199, 203, 207, 211, 215, 219, 223,
     65   227, 231, 235, 239, 243, 247, 251, 127,
     66   129, 131, 133, 135, 137, 139, 141, 143,
     67   145, 147, 149, 151, 153, 155, 157, 159,
     68   161, 163, 165, 167, 169, 171, 173, 175,
     69   177, 179, 181, 183, 185, 187, 189, 191,
     70   193, 195, 197, 199, 201, 203, 205, 207,
     71   209, 211, 213, 215, 217, 219, 221, 223,
     72   225, 227, 229, 231, 233, 235, 237, 239,
     73   241, 243, 245, 247, 249, 251, 253, 127
     74 };
     75 
     76 void VP8LoadFinalBytes(VP8BitReader* const br) {
     77   assert(br != NULL && br->buf_ != NULL);
     78   // Only read 8bits at a time
     79   if (br->buf_ < br->buf_end_) {
     80     br->bits_ += 8;
     81     br->value_ = (bit_t)(*br->buf_++) | (br->value_ << 8);
     82   } else if (!br->eof_) {
     83     br->value_ <<= 8;
     84     br->bits_ += 8;
     85     br->eof_ = 1;
     86   }
     87 }
     88 
     89 //------------------------------------------------------------------------------
     90 // Higher-level calls
     91 
     92 uint32_t VP8GetValue(VP8BitReader* const br, int bits) {
     93   uint32_t v = 0;
     94   while (bits-- > 0) {
     95     v |= VP8GetBit(br, 0x80) << bits;
     96   }
     97   return v;
     98 }
     99 
    100 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
    101   const int value = VP8GetValue(br, bits);
    102   return VP8Get(br) ? -value : value;
    103 }
    104 
    105 //------------------------------------------------------------------------------
    106 // VP8LBitReader
    107 
    108 #define LBITS 64      // Number of bits prefetched.
    109 #define WBITS 32      // Minimum number of bytes needed after VP8LFillBitWindow.
    110 #define LOG8_WBITS 4  // Number of bytes needed to store WBITS bits.
    111 
    112 #if !defined(WEBP_FORCE_ALIGNED) && \
    113     (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \
    114      defined(__i386__) || defined(_M_IX86) || \
    115      defined(__x86_64__) || defined(_M_X64))
    116 #define VP8L_USE_UNALIGNED_LOAD
    117 #endif
    118 
    119 static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = {
    120   0,
    121   0x000001, 0x000003, 0x000007, 0x00000f,
    122   0x00001f, 0x00003f, 0x00007f, 0x0000ff,
    123   0x0001ff, 0x0003ff, 0x0007ff, 0x000fff,
    124   0x001fff, 0x003fff, 0x007fff, 0x00ffff,
    125   0x01ffff, 0x03ffff, 0x07ffff, 0x0fffff,
    126   0x1fffff, 0x3fffff, 0x7fffff, 0xffffff
    127 };
    128 
    129 void VP8LInitBitReader(VP8LBitReader* const br, const uint8_t* const start,
    130                        size_t length) {
    131   size_t i;
    132   vp8l_val_t value = 0;
    133   assert(br != NULL);
    134   assert(start != NULL);
    135   assert(length < 0xfffffff8u);   // can't happen with a RIFF chunk.
    136 
    137   br->len_ = length;
    138   br->val_ = 0;
    139   br->bit_pos_ = 0;
    140   br->eos_ = 0;
    141   br->error_ = 0;
    142 
    143   if (length > sizeof(br->val_)) {
    144     length = sizeof(br->val_);
    145   }
    146   for (i = 0; i < length; ++i) {
    147     value |= (vp8l_val_t)start[i] << (8 * i);
    148   }
    149   br->val_ = value;
    150   br->pos_ = length;
    151   br->buf_ = start;
    152 }
    153 
    154 // Special version that assumes br->pos_ <= br_len_.
    155 static int IsEndOfStreamSpecial(const VP8LBitReader* const br) {
    156   assert(br->pos_ <= br->len_);
    157   return br->pos_ == br->len_ && br->bit_pos_ >= LBITS;
    158 }
    159 
    160 static int IsEndOfStream(const VP8LBitReader* const br) {
    161   return (br->pos_ > br->len_) || IsEndOfStreamSpecial(br);
    162 }
    163 
    164 void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
    165                             const uint8_t* const buf, size_t len) {
    166   assert(br != NULL);
    167   assert(buf != NULL);
    168   assert(len < 0xfffffff8u);   // can't happen with a RIFF chunk.
    169   br->buf_ = buf;
    170   br->len_ = len;
    171   br->eos_ = IsEndOfStream(br);
    172 }
    173 
    174 // If not at EOS, reload up to LBITS byte-by-byte
    175 static void ShiftBytes(VP8LBitReader* const br) {
    176   while (br->bit_pos_ >= 8 && br->pos_ < br->len_) {
    177     br->val_ >>= 8;
    178     br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (LBITS - 8);
    179     ++br->pos_;
    180     br->bit_pos_ -= 8;
    181   }
    182 }
    183 
    184 void VP8LFillBitWindow(VP8LBitReader* const br) {
    185   if (br->bit_pos_ >= WBITS) {
    186     // TODO(jzern): given the fixed read size it may be possible to force
    187     //              alignment in this block.
    188 #if defined(VP8L_USE_UNALIGNED_LOAD)
    189     if (br->pos_ + sizeof(br->val_) < br->len_) {
    190       br->val_ >>= WBITS;
    191       br->bit_pos_ -= WBITS;
    192       // The expression below needs a little-endian arch to work correctly.
    193       // This gives a large speedup for decoding speed.
    194       br->val_ |= (vp8l_val_t)*(const uint32_t*)(br->buf_ + br->pos_) <<
    195                   (LBITS - WBITS);
    196       br->pos_ += LOG8_WBITS;
    197       return;
    198     }
    199 #endif
    200     ShiftBytes(br);       // Slow path.
    201     br->eos_ = IsEndOfStreamSpecial(br);
    202   }
    203 }
    204 
    205 uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) {
    206   assert(n_bits >= 0);
    207   // Flag an error if end_of_stream or n_bits is more than allowed limit.
    208   if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) {
    209     const uint32_t val =
    210         (uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits];
    211     const int new_bits = br->bit_pos_ + n_bits;
    212     br->bit_pos_ = new_bits;
    213     // If this read is going to cross the read buffer, set the eos flag.
    214     br->eos_ = IsEndOfStreamSpecial(br);
    215     ShiftBytes(br);
    216     return val;
    217   } else {
    218     br->error_ = 1;
    219     return 0;
    220   }
    221 }
    222 
    223 //------------------------------------------------------------------------------
    224