|
Packit |
9c6abc |
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Use of this source code is governed by a BSD-style license
|
|
Packit |
9c6abc |
// that can be found in the COPYING file in the root of the source
|
|
Packit |
9c6abc |
// tree. An additional intellectual property rights grant can be found
|
|
Packit |
9c6abc |
// in the file PATENTS. All contributing project authors may
|
|
Packit |
9c6abc |
// be found in the AUTHORS file in the root of the source tree.
|
|
Packit |
9c6abc |
// -----------------------------------------------------------------------------
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Image transforms and color space conversion methods for lossless decoder.
|
|
Packit |
9c6abc |
//
|
|
Packit |
9c6abc |
// Authors: Vikas Arora (vikaas.arora@gmail.com)
|
|
Packit |
9c6abc |
// Jyrki Alakuijala (jyrki@google.com)
|
|
Packit |
9c6abc |
// Vincent Rabaud (vrabaud@google.com)
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifndef WEBP_DSP_LOSSLESS_COMMON_H_
|
|
Packit |
9c6abc |
#define WEBP_DSP_LOSSLESS_COMMON_H_
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#include "src/webp/types.h"
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#include "src/utils/utils.h"
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
extern "C" {
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
//------------------------------------------------------------------------------
|
|
Packit |
9c6abc |
// Decoding
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// color mapping related functions.
|
|
Packit |
9c6abc |
static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {
|
|
Packit |
9c6abc |
return (idx >> 8) & 0xff;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {
|
|
Packit |
9c6abc |
return idx;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {
|
|
Packit |
9c6abc |
return val;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {
|
|
Packit |
9c6abc |
return (val >> 8) & 0xff;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
//------------------------------------------------------------------------------
|
|
Packit |
9c6abc |
// Misc methods.
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Computes sampled size of 'size' when sampling using 'sampling bits'.
|
|
Packit |
9c6abc |
static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
|
|
Packit |
9c6abc |
uint32_t sampling_bits) {
|
|
Packit |
9c6abc |
return (size + (1 << sampling_bits) - 1) >> sampling_bits;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Converts near lossless quality into max number of bits shaved off.
|
|
Packit |
9c6abc |
static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {
|
|
Packit |
9c6abc |
// 100 -> 0
|
|
Packit |
9c6abc |
// 80..99 -> 1
|
|
Packit |
9c6abc |
// 60..79 -> 2
|
|
Packit |
9c6abc |
// 40..59 -> 3
|
|
Packit |
9c6abc |
// 20..39 -> 4
|
|
Packit |
9c6abc |
// 0..19 -> 5
|
|
Packit |
9c6abc |
return 5 - near_lossless_quality / 20;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// -----------------------------------------------------------------------------
|
|
Packit |
9c6abc |
// Faster logarithm for integers. Small values use a look-up table.
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// The threshold till approximate version of log_2 can be used.
|
|
Packit |
9c6abc |
// Practically, we can get rid of the call to log() as the two values match to
|
|
Packit |
9c6abc |
// very high degree (the ratio of these two is 0.99999x).
|
|
Packit |
9c6abc |
// Keeping a high threshold for now.
|
|
Packit |
9c6abc |
#define APPROX_LOG_WITH_CORRECTION_MAX 65536
|
|
Packit |
9c6abc |
#define APPROX_LOG_MAX 4096
|
|
Packit |
9c6abc |
#define LOG_2_RECIPROCAL 1.44269504088896338700465094007086
|
|
Packit |
9c6abc |
#define LOG_LOOKUP_IDX_MAX 256
|
|
Packit |
9c6abc |
extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
|
|
Packit |
9c6abc |
extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
|
|
Packit |
9c6abc |
typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
|
|
Packit |
9c6abc |
extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
|
|
Packit |
9c6abc |
return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
// Fast calculation of v * log2(v) for integer input.
|
|
Packit |
9c6abc |
static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
|
|
Packit |
9c6abc |
return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// -----------------------------------------------------------------------------
|
|
Packit |
9c6abc |
// PrefixEncode()
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Splitting of distance and length codes into prefixes and
|
|
Packit |
9c6abc |
// extra bits. The prefixes are encoded with an entropy code
|
|
Packit |
9c6abc |
// while the extra bits are stored just as normal bits.
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
|
|
Packit |
9c6abc |
int* const extra_bits) {
|
|
Packit |
9c6abc |
const int highest_bit = BitsLog2Floor(--distance);
|
|
Packit |
9c6abc |
const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
|
|
Packit |
9c6abc |
*extra_bits = highest_bit - 1;
|
|
Packit |
9c6abc |
*code = 2 * highest_bit + second_highest_bit;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
|
|
Packit |
9c6abc |
int* const extra_bits,
|
|
Packit |
9c6abc |
int* const extra_bits_value) {
|
|
Packit |
9c6abc |
const int highest_bit = BitsLog2Floor(--distance);
|
|
Packit |
9c6abc |
const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
|
|
Packit |
9c6abc |
*extra_bits = highest_bit - 1;
|
|
Packit |
9c6abc |
*extra_bits_value = distance & ((1 << *extra_bits) - 1);
|
|
Packit |
9c6abc |
*code = 2 * highest_bit + second_highest_bit;
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#define PREFIX_LOOKUP_IDX_MAX 512
|
|
Packit |
9c6abc |
typedef struct {
|
|
Packit |
9c6abc |
int8_t code_;
|
|
Packit |
9c6abc |
int8_t extra_bits_;
|
|
Packit |
9c6abc |
} VP8LPrefixCode;
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// These tables are derived using VP8LPrefixEncodeNoLUT.
|
|
Packit |
9c6abc |
extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
|
|
Packit |
9c6abc |
extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
|
|
Packit |
9c6abc |
int* const extra_bits) {
|
|
Packit |
9c6abc |
if (distance < PREFIX_LOOKUP_IDX_MAX) {
|
|
Packit |
9c6abc |
const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
|
|
Packit |
9c6abc |
*code = prefix_code.code_;
|
|
Packit |
9c6abc |
*extra_bits = prefix_code.extra_bits_;
|
|
Packit |
9c6abc |
} else {
|
|
Packit |
9c6abc |
VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
|
|
Packit |
9c6abc |
int* const extra_bits,
|
|
Packit |
9c6abc |
int* const extra_bits_value) {
|
|
Packit |
9c6abc |
if (distance < PREFIX_LOOKUP_IDX_MAX) {
|
|
Packit |
9c6abc |
const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
|
|
Packit |
9c6abc |
*code = prefix_code.code_;
|
|
Packit |
9c6abc |
*extra_bits = prefix_code.extra_bits_;
|
|
Packit |
9c6abc |
*extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
|
|
Packit |
9c6abc |
} else {
|
|
Packit |
9c6abc |
VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Sum of each component, mod 256.
|
|
Packit |
9c6abc |
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
|
|
Packit |
9c6abc |
uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {
|
|
Packit |
9c6abc |
const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);
|
|
Packit |
9c6abc |
const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);
|
|
Packit |
9c6abc |
return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Difference of each component, mod 256.
|
|
Packit |
9c6abc |
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
|
|
Packit |
9c6abc |
uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
|
|
Packit |
9c6abc |
const uint32_t alpha_and_green =
|
|
Packit |
9c6abc |
0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
|
|
Packit |
9c6abc |
const uint32_t red_and_blue =
|
|
Packit |
9c6abc |
0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
|
|
Packit |
9c6abc |
return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
//------------------------------------------------------------------------------
|
|
Packit |
9c6abc |
// Transform-related functions use din both encoding and decoding.
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// Macros used to create a batch predictor that iteratively uses a
|
|
Packit |
9c6abc |
// one-pixel predictor.
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// The predictor is added to the output pixel (which
|
|
Packit |
9c6abc |
// is therefore considered as a residual) to get the final prediction.
|
|
Packit |
9c6abc |
#define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \
|
|
Packit |
9c6abc |
static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \
|
|
Packit |
9c6abc |
int num_pixels, uint32_t* out) { \
|
|
Packit |
9c6abc |
int x; \
|
|
Packit |
9c6abc |
for (x = 0; x < num_pixels; ++x) { \
|
|
Packit |
9c6abc |
const uint32_t pred = (PREDICTOR)(out[x - 1], upper + x); \
|
|
Packit |
9c6abc |
out[x] = VP8LAddPixels(in[x], pred); \
|
|
Packit |
9c6abc |
} \
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
// It subtracts the prediction from the input pixel and stores the residual
|
|
Packit |
9c6abc |
// in the output pixel.
|
|
Packit |
9c6abc |
#define GENERATE_PREDICTOR_SUB(PREDICTOR, PREDICTOR_SUB) \
|
|
Packit |
9c6abc |
static void PREDICTOR_SUB(const uint32_t* in, const uint32_t* upper, \
|
|
Packit |
9c6abc |
int num_pixels, uint32_t* out) { \
|
|
Packit |
9c6abc |
int x; \
|
|
Packit |
9c6abc |
for (x = 0; x < num_pixels; ++x) { \
|
|
Packit |
9c6abc |
const uint32_t pred = (PREDICTOR)(in[x - 1], upper + x); \
|
|
Packit |
9c6abc |
out[x] = VP8LSubPixels(in[x], pred); \
|
|
Packit |
9c6abc |
} \
|
|
Packit |
9c6abc |
}
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#ifdef __cplusplus
|
|
Packit |
9c6abc |
} // extern "C"
|
|
Packit |
9c6abc |
#endif
|
|
Packit |
9c6abc |
|
|
Packit |
9c6abc |
#endif // WEBP_DSP_LOSSLESS_COMMON_H_
|