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