xref: /aosp_15_r20/external/webp/src/dsp/lossless_common.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Image transforms and color space conversion methods for lossless decoder.
11*b2055c35SXin Li //
12*b2055c35SXin Li // Authors: Vikas Arora ([email protected])
13*b2055c35SXin Li //          Jyrki Alakuijala ([email protected])
14*b2055c35SXin Li //          Vincent Rabaud ([email protected])
15*b2055c35SXin Li 
16*b2055c35SXin Li #ifndef WEBP_DSP_LOSSLESS_COMMON_H_
17*b2055c35SXin Li #define WEBP_DSP_LOSSLESS_COMMON_H_
18*b2055c35SXin Li 
19*b2055c35SXin Li #include "src/dsp/cpu.h"
20*b2055c35SXin Li #include "src/utils/utils.h"
21*b2055c35SXin Li #include "src/webp/types.h"
22*b2055c35SXin Li 
23*b2055c35SXin Li #ifdef __cplusplus
24*b2055c35SXin Li extern "C" {
25*b2055c35SXin Li #endif
26*b2055c35SXin Li 
27*b2055c35SXin Li //------------------------------------------------------------------------------
28*b2055c35SXin Li // Decoding
29*b2055c35SXin Li 
30*b2055c35SXin Li // color mapping related functions.
VP8GetARGBIndex(uint32_t idx)31*b2055c35SXin Li static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) {
32*b2055c35SXin Li   return (idx >> 8) & 0xff;
33*b2055c35SXin Li }
34*b2055c35SXin Li 
VP8GetAlphaIndex(uint8_t idx)35*b2055c35SXin Li static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) {
36*b2055c35SXin Li   return idx;
37*b2055c35SXin Li }
38*b2055c35SXin Li 
VP8GetARGBValue(uint32_t val)39*b2055c35SXin Li static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) {
40*b2055c35SXin Li   return val;
41*b2055c35SXin Li }
42*b2055c35SXin Li 
VP8GetAlphaValue(uint32_t val)43*b2055c35SXin Li static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) {
44*b2055c35SXin Li   return (val >> 8) & 0xff;
45*b2055c35SXin Li }
46*b2055c35SXin Li 
47*b2055c35SXin Li //------------------------------------------------------------------------------
48*b2055c35SXin Li // Misc methods.
49*b2055c35SXin Li 
50*b2055c35SXin Li // Computes sampled size of 'size' when sampling using 'sampling bits'.
VP8LSubSampleSize(uint32_t size,uint32_t sampling_bits)51*b2055c35SXin Li static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size,
52*b2055c35SXin Li                                               uint32_t sampling_bits) {
53*b2055c35SXin Li   return (size + (1 << sampling_bits) - 1) >> sampling_bits;
54*b2055c35SXin Li }
55*b2055c35SXin Li 
56*b2055c35SXin Li // Converts near lossless quality into max number of bits shaved off.
VP8LNearLosslessBits(int near_lossless_quality)57*b2055c35SXin Li static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) {
58*b2055c35SXin Li   //    100 -> 0
59*b2055c35SXin Li   // 80..99 -> 1
60*b2055c35SXin Li   // 60..79 -> 2
61*b2055c35SXin Li   // 40..59 -> 3
62*b2055c35SXin Li   // 20..39 -> 4
63*b2055c35SXin Li   //  0..19 -> 5
64*b2055c35SXin Li   return 5 - near_lossless_quality / 20;
65*b2055c35SXin Li }
66*b2055c35SXin Li 
67*b2055c35SXin Li // -----------------------------------------------------------------------------
68*b2055c35SXin Li // Faster logarithm for integers. Small values use a look-up table.
69*b2055c35SXin Li 
70*b2055c35SXin Li // The threshold till approximate version of log_2 can be used.
71*b2055c35SXin Li // Practically, we can get rid of the call to log() as the two values match to
72*b2055c35SXin Li // very high degree (the ratio of these two is 0.99999x).
73*b2055c35SXin Li // Keeping a high threshold for now.
74*b2055c35SXin Li #define APPROX_LOG_WITH_CORRECTION_MAX  65536
75*b2055c35SXin Li #define APPROX_LOG_MAX                   4096
76*b2055c35SXin Li #define LOG_2_RECIPROCAL 1.44269504088896338700465094007086
77*b2055c35SXin Li #define LOG_LOOKUP_IDX_MAX 256
78*b2055c35SXin Li extern const float kLog2Table[LOG_LOOKUP_IDX_MAX];
79*b2055c35SXin Li extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX];
80*b2055c35SXin Li typedef float (*VP8LFastLog2SlowFunc)(uint32_t v);
81*b2055c35SXin Li 
82*b2055c35SXin Li extern VP8LFastLog2SlowFunc VP8LFastLog2Slow;
83*b2055c35SXin Li extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow;
84*b2055c35SXin Li 
VP8LFastLog2(uint32_t v)85*b2055c35SXin Li static WEBP_INLINE float VP8LFastLog2(uint32_t v) {
86*b2055c35SXin Li   return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v);
87*b2055c35SXin Li }
88*b2055c35SXin Li // Fast calculation of v * log2(v) for integer input.
VP8LFastSLog2(uint32_t v)89*b2055c35SXin Li static WEBP_INLINE float VP8LFastSLog2(uint32_t v) {
90*b2055c35SXin Li   return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v);
91*b2055c35SXin Li }
92*b2055c35SXin Li 
93*b2055c35SXin Li // -----------------------------------------------------------------------------
94*b2055c35SXin Li // PrefixEncode()
95*b2055c35SXin Li 
96*b2055c35SXin Li // Splitting of distance and length codes into prefixes and
97*b2055c35SXin Li // extra bits. The prefixes are encoded with an entropy code
98*b2055c35SXin Li // while the extra bits are stored just as normal bits.
VP8LPrefixEncodeBitsNoLUT(int distance,int * const code,int * const extra_bits)99*b2055c35SXin Li static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code,
100*b2055c35SXin Li                                                   int* const extra_bits) {
101*b2055c35SXin Li   const int highest_bit = BitsLog2Floor(--distance);
102*b2055c35SXin Li   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
103*b2055c35SXin Li   *extra_bits = highest_bit - 1;
104*b2055c35SXin Li   *code = 2 * highest_bit + second_highest_bit;
105*b2055c35SXin Li }
106*b2055c35SXin Li 
VP8LPrefixEncodeNoLUT(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)107*b2055c35SXin Li static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code,
108*b2055c35SXin Li                                               int* const extra_bits,
109*b2055c35SXin Li                                               int* const extra_bits_value) {
110*b2055c35SXin Li   const int highest_bit = BitsLog2Floor(--distance);
111*b2055c35SXin Li   const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
112*b2055c35SXin Li   *extra_bits = highest_bit - 1;
113*b2055c35SXin Li   *extra_bits_value = distance & ((1 << *extra_bits) - 1);
114*b2055c35SXin Li   *code = 2 * highest_bit + second_highest_bit;
115*b2055c35SXin Li }
116*b2055c35SXin Li 
117*b2055c35SXin Li #define PREFIX_LOOKUP_IDX_MAX   512
118*b2055c35SXin Li typedef struct {
119*b2055c35SXin Li   int8_t code_;
120*b2055c35SXin Li   int8_t extra_bits_;
121*b2055c35SXin Li } VP8LPrefixCode;
122*b2055c35SXin Li 
123*b2055c35SXin Li // These tables are derived using VP8LPrefixEncodeNoLUT.
124*b2055c35SXin Li extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX];
125*b2055c35SXin Li extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX];
VP8LPrefixEncodeBits(int distance,int * const code,int * const extra_bits)126*b2055c35SXin Li static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code,
127*b2055c35SXin Li                                              int* const extra_bits) {
128*b2055c35SXin Li   if (distance < PREFIX_LOOKUP_IDX_MAX) {
129*b2055c35SXin Li     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
130*b2055c35SXin Li     *code = prefix_code.code_;
131*b2055c35SXin Li     *extra_bits = prefix_code.extra_bits_;
132*b2055c35SXin Li   } else {
133*b2055c35SXin Li     VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits);
134*b2055c35SXin Li   }
135*b2055c35SXin Li }
136*b2055c35SXin Li 
VP8LPrefixEncode(int distance,int * const code,int * const extra_bits,int * const extra_bits_value)137*b2055c35SXin Li static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
138*b2055c35SXin Li                                          int* const extra_bits,
139*b2055c35SXin Li                                          int* const extra_bits_value) {
140*b2055c35SXin Li   if (distance < PREFIX_LOOKUP_IDX_MAX) {
141*b2055c35SXin Li     const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance];
142*b2055c35SXin Li     *code = prefix_code.code_;
143*b2055c35SXin Li     *extra_bits = prefix_code.extra_bits_;
144*b2055c35SXin Li     *extra_bits_value = kPrefixEncodeExtraBitsValue[distance];
145*b2055c35SXin Li   } else {
146*b2055c35SXin Li     VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value);
147*b2055c35SXin Li   }
148*b2055c35SXin Li }
149*b2055c35SXin Li 
150*b2055c35SXin Li // Sum of each component, mod 256.
151*b2055c35SXin Li static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
VP8LAddPixels(uint32_t a,uint32_t b)152*b2055c35SXin Li uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {
153*b2055c35SXin Li   const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);
154*b2055c35SXin Li   const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);
155*b2055c35SXin Li   return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
156*b2055c35SXin Li }
157*b2055c35SXin Li 
158*b2055c35SXin Li // Difference of each component, mod 256.
159*b2055c35SXin Li static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
VP8LSubPixels(uint32_t a,uint32_t b)160*b2055c35SXin Li uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
161*b2055c35SXin Li   const uint32_t alpha_and_green =
162*b2055c35SXin Li       0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
163*b2055c35SXin Li   const uint32_t red_and_blue =
164*b2055c35SXin Li       0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu);
165*b2055c35SXin Li   return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
166*b2055c35SXin Li }
167*b2055c35SXin Li 
168*b2055c35SXin Li //------------------------------------------------------------------------------
169*b2055c35SXin Li // Transform-related functions used in both encoding and decoding.
170*b2055c35SXin Li 
171*b2055c35SXin Li // Macros used to create a batch predictor that iteratively uses a
172*b2055c35SXin Li // one-pixel predictor.
173*b2055c35SXin Li 
174*b2055c35SXin Li // The predictor is added to the output pixel (which
175*b2055c35SXin Li // is therefore considered as a residual) to get the final prediction.
176*b2055c35SXin Li #define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD)             \
177*b2055c35SXin Li static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \
178*b2055c35SXin Li                           int num_pixels, uint32_t* out) {           \
179*b2055c35SXin Li   int x;                                                             \
180*b2055c35SXin Li   assert(upper != NULL);                                             \
181*b2055c35SXin Li   for (x = 0; x < num_pixels; ++x) {                                 \
182*b2055c35SXin Li     const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x);       \
183*b2055c35SXin Li     out[x] = VP8LAddPixels(in[x], pred);                             \
184*b2055c35SXin Li   }                                                                  \
185*b2055c35SXin Li }
186*b2055c35SXin Li 
187*b2055c35SXin Li #ifdef __cplusplus
188*b2055c35SXin Li }    // extern "C"
189*b2055c35SXin Li #endif
190*b2055c35SXin Li 
191*b2055c35SXin Li #endif  // WEBP_DSP_LOSSLESS_COMMON_H_
192