1// Copyright 2010 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// YUV->RGB conversion functions
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include "./yuv.h"
15
16#include <stdlib.h>
17
18#if defined(WEBP_YUV_USE_TABLE)
19
20static int done = 0;
21
22static WEBP_INLINE uint8_t clip(int v, int max_value) {
23  return v < 0 ? 0 : v > max_value ? max_value : v;
24}
25
26int16_t VP8kVToR[256], VP8kUToB[256];
27int32_t VP8kVToG[256], VP8kUToG[256];
28uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
29uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
30
31WEBP_TSAN_IGNORE_FUNCTION void VP8YUVInit(void) {
32  int i;
33  if (done) {
34    return;
35  }
36#ifndef USE_YUVj
37  for (i = 0; i < 256; ++i) {
38    VP8kVToR[i] = (89858 * (i - 128) + YUV_HALF) >> YUV_FIX;
39    VP8kUToG[i] = -22014 * (i - 128) + YUV_HALF;
40    VP8kVToG[i] = -45773 * (i - 128);
41    VP8kUToB[i] = (113618 * (i - 128) + YUV_HALF) >> YUV_FIX;
42  }
43  for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) {
44    const int k = ((i - 16) * 76283 + YUV_HALF) >> YUV_FIX;
45    VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255);
46    VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15);
47  }
48#else
49  for (i = 0; i < 256; ++i) {
50    VP8kVToR[i] = (91881 * (i - 128) + YUV_HALF) >> YUV_FIX;
51    VP8kUToG[i] = -22554 * (i - 128) + YUV_HALF;
52    VP8kVToG[i] = -46802 * (i - 128);
53    VP8kUToB[i] = (116130 * (i - 128) + YUV_HALF) >> YUV_FIX;
54  }
55  for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) {
56    const int k = i;
57    VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255);
58    VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15);
59  }
60#endif
61
62  done = 1;
63}
64
65#else
66
67WEBP_TSAN_IGNORE_FUNCTION void VP8YUVInit(void) {}
68
69#endif  // WEBP_YUV_USE_TABLE
70
71//-----------------------------------------------------------------------------
72// Plain-C version
73
74#define ROW_FUNC(FUNC_NAME, FUNC, XSTEP)                                       \
75static void FUNC_NAME(const uint8_t* y,                                        \
76                      const uint8_t* u, const uint8_t* v,                      \
77                      uint8_t* dst, int len) {                                 \
78  const uint8_t* const end = dst + (len & ~1) * XSTEP;                         \
79  while (dst != end) {                                                         \
80    FUNC(y[0], u[0], v[0], dst);                                               \
81    FUNC(y[1], u[0], v[0], dst + XSTEP);                                       \
82    y += 2;                                                                    \
83    ++u;                                                                       \
84    ++v;                                                                       \
85    dst += 2 * XSTEP;                                                          \
86  }                                                                            \
87  if (len & 1) {                                                               \
88    FUNC(y[0], u[0], v[0], dst);                                               \
89  }                                                                            \
90}                                                                              \
91
92// All variants implemented.
93ROW_FUNC(YuvToRgbRow,      VP8YuvToRgb,  3)
94ROW_FUNC(YuvToBgrRow,      VP8YuvToBgr,  3)
95ROW_FUNC(YuvToRgbaRow,     VP8YuvToRgba, 4)
96ROW_FUNC(YuvToBgraRow,     VP8YuvToBgra, 4)
97ROW_FUNC(YuvToArgbRow,     VP8YuvToArgb, 4)
98ROW_FUNC(YuvToRgba4444Row, VP8YuvToRgba4444, 2)
99ROW_FUNC(YuvToRgb565Row,   VP8YuvToRgb565, 2)
100
101#undef ROW_FUNC
102
103// Main call for processing a plane with a WebPSamplerRowFunc function:
104void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
105                             const uint8_t* u, const uint8_t* v, int uv_stride,
106                             uint8_t* dst, int dst_stride,
107                             int width, int height, WebPSamplerRowFunc func) {
108  int j;
109  for (j = 0; j < height; ++j) {
110    func(y, u, v, dst, width);
111    y += y_stride;
112    if (j & 1) {
113      u += uv_stride;
114      v += uv_stride;
115    }
116    dst += dst_stride;
117  }
118}
119
120//-----------------------------------------------------------------------------
121// Main call
122
123WebPSamplerRowFunc WebPSamplers[MODE_LAST];
124
125extern void WebPInitSamplersSSE2(void);
126extern void WebPInitSamplersMIPS32(void);
127extern void WebPInitSamplersMIPSdspR2(void);
128
129static volatile VP8CPUInfo yuv_last_cpuinfo_used =
130    (VP8CPUInfo)&yuv_last_cpuinfo_used;
131
132WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplers(void) {
133  if (yuv_last_cpuinfo_used == VP8GetCPUInfo) return;
134
135  WebPSamplers[MODE_RGB]       = YuvToRgbRow;
136  WebPSamplers[MODE_RGBA]      = YuvToRgbaRow;
137  WebPSamplers[MODE_BGR]       = YuvToBgrRow;
138  WebPSamplers[MODE_BGRA]      = YuvToBgraRow;
139  WebPSamplers[MODE_ARGB]      = YuvToArgbRow;
140  WebPSamplers[MODE_RGBA_4444] = YuvToRgba4444Row;
141  WebPSamplers[MODE_RGB_565]   = YuvToRgb565Row;
142  WebPSamplers[MODE_rgbA]      = YuvToRgbaRow;
143  WebPSamplers[MODE_bgrA]      = YuvToBgraRow;
144  WebPSamplers[MODE_Argb]      = YuvToArgbRow;
145  WebPSamplers[MODE_rgbA_4444] = YuvToRgba4444Row;
146
147  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
148  if (VP8GetCPUInfo != NULL) {
149#if defined(WEBP_USE_SSE2)
150    if (VP8GetCPUInfo(kSSE2)) {
151      WebPInitSamplersSSE2();
152    }
153#endif  // WEBP_USE_SSE2
154#if defined(WEBP_USE_MIPS32)
155    if (VP8GetCPUInfo(kMIPS32)) {
156      WebPInitSamplersMIPS32();
157    }
158#endif  // WEBP_USE_MIPS32
159#if defined(WEBP_USE_MIPS_DSP_R2)
160    if (VP8GetCPUInfo(kMIPSdspR2)) {
161      WebPInitSamplersMIPSdspR2();
162    }
163#endif  // WEBP_USE_MIPS_DSP_R2
164  }
165  yuv_last_cpuinfo_used = VP8GetCPUInfo;
166}
167
168//-----------------------------------------------------------------------------
169// ARGB -> YUV converters
170
171static void ConvertARGBToY(const uint32_t* argb, uint8_t* y, int width) {
172  int i;
173  for (i = 0; i < width; ++i) {
174    const uint32_t p = argb[i];
175    y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >>  0) & 0xff,
176                     YUV_HALF);
177  }
178}
179
180void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
181                           int src_width, int do_store) {
182  // No rounding. Last pixel is dealt with separately.
183  const int uv_width = src_width >> 1;
184  int i;
185  for (i = 0; i < uv_width; ++i) {
186    const uint32_t v0 = argb[2 * i + 0];
187    const uint32_t v1 = argb[2 * i + 1];
188    // VP8RGBToU/V expects four accumulated pixels. Hence we need to
189    // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
190    const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
191    const int g = ((v0 >>  7) & 0x1fe) + ((v1 >>  7) & 0x1fe);
192    const int b = ((v0 <<  1) & 0x1fe) + ((v1 <<  1) & 0x1fe);
193    const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
194    const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
195    if (do_store) {
196      u[i] = tmp_u;
197      v[i] = tmp_v;
198    } else {
199      // Approximated average-of-four. But it's an acceptable diff.
200      u[i] = (u[i] + tmp_u + 1) >> 1;
201      v[i] = (v[i] + tmp_v + 1) >> 1;
202    }
203  }
204  if (src_width & 1) {       // last pixel
205    const uint32_t v0 = argb[2 * i + 0];
206    const int r = (v0 >> 14) & 0x3fc;
207    const int g = (v0 >>  6) & 0x3fc;
208    const int b = (v0 <<  2) & 0x3fc;
209    const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
210    const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
211    if (do_store) {
212      u[i] = tmp_u;
213      v[i] = tmp_v;
214    } else {
215      u[i] = (u[i] + tmp_u + 1) >> 1;
216      v[i] = (v[i] + tmp_v + 1) >> 1;
217    }
218  }
219}
220
221//-----------------------------------------------------------------------------
222
223static void ConvertRGB24ToY(const uint8_t* rgb, uint8_t* y, int width) {
224  int i;
225  for (i = 0; i < width; ++i, rgb += 3) {
226    y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
227  }
228}
229
230static void ConvertBGR24ToY(const uint8_t* bgr, uint8_t* y, int width) {
231  int i;
232  for (i = 0; i < width; ++i, bgr += 3) {
233    y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
234  }
235}
236
237void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
238                             uint8_t* u, uint8_t* v, int width) {
239  int i;
240  for (i = 0; i < width; i += 1, rgb += 4) {
241    const int r = rgb[0], g = rgb[1], b = rgb[2];
242    u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
243    v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
244  }
245}
246
247//-----------------------------------------------------------------------------
248
249#define MAX_Y ((1 << 10) - 1)    // 10b precision over 16b-arithmetic
250static uint16_t clip_y(int v) {
251  return (v < 0) ? 0 : (v > MAX_Y) ? MAX_Y : (uint16_t)v;
252}
253
254static uint64_t SharpYUVUpdateY_C(const uint16_t* ref, const uint16_t* src,
255                                  uint16_t* dst, int len) {
256  uint64_t diff = 0;
257  int i;
258  for (i = 0; i < len; ++i) {
259    const int diff_y = ref[i] - src[i];
260    const int new_y = (int)dst[i] + diff_y;
261    dst[i] = clip_y(new_y);
262    diff += (uint64_t)abs(diff_y);
263  }
264  return diff;
265}
266
267static void SharpYUVUpdateRGB_C(const int16_t* ref, const int16_t* src,
268                                int16_t* dst, int len) {
269  int i;
270  for (i = 0; i < len; ++i) {
271    const int diff_uv = ref[i] - src[i];
272    dst[i] += diff_uv;
273  }
274}
275
276static void SharpYUVFilterRow_C(const int16_t* A, const int16_t* B, int len,
277                                const uint16_t* best_y, uint16_t* out) {
278  int i;
279  for (i = 0; i < len; ++i, ++A, ++B) {
280    const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
281    const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
282    out[2 * i + 0] = clip_y(best_y[2 * i + 0] + v0);
283    out[2 * i + 1] = clip_y(best_y[2 * i + 1] + v1);
284  }
285}
286
287#undef MAX_Y
288
289//-----------------------------------------------------------------------------
290
291void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
292void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
293void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
294                              uint8_t* u, uint8_t* v, int width);
295
296void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
297void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
298                            int src_width, int do_store);
299
300uint64_t (*WebPSharpYUVUpdateY)(const uint16_t* ref, const uint16_t* src,
301                                uint16_t* dst, int len);
302void (*WebPSharpYUVUpdateRGB)(const int16_t* ref, const int16_t* src,
303                              int16_t* dst, int len);
304void (*WebPSharpYUVFilterRow)(const int16_t* A, const int16_t* B, int len,
305                              const uint16_t* best_y, uint16_t* out);
306
307static volatile VP8CPUInfo rgba_to_yuv_last_cpuinfo_used =
308    (VP8CPUInfo)&rgba_to_yuv_last_cpuinfo_used;
309
310extern void WebPInitConvertARGBToYUVSSE2(void);
311extern void WebPInitSharpYUVSSE2(void);
312
313WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUV(void) {
314  if (rgba_to_yuv_last_cpuinfo_used == VP8GetCPUInfo) return;
315
316  WebPConvertARGBToY = ConvertARGBToY;
317  WebPConvertARGBToUV = WebPConvertARGBToUV_C;
318
319  WebPConvertRGB24ToY = ConvertRGB24ToY;
320  WebPConvertBGR24ToY = ConvertBGR24ToY;
321
322  WebPConvertRGBA32ToUV = WebPConvertRGBA32ToUV_C;
323
324  WebPSharpYUVUpdateY = SharpYUVUpdateY_C;
325  WebPSharpYUVUpdateRGB = SharpYUVUpdateRGB_C;
326  WebPSharpYUVFilterRow = SharpYUVFilterRow_C;
327
328  if (VP8GetCPUInfo != NULL) {
329#if defined(WEBP_USE_SSE2)
330    if (VP8GetCPUInfo(kSSE2)) {
331      WebPInitConvertARGBToYUVSSE2();
332      WebPInitSharpYUVSSE2();
333    }
334#endif  // WEBP_USE_SSE2
335  }
336  rgba_to_yuv_last_cpuinfo_used = VP8GetCPUInfo;
337}
338