| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef WEBP_DSP_LOSSLESS_COMMON_H_ |
| 17 |
#define WEBP_DSP_LOSSLESS_COMMON_H_ |
| 18 |
|
| 19 |
#include "src/dsp/cpu.h" |
| 20 |
#include "src/utils/utils.h" |
| 21 |
#include "src/webp/types.h" |
| 22 |
|
| 23 |
#ifdef __cplusplus |
| 24 |
extern "C" { |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static WEBP_INLINE uint32_t VP8GetARGBIndex(uint32_t idx) { |
| 32 |
return (idx >> 8) & 0xff; |
| 33 |
} |
| 34 |
|
| 35 |
static WEBP_INLINE uint8_t VP8GetAlphaIndex(uint8_t idx) { |
| 36 |
return idx; |
| 37 |
} |
| 38 |
|
| 39 |
static WEBP_INLINE uint32_t VP8GetARGBValue(uint32_t val) { |
| 40 |
return val; |
| 41 |
} |
| 42 |
|
| 43 |
static WEBP_INLINE uint8_t VP8GetAlphaValue(uint32_t val) { |
| 44 |
return (val >> 8) & 0xff; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
static WEBP_INLINE uint32_t VP8LSubSampleSize(uint32_t size, |
| 52 |
uint32_t sampling_bits) { |
| 53 |
return (size + (1 << sampling_bits) - 1) >> sampling_bits; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
static WEBP_INLINE int VP8LNearLosslessBits(int near_lossless_quality) { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
return 5 - near_lossless_quality / 20; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define APPROX_LOG_WITH_CORRECTION_MAX 65536 |
| 75 |
#define APPROX_LOG_MAX 4096 |
| 76 |
#define LOG_2_RECIPROCAL 1.44269504088896338700465094007086 |
| 77 |
#define LOG_LOOKUP_IDX_MAX 256 |
| 78 |
extern const float kLog2Table[LOG_LOOKUP_IDX_MAX]; |
| 79 |
extern const float kSLog2Table[LOG_LOOKUP_IDX_MAX]; |
| 80 |
typedef float (*VP8LFastLog2SlowFunc)(uint32_t v); |
| 81 |
|
| 82 |
extern VP8LFastLog2SlowFunc VP8LFastLog2Slow; |
| 83 |
extern VP8LFastLog2SlowFunc VP8LFastSLog2Slow; |
| 84 |
|
| 85 |
static WEBP_INLINE float VP8LFastLog2(uint32_t v) { |
| 86 |
return (v < LOG_LOOKUP_IDX_MAX) ? kLog2Table[v] : VP8LFastLog2Slow(v); |
| 87 |
} |
| 88 |
|
| 89 |
static WEBP_INLINE float VP8LFastSLog2(uint32_t v) { |
| 90 |
return (v < LOG_LOOKUP_IDX_MAX) ? kSLog2Table[v] : VP8LFastSLog2Slow(v); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static WEBP_INLINE void VP8LPrefixEncodeBitsNoLUT(int distance, int* const code, |
| 100 |
int* const extra_bits) { |
| 101 |
const int highest_bit = BitsLog2Floor(--distance); |
| 102 |
const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; |
| 103 |
*extra_bits = highest_bit - 1; |
| 104 |
*code = 2 * highest_bit + second_highest_bit; |
| 105 |
} |
| 106 |
|
| 107 |
static WEBP_INLINE void VP8LPrefixEncodeNoLUT(int distance, int* const code, |
| 108 |
int* const extra_bits, |
| 109 |
int* const extra_bits_value) { |
| 110 |
const int highest_bit = BitsLog2Floor(--distance); |
| 111 |
const int second_highest_bit = (distance >> (highest_bit - 1)) & 1; |
| 112 |
*extra_bits = highest_bit - 1; |
| 113 |
*extra_bits_value = distance & ((1 << *extra_bits) - 1); |
| 114 |
*code = 2 * highest_bit + second_highest_bit; |
| 115 |
} |
| 116 |
|
| 117 |
#define PREFIX_LOOKUP_IDX_MAX 512 |
| 118 |
typedef struct { |
| 119 |
int8_t code_; |
| 120 |
int8_t extra_bits_; |
| 121 |
} VP8LPrefixCode; |
| 122 |
|
| 123 |
|
| 124 |
extern const VP8LPrefixCode kPrefixEncodeCode[PREFIX_LOOKUP_IDX_MAX]; |
| 125 |
extern const uint8_t kPrefixEncodeExtraBitsValue[PREFIX_LOOKUP_IDX_MAX]; |
| 126 |
static WEBP_INLINE void VP8LPrefixEncodeBits(int distance, int* const code, |
| 127 |
int* const extra_bits) { |
| 128 |
if (distance < PREFIX_LOOKUP_IDX_MAX) { |
| 129 |
const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance]; |
| 130 |
*code = prefix_code.code_; |
| 131 |
*extra_bits = prefix_code.extra_bits_; |
| 132 |
} else { |
| 133 |
VP8LPrefixEncodeBitsNoLUT(distance, code, extra_bits); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code, |
| 138 |
int* const extra_bits, |
| 139 |
int* const extra_bits_value) { |
| 140 |
if (distance < PREFIX_LOOKUP_IDX_MAX) { |
| 141 |
const VP8LPrefixCode prefix_code = kPrefixEncodeCode[distance]; |
| 142 |
*code = prefix_code.code_; |
| 143 |
*extra_bits = prefix_code.extra_bits_; |
| 144 |
*extra_bits_value = kPrefixEncodeExtraBitsValue[distance]; |
| 145 |
} else { |
| 146 |
VP8LPrefixEncodeNoLUT(distance, code, extra_bits, extra_bits_value); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE |
| 152 |
uint32_t VP8LAddPixels(uint32_t a, uint32_t b) { |
| 153 |
const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u); |
| 154 |
const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu); |
| 155 |
return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE |
| 160 |
uint32_t VP8LSubPixels(uint32_t a, uint32_t b) { |
| 161 |
const uint32_t alpha_and_green = |
| 162 |
0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u); |
| 163 |
const uint32_t red_and_blue = |
| 164 |
0xff00ff00u + (a & 0x00ff00ffu) - (b & 0x00ff00ffu); |
| 165 |
return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#define GENERATE_PREDICTOR_ADD(PREDICTOR, PREDICTOR_ADD) \ |
| 177 |
static void PREDICTOR_ADD(const uint32_t* in, const uint32_t* upper, \ |
| 178 |
int num_pixels, uint32_t* out) { \ |
| 179 |
int x; \ |
| 180 |
assert(upper != NULL); \ |
| 181 |
for (x = 0; x < num_pixels; ++x) { \ |
| 182 |
const uint32_t pred = (PREDICTOR)(&out[x - 1], upper + x); \ |
| 183 |
out[x] = VP8LAddPixels(in[x], pred); \ |
| 184 |
} \ |
| 185 |
} |
| 186 |
|
| 187 |
#ifdef __cplusplus |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
#endif |