Update metrics verification for dev-proxy.
[chromium-blink-merge.git] / third_party / libwebp / enc / picture_csp.c
blob7964f25773bf19fe9897a6dad42395f6f21e4de0
1 // Copyright 2014 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 // WebPPicture utils for colorspace conversion
12 // Author: Skal (pascal.massimino@gmail.com)
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <math.h>
18 #include "./vp8enci.h"
19 #include "../utils/random.h"
20 #include "../dsp/yuv.h"
22 // Uncomment to disable gamma-compression during RGB->U/V averaging
23 #define USE_GAMMA_COMPRESSION
25 static const union {
26 uint32_t argb;
27 uint8_t bytes[4];
28 } test_endian = { 0xff000000u };
29 #define ALPHA_IS_LAST (test_endian.bytes[3] == 0xff)
31 static WEBP_INLINE uint32_t MakeARGB32(int a, int r, int g, int b) {
32 return (((uint32_t)a << 24) | (r << 16) | (g << 8) | b);
35 //------------------------------------------------------------------------------
36 // Detection of non-trivial transparency
38 // Returns true if alpha[] has non-0xff values.
39 static int CheckNonOpaque(const uint8_t* alpha, int width, int height,
40 int x_step, int y_step) {
41 if (alpha == NULL) return 0;
42 while (height-- > 0) {
43 int x;
44 for (x = 0; x < width * x_step; x += x_step) {
45 if (alpha[x] != 0xff) return 1; // TODO(skal): check 4/8 bytes at a time.
47 alpha += y_step;
49 return 0;
52 // Checking for the presence of non-opaque alpha.
53 int WebPPictureHasTransparency(const WebPPicture* picture) {
54 if (picture == NULL) return 0;
55 if (!picture->use_argb) {
56 return CheckNonOpaque(picture->a, picture->width, picture->height,
57 1, picture->a_stride);
58 } else {
59 int x, y;
60 const uint32_t* argb = picture->argb;
61 if (argb == NULL) return 0;
62 for (y = 0; y < picture->height; ++y) {
63 for (x = 0; x < picture->width; ++x) {
64 if (argb[x] < 0xff000000u) return 1; // test any alpha values != 0xff
66 argb += picture->argb_stride;
69 return 0;
72 //------------------------------------------------------------------------------
73 // RGB -> YUV conversion
75 static int RGBToY(int r, int g, int b, VP8Random* const rg) {
76 return VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));
79 static int RGBToU(int r, int g, int b, VP8Random* const rg) {
80 return VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
83 static int RGBToV(int r, int g, int b, VP8Random* const rg) {
84 return VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
87 //------------------------------------------------------------------------------
89 #if defined(USE_GAMMA_COMPRESSION)
91 // gamma-compensates loss of resolution during chroma subsampling
92 #define kGamma 0.80
93 #define kGammaFix 12 // fixed-point precision for linear values
94 #define kGammaScale ((1 << kGammaFix) - 1)
95 #define kGammaTabFix 7 // fixed-point fractional bits precision
96 #define kGammaTabScale (1 << kGammaTabFix)
97 #define kGammaTabRounder (kGammaTabScale >> 1)
98 #define kGammaTabSize (1 << (kGammaFix - kGammaTabFix))
100 static int kLinearToGammaTab[kGammaTabSize + 1];
101 static uint16_t kGammaToLinearTab[256];
102 static int kGammaTablesOk = 0;
104 static void InitGammaTables(void) {
105 if (!kGammaTablesOk) {
106 int v;
107 const double scale = 1. / kGammaScale;
108 for (v = 0; v <= 255; ++v) {
109 kGammaToLinearTab[v] =
110 (uint16_t)(pow(v / 255., kGamma) * kGammaScale + .5);
112 for (v = 0; v <= kGammaTabSize; ++v) {
113 const double x = scale * (v << kGammaTabFix);
114 kLinearToGammaTab[v] = (int)(pow(x, 1. / kGamma) * 255. + .5);
116 kGammaTablesOk = 1;
120 static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {
121 return kGammaToLinearTab[v];
124 // Convert a linear value 'v' to YUV_FIX+2 fixed-point precision
125 // U/V value, suitable for RGBToU/V calls.
126 static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
127 const int v = base_value << shift; // final uplifted value
128 const int tab_pos = v >> (kGammaTabFix + 2); // integer part
129 const int x = v & ((kGammaTabScale << 2) - 1); // fractional part
130 const int v0 = kLinearToGammaTab[tab_pos];
131 const int v1 = kLinearToGammaTab[tab_pos + 1];
132 const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate
133 return (y + kGammaTabRounder) >> kGammaTabFix; // descale
136 #else
138 static void InitGammaTables(void) {}
139 static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }
140 static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
141 return (int)(base_value << shift);
144 #endif // USE_GAMMA_COMPRESSION
146 //------------------------------------------------------------------------------
148 #define SUM4(ptr) LinearToGamma( \
149 GammaToLinear((ptr)[0]) + \
150 GammaToLinear((ptr)[step]) + \
151 GammaToLinear((ptr)[rgb_stride]) + \
152 GammaToLinear((ptr)[rgb_stride + step]), 0) \
154 #define SUM2H(ptr) \
155 LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[step]), 1)
156 #define SUM2V(ptr) \
157 LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)
158 #define SUM1(ptr) \
159 LinearToGamma(GammaToLinear((ptr)[0]), 2)
161 #define RGB_TO_UV(x, y, SUM) { \
162 const int src = (2 * (step * (x) + (y) * rgb_stride)); \
163 const int dst = (x) + (y) * picture->uv_stride; \
164 const int r = SUM(r_ptr + src); \
165 const int g = SUM(g_ptr + src); \
166 const int b = SUM(b_ptr + src); \
167 picture->u[dst] = RGBToU(r, g, b, &rg); \
168 picture->v[dst] = RGBToV(r, g, b, &rg); \
171 static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
172 const uint8_t* const g_ptr,
173 const uint8_t* const b_ptr,
174 const uint8_t* const a_ptr,
175 int step, // bytes per pixel
176 int rgb_stride, // bytes per scanline
177 float dithering,
178 WebPPicture* const picture) {
179 int x, y;
180 const int width = picture->width;
181 const int height = picture->height;
182 const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);
183 VP8Random rg;
185 if (has_alpha) {
186 picture->colorspace |= WEBP_CSP_ALPHA_BIT;
187 } else {
188 picture->colorspace &= WEBP_CSP_UV_MASK;
190 picture->use_argb = 0;
192 if (!WebPPictureAllocYUVA(picture, width, height)) return 0;
194 VP8InitRandom(&rg, dithering);
195 InitGammaTables();
197 // Import luma plane
198 for (y = 0; y < height; ++y) {
199 uint8_t* const dst = &picture->y[y * picture->y_stride];
200 for (x = 0; x < width; ++x) {
201 const int offset = step * x + y * rgb_stride;
202 dst[x] = RGBToY(r_ptr[offset], g_ptr[offset], b_ptr[offset], &rg);
206 // Downsample U/V plane
207 for (y = 0; y < (height >> 1); ++y) {
208 for (x = 0; x < (width >> 1); ++x) {
209 RGB_TO_UV(x, y, SUM4);
211 if (width & 1) {
212 RGB_TO_UV(x, y, SUM2V);
215 if (height & 1) {
216 for (x = 0; x < (width >> 1); ++x) {
217 RGB_TO_UV(x, y, SUM2H);
219 if (width & 1) {
220 RGB_TO_UV(x, y, SUM1);
224 if (has_alpha) {
225 assert(step >= 4);
226 assert(picture->a != NULL);
227 for (y = 0; y < height; ++y) {
228 for (x = 0; x < width; ++x) {
229 picture->a[x + y * picture->a_stride] =
230 a_ptr[step * x + y * rgb_stride];
234 return 1;
237 #undef SUM4
238 #undef SUM2V
239 #undef SUM2H
240 #undef SUM1
241 #undef RGB_TO_UV
243 //------------------------------------------------------------------------------
244 // call for ARGB->YUVA conversion
246 int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,
247 float dithering) {
248 if (picture == NULL) return 0;
249 if (picture->argb == NULL) {
250 return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
251 } else {
252 const uint8_t* const argb = (const uint8_t*)picture->argb;
253 const uint8_t* const r = ALPHA_IS_LAST ? argb + 2 : argb + 1;
254 const uint8_t* const g = ALPHA_IS_LAST ? argb + 1 : argb + 2;
255 const uint8_t* const b = ALPHA_IS_LAST ? argb + 0 : argb + 3;
256 const uint8_t* const a = ALPHA_IS_LAST ? argb + 3 : argb + 0;
258 picture->colorspace = colorspace;
259 return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,
260 dithering, picture);
264 int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {
265 return WebPPictureARGBToYUVADithered(picture, colorspace, 0.f);
268 //------------------------------------------------------------------------------
269 // call for YUVA -> ARGB conversion
271 int WebPPictureYUVAToARGB(WebPPicture* picture) {
272 if (picture == NULL) return 0;
273 if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {
274 return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
276 if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {
277 return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
279 if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {
280 return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
282 // Allocate a new argb buffer (discarding the previous one).
283 if (!WebPPictureAllocARGB(picture, picture->width, picture->height)) return 0;
284 picture->use_argb = 1;
286 // Convert
288 int y;
289 const int width = picture->width;
290 const int height = picture->height;
291 const int argb_stride = 4 * picture->argb_stride;
292 uint8_t* dst = (uint8_t*)picture->argb;
293 const uint8_t *cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;
294 WebPUpsampleLinePairFunc upsample = WebPGetLinePairConverter(ALPHA_IS_LAST);
296 // First row, with replicated top samples.
297 upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
298 cur_y += picture->y_stride;
299 dst += argb_stride;
300 // Center rows.
301 for (y = 1; y + 1 < height; y += 2) {
302 const uint8_t* const top_u = cur_u;
303 const uint8_t* const top_v = cur_v;
304 cur_u += picture->uv_stride;
305 cur_v += picture->uv_stride;
306 upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,
307 dst, dst + argb_stride, width);
308 cur_y += 2 * picture->y_stride;
309 dst += 2 * argb_stride;
311 // Last row (if needed), with replicated bottom samples.
312 if (height > 1 && !(height & 1)) {
313 upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
315 // Insert alpha values if needed, in replacement for the default 0xff ones.
316 if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {
317 for (y = 0; y < height; ++y) {
318 uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;
319 const uint8_t* const src = picture->a + y * picture->a_stride;
320 int x;
321 for (x = 0; x < width; ++x) {
322 argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);
327 return 1;
330 //------------------------------------------------------------------------------
331 // automatic import / conversion
333 static int Import(WebPPicture* const picture,
334 const uint8_t* const rgb, int rgb_stride,
335 int step, int swap_rb, int import_alpha) {
336 int y;
337 const uint8_t* const r_ptr = rgb + (swap_rb ? 2 : 0);
338 const uint8_t* const g_ptr = rgb + 1;
339 const uint8_t* const b_ptr = rgb + (swap_rb ? 0 : 2);
340 const uint8_t* const a_ptr = import_alpha ? rgb + 3 : NULL;
341 const int width = picture->width;
342 const int height = picture->height;
344 if (!picture->use_argb) {
345 return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,
346 0.f /* no dithering */, picture);
348 if (!WebPPictureAlloc(picture)) return 0;
350 assert(step >= (import_alpha ? 4 : 3));
351 for (y = 0; y < height; ++y) {
352 uint32_t* const dst = &picture->argb[y * picture->argb_stride];
353 int x;
354 for (x = 0; x < width; ++x) {
355 const int offset = step * x + y * rgb_stride;
356 dst[x] = MakeARGB32(import_alpha ? a_ptr[offset] : 0xff,
357 r_ptr[offset], g_ptr[offset], b_ptr[offset]);
360 return 1;
363 // Public API
365 int WebPPictureImportRGB(WebPPicture* picture,
366 const uint8_t* rgb, int rgb_stride) {
367 return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 0, 0) : 0;
370 int WebPPictureImportBGR(WebPPicture* picture,
371 const uint8_t* rgb, int rgb_stride) {
372 return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 1, 0) : 0;
375 int WebPPictureImportRGBA(WebPPicture* picture,
376 const uint8_t* rgba, int rgba_stride) {
377 return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 1) : 0;
380 int WebPPictureImportBGRA(WebPPicture* picture,
381 const uint8_t* rgba, int rgba_stride) {
382 return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 1) : 0;
385 int WebPPictureImportRGBX(WebPPicture* picture,
386 const uint8_t* rgba, int rgba_stride) {
387 return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 0) : 0;
390 int WebPPictureImportBGRX(WebPPicture* picture,
391 const uint8_t* rgba, int rgba_stride) {
392 return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 0) : 0;
395 //------------------------------------------------------------------------------