2015-12-04 14:21:22 +01:00
|
|
|
// Copyright 2014 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Specific inlined methods for boolean decoder [VP8GetBit() ...]
|
|
|
|
// This file should be included by the .c sources that actually need to call
|
|
|
|
// these methods.
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2017-12-11 18:11:11 +01:00
|
|
|
#ifndef WEBP_UTILS_BIT_READER_INL_UTILS_H_
|
|
|
|
#define WEBP_UTILS_BIT_READER_INL_UTILS_H_
|
2015-12-04 14:21:22 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2017-12-11 18:11:11 +01:00
|
|
|
#include "src/webp/config.h"
|
2015-12-04 14:21:22 +01:00
|
|
|
#endif
|
|
|
|
|
2017-02-17 15:49:40 +01:00
|
|
|
#include <string.h> // for memcpy
|
2015-12-04 14:21:22 +01:00
|
|
|
|
2017-12-11 18:11:11 +01:00
|
|
|
#include "src/dsp/dsp.h"
|
|
|
|
#include "src/utils/bit_reader_utils.h"
|
|
|
|
#include "src/utils/endian_inl_utils.h"
|
|
|
|
#include "src/utils/utils.h"
|
2015-12-04 14:21:22 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Derived type lbit_t = natural type for memory I/O
|
|
|
|
|
|
|
|
#if (BITS > 32)
|
|
|
|
typedef uint64_t lbit_t;
|
|
|
|
#elif (BITS > 16)
|
|
|
|
typedef uint32_t lbit_t;
|
|
|
|
#elif (BITS > 8)
|
|
|
|
typedef uint16_t lbit_t;
|
|
|
|
#else
|
|
|
|
typedef uint8_t lbit_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern const uint8_t kVP8Log2Range[128];
|
|
|
|
extern const uint8_t kVP8NewRange[128];
|
|
|
|
|
|
|
|
// special case for the tail byte-reading
|
|
|
|
void VP8LoadFinalBytes(VP8BitReader* const br);
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Inlined critical functions
|
|
|
|
|
|
|
|
// makes sure br->value_ has at least BITS bits worth of data
|
2016-07-08 12:29:58 +02:00
|
|
|
static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE
|
2021-11-19 13:54:40 +01:00
|
|
|
void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) {
|
2015-12-04 14:21:22 +01:00
|
|
|
assert(br != NULL && br->buf_ != NULL);
|
|
|
|
// Read 'BITS' bits at a time if possible.
|
|
|
|
if (br->buf_ < br->buf_max_) {
|
|
|
|
// convert memory type to register type (with some zero'ing!)
|
|
|
|
bit_t bits;
|
2017-02-17 15:49:40 +01:00
|
|
|
#if defined(WEBP_USE_MIPS32)
|
2015-12-04 14:21:22 +01:00
|
|
|
// This is needed because of un-aligned read.
|
|
|
|
lbit_t in_bits;
|
|
|
|
lbit_t* p_buf_ = (lbit_t*)br->buf_;
|
|
|
|
__asm__ volatile(
|
|
|
|
".set push \n\t"
|
|
|
|
".set at \n\t"
|
|
|
|
".set macro \n\t"
|
|
|
|
"ulw %[in_bits], 0(%[p_buf_]) \n\t"
|
|
|
|
".set pop \n\t"
|
|
|
|
: [in_bits]"=r"(in_bits)
|
|
|
|
: [p_buf_]"r"(p_buf_)
|
|
|
|
: "memory", "at"
|
|
|
|
);
|
|
|
|
#else
|
2017-02-17 15:49:40 +01:00
|
|
|
lbit_t in_bits;
|
|
|
|
memcpy(&in_bits, br->buf_, sizeof(in_bits));
|
2015-12-04 14:21:22 +01:00
|
|
|
#endif
|
|
|
|
br->buf_ += BITS >> 3;
|
|
|
|
#if !defined(WORDS_BIGENDIAN)
|
|
|
|
#if (BITS > 32)
|
|
|
|
bits = BSwap64(in_bits);
|
|
|
|
bits >>= 64 - BITS;
|
|
|
|
#elif (BITS >= 24)
|
|
|
|
bits = BSwap32(in_bits);
|
|
|
|
bits >>= (32 - BITS);
|
|
|
|
#elif (BITS == 16)
|
|
|
|
bits = BSwap16(in_bits);
|
|
|
|
#else // BITS == 8
|
|
|
|
bits = (bit_t)in_bits;
|
|
|
|
#endif // BITS > 32
|
|
|
|
#else // WORDS_BIGENDIAN
|
|
|
|
bits = (bit_t)in_bits;
|
|
|
|
if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);
|
|
|
|
#endif
|
|
|
|
br->value_ = bits | (br->value_ << BITS);
|
|
|
|
br->bits_ += BITS;
|
|
|
|
} else {
|
|
|
|
VP8LoadFinalBytes(br); // no need to be inlined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a bit with proba 'prob'. Speed-critical function!
|
2021-11-19 13:54:40 +01:00
|
|
|
static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br,
|
2019-10-27 13:29:43 +01:00
|
|
|
int prob, const char label[]) {
|
2015-12-04 14:21:22 +01:00
|
|
|
// Don't move this declaration! It makes a big speed difference to store
|
|
|
|
// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
|
|
|
|
// alter br->range_ value.
|
|
|
|
range_t range = br->range_;
|
|
|
|
if (br->bits_ < 0) {
|
|
|
|
VP8LoadNewBytes(br);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const int pos = br->bits_;
|
|
|
|
const range_t split = (range * prob) >> 8;
|
|
|
|
const range_t value = (range_t)(br->value_ >> pos);
|
2017-02-17 15:49:40 +01:00
|
|
|
const int bit = (value > split);
|
|
|
|
if (bit) {
|
|
|
|
range -= split;
|
2015-12-04 14:21:22 +01:00
|
|
|
br->value_ -= (bit_t)(split + 1) << pos;
|
|
|
|
} else {
|
2017-02-17 15:49:40 +01:00
|
|
|
range = split + 1;
|
2015-12-04 14:21:22 +01:00
|
|
|
}
|
2017-02-17 15:49:40 +01:00
|
|
|
{
|
|
|
|
const int shift = 7 ^ BitsLog2Floor(range);
|
|
|
|
range <<= shift;
|
2015-12-04 14:21:22 +01:00
|
|
|
br->bits_ -= shift;
|
|
|
|
}
|
2017-02-17 15:49:40 +01:00
|
|
|
br->range_ = range - 1;
|
2019-10-27 13:29:43 +01:00
|
|
|
BT_TRACK(br);
|
2015-12-04 14:21:22 +01:00
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
|
2017-02-17 15:49:40 +01:00
|
|
|
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
|
2021-11-19 13:54:40 +01:00
|
|
|
int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v,
|
|
|
|
const char label[]) {
|
2015-12-04 14:21:22 +01:00
|
|
|
if (br->bits_ < 0) {
|
|
|
|
VP8LoadNewBytes(br);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const int pos = br->bits_;
|
|
|
|
const range_t split = br->range_ >> 1;
|
|
|
|
const range_t value = (range_t)(br->value_ >> pos);
|
|
|
|
const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0
|
|
|
|
br->bits_ -= 1;
|
|
|
|
br->range_ += mask;
|
|
|
|
br->range_ |= 1;
|
|
|
|
br->value_ -= (bit_t)((split + 1) & mask) << pos;
|
2019-10-27 13:29:43 +01:00
|
|
|
BT_TRACK(br);
|
2015-12-04 14:21:22 +01:00
|
|
|
return (v ^ mask) - mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 13:54:40 +01:00
|
|
|
static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br,
|
2019-10-27 13:29:43 +01:00
|
|
|
int prob, const char label[]) {
|
2017-02-17 15:49:40 +01:00
|
|
|
// Don't move this declaration! It makes a big speed difference to store
|
|
|
|
// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't
|
|
|
|
// alter br->range_ value.
|
|
|
|
range_t range = br->range_;
|
|
|
|
if (br->bits_ < 0) {
|
|
|
|
VP8LoadNewBytes(br);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const int pos = br->bits_;
|
|
|
|
const range_t split = (range * prob) >> 8;
|
|
|
|
const range_t value = (range_t)(br->value_ >> pos);
|
|
|
|
int bit; // Don't use 'const int bit = (value > split);", it's slower.
|
|
|
|
if (value > split) {
|
|
|
|
range -= split + 1;
|
|
|
|
br->value_ -= (bit_t)(split + 1) << pos;
|
|
|
|
bit = 1;
|
|
|
|
} else {
|
|
|
|
range = split;
|
|
|
|
bit = 0;
|
|
|
|
}
|
|
|
|
if (range <= (range_t)0x7e) {
|
|
|
|
const int shift = kVP8Log2Range[range];
|
|
|
|
range = kVP8NewRange[range];
|
|
|
|
br->bits_ -= shift;
|
|
|
|
}
|
|
|
|
br->range_ = range;
|
2019-10-27 13:29:43 +01:00
|
|
|
BT_TRACK(br);
|
2017-02-17 15:49:40 +01:00
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 14:21:22 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2018-12-10 14:33:51 +01:00
|
|
|
#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_
|