2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
2010-09-30 15:34:38 +02: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/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Boolean decoder
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-09-07 11:26:35 +02:00
|
|
|
#ifndef WEBP_UTILS_BIT_READER_H_
|
|
|
|
#define WEBP_UTILS_BIT_READER_H_
|
2010-09-30 15:34:38 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
2011-05-03 02:19:00 +02:00
|
|
|
#include "../webp/decode_vp8.h"
|
2010-09-30 15:34:38 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 15:34:38 +02:00
|
|
|
// Bitreader and code-tree reader
|
|
|
|
|
2011-09-07 22:38:31 +02:00
|
|
|
typedef struct VP8BitReader VP8BitReader;
|
|
|
|
struct VP8BitReader {
|
2010-09-30 15:34:38 +02:00
|
|
|
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
|
|
|
|
uint32_t range_; // current range minus 1. In [127, 254] interval.
|
|
|
|
uint32_t value_; // current value
|
2011-03-11 00:05:59 +01:00
|
|
|
int missing_; // number of missing bits in value_ (8bit)
|
2011-09-07 22:38:31 +02:00
|
|
|
};
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2010-12-20 14:45:49 +01:00
|
|
|
// Initialize the bit reader and the boolean decoder.
|
2011-03-11 00:05:59 +01:00
|
|
|
void VP8InitBitReader(VP8BitReader* const br,
|
|
|
|
const uint8_t* const start, const uint8_t* const end);
|
2010-09-30 15:34:38 +02:00
|
|
|
|
|
|
|
// return the next value made of 'num_bits' bits
|
|
|
|
uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) {
|
2010-09-30 15:34:38 +02:00
|
|
|
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];
|
|
|
|
extern const uint8_t kVP8NewRange[128];
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE uint32_t VP8GetByte(VP8BitReader* const br) {
|
2010-09-30 15:34:38 +02:00
|
|
|
assert(br);
|
|
|
|
if (br->buf_ < br->buf_end_) {
|
|
|
|
assert(br->buf_);
|
|
|
|
return *br->buf_++;
|
|
|
|
}
|
|
|
|
br->eof_ = 1;
|
2011-03-11 00:05:59 +01:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE uint32_t VP8BitUpdate(
|
|
|
|
VP8BitReader* const br, uint32_t split) {
|
2011-03-11 00:05:59 +01:00
|
|
|
uint32_t bit;
|
2011-05-30 07:35:44 +02:00
|
|
|
const uint32_t value_split = (split + 1) << 8;
|
2011-03-11 00:05:59 +01:00
|
|
|
// Make sure we have a least 8 bits in 'value_'
|
|
|
|
if (br->missing_ > 0) {
|
|
|
|
br->value_ |= VP8GetByte(br) << br->missing_;
|
|
|
|
br->missing_ -= 8;
|
|
|
|
}
|
2011-05-30 07:35:44 +02:00
|
|
|
bit = (br->value_ >= value_split);
|
2011-03-11 00:05:59 +01:00
|
|
|
if (bit) {
|
|
|
|
br->range_ -= split + 1;
|
2011-05-30 07:35:44 +02:00
|
|
|
br->value_ -= value_split;
|
2011-03-11 00:05:59 +01:00
|
|
|
} else {
|
|
|
|
br->range_ = split;
|
|
|
|
}
|
|
|
|
return bit;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE void VP8Shift(VP8BitReader* const br) {
|
2010-09-30 15:34:38 +02:00
|
|
|
// range_ is in [0..127] interval here.
|
|
|
|
const int shift = kVP8Log2Range[br->range_];
|
|
|
|
br->range_ = kVP8NewRange[br->range_];
|
|
|
|
br->value_ <<= shift;
|
2011-03-11 00:05:59 +01:00
|
|
|
br->missing_ += shift;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE uint32_t VP8GetBit(VP8BitReader* const br, int prob) {
|
2010-09-30 15:34:38 +02:00
|
|
|
const uint32_t split = (br->range_ * prob) >> 8;
|
2011-03-11 00:05:59 +01:00
|
|
|
const uint32_t bit = VP8BitUpdate(br, split);
|
2010-09-30 15:34:38 +02:00
|
|
|
if (br->range_ < 0x7f) {
|
|
|
|
VP8Shift(br);
|
|
|
|
}
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
|
2010-09-30 15:34:38 +02:00
|
|
|
const uint32_t split = br->range_ >> 1;
|
2011-03-11 00:05:59 +01:00
|
|
|
const uint32_t bit = VP8BitUpdate(br, split);
|
2010-09-30 15:34:38 +02:00
|
|
|
VP8Shift(br);
|
2011-03-11 00:05:59 +01:00
|
|
|
return bit ? -v : v;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2011-09-07 11:26:35 +02:00
|
|
|
#endif /* WEBP_UTILS_BIT_READER_H_ */
|