Path: blob/master/thirdparty/libwebp/src/utils/bit_reader_inl_utils.h
9912 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 <string.h> // for memcpy2324#include "src/dsp/dsp.h"25#include "src/utils/bit_reader_utils.h"26#include "src/utils/endian_inl_utils.h"27#include "src/utils/utils.h"2829#ifdef __cplusplus30extern "C" {31#endif3233//------------------------------------------------------------------------------34// Derived type lbit_t = natural type for memory I/O3536#if (BITS > 32)37typedef uint64_t lbit_t;38#elif (BITS > 16)39typedef uint32_t lbit_t;40#elif (BITS > 8)41typedef uint16_t lbit_t;42#else43typedef uint8_t lbit_t;44#endif4546extern const uint8_t kVP8Log2Range[128];47extern const uint8_t kVP8NewRange[128];4849// special case for the tail byte-reading50void VP8LoadFinalBytes(VP8BitReader* const br);5152//------------------------------------------------------------------------------53// Inlined critical functions5455// makes sure br->value_ has at least BITS bits worth of data56static WEBP_UBSAN_IGNORE_UNDEF WEBP_INLINE57void VP8LoadNewBytes(VP8BitReader* WEBP_RESTRICT const br) {58assert(br != NULL && br->buf_ != NULL);59// Read 'BITS' bits at a time if possible.60if (br->buf_ < br->buf_max_) {61// convert memory type to register type (with some zero'ing!)62bit_t bits;63#if defined(WEBP_USE_MIPS32)64// This is needed because of un-aligned read.65lbit_t in_bits;66lbit_t* p_buf_ = (lbit_t*)br->buf_;67__asm__ volatile(68".set push \n\t"69".set at \n\t"70".set macro \n\t"71"ulw %[in_bits], 0(%[p_buf_]) \n\t"72".set pop \n\t"73: [in_bits]"=r"(in_bits)74: [p_buf_]"r"(p_buf_)75: "memory", "at"76);77#else78lbit_t in_bits;79memcpy(&in_bits, br->buf_, sizeof(in_bits));80#endif81br->buf_ += BITS >> 3;82#if !defined(WORDS_BIGENDIAN)83#if (BITS > 32)84bits = BSwap64(in_bits);85bits >>= 64 - BITS;86#elif (BITS >= 24)87bits = BSwap32(in_bits);88bits >>= (32 - BITS);89#elif (BITS == 16)90bits = BSwap16(in_bits);91#else // BITS == 892bits = (bit_t)in_bits;93#endif // BITS > 3294#else // WORDS_BIGENDIAN95bits = (bit_t)in_bits;96if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS);97#endif98br->value_ = bits | (br->value_ << BITS);99br->bits_ += BITS;100} else {101VP8LoadFinalBytes(br); // no need to be inlined102}103}104105// Read a bit with proba 'prob'. Speed-critical function!106static WEBP_INLINE int VP8GetBit(VP8BitReader* WEBP_RESTRICT const br,107int prob, const char label[]) {108// Don't move this declaration! It makes a big speed difference to store109// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't110// alter br->range_ value.111range_t range = br->range_;112if (br->bits_ < 0) {113VP8LoadNewBytes(br);114}115{116const int pos = br->bits_;117const range_t split = (range * prob) >> 8;118const range_t value = (range_t)(br->value_ >> pos);119const int bit = (value > split);120if (bit) {121range -= split;122br->value_ -= (bit_t)(split + 1) << pos;123} else {124range = split + 1;125}126{127const int shift = 7 ^ BitsLog2Floor(range);128range <<= shift;129br->bits_ -= shift;130}131br->range_ = range - 1;132BT_TRACK(br);133return bit;134}135}136137// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)138static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE139int VP8GetSigned(VP8BitReader* WEBP_RESTRICT const br, int v,140const char label[]) {141if (br->bits_ < 0) {142VP8LoadNewBytes(br);143}144{145const int pos = br->bits_;146const range_t split = br->range_ >> 1;147const range_t value = (range_t)(br->value_ >> pos);148const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0149br->bits_ -= 1;150br->range_ += (range_t)mask;151br->range_ |= 1;152br->value_ -= (bit_t)((split + 1) & (uint32_t)mask) << pos;153BT_TRACK(br);154return (v ^ mask) - mask;155}156}157158static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* WEBP_RESTRICT const br,159int prob, const char label[]) {160// Don't move this declaration! It makes a big speed difference to store161// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't162// alter br->range_ value.163range_t range = br->range_;164if (br->bits_ < 0) {165VP8LoadNewBytes(br);166}167{168const int pos = br->bits_;169const range_t split = (range * prob) >> 8;170const range_t value = (range_t)(br->value_ >> pos);171int bit; // Don't use 'const int bit = (value > split);", it's slower.172if (value > split) {173range -= split + 1;174br->value_ -= (bit_t)(split + 1) << pos;175bit = 1;176} else {177range = split;178bit = 0;179}180if (range <= (range_t)0x7e) {181const int shift = kVP8Log2Range[range];182range = kVP8NewRange[range];183br->bits_ -= shift;184}185br->range_ = range;186BT_TRACK(br);187return bit;188}189}190191#ifdef __cplusplus192} // extern "C"193#endif194195#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_196197198