OLD | NEW |
1 // Copyright 2010 Google Inc. All Rights Reserved. | 1 // Copyright 2010 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // inline YUV<->RGB conversion function | 10 // inline YUV<->RGB conversion function |
11 // | 11 // |
12 // The exact naming is Y'CbCr, following the ITU-R BT.601 standard. | 12 // The exact naming is Y'CbCr, following the ITU-R BT.601 standard. |
13 // More information at: http://en.wikipedia.org/wiki/YCbCr | 13 // More information at: http://en.wikipedia.org/wiki/YCbCr |
14 // Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16 | 14 // Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16 |
15 // U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128 | 15 // U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128 |
16 // V = 0.4394 * R - 0.3679 * G - 0.0715 * 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. | 17 // We use 16bit fixed point operations for RGB->YUV conversion (YUV_FIX). |
18 // | 18 // |
19 // For the Y'CbCr to RGB conversion, the BT.601 specification reads: | 19 // For the Y'CbCr to RGB conversion, the BT.601 specification reads: |
20 // R = 1.164 * (Y-16) + 1.596 * (V-128) | 20 // R = 1.164 * (Y-16) + 1.596 * (V-128) |
21 // G = 1.164 * (Y-16) - 0.813 * (V-128) - 0.391 * (U-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) | 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. | 23 // where Y is in the [16,235] range, and U/V in the [16,240] range. |
24 // In the table-lookup version (WEBP_YUV_USE_TABLE), the common factor | 24 // In the table-lookup version (WEBP_YUV_USE_TABLE), the common factor |
25 // "1.164 * (Y-16)" can be handled as an offset in the VP8kClip[] table. | 25 // "1.164 * (Y-16)" can be handled as an offset in the VP8kClip[] table. |
26 // So in this case the formulae should be read as: | 26 // So in this case the formulae should read: |
27 // R = 1.164 * [Y + 1.371 * (V-128) ] - 18.624 | 27 // R = 1.164 * [Y + 1.371 * (V-128) ] - 18.624 |
28 // G = 1.164 * [Y - 0.698 * (V-128) - 0.336 * (U-128)] - 18.624 | 28 // G = 1.164 * [Y - 0.698 * (V-128) - 0.336 * (U-128)] - 18.624 |
29 // B = 1.164 * [Y + 1.733 * (U-128)] - 18.624 | 29 // B = 1.164 * [Y + 1.733 * (U-128)] - 18.624 |
30 // once factorized. Here too, 16bit fixed precision is used. | 30 // once factorized. |
| 31 // For YUV->RGB conversion, only 14bit fixed precision is used (YUV_FIX2). |
| 32 // That's the maximum possible for a convenient ARM implementation. |
31 // | 33 // |
32 // Author: Skal (pascal.massimino@gmail.com) | 34 // Author: Skal (pascal.massimino@gmail.com) |
33 | 35 |
34 #ifndef WEBP_DSP_YUV_H_ | 36 #ifndef WEBP_DSP_YUV_H_ |
35 #define WEBP_DSP_YUV_H_ | 37 #define WEBP_DSP_YUV_H_ |
36 | 38 |
| 39 #include "./dsp.h" |
37 #include "../dec/decode_vp8.h" | 40 #include "../dec/decode_vp8.h" |
38 | 41 |
39 // Define the following to use the LUT-based code: | 42 // Define the following to use the LUT-based code: |
40 #define WEBP_YUV_USE_TABLE | 43 // #define WEBP_YUV_USE_TABLE |
41 | 44 |
42 #if defined(WEBP_EXPERIMENTAL_FEATURES) | 45 #if defined(WEBP_EXPERIMENTAL_FEATURES) |
43 // Do NOT activate this feature for real compression. This is only experimental! | 46 // Do NOT activate this feature for real compression. This is only experimental! |
44 // This flag is for comparison purpose against JPEG's "YUVj" natural colorspace. | 47 // This flag is for comparison purpose against JPEG's "YUVj" natural colorspace. |
45 // This colorspace is close to Rec.601's Y'CbCr model with the notable | 48 // This colorspace is close to Rec.601's Y'CbCr model with the notable |
46 // difference of allowing larger range for luma/chroma. | 49 // difference of allowing larger range for luma/chroma. |
47 // See http://en.wikipedia.org/wiki/YCbCr#JPEG_conversion paragraph, and its | 50 // See http://en.wikipedia.org/wiki/YCbCr#JPEG_conversion paragraph, and its |
48 // difference with http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion | 51 // difference with http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion |
49 // #define USE_YUVj | 52 // #define USE_YUVj |
50 #endif | 53 #endif |
51 | 54 |
52 //------------------------------------------------------------------------------ | 55 //------------------------------------------------------------------------------ |
53 // YUV -> RGB conversion | 56 // YUV -> RGB conversion |
54 | 57 |
55 #if defined(__cplusplus) || defined(c_plusplus) | 58 #ifdef __cplusplus |
56 extern "C" { | 59 extern "C" { |
57 #endif | 60 #endif |
58 | 61 |
59 enum { YUV_FIX = 16, // fixed-point precision | 62 enum { |
60 YUV_HALF = 1 << (YUV_FIX - 1), | 63 YUV_FIX = 16, // fixed-point precision for RGB->YUV |
61 YUV_MASK = (256 << YUV_FIX) - 1, | 64 YUV_HALF = 1 << (YUV_FIX - 1), |
62 YUV_RANGE_MIN = -227, // min value of r/g/b output | 65 YUV_MASK = (256 << YUV_FIX) - 1, |
63 YUV_RANGE_MAX = 256 + 226 // max value of r/g/b output | 66 YUV_RANGE_MIN = -227, // min value of r/g/b output |
| 67 YUV_RANGE_MAX = 256 + 226, // max value of r/g/b output |
| 68 |
| 69 YUV_FIX2 = 14, // fixed-point precision for YUV->RGB |
| 70 YUV_HALF2 = 1 << (YUV_FIX2 - 1), |
| 71 YUV_MASK2 = (256 << YUV_FIX2) - 1 |
64 }; | 72 }; |
65 | 73 |
66 #ifdef WEBP_YUV_USE_TABLE | 74 // These constants are 14b fixed-point version of ITU-R BT.601 constants. |
| 75 #define kYScale 19077 // 1.164 = 255 / 219 |
| 76 #define kVToR 26149 // 1.596 = 255 / 112 * 0.701 |
| 77 #define kUToG 6419 // 0.391 = 255 / 112 * 0.886 * 0.114 / 0.587 |
| 78 #define kVToG 13320 // 0.813 = 255 / 112 * 0.701 * 0.299 / 0.587 |
| 79 #define kUToB 33050 // 2.018 = 255 / 112 * 0.886 |
| 80 #define kRCst (-kYScale * 16 - kVToR * 128 + YUV_HALF2) |
| 81 #define kGCst (-kYScale * 16 + kUToG * 128 + kVToG * 128 + YUV_HALF2) |
| 82 #define kBCst (-kYScale * 16 - kUToB * 128 + YUV_HALF2) |
67 | 83 |
68 extern int16_t VP8kVToR[256], VP8kUToB[256]; | 84 //------------------------------------------------------------------------------ |
69 extern int32_t VP8kVToG[256], VP8kUToG[256]; | |
70 extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; | |
71 extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; | |
72 | 85 |
73 static WEBP_INLINE void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v, | 86 #if !defined(WEBP_YUV_USE_TABLE) |
74 uint8_t* const rgb) { | 87 |
75 const int r_off = VP8kVToR[v]; | 88 // slower on x86 by ~7-8%, but bit-exact with the SSE2 version |
76 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 89 |
77 const int b_off = VP8kUToB[u]; | 90 static WEBP_INLINE int VP8Clip8(int v) { |
78 rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN]; | 91 return ((v & ~YUV_MASK2) == 0) ? (v >> YUV_FIX2) : (v < 0) ? 0 : 255; |
79 rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; | |
80 rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN]; | |
81 } | 92 } |
82 | 93 |
83 static WEBP_INLINE void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v, | 94 static WEBP_INLINE int VP8YUVToR(int y, int v) { |
84 uint8_t* const bgr) { | 95 return VP8Clip8(kYScale * y + kVToR * v + kRCst); |
85 const int r_off = VP8kVToR[v]; | |
86 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | |
87 const int b_off = VP8kUToB[u]; | |
88 bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN]; | |
89 bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; | |
90 bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN]; | |
91 } | 96 } |
92 | 97 |
93 static WEBP_INLINE void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v, | 98 static WEBP_INLINE int VP8YUVToG(int y, int u, int v) { |
| 99 return VP8Clip8(kYScale * y - kUToG * u - kVToG * v + kGCst); |
| 100 } |
| 101 |
| 102 static WEBP_INLINE int VP8YUVToB(int y, int u) { |
| 103 return VP8Clip8(kYScale * y + kUToB * u + kBCst); |
| 104 } |
| 105 |
| 106 static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v, |
| 107 uint8_t* const rgb) { |
| 108 rgb[0] = VP8YUVToR(y, v); |
| 109 rgb[1] = VP8YUVToG(y, u, v); |
| 110 rgb[2] = VP8YUVToB(y, u); |
| 111 } |
| 112 |
| 113 static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v, |
| 114 uint8_t* const bgr) { |
| 115 bgr[0] = VP8YUVToB(y, u); |
| 116 bgr[1] = VP8YUVToG(y, u, v); |
| 117 bgr[2] = VP8YUVToR(y, v); |
| 118 } |
| 119 |
| 120 static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v, |
94 uint8_t* const rgb) { | 121 uint8_t* const rgb) { |
95 const int r_off = VP8kVToR[v]; | 122 const int r = VP8YUVToR(y, v); // 5 usable bits |
96 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 123 const int g = VP8YUVToG(y, u, v); // 6 usable bits |
97 const int b_off = VP8kUToB[u]; | 124 const int b = VP8YUVToB(y, u); // 5 usable bits |
98 const uint8_t rg = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) | | 125 const int rg = (r & 0xf8) | (g >> 5); |
99 (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5)); | 126 const int gb = ((g << 3) & 0xe0) | (b >> 3); |
100 const uint8_t gb = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) | | |
101 (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3)); | |
102 #ifdef WEBP_SWAP_16BIT_CSP | 127 #ifdef WEBP_SWAP_16BIT_CSP |
103 rgb[0] = gb; | 128 rgb[0] = gb; |
104 rgb[1] = rg; | 129 rgb[1] = rg; |
105 #else | 130 #else |
106 rgb[0] = rg; | 131 rgb[0] = rg; |
107 rgb[1] = gb; | 132 rgb[1] = gb; |
108 #endif | 133 #endif |
109 } | 134 } |
110 | 135 |
111 static WEBP_INLINE void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v, | 136 static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v, |
112 uint8_t* const argb) { | 137 uint8_t* const argb) { |
113 const int r_off = VP8kVToR[v]; | 138 const int r = VP8YUVToR(y, v); // 4 usable bits |
114 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 139 const int g = VP8YUVToG(y, u, v); // 4 usable bits |
115 const int b_off = VP8kUToB[u]; | 140 const int b = VP8YUVToB(y, u); // 4 usable bits |
116 const uint8_t rg = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) | | 141 const int rg = (r & 0xf0) | (g >> 4); |
117 VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]); | 142 const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits |
118 const uint8_t ba = (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4) | 0x0f; | |
119 #ifdef WEBP_SWAP_16BIT_CSP | 143 #ifdef WEBP_SWAP_16BIT_CSP |
120 argb[0] = ba; | 144 argb[0] = ba; |
121 argb[1] = rg; | 145 argb[1] = rg; |
122 #else | 146 #else |
123 argb[0] = rg; | 147 argb[0] = rg; |
124 argb[1] = ba; | 148 argb[1] = ba; |
125 #endif | 149 #endif |
126 } | 150 } |
127 | 151 |
128 #else // Table-free version (slower on x86) | 152 #else |
129 | 153 |
130 // These constants are 16b fixed-point version of ITU-R BT.601 constants | 154 // Table-based version, not totally equivalent to the SSE2 version. |
131 #define kYScale 76309 // 1.164 = 255 / 219 | 155 // Rounding diff is only +/-1 though. |
132 #define kVToR 104597 // 1.596 = 255 / 112 * 0.701 | |
133 #define kUToG 25674 // 0.391 = 255 / 112 * 0.886 * 0.114 / 0.587 | |
134 #define kVToG 53278 // 0.813 = 255 / 112 * 0.701 * 0.299 / 0.587 | |
135 #define kUToB 132201 // 2.018 = 255 / 112 * 0.886 | |
136 #define kRCst (-kYScale * 16 - kVToR * 128 + YUV_HALF) | |
137 #define kGCst (-kYScale * 16 + kUToG * 128 + kVToG * 128 + YUV_HALF) | |
138 #define kBCst (-kYScale * 16 - kUToB * 128 + YUV_HALF) | |
139 | 156 |
140 static WEBP_INLINE uint8_t VP8Clip8(int v) { | 157 extern int16_t VP8kVToR[256], VP8kUToB[256]; |
141 return ((v & ~YUV_MASK) == 0) ? (uint8_t)(v >> YUV_FIX) | 158 extern int32_t VP8kVToG[256], VP8kUToG[256]; |
142 : (v < 0) ? 0u : 255u; | 159 extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; |
| 160 extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; |
| 161 |
| 162 static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v, |
| 163 uint8_t* const rgb) { |
| 164 const int r_off = VP8kVToR[v]; |
| 165 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
| 166 const int b_off = VP8kUToB[u]; |
| 167 rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN]; |
| 168 rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; |
| 169 rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN]; |
143 } | 170 } |
144 | 171 |
145 static WEBP_INLINE uint8_t VP8ClipN(int v, int N) { // clip to N bits | 172 static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v, |
146 return ((v & ~YUV_MASK) == 0) ? (uint8_t)(v >> (YUV_FIX + (8 - N))) | 173 uint8_t* const bgr) { |
147 : (v < 0) ? 0u : (255u >> (8 - N)); | 174 const int r_off = VP8kVToR[v]; |
| 175 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
| 176 const int b_off = VP8kUToB[u]; |
| 177 bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN]; |
| 178 bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; |
| 179 bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN]; |
148 } | 180 } |
149 | 181 |
150 static WEBP_INLINE int VP8YUVToR(int y, int v) { | 182 static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v, |
151 return kYScale * y + kVToR * v + kRCst; | |
152 } | |
153 | |
154 static WEBP_INLINE int VP8YUVToG(int y, int u, int v) { | |
155 return kYScale * y - kUToG * u - kVToG * v + kGCst; | |
156 } | |
157 | |
158 static WEBP_INLINE int VP8YUVToB(int y, int u) { | |
159 return kYScale * y + kUToB * u + kBCst; | |
160 } | |
161 | |
162 static WEBP_INLINE void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v, | |
163 uint8_t* const rgb) { | |
164 rgb[0] = VP8Clip8(VP8YUVToR(y, v)); | |
165 rgb[1] = VP8Clip8(VP8YUVToG(y, u, v)); | |
166 rgb[2] = VP8Clip8(VP8YUVToB(y, u)); | |
167 } | |
168 | |
169 static WEBP_INLINE void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v, | |
170 uint8_t* const bgr) { | |
171 bgr[0] = VP8Clip8(VP8YUVToB(y, u)); | |
172 bgr[1] = VP8Clip8(VP8YUVToG(y, u, v)); | |
173 bgr[2] = VP8Clip8(VP8YUVToR(y, v)); | |
174 } | |
175 | |
176 static WEBP_INLINE void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v, | |
177 uint8_t* const rgb) { | 183 uint8_t* const rgb) { |
178 const int r = VP8Clip8(VP8YUVToR(y, u)); | 184 const int r_off = VP8kVToR[v]; |
179 const int g = VP8ClipN(VP8YUVToG(y, u, v), 6); | 185 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
180 const int b = VP8ClipN(VP8YUVToB(y, v), 5); | 186 const int b_off = VP8kUToB[u]; |
181 const uint8_t rg = (r & 0xf8) | (g >> 3); | 187 const int rg = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) | |
182 const uint8_t gb = (g << 5) | b; | 188 (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5)); |
| 189 const int gb = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) | |
| 190 (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3)); |
183 #ifdef WEBP_SWAP_16BIT_CSP | 191 #ifdef WEBP_SWAP_16BIT_CSP |
184 rgb[0] = gb; | 192 rgb[0] = gb; |
185 rgb[1] = rg; | 193 rgb[1] = rg; |
186 #else | 194 #else |
187 rgb[0] = rg; | 195 rgb[0] = rg; |
188 rgb[1] = gb; | 196 rgb[1] = gb; |
189 #endif | 197 #endif |
190 } | 198 } |
191 | 199 |
192 static WEBP_INLINE void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v, | 200 static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v, |
193 uint8_t* const argb) { | 201 uint8_t* const argb) { |
194 const int r = VP8Clip8(VP8YUVToR(y, u)); | 202 const int r_off = VP8kVToR[v]; |
195 const int g = VP8ClipN(VP8YUVToG(y, u, v), 4); | 203 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
196 const int b = VP8Clip8(VP8YUVToB(y, v)); | 204 const int b_off = VP8kUToB[u]; |
197 const uint8_t rg = (r & 0xf0) | g; | 205 const int rg = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) | |
198 const uint8_t ba = b | 0x0f; // overwrite the lower 4 bits | 206 VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]); |
| 207 const int ba = (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4) | 0x0f; |
199 #ifdef WEBP_SWAP_16BIT_CSP | 208 #ifdef WEBP_SWAP_16BIT_CSP |
200 argb[0] = ba; | 209 argb[0] = ba; |
201 argb[1] = rg; | 210 argb[1] = rg; |
202 #else | 211 #else |
203 argb[0] = rg; | 212 argb[0] = rg; |
204 argb[1] = ba; | 213 argb[1] = ba; |
205 #endif | 214 #endif |
206 } | 215 } |
207 | 216 |
208 #endif // WEBP_YUV_USE_TABLE | 217 #endif // WEBP_YUV_USE_TABLE |
209 | 218 |
| 219 //----------------------------------------------------------------------------- |
| 220 // Alpha handling variants |
| 221 |
210 static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, | 222 static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, |
211 uint8_t* const argb) { | 223 uint8_t* const argb) { |
212 argb[0] = 0xff; | 224 argb[0] = 0xff; |
213 VP8YuvToRgb(y, u, v, argb + 1); | 225 VP8YuvToRgb(y, u, v, argb + 1); |
214 } | 226 } |
215 | 227 |
216 static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, | 228 static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, |
217 uint8_t* const bgra) { | 229 uint8_t* const bgra) { |
218 VP8YuvToBgr(y, u, v, bgra); | 230 VP8YuvToBgr(y, u, v, bgra); |
219 bgra[3] = 0xff; | 231 bgra[3] = 0xff; |
220 } | 232 } |
221 | 233 |
222 static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, | 234 static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, |
223 uint8_t* const rgba) { | 235 uint8_t* const rgba) { |
224 VP8YuvToRgb(y, u, v, rgba); | 236 VP8YuvToRgb(y, u, v, rgba); |
225 rgba[3] = 0xff; | 237 rgba[3] = 0xff; |
226 } | 238 } |
227 | 239 |
228 // Must be called before everything, to initialize the tables. | 240 // Must be called before everything, to initialize the tables. |
229 void VP8YUVInit(void); | 241 void VP8YUVInit(void); |
230 | 242 |
| 243 //----------------------------------------------------------------------------- |
| 244 // SSE2 extra functions (mostly for upsampling_sse2.c) |
| 245 |
| 246 #if defined(WEBP_USE_SSE2) |
| 247 |
| 248 #if defined(FANCY_UPSAMPLING) |
| 249 // Process 32 pixels and store the result (24b or 32b per pixel) in *dst. |
| 250 void VP8YuvToRgba32(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
| 251 uint8_t* dst); |
| 252 void VP8YuvToRgb32(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
| 253 uint8_t* dst); |
| 254 void VP8YuvToBgra32(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
| 255 uint8_t* dst); |
| 256 void VP8YuvToBgr32(const uint8_t* y, const uint8_t* u, const uint8_t* v, |
| 257 uint8_t* dst); |
| 258 #endif // FANCY_UPSAMPLING |
| 259 |
| 260 // Must be called to initialize tables before using the functions. |
| 261 void VP8YUVInitSSE2(void); |
| 262 |
| 263 #endif // WEBP_USE_SSE2 |
| 264 |
231 //------------------------------------------------------------------------------ | 265 //------------------------------------------------------------------------------ |
232 // RGB -> YUV conversion | 266 // RGB -> YUV conversion |
233 | 267 |
234 static WEBP_INLINE int VP8ClipUV(int v) { | 268 // Stub functions that can be called with various rounding values: |
235 v = (v + (257 << (YUV_FIX + 2 - 1))) >> (YUV_FIX + 2); | 269 static WEBP_INLINE int VP8ClipUV(int uv, int rounding) { |
236 return ((v & ~0xff) == 0) ? v : (v < 0) ? 0 : 255; | 270 uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2); |
| 271 return ((uv & ~0xff) == 0) ? uv : (uv < 0) ? 0 : 255; |
237 } | 272 } |
238 | 273 |
239 #ifndef USE_YUVj | 274 #ifndef USE_YUVj |
240 | 275 |
241 static WEBP_INLINE int VP8RGBToY(int r, int g, int b) { | 276 static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) { |
242 const int kRound = (1 << (YUV_FIX - 1)) + (16 << YUV_FIX); | |
243 const int luma = 16839 * r + 33059 * g + 6420 * b; | 277 const int luma = 16839 * r + 33059 * g + 6420 * b; |
244 return (luma + kRound) >> YUV_FIX; // no need to clip | 278 return (luma + rounding + (16 << YUV_FIX)) >> YUV_FIX; // no need to clip |
245 } | 279 } |
246 | 280 |
247 static WEBP_INLINE int VP8RGBToU(int r, int g, int b) { | 281 static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) { |
248 const int u = -9719 * r - 19081 * g + 28800 * b; | 282 const int u = -9719 * r - 19081 * g + 28800 * b; |
249 return VP8ClipUV(u); | 283 return VP8ClipUV(u, rounding); |
250 } | 284 } |
251 | 285 |
252 static WEBP_INLINE int VP8RGBToV(int r, int g, int b) { | 286 static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) { |
253 const int v = +28800 * r - 24116 * g - 4684 * b; | 287 const int v = +28800 * r - 24116 * g - 4684 * b; |
254 return VP8ClipUV(v); | 288 return VP8ClipUV(v, rounding); |
255 } | 289 } |
256 | 290 |
257 #else | 291 #else |
258 | 292 |
259 // This JPEG-YUV colorspace, only for comparison! | 293 // This JPEG-YUV colorspace, only for comparison! |
260 // These are also 16-bit precision coefficients from Rec.601, but with full | 294 // These are also 16bit precision coefficients from Rec.601, but with full |
261 // [0..255] output range. | 295 // [0..255] output range. |
262 static WEBP_INLINE int VP8RGBToY(int r, int g, int b) { | 296 static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) { |
263 const int kRound = (1 << (YUV_FIX - 1)); | |
264 const int luma = 19595 * r + 38470 * g + 7471 * b; | 297 const int luma = 19595 * r + 38470 * g + 7471 * b; |
265 return (luma + kRound) >> YUV_FIX; // no need to clip | 298 return (luma + rounding) >> YUV_FIX; // no need to clip |
266 } | 299 } |
267 | 300 |
268 static WEBP_INLINE int VP8RGBToU(int r, int g, int b) { | 301 static WEBP_INLINE int VP8_RGB_TO_U(int r, int g, int b, int rounding) { |
269 const int u = -11058 * r - 21710 * g + 32768 * b; | 302 const int u = -11058 * r - 21710 * g + 32768 * b; |
270 return VP8ClipUV(u); | 303 return VP8ClipUV(u, rounding); |
271 } | 304 } |
272 | 305 |
273 static WEBP_INLINE int VP8RGBToV(int r, int g, int b) { | 306 static WEBP_INLINE int VP8_RGB_TO_V(int r, int g, int b, int rounding) { |
274 const int v = 32768 * r - 27439 * g - 5329 * b; | 307 const int v = 32768 * r - 27439 * g - 5329 * b; |
275 return VP8ClipUV(v); | 308 return VP8ClipUV(v, rounding); |
276 } | 309 } |
277 | 310 |
278 #endif // USE_YUVj | 311 #endif // USE_YUVj |
279 | 312 |
280 #if defined(__cplusplus) || defined(c_plusplus) | 313 #ifdef __cplusplus |
281 } // extern "C" | 314 } // extern "C" |
282 #endif | 315 #endif |
283 | 316 |
284 #endif /* WEBP_DSP_YUV_H_ */ | 317 #endif /* WEBP_DSP_YUV_H_ */ |
OLD | NEW |