Path: blob/master/thirdparty/libwebp/src/dsp/lossless_common.h
9913 views
// Copyright 2012 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// Image transforms and color space conversion methods for lossless decoder.10//11// Authors: Vikas Arora ([email protected])12// Jyrki Alakuijala ([email protected])13// Vincent Rabaud ([email protected])1415#ifndef WEBP_DSP_LOSSLESS_COMMON_H_16#define WEBP_DSP_LOSSLESS_COMMON_H_1718#include "src/dsp/cpu.h"19#include "src/utils/utils.h"20#include "src/webp/types.h"2122#ifdef __cplusplus23extern "C" {24#endif2526//------------------------------------------------------------------------------27// Decoding2829// color mapping related functions.30static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {31return (idx >> 8) & 0xff;32}3334static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {35return idx;36}3738static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {39return val;40}4142static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {43return (val >> 8) & 0xff;44}4546//------------------------------------------------------------------------------47// Misc methods.4849// Computes sampled size of 'size' when sampling using 'sampling bits'.50static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,51uint32_t sampling_bits) {52return (size + (1 << sampling_bits) - 1) >> sampling_bits;53}5455// Converts near lossless quality into max number of bits shaved off.56static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {57// 100 -> 058// 80..99 -> 159// 60..79 -> 260// 40..59 -> 361// 20..39 -> 462// 0..19 -> 563return 5 - near_lossless_quality / 20;64}6566// -----------------------------------------------------------------------------67// Faster logarithm for integers. Small values use a look-up table.6869// The threshold till approximate version of log_2 can be used.70// Practically, we can get rid of the call to log() as the two values match to71// very high degree (the ratio of these two is 0.99999x).72// Keeping a high threshold for now.73#define APPROX_LOG_WITH_CORRECTION_MAX 6553674#define APPROX_LOG_MAX 409675// VP8LFastLog2 and VP8LFastSLog2 are used on elements from image histograms.76// The histogram values cannot exceed the maximum number of pixels, which77// is (1 << 14) * (1 << 14). Therefore S * log(S) < (1 << 33).78// No more than 32 bits of precision should be chosen.79// To match the original float implementation, 23 bits of precision are used.80#define LOG_2_PRECISION_BITS 2381#define LOG_2_RECIPROCAL 1.4426950408889633870046509400708682// LOG_2_RECIPROCAL * (1 << LOG_2_PRECISION_BITS)83#define LOG_2_RECIPROCAL_FIXED_DOUBLE 12102203.16156148537993431091308593750084#define LOG_2_RECIPROCAL_FIXED ((uint64_t)12102203)85#define LOG_LOOKUP_IDX_MAX 25686extern const uint32_t kLog2Table[LOG_LOOKUP_IDX_MAX];87extern const uint64_t kSLog2Table[LOG_LOOKUP_IDX_MAX];88typedef uint32_t (*VP8LFastLog2SlowFunc)(uint32_t v);89typedef uint64_t (*VP8LFastSLog2SlowFunc)(uint32_t v);9091extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;92extern VP8LFastSLog2SlowFunc VP8LFastSLog2Slow;9394static WEBP_INLINE uint32_t VP8LFastLog2(uint32_t v) {95return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);96}97// Fast calculation of v * log2(v) for integer input.98static WEBP_INLINE uint64_t VP8LFastSLog2(uint32_t v) {99return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);100}101102static WEBP_INLINE uint64_t RightShiftRound(uint64_t v, uint32_t shift) {103return (v + (1ull << shift >> 1)) >> shift;104}105106static WEBP_INLINE int64_t DivRound(int64_t a, int64_t b) {107return ((a < 0) == (b < 0)) ? ((a + b / 2) / b) : ((a - b / 2) / b);108}109110#define WEBP_INT64_MAX ((int64_t)((1ull << 63) - 1))111#define WEBP_UINT64_MAX (~0ull)112113// -----------------------------------------------------------------------------114// PrefixEncode()115116// Splitting of distance and length codes into prefixes and117// extra bits. The prefixes are encoded with an entropy code118// while the extra bits are stored just as normal bits.119static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,120int* const extra_bits) {121const int highest_bit = BitsLog2Floor(--distance);122const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;123*extra_bits = highest_bit - 1;124*code = 2 * highest_bit + second_highest_bit;125}126127static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,128int* const extra_bits,129int* const extra_bits_value) {130const int highest_bit = BitsLog2Floor(--distance);131const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;132*extra_bits = highest_bit - 1;133*extra_bits_value = distance & ((1 << *extra_bits) - 1);134*code = 2 * highest_bit + second_highest_bit;135}136137#define PREFIX_LOOKUP_IDX_MAX 512138typedef struct {139int8_t code_;140int8_t extra_bits_;141} VP8LPrefixCode;142143// These tables are derived using VP8LPrefixEncodeNoLUT.144extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];145extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];146static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,147int* const extra_bits) {148if (distance < PREFIX_LOOKUP_IDX_MAX) {149const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];150*code = prefix_code.code_;151*extra_bits = prefix_code.extra_bits_;152} else {153VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);154}155}156157static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,158int* const extra_bits,159int* const extra_bits_value) {160if (distance < PREFIX_LOOKUP_IDX_MAX) {161const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];162*code = prefix_code.code_;163*extra_bits = prefix_code.extra_bits_;164*extra_bits_value = kPrefixEncodeExtraBitsValue[distance];165} else {166VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);167}168}169170// Sum of each component, mod 256.171static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE172uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {173const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);174const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);175return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);176}177178// Difference of each component, mod 256.179static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE180uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {181const uint32_t alpha_and_green =1820x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);183const uint32_t red_and_blue =1840xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);185return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);186}187188//------------------------------------------------------------------------------189// Transform-related functions used in both encoding and decoding.190191// Macros used to create a batch predictor that iteratively uses a192// one-pixel predictor.193194// The predictor is added to the output pixel (which195// is therefore considered as a residual) to get the final prediction.196#define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \197static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \198int num_pixels, uint32_t* WEBP_RESTRICT out) { \199int x; \200assert(upper != NULL); \201for (x = 0; x < num_pixels; ++x) { \202const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x); \203out[x] = VP8LAddPixels(in[x], pred); \204} \205}206207#ifdef __cplusplus208} // extern "C"209#endif210211#endif // WEBP_DSP_LOSSLESS_COMMON_H_212213214