/src/libwebp/src/dsp/lossless.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2012 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // Image transforms and color space conversion methods for lossless decoder. |
11 | | // |
12 | | // Authors: Vikas Arora (vikaas.arora@gmail.com) |
13 | | // Jyrki Alakuijala (jyrki@google.com) |
14 | | // Urvang Joshi (urvang@google.com) |
15 | | |
16 | | #include "src/dsp/lossless.h" |
17 | | |
18 | | #include <assert.h> |
19 | | #include <stdlib.h> |
20 | | #include <string.h> |
21 | | |
22 | | #include "src/dec/vp8li_dec.h" |
23 | | #include "src/dsp/cpu.h" |
24 | | #include "src/dsp/dsp.h" |
25 | | #include "src/dsp/lossless_common.h" |
26 | | #include "src/utils/endian_inl_utils.h" |
27 | | #include "src/utils/utils.h" |
28 | | #include "src/webp/decode.h" |
29 | | #include "src/webp/format_constants.h" |
30 | | #include "src/webp/types.h" |
31 | | |
32 | | //------------------------------------------------------------------------------ |
33 | | // Image transforms. |
34 | | |
35 | 21.2M | static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) { |
36 | 21.2M | return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1); |
37 | 21.2M | } |
38 | | |
39 | 2.12M | static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) { |
40 | 2.12M | return Average2(Average2(a0, a2), a1); |
41 | 2.12M | } |
42 | | |
43 | | static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1, |
44 | 2.24M | uint32_t a2, uint32_t a3) { |
45 | 2.24M | return Average2(Average2(a0, a1), Average2(a2, a3)); |
46 | 2.24M | } |
47 | | |
48 | 18.4M | static WEBP_INLINE uint32_t Clip255(uint32_t a) { |
49 | 18.4M | if (a < 256) { |
50 | 17.6M | return a; |
51 | 17.6M | } |
52 | | // return 0, when a is a negative integer. |
53 | | // return 255, when a is positive. |
54 | 741k | return ~a >> 24; |
55 | 18.4M | } |
56 | | |
57 | 10.4M | static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) { |
58 | 10.4M | return Clip255((uint32_t)(a + b - c)); |
59 | 10.4M | } |
60 | | |
61 | | static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1, |
62 | 2.60M | uint32_t c2) { |
63 | 2.60M | const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24); |
64 | 2.60M | const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, |
65 | 2.60M | (c1 >> 16) & 0xff, |
66 | 2.60M | (c2 >> 16) & 0xff); |
67 | 2.60M | const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, |
68 | 2.60M | (c1 >> 8) & 0xff, |
69 | 2.60M | (c2 >> 8) & 0xff); |
70 | 2.60M | const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff); |
71 | 2.60M | return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b; |
72 | 2.60M | } |
73 | | |
74 | 7.99M | static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) { |
75 | 7.99M | return Clip255((uint32_t)(a + (a - b) / 2)); |
76 | 7.99M | } |
77 | | |
78 | | static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1, |
79 | 1.99M | uint32_t c2) { |
80 | 1.99M | const uint32_t ave = Average2(c0, c1); |
81 | 1.99M | const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24); |
82 | 1.99M | const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff); |
83 | 1.99M | const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff); |
84 | 1.99M | const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff); |
85 | 1.99M | return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b; |
86 | 1.99M | } |
87 | | |
88 | | // gcc <= 4.9 on ARM generates incorrect code in Select() when Sub3() is |
89 | | // inlined. |
90 | | #if defined(__arm__) && defined(__GNUC__) && LOCAL_GCC_VERSION <= 0x409 |
91 | | # define LOCAL_INLINE __attribute__ ((noinline)) |
92 | | #else |
93 | | # define LOCAL_INLINE WEBP_INLINE |
94 | | #endif |
95 | | |
96 | 9.12M | static LOCAL_INLINE int Sub3(int a, int b, int c) { |
97 | 9.12M | const int pb = b - c; |
98 | 9.12M | const int pa = a - c; |
99 | 9.12M | return abs(pb) - abs(pa); |
100 | 9.12M | } |
101 | | |
102 | | #undef LOCAL_INLINE |
103 | | |
104 | 2.28M | static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) { |
105 | 2.28M | const int pa_minus_pb = |
106 | 2.28M | Sub3((a >> 24) , (b >> 24) , (c >> 24) ) + |
107 | 2.28M | Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) + |
108 | 2.28M | Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) + |
109 | 2.28M | Sub3((a ) & 0xff, (b ) & 0xff, (c ) & 0xff); |
110 | 2.28M | return (pa_minus_pb <= 0) ? a : b; |
111 | 2.28M | } |
112 | | |
113 | | //------------------------------------------------------------------------------ |
114 | | // Predictors |
115 | | |
116 | | static uint32_t VP8LPredictor0_C(const uint32_t* const left, |
117 | 188M | const uint32_t* const top) { |
118 | 188M | (void)top; |
119 | 188M | (void)left; |
120 | 188M | return ARGB_BLACK; |
121 | 188M | } |
122 | | static uint32_t VP8LPredictor1_C(const uint32_t* const left, |
123 | 191M | const uint32_t* const top) { |
124 | 191M | (void)top; |
125 | 191M | return *left; |
126 | 191M | } |
127 | | uint32_t VP8LPredictor2_C(const uint32_t* const left, |
128 | 171M | const uint32_t* const top) { |
129 | 171M | (void)left; |
130 | 171M | return top[0]; |
131 | 171M | } |
132 | | uint32_t VP8LPredictor3_C(const uint32_t* const left, |
133 | 160M | const uint32_t* const top) { |
134 | 160M | (void)left; |
135 | 160M | return top[1]; |
136 | 160M | } |
137 | | uint32_t VP8LPredictor4_C(const uint32_t* const left, |
138 | 159M | const uint32_t* const top) { |
139 | 159M | (void)left; |
140 | 159M | return top[-1]; |
141 | 159M | } |
142 | | uint32_t VP8LPredictor5_C(const uint32_t* const left, |
143 | 2.12M | const uint32_t* const top) { |
144 | 2.12M | const uint32_t pred = Average3(*left, top[0], top[1]); |
145 | 2.12M | return pred; |
146 | 2.12M | } |
147 | | uint32_t VP8LPredictor6_C(const uint32_t* const left, |
148 | 2.01M | const uint32_t* const top) { |
149 | 2.01M | const uint32_t pred = Average2(*left, top[-1]); |
150 | 2.01M | return pred; |
151 | 2.01M | } |
152 | | uint32_t VP8LPredictor7_C(const uint32_t* const left, |
153 | 1.98M | const uint32_t* const top) { |
154 | 1.98M | const uint32_t pred = Average2(*left, top[0]); |
155 | 1.98M | return pred; |
156 | 1.98M | } |
157 | | uint32_t VP8LPredictor8_C(const uint32_t* const left, |
158 | 2.09M | const uint32_t* const top) { |
159 | 2.09M | const uint32_t pred = Average2(top[-1], top[0]); |
160 | 2.09M | (void)left; |
161 | 2.09M | return pred; |
162 | 2.09M | } |
163 | | uint32_t VP8LPredictor9_C(const uint32_t* const left, |
164 | 2.20M | const uint32_t* const top) { |
165 | 2.20M | const uint32_t pred = Average2(top[0], top[1]); |
166 | 2.20M | (void)left; |
167 | 2.20M | return pred; |
168 | 2.20M | } |
169 | | uint32_t VP8LPredictor10_C(const uint32_t* const left, |
170 | 2.24M | const uint32_t* const top) { |
171 | 2.24M | const uint32_t pred = Average4(*left, top[-1], top[0], top[1]); |
172 | 2.24M | return pred; |
173 | 2.24M | } |
174 | | uint32_t VP8LPredictor11_C(const uint32_t* const left, |
175 | 2.28M | const uint32_t* const top) { |
176 | 2.28M | const uint32_t pred = Select(top[0], *left, top[-1]); |
177 | 2.28M | return pred; |
178 | 2.28M | } |
179 | | uint32_t VP8LPredictor12_C(const uint32_t* const left, |
180 | 2.60M | const uint32_t* const top) { |
181 | 2.60M | const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]); |
182 | 2.60M | return pred; |
183 | 2.60M | } |
184 | | uint32_t VP8LPredictor13_C(const uint32_t* const left, |
185 | 1.99M | const uint32_t* const top) { |
186 | 1.99M | const uint32_t pred = ClampedAddSubtractHalf(*left, top[0], top[-1]); |
187 | 1.99M | return pred; |
188 | 1.99M | } |
189 | | |
190 | | static void PredictorAdd0_C(const uint32_t* in, const uint32_t* upper, |
191 | 1.46M | int num_pixels, uint32_t* WEBP_RESTRICT out) { |
192 | 1.46M | int x; |
193 | 1.46M | (void)upper; |
194 | 16.0M | for (x = 0; x < num_pixels; ++x) out[x] = VP8LAddPixels(in[x], ARGB_BLACK); |
195 | 1.46M | } |
196 | | static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper, |
197 | 412k | int num_pixels, uint32_t* WEBP_RESTRICT out) { |
198 | 412k | int i; |
199 | 412k | uint32_t left = out[-1]; |
200 | 412k | (void)upper; |
201 | 2.55M | for (i = 0; i < num_pixels; ++i) { |
202 | 2.14M | out[i] = left = VP8LAddPixels(in[i], left); |
203 | 2.14M | } |
204 | 412k | } |
205 | | GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C) |
206 | | GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C) |
207 | | GENERATE_PREDICTOR_ADD(VP8LPredictor4_C, PredictorAdd4_C) |
208 | | GENERATE_PREDICTOR_ADD(VP8LPredictor5_C, PredictorAdd5_C) |
209 | | GENERATE_PREDICTOR_ADD(VP8LPredictor6_C, PredictorAdd6_C) |
210 | | GENERATE_PREDICTOR_ADD(VP8LPredictor7_C, PredictorAdd7_C) |
211 | | GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C) |
212 | | GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C) |
213 | | GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C) |
214 | | GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C) |
215 | | GENERATE_PREDICTOR_ADD(VP8LPredictor12_C, PredictorAdd12_C) |
216 | | GENERATE_PREDICTOR_ADD(VP8LPredictor13_C, PredictorAdd13_C) |
217 | | |
218 | | //------------------------------------------------------------------------------ |
219 | | |
220 | | // Inverse prediction. |
221 | | static void PredictorInverseTransform_C(const VP8LTransform* const transform, |
222 | | int y_start, int y_end, |
223 | 73.6k | const uint32_t* in, uint32_t* out) { |
224 | 73.6k | const int width = transform->xsize; |
225 | 73.6k | if (y_start == 0) { // First Row follows the L (mode=1) mode. |
226 | 1.77k | PredictorAdd0_C(in, NULL, 1, out); |
227 | 1.77k | PredictorAdd1_C(in + 1, NULL, width - 1, out + 1); |
228 | 1.77k | in += width; |
229 | 1.77k | out += width; |
230 | 1.77k | ++y_start; |
231 | 1.77k | } |
232 | | |
233 | 73.6k | { |
234 | 73.6k | int y = y_start; |
235 | 73.6k | const int tile_width = 1 << transform->bits; |
236 | 73.6k | const int mask = tile_width - 1; |
237 | 73.6k | const int tiles_per_row = VP8LSubSampleSize(width, transform->bits); |
238 | 73.6k | const uint32_t* pred_mode_base = |
239 | 73.6k | transform->data + (y >> transform->bits) * tiles_per_row; |
240 | | |
241 | 1.22M | while (y < y_end) { |
242 | 1.15M | const uint32_t* pred_mode_src = pred_mode_base; |
243 | 1.15M | int x = 1; |
244 | | // First pixel follows the T (mode=2) mode. |
245 | 1.15M | PredictorAdd2_C(in, out - width, 1, out); |
246 | | // .. the rest: |
247 | 59.4M | while (x < width) { |
248 | 58.3M | const VP8LPredictorAddSubFunc pred_func = |
249 | 58.3M | VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf]; |
250 | 58.3M | int x_end = (x & ~mask) + tile_width; |
251 | 58.3M | if (x_end > width) x_end = width; |
252 | 58.3M | pred_func(in + x, out + x - width, x_end - x, out + x); |
253 | 58.3M | x = x_end; |
254 | 58.3M | } |
255 | 1.15M | in += width; |
256 | 1.15M | out += width; |
257 | 1.15M | ++y; |
258 | 1.15M | if ((y & mask) == 0) { // Use the same mask, since tiles are squares. |
259 | 44.3k | pred_mode_base += tiles_per_row; |
260 | 44.3k | } |
261 | 1.15M | } |
262 | 73.6k | } |
263 | 73.6k | } |
264 | | |
265 | | // Add green to blue and red channels (i.e. perform the inverse transform of |
266 | | // 'subtract green'). |
267 | | void VP8LAddGreenToBlueAndRed_C(const uint32_t* src, int num_pixels, |
268 | 371 | uint32_t* dst) { |
269 | 371 | int i; |
270 | 1.12k | for (i = 0; i < num_pixels; ++i) { |
271 | 750 | const uint32_t argb = src[i]; |
272 | 750 | const uint32_t green = ((argb >> 8) & 0xff); |
273 | 750 | uint32_t red_blue = (argb & 0x00ff00ffu); |
274 | 750 | red_blue += (green << 16) | green; |
275 | 750 | red_blue &= 0x00ff00ffu; |
276 | 750 | dst[i] = (argb & 0xff00ff00u) | red_blue; |
277 | 750 | } |
278 | 371 | } |
279 | | |
280 | | static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, |
281 | 94.5k | int8_t color) { |
282 | 94.5k | return ((int)color_pred * color) >> 5; |
283 | 94.5k | } |
284 | | |
285 | | static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code, |
286 | 1.10M | VP8LMultipliers* const m) { |
287 | 1.10M | m->green_to_red = (color_code >> 0) & 0xff; |
288 | 1.10M | m->green_to_blue = (color_code >> 8) & 0xff; |
289 | 1.10M | m->red_to_blue = (color_code >> 16) & 0xff; |
290 | 1.10M | } |
291 | | |
292 | | void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, |
293 | | const uint32_t* src, int num_pixels, |
294 | 23.5k | uint32_t* dst) { |
295 | 23.5k | int i; |
296 | 55.0k | for (i = 0; i < num_pixels; ++i) { |
297 | 31.5k | const uint32_t argb = src[i]; |
298 | 31.5k | const int8_t green = (int8_t)(argb >> 8); |
299 | 31.5k | const uint32_t red = argb >> 16; |
300 | 31.5k | int new_red = red & 0xff; |
301 | 31.5k | int new_blue = argb & 0xff; |
302 | 31.5k | new_red += ColorTransformDelta((int8_t)m->green_to_red, green); |
303 | 31.5k | new_red &= 0xff; |
304 | 31.5k | new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green); |
305 | 31.5k | new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red); |
306 | 31.5k | new_blue &= 0xff; |
307 | 31.5k | dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue); |
308 | 31.5k | } |
309 | 23.5k | } |
310 | | |
311 | | // Color space inverse transform. |
312 | | static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform, |
313 | | int y_start, int y_end, |
314 | 1.71k | const uint32_t* src, uint32_t* dst) { |
315 | 1.71k | const int width = transform->xsize; |
316 | 1.71k | const int tile_width = 1 << transform->bits; |
317 | 1.71k | const int mask = tile_width - 1; |
318 | 1.71k | const int safe_width = width & ~mask; |
319 | 1.71k | const int remaining_width = width - safe_width; |
320 | 1.71k | const int tiles_per_row = VP8LSubSampleSize(width, transform->bits); |
321 | 1.71k | int y = y_start; |
322 | 1.71k | const uint32_t* pred_row = |
323 | 1.71k | transform->data + (y >> transform->bits) * tiles_per_row; |
324 | | |
325 | 28.5k | while (y < y_end) { |
326 | 26.8k | const uint32_t* pred = pred_row; |
327 | 26.8k | VP8LMultipliers m = { 0, 0, 0 }; |
328 | 26.8k | const uint32_t* const src_safe_end = src + safe_width; |
329 | 26.8k | const uint32_t* const src_end = src + width; |
330 | 1.10M | while (src < src_safe_end) { |
331 | 1.08M | ColorCodeToMultipliers(*pred++, &m); |
332 | 1.08M | VP8LTransformColorInverse(&m, src, tile_width, dst); |
333 | 1.08M | src += tile_width; |
334 | 1.08M | dst += tile_width; |
335 | 1.08M | } |
336 | 26.8k | if (src < src_end) { // Left-overs using C-version. |
337 | 24.5k | ColorCodeToMultipliers(*pred++, &m); |
338 | 24.5k | VP8LTransformColorInverse(&m, src, remaining_width, dst); |
339 | 24.5k | src += remaining_width; |
340 | 24.5k | dst += remaining_width; |
341 | 24.5k | } |
342 | 26.8k | ++y; |
343 | 26.8k | if ((y & mask) == 0) pred_row += tiles_per_row; |
344 | 26.8k | } |
345 | 1.71k | } |
346 | | |
347 | | // Separate out pixels packed together using pixel-bundling. |
348 | | // We define two methods for ARGB data (uint32_t) and alpha-only data (uint8_t). |
349 | | #define COLOR_INDEX_INVERSE(FUNC_NAME, F_NAME, STATIC_DECL, TYPE, BIT_SUFFIX, \ |
350 | | GET_INDEX, GET_VALUE) \ |
351 | | static void F_NAME(const TYPE* src, const uint32_t* const color_map, \ |
352 | 16.9k | TYPE* dst, int y_start, int y_end, int width) { \ |
353 | 16.9k | int y; \ |
354 | 272k | for (y = y_start; y < y_end; ++y) { \ |
355 | 255k | int x; \ |
356 | 677M | for (x = 0; x < width; ++x) { \ |
357 | 677M | *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \ |
358 | 677M | } \ |
359 | 255k | } \ |
360 | 16.9k | } \ Line | Count | Source | 352 | 14.1k | TYPE* dst, int y_start, int y_end, int width) { \ | 353 | 14.1k | int y; \ | 354 | 230k | for (y = y_start; y < y_end; ++y) { \ | 355 | 216k | int x; \ | 356 | 673M | for (x = 0; x < width; ++x) { \ | 357 | 673M | *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \ | 358 | 673M | } \ | 359 | 216k | } \ | 360 | 14.1k | } \ |
Line | Count | Source | 352 | 2.73k | TYPE* dst, int y_start, int y_end, int width) { \ | 353 | 2.73k | int y; \ | 354 | 41.3k | for (y = y_start; y < y_end; ++y) { \ | 355 | 38.6k | int x; \ | 356 | 3.74M | for (x = 0; x < width; ++x) { \ | 357 | 3.70M | *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]); \ | 358 | 3.70M | } \ | 359 | 38.6k | } \ | 360 | 2.73k | } \ |
|
361 | | STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform, \ |
362 | | int y_start, int y_end, const TYPE* src, \ |
363 | 39.6k | TYPE* dst) { \ |
364 | 39.6k | int y; \ |
365 | 39.6k | const int bits_per_pixel = 8 >> transform->bits; \ |
366 | 39.6k | const int width = transform->xsize; \ |
367 | 39.6k | const uint32_t* const color_map = transform->data; \ |
368 | 39.6k | if (bits_per_pixel < 8) { \ |
369 | 22.6k | const int pixels_per_byte = 1 << transform->bits; \ |
370 | 22.6k | const int count_mask = pixels_per_byte - 1; \ |
371 | 22.6k | const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \ |
372 | 378k | for (y = y_start; y < y_end; ++y) { \ |
373 | 355k | uint32_t packed_pixels = 0; \ |
374 | 355k | int x; \ |
375 | 222M | for (x = 0; x < width; ++x) { \ |
376 | 221M | /* We need to load fresh 'packed_pixels' once every */ \ |
377 | 221M | /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \ |
378 | 221M | /* is a power of 2, so can just use a mask for that, instead of */ \ |
379 | 221M | /* decrementing a counter. */ \ |
380 | 221M | if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \ |
381 | 221M | *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \ |
382 | 221M | packed_pixels >>= bits_per_pixel; \ |
383 | 221M | } \ |
384 | 355k | } \ |
385 | 22.6k | } else { \ |
386 | 16.9k | VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \ |
387 | 16.9k | } \ |
388 | 39.6k | } VP8LColorIndexInverseTransformAlpha Line | Count | Source | 363 | 5.79k | TYPE* dst) { \ | 364 | 5.79k | int y; \ | 365 | 5.79k | const int bits_per_pixel = 8 >> transform->bits; \ | 366 | 5.79k | const int width = transform->xsize; \ | 367 | 5.79k | const uint32_t* const color_map = transform->data; \ | 368 | 5.79k | if (bits_per_pixel < 8) { \ | 369 | 3.05k | const int pixels_per_byte = 1 << transform->bits; \ | 370 | 3.05k | const int count_mask = pixels_per_byte - 1; \ | 371 | 3.05k | const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \ | 372 | 49.7k | for (y = y_start; y < y_end; ++y) { \ | 373 | 46.7k | uint32_t packed_pixels = 0; \ | 374 | 46.7k | int x; \ | 375 | 2.59M | for (x = 0; x < width; ++x) { \ | 376 | 2.54M | /* We need to load fresh 'packed_pixels' once every */ \ | 377 | 2.54M | /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \ | 378 | 2.54M | /* is a power of 2, so can just use a mask for that, instead of */ \ | 379 | 2.54M | /* decrementing a counter. */ \ | 380 | 2.54M | if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \ | 381 | 2.54M | *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \ | 382 | 2.54M | packed_pixels >>= bits_per_pixel; \ | 383 | 2.54M | } \ | 384 | 46.7k | } \ | 385 | 3.05k | } else { \ | 386 | 2.73k | VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \ | 387 | 2.73k | } \ | 388 | 5.79k | } |
lossless.c:ColorIndexInverseTransform_C Line | Count | Source | 363 | 33.8k | TYPE* dst) { \ | 364 | 33.8k | int y; \ | 365 | 33.8k | const int bits_per_pixel = 8 >> transform->bits; \ | 366 | 33.8k | const int width = transform->xsize; \ | 367 | 33.8k | const uint32_t* const color_map = transform->data; \ | 368 | 33.8k | if (bits_per_pixel < 8) { \ | 369 | 19.6k | const int pixels_per_byte = 1 << transform->bits; \ | 370 | 19.6k | const int count_mask = pixels_per_byte - 1; \ | 371 | 19.6k | const uint32_t bit_mask = (1 << bits_per_pixel) - 1; \ | 372 | 328k | for (y = y_start; y < y_end; ++y) { \ | 373 | 309k | uint32_t packed_pixels = 0; \ | 374 | 309k | int x; \ | 375 | 219M | for (x = 0; x < width; ++x) { \ | 376 | 219M | /* We need to load fresh 'packed_pixels' once every */ \ | 377 | 219M | /* 'pixels_per_byte' increments of x. Fortunately, pixels_per_byte */ \ | 378 | 219M | /* is a power of 2, so can just use a mask for that, instead of */ \ | 379 | 219M | /* decrementing a counter. */ \ | 380 | 219M | if ((x & count_mask) == 0) packed_pixels = GET_INDEX(*src++); \ | 381 | 219M | *dst++ = GET_VALUE(color_map[packed_pixels & bit_mask]); \ | 382 | 219M | packed_pixels >>= bits_per_pixel; \ | 383 | 219M | } \ | 384 | 309k | } \ | 385 | 19.6k | } else { \ | 386 | 14.1k | VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width); \ | 387 | 14.1k | } \ | 388 | 33.8k | } |
|
389 | | |
390 | | COLOR_INDEX_INVERSE(ColorIndexInverseTransform_C, MapARGB_C, static, |
391 | | uint32_t, 32b, VP8GetARGBIndex, VP8GetARGBValue) |
392 | | COLOR_INDEX_INVERSE(VP8LColorIndexInverseTransformAlpha, MapAlpha_C, , |
393 | | uint8_t, 8b, VP8GetAlphaIndex, VP8GetAlphaValue) |
394 | | |
395 | | #undef COLOR_INDEX_INVERSE |
396 | | |
397 | | void VP8LInverseTransform(const VP8LTransform* const transform, |
398 | | int row_start, int row_end, |
399 | 122k | const uint32_t* const in, uint32_t* const out) { |
400 | 122k | const int width = transform->xsize; |
401 | 122k | assert(row_start < row_end); |
402 | 122k | assert(row_end <= transform->ysize); |
403 | 122k | switch (transform->type) { |
404 | 12.9k | case SUBTRACT_GREEN_TRANSFORM: |
405 | 12.9k | VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out); |
406 | 12.9k | break; |
407 | 73.6k | case PREDICTOR_TRANSFORM: |
408 | 73.6k | PredictorInverseTransform_C(transform, row_start, row_end, in, out); |
409 | 73.6k | if (row_end != transform->ysize) { |
410 | | // The last predicted row in this iteration will be the top-pred row |
411 | | // for the first row in next iteration. |
412 | 71.9k | memcpy(out - width, out + (row_end - row_start - 1) * width, |
413 | 71.9k | width * sizeof(*out)); |
414 | 71.9k | } |
415 | 73.6k | break; |
416 | 1.71k | case CROSS_COLOR_TRANSFORM: |
417 | 1.71k | ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out); |
418 | 1.71k | break; |
419 | 33.8k | case COLOR_INDEXING_TRANSFORM: |
420 | 33.8k | if (in == out && transform->bits > 0) { |
421 | | // Move packed pixels to the end of unpacked region, so that unpacking |
422 | | // can occur seamlessly. |
423 | | // Also, note that this is the only transform that applies on |
424 | | // the effective width of VP8LSubSampleSize(xsize, bits). All other |
425 | | // transforms work on effective width of 'xsize'. |
426 | 3.23k | const int out_stride = (row_end - row_start) * width; |
427 | 3.23k | const int in_stride = (row_end - row_start) * |
428 | 3.23k | VP8LSubSampleSize(transform->xsize, transform->bits); |
429 | 3.23k | uint32_t* const src = out + out_stride - in_stride; |
430 | 3.23k | memmove(src, out, in_stride * sizeof(*src)); |
431 | 3.23k | ColorIndexInverseTransform_C(transform, row_start, row_end, src, out); |
432 | 30.5k | } else { |
433 | 30.5k | ColorIndexInverseTransform_C(transform, row_start, row_end, in, out); |
434 | 30.5k | } |
435 | 33.8k | break; |
436 | 122k | } |
437 | 122k | } |
438 | | |
439 | | //------------------------------------------------------------------------------ |
440 | | // Color space conversion. |
441 | | |
442 | 0 | static int is_big_endian(void) { |
443 | 0 | static const union { |
444 | 0 | uint16_t w; |
445 | 0 | uint8_t b[2]; |
446 | 0 | } tmp = { 1 }; |
447 | 0 | return (tmp.b[0] != 1); |
448 | 0 | } |
449 | | |
450 | | void VP8LConvertBGRAToRGB_C(const uint32_t* WEBP_RESTRICT src, |
451 | 0 | int num_pixels, uint8_t* WEBP_RESTRICT dst) { |
452 | 0 | const uint32_t* const src_end = src + num_pixels; |
453 | 0 | while (src < src_end) { |
454 | 0 | const uint32_t argb = *src++; |
455 | 0 | *dst++ = (argb >> 16) & 0xff; |
456 | 0 | *dst++ = (argb >> 8) & 0xff; |
457 | 0 | *dst++ = (argb >> 0) & 0xff; |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | | void VP8LConvertBGRAToRGBA_C(const uint32_t* WEBP_RESTRICT src, |
462 | 1.89M | int num_pixels, uint8_t* WEBP_RESTRICT dst) { |
463 | 1.89M | const uint32_t* const src_end = src + num_pixels; |
464 | 8.99M | while (src < src_end) { |
465 | 7.09M | const uint32_t argb = *src++; |
466 | 7.09M | *dst++ = (argb >> 16) & 0xff; |
467 | 7.09M | *dst++ = (argb >> 8) & 0xff; |
468 | 7.09M | *dst++ = (argb >> 0) & 0xff; |
469 | 7.09M | *dst++ = (argb >> 24) & 0xff; |
470 | 7.09M | } |
471 | 1.89M | } |
472 | | |
473 | | void VP8LConvertBGRAToRGBA4444_C(const uint32_t* WEBP_RESTRICT src, |
474 | 0 | int num_pixels, uint8_t* WEBP_RESTRICT dst) { |
475 | 0 | const uint32_t* const src_end = src + num_pixels; |
476 | 0 | while (src < src_end) { |
477 | 0 | const uint32_t argb = *src++; |
478 | 0 | const uint8_t rg = ((argb >> 16) & 0xf0) | ((argb >> 12) & 0xf); |
479 | 0 | const uint8_t ba = ((argb >> 0) & 0xf0) | ((argb >> 28) & 0xf); |
480 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
481 | | *dst++ = ba; |
482 | | *dst++ = rg; |
483 | | #else |
484 | 0 | *dst++ = rg; |
485 | 0 | *dst++ = ba; |
486 | 0 | #endif |
487 | 0 | } |
488 | 0 | } |
489 | | |
490 | | void VP8LConvertBGRAToRGB565_C(const uint32_t* WEBP_RESTRICT src, |
491 | 0 | int num_pixels, uint8_t* WEBP_RESTRICT dst) { |
492 | 0 | const uint32_t* const src_end = src + num_pixels; |
493 | 0 | while (src < src_end) { |
494 | 0 | const uint32_t argb = *src++; |
495 | 0 | const uint8_t rg = ((argb >> 16) & 0xf8) | ((argb >> 13) & 0x7); |
496 | 0 | const uint8_t gb = ((argb >> 5) & 0xe0) | ((argb >> 3) & 0x1f); |
497 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
498 | | *dst++ = gb; |
499 | | *dst++ = rg; |
500 | | #else |
501 | 0 | *dst++ = rg; |
502 | 0 | *dst++ = gb; |
503 | 0 | #endif |
504 | 0 | } |
505 | 0 | } |
506 | | |
507 | | void VP8LConvertBGRAToBGR_C(const uint32_t* WEBP_RESTRICT src, |
508 | 0 | int num_pixels, uint8_t* WEBP_RESTRICT dst) { |
509 | 0 | const uint32_t* const src_end = src + num_pixels; |
510 | 0 | while (src < src_end) { |
511 | 0 | const uint32_t argb = *src++; |
512 | 0 | *dst++ = (argb >> 0) & 0xff; |
513 | 0 | *dst++ = (argb >> 8) & 0xff; |
514 | 0 | *dst++ = (argb >> 16) & 0xff; |
515 | 0 | } |
516 | 0 | } |
517 | | |
518 | | static void CopyOrSwap(const uint32_t* WEBP_RESTRICT src, int num_pixels, |
519 | 0 | uint8_t* WEBP_RESTRICT dst, int swap_on_big_endian) { |
520 | 0 | if (is_big_endian() == swap_on_big_endian) { |
521 | 0 | const uint32_t* const src_end = src + num_pixels; |
522 | 0 | while (src < src_end) { |
523 | 0 | const uint32_t argb = *src++; |
524 | 0 | WebPUint32ToMem(dst, BSwap32(argb)); |
525 | 0 | dst += sizeof(argb); |
526 | 0 | } |
527 | 0 | } else { |
528 | 0 | memcpy(dst, src, num_pixels * sizeof(*src)); |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels, |
533 | 2.14M | WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) { |
534 | 2.14M | switch (out_colorspace) { |
535 | 0 | case MODE_RGB: |
536 | 0 | VP8LConvertBGRAToRGB(in_data, num_pixels, rgba); |
537 | 0 | break; |
538 | 2.14M | case MODE_RGBA: |
539 | 2.14M | VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba); |
540 | 2.14M | break; |
541 | 0 | case MODE_rgbA: |
542 | 0 | VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba); |
543 | 0 | WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0); |
544 | 0 | break; |
545 | 0 | case MODE_BGR: |
546 | 0 | VP8LConvertBGRAToBGR(in_data, num_pixels, rgba); |
547 | 0 | break; |
548 | 0 | case MODE_BGRA: |
549 | 0 | CopyOrSwap(in_data, num_pixels, rgba, 1); |
550 | 0 | break; |
551 | 0 | case MODE_bgrA: |
552 | 0 | CopyOrSwap(in_data, num_pixels, rgba, 1); |
553 | 0 | WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0); |
554 | 0 | break; |
555 | 0 | case MODE_ARGB: |
556 | 0 | CopyOrSwap(in_data, num_pixels, rgba, 0); |
557 | 0 | break; |
558 | 0 | case MODE_Argb: |
559 | 0 | CopyOrSwap(in_data, num_pixels, rgba, 0); |
560 | 0 | WebPApplyAlphaMultiply(rgba, 1, num_pixels, 1, 0); |
561 | 0 | break; |
562 | 0 | case MODE_RGBA_4444: |
563 | 0 | VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba); |
564 | 0 | break; |
565 | 0 | case MODE_rgbA_4444: |
566 | 0 | VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba); |
567 | 0 | WebPApplyAlphaMultiply4444(rgba, num_pixels, 1, 0); |
568 | 0 | break; |
569 | 0 | case MODE_RGB_565: |
570 | 0 | VP8LConvertBGRAToRGB565(in_data, num_pixels, rgba); |
571 | 0 | break; |
572 | 0 | default: |
573 | 0 | assert(0); // Code flow should not reach here. |
574 | 2.14M | } |
575 | 2.14M | } |
576 | | |
577 | | //------------------------------------------------------------------------------ |
578 | | |
579 | | VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed; |
580 | | VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed_SSE; |
581 | | VP8LPredictorAddSubFunc VP8LPredictorsAdd[16]; |
582 | | VP8LPredictorAddSubFunc VP8LPredictorsAdd_SSE[16]; |
583 | | VP8LPredictorFunc VP8LPredictors[16]; |
584 | | |
585 | | // exposed plain-C implementations |
586 | | VP8LPredictorAddSubFunc VP8LPredictorsAdd_C[16]; |
587 | | |
588 | | VP8LTransformColorInverseFunc VP8LTransformColorInverse; |
589 | | VP8LTransformColorInverseFunc VP8LTransformColorInverse_SSE; |
590 | | |
591 | | VP8LConvertFunc VP8LConvertBGRAToRGB; |
592 | | VP8LConvertFunc VP8LConvertBGRAToRGB_SSE; |
593 | | VP8LConvertFunc VP8LConvertBGRAToRGBA; |
594 | | VP8LConvertFunc VP8LConvertBGRAToRGBA_SSE; |
595 | | VP8LConvertFunc VP8LConvertBGRAToRGBA4444; |
596 | | VP8LConvertFunc VP8LConvertBGRAToRGB565; |
597 | | VP8LConvertFunc VP8LConvertBGRAToBGR; |
598 | | |
599 | | VP8LMapARGBFunc VP8LMapColor32b; |
600 | | VP8LMapAlphaFunc VP8LMapColor8b; |
601 | | |
602 | | extern VP8CPUInfo VP8GetCPUInfo; |
603 | | extern void VP8LDspInitSSE2(void); |
604 | | extern void VP8LDspInitSSE41(void); |
605 | | extern void VP8LDspInitAVX2(void); |
606 | | extern void VP8LDspInitNEON(void); |
607 | | extern void VP8LDspInitMIPSdspR2(void); |
608 | | extern void VP8LDspInitMSA(void); |
609 | | |
610 | 3 | #define COPY_PREDICTOR_ARRAY(IN, OUT) do { \ |
611 | 3 | (OUT)[0] = IN##0_C; \ |
612 | 3 | (OUT)[1] = IN##1_C; \ |
613 | 3 | (OUT)[2] = IN##2_C; \ |
614 | 3 | (OUT)[3] = IN##3_C; \ |
615 | 3 | (OUT)[4] = IN##4_C; \ |
616 | 3 | (OUT)[5] = IN##5_C; \ |
617 | 3 | (OUT)[6] = IN##6_C; \ |
618 | 3 | (OUT)[7] = IN##7_C; \ |
619 | 3 | (OUT)[8] = IN##8_C; \ |
620 | 3 | (OUT)[9] = IN##9_C; \ |
621 | 3 | (OUT)[10] = IN##10_C; \ |
622 | 3 | (OUT)[11] = IN##11_C; \ |
623 | 3 | (OUT)[12] = IN##12_C; \ |
624 | 3 | (OUT)[13] = IN##13_C; \ |
625 | 3 | (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \ |
626 | 3 | (OUT)[15] = IN##0_C; \ |
627 | 3 | } while (0); |
628 | | |
629 | 1 | WEBP_DSP_INIT_FUNC(VP8LDspInit) { |
630 | 1 | COPY_PREDICTOR_ARRAY(VP8LPredictor, VP8LPredictors) |
631 | 1 | COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd) |
632 | 1 | COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C) |
633 | | |
634 | 1 | #if !WEBP_NEON_OMIT_C_CODE |
635 | 1 | VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C; |
636 | | |
637 | 1 | VP8LTransformColorInverse = VP8LTransformColorInverse_C; |
638 | | |
639 | 1 | VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C; |
640 | 1 | VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C; |
641 | 1 | VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C; |
642 | 1 | #endif |
643 | | |
644 | 1 | VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C; |
645 | 1 | VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C; |
646 | | |
647 | 1 | VP8LMapColor32b = MapARGB_C; |
648 | 1 | VP8LMapColor8b = MapAlpha_C; |
649 | | |
650 | | // If defined, use CPUInfo() to overwrite some pointers with faster versions. |
651 | 1 | if (VP8GetCPUInfo != NULL) { |
652 | 1 | #if defined(WEBP_HAVE_SSE2) |
653 | 1 | if (VP8GetCPUInfo(kSSE2)) { |
654 | 1 | VP8LDspInitSSE2(); |
655 | 1 | #if defined(WEBP_HAVE_SSE41) |
656 | 1 | if (VP8GetCPUInfo(kSSE4_1)) { |
657 | 1 | VP8LDspInitSSE41(); |
658 | | #if defined(WEBP_HAVE_AVX2) |
659 | | if (VP8GetCPUInfo(kAVX2)) { |
660 | | VP8LDspInitAVX2(); |
661 | | } |
662 | | #endif |
663 | 1 | } |
664 | 1 | #endif |
665 | 1 | } |
666 | 1 | #endif |
667 | | #if defined(WEBP_USE_MIPS_DSP_R2) |
668 | | if (VP8GetCPUInfo(kMIPSdspR2)) { |
669 | | VP8LDspInitMIPSdspR2(); |
670 | | } |
671 | | #endif |
672 | | #if defined(WEBP_USE_MSA) |
673 | | if (VP8GetCPUInfo(kMSA)) { |
674 | | VP8LDspInitMSA(); |
675 | | } |
676 | | #endif |
677 | 1 | } |
678 | | |
679 | | #if defined(WEBP_HAVE_NEON) |
680 | | if (WEBP_NEON_OMIT_C_CODE || |
681 | | (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) { |
682 | | VP8LDspInitNEON(); |
683 | | } |
684 | | #endif |
685 | | |
686 | 1 | assert(VP8LAddGreenToBlueAndRed != NULL); |
687 | 1 | assert(VP8LTransformColorInverse != NULL); |
688 | 1 | assert(VP8LConvertBGRAToRGBA != NULL); |
689 | 1 | assert(VP8LConvertBGRAToRGB != NULL); |
690 | 1 | assert(VP8LConvertBGRAToBGR != NULL); |
691 | 1 | assert(VP8LConvertBGRAToRGBA4444 != NULL); |
692 | 1 | assert(VP8LConvertBGRAToRGB565 != NULL); |
693 | 1 | assert(VP8LMapColor32b != NULL); |
694 | 1 | assert(VP8LMapColor8b != NULL); |
695 | 1 | } |
696 | | #undef COPY_PREDICTOR_ARRAY |
697 | | |
698 | | //------------------------------------------------------------------------------ |