1 | // Copyright 2011 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 | // YUV to RGB upsampling functions. |
11 | // |
12 | // Author: somnath@google.com (Somnath Banerjee) |
13 | |
14 | #include "./dsp.h" |
15 | #include "./yuv.h" |
16 | |
17 | #include <assert.h> |
18 | |
19 | //------------------------------------------------------------------------------ |
20 | // Fancy upsampler |
21 | |
22 | #ifdef FANCY_UPSAMPLING |
23 | |
24 | // Fancy upsampling functions to convert YUV to RGB |
25 | WebPUpsampleLinePairFunc WebPUpsamplers[MODE_LAST]; |
26 | |
27 | // Given samples laid out in a square as: |
28 | // [a b] |
29 | // [c d] |
30 | // we interpolate u/v as: |
31 | // ([9*a + 3*b + 3*c + d 3*a + 9*b + 3*c + d] + [8 8]) / 16 |
32 | // ([3*a + b + 9*c + 3*d a + 3*b + 3*c + 9*d] [8 8]) / 16 |
33 | |
34 | // We process u and v together stashed into 32bit (16bit each). |
35 | #define LOAD_UV(u, v) ((u) | ((v) << 16)) |
36 | |
37 | #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \ |
38 | static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \ |
39 | const uint8_t* top_u, const uint8_t* top_v, \ |
40 | const uint8_t* cur_u, const uint8_t* cur_v, \ |
41 | uint8_t* top_dst, uint8_t* bottom_dst, int len) { \ |
42 | int x; \ |
43 | const int last_pixel_pair = (len - 1) >> 1; \ |
44 | uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]); /* top-left sample */ \ |
45 | uint32_t l_uv = LOAD_UV(cur_u[0], cur_v[0]); /* left-sample */ \ |
46 | assert(top_y != NULL); \ |
47 | { \ |
48 | const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \ |
49 | FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \ |
50 | } \ |
51 | if (bottom_y != NULL) { \ |
52 | const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \ |
53 | FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst); \ |
54 | } \ |
55 | for (x = 1; x <= last_pixel_pair; ++x) { \ |
56 | const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]); /* top sample */ \ |
57 | const uint32_t uv = LOAD_UV(cur_u[x], cur_v[x]); /* sample */ \ |
58 | /* precompute invariant values associated with first and second diagonals*/\ |
59 | const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u; \ |
60 | const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3; \ |
61 | const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3; \ |
62 | { \ |
63 | const uint32_t uv0 = (diag_12 + tl_uv) >> 1; \ |
64 | const uint32_t uv1 = (diag_03 + t_uv) >> 1; \ |
65 | FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \ |
66 | top_dst + (2 * x - 1) * XSTEP); \ |
67 | FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16), \ |
68 | top_dst + (2 * x - 0) * XSTEP); \ |
69 | } \ |
70 | if (bottom_y != NULL) { \ |
71 | const uint32_t uv0 = (diag_03 + l_uv) >> 1; \ |
72 | const uint32_t uv1 = (diag_12 + uv) >> 1; \ |
73 | FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \ |
74 | bottom_dst + (2 * x - 1) * XSTEP); \ |
75 | FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16), \ |
76 | bottom_dst + (2 * x + 0) * XSTEP); \ |
77 | } \ |
78 | tl_uv = t_uv; \ |
79 | l_uv = uv; \ |
80 | } \ |
81 | if (!(len & 1)) { \ |
82 | { \ |
83 | const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \ |
84 | FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16), \ |
85 | top_dst + (len - 1) * XSTEP); \ |
86 | } \ |
87 | if (bottom_y != NULL) { \ |
88 | const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \ |
89 | FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16), \ |
90 | bottom_dst + (len - 1) * XSTEP); \ |
91 | } \ |
92 | } \ |
93 | } |
94 | |
95 | // All variants implemented. |
96 | UPSAMPLE_FUNC(UpsampleRgbLinePair, VP8YuvToRgb, 3) |
97 | UPSAMPLE_FUNC(UpsampleBgrLinePair, VP8YuvToBgr, 3) |
98 | UPSAMPLE_FUNC(UpsampleRgbaLinePair, VP8YuvToRgba, 4) |
99 | UPSAMPLE_FUNC(UpsampleBgraLinePair, VP8YuvToBgra, 4) |
100 | UPSAMPLE_FUNC(UpsampleArgbLinePair, VP8YuvToArgb, 4) |
101 | UPSAMPLE_FUNC(UpsampleRgba4444LinePair, VP8YuvToRgba4444, 2) |
102 | UPSAMPLE_FUNC(UpsampleRgb565LinePair, VP8YuvToRgb565, 2) |
103 | |
104 | #undef LOAD_UV |
105 | #undef UPSAMPLE_FUNC |
106 | |
107 | #endif // FANCY_UPSAMPLING |
108 | |
109 | //------------------------------------------------------------------------------ |
110 | |
111 | #if !defined(FANCY_UPSAMPLING) |
112 | #define DUAL_SAMPLE_FUNC(FUNC_NAME, FUNC) \ |
113 | static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bot_y, \ |
114 | const uint8_t* top_u, const uint8_t* top_v, \ |
115 | const uint8_t* bot_u, const uint8_t* bot_v, \ |
116 | uint8_t* top_dst, uint8_t* bot_dst, int len) { \ |
117 | const int half_len = len >> 1; \ |
118 | int x; \ |
119 | assert(top_dst != NULL); \ |
120 | { \ |
121 | for (x = 0; x < half_len; ++x) { \ |
122 | FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x + 0); \ |
123 | FUNC(top_y[2 * x + 1], top_u[x], top_v[x], top_dst + 8 * x + 4); \ |
124 | } \ |
125 | if (len & 1) FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x); \ |
126 | } \ |
127 | if (bot_dst != NULL) { \ |
128 | for (x = 0; x < half_len; ++x) { \ |
129 | FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x + 0); \ |
130 | FUNC(bot_y[2 * x + 1], bot_u[x], bot_v[x], bot_dst + 8 * x + 4); \ |
131 | } \ |
132 | if (len & 1) FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x); \ |
133 | } \ |
134 | } |
135 | |
136 | DUAL_SAMPLE_FUNC(DualLineSamplerBGRA, VP8YuvToBgra) |
137 | DUAL_SAMPLE_FUNC(DualLineSamplerARGB, VP8YuvToArgb) |
138 | #undef DUAL_SAMPLE_FUNC |
139 | |
140 | #endif // !FANCY_UPSAMPLING |
141 | |
142 | WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last) { |
143 | WebPInitUpsamplers(); |
144 | VP8YUVInit(); |
145 | #ifdef FANCY_UPSAMPLING |
146 | return WebPUpsamplers[alpha_is_last ? MODE_BGRA : MODE_ARGB]; |
147 | #else |
148 | return (alpha_is_last ? DualLineSamplerBGRA : DualLineSamplerARGB); |
149 | #endif |
150 | } |
151 | |
152 | //------------------------------------------------------------------------------ |
153 | // YUV444 converter |
154 | |
155 | #define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP) \ |
156 | extern void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \ |
157 | uint8_t* dst, int len); \ |
158 | void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \ |
159 | uint8_t* dst, int len) { \ |
160 | int i; \ |
161 | for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * XSTEP]); \ |
162 | } |
163 | |
164 | YUV444_FUNC(WebPYuv444ToRgbC, VP8YuvToRgb, 3) |
165 | YUV444_FUNC(WebPYuv444ToBgrC, VP8YuvToBgr, 3) |
166 | YUV444_FUNC(WebPYuv444ToRgbaC, VP8YuvToRgba, 4) |
167 | YUV444_FUNC(WebPYuv444ToBgraC, VP8YuvToBgra, 4) |
168 | YUV444_FUNC(WebPYuv444ToArgbC, VP8YuvToArgb, 4) |
169 | YUV444_FUNC(WebPYuv444ToRgba4444C, VP8YuvToRgba4444, 2) |
170 | YUV444_FUNC(WebPYuv444ToRgb565C, VP8YuvToRgb565, 2) |
171 | |
172 | #undef YUV444_FUNC |
173 | |
174 | WebPYUV444Converter WebPYUV444Converters[MODE_LAST]; |
175 | |
176 | extern void WebPInitYUV444ConvertersMIPSdspR2(void); |
177 | extern void (void); |
178 | |
179 | static volatile VP8CPUInfo upsampling_last_cpuinfo_used1 = |
180 | (VP8CPUInfo)&upsampling_last_cpuinfo_used1; |
181 | |
182 | WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444Converters(void) { |
183 | if (upsampling_last_cpuinfo_used1 == VP8GetCPUInfo) return; |
184 | |
185 | WebPYUV444Converters[MODE_RGB] = WebPYuv444ToRgbC; |
186 | WebPYUV444Converters[MODE_RGBA] = WebPYuv444ToRgbaC; |
187 | WebPYUV444Converters[MODE_BGR] = WebPYuv444ToBgrC; |
188 | WebPYUV444Converters[MODE_BGRA] = WebPYuv444ToBgraC; |
189 | WebPYUV444Converters[MODE_ARGB] = WebPYuv444ToArgbC; |
190 | WebPYUV444Converters[MODE_RGBA_4444] = WebPYuv444ToRgba4444C; |
191 | WebPYUV444Converters[MODE_RGB_565] = WebPYuv444ToRgb565C; |
192 | WebPYUV444Converters[MODE_rgbA] = WebPYuv444ToRgbaC; |
193 | WebPYUV444Converters[MODE_bgrA] = WebPYuv444ToBgraC; |
194 | WebPYUV444Converters[MODE_Argb] = WebPYuv444ToArgbC; |
195 | WebPYUV444Converters[MODE_rgbA_4444] = WebPYuv444ToRgba4444C; |
196 | |
197 | if (VP8GetCPUInfo != NULL) { |
198 | #if defined(WEBP_USE_SSE2) |
199 | if (VP8GetCPUInfo(kSSE2)) { |
200 | WebPInitYUV444ConvertersSSE2(); |
201 | } |
202 | #endif |
203 | #if defined(WEBP_USE_MIPS_DSP_R2) |
204 | if (VP8GetCPUInfo(kMIPSdspR2)) { |
205 | WebPInitYUV444ConvertersMIPSdspR2(); |
206 | } |
207 | #endif |
208 | } |
209 | upsampling_last_cpuinfo_used1 = VP8GetCPUInfo; |
210 | } |
211 | |
212 | //------------------------------------------------------------------------------ |
213 | // Main calls |
214 | |
215 | extern void (void); |
216 | extern void WebPInitUpsamplersNEON(void); |
217 | extern void WebPInitUpsamplersMIPSdspR2(void); |
218 | extern void WebPInitUpsamplersMSA(void); |
219 | |
220 | static volatile VP8CPUInfo upsampling_last_cpuinfo_used2 = |
221 | (VP8CPUInfo)&upsampling_last_cpuinfo_used2; |
222 | |
223 | WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplers(void) { |
224 | if (upsampling_last_cpuinfo_used2 == VP8GetCPUInfo) return; |
225 | |
226 | #ifdef FANCY_UPSAMPLING |
227 | WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair; |
228 | WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair; |
229 | WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair; |
230 | WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair; |
231 | WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair; |
232 | WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair; |
233 | WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair; |
234 | WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair; |
235 | WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair; |
236 | WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair; |
237 | WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair; |
238 | |
239 | // If defined, use CPUInfo() to overwrite some pointers with faster versions. |
240 | if (VP8GetCPUInfo != NULL) { |
241 | #if defined(WEBP_USE_SSE2) |
242 | if (VP8GetCPUInfo(kSSE2)) { |
243 | WebPInitUpsamplersSSE2(); |
244 | } |
245 | #endif |
246 | #if defined(WEBP_USE_NEON) |
247 | if (VP8GetCPUInfo(kNEON)) { |
248 | WebPInitUpsamplersNEON(); |
249 | } |
250 | #endif |
251 | #if defined(WEBP_USE_MIPS_DSP_R2) |
252 | if (VP8GetCPUInfo(kMIPSdspR2)) { |
253 | WebPInitUpsamplersMIPSdspR2(); |
254 | } |
255 | #endif |
256 | #if defined(WEBP_USE_MSA) |
257 | if (VP8GetCPUInfo(kMSA)) { |
258 | WebPInitUpsamplersMSA(); |
259 | } |
260 | #endif |
261 | } |
262 | #endif // FANCY_UPSAMPLING |
263 | upsampling_last_cpuinfo_used2 = VP8GetCPUInfo; |
264 | } |
265 | |
266 | //------------------------------------------------------------------------------ |
267 | |