Path: blob/master/thirdparty/libwebp/src/dsp/lossless_common.h
21382 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 <assert.h>19#include <stddef.h>2021#include "src/dsp/cpu.h"22#include "src/utils/utils.h"23#include "src/webp/types.h"2425#ifdef __cplusplus26extern "C" {27#endif2829//------------------------------------------------------------------------------30// Decoding3132// color mapping related functions.33static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {34return (idx >> 8) & 0xff;35}3637static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {38return idx;39}4041static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {42return val;43}4445static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {46return (val >> 8) & 0xff;47}4849//------------------------------------------------------------------------------50// Misc methods.5152// Computes sampled size of 'size' when sampling using 'sampling bits'.53static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,54uint32_t sampling_bits) {55return (size + (1 << sampling_bits) - 1) >> sampling_bits;56}5758// Converts near lossless quality into max number of bits shaved off.59static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {60// 100 -> 061// 80..99 -> 162// 60..79 -> 263// 40..59 -> 364// 20..39 -> 465// 0..19 -> 566return 5 - near_lossless_quality / 20;67}6869// -----------------------------------------------------------------------------70// Faster logarithm for integers. Small values use a look-up table.7172// The threshold till approximate version of log_2 can be used.73// Practically, we can get rid of the call to log() as the two values match to74// very high degree (the ratio of these two is 0.99999x).75// Keeping a high threshold for now.76#define APPROX_LOG_WITH_CORRECTION_MAX 6553677#define APPROX_LOG_MAX 409678// VP8LFastLog2 and VP8LFastSLog2 are used on elements from image histograms.79// The histogram values cannot exceed the maximum number of pixels, which80// is (1 << 14) * (1 << 14). Therefore S * log(S) < (1 << 33).81// No more than 32 bits of precision should be chosen.82// To match the original float implementation, 23 bits of precision are used.83#define LOG_2_PRECISION_BITS 2384#define LOG_2_RECIPROCAL 1.4426950408889633870046509400708685// LOG_2_RECIPROCAL * (1 << LOG_2_PRECISION_BITS)86#define LOG_2_RECIPROCAL_FIXED_DOUBLE 12102203.16156148537993431091308593750087#define LOG_2_RECIPROCAL_FIXED ((uint64_t)12102203)88#define LOG_LOOKUP_IDX_MAX 25689extern const uint32_t kLog2Table[LOG_LOOKUP_IDX_MAX];90extern const uint64_t kSLog2Table[LOG_LOOKUP_IDX_MAX];91typedef uint32_t (*VP8LFastLog2SlowFunc)(uint32_t v);92typedef uint64_t (*VP8LFastSLog2SlowFunc)(uint32_t v);9394extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;95extern VP8LFastSLog2SlowFunc VP8LFastSLog2Slow;9697static WEBP_INLINE uint32_t VP8LFastLog2(uint32_t v) {98return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);99}100// Fast calculation of v * log2(v) for integer input.101static WEBP_INLINE uint64_t VP8LFastSLog2(uint32_t v) {102return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);103}104105static WEBP_INLINE uint64_t RightShiftRound(uint64_t v, uint32_t shift) {106return (v + (1ull << shift >> 1)) >> shift;107}108109static WEBP_INLINE int64_t DivRound(int64_t a, int64_t b) {110return ((a < 0) == (b < 0)) ? ((a + b / 2) / b) : ((a - b / 2) / b);111}112113#define WEBP_INT64_MAX ((int64_t)((1ull << 63) - 1))114#define WEBP_UINT64_MAX (~0ull)115116// -----------------------------------------------------------------------------117// PrefixEncode()118119// Splitting of distance and length codes into prefixes and120// extra bits. The prefixes are encoded with an entropy code121// while the extra bits are stored just as normal bits.122static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,123int* const extra_bits) {124const int highest_bit = BitsLog2Floor(--distance);125const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;126*extra_bits = highest_bit - 1;127*code = 2 * highest_bit + second_highest_bit;128}129130static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,131int* const extra_bits,132int* const extra_bits_value) {133const int highest_bit = BitsLog2Floor(--distance);134const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;135*extra_bits = highest_bit - 1;136*extra_bits_value = distance & ((1 << *extra_bits) - 1);137*code = 2 * highest_bit + second_highest_bit;138}139140#define PREFIX_LOOKUP_IDX_MAX 512141typedef struct {142int8_t code;143int8_t extra_bits;144} VP8LPrefixCode;145146// These tables are derived using VP8LPrefixEncodeNoLUT.147extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];148extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];149static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,150int* const extra_bits) {151if (distance < PREFIX_LOOKUP_IDX_MAX) {152const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];153*code = prefix_code.code;154*extra_bits = prefix_code.extra_bits;155} else {156VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);157}158}159160static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,161int* const extra_bits,162int* const extra_bits_value) {163if (distance < PREFIX_LOOKUP_IDX_MAX) {164const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];165*code = prefix_code.code;166*extra_bits = prefix_code.extra_bits;167*extra_bits_value = kPrefixEncodeExtraBitsValue[distance];168} else {169VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);170}171}172173// Sum of each component, mod 256.174static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE175uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {176const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);177const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);178return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);179}180181// Difference of each component, mod 256.182static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE183uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {184const uint32_t alpha_and_green =1850x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);186const uint32_t red_and_blue =1870xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);188return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);189}190191//------------------------------------------------------------------------------192// Transform-related functions used in both encoding and decoding.193194// Macros used to create a batch predictor that iteratively uses a195// one-pixel predictor.196197// The predictor is added to the output pixel (which198// is therefore considered as a residual) to get the final prediction.199#define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \200static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \201int num_pixels, uint32_t* WEBP_RESTRICT out) { \202int x; \203assert(upper != NULL); \204for (x = 0; x < num_pixels; ++x) { \205const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x); \206out[x] = VP8LAddPixels(in[x], pred); \207} \208}209210#ifdef __cplusplus211} // extern "C"212#endif213214#endif // WEBP_DSP_LOSSLESS_COMMON_H_215216217