Coverage Report

Created: 2026-01-17 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/sharpyuv/sharpyuv.c
Line
Count
Source
1
// Copyright 2022 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
// Sharp RGB to YUV conversion.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include "./sharpyuv.h"
15
16
#include <assert.h>
17
#include <limits.h>
18
#include <stddef.h>
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include "./sharpyuv_cpu.h"
23
#include "./sharpyuv_dsp.h"
24
#include "./sharpyuv_gamma.h"
25
#include "webp/types.h"
26
27
//------------------------------------------------------------------------------
28
29
0
int SharpYuvGetVersion(void) { return SHARPYUV_VERSION; }
30
31
//------------------------------------------------------------------------------
32
// Sharp RGB->YUV conversion
33
34
static const int kNumIterations = 4;
35
36
699M
#define YUV_FIX 16  // fixed-point precision for RGB->YUV
37
static const int kYuvHalf = 1 << (YUV_FIX - 1);
38
39
// Max bit depth so that intermediate calculations fit in 16 bits.
40
static const int kMaxBitDepth = 14;
41
42
// Returns the precision shift to use based on the input rgb_bit_depth.
43
84.4M
static int GetPrecisionShift(int rgb_bit_depth) {
44
  // Try to add 2 bits of precision if it fits in kMaxBitDepth. Otherwise remove
45
  // bits if needed.
46
84.4M
  return ((rgb_bit_depth + 2) <= kMaxBitDepth) ? 2
47
84.4M
                                               : (kMaxBitDepth - rgb_bit_depth);
48
84.4M
}
49
50
typedef int16_t fixed_t;     // signed type with extra precision for UV
51
typedef uint16_t fixed_y_t;  // unsigned type with extra precision for W
52
53
//------------------------------------------------------------------------------
54
55
126M
static uint8_t clip_8b(fixed_t v) {
56
126M
  return (!(v & ~0xff)) ? (uint8_t)v : (v < 0) ? 0u : 255u;
57
126M
}
58
59
0
static uint16_t clip(fixed_t v, int max) {
60
0
  return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
61
0
}
62
63
16.5M
static fixed_y_t clip_bit_depth(int y, int bit_depth) {
64
16.5M
  const int max = (1 << bit_depth) - 1;
65
16.5M
  return (!(y & ~max)) ? (fixed_y_t)y : (y < 0) ? 0 : max;
66
16.5M
}
67
68
//------------------------------------------------------------------------------
69
70
445M
static int RGBToGray(int64_t r, int64_t g, int64_t b) {
71
445M
  const int64_t luma = 13933 * r + 46871 * g + 4732 * b + kYuvHalf;
72
445M
  return (int)(luma >> YUV_FIX);
73
445M
}
74
75
static uint32_t ScaleDown(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
76
                          int bit_depth,
77
216M
                          SharpYuvTransferFunctionType transfer_type) {
78
216M
  const uint32_t A = SharpYuvGammaToLinear(a, bit_depth, transfer_type);
79
216M
  const uint32_t B = SharpYuvGammaToLinear(b, bit_depth, transfer_type);
80
216M
  const uint32_t C = SharpYuvGammaToLinear(c, bit_depth, transfer_type);
81
216M
  const uint32_t D = SharpYuvGammaToLinear(d, bit_depth, transfer_type);
82
216M
  return SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth,
83
216M
                               transfer_type);
84
216M
}
85
86
static WEBP_INLINE void UpdateW(const fixed_y_t* src, fixed_y_t* dst, int w,
87
                                int bit_depth,
88
3.85M
                                SharpYuvTransferFunctionType transfer_type) {
89
3.85M
  int i = 0;
90
288M
  do {
91
288M
    const uint32_t R =
92
288M
        SharpYuvGammaToLinear(src[0 * w + i], bit_depth, transfer_type);
93
288M
    const uint32_t G =
94
288M
        SharpYuvGammaToLinear(src[1 * w + i], bit_depth, transfer_type);
95
288M
    const uint32_t B =
96
288M
        SharpYuvGammaToLinear(src[2 * w + i], bit_depth, transfer_type);
97
288M
    const uint32_t Y = RGBToGray(R, G, B);
98
288M
    dst[i] = (fixed_y_t)SharpYuvLinearToGamma(Y, bit_depth, transfer_type);
99
288M
  } while (++i < w);
100
3.85M
}
101
102
static void UpdateChroma(const fixed_y_t* src1, const fixed_y_t* src2,
103
                         fixed_t* dst, int uv_w, int bit_depth,
104
1.92M
                         SharpYuvTransferFunctionType transfer_type) {
105
1.92M
  int i = 0;
106
72.0M
  do {
107
72.0M
    const int r =
108
72.0M
        ScaleDown(src1[0 * uv_w + 0], src1[0 * uv_w + 1], src2[0 * uv_w + 0],
109
72.0M
                  src2[0 * uv_w + 1], bit_depth, transfer_type);
110
72.0M
    const int g =
111
72.0M
        ScaleDown(src1[2 * uv_w + 0], src1[2 * uv_w + 1], src2[2 * uv_w + 0],
112
72.0M
                  src2[2 * uv_w + 1], bit_depth, transfer_type);
113
72.0M
    const int b =
114
72.0M
        ScaleDown(src1[4 * uv_w + 0], src1[4 * uv_w + 1], src2[4 * uv_w + 0],
115
72.0M
                  src2[4 * uv_w + 1], bit_depth, transfer_type);
116
72.0M
    const int W = RGBToGray(r, g, b);
117
72.0M
    dst[0 * uv_w] = (fixed_t)(r - W);
118
72.0M
    dst[1 * uv_w] = (fixed_t)(g - W);
119
72.0M
    dst[2 * uv_w] = (fixed_t)(b - W);
120
72.0M
    dst += 1;
121
72.0M
    src1 += 2;
122
72.0M
    src2 += 2;
123
72.0M
  } while (++i < uv_w);
124
1.92M
}
125
126
1.10M
static void StoreGray(const fixed_y_t* rgb, fixed_y_t* y, int w) {
127
1.10M
  int i = 0;
128
1.10M
  assert(w > 0);
129
85.2M
  do {
130
85.2M
    y[i] = RGBToGray(rgb[0 * w + i], rgb[1 * w + i], rgb[2 * w + i]);
131
85.2M
  } while (++i < w);
132
1.10M
}
133
134
//------------------------------------------------------------------------------
135
136
16.5M
static WEBP_INLINE fixed_y_t Filter2(int A, int B, int W0, int bit_depth) {
137
16.5M
  const int v0 = (A * 3 + B + 2) >> 2;
138
16.5M
  return clip_bit_depth(v0 + W0, bit_depth);
139
16.5M
}
140
141
//------------------------------------------------------------------------------
142
143
253M
static WEBP_INLINE int Shift(int v, int shift) {
144
253M
  return (shift >= 0) ? (v << shift) : (v >> -shift);
145
253M
}
146
147
static void ImportOneRow(const uint8_t* const r_ptr, const uint8_t* const g_ptr,
148
                         const uint8_t* const b_ptr, int rgb_step,
149
                         int rgb_bit_depth, int pic_width,
150
1.09M
                         fixed_y_t* const dst) {
151
  // Convert the rgb_step from a number of bytes to a number of uint8_t or
152
  // uint16_t values depending the bit depth.
153
1.09M
  const int step = (rgb_bit_depth > 8) ? rgb_step / 2 : rgb_step;
154
1.09M
  int i = 0;
155
1.09M
  const int w = (pic_width + 1) & ~1;
156
84.3M
  do {
157
84.3M
    const int off = i * step;
158
84.3M
    const int shift = GetPrecisionShift(rgb_bit_depth);
159
84.3M
    if (rgb_bit_depth == 8) {
160
84.3M
      dst[i + 0 * w] = Shift(r_ptr[off], shift);
161
84.3M
      dst[i + 1 * w] = Shift(g_ptr[off], shift);
162
84.3M
      dst[i + 2 * w] = Shift(b_ptr[off], shift);
163
84.3M
    } else {
164
0
      dst[i + 0 * w] = Shift(((uint16_t*)r_ptr)[off], shift);
165
0
      dst[i + 1 * w] = Shift(((uint16_t*)g_ptr)[off], shift);
166
0
      dst[i + 2 * w] = Shift(((uint16_t*)b_ptr)[off], shift);
167
0
    }
168
84.3M
  } while (++i < pic_width);
169
1.09M
  if (pic_width & 1) {  // replicate rightmost pixel
170
452k
    dst[pic_width + 0 * w] = dst[pic_width + 0 * w - 1];
171
452k
    dst[pic_width + 1 * w] = dst[pic_width + 1 * w - 1];
172
452k
    dst[pic_width + 2 * w] = dst[pic_width + 2 * w - 1];
173
452k
  }
174
1.09M
}
175
176
static void InterpolateTwoRows(const fixed_y_t* const best_y,
177
                               const fixed_t* prev_uv, const fixed_t* cur_uv,
178
                               const fixed_t* next_uv, int w, fixed_y_t* out1,
179
1.37M
                               fixed_y_t* out2, int bit_depth) {
180
1.37M
  const int uv_w = w >> 1;
181
1.37M
  const int len = (w - 1) >> 1;  // length to filter
182
1.37M
  int k = 3;
183
5.50M
  while (k-- > 0) {  // process each R/G/B segments in turn
184
    // special boundary case for i==0
185
4.12M
    out1[0] = Filter2(cur_uv[0], prev_uv[0], best_y[0], bit_depth);
186
4.12M
    out2[0] = Filter2(cur_uv[0], next_uv[0], best_y[w], bit_depth);
187
188
4.12M
    SharpYuvFilterRow(cur_uv, prev_uv, len, best_y + 0 + 1, out1 + 1,
189
4.12M
                      bit_depth);
190
4.12M
    SharpYuvFilterRow(cur_uv, next_uv, len, best_y + w + 1, out2 + 1,
191
4.12M
                      bit_depth);
192
193
    // special boundary case for i == w - 1 when w is even
194
4.12M
    if (!(w & 1)) {
195
4.12M
      out1[w - 1] = Filter2(cur_uv[uv_w - 1], prev_uv[uv_w - 1],
196
4.12M
                            best_y[w - 1 + 0], bit_depth);
197
4.12M
      out2[w - 1] = Filter2(cur_uv[uv_w - 1], next_uv[uv_w - 1],
198
4.12M
                            best_y[w - 1 + w], bit_depth);
199
4.12M
    }
200
4.12M
    out1 += w;
201
4.12M
    out2 += w;
202
4.12M
    prev_uv += uv_w;
203
4.12M
    cur_uv += uv_w;
204
4.12M
    next_uv += uv_w;
205
4.12M
  }
206
1.37M
}
207
208
static WEBP_INLINE int RGBToYUVComponent(int r, int g, int b,
209
126M
                                         const int coeffs[4], int sfix) {
210
126M
  const int srounder = 1 << (YUV_FIX + sfix - 1);
211
126M
  const int luma =
212
126M
      coeffs[0] * r + coeffs[1] * g + coeffs[2] * b + coeffs[3] + srounder;
213
126M
  return (luma >> (YUV_FIX + sfix));
214
126M
}
215
216
static int ConvertWRGBToYUV(const fixed_y_t* best_y, const fixed_t* best_uv,
217
                            uint8_t* y_ptr, int y_stride, uint8_t* u_ptr,
218
                            int u_stride, uint8_t* v_ptr, int v_stride,
219
                            int rgb_bit_depth, int yuv_bit_depth, int width,
220
                            int height,
221
18.6k
                            const SharpYuvConversionMatrix* yuv_matrix) {
222
18.6k
  int i, j;
223
18.6k
  const fixed_t* const best_uv_base = best_uv;
224
18.6k
  const int w = (width + 1) & ~1;
225
18.6k
  const int h = (height + 1) & ~1;
226
18.6k
  const int uv_w = w >> 1;
227
18.6k
  const int uv_h = h >> 1;
228
18.6k
  const int sfix = GetPrecisionShift(rgb_bit_depth);
229
18.6k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
230
231
18.6k
  best_uv = best_uv_base;
232
18.6k
  j = 0;
233
1.09M
  do {
234
1.09M
    i = 0;
235
84.3M
    do {
236
84.3M
      const int off = (i >> 1);
237
84.3M
      const int W = best_y[i];
238
84.3M
      const int r = best_uv[off + 0 * uv_w] + W;
239
84.3M
      const int g = best_uv[off + 1 * uv_w] + W;
240
84.3M
      const int b = best_uv[off + 2 * uv_w] + W;
241
84.3M
      const int y = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_y, sfix);
242
84.3M
      if (yuv_bit_depth <= 8) {
243
84.3M
        y_ptr[i] = clip_8b(y);
244
84.3M
      } else {
245
0
        ((uint16_t*)y_ptr)[i] = clip(y, yuv_max);
246
0
      }
247
84.3M
    } while (++i < width);
248
1.09M
    best_y += w;
249
1.09M
    best_uv += (j & 1) * 3 * uv_w;
250
1.09M
    y_ptr += y_stride;
251
1.09M
  } while (++j < height);
252
253
18.6k
  best_uv = best_uv_base;
254
18.6k
  j = 0;
255
551k
  do {
256
551k
    i = 0;
257
21.3M
    do {
258
      // Note r, g and b values here are off by W, but a constant offset on all
259
      // 3 components doesn't change the value of u and v with a YCbCr matrix.
260
21.3M
      const int r = best_uv[i + 0 * uv_w];
261
21.3M
      const int g = best_uv[i + 1 * uv_w];
262
21.3M
      const int b = best_uv[i + 2 * uv_w];
263
21.3M
      const int u = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_u, sfix);
264
21.3M
      const int v = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_v, sfix);
265
21.3M
      if (yuv_bit_depth <= 8) {
266
21.3M
        u_ptr[i] = clip_8b(u);
267
21.3M
        v_ptr[i] = clip_8b(v);
268
21.3M
      } else {
269
0
        ((uint16_t*)u_ptr)[i] = clip(u, yuv_max);
270
0
        ((uint16_t*)v_ptr)[i] = clip(v, yuv_max);
271
0
      }
272
21.3M
    } while (++i < uv_w);
273
551k
    best_uv += 3 * uv_w;
274
551k
    u_ptr += u_stride;
275
551k
    v_ptr += v_stride;
276
551k
  } while (++j < uv_h);
277
18.6k
  return 1;
278
18.6k
}
279
280
//------------------------------------------------------------------------------
281
// Main function
282
283
18.6k
static void* SafeMalloc(uint64_t nmemb, size_t size) {
284
18.6k
  const uint64_t total_size = nmemb * (uint64_t)size;
285
18.6k
  if (total_size != (size_t)total_size) return NULL;
286
18.6k
  return malloc((size_t)total_size);
287
18.6k
}
288
289
static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr,
290
                            const uint8_t* b_ptr, int rgb_step, int rgb_stride,
291
                            int rgb_bit_depth, uint8_t* y_ptr, int y_stride,
292
                            uint8_t* u_ptr, int u_stride, uint8_t* v_ptr,
293
                            int v_stride, int yuv_bit_depth, int width,
294
                            int height,
295
                            const SharpYuvConversionMatrix* yuv_matrix,
296
18.6k
                            SharpYuvTransferFunctionType transfer_type) {
297
  // we expand the right/bottom border if needed
298
18.6k
  const int w = (width + 1) & ~1;
299
18.6k
  const int h = (height + 1) & ~1;
300
18.6k
  const int uv_w = w >> 1;
301
18.6k
  const int uv_h = h >> 1;
302
18.6k
  const int y_bit_depth = rgb_bit_depth + GetPrecisionShift(rgb_bit_depth);
303
18.6k
  uint64_t prev_diff_y_sum = ~0;
304
18.6k
  int j, iter;
305
306
18.6k
  const uint64_t tmp_buffer_size = (uint64_t)w * 3 * 2;
307
18.6k
  const uint64_t best_y_base_size = (uint64_t)w * h;
308
18.6k
  const uint64_t target_y_base_size = (uint64_t)w * h;
309
18.6k
  const uint64_t best_rgb_y_size = (uint64_t)w * 2;
310
18.6k
  const uint64_t best_uv_base_size = (uint64_t)uv_w * 3 * uv_h;
311
18.6k
  const uint64_t target_uv_base_size = (uint64_t)uv_w * 3 * uv_h;
312
18.6k
  const uint64_t best_rgb_uv_size = (uint64_t)uv_w * 3;
313
18.6k
  fixed_y_t* const tmp_buffer = (fixed_y_t*)SafeMalloc(
314
18.6k
      (tmp_buffer_size + best_y_base_size + target_y_base_size +
315
18.6k
       best_rgb_y_size) +
316
18.6k
          (best_uv_base_size + target_uv_base_size + best_rgb_uv_size),
317
18.6k
      sizeof(*tmp_buffer));
318
18.6k
  fixed_y_t *best_y_base, *target_y_base, *best_rgb_y;
319
18.6k
  fixed_t *best_uv_base, *target_uv_base, *best_rgb_uv;
320
18.6k
  fixed_y_t *best_y, *target_y;
321
18.6k
  fixed_t *best_uv, *target_uv;
322
18.6k
  const uint64_t diff_y_threshold = (uint64_t)(3.0 * w * h);
323
18.6k
  int ok;
324
18.6k
  assert(w > 0);
325
18.6k
  assert(h > 0);
326
18.6k
  assert(sizeof(fixed_y_t) == sizeof(fixed_t));
327
328
18.6k
  if (tmp_buffer == NULL) {
329
0
    ok = 0;
330
0
    goto End;
331
0
  }
332
18.6k
  best_y_base = tmp_buffer + tmp_buffer_size;
333
18.6k
  target_y_base = best_y_base + best_y_base_size;
334
18.6k
  best_rgb_y = target_y_base + target_y_base_size;
335
18.6k
  best_uv_base = (fixed_t*)(best_rgb_y + best_rgb_y_size);
336
18.6k
  target_uv_base = best_uv_base + best_uv_base_size;
337
18.6k
  best_rgb_uv = target_uv_base + target_uv_base_size;
338
18.6k
  best_y = best_y_base;
339
18.6k
  target_y = target_y_base;
340
18.6k
  best_uv = best_uv_base;
341
18.6k
  target_uv = target_uv_base;
342
343
  // Import RGB samples to W/RGB representation.
344
570k
  for (j = 0; j < height; j += 2) {
345
551k
    const int is_last_row = (j == height - 1);
346
551k
    fixed_y_t* const src1 = tmp_buffer + 0 * w;
347
551k
    fixed_y_t* const src2 = tmp_buffer + 3 * w;
348
349
    // prepare two rows of input
350
551k
    ImportOneRow(r_ptr, g_ptr, b_ptr, rgb_step, rgb_bit_depth, width, src1);
351
551k
    if (!is_last_row) {
352
543k
      ImportOneRow(r_ptr + rgb_stride, g_ptr + rgb_stride, b_ptr + rgb_stride,
353
543k
                   rgb_step, rgb_bit_depth, width, src2);
354
543k
    } else {
355
8.24k
      memcpy(src2, src1, 3 * w * sizeof(*src2));
356
8.24k
    }
357
551k
    StoreGray(src1, best_y + 0, w);
358
551k
    StoreGray(src2, best_y + w, w);
359
360
551k
    UpdateW(src1, target_y, w, y_bit_depth, transfer_type);
361
551k
    UpdateW(src2, target_y + w, w, y_bit_depth, transfer_type);
362
551k
    UpdateChroma(src1, src2, target_uv, uv_w, y_bit_depth, transfer_type);
363
551k
    memcpy(best_uv, target_uv, 3 * uv_w * sizeof(*best_uv));
364
551k
    best_y += 2 * w;
365
551k
    best_uv += 3 * uv_w;
366
551k
    target_y += 2 * w;
367
551k
    target_uv += 3 * uv_w;
368
551k
    r_ptr += 2 * rgb_stride;
369
551k
    g_ptr += 2 * rgb_stride;
370
551k
    b_ptr += 2 * rgb_stride;
371
551k
  }
372
373
  // Iterate and resolve clipping conflicts.
374
52.8k
  for (iter = 0; iter < kNumIterations; ++iter) {
375
48.8k
    const fixed_t* cur_uv = best_uv_base;
376
48.8k
    const fixed_t* prev_uv = best_uv_base;
377
48.8k
    uint64_t diff_y_sum = 0;
378
379
48.8k
    best_y = best_y_base;
380
48.8k
    best_uv = best_uv_base;
381
48.8k
    target_y = target_y_base;
382
48.8k
    target_uv = target_uv_base;
383
48.8k
    j = 0;
384
1.37M
    do {
385
1.37M
      fixed_y_t* const src1 = tmp_buffer + 0 * w;
386
1.37M
      fixed_y_t* const src2 = tmp_buffer + 3 * w;
387
1.37M
      {
388
1.37M
        const fixed_t* const next_uv = cur_uv + ((j < h - 2) ? 3 * uv_w : 0);
389
1.37M
        InterpolateTwoRows(best_y, prev_uv, cur_uv, next_uv, w, src1, src2,
390
1.37M
                           y_bit_depth);
391
1.37M
        prev_uv = cur_uv;
392
1.37M
        cur_uv = next_uv;
393
1.37M
      }
394
395
1.37M
      UpdateW(src1, best_rgb_y + 0 * w, w, y_bit_depth, transfer_type);
396
1.37M
      UpdateW(src2, best_rgb_y + 1 * w, w, y_bit_depth, transfer_type);
397
1.37M
      UpdateChroma(src1, src2, best_rgb_uv, uv_w, y_bit_depth, transfer_type);
398
399
      // update two rows of Y and one row of RGB
400
1.37M
      diff_y_sum +=
401
1.37M
          SharpYuvUpdateY(target_y, best_rgb_y, best_y, 2 * w, y_bit_depth);
402
1.37M
      SharpYuvUpdateRGB(target_uv, best_rgb_uv, best_uv, 3 * uv_w);
403
404
1.37M
      best_y += 2 * w;
405
1.37M
      best_uv += 3 * uv_w;
406
1.37M
      target_y += 2 * w;
407
1.37M
      target_uv += 3 * uv_w;
408
1.37M
      j += 2;
409
1.37M
    } while (j < h);
410
    // test exit condition
411
48.8k
    if (iter > 0) {
412
30.1k
      if (diff_y_sum < diff_y_threshold) break;
413
16.5k
      if (diff_y_sum > prev_diff_y_sum) break;
414
16.5k
    }
415
34.2k
    prev_diff_y_sum = diff_y_sum;
416
34.2k
  }
417
418
  // final reconstruction
419
18.6k
  ok = ConvertWRGBToYUV(best_y_base, best_uv_base, y_ptr, y_stride, u_ptr,
420
18.6k
                        u_stride, v_ptr, v_stride, rgb_bit_depth, yuv_bit_depth,
421
18.6k
                        width, height, yuv_matrix);
422
423
18.6k
End:
424
18.6k
  free(tmp_buffer);
425
18.6k
  return ok;
426
18.6k
}
427
428
#if defined(WEBP_USE_THREAD) && !defined(_WIN32)
429
#include <pthread.h>  // NOLINT
430
431
#define LOCK_ACCESS                                                 \
432
37.2k
  static pthread_mutex_t sharpyuv_lock = PTHREAD_MUTEX_INITIALIZER; \
433
37.2k
  if (pthread_mutex_lock(&sharpyuv_lock)) return
434
#define UNLOCK_ACCESS_AND_RETURN                \
435
37.2k
  do {                                          \
436
37.2k
    (void)pthread_mutex_unlock(&sharpyuv_lock); \
437
37.2k
    return;                                     \
438
37.2k
  } while (0)
439
#else  // !(defined(WEBP_USE_THREAD) && !defined(_WIN32))
440
#define LOCK_ACCESS \
441
  do {              \
442
  } while (0)
443
#define UNLOCK_ACCESS_AND_RETURN return
444
#endif  // defined(WEBP_USE_THREAD) && !defined(_WIN32)
445
446
// Hidden exported init function.
447
// By default SharpYuvConvert calls it with SharpYuvGetCPUInfo. If needed,
448
// users can declare it as extern and call it with an alternate VP8CPUInfo
449
// function.
450
extern VP8CPUInfo SharpYuvGetCPUInfo;
451
SHARPYUV_EXTERN void SharpYuvInit(VP8CPUInfo cpu_info_func);
452
37.2k
void SharpYuvInit(VP8CPUInfo cpu_info_func) {
453
37.2k
  static volatile VP8CPUInfo sharpyuv_last_cpuinfo_used =
454
37.2k
      (VP8CPUInfo)&sharpyuv_last_cpuinfo_used;
455
37.2k
  LOCK_ACCESS;
456
  // Only update SharpYuvGetCPUInfo when called from external code to avoid a
457
  // race on reading the value in SharpYuvConvert().
458
37.2k
  if (cpu_info_func != (VP8CPUInfo)&SharpYuvGetCPUInfo) {
459
18.6k
    SharpYuvGetCPUInfo = cpu_info_func;
460
18.6k
  }
461
37.2k
  if (sharpyuv_last_cpuinfo_used == SharpYuvGetCPUInfo) {
462
29.2k
    UNLOCK_ACCESS_AND_RETURN;
463
29.2k
  }
464
465
8.05k
  SharpYuvInitDsp();
466
8.05k
  SharpYuvInitGammaTables();
467
468
8.05k
  sharpyuv_last_cpuinfo_used = SharpYuvGetCPUInfo;
469
8.05k
  UNLOCK_ACCESS_AND_RETURN;
470
8.05k
}
471
472
int SharpYuvConvert(const void* r_ptr, const void* g_ptr, const void* b_ptr,
473
                    int rgb_step, int rgb_stride, int rgb_bit_depth,
474
                    void* y_ptr, int y_stride, void* u_ptr, int u_stride,
475
                    void* v_ptr, int v_stride, int yuv_bit_depth, int width,
476
18.6k
                    int height, const SharpYuvConversionMatrix* yuv_matrix) {
477
18.6k
  SharpYuvOptions options;
478
18.6k
  options.yuv_matrix = yuv_matrix;
479
18.6k
  options.transfer_type = kSharpYuvTransferFunctionSrgb;
480
18.6k
  return SharpYuvConvertWithOptions(
481
18.6k
      r_ptr, g_ptr, b_ptr, rgb_step, rgb_stride, rgb_bit_depth, y_ptr, y_stride,
482
18.6k
      u_ptr, u_stride, v_ptr, v_stride, yuv_bit_depth, width, height, &options);
483
18.6k
}
484
485
int SharpYuvOptionsInitInternal(const SharpYuvConversionMatrix* yuv_matrix,
486
0
                                SharpYuvOptions* options, int version) {
487
0
  const int major = (version >> 24);
488
0
  const int minor = (version >> 16) & 0xff;
489
0
  if (options == NULL || yuv_matrix == NULL ||
490
0
      (major == SHARPYUV_VERSION_MAJOR && major == 0 &&
491
0
       minor != SHARPYUV_VERSION_MINOR) ||
492
0
      (major != SHARPYUV_VERSION_MAJOR)) {
493
0
    return 0;
494
0
  }
495
0
  options->yuv_matrix = yuv_matrix;
496
0
  options->transfer_type = kSharpYuvTransferFunctionSrgb;
497
0
  return 1;
498
0
}
499
500
int SharpYuvConvertWithOptions(const void* r_ptr, const void* g_ptr,
501
                               const void* b_ptr, int rgb_step, int rgb_stride,
502
                               int rgb_bit_depth, void* y_ptr, int y_stride,
503
                               void* u_ptr, int u_stride, void* v_ptr,
504
                               int v_stride, int yuv_bit_depth, int width,
505
18.6k
                               int height, const SharpYuvOptions* options) {
506
18.6k
  const SharpYuvConversionMatrix* yuv_matrix = options->yuv_matrix;
507
18.6k
  SharpYuvTransferFunctionType transfer_type = options->transfer_type;
508
18.6k
  SharpYuvConversionMatrix scaled_matrix;
509
18.6k
  const int rgb_max = (1 << rgb_bit_depth) - 1;
510
18.6k
  const int rgb_round = 1 << (rgb_bit_depth - 1);
511
18.6k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
512
18.6k
  const int sfix = GetPrecisionShift(rgb_bit_depth);
513
514
18.6k
  if (width < 1 || height < 1 || width == INT_MAX || height == INT_MAX ||
515
18.6k
      r_ptr == NULL || g_ptr == NULL || b_ptr == NULL || y_ptr == NULL ||
516
18.6k
      u_ptr == NULL || v_ptr == NULL) {
517
0
    return 0;
518
0
  }
519
18.6k
  if (rgb_bit_depth != 8 && rgb_bit_depth != 10 && rgb_bit_depth != 12 &&
520
0
      rgb_bit_depth != 16) {
521
0
    return 0;
522
0
  }
523
18.6k
  if (yuv_bit_depth != 8 && yuv_bit_depth != 10 && yuv_bit_depth != 12) {
524
0
    return 0;
525
0
  }
526
18.6k
  if (rgb_bit_depth > 8 && (rgb_step % 2 != 0 || rgb_stride % 2 != 0)) {
527
    // Step/stride should be even for uint16_t buffers.
528
0
    return 0;
529
0
  }
530
18.6k
  if (yuv_bit_depth > 8 &&
531
0
      (y_stride % 2 != 0 || u_stride % 2 != 0 || v_stride % 2 != 0)) {
532
    // Stride should be even for uint16_t buffers.
533
0
    return 0;
534
0
  }
535
  // The address of the function pointer is used to avoid a read race.
536
18.6k
  SharpYuvInit((VP8CPUInfo)&SharpYuvGetCPUInfo);
537
538
  // Add scaling factor to go from rgb_bit_depth to yuv_bit_depth, to the
539
  // rgb->yuv conversion matrix.
540
18.6k
  if (rgb_bit_depth == yuv_bit_depth) {
541
18.6k
    memcpy(&scaled_matrix, yuv_matrix, sizeof(scaled_matrix));
542
18.6k
  } else {
543
0
    int i;
544
0
    for (i = 0; i < 3; ++i) {
545
0
      scaled_matrix.rgb_to_y[i] =
546
0
          (yuv_matrix->rgb_to_y[i] * yuv_max + rgb_round) / rgb_max;
547
0
      scaled_matrix.rgb_to_u[i] =
548
0
          (yuv_matrix->rgb_to_u[i] * yuv_max + rgb_round) / rgb_max;
549
0
      scaled_matrix.rgb_to_v[i] =
550
0
          (yuv_matrix->rgb_to_v[i] * yuv_max + rgb_round) / rgb_max;
551
0
    }
552
0
  }
553
  // Also incorporate precision change scaling.
554
18.6k
  scaled_matrix.rgb_to_y[3] = Shift(yuv_matrix->rgb_to_y[3], sfix);
555
18.6k
  scaled_matrix.rgb_to_u[3] = Shift(yuv_matrix->rgb_to_u[3], sfix);
556
18.6k
  scaled_matrix.rgb_to_v[3] = Shift(yuv_matrix->rgb_to_v[3], sfix);
557
558
18.6k
  return DoSharpArgbToYuv(
559
18.6k
      (const uint8_t*)r_ptr, (const uint8_t*)g_ptr, (const uint8_t*)b_ptr,
560
18.6k
      rgb_step, rgb_stride, rgb_bit_depth, (uint8_t*)y_ptr, y_stride,
561
18.6k
      (uint8_t*)u_ptr, u_stride, (uint8_t*)v_ptr, v_stride, yuv_bit_depth,
562
18.6k
      width, height, &scaled_matrix, transfer_type);
563
18.6k
}
564
565
//------------------------------------------------------------------------------