Coverage Report

Created: 2026-08-31 06:20

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
465k
#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
22.6k
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
22.6k
  return ((rgb_bit_depth + 2) <= kMaxBitDepth) ? 2
47
22.6k
                                               : (kMaxBitDepth - rgb_bit_depth);
48
22.6k
}
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
52.9k
static uint8_t clip_8b(fixed_t v) {
56
52.9k
  return (!(v & ~0xff)) ? (uint8_t)v : (v < 0) ? 0u : 255u;
57
52.9k
}
58
59
4.73k
static uint16_t clip(fixed_t v, int max) {
60
4.73k
  return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
61
4.73k
}
62
63
314k
static fixed_y_t clip_bit_depth(int y, int bit_depth) {
64
314k
  const int max = (1 << bit_depth) - 1;
65
314k
  return (!(y & ~max)) ? (fixed_y_t)y : (y < 0) ? 0 : max;
66
314k
}
67
68
//------------------------------------------------------------------------------
69
70
350k
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
350k
  const int64_t luma = 13933 * r + 46871 * g + 4732 * b + kYuvHalf;
73
350k
  return (int)(luma >> YUV_FIX);
74
350k
}
75
76
static uint32_t ScaleDown(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
77
                          int bit_depth,
78
175k
                          SharpYuvTransferFunctionType transfer_type) {
79
175k
  const uint32_t A = SharpYuvGammaToLinear(a, bit_depth, transfer_type);
80
175k
  const uint32_t B = SharpYuvGammaToLinear(b, bit_depth, transfer_type);
81
175k
  const uint32_t C = SharpYuvGammaToLinear(c, bit_depth, transfer_type);
82
175k
  const uint32_t D = SharpYuvGammaToLinear(d, bit_depth, transfer_type);
83
175k
  return SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth,
84
175k
                               transfer_type);
85
175k
}
86
87
static WEBP_INLINE void UpdateW(const fixed_y_t* src, fixed_y_t* dst, int w,
88
                                int bit_depth,
89
69.3k
                                SharpYuvTransferFunctionType transfer_type) {
90
69.3k
  int i = 0;
91
234k
  do {
92
234k
    const uint32_t R =
93
234k
        SharpYuvGammaToLinear(src[0 * w + i], bit_depth, transfer_type);
94
234k
    const uint32_t G =
95
234k
        SharpYuvGammaToLinear(src[1 * w + i], bit_depth, transfer_type);
96
234k
    const uint32_t B =
97
234k
        SharpYuvGammaToLinear(src[2 * w + i], bit_depth, transfer_type);
98
234k
    const uint32_t Y = RGBToGray(R, G, B);
99
234k
    dst[i] = (fixed_y_t)SharpYuvLinearToGamma(Y, bit_depth, transfer_type);
100
234k
  } while (++i < w);
101
69.3k
}
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
34.6k
                         SharpYuvTransferFunctionType transfer_type) {
106
34.6k
  int i = 0;
107
58.6k
  do {
108
58.6k
    const int r =
109
58.6k
        ScaleDown(src1[0 * uv_w + 0], src1[0 * uv_w + 1], src2[0 * uv_w + 0],
110
58.6k
                  src2[0 * uv_w + 1], bit_depth, transfer_type);
111
58.6k
    const int g =
112
58.6k
        ScaleDown(src1[2 * uv_w + 0], src1[2 * uv_w + 1], src2[2 * uv_w + 0],
113
58.6k
                  src2[2 * uv_w + 1], bit_depth, transfer_type);
114
58.6k
    const int b =
115
58.6k
        ScaleDown(src1[4 * uv_w + 0], src1[4 * uv_w + 1], src2[4 * uv_w + 0],
116
58.6k
                  src2[4 * uv_w + 1], bit_depth, transfer_type);
117
58.6k
    const int W = RGBToGray(r, g, b);
118
58.6k
    dst[0 * uv_w] = (fixed_t)(r - W);
119
58.6k
    dst[1 * uv_w] = (fixed_t)(g - W);
120
58.6k
    dst[2 * uv_w] = (fixed_t)(b - W);
121
58.6k
    dst += 1;
122
58.6k
    src1 += 2;
123
58.6k
    src2 += 2;
124
58.6k
  } while (++i < uv_w);
125
34.6k
}
126
127
17.0k
static void StoreGray(const fixed_y_t* rgb, fixed_y_t* y, int w) {
128
17.0k
  int i = 0;
129
17.0k
  assert(w > 0);
130
57.0k
  do {
131
57.0k
    y[i] = RGBToGray(rgb[0 * w + i], rgb[1 * w + i], rgb[2 * w + i]);
132
57.0k
  } while (++i < w);
133
17.0k
}
134
135
//------------------------------------------------------------------------------
136
137
314k
static WEBP_INLINE fixed_y_t Filter2(int A, int B, int W0, int bit_depth) {
138
314k
  const int v0 = (A * 3 + B + 2) >> 2;
139
314k
  return clip_bit_depth(v0 + W0, bit_depth);
140
314k
}
141
142
//------------------------------------------------------------------------------
143
144
94.7k
static WEBP_INLINE int Shift(int v, int shift) {
145
94.7k
  return (shift >= 0) ? (v << shift) : (v >> -shift);
146
94.7k
}
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
15.2k
                         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
15.2k
  const int step = (rgb_bit_depth > 8) ? rgb_step / 2 : rgb_step;
155
15.2k
  const int w = (pic_width + 1) & ~1;
156
15.2k
  const int shift = GetPrecisionShift(rgb_bit_depth);
157
15.2k
  const int max_val = (1 << rgb_bit_depth) - 1;
158
15.2k
  int i = 0;
159
160
15.2k
  if (rgb_bit_depth == 8) {
161
26.7k
    do {
162
26.7k
      const int off = i * step;
163
26.7k
      dst[i + 0 * w] = Shift(r_ptr[off], shift);
164
26.7k
      dst[i + 1 * w] = Shift(g_ptr[off], shift);
165
26.7k
      dst[i + 2 * w] = Shift(b_ptr[off], shift);
166
26.7k
    } while (++i < pic_width);
167
14.2k
  } else if (rgb_bit_depth < 16) {
168
2.35k
    do {
169
2.35k
      const int off = i * step;
170
2.35k
      int r = ((const uint16_t*)r_ptr)[off];
171
2.35k
      int g = ((const uint16_t*)g_ptr)[off];
172
2.35k
      int b = ((const uint16_t*)b_ptr)[off];
173
2.35k
      dst[i + 0 * w] = Shift(r > max_val ? max_val : r, shift);
174
2.35k
      dst[i + 1 * w] = Shift(g > max_val ? max_val : g, shift);
175
2.35k
      dst[i + 2 * w] = Shift(b > max_val ? max_val : b, shift);
176
2.35k
    } while (++i < pic_width);
177
996
  } 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
15.2k
  if (pic_width & 1) {  // replicate rightmost pixel
190
13.4k
    dst[pic_width + 0 * w] = dst[pic_width + 0 * w - 1];
191
13.4k
    dst[pic_width + 1 * w] = dst[pic_width + 1 * w - 1];
192
13.4k
    dst[pic_width + 2 * w] = dst[pic_width + 2 * w - 1];
193
13.4k
  }
194
15.2k
}
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
26.1k
                               fixed_y_t* out2, int bit_depth) {
200
26.1k
  const int uv_w = w >> 1;
201
26.1k
  const int len = (w - 1) >> 1;  // length to filter
202
26.1k
  int k = 3;
203
104k
  while (k-- > 0) {  // process each R/G/B segments in turn
204
    // special boundary case for i==0
205
78.5k
    out1[0] = Filter2(cur_uv[0], prev_uv[0], best_y[0], bit_depth);
206
78.5k
    out2[0] = Filter2(cur_uv[0], next_uv[0], best_y[w], bit_depth);
207
208
78.5k
    SharpYuvFilterRow(cur_uv, prev_uv, len, best_y + 0 + 1, out1 + 1,
209
78.5k
                      bit_depth);
210
78.5k
    SharpYuvFilterRow(cur_uv, next_uv, len, best_y + w + 1, out2 + 1,
211
78.5k
                      bit_depth);
212
213
    // special boundary case for i == w - 1 when w is even
214
78.5k
    if (!(w & 1)) {
215
78.5k
      out1[w - 1] = Filter2(cur_uv[uv_w - 1], prev_uv[uv_w - 1],
216
78.5k
                            best_y[w - 1 + 0], bit_depth);
217
78.5k
      out2[w - 1] = Filter2(cur_uv[uv_w - 1], next_uv[uv_w - 1],
218
78.5k
                            best_y[w - 1 + w], bit_depth);
219
78.5k
    }
220
78.5k
    out1 += w;
221
78.5k
    out2 += w;
222
78.5k
    prev_uv += uv_w;
223
78.5k
    cur_uv += uv_w;
224
78.5k
    next_uv += uv_w;
225
78.5k
  }
226
26.1k
}
227
228
static WEBP_INLINE int RGBToYUVComponent(int r, int g, int b,
229
57.6k
                                         const int coeffs[4], int sfix) {
230
57.6k
  const int64_t srounder = 1LL << (YUV_FIX + sfix - 1);
231
57.6k
  const int64_t luma = (int64_t)coeffs[0] * r + (int64_t)coeffs[1] * g +
232
57.6k
                       (int64_t)coeffs[2] * b + coeffs[3] + srounder;
233
57.6k
  return (int)(luma >> (YUV_FIX + sfix));
234
57.6k
}
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.47k
                            const SharpYuvConversionMatrix* yuv_matrix) {
242
2.47k
  int i, j;
243
2.47k
  const fixed_t* const best_uv_base = best_uv;
244
2.47k
  const int w = (width + 1) & ~1;
245
2.47k
  const int h = (height + 1) & ~1;
246
2.47k
  const int uv_w = w >> 1;
247
2.47k
  const int uv_h = h >> 1;
248
2.47k
  const int sfix = GetPrecisionShift(rgb_bit_depth);
249
2.47k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
250
251
2.47k
  best_uv = best_uv_base;
252
2.47k
  j = 0;
253
15.2k
  do {
254
15.2k
    i = 0;
255
29.0k
    do {
256
29.0k
      const int off = (i >> 1);
257
29.0k
      const int W = best_y[i];
258
29.0k
      const int r = best_uv[off + 0 * uv_w] + W;
259
29.0k
      const int g = best_uv[off + 1 * uv_w] + W;
260
29.0k
      const int b = best_uv[off + 2 * uv_w] + W;
261
29.0k
      const int y = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_y, sfix);
262
29.0k
      if (yuv_bit_depth <= 8) {
263
26.7k
        y_ptr[i] = clip_8b(y);
264
26.7k
      } else {
265
2.35k
        ((uint16_t*)y_ptr)[i] = clip(y, yuv_max);
266
2.35k
      }
267
29.0k
    } while (++i < width);
268
15.2k
    best_y += w;
269
15.2k
    best_uv += (j & 1) * 3 * uv_w;
270
15.2k
    y_ptr += y_stride;
271
15.2k
  } while (++j < height);
272
273
2.47k
  best_uv = best_uv_base;
274
2.47k
  j = 0;
275
8.51k
  do {
276
8.51k
    i = 0;
277
14.2k
    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
14.2k
      const int r = best_uv[i + 0 * uv_w];
281
14.2k
      const int g = best_uv[i + 1 * uv_w];
282
14.2k
      const int b = best_uv[i + 2 * uv_w];
283
14.2k
      const int u = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_u, sfix);
284
14.2k
      const int v = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_v, sfix);
285
14.2k
      if (yuv_bit_depth <= 8) {
286
13.0k
        u_ptr[i] = clip_8b(u);
287
13.0k
        v_ptr[i] = clip_8b(v);
288
13.0k
      } else {
289
1.18k
        ((uint16_t*)u_ptr)[i] = clip(u, yuv_max);
290
1.18k
        ((uint16_t*)v_ptr)[i] = clip(v, yuv_max);
291
1.18k
      }
292
14.2k
    } while (++i < uv_w);
293
8.51k
    best_uv += 3 * uv_w;
294
8.51k
    u_ptr += u_stride;
295
8.51k
    v_ptr += v_stride;
296
8.51k
  } while (++j < uv_h);
297
2.47k
  return 1;
298
2.47k
}
299
300
//------------------------------------------------------------------------------
301
// Main function
302
303
2.47k
static void* SafeMalloc(uint64_t nmemb, size_t size) {
304
2.47k
  const uint64_t total_size = nmemb * (uint64_t)size;
305
2.47k
  if (total_size != (size_t)total_size) return NULL;
306
2.47k
  return malloc((size_t)total_size);
307
2.47k
}
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.47k
                            SharpYuvTransferFunctionType transfer_type) {
317
  // we expand the right/bottom border if needed
318
2.47k
  const int w = (width + 1) & ~1;
319
2.47k
  const int h = (height + 1) & ~1;
320
2.47k
  const int uv_w = w >> 1;
321
2.47k
  const int uv_h = h >> 1;
322
2.47k
  const int y_bit_depth = rgb_bit_depth + GetPrecisionShift(rgb_bit_depth);
323
2.47k
  uint64_t prev_diff_y_sum = ~0;
324
2.47k
  int j, iter;
325
326
2.47k
  const uint64_t tmp_buffer_size = (uint64_t)w * 3 * 2;
327
2.47k
  const uint64_t best_y_base_size = (uint64_t)w * h;
328
2.47k
  const uint64_t target_y_base_size = (uint64_t)w * h;
329
2.47k
  const uint64_t best_rgb_y_size = (uint64_t)w * 2;
330
2.47k
  const uint64_t best_uv_base_size = (uint64_t)uv_w * 3 * uv_h;
331
2.47k
  const uint64_t target_uv_base_size = (uint64_t)uv_w * 3 * uv_h;
332
2.47k
  const uint64_t best_rgb_uv_size = (uint64_t)uv_w * 3;
333
2.47k
  fixed_y_t* const tmp_buffer = (fixed_y_t*)SafeMalloc(
334
2.47k
      (tmp_buffer_size + best_y_base_size + target_y_base_size +
335
2.47k
       best_rgb_y_size) +
336
2.47k
          (best_uv_base_size + target_uv_base_size + best_rgb_uv_size),
337
2.47k
      sizeof(*tmp_buffer));
338
2.47k
  fixed_y_t *best_y_base, *target_y_base, *best_rgb_y;
339
2.47k
  fixed_t *best_uv_base, *target_uv_base, *best_rgb_uv;
340
2.47k
  fixed_y_t *best_y, *target_y;
341
2.47k
  fixed_t *best_uv, *target_uv;
342
2.47k
  const uint64_t diff_y_threshold = (uint64_t)(3.0 * w * h);
343
2.47k
  int ok;
344
2.47k
  assert(w > 0);
345
2.47k
  assert(h > 0);
346
2.47k
  assert(sizeof(fixed_y_t) == sizeof(fixed_t));
347
348
2.47k
  if (tmp_buffer == NULL) {
349
0
    ok = 0;
350
0
    goto End;
351
0
  }
352
2.47k
  best_y_base = tmp_buffer + tmp_buffer_size;
353
2.47k
  target_y_base = best_y_base + best_y_base_size;
354
2.47k
  best_rgb_y = target_y_base + target_y_base_size;
355
2.47k
  best_uv_base = (fixed_t*)(best_rgb_y + best_rgb_y_size);
356
2.47k
  target_uv_base = best_uv_base + best_uv_base_size;
357
2.47k
  best_rgb_uv = target_uv_base + target_uv_base_size;
358
2.47k
  best_y = best_y_base;
359
2.47k
  target_y = target_y_base;
360
2.47k
  best_uv = best_uv_base;
361
2.47k
  target_uv = target_uv_base;
362
363
  // Import RGB samples to W/RGB representation.
364
10.9k
  for (j = 0; j < height; j += 2) {
365
8.51k
    const int is_last_row = (j == height - 1);
366
8.51k
    fixed_y_t* const src1 = tmp_buffer + 0 * w;
367
8.51k
    fixed_y_t* const src2 = tmp_buffer + 3 * w;
368
369
    // prepare two rows of input
370
8.51k
    ImportOneRow(r_ptr, g_ptr, b_ptr, rgb_step, rgb_bit_depth, width, src1);
371
8.51k
    if (!is_last_row) {
372
6.70k
      ImportOneRow(r_ptr + rgb_stride, g_ptr + rgb_stride, b_ptr + rgb_stride,
373
6.70k
                   rgb_step, rgb_bit_depth, width, src2);
374
6.70k
    } else {
375
1.80k
      memcpy(src2, src1, 3 * w * sizeof(*src2));
376
1.80k
    }
377
8.51k
    StoreGray(src1, best_y + 0, w);
378
8.51k
    StoreGray(src2, best_y + w, w);
379
380
8.51k
    UpdateW(src1, target_y, w, y_bit_depth, transfer_type);
381
8.51k
    UpdateW(src2, target_y + w, w, y_bit_depth, transfer_type);
382
8.51k
    UpdateChroma(src1, src2, target_uv, uv_w, y_bit_depth, transfer_type);
383
8.51k
    memcpy(best_uv, target_uv, 3 * uv_w * sizeof(*best_uv));
384
8.51k
    best_y += 2 * w;
385
8.51k
    best_uv += 3 * uv_w;
386
8.51k
    target_y += 2 * w;
387
8.51k
    target_uv += 3 * uv_w;
388
8.51k
    r_ptr += 2 * rgb_stride;
389
8.51k
    g_ptr += 2 * rgb_stride;
390
8.51k
    b_ptr += 2 * rgb_stride;
391
8.51k
  }
392
393
  // Iterate and resolve clipping conflicts.
394
8.22k
  for (iter = 0; iter < kNumIterations; ++iter) {
395
7.44k
    const fixed_t* cur_uv = best_uv_base;
396
7.44k
    const fixed_t* prev_uv = best_uv_base;
397
7.44k
    uint64_t diff_y_sum = 0;
398
399
7.44k
    best_y = best_y_base;
400
7.44k
    best_uv = best_uv_base;
401
7.44k
    target_y = target_y_base;
402
7.44k
    target_uv = target_uv_base;
403
7.44k
    j = 0;
404
26.1k
    do {
405
26.1k
      fixed_y_t* const src1 = tmp_buffer + 0 * w;
406
26.1k
      fixed_y_t* const src2 = tmp_buffer + 3 * w;
407
26.1k
      {
408
26.1k
        const fixed_t* const next_uv = cur_uv + ((j < h - 2) ? 3 * uv_w : 0);
409
26.1k
        InterpolateTwoRows(best_y, prev_uv, cur_uv, next_uv, w, src1, src2,
410
26.1k
                           y_bit_depth);
411
26.1k
        prev_uv = cur_uv;
412
26.1k
        cur_uv = next_uv;
413
26.1k
      }
414
415
26.1k
      UpdateW(src1, best_rgb_y + 0 * w, w, y_bit_depth, transfer_type);
416
26.1k
      UpdateW(src2, best_rgb_y + 1 * w, w, y_bit_depth, transfer_type);
417
26.1k
      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
26.1k
      diff_y_sum +=
421
26.1k
          SharpYuvUpdateY(target_y, best_rgb_y, best_y, 2 * w, y_bit_depth);
422
26.1k
      SharpYuvUpdateRGB(target_uv, best_rgb_uv, best_uv, 3 * uv_w);
423
424
26.1k
      best_y += 2 * w;
425
26.1k
      best_uv += 3 * uv_w;
426
26.1k
      target_y += 2 * w;
427
26.1k
      target_uv += 3 * uv_w;
428
26.1k
      j += 2;
429
26.1k
    } while (j < h);
430
    // test exit condition
431
7.44k
    if (iter > 0) {
432
4.97k
      if (diff_y_sum < diff_y_threshold) break;
433
4.00k
      if (diff_y_sum > prev_diff_y_sum) break;
434
4.00k
    }
435
5.74k
    prev_diff_y_sum = diff_y_sum;
436
5.74k
  }
437
438
  // final reconstruction
439
2.47k
  ok = ConvertWRGBToYUV(best_y_base, best_uv_base, y_ptr, y_stride, u_ptr,
440
2.47k
                        u_stride, v_ptr, v_stride, rgb_bit_depth, yuv_bit_depth,
441
2.47k
                        width, height, yuv_matrix);
442
443
2.47k
End:
444
2.47k
  free(tmp_buffer);
445
2.47k
  return ok;
446
2.47k
}
447
448
#if defined(WEBP_USE_THREAD) && !defined(_WIN32)
449
#include <pthread.h>  // NOLINT
450
451
#define LOCK_ACCESS                                                 \
452
2.47k
  static pthread_mutex_t sharpyuv_lock = PTHREAD_MUTEX_INITIALIZER; \
453
2.47k
  if (pthread_mutex_lock(&sharpyuv_lock)) return
454
#define UNLOCK_ACCESS_AND_RETURN                \
455
2.47k
  do {                                          \
456
2.47k
    (void)pthread_mutex_unlock(&sharpyuv_lock); \
457
2.47k
    return;                                     \
458
2.47k
  } 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.47k
void SharpYuvInit(VP8CPUInfo cpu_info_func) {
473
2.47k
  static volatile VP8CPUInfo sharpyuv_last_cpuinfo_used =
474
2.47k
      (VP8CPUInfo)&sharpyuv_last_cpuinfo_used;
475
2.47k
  LOCK_ACCESS;
476
  // Only update SharpYuvGetCPUInfo when called from external code to avoid a
477
  // race on reading the value in SharpYuvConvert().
478
2.47k
  if (cpu_info_func != (VP8CPUInfo)&SharpYuvGetCPUInfo) {
479
0
    SharpYuvGetCPUInfo = cpu_info_func;
480
0
  }
481
2.47k
  if (sharpyuv_last_cpuinfo_used == SharpYuvGetCPUInfo) {
482
2.47k
    UNLOCK_ACCESS_AND_RETURN;
483
2.47k
  }
484
485
1
  SharpYuvInitDsp();
486
1
  SharpYuvInitGammaTables();
487
488
1
  sharpyuv_last_cpuinfo_used = SharpYuvGetCPUInfo;
489
1
  UNLOCK_ACCESS_AND_RETURN;
490
1
}
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.47k
                    int height, const SharpYuvConversionMatrix* yuv_matrix) {
497
2.47k
  SharpYuvOptions options;
498
2.47k
  options.yuv_matrix = yuv_matrix;
499
2.47k
  options.transfer_type = kSharpYuvTransferFunctionSrgb;
500
2.47k
  return SharpYuvConvertWithOptions(
501
2.47k
      r_ptr, g_ptr, b_ptr, rgb_step, rgb_stride, rgb_bit_depth, y_ptr, y_stride,
502
2.47k
      u_ptr, u_stride, v_ptr, v_stride, yuv_bit_depth, width, height, &options);
503
2.47k
}
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.47k
                               int height, const SharpYuvOptions* options) {
526
2.47k
  const SharpYuvConversionMatrix* yuv_matrix = options->yuv_matrix;
527
2.47k
  SharpYuvTransferFunctionType transfer_type = options->transfer_type;
528
2.47k
  SharpYuvConversionMatrix scaled_matrix;
529
2.47k
  const int rgb_max = (1 << rgb_bit_depth) - 1;
530
2.47k
  const int rgb_round = 1 << (rgb_bit_depth - 1);
531
2.47k
  const int yuv_max = (1 << yuv_bit_depth) - 1;
532
2.47k
  const int sfix = GetPrecisionShift(rgb_bit_depth);
533
534
2.47k
  if (width < 1 || height < 1 || width == INT_MAX || height == INT_MAX ||
535
2.47k
      r_ptr == NULL || g_ptr == NULL || b_ptr == NULL || y_ptr == NULL ||
536
2.47k
      u_ptr == NULL || v_ptr == NULL) {
537
0
    return 0;
538
0
  }
539
2.47k
  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.47k
  if (yuv_bit_depth != 8 && yuv_bit_depth != 10 && yuv_bit_depth != 12) {
544
0
    return 0;
545
0
  }
546
2.47k
  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.47k
  {
551
2.47k
    const uint64_t yuv_bytes = (yuv_bit_depth > 8) ? 2 : 1;
552
2.47k
    const uint64_t uv_width = (width + 1) / 2;
553
2.47k
    const uint64_t abs_step =
554
2.47k
        (uint64_t)((rgb_step < 0) ? -(int64_t)rgb_step : (int64_t)rgb_step);
555
2.47k
    const uint64_t abs_stride =
556
2.47k
        (uint64_t)((rgb_stride < 0) ? -(int64_t)rgb_stride
557
2.47k
                                    : (int64_t)rgb_stride);
558
2.47k
    const uint64_t total_rgb_size = (uint64_t)height * abs_stride;
559
2.47k
    const uint64_t uv_height = (height + 1) / 2;
560
2.47k
    const uint64_t total_y_size = (uint64_t)height * y_stride;
561
2.47k
    const uint64_t total_u_size = uv_height * u_stride;
562
2.47k
    const uint64_t total_v_size = uv_height * v_stride;
563
564
2.47k
    if (y_stride < 0 || (uint64_t)y_stride < (uint64_t)width * yuv_bytes ||
565
2.47k
        u_stride < 0 || (uint64_t)u_stride < uv_width * yuv_bytes ||
566
2.47k
        v_stride < 0 || (uint64_t)v_stride < uv_width * yuv_bytes) {
567
0
      return 0;
568
0
    }
569
2.47k
    if (abs_step == 0 || abs_stride < (uint64_t)width * abs_step) {
570
0
      return 0;
571
0
    }
572
2.47k
    if (total_rgb_size != (size_t)total_rgb_size ||
573
2.47k
        total_y_size != (size_t)total_y_size ||
574
2.47k
        total_u_size != (size_t)total_u_size ||
575
2.47k
        total_v_size != (size_t)total_v_size) {
576
0
      return 0;
577
0
    }
578
2.47k
  }
579
2.47k
  if (yuv_bit_depth > 8 &&
580
195
      (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.47k
  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.47k
  if (rgb_bit_depth == yuv_bit_depth) {
590
2.47k
    memcpy(&scaled_matrix, yuv_matrix, sizeof(scaled_matrix));
591
2.47k
  } 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.47k
  scaled_matrix.rgb_to_y[3] = Shift(yuv_matrix->rgb_to_y[3], sfix);
604
2.47k
  scaled_matrix.rgb_to_u[3] = Shift(yuv_matrix->rgb_to_u[3], sfix);
605
2.47k
  scaled_matrix.rgb_to_v[3] = Shift(yuv_matrix->rgb_to_v[3], sfix);
606
607
2.47k
  return DoSharpArgbToYuv(
608
2.47k
      (const uint8_t*)r_ptr, (const uint8_t*)g_ptr, (const uint8_t*)b_ptr,
609
2.47k
      rgb_step, rgb_stride, rgb_bit_depth, (uint8_t*)y_ptr, y_stride,
610
2.47k
      (uint8_t*)u_ptr, u_stride, (uint8_t*)v_ptr, v_stride, yuv_bit_depth,
611
2.47k
      width, height, &scaled_matrix, transfer_type);
612
2.47k
}
613
614
//------------------------------------------------------------------------------