TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / android / camera / Converters.h
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef HW_EMULATOR_CAMERA_CONVERTERS_H
18 #define HW_EMULATOR_CAMERA_CONVERTERS_H
19
20 #include <endian.h>
21
22 #ifndef __BYTE_ORDER
23 #error "could not determine byte order"
24 #endif
25
26 /*
27  * Contains declaration of framebuffer conversion routines.
28  *
29  * NOTE: RGB and big/little endian considerations. Wherewer in this code RGB
30  * pixels are represented as WORD, or DWORD, the color order inside the
31  * WORD / DWORD matches the one that would occur if that WORD / DWORD would have
32  * been read from the typecasted framebuffer:
33  *
34  *      const uint32_t rgb = *reinterpret_cast<const uint32_t*>(framebuffer);
35  *
36  * So, if this code runs on the little endian CPU, red color in 'rgb' would be
37  * masked as 0x000000ff, and blue color would be masked as 0x00ff0000, while if
38  * the code runs on a big endian CPU, the red color in 'rgb' would be masked as
39  * 0xff000000, and blue color would be masked as 0x0000ff00,
40  */
41
42 namespace android {
43
44 /*
45  * RGB565 color masks
46  */
47
48 #if __BYTE_ORDER == __LITTLE_ENDIAN
49 static const uint16_t kRed5     = 0x001f;
50 static const uint16_t kGreen6   = 0x07e0;
51 static const uint16_t kBlue5    = 0xf800;
52 #else   // __BYTE_ORDER
53 static const uint16_t kRed5     = 0xf800;
54 static const uint16_t kGreen6   = 0x07e0;
55 static const uint16_t kBlue5    = 0x001f;
56 #endif  // __BYTE_ORDER
57 static const uint32_t kBlack16  = 0x0000;
58 static const uint32_t kWhite16  = kRed5 | kGreen6 | kBlue5;
59
60 /*
61  * RGB32 color masks
62  */
63
64 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 static const uint32_t kRed8     = 0x000000ff;
66 static const uint32_t kGreen8   = 0x0000ff00;
67 static const uint32_t kBlue8    = 0x00ff0000;
68 #else   // __BYTE_ORDER
69 static const uint32_t kRed8     = 0x00ff0000;
70 static const uint32_t kGreen8   = 0x0000ff00;
71 static const uint32_t kBlue8    = 0x000000ff;
72 #endif  // __BYTE_ORDER
73 static const uint32_t kBlack32  = 0x00000000;
74 static const uint32_t kWhite32  = kRed8 | kGreen8 | kBlue8;
75
76 /*
77  * Extracting, and saving color bytes from / to WORD / DWORD RGB.
78  */
79
80 #if __BYTE_ORDER == __LITTLE_ENDIAN
81 /* Extract red, green, and blue bytes from RGB565 word. */
82 #define R16(rgb)    static_cast<uint8_t>(rgb & kRed5)
83 #define G16(rgb)    static_cast<uint8_t>((rgb & kGreen6) >> 5)
84 #define B16(rgb)    static_cast<uint8_t>((rgb & kBlue5) >> 11)
85 /* Make 8 bits red, green, and blue, extracted from RGB565 word. */
86 #define R16_32(rgb) static_cast<uint8_t>(((rgb & kRed5) << 3) | ((rgb & kRed5) >> 2))
87 #define G16_32(rgb) static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9))
88 #define B16_32(rgb) static_cast<uint8_t>(((rgb & kBlue5) >> 8) | ((rgb & kBlue5) >> 14))
89 /* Extract red, green, and blue bytes from RGB32 dword. */
90 #define R32(rgb)    static_cast<uint8_t>(rgb & kRed8)
91 #define G32(rgb)    static_cast<uint8_t>(((rgb & kGreen8) >> 8) & 0xff)
92 #define B32(rgb)    static_cast<uint8_t>(((rgb & kBlue8) >> 16) & 0xff)
93 /* Build RGB565 word from red, green, and blue bytes. */
94 #define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(b) << 6) | g) << 5) | r)
95 /* Build RGB32 dword from red, green, and blue bytes. */
96 #define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(b) << 8) | g) << 8) | r)
97 #else   // __BYTE_ORDER
98 /* Extract red, green, and blue bytes from RGB565 word. */
99 #define R16(rgb)    static_cast<uint8_t>((rgb & kRed5) >> 11)
100 #define G16(rgb)    static_cast<uint8_t>((rgb & kGreen6) >> 5)
101 #define B16(rgb)    static_cast<uint8_t>(rgb & kBlue5)
102 /* Make 8 bits red, green, and blue, extracted from RGB565 word. */
103 #define R16_32(rgb) static_cast<uint8_t>(((rgb & kRed5) >> 8) | ((rgb & kRed5) >> 14))
104 #define G16_32(rgb) static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9))
105 #define B16_32(rgb) static_cast<uint8_t>(((rgb & kBlue5) << 3) | ((rgb & kBlue5) >> 2))
106 /* Extract red, green, and blue bytes from RGB32 dword. */
107 #define R32(rgb)    static_cast<uint8_t>((rgb & kRed8) >> 16)
108 #define G32(rgb)    static_cast<uint8_t>((rgb & kGreen8) >> 8)
109 #define B32(rgb)    static_cast<uint8_t>(rgb & kBlue8)
110 /* Build RGB565 word from red, green, and blue bytes. */
111 #define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(r) << 6) | g) << 5) | b)
112 /* Build RGB32 dword from red, green, and blue bytes. */
113 #define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(r) << 8) | g) << 8) | b)
114 #endif  // __BYTE_ORDER
115
116 /* An union that simplifies breaking 32 bit RGB into separate R, G, and B colors.
117  */
118 typedef union RGB32_t {
119     uint32_t    color;
120     struct {
121 #if __BYTE_ORDER == __LITTLE_ENDIAN
122         uint8_t r; uint8_t g; uint8_t b; uint8_t a;
123 #else   // __BYTE_ORDER
124         uint8_t a; uint8_t b; uint8_t g; uint8_t r;
125 #endif  // __BYTE_ORDER
126     };
127 } RGB32_t;
128
129
130 /* Clips a value to the unsigned 0-255 range, treating negative values as zero.
131  */
132 static __inline__ int
133 clamp(int x)
134 {
135     if (x > 255) return 255;
136     if (x < 0)   return 0;
137     return x;
138 }
139
140 /********************************************************************************
141  * Basics of RGB -> YUV conversion
142  *******************************************************************************/
143
144 /*
145  * RGB -> YUV conversion macros
146  */
147 #define RGB2Y(r, g, b) (uint8_t)(((66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) +  16)
148 #define RGB2U(r, g, b) (uint8_t)(((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
149 #define RGB2V(r, g, b) (uint8_t)(((112 * (r) - 94 * (g) -  18 * (b) + 128) >> 8) + 128)
150
151 /* Converts R8 G8 B8 color to YUV. */
152 static __inline__ void
153 R8G8B8ToYUV(uint8_t r, uint8_t g, uint8_t b, uint8_t* y, uint8_t* u, uint8_t* v)
154 {
155     *y = RGB2Y((int)r, (int)g, (int)b);
156     *u = RGB2U((int)r, (int)g, (int)b);
157     *v = RGB2V((int)r, (int)g, (int)b);
158 }
159
160 /* Converts RGB565 color to YUV. */
161 static __inline__ void
162 RGB565ToYUV(uint16_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
163 {
164     R8G8B8ToYUV(R16_32(rgb), G16_32(rgb), B16_32(rgb), y, u, v);
165 }
166
167 /* Converts RGB32 color to YUV. */
168 static __inline__ void
169 RGB32ToYUV(uint32_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
170 {
171     RGB32_t rgb_c;
172     rgb_c.color = rgb;
173     R8G8B8ToYUV(rgb_c.r, rgb_c.g, rgb_c.b, y, u, v);
174 }
175
176 /********************************************************************************
177  * Basics of YUV -> RGB conversion.
178  * Note that due to the fact that guest uses RGB only on preview window, and the
179  * RGB format that is used is RGB565, we can limit YUV -> RGB conversions to
180  * RGB565 only.
181  *******************************************************************************/
182
183 /*
184  * YUV -> RGB conversion macros
185  */
186
187 /* "Optimized" macros that take specialy prepared Y, U, and V values:
188  *  C = Y - 16
189  *  D = U - 128
190  *  E = V - 128
191  */
192 #define YUV2RO(C, D, E) clamp((298 * (C) + 409 * (E) + 128) >> 8)
193 #define YUV2GO(C, D, E) clamp((298 * (C) - 100 * (D) - 208 * (E) + 128) >> 8)
194 #define YUV2BO(C, D, E) clamp((298 * (C) + 516 * (D) + 128) >> 8)
195
196 /*
197  *  Main macros that take the original Y, U, and V values
198  */
199 #define YUV2R(y, u, v) clamp((298 * ((y)-16) + 409 * ((v)-128) + 128) >> 8)
200 #define YUV2G(y, u, v) clamp((298 * ((y)-16) - 100 * ((u)-128) - 208 * ((v)-128) + 128) >> 8)
201 #define YUV2B(y, u, v) clamp((298 * ((y)-16) + 516 * ((u)-128) + 128) >> 8)
202
203
204 /* Converts YUV color to RGB565. */
205 static __inline__ uint16_t
206 YUVToRGB565(int y, int u, int v)
207 {
208     /* Calculate C, D, and E values for the optimized macro. */
209     y -= 16; u -= 128; v -= 128;
210     const uint16_t r = (YUV2RO(y,u,v) >> 3) & 0x1f;
211     const uint16_t g = (YUV2GO(y,u,v) >> 2) & 0x3f;
212     const uint16_t b = (YUV2BO(y,u,v) >> 3) & 0x1f;
213     return RGB565(r, g, b);
214 }
215
216 /* Converts YUV color to RGB32. */
217 static __inline__ uint32_t
218 YUVToRGB32(int y, int u, int v)
219 {
220     /* Calculate C, D, and E values for the optimized macro. */
221     y -= 16; u -= 128; v -= 128;
222     RGB32_t rgb;
223     rgb.r = YUV2RO(y,u,v) & 0xff;
224     rgb.g = YUV2GO(y,u,v) & 0xff;
225     rgb.b = YUV2BO(y,u,v) & 0xff;
226     return rgb.color;
227 }
228
229 /* YUV pixel descriptor. */
230 struct YUVPixel {
231     uint8_t     Y;
232     uint8_t     U;
233     uint8_t     V;
234
235     inline YUVPixel()
236         : Y(0), U(0), V(0)
237     {
238     }
239
240     inline explicit YUVPixel(uint16_t rgb565)
241     {
242         RGB565ToYUV(rgb565, &Y, &U, &V);
243     }
244
245     inline explicit YUVPixel(uint32_t rgb32)
246     {
247         RGB32ToYUV(rgb32, &Y, &U, &V);
248     }
249
250     inline void get(uint8_t* pY, uint8_t* pU, uint8_t* pV) const
251     {
252         *pY = Y; *pU = U; *pV = V;
253     }
254 };
255
256 /* Converts an YV12 framebuffer to RGB565 framebuffer.
257  * Param:
258  *  yv12 - YV12 framebuffer.
259  *  rgb - RGB565 framebuffer.
260  *  width, height - Dimensions for both framebuffers.
261  */
262 void YV12ToRGB565(const void* yv12, void* rgb, int width, int height);
263
264 /* Converts an YV12 framebuffer to RGB32 framebuffer.
265  * Param:
266  *  yv12 - YV12 framebuffer.
267  *  rgb - RGB32 framebuffer.
268  *  width, height - Dimensions for both framebuffers.
269  */
270 void YV12ToRGB32(const void* yv12, void* rgb, int width, int height);
271
272 /* Converts an YU12 framebuffer to RGB32 framebuffer.
273  * Param:
274  *  yu12 - YU12 framebuffer.
275  *  rgb - RGB32 framebuffer.
276  *  width, height - Dimensions for both framebuffers.
277  */
278 void YU12ToRGB32(const void* yu12, void* rgb, int width, int height);
279
280 /* Converts an NV12 framebuffer to RGB565 framebuffer.
281  * Param:
282  *  nv12 - NV12 framebuffer.
283  *  rgb - RGB565 framebuffer.
284  *  width, height - Dimensions for both framebuffers.
285  */
286 void NV12ToRGB565(const void* nv12, void* rgb, int width, int height);
287
288 /* Converts an NV12 framebuffer to RGB32 framebuffer.
289  * Param:
290  *  nv12 - NV12 framebuffer.
291  *  rgb - RGB32 framebuffer.
292  *  width, height - Dimensions for both framebuffers.
293  */
294 void NV12ToRGB32(const void* nv12, void* rgb, int width, int height);
295
296 /* Converts an NV21 framebuffer to RGB565 framebuffer.
297  * Param:
298  *  nv21 - NV21 framebuffer.
299  *  rgb - RGB565 framebuffer.
300  *  width, height - Dimensions for both framebuffers.
301  */
302 void NV21ToRGB565(const void* nv21, void* rgb, int width, int height);
303
304 /* Converts an NV21 framebuffer to RGB32 framebuffer.
305  * Param:
306  *  nv21 - NV21 framebuffer.
307  *  rgb - RGB32 framebuffer.
308  *  width, height - Dimensions for both framebuffers.
309  */
310 void NV21ToRGB32(const void* nv21, void* rgb, int width, int height);
311
312 }; /* namespace android */
313
314 #endif  /* HW_EMULATOR_CAMERA_CONVERTERS_H */