mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-27 22:28:22 +01:00
rename BitReader to VP8LBitReader
Change-Id: I192b76422e131a94fb58c2c4a5520a5dba807126
This commit is contained in:
parent
90ead710dc
commit
fac0f12e1b
@ -82,7 +82,7 @@ static int DecodeImageStream(int xsize, int ysize,
|
|||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
static int ReadImageSize(BitReader* const br,
|
static int ReadImageSize(VP8LBitReader* const br,
|
||||||
int* const width, int* const height) {
|
int* const width, int* const height) {
|
||||||
const int signature = VP8LReadBits(br, 8);
|
const int signature = VP8LReadBits(br, 8);
|
||||||
if (signature != LOSSLESS_MAGIC_BYTE &&
|
if (signature != LOSSLESS_MAGIC_BYTE &&
|
||||||
@ -100,7 +100,7 @@ int VP8LGetInfo(const uint8_t* data, int data_size,
|
|||||||
return 0; // not enough data
|
return 0; // not enough data
|
||||||
} else {
|
} else {
|
||||||
int w, h;
|
int w, h;
|
||||||
BitReader br;
|
VP8LBitReader br;
|
||||||
VP8LInitBitReader(&br, data, data_size);
|
VP8LInitBitReader(&br, data, data_size);
|
||||||
if (!ReadImageSize(&br, &w, &h)) {
|
if (!ReadImageSize(&br, &w, &h)) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -114,7 +114,7 @@ int VP8LGetInfo(const uint8_t* data, int data_size,
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
static WEBP_INLINE int GetCopyDistance(int distance_symbol,
|
static WEBP_INLINE int GetCopyDistance(int distance_symbol,
|
||||||
BitReader* const br) {
|
VP8LBitReader* const br) {
|
||||||
int extra_bits, offset;
|
int extra_bits, offset;
|
||||||
if (distance_symbol < 4) {
|
if (distance_symbol < 4) {
|
||||||
return distance_symbol + 1;
|
return distance_symbol + 1;
|
||||||
@ -125,7 +125,7 @@ static WEBP_INLINE int GetCopyDistance(int distance_symbol,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static WEBP_INLINE int GetCopyLength(int length_symbol,
|
static WEBP_INLINE int GetCopyLength(int length_symbol,
|
||||||
BitReader* const br) {
|
VP8LBitReader* const br) {
|
||||||
// Length and distance prefixes are encoded the same way.
|
// Length and distance prefixes are encoded the same way.
|
||||||
return GetCopyDistance(length_symbol, br);
|
return GetCopyDistance(length_symbol, br);
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
|
|||||||
// Decodes the next Huffman code from bit-stream.
|
// Decodes the next Huffman code from bit-stream.
|
||||||
// FillBitWindow(br) needs to be called at minimum every second call
|
// FillBitWindow(br) needs to be called at minimum every second call
|
||||||
// to ReadSymbolUnsafe.
|
// to ReadSymbolUnsafe.
|
||||||
static int ReadSymbolUnsafe(const HuffmanTree* tree, BitReader* const br) {
|
static int ReadSymbolUnsafe(const HuffmanTree* tree, VP8LBitReader* const br) {
|
||||||
const HuffmanTreeNode* node = tree->root_;
|
const HuffmanTreeNode* node = tree->root_;
|
||||||
assert(node != NULL);
|
assert(node != NULL);
|
||||||
while (!HuffmanTreeNodeIsLeaf(node)) {
|
while (!HuffmanTreeNodeIsLeaf(node)) {
|
||||||
@ -155,7 +155,7 @@ static int ReadSymbolUnsafe(const HuffmanTree* tree, BitReader* const br) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static WEBP_INLINE int ReadSymbol(const HuffmanTree* tree,
|
static WEBP_INLINE int ReadSymbol(const HuffmanTree* tree,
|
||||||
BitReader* const br) {
|
VP8LBitReader* const br) {
|
||||||
const int read_safe = (br->pos_ > br->len_ - 8);
|
const int read_safe = (br->pos_ > br->len_ - 8);
|
||||||
if (!read_safe) {
|
if (!read_safe) {
|
||||||
return ReadSymbolUnsafe(tree, br);
|
return ReadSymbolUnsafe(tree, br);
|
||||||
@ -173,7 +173,7 @@ static int ReadHuffmanCodeLengths(
|
|||||||
VP8LDecoder* const dec, const int* const code_length_code_lengths,
|
VP8LDecoder* const dec, const int* const code_length_code_lengths,
|
||||||
int num_codes, int num_symbols, int* const code_lengths) {
|
int num_codes, int num_symbols, int* const code_lengths) {
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
int symbol;
|
int symbol;
|
||||||
int max_symbol;
|
int max_symbol;
|
||||||
int prev_code_len = DEFAULT_CODE_LENGTH;
|
int prev_code_len = DEFAULT_CODE_LENGTH;
|
||||||
@ -229,7 +229,7 @@ static int ReadHuffmanCodeLengths(
|
|||||||
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
|
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
|
||||||
HuffmanTree* const tree) {
|
HuffmanTree* const tree) {
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
const int simple_code = VP8LReadBits(br, 1);
|
const int simple_code = VP8LReadBits(br, 1);
|
||||||
|
|
||||||
if (simple_code) { // Read symbols, codes & code lengths directly.
|
if (simple_code) { // Read symbols, codes & code lengths directly.
|
||||||
@ -297,7 +297,7 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
|
|||||||
int* const color_cache_bits_ptr) {
|
int* const color_cache_bits_ptr) {
|
||||||
int ok = 0;
|
int ok = 0;
|
||||||
int i;
|
int i;
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
VP8LMetadata* const hdr = &dec->hdr_;
|
VP8LMetadata* const hdr = &dec->hdr_;
|
||||||
uint32_t* huffman_image = NULL;
|
uint32_t* huffman_image = NULL;
|
||||||
HuffmanTree* htrees = NULL;
|
HuffmanTree* htrees = NULL;
|
||||||
@ -586,7 +586,7 @@ static int DecodeImageData(VP8LDecoder* const dec,
|
|||||||
int process_row) {
|
int process_row) {
|
||||||
int ok = 1;
|
int ok = 1;
|
||||||
int col = 0, row = 0;
|
int col = 0, row = 0;
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
VP8LMetadata* const hdr = &dec->hdr_;
|
VP8LMetadata* const hdr = &dec->hdr_;
|
||||||
VP8LColorCache* const color_cache = hdr->color_cache_;
|
VP8LColorCache* const color_cache = hdr->color_cache_;
|
||||||
uint32_t* src = data;
|
uint32_t* src = data;
|
||||||
@ -744,7 +744,7 @@ static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
|
|||||||
static int ReadTransform(int* const xsize, int const* ysize,
|
static int ReadTransform(int* const xsize, int const* ysize,
|
||||||
VP8LDecoder* const dec) {
|
VP8LDecoder* const dec) {
|
||||||
int ok = 1;
|
int ok = 1;
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
|
VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
|
||||||
const VP8LImageTransformType type =
|
const VP8LImageTransformType type =
|
||||||
(VP8LImageTransformType)VP8LReadBits(br, 2);
|
(VP8LImageTransformType)VP8LReadBits(br, 2);
|
||||||
@ -871,7 +871,7 @@ static int DecodeImageStream(int xsize, int ysize,
|
|||||||
uint32_t* data = NULL;
|
uint32_t* data = NULL;
|
||||||
int color_cache_bits = 0;
|
int color_cache_bits = 0;
|
||||||
|
|
||||||
BitReader* const br = &dec->br_;
|
VP8LBitReader* const br = &dec->br_;
|
||||||
int transform_start_idx = dec->next_transform_;
|
int transform_start_idx = dec->next_transform_;
|
||||||
|
|
||||||
// Step#1: Read the transforms (may recurse).
|
// Step#1: Read the transforms (may recurse).
|
||||||
|
@ -77,7 +77,7 @@ typedef struct {
|
|||||||
uint32_t *argb_; // Internal data: always in BGRA color mode.
|
uint32_t *argb_; // Internal data: always in BGRA color mode.
|
||||||
uint32_t *argb_cache_; // Scratch buffer for temporary BGRA storage.
|
uint32_t *argb_cache_; // Scratch buffer for temporary BGRA storage.
|
||||||
|
|
||||||
BitReader br_;
|
VP8LBitReader br_;
|
||||||
|
|
||||||
int width_;
|
int width_;
|
||||||
int height_;
|
int height_;
|
||||||
|
@ -95,14 +95,16 @@ int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// BitReader
|
// VP8LBitReader
|
||||||
|
|
||||||
|
#define MAX_NUM_BIT_READ 25
|
||||||
|
|
||||||
static const uint32_t kBitMask[MAX_NUM_BIT_READ] = {
|
static const uint32_t kBitMask[MAX_NUM_BIT_READ] = {
|
||||||
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767,
|
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767,
|
||||||
65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215
|
65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215
|
||||||
};
|
};
|
||||||
|
|
||||||
void VP8LInitBitReader(BitReader* const br,
|
void VP8LInitBitReader(VP8LBitReader* const br,
|
||||||
const uint8_t* const start,
|
const uint8_t* const start,
|
||||||
size_t length) {
|
size_t length) {
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -122,14 +124,14 @@ void VP8LInitBitReader(BitReader* const br,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VP8LBitReaderSetBuffer(BitReader* const br,
|
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
|
||||||
const uint8_t* const buf, size_t len) {
|
const uint8_t* const buf, size_t len) {
|
||||||
br->eos_ = (br->pos_ >= len);
|
br->eos_ = (br->pos_ >= len);
|
||||||
br->buf_ = buf;
|
br->buf_ = buf;
|
||||||
br->len_ = len;
|
br->len_ = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ShiftBytes(BitReader* const br) {
|
static void ShiftBytes(VP8LBitReader* const br) {
|
||||||
while (br->bit_pos_ >= 8 && br->pos_ < br->len_) {
|
while (br->bit_pos_ >= 8 && br->pos_ < br->len_) {
|
||||||
br->val_ >>= 8;
|
br->val_ >>= 8;
|
||||||
br->val_ |= ((uint64_t)br->buf_[br->pos_]) << 56;
|
br->val_ |= ((uint64_t)br->buf_[br->pos_]) << 56;
|
||||||
@ -138,7 +140,7 @@ static void ShiftBytes(BitReader* const br) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VP8LFillBitWindow(BitReader* const br) {
|
void VP8LFillBitWindow(VP8LBitReader* const br) {
|
||||||
if (br->bit_pos_ >= 32) {
|
if (br->bit_pos_ >= 32) {
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
if (br->pos_ < br->len_ - 8) {
|
if (br->pos_ < br->len_ - 8) {
|
||||||
@ -162,7 +164,7 @@ void VP8LFillBitWindow(BitReader* const br) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t VP8LReadOneBit(BitReader* const br) {
|
uint32_t VP8LReadOneBit(VP8LBitReader* const br) {
|
||||||
const uint32_t val = (br->val_ >> br->bit_pos_) & 1;
|
const uint32_t val = (br->val_ >> br->bit_pos_) & 1;
|
||||||
// Flag an error at end_of_stream.
|
// Flag an error at end_of_stream.
|
||||||
if (!br->eos_) {
|
if (!br->eos_) {
|
||||||
@ -180,7 +182,7 @@ uint32_t VP8LReadOneBit(BitReader* const br) {
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t VP8LReadBits(BitReader* const br, int n_bits) {
|
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) {
|
||||||
uint32_t val = 0;
|
uint32_t val = 0;
|
||||||
assert(n_bits >= 0);
|
assert(n_bits >= 0);
|
||||||
// Flag an error if end_of_stream or n_bits is more than allowed limit.
|
// Flag an error if end_of_stream or n_bits is more than allowed limit.
|
||||||
|
@ -152,8 +152,6 @@ static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) {
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Bitreader
|
// Bitreader
|
||||||
|
|
||||||
#define MAX_NUM_BIT_READ 25
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint64_t val_;
|
uint64_t val_;
|
||||||
const uint8_t* buf_;
|
const uint8_t* buf_;
|
||||||
@ -162,36 +160,36 @@ typedef struct {
|
|||||||
int bit_pos_;
|
int bit_pos_;
|
||||||
int eos_;
|
int eos_;
|
||||||
int error_;
|
int error_;
|
||||||
} BitReader;
|
} VP8LBitReader;
|
||||||
|
|
||||||
void VP8LInitBitReader(BitReader* const br,
|
void VP8LInitBitReader(VP8LBitReader* const br,
|
||||||
const uint8_t* const start,
|
const uint8_t* const start,
|
||||||
size_t length);
|
size_t length);
|
||||||
|
|
||||||
// Sets a new data buffer.
|
// Sets a new data buffer.
|
||||||
void VP8LBitReaderSetBuffer(BitReader* const br,
|
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
|
||||||
const uint8_t* const buffer, size_t length);
|
const uint8_t* const buffer, size_t length);
|
||||||
|
|
||||||
// Reads the specified number of bits from Read Buffer.
|
// 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 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.
|
// Flags eos if this read attempt is going to cross the read buffer.
|
||||||
uint32_t VP8LReadBits(BitReader* const br, int n_bits);
|
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits);
|
||||||
|
|
||||||
// Reads one bit from Read Buffer. Flags an error in case end_of_stream.
|
// Reads one bit from Read Buffer. Flags an error in case end_of_stream.
|
||||||
// Flags eos after reading last bit from the buffer.
|
// Flags eos after reading last bit from the buffer.
|
||||||
uint32_t VP8LReadOneBit(BitReader* const br);
|
uint32_t VP8LReadOneBit(VP8LBitReader* const br);
|
||||||
|
|
||||||
// VP8LReadOneBitUnsafe is faster than VP8LReadOneBit, but it can be called only
|
// VP8LReadOneBitUnsafe is faster than VP8LReadOneBit, but it can be called only
|
||||||
// 32 times after the last VP8LFillBitWindow. Any subsequent calls
|
// 32 times after the last VP8LFillBitWindow. Any subsequent calls
|
||||||
// (without VP8LFillBitWindow) will return invalid data.
|
// (without VP8LFillBitWindow) will return invalid data.
|
||||||
static WEBP_INLINE uint32_t VP8LReadOneBitUnsafe(BitReader* const br) {
|
static WEBP_INLINE uint32_t VP8LReadOneBitUnsafe(VP8LBitReader* const br) {
|
||||||
const uint32_t val = (br->val_ >> br->bit_pos_) & 1;
|
const uint32_t val = (br->val_ >> br->bit_pos_) & 1;
|
||||||
++br->bit_pos_;
|
++br->bit_pos_;
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advances the Read buffer by 4 bytes to make room for reading next 32 bits.
|
// Advances the Read buffer by 4 bytes to make room for reading next 32 bits.
|
||||||
void VP8LFillBitWindow(BitReader* const br);
|
void VP8LFillBitWindow(VP8LBitReader* const br);
|
||||||
|
|
||||||
#if defined(__cplusplus) || defined(c_plusplus)
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
Loading…
Reference in New Issue
Block a user