Coverage Report

Created: 2025-07-23 06:34

/src/libwebp/src/enc/vp8l_enc.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// main entry for the lossless encoder.
11
//
12
// Author: Vikas Arora (vikaas.arora@gmail.com)
13
//
14
15
#include <assert.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
#include "src/dsp/lossless.h"
20
#include "src/dsp/lossless_common.h"
21
#include "src/enc/backward_references_enc.h"
22
#include "src/enc/histogram_enc.h"
23
#include "src/enc/vp8i_enc.h"
24
#include "src/enc/vp8li_enc.h"
25
#include "src/utils/bit_writer_utils.h"
26
#include "src/utils/huffman_encode_utils.h"
27
#include "src/utils/palette.h"
28
#include "src/utils/thread_utils.h"
29
#include "src/utils/utils.h"
30
#include "src/webp/encode.h"
31
#include "src/webp/format_constants.h"
32
#include "src/webp/types.h"
33
34
// Maximum number of histogram images (sub-blocks).
35
0
#define MAX_HUFF_IMAGE_SIZE       2600
36
0
#define MAX_HUFFMAN_BITS (MIN_HUFFMAN_BITS + (1 << NUM_HUFFMAN_BITS) - 1)
37
// Empirical value for which it becomes too computationally expensive to
38
// compute the best predictor image.
39
0
#define MAX_PREDICTOR_IMAGE_SIZE (1 << 14)
40
41
// -----------------------------------------------------------------------------
42
// Palette
43
44
// These five modes are evaluated and their respective entropy is computed.
45
typedef enum {
46
  kDirect = 0,
47
  kSpatial = 1,
48
  kSubGreen = 2,
49
  kSpatialSubGreen = 3,
50
  kPalette = 4,
51
  kPaletteAndSpatial = 5,
52
  kNumEntropyIx = 6
53
} EntropyIx;
54
55
typedef enum {
56
  kHistoAlpha = 0,
57
  kHistoAlphaPred,
58
  kHistoGreen,
59
  kHistoGreenPred,
60
  kHistoRed,
61
  kHistoRedPred,
62
  kHistoBlue,
63
  kHistoBluePred,
64
  kHistoRedSubGreen,
65
  kHistoRedPredSubGreen,
66
  kHistoBlueSubGreen,
67
  kHistoBluePredSubGreen,
68
  kHistoPalette,
69
  kHistoTotal  // Must be last.
70
} HistoIx;
71
72
73
0
#define NUM_BUCKETS 256
74
75
typedef uint32_t HistogramBuckets[NUM_BUCKETS];
76
77
// Keeping track of histograms, indexed by HistoIx.
78
// Ideally, this would just be a struct with meaningful fields, but the
79
// calculation of `entropy_comp` uses the index. One refactoring at a time :)
80
typedef struct {
81
  HistogramBuckets category[kHistoTotal];
82
} Histograms;
83
84
static void AddSingleSubGreen(uint32_t p,
85
0
                              HistogramBuckets r, HistogramBuckets b) {
86
0
  const int green = (int)p >> 8;  // The upper bits are masked away later.
87
0
  ++r[(((int)p >> 16) - green) & 0xff];
88
0
  ++b[(((int)p >>  0) - green) & 0xff];
89
0
}
90
91
static void AddSingle(uint32_t p,
92
                      HistogramBuckets a, HistogramBuckets r,
93
0
                      HistogramBuckets g, HistogramBuckets b) {
94
0
  ++a[(p >> 24) & 0xff];
95
0
  ++r[(p >> 16) & 0xff];
96
0
  ++g[(p >>  8) & 0xff];
97
0
  ++b[(p >>  0) & 0xff];
98
0
}
99
100
0
static WEBP_INLINE uint8_t HashPix(uint32_t pix) {
101
  // Note that masking with 0xffffffffu is for preventing an
102
  // 'unsigned int overflow' warning. Doesn't impact the compiled code.
103
0
  return ((((uint64_t)pix + (pix >> 19)) * 0x39c5fba7ull) & 0xffffffffu) >> 24;
104
0
}
105
106
static int AnalyzeEntropy(const uint32_t* argb,
107
                          int width, int height, int argb_stride,
108
                          int use_palette,
109
                          int palette_size, int transform_bits,
110
                          EntropyIx* const min_entropy_ix,
111
0
                          int* const red_and_blue_always_zero) {
112
0
  Histograms* histo;
113
114
0
  if (use_palette && palette_size <= 16) {
115
    // In the case of small palettes, we pack 2, 4 or 8 pixels together. In
116
    // practice, small palettes are better than any other transform.
117
0
    *min_entropy_ix = kPalette;
118
0
    *red_and_blue_always_zero = 1;
119
0
    return 1;
120
0
  }
121
122
0
  histo = (Histograms*)WebPSafeCalloc(1, sizeof(*histo));
123
0
  if (histo != NULL) {
124
0
    int i, x, y;
125
0
    const uint32_t* prev_row = NULL;
126
0
    const uint32_t* curr_row = argb;
127
0
    uint32_t pix_prev = argb[0];  // Skip the first pixel.
128
0
    for (y = 0; y < height; ++y) {
129
0
      for (x = 0; x < width; ++x) {
130
0
        const uint32_t pix = curr_row[x];
131
0
        const uint32_t pix_diff = VP8LSubPixels(pix, pix_prev);
132
0
        pix_prev = pix;
133
0
        if ((pix_diff == 0) || (prev_row != NULL && pix == prev_row[x])) {
134
0
          continue;
135
0
        }
136
0
        AddSingle(pix,
137
0
                  histo->category[kHistoAlpha],
138
0
                  histo->category[kHistoRed],
139
0
                  histo->category[kHistoGreen],
140
0
                  histo->category[kHistoBlue]);
141
0
        AddSingle(pix_diff,
142
0
                  histo->category[kHistoAlphaPred],
143
0
                  histo->category[kHistoRedPred],
144
0
                  histo->category[kHistoGreenPred],
145
0
                  histo->category[kHistoBluePred]);
146
0
        AddSingleSubGreen(pix,
147
0
                          histo->category[kHistoRedSubGreen],
148
0
                          histo->category[kHistoBlueSubGreen]);
149
0
        AddSingleSubGreen(pix_diff,
150
0
                          histo->category[kHistoRedPredSubGreen],
151
0
                          histo->category[kHistoBluePredSubGreen]);
152
0
        {
153
          // Approximate the palette by the entropy of the multiplicative hash.
154
0
          const uint8_t hash = HashPix(pix);
155
0
          ++histo->category[kHistoPalette][hash];
156
0
        }
157
0
      }
158
0
      prev_row = curr_row;
159
0
      curr_row += argb_stride;
160
0
    }
161
0
    {
162
0
      uint64_t entropy_comp[kHistoTotal];
163
0
      uint64_t entropy[kNumEntropyIx];
164
0
      int k;
165
0
      int last_mode_to_analyze = use_palette ? kPalette : kSpatialSubGreen;
166
0
      int j;
167
      // Let's add one zero to the predicted histograms. The zeros are removed
168
      // too efficiently by the pix_diff == 0 comparison, at least one of the
169
      // zeros is likely to exist.
170
0
      ++histo->category[kHistoRedPredSubGreen][0];
171
0
      ++histo->category[kHistoBluePredSubGreen][0];
172
0
      ++histo->category[kHistoRedPred][0];
173
0
      ++histo->category[kHistoGreenPred][0];
174
0
      ++histo->category[kHistoBluePred][0];
175
0
      ++histo->category[kHistoAlphaPred][0];
176
177
0
      for (j = 0; j < kHistoTotal; ++j) {
178
0
        entropy_comp[j] = VP8LBitsEntropy(histo->category[j], NUM_BUCKETS);
179
0
      }
180
0
      entropy[kDirect] = entropy_comp[kHistoAlpha] +
181
0
          entropy_comp[kHistoRed] +
182
0
          entropy_comp[kHistoGreen] +
183
0
          entropy_comp[kHistoBlue];
184
0
      entropy[kSpatial] = entropy_comp[kHistoAlphaPred] +
185
0
          entropy_comp[kHistoRedPred] +
186
0
          entropy_comp[kHistoGreenPred] +
187
0
          entropy_comp[kHistoBluePred];
188
0
      entropy[kSubGreen] = entropy_comp[kHistoAlpha] +
189
0
          entropy_comp[kHistoRedSubGreen] +
190
0
          entropy_comp[kHistoGreen] +
191
0
          entropy_comp[kHistoBlueSubGreen];
192
0
      entropy[kSpatialSubGreen] = entropy_comp[kHistoAlphaPred] +
193
0
          entropy_comp[kHistoRedPredSubGreen] +
194
0
          entropy_comp[kHistoGreenPred] +
195
0
          entropy_comp[kHistoBluePredSubGreen];
196
0
      entropy[kPalette] = entropy_comp[kHistoPalette];
197
198
      // When including transforms, there is an overhead in bits from
199
      // storing them. This overhead is small but matters for small images.
200
      // For spatial, there are 14 transformations.
201
0
      entropy[kSpatial] += (uint64_t)VP8LSubSampleSize(width, transform_bits) *
202
0
                           VP8LSubSampleSize(height, transform_bits) *
203
0
                           VP8LFastLog2(14);
204
      // For color transforms: 24 as only 3 channels are considered in a
205
      // ColorTransformElement.
206
0
      entropy[kSpatialSubGreen] +=
207
0
          (uint64_t)VP8LSubSampleSize(width, transform_bits) *
208
0
          VP8LSubSampleSize(height, transform_bits) * VP8LFastLog2(24);
209
      // For palettes, add the cost of storing the palette.
210
      // We empirically estimate the cost of a compressed entry as 8 bits.
211
      // The palette is differential-coded when compressed hence a much
212
      // lower cost than sizeof(uint32_t)*8.
213
0
      entropy[kPalette] += (palette_size * 8ull) << LOG_2_PRECISION_BITS;
214
215
0
      *min_entropy_ix = kDirect;
216
0
      for (k = kDirect + 1; k <= last_mode_to_analyze; ++k) {
217
0
        if (entropy[*min_entropy_ix] > entropy[k]) {
218
0
          *min_entropy_ix = (EntropyIx)k;
219
0
        }
220
0
      }
221
0
      assert((int)*min_entropy_ix <= last_mode_to_analyze);
222
0
      *red_and_blue_always_zero = 1;
223
      // Let's check if the histogram of the chosen entropy mode has
224
      // non-zero red and blue values. If all are zero, we can later skip
225
      // the cross color optimization.
226
0
      {
227
0
        static const uint8_t kHistoPairs[5][2] = {
228
0
          { kHistoRed, kHistoBlue },
229
0
          { kHistoRedPred, kHistoBluePred },
230
0
          { kHistoRedSubGreen, kHistoBlueSubGreen },
231
0
          { kHistoRedPredSubGreen, kHistoBluePredSubGreen },
232
0
          { kHistoRed, kHistoBlue }
233
0
        };
234
0
        const HistogramBuckets* const red_histo =
235
0
            &histo->category[kHistoPairs[*min_entropy_ix][0]];
236
0
        const HistogramBuckets* const blue_histo =
237
0
            &histo->category[kHistoPairs[*min_entropy_ix][1]];
238
0
        for (i = 1; i < NUM_BUCKETS; ++i) {
239
0
          if (((*red_histo)[i] | (*blue_histo)[i]) != 0) {
240
0
            *red_and_blue_always_zero = 0;
241
0
            break;
242
0
          }
243
0
        }
244
0
      }
245
0
    }
246
0
    WebPSafeFree(histo);
247
0
    return 1;
248
0
  } else {
249
0
    return 0;
250
0
  }
251
0
}
252
253
// Clamp histogram and transform bits.
254
static int ClampBits(int width, int height, int bits, int min_bits,
255
0
                     int max_bits, int image_size_max) {
256
0
  int image_size;
257
0
  bits = (bits < min_bits) ? min_bits : (bits > max_bits) ? max_bits : bits;
258
0
  image_size = VP8LSubSampleSize(width, bits) * VP8LSubSampleSize(height, bits);
259
0
  while (bits < max_bits && image_size > image_size_max) {
260
0
    ++bits;
261
0
    image_size =
262
0
        VP8LSubSampleSize(width, bits) * VP8LSubSampleSize(height, bits);
263
0
  }
264
  // In case the bits reduce the image too much, choose the smallest value
265
  // setting the histogram image size to 1.
266
0
  while (bits > min_bits && image_size == 1) {
267
0
    image_size = VP8LSubSampleSize(width, bits - 1) *
268
0
                 VP8LSubSampleSize(height, bits - 1);
269
0
    if (image_size != 1) break;
270
0
    --bits;
271
0
  }
272
0
  return bits;
273
0
}
274
275
0
static int GetHistoBits(int method, int use_palette, int width, int height) {
276
  // Make tile size a function of encoding method (Range: 0 to 6).
277
0
  const int histo_bits = (use_palette ? 9 : 7) - method;
278
0
  return ClampBits(width, height, histo_bits, MIN_HUFFMAN_BITS,
279
0
                   MAX_HUFFMAN_BITS, MAX_HUFF_IMAGE_SIZE);
280
0
}
281
282
0
static int GetTransformBits(int method, int histo_bits) {
283
0
  const int max_transform_bits = (method < 4) ? 6 : (method > 4) ? 4 : 5;
284
0
  const int res =
285
0
      (histo_bits > max_transform_bits) ? max_transform_bits : histo_bits;
286
0
  assert(res <= MAX_TRANSFORM_BITS);
287
0
  return res;
288
0
}
289
290
// Set of parameters to be used in each iteration of the cruncher.
291
#define CRUNCH_SUBCONFIGS_MAX 2
292
typedef struct {
293
  int lz77;
294
  int do_no_cache;
295
} CrunchSubConfig;
296
typedef struct {
297
  int entropy_idx;
298
  PaletteSorting palette_sorting_type;
299
  CrunchSubConfig sub_configs[CRUNCH_SUBCONFIGS_MAX];
300
  int sub_configs_size;
301
} CrunchConfig;
302
303
// +2 because we add a palette sorting configuration for kPalette and
304
// kPaletteAndSpatial.
305
#define CRUNCH_CONFIGS_MAX (kNumEntropyIx + 2 * kPaletteSortingNum)
306
307
static int EncoderAnalyze(VP8LEncoder* const enc,
308
                          CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX],
309
                          int* const crunch_configs_size,
310
0
                          int* const red_and_blue_always_zero) {
311
0
  const WebPPicture* const pic = enc->pic;
312
0
  const int width = pic->width;
313
0
  const int height = pic->height;
314
0
  const WebPConfig* const config = enc->config;
315
0
  const int method = config->method;
316
0
  const int low_effort = (config->method == 0);
317
0
  int i;
318
0
  int use_palette, transform_bits;
319
0
  int n_lz77s;
320
  // If set to 0, analyze the cache with the computed cache value. If 1, also
321
  // analyze with no-cache.
322
0
  int do_no_cache = 0;
323
0
  assert(pic != NULL && pic->argb != NULL);
324
325
  // Check whether a palette is possible.
326
0
  enc->palette_size = GetColorPalette(pic, enc->palette_sorted);
327
0
  use_palette = (enc->palette_size <= MAX_PALETTE_SIZE);
328
0
  if (!use_palette) {
329
0
    enc->palette_size = 0;
330
0
  }
331
332
  // Empirical bit sizes.
333
0
  enc->histo_bits = GetHistoBits(method, use_palette,
334
0
                                 pic->width, pic->height);
335
0
  transform_bits = GetTransformBits(method, enc->histo_bits);
336
0
  enc->predictor_transform_bits = transform_bits;
337
0
  enc->cross_color_transform_bits = transform_bits;
338
339
0
  if (low_effort) {
340
    // AnalyzeEntropy is somewhat slow.
341
0
    crunch_configs[0].entropy_idx = use_palette ? kPalette : kSpatialSubGreen;
342
0
    crunch_configs[0].palette_sorting_type =
343
0
        use_palette ? kSortedDefault : kUnusedPalette;
344
0
    n_lz77s = 1;
345
0
    *crunch_configs_size = 1;
346
0
  } else {
347
0
    EntropyIx min_entropy_ix;
348
    // Try out multiple LZ77 on images with few colors.
349
0
    n_lz77s = (enc->palette_size > 0 && enc->palette_size <= 16) ? 2 : 1;
350
0
    if (!AnalyzeEntropy(pic->argb, width, height, pic->argb_stride, use_palette,
351
0
                        enc->palette_size, transform_bits, &min_entropy_ix,
352
0
                        red_and_blue_always_zero)) {
353
0
      return 0;
354
0
    }
355
0
    if (method == 6 && config->quality == 100) {
356
0
      do_no_cache = 1;
357
      // Go brute force on all transforms.
358
0
      *crunch_configs_size = 0;
359
0
      for (i = 0; i < kNumEntropyIx; ++i) {
360
        // We can only apply kPalette or kPaletteAndSpatial if we can indeed use
361
        // a palette.
362
0
        if ((i != kPalette && i != kPaletteAndSpatial) || use_palette) {
363
0
          assert(*crunch_configs_size < CRUNCH_CONFIGS_MAX);
364
0
          if (use_palette && (i == kPalette || i == kPaletteAndSpatial)) {
365
0
            int sorting_method;
366
0
            for (sorting_method = 0; sorting_method < kPaletteSortingNum;
367
0
                 ++sorting_method) {
368
0
              const PaletteSorting typed_sorting_method =
369
0
                  (PaletteSorting)sorting_method;
370
              // TODO(vrabaud) kSortedDefault should be tested. It is omitted
371
              // for now for backward compatibility.
372
0
              if (typed_sorting_method == kUnusedPalette ||
373
0
                  typed_sorting_method == kSortedDefault) {
374
0
                continue;
375
0
              }
376
0
              crunch_configs[(*crunch_configs_size)].entropy_idx = i;
377
0
              crunch_configs[(*crunch_configs_size)].palette_sorting_type =
378
0
                  typed_sorting_method;
379
0
              ++*crunch_configs_size;
380
0
            }
381
0
          } else {
382
0
            crunch_configs[(*crunch_configs_size)].entropy_idx = i;
383
0
            crunch_configs[(*crunch_configs_size)].palette_sorting_type =
384
0
                kUnusedPalette;
385
0
            ++*crunch_configs_size;
386
0
          }
387
0
        }
388
0
      }
389
0
    } else {
390
      // Only choose the guessed best transform.
391
0
      *crunch_configs_size = 1;
392
0
      crunch_configs[0].entropy_idx = min_entropy_ix;
393
0
      crunch_configs[0].palette_sorting_type =
394
0
          use_palette ? kMinimizeDelta : kUnusedPalette;
395
0
      if (config->quality >= 75 && method == 5) {
396
        // Test with and without color cache.
397
0
        do_no_cache = 1;
398
        // If we have a palette, also check in combination with spatial.
399
0
        if (min_entropy_ix == kPalette) {
400
0
          *crunch_configs_size = 2;
401
0
          crunch_configs[1].entropy_idx = kPaletteAndSpatial;
402
0
          crunch_configs[1].palette_sorting_type = kMinimizeDelta;
403
0
        }
404
0
      }
405
0
    }
406
0
  }
407
  // Fill in the different LZ77s.
408
0
  assert(n_lz77s <= CRUNCH_SUBCONFIGS_MAX);
409
0
  for (i = 0; i < *crunch_configs_size; ++i) {
410
0
    int j;
411
0
    for (j = 0; j < n_lz77s; ++j) {
412
0
      assert(j < CRUNCH_SUBCONFIGS_MAX);
413
0
      crunch_configs[i].sub_configs[j].lz77 =
414
0
          (j == 0) ? kLZ77Standard | kLZ77RLE : kLZ77Box;
415
0
      crunch_configs[i].sub_configs[j].do_no_cache = do_no_cache;
416
0
    }
417
0
    crunch_configs[i].sub_configs_size = n_lz77s;
418
0
  }
419
0
  return 1;
420
0
}
421
422
0
static int EncoderInit(VP8LEncoder* const enc) {
423
0
  const WebPPicture* const pic = enc->pic;
424
0
  const int width = pic->width;
425
0
  const int height = pic->height;
426
0
  const int pix_cnt = width * height;
427
  // we round the block size up, so we're guaranteed to have
428
  // at most MAX_REFS_BLOCK_PER_IMAGE blocks used:
429
0
  const int refs_block_size = (pix_cnt - 1) / MAX_REFS_BLOCK_PER_IMAGE + 1;
430
0
  int i;
431
0
  if (!VP8LHashChainInit(&enc->hash_chain, pix_cnt)) return 0;
432
433
0
  for (i = 0; i < 4; ++i) VP8LBackwardRefsInit(&enc->refs[i], refs_block_size);
434
435
0
  return 1;
436
0
}
437
438
// Returns false in case of memory error.
439
static int GetHuffBitLengthsAndCodes(
440
    const VP8LHistogramSet* const histogram_image,
441
0
    HuffmanTreeCode* const huffman_codes) {
442
0
  int i, k;
443
0
  int ok = 0;
444
0
  uint64_t total_length_size = 0;
445
0
  uint8_t* mem_buf = NULL;
446
0
  const int histogram_image_size = histogram_image->size;
447
0
  int max_num_symbols = 0;
448
0
  uint8_t* buf_rle = NULL;
449
0
  HuffmanTree* huff_tree = NULL;
450
451
  // Iterate over all histograms and get the aggregate number of codes used.
452
0
  for (i = 0; i < histogram_image_size; ++i) {
453
0
    const VP8LHistogram* const histo = histogram_image->histograms[i];
454
0
    HuffmanTreeCode* const codes = &huffman_codes[5 * i];
455
0
    assert(histo != NULL);
456
0
    for (k = 0; k < 5; ++k) {
457
0
      const int num_symbols =
458
0
          (k == 0) ? VP8LHistogramNumCodes(histo->palette_code_bits) :
459
0
          (k == 4) ? NUM_DISTANCE_CODES : 256;
460
0
      codes[k].num_symbols = num_symbols;
461
0
      total_length_size += num_symbols;
462
0
    }
463
0
  }
464
465
  // Allocate and Set Huffman codes.
466
0
  {
467
0
    uint16_t* codes;
468
0
    uint8_t* lengths;
469
0
    mem_buf = (uint8_t*)WebPSafeCalloc(total_length_size,
470
0
                                       sizeof(*lengths) + sizeof(*codes));
471
0
    if (mem_buf == NULL) goto End;
472
473
0
    codes = (uint16_t*)mem_buf;
474
0
    lengths = (uint8_t*)&codes[total_length_size];
475
0
    for (i = 0; i < 5 * histogram_image_size; ++i) {
476
0
      const int bit_length = huffman_codes[i].num_symbols;
477
0
      huffman_codes[i].codes = codes;
478
0
      huffman_codes[i].code_lengths = lengths;
479
0
      codes += bit_length;
480
0
      lengths += bit_length;
481
0
      if (max_num_symbols < bit_length) {
482
0
        max_num_symbols = bit_length;
483
0
      }
484
0
    }
485
0
  }
486
487
0
  buf_rle = (uint8_t*)WebPSafeMalloc(1ULL, max_num_symbols);
488
0
  huff_tree = (HuffmanTree*)WebPSafeMalloc(3ULL * max_num_symbols,
489
0
                                           sizeof(*huff_tree));
490
0
  if (buf_rle == NULL || huff_tree == NULL) goto End;
491
492
  // Create Huffman trees.
493
0
  for (i = 0; i < histogram_image_size; ++i) {
494
0
    HuffmanTreeCode* const codes = &huffman_codes[5 * i];
495
0
    VP8LHistogram* const histo = histogram_image->histograms[i];
496
0
    VP8LCreateHuffmanTree(histo->literal, 15, buf_rle, huff_tree, codes + 0);
497
0
    VP8LCreateHuffmanTree(histo->red, 15, buf_rle, huff_tree, codes + 1);
498
0
    VP8LCreateHuffmanTree(histo->blue, 15, buf_rle, huff_tree, codes + 2);
499
0
    VP8LCreateHuffmanTree(histo->alpha, 15, buf_rle, huff_tree, codes + 3);
500
0
    VP8LCreateHuffmanTree(histo->distance, 15, buf_rle, huff_tree, codes + 4);
501
0
  }
502
0
  ok = 1;
503
0
 End:
504
0
  WebPSafeFree(huff_tree);
505
0
  WebPSafeFree(buf_rle);
506
0
  if (!ok) {
507
0
    WebPSafeFree(mem_buf);
508
0
    memset(huffman_codes, 0, 5 * histogram_image_size * sizeof(*huffman_codes));
509
0
  }
510
0
  return ok;
511
0
}
512
513
static void StoreHuffmanTreeOfHuffmanTreeToBitMask(
514
0
    VP8LBitWriter* const bw, const uint8_t* code_length_bitdepth) {
515
  // RFC 1951 will calm you down if you are worried about this funny sequence.
516
  // This sequence is tuned from that, but more weighted for lower symbol count,
517
  // and more spiking histograms.
518
0
  static const uint8_t kStorageOrder[CODE_LENGTH_CODES] = {
519
0
    17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
520
0
  };
521
0
  int i;
522
  // Throw away trailing zeros:
523
0
  int codes_to_store = CODE_LENGTH_CODES;
524
0
  for (; codes_to_store > 4; --codes_to_store) {
525
0
    if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
526
0
      break;
527
0
    }
528
0
  }
529
0
  VP8LPutBits(bw, codes_to_store - 4, 4);
530
0
  for (i = 0; i < codes_to_store; ++i) {
531
0
    VP8LPutBits(bw, code_length_bitdepth[kStorageOrder[i]], 3);
532
0
  }
533
0
}
534
535
static void ClearHuffmanTreeIfOnlyOneSymbol(
536
0
    HuffmanTreeCode* const huffman_code) {
537
0
  int k;
538
0
  int count = 0;
539
0
  for (k = 0; k < huffman_code->num_symbols; ++k) {
540
0
    if (huffman_code->code_lengths[k] != 0) {
541
0
      ++count;
542
0
      if (count > 1) return;
543
0
    }
544
0
  }
545
0
  for (k = 0; k < huffman_code->num_symbols; ++k) {
546
0
    huffman_code->code_lengths[k] = 0;
547
0
    huffman_code->codes[k] = 0;
548
0
  }
549
0
}
550
551
static void StoreHuffmanTreeToBitMask(
552
    VP8LBitWriter* const bw,
553
    const HuffmanTreeToken* const tokens, const int num_tokens,
554
0
    const HuffmanTreeCode* const huffman_code) {
555
0
  int i;
556
0
  for (i = 0; i < num_tokens; ++i) {
557
0
    const int ix = tokens[i].code;
558
0
    const int extra_bits = tokens[i].extra_bits;
559
0
    VP8LPutBits(bw, huffman_code->codes[ix], huffman_code->code_lengths[ix]);
560
0
    switch (ix) {
561
0
      case 16:
562
0
        VP8LPutBits(bw, extra_bits, 2);
563
0
        break;
564
0
      case 17:
565
0
        VP8LPutBits(bw, extra_bits, 3);
566
0
        break;
567
0
      case 18:
568
0
        VP8LPutBits(bw, extra_bits, 7);
569
0
        break;
570
0
    }
571
0
  }
572
0
}
573
574
// 'huff_tree' and 'tokens' are pre-alloacted buffers.
575
static void StoreFullHuffmanCode(VP8LBitWriter* const bw,
576
                                 HuffmanTree* const huff_tree,
577
                                 HuffmanTreeToken* const tokens,
578
0
                                 const HuffmanTreeCode* const tree) {
579
0
  uint8_t code_length_bitdepth[CODE_LENGTH_CODES] = { 0 };
580
0
  uint16_t code_length_bitdepth_symbols[CODE_LENGTH_CODES] = { 0 };
581
0
  const int max_tokens = tree->num_symbols;
582
0
  int num_tokens;
583
0
  HuffmanTreeCode huffman_code;
584
0
  huffman_code.num_symbols = CODE_LENGTH_CODES;
585
0
  huffman_code.code_lengths = code_length_bitdepth;
586
0
  huffman_code.codes = code_length_bitdepth_symbols;
587
588
0
  VP8LPutBits(bw, 0, 1);
589
0
  num_tokens = VP8LCreateCompressedHuffmanTree(tree, tokens, max_tokens);
590
0
  {
591
0
    uint32_t histogram[CODE_LENGTH_CODES] = { 0 };
592
0
    uint8_t buf_rle[CODE_LENGTH_CODES] = { 0 };
593
0
    int i;
594
0
    for (i = 0; i < num_tokens; ++i) {
595
0
      ++histogram[tokens[i].code];
596
0
    }
597
598
0
    VP8LCreateHuffmanTree(histogram, 7, buf_rle, huff_tree, &huffman_code);
599
0
  }
600
601
0
  StoreHuffmanTreeOfHuffmanTreeToBitMask(bw, code_length_bitdepth);
602
0
  ClearHuffmanTreeIfOnlyOneSymbol(&huffman_code);
603
0
  {
604
0
    int trailing_zero_bits = 0;
605
0
    int trimmed_length = num_tokens;
606
0
    int write_trimmed_length;
607
0
    int length;
608
0
    int i = num_tokens;
609
0
    while (i-- > 0) {
610
0
      const int ix = tokens[i].code;
611
0
      if (ix == 0 || ix == 17 || ix == 18) {
612
0
        --trimmed_length;   // discount trailing zeros
613
0
        trailing_zero_bits += code_length_bitdepth[ix];
614
0
        if (ix == 17) {
615
0
          trailing_zero_bits += 3;
616
0
        } else if (ix == 18) {
617
0
          trailing_zero_bits += 7;
618
0
        }
619
0
      } else {
620
0
        break;
621
0
      }
622
0
    }
623
0
    write_trimmed_length = (trimmed_length > 1 && trailing_zero_bits > 12);
624
0
    length = write_trimmed_length ? trimmed_length : num_tokens;
625
0
    VP8LPutBits(bw, write_trimmed_length, 1);
626
0
    if (write_trimmed_length) {
627
0
      if (trimmed_length == 2) {
628
0
        VP8LPutBits(bw, 0, 3 + 2);     // nbitpairs=1, trimmed_length=2
629
0
      } else {
630
0
        const int nbits = BitsLog2Floor(trimmed_length - 2);
631
0
        const int nbitpairs = nbits / 2 + 1;
632
0
        assert(trimmed_length > 2);
633
0
        assert(nbitpairs - 1 < 8);
634
0
        VP8LPutBits(bw, nbitpairs - 1, 3);
635
0
        VP8LPutBits(bw, trimmed_length - 2, nbitpairs * 2);
636
0
      }
637
0
    }
638
0
    StoreHuffmanTreeToBitMask(bw, tokens, length, &huffman_code);
639
0
  }
640
0
}
641
642
// 'huff_tree' and 'tokens' are pre-alloacted buffers.
643
static void StoreHuffmanCode(VP8LBitWriter* const bw,
644
                             HuffmanTree* const huff_tree,
645
                             HuffmanTreeToken* const tokens,
646
0
                             const HuffmanTreeCode* const huffman_code) {
647
0
  int i;
648
0
  int count = 0;
649
0
  int symbols[2] = { 0, 0 };
650
0
  const int kMaxBits = 8;
651
0
  const int kMaxSymbol = 1 << kMaxBits;
652
653
  // Check whether it's a small tree.
654
0
  for (i = 0; i < huffman_code->num_symbols && count < 3; ++i) {
655
0
    if (huffman_code->code_lengths[i] != 0) {
656
0
      if (count < 2) symbols[count] = i;
657
0
      ++count;
658
0
    }
659
0
  }
660
661
0
  if (count == 0) {   // emit minimal tree for empty cases
662
    // bits: small tree marker: 1, count-1: 0, large 8-bit code: 0, code: 0
663
0
    VP8LPutBits(bw, 0x01, 4);
664
0
  } else if (count <= 2 && symbols[0] < kMaxSymbol && symbols[1] < kMaxSymbol) {
665
0
    VP8LPutBits(bw, 1, 1);  // Small tree marker to encode 1 or 2 symbols.
666
0
    VP8LPutBits(bw, count - 1, 1);
667
0
    if (symbols[0] <= 1) {
668
0
      VP8LPutBits(bw, 0, 1);  // Code bit for small (1 bit) symbol value.
669
0
      VP8LPutBits(bw, symbols[0], 1);
670
0
    } else {
671
0
      VP8LPutBits(bw, 1, 1);
672
0
      VP8LPutBits(bw, symbols[0], 8);
673
0
    }
674
0
    if (count == 2) {
675
0
      VP8LPutBits(bw, symbols[1], 8);
676
0
    }
677
0
  } else {
678
0
    StoreFullHuffmanCode(bw, huff_tree, tokens, huffman_code);
679
0
  }
680
0
}
681
682
static WEBP_INLINE void WriteHuffmanCode(VP8LBitWriter* const bw,
683
                             const HuffmanTreeCode* const code,
684
0
                             int code_index) {
685
0
  const int depth = code->code_lengths[code_index];
686
0
  const int symbol = code->codes[code_index];
687
0
  VP8LPutBits(bw, symbol, depth);
688
0
}
689
690
static WEBP_INLINE void WriteHuffmanCodeWithExtraBits(
691
    VP8LBitWriter* const bw,
692
    const HuffmanTreeCode* const code,
693
    int code_index,
694
    int bits,
695
0
    int n_bits) {
696
0
  const int depth = code->code_lengths[code_index];
697
0
  const int symbol = code->codes[code_index];
698
0
  VP8LPutBits(bw, (bits << depth) | symbol, depth + n_bits);
699
0
}
700
701
static int StoreImageToBitMask(VP8LBitWriter* const bw, int width,
702
                               int histo_bits,
703
                               const VP8LBackwardRefs* const refs,
704
                               const uint32_t* histogram_symbols,
705
                               const HuffmanTreeCode* const huffman_codes,
706
0
                               const WebPPicture* const pic) {
707
0
  const int histo_xsize = histo_bits ? VP8LSubSampleSize(width, histo_bits) : 1;
708
0
  const int tile_mask = (histo_bits == 0) ? 0 : -(1 << histo_bits);
709
  // x and y trace the position in the image.
710
0
  int x = 0;
711
0
  int y = 0;
712
0
  int tile_x = x & tile_mask;
713
0
  int tile_y = y & tile_mask;
714
0
  int histogram_ix = (histogram_symbols[0] >> 8) & 0xffff;
715
0
  const HuffmanTreeCode* codes = huffman_codes + 5 * histogram_ix;
716
0
  VP8LRefsCursor c = VP8LRefsCursorInit(refs);
717
0
  while (VP8LRefsCursorOk(&c)) {
718
0
    const PixOrCopy* const v = c.cur_pos;
719
0
    if ((tile_x != (x & tile_mask)) || (tile_y != (y & tile_mask))) {
720
0
      tile_x = x & tile_mask;
721
0
      tile_y = y & tile_mask;
722
0
      histogram_ix = (histogram_symbols[(y >> histo_bits) * histo_xsize +
723
0
                                        (x >> histo_bits)] >>
724
0
                      8) &
725
0
                     0xffff;
726
0
      codes = huffman_codes + 5 * histogram_ix;
727
0
    }
728
0
    if (PixOrCopyIsLiteral(v)) {
729
0
      static const uint8_t order[] = { 1, 2, 0, 3 };
730
0
      int k;
731
0
      for (k = 0; k < 4; ++k) {
732
0
        const int code = PixOrCopyLiteral(v, order[k]);
733
0
        WriteHuffmanCode(bw, codes + k, code);
734
0
      }
735
0
    } else if (PixOrCopyIsCacheIdx(v)) {
736
0
      const int code = PixOrCopyCacheIdx(v);
737
0
      const int literal_ix = 256 + NUM_LENGTH_CODES + code;
738
0
      WriteHuffmanCode(bw, codes, literal_ix);
739
0
    } else {
740
0
      int bits, n_bits;
741
0
      int code;
742
743
0
      const int distance = PixOrCopyDistance(v);
744
0
      VP8LPrefixEncode(v->len, &code, &n_bits, &bits);
745
0
      WriteHuffmanCodeWithExtraBits(bw, codes, 256 + code, bits, n_bits);
746
747
      // Don't write the distance with the extra bits code since
748
      // the distance can be up to 18 bits of extra bits, and the prefix
749
      // 15 bits, totaling to 33, and our PutBits only supports up to 32 bits.
750
0
      VP8LPrefixEncode(distance, &code, &n_bits, &bits);
751
0
      WriteHuffmanCode(bw, codes + 4, code);
752
0
      VP8LPutBits(bw, bits, n_bits);
753
0
    }
754
0
    x += PixOrCopyLength(v);
755
0
    while (x >= width) {
756
0
      x -= width;
757
0
      ++y;
758
0
    }
759
0
    VP8LRefsCursorNext(&c);
760
0
  }
761
0
  if (bw->error) {
762
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
763
0
  }
764
0
  return 1;
765
0
}
766
767
// Special case of EncodeImageInternal() for cache-bits=0, histo_bits=31.
768
// pic and percent are for progress.
769
static int EncodeImageNoHuffman(VP8LBitWriter* const bw,
770
                                const uint32_t* const argb,
771
                                VP8LHashChain* const hash_chain,
772
                                VP8LBackwardRefs* const refs_array, int width,
773
                                int height, int quality, int low_effort,
774
                                const WebPPicture* const pic, int percent_range,
775
0
                                int* const percent) {
776
0
  int i;
777
0
  int max_tokens = 0;
778
0
  VP8LBackwardRefs* refs;
779
0
  HuffmanTreeToken* tokens = NULL;
780
0
  HuffmanTreeCode huffman_codes[5] = {{0, NULL, NULL}};
781
0
  const uint32_t histogram_symbols[1] = {0};  // only one tree, one symbol
782
0
  int cache_bits = 0;
783
0
  VP8LHistogramSet* histogram_image = NULL;
784
0
  HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc(
785
0
      3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree));
786
0
  if (huff_tree == NULL) {
787
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
788
0
    goto Error;
789
0
  }
790
791
  // Calculate backward references from ARGB image.
792
0
  if (!VP8LHashChainFill(hash_chain, quality, argb, width, height, low_effort,
793
0
                         pic, percent_range / 2, percent)) {
794
0
    goto Error;
795
0
  }
796
0
  if (!VP8LGetBackwardReferences(width, height, argb, quality, /*low_effort=*/0,
797
0
                                 kLZ77Standard | kLZ77RLE, cache_bits,
798
0
                                 /*do_no_cache=*/0, hash_chain, refs_array,
799
0
                                 &cache_bits, pic,
800
0
                                 percent_range - percent_range / 2, percent)) {
801
0
    goto Error;
802
0
  }
803
0
  refs = &refs_array[0];
804
0
  histogram_image = VP8LAllocateHistogramSet(1, cache_bits);
805
0
  if (histogram_image == NULL) {
806
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
807
0
    goto Error;
808
0
  }
809
0
  VP8LHistogramSetClear(histogram_image);
810
811
  // Build histogram image and symbols from backward references.
812
0
  VP8LHistogramStoreRefs(refs, /*distance_modifier=*/NULL,
813
0
                         /*distance_modifier_arg0=*/0,
814
0
                         histogram_image->histograms[0]);
815
816
  // Create Huffman bit lengths and codes for each histogram image.
817
0
  assert(histogram_image->size == 1);
818
0
  if (!GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
819
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
820
0
    goto Error;
821
0
  }
822
823
  // No color cache, no Huffman image.
824
0
  VP8LPutBits(bw, 0, 1);
825
826
  // Find maximum number of symbols for the huffman tree-set.
827
0
  for (i = 0; i < 5; ++i) {
828
0
    HuffmanTreeCode* const codes = &huffman_codes[i];
829
0
    if (max_tokens < codes->num_symbols) {
830
0
      max_tokens = codes->num_symbols;
831
0
    }
832
0
  }
833
834
0
  tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens));
835
0
  if (tokens == NULL) {
836
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
837
0
    goto Error;
838
0
  }
839
840
  // Store Huffman codes.
841
0
  for (i = 0; i < 5; ++i) {
842
0
    HuffmanTreeCode* const codes = &huffman_codes[i];
843
0
    StoreHuffmanCode(bw, huff_tree, tokens, codes);
844
0
    ClearHuffmanTreeIfOnlyOneSymbol(codes);
845
0
  }
846
847
  // Store actual literals.
848
0
  if (!StoreImageToBitMask(bw, width, 0, refs, histogram_symbols, huffman_codes,
849
0
                           pic)) {
850
0
    goto Error;
851
0
  }
852
853
0
 Error:
854
0
  WebPSafeFree(tokens);
855
0
  WebPSafeFree(huff_tree);
856
0
  VP8LFreeHistogramSet(histogram_image);
857
0
  WebPSafeFree(huffman_codes[0].codes);
858
0
  return (pic->error_code == VP8_ENC_OK);
859
0
}
860
861
// pic and percent are for progress.
862
static int EncodeImageInternal(
863
    VP8LBitWriter* const bw, const uint32_t* const argb,
864
    VP8LHashChain* const hash_chain, VP8LBackwardRefs refs_array[4], int width,
865
    int height, int quality, int low_effort, const CrunchConfig* const config,
866
    int* cache_bits, int histogram_bits_in, size_t init_byte_position,
867
    int* const hdr_size, int* const data_size, const WebPPicture* const pic,
868
0
    int percent_range, int* const percent) {
869
0
  const uint32_t histogram_image_xysize =
870
0
      VP8LSubSampleSize(width, histogram_bits_in) *
871
0
      VP8LSubSampleSize(height, histogram_bits_in);
872
0
  int remaining_percent = percent_range;
873
0
  int percent_start = *percent;
874
0
  VP8LHistogramSet* histogram_image = NULL;
875
0
  VP8LHistogram* tmp_histo = NULL;
876
0
  uint32_t i, histogram_image_size = 0;
877
0
  size_t bit_array_size = 0;
878
0
  HuffmanTree* const huff_tree = (HuffmanTree*)WebPSafeMalloc(
879
0
      3ULL * CODE_LENGTH_CODES, sizeof(*huff_tree));
880
0
  HuffmanTreeToken* tokens = NULL;
881
0
  HuffmanTreeCode* huffman_codes = NULL;
882
0
  uint32_t* const histogram_argb = (uint32_t*)WebPSafeMalloc(
883
0
      histogram_image_xysize, sizeof(*histogram_argb));
884
0
  int sub_configs_idx;
885
0
  int cache_bits_init, write_histogram_image;
886
0
  VP8LBitWriter bw_init = *bw, bw_best;
887
0
  int hdr_size_tmp;
888
0
  VP8LHashChain hash_chain_histogram;  // histogram image hash chain
889
0
  size_t bw_size_best = ~(size_t)0;
890
0
  assert(histogram_bits_in >= MIN_HUFFMAN_BITS);
891
0
  assert(histogram_bits_in <= MAX_HUFFMAN_BITS);
892
0
  assert(hdr_size != NULL);
893
0
  assert(data_size != NULL);
894
895
0
  memset(&hash_chain_histogram, 0, sizeof(hash_chain_histogram));
896
0
  if (!VP8LBitWriterInit(&bw_best, 0)) {
897
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
898
0
    goto Error;
899
0
  }
900
901
  // Make sure we can allocate the different objects.
902
0
  if (huff_tree == NULL || histogram_argb == NULL ||
903
0
      !VP8LHashChainInit(&hash_chain_histogram, histogram_image_xysize)) {
904
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
905
0
    goto Error;
906
0
  }
907
908
0
  percent_range = remaining_percent / 5;
909
0
  if (!VP8LHashChainFill(hash_chain, quality, argb, width, height,
910
0
                         low_effort, pic, percent_range, percent)) {
911
0
    goto Error;
912
0
  }
913
0
  percent_start += percent_range;
914
0
  remaining_percent -= percent_range;
915
916
  // If the value is different from zero, it has been set during the palette
917
  // analysis.
918
0
  cache_bits_init = (*cache_bits == 0) ? MAX_COLOR_CACHE_BITS : *cache_bits;
919
  // If several iterations will happen, clone into bw_best.
920
0
  if ((config->sub_configs_size > 1 || config->sub_configs[0].do_no_cache) &&
921
0
      !VP8LBitWriterClone(bw, &bw_best)) {
922
0
    WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
923
0
    goto Error;
924
0
  }
925
926
0
  for (sub_configs_idx = 0; sub_configs_idx < config->sub_configs_size;
927
0
       ++sub_configs_idx) {
928
0
    const CrunchSubConfig* const sub_config =
929
0
        &config->sub_configs[sub_configs_idx];
930
0
    int cache_bits_best, i_cache;
931
0
    int i_remaining_percent = remaining_percent / config->sub_configs_size;
932
0
    int i_percent_range = i_remaining_percent / 4;
933
0
    i_remaining_percent -= i_percent_range;
934
935
0
    if (!VP8LGetBackwardReferences(
936
0
            width, height, argb, quality, low_effort, sub_config->lz77,
937
0
            cache_bits_init, sub_config->do_no_cache, hash_chain,
938
0
            &refs_array[0], &cache_bits_best, pic, i_percent_range, percent)) {
939
0
      goto Error;
940
0
    }
941
942
0
    for (i_cache = 0; i_cache < (sub_config->do_no_cache ? 2 : 1); ++i_cache) {
943
0
      const int cache_bits_tmp = (i_cache == 0) ? cache_bits_best : 0;
944
0
      int histogram_bits = histogram_bits_in;
945
      // Speed-up: no need to study the no-cache case if it was already studied
946
      // in i_cache == 0.
947
0
      if (i_cache == 1 && cache_bits_best == 0) break;
948
949
      // Reset the bit writer for this iteration.
950
0
      VP8LBitWriterReset(&bw_init, bw);
951
952
      // Build histogram image and symbols from backward references.
953
0
      histogram_image =
954
0
          VP8LAllocateHistogramSet(histogram_image_xysize, cache_bits_tmp);
955
0
      tmp_histo = VP8LAllocateHistogram(cache_bits_tmp);
956
0
      if (histogram_image == NULL || tmp_histo == NULL) {
957
0
        WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
958
0
        goto Error;
959
0
      }
960
961
0
      i_percent_range = i_remaining_percent / 3;
962
0
      i_remaining_percent -= i_percent_range;
963
0
      if (!VP8LGetHistoImageSymbols(
964
0
              width, height, &refs_array[i_cache], quality, low_effort,
965
0
              histogram_bits, cache_bits_tmp, histogram_image, tmp_histo,
966
0
              histogram_argb, pic, i_percent_range, percent)) {
967
0
        goto Error;
968
0
      }
969
      // Create Huffman bit lengths and codes for each histogram image.
970
0
      histogram_image_size = histogram_image->size;
971
0
      bit_array_size = 5 * histogram_image_size;
972
0
      huffman_codes = (HuffmanTreeCode*)WebPSafeCalloc(bit_array_size,
973
0
                                                       sizeof(*huffman_codes));
974
      // Note: some histogram_image entries may point to tmp_histos[], so the
975
      // latter need to outlive the following call to
976
      // GetHuffBitLengthsAndCodes().
977
0
      if (huffman_codes == NULL ||
978
0
          !GetHuffBitLengthsAndCodes(histogram_image, huffman_codes)) {
979
0
        WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
980
0
        goto Error;
981
0
      }
982
      // Free combined histograms.
983
0
      VP8LFreeHistogramSet(histogram_image);
984
0
      histogram_image = NULL;
985
986
      // Free scratch histograms.
987
0
      VP8LFreeHistogram(tmp_histo);
988
0
      tmp_histo = NULL;
989
990
      // Color Cache parameters.
991
0
      if (cache_bits_tmp > 0) {
992
0
        VP8LPutBits(bw, 1, 1);
993
0
        VP8LPutBits(bw, cache_bits_tmp, 4);
994
0
      } else {
995
0
        VP8LPutBits(bw, 0, 1);
996
0
      }
997
998
      // Huffman image + meta huffman.
999
0
      histogram_image_size = 0;
1000
0
      for (i = 0; i < histogram_image_xysize; ++i) {
1001
0
        if (histogram_argb[i] >= histogram_image_size) {
1002
0
          histogram_image_size = histogram_argb[i] + 1;
1003
0
        }
1004
0
        histogram_argb[i] <<= 8;
1005
0
      }
1006
1007
0
      write_histogram_image = (histogram_image_size > 1);
1008
0
      VP8LPutBits(bw, write_histogram_image, 1);
1009
0
      if (write_histogram_image) {
1010
0
        VP8LOptimizeSampling(histogram_argb, width, height, histogram_bits_in,
1011
0
                             MAX_HUFFMAN_BITS, &histogram_bits);
1012
0
        VP8LPutBits(bw, histogram_bits - 2, 3);
1013
0
        i_percent_range = i_remaining_percent / 2;
1014
0
        i_remaining_percent -= i_percent_range;
1015
0
        if (!EncodeImageNoHuffman(
1016
0
                bw, histogram_argb, &hash_chain_histogram, &refs_array[2],
1017
0
                VP8LSubSampleSize(width, histogram_bits),
1018
0
                VP8LSubSampleSize(height, histogram_bits), quality, low_effort,
1019
0
                pic, i_percent_range, percent)) {
1020
0
          goto Error;
1021
0
        }
1022
0
      }
1023
1024
      // Store Huffman codes.
1025
0
      {
1026
0
        int max_tokens = 0;
1027
        // Find maximum number of symbols for the huffman tree-set.
1028
0
        for (i = 0; i < 5 * histogram_image_size; ++i) {
1029
0
          HuffmanTreeCode* const codes = &huffman_codes[i];
1030
0
          if (max_tokens < codes->num_symbols) {
1031
0
            max_tokens = codes->num_symbols;
1032
0
          }
1033
0
        }
1034
0
        tokens = (HuffmanTreeToken*)WebPSafeMalloc(max_tokens, sizeof(*tokens));
1035
0
        if (tokens == NULL) {
1036
0
          WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1037
0
          goto Error;
1038
0
        }
1039
0
        for (i = 0; i < 5 * histogram_image_size; ++i) {
1040
0
          HuffmanTreeCode* const codes = &huffman_codes[i];
1041
0
          StoreHuffmanCode(bw, huff_tree, tokens, codes);
1042
0
          ClearHuffmanTreeIfOnlyOneSymbol(codes);
1043
0
        }
1044
0
      }
1045
      // Store actual literals.
1046
0
      hdr_size_tmp = (int)(VP8LBitWriterNumBytes(bw) - init_byte_position);
1047
0
      if (!StoreImageToBitMask(bw, width, histogram_bits, &refs_array[i_cache],
1048
0
                               histogram_argb, huffman_codes, pic)) {
1049
0
        goto Error;
1050
0
      }
1051
      // Keep track of the smallest image so far.
1052
0
      if (VP8LBitWriterNumBytes(bw) < bw_size_best) {
1053
0
        bw_size_best = VP8LBitWriterNumBytes(bw);
1054
0
        *cache_bits = cache_bits_tmp;
1055
0
        *hdr_size = hdr_size_tmp;
1056
0
        *data_size =
1057
0
            (int)(VP8LBitWriterNumBytes(bw) - init_byte_position - *hdr_size);
1058
0
        VP8LBitWriterSwap(bw, &bw_best);
1059
0
      }
1060
0
      WebPSafeFree(tokens);
1061
0
      tokens = NULL;
1062
0
      if (huffman_codes != NULL) {
1063
0
        WebPSafeFree(huffman_codes->codes);
1064
0
        WebPSafeFree(huffman_codes);
1065
0
        huffman_codes = NULL;
1066
0
      }
1067
0
    }
1068
0
  }
1069
0
  VP8LBitWriterSwap(bw, &bw_best);
1070
1071
0
  if (!WebPReportProgress(pic, percent_start + remaining_percent, percent)) {
1072
0
    goto Error;
1073
0
  }
1074
1075
0
 Error:
1076
0
  WebPSafeFree(tokens);
1077
0
  WebPSafeFree(huff_tree);
1078
0
  VP8LFreeHistogramSet(histogram_image);
1079
0
  VP8LFreeHistogram(tmp_histo);
1080
0
  VP8LHashChainClear(&hash_chain_histogram);
1081
0
  if (huffman_codes != NULL) {
1082
0
    WebPSafeFree(huffman_codes->codes);
1083
0
    WebPSafeFree(huffman_codes);
1084
0
  }
1085
0
  WebPSafeFree(histogram_argb);
1086
0
  VP8LBitWriterWipeOut(&bw_best);
1087
0
  return (pic->error_code == VP8_ENC_OK);
1088
0
}
1089
1090
// -----------------------------------------------------------------------------
1091
// Transforms
1092
1093
static void ApplySubtractGreen(VP8LEncoder* const enc, int width, int height,
1094
0
                               VP8LBitWriter* const bw) {
1095
0
  VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1096
0
  VP8LPutBits(bw, SUBTRACT_GREEN_TRANSFORM, 2);
1097
0
  VP8LSubtractGreenFromBlueAndRed(enc->argb, width * height);
1098
0
}
1099
1100
static int ApplyPredictFilter(VP8LEncoder* const enc, int width, int height,
1101
                              int quality, int low_effort,
1102
                              int used_subtract_green, VP8LBitWriter* const bw,
1103
                              int percent_range, int* const percent,
1104
0
                              int* const best_bits) {
1105
0
  const int near_lossless_strength =
1106
0
      enc->use_palette ? 100 : enc->config->near_lossless;
1107
0
  const int max_bits = ClampBits(width, height, enc->predictor_transform_bits,
1108
0
                                 MIN_TRANSFORM_BITS, MAX_TRANSFORM_BITS,
1109
0
                                 MAX_PREDICTOR_IMAGE_SIZE);
1110
0
  const int min_bits = ClampBits(
1111
0
      width, height,
1112
0
      max_bits - 2 * (enc->config->method > 4 ? enc->config->method - 4 : 0),
1113
0
      MIN_TRANSFORM_BITS, MAX_TRANSFORM_BITS, MAX_PREDICTOR_IMAGE_SIZE);
1114
1115
0
  if (!VP8LResidualImage(width, height, min_bits, max_bits, low_effort,
1116
0
                         enc->argb, enc->argb_scratch, enc->transform_data,
1117
0
                         near_lossless_strength, enc->config->exact,
1118
0
                         used_subtract_green, enc->pic, percent_range / 2,
1119
0
                         percent, best_bits)) {
1120
0
    return 0;
1121
0
  }
1122
0
  VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1123
0
  VP8LPutBits(bw, PREDICTOR_TRANSFORM, 2);
1124
0
  assert(*best_bits >= MIN_TRANSFORM_BITS && *best_bits <= MAX_TRANSFORM_BITS);
1125
0
  VP8LPutBits(bw, *best_bits - MIN_TRANSFORM_BITS, NUM_TRANSFORM_BITS);
1126
0
  return EncodeImageNoHuffman(
1127
0
      bw, enc->transform_data, &enc->hash_chain, &enc->refs[0],
1128
0
      VP8LSubSampleSize(width, *best_bits),
1129
0
      VP8LSubSampleSize(height, *best_bits), quality, low_effort, enc->pic,
1130
0
      percent_range - percent_range / 2, percent);
1131
0
}
1132
1133
static int ApplyCrossColorFilter(VP8LEncoder* const enc, int width, int height,
1134
                                 int quality, int low_effort,
1135
                                 VP8LBitWriter* const bw, int percent_range,
1136
0
                                 int* const percent, int* const best_bits) {
1137
0
  const int min_bits = enc->cross_color_transform_bits;
1138
1139
0
  if (!VP8LColorSpaceTransform(width, height, min_bits, quality, enc->argb,
1140
0
                               enc->transform_data, enc->pic, percent_range / 2,
1141
0
                               percent, best_bits)) {
1142
0
    return 0;
1143
0
  }
1144
0
  VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1145
0
  VP8LPutBits(bw, CROSS_COLOR_TRANSFORM, 2);
1146
0
  assert(*best_bits >= MIN_TRANSFORM_BITS && *best_bits <= MAX_TRANSFORM_BITS);
1147
0
  VP8LPutBits(bw, *best_bits - MIN_TRANSFORM_BITS, NUM_TRANSFORM_BITS);
1148
0
  return EncodeImageNoHuffman(
1149
0
      bw, enc->transform_data, &enc->hash_chain, &enc->refs[0],
1150
0
      VP8LSubSampleSize(width, *best_bits),
1151
0
      VP8LSubSampleSize(height, *best_bits), quality, low_effort, enc->pic,
1152
0
      percent_range - percent_range / 2, percent);
1153
0
}
1154
1155
// -----------------------------------------------------------------------------
1156
1157
static int WriteRiffHeader(const WebPPicture* const pic, size_t riff_size,
1158
0
                           size_t vp8l_size) {
1159
0
  uint8_t riff[RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + VP8L_SIGNATURE_SIZE] = {
1160
0
    'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P',
1161
0
    'V', 'P', '8', 'L', 0, 0, 0, 0, VP8L_MAGIC_BYTE,
1162
0
  };
1163
0
  PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
1164
0
  PutLE32(riff + RIFF_HEADER_SIZE + TAG_SIZE, (uint32_t)vp8l_size);
1165
0
  return pic->writer(riff, sizeof(riff), pic);
1166
0
}
1167
1168
static int WriteImageSize(const WebPPicture* const pic,
1169
0
                          VP8LBitWriter* const bw) {
1170
0
  const int width = pic->width - 1;
1171
0
  const int height = pic->height - 1;
1172
0
  assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
1173
1174
0
  VP8LPutBits(bw, width, VP8L_IMAGE_SIZE_BITS);
1175
0
  VP8LPutBits(bw, height, VP8L_IMAGE_SIZE_BITS);
1176
0
  return !bw->error;
1177
0
}
1178
1179
0
static int WriteRealAlphaAndVersion(VP8LBitWriter* const bw, int has_alpha) {
1180
0
  VP8LPutBits(bw, has_alpha, 1);
1181
0
  VP8LPutBits(bw, VP8L_VERSION, VP8L_VERSION_BITS);
1182
0
  return !bw->error;
1183
0
}
1184
1185
static int WriteImage(const WebPPicture* const pic, VP8LBitWriter* const bw,
1186
0
                      size_t* const coded_size) {
1187
0
  const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
1188
0
  const size_t webpll_size = VP8LBitWriterNumBytes(bw);
1189
0
  const size_t vp8l_size = VP8L_SIGNATURE_SIZE + webpll_size;
1190
0
  const size_t pad = vp8l_size & 1;
1191
0
  const size_t riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8l_size + pad;
1192
0
  *coded_size = 0;
1193
1194
0
  if (bw->error) {
1195
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1196
0
  }
1197
1198
0
  if (!WriteRiffHeader(pic, riff_size, vp8l_size) ||
1199
0
      !pic->writer(webpll_data, webpll_size, pic)) {
1200
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE);
1201
0
  }
1202
1203
0
  if (pad) {
1204
0
    const uint8_t pad_byte[1] = { 0 };
1205
0
    if (!pic->writer(pad_byte, 1, pic)) {
1206
0
      return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_WRITE);
1207
0
    }
1208
0
  }
1209
0
  *coded_size = CHUNK_HEADER_SIZE + riff_size;
1210
0
  return 1;
1211
0
}
1212
1213
// -----------------------------------------------------------------------------
1214
1215
0
static void ClearTransformBuffer(VP8LEncoder* const enc) {
1216
0
  WebPSafeFree(enc->transform_mem);
1217
0
  enc->transform_mem = NULL;
1218
0
  enc->transform_mem_size = 0;
1219
0
}
1220
1221
// Allocates the memory for argb (W x H) buffer, 2 rows of context for
1222
// prediction and transform data.
1223
// Flags influencing the memory allocated:
1224
//  enc->transform_bits
1225
//  enc->use_predict, enc->use_cross_color
1226
static int AllocateTransformBuffer(VP8LEncoder* const enc, int width,
1227
0
                                   int height) {
1228
0
  const uint64_t image_size = (uint64_t)width * height;
1229
  // VP8LResidualImage needs room for 2 scanlines of uint32 pixels with an extra
1230
  // pixel in each, plus 2 regular scanlines of bytes.
1231
  // TODO(skal): Clean up by using arithmetic in bytes instead of words.
1232
0
  const uint64_t argb_scratch_size =
1233
0
      enc->use_predict ? (width + 1) * 2 + (width * 2 + sizeof(uint32_t) - 1) /
1234
0
                                               sizeof(uint32_t)
1235
0
                        : 0;
1236
0
  const uint64_t transform_data_size =
1237
0
      (enc->use_predict || enc->use_cross_color)
1238
0
          ? (uint64_t)VP8LSubSampleSize(width, MIN_TRANSFORM_BITS) *
1239
0
                VP8LSubSampleSize(height, MIN_TRANSFORM_BITS)
1240
0
          : 0;
1241
0
  const uint64_t max_alignment_in_words =
1242
0
      (WEBP_ALIGN_CST + sizeof(uint32_t) - 1) / sizeof(uint32_t);
1243
0
  const uint64_t mem_size = image_size + max_alignment_in_words +
1244
0
                            argb_scratch_size + max_alignment_in_words +
1245
0
                            transform_data_size;
1246
0
  uint32_t* mem = enc->transform_mem;
1247
0
  if (mem == NULL || mem_size > enc->transform_mem_size) {
1248
0
    ClearTransformBuffer(enc);
1249
0
    mem = (uint32_t*)WebPSafeMalloc(mem_size, sizeof(*mem));
1250
0
    if (mem == NULL) {
1251
0
      return WebPEncodingSetError(enc->pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1252
0
    }
1253
0
    enc->transform_mem = mem;
1254
0
    enc->transform_mem_size = (size_t)mem_size;
1255
0
    enc->argb_content = kEncoderNone;
1256
0
  }
1257
0
  enc->argb = mem;
1258
0
  mem = (uint32_t*)WEBP_ALIGN(mem + image_size);
1259
0
  enc->argb_scratch = mem;
1260
0
  mem = (uint32_t*)WEBP_ALIGN(mem + argb_scratch_size);
1261
0
  enc->transform_data = mem;
1262
1263
0
  enc->current_width = width;
1264
0
  return 1;
1265
0
}
1266
1267
0
static int MakeInputImageCopy(VP8LEncoder* const enc) {
1268
0
  const WebPPicture* const picture = enc->pic;
1269
0
  const int width = picture->width;
1270
0
  const int height = picture->height;
1271
1272
0
  if (!AllocateTransformBuffer(enc, width, height)) return 0;
1273
0
  if (enc->argb_content == kEncoderARGB) return 1;
1274
1275
0
  {
1276
0
    uint32_t* dst = enc->argb;
1277
0
    const uint32_t* src = picture->argb;
1278
0
    int y;
1279
0
    for (y = 0; y < height; ++y) {
1280
0
      memcpy(dst, src, width * sizeof(*dst));
1281
0
      dst += width;
1282
0
      src += picture->argb_stride;
1283
0
    }
1284
0
  }
1285
0
  enc->argb_content = kEncoderARGB;
1286
0
  assert(enc->current_width == width);
1287
0
  return 1;
1288
0
}
1289
1290
// -----------------------------------------------------------------------------
1291
1292
0
#define APPLY_PALETTE_GREEDY_MAX 4
1293
1294
static WEBP_INLINE uint32_t SearchColorGreedy(const uint32_t palette[],
1295
                                              int palette_size,
1296
0
                                              uint32_t color) {
1297
0
  (void)palette_size;
1298
0
  assert(palette_size < APPLY_PALETTE_GREEDY_MAX);
1299
0
  assert(3 == APPLY_PALETTE_GREEDY_MAX - 1);
1300
0
  if (color == palette[0]) return 0;
1301
0
  if (color == palette[1]) return 1;
1302
0
  if (color == palette[2]) return 2;
1303
0
  return 3;
1304
0
}
1305
1306
0
static WEBP_INLINE uint32_t ApplyPaletteHash0(uint32_t color) {
1307
  // Focus on the green color.
1308
0
  return (color >> 8) & 0xff;
1309
0
}
1310
1311
0
#define PALETTE_INV_SIZE_BITS 11
1312
#define PALETTE_INV_SIZE (1 << PALETTE_INV_SIZE_BITS)
1313
1314
0
static WEBP_INLINE uint32_t ApplyPaletteHash1(uint32_t color) {
1315
  // Forget about alpha.
1316
0
  return ((uint32_t)((color & 0x00ffffffu) * 4222244071ull)) >>
1317
0
         (32 - PALETTE_INV_SIZE_BITS);
1318
0
}
1319
1320
0
static WEBP_INLINE uint32_t ApplyPaletteHash2(uint32_t color) {
1321
  // Forget about alpha.
1322
0
  return ((uint32_t)((color & 0x00ffffffu) * ((1ull << 31) - 1))) >>
1323
0
         (32 - PALETTE_INV_SIZE_BITS);
1324
0
}
1325
1326
// Use 1 pixel cache for ARGB pixels.
1327
0
#define APPLY_PALETTE_FOR(COLOR_INDEX) do {         \
1328
0
  uint32_t prev_pix = palette[0];                   \
1329
0
  uint32_t prev_idx = 0;                            \
1330
0
  for (y = 0; y < height; ++y) {                    \
1331
0
    for (x = 0; x < width; ++x) {                   \
1332
0
      const uint32_t pix = src[x];                  \
1333
0
      if (pix != prev_pix) {                        \
1334
0
        prev_idx = COLOR_INDEX;                     \
1335
0
        prev_pix = pix;                             \
1336
0
      }                                             \
1337
0
      tmp_row[x] = prev_idx;                        \
1338
0
    }                                               \
1339
0
    VP8LBundleColorMap(tmp_row, width, xbits, dst); \
1340
0
    src += src_stride;                              \
1341
0
    dst += dst_stride;                              \
1342
0
  }                                                 \
1343
0
} while (0)
1344
1345
// Remap argb values in src[] to packed palettes entries in dst[]
1346
// using 'row' as a temporary buffer of size 'width'.
1347
// We assume that all src[] values have a corresponding entry in the palette.
1348
// Note: src[] can be the same as dst[]
1349
static int ApplyPalette(const uint32_t* src, uint32_t src_stride, uint32_t* dst,
1350
                        uint32_t dst_stride, const uint32_t* palette,
1351
                        int palette_size, int width, int height, int xbits,
1352
0
                        const WebPPicture* const pic) {
1353
  // TODO(skal): this tmp buffer is not needed if VP8LBundleColorMap() can be
1354
  // made to work in-place.
1355
0
  uint8_t* const tmp_row = (uint8_t*)WebPSafeMalloc(width, sizeof(*tmp_row));
1356
0
  int x, y;
1357
1358
0
  if (tmp_row == NULL) {
1359
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1360
0
  }
1361
1362
0
  if (palette_size < APPLY_PALETTE_GREEDY_MAX) {
1363
0
    APPLY_PALETTE_FOR(SearchColorGreedy(palette, palette_size, pix));
1364
0
  } else {
1365
0
    int i, j;
1366
0
    uint16_t buffer[PALETTE_INV_SIZE];
1367
0
    uint32_t (*const hash_functions[])(uint32_t) = {
1368
0
        ApplyPaletteHash0, ApplyPaletteHash1, ApplyPaletteHash2
1369
0
    };
1370
1371
    // Try to find a perfect hash function able to go from a color to an index
1372
    // within 1 << PALETTE_INV_SIZE_BITS in order to build a hash map to go
1373
    // from color to index in palette.
1374
0
    for (i = 0; i < 3; ++i) {
1375
0
      int use_LUT = 1;
1376
      // Set each element in buffer to max uint16_t.
1377
0
      memset(buffer, 0xff, sizeof(buffer));
1378
0
      for (j = 0; j < palette_size; ++j) {
1379
0
        const uint32_t ind = hash_functions[i](palette[j]);
1380
0
        if (buffer[ind] != 0xffffu) {
1381
0
          use_LUT = 0;
1382
0
          break;
1383
0
        } else {
1384
0
          buffer[ind] = j;
1385
0
        }
1386
0
      }
1387
0
      if (use_LUT) break;
1388
0
    }
1389
1390
0
    if (i == 0) {
1391
0
      APPLY_PALETTE_FOR(buffer[ApplyPaletteHash0(pix)]);
1392
0
    } else if (i == 1) {
1393
0
      APPLY_PALETTE_FOR(buffer[ApplyPaletteHash1(pix)]);
1394
0
    } else if (i == 2) {
1395
0
      APPLY_PALETTE_FOR(buffer[ApplyPaletteHash2(pix)]);
1396
0
    } else {
1397
0
      uint32_t idx_map[MAX_PALETTE_SIZE];
1398
0
      uint32_t palette_sorted[MAX_PALETTE_SIZE];
1399
0
      PrepareMapToPalette(palette, palette_size, palette_sorted, idx_map);
1400
0
      APPLY_PALETTE_FOR(
1401
0
          idx_map[SearchColorNoIdx(palette_sorted, pix, palette_size)]);
1402
0
    }
1403
0
  }
1404
0
  WebPSafeFree(tmp_row);
1405
0
  return 1;
1406
0
}
1407
#undef APPLY_PALETTE_FOR
1408
#undef PALETTE_INV_SIZE_BITS
1409
#undef PALETTE_INV_SIZE
1410
#undef APPLY_PALETTE_GREEDY_MAX
1411
1412
// Note: Expects "enc->palette" to be set properly.
1413
0
static int MapImageFromPalette(VP8LEncoder* const enc) {
1414
0
  const WebPPicture* const pic = enc->pic;
1415
0
  const int width = pic->width;
1416
0
  const int height = pic->height;
1417
0
  const uint32_t* const palette = enc->palette;
1418
0
  const int palette_size = enc->palette_size;
1419
0
  int xbits;
1420
1421
  // Replace each input pixel by corresponding palette index.
1422
  // This is done line by line.
1423
0
  if (palette_size <= 4) {
1424
0
    xbits = (palette_size <= 2) ? 3 : 2;
1425
0
  } else {
1426
0
    xbits = (palette_size <= 16) ? 1 : 0;
1427
0
  }
1428
1429
0
  if (!AllocateTransformBuffer(enc, VP8LSubSampleSize(width, xbits), height)) {
1430
0
    return 0;
1431
0
  }
1432
0
  if (!ApplyPalette(pic->argb, pic->argb_stride, enc->argb,
1433
0
                    enc->current_width, palette, palette_size, width, height,
1434
0
                    xbits, pic)) {
1435
0
    return 0;
1436
0
  }
1437
0
  enc->argb_content = kEncoderPalette;
1438
0
  return 1;
1439
0
}
1440
1441
// Save palette[] to bitstream.
1442
static int EncodePalette(VP8LBitWriter* const bw, int low_effort,
1443
                         VP8LEncoder* const enc, int percent_range,
1444
0
                         int* const percent) {
1445
0
  int i;
1446
0
  uint32_t tmp_palette[MAX_PALETTE_SIZE];
1447
0
  const int palette_size = enc->palette_size;
1448
0
  const uint32_t* const palette = enc->palette;
1449
  // If the last element is 0, do not store it and count on automatic palette
1450
  // 0-filling. This can only happen if there is no pixel packing, hence if
1451
  // there are strictly more than 16 colors (after 0 is removed).
1452
0
  const uint32_t encoded_palette_size =
1453
0
      (enc->palette[palette_size - 1] == 0 && palette_size > 17)
1454
0
          ? palette_size - 1
1455
0
          : palette_size;
1456
0
  VP8LPutBits(bw, TRANSFORM_PRESENT, 1);
1457
0
  VP8LPutBits(bw, COLOR_INDEXING_TRANSFORM, 2);
1458
0
  assert(palette_size >= 1 && palette_size <= MAX_PALETTE_SIZE);
1459
0
  VP8LPutBits(bw, encoded_palette_size - 1, 8);
1460
0
  for (i = encoded_palette_size - 1; i >= 1; --i) {
1461
0
    tmp_palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
1462
0
  }
1463
0
  tmp_palette[0] = palette[0];
1464
0
  return EncodeImageNoHuffman(
1465
0
      bw, tmp_palette, &enc->hash_chain, &enc->refs[0], encoded_palette_size,
1466
0
      1, /*quality=*/20, low_effort, enc->pic, percent_range, percent);
1467
0
}
1468
1469
// -----------------------------------------------------------------------------
1470
// VP8LEncoder
1471
1472
static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
1473
0
                                   const WebPPicture* const picture) {
1474
0
  VP8LEncoder* const enc = (VP8LEncoder*)WebPSafeCalloc(1ULL, sizeof(*enc));
1475
0
  if (enc == NULL) {
1476
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1477
0
    return NULL;
1478
0
  }
1479
0
  enc->config = config;
1480
0
  enc->pic = picture;
1481
0
  enc->argb_content = kEncoderNone;
1482
1483
0
  VP8LEncDspInit();
1484
1485
0
  return enc;
1486
0
}
1487
1488
0
static void VP8LEncoderDelete(VP8LEncoder* enc) {
1489
0
  if (enc != NULL) {
1490
0
    int i;
1491
0
    VP8LHashChainClear(&enc->hash_chain);
1492
0
    for (i = 0; i < 4; ++i) VP8LBackwardRefsClear(&enc->refs[i]);
1493
0
    ClearTransformBuffer(enc);
1494
0
    WebPSafeFree(enc);
1495
0
  }
1496
0
}
1497
1498
// -----------------------------------------------------------------------------
1499
// Main call
1500
1501
typedef struct {
1502
  const WebPConfig* config;
1503
  const WebPPicture* picture;
1504
  VP8LBitWriter* bw;
1505
  VP8LEncoder* enc;
1506
  CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX];
1507
  int num_crunch_configs;
1508
  int red_and_blue_always_zero;
1509
  WebPAuxStats* stats;
1510
} StreamEncodeContext;
1511
1512
0
static int EncodeStreamHook(void* input, void* data2) {
1513
0
  StreamEncodeContext* const params = (StreamEncodeContext*)input;
1514
0
  const WebPConfig* const config = params->config;
1515
0
  const WebPPicture* const picture = params->picture;
1516
0
  VP8LBitWriter* const bw = params->bw;
1517
0
  VP8LEncoder* const enc = params->enc;
1518
0
  const CrunchConfig* const crunch_configs = params->crunch_configs;
1519
0
  const int num_crunch_configs = params->num_crunch_configs;
1520
0
  const int red_and_blue_always_zero = params->red_and_blue_always_zero;
1521
0
#if !defined(WEBP_DISABLE_STATS)
1522
0
  WebPAuxStats* const stats = params->stats;
1523
0
#endif
1524
0
  const int quality = (int)config->quality;
1525
0
  const int low_effort = (config->method == 0);
1526
0
#if (WEBP_NEAR_LOSSLESS == 1)
1527
0
  const int width = picture->width;
1528
0
#endif
1529
0
  const int height = picture->height;
1530
0
  const size_t byte_position = VP8LBitWriterNumBytes(bw);
1531
0
  int percent = 2;  // for WebPProgressHook
1532
0
#if (WEBP_NEAR_LOSSLESS == 1)
1533
0
  int use_near_lossless = 0;
1534
0
#endif
1535
0
  int hdr_size = 0;
1536
0
  int data_size = 0;
1537
0
  int idx;
1538
0
  size_t best_size = ~(size_t)0;
1539
0
  VP8LBitWriter bw_init = *bw, bw_best;
1540
0
  (void)data2;
1541
1542
0
  if (!VP8LBitWriterInit(&bw_best, 0) ||
1543
0
      (num_crunch_configs > 1 && !VP8LBitWriterClone(bw, &bw_best))) {
1544
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1545
0
    goto Error;
1546
0
  }
1547
1548
0
  for (idx = 0; idx < num_crunch_configs; ++idx) {
1549
0
    const int entropy_idx = crunch_configs[idx].entropy_idx;
1550
0
    int remaining_percent = 97 / num_crunch_configs, percent_range;
1551
0
    int predictor_transform_bits = 0, cross_color_transform_bits = 0;
1552
0
    enc->use_palette =
1553
0
        (entropy_idx == kPalette) || (entropy_idx == kPaletteAndSpatial);
1554
0
    enc->use_subtract_green =
1555
0
        (entropy_idx == kSubGreen) || (entropy_idx == kSpatialSubGreen);
1556
0
    enc->use_predict = (entropy_idx == kSpatial) ||
1557
0
                       (entropy_idx == kSpatialSubGreen) ||
1558
0
                       (entropy_idx == kPaletteAndSpatial);
1559
    // When using a palette, R/B==0, hence no need to test for cross-color.
1560
0
    if (low_effort || enc->use_palette) {
1561
0
      enc->use_cross_color = 0;
1562
0
    } else {
1563
0
      enc->use_cross_color = red_and_blue_always_zero ? 0 : enc->use_predict;
1564
0
    }
1565
    // Reset any parameter in the encoder that is set in the previous iteration.
1566
0
    enc->cache_bits = 0;
1567
0
    VP8LBackwardRefsClear(&enc->refs[0]);
1568
0
    VP8LBackwardRefsClear(&enc->refs[1]);
1569
1570
0
#if (WEBP_NEAR_LOSSLESS == 1)
1571
    // Apply near-lossless preprocessing.
1572
0
    use_near_lossless = (config->near_lossless < 100) && !enc->use_palette &&
1573
0
                        !enc->use_predict;
1574
0
    if (use_near_lossless) {
1575
0
      if (!AllocateTransformBuffer(enc, width, height)) goto Error;
1576
0
      if ((enc->argb_content != kEncoderNearLossless) &&
1577
0
          !VP8ApplyNearLossless(picture, config->near_lossless, enc->argb)) {
1578
0
        WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1579
0
        goto Error;
1580
0
      }
1581
0
      enc->argb_content = kEncoderNearLossless;
1582
0
    } else {
1583
0
      enc->argb_content = kEncoderNone;
1584
0
    }
1585
#else
1586
    enc->argb_content = kEncoderNone;
1587
#endif
1588
1589
    // Encode palette
1590
0
    if (enc->use_palette) {
1591
0
      if (!PaletteSort(crunch_configs[idx].palette_sorting_type, enc->pic,
1592
0
                       enc->palette_sorted, enc->palette_size,
1593
0
                       enc->palette)) {
1594
0
        WebPEncodingSetError(enc->pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
1595
0
        goto Error;
1596
0
      }
1597
0
      percent_range = remaining_percent / 4;
1598
0
      if (!EncodePalette(bw, low_effort, enc, percent_range, &percent)) {
1599
0
        goto Error;
1600
0
      }
1601
0
      remaining_percent -= percent_range;
1602
0
      if (!MapImageFromPalette(enc)) goto Error;
1603
      // If using a color cache, do not have it bigger than the number of
1604
      // colors.
1605
0
      if (enc->palette_size < (1 << MAX_COLOR_CACHE_BITS)) {
1606
0
        enc->cache_bits = BitsLog2Floor(enc->palette_size) + 1;
1607
0
      }
1608
0
    }
1609
    // In case image is not packed.
1610
0
    if (enc->argb_content != kEncoderNearLossless &&
1611
0
        enc->argb_content != kEncoderPalette) {
1612
0
      if (!MakeInputImageCopy(enc)) goto Error;
1613
0
    }
1614
1615
    // -------------------------------------------------------------------------
1616
    // Apply transforms and write transform data.
1617
1618
0
    if (enc->use_subtract_green) {
1619
0
      ApplySubtractGreen(enc, enc->current_width, height, bw);
1620
0
    }
1621
1622
0
    if (enc->use_predict) {
1623
0
      percent_range = remaining_percent / 3;
1624
0
      if (!ApplyPredictFilter(enc, enc->current_width, height, quality,
1625
0
                              low_effort, enc->use_subtract_green, bw,
1626
0
                              percent_range, &percent,
1627
0
                              &predictor_transform_bits)) {
1628
0
        goto Error;
1629
0
      }
1630
0
      remaining_percent -= percent_range;
1631
0
    }
1632
1633
0
    if (enc->use_cross_color) {
1634
0
      percent_range = remaining_percent / 2;
1635
0
      if (!ApplyCrossColorFilter(enc, enc->current_width, height, quality,
1636
0
                                 low_effort, bw, percent_range, &percent,
1637
0
                                 &cross_color_transform_bits)) {
1638
0
        goto Error;
1639
0
      }
1640
0
      remaining_percent -= percent_range;
1641
0
    }
1642
1643
0
    VP8LPutBits(bw, !TRANSFORM_PRESENT, 1);  // No more transforms.
1644
1645
    // -------------------------------------------------------------------------
1646
    // Encode and write the transformed image.
1647
0
    if (!EncodeImageInternal(
1648
0
            bw, enc->argb, &enc->hash_chain, enc->refs, enc->current_width,
1649
0
            height, quality, low_effort, &crunch_configs[idx],
1650
0
            &enc->cache_bits, enc->histo_bits, byte_position, &hdr_size,
1651
0
            &data_size, picture, remaining_percent, &percent)) {
1652
0
      goto Error;
1653
0
    }
1654
1655
    // If we are better than what we already have.
1656
0
    if (VP8LBitWriterNumBytes(bw) < best_size) {
1657
0
      best_size = VP8LBitWriterNumBytes(bw);
1658
      // Store the BitWriter.
1659
0
      VP8LBitWriterSwap(bw, &bw_best);
1660
0
#if !defined(WEBP_DISABLE_STATS)
1661
      // Update the stats.
1662
0
      if (stats != NULL) {
1663
0
        stats->lossless_features = 0;
1664
0
        if (enc->use_predict) stats->lossless_features |= 1;
1665
0
        if (enc->use_cross_color) stats->lossless_features |= 2;
1666
0
        if (enc->use_subtract_green) stats->lossless_features |= 4;
1667
0
        if (enc->use_palette) stats->lossless_features |= 8;
1668
0
        stats->histogram_bits = enc->histo_bits;
1669
0
        stats->transform_bits = predictor_transform_bits;
1670
0
        stats->cross_color_transform_bits = cross_color_transform_bits;
1671
0
        stats->cache_bits = enc->cache_bits;
1672
0
        stats->palette_size = enc->palette_size;
1673
0
        stats->lossless_size = (int)(best_size - byte_position);
1674
0
        stats->lossless_hdr_size = hdr_size;
1675
0
        stats->lossless_data_size = data_size;
1676
0
      }
1677
0
#endif
1678
0
    }
1679
    // Reset the bit writer for the following iteration if any.
1680
0
    if (num_crunch_configs > 1) VP8LBitWriterReset(&bw_init, bw);
1681
0
  }
1682
0
  VP8LBitWriterSwap(&bw_best, bw);
1683
1684
0
 Error:
1685
0
  VP8LBitWriterWipeOut(&bw_best);
1686
  // The hook should return false in case of error.
1687
0
  return (params->picture->error_code == VP8_ENC_OK);
1688
0
}
1689
1690
int VP8LEncodeStream(const WebPConfig* const config,
1691
                     const WebPPicture* const picture,
1692
0
                     VP8LBitWriter* const bw_main) {
1693
0
  VP8LEncoder* const enc_main = VP8LEncoderNew(config, picture);
1694
0
  VP8LEncoder* enc_side = NULL;
1695
0
  CrunchConfig crunch_configs[CRUNCH_CONFIGS_MAX];
1696
0
  int num_crunch_configs_main, num_crunch_configs_side = 0;
1697
0
  int idx;
1698
0
  int red_and_blue_always_zero = 0;
1699
0
  WebPWorker worker_main, worker_side;
1700
0
  StreamEncodeContext params_main, params_side;
1701
  // The main thread uses picture->stats, the side thread uses stats_side.
1702
0
  WebPAuxStats stats_side;
1703
0
  VP8LBitWriter bw_side;
1704
0
  WebPPicture picture_side;
1705
0
  const WebPWorkerInterface* const worker_interface = WebPGetWorkerInterface();
1706
0
  int ok_main;
1707
1708
0
  if (enc_main == NULL || !VP8LBitWriterInit(&bw_side, 0)) {
1709
0
    VP8LEncoderDelete(enc_main);
1710
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1711
0
  }
1712
1713
  // Avoid "garbage value" error from Clang's static analysis tool.
1714
0
  if (!WebPPictureInit(&picture_side)) {
1715
0
    goto Error;
1716
0
  }
1717
1718
  // Analyze image (entropy, num_palettes etc)
1719
0
  if (!EncoderAnalyze(enc_main, crunch_configs, &num_crunch_configs_main,
1720
0
                      &red_and_blue_always_zero) ||
1721
0
      !EncoderInit(enc_main)) {
1722
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1723
0
    goto Error;
1724
0
  }
1725
1726
  // Split the configs between the main and side threads (if any).
1727
0
  if (config->thread_level > 0) {
1728
0
    num_crunch_configs_side = num_crunch_configs_main / 2;
1729
0
    for (idx = 0; idx < num_crunch_configs_side; ++idx) {
1730
0
      params_side.crunch_configs[idx] =
1731
0
          crunch_configs[num_crunch_configs_main - num_crunch_configs_side +
1732
0
                         idx];
1733
0
    }
1734
0
    params_side.num_crunch_configs = num_crunch_configs_side;
1735
0
  }
1736
0
  num_crunch_configs_main -= num_crunch_configs_side;
1737
0
  for (idx = 0; idx < num_crunch_configs_main; ++idx) {
1738
0
    params_main.crunch_configs[idx] = crunch_configs[idx];
1739
0
  }
1740
0
  params_main.num_crunch_configs = num_crunch_configs_main;
1741
1742
  // Fill in the parameters for the thread workers.
1743
0
  {
1744
0
    const int params_size = (num_crunch_configs_side > 0) ? 2 : 1;
1745
0
    for (idx = 0; idx < params_size; ++idx) {
1746
      // Create the parameters for each worker.
1747
0
      WebPWorker* const worker = (idx == 0) ? &worker_main : &worker_side;
1748
0
      StreamEncodeContext* const param =
1749
0
          (idx == 0) ? &params_main : &params_side;
1750
0
      param->config = config;
1751
0
      param->red_and_blue_always_zero = red_and_blue_always_zero;
1752
0
      if (idx == 0) {
1753
0
        param->picture = picture;
1754
0
        param->stats = picture->stats;
1755
0
        param->bw = bw_main;
1756
0
        param->enc = enc_main;
1757
0
      } else {
1758
        // Create a side picture (error_code is not thread-safe).
1759
0
        if (!WebPPictureView(picture, /*left=*/0, /*top=*/0, picture->width,
1760
0
                             picture->height, &picture_side)) {
1761
0
          assert(0);
1762
0
        }
1763
0
        picture_side.progress_hook = NULL;  // Progress hook is not thread-safe.
1764
0
        param->picture = &picture_side;  // No need to free a view afterwards.
1765
0
        param->stats = (picture->stats == NULL) ? NULL : &stats_side;
1766
        // Create a side bit writer.
1767
0
        if (!VP8LBitWriterClone(bw_main, &bw_side)) {
1768
0
          WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1769
0
          goto Error;
1770
0
        }
1771
0
        param->bw = &bw_side;
1772
        // Create a side encoder.
1773
0
        enc_side = VP8LEncoderNew(config, &picture_side);
1774
0
        if (enc_side == NULL || !EncoderInit(enc_side)) {
1775
0
          WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1776
0
          goto Error;
1777
0
        }
1778
        // Copy the values that were computed for the main encoder.
1779
0
        enc_side->histo_bits = enc_main->histo_bits;
1780
0
        enc_side->predictor_transform_bits =
1781
0
            enc_main->predictor_transform_bits;
1782
0
        enc_side->cross_color_transform_bits =
1783
0
            enc_main->cross_color_transform_bits;
1784
0
        enc_side->palette_size = enc_main->palette_size;
1785
0
        memcpy(enc_side->palette, enc_main->palette,
1786
0
               sizeof(enc_main->palette));
1787
0
        memcpy(enc_side->palette_sorted, enc_main->palette_sorted,
1788
0
               sizeof(enc_main->palette_sorted));
1789
0
        param->enc = enc_side;
1790
0
      }
1791
      // Create the workers.
1792
0
      worker_interface->Init(worker);
1793
0
      worker->data1 = param;
1794
0
      worker->data2 = NULL;
1795
0
      worker->hook = EncodeStreamHook;
1796
0
    }
1797
0
  }
1798
1799
  // Start the second thread if needed.
1800
0
  if (num_crunch_configs_side != 0) {
1801
0
    if (!worker_interface->Reset(&worker_side)) {
1802
0
      WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1803
0
      goto Error;
1804
0
    }
1805
0
#if !defined(WEBP_DISABLE_STATS)
1806
    // This line is here and not in the param initialization above to remove a
1807
    // Clang static analyzer warning.
1808
0
    if (picture->stats != NULL) {
1809
0
      memcpy(&stats_side, picture->stats, sizeof(stats_side));
1810
0
    }
1811
0
#endif
1812
0
    worker_interface->Launch(&worker_side);
1813
0
  }
1814
  // Execute the main thread.
1815
0
  worker_interface->Execute(&worker_main);
1816
0
  ok_main = worker_interface->Sync(&worker_main);
1817
0
  worker_interface->End(&worker_main);
1818
0
  if (num_crunch_configs_side != 0) {
1819
    // Wait for the second thread.
1820
0
    const int ok_side = worker_interface->Sync(&worker_side);
1821
0
    worker_interface->End(&worker_side);
1822
0
    if (!ok_main || !ok_side) {
1823
0
      if (picture->error_code == VP8_ENC_OK) {
1824
0
        assert(picture_side.error_code != VP8_ENC_OK);
1825
0
        WebPEncodingSetError(picture, picture_side.error_code);
1826
0
      }
1827
0
      goto Error;
1828
0
    }
1829
0
    if (VP8LBitWriterNumBytes(&bw_side) < VP8LBitWriterNumBytes(bw_main)) {
1830
0
      VP8LBitWriterSwap(bw_main, &bw_side);
1831
0
#if !defined(WEBP_DISABLE_STATS)
1832
0
      if (picture->stats != NULL) {
1833
0
        memcpy(picture->stats, &stats_side, sizeof(*picture->stats));
1834
0
      }
1835
0
#endif
1836
0
    }
1837
0
  }
1838
1839
0
 Error:
1840
0
  VP8LBitWriterWipeOut(&bw_side);
1841
0
  VP8LEncoderDelete(enc_main);
1842
0
  VP8LEncoderDelete(enc_side);
1843
0
  return (picture->error_code == VP8_ENC_OK);
1844
0
}
1845
1846
#undef CRUNCH_CONFIGS_MAX
1847
#undef CRUNCH_SUBCONFIGS_MAX
1848
1849
int VP8LEncodeImage(const WebPConfig* const config,
1850
0
                    const WebPPicture* const picture) {
1851
0
  int width, height;
1852
0
  int has_alpha;
1853
0
  size_t coded_size;
1854
0
  int percent = 0;
1855
0
  int initial_size;
1856
0
  VP8LBitWriter bw;
1857
1858
0
  if (picture == NULL) return 0;
1859
1860
0
  if (config == NULL || picture->argb == NULL) {
1861
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
1862
0
  }
1863
1864
0
  width = picture->width;
1865
0
  height = picture->height;
1866
  // Initialize BitWriter with size corresponding to 16 bpp to photo images and
1867
  // 8 bpp for graphical images.
1868
0
  initial_size = (config->image_hint == WEBP_HINT_GRAPH) ?
1869
0
      width * height : width * height * 2;
1870
0
  if (!VP8LBitWriterInit(&bw, initial_size)) {
1871
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1872
0
    goto Error;
1873
0
  }
1874
1875
0
  if (!WebPReportProgress(picture, 1, &percent)) {
1876
0
 UserAbort:
1877
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_USER_ABORT);
1878
0
    goto Error;
1879
0
  }
1880
  // Reset stats (for pure lossless coding)
1881
0
  if (picture->stats != NULL) {
1882
0
    WebPAuxStats* const stats = picture->stats;
1883
0
    memset(stats, 0, sizeof(*stats));
1884
0
    stats->PSNR[0] = 99.f;
1885
0
    stats->PSNR[1] = 99.f;
1886
0
    stats->PSNR[2] = 99.f;
1887
0
    stats->PSNR[3] = 99.f;
1888
0
    stats->PSNR[4] = 99.f;
1889
0
  }
1890
1891
  // Write image size.
1892
0
  if (!WriteImageSize(picture, &bw)) {
1893
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1894
0
    goto Error;
1895
0
  }
1896
1897
0
  has_alpha = WebPPictureHasTransparency(picture);
1898
  // Write the non-trivial Alpha flag and lossless version.
1899
0
  if (!WriteRealAlphaAndVersion(&bw, has_alpha)) {
1900
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1901
0
    goto Error;
1902
0
  }
1903
1904
0
  if (!WebPReportProgress(picture, 2, &percent)) goto UserAbort;
1905
1906
  // Encode main image stream.
1907
0
  if (!VP8LEncodeStream(config, picture, &bw)) goto Error;
1908
1909
0
  if (!WebPReportProgress(picture, 99, &percent)) goto UserAbort;
1910
1911
  // Finish the RIFF chunk.
1912
0
  if (!WriteImage(picture, &bw, &coded_size)) goto Error;
1913
1914
0
  if (!WebPReportProgress(picture, 100, &percent)) goto UserAbort;
1915
1916
0
#if !defined(WEBP_DISABLE_STATS)
1917
  // Save size.
1918
0
  if (picture->stats != NULL) {
1919
0
    picture->stats->coded_size += (int)coded_size;
1920
0
    picture->stats->lossless_size = (int)coded_size;
1921
0
  }
1922
0
#endif
1923
1924
0
  if (picture->extra_info != NULL) {
1925
0
    const int mb_w = (width + 15) >> 4;
1926
0
    const int mb_h = (height + 15) >> 4;
1927
0
    memset(picture->extra_info, 0, mb_w * mb_h * sizeof(*picture->extra_info));
1928
0
  }
1929
1930
0
 Error:
1931
0
  if (bw.error) {
1932
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
1933
0
  }
1934
0
  VP8LBitWriterWipeOut(&bw);
1935
0
  return (picture->error_code == VP8_ENC_OK);
1936
0
}
1937
1938
//------------------------------------------------------------------------------