| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "src/utils/quant_levels_dec_utils.h" |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "src/utils/utils.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define FIX 16 // fix-point precision for averaging |
| 26 |
#define LFIX 2 // extra precision for look-up table |
| 27 |
#define LUT_SIZE ((1 << (8 + LFIX)) - 1) // look-up table size |
| 28 |
|
| 29 |
#if defined(USE_DITHERING) |
| 30 |
|
| 31 |
#define DFIX 4 // extra precision for ordered dithering |
| 32 |
#define DSIZE 4 // dithering size (must be a power of two) |
| 33 |
|
| 34 |
static const uint8_t kOrderedDither[DSIZE][DSIZE] = { |
| 35 |
{ 0, 8, 2, 10 }, |
| 36 |
{ 12, 4, 14, 6 }, |
| 37 |
{ 3, 11, 1, 9 }, |
| 38 |
{ 15, 7, 13, 5 } |
| 39 |
}; |
| 40 |
|
| 41 |
#else |
| 42 |
#define DFIX 0 |
| 43 |
#endif |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
int width_, height_; |
| 47 |
int stride_; |
| 48 |
int row_; |
| 49 |
uint8_t* src_; |
| 50 |
uint8_t* dst_; |
| 51 |
|
| 52 |
int radius_; |
| 53 |
int scale_; |
| 54 |
|
| 55 |
void* mem_; |
| 56 |
|
| 57 |
|
| 58 |
uint16_t* start_; |
| 59 |
uint16_t* cur_; |
| 60 |
uint16_t* end_; |
| 61 |
uint16_t* top_; |
| 62 |
uint16_t* average_; |
| 63 |
|
| 64 |
|
| 65 |
int num_levels_; |
| 66 |
int min_, max_; |
| 67 |
int min_level_dist_; |
| 68 |
|
| 69 |
int16_t* correction_; |
| 70 |
} SmoothParams; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define CLIP_8b_MASK (int)(~0U << (8 + DFIX)) |
| 75 |
static WEBP_INLINE uint8_t clip_8b(int v) { |
| 76 |
return (!(v & CLIP_8b_MASK)) ? (uint8_t)(v >> DFIX) : (v < 0) ? 0u : 255u; |
| 77 |
} |
| 78 |
#undef CLIP_8b_MASK |
| 79 |
|
| 80 |
|
| 81 |
static void VFilter(SmoothParams* const p) { |
| 82 |
const uint8_t* src = p->src_; |
| 83 |
const int w = p->width_; |
| 84 |
uint16_t* const cur = p->cur_; |
| 85 |
const uint16_t* const top = p->top_; |
| 86 |
uint16_t* const out = p->end_; |
| 87 |
uint16_t sum = 0; |
| 88 |
int x; |
| 89 |
|
| 90 |
for (x = 0; x < w; ++x) { |
| 91 |
uint16_t new_value; |
| 92 |
sum += src[x]; |
| 93 |
new_value = top[x] + sum; |
| 94 |
out[x] = new_value - cur[x]; |
| 95 |
cur[x] = new_value; |
| 96 |
} |
| 97 |
|
| 98 |
p->top_ = p->cur_; |
| 99 |
p->cur_ += w; |
| 100 |
if (p->cur_ == p->end_) p->cur_ = p->start_; |
| 101 |
|
| 102 |
|
| 103 |
if (p->row_ >= 0 && p->row_ < p->height_ - 1) { |
| 104 |
p->src_ += p->stride_; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static void HFilter(SmoothParams* const p) { |
| 111 |
const uint16_t* const in = p->end_; |
| 112 |
uint16_t* const out = p->average_; |
| 113 |
const uint32_t scale = p->scale_; |
| 114 |
const int w = p->width_; |
| 115 |
const int r = p->radius_; |
| 116 |
|
| 117 |
int x; |
| 118 |
for (x = 0; x <= r; ++x) { |
| 119 |
const uint16_t delta = in[x + r - 1] + in[r - x]; |
| 120 |
out[x] = (delta * scale) >> FIX; |
| 121 |
} |
| 122 |
for (; x < w - r; ++x) { |
| 123 |
const uint16_t delta = in[x + r] - in[x - r - 1]; |
| 124 |
out[x] = (delta * scale) >> FIX; |
| 125 |
} |
| 126 |
for (; x < w; ++x) { |
| 127 |
const uint16_t delta = |
| 128 |
2 * in[w - 1] - in[2 * w - 2 - r - x] - in[x - r - 1]; |
| 129 |
out[x] = (delta * scale) >> FIX; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
static void ApplyFilter(SmoothParams* const p) { |
| 135 |
const uint16_t* const average = p->average_; |
| 136 |
const int w = p->width_; |
| 137 |
const int16_t* const correction = p->correction_; |
| 138 |
#if defined(USE_DITHERING) |
| 139 |
const uint8_t* const dither = kOrderedDither[p->row_ % DSIZE]; |
| 140 |
#endif |
| 141 |
uint8_t* const dst = p->dst_; |
| 142 |
int x; |
| 143 |
for (x = 0; x < w; ++x) { |
| 144 |
const int v = dst[x]; |
| 145 |
if (v < p->max_ && v > p->min_) { |
| 146 |
const int c = (v << DFIX) + correction[average[x] - (v << LFIX)]; |
| 147 |
#if defined(USE_DITHERING) |
| 148 |
dst[x] = clip_8b(c + dither[x % DSIZE]); |
| 149 |
#else |
| 150 |
dst[x] = clip_8b(c); |
| 151 |
#endif |
| 152 |
} |
| 153 |
} |
| 154 |
p->dst_ += p->stride_; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void InitCorrectionLUT(int16_t* const lut, int min_dist) { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
const int threshold1 = min_dist << LFIX; |
| 168 |
const int threshold2 = (3 * threshold1) >> 2; |
| 169 |
const int max_threshold = threshold2 << DFIX; |
| 170 |
const int delta = threshold1 - threshold2; |
| 171 |
int i; |
| 172 |
for (i = 1; i <= LUT_SIZE; ++i) { |
| 173 |
int c = (i <= threshold2) ? (i << DFIX) |
| 174 |
: (i < threshold1) ? max_threshold * (threshold1 - i) / delta |
| 175 |
: 0; |
| 176 |
c >>= LFIX; |
| 177 |
lut[+i] = +c; |
| 178 |
lut[-i] = -c; |
| 179 |
} |
| 180 |
lut[0] = 0; |
| 181 |
} |
| 182 |
|
| 183 |
static void CountLevels(SmoothParams* const p) { |
| 184 |
int i, j, last_level; |
| 185 |
uint8_t used_levels[256] = { 0 }; |
| 186 |
const uint8_t* data = p->src_; |
| 187 |
p->min_ = 255; |
| 188 |
p->max_ = 0; |
| 189 |
for (j = 0; j < p->height_; ++j) { |
| 190 |
for (i = 0; i < p->width_; ++i) { |
| 191 |
const int v = data[i]; |
| 192 |
if (v < p->min_) p->min_ = v; |
| 193 |
if (v > p->max_) p->max_ = v; |
| 194 |
used_levels[v] = 1; |
| 195 |
} |
| 196 |
data += p->stride_; |
| 197 |
} |
| 198 |
|
| 199 |
p->min_level_dist_ = p->max_ - p->min_; |
| 200 |
last_level = -1; |
| 201 |
for (i = 0; i < 256; ++i) { |
| 202 |
if (used_levels[i]) { |
| 203 |
++p->num_levels_; |
| 204 |
if (last_level >= 0) { |
| 205 |
const int level_dist = i - last_level; |
| 206 |
if (level_dist < p->min_level_dist_) { |
| 207 |
p->min_level_dist_ = level_dist; |
| 208 |
} |
| 209 |
} |
| 210 |
last_level = i; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
static int InitParams(uint8_t* const data, int width, int height, int stride, |
| 217 |
int radius, SmoothParams* const p) { |
| 218 |
const int R = 2 * radius + 1; |
| 219 |
|
| 220 |
const size_t size_scratch_m = (R + 1) * width * sizeof(*p->start_); |
| 221 |
const size_t size_m = width * sizeof(*p->average_); |
| 222 |
const size_t size_lut = (1 + 2 * LUT_SIZE) * sizeof(*p->correction_); |
| 223 |
const size_t total_size = size_scratch_m + size_m + size_lut; |
| 224 |
uint8_t* mem = (uint8_t*)WebPSafeMalloc(1U, total_size); |
| 225 |
|
| 226 |
if (mem == NULL) return 0; |
| 227 |
p->mem_ = (void*)mem; |
| 228 |
|
| 229 |
p->start_ = (uint16_t*)mem; |
| 230 |
p->cur_ = p->start_; |
| 231 |
p->end_ = p->start_ + R * width; |
| 232 |
p->top_ = p->end_ - width; |
| 233 |
memset(p->top_, 0, width * sizeof(*p->top_)); |
| 234 |
mem += size_scratch_m; |
| 235 |
|
| 236 |
p->average_ = (uint16_t*)mem; |
| 237 |
mem += size_m; |
| 238 |
|
| 239 |
p->width_ = width; |
| 240 |
p->height_ = height; |
| 241 |
p->stride_ = stride; |
| 242 |
p->src_ = data; |
| 243 |
p->dst_ = data; |
| 244 |
p->radius_ = radius; |
| 245 |
p->scale_ = (1 << (FIX + LFIX)) / (R * R); |
| 246 |
p->row_ = -radius; |
| 247 |
|
| 248 |
|
| 249 |
CountLevels(p); |
| 250 |
|
| 251 |
|
| 252 |
p->correction_ = ((int16_t*)mem) + LUT_SIZE; |
| 253 |
InitCorrectionLUT(p->correction_, p->min_level_dist_); |
| 254 |
|
| 255 |
return 1; |
| 256 |
} |
| 257 |
|
| 258 |
static void CleanupParams(SmoothParams* const p) { |
| 259 |
WebPSafeFree(p->mem_); |
| 260 |
} |
| 261 |
|
| 262 |
int WebPDequantizeLevels(uint8_t* const data, int width, int height, int stride, |
| 263 |
int strength) { |
| 264 |
int radius = 4 * strength / 100; |
| 265 |
|
| 266 |
if (strength < 0 || strength > 100) return 0; |
| 267 |
if (data == NULL || width <= 0 || height <= 0) return 0; |
| 268 |
|
| 269 |
|
| 270 |
if (2 * radius + 1 > width) radius = (width - 1) >> 1; |
| 271 |
if (2 * radius + 1 > height) radius = (height - 1) >> 1; |
| 272 |
|
| 273 |
if (radius > 0) { |
| 274 |
SmoothParams p; |
| 275 |
memset(&p, 0, sizeof(p)); |
| 276 |
if (!InitParams(data, width, height, stride, radius, &p)) return 0; |
| 277 |
if (p.num_levels_ > 2) { |
| 278 |
for (; p.row_ < p.height_; ++p.row_) { |
| 279 |
VFilter(&p); |
| 280 |
|
| 281 |
|
| 282 |
if (p.row_ >= p.radius_) { |
| 283 |
HFilter(&p); |
| 284 |
ApplyFilter(&p); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
CleanupParams(&p); |
| 289 |
} |
| 290 |
return 1; |
| 291 |
} |