/src/libwebp/src/dsp/yuv.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2010 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 | | // inline YUV<->RGB conversion function |
11 | | // |
12 | | // The exact naming is Y'CbCr, following the ITU-R BT.601 standard. |
13 | | // More information at: https://en.wikipedia.org/wiki/YCbCr |
14 | | // Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16 |
15 | | // U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128 |
16 | | // V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 128 |
17 | | // We use 16bit fixed point operations for RGB->YUV conversion (YUV_FIX). |
18 | | // |
19 | | // For the Y'CbCr to RGB conversion, the BT.601 specification reads: |
20 | | // R = 1.164 * (Y-16) + 1.596 * (V-128) |
21 | | // G = 1.164 * (Y-16) - 0.813 * (V-128) - 0.391 * (U-128) |
22 | | // B = 1.164 * (Y-16) + 2.018 * (U-128) |
23 | | // where Y is in the [16,235] range, and U/V in the [16,240] range. |
24 | | // |
25 | | // The fixed-point implementation used here is: |
26 | | // R = (19077 . y + 26149 . v - 14234) >> 6 |
27 | | // G = (19077 . y - 6419 . u - 13320 . v + 8708) >> 6 |
28 | | // B = (19077 . y + 33050 . u - 17685) >> 6 |
29 | | // where the '.' operator is the mulhi_epu16 variant: |
30 | | // a . b = ((a << 8) * b) >> 16 |
31 | | // that preserves 8 bits of fractional precision before final descaling. |
32 | | |
33 | | // Author: Skal (pascal.massimino@gmail.com) |
34 | | |
35 | | #ifndef WEBP_DSP_YUV_H_ |
36 | | #define WEBP_DSP_YUV_H_ |
37 | | |
38 | | #include "src/dsp/dsp.h" |
39 | | #include "src/dec/vp8_dec.h" |
40 | | |
41 | | //------------------------------------------------------------------------------ |
42 | | // YUV -> RGB conversion |
43 | | |
44 | | #ifdef __cplusplus |
45 | | extern "C" { |
46 | | #endif |
47 | | |
48 | | enum { |
49 | | YUV_FIX = 16, // fixed-point precision for RGB->YUV |
50 | | YUV_HALF = 1 << (YUV_FIX - 1), |
51 | | |
52 | | YUV_FIX2 = 6, // fixed-point precision for YUV->RGB |
53 | | YUV_MASK2 = (256 << YUV_FIX2) - 1 |
54 | | }; |
55 | | |
56 | | //------------------------------------------------------------------------------ |
57 | | // slower on x86 by ~7-8%, but bit-exact with the SSE2/NEON version |
58 | | |
59 | 0 | static WEBP_INLINE int MultHi(int v, int coeff) { // _mm_mulhi_epu16 emulation |
60 | 0 | return (v * coeff) >> 8; |
61 | 0 | } Unexecuted instantiation: io_dec.c:MultHi Unexecuted instantiation: upsampling.c:MultHi Unexecuted instantiation: yuv.c:MultHi Unexecuted instantiation: upsampling_sse2.c:MultHi Unexecuted instantiation: yuv_sse2.c:MultHi Unexecuted instantiation: upsampling_sse41.c:MultHi Unexecuted instantiation: yuv_sse41.c:MultHi Unexecuted instantiation: picture_csp_enc.c:MultHi Unexecuted instantiation: picture_tools_enc.c:MultHi Unexecuted instantiation: lossless_enc.c:MultHi |
62 | | |
63 | 0 | static WEBP_INLINE int VP8Clip8(int v) { |
64 | 0 | return ((v & ~YUV_MASK2) == 0) ? (v >> YUV_FIX2) : (v < 0) ? 0 : 255; |
65 | 0 | } Unexecuted instantiation: io_dec.c:VP8Clip8 Unexecuted instantiation: upsampling.c:VP8Clip8 Unexecuted instantiation: yuv.c:VP8Clip8 Unexecuted instantiation: upsampling_sse2.c:VP8Clip8 Unexecuted instantiation: yuv_sse2.c:VP8Clip8 Unexecuted instantiation: upsampling_sse41.c:VP8Clip8 Unexecuted instantiation: yuv_sse41.c:VP8Clip8 Unexecuted instantiation: picture_csp_enc.c:VP8Clip8 Unexecuted instantiation: picture_tools_enc.c:VP8Clip8 Unexecuted instantiation: lossless_enc.c:VP8Clip8 |
66 | | |
67 | 0 | static WEBP_INLINE int VP8YUVToR(int y, int v) { |
68 | 0 | return VP8Clip8(MultHi(y, 19077) + MultHi(v, 26149) - 14234); |
69 | 0 | } Unexecuted instantiation: io_dec.c:VP8YUVToR Unexecuted instantiation: upsampling.c:VP8YUVToR Unexecuted instantiation: yuv.c:VP8YUVToR Unexecuted instantiation: upsampling_sse2.c:VP8YUVToR Unexecuted instantiation: yuv_sse2.c:VP8YUVToR Unexecuted instantiation: upsampling_sse41.c:VP8YUVToR Unexecuted instantiation: yuv_sse41.c:VP8YUVToR Unexecuted instantiation: picture_csp_enc.c:VP8YUVToR Unexecuted instantiation: picture_tools_enc.c:VP8YUVToR Unexecuted instantiation: lossless_enc.c:VP8YUVToR |
70 | | |
71 | 0 | static WEBP_INLINE int VP8YUVToG(int y, int u, int v) { |
72 | 0 | return VP8Clip8(MultHi(y, 19077) - MultHi(u, 6419) - MultHi(v, 13320) + 8708); |
73 | 0 | } Unexecuted instantiation: io_dec.c:VP8YUVToG Unexecuted instantiation: upsampling.c:VP8YUVToG Unexecuted instantiation: yuv.c:VP8YUVToG Unexecuted instantiation: upsampling_sse2.c:VP8YUVToG Unexecuted instantiation: yuv_sse2.c:VP8YUVToG Unexecuted instantiation: upsampling_sse41.c:VP8YUVToG Unexecuted instantiation: yuv_sse41.c:VP8YUVToG Unexecuted instantiation: picture_csp_enc.c:VP8YUVToG Unexecuted instantiation: picture_tools_enc.c:VP8YUVToG Unexecuted instantiation: lossless_enc.c:VP8YUVToG |
74 | | |
75 | 0 | static WEBP_INLINE int VP8YUVToB(int y, int u) { |
76 | 0 | return VP8Clip8(MultHi(y, 19077) + MultHi(u, 33050) - 17685); |
77 | 0 | } Unexecuted instantiation: io_dec.c:VP8YUVToB Unexecuted instantiation: upsampling.c:VP8YUVToB Unexecuted instantiation: yuv.c:VP8YUVToB Unexecuted instantiation: upsampling_sse2.c:VP8YUVToB Unexecuted instantiation: yuv_sse2.c:VP8YUVToB Unexecuted instantiation: upsampling_sse41.c:VP8YUVToB Unexecuted instantiation: yuv_sse41.c:VP8YUVToB Unexecuted instantiation: picture_csp_enc.c:VP8YUVToB Unexecuted instantiation: picture_tools_enc.c:VP8YUVToB Unexecuted instantiation: lossless_enc.c:VP8YUVToB |
78 | | |
79 | | static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v, |
80 | 0 | uint8_t* const rgb) { |
81 | 0 | rgb[0] = VP8YUVToR(y, v); |
82 | 0 | rgb[1] = VP8YUVToG(y, u, v); |
83 | 0 | rgb[2] = VP8YUVToB(y, u); |
84 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToRgb Unexecuted instantiation: upsampling.c:VP8YuvToRgb Unexecuted instantiation: yuv.c:VP8YuvToRgb Unexecuted instantiation: upsampling_sse2.c:VP8YuvToRgb Unexecuted instantiation: yuv_sse2.c:VP8YuvToRgb Unexecuted instantiation: upsampling_sse41.c:VP8YuvToRgb Unexecuted instantiation: yuv_sse41.c:VP8YuvToRgb Unexecuted instantiation: picture_csp_enc.c:VP8YuvToRgb Unexecuted instantiation: picture_tools_enc.c:VP8YuvToRgb Unexecuted instantiation: lossless_enc.c:VP8YuvToRgb |
85 | | |
86 | | static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v, |
87 | 0 | uint8_t* const bgr) { |
88 | 0 | bgr[0] = VP8YUVToB(y, u); |
89 | 0 | bgr[1] = VP8YUVToG(y, u, v); |
90 | 0 | bgr[2] = VP8YUVToR(y, v); |
91 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToBgr Unexecuted instantiation: upsampling.c:VP8YuvToBgr Unexecuted instantiation: yuv.c:VP8YuvToBgr Unexecuted instantiation: upsampling_sse2.c:VP8YuvToBgr Unexecuted instantiation: yuv_sse2.c:VP8YuvToBgr Unexecuted instantiation: upsampling_sse41.c:VP8YuvToBgr Unexecuted instantiation: yuv_sse41.c:VP8YuvToBgr Unexecuted instantiation: picture_csp_enc.c:VP8YuvToBgr Unexecuted instantiation: picture_tools_enc.c:VP8YuvToBgr Unexecuted instantiation: lossless_enc.c:VP8YuvToBgr |
92 | | |
93 | | static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v, |
94 | 0 | uint8_t* const rgb) { |
95 | 0 | const int r = VP8YUVToR(y, v); // 5 usable bits |
96 | 0 | const int g = VP8YUVToG(y, u, v); // 6 usable bits |
97 | 0 | const int b = VP8YUVToB(y, u); // 5 usable bits |
98 | 0 | const int rg = (r & 0xf8) | (g >> 5); |
99 | 0 | const int gb = ((g << 3) & 0xe0) | (b >> 3); |
100 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
101 | | rgb[0] = gb; |
102 | | rgb[1] = rg; |
103 | | #else |
104 | 0 | rgb[0] = rg; |
105 | 0 | rgb[1] = gb; |
106 | 0 | #endif |
107 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToRgb565 Unexecuted instantiation: upsampling.c:VP8YuvToRgb565 Unexecuted instantiation: yuv.c:VP8YuvToRgb565 Unexecuted instantiation: upsampling_sse2.c:VP8YuvToRgb565 Unexecuted instantiation: yuv_sse2.c:VP8YuvToRgb565 Unexecuted instantiation: upsampling_sse41.c:VP8YuvToRgb565 Unexecuted instantiation: yuv_sse41.c:VP8YuvToRgb565 Unexecuted instantiation: picture_csp_enc.c:VP8YuvToRgb565 Unexecuted instantiation: picture_tools_enc.c:VP8YuvToRgb565 Unexecuted instantiation: lossless_enc.c:VP8YuvToRgb565 |
108 | | |
109 | | static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v, |
110 | 0 | uint8_t* const argb) { |
111 | 0 | const int r = VP8YUVToR(y, v); // 4 usable bits |
112 | 0 | const int g = VP8YUVToG(y, u, v); // 4 usable bits |
113 | 0 | const int b = VP8YUVToB(y, u); // 4 usable bits |
114 | 0 | const int rg = (r & 0xf0) | (g >> 4); |
115 | 0 | const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits |
116 | | #if (WEBP_SWAP_16BIT_CSP == 1) |
117 | | argb[0] = ba; |
118 | | argb[1] = rg; |
119 | | #else |
120 | 0 | argb[0] = rg; |
121 | 0 | argb[1] = ba; |
122 | 0 | #endif |
123 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToRgba4444 Unexecuted instantiation: upsampling.c:VP8YuvToRgba4444 Unexecuted instantiation: yuv.c:VP8YuvToRgba4444 Unexecuted instantiation: upsampling_sse2.c:VP8YuvToRgba4444 Unexecuted instantiation: yuv_sse2.c:VP8YuvToRgba4444 Unexecuted instantiation: upsampling_sse41.c:VP8YuvToRgba4444 Unexecuted instantiation: yuv_sse41.c:VP8YuvToRgba4444 Unexecuted instantiation: picture_csp_enc.c:VP8YuvToRgba4444 Unexecuted instantiation: picture_tools_enc.c:VP8YuvToRgba4444 Unexecuted instantiation: lossless_enc.c:VP8YuvToRgba4444 |
124 | | |
125 | | //----------------------------------------------------------------------------- |
126 | | // Alpha handling variants |
127 | | |
128 | | static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, |
129 | 0 | uint8_t* const argb) { |
130 | 0 | argb[0] = 0xff; |
131 | 0 | VP8YuvToRgb(y, u, v, argb + 1); |
132 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToArgb Unexecuted instantiation: upsampling.c:VP8YuvToArgb Unexecuted instantiation: yuv.c:VP8YuvToArgb Unexecuted instantiation: upsampling_sse2.c:VP8YuvToArgb Unexecuted instantiation: yuv_sse2.c:VP8YuvToArgb Unexecuted instantiation: upsampling_sse41.c:VP8YuvToArgb Unexecuted instantiation: yuv_sse41.c:VP8YuvToArgb Unexecuted instantiation: picture_csp_enc.c:VP8YuvToArgb Unexecuted instantiation: picture_tools_enc.c:VP8YuvToArgb Unexecuted instantiation: lossless_enc.c:VP8YuvToArgb |
133 | | |
134 | | static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, |
135 | 0 | uint8_t* const bgra) { |
136 | 0 | VP8YuvToBgr(y, u, v, bgra); |
137 | 0 | bgra[3] = 0xff; |
138 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToBgra Unexecuted instantiation: upsampling.c:VP8YuvToBgra Unexecuted instantiation: yuv.c:VP8YuvToBgra Unexecuted instantiation: upsampling_sse2.c:VP8YuvToBgra Unexecuted instantiation: yuv_sse2.c:VP8YuvToBgra Unexecuted instantiation: upsampling_sse41.c:VP8YuvToBgra Unexecuted instantiation: yuv_sse41.c:VP8YuvToBgra Unexecuted instantiation: picture_csp_enc.c:VP8YuvToBgra Unexecuted instantiation: picture_tools_enc.c:VP8YuvToBgra Unexecuted instantiation: lossless_enc.c:VP8YuvToBgra |
139 | | |
140 | | static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, |
141 | 0 | uint8_t* const rgba) { |
142 | 0 | VP8YuvToRgb(y, u, v, rgba); |
143 | 0 | rgba[3] = 0xff; |
144 | 0 | } Unexecuted instantiation: io_dec.c:VP8YuvToRgba Unexecuted instantiation: upsampling.c:VP8YuvToRgba Unexecuted instantiation: yuv.c:VP8YuvToRgba Unexecuted instantiation: upsampling_sse2.c:VP8YuvToRgba Unexecuted instantiation: yuv_sse2.c:VP8YuvToRgba Unexecuted instantiation: upsampling_sse41.c:VP8YuvToRgba Unexecuted instantiation: yuv_sse41.c:VP8YuvToRgba Unexecuted instantiation: picture_csp_enc.c:VP8YuvToRgba Unexecuted instantiation: picture_tools_enc.c:VP8YuvToRgba Unexecuted instantiation: lossless_enc.c:VP8YuvToRgba |
145 | | |
146 | | //----------------------------------------------------------------------------- |
147 | | // SSE2 extra functions (mostly for upsampling_sse2.c) |
148 | | |
149 | | #if defined(WEBP_USE_SSE2) |
150 | | |
151 | | // Process 32 pixels and store the result (16b, 24b or 32b per pixel) in *dst. |
152 | | void VP8YuvToRgba32_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
153 | | uint8_t* dst); |
154 | | void VP8YuvToRgb32_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
155 | | uint8_t* dst); |
156 | | void VP8YuvToBgra32_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
157 | | uint8_t* dst); |
158 | | void VP8YuvToBgr32_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
159 | | uint8_t* dst); |
160 | | void VP8YuvToArgb32_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
161 | | uint8_t* dst); |
162 | | void VP8YuvToRgba444432_SSE2(const uint8_t* y, const uint8_t* u, |
163 | | const uint8_t* v, uint8_t* dst); |
164 | | void VP8YuvToRgb56532_SSE2(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
165 | | uint8_t* dst); |
166 | | |
167 | | #endif // WEBP_USE_SSE2 |
168 | | |
169 | | //----------------------------------------------------------------------------- |
170 | | // SSE41 extra functions (mostly for upsampling_sse41.c) |
171 | | |
172 | | #if defined(WEBP_USE_SSE41) |
173 | | |
174 | | // Process 32 pixels and store the result (16b, 24b or 32b per pixel) in *dst. |
175 | | void VP8YuvToRgb32_SSE41(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
176 | | uint8_t* dst); |
177 | | void VP8YuvToBgr32_SSE41(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
178 | | uint8_t* dst); |
179 | | |
180 | | #endif // WEBP_USE_SSE41 |
181 | | |
182 | | //------------------------------------------------------------------------------ |
183 | | // RGB -> YUV conversion |
184 | | |
185 | | // Stub functions that can be called with various rounding values: |
186 | 0 | static WEBP_INLINE int VP8ClipUV(int uv, int rounding) { |
187 | 0 | uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2); |
188 | 0 | return ((uv & ~0xff) == 0) ? uv : (uv < 0) ? 0 : 255; |
189 | 0 | } Unexecuted instantiation: io_dec.c:VP8ClipUV Unexecuted instantiation: upsampling.c:VP8ClipUV Unexecuted instantiation: yuv.c:VP8ClipUV Unexecuted instantiation: upsampling_sse2.c:VP8ClipUV Unexecuted instantiation: yuv_sse2.c:VP8ClipUV Unexecuted instantiation: upsampling_sse41.c:VP8ClipUV Unexecuted instantiation: yuv_sse41.c:VP8ClipUV Unexecuted instantiation: picture_csp_enc.c:VP8ClipUV Unexecuted instantiation: picture_tools_enc.c:VP8ClipUV Unexecuted instantiation: lossless_enc.c:VP8ClipUV |
190 | | |
191 | 0 | static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) { |
192 | 0 | const int luma = 16839 * r + 33059 * g + 6420 * b; |
193 | 0 | return (luma + rounding + (16 << YUV_FIX)) >> YUV_FIX; // no need to clip |
194 | 0 | } Unexecuted instantiation: io_dec.c:VP8RGBToY Unexecuted instantiation: upsampling.c:VP8RGBToY Unexecuted instantiation: yuv.c:VP8RGBToY Unexecuted instantiation: upsampling_sse2.c:VP8RGBToY Unexecuted instantiation: yuv_sse2.c:VP8RGBToY Unexecuted instantiation: upsampling_sse41.c:VP8RGBToY Unexecuted instantiation: yuv_sse41.c:VP8RGBToY Unexecuted instantiation: picture_csp_enc.c:VP8RGBToY Unexecuted instantiation: picture_tools_enc.c:VP8RGBToY Unexecuted instantiation: lossless_enc.c:VP8RGBToY |
195 | | |
196 | 0 | static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) { |
197 | 0 | const int u = -9719 * r - 19081 * g + 28800 * b; |
198 | 0 | return VP8ClipUV(u, rounding); |
199 | 0 | } Unexecuted instantiation: io_dec.c:VP8RGBToU Unexecuted instantiation: upsampling.c:VP8RGBToU Unexecuted instantiation: yuv.c:VP8RGBToU Unexecuted instantiation: upsampling_sse2.c:VP8RGBToU Unexecuted instantiation: yuv_sse2.c:VP8RGBToU Unexecuted instantiation: upsampling_sse41.c:VP8RGBToU Unexecuted instantiation: yuv_sse41.c:VP8RGBToU Unexecuted instantiation: picture_csp_enc.c:VP8RGBToU Unexecuted instantiation: picture_tools_enc.c:VP8RGBToU Unexecuted instantiation: lossless_enc.c:VP8RGBToU |
200 | | |
201 | 0 | static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) { |
202 | 0 | const int v = +28800 * r - 24116 * g - 4684 * b; |
203 | 0 | return VP8ClipUV(v, rounding); |
204 | 0 | } Unexecuted instantiation: io_dec.c:VP8RGBToV Unexecuted instantiation: upsampling.c:VP8RGBToV Unexecuted instantiation: yuv.c:VP8RGBToV Unexecuted instantiation: upsampling_sse2.c:VP8RGBToV Unexecuted instantiation: yuv_sse2.c:VP8RGBToV Unexecuted instantiation: upsampling_sse41.c:VP8RGBToV Unexecuted instantiation: yuv_sse41.c:VP8RGBToV Unexecuted instantiation: picture_csp_enc.c:VP8RGBToV Unexecuted instantiation: picture_tools_enc.c:VP8RGBToV Unexecuted instantiation: lossless_enc.c:VP8RGBToV |
205 | | |
206 | | #ifdef __cplusplus |
207 | | } // extern "C" |
208 | | #endif |
209 | | |
210 | | #endif // WEBP_DSP_YUV_H_ |