xref: /aosp_15_r20/external/webp/src/enc/predictor_enc.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2016 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 // Image transform methods for lossless encoder.
11*b2055c35SXin Li //
12*b2055c35SXin Li // Authors: Vikas Arora ([email protected])
13*b2055c35SXin Li //          Jyrki Alakuijala ([email protected])
14*b2055c35SXin Li //          Urvang Joshi ([email protected])
15*b2055c35SXin Li //          Vincent Rabaud ([email protected])
16*b2055c35SXin Li 
17*b2055c35SXin Li #include "src/dsp/lossless.h"
18*b2055c35SXin Li #include "src/dsp/lossless_common.h"
19*b2055c35SXin Li #include "src/enc/vp8i_enc.h"
20*b2055c35SXin Li #include "src/enc/vp8li_enc.h"
21*b2055c35SXin Li 
22*b2055c35SXin Li #define MAX_DIFF_COST (1e30f)
23*b2055c35SXin Li 
24*b2055c35SXin Li static const float kSpatialPredictorBias = 15.f;
25*b2055c35SXin Li static const int kPredLowEffort = 11;
26*b2055c35SXin Li static const uint32_t kMaskAlpha = 0xff000000;
27*b2055c35SXin Li 
28*b2055c35SXin Li // Mostly used to reduce code size + readability
GetMin(int a,int b)29*b2055c35SXin Li static WEBP_INLINE int GetMin(int a, int b) { return (a > b) ? b : a; }
30*b2055c35SXin Li 
31*b2055c35SXin Li //------------------------------------------------------------------------------
32*b2055c35SXin Li // Methods to calculate Entropy (Shannon).
33*b2055c35SXin Li 
PredictionCostSpatial(const int counts[256],int weight_0,float exp_val)34*b2055c35SXin Li static float PredictionCostSpatial(const int counts[256], int weight_0,
35*b2055c35SXin Li                                    float exp_val) {
36*b2055c35SXin Li   const int significant_symbols = 256 >> 4;
37*b2055c35SXin Li   const float exp_decay_factor = 0.6f;
38*b2055c35SXin Li   float bits = (float)weight_0 * counts[0];
39*b2055c35SXin Li   int i;
40*b2055c35SXin Li   for (i = 1; i < significant_symbols; ++i) {
41*b2055c35SXin Li     bits += exp_val * (counts[i] + counts[256 - i]);
42*b2055c35SXin Li     exp_val *= exp_decay_factor;
43*b2055c35SXin Li   }
44*b2055c35SXin Li   return (float)(-0.1 * bits);
45*b2055c35SXin Li }
46*b2055c35SXin Li 
PredictionCostSpatialHistogram(const int accumulated[4][256],const int tile[4][256])47*b2055c35SXin Li static float PredictionCostSpatialHistogram(const int accumulated[4][256],
48*b2055c35SXin Li                                             const int tile[4][256]) {
49*b2055c35SXin Li   int i;
50*b2055c35SXin Li   float retval = 0.f;
51*b2055c35SXin Li   for (i = 0; i < 4; ++i) {
52*b2055c35SXin Li     const float kExpValue = 0.94f;
53*b2055c35SXin Li     retval += PredictionCostSpatial(tile[i], 1, kExpValue);
54*b2055c35SXin Li     retval += VP8LCombinedShannonEntropy(tile[i], accumulated[i]);
55*b2055c35SXin Li   }
56*b2055c35SXin Li   return (float)retval;
57*b2055c35SXin Li }
58*b2055c35SXin Li 
UpdateHisto(int histo_argb[4][256],uint32_t argb)59*b2055c35SXin Li static WEBP_INLINE void UpdateHisto(int histo_argb[4][256], uint32_t argb) {
60*b2055c35SXin Li   ++histo_argb[0][argb >> 24];
61*b2055c35SXin Li   ++histo_argb[1][(argb >> 16) & 0xff];
62*b2055c35SXin Li   ++histo_argb[2][(argb >> 8) & 0xff];
63*b2055c35SXin Li   ++histo_argb[3][argb & 0xff];
64*b2055c35SXin Li }
65*b2055c35SXin Li 
66*b2055c35SXin Li //------------------------------------------------------------------------------
67*b2055c35SXin Li // Spatial transform functions.
68*b2055c35SXin Li 
PredictBatch(int mode,int x_start,int y,int num_pixels,const uint32_t * current,const uint32_t * upper,uint32_t * out)69*b2055c35SXin Li static WEBP_INLINE void PredictBatch(int mode, int x_start, int y,
70*b2055c35SXin Li                                      int num_pixels, const uint32_t* current,
71*b2055c35SXin Li                                      const uint32_t* upper, uint32_t* out) {
72*b2055c35SXin Li   if (x_start == 0) {
73*b2055c35SXin Li     if (y == 0) {
74*b2055c35SXin Li       // ARGB_BLACK.
75*b2055c35SXin Li       VP8LPredictorsSub[0](current, NULL, 1, out);
76*b2055c35SXin Li     } else {
77*b2055c35SXin Li       // Top one.
78*b2055c35SXin Li       VP8LPredictorsSub[2](current, upper, 1, out);
79*b2055c35SXin Li     }
80*b2055c35SXin Li     ++x_start;
81*b2055c35SXin Li     ++out;
82*b2055c35SXin Li     --num_pixels;
83*b2055c35SXin Li   }
84*b2055c35SXin Li   if (y == 0) {
85*b2055c35SXin Li     // Left one.
86*b2055c35SXin Li     VP8LPredictorsSub[1](current + x_start, NULL, num_pixels, out);
87*b2055c35SXin Li   } else {
88*b2055c35SXin Li     VP8LPredictorsSub[mode](current + x_start, upper + x_start, num_pixels,
89*b2055c35SXin Li                             out);
90*b2055c35SXin Li   }
91*b2055c35SXin Li }
92*b2055c35SXin Li 
93*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
GetMax(int a,int b)94*b2055c35SXin Li static WEBP_INLINE int GetMax(int a, int b) { return (a < b) ? b : a; }
95*b2055c35SXin Li 
MaxDiffBetweenPixels(uint32_t p1,uint32_t p2)96*b2055c35SXin Li static int MaxDiffBetweenPixels(uint32_t p1, uint32_t p2) {
97*b2055c35SXin Li   const int diff_a = abs((int)(p1 >> 24) - (int)(p2 >> 24));
98*b2055c35SXin Li   const int diff_r = abs((int)((p1 >> 16) & 0xff) - (int)((p2 >> 16) & 0xff));
99*b2055c35SXin Li   const int diff_g = abs((int)((p1 >> 8) & 0xff) - (int)((p2 >> 8) & 0xff));
100*b2055c35SXin Li   const int diff_b = abs((int)(p1 & 0xff) - (int)(p2 & 0xff));
101*b2055c35SXin Li   return GetMax(GetMax(diff_a, diff_r), GetMax(diff_g, diff_b));
102*b2055c35SXin Li }
103*b2055c35SXin Li 
MaxDiffAroundPixel(uint32_t current,uint32_t up,uint32_t down,uint32_t left,uint32_t right)104*b2055c35SXin Li static int MaxDiffAroundPixel(uint32_t current, uint32_t up, uint32_t down,
105*b2055c35SXin Li                               uint32_t left, uint32_t right) {
106*b2055c35SXin Li   const int diff_up = MaxDiffBetweenPixels(current, up);
107*b2055c35SXin Li   const int diff_down = MaxDiffBetweenPixels(current, down);
108*b2055c35SXin Li   const int diff_left = MaxDiffBetweenPixels(current, left);
109*b2055c35SXin Li   const int diff_right = MaxDiffBetweenPixels(current, right);
110*b2055c35SXin Li   return GetMax(GetMax(diff_up, diff_down), GetMax(diff_left, diff_right));
111*b2055c35SXin Li }
112*b2055c35SXin Li 
AddGreenToBlueAndRed(uint32_t argb)113*b2055c35SXin Li static uint32_t AddGreenToBlueAndRed(uint32_t argb) {
114*b2055c35SXin Li   const uint32_t green = (argb >> 8) & 0xff;
115*b2055c35SXin Li   uint32_t red_blue = argb & 0x00ff00ffu;
116*b2055c35SXin Li   red_blue += (green << 16) | green;
117*b2055c35SXin Li   red_blue &= 0x00ff00ffu;
118*b2055c35SXin Li   return (argb & 0xff00ff00u) | red_blue;
119*b2055c35SXin Li }
120*b2055c35SXin Li 
MaxDiffsForRow(int width,int stride,const uint32_t * const argb,uint8_t * const max_diffs,int used_subtract_green)121*b2055c35SXin Li static void MaxDiffsForRow(int width, int stride, const uint32_t* const argb,
122*b2055c35SXin Li                            uint8_t* const max_diffs, int used_subtract_green) {
123*b2055c35SXin Li   uint32_t current, up, down, left, right;
124*b2055c35SXin Li   int x;
125*b2055c35SXin Li   if (width <= 2) return;
126*b2055c35SXin Li   current = argb[0];
127*b2055c35SXin Li   right = argb[1];
128*b2055c35SXin Li   if (used_subtract_green) {
129*b2055c35SXin Li     current = AddGreenToBlueAndRed(current);
130*b2055c35SXin Li     right = AddGreenToBlueAndRed(right);
131*b2055c35SXin Li   }
132*b2055c35SXin Li   // max_diffs[0] and max_diffs[width - 1] are never used.
133*b2055c35SXin Li   for (x = 1; x < width - 1; ++x) {
134*b2055c35SXin Li     up = argb[-stride + x];
135*b2055c35SXin Li     down = argb[stride + x];
136*b2055c35SXin Li     left = current;
137*b2055c35SXin Li     current = right;
138*b2055c35SXin Li     right = argb[x + 1];
139*b2055c35SXin Li     if (used_subtract_green) {
140*b2055c35SXin Li       up = AddGreenToBlueAndRed(up);
141*b2055c35SXin Li       down = AddGreenToBlueAndRed(down);
142*b2055c35SXin Li       right = AddGreenToBlueAndRed(right);
143*b2055c35SXin Li     }
144*b2055c35SXin Li     max_diffs[x] = MaxDiffAroundPixel(current, up, down, left, right);
145*b2055c35SXin Li   }
146*b2055c35SXin Li }
147*b2055c35SXin Li 
148*b2055c35SXin Li // Quantize the difference between the actual component value and its prediction
149*b2055c35SXin Li // to a multiple of quantization, working modulo 256, taking care not to cross
150*b2055c35SXin Li // a boundary (inclusive upper limit).
NearLosslessComponent(uint8_t value,uint8_t predict,uint8_t boundary,int quantization)151*b2055c35SXin Li static uint8_t NearLosslessComponent(uint8_t value, uint8_t predict,
152*b2055c35SXin Li                                      uint8_t boundary, int quantization) {
153*b2055c35SXin Li   const int residual = (value - predict) & 0xff;
154*b2055c35SXin Li   const int boundary_residual = (boundary - predict) & 0xff;
155*b2055c35SXin Li   const int lower = residual & ~(quantization - 1);
156*b2055c35SXin Li   const int upper = lower + quantization;
157*b2055c35SXin Li   // Resolve ties towards a value closer to the prediction (i.e. towards lower
158*b2055c35SXin Li   // if value comes after prediction and towards upper otherwise).
159*b2055c35SXin Li   const int bias = ((boundary - value) & 0xff) < boundary_residual;
160*b2055c35SXin Li   if (residual - lower < upper - residual + bias) {
161*b2055c35SXin Li     // lower is closer to residual than upper.
162*b2055c35SXin Li     if (residual > boundary_residual && lower <= boundary_residual) {
163*b2055c35SXin Li       // Halve quantization step to avoid crossing boundary. This midpoint is
164*b2055c35SXin Li       // on the same side of boundary as residual because midpoint >= residual
165*b2055c35SXin Li       // (since lower is closer than upper) and residual is above the boundary.
166*b2055c35SXin Li       return lower + (quantization >> 1);
167*b2055c35SXin Li     }
168*b2055c35SXin Li     return lower;
169*b2055c35SXin Li   } else {
170*b2055c35SXin Li     // upper is closer to residual than lower.
171*b2055c35SXin Li     if (residual <= boundary_residual && upper > boundary_residual) {
172*b2055c35SXin Li       // Halve quantization step to avoid crossing boundary. This midpoint is
173*b2055c35SXin Li       // on the same side of boundary as residual because midpoint <= residual
174*b2055c35SXin Li       // (since upper is closer than lower) and residual is below the boundary.
175*b2055c35SXin Li       return lower + (quantization >> 1);
176*b2055c35SXin Li     }
177*b2055c35SXin Li     return upper & 0xff;
178*b2055c35SXin Li   }
179*b2055c35SXin Li }
180*b2055c35SXin Li 
NearLosslessDiff(uint8_t a,uint8_t b)181*b2055c35SXin Li static WEBP_INLINE uint8_t NearLosslessDiff(uint8_t a, uint8_t b) {
182*b2055c35SXin Li   return (uint8_t)((((int)(a) - (int)(b))) & 0xff);
183*b2055c35SXin Li }
184*b2055c35SXin Li 
185*b2055c35SXin Li // Quantize every component of the difference between the actual pixel value and
186*b2055c35SXin Li // its prediction to a multiple of a quantization (a power of 2, not larger than
187*b2055c35SXin Li // max_quantization which is a power of 2, smaller than max_diff). Take care if
188*b2055c35SXin Li // value and predict have undergone subtract green, which means that red and
189*b2055c35SXin Li // blue are represented as offsets from green.
NearLossless(uint32_t value,uint32_t predict,int max_quantization,int max_diff,int used_subtract_green)190*b2055c35SXin Li static uint32_t NearLossless(uint32_t value, uint32_t predict,
191*b2055c35SXin Li                              int max_quantization, int max_diff,
192*b2055c35SXin Li                              int used_subtract_green) {
193*b2055c35SXin Li   int quantization;
194*b2055c35SXin Li   uint8_t new_green = 0;
195*b2055c35SXin Li   uint8_t green_diff = 0;
196*b2055c35SXin Li   uint8_t a, r, g, b;
197*b2055c35SXin Li   if (max_diff <= 2) {
198*b2055c35SXin Li     return VP8LSubPixels(value, predict);
199*b2055c35SXin Li   }
200*b2055c35SXin Li   quantization = max_quantization;
201*b2055c35SXin Li   while (quantization >= max_diff) {
202*b2055c35SXin Li     quantization >>= 1;
203*b2055c35SXin Li   }
204*b2055c35SXin Li   if ((value >> 24) == 0 || (value >> 24) == 0xff) {
205*b2055c35SXin Li     // Preserve transparency of fully transparent or fully opaque pixels.
206*b2055c35SXin Li     a = NearLosslessDiff((value >> 24) & 0xff, (predict >> 24) & 0xff);
207*b2055c35SXin Li   } else {
208*b2055c35SXin Li     a = NearLosslessComponent(value >> 24, predict >> 24, 0xff, quantization);
209*b2055c35SXin Li   }
210*b2055c35SXin Li   g = NearLosslessComponent((value >> 8) & 0xff, (predict >> 8) & 0xff, 0xff,
211*b2055c35SXin Li                             quantization);
212*b2055c35SXin Li   if (used_subtract_green) {
213*b2055c35SXin Li     // The green offset will be added to red and blue components during decoding
214*b2055c35SXin Li     // to obtain the actual red and blue values.
215*b2055c35SXin Li     new_green = ((predict >> 8) + g) & 0xff;
216*b2055c35SXin Li     // The amount by which green has been adjusted during quantization. It is
217*b2055c35SXin Li     // subtracted from red and blue for compensation, to avoid accumulating two
218*b2055c35SXin Li     // quantization errors in them.
219*b2055c35SXin Li     green_diff = NearLosslessDiff(new_green, (value >> 8) & 0xff);
220*b2055c35SXin Li   }
221*b2055c35SXin Li   r = NearLosslessComponent(NearLosslessDiff((value >> 16) & 0xff, green_diff),
222*b2055c35SXin Li                             (predict >> 16) & 0xff, 0xff - new_green,
223*b2055c35SXin Li                             quantization);
224*b2055c35SXin Li   b = NearLosslessComponent(NearLosslessDiff(value & 0xff, green_diff),
225*b2055c35SXin Li                             predict & 0xff, 0xff - new_green, quantization);
226*b2055c35SXin Li   return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
227*b2055c35SXin Li }
228*b2055c35SXin Li #endif  // (WEBP_NEAR_LOSSLESS == 1)
229*b2055c35SXin Li 
230*b2055c35SXin Li // Stores the difference between the pixel and its prediction in "out".
231*b2055c35SXin Li // In case of a lossy encoding, updates the source image to avoid propagating
232*b2055c35SXin Li // the deviation further to pixels which depend on the current pixel for their
233*b2055c35SXin Li // predictions.
GetResidual(int width,int height,uint32_t * const upper_row,uint32_t * const current_row,const uint8_t * const max_diffs,int mode,int x_start,int x_end,int y,int max_quantization,int exact,int used_subtract_green,uint32_t * const out)234*b2055c35SXin Li static WEBP_INLINE void GetResidual(
235*b2055c35SXin Li     int width, int height, uint32_t* const upper_row,
236*b2055c35SXin Li     uint32_t* const current_row, const uint8_t* const max_diffs, int mode,
237*b2055c35SXin Li     int x_start, int x_end, int y, int max_quantization, int exact,
238*b2055c35SXin Li     int used_subtract_green, uint32_t* const out) {
239*b2055c35SXin Li   if (exact) {
240*b2055c35SXin Li     PredictBatch(mode, x_start, y, x_end - x_start, current_row, upper_row,
241*b2055c35SXin Li                  out);
242*b2055c35SXin Li   } else {
243*b2055c35SXin Li     const VP8LPredictorFunc pred_func = VP8LPredictors[mode];
244*b2055c35SXin Li     int x;
245*b2055c35SXin Li     for (x = x_start; x < x_end; ++x) {
246*b2055c35SXin Li       uint32_t predict;
247*b2055c35SXin Li       uint32_t residual;
248*b2055c35SXin Li       if (y == 0) {
249*b2055c35SXin Li         predict = (x == 0) ? ARGB_BLACK : current_row[x - 1];  // Left.
250*b2055c35SXin Li       } else if (x == 0) {
251*b2055c35SXin Li         predict = upper_row[x];  // Top.
252*b2055c35SXin Li       } else {
253*b2055c35SXin Li         predict = pred_func(&current_row[x - 1], upper_row + x);
254*b2055c35SXin Li       }
255*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
256*b2055c35SXin Li       if (max_quantization == 1 || mode == 0 || y == 0 || y == height - 1 ||
257*b2055c35SXin Li           x == 0 || x == width - 1) {
258*b2055c35SXin Li         residual = VP8LSubPixels(current_row[x], predict);
259*b2055c35SXin Li       } else {
260*b2055c35SXin Li         residual = NearLossless(current_row[x], predict, max_quantization,
261*b2055c35SXin Li                                 max_diffs[x], used_subtract_green);
262*b2055c35SXin Li         // Update the source image.
263*b2055c35SXin Li         current_row[x] = VP8LAddPixels(predict, residual);
264*b2055c35SXin Li         // x is never 0 here so we do not need to update upper_row like below.
265*b2055c35SXin Li       }
266*b2055c35SXin Li #else
267*b2055c35SXin Li       (void)max_diffs;
268*b2055c35SXin Li       (void)height;
269*b2055c35SXin Li       (void)max_quantization;
270*b2055c35SXin Li       (void)used_subtract_green;
271*b2055c35SXin Li       residual = VP8LSubPixels(current_row[x], predict);
272*b2055c35SXin Li #endif
273*b2055c35SXin Li       if ((current_row[x] & kMaskAlpha) == 0) {
274*b2055c35SXin Li         // If alpha is 0, cleanup RGB. We can choose the RGB values of the
275*b2055c35SXin Li         // residual for best compression. The prediction of alpha itself can be
276*b2055c35SXin Li         // non-zero and must be kept though. We choose RGB of the residual to be
277*b2055c35SXin Li         // 0.
278*b2055c35SXin Li         residual &= kMaskAlpha;
279*b2055c35SXin Li         // Update the source image.
280*b2055c35SXin Li         current_row[x] = predict & ~kMaskAlpha;
281*b2055c35SXin Li         // The prediction for the rightmost pixel in a row uses the leftmost
282*b2055c35SXin Li         // pixel
283*b2055c35SXin Li         // in that row as its top-right context pixel. Hence if we change the
284*b2055c35SXin Li         // leftmost pixel of current_row, the corresponding change must be
285*b2055c35SXin Li         // applied
286*b2055c35SXin Li         // to upper_row as well where top-right context is being read from.
287*b2055c35SXin Li         if (x == 0 && y != 0) upper_row[width] = current_row[0];
288*b2055c35SXin Li       }
289*b2055c35SXin Li       out[x - x_start] = residual;
290*b2055c35SXin Li     }
291*b2055c35SXin Li   }
292*b2055c35SXin Li }
293*b2055c35SXin Li 
294*b2055c35SXin Li // Returns best predictor and updates the accumulated histogram.
295*b2055c35SXin Li // If max_quantization > 1, assumes that near lossless processing will be
296*b2055c35SXin Li // applied, quantizing residuals to multiples of quantization levels up to
297*b2055c35SXin Li // max_quantization (the actual quantization level depends on smoothness near
298*b2055c35SXin Li // the given pixel).
GetBestPredictorForTile(int width,int height,int tile_x,int tile_y,int bits,int accumulated[4][256],uint32_t * const argb_scratch,const uint32_t * const argb,int max_quantization,int exact,int used_subtract_green,const uint32_t * const modes)299*b2055c35SXin Li static int GetBestPredictorForTile(int width, int height,
300*b2055c35SXin Li                                    int tile_x, int tile_y, int bits,
301*b2055c35SXin Li                                    int accumulated[4][256],
302*b2055c35SXin Li                                    uint32_t* const argb_scratch,
303*b2055c35SXin Li                                    const uint32_t* const argb,
304*b2055c35SXin Li                                    int max_quantization,
305*b2055c35SXin Li                                    int exact, int used_subtract_green,
306*b2055c35SXin Li                                    const uint32_t* const modes) {
307*b2055c35SXin Li   const int kNumPredModes = 14;
308*b2055c35SXin Li   const int start_x = tile_x << bits;
309*b2055c35SXin Li   const int start_y = tile_y << bits;
310*b2055c35SXin Li   const int tile_size = 1 << bits;
311*b2055c35SXin Li   const int max_y = GetMin(tile_size, height - start_y);
312*b2055c35SXin Li   const int max_x = GetMin(tile_size, width - start_x);
313*b2055c35SXin Li   // Whether there exist columns just outside the tile.
314*b2055c35SXin Li   const int have_left = (start_x > 0);
315*b2055c35SXin Li   // Position and size of the strip covering the tile and adjacent columns if
316*b2055c35SXin Li   // they exist.
317*b2055c35SXin Li   const int context_start_x = start_x - have_left;
318*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
319*b2055c35SXin Li   const int context_width = max_x + have_left + (max_x < width - start_x);
320*b2055c35SXin Li #endif
321*b2055c35SXin Li   const int tiles_per_row = VP8LSubSampleSize(width, bits);
322*b2055c35SXin Li   // Prediction modes of the left and above neighbor tiles.
323*b2055c35SXin Li   const int left_mode = (tile_x > 0) ?
324*b2055c35SXin Li       (modes[tile_y * tiles_per_row + tile_x - 1] >> 8) & 0xff : 0xff;
325*b2055c35SXin Li   const int above_mode = (tile_y > 0) ?
326*b2055c35SXin Li       (modes[(tile_y - 1) * tiles_per_row + tile_x] >> 8) & 0xff : 0xff;
327*b2055c35SXin Li   // The width of upper_row and current_row is one pixel larger than image width
328*b2055c35SXin Li   // to allow the top right pixel to point to the leftmost pixel of the next row
329*b2055c35SXin Li   // when at the right edge.
330*b2055c35SXin Li   uint32_t* upper_row = argb_scratch;
331*b2055c35SXin Li   uint32_t* current_row = upper_row + width + 1;
332*b2055c35SXin Li   uint8_t* const max_diffs = (uint8_t*)(current_row + width + 1);
333*b2055c35SXin Li   float best_diff = MAX_DIFF_COST;
334*b2055c35SXin Li   int best_mode = 0;
335*b2055c35SXin Li   int mode;
336*b2055c35SXin Li   int histo_stack_1[4][256];
337*b2055c35SXin Li   int histo_stack_2[4][256];
338*b2055c35SXin Li   // Need pointers to be able to swap arrays.
339*b2055c35SXin Li   int (*histo_argb)[256] = histo_stack_1;
340*b2055c35SXin Li   int (*best_histo)[256] = histo_stack_2;
341*b2055c35SXin Li   int i, j;
342*b2055c35SXin Li   uint32_t residuals[1 << MAX_TRANSFORM_BITS];
343*b2055c35SXin Li   assert(bits <= MAX_TRANSFORM_BITS);
344*b2055c35SXin Li   assert(max_x <= (1 << MAX_TRANSFORM_BITS));
345*b2055c35SXin Li 
346*b2055c35SXin Li   for (mode = 0; mode < kNumPredModes; ++mode) {
347*b2055c35SXin Li     float cur_diff;
348*b2055c35SXin Li     int relative_y;
349*b2055c35SXin Li     memset(histo_argb, 0, sizeof(histo_stack_1));
350*b2055c35SXin Li     if (start_y > 0) {
351*b2055c35SXin Li       // Read the row above the tile which will become the first upper_row.
352*b2055c35SXin Li       // Include a pixel to the left if it exists; include a pixel to the right
353*b2055c35SXin Li       // in all cases (wrapping to the leftmost pixel of the next row if it does
354*b2055c35SXin Li       // not exist).
355*b2055c35SXin Li       memcpy(current_row + context_start_x,
356*b2055c35SXin Li              argb + (start_y - 1) * width + context_start_x,
357*b2055c35SXin Li              sizeof(*argb) * (max_x + have_left + 1));
358*b2055c35SXin Li     }
359*b2055c35SXin Li     for (relative_y = 0; relative_y < max_y; ++relative_y) {
360*b2055c35SXin Li       const int y = start_y + relative_y;
361*b2055c35SXin Li       int relative_x;
362*b2055c35SXin Li       uint32_t* tmp = upper_row;
363*b2055c35SXin Li       upper_row = current_row;
364*b2055c35SXin Li       current_row = tmp;
365*b2055c35SXin Li       // Read current_row. Include a pixel to the left if it exists; include a
366*b2055c35SXin Li       // pixel to the right in all cases except at the bottom right corner of
367*b2055c35SXin Li       // the image (wrapping to the leftmost pixel of the next row if it does
368*b2055c35SXin Li       // not exist in the current row).
369*b2055c35SXin Li       memcpy(current_row + context_start_x,
370*b2055c35SXin Li              argb + y * width + context_start_x,
371*b2055c35SXin Li              sizeof(*argb) * (max_x + have_left + (y + 1 < height)));
372*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
373*b2055c35SXin Li       if (max_quantization > 1 && y >= 1 && y + 1 < height) {
374*b2055c35SXin Li         MaxDiffsForRow(context_width, width, argb + y * width + context_start_x,
375*b2055c35SXin Li                        max_diffs + context_start_x, used_subtract_green);
376*b2055c35SXin Li       }
377*b2055c35SXin Li #endif
378*b2055c35SXin Li 
379*b2055c35SXin Li       GetResidual(width, height, upper_row, current_row, max_diffs, mode,
380*b2055c35SXin Li                   start_x, start_x + max_x, y, max_quantization, exact,
381*b2055c35SXin Li                   used_subtract_green, residuals);
382*b2055c35SXin Li       for (relative_x = 0; relative_x < max_x; ++relative_x) {
383*b2055c35SXin Li         UpdateHisto(histo_argb, residuals[relative_x]);
384*b2055c35SXin Li       }
385*b2055c35SXin Li     }
386*b2055c35SXin Li     cur_diff = PredictionCostSpatialHistogram(
387*b2055c35SXin Li         (const int (*)[256])accumulated, (const int (*)[256])histo_argb);
388*b2055c35SXin Li     // Favor keeping the areas locally similar.
389*b2055c35SXin Li     if (mode == left_mode) cur_diff -= kSpatialPredictorBias;
390*b2055c35SXin Li     if (mode == above_mode) cur_diff -= kSpatialPredictorBias;
391*b2055c35SXin Li 
392*b2055c35SXin Li     if (cur_diff < best_diff) {
393*b2055c35SXin Li       int (*tmp)[256] = histo_argb;
394*b2055c35SXin Li       histo_argb = best_histo;
395*b2055c35SXin Li       best_histo = tmp;
396*b2055c35SXin Li       best_diff = cur_diff;
397*b2055c35SXin Li       best_mode = mode;
398*b2055c35SXin Li     }
399*b2055c35SXin Li   }
400*b2055c35SXin Li 
401*b2055c35SXin Li   for (i = 0; i < 4; i++) {
402*b2055c35SXin Li     for (j = 0; j < 256; j++) {
403*b2055c35SXin Li       accumulated[i][j] += best_histo[i][j];
404*b2055c35SXin Li     }
405*b2055c35SXin Li   }
406*b2055c35SXin Li 
407*b2055c35SXin Li   return best_mode;
408*b2055c35SXin Li }
409*b2055c35SXin Li 
410*b2055c35SXin Li // Converts pixels of the image to residuals with respect to predictions.
411*b2055c35SXin Li // If max_quantization > 1, applies near lossless processing, quantizing
412*b2055c35SXin Li // residuals to multiples of quantization levels up to max_quantization
413*b2055c35SXin Li // (the actual quantization level depends on smoothness near the given pixel).
CopyImageWithPrediction(int width,int height,int bits,uint32_t * const modes,uint32_t * const argb_scratch,uint32_t * const argb,int low_effort,int max_quantization,int exact,int used_subtract_green)414*b2055c35SXin Li static void CopyImageWithPrediction(int width, int height,
415*b2055c35SXin Li                                     int bits, uint32_t* const modes,
416*b2055c35SXin Li                                     uint32_t* const argb_scratch,
417*b2055c35SXin Li                                     uint32_t* const argb,
418*b2055c35SXin Li                                     int low_effort, int max_quantization,
419*b2055c35SXin Li                                     int exact, int used_subtract_green) {
420*b2055c35SXin Li   const int tiles_per_row = VP8LSubSampleSize(width, bits);
421*b2055c35SXin Li   // The width of upper_row and current_row is one pixel larger than image width
422*b2055c35SXin Li   // to allow the top right pixel to point to the leftmost pixel of the next row
423*b2055c35SXin Li   // when at the right edge.
424*b2055c35SXin Li   uint32_t* upper_row = argb_scratch;
425*b2055c35SXin Li   uint32_t* current_row = upper_row + width + 1;
426*b2055c35SXin Li   uint8_t* current_max_diffs = (uint8_t*)(current_row + width + 1);
427*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
428*b2055c35SXin Li   uint8_t* lower_max_diffs = current_max_diffs + width;
429*b2055c35SXin Li #endif
430*b2055c35SXin Li   int y;
431*b2055c35SXin Li 
432*b2055c35SXin Li   for (y = 0; y < height; ++y) {
433*b2055c35SXin Li     int x;
434*b2055c35SXin Li     uint32_t* const tmp32 = upper_row;
435*b2055c35SXin Li     upper_row = current_row;
436*b2055c35SXin Li     current_row = tmp32;
437*b2055c35SXin Li     memcpy(current_row, argb + y * width,
438*b2055c35SXin Li            sizeof(*argb) * (width + (y + 1 < height)));
439*b2055c35SXin Li 
440*b2055c35SXin Li     if (low_effort) {
441*b2055c35SXin Li       PredictBatch(kPredLowEffort, 0, y, width, current_row, upper_row,
442*b2055c35SXin Li                    argb + y * width);
443*b2055c35SXin Li     } else {
444*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
445*b2055c35SXin Li       if (max_quantization > 1) {
446*b2055c35SXin Li         // Compute max_diffs for the lower row now, because that needs the
447*b2055c35SXin Li         // contents of argb for the current row, which we will overwrite with
448*b2055c35SXin Li         // residuals before proceeding with the next row.
449*b2055c35SXin Li         uint8_t* const tmp8 = current_max_diffs;
450*b2055c35SXin Li         current_max_diffs = lower_max_diffs;
451*b2055c35SXin Li         lower_max_diffs = tmp8;
452*b2055c35SXin Li         if (y + 2 < height) {
453*b2055c35SXin Li           MaxDiffsForRow(width, width, argb + (y + 1) * width, lower_max_diffs,
454*b2055c35SXin Li                          used_subtract_green);
455*b2055c35SXin Li         }
456*b2055c35SXin Li       }
457*b2055c35SXin Li #endif
458*b2055c35SXin Li       for (x = 0; x < width;) {
459*b2055c35SXin Li         const int mode =
460*b2055c35SXin Li             (modes[(y >> bits) * tiles_per_row + (x >> bits)] >> 8) & 0xff;
461*b2055c35SXin Li         int x_end = x + (1 << bits);
462*b2055c35SXin Li         if (x_end > width) x_end = width;
463*b2055c35SXin Li         GetResidual(width, height, upper_row, current_row, current_max_diffs,
464*b2055c35SXin Li                     mode, x, x_end, y, max_quantization, exact,
465*b2055c35SXin Li                     used_subtract_green, argb + y * width + x);
466*b2055c35SXin Li         x = x_end;
467*b2055c35SXin Li       }
468*b2055c35SXin Li     }
469*b2055c35SXin Li   }
470*b2055c35SXin Li }
471*b2055c35SXin Li 
472*b2055c35SXin Li // Finds the best predictor for each tile, and converts the image to residuals
473*b2055c35SXin Li // with respect to predictions. If near_lossless_quality < 100, applies
474*b2055c35SXin Li // near lossless processing, shaving off more bits of residuals for lower
475*b2055c35SXin Li // qualities.
VP8LResidualImage(int width,int height,int bits,int low_effort,uint32_t * const argb,uint32_t * const argb_scratch,uint32_t * const image,int near_lossless_quality,int exact,int used_subtract_green,const WebPPicture * const pic,int percent_range,int * const percent)476*b2055c35SXin Li int VP8LResidualImage(int width, int height, int bits, int low_effort,
477*b2055c35SXin Li                       uint32_t* const argb, uint32_t* const argb_scratch,
478*b2055c35SXin Li                       uint32_t* const image, int near_lossless_quality,
479*b2055c35SXin Li                       int exact, int used_subtract_green,
480*b2055c35SXin Li                       const WebPPicture* const pic, int percent_range,
481*b2055c35SXin Li                       int* const percent) {
482*b2055c35SXin Li   const int tiles_per_row = VP8LSubSampleSize(width, bits);
483*b2055c35SXin Li   const int tiles_per_col = VP8LSubSampleSize(height, bits);
484*b2055c35SXin Li   int percent_start = *percent;
485*b2055c35SXin Li   int tile_y;
486*b2055c35SXin Li   int histo[4][256];
487*b2055c35SXin Li   const int max_quantization = 1 << VP8LNearLosslessBits(near_lossless_quality);
488*b2055c35SXin Li   if (low_effort) {
489*b2055c35SXin Li     int i;
490*b2055c35SXin Li     for (i = 0; i < tiles_per_row * tiles_per_col; ++i) {
491*b2055c35SXin Li       image[i] = ARGB_BLACK | (kPredLowEffort << 8);
492*b2055c35SXin Li     }
493*b2055c35SXin Li   } else {
494*b2055c35SXin Li     memset(histo, 0, sizeof(histo));
495*b2055c35SXin Li     for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) {
496*b2055c35SXin Li       int tile_x;
497*b2055c35SXin Li       for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) {
498*b2055c35SXin Li         const int pred = GetBestPredictorForTile(
499*b2055c35SXin Li             width, height, tile_x, tile_y, bits, histo, argb_scratch, argb,
500*b2055c35SXin Li             max_quantization, exact, used_subtract_green, image);
501*b2055c35SXin Li         image[tile_y * tiles_per_row + tile_x] = ARGB_BLACK | (pred << 8);
502*b2055c35SXin Li       }
503*b2055c35SXin Li 
504*b2055c35SXin Li       if (!WebPReportProgress(
505*b2055c35SXin Li               pic, percent_start + percent_range * tile_y / tiles_per_col,
506*b2055c35SXin Li               percent)) {
507*b2055c35SXin Li         return 0;
508*b2055c35SXin Li       }
509*b2055c35SXin Li     }
510*b2055c35SXin Li   }
511*b2055c35SXin Li 
512*b2055c35SXin Li   CopyImageWithPrediction(width, height, bits, image, argb_scratch, argb,
513*b2055c35SXin Li                           low_effort, max_quantization, exact,
514*b2055c35SXin Li                           used_subtract_green);
515*b2055c35SXin Li   return WebPReportProgress(pic, percent_start + percent_range, percent);
516*b2055c35SXin Li }
517*b2055c35SXin Li 
518*b2055c35SXin Li //------------------------------------------------------------------------------
519*b2055c35SXin Li // Color transform functions.
520*b2055c35SXin Li 
MultipliersClear(VP8LMultipliers * const m)521*b2055c35SXin Li static WEBP_INLINE void MultipliersClear(VP8LMultipliers* const m) {
522*b2055c35SXin Li   m->green_to_red_ = 0;
523*b2055c35SXin Li   m->green_to_blue_ = 0;
524*b2055c35SXin Li   m->red_to_blue_ = 0;
525*b2055c35SXin Li }
526*b2055c35SXin Li 
ColorCodeToMultipliers(uint32_t color_code,VP8LMultipliers * const m)527*b2055c35SXin Li static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
528*b2055c35SXin Li                                                VP8LMultipliers* const m) {
529*b2055c35SXin Li   m->green_to_red_  = (color_code >>  0) & 0xff;
530*b2055c35SXin Li   m->green_to_blue_ = (color_code >>  8) & 0xff;
531*b2055c35SXin Li   m->red_to_blue_   = (color_code >> 16) & 0xff;
532*b2055c35SXin Li }
533*b2055c35SXin Li 
MultipliersToColorCode(const VP8LMultipliers * const m)534*b2055c35SXin Li static WEBP_INLINE uint32_t MultipliersToColorCode(
535*b2055c35SXin Li     const VP8LMultipliers* const m) {
536*b2055c35SXin Li   return 0xff000000u |
537*b2055c35SXin Li          ((uint32_t)(m->red_to_blue_) << 16) |
538*b2055c35SXin Li          ((uint32_t)(m->green_to_blue_) << 8) |
539*b2055c35SXin Li          m->green_to_red_;
540*b2055c35SXin Li }
541*b2055c35SXin Li 
PredictionCostCrossColor(const int accumulated[256],const int counts[256])542*b2055c35SXin Li static float PredictionCostCrossColor(const int accumulated[256],
543*b2055c35SXin Li                                       const int counts[256]) {
544*b2055c35SXin Li   // Favor low entropy, locally and globally.
545*b2055c35SXin Li   // Favor small absolute values for PredictionCostSpatial
546*b2055c35SXin Li   static const float kExpValue = 2.4f;
547*b2055c35SXin Li   return VP8LCombinedShannonEntropy(counts, accumulated) +
548*b2055c35SXin Li          PredictionCostSpatial(counts, 3, kExpValue);
549*b2055c35SXin Li }
550*b2055c35SXin Li 
GetPredictionCostCrossColorRed(const uint32_t * argb,int stride,int tile_width,int tile_height,VP8LMultipliers prev_x,VP8LMultipliers prev_y,int green_to_red,const int accumulated_red_histo[256])551*b2055c35SXin Li static float GetPredictionCostCrossColorRed(
552*b2055c35SXin Li     const uint32_t* argb, int stride, int tile_width, int tile_height,
553*b2055c35SXin Li     VP8LMultipliers prev_x, VP8LMultipliers prev_y, int green_to_red,
554*b2055c35SXin Li     const int accumulated_red_histo[256]) {
555*b2055c35SXin Li   int histo[256] = { 0 };
556*b2055c35SXin Li   float cur_diff;
557*b2055c35SXin Li 
558*b2055c35SXin Li   VP8LCollectColorRedTransforms(argb, stride, tile_width, tile_height,
559*b2055c35SXin Li                                 green_to_red, histo);
560*b2055c35SXin Li 
561*b2055c35SXin Li   cur_diff = PredictionCostCrossColor(accumulated_red_histo, histo);
562*b2055c35SXin Li   if ((uint8_t)green_to_red == prev_x.green_to_red_) {
563*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
564*b2055c35SXin Li   }
565*b2055c35SXin Li   if ((uint8_t)green_to_red == prev_y.green_to_red_) {
566*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
567*b2055c35SXin Li   }
568*b2055c35SXin Li   if (green_to_red == 0) {
569*b2055c35SXin Li     cur_diff -= 3;
570*b2055c35SXin Li   }
571*b2055c35SXin Li   return cur_diff;
572*b2055c35SXin Li }
573*b2055c35SXin Li 
GetBestGreenToRed(const uint32_t * argb,int stride,int tile_width,int tile_height,VP8LMultipliers prev_x,VP8LMultipliers prev_y,int quality,const int accumulated_red_histo[256],VP8LMultipliers * const best_tx)574*b2055c35SXin Li static void GetBestGreenToRed(
575*b2055c35SXin Li     const uint32_t* argb, int stride, int tile_width, int tile_height,
576*b2055c35SXin Li     VP8LMultipliers prev_x, VP8LMultipliers prev_y, int quality,
577*b2055c35SXin Li     const int accumulated_red_histo[256], VP8LMultipliers* const best_tx) {
578*b2055c35SXin Li   const int kMaxIters = 4 + ((7 * quality) >> 8);  // in range [4..6]
579*b2055c35SXin Li   int green_to_red_best = 0;
580*b2055c35SXin Li   int iter, offset;
581*b2055c35SXin Li   float best_diff = GetPredictionCostCrossColorRed(
582*b2055c35SXin Li       argb, stride, tile_width, tile_height, prev_x, prev_y,
583*b2055c35SXin Li       green_to_red_best, accumulated_red_histo);
584*b2055c35SXin Li   for (iter = 0; iter < kMaxIters; ++iter) {
585*b2055c35SXin Li     // ColorTransformDelta is a 3.5 bit fixed point, so 32 is equal to
586*b2055c35SXin Li     // one in color computation. Having initial delta here as 1 is sufficient
587*b2055c35SXin Li     // to explore the range of (-2, 2).
588*b2055c35SXin Li     const int delta = 32 >> iter;
589*b2055c35SXin Li     // Try a negative and a positive delta from the best known value.
590*b2055c35SXin Li     for (offset = -delta; offset <= delta; offset += 2 * delta) {
591*b2055c35SXin Li       const int green_to_red_cur = offset + green_to_red_best;
592*b2055c35SXin Li       const float cur_diff = GetPredictionCostCrossColorRed(
593*b2055c35SXin Li           argb, stride, tile_width, tile_height, prev_x, prev_y,
594*b2055c35SXin Li           green_to_red_cur, accumulated_red_histo);
595*b2055c35SXin Li       if (cur_diff < best_diff) {
596*b2055c35SXin Li         best_diff = cur_diff;
597*b2055c35SXin Li         green_to_red_best = green_to_red_cur;
598*b2055c35SXin Li       }
599*b2055c35SXin Li     }
600*b2055c35SXin Li   }
601*b2055c35SXin Li   best_tx->green_to_red_ = (green_to_red_best & 0xff);
602*b2055c35SXin Li }
603*b2055c35SXin Li 
GetPredictionCostCrossColorBlue(const uint32_t * argb,int stride,int tile_width,int tile_height,VP8LMultipliers prev_x,VP8LMultipliers prev_y,int green_to_blue,int red_to_blue,const int accumulated_blue_histo[256])604*b2055c35SXin Li static float GetPredictionCostCrossColorBlue(
605*b2055c35SXin Li     const uint32_t* argb, int stride, int tile_width, int tile_height,
606*b2055c35SXin Li     VP8LMultipliers prev_x, VP8LMultipliers prev_y,
607*b2055c35SXin Li     int green_to_blue, int red_to_blue, const int accumulated_blue_histo[256]) {
608*b2055c35SXin Li   int histo[256] = { 0 };
609*b2055c35SXin Li   float cur_diff;
610*b2055c35SXin Li 
611*b2055c35SXin Li   VP8LCollectColorBlueTransforms(argb, stride, tile_width, tile_height,
612*b2055c35SXin Li                                  green_to_blue, red_to_blue, histo);
613*b2055c35SXin Li 
614*b2055c35SXin Li   cur_diff = PredictionCostCrossColor(accumulated_blue_histo, histo);
615*b2055c35SXin Li   if ((uint8_t)green_to_blue == prev_x.green_to_blue_) {
616*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
617*b2055c35SXin Li   }
618*b2055c35SXin Li   if ((uint8_t)green_to_blue == prev_y.green_to_blue_) {
619*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
620*b2055c35SXin Li   }
621*b2055c35SXin Li   if ((uint8_t)red_to_blue == prev_x.red_to_blue_) {
622*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
623*b2055c35SXin Li   }
624*b2055c35SXin Li   if ((uint8_t)red_to_blue == prev_y.red_to_blue_) {
625*b2055c35SXin Li     cur_diff -= 3;  // favor keeping the areas locally similar
626*b2055c35SXin Li   }
627*b2055c35SXin Li   if (green_to_blue == 0) {
628*b2055c35SXin Li     cur_diff -= 3;
629*b2055c35SXin Li   }
630*b2055c35SXin Li   if (red_to_blue == 0) {
631*b2055c35SXin Li     cur_diff -= 3;
632*b2055c35SXin Li   }
633*b2055c35SXin Li   return cur_diff;
634*b2055c35SXin Li }
635*b2055c35SXin Li 
636*b2055c35SXin Li #define kGreenRedToBlueNumAxis 8
637*b2055c35SXin Li #define kGreenRedToBlueMaxIters 7
GetBestGreenRedToBlue(const uint32_t * argb,int stride,int tile_width,int tile_height,VP8LMultipliers prev_x,VP8LMultipliers prev_y,int quality,const int accumulated_blue_histo[256],VP8LMultipliers * const best_tx)638*b2055c35SXin Li static void GetBestGreenRedToBlue(
639*b2055c35SXin Li     const uint32_t* argb, int stride, int tile_width, int tile_height,
640*b2055c35SXin Li     VP8LMultipliers prev_x, VP8LMultipliers prev_y, int quality,
641*b2055c35SXin Li     const int accumulated_blue_histo[256],
642*b2055c35SXin Li     VP8LMultipliers* const best_tx) {
643*b2055c35SXin Li   const int8_t offset[kGreenRedToBlueNumAxis][2] =
644*b2055c35SXin Li       {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
645*b2055c35SXin Li   const int8_t delta_lut[kGreenRedToBlueMaxIters] = { 16, 16, 8, 4, 2, 2, 2 };
646*b2055c35SXin Li   const int iters =
647*b2055c35SXin Li       (quality < 25) ? 1 : (quality > 50) ? kGreenRedToBlueMaxIters : 4;
648*b2055c35SXin Li   int green_to_blue_best = 0;
649*b2055c35SXin Li   int red_to_blue_best = 0;
650*b2055c35SXin Li   int iter;
651*b2055c35SXin Li   // Initial value at origin:
652*b2055c35SXin Li   float best_diff = GetPredictionCostCrossColorBlue(
653*b2055c35SXin Li       argb, stride, tile_width, tile_height, prev_x, prev_y,
654*b2055c35SXin Li       green_to_blue_best, red_to_blue_best, accumulated_blue_histo);
655*b2055c35SXin Li   for (iter = 0; iter < iters; ++iter) {
656*b2055c35SXin Li     const int delta = delta_lut[iter];
657*b2055c35SXin Li     int axis;
658*b2055c35SXin Li     for (axis = 0; axis < kGreenRedToBlueNumAxis; ++axis) {
659*b2055c35SXin Li       const int green_to_blue_cur =
660*b2055c35SXin Li           offset[axis][0] * delta + green_to_blue_best;
661*b2055c35SXin Li       const int red_to_blue_cur = offset[axis][1] * delta + red_to_blue_best;
662*b2055c35SXin Li       const float cur_diff = GetPredictionCostCrossColorBlue(
663*b2055c35SXin Li           argb, stride, tile_width, tile_height, prev_x, prev_y,
664*b2055c35SXin Li           green_to_blue_cur, red_to_blue_cur, accumulated_blue_histo);
665*b2055c35SXin Li       if (cur_diff < best_diff) {
666*b2055c35SXin Li         best_diff = cur_diff;
667*b2055c35SXin Li         green_to_blue_best = green_to_blue_cur;
668*b2055c35SXin Li         red_to_blue_best = red_to_blue_cur;
669*b2055c35SXin Li       }
670*b2055c35SXin Li       if (quality < 25 && iter == 4) {
671*b2055c35SXin Li         // Only axis aligned diffs for lower quality.
672*b2055c35SXin Li         break;  // next iter.
673*b2055c35SXin Li       }
674*b2055c35SXin Li     }
675*b2055c35SXin Li     if (delta == 2 && green_to_blue_best == 0 && red_to_blue_best == 0) {
676*b2055c35SXin Li       // Further iterations would not help.
677*b2055c35SXin Li       break;  // out of iter-loop.
678*b2055c35SXin Li     }
679*b2055c35SXin Li   }
680*b2055c35SXin Li   best_tx->green_to_blue_ = green_to_blue_best & 0xff;
681*b2055c35SXin Li   best_tx->red_to_blue_ = red_to_blue_best & 0xff;
682*b2055c35SXin Li }
683*b2055c35SXin Li #undef kGreenRedToBlueMaxIters
684*b2055c35SXin Li #undef kGreenRedToBlueNumAxis
685*b2055c35SXin Li 
GetBestColorTransformForTile(int tile_x,int tile_y,int bits,VP8LMultipliers prev_x,VP8LMultipliers prev_y,int quality,int xsize,int ysize,const int accumulated_red_histo[256],const int accumulated_blue_histo[256],const uint32_t * const argb)686*b2055c35SXin Li static VP8LMultipliers GetBestColorTransformForTile(
687*b2055c35SXin Li     int tile_x, int tile_y, int bits,
688*b2055c35SXin Li     VP8LMultipliers prev_x,
689*b2055c35SXin Li     VP8LMultipliers prev_y,
690*b2055c35SXin Li     int quality, int xsize, int ysize,
691*b2055c35SXin Li     const int accumulated_red_histo[256],
692*b2055c35SXin Li     const int accumulated_blue_histo[256],
693*b2055c35SXin Li     const uint32_t* const argb) {
694*b2055c35SXin Li   const int max_tile_size = 1 << bits;
695*b2055c35SXin Li   const int tile_y_offset = tile_y * max_tile_size;
696*b2055c35SXin Li   const int tile_x_offset = tile_x * max_tile_size;
697*b2055c35SXin Li   const int all_x_max = GetMin(tile_x_offset + max_tile_size, xsize);
698*b2055c35SXin Li   const int all_y_max = GetMin(tile_y_offset + max_tile_size, ysize);
699*b2055c35SXin Li   const int tile_width = all_x_max - tile_x_offset;
700*b2055c35SXin Li   const int tile_height = all_y_max - tile_y_offset;
701*b2055c35SXin Li   const uint32_t* const tile_argb = argb + tile_y_offset * xsize
702*b2055c35SXin Li                                   + tile_x_offset;
703*b2055c35SXin Li   VP8LMultipliers best_tx;
704*b2055c35SXin Li   MultipliersClear(&best_tx);
705*b2055c35SXin Li 
706*b2055c35SXin Li   GetBestGreenToRed(tile_argb, xsize, tile_width, tile_height,
707*b2055c35SXin Li                     prev_x, prev_y, quality, accumulated_red_histo, &best_tx);
708*b2055c35SXin Li   GetBestGreenRedToBlue(tile_argb, xsize, tile_width, tile_height,
709*b2055c35SXin Li                         prev_x, prev_y, quality, accumulated_blue_histo,
710*b2055c35SXin Li                         &best_tx);
711*b2055c35SXin Li   return best_tx;
712*b2055c35SXin Li }
713*b2055c35SXin Li 
CopyTileWithColorTransform(int xsize,int ysize,int tile_x,int tile_y,int max_tile_size,VP8LMultipliers color_transform,uint32_t * argb)714*b2055c35SXin Li static void CopyTileWithColorTransform(int xsize, int ysize,
715*b2055c35SXin Li                                        int tile_x, int tile_y,
716*b2055c35SXin Li                                        int max_tile_size,
717*b2055c35SXin Li                                        VP8LMultipliers color_transform,
718*b2055c35SXin Li                                        uint32_t* argb) {
719*b2055c35SXin Li   const int xscan = GetMin(max_tile_size, xsize - tile_x);
720*b2055c35SXin Li   int yscan = GetMin(max_tile_size, ysize - tile_y);
721*b2055c35SXin Li   argb += tile_y * xsize + tile_x;
722*b2055c35SXin Li   while (yscan-- > 0) {
723*b2055c35SXin Li     VP8LTransformColor(&color_transform, argb, xscan);
724*b2055c35SXin Li     argb += xsize;
725*b2055c35SXin Li   }
726*b2055c35SXin Li }
727*b2055c35SXin Li 
VP8LColorSpaceTransform(int width,int height,int bits,int quality,uint32_t * const argb,uint32_t * image,const WebPPicture * const pic,int percent_range,int * const percent)728*b2055c35SXin Li int VP8LColorSpaceTransform(int width, int height, int bits, int quality,
729*b2055c35SXin Li                             uint32_t* const argb, uint32_t* image,
730*b2055c35SXin Li                             const WebPPicture* const pic, int percent_range,
731*b2055c35SXin Li                             int* const percent) {
732*b2055c35SXin Li   const int max_tile_size = 1 << bits;
733*b2055c35SXin Li   const int tile_xsize = VP8LSubSampleSize(width, bits);
734*b2055c35SXin Li   const int tile_ysize = VP8LSubSampleSize(height, bits);
735*b2055c35SXin Li   int percent_start = *percent;
736*b2055c35SXin Li   int accumulated_red_histo[256] = { 0 };
737*b2055c35SXin Li   int accumulated_blue_histo[256] = { 0 };
738*b2055c35SXin Li   int tile_x, tile_y;
739*b2055c35SXin Li   VP8LMultipliers prev_x, prev_y;
740*b2055c35SXin Li   MultipliersClear(&prev_y);
741*b2055c35SXin Li   MultipliersClear(&prev_x);
742*b2055c35SXin Li   for (tile_y = 0; tile_y < tile_ysize; ++tile_y) {
743*b2055c35SXin Li     for (tile_x = 0; tile_x < tile_xsize; ++tile_x) {
744*b2055c35SXin Li       int y;
745*b2055c35SXin Li       const int tile_x_offset = tile_x * max_tile_size;
746*b2055c35SXin Li       const int tile_y_offset = tile_y * max_tile_size;
747*b2055c35SXin Li       const int all_x_max = GetMin(tile_x_offset + max_tile_size, width);
748*b2055c35SXin Li       const int all_y_max = GetMin(tile_y_offset + max_tile_size, height);
749*b2055c35SXin Li       const int offset = tile_y * tile_xsize + tile_x;
750*b2055c35SXin Li       if (tile_y != 0) {
751*b2055c35SXin Li         ColorCodeToMultipliers(image[offset - tile_xsize], &prev_y);
752*b2055c35SXin Li       }
753*b2055c35SXin Li       prev_x = GetBestColorTransformForTile(tile_x, tile_y, bits,
754*b2055c35SXin Li                                             prev_x, prev_y,
755*b2055c35SXin Li                                             quality, width, height,
756*b2055c35SXin Li                                             accumulated_red_histo,
757*b2055c35SXin Li                                             accumulated_blue_histo,
758*b2055c35SXin Li                                             argb);
759*b2055c35SXin Li       image[offset] = MultipliersToColorCode(&prev_x);
760*b2055c35SXin Li       CopyTileWithColorTransform(width, height, tile_x_offset, tile_y_offset,
761*b2055c35SXin Li                                  max_tile_size, prev_x, argb);
762*b2055c35SXin Li 
763*b2055c35SXin Li       // Gather accumulated histogram data.
764*b2055c35SXin Li       for (y = tile_y_offset; y < all_y_max; ++y) {
765*b2055c35SXin Li         int ix = y * width + tile_x_offset;
766*b2055c35SXin Li         const int ix_end = ix + all_x_max - tile_x_offset;
767*b2055c35SXin Li         for (; ix < ix_end; ++ix) {
768*b2055c35SXin Li           const uint32_t pix = argb[ix];
769*b2055c35SXin Li           if (ix >= 2 &&
770*b2055c35SXin Li               pix == argb[ix - 2] &&
771*b2055c35SXin Li               pix == argb[ix - 1]) {
772*b2055c35SXin Li             continue;  // repeated pixels are handled by backward references
773*b2055c35SXin Li           }
774*b2055c35SXin Li           if (ix >= width + 2 &&
775*b2055c35SXin Li               argb[ix - 2] == argb[ix - width - 2] &&
776*b2055c35SXin Li               argb[ix - 1] == argb[ix - width - 1] &&
777*b2055c35SXin Li               pix == argb[ix - width]) {
778*b2055c35SXin Li             continue;  // repeated pixels are handled by backward references
779*b2055c35SXin Li           }
780*b2055c35SXin Li           ++accumulated_red_histo[(pix >> 16) & 0xff];
781*b2055c35SXin Li           ++accumulated_blue_histo[(pix >> 0) & 0xff];
782*b2055c35SXin Li         }
783*b2055c35SXin Li       }
784*b2055c35SXin Li     }
785*b2055c35SXin Li     if (!WebPReportProgress(
786*b2055c35SXin Li             pic, percent_start + percent_range * tile_y / tile_ysize,
787*b2055c35SXin Li             percent)) {
788*b2055c35SXin Li       return 0;
789*b2055c35SXin Li     }
790*b2055c35SXin Li   }
791*b2055c35SXin Li   return 1;
792*b2055c35SXin Li }
793