summaryrefslogtreecommitdiff
path: root/thirdparty/libwebp/src/enc/delta_palettization_enc.c
blob: a61c8e6c936ba120b8b91c56675d6829549a6e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
// Author: Mislav Bradac (mislavm@google.com)
//

#include "src/enc/delta_palettization_enc.h"

#ifdef WEBP_EXPERIMENTAL_FEATURES
#include "src/webp/types.h"
#include "src/dsp/lossless.h"

#define MK_COL(r, g, b) (((r) << 16) + ((g) << 8) + (b))

// Format allows palette up to 256 entries, but more palette entries produce
// bigger entropy. In the future it will probably be useful to add more entries
// that are far from the origin of the palette or choose remaining entries
// dynamically.
#define DELTA_PALETTE_SIZE 226

// Palette used for delta_palettization. Entries are roughly sorted by distance
// of their signed equivalents from the origin.
static const uint32_t kDeltaPalette[DELTA_PALETTE_SIZE] = {
  MK_COL(0u, 0u, 0u),
  MK_COL(255u, 255u, 255u),
  MK_COL(1u, 1u, 1u),
  MK_COL(254u, 254u, 254u),
  MK_COL(2u, 2u, 2u),
  MK_COL(4u, 4u, 4u),
  MK_COL(252u, 252u, 252u),
  MK_COL(250u, 0u, 0u),
  MK_COL(0u, 250u, 0u),
  MK_COL(0u, 0u, 250u),
  MK_COL(6u, 0u, 0u),
  MK_COL(0u, 6u, 0u),
  MK_COL(0u, 0u, 6u),
  MK_COL(0u, 0u, 248u),
  MK_COL(0u, 0u, 8u),
  MK_COL(0u, 248u, 0u),
  MK_COL(0u, 248u, 248u),
  MK_COL(0u, 248u, 8u),
  MK_COL(0u, 8u, 0u),
  MK_COL(0u, 8u, 248u),
  MK_COL(0u, 8u, 8u),
  MK_COL(8u, 8u, 8u),
  MK_COL(248u, 0u, 0u),
  MK_COL(248u, 0u, 248u),
  MK_COL(248u, 0u, 8u),
  MK_COL(248u, 248u, 0u),
  MK_COL(248u, 8u, 0u),
  MK_COL(8u, 0u, 0u),
  MK_COL(8u, 0u, 248u),
  MK_COL(8u, 0u, 8u),
  MK_COL(8u, 248u, 0u),
  MK_COL(8u, 8u, 0u),
  MK_COL(23u, 23u, 23u),
  MK_COL(13u, 13u, 13u),
  MK_COL(232u, 232u, 232u),
  MK_COL(244u, 244u, 244u),
  MK_COL(245u, 245u, 250u),
  MK_COL(50u, 50u, 50u),
  MK_COL(204u, 204u, 204u),
  MK_COL(236u, 236u, 236u),
  MK_COL(16u, 16u, 16u),
  MK_COL(240u, 16u, 16u),
  MK_COL(16u, 240u, 16u),
  MK_COL(240u, 240u, 16u),
  MK_COL(16u, 16u, 240u),
  MK_COL(240u, 16u, 240u),
  MK_COL(16u, 240u, 240u),
  MK_COL(240u, 240u, 240u),
  MK_COL(0u, 0u, 232u),
  MK_COL(0u, 232u, 0u),
  MK_COL(232u, 0u, 0u),
  MK_COL(0u, 0u, 24u),
  MK_COL(0u, 24u, 0u),
  MK_COL(24u, 0u, 0u),
  MK_COL(32u, 32u, 32u),
  MK_COL(224u, 32u, 32u),
  MK_COL(32u, 224u, 32u),
  MK_COL(224u, 224u, 32u),
  MK_COL(32u, 32u, 224u),
  MK_COL(224u, 32u, 224u),
  MK_COL(32u, 224u, 224u),
  MK_COL(224u, 224u, 224u),
  MK_COL(0u, 0u, 176u),
  MK_COL(0u, 0u, 80u),
  MK_COL(0u, 176u, 0u),
  MK_COL(0u, 176u, 176u),
  MK_COL(0u, 176u, 80u),
  MK_COL(0u, 80u, 0u),
  MK_COL(0u, 80u, 176u),
  MK_COL(0u, 80u, 80u),
  MK_COL(176u, 0u, 0u),
  MK_COL(176u, 0u, 176u),
  MK_COL(176u, 0u, 80u),
  MK_COL(176u, 176u, 0u),
  MK_COL(176u, 80u, 0u),
  MK_COL(80u, 0u, 0u),
  MK_COL(80u, 0u, 176u),
  MK_COL(80u, 0u, 80u),
  MK_COL(80u, 176u, 0u),
  MK_COL(80u, 80u, 0u),
  MK_COL(0u, 0u, 152u),
  MK_COL(0u, 0u, 104u),
  MK_COL(0u, 152u, 0u),
  MK_COL(0u, 152u, 152u),
  MK_COL(0u, 152u, 104u),
  MK_COL(0u, 104u, 0u),
  MK_COL(0u, 104u, 152u),
  MK_COL(0u, 104u, 104u),
  MK_COL(152u, 0u, 0u),
  MK_COL(152u, 0u, 152u),
  MK_COL(152u, 0u, 104u),
  MK_COL(152u, 152u, 0u),
  MK_COL(152u, 104u, 0u),
  MK_COL(104u, 0u, 0u),
  MK_COL(104u, 0u, 152u),
  MK_COL(104u, 0u, 104u),
  MK_COL(104u, 152u, 0u),
  MK_COL(104u, 104u, 0u),
  MK_COL(216u, 216u, 216u),
  MK_COL(216u, 216u, 40u),
  MK_COL(216u, 216u, 176u),
  MK_COL(216u, 216u, 80u),
  MK_COL(216u, 40u, 216u),
  MK_COL(216u, 40u, 40u),
  MK_COL(216u, 40u, 176u),
  MK_COL(216u, 40u, 80u),
  MK_COL(216u, 176u, 216u),
  MK_COL(216u, 176u, 40u),
  MK_COL(216u, 176u, 176u),
  MK_COL(216u, 176u, 80u),
  MK_COL(216u, 80u, 216u),
  MK_COL(216u, 80u, 40u),
  MK_COL(216u, 80u, 176u),
  MK_COL(216u, 80u, 80u),
  MK_COL(40u, 216u, 216u),
  MK_COL(40u, 216u, 40u),
  MK_COL(40u, 216u, 176u),
  MK_COL(40u, 216u, 80u),
  MK_COL(40u, 40u, 216u),
  MK_COL(40u, 40u, 40u),
  MK_COL(40u, 40u, 176u),
  MK_COL(40u, 40u, 80u),
  MK_COL(40u, 176u, 216u),
  MK_COL(40u, 176u, 40u),
  MK_COL(40u, 176u, 176u),
  MK_COL(40u, 176u, 80u),
  MK_COL(40u, 80u, 216u),
  MK_COL(40u, 80u, 40u),
  MK_COL(40u, 80u, 176u),
  MK_COL(40u, 80u, 80u),
  MK_COL(80u, 216u, 216u),
  MK_COL(80u, 216u, 40u),
  MK_COL(80u, 216u, 176u),
  MK_COL(80u, 216u, 80u),
  MK_COL(80u, 40u, 216u),
  MK_COL(80u, 40u, 40u),
  MK_COL(80u, 40u, 176u),
  MK_COL(80u, 40u, 80u),
  MK_COL(80u, 176u, 216u),
  MK_COL(80u, 176u, 40u),
  MK_COL(80u, 176u, 176u),
  MK_COL(80u, 176u, 80u),
  MK_COL(80u, 80u, 216u),
  MK_COL(80u, 80u, 40u),
  MK_COL(80u, 80u, 176u),
  MK_COL(80u, 80u, 80u),
  MK_COL(0u, 0u, 192u),
  MK_COL(0u, 0u, 64u),
  MK_COL(0u, 0u, 128u),
  MK_COL(0u, 192u, 0u),
  MK_COL(0u, 192u, 192u),
  MK_COL(0u, 192u, 64u),
  MK_COL(0u, 192u, 128u),
  MK_COL(0u, 64u, 0u),
  MK_COL(0u, 64u, 192u),
  MK_COL(0u, 64u, 64u),
  MK_COL(0u, 64u, 128u),
  MK_COL(0u, 128u, 0u),
  MK_COL(0u, 128u, 192u),
  MK_COL(0u, 128u, 64u),
  MK_COL(0u, 128u, 128u),
  MK_COL(176u, 216u, 216u),
  MK_COL(176u, 216u, 40u),
  MK_COL(176u, 216u, 176u),
  MK_COL(176u, 216u, 80u),
  MK_COL(176u, 40u, 216u),
  MK_COL(176u, 40u, 40u),
  MK_COL(176u, 40u, 176u),
  MK_COL(176u, 40u, 80u),
  MK_COL(176u, 176u, 216u),
  MK_COL(176u, 176u, 40u),
  MK_COL(176u, 176u, 176u),
  MK_COL(176u, 176u, 80u),
  MK_COL(176u, 80u, 216u),
  MK_COL(176u, 80u, 40u),
  MK_COL(176u, 80u, 176u),
  MK_COL(176u, 80u, 80u),
  MK_COL(192u, 0u, 0u),
  MK_COL(192u, 0u, 192u),
  MK_COL(192u, 0u, 64u),
  MK_COL(192u, 0u, 128u),
  MK_COL(192u, 192u, 0u),
  MK_COL(192u, 192u, 192u),
  MK_COL(192u, 192u, 64u),
  MK_COL(192u, 192u, 128u),
  MK_COL(192u, 64u, 0u),
  MK_COL(192u, 64u, 192u),
  MK_COL(192u, 64u, 64u),
  MK_COL(192u, 64u, 128u),
  MK_COL(192u, 128u, 0u),
  MK_COL(192u, 128u, 192u),
  MK_COL(192u, 128u, 64u),
  MK_COL(192u, 128u, 128u),
  MK_COL(64u, 0u, 0u),
  MK_COL(64u, 0u, 192u),
  MK_COL(64u, 0u, 64u),
  MK_COL(64u, 0u, 128u),
  MK_COL(64u, 192u, 0u),
  MK_COL(64u, 192u, 192u),
  MK_COL(64u, 192u, 64u),
  MK_COL(64u, 192u, 128u),
  MK_COL(64u, 64u, 0u),
  MK_COL(64u, 64u, 192u),
  MK_COL(64u, 64u, 64u),
  MK_COL(64u, 64u, 128u),
  MK_COL(64u, 128u, 0u),
  MK_COL(64u, 128u, 192u),
  MK_COL(64u, 128u, 64u),
  MK_COL(64u, 128u, 128u),
  MK_COL(128u, 0u, 0u),
  MK_COL(128u, 0u, 192u),
  MK_COL(128u, 0u, 64u),
  MK_COL(128u, 0u, 128u),
  MK_COL(128u, 192u, 0u),
  MK_COL(128u, 192u, 192u),
  MK_COL(128u, 192u, 64u),
  MK_COL(128u, 192u, 128u),
  MK_COL(128u, 64u, 0u),
  MK_COL(128u, 64u, 192u),
  MK_COL(128u, 64u, 64u),
  MK_COL(128u, 64u, 128u),
  MK_COL(128u, 128u, 0u),
  MK_COL(128u, 128u, 192u),
  MK_COL(128u, 128u, 64u),
  MK_COL(128u, 128u, 128u),
};

#undef MK_COL

//------------------------------------------------------------------------------
// TODO(skal): move the functions to dsp/lossless.c when the correct
// granularity is found. For now, we'll just copy-paste some useful bits
// here instead.

// In-place sum of each component with mod 256.
static WEBP_INLINE void AddPixelsEq(uint32_t* a, uint32_t b) {
  const uint32_t alpha_and_green = (*a & 0xff00ff00u) + (b & 0xff00ff00u);
  const uint32_t red_and_blue = (*a & 0x00ff00ffu) + (b & 0x00ff00ffu);
  *a = (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
}

static WEBP_INLINE uint32_t Clip255(uint32_t a) {
  if (a < 256) {
    return a;
  }
  // return 0, when a is a negative integer.
  // return 255, when a is positive.
  return ~a >> 24;
}

// Delta palettization functions.
static WEBP_INLINE int Square(int x) {
  return x * x;
}

static WEBP_INLINE uint32_t Intensity(uint32_t a) {
  return
      30 * ((a >> 16) & 0xff) +
      59 * ((a >>  8) & 0xff) +
      11 * ((a >>  0) & 0xff);
}

static uint32_t CalcDist(uint32_t predicted_value, uint32_t actual_value,
                         uint32_t palette_entry) {
  int i;
  uint32_t distance = 0;
  AddPixelsEq(&predicted_value, palette_entry);
  for (i = 0; i < 32; i += 8) {
    const int32_t av = (actual_value >> i) & 0xff;
    const int32_t pv = (predicted_value >> i) & 0xff;
    distance += Square(pv - av);
  }
  // We sum square of intensity difference with factor 10, but because Intensity
  // returns 100 times real intensity we need to multiply differences of colors
  // by 1000.
  distance *= 1000u;
  distance += Square(Intensity(predicted_value)
                     - Intensity(actual_value));
  return distance;
}

static uint32_t Predict(int x, int y, uint32_t* image) {
  const uint32_t t = (y == 0) ? ARGB_BLACK : image[x];
  const uint32_t l = (x == 0) ? ARGB_BLACK : image[x - 1];
  const uint32_t p =
      (((((t >> 24) & 0xff) + ((l >> 24) & 0xff)) / 2) << 24) +
      (((((t >> 16) & 0xff) + ((l >> 16) & 0xff)) / 2) << 16) +
      (((((t >>  8) & 0xff) + ((l >>  8) & 0xff)) / 2) <<  8) +
      (((((t >>  0) & 0xff) + ((l >>  0) & 0xff)) / 2) <<  0);
  if (x == 0 && y == 0) return ARGB_BLACK;
  if (x == 0) return t;
  if (y == 0) return l;
  return p;
}

static WEBP_INLINE int AddSubtractComponentFullWithCoefficient(
    int a, int b, int c) {
  return Clip255(a + ((b - c) >> 2));
}

static WEBP_INLINE uint32_t ClampedAddSubtractFullWithCoefficient(
    uint32_t c0, uint32_t c1, uint32_t c2) {
  const int a = AddSubtractComponentFullWithCoefficient(
      c0 >> 24, c1 >> 24, c2 >> 24);
  const int r = AddSubtractComponentFullWithCoefficient((c0 >> 16) & 0xff,
                                                       (c1 >> 16) & 0xff,
                                                       (c2 >> 16) & 0xff);
  const int g = AddSubtractComponentFullWithCoefficient((c0 >> 8) & 0xff,
                                                       (c1 >> 8) & 0xff,
                                                       (c2 >> 8) & 0xff);
  const int b = AddSubtractComponentFullWithCoefficient(
      c0 & 0xff, c1 & 0xff, c2 & 0xff);
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
}

//------------------------------------------------------------------------------

// Find palette entry with minimum error from difference of actual pixel value
// and predicted pixel value. Propagate error of pixel to its top and left pixel
// in src array. Write predicted_value + palette_entry to new_image. Return
// index of best palette entry.
static int FindBestPaletteEntry(uint32_t src, uint32_t predicted_value,
                                const uint32_t palette[], int palette_size) {
  int i;
  int idx = 0;
  uint32_t best_distance = CalcDist(predicted_value, src, palette[0]);
  for (i = 1; i < palette_size; ++i) {
    const uint32_t distance = CalcDist(predicted_value, src, palette[i]);
    if (distance < best_distance) {
      best_distance = distance;
      idx = i;
    }
  }
  return idx;
}

static void ApplyBestPaletteEntry(int x, int y,
                                  uint32_t new_value, uint32_t palette_value,
                                  uint32_t* src, int src_stride,
                                  uint32_t* new_image) {
  AddPixelsEq(&new_value, palette_value);
  if (x > 0) {
    src[x - 1] = ClampedAddSubtractFullWithCoefficient(src[x - 1],
                                                       new_value, src[x]);
  }
  if (y > 0) {
    src[x - src_stride] =
        ClampedAddSubtractFullWithCoefficient(src[x - src_stride],
                                              new_value, src[x]);
  }
  new_image[x] = new_value;
}

//------------------------------------------------------------------------------
// Main entry point

static WebPEncodingError ApplyDeltaPalette(uint32_t* src, uint32_t* dst,
                                           uint32_t src_stride,
                                           uint32_t dst_stride,
                                           const uint32_t* palette,
                                           int palette_size,
                                           int width, int height,
                                           int num_passes) {
  int x, y;
  WebPEncodingError err = VP8_ENC_OK;
  uint32_t* new_image = (uint32_t*)WebPSafeMalloc(width, sizeof(*new_image));
  uint8_t* const tmp_row = (uint8_t*)WebPSafeMalloc(width, sizeof(*tmp_row));
  if (new_image == NULL || tmp_row == NULL) {
    err = VP8_ENC_ERROR_OUT_OF_MEMORY;
    goto Error;
  }

  while (num_passes--) {
    uint32_t* cur_src = src;
    uint32_t* cur_dst = dst;
    for (y = 0; y < height; ++y) {
      for (x = 0; x < width; ++x) {
        const uint32_t predicted_value = Predict(x, y, new_image);
        tmp_row[x] = FindBestPaletteEntry(cur_src[x], predicted_value,
                                          palette, palette_size);
        ApplyBestPaletteEntry(x, y, predicted_value, palette[tmp_row[x]],
                              cur_src, src_stride, new_image);
      }
      for (x = 0; x < width; ++x) {
        cur_dst[x] = palette[tmp_row[x]];
      }
      cur_src += src_stride;
      cur_dst += dst_stride;
    }
  }
 Error:
  WebPSafeFree(new_image);
  WebPSafeFree(tmp_row);
  return err;
}

// replaces enc->argb_ by a palettizable approximation of it,
// and generates optimal enc->palette_[]
WebPEncodingError WebPSearchOptimalDeltaPalette(VP8LEncoder* const enc) {
  const WebPPicture* const pic = enc->pic_;
  uint32_t* src = pic->argb;
  uint32_t* dst = enc->argb_;
  const int width = pic->width;
  const int height = pic->height;

  WebPEncodingError err = VP8_ENC_OK;
  memcpy(enc->palette_, kDeltaPalette, sizeof(kDeltaPalette));
  enc->palette_[DELTA_PALETTE_SIZE - 1] = src[0] - 0xff000000u;
  enc->palette_size_ = DELTA_PALETTE_SIZE;
  err = ApplyDeltaPalette(src, dst, pic->argb_stride, enc->current_width_,
                          enc->palette_, enc->palette_size_,
                          width, height, 2);
  if (err != VP8_ENC_OK) goto Error;

 Error:
  return err;
}

#else  // !WEBP_EXPERIMENTAL_FEATURES

WebPEncodingError WebPSearchOptimalDeltaPalette(VP8LEncoder* const enc) {
  (void)enc;
  return VP8_ENC_ERROR_INVALID_CONFIGURATION;
}

#endif  // WEBP_EXPERIMENTAL_FEATURES