2012-01-06 14:49:06 -08:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
2010-09-30 09:34:38 -04:00
|
|
|
//
|
2013-06-06 23:05:58 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2010-09-30 09:34:38 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Boolean decoder
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-09-07 09:26:35 +00:00
|
|
|
#include "./bit_reader.h"
|
2010-09-30 09:34:38 -04:00
|
|
|
|
faster decoding (3%-6%)
. revamped the boolean decoder to use less shifts
. added some description and ASCII art as explanations too.
. clarified the types further (bit_t, lbit_t, range_t, etc.)
. changed the negative field 'missing_' into positive 'bits_'
Some stats, decoding some randomly encoded WebP files:
with USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.4s (1.120 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.5s (1.128 ms/file/iterations)
without USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.5s (1.131 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.6s (1.142 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.6s (1.143 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.6s (1.149 ms/file/iterations)
Change-Id: I9277fb051676c05582e9c7ea3cb5a4b2a3ffb12e
2013-02-14 15:42:58 +01:00
|
|
|
#ifndef USE_RIGHT_JUSTIFY
|
|
|
|
#define MK(X) (((range_t)(X) << (BITS)) | (MASK))
|
|
|
|
#else
|
|
|
|
#define MK(X) ((range_t)(X))
|
|
|
|
#endif
|
2012-01-29 17:38:37 -08:00
|
|
|
|
2011-08-25 14:22:32 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 09:34:38 -04:00
|
|
|
// VP8BitReader
|
|
|
|
|
2011-03-10 15:05:59 -08:00
|
|
|
void VP8InitBitReader(VP8BitReader* const br,
|
|
|
|
const uint8_t* const start, const uint8_t* const end) {
|
2012-07-09 16:00:37 -07:00
|
|
|
assert(br != NULL);
|
|
|
|
assert(start != NULL);
|
2011-03-10 15:05:59 -08:00
|
|
|
assert(start <= end);
|
2012-01-29 17:38:37 -08:00
|
|
|
br->range_ = MK(255 - 1);
|
2011-03-10 15:05:59 -08:00
|
|
|
br->buf_ = start;
|
|
|
|
br->buf_end_ = end;
|
|
|
|
br->value_ = 0;
|
faster decoding (3%-6%)
. revamped the boolean decoder to use less shifts
. added some description and ASCII art as explanations too.
. clarified the types further (bit_t, lbit_t, range_t, etc.)
. changed the negative field 'missing_' into positive 'bits_'
Some stats, decoding some randomly encoded WebP files:
with USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.4s (1.120 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.5s (1.128 ms/file/iterations)
without USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.5s (1.131 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.6s (1.142 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.6s (1.143 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.6s (1.149 ms/file/iterations)
Change-Id: I9277fb051676c05582e9c7ea3cb5a4b2a3ffb12e
2013-02-14 15:42:58 +01:00
|
|
|
br->bits_ = -8; // to load the very first 8bits
|
2011-03-10 15:05:59 -08:00
|
|
|
br->eof_ = 0;
|
2010-09-30 09:34:38 -04:00
|
|
|
}
|
|
|
|
|
2014-05-14 07:07:08 +02:00
|
|
|
void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) {
|
|
|
|
if (br->buf_ != NULL) {
|
|
|
|
br->buf_ += offset;
|
|
|
|
br->buf_end_ += offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-30 09:34:38 -04:00
|
|
|
const uint8_t kVP8Log2Range[128] = {
|
|
|
|
7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2012-01-29 17:38:37 -08:00
|
|
|
// range = (range << kVP8Log2Range[range]) + trailing 1's
|
faster decoding (3%-6%)
. revamped the boolean decoder to use less shifts
. added some description and ASCII art as explanations too.
. clarified the types further (bit_t, lbit_t, range_t, etc.)
. changed the negative field 'missing_' into positive 'bits_'
Some stats, decoding some randomly encoded WebP files:
with USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.4s (1.120 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.5s (1.128 ms/file/iterations)
without USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.5s (1.131 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.6s (1.142 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.6s (1.143 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.6s (1.149 ms/file/iterations)
Change-Id: I9277fb051676c05582e9c7ea3cb5a4b2a3ffb12e
2013-02-14 15:42:58 +01:00
|
|
|
const range_t kVP8NewRange[128] = {
|
2012-01-29 17:38:37 -08:00
|
|
|
MK(127), MK(127), MK(191), MK(127), MK(159), MK(191), MK(223), MK(127),
|
|
|
|
MK(143), MK(159), MK(175), MK(191), MK(207), MK(223), MK(239), MK(127),
|
|
|
|
MK(135), MK(143), MK(151), MK(159), MK(167), MK(175), MK(183), MK(191),
|
|
|
|
MK(199), MK(207), MK(215), MK(223), MK(231), MK(239), MK(247), MK(127),
|
|
|
|
MK(131), MK(135), MK(139), MK(143), MK(147), MK(151), MK(155), MK(159),
|
|
|
|
MK(163), MK(167), MK(171), MK(175), MK(179), MK(183), MK(187), MK(191),
|
|
|
|
MK(195), MK(199), MK(203), MK(207), MK(211), MK(215), MK(219), MK(223),
|
|
|
|
MK(227), MK(231), MK(235), MK(239), MK(243), MK(247), MK(251), MK(127),
|
|
|
|
MK(129), MK(131), MK(133), MK(135), MK(137), MK(139), MK(141), MK(143),
|
|
|
|
MK(145), MK(147), MK(149), MK(151), MK(153), MK(155), MK(157), MK(159),
|
|
|
|
MK(161), MK(163), MK(165), MK(167), MK(169), MK(171), MK(173), MK(175),
|
|
|
|
MK(177), MK(179), MK(181), MK(183), MK(185), MK(187), MK(189), MK(191),
|
|
|
|
MK(193), MK(195), MK(197), MK(199), MK(201), MK(203), MK(205), MK(207),
|
|
|
|
MK(209), MK(211), MK(213), MK(215), MK(217), MK(219), MK(221), MK(223),
|
|
|
|
MK(225), MK(227), MK(229), MK(231), MK(233), MK(235), MK(237), MK(239),
|
|
|
|
MK(241), MK(243), MK(245), MK(247), MK(249), MK(251), MK(253), MK(127)
|
2010-09-30 09:34:38 -04:00
|
|
|
};
|
|
|
|
|
2012-01-29 17:38:37 -08:00
|
|
|
#undef MK
|
|
|
|
|
|
|
|
void VP8LoadFinalBytes(VP8BitReader* const br) {
|
2012-07-09 16:00:37 -07:00
|
|
|
assert(br != NULL && br->buf_ != NULL);
|
2012-01-29 17:38:37 -08:00
|
|
|
// Only read 8bits at a time
|
|
|
|
if (br->buf_ < br->buf_end_) {
|
faster decoding (3%-6%)
. revamped the boolean decoder to use less shifts
. added some description and ASCII art as explanations too.
. clarified the types further (bit_t, lbit_t, range_t, etc.)
. changed the negative field 'missing_' into positive 'bits_'
Some stats, decoding some randomly encoded WebP files:
with USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.3s (1.097 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.4s (1.120 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.5s (1.128 ms/file/iterations)
without USE_RIGHT_JUSTIFY:
BITS=32 => 133 files, 50 loops => 7.5s (1.131 ms/file/iterations)
BITS=24 => 133 files, 50 loops => 7.6s (1.142 ms/file/iterations)
BITS=16 => 133 files, 50 loops => 7.6s (1.143 ms/file/iterations)
BITS=8 => 133 files, 50 loops => 7.6s (1.149 ms/file/iterations)
Change-Id: I9277fb051676c05582e9c7ea3cb5a4b2a3ffb12e
2013-02-14 15:42:58 +01:00
|
|
|
#ifndef USE_RIGHT_JUSTIFY
|
|
|
|
br->value_ |= (bit_t)(*br->buf_++) << ((BITS) - 8 - br->bits_);
|
|
|
|
#else
|
|
|
|
br->value_ = (bit_t)(*br->buf_++) | (br->value_ << 8);
|
|
|
|
#endif
|
|
|
|
br->bits_ += 8;
|
|
|
|
} else if (!br->eof_) {
|
|
|
|
#ifdef USE_RIGHT_JUSTIFY
|
|
|
|
// These are not strictly needed, but it makes the behaviour
|
|
|
|
// consistent for both USE_RIGHT_JUSTIFY and !USE_RIGHT_JUSTIFY.
|
|
|
|
br->value_ <<= 8;
|
|
|
|
br->bits_ += 8;
|
|
|
|
#endif
|
2012-01-29 17:38:37 -08:00
|
|
|
br->eof_ = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 14:22:32 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 09:34:38 -04:00
|
|
|
// Higher-level calls
|
|
|
|
|
|
|
|
uint32_t VP8GetValue(VP8BitReader* const br, int bits) {
|
|
|
|
uint32_t v = 0;
|
|
|
|
while (bits-- > 0) {
|
|
|
|
v |= VP8GetBit(br, 0x80) << bits;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
|
2010-11-17 20:06:01 -08:00
|
|
|
const int value = VP8GetValue(br, bits);
|
2010-09-30 09:34:38 -04:00
|
|
|
return VP8Get(br) ? -value : value;
|
|
|
|
}
|
|
|
|
|
2012-04-10 17:23:38 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2012-04-13 01:56:31 -07:00
|
|
|
// VP8LBitReader
|
|
|
|
|
2013-02-20 00:13:23 +01:00
|
|
|
#define LBITS 64 // Number of bits prefetched.
|
|
|
|
#define WBITS 32 // Minimum number of bytes needed after VP8LFillBitWindow.
|
|
|
|
#define LOG8_WBITS 4 // Number of bytes needed to store WBITS bits.
|
|
|
|
|
2014-05-22 07:17:24 +02:00
|
|
|
static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = {
|
2014-05-22 21:38:53 -07:00
|
|
|
0,
|
|
|
|
0x000001, 0x000003, 0x000007, 0x00000f,
|
|
|
|
0x00001f, 0x00003f, 0x00007f, 0x0000ff,
|
|
|
|
0x0001ff, 0x0003ff, 0x0007ff, 0x000fff,
|
|
|
|
0x001fff, 0x003fff, 0x007fff, 0x00ffff,
|
|
|
|
0x01ffff, 0x03ffff, 0x07ffff, 0x0fffff,
|
|
|
|
0x1fffff, 0x3fffff, 0x7fffff, 0xffffff
|
2012-04-10 17:23:38 -07:00
|
|
|
};
|
|
|
|
|
2014-05-22 21:38:53 -07:00
|
|
|
void VP8LInitBitReader(VP8LBitReader* const br, const uint8_t* const start,
|
2012-04-10 17:23:38 -07:00
|
|
|
size_t length) {
|
|
|
|
size_t i;
|
2014-05-22 21:38:53 -07:00
|
|
|
vp8l_val_t value = 0;
|
2012-07-09 16:00:37 -07:00
|
|
|
assert(br != NULL);
|
|
|
|
assert(start != NULL);
|
|
|
|
assert(length < 0xfffffff8u); // can't happen with a RIFF chunk.
|
2012-04-10 17:23:38 -07:00
|
|
|
|
|
|
|
br->len_ = length;
|
|
|
|
br->val_ = 0;
|
|
|
|
br->bit_pos_ = 0;
|
|
|
|
br->eos_ = 0;
|
|
|
|
br->error_ = 0;
|
2014-05-22 21:38:53 -07:00
|
|
|
|
|
|
|
if (length > sizeof(br->val_)) {
|
|
|
|
length = sizeof(br->val_);
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
2014-05-22 21:38:53 -07:00
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
value |= (vp8l_val_t)start[i] << (8 * i);
|
|
|
|
}
|
|
|
|
br->val_ = value;
|
|
|
|
br->pos_ = length;
|
|
|
|
br->buf_ = start;
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:13:14 -07:00
|
|
|
// Special version that assumes br->pos_ <= br_len_.
|
|
|
|
static int IsEndOfStreamSpecial(const VP8LBitReader* const br) {
|
|
|
|
assert(br->pos_ <= br->len_);
|
|
|
|
return br->pos_ == br->len_ && br->bit_pos_ >= LBITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int IsEndOfStream(const VP8LBitReader* const br) {
|
|
|
|
return (br->pos_ > br->len_) || IsEndOfStreamSpecial(br);
|
|
|
|
}
|
|
|
|
|
2012-04-13 01:56:31 -07:00
|
|
|
void VP8LBitReaderSetBuffer(VP8LBitReader* const br,
|
2012-04-05 09:31:10 +00:00
|
|
|
const uint8_t* const buf, size_t len) {
|
2012-07-09 16:00:37 -07:00
|
|
|
assert(br != NULL);
|
|
|
|
assert(buf != NULL);
|
|
|
|
assert(len < 0xfffffff8u); // can't happen with a RIFF chunk.
|
2012-04-05 09:31:10 +00:00
|
|
|
br->buf_ = buf;
|
|
|
|
br->len_ = len;
|
2014-04-22 15:13:14 -07:00
|
|
|
br->eos_ = IsEndOfStream(br);
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
|
|
|
|
2013-02-20 00:13:23 +01:00
|
|
|
// If not at EOS, reload up to LBITS byte-by-byte
|
2012-04-13 01:56:31 -07:00
|
|
|
static void ShiftBytes(VP8LBitReader* const br) {
|
2012-04-10 17:23:38 -07:00
|
|
|
while (br->bit_pos_ >= 8 && br->pos_ < br->len_) {
|
|
|
|
br->val_ >>= 8;
|
2013-02-20 00:13:23 +01:00
|
|
|
br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (LBITS - 8);
|
2012-04-10 17:23:38 -07:00
|
|
|
++br->pos_;
|
|
|
|
br->bit_pos_ -= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 01:56:31 -07:00
|
|
|
void VP8LFillBitWindow(VP8LBitReader* const br) {
|
2013-02-20 00:13:23 +01:00
|
|
|
if (br->bit_pos_ >= WBITS) {
|
|
|
|
#if (defined(__x86_64__) || defined(_M_X64))
|
|
|
|
if (br->pos_ + sizeof(br->val_) < br->len_) {
|
|
|
|
br->val_ >>= WBITS;
|
|
|
|
br->bit_pos_ -= WBITS;
|
2012-04-10 17:23:38 -07:00
|
|
|
// The expression below needs a little-endian arch to work correctly.
|
|
|
|
// This gives a large speedup for decoding speed.
|
2013-02-20 00:13:23 +01:00
|
|
|
br->val_ |= *(const vp8l_val_t*)(br->buf_ + br->pos_) << (LBITS - WBITS);
|
|
|
|
br->pos_ += LOG8_WBITS;
|
|
|
|
return;
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-02-20 00:13:23 +01:00
|
|
|
ShiftBytes(br); // Slow path.
|
2014-04-22 15:13:14 -07:00
|
|
|
br->eos_ = IsEndOfStreamSpecial(br);
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 01:56:31 -07:00
|
|
|
uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) {
|
2012-04-10 17:23:38 -07:00
|
|
|
assert(n_bits >= 0);
|
|
|
|
// Flag an error if end_of_stream or n_bits is more than allowed limit.
|
2014-05-22 07:17:24 +02:00
|
|
|
if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) {
|
2013-02-20 00:13:23 +01:00
|
|
|
const uint32_t val =
|
|
|
|
(uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits];
|
|
|
|
const int new_bits = br->bit_pos_ + n_bits;
|
|
|
|
br->bit_pos_ = new_bits;
|
2012-04-10 17:23:38 -07:00
|
|
|
// If this read is going to cross the read buffer, set the eos flag.
|
2014-04-22 15:13:14 -07:00
|
|
|
br->eos_ = IsEndOfStreamSpecial(br);
|
2013-02-20 00:13:23 +01:00
|
|
|
ShiftBytes(br);
|
|
|
|
return val;
|
2012-04-10 17:23:38 -07:00
|
|
|
} else {
|
|
|
|
br->error_ = 1;
|
2013-02-20 00:13:23 +01:00
|
|
|
return 0;
|
2012-04-10 17:23:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 14:22:32 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 09:34:38 -04:00
|
|
|
|