xref: /aosp_15_r20/external/webp/src/enc/near_lossless_enc.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2014 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 // Near-lossless image preprocessing adjusts pixel values to help
11*b2055c35SXin Li // compressibility with a guarantee of maximum deviation between original and
12*b2055c35SXin Li // resulting pixel values.
13*b2055c35SXin Li //
14*b2055c35SXin Li // Author: Jyrki Alakuijala ([email protected])
15*b2055c35SXin Li // Converted to C by Aleksander Kramarz ([email protected])
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <assert.h>
18*b2055c35SXin Li #include <stdlib.h>
19*b2055c35SXin Li 
20*b2055c35SXin Li #include "src/dsp/lossless_common.h"
21*b2055c35SXin Li #include "src/utils/utils.h"
22*b2055c35SXin Li #include "src/enc/vp8li_enc.h"
23*b2055c35SXin Li 
24*b2055c35SXin Li #if (WEBP_NEAR_LOSSLESS == 1)
25*b2055c35SXin Li 
26*b2055c35SXin Li #define MIN_DIM_FOR_NEAR_LOSSLESS 64
27*b2055c35SXin Li #define MAX_LIMIT_BITS             5
28*b2055c35SXin Li 
29*b2055c35SXin Li // Quantizes the value up or down to a multiple of 1<<bits (or to 255),
30*b2055c35SXin Li // choosing the closer one, resolving ties using bankers' rounding.
FindClosestDiscretized(uint32_t a,int bits)31*b2055c35SXin Li static uint32_t FindClosestDiscretized(uint32_t a, int bits) {
32*b2055c35SXin Li   const uint32_t mask = (1u << bits) - 1;
33*b2055c35SXin Li   const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
34*b2055c35SXin Li   assert(bits > 0);
35*b2055c35SXin Li   if (biased > 0xff) return 0xff;
36*b2055c35SXin Li   return biased & ~mask;
37*b2055c35SXin Li }
38*b2055c35SXin Li 
39*b2055c35SXin Li // Applies FindClosestDiscretized to all channels of pixel.
ClosestDiscretizedArgb(uint32_t a,int bits)40*b2055c35SXin Li static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
41*b2055c35SXin Li   return
42*b2055c35SXin Li       (FindClosestDiscretized(a >> 24, bits) << 24) |
43*b2055c35SXin Li       (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
44*b2055c35SXin Li       (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
45*b2055c35SXin Li       (FindClosestDiscretized(a & 0xff, bits));
46*b2055c35SXin Li }
47*b2055c35SXin Li 
48*b2055c35SXin Li // Checks if distance between corresponding channel values of pixels a and b
49*b2055c35SXin Li // is within the given limit.
IsNear(uint32_t a,uint32_t b,int limit)50*b2055c35SXin Li static int IsNear(uint32_t a, uint32_t b, int limit) {
51*b2055c35SXin Li   int k;
52*b2055c35SXin Li   for (k = 0; k < 4; ++k) {
53*b2055c35SXin Li     const int delta =
54*b2055c35SXin Li         (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
55*b2055c35SXin Li     if (delta >= limit || delta <= -limit) {
56*b2055c35SXin Li       return 0;
57*b2055c35SXin Li     }
58*b2055c35SXin Li   }
59*b2055c35SXin Li   return 1;
60*b2055c35SXin Li }
61*b2055c35SXin Li 
IsSmooth(const uint32_t * const prev_row,const uint32_t * const curr_row,const uint32_t * const next_row,int ix,int limit)62*b2055c35SXin Li static int IsSmooth(const uint32_t* const prev_row,
63*b2055c35SXin Li                     const uint32_t* const curr_row,
64*b2055c35SXin Li                     const uint32_t* const next_row,
65*b2055c35SXin Li                     int ix, int limit) {
66*b2055c35SXin Li   // Check that all pixels in 4-connected neighborhood are smooth.
67*b2055c35SXin Li   return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
68*b2055c35SXin Li           IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
69*b2055c35SXin Li           IsNear(curr_row[ix], prev_row[ix], limit) &&
70*b2055c35SXin Li           IsNear(curr_row[ix], next_row[ix], limit));
71*b2055c35SXin Li }
72*b2055c35SXin Li 
73*b2055c35SXin Li // Adjusts pixel values of image with given maximum error.
NearLossless(int xsize,int ysize,const uint32_t * argb_src,int stride,int limit_bits,uint32_t * copy_buffer,uint32_t * argb_dst)74*b2055c35SXin Li static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,
75*b2055c35SXin Li                          int stride, int limit_bits, uint32_t* copy_buffer,
76*b2055c35SXin Li                          uint32_t* argb_dst) {
77*b2055c35SXin Li   int x, y;
78*b2055c35SXin Li   const int limit = 1 << limit_bits;
79*b2055c35SXin Li   uint32_t* prev_row = copy_buffer;
80*b2055c35SXin Li   uint32_t* curr_row = prev_row + xsize;
81*b2055c35SXin Li   uint32_t* next_row = curr_row + xsize;
82*b2055c35SXin Li   memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
83*b2055c35SXin Li   memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
84*b2055c35SXin Li 
85*b2055c35SXin Li   for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
86*b2055c35SXin Li     if (y == 0 || y == ysize - 1) {
87*b2055c35SXin Li       memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
88*b2055c35SXin Li     } else {
89*b2055c35SXin Li       memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
90*b2055c35SXin Li       argb_dst[0] = argb_src[0];
91*b2055c35SXin Li       argb_dst[xsize - 1] = argb_src[xsize - 1];
92*b2055c35SXin Li       for (x = 1; x < xsize - 1; ++x) {
93*b2055c35SXin Li         if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
94*b2055c35SXin Li           argb_dst[x] = curr_row[x];
95*b2055c35SXin Li         } else {
96*b2055c35SXin Li           argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
97*b2055c35SXin Li         }
98*b2055c35SXin Li       }
99*b2055c35SXin Li     }
100*b2055c35SXin Li     {
101*b2055c35SXin Li       // Three-way swap.
102*b2055c35SXin Li       uint32_t* const temp = prev_row;
103*b2055c35SXin Li       prev_row = curr_row;
104*b2055c35SXin Li       curr_row = next_row;
105*b2055c35SXin Li       next_row = temp;
106*b2055c35SXin Li     }
107*b2055c35SXin Li   }
108*b2055c35SXin Li }
109*b2055c35SXin Li 
VP8ApplyNearLossless(const WebPPicture * const picture,int quality,uint32_t * const argb_dst)110*b2055c35SXin Li int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
111*b2055c35SXin Li                          uint32_t* const argb_dst) {
112*b2055c35SXin Li   int i;
113*b2055c35SXin Li   const int xsize = picture->width;
114*b2055c35SXin Li   const int ysize = picture->height;
115*b2055c35SXin Li   const int stride = picture->argb_stride;
116*b2055c35SXin Li   uint32_t* const copy_buffer =
117*b2055c35SXin Li       (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
118*b2055c35SXin Li   const int limit_bits = VP8LNearLosslessBits(quality);
119*b2055c35SXin Li   assert(argb_dst != NULL);
120*b2055c35SXin Li   assert(limit_bits > 0);
121*b2055c35SXin Li   assert(limit_bits <= MAX_LIMIT_BITS);
122*b2055c35SXin Li   if (copy_buffer == NULL) {
123*b2055c35SXin Li     return 0;
124*b2055c35SXin Li   }
125*b2055c35SXin Li   // For small icon images, don't attempt to apply near-lossless compression.
126*b2055c35SXin Li   if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
127*b2055c35SXin Li        ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
128*b2055c35SXin Li       ysize < 3) {
129*b2055c35SXin Li     for (i = 0; i < ysize; ++i) {
130*b2055c35SXin Li       memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
131*b2055c35SXin Li              xsize * sizeof(*argb_dst));
132*b2055c35SXin Li     }
133*b2055c35SXin Li     WebPSafeFree(copy_buffer);
134*b2055c35SXin Li     return 1;
135*b2055c35SXin Li   }
136*b2055c35SXin Li 
137*b2055c35SXin Li   NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
138*b2055c35SXin Li                argb_dst);
139*b2055c35SXin Li   for (i = limit_bits - 1; i != 0; --i) {
140*b2055c35SXin Li     NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
141*b2055c35SXin Li   }
142*b2055c35SXin Li   WebPSafeFree(copy_buffer);
143*b2055c35SXin Li   return 1;
144*b2055c35SXin Li }
145*b2055c35SXin Li #else  // (WEBP_NEAR_LOSSLESS == 1)
146*b2055c35SXin Li 
147*b2055c35SXin Li // Define a stub to suppress compiler warnings.
148*b2055c35SXin Li extern void VP8LNearLosslessStub(void);
VP8LNearLosslessStub(void)149*b2055c35SXin Li void VP8LNearLosslessStub(void) {}
150*b2055c35SXin Li 
151*b2055c35SXin Li #endif  // (WEBP_NEAR_LOSSLESS == 1)
152