| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 #include <stdint.h> | |
| 7 | |
| 8 #include "build/build_config.h" | |
| 9 #include "media/base/simd/convert_yuv_to_rgb.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 #define packuswb(x) ((x) < 0 ? 0 : ((x) > 255 ? 255 : (x))) | |
| 14 #define paddsw(x, y) (((x) + (y)) < -32768 ? -32768 : \ | |
| 15 (((x) + (y)) > 32767 ? 32767 : ((x) + (y)))) | |
| 16 | |
| 17 // On Android, pixel layout is RGBA (see skia/include/core/SkColorPriv.h); | |
| 18 // however, other Chrome platforms use BGRA (see skia/config/SkUserConfig.h). | |
| 19 // Ideally, android should not use the functions here due to performance issue | |
| 20 // (http://crbug.com/249980). | |
| 21 #if defined(OS_ANDROID) | |
| 22 #define SK_R32_SHIFT 0 | |
| 23 #define SK_G32_SHIFT 8 | |
| 24 #define SK_B32_SHIFT 16 | |
| 25 #define SK_A32_SHIFT 24 | |
| 26 #define R_INDEX 0 | |
| 27 #define G_INDEX 1 | |
| 28 #define B_INDEX 2 | |
| 29 #define A_INDEX 3 | |
| 30 #else | |
| 31 #define SK_B32_SHIFT 0 | |
| 32 #define SK_G32_SHIFT 8 | |
| 33 #define SK_R32_SHIFT 16 | |
| 34 #define SK_A32_SHIFT 24 | |
| 35 #define B_INDEX 0 | |
| 36 #define G_INDEX 1 | |
| 37 #define R_INDEX 2 | |
| 38 #define A_INDEX 3 | |
| 39 #endif | |
| 40 | |
| 41 static inline void ConvertYUVToRGB32_C(uint8_t y, | |
| 42 uint8_t u, | |
| 43 uint8_t v, | |
| 44 uint8_t* rgb_buf, | |
| 45 const int16_t* convert_table) { | |
| 46 int b = convert_table[4 * (256 + u) + B_INDEX]; | |
| 47 int g = convert_table[4 * (256 + u) + G_INDEX]; | |
| 48 int r = convert_table[4 * (256 + u) + R_INDEX]; | |
| 49 int a = convert_table[4 * (256 + u) + A_INDEX]; | |
| 50 | |
| 51 b = paddsw(b, convert_table[4 * (512 + v) + B_INDEX]); | |
| 52 g = paddsw(g, convert_table[4 * (512 + v) + G_INDEX]); | |
| 53 r = paddsw(r, convert_table[4 * (512 + v) + R_INDEX]); | |
| 54 a = paddsw(a, convert_table[4 * (512 + v) + A_INDEX]); | |
| 55 | |
| 56 b = paddsw(b, convert_table[4 * y + B_INDEX]); | |
| 57 g = paddsw(g, convert_table[4 * y + G_INDEX]); | |
| 58 r = paddsw(r, convert_table[4 * y + R_INDEX]); | |
| 59 a = paddsw(a, convert_table[4 * y + A_INDEX]); | |
| 60 | |
| 61 b >>= 6; | |
| 62 g >>= 6; | |
| 63 r >>= 6; | |
| 64 a >>= 6; | |
| 65 | |
| 66 *reinterpret_cast<uint32_t*>(rgb_buf) = | |
| 67 (packuswb(b) << SK_B32_SHIFT) | (packuswb(g) << SK_G32_SHIFT) | | |
| 68 (packuswb(r) << SK_R32_SHIFT) | (packuswb(a) << SK_A32_SHIFT); | |
| 69 } | |
| 70 | |
| 71 static inline void ConvertYUVAToARGB_C(uint8_t y, | |
| 72 uint8_t u, | |
| 73 uint8_t v, | |
| 74 uint8_t a, | |
| 75 uint8_t* rgb_buf, | |
| 76 const int16_t* convert_table) { | |
| 77 int b = convert_table[4 * (256 + u) + 0]; | |
| 78 int g = convert_table[4 * (256 + u) + 1]; | |
| 79 int r = convert_table[4 * (256 + u) + 2]; | |
| 80 | |
| 81 b = paddsw(b, convert_table[4 * (512 + v) + 0]); | |
| 82 g = paddsw(g, convert_table[4 * (512 + v) + 1]); | |
| 83 r = paddsw(r, convert_table[4 * (512 + v) + 2]); | |
| 84 | |
| 85 b = paddsw(b, convert_table[4 * y + 0]); | |
| 86 g = paddsw(g, convert_table[4 * y + 1]); | |
| 87 r = paddsw(r, convert_table[4 * y + 2]); | |
| 88 | |
| 89 b >>= 6; | |
| 90 g >>= 6; | |
| 91 r >>= 6; | |
| 92 | |
| 93 b = packuswb(b) * a >> 8; | |
| 94 g = packuswb(g) * a >> 8; | |
| 95 r = packuswb(r) * a >> 8; | |
| 96 | |
| 97 *reinterpret_cast<uint32_t*>(rgb_buf) = | |
| 98 (b << SK_B32_SHIFT) | (g << SK_G32_SHIFT) | (r << SK_R32_SHIFT) | | |
| 99 (a << SK_A32_SHIFT); | |
| 100 } | |
| 101 | |
| 102 void ConvertYUVToRGB32Row_C(const uint8_t* y_buf, | |
| 103 const uint8_t* u_buf, | |
| 104 const uint8_t* v_buf, | |
| 105 uint8_t* rgb_buf, | |
| 106 ptrdiff_t width, | |
| 107 const int16_t* convert_table) { | |
| 108 for (int x = 0; x < width; x += 2) { | |
| 109 uint8_t u = u_buf[x >> 1]; | |
| 110 uint8_t v = v_buf[x >> 1]; | |
| 111 uint8_t y0 = y_buf[x]; | |
| 112 ConvertYUVToRGB32_C(y0, u, v, rgb_buf, convert_table); | |
| 113 if ((x + 1) < width) { | |
| 114 uint8_t y1 = y_buf[x + 1]; | |
| 115 ConvertYUVToRGB32_C(y1, u, v, rgb_buf + 4, convert_table); | |
| 116 } | |
| 117 rgb_buf += 8; // Advance 2 pixels. | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void ConvertYUVAToARGBRow_C(const uint8_t* y_buf, | |
| 122 const uint8_t* u_buf, | |
| 123 const uint8_t* v_buf, | |
| 124 const uint8_t* a_buf, | |
| 125 uint8_t* rgba_buf, | |
| 126 ptrdiff_t width, | |
| 127 const int16_t* convert_table) { | |
| 128 for (int x = 0; x < width; x += 2) { | |
| 129 uint8_t u = u_buf[x >> 1]; | |
| 130 uint8_t v = v_buf[x >> 1]; | |
| 131 uint8_t y0 = y_buf[x]; | |
| 132 uint8_t a0 = a_buf[x]; | |
| 133 ConvertYUVAToARGB_C(y0, u, v, a0, rgba_buf, convert_table); | |
| 134 if ((x + 1) < width) { | |
| 135 uint8_t y1 = y_buf[x + 1]; | |
| 136 uint8_t a1 = a_buf[x + 1]; | |
| 137 ConvertYUVAToARGB_C(y1, u, v, a1, rgba_buf + 4, convert_table); | |
| 138 } | |
| 139 rgba_buf += 8; // Advance 2 pixels. | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 // 16.16 fixed point is used. A shift by 16 isolates the integer. | |
| 144 // A shift by 17 is used to further subsample the chrominence channels. | |
| 145 // & 0xffff isolates the fixed point fraction. >> 2 to get the upper 2 bits, | |
| 146 // for 1/65536 pixel accurate interpolation. | |
| 147 void ScaleYUVToRGB32Row_C(const uint8_t* y_buf, | |
| 148 const uint8_t* u_buf, | |
| 149 const uint8_t* v_buf, | |
| 150 uint8_t* rgb_buf, | |
| 151 ptrdiff_t width, | |
| 152 ptrdiff_t source_dx, | |
| 153 const int16_t* convert_table) { | |
| 154 int x = 0; | |
| 155 for (int i = 0; i < width; i += 2) { | |
| 156 int y = y_buf[x >> 16]; | |
| 157 int u = u_buf[(x >> 17)]; | |
| 158 int v = v_buf[(x >> 17)]; | |
| 159 ConvertYUVToRGB32_C(y, u, v, rgb_buf, convert_table); | |
| 160 x += source_dx; | |
| 161 if ((i + 1) < width) { | |
| 162 y = y_buf[x >> 16]; | |
| 163 ConvertYUVToRGB32_C(y, u, v, rgb_buf+4, convert_table); | |
| 164 x += source_dx; | |
| 165 } | |
| 166 rgb_buf += 8; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void LinearScaleYUVToRGB32Row_C(const uint8_t* y_buf, | |
| 171 const uint8_t* u_buf, | |
| 172 const uint8_t* v_buf, | |
| 173 uint8_t* rgb_buf, | |
| 174 ptrdiff_t width, | |
| 175 ptrdiff_t source_dx, | |
| 176 const int16_t* convert_table) { | |
| 177 // Avoid point-sampling for down-scaling by > 2:1. | |
| 178 int source_x = 0; | |
| 179 if (source_dx >= 0x20000) | |
| 180 source_x += 0x8000; | |
| 181 LinearScaleYUVToRGB32RowWithRange_C(y_buf, u_buf, v_buf, rgb_buf, width, | |
| 182 source_x, source_dx, convert_table); | |
| 183 } | |
| 184 | |
| 185 void LinearScaleYUVToRGB32RowWithRange_C(const uint8_t* y_buf, | |
| 186 const uint8_t* u_buf, | |
| 187 const uint8_t* v_buf, | |
| 188 uint8_t* rgb_buf, | |
| 189 int dest_width, | |
| 190 int x, | |
| 191 int source_dx, | |
| 192 const int16_t* convert_table) { | |
| 193 for (int i = 0; i < dest_width; i += 2) { | |
| 194 int y0 = y_buf[x >> 16]; | |
| 195 int y1 = y_buf[(x >> 16) + 1]; | |
| 196 int u0 = u_buf[(x >> 17)]; | |
| 197 int u1 = u_buf[(x >> 17) + 1]; | |
| 198 int v0 = v_buf[(x >> 17)]; | |
| 199 int v1 = v_buf[(x >> 17) + 1]; | |
| 200 int y_frac = (x & 65535); | |
| 201 int uv_frac = ((x >> 1) & 65535); | |
| 202 int y = (y_frac * y1 + (y_frac ^ 65535) * y0) >> 16; | |
| 203 int u = (uv_frac * u1 + (uv_frac ^ 65535) * u0) >> 16; | |
| 204 int v = (uv_frac * v1 + (uv_frac ^ 65535) * v0) >> 16; | |
| 205 ConvertYUVToRGB32_C(y, u, v, rgb_buf, convert_table); | |
| 206 x += source_dx; | |
| 207 if ((i + 1) < dest_width) { | |
| 208 y0 = y_buf[x >> 16]; | |
| 209 y1 = y_buf[(x >> 16) + 1]; | |
| 210 y_frac = (x & 65535); | |
| 211 y = (y_frac * y1 + (y_frac ^ 65535) * y0) >> 16; | |
| 212 ConvertYUVToRGB32_C(y, u, v, rgb_buf+4, convert_table); | |
| 213 x += source_dx; | |
| 214 } | |
| 215 rgb_buf += 8; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void ConvertYUVToRGB32_C(const uint8_t* yplane, | |
| 220 const uint8_t* uplane, | |
| 221 const uint8_t* vplane, | |
| 222 uint8_t* rgbframe, | |
| 223 int width, | |
| 224 int height, | |
| 225 int ystride, | |
| 226 int uvstride, | |
| 227 int rgbstride, | |
| 228 YUVType yuv_type) { | |
| 229 unsigned int y_shift = GetVerticalShift(yuv_type); | |
| 230 const int16_t* lookup_table = GetLookupTable(yuv_type); | |
| 231 for (int y = 0; y < height; ++y) { | |
| 232 uint8_t* rgb_row = rgbframe + y * rgbstride; | |
| 233 const uint8_t* y_ptr = yplane + y * ystride; | |
| 234 const uint8_t* u_ptr = uplane + (y >> y_shift) * uvstride; | |
| 235 const uint8_t* v_ptr = vplane + (y >> y_shift) * uvstride; | |
| 236 | |
| 237 ConvertYUVToRGB32Row_C(y_ptr, | |
| 238 u_ptr, | |
| 239 v_ptr, | |
| 240 rgb_row, | |
| 241 width, | |
| 242 lookup_table); | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 void ConvertYUVAToARGB_C(const uint8_t* yplane, | |
| 247 const uint8_t* uplane, | |
| 248 const uint8_t* vplane, | |
| 249 const uint8_t* aplane, | |
| 250 uint8_t* rgbaframe, | |
| 251 int width, | |
| 252 int height, | |
| 253 int ystride, | |
| 254 int uvstride, | |
| 255 int astride, | |
| 256 int rgbastride, | |
| 257 YUVType yuv_type) { | |
| 258 unsigned int y_shift = GetVerticalShift(yuv_type); | |
| 259 const int16_t* lookup_table = GetLookupTable(yuv_type); | |
| 260 for (int y = 0; y < height; y++) { | |
| 261 uint8_t* rgba_row = rgbaframe + y * rgbastride; | |
| 262 const uint8_t* y_ptr = yplane + y * ystride; | |
| 263 const uint8_t* u_ptr = uplane + (y >> y_shift) * uvstride; | |
| 264 const uint8_t* v_ptr = vplane + (y >> y_shift) * uvstride; | |
| 265 const uint8_t* a_ptr = aplane + y * astride; | |
| 266 | |
| 267 ConvertYUVAToARGBRow_C(y_ptr, | |
| 268 u_ptr, | |
| 269 v_ptr, | |
| 270 a_ptr, | |
| 271 rgba_row, | |
| 272 width, | |
| 273 lookup_table); | |
| 274 } | |
| 275 } | |
| 276 | |
| 277 } // namespace media | |
| OLD | NEW |