2015-01-03 19:52:37 +00:00
|
|
|
//
|
2014-02-10 01:10:30 +00:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2014-02-11 01:37:07 +00:00
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
2014-02-10 01:10:30 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Boolean decoder
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
// Vikas Arora (vikaas.arora@gmail.com)
|
|
|
|
|
|
|
|
#ifndef WEBP_UTILS_BIT_READER_H_
|
|
|
|
#define WEBP_UTILS_BIT_READER_H_
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <stdlib.h> // _byteswap_ulong
|
|
|
|
#endif
|
2014-02-11 01:37:07 +00:00
|
|
|
#include <string.h> // For memcpy
|
2014-09-14 19:26:31 +00:00
|
|
|
#include "../types.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2014-02-10 01:10:30 +00:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
#define BITS 32 // can be 32, 16 or 8
|
|
|
|
#define MASK ((((bit_t)1) << (BITS)) - 1)
|
|
|
|
#if (BITS == 32)
|
|
|
|
typedef uint64_t bit_t; // natural register type
|
|
|
|
typedef uint32_t lbit_t; // natural type for memory I/O
|
2014-02-10 01:10:30 +00:00
|
|
|
#elif (BITS == 16)
|
|
|
|
typedef uint32_t bit_t;
|
|
|
|
typedef uint16_t lbit_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t bit_t;
|
|
|
|
typedef uint8_t lbit_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2014-02-11 01:37:07 +00:00
|
|
|
// Bitreader and code-tree reader
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
typedef struct VP8BitReader VP8BitReader;
|
|
|
|
struct VP8BitReader {
|
|
|
|
const uint8_t* buf_; // next byte to be read
|
|
|
|
const uint8_t* buf_end_; // end of read buffer
|
|
|
|
int eof_; // true if input is exhausted
|
|
|
|
|
|
|
|
// boolean decoder
|
2014-02-11 01:37:07 +00:00
|
|
|
bit_t range_; // current range minus 1. In [127, 254] interval.
|
|
|
|
bit_t value_; // current value
|
|
|
|
int missing_; // number of missing bits in value_ (8bit)
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize the bit reader and the boolean decoder.
|
|
|
|
void VP8InitBitReader(VP8BitReader* const br,
|
|
|
|
const uint8_t* const start, const uint8_t* const end);
|
|
|
|
|
|
|
|
// return the next value made of 'num_bits' bits
|
|
|
|
uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
|
|
|
|
static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {
|
|
|
|
return VP8GetValue(br, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the next value with sign-extension.
|
|
|
|
int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);
|
|
|
|
|
|
|
|
// Read a bit with proba 'prob'. Speed-critical function!
|
|
|
|
extern const uint8_t kVP8Log2Range[128];
|
2014-02-11 01:37:07 +00:00
|
|
|
extern const bit_t kVP8NewRange[128];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void VP8LoadFinalBytes(VP8BitReader* const br); // special case for the tail
|
|
|
|
|
|
|
|
static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) {
|
2014-02-11 01:37:07 +00:00
|
|
|
assert(br && br->buf_);
|
2014-02-10 01:10:30 +00:00
|
|
|
// Read 'BITS' bits at a time if possible.
|
|
|
|
if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) {
|
|
|
|
// convert memory type to register type (with some zero'ing!)
|
|
|
|
bit_t bits;
|
2014-02-11 01:37:07 +00:00
|
|
|
lbit_t in_bits = *(lbit_t*)br->buf_;
|
2014-02-10 01:10:30 +00:00
|
|
|
br->buf_ += (BITS) >> 3;
|
|
|
|
#if !defined(__BIG_ENDIAN__)
|
2014-02-11 01:37:07 +00:00
|
|
|
#if (BITS == 32)
|
2014-02-10 01:10:30 +00:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2014-02-11 01:37:07 +00:00
|
|
|
__asm__ volatile("bswap %k0" : "=r"(in_bits) : "0"(in_bits));
|
|
|
|
bits = (bit_t)in_bits; // 32b -> 64b zero-extension
|
2014-02-10 01:10:30 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2014-02-11 01:37:07 +00:00
|
|
|
bits = _byteswap_ulong(in_bits);
|
2014-02-10 01:10:30 +00:00
|
|
|
#else
|
|
|
|
bits = (bit_t)(in_bits >> 24) | ((in_bits >> 8) & 0xff00)
|
|
|
|
| ((in_bits << 8) & 0xff0000) | (in_bits << 24);
|
|
|
|
#endif // x86
|
|
|
|
#elif (BITS == 16)
|
|
|
|
// gcc will recognize a 'rorw $8, ...' here:
|
|
|
|
bits = (bit_t)(in_bits >> 8) | ((in_bits & 0xff) << 8);
|
|
|
|
#endif
|
2014-02-11 01:37:07 +00:00
|
|
|
#else // LITTLE_ENDIAN
|
2014-02-10 01:10:30 +00:00
|
|
|
bits = (bit_t)in_bits;
|
|
|
|
#endif
|
2014-02-11 01:37:07 +00:00
|
|
|
br->value_ |= bits << br->missing_;
|
|
|
|
br->missing_ -= (BITS);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
VP8LoadFinalBytes(br); // no need to be inlined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
static WEBP_INLINE int VP8BitUpdate(VP8BitReader* const br, bit_t split) {
|
|
|
|
const bit_t value_split = split | (MASK);
|
|
|
|
if (br->missing_ > 0) { // Make sure we have a least BITS bits in 'value_'
|
2014-02-10 01:10:30 +00:00
|
|
|
VP8LoadNewBytes(br);
|
|
|
|
}
|
2014-02-11 01:37:07 +00:00
|
|
|
if (br->value_ > value_split) {
|
|
|
|
br->range_ -= value_split + 1;
|
|
|
|
br->value_ -= value_split + 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
2014-02-11 01:37:07 +00:00
|
|
|
br->range_ = value_split;
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE void VP8Shift(VP8BitReader* const br) {
|
|
|
|
// range_ is in [0..127] interval here.
|
2014-02-11 01:37:07 +00:00
|
|
|
const int idx = br->range_ >> (BITS);
|
2014-02-10 01:10:30 +00:00
|
|
|
const int shift = kVP8Log2Range[idx];
|
|
|
|
br->range_ = kVP8NewRange[idx];
|
|
|
|
br->value_ <<= shift;
|
2014-02-11 01:37:07 +00:00
|
|
|
br->missing_ += shift;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
|
|
|
|
// It's important to avoid generating a 64bit x 64bit multiply here.
|
|
|
|
// We just need an 8b x 8b after all.
|
2014-02-11 01:37:07 +00:00
|
|
|
const bit_t split =
|
|
|
|
(bit_t)((uint32_t)(br->range_ >> (BITS)) * prob) << ((BITS) - 8);
|
2014-02-10 01:10:30 +00:00
|
|
|
const int bit = VP8BitUpdate(br, split);
|
2014-02-11 01:37:07 +00:00
|
|
|
if (br->range_ <= (((bit_t)0x7e << (BITS)) | (MASK))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
VP8Shift(br);
|
|
|
|
}
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
|
2014-02-11 01:37:07 +00:00
|
|
|
const bit_t split = (br->range_ >> 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
const int bit = VP8BitUpdate(br, split);
|
|
|
|
VP8Shift(br);
|
|
|
|
return bit ? -v : v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Bitreader
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2014-02-11 01:37:07 +00:00
|
|
|
uint64_t val_;
|
|
|
|
const uint8_t* buf_;
|
|
|
|
size_t len_;
|
|
|
|
size_t pos_;
|
|
|
|
int bit_pos_;
|
|
|
|
int eos_;
|
|
|
|
int error_;
|
2014-02-10 01:10:30 +00:00
|
|
|
} VP8LBitReader;
|
|
|
|
|
|
|
|
void VP8LInitBitReader(VP8LBitReader* const br,
|
|
|
|
const uint8_t* const start,
|
|
|
|
size_t length);
|
|
|
|
|
|
|
|
// Sets a new data buffer.
|
|
|
|
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
|
|
|
|
const uint8_t* const buffer, size_t length);
|
|
|
|
|
|
|
|
// Reads the specified number of bits from Read Buffer.
|
|
|
|
// Flags an error in case end_of_stream or n_bits is more than allowed limit.
|
|
|
|
// Flags eos if this read attempt is going to cross the read buffer.
|
|
|
|
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
// Reads one bit from Read Buffer. Flags an error in case end_of_stream.
|
|
|
|
// Flags eos after reading last bit from the buffer.
|
|
|
|
uint32_t VP8LReadOneBit(VP8LBitReader* const br);
|
|
|
|
|
|
|
|
// VP8LReadOneBitUnsafe is faster than VP8LReadOneBit, but it can be called only
|
|
|
|
// 32 times after the last VP8LFillBitWindow. Any subsequent calls
|
|
|
|
// (without VP8LFillBitWindow) will return invalid data.
|
|
|
|
static WEBP_INLINE uint32_t VP8LReadOneBitUnsafe(VP8LBitReader* const br) {
|
|
|
|
const uint32_t val = (br->val_ >> br->bit_pos_) & 1;
|
|
|
|
++br->bit_pos_;
|
|
|
|
return val;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
// Advances the Read buffer by 4 bytes to make room for reading next 32 bits.
|
2014-02-10 01:10:30 +00:00
|
|
|
void VP8LFillBitWindow(VP8LBitReader* const br);
|
|
|
|
|
2014-02-11 01:37:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2014-02-10 01:10:30 +00:00
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* WEBP_UTILS_BIT_READER_H_ */
|