Path: blob/master/thirdparty/libwebp/src/utils/bit_reader_inl_utils.h
21083 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Specific inlined methods for boolean decoder [VP8GetBit() ...]10// This file should be included by the .c sources that actually need to call11// these methods.12//13// Author: Skal ([email protected])1415#ifndef WEBP_UTILS_BIT_READER_INL_UTILS_H_16#define WEBP_UTILS_BIT_READER_INL_UTILS_H_1718#ifdef HAVE_CONFIG_H19#include "src/webp/config.h"20#endif2122#include <assert.h>23#include <string.h> // for memcpy2425#include "src/dsp/cpu.h"26#include "src/dsp/dsp.h"27#include "src/utils/bit_reader_utils.h"28#include "src/utils/endian_inl_utils.h"29#include "src/utils/utils.h"30#include "src/webp/types.h"3132#ifdef __cplusplus33extern "C" {34#endif3536//------------------------------------------------------------------------------37// Derived type lbit_t = natural type for memory I/O3839#if (BITS > 32)40typedef uint64_t lbit_t;41#elif (BITS > 16)42typedef uint32_t lbit_t;43#elif (BITS > 8)44typedef uint16_t lbit_t;45#else46typedef uint8_t lbit_t;47#endif4849extern const uint8_t kVP8Log2Range[128];50extern const uint8_t kVP8NewRange[128];5152// special case for the tail byte-reading53void VP8LoadFinalBytes(VP8BitReader* const br);5455//------------------------------------------------------------------------------56// Inlined critical functions5758// makes sure br->value has at least BITS bits worth of data59static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE60void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) {61assert(br != NULL && br->buf != NULL);62// Read 'BITS' bits at a time if possible.63if (br->buf < br->buf_max) {64// convert memory type to register type (with some zero'ing!)65bit_t bits;66#if defined(WEBP_USE_MIPS32)67// This is needed because of un-aligned read.68lbit_t in_bits;69lbit_t* p_buf = (lbit_t*)br->buf;70__asm__ volatile(71".set push \n\t"72".set at \n\t"73".set macro \n\t"74"ulw %[in_bits], 0(%[p_buf]) \n\t"75".set pop \n\t"76: [in_bits]"=r"(in_bits)77: [p_buf]"r"(p_buf)78: "memory", "at"79);80#else81lbit_t in_bits;82memcpy(&in_bits, br->buf, sizeof(in_bits));83#endif84br->buf += BITS >> 3;85#if !defined(WORDS_BIGENDIAN)86#if (BITS > 32)87bits = BSwap64(in_bits);88bits >>= 64 - BITS;89#elif (BITS >= 24)90bits = BSwap32(in_bits);91bits >>= (32 - BITS);92#elif (BITS == 16)93bits = BSwap16(in_bits);94#else // BITS == 895bits = (bit_t)in_bits;96#endif // BITS > 3297#else // WORDS_BIGENDIAN98bits = (bit_t)in_bits;99if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);100#endif101br->value = bits | (br->value << BITS);102br->bits += BITS;103} else {104VP8LoadFinalBytes(br); // no need to be inlined105}106}107108// Read a bit with proba 'prob'. Speed-critical function!109static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br,110int prob, const char label[]) {111// Don't move this declaration! It makes a big speed difference to store112// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't113// alter br->range value.114range_t range = br->range;115if (br->bits < 0) {116VP8LoadNewBytes(br);117}118{119const int pos = br->bits;120const range_t split = (range * prob) >> 8;121const range_t value = (range_t)(br->value >> pos);122const int bit = (value > split);123if (bit) {124range -= split;125br->value -= (bit_t)(split + 1) << pos;126} else {127range = split + 1;128}129{130const int shift = 7 ^ BitsLog2Floor(range);131range <<= shift;132br->bits -= shift;133}134br->range = range - 1;135BT_TRACK(br);136return bit;137}138}139140// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)141static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE142int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v,143const char label[]) {144if (br->bits < 0) {145VP8LoadNewBytes(br);146}147{148const int pos = br->bits;149const range_t split = br->range >> 1;150const range_t value = (range_t)(br->value >> pos);151const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0152br->bits -= 1;153br->range += (range_t)mask;154br->range |= 1;155br->value -= (bit_t)((split + 1) & (uint32_t)mask) << pos;156BT_TRACK(br);157return (v ^ mask) - mask;158}159}160161static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br,162int prob, const char label[]) {163// Don't move this declaration! It makes a big speed difference to store164// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't165// alter br->range value.166range_t range = br->range;167if (br->bits < 0) {168VP8LoadNewBytes(br);169}170{171const int pos = br->bits;172const range_t split = (range * prob) >> 8;173const range_t value = (range_t)(br->value >> pos);174int bit; // Don't use 'const int bit = (value > split);", it's slower.175if (value > split) {176range -= split + 1;177br->value -= (bit_t)(split + 1) << pos;178bit = 1;179} else {180range = split;181bit = 0;182}183if (range <= (range_t)0x7e) {184const int shift = kVP8Log2Range[range];185range = kVP8NewRange[range];186br->bits -= shift;187}188br->range = range;189BT_TRACK(br);190return bit;191}192}193194#ifdef __cplusplus195} // extern "C"196#endif197198#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_199200201