Path: blob/master/3rdparty/libwebp/src/utils/bit_reader_inl_utils.h
16358 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* 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* const br, int prob) {107// Don't move this declaration! It makes a big speed difference to store108// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't109// alter br->range_ value.110range_t range = br->range_;111if (br->bits_ < 0) {112VP8LoadNewBytes(br);113}114{115const int pos = br->bits_;116const range_t split = (range * prob) >> 8;117const range_t value = (range_t)(br->value_ >> pos);118const int bit = (value > split);119if (bit) {120range -= split;121br->value_ -= (bit_t)(split + 1) << pos;122} else {123range = split + 1;124}125{126const int shift = 7 ^ BitsLog2Floor(range);127range <<= shift;128br->bits_ -= shift;129}130br->range_ = range - 1;131return bit;132}133}134135// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)136static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE137int VP8GetSigned(VP8BitReader* const br, int v) {138if (br->bits_ < 0) {139VP8LoadNewBytes(br);140}141{142const int pos = br->bits_;143const range_t split = br->range_ >> 1;144const range_t value = (range_t)(br->value_ >> pos);145const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0146br->bits_ -= 1;147br->range_ += mask;148br->range_ |= 1;149br->value_ -= (bit_t)((split + 1) & mask) << pos;150return (v ^ mask) - mask;151}152}153154static WEBP_INLINE int VP8GetBitAlt(VP8BitReader* const br, int prob) {155// Don't move this declaration! It makes a big speed difference to store156// 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't157// alter br->range_ value.158range_t range = br->range_;159if (br->bits_ < 0) {160VP8LoadNewBytes(br);161}162{163const int pos = br->bits_;164const range_t split = (range * prob) >> 8;165const range_t value = (range_t)(br->value_ >> pos);166int bit; // Don't use 'const int bit = (value > split);", it's slower.167if (value > split) {168range -= split + 1;169br->value_ -= (bit_t)(split + 1) << pos;170bit = 1;171} else {172range = split;173bit = 0;174}175if (range <= (range_t)0x7e) {176const int shift = kVP8Log2Range[range];177range = kVP8NewRange[range];178br->bits_ -= shift;179}180br->range_ = range;181return bit;182}183}184185#ifdef __cplusplus186} // extern "C"187#endif188189#endif // WEBP_UTILS_BIT_READER_INL_UTILS_H_190191192