Coverage Report

Created: 2026-08-31 06:22

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