picture_csp.c revision af51b94a435132e9014c324e25fb686b3d07a8c8
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
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include <assert.h>
15#include <stdlib.h>
16#include <math.h>
17
18#include "./vp8enci.h"
19#include "../utils/random.h"
20#include "../dsp/yuv.h"
21
22// Uncomment to disable gamma-compression during RGB->U/V averaging
23#define USE_GAMMA_COMPRESSION
24
25static 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)
30
31static WEBP_INLINE uint32_t MakeARGB32(int a, int r, int g, int b) {
32  return (((uint32_t)a << 24) | (r << 16) | (g << 8) | b);
33}
34
35//------------------------------------------------------------------------------
36// Detection of non-trivial transparency
37
38// Returns true if alpha[] has non-0xff values.
39static 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.
46    }
47    alpha += y_step;
48  }
49  return 0;
50}
51
52// Checking for the presence of non-opaque alpha.
53int 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
65      }
66      argb += picture->argb_stride;
67    }
68  }
69  return 0;
70}
71
72//------------------------------------------------------------------------------
73// RGB -> YUV conversion
74
75static int RGBToY(int r, int g, int b, VP8Random* const rg) {
76  return VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));
77}
78
79static int RGBToU(int r, int g, int b, VP8Random* const rg) {
80  return VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
81}
82
83static int RGBToV(int r, int g, int b, VP8Random* const rg) {
84  return VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
85}
86
87//------------------------------------------------------------------------------
88
89#if defined(USE_GAMMA_COMPRESSION)
90
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))
99
100static int kLinearToGammaTab[kGammaTabSize + 1];
101static uint16_t kGammaToLinearTab[256];
102static int kGammaTablesOk = 0;
103
104static 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);
111    }
112    for (v = 0; v <= kGammaTabSize; ++v) {
113      const double x = scale * (v << kGammaTabFix);
114      kLinearToGammaTab[v] = (int)(pow(x, 1. / kGamma) * 255. + .5);
115    }
116    kGammaTablesOk = 1;
117  }
118}
119
120static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {
121  return kGammaToLinearTab[v];
122}
123
124// Convert a linear value 'v' to YUV_FIX+2 fixed-point precision
125// U/V value, suitable for RGBToU/V calls.
126static 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
134}
135
136#else
137
138static void InitGammaTables(void) {}
139static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }
140static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
141  return (int)(base_value << shift);
142}
143
144#endif    // USE_GAMMA_COMPRESSION
145
146//------------------------------------------------------------------------------
147
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)          \
153
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)
160
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);                \
169}
170
171static 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;
184
185  if (has_alpha) {
186    picture->colorspace |= WEBP_CSP_ALPHA_BIT;
187  } else {
188    picture->colorspace &= WEBP_CSP_UV_MASK;
189  }
190  picture->use_argb = 0;
191
192  if (!WebPPictureAllocYUVA(picture, width, height)) return 0;
193
194  VP8InitRandom(&rg, dithering);
195  InitGammaTables();
196
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);
203    }
204  }
205
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);
210    }
211    if (width & 1) {
212      RGB_TO_UV(x, y, SUM2V);
213    }
214  }
215  if (height & 1) {
216    for (x = 0; x < (width >> 1); ++x) {
217      RGB_TO_UV(x, y, SUM2H);
218    }
219    if (width & 1) {
220      RGB_TO_UV(x, y, SUM1);
221    }
222  }
223
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];
231      }
232    }
233  }
234  return 1;
235}
236
237#undef SUM4
238#undef SUM2V
239#undef SUM2H
240#undef SUM1
241#undef RGB_TO_UV
242
243//------------------------------------------------------------------------------
244// call for ARGB->YUVA conversion
245
246int 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;
257
258    picture->colorspace = colorspace;
259    return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,
260                              dithering, picture);
261  }
262}
263
264int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {
265  return WebPPictureARGBToYUVADithered(picture, colorspace, 0.f);
266}
267
268//------------------------------------------------------------------------------
269// call for YUVA -> ARGB conversion
270
271int 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);
275  }
276  if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {
277    return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
278  }
279  if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {
280    return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
281  }
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;
285
286  // Convert
287  {
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);
295
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;
310    }
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);
314    }
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);
323        }
324      }
325    }
326  }
327  return 1;
328}
329
330//------------------------------------------------------------------------------
331// automatic import / conversion
332
333static 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;
343
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);
347  }
348  if (!WebPPictureAlloc(picture)) return 0;
349
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]);
358    }
359  }
360  return 1;
361}
362
363// Public API
364
365int WebPPictureImportRGB(WebPPicture* picture,
366                         const uint8_t* rgb, int rgb_stride) {
367  return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 0, 0) : 0;
368}
369
370int WebPPictureImportBGR(WebPPicture* picture,
371                         const uint8_t* rgb, int rgb_stride) {
372  return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 1, 0) : 0;
373}
374
375int WebPPictureImportRGBA(WebPPicture* picture,
376                          const uint8_t* rgba, int rgba_stride) {
377  return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 1) : 0;
378}
379
380int WebPPictureImportBGRA(WebPPicture* picture,
381                          const uint8_t* rgba, int rgba_stride) {
382  return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 1) : 0;
383}
384
385int WebPPictureImportRGBX(WebPPicture* picture,
386                          const uint8_t* rgba, int rgba_stride) {
387  return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 0) : 0;
388}
389
390int WebPPictureImportBGRX(WebPPicture* picture,
391                          const uint8_t* rgba, int rgba_stride) {
392  return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 0) : 0;
393}
394
395//------------------------------------------------------------------------------
396