/work/libwebp/sharpyuv/sharpyuv_dsp.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 | | // Speed-critical functions for Sharp YUV. |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include "./sharpyuv_dsp.h" |
15 | | |
16 | | #include <assert.h> |
17 | | #include <stddef.h> |
18 | | #include <stdint.h> |
19 | | #include <stdlib.h> |
20 | | |
21 | | #include "./sharpyuv.h" |
22 | | #include "./sharpyuv_cpu.h" |
23 | | #include "./sharpyuv_gamma.h" |
24 | | #include "src/dsp/cpu.h" |
25 | | #include "webp/types.h" |
26 | | |
27 | | //----------------------------------------------------------------------------- |
28 | | |
29 | | #if !WEBP_NEON_OMIT_C_CODE |
30 | | static uint64_t SharpYuvUpdateY_C(const uint16_t* ref, const uint16_t* src, |
31 | 0 | uint16_t* dst, int len, int bit_depth) { |
32 | 0 | uint64_t diff = 0; |
33 | 0 | int i; |
34 | 0 | const int max_y = (1 << bit_depth) - 1; |
35 | 0 | for (i = 0; i < len; ++i) { |
36 | 0 | const int diff_y = ref[i] - src[i]; |
37 | 0 | const int new_y = (int)dst[i] + diff_y; |
38 | 0 | dst[i] = SharpYuvClip16(new_y, max_y); |
39 | 0 | diff += (uint64_t)abs(diff_y); |
40 | 0 | } |
41 | 0 | return diff; |
42 | 0 | } |
43 | | |
44 | | static void SharpYuvUpdateRGB_C(const int16_t* ref, const int16_t* src, |
45 | 0 | int16_t* dst, int len) { |
46 | 0 | int i; |
47 | 0 | for (i = 0; i < len; ++i) { |
48 | 0 | const int diff_uv = ref[i] - src[i]; |
49 | 0 | dst[i] += diff_uv; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | static void SharpYuvFilterRow_C(const int16_t* A, const int16_t* B, int len, |
54 | | const uint16_t* best_y, uint16_t* out, |
55 | 0 | int bit_depth) { |
56 | 0 | int i; |
57 | 0 | const int max_y = (1 << bit_depth) - 1; |
58 | 0 | for (i = 0; i < len; ++i, ++A, ++B) { |
59 | 0 | const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4; |
60 | 0 | const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4; |
61 | 0 | out[2 * i + 0] = SharpYuvClip16(best_y[2 * i + 0] + v0, max_y); |
62 | 0 | out[2 * i + 1] = SharpYuvClip16(best_y[2 * i + 1] + v1, max_y); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | static void SharpYuvConvertRowY_C(const uint16_t* best_y, |
67 | | const int16_t* best_uv, int width, int uv_w, |
68 | | const int coeffs[4], int sfix, |
69 | 0 | int yuv_bit_depth, void* y_out) { |
70 | 0 | const int yuv_max = (1 << yuv_bit_depth) - 1; |
71 | 0 | const size_t uv_w_size = (size_t)uv_w; |
72 | 0 | int i; |
73 | 0 | for (i = 0; i < width; ++i) { |
74 | 0 | const size_t off = (size_t)(i >> 1); |
75 | 0 | const int W = best_y[i]; |
76 | 0 | const int r = best_uv[off + 0 * uv_w_size] + W; |
77 | 0 | const int g = best_uv[off + 1 * uv_w_size] + W; |
78 | 0 | const int b = best_uv[off + 2 * uv_w_size] + W; |
79 | 0 | const int y = SharpYuvConvertComponent(r, g, b, coeffs, sfix); |
80 | 0 | if (yuv_bit_depth <= 8) { |
81 | 0 | ((uint8_t*)y_out)[i] = (uint8_t)SharpYuvClip16(y, 255); |
82 | 0 | } else { |
83 | 0 | ((uint16_t*)y_out)[i] = SharpYuvClip16(y, yuv_max); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | static void SharpYuvConvertRowUV_C(const int16_t* best_uv, int uv_w, |
89 | | const int coeffs_u[4], const int coeffs_v[4], |
90 | | int sfix, int yuv_bit_depth, void* u_out, |
91 | 0 | void* v_out) { |
92 | 0 | const int yuv_max = (1 << yuv_bit_depth) - 1; |
93 | 0 | const size_t uv_w_size = (size_t)uv_w; |
94 | 0 | int i; |
95 | 0 | for (i = 0; i < uv_w; ++i) { |
96 | 0 | const int r = best_uv[i + 0 * uv_w_size]; |
97 | 0 | const int g = best_uv[i + 1 * uv_w_size]; |
98 | 0 | const int b = best_uv[i + 2 * uv_w_size]; |
99 | 0 | const int u = SharpYuvConvertComponent(r, g, b, coeffs_u, sfix); |
100 | 0 | const int v = SharpYuvConvertComponent(r, g, b, coeffs_v, sfix); |
101 | 0 | if (yuv_bit_depth <= 8) { |
102 | 0 | ((uint8_t*)u_out)[i] = (uint8_t)SharpYuvClip16(u, 255); |
103 | 0 | ((uint8_t*)v_out)[i] = (uint8_t)SharpYuvClip16(v, 255); |
104 | 0 | } else { |
105 | 0 | ((uint16_t*)u_out)[i] = SharpYuvClip16(u, yuv_max); |
106 | 0 | ((uint16_t*)v_out)[i] = SharpYuvClip16(v, yuv_max); |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | // Fast paths for the BT.709 and sRGB transfer functions, used by |
112 | | // UpdateW/UpdateChroma in sharpyuv.c. |
113 | | static void UpdateW_C(const uint16_t* src, uint16_t* dst, int w, int bit_depth, |
114 | 0 | SharpYuvTransferFunctionType transfer_type) { |
115 | 0 | const size_t w_size = (size_t)w; |
116 | 0 | int i = 0; |
117 | 0 | do { |
118 | 0 | const uint32_t R = |
119 | 0 | SharpYuvGammaToLinear(src[0 * w_size + i], bit_depth, transfer_type); |
120 | 0 | const uint32_t G = |
121 | 0 | SharpYuvGammaToLinear(src[1 * w_size + i], bit_depth, transfer_type); |
122 | 0 | const uint32_t B = |
123 | 0 | SharpYuvGammaToLinear(src[2 * w_size + i], bit_depth, transfer_type); |
124 | 0 | const int Y = SharpYuvRGBToGray(R, G, B); |
125 | 0 | dst[i] = SharpYuvLinearToGamma((uint32_t)Y, bit_depth, transfer_type); |
126 | 0 | } while (++i < w); |
127 | 0 | } |
128 | | |
129 | | static uint32_t ScaleDown_C(uint16_t a, uint16_t b, uint16_t c, uint16_t d, |
130 | | int bit_depth, |
131 | 0 | SharpYuvTransferFunctionType transfer_type) { |
132 | 0 | const uint32_t A = SharpYuvGammaToLinear(a, bit_depth, transfer_type); |
133 | 0 | const uint32_t B = SharpYuvGammaToLinear(b, bit_depth, transfer_type); |
134 | 0 | const uint32_t C = SharpYuvGammaToLinear(c, bit_depth, transfer_type); |
135 | 0 | const uint32_t D = SharpYuvGammaToLinear(d, bit_depth, transfer_type); |
136 | 0 | return SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth, |
137 | 0 | transfer_type); |
138 | 0 | } |
139 | | |
140 | | static void UpdateChroma_C(const uint16_t* src1, const uint16_t* src2, |
141 | | int16_t* dst, int uv_w, int bit_depth, |
142 | 0 | SharpYuvTransferFunctionType transfer_type) { |
143 | 0 | const size_t uv_w_size = (size_t)uv_w; |
144 | 0 | int i = 0; |
145 | 0 | do { |
146 | 0 | const int r = |
147 | 0 | (int)ScaleDown_C(src1[0 * uv_w_size + 0], src1[0 * uv_w_size + 1], |
148 | 0 | src2[0 * uv_w_size + 0], src2[0 * uv_w_size + 1], |
149 | 0 | bit_depth, transfer_type); |
150 | 0 | const int g = |
151 | 0 | (int)ScaleDown_C(src1[2 * uv_w_size + 0], src1[2 * uv_w_size + 1], |
152 | 0 | src2[2 * uv_w_size + 0], src2[2 * uv_w_size + 1], |
153 | 0 | bit_depth, transfer_type); |
154 | 0 | const int b = |
155 | 0 | (int)ScaleDown_C(src1[4 * uv_w_size + 0], src1[4 * uv_w_size + 1], |
156 | 0 | src2[4 * uv_w_size + 0], src2[4 * uv_w_size + 1], |
157 | 0 | bit_depth, transfer_type); |
158 | 0 | const int W = SharpYuvRGBToGray(r, g, b); |
159 | 0 | dst[0 * uv_w_size] = (int16_t)(r - W); |
160 | 0 | dst[1 * uv_w_size] = (int16_t)(g - W); |
161 | 0 | dst[2 * uv_w_size] = (int16_t)(b - W); |
162 | 0 | dst += 1; |
163 | 0 | src1 += 2; |
164 | 0 | src2 += 2; |
165 | 0 | } while (++i < uv_w); |
166 | 0 | } |
167 | | |
168 | | static void SharpYuvUpdateW709_C(const uint16_t* src, uint16_t* dst, int w, |
169 | 0 | int bit_depth) { |
170 | 0 | UpdateW_C(src, dst, w, bit_depth, kSharpYuvTransferFunctionBt709); |
171 | 0 | } |
172 | | |
173 | | static void SharpYuvUpdateChroma709_C(const uint16_t* src1, |
174 | | const uint16_t* src2, int16_t* dst, |
175 | 0 | int uv_w, int bit_depth) { |
176 | 0 | UpdateChroma_C(src1, src2, dst, uv_w, bit_depth, |
177 | 0 | kSharpYuvTransferFunctionBt709); |
178 | 0 | } |
179 | | |
180 | | static void SharpYuvUpdateWSrgb_C(const uint16_t* src, uint16_t* dst, int w, |
181 | 0 | int bit_depth) { |
182 | 0 | UpdateW_C(src, dst, w, bit_depth, kSharpYuvTransferFunctionSrgb); |
183 | 0 | } |
184 | | |
185 | | static void SharpYuvUpdateChromaSrgb_C(const uint16_t* src1, |
186 | | const uint16_t* src2, int16_t* dst, |
187 | 0 | int uv_w, int bit_depth) { |
188 | 0 | UpdateChroma_C(src1, src2, dst, uv_w, bit_depth, |
189 | 0 | kSharpYuvTransferFunctionSrgb); |
190 | 0 | } |
191 | | #endif // !WEBP_NEON_OMIT_C_CODE |
192 | | |
193 | | //----------------------------------------------------------------------------- |
194 | | |
195 | | uint64_t (*SharpYuvUpdateY)(const uint16_t* src, const uint16_t* ref, |
196 | | uint16_t* dst, int len, int bit_depth); |
197 | | void (*SharpYuvUpdateRGB)(const int16_t* src, const int16_t* ref, int16_t* dst, |
198 | | int len); |
199 | | void (*SharpYuvFilterRow)(const int16_t* A, const int16_t* B, int len, |
200 | | const uint16_t* best_y, uint16_t* out, int bit_depth); |
201 | | void (*SharpYuvConvertRowY)(const uint16_t* best_y, const int16_t* best_uv, |
202 | | int width, int uv_w, const int coeffs[4], int sfix, |
203 | | int yuv_bit_depth, void* y_out); |
204 | | void (*SharpYuvConvertRowUV)(const int16_t* best_uv, int uv_w, |
205 | | const int coeffs_u[4], const int coeffs_v[4], |
206 | | int sfix, int yuv_bit_depth, void* u_out, |
207 | | void* v_out); |
208 | | void (*SharpYuvUpdateW709)(const uint16_t* src, uint16_t* dst, int w, |
209 | | int bit_depth); |
210 | | void (*SharpYuvUpdateChroma709)(const uint16_t* src1, const uint16_t* src2, |
211 | | int16_t* dst, int uv_w, int bit_depth); |
212 | | void (*SharpYuvUpdateWSrgb)(const uint16_t* src, uint16_t* dst, int w, |
213 | | int bit_depth); |
214 | | void (*SharpYuvUpdateChromaSrgb)(const uint16_t* src1, const uint16_t* src2, |
215 | | int16_t* dst, int uv_w, int bit_depth); |
216 | | |
217 | | extern VP8CPUInfo SharpYuvGetCPUInfo; |
218 | | extern void InitSharpYuvSSE2(void); |
219 | | extern void InitSharpYuvAVX2(void); |
220 | | extern void InitSharpYuvNEON(void); |
221 | | |
222 | 0 | void SharpYuvInitDsp(void) { |
223 | 0 | #if !WEBP_NEON_OMIT_C_CODE |
224 | 0 | SharpYuvUpdateY = SharpYuvUpdateY_C; |
225 | 0 | SharpYuvUpdateRGB = SharpYuvUpdateRGB_C; |
226 | 0 | SharpYuvFilterRow = SharpYuvFilterRow_C; |
227 | 0 | SharpYuvConvertRowY = SharpYuvConvertRowY_C; |
228 | 0 | SharpYuvConvertRowUV = SharpYuvConvertRowUV_C; |
229 | 0 | SharpYuvUpdateW709 = SharpYuvUpdateW709_C; |
230 | 0 | SharpYuvUpdateChroma709 = SharpYuvUpdateChroma709_C; |
231 | 0 | SharpYuvUpdateWSrgb = SharpYuvUpdateWSrgb_C; |
232 | 0 | SharpYuvUpdateChromaSrgb = SharpYuvUpdateChromaSrgb_C; |
233 | 0 | #endif |
234 | |
|
235 | 0 | if (SharpYuvGetCPUInfo != NULL) { |
236 | 0 | #if defined(WEBP_HAVE_SSE2) |
237 | 0 | if (SharpYuvGetCPUInfo(kSSE2)) { |
238 | 0 | InitSharpYuvSSE2(); |
239 | 0 | } |
240 | 0 | #endif // WEBP_HAVE_SSE2 |
241 | 0 | #if defined(WEBP_HAVE_AVX2) |
242 | 0 | if (SharpYuvGetCPUInfo(kAVX2)) { |
243 | 0 | InitSharpYuvAVX2(); |
244 | 0 | } |
245 | 0 | #endif // WEBP_HAVE_AVX2 |
246 | 0 | } |
247 | |
|
248 | | #if defined(WEBP_HAVE_NEON) |
249 | | if (WEBP_NEON_OMIT_C_CODE || |
250 | | (SharpYuvGetCPUInfo != NULL && SharpYuvGetCPUInfo(kNEON))) { |
251 | | InitSharpYuvNEON(); |
252 | | } |
253 | | #endif // WEBP_HAVE_NEON |
254 | |
|
255 | 0 | assert(SharpYuvUpdateY != NULL); |
256 | 0 | assert(SharpYuvUpdateRGB != NULL); |
257 | 0 | assert(SharpYuvFilterRow != NULL); |
258 | 0 | assert(SharpYuvConvertRowY != NULL); |
259 | 0 | assert(SharpYuvConvertRowUV != NULL); |
260 | 0 | assert(SharpYuvUpdateW709 != NULL); |
261 | 0 | assert(SharpYuvUpdateChroma709 != NULL); |
262 | 0 | assert(SharpYuvUpdateWSrgb != NULL); |
263 | | assert(SharpYuvUpdateChromaSrgb != NULL); |
264 | 0 | } |