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