2013-12-16 13:45:57 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-03-20 14:44:15 +00:00
|
|
|
/* Bit reading helpers */
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#ifndef BROTLI_DEC_BIT_READER_H_
|
|
|
|
#define BROTLI_DEC_BIT_READER_H_
|
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
#include <string.h>
|
2015-05-07 14:53:43 +00:00
|
|
|
#include "./port.h"
|
2013-11-15 18:02:17 +00:00
|
|
|
#include "./streams.h"
|
2013-10-11 08:26:07 +00:00
|
|
|
#include "./types.h"
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-02-25 12:32:17 +00:00
|
|
|
#if (defined(__x86_64__) || defined(_M_X64))
|
|
|
|
/* This should be set to 1 only on little-endian machines. */
|
|
|
|
#define BROTLI_USE_64_BITS 1
|
2015-05-07 18:36:35 +00:00
|
|
|
#elif (defined(__arm__) && defined(__BYTE_ORDER__) \
|
|
|
|
&& (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
|
|
|
|
/* Enable some optimizations for ARM architectures with little endian byte
|
|
|
|
order. So far the optimizations have been tested on a Cortex-A7. */
|
2015-05-07 15:10:27 +00:00
|
|
|
#define ARMv7
|
|
|
|
#define BROTLI_USE_64_BITS 1
|
2015-02-25 12:32:17 +00:00
|
|
|
#else
|
|
|
|
#define BROTLI_USE_64_BITS 0
|
|
|
|
#endif
|
2013-11-15 18:02:17 +00:00
|
|
|
#define BROTLI_MAX_NUM_BIT_READ 25
|
|
|
|
#define BROTLI_READ_SIZE 4096
|
2015-06-26 15:37:00 +00:00
|
|
|
#define BROTLI_IBUF_SIZE (2 * BROTLI_READ_SIZE + 128)
|
2013-11-15 18:02:17 +00:00
|
|
|
#define BROTLI_IBUF_MASK (2 * BROTLI_READ_SIZE - 1)
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2014-02-14 14:04:23 +00:00
|
|
|
#define UNALIGNED_COPY64(dst, src) memcpy(dst, src, 8)
|
2014-11-26 09:37:33 +00:00
|
|
|
#define UNALIGNED_MOVE64(dst, src) memmove(dst, src, 8)
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-05-07 15:10:27 +00:00
|
|
|
#ifdef ARMv7
|
|
|
|
/* Arm instructions can shift and negate registers before an AND operation. */
|
|
|
|
static BROTLI_INLINE uint32_t BitMask(int n) { return ~((0xffffffff) << n); }
|
|
|
|
#else
|
2013-11-15 18:02:17 +00:00
|
|
|
static const uint32_t kBitMask[BROTLI_MAX_NUM_BIT_READ] = {
|
|
|
|
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
|
|
|
|
};
|
2015-05-07 15:10:27 +00:00
|
|
|
static BROTLI_INLINE uint32_t BitMask(int n) { return kBitMask[n]; }
|
|
|
|
#endif
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-11-15 18:02:17 +00:00
|
|
|
typedef struct {
|
2015-02-25 12:32:17 +00:00
|
|
|
#if (BROTLI_USE_64_BITS)
|
2014-02-14 14:04:23 +00:00
|
|
|
uint64_t val_; /* pre-fetched bits */
|
2015-02-25 12:32:17 +00:00
|
|
|
#else
|
|
|
|
uint32_t val_; /* pre-fetched bits */
|
|
|
|
#endif
|
2014-02-14 14:04:23 +00:00
|
|
|
uint32_t pos_; /* byte position in stream */
|
|
|
|
uint32_t bit_pos_; /* current bit-reading position in val_ */
|
|
|
|
uint32_t bit_end_pos_; /* bit-reading end position from LSB of val_ */
|
|
|
|
int eos_; /* input stream is finished */
|
2015-05-07 14:53:43 +00:00
|
|
|
uint8_t* buf_ptr_; /* next input will write here */
|
|
|
|
BrotliInput input_; /* input callback */
|
2015-03-20 15:13:15 +00:00
|
|
|
|
|
|
|
/* Set to 0 to support partial data streaming. Set to 1 to expect full data or
|
|
|
|
for the last chunk of partial data. */
|
|
|
|
int finish_;
|
|
|
|
/* indicates how much bytes already read when reading partial data */
|
|
|
|
int tmp_bytes_read_;
|
2015-05-07 14:53:43 +00:00
|
|
|
|
|
|
|
/* Input byte buffer, consist of a ringbuffer and a "slack" region where */
|
|
|
|
/* bytes from the start of the ringbuffer are copied. */
|
|
|
|
uint8_t buf_[BROTLI_IBUF_SIZE];
|
2013-11-15 18:02:17 +00:00
|
|
|
} BrotliBitReader;
|
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Initializes the bitreader fields. After this, BrotliWarmupBitReader must
|
|
|
|
be used. */
|
|
|
|
void BrotliInitBitReader(BrotliBitReader* const br,
|
|
|
|
BrotliInput input, int finish);
|
|
|
|
|
|
|
|
/* Fetches data to fill up internal buffers. Returns 0 if there wasn't enough */
|
|
|
|
/* data to read. It then buffers the read data and can be called again with */
|
|
|
|
/* more data. If br->finish_ is 1, never fails. */
|
|
|
|
int BrotliWarmupBitReader(BrotliBitReader* const br);
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Return the prefetched bits, so they can be looked up. */
|
2013-10-11 08:26:07 +00:00
|
|
|
static BROTLI_INLINE uint32_t BrotliPrefetchBits(BrotliBitReader* const br) {
|
|
|
|
return (uint32_t)(br->val_ >> br->bit_pos_);
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:32:17 +00:00
|
|
|
/*
|
|
|
|
* Reload up to 32 bits byte-by-byte.
|
|
|
|
* This function works on both little and big endian.
|
|
|
|
*/
|
|
|
|
static BROTLI_INLINE void ShiftBytes32(BrotliBitReader* const br) {
|
2013-11-15 18:02:17 +00:00
|
|
|
while (br->bit_pos_ >= 8) {
|
|
|
|
br->val_ >>= 8;
|
2015-02-25 12:32:17 +00:00
|
|
|
br->val_ |= ((uint32_t)br->buf_[br->pos_ & BROTLI_IBUF_MASK]) << 24;
|
2013-11-15 18:02:17 +00:00
|
|
|
++br->pos_;
|
|
|
|
br->bit_pos_ -= 8;
|
2014-02-14 14:04:23 +00:00
|
|
|
br->bit_end_pos_ -= 8;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Fills up the input ringbuffer by calling the input callback.
|
|
|
|
|
|
|
|
Does nothing if there are at least 32 bytes present after current position.
|
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
Returns 0 if one of:
|
2013-12-16 13:45:57 +00:00
|
|
|
- the input callback returned an error, or
|
|
|
|
- there is no more input and the position is past the end of the stream.
|
2015-03-20 15:13:15 +00:00
|
|
|
- finish is false and less than BROTLI_READ_SIZE are available - a next call
|
|
|
|
when more data is available makes it continue including the partially read
|
|
|
|
data
|
2013-12-16 13:45:57 +00:00
|
|
|
|
|
|
|
After encountering the end of the input stream, 32 additional zero bytes are
|
|
|
|
copied to the ringbuffer, therefore it is safe to call this function after
|
|
|
|
every 32 bytes of input is read.
|
|
|
|
*/
|
2013-11-15 18:02:17 +00:00
|
|
|
static BROTLI_INLINE int BrotliReadMoreInput(BrotliBitReader* const br) {
|
2015-05-07 14:53:43 +00:00
|
|
|
if (PREDICT_TRUE(br->bit_end_pos_ > 256)) {
|
2013-11-15 18:02:17 +00:00
|
|
|
return 1;
|
2015-05-07 14:53:43 +00:00
|
|
|
} else if (PREDICT_FALSE(br->eos_)) {
|
2014-02-14 14:04:23 +00:00
|
|
|
return br->bit_pos_ <= br->bit_end_pos_;
|
2013-11-15 18:02:17 +00:00
|
|
|
} else {
|
2014-01-08 11:34:35 +00:00
|
|
|
uint8_t* dst = br->buf_ptr_;
|
2015-03-20 15:13:15 +00:00
|
|
|
int bytes_read = BrotliRead(br->input_, dst + br->tmp_bytes_read_,
|
|
|
|
(size_t) (BROTLI_READ_SIZE - br->tmp_bytes_read_));
|
2013-11-15 18:02:17 +00:00
|
|
|
if (bytes_read < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
bytes_read += br->tmp_bytes_read_;
|
|
|
|
br->tmp_bytes_read_ = 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
if (bytes_read < BROTLI_READ_SIZE) {
|
2015-03-20 15:13:15 +00:00
|
|
|
if (!br->finish_) {
|
|
|
|
br->tmp_bytes_read_ = bytes_read;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
br->eos_ = 1;
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Store 32 bytes of zero after the stream end. */
|
2015-05-07 15:10:27 +00:00
|
|
|
#if (BROTLI_USE_64_BITS) && !defined(ARMv7)
|
2013-11-15 18:02:17 +00:00
|
|
|
*(uint64_t*)(dst + bytes_read) = 0;
|
|
|
|
*(uint64_t*)(dst + bytes_read + 8) = 0;
|
|
|
|
*(uint64_t*)(dst + bytes_read + 16) = 0;
|
|
|
|
*(uint64_t*)(dst + bytes_read + 24) = 0;
|
|
|
|
#else
|
|
|
|
memset(dst + bytes_read, 0, 32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (dst == br->buf_) {
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Copy the head of the ringbuffer to the slack region. */
|
2015-05-07 15:10:27 +00:00
|
|
|
#if (BROTLI_USE_64_BITS) && !defined(ARMv7)
|
2013-11-15 18:02:17 +00:00
|
|
|
UNALIGNED_COPY64(br->buf_ + BROTLI_IBUF_SIZE - 32, br->buf_);
|
|
|
|
UNALIGNED_COPY64(br->buf_ + BROTLI_IBUF_SIZE - 24, br->buf_ + 8);
|
|
|
|
UNALIGNED_COPY64(br->buf_ + BROTLI_IBUF_SIZE - 16, br->buf_ + 16);
|
|
|
|
UNALIGNED_COPY64(br->buf_ + BROTLI_IBUF_SIZE - 8, br->buf_ + 24);
|
|
|
|
#else
|
|
|
|
memcpy(br->buf_ + (BROTLI_READ_SIZE << 1), br->buf_, 32);
|
|
|
|
#endif
|
2014-01-08 11:34:35 +00:00
|
|
|
br->buf_ptr_ = br->buf_ + BROTLI_READ_SIZE;
|
|
|
|
} else {
|
|
|
|
br->buf_ptr_ = br->buf_;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2014-02-14 14:04:23 +00:00
|
|
|
br->bit_end_pos_ += ((uint32_t)bytes_read << 3);
|
2013-11-15 18:02:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:37:00 +00:00
|
|
|
/* Similar to BrotliReadMoreInput, but guarantees num bytes available. The
|
|
|
|
maximum value for num is 128 bytes, the slack region size. */
|
|
|
|
static BROTLI_INLINE int BrotliReadInputAmount(
|
|
|
|
BrotliBitReader* const br, size_t num) {
|
|
|
|
if (PREDICT_TRUE(br->bit_end_pos_ > (num << 3))) {
|
|
|
|
return 1;
|
|
|
|
} else if (PREDICT_FALSE(br->eos_)) {
|
|
|
|
return br->bit_pos_ <= br->bit_end_pos_;
|
|
|
|
} else {
|
|
|
|
uint8_t* dst = br->buf_ptr_;
|
|
|
|
int bytes_read = BrotliRead(br->input_, dst + br->tmp_bytes_read_,
|
|
|
|
(size_t) (BROTLI_READ_SIZE - br->tmp_bytes_read_));
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bytes_read += br->tmp_bytes_read_;
|
|
|
|
br->tmp_bytes_read_ = 0;
|
|
|
|
if (bytes_read < BROTLI_READ_SIZE) {
|
|
|
|
if (!br->finish_) {
|
|
|
|
br->tmp_bytes_read_ = bytes_read;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
br->eos_ = 1;
|
|
|
|
/* Store num bytes of zero after the stream end. */
|
|
|
|
memset(dst + bytes_read, 0, num);
|
|
|
|
}
|
|
|
|
if (dst == br->buf_) {
|
|
|
|
/* Copy the head of the ringbuffer to the slack region. */
|
|
|
|
memcpy(br->buf_ + (BROTLI_READ_SIZE << 1), br->buf_, num);
|
|
|
|
br->buf_ptr_ = br->buf_ + BROTLI_READ_SIZE;
|
|
|
|
} else {
|
|
|
|
br->buf_ptr_ = br->buf_;
|
|
|
|
}
|
|
|
|
br->bit_end_pos_ += ((uint32_t)bytes_read << 3);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:32:17 +00:00
|
|
|
/* Guarantees that there are at least 24 bits in the buffer. */
|
2013-11-15 18:02:17 +00:00
|
|
|
static BROTLI_INLINE void BrotliFillBitWindow(BrotliBitReader* const br) {
|
2015-02-25 12:32:17 +00:00
|
|
|
#if (BROTLI_USE_64_BITS)
|
2015-06-12 13:12:23 +00:00
|
|
|
if (br->bit_pos_ >= 32) {
|
2015-02-25 12:32:17 +00:00
|
|
|
/*
|
2015-06-12 13:12:23 +00:00
|
|
|
* Advances the Read buffer by 4 bytes to make room for reading next
|
2015-02-25 12:32:17 +00:00
|
|
|
* 24 bits.
|
|
|
|
* The expression below needs a little-endian arch to work correctly.
|
|
|
|
* This gives a large speedup for decoding speed.
|
|
|
|
*/
|
2015-06-12 13:12:23 +00:00
|
|
|
br->val_ >>= 32;
|
|
|
|
br->val_ |= ((uint64_t)(*(const uint32_t*)(
|
|
|
|
br->buf_ + (br->pos_ & BROTLI_IBUF_MASK)))) << 32;
|
|
|
|
br->pos_ += 4;
|
|
|
|
br->bit_pos_ -= 32;
|
|
|
|
br->bit_end_pos_ -= 32;
|
2015-02-25 12:32:17 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
#else
|
2015-02-25 12:32:17 +00:00
|
|
|
ShiftBytes32(br);
|
2013-11-15 18:02:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Reads the specified number of bits from Read Buffer. */
|
2013-11-15 18:02:17 +00:00
|
|
|
static BROTLI_INLINE uint32_t BrotliReadBits(
|
|
|
|
BrotliBitReader* const br, int n_bits) {
|
2013-12-13 09:39:46 +00:00
|
|
|
uint32_t val;
|
2015-02-25 12:32:17 +00:00
|
|
|
#if (BROTLI_USE_64_BITS)
|
2015-05-07 15:10:27 +00:00
|
|
|
#if defined(ARMv7)
|
|
|
|
if ((64 - br->bit_pos_) < ((uint32_t) n_bits)) {
|
|
|
|
BrotliFillBitWindow(br);
|
|
|
|
}
|
|
|
|
val = (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
|
|
|
|
#else
|
2013-11-15 18:02:17 +00:00
|
|
|
BrotliFillBitWindow(br);
|
2015-05-07 15:10:27 +00:00
|
|
|
val = (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
|
|
|
|
#endif /* defined (ARMv7) */
|
2015-02-25 12:32:17 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* The if statement gives 2-4% speed boost on Canterbury data set with
|
|
|
|
* asm.js/firefox/x86-64.
|
|
|
|
*/
|
|
|
|
if ((32 - br->bit_pos_) < ((uint32_t) n_bits)) {
|
|
|
|
BrotliFillBitWindow(br);
|
|
|
|
}
|
2015-05-07 17:45:21 +00:00
|
|
|
val = (br->val_ >> br->bit_pos_) & BitMask(n_bits);
|
2015-05-07 15:10:27 +00:00
|
|
|
#endif /* BROTLI_USE_64_BITS */
|
2013-12-12 12:18:04 +00:00
|
|
|
#ifdef BROTLI_DECODE_DEBUG
|
2014-02-14 14:04:23 +00:00
|
|
|
printf("[BrotliReadBits] %010d %2d val: %6x\n",
|
2013-12-12 12:18:04 +00:00
|
|
|
(br->pos_ << 3) + br->bit_pos_ - 64, n_bits, val);
|
|
|
|
#endif
|
2014-01-08 11:34:35 +00:00
|
|
|
br->bit_pos_ += (uint32_t)n_bits;
|
2013-11-15 18:02:17 +00:00
|
|
|
return val;
|
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2013-12-16 13:45:57 +00:00
|
|
|
} /* extern "C" */
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
#endif /* BROTLI_DEC_BIT_READER_H_ */
|