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) \ 87 static_cast<uint8_t>(((rgb & kRed5) << 3) | ((rgb & kRed5) >> 2)) 88 #define G16_32(rgb) \ 89 static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9)) 90 #define B16_32(rgb) \ 91 static_cast<uint8_t>(((rgb & kBlue5) >> 8) | ((rgb & kBlue5) >> 14)) 92 /* Extract red, green, and blue bytes from RGB32 dword. */ 93 #define R32(rgb) static_cast<uint8_t>(rgb & kRed8) 94 #define G32(rgb) static_cast<uint8_t>(((rgb & kGreen8) >> 8) & 0xff) 95 #define B32(rgb) static_cast<uint8_t>(((rgb & kBlue8) >> 16) & 0xff) 96 /* Build RGB565 word from red, green, and blue bytes. */ 97 #define RGB565(r, g, b) \ 98 static_cast<uint16_t>((((static_cast<uint16_t>(b) << 6) | g) << 5) | r) 99 /* Build RGB32 dword from red, green, and blue bytes. */ 100 #define RGB32(r, g, b) \ 101 static_cast<uint32_t>((((static_cast<uint32_t>(b) << 8) | g) << 8) | r) 102 #else // __BYTE_ORDER 103 /* Extract red, green, and blue bytes from RGB565 word. */ 104 #define R16(rgb) static_cast<uint8_t>((rgb & kRed5) >> 11) 105 #define G16(rgb) static_cast<uint8_t>((rgb & kGreen6) >> 5) 106 #define B16(rgb) static_cast<uint8_t>(rgb & kBlue5) 107 /* Make 8 bits red, green, and blue, extracted from RGB565 word. */ 108 #define R16_32(rgb) \ 109 static_cast<uint8_t>(((rgb & kRed5) >> 8) | ((rgb & kRed5) >> 14)) 110 #define G16_32(rgb) \ 111 static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9)) 112 #define B16_32(rgb) \ 113 static_cast<uint8_t>(((rgb & kBlue5) << 3) | ((rgb & kBlue5) >> 2)) 114 /* Extract red, green, and blue bytes from RGB32 dword. */ 115 #define R32(rgb) static_cast<uint8_t>((rgb & kRed8) >> 16) 116 #define G32(rgb) static_cast<uint8_t>((rgb & kGreen8) >> 8) 117 #define B32(rgb) static_cast<uint8_t>(rgb & kBlue8) 118 /* Build RGB565 word from red, green, and blue bytes. */ 119 #define RGB565(r, g, b) \ 120 static_cast<uint16_t>((((static_cast<uint16_t>(r) << 6) | g) << 5) | b) 121 /* Build RGB32 dword from red, green, and blue bytes. */ 122 #define RGB32(r, g, b) \ 123 static_cast<uint32_t>((((static_cast<uint32_t>(r) << 8) | g) << 8) | b) 124 #endif // __BYTE_ORDER 125 126 /* An union that simplifies breaking 32 bit RGB into separate R, G, and B 127 * colors. 128 */ 129 typedef union RGB32_t { 130 uint32_t color; 131 struct { 132 #if __BYTE_ORDER == __LITTLE_ENDIAN 133 uint8_t r; 134 uint8_t g; 135 uint8_t b; 136 uint8_t a; 137 #else // __BYTE_ORDER 138 uint8_t a; 139 uint8_t b; 140 uint8_t g; 141 uint8_t r; 142 #endif // __BYTE_ORDER 143 }; 144 } RGB32_t; 145 146 /* Clips a value to the unsigned 0-255 range, treating negative values as zero. 147 */ 148 static __inline__ int clamp(int x) { 149 if (x > 255) return 255; 150 if (x < 0) return 0; 151 return x; 152 } 153 154 /******************************************************************************** 155 * Basics of RGB -> YUV conversion 156 *******************************************************************************/ 157 158 /* 159 * RGB -> YUV conversion macros 160 */ 161 #define RGB2Y(r, g, b) \ 162 (uint8_t)(((66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16) 163 #define RGB2U(r, g, b) \ 164 (uint8_t)(((-38 * (r)-74 * (g) + 112 * (b) + 128) >> 8) + 128) 165 #define RGB2V(r, g, b) \ 166 (uint8_t)(((112 * (r)-94 * (g)-18 * (b) + 128) >> 8) + 128) 167 168 /* Converts R8 G8 B8 color to YUV. */ 169 static __inline__ void R8G8B8ToYUV(uint8_t r, uint8_t g, uint8_t b, uint8_t* y, 170 uint8_t* u, uint8_t* v) { 171 *y = RGB2Y((int)r, (int)g, (int)b); 172 *u = RGB2U((int)r, (int)g, (int)b); 173 *v = RGB2V((int)r, (int)g, (int)b); 174 } 175 176 /* Converts RGB565 color to YUV. */ 177 static __inline__ void RGB565ToYUV(uint16_t rgb, uint8_t* y, uint8_t* u, 178 uint8_t* v) { 179 R8G8B8ToYUV(R16_32(rgb), G16_32(rgb), B16_32(rgb), y, u, v); 180 } 181 182 /* Converts RGB32 color to YUV. */ 183 static __inline__ void RGB32ToYUV(uint32_t rgb, uint8_t* y, uint8_t* u, 184 uint8_t* v) { 185 RGB32_t rgb_c; 186 rgb_c.color = rgb; 187 R8G8B8ToYUV(rgb_c.r, rgb_c.g, rgb_c.b, y, u, v); 188 } 189 190 /******************************************************************************** 191 * Basics of YUV -> RGB conversion. 192 * Note that due to the fact that guest uses RGB only on preview window, and the 193 * RGB format that is used is RGB565, we can limit YUV -> RGB conversions to 194 * RGB565 only. 195 *******************************************************************************/ 196 197 /* 198 * YUV -> RGB conversion macros 199 */ 200 201 /* "Optimized" macros that take specialy prepared Y, U, and V values: 202 * C = Y - 16 203 * D = U - 128 204 * E = V - 128 205 */ 206 #define YUV2RO(C, D, E) clamp((298 * (C) + 409 * (E) + 128) >> 8) 207 #define YUV2GO(C, D, E) clamp((298 * (C)-100 * (D)-208 * (E) + 128) >> 8) 208 #define YUV2BO(C, D, E) clamp((298 * (C) + 516 * (D) + 128) >> 8) 209 210 /* 211 * Main macros that take the original Y, U, and V values 212 */ 213 #define YUV2R(y, u, v) clamp((298 * ((y)-16) + 409 * ((v)-128) + 128) >> 8) 214 #define YUV2G(y, u, v) \ 215 clamp((298 * ((y)-16) - 100 * ((u)-128) - 208 * ((v)-128) + 128) >> 8) 216 #define YUV2B(y, u, v) clamp((298 * ((y)-16) + 516 * ((u)-128) + 128) >> 8) 217 218 /* Converts YUV color to RGB565. */ 219 static __inline__ uint16_t YUVToRGB565(int y, int u, int v) { 220 /* Calculate C, D, and E values for the optimized macro. */ 221 y -= 16; 222 u -= 128; 223 v -= 128; 224 const uint16_t r = (YUV2RO(y, u, v) >> 3) & 0x1f; 225 const uint16_t g = (YUV2GO(y, u, v) >> 2) & 0x3f; 226 const uint16_t b = (YUV2BO(y, u, v) >> 3) & 0x1f; 227 return RGB565(r, g, b); 228 } 229 230 /* Converts YUV color to RGB32. */ 231 static __inline__ uint32_t YUVToRGB32(int y, int u, int v) { 232 /* Calculate C, D, and E values for the optimized macro. */ 233 y -= 16; 234 u -= 128; 235 v -= 128; 236 RGB32_t rgb; 237 rgb.r = YUV2RO(y, u, v) & 0xff; 238 rgb.g = YUV2GO(y, u, v) & 0xff; 239 rgb.b = YUV2BO(y, u, v) & 0xff; 240 return rgb.color; 241 } 242 243 /* YUV pixel descriptor. */ 244 struct YUVPixel { 245 uint8_t Y; 246 uint8_t U; 247 uint8_t V; 248 249 inline YUVPixel() : Y(0), U(0), V(0) {} 250 251 inline explicit YUVPixel(uint16_t rgb565) { RGB565ToYUV(rgb565, &Y, &U, &V); } 252 253 inline explicit YUVPixel(uint32_t rgb32) { RGB32ToYUV(rgb32, &Y, &U, &V); } 254 255 inline void get(uint8_t* pY, uint8_t* pU, uint8_t* pV) const { 256 *pY = Y; 257 *pU = U; 258 *pV = V; 259 } 260 }; 261 262 /* Converts an YV12 framebuffer to RGB565 framebuffer. 263 * Param: 264 * yv12 - YV12 framebuffer. 265 * rgb - RGB565 framebuffer. 266 * width, height - Dimensions for both framebuffers. 267 */ 268 void YV12ToRGB565(const void* yv12, void* rgb, int width, int height); 269 270 /* Converts an YV12 framebuffer to RGB32 framebuffer. 271 * Param: 272 * yv12 - YV12 framebuffer. 273 * rgb - RGB32 framebuffer. 274 * width, height - Dimensions for both framebuffers. 275 */ 276 void YV12ToRGB32(const void* yv12, void* rgb, int width, int height); 277 278 /* Converts an YU12 framebuffer to RGB32 framebuffer. 279 * Param: 280 * yu12 - YU12 framebuffer. 281 * rgb - RGB32 framebuffer. 282 * width, height - Dimensions for both framebuffers. 283 */ 284 void YU12ToRGB32(const void* yu12, void* rgb, int width, int height); 285 286 /* Converts an NV12 framebuffer to RGB565 framebuffer. 287 * Param: 288 * nv12 - NV12 framebuffer. 289 * rgb - RGB565 framebuffer. 290 * width, height - Dimensions for both framebuffers. 291 */ 292 void NV12ToRGB565(const void* nv12, void* rgb, int width, int height); 293 294 /* Converts an NV12 framebuffer to RGB32 framebuffer. 295 * Param: 296 * nv12 - NV12 framebuffer. 297 * rgb - RGB32 framebuffer. 298 * width, height - Dimensions for both framebuffers. 299 */ 300 void NV12ToRGB32(const void* nv12, void* rgb, int width, int height); 301 302 /* Converts an NV21 framebuffer to RGB565 framebuffer. 303 * Param: 304 * nv21 - NV21 framebuffer. 305 * rgb - RGB565 framebuffer. 306 * width, height - Dimensions for both framebuffers. 307 */ 308 void NV21ToRGB565(const void* nv21, void* rgb, int width, int height); 309 310 /* Converts an NV21 framebuffer to RGB32 framebuffer. 311 * Param: 312 * nv21 - NV21 framebuffer. 313 * rgb - RGB32 framebuffer. 314 * width, height - Dimensions for both framebuffers. 315 */ 316 void NV21ToRGB32(const void* nv21, void* rgb, int width, int height); 317 318 }; /* namespace android */ 319 320 #endif /* HW_EMULATOR_CAMERA_CONVERTERS_H */ 321