OLD | NEW |
1 // Copyright 2010 Google Inc. | 1 // Copyright 2010 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // This code is licensed under the same terms as WebM: | 3 // This code is licensed under the same terms as WebM: |
4 // Software License Agreement: http://www.webmproject.org/license/software/ | 4 // Software License Agreement: http://www.webmproject.org/license/software/ |
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ | 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
6 // ----------------------------------------------------------------------------- | 6 // ----------------------------------------------------------------------------- |
7 // | 7 // |
8 // inline YUV->RGB conversion function | 8 // inline YUV<->RGB conversion function |
9 // | 9 // |
10 // Author: Skal (pascal.massimino@gmail.com) | 10 // Author: Skal (pascal.massimino@gmail.com) |
11 | 11 |
12 #ifndef WEBP_DSP_YUV_H_ | 12 #ifndef WEBP_DSP_YUV_H_ |
13 #define WEBP_DSP_YUV_H_ | 13 #define WEBP_DSP_YUV_H_ |
14 | 14 |
15 #include "../webp/decode_vp8.h" | 15 #include "../dec/decode_vp8.h" |
| 16 |
| 17 //------------------------------------------------------------------------------ |
| 18 // YUV -> RGB conversion |
16 | 19 |
17 #if defined(__cplusplus) || defined(c_plusplus) | 20 #if defined(__cplusplus) || defined(c_plusplus) |
18 extern "C" { | 21 extern "C" { |
19 #endif | 22 #endif |
20 | 23 |
21 enum { YUV_FIX = 16, // fixed-point precision | 24 enum { YUV_FIX = 16, // fixed-point precision |
22 YUV_RANGE_MIN = -227, // min value of r/g/b output | 25 YUV_RANGE_MIN = -227, // min value of r/g/b output |
23 YUV_RANGE_MAX = 256 + 226 // max value of r/g/b output | 26 YUV_RANGE_MAX = 256 + 226 // max value of r/g/b output |
24 }; | 27 }; |
25 extern int16_t VP8kVToR[256], VP8kUToB[256]; | 28 extern int16_t VP8kVToR[256], VP8kUToB[256]; |
26 extern int32_t VP8kVToG[256], VP8kUToG[256]; | 29 extern int32_t VP8kVToG[256], VP8kUToG[256]; |
27 extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; | 30 extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; |
28 extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; | 31 extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; |
29 | 32 |
30 static inline void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v, | 33 static WEBP_INLINE void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v, |
31 uint8_t* const rgb) { | 34 uint8_t* const rgb) { |
32 const int r_off = VP8kVToR[v]; | 35 const int r_off = VP8kVToR[v]; |
33 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 36 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
34 const int b_off = VP8kUToB[u]; | 37 const int b_off = VP8kUToB[u]; |
35 rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN]; | 38 rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN]; |
36 rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; | 39 rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; |
37 rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN]; | 40 rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN]; |
38 } | 41 } |
39 | 42 |
40 static inline void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v, | 43 static WEBP_INLINE void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v, |
41 uint8_t* const rgb) { | 44 uint8_t* const rgb) { |
42 const int r_off = VP8kVToR[v]; | 45 const int r_off = VP8kVToR[v]; |
43 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 46 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
44 const int b_off = VP8kUToB[u]; | 47 const int b_off = VP8kUToB[u]; |
45 rgb[0] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) | | 48 rgb[0] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) | |
46 (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5)); | 49 (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5)); |
47 rgb[1] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) | | 50 rgb[1] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) | |
48 (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3)); | 51 (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3)); |
49 } | 52 } |
50 | 53 |
51 static inline void VP8YuvToArgbKeepA(uint8_t y, uint8_t u, uint8_t v, | 54 static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, |
52 uint8_t* const argb) { | 55 uint8_t* const argb) { |
53 // Don't update Aplha (argb[0]) | 56 argb[0] = 0xff; |
54 VP8YuvToRgb(y, u, v, argb + 1); | 57 VP8YuvToRgb(y, u, v, argb + 1); |
55 } | 58 } |
56 | 59 |
57 static inline void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v, | 60 static WEBP_INLINE void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v, |
58 uint8_t* const argb) { | |
59 argb[0] = 0xff; | |
60 VP8YuvToArgbKeepA(y, u, v, argb); | |
61 } | |
62 | |
63 static inline void VP8YuvToRgba4444KeepA(uint8_t y, uint8_t u, uint8_t v, | |
64 uint8_t* const argb) { | 61 uint8_t* const argb) { |
65 const int r_off = VP8kVToR[v]; | 62 const int r_off = VP8kVToR[v]; |
66 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 63 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
67 const int b_off = VP8kUToB[u]; | 64 const int b_off = VP8kUToB[u]; |
68 // Don't update Aplha (last 4 bits of argb[1]) | 65 // Don't update alpha (last 4 bits of argb[1]) |
69 argb[0] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) | | 66 argb[0] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) | |
70 VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]); | 67 VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]); |
71 argb[1] = (argb[1] & 0x0f) | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4); | 68 argb[1] = 0x0f | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4); |
72 } | 69 } |
73 | 70 |
74 static inline void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v, | 71 static WEBP_INLINE void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v, |
75 uint8_t* const argb) { | 72 uint8_t* const bgr) { |
76 argb[1] = 0x0f; | |
77 VP8YuvToRgba4444KeepA(y, u, v, argb); | |
78 } | |
79 | |
80 static inline void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v, | |
81 uint8_t* const bgr) { | |
82 const int r_off = VP8kVToR[v]; | 73 const int r_off = VP8kVToR[v]; |
83 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; | 74 const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX; |
84 const int b_off = VP8kUToB[u]; | 75 const int b_off = VP8kUToB[u]; |
85 bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN]; | 76 bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN]; |
86 bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; | 77 bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN]; |
87 bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN]; | 78 bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN]; |
88 } | 79 } |
89 | 80 |
90 static inline void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, | 81 static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v, |
91 uint8_t* const bgra) { | 82 uint8_t* const bgra) { |
92 VP8YuvToBgr(y, u, v, bgra); | 83 VP8YuvToBgr(y, u, v, bgra); |
93 bgra[3] = 0xff; | 84 bgra[3] = 0xff; |
94 } | 85 } |
95 | 86 |
96 static inline void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, | 87 static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v, |
97 uint8_t* const rgba) { | 88 uint8_t* const rgba) { |
98 VP8YuvToRgb(y, u, v, rgba); | 89 VP8YuvToRgb(y, u, v, rgba); |
99 rgba[3] = 0xff; | 90 rgba[3] = 0xff; |
100 } | 91 } |
101 | 92 |
| 93 static WEBP_INLINE uint32_t VP8Clip4Bits(uint8_t c) { |
| 94 const uint32_t v = (c + 8) >> 4; |
| 95 return (v > 15) ? 15 : v; |
| 96 } |
| 97 |
102 // Must be called before everything, to initialize the tables. | 98 // Must be called before everything, to initialize the tables. |
103 void VP8YUVInit(void); | 99 void VP8YUVInit(void); |
104 | 100 |
| 101 //------------------------------------------------------------------------------ |
| 102 // RGB -> YUV conversion |
| 103 // The exact naming is Y'CbCr, following the ITU-R BT.601 standard. |
| 104 // More information at: http://en.wikipedia.org/wiki/YCbCr |
| 105 // Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16 |
| 106 // U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128 |
| 107 // V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 128 |
| 108 // We use 16bit fixed point operations. |
| 109 |
| 110 static WEBP_INLINE int VP8ClipUV(int v) { |
| 111 v = (v + (257 << (YUV_FIX + 2 - 1))) >> (YUV_FIX + 2); |
| 112 return ((v & ~0xff) == 0) ? v : (v < 0) ? 0 : 255; |
| 113 } |
| 114 |
| 115 static WEBP_INLINE int VP8RGBToY(int r, int g, int b) { |
| 116 const int kRound = (1 << (YUV_FIX - 1)) + (16 << YUV_FIX); |
| 117 const int luma = 16839 * r + 33059 * g + 6420 * b; |
| 118 return (luma + kRound) >> YUV_FIX; // no need to clip |
| 119 } |
| 120 |
| 121 static WEBP_INLINE int VP8RGBToU(int r, int g, int b) { |
| 122 return VP8ClipUV(-9719 * r - 19081 * g + 28800 * b); |
| 123 } |
| 124 |
| 125 static WEBP_INLINE int VP8RGBToV(int r, int g, int b) { |
| 126 return VP8ClipUV(+28800 * r - 24116 * g - 4684 * b); |
| 127 } |
| 128 |
105 #if defined(__cplusplus) || defined(c_plusplus) | 129 #if defined(__cplusplus) || defined(c_plusplus) |
106 } // extern "C" | 130 } // extern "C" |
107 #endif | 131 #endif |
108 | 132 |
109 #endif /* WEBP_DSP_YUV_H_ */ | 133 #endif /* WEBP_DSP_YUV_H_ */ |
OLD | NEW |