| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "libyuv/row.h" |
| 12 |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> // For memcpy and memset. |
| 15 |
| 16 | #include "libyuv/basic_types.h" |
| 17 |
| 18 | #ifdef __cplusplus |
| 19 | namespace libyuv { |
| 20 | extern "C" { |
| 21 | #endif |
| 22 |
| 23 | // The following ifdef from row_win makes the C code match the row_win code, |
| 24 | // which is 7 bit fixed point. |
| 25 | #if !defined(LIBYUV_DISABLE_X86) && defined(_MSC_VER) && \ |
| 26 | (defined(_M_IX86) || (defined(_M_X64) && !defined(__clang__))) |
| 27 | #define LIBYUV_RGB7 1 |
| 28 | #endif |
| 29 | // mips use 7 bit RGBToY |
| 30 | #if (!defined(LIBYUV_DISABLE_MMI) && defined(_MIPS_ARCH_LOONGSON3A)) || \ |
| 31 | (!defined(LIBYUV_DISABLE_MSA) && defined(__mips_msa)) |
| 32 | #define LIBYUV_RGB7 1 |
| 33 | #endif |
| 34 |
| 35 | // llvm x86 is poor at ternary operator, so use branchless min/max. |
| 36 |
| 37 | #define USE_BRANCHLESS 1 |
| 38 | #if USE_BRANCHLESS |
| 39 | static __inline int32_t clamp0(int32_t v) { |
| 40 | return ((-(v) >> 31) & (v)); |
| 41 | } |
| 42 |
| 43 | static __inline int32_t clamp255(int32_t v) { |
| 44 | return (((255 - (v)) >> 31) | (v)) & 255; |
| 45 | } |
| 46 |
| 47 | static __inline int32_t clamp1023(int32_t v) { |
| 48 | return (((1023 - (v)) >> 31) | (v)) & 1023; |
| 49 | } |
| 50 |
| 51 | static __inline uint32_t Abs(int32_t v) { |
| 52 | int m = v >> 31; |
| 53 | return (v + m) ^ m; |
| 54 | } |
| 55 | #else // USE_BRANCHLESS |
| 56 | static __inline int32_t clamp0(int32_t v) { |
| 57 | return (v < 0) ? 0 : v; |
| 58 | } |
| 59 |
| 60 | static __inline int32_t clamp255(int32_t v) { |
| 61 | return (v > 255) ? 255 : v; |
| 62 | } |
| 63 |
| 64 | static __inline int32_t clamp1023(int32_t v) { |
| 65 | return (v > 1023) ? 1023 : v; |
| 66 | } |
| 67 |
| 68 | static __inline uint32_t Abs(int32_t v) { |
| 69 | return (v < 0) ? -v : v; |
| 70 | } |
| 71 | #endif // USE_BRANCHLESS |
| 72 | static __inline uint32_t Clamp(int32_t val) { |
| 73 | int v = clamp0(val); |
| 74 | return (uint32_t)(clamp255(v)); |
| 75 | } |
| 76 |
| 77 | static __inline uint32_t Clamp10(int32_t val) { |
| 78 | int v = clamp0(val); |
| 79 | return (uint32_t)(clamp1023(v)); |
| 80 | } |
| 81 |
| 82 | // Little Endian |
| 83 | #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \ |
| 84 | defined(_M_IX86) || defined(__arm__) || defined(_M_ARM) || \ |
| 85 | (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 86 | #define WRITEWORD(p, v) *(uint32_t*)(p) = v |
| 87 | #else |
| 88 | static inline void WRITEWORD(uint8_t* p, uint32_t v) { |
| 89 | p[0] = (uint8_t)(v & 255); |
| 90 | p[1] = (uint8_t)((v >> 8) & 255); |
| 91 | p[2] = (uint8_t)((v >> 16) & 255); |
| 92 | p[3] = (uint8_t)((v >> 24) & 255); |
| 93 | } |
| 94 | #endif |
| 95 |
| 96 | void RGB24ToARGBRow_C(const uint8_t* src_rgb24, uint8_t* dst_argb, int width) { |
| 97 | int x; |
| 98 | for (x = 0; x < width; ++x) { |
| 99 | uint8_t b = src_rgb24[0]; |
| 100 | uint8_t g = src_rgb24[1]; |
| 101 | uint8_t r = src_rgb24[2]; |
| 102 | dst_argb[0] = b; |
| 103 | dst_argb[1] = g; |
| 104 | dst_argb[2] = r; |
| 105 | dst_argb[3] = 255u; |
| 106 | dst_argb += 4; |
| 107 | src_rgb24 += 3; |
| 108 | } |
| 109 | } |
| 110 |
| 111 | void RAWToARGBRow_C(const uint8_t* src_raw, uint8_t* dst_argb, int width) { |
| 112 | int x; |
| 113 | for (x = 0; x < width; ++x) { |
| 114 | uint8_t r = src_raw[0]; |
| 115 | uint8_t g = src_raw[1]; |
| 116 | uint8_t b = src_raw[2]; |
| 117 | dst_argb[0] = b; |
| 118 | dst_argb[1] = g; |
| 119 | dst_argb[2] = r; |
| 120 | dst_argb[3] = 255u; |
| 121 | dst_argb += 4; |
| 122 | src_raw += 3; |
| 123 | } |
| 124 | } |
| 125 |
| 126 | void RAWToRGBARow_C(const uint8_t* src_raw, uint8_t* dst_rgba, int width) { |
| 127 | int x; |
| 128 | for (x = 0; x < width; ++x) { |
| 129 | uint8_t r = src_raw[0]; |
| 130 | uint8_t g = src_raw[1]; |
| 131 | uint8_t b = src_raw[2]; |
| 132 | dst_rgba[0] = 255u; |
| 133 | dst_rgba[1] = b; |
| 134 | dst_rgba[2] = g; |
| 135 | dst_rgba[3] = r; |
| 136 | dst_rgba += 4; |
| 137 | src_raw += 3; |
| 138 | } |
| 139 | } |
| 140 |
| 141 | void RAWToRGB24Row_C(const uint8_t* src_raw, uint8_t* dst_rgb24, int width) { |
| 142 | int x; |
| 143 | for (x = 0; x < width; ++x) { |
| 144 | uint8_t r = src_raw[0]; |
| 145 | uint8_t g = src_raw[1]; |
| 146 | uint8_t b = src_raw[2]; |
| 147 | dst_rgb24[0] = b; |
| 148 | dst_rgb24[1] = g; |
| 149 | dst_rgb24[2] = r; |
| 150 | dst_rgb24 += 3; |
| 151 | src_raw += 3; |
| 152 | } |
| 153 | } |
| 154 |
| 155 | void RGB565ToARGBRow_C(const uint8_t* src_rgb565, |
| 156 | uint8_t* dst_argb, |
| 157 | int width) { |
| 158 | int x; |
| 159 | for (x = 0; x < width; ++x) { |
| 160 | uint8_t b = src_rgb565[0] & 0x1f; |
| 161 | uint8_t g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3); |
| 162 | uint8_t r = src_rgb565[1] >> 3; |
| 163 | dst_argb[0] = (b << 3) | (b >> 2); |
| 164 | dst_argb[1] = (g << 2) | (g >> 4); |
| 165 | dst_argb[2] = (r << 3) | (r >> 2); |
| 166 | dst_argb[3] = 255u; |
| 167 | dst_argb += 4; |
| 168 | src_rgb565 += 2; |
| 169 | } |
| 170 | } |
| 171 |
| 172 | void ARGB1555ToARGBRow_C(const uint8_t* src_argb1555, |
| 173 | uint8_t* dst_argb, |
| 174 | int width) { |
| 175 | int x; |
| 176 | for (x = 0; x < width; ++x) { |
| 177 | uint8_t b = src_argb1555[0] & 0x1f; |
| 178 | uint8_t g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3); |
| 179 | uint8_t r = (src_argb1555[1] & 0x7c) >> 2; |
| 180 | uint8_t a = src_argb1555[1] >> 7; |
| 181 | dst_argb[0] = (b << 3) | (b >> 2); |
| 182 | dst_argb[1] = (g << 3) | (g >> 2); |
| 183 | dst_argb[2] = (r << 3) | (r >> 2); |
| 184 | dst_argb[3] = -a; |
| 185 | dst_argb += 4; |
| 186 | src_argb1555 += 2; |
| 187 | } |
| 188 | } |
| 189 |
| 190 | void ARGB4444ToARGBRow_C(const uint8_t* src_argb4444, |
| 191 | uint8_t* dst_argb, |
| 192 | int width) { |
| 193 | int x; |
| 194 | for (x = 0; x < width; ++x) { |
| 195 | uint8_t b = src_argb4444[0] & 0x0f; |
| 196 | uint8_t g = src_argb4444[0] >> 4; |
| 197 | uint8_t r = src_argb4444[1] & 0x0f; |
| 198 | uint8_t a = src_argb4444[1] >> 4; |
| 199 | dst_argb[0] = (b << 4) | b; |
| 200 | dst_argb[1] = (g << 4) | g; |
| 201 | dst_argb[2] = (r << 4) | r; |
| 202 | dst_argb[3] = (a << 4) | a; |
| 203 | dst_argb += 4; |
| 204 | src_argb4444 += 2; |
| 205 | } |
| 206 | } |
| 207 |
| 208 | void AR30ToARGBRow_C(const uint8_t* src_ar30, uint8_t* dst_argb, int width) { |
| 209 | int x; |
| 210 | for (x = 0; x < width; ++x) { |
| 211 | uint32_t ar30 = *(const uint32_t*)src_ar30; |
| 212 | uint32_t b = (ar30 >> 2) & 0xff; |
| 213 | uint32_t g = (ar30 >> 12) & 0xff; |
| 214 | uint32_t r = (ar30 >> 22) & 0xff; |
| 215 | uint32_t a = (ar30 >> 30) * 0x55; // Replicate 2 bits to 8 bits. |
| 216 | *(uint32_t*)(dst_argb) = b | (g << 8) | (r << 16) | (a << 24); |
| 217 | dst_argb += 4; |
| 218 | src_ar30 += 4; |
| 219 | } |
| 220 | } |
| 221 |
| 222 | void AR30ToABGRRow_C(const uint8_t* src_ar30, uint8_t* dst_abgr, int width) { |
| 223 | int x; |
| 224 | for (x = 0; x < width; ++x) { |
| 225 | uint32_t ar30 = *(const uint32_t*)src_ar30; |
| 226 | uint32_t b = (ar30 >> 2) & 0xff; |
| 227 | uint32_t g = (ar30 >> 12) & 0xff; |
| 228 | uint32_t r = (ar30 >> 22) & 0xff; |
| 229 | uint32_t a = (ar30 >> 30) * 0x55; // Replicate 2 bits to 8 bits. |
| 230 | *(uint32_t*)(dst_abgr) = r | (g << 8) | (b << 16) | (a << 24); |
| 231 | dst_abgr += 4; |
| 232 | src_ar30 += 4; |
| 233 | } |
| 234 | } |
| 235 |
| 236 | void AR30ToAB30Row_C(const uint8_t* src_ar30, uint8_t* dst_ab30, int width) { |
| 237 | int x; |
| 238 | for (x = 0; x < width; ++x) { |
| 239 | uint32_t ar30 = *(const uint32_t*)src_ar30; |
| 240 | uint32_t b = ar30 & 0x3ff; |
| 241 | uint32_t ga = ar30 & 0xc00ffc00; |
| 242 | uint32_t r = (ar30 >> 20) & 0x3ff; |
| 243 | *(uint32_t*)(dst_ab30) = r | ga | (b << 20); |
| 244 | dst_ab30 += 4; |
| 245 | src_ar30 += 4; |
| 246 | } |
| 247 | } |
| 248 |
| 249 | void ARGBToRGB24Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) { |
| 250 | int x; |
| 251 | for (x = 0; x < width; ++x) { |
| 252 | uint8_t b = src_argb[0]; |
| 253 | uint8_t g = src_argb[1]; |
| 254 | uint8_t r = src_argb[2]; |
| 255 | dst_rgb[0] = b; |
| 256 | dst_rgb[1] = g; |
| 257 | dst_rgb[2] = r; |
| 258 | dst_rgb += 3; |
| 259 | src_argb += 4; |
| 260 | } |
| 261 | } |
| 262 |
| 263 | void ARGBToRAWRow_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) { |
| 264 | int x; |
| 265 | for (x = 0; x < width; ++x) { |
| 266 | uint8_t b = src_argb[0]; |
| 267 | uint8_t g = src_argb[1]; |
| 268 | uint8_t r = src_argb[2]; |
| 269 | dst_rgb[0] = r; |
| 270 | dst_rgb[1] = g; |
| 271 | dst_rgb[2] = b; |
| 272 | dst_rgb += 3; |
| 273 | src_argb += 4; |
| 274 | } |
| 275 | } |
| 276 |
| 277 | void ARGBToRGB565Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) { |
| 278 | int x; |
| 279 | for (x = 0; x < width - 1; x += 2) { |
| 280 | uint8_t b0 = src_argb[0] >> 3; |
| 281 | uint8_t g0 = src_argb[1] >> 2; |
| 282 | uint8_t r0 = src_argb[2] >> 3; |
| 283 | uint8_t b1 = src_argb[4] >> 3; |
| 284 | uint8_t g1 = src_argb[5] >> 2; |
| 285 | uint8_t r1 = src_argb[6] >> 3; |
| 286 | WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | |
| 287 | (r1 << 27)); |
| 288 | dst_rgb += 4; |
| 289 | src_argb += 8; |
| 290 | } |
| 291 | if (width & 1) { |
| 292 | uint8_t b0 = src_argb[0] >> 3; |
| 293 | uint8_t g0 = src_argb[1] >> 2; |
| 294 | uint8_t r0 = src_argb[2] >> 3; |
| 295 | *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11); |
| 296 | } |
| 297 | } |
| 298 |
| 299 | // dither4 is a row of 4 values from 4x4 dither matrix. |
| 300 | // The 4x4 matrix contains values to increase RGB. When converting to |
| 301 | // fewer bits (565) this provides an ordered dither. |
| 302 | // The order in the 4x4 matrix in first byte is upper left. |
| 303 | // The 4 values are passed as an int, then referenced as an array, so |
| 304 | // endian will not affect order of the original matrix. But the dither4 |
| 305 | // will containing the first pixel in the lower byte for little endian |
| 306 | // or the upper byte for big endian. |
| 307 | void ARGBToRGB565DitherRow_C(const uint8_t* src_argb, |
| 308 | uint8_t* dst_rgb, |
| 309 | const uint32_t dither4, |
| 310 | int width) { |
| 311 | int x; |
| 312 | for (x = 0; x < width - 1; x += 2) { |
| 313 | int dither0 = ((const unsigned char*)(&dither4))[x & 3]; |
| 314 | int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3]; |
| 315 | uint8_t b0 = clamp255(src_argb[0] + dither0) >> 3; |
| 316 | uint8_t g0 = clamp255(src_argb[1] + dither0) >> 2; |
| 317 | uint8_t r0 = clamp255(src_argb[2] + dither0) >> 3; |
| 318 | uint8_t b1 = clamp255(src_argb[4] + dither1) >> 3; |
| 319 | uint8_t g1 = clamp255(src_argb[5] + dither1) >> 2; |
| 320 | uint8_t r1 = clamp255(src_argb[6] + dither1) >> 3; |
| 321 | WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | |
| 322 | (r1 << 27)); |
| 323 | dst_rgb += 4; |
| 324 | src_argb += 8; |
| 325 | } |
| 326 | if (width & 1) { |
| 327 | int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3]; |
| 328 | uint8_t b0 = clamp255(src_argb[0] + dither0) >> 3; |
| 329 | uint8_t g0 = clamp255(src_argb[1] + dither0) >> 2; |
| 330 | uint8_t r0 = clamp255(src_argb[2] + dither0) >> 3; |
| 331 | *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11); |
| 332 | } |
| 333 | } |
| 334 |
| 335 | void ARGBToARGB1555Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) { |
| 336 | int x; |
| 337 | for (x = 0; x < width - 1; x += 2) { |
| 338 | uint8_t b0 = src_argb[0] >> 3; |
| 339 | uint8_t g0 = src_argb[1] >> 3; |
| 340 | uint8_t r0 = src_argb[2] >> 3; |
| 341 | uint8_t a0 = src_argb[3] >> 7; |
| 342 | uint8_t b1 = src_argb[4] >> 3; |
| 343 | uint8_t g1 = src_argb[5] >> 3; |
| 344 | uint8_t r1 = src_argb[6] >> 3; |
| 345 | uint8_t a1 = src_argb[7] >> 7; |
| 346 | *(uint32_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15) | |
| 347 | (b1 << 16) | (g1 << 21) | (r1 << 26) | (a1 << 31); |
| 348 | dst_rgb += 4; |
| 349 | src_argb += 8; |
| 350 | } |
| 351 | if (width & 1) { |
| 352 | uint8_t b0 = src_argb[0] >> 3; |
| 353 | uint8_t g0 = src_argb[1] >> 3; |
| 354 | uint8_t r0 = src_argb[2] >> 3; |
| 355 | uint8_t a0 = src_argb[3] >> 7; |
| 356 | *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15); |
| 357 | } |
| 358 | } |
| 359 |
| 360 | void ARGBToARGB4444Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) { |
| 361 | int x; |
| 362 | for (x = 0; x < width - 1; x += 2) { |
| 363 | uint8_t b0 = src_argb[0] >> 4; |
| 364 | uint8_t g0 = src_argb[1] >> 4; |
| 365 | uint8_t r0 = src_argb[2] >> 4; |
| 366 | uint8_t a0 = src_argb[3] >> 4; |
| 367 | uint8_t b1 = src_argb[4] >> 4; |
| 368 | uint8_t g1 = src_argb[5] >> 4; |
| 369 | uint8_t r1 = src_argb[6] >> 4; |
| 370 | uint8_t a1 = src_argb[7] >> 4; |
| 371 | *(uint32_t*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12) | |
| 372 | (b1 << 16) | (g1 << 20) | (r1 << 24) | (a1 << 28); |
| 373 | dst_rgb += 4; |
| 374 | src_argb += 8; |
| 375 | } |
| 376 | if (width & 1) { |
| 377 | uint8_t b0 = src_argb[0] >> 4; |
| 378 | uint8_t g0 = src_argb[1] >> 4; |
| 379 | uint8_t r0 = src_argb[2] >> 4; |
| 380 | uint8_t a0 = src_argb[3] >> 4; |
| 381 | *(uint16_t*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12); |
| 382 | } |
| 383 | } |
| 384 |
| 385 | void ABGRToAR30Row_C(const uint8_t* src_abgr, uint8_t* dst_ar30, int width) { |
| 386 | int x; |
| 387 | for (x = 0; x < width; ++x) { |
| 388 | uint32_t b0 = (src_abgr[0] >> 6) | ((uint32_t)(src_abgr[0]) << 2); |
| 389 | uint32_t g0 = (src_abgr[1] >> 6) | ((uint32_t)(src_abgr[1]) << 2); |
| 390 | uint32_t r0 = (src_abgr[2] >> 6) | ((uint32_t)(src_abgr[2]) << 2); |
| 391 | uint32_t a0 = (src_abgr[3] >> 6); |
| 392 | *(uint32_t*)(dst_ar30) = r0 | (g0 << 10) | (b0 << 20) | (a0 << 30); |
| 393 | dst_ar30 += 4; |
| 394 | src_abgr += 4; |
| 395 | } |
| 396 | } |
| 397 |
| 398 | void ARGBToAR30Row_C(const uint8_t* src_argb, uint8_t* dst_ar30, int width) { |
| 399 | int x; |
| 400 | for (x = 0; x < width; ++x) { |
| 401 | uint32_t b0 = (src_argb[0] >> 6) | ((uint32_t)(src_argb[0]) << 2); |
| 402 | uint32_t g0 = (src_argb[1] >> 6) | ((uint32_t)(src_argb[1]) << 2); |
| 403 | uint32_t r0 = (src_argb[2] >> 6) | ((uint32_t)(src_argb[2]) << 2); |
| 404 | uint32_t a0 = (src_argb[3] >> 6); |
| 405 | *(uint32_t*)(dst_ar30) = b0 | (g0 << 10) | (r0 << 20) | (a0 << 30); |
| 406 | dst_ar30 += 4; |
| 407 | src_argb += 4; |
| 408 | } |
| 409 | } |
| 410 |
| 411 | #ifdef LIBYUV_RGB7 |
| 412 | // Old 7 bit math for compatibility on unsupported platforms. |
| 413 | static __inline int RGBToY(uint8_t r, uint8_t g, uint8_t b) { |
| 414 | return ((33 * r + 65 * g + 13 * b) >> 7) + 16; |
| 415 | } |
| 416 | #else |
| 417 | // 8 bit |
| 418 | // Intel SSE/AVX uses the following equivalent formula |
| 419 | // 0x7e80 = (66 + 129 + 25) * -128 + 0x1000 (for +16) and 0x0080 for round. |
| 420 | // return (66 * ((int)r - 128) + 129 * ((int)g - 128) + 25 * ((int)b - 128) + |
| 421 | // 0x7e80) >> 8; |
| 422 |
| 423 | static __inline int RGBToY(uint8_t r, uint8_t g, uint8_t b) { |
| 424 | return (66 * r + 129 * g + 25 * b + 0x1080) >> 8; |
| 425 | } |
| 426 | #endif |
| 427 |
| 428 | static __inline int RGBToU(uint8_t r, uint8_t g, uint8_t b) { |
| 429 | return (112 * b - 74 * g - 38 * r + 0x8080) >> 8; |
| 430 | } |
| 431 | static __inline int RGBToV(uint8_t r, uint8_t g, uint8_t b) { |
| 432 | return (112 * r - 94 * g - 18 * b + 0x8080) >> 8; |
| 433 | } |
| 434 |
| 435 | // ARGBToY_C and ARGBToUV_C |
| 436 | #define MAKEROWY(NAME, R, G, B, BPP) \ |
| 437 | void NAME##ToYRow_C(const uint8_t* src_argb0, uint8_t* dst_y, int width) { \ |
| 438 | int x; \ |
| 439 | for (x = 0; x < width; ++x) { \ |
| 440 | dst_y[0] = RGBToY(src_argb0[R], src_argb0[G], src_argb0[B]); \ |
| 441 | src_argb0 += BPP; \ |
| 442 | dst_y += 1; \ |
| 443 | } \ |
| 444 | } \ |
| 445 | void NAME##ToUVRow_C(const uint8_t* src_rgb0, int src_stride_rgb, \ |
| 446 | uint8_t* dst_u, uint8_t* dst_v, int width) { \ |
| 447 | const uint8_t* src_rgb1 = src_rgb0 + src_stride_rgb; \ |
| 448 | int x; \ |
| 449 | for (x = 0; x < width - 1; x += 2) { \ |
| 450 | uint8_t ab = (src_rgb0[B] + src_rgb0[B + BPP] + src_rgb1[B] + \ |
| 451 | src_rgb1[B + BPP]) >> \ |
| 452 | 2; \ |
| 453 | uint8_t ag = (src_rgb0[G] + src_rgb0[G + BPP] + src_rgb1[G] + \ |
| 454 | src_rgb1[G + BPP]) >> \ |
| 455 | 2; \ |
| 456 | uint8_t ar = (src_rgb0[R] + src_rgb0[R + BPP] + src_rgb1[R] + \ |
| 457 | src_rgb1[R + BPP]) >> \ |
| 458 | 2; \ |
| 459 | dst_u[0] = RGBToU(ar, ag, ab); \ |
| 460 | dst_v[0] = RGBToV(ar, ag, ab); \ |
| 461 | src_rgb0 += BPP * 2; \ |
| 462 | src_rgb1 += BPP * 2; \ |
| 463 | dst_u += 1; \ |
| 464 | dst_v += 1; \ |
| 465 | } \ |
| 466 | if (width & 1) { \ |
| 467 | uint8_t ab = (src_rgb0[B] + src_rgb1[B]) >> 1; \ |
| 468 | uint8_t ag = (src_rgb0[G] + src_rgb1[G]) >> 1; \ |
| 469 | uint8_t ar = (src_rgb0[R] + src_rgb1[R]) >> 1; \ |
| 470 | dst_u[0] = RGBToU(ar, ag, ab); \ |
| 471 | dst_v[0] = RGBToV(ar, ag, ab); \ |
| 472 | } \ |
| 473 | } |
| 474 |
| 475 | MAKEROWY(ARGB, 2, 1, 0, 4) |
| 476 | MAKEROWY(BGRA, 1, 2, 3, 4) |
| 477 | MAKEROWY(ABGR, 0, 1, 2, 4) |
| 478 | MAKEROWY(RGBA, 3, 2, 1, 4) |
| 479 | MAKEROWY(RGB24, 2, 1, 0, 3) |
| 480 | MAKEROWY(RAW, 0, 1, 2, 3) |
| 481 | #undef MAKEROWY |
| 482 |
| 483 | // JPeg uses a variation on BT.601-1 full range |
| 484 | // y = 0.29900 * r + 0.58700 * g + 0.11400 * b |
| 485 | // u = -0.16874 * r - 0.33126 * g + 0.50000 * b + center |
| 486 | // v = 0.50000 * r - 0.41869 * g - 0.08131 * b + center |
| 487 | // BT.601 Mpeg range uses: |
| 488 | // b 0.1016 * 255 = 25.908 = 25 |
| 489 | // g 0.5078 * 255 = 129.489 = 129 |
| 490 | // r 0.2578 * 255 = 65.739 = 66 |
| 491 | // JPeg 7 bit Y (deprecated) |
| 492 | // b 0.11400 * 128 = 14.592 = 15 |
| 493 | // g 0.58700 * 128 = 75.136 = 75 |
| 494 | // r 0.29900 * 128 = 38.272 = 38 |
| 495 | // JPeg 8 bit Y: |
| 496 | // b 0.11400 * 256 = 29.184 = 29 |
| 497 | // g 0.58700 * 256 = 150.272 = 150 |
| 498 | // r 0.29900 * 256 = 76.544 = 77 |
| 499 | // JPeg 8 bit U: |
| 500 | // b 0.50000 * 255 = 127.5 = 127 |
| 501 | // g -0.33126 * 255 = -84.4713 = -84 |
| 502 | // r -0.16874 * 255 = -43.0287 = -43 |
| 503 | // JPeg 8 bit V: |
| 504 | // b -0.08131 * 255 = -20.73405 = -20 |
| 505 | // g -0.41869 * 255 = -106.76595 = -107 |
| 506 | // r 0.50000 * 255 = 127.5 = 127 |
| 507 |
| 508 | #ifdef LIBYUV_RGB7 |
| 509 | // Old 7 bit math for compatibility on unsupported platforms. |
| 510 | static __inline int RGBToYJ(uint8_t r, uint8_t g, uint8_t b) { |
| 511 | return (38 * r + 75 * g + 15 * b + 64) >> 7; |
| 512 | } |
| 513 | #else |
| 514 | // 8 bit |
| 515 | static __inline int RGBToYJ(uint8_t r, uint8_t g, uint8_t b) { |
| 516 | return (77 * r + 150 * g + 29 * b + 128) >> 8; |
| 517 | } |
| 518 | #endif |
| 519 |
| 520 | static __inline int RGBToUJ(uint8_t r, uint8_t g, uint8_t b) { |
| 521 | return (127 * b - 84 * g - 43 * r + 0x8080) >> 8; |
| 522 | } |
| 523 | static __inline int RGBToVJ(uint8_t r, uint8_t g, uint8_t b) { |
| 524 | return (127 * r - 107 * g - 20 * b + 0x8080) >> 8; |
| 525 | } |
| 526 |
| 527 | #define AVGB(a, b) (((a) + (b) + 1) >> 1) |
| 528 |
| 529 | // ARGBToYJ_C and ARGBToUVJ_C |
| 530 | #define MAKEROWYJ(NAME, R, G, B, BPP) \ |
| 531 | void NAME##ToYJRow_C(const uint8_t* src_argb0, uint8_t* dst_y, int width) { \ |
| 532 | int x; \ |
| 533 | for (x = 0; x < width; ++x) { \ |
| 534 | dst_y[0] = RGBToYJ(src_argb0[R], src_argb0[G], src_argb0[B]); \ |
| 535 | src_argb0 += BPP; \ |
| 536 | dst_y += 1; \ |
| 537 | } \ |
| 538 | } \ |
| 539 | void NAME##ToUVJRow_C(const uint8_t* src_rgb0, int src_stride_rgb, \ |
| 540 | uint8_t* dst_u, uint8_t* dst_v, int width) { \ |
| 541 | const uint8_t* src_rgb1 = src_rgb0 + src_stride_rgb; \ |
| 542 | int x; \ |
| 543 | for (x = 0; x < width - 1; x += 2) { \ |
| 544 | uint8_t ab = AVGB(AVGB(src_rgb0[B], src_rgb1[B]), \ |
| 545 | AVGB(src_rgb0[B + BPP], src_rgb1[B + BPP])); \ |
| 546 | uint8_t ag = AVGB(AVGB(src_rgb0[G], src_rgb1[G]), \ |
| 547 | AVGB(src_rgb0[G + BPP], src_rgb1[G + BPP])); \ |
| 548 | uint8_t ar = AVGB(AVGB(src_rgb0[R], src_rgb1[R]), \ |
| 549 | AVGB(src_rgb0[R + BPP], src_rgb1[R + BPP])); \ |
| 550 | dst_u[0] = RGBToUJ(ar, ag, ab); \ |
| 551 | dst_v[0] = RGBToVJ(ar, ag, ab); \ |
| 552 | src_rgb0 += BPP * 2; \ |
| 553 | src_rgb1 += BPP * 2; \ |
| 554 | dst_u += 1; \ |
| 555 | dst_v += 1; \ |
| 556 | } \ |
| 557 | if (width & 1) { \ |
| 558 | uint8_t ab = AVGB(src_rgb0[B], src_rgb1[B]); \ |
| 559 | uint8_t ag = AVGB(src_rgb0[G], src_rgb1[G]); \ |
| 560 | uint8_t ar = AVGB(src_rgb0[R], src_rgb1[R]); \ |
| 561 | dst_u[0] = RGBToUJ(ar, ag, ab); \ |
| 562 | dst_v[0] = RGBToVJ(ar, ag, ab); \ |
| 563 | } \ |
| 564 | } |
| 565 |
| 566 | MAKEROWYJ(ARGB, 2, 1, 0, 4) |
| 567 | MAKEROWYJ(RGBA, 3, 2, 1, 4) |
| 568 | #undef MAKEROWYJ |
| 569 |
| 570 | void RGB565ToYRow_C(const uint8_t* src_rgb565, uint8_t* dst_y, int width) { |
| 571 | int x; |
| 572 | for (x = 0; x < width; ++x) { |
| 573 | uint8_t b = src_rgb565[0] & 0x1f; |
| 574 | uint8_t g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3); |
| 575 | uint8_t r = src_rgb565[1] >> 3; |
| 576 | b = (b << 3) | (b >> 2); |
| 577 | g = (g << 2) | (g >> 4); |
| 578 | r = (r << 3) | (r >> 2); |
| 579 | dst_y[0] = RGBToY(r, g, b); |
| 580 | src_rgb565 += 2; |
| 581 | dst_y += 1; |
| 582 | } |
| 583 | } |
| 584 |
| 585 | void ARGB1555ToYRow_C(const uint8_t* src_argb1555, uint8_t* dst_y, int width) { |
| 586 | int x; |
| 587 | for (x = 0; x < width; ++x) { |
| 588 | uint8_t b = src_argb1555[0] & 0x1f; |
| 589 | uint8_t g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3); |
| 590 | uint8_t r = (src_argb1555[1] & 0x7c) >> 2; |
| 591 | b = (b << 3) | (b >> 2); |
| 592 | g = (g << 3) | (g >> 2); |
| 593 | r = (r << 3) | (r >> 2); |
| 594 | dst_y[0] = RGBToY(r, g, b); |
| 595 | src_argb1555 += 2; |
| 596 | dst_y += 1; |
| 597 | } |
| 598 | } |
| 599 |
| 600 | void ARGB4444ToYRow_C(const uint8_t* src_argb4444, uint8_t* dst_y, int width) { |
| 601 | int x; |
| 602 | for (x = 0; x < width; ++x) { |
| 603 | uint8_t b = src_argb4444[0] & 0x0f; |
| 604 | uint8_t g = src_argb4444[0] >> 4; |
| 605 | uint8_t r = src_argb4444[1] & 0x0f; |
| 606 | b = (b << 4) | b; |
| 607 | g = (g << 4) | g; |
| 608 | r = (r << 4) | r; |
| 609 | dst_y[0] = RGBToY(r, g, b); |
| 610 | src_argb4444 += 2; |
| 611 | dst_y += 1; |
| 612 | } |
| 613 | } |
| 614 |
| 615 | void RGB565ToUVRow_C(const uint8_t* src_rgb565, |
| 616 | int src_stride_rgb565, |
| 617 | uint8_t* dst_u, |
| 618 | uint8_t* dst_v, |
| 619 | int width) { |
| 620 | const uint8_t* next_rgb565 = src_rgb565 + src_stride_rgb565; |
| 621 | int x; |
| 622 | for (x = 0; x < width - 1; x += 2) { |
| 623 | uint8_t b0 = src_rgb565[0] & 0x1f; |
| 624 | uint8_t g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3); |
| 625 | uint8_t r0 = src_rgb565[1] >> 3; |
| 626 | uint8_t b1 = src_rgb565[2] & 0x1f; |
| 627 | uint8_t g1 = (src_rgb565[2] >> 5) | ((src_rgb565[3] & 0x07) << 3); |
| 628 | uint8_t r1 = src_rgb565[3] >> 3; |
| 629 | uint8_t b2 = next_rgb565[0] & 0x1f; |
| 630 | uint8_t g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3); |
| 631 | uint8_t r2 = next_rgb565[1] >> 3; |
| 632 | uint8_t b3 = next_rgb565[2] & 0x1f; |
| 633 | uint8_t g3 = (next_rgb565[2] >> 5) | ((next_rgb565[3] & 0x07) << 3); |
| 634 | uint8_t r3 = next_rgb565[3] >> 3; |
| 635 | uint8_t b = (b0 + b1 + b2 + b3); // 565 * 4 = 787. |
| 636 | uint8_t g = (g0 + g1 + g2 + g3); |
| 637 | uint8_t r = (r0 + r1 + r2 + r3); |
| 638 | b = (b << 1) | (b >> 6); // 787 -> 888. |
| 639 | r = (r << 1) | (r >> 6); |
| 640 | dst_u[0] = RGBToU(r, g, b); |
| 641 | dst_v[0] = RGBToV(r, g, b); |
| 642 | src_rgb565 += 4; |
| 643 | next_rgb565 += 4; |
| 644 | dst_u += 1; |
| 645 | dst_v += 1; |
| 646 | } |
| 647 | if (width & 1) { |
| 648 | uint8_t b0 = src_rgb565[0] & 0x1f; |
| 649 | uint8_t g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3); |
| 650 | uint8_t r0 = src_rgb565[1] >> 3; |
| 651 | uint8_t b2 = next_rgb565[0] & 0x1f; |
| 652 | uint8_t g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3); |
| 653 | uint8_t r2 = next_rgb565[1] >> 3; |
| 654 | uint8_t b = (b0 + b2); // 565 * 2 = 676. |
| 655 | uint8_t g = (g0 + g2); |
| 656 | uint8_t r = (r0 + r2); |
| 657 | b = (b << 2) | (b >> 4); // 676 -> 888 |
| 658 | g = (g << 1) | (g >> 6); |
| 659 | r = (r << 2) | (r >> 4); |
| 660 | dst_u[0] = RGBToU(r, g, b); |
| 661 | dst_v[0] = RGBToV(r, g, b); |
| 662 | } |
| 663 | } |
| 664 |
| 665 | void ARGB1555ToUVRow_C(const uint8_t* src_argb1555, |
| 666 | int src_stride_argb1555, |
| 667 | uint8_t* dst_u, |
| 668 | uint8_t* dst_v, |
| 669 | int width) { |
| 670 | const uint8_t* next_argb1555 = src_argb1555 + src_stride_argb1555; |
| 671 | int x; |
| 672 | for (x = 0; x < width - 1; x += 2) { |
| 673 | uint8_t b0 = src_argb1555[0] & 0x1f; |
| 674 | uint8_t g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3); |
| 675 | uint8_t r0 = (src_argb1555[1] & 0x7c) >> 2; |
| 676 | uint8_t b1 = src_argb1555[2] & 0x1f; |
| 677 | uint8_t g1 = (src_argb1555[2] >> 5) | ((src_argb1555[3] & 0x03) << 3); |
| 678 | uint8_t r1 = (src_argb1555[3] & 0x7c) >> 2; |
| 679 | uint8_t b2 = next_argb1555[0] & 0x1f; |
| 680 | uint8_t g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3); |
| 681 | uint8_t r2 = (next_argb1555[1] & 0x7c) >> 2; |
| 682 | uint8_t b3 = next_argb1555[2] & 0x1f; |
| 683 | uint8_t g3 = (next_argb1555[2] >> 5) | ((next_argb1555[3] & 0x03) << 3); |
| 684 | uint8_t r3 = (next_argb1555[3] & 0x7c) >> 2; |
| 685 | uint8_t b = (b0 + b1 + b2 + b3); // 555 * 4 = 777. |
| 686 | uint8_t g = (g0 + g1 + g2 + g3); |
| 687 | uint8_t r = (r0 + r1 + r2 + r3); |
| 688 | b = (b << 1) | (b >> 6); // 777 -> 888. |
| 689 | g = (g << 1) | (g >> 6); |
| 690 | r = (r << 1) | (r >> 6); |
| 691 | dst_u[0] = RGBToU(r, g, b); |
| 692 | dst_v[0] = RGBToV(r, g, b); |
| 693 | src_argb1555 += 4; |
| 694 | next_argb1555 += 4; |
| 695 | dst_u += 1; |
| 696 | dst_v += 1; |
| 697 | } |
| 698 | if (width & 1) { |
| 699 | uint8_t b0 = src_argb1555[0] & 0x1f; |
| 700 | uint8_t g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3); |
| 701 | uint8_t r0 = (src_argb1555[1] & 0x7c) >> 2; |
| 702 | uint8_t b2 = next_argb1555[0] & 0x1f; |
| 703 | uint8_t g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3); |
| 704 | uint8_t r2 = next_argb1555[1] >> 3; |
| 705 | uint8_t b = (b0 + b2); // 555 * 2 = 666. |
| 706 | uint8_t g = (g0 + g2); |
| 707 | uint8_t r = (r0 + r2); |
| 708 | b = (b << 2) | (b >> 4); // 666 -> 888. |
| 709 | g = (g << 2) | (g >> 4); |
| 710 | r = (r << 2) | (r >> 4); |
| 711 | dst_u[0] = RGBToU(r, g, b); |
| 712 | dst_v[0] = RGBToV(r, g, b); |
| 713 | } |
| 714 | } |
| 715 |
| 716 | void ARGB4444ToUVRow_C(const uint8_t* src_argb4444, |
| 717 | int src_stride_argb4444, |
| 718 | uint8_t* dst_u, |
| 719 | uint8_t* dst_v, |
| 720 | int width) { |
| 721 | const uint8_t* next_argb4444 = src_argb4444 + src_stride_argb4444; |
| 722 | int x; |
| 723 | for (x = 0; x < width - 1; x += 2) { |
| 724 | uint8_t b0 = src_argb4444[0] & 0x0f; |
| 725 | uint8_t g0 = src_argb4444[0] >> 4; |
| 726 | uint8_t r0 = src_argb4444[1] & 0x0f; |
| 727 | uint8_t b1 = src_argb4444[2] & 0x0f; |
| 728 | uint8_t g1 = src_argb4444[2] >> 4; |
| 729 | uint8_t r1 = src_argb4444[3] & 0x0f; |
| 730 | uint8_t b2 = next_argb4444[0] & 0x0f; |
| 731 | uint8_t g2 = next_argb4444[0] >> 4; |
| 732 | uint8_t r2 = next_argb4444[1] & 0x0f; |
| 733 | uint8_t b3 = next_argb4444[2] & 0x0f; |
| 734 | uint8_t g3 = next_argb4444[2] >> 4; |
| 735 | uint8_t r3 = next_argb4444[3] & 0x0f; |
| 736 | uint8_t b = (b0 + b1 + b2 + b3); // 444 * 4 = 666. |
| 737 | uint8_t g = (g0 + g1 + g2 + g3); |
| 738 | uint8_t r = (r0 + r1 + r2 + r3); |
| 739 | b = (b << 2) | (b >> 4); // 666 -> 888. |
| 740 | g = (g << 2) | (g >> 4); |
| 741 | r = (r << 2) | (r >> 4); |
| 742 | dst_u[0] = RGBToU(r, g, b); |
| 743 | dst_v[0] = RGBToV(r, g, b); |
| 744 | src_argb4444 += 4; |
| 745 | next_argb4444 += 4; |
| 746 | dst_u += 1; |
| 747 | dst_v += 1; |
| 748 | } |
| 749 | if (width & 1) { |
| 750 | uint8_t b0 = src_argb4444[0] & 0x0f; |
| 751 | uint8_t g0 = src_argb4444[0] >> 4; |
| 752 | uint8_t r0 = src_argb4444[1] & 0x0f; |
| 753 | uint8_t b2 = next_argb4444[0] & 0x0f; |
| 754 | uint8_t g2 = next_argb4444[0] >> 4; |
| 755 | uint8_t r2 = next_argb4444[1] & 0x0f; |
| 756 | uint8_t b = (b0 + b2); // 444 * 2 = 555. |
| 757 | uint8_t g = (g0 + g2); |
| 758 | uint8_t r = (r0 + r2); |
| 759 | b = (b << 3) | (b >> 2); // 555 -> 888. |
| 760 | g = (g << 3) | (g >> 2); |
| 761 | r = (r << 3) | (r >> 2); |
| 762 | dst_u[0] = RGBToU(r, g, b); |
| 763 | dst_v[0] = RGBToV(r, g, b); |
| 764 | } |
| 765 | } |
| 766 |
| 767 | void ARGBToUV444Row_C(const uint8_t* src_argb, |
| 768 | uint8_t* dst_u, |
| 769 | uint8_t* dst_v, |
| 770 | int width) { |
| 771 | int x; |
| 772 | for (x = 0; x < width; ++x) { |
| 773 | uint8_t ab = src_argb[0]; |
| 774 | uint8_t ag = src_argb[1]; |
| 775 | uint8_t ar = src_argb[2]; |
| 776 | dst_u[0] = RGBToU(ar, ag, ab); |
| 777 | dst_v[0] = RGBToV(ar, ag, ab); |
| 778 | src_argb += 4; |
| 779 | dst_u += 1; |
| 780 | dst_v += 1; |
| 781 | } |
| 782 | } |
| 783 |
| 784 | void ARGBGrayRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) { |
| 785 | int x; |
| 786 | for (x = 0; x < width; ++x) { |
| 787 | uint8_t y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]); |
| 788 | dst_argb[2] = dst_argb[1] = dst_argb[0] = y; |
| 789 | dst_argb[3] = src_argb[3]; |
| 790 | dst_argb += 4; |
| 791 | src_argb += 4; |
| 792 | } |
| 793 | } |
| 794 |
| 795 | // Convert a row of image to Sepia tone. |
| 796 | void ARGBSepiaRow_C(uint8_t* dst_argb, int width) { |
| 797 | int x; |
| 798 | for (x = 0; x < width; ++x) { |
| 799 | int b = dst_argb[0]; |
| 800 | int g = dst_argb[1]; |
| 801 | int r = dst_argb[2]; |
| 802 | int sb = (b * 17 + g * 68 + r * 35) >> 7; |
| 803 | int sg = (b * 22 + g * 88 + r * 45) >> 7; |
| 804 | int sr = (b * 24 + g * 98 + r * 50) >> 7; |
| 805 | // b does not over flow. a is preserved from original. |
| 806 | dst_argb[0] = sb; |
| 807 | dst_argb[1] = clamp255(sg); |
| 808 | dst_argb[2] = clamp255(sr); |
| 809 | dst_argb += 4; |
| 810 | } |
| 811 | } |
| 812 |
| 813 | // Apply color matrix to a row of image. Matrix is signed. |
| 814 | // TODO(fbarchard): Consider adding rounding (+32). |
| 815 | void ARGBColorMatrixRow_C(const uint8_t* src_argb, |
| 816 | uint8_t* dst_argb, |
| 817 | const int8_t* matrix_argb, |
| 818 | int width) { |
| 819 | int x; |
| 820 | for (x = 0; x < width; ++x) { |
| 821 | int b = src_argb[0]; |
| 822 | int g = src_argb[1]; |
| 823 | int r = src_argb[2]; |
| 824 | int a = src_argb[3]; |
| 825 | int sb = (b * matrix_argb[0] + g * matrix_argb[1] + r * matrix_argb[2] + |
| 826 | a * matrix_argb[3]) >> |
| 827 | 6; |
| 828 | int sg = (b * matrix_argb[4] + g * matrix_argb[5] + r * matrix_argb[6] + |
| 829 | a * matrix_argb[7]) >> |
| 830 | 6; |
| 831 | int sr = (b * matrix_argb[8] + g * matrix_argb[9] + r * matrix_argb[10] + |
| 832 | a * matrix_argb[11]) >> |
| 833 | 6; |
| 834 | int sa = (b * matrix_argb[12] + g * matrix_argb[13] + r * matrix_argb[14] + |
| 835 | a * matrix_argb[15]) >> |
| 836 | 6; |
| 837 | dst_argb[0] = Clamp(sb); |
| 838 | dst_argb[1] = Clamp(sg); |
| 839 | dst_argb[2] = Clamp(sr); |
| 840 | dst_argb[3] = Clamp(sa); |
| 841 | src_argb += 4; |
| 842 | dst_argb += 4; |
| 843 | } |
| 844 | } |
| 845 |
| 846 | // Apply color table to a row of image. |
| 847 | void ARGBColorTableRow_C(uint8_t* dst_argb, |
| 848 | const uint8_t* table_argb, |
| 849 | int width) { |
| 850 | int x; |
| 851 | for (x = 0; x < width; ++x) { |
| 852 | int b = dst_argb[0]; |
| 853 | int g = dst_argb[1]; |
| 854 | int r = dst_argb[2]; |
| 855 | int a = dst_argb[3]; |
| 856 | dst_argb[0] = table_argb[b * 4 + 0]; |
| 857 | dst_argb[1] = table_argb[g * 4 + 1]; |
| 858 | dst_argb[2] = table_argb[r * 4 + 2]; |
| 859 | dst_argb[3] = table_argb[a * 4 + 3]; |
| 860 | dst_argb += 4; |
| 861 | } |
| 862 | } |
| 863 |
| 864 | // Apply color table to a row of image. |
| 865 | void RGBColorTableRow_C(uint8_t* dst_argb, |
| 866 | const uint8_t* table_argb, |
| 867 | int width) { |
| 868 | int x; |
| 869 | for (x = 0; x < width; ++x) { |
| 870 | int b = dst_argb[0]; |
| 871 | int g = dst_argb[1]; |
| 872 | int r = dst_argb[2]; |
| 873 | dst_argb[0] = table_argb[b * 4 + 0]; |
| 874 | dst_argb[1] = table_argb[g * 4 + 1]; |
| 875 | dst_argb[2] = table_argb[r * 4 + 2]; |
| 876 | dst_argb += 4; |
| 877 | } |
| 878 | } |
| 879 |
| 880 | void ARGBQuantizeRow_C(uint8_t* dst_argb, |
| 881 | int scale, |
| 882 | int interval_size, |
| 883 | int interval_offset, |
| 884 | int width) { |
| 885 | int x; |
| 886 | for (x = 0; x < width; ++x) { |
| 887 | int b = dst_argb[0]; |
| 888 | int g = dst_argb[1]; |
| 889 | int r = dst_argb[2]; |
| 890 | dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset; |
| 891 | dst_argb[1] = (g * scale >> 16) * interval_size + interval_offset; |
| 892 | dst_argb[2] = (r * scale >> 16) * interval_size + interval_offset; |
| 893 | dst_argb += 4; |
| 894 | } |
| 895 | } |
| 896 |
| 897 | #define REPEAT8(v) (v) | ((v) << 8) |
| 898 | #define SHADE(f, v) v* f >> 24 |
| 899 |
| 900 | void ARGBShadeRow_C(const uint8_t* src_argb, |
| 901 | uint8_t* dst_argb, |
| 902 | int width, |
| 903 | uint32_t value) { |
| 904 | const uint32_t b_scale = REPEAT8(value & 0xff); |
| 905 | const uint32_t g_scale = REPEAT8((value >> 8) & 0xff); |
| 906 | const uint32_t r_scale = REPEAT8((value >> 16) & 0xff); |
| 907 | const uint32_t a_scale = REPEAT8(value >> 24); |
| 908 |
| 909 | int i; |
| 910 | for (i = 0; i < width; ++i) { |
| 911 | const uint32_t b = REPEAT8(src_argb[0]); |
| 912 | const uint32_t g = REPEAT8(src_argb[1]); |
| 913 | const uint32_t r = REPEAT8(src_argb[2]); |
| 914 | const uint32_t a = REPEAT8(src_argb[3]); |
| 915 | dst_argb[0] = SHADE(b, b_scale); |
| 916 | dst_argb[1] = SHADE(g, g_scale); |
| 917 | dst_argb[2] = SHADE(r, r_scale); |
| 918 | dst_argb[3] = SHADE(a, a_scale); |
| 919 | src_argb += 4; |
| 920 | dst_argb += 4; |
| 921 | } |
| 922 | } |
| 923 | #undef REPEAT8 |
| 924 | #undef SHADE |
| 925 |
| 926 | #define REPEAT8(v) (v) | ((v) << 8) |
| 927 | #define SHADE(f, v) v* f >> 16 |
| 928 |
| 929 | void ARGBMultiplyRow_C(const uint8_t* src_argb0, |
| 930 | const uint8_t* src_argb1, |
| 931 | uint8_t* dst_argb, |
| 932 | int width) { |
| 933 | int i; |
| 934 | for (i = 0; i < width; ++i) { |
| 935 | const uint32_t b = REPEAT8(src_argb0[0]); |
| 936 | const uint32_t g = REPEAT8(src_argb0[1]); |
| 937 | const uint32_t r = REPEAT8(src_argb0[2]); |
| 938 | const uint32_t a = REPEAT8(src_argb0[3]); |
| 939 | const uint32_t b_scale = src_argb1[0]; |
| 940 | const uint32_t g_scale = src_argb1[1]; |
| 941 | const uint32_t r_scale = src_argb1[2]; |
| 942 | const uint32_t a_scale = src_argb1[3]; |
| 943 | dst_argb[0] = SHADE(b, b_scale); |
| 944 | dst_argb[1] = SHADE(g, g_scale); |
| 945 | dst_argb[2] = SHADE(r, r_scale); |
| 946 | dst_argb[3] = SHADE(a, a_scale); |
| 947 | src_argb0 += 4; |
| 948 | src_argb1 += 4; |
| 949 | dst_argb += 4; |
| 950 | } |
| 951 | } |
| 952 | #undef REPEAT8 |
| 953 | #undef SHADE |
| 954 |
| 955 | #define SHADE(f, v) clamp255(v + f) |
| 956 |
| 957 | void ARGBAddRow_C(const uint8_t* src_argb0, |
| 958 | const uint8_t* src_argb1, |
| 959 | uint8_t* dst_argb, |
| 960 | int width) { |
| 961 | int i; |
| 962 | for (i = 0; i < width; ++i) { |
| 963 | const int b = src_argb0[0]; |
| 964 | const int g = src_argb0[1]; |
| 965 | const int r = src_argb0[2]; |
| 966 | const int a = src_argb0[3]; |
| 967 | const int b_add = src_argb1[0]; |
| 968 | const int g_add = src_argb1[1]; |
| 969 | const int r_add = src_argb1[2]; |
| 970 | const int a_add = src_argb1[3]; |
| 971 | dst_argb[0] = SHADE(b, b_add); |
| 972 | dst_argb[1] = SHADE(g, g_add); |
| 973 | dst_argb[2] = SHADE(r, r_add); |
| 974 | dst_argb[3] = SHADE(a, a_add); |
| 975 | src_argb0 += 4; |
| 976 | src_argb1 += 4; |
| 977 | dst_argb += 4; |
| 978 | } |
| 979 | } |
| 980 | #undef SHADE |
| 981 |
| 982 | #define SHADE(f, v) clamp0(f - v) |
| 983 |
| 984 | void ARGBSubtractRow_C(const uint8_t* src_argb0, |
| 985 | const uint8_t* src_argb1, |
| 986 | uint8_t* dst_argb, |
| 987 | int width) { |
| 988 | int i; |
| 989 | for (i = 0; i < width; ++i) { |
| 990 | const int b = src_argb0[0]; |
| 991 | const int g = src_argb0[1]; |
| 992 | const int r = src_argb0[2]; |
| 993 | const int a = src_argb0[3]; |
| 994 | const int b_sub = src_argb1[0]; |
| 995 | const int g_sub = src_argb1[1]; |
| 996 | const int r_sub = src_argb1[2]; |
| 997 | const int a_sub = src_argb1[3]; |
| 998 | dst_argb[0] = SHADE(b, b_sub); |
| 999 | dst_argb[1] = SHADE(g, g_sub); |
| 1000 | dst_argb[2] = SHADE(r, r_sub); |
| 1001 | dst_argb[3] = SHADE(a, a_sub); |
| 1002 | src_argb0 += 4; |
| 1003 | src_argb1 += 4; |
| 1004 | dst_argb += 4; |
| 1005 | } |
| 1006 | } |
| 1007 | #undef SHADE |
| 1008 |
| 1009 | // Sobel functions which mimics SSSE3. |
| 1010 | void SobelXRow_C(const uint8_t* src_y0, |
| 1011 | const uint8_t* src_y1, |
| 1012 | const uint8_t* src_y2, |
| 1013 | uint8_t* dst_sobelx, |
| 1014 | int width) { |
| 1015 | int i; |
| 1016 | for (i = 0; i < width; ++i) { |
| 1017 | int a = src_y0[i]; |
| 1018 | int b = src_y1[i]; |
| 1019 | int c = src_y2[i]; |
| 1020 | int a_sub = src_y0[i + 2]; |
| 1021 | int b_sub = src_y1[i + 2]; |
| 1022 | int c_sub = src_y2[i + 2]; |
| 1023 | int a_diff = a - a_sub; |
| 1024 | int b_diff = b - b_sub; |
| 1025 | int c_diff = c - c_sub; |
| 1026 | int sobel = Abs(a_diff + b_diff * 2 + c_diff); |
| 1027 | dst_sobelx[i] = (uint8_t)(clamp255(sobel)); |
| 1028 | } |
| 1029 | } |
| 1030 |
| 1031 | void SobelYRow_C(const uint8_t* src_y0, |
| 1032 | const uint8_t* src_y1, |
| 1033 | uint8_t* dst_sobely, |
| 1034 | int width) { |
| 1035 | int i; |
| 1036 | for (i = 0; i < width; ++i) { |
| 1037 | int a = src_y0[i + 0]; |
| 1038 | int b = src_y0[i + 1]; |
| 1039 | int c = src_y0[i + 2]; |
| 1040 | int a_sub = src_y1[i + 0]; |
| 1041 | int b_sub = src_y1[i + 1]; |
| 1042 | int c_sub = src_y1[i + 2]; |
| 1043 | int a_diff = a - a_sub; |
| 1044 | int b_diff = b - b_sub; |
| 1045 | int c_diff = c - c_sub; |
| 1046 | int sobel = Abs(a_diff + b_diff * 2 + c_diff); |
| 1047 | dst_sobely[i] = (uint8_t)(clamp255(sobel)); |
| 1048 | } |
| 1049 | } |
| 1050 |
| 1051 | void SobelRow_C(const uint8_t* src_sobelx, |
| 1052 | const uint8_t* src_sobely, |
| 1053 | uint8_t* dst_argb, |
| 1054 | int width) { |
| 1055 | int i; |
| 1056 | for (i = 0; i < width; ++i) { |
| 1057 | int r = src_sobelx[i]; |
| 1058 | int b = src_sobely[i]; |
| 1059 | int s = clamp255(r + b); |
| 1060 | dst_argb[0] = (uint8_t)(s); |
| 1061 | dst_argb[1] = (uint8_t)(s); |
| 1062 | dst_argb[2] = (uint8_t)(s); |
| 1063 | dst_argb[3] = (uint8_t)(255u); |
| 1064 | dst_argb += 4; |
| 1065 | } |
| 1066 | } |
| 1067 |
| 1068 | void SobelToPlaneRow_C(const uint8_t* src_sobelx, |
| 1069 | const uint8_t* src_sobely, |
| 1070 | uint8_t* dst_y, |
| 1071 | int width) { |
| 1072 | int i; |
| 1073 | for (i = 0; i < width; ++i) { |
| 1074 | int r = src_sobelx[i]; |
| 1075 | int b = src_sobely[i]; |
| 1076 | int s = clamp255(r + b); |
| 1077 | dst_y[i] = (uint8_t)(s); |
| 1078 | } |
| 1079 | } |
| 1080 |
| 1081 | void SobelXYRow_C(const uint8_t* src_sobelx, |
| 1082 | const uint8_t* src_sobely, |
| 1083 | uint8_t* dst_argb, |
| 1084 | int width) { |
| 1085 | int i; |
| 1086 | for (i = 0; i < width; ++i) { |
| 1087 | int r = src_sobelx[i]; |
| 1088 | int b = src_sobely[i]; |
| 1089 | int g = clamp255(r + b); |
| 1090 | dst_argb[0] = (uint8_t)(b); |
| 1091 | dst_argb[1] = (uint8_t)(g); |
| 1092 | dst_argb[2] = (uint8_t)(r); |
| 1093 | dst_argb[3] = (uint8_t)(255u); |
| 1094 | dst_argb += 4; |
| 1095 | } |
| 1096 | } |
| 1097 |
| 1098 | void J400ToARGBRow_C(const uint8_t* src_y, uint8_t* dst_argb, int width) { |
| 1099 | // Copy a Y to RGB. |
| 1100 | int x; |
| 1101 | for (x = 0; x < width; ++x) { |
| 1102 | uint8_t y = src_y[0]; |
| 1103 | dst_argb[2] = dst_argb[1] = dst_argb[0] = y; |
| 1104 | dst_argb[3] = 255u; |
| 1105 | dst_argb += 4; |
| 1106 | ++src_y; |
| 1107 | } |
| 1108 | } |
| 1109 |
| 1110 | // TODO(fbarchard): Unify these structures to be platform independent. |
| 1111 | // TODO(fbarchard): Generate SIMD structures from float matrix. |
| 1112 |
| 1113 | // BT.601 YUV to RGB reference |
| 1114 | // R = (Y - 16) * 1.164 - V * -1.596 |
| 1115 | // G = (Y - 16) * 1.164 - U * 0.391 - V * 0.813 |
| 1116 | // B = (Y - 16) * 1.164 - U * -2.018 |
| 1117 |
| 1118 | // Y contribution to R,G,B. Scale and bias. |
| 1119 | #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */ |
| 1120 | #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */ |
| 1121 |
| 1122 | // U and V contributions to R,G,B. |
| 1123 | #define UB -128 /* max(-128, round(-2.018 * 64)) */ |
| 1124 | #define UG 25 /* round(0.391 * 64) */ |
| 1125 | #define VG 52 /* round(0.813 * 64) */ |
| 1126 | #define VR -102 /* round(-1.596 * 64) */ |
| 1127 |
| 1128 | // Bias values to subtract 16 from Y and 128 from U and V. |
| 1129 | #define BB (UB * 128 + YGB) |
| 1130 | #define BG (UG * 128 + VG * 128 + YGB) |
| 1131 | #define BR (VR * 128 + YGB) |
| 1132 |
| 1133 | #if defined(__aarch64__) // 64 bit arm |
| 1134 | const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = { |
| 1135 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1136 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1137 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1138 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1139 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1140 | {0x0101 * YG, 0, 0, 0}}; |
| 1141 | const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = { |
| 1142 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1143 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1144 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1145 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1146 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1147 | {0x0101 * YG, 0, 0, 0}}; |
| 1148 | #elif defined(__arm__) // 32 bit arm |
| 1149 | const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = { |
| 1150 | {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1151 | {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1152 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1153 | {0x0101 * YG, 0, 0, 0}}; |
| 1154 | const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = { |
| 1155 | {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1156 | {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1157 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1158 | {0x0101 * YG, 0, 0, 0}}; |
| 1159 | #else |
| 1160 | const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = { |
| 1161 | {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, |
| 1162 | UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0}, |
| 1163 | {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, |
| 1164 | UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1165 | {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, |
| 1166 | 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR}, |
| 1167 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1168 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1169 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1170 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1171 | const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = { |
| 1172 | {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, |
| 1173 | VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0}, |
| 1174 | {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, |
| 1175 | VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1176 | {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, |
| 1177 | 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB}, |
| 1178 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1179 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1180 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1181 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1182 | #endif |
| 1183 |
| 1184 | #undef BB |
| 1185 | #undef BG |
| 1186 | #undef BR |
| 1187 | #undef YGB |
| 1188 | #undef UB |
| 1189 | #undef UG |
| 1190 | #undef VG |
| 1191 | #undef VR |
| 1192 | #undef YG |
| 1193 |
| 1194 | // JPEG YUV to RGB reference |
| 1195 | // * R = Y - V * -1.40200 |
| 1196 | // * G = Y - U * 0.34414 - V * 0.71414 |
| 1197 | // * B = Y - U * -1.77200 |
| 1198 |
| 1199 | // Y contribution to R,G,B. Scale and bias. |
| 1200 | #define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */ |
| 1201 | #define YGB 32 /* 64 / 2 */ |
| 1202 |
| 1203 | // U and V contributions to R,G,B. |
| 1204 | #define UB -113 /* round(-1.77200 * 64) */ |
| 1205 | #define UG 22 /* round(0.34414 * 64) */ |
| 1206 | #define VG 46 /* round(0.71414 * 64) */ |
| 1207 | #define VR -90 /* round(-1.40200 * 64) */ |
| 1208 |
| 1209 | // Bias values to round, and subtract 128 from U and V. |
| 1210 | #define BB (UB * 128 + YGB) |
| 1211 | #define BG (UG * 128 + VG * 128 + YGB) |
| 1212 | #define BR (VR * 128 + YGB) |
| 1213 |
| 1214 | #if defined(__aarch64__) |
| 1215 | const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = { |
| 1216 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1217 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1218 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1219 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1220 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1221 | {0x0101 * YG, 0, 0, 0}}; |
| 1222 | const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = { |
| 1223 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1224 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1225 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1226 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1227 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1228 | {0x0101 * YG, 0, 0, 0}}; |
| 1229 | #elif defined(__arm__) |
| 1230 | const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = { |
| 1231 | {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1232 | {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1233 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1234 | {0x0101 * YG, 0, 0, 0}}; |
| 1235 | const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = { |
| 1236 | {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1237 | {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1238 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1239 | {0x0101 * YG, 0, 0, 0}}; |
| 1240 | #else |
| 1241 | const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = { |
| 1242 | {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, |
| 1243 | UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0}, |
| 1244 | {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, |
| 1245 | UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1246 | {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, |
| 1247 | 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR}, |
| 1248 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1249 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1250 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1251 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1252 | const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = { |
| 1253 | {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, |
| 1254 | VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0}, |
| 1255 | {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, |
| 1256 | VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1257 | {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, |
| 1258 | 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB}, |
| 1259 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1260 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1261 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1262 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1263 | #endif |
| 1264 |
| 1265 | #undef BB |
| 1266 | #undef BG |
| 1267 | #undef BR |
| 1268 | #undef YGB |
| 1269 | #undef UB |
| 1270 | #undef UG |
| 1271 | #undef VG |
| 1272 | #undef VR |
| 1273 | #undef YG |
| 1274 |
| 1275 | // BT.709 YUV to RGB reference |
| 1276 | // R = (Y - 16) * 1.164 - V * -1.793 |
| 1277 | // G = (Y - 16) * 1.164 - U * 0.213 - V * 0.533 |
| 1278 | // B = (Y - 16) * 1.164 - U * -2.112 |
| 1279 | // See also http://www.equasys.de/colorconversion.html |
| 1280 |
| 1281 | // Y contribution to R,G,B. Scale and bias. |
| 1282 | #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */ |
| 1283 | #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */ |
| 1284 |
| 1285 | // TODO(fbarchard): Find way to express 2.112 instead of 2.0. |
| 1286 | // U and V contributions to R,G,B. |
| 1287 | #define UB -128 /* max(-128, round(-2.112 * 64)) */ |
| 1288 | #define UG 14 /* round(0.213 * 64) */ |
| 1289 | #define VG 34 /* round(0.533 * 64) */ |
| 1290 | #define VR -115 /* round(-1.793 * 64) */ |
| 1291 |
| 1292 | // Bias values to round, and subtract 128 from U and V. |
| 1293 | #define BB (UB * 128 + YGB) |
| 1294 | #define BG (UG * 128 + VG * 128 + YGB) |
| 1295 | #define BR (VR * 128 + YGB) |
| 1296 |
| 1297 | #if defined(__aarch64__) |
| 1298 | const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = { |
| 1299 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1300 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1301 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1302 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1303 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1304 | {0x0101 * YG, 0, 0, 0}}; |
| 1305 | const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = { |
| 1306 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1307 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1308 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1309 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1310 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1311 | {0x0101 * YG, 0, 0, 0}}; |
| 1312 | #elif defined(__arm__) |
| 1313 | const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = { |
| 1314 | {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1315 | {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1316 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1317 | {0x0101 * YG, 0, 0, 0}}; |
| 1318 | const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = { |
| 1319 | {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1320 | {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1321 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1322 | {0x0101 * YG, 0, 0, 0}}; |
| 1323 | #else |
| 1324 | const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = { |
| 1325 | {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, |
| 1326 | UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0}, |
| 1327 | {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, |
| 1328 | UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1329 | {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, |
| 1330 | 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR}, |
| 1331 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1332 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1333 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1334 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1335 | const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = { |
| 1336 | {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, |
| 1337 | VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0}, |
| 1338 | {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, |
| 1339 | VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1340 | {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, |
| 1341 | 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB}, |
| 1342 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1343 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1344 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1345 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1346 | #endif |
| 1347 |
| 1348 | #undef BB |
| 1349 | #undef BG |
| 1350 | #undef BR |
| 1351 | #undef YGB |
| 1352 | #undef UB |
| 1353 | #undef UG |
| 1354 | #undef VG |
| 1355 | #undef VR |
| 1356 | #undef YG |
| 1357 |
| 1358 | // BT.2020 YUV to RGB reference |
| 1359 | // R = (Y - 16) * 1.164384 - V * -1.67867 |
| 1360 | // G = (Y - 16) * 1.164384 - U * 0.187326 - V * -0.65042 |
| 1361 | // B = (Y - 16) * 1.164384 - U * -2.14177 |
| 1362 |
| 1363 | // Y contribution to R,G,B. Scale and bias. |
| 1364 | #define YG 19003 /* round(1.164384 * 64 * 256 * 256 / 257) */ |
| 1365 | #define YGB -1160 /* 1.164384 * 64 * -16 + 64 / 2 */ |
| 1366 |
| 1367 | // TODO(fbarchard): Improve accuracy; the B channel is off by 7%. |
| 1368 | #define UB -128 /* max(-128, round(-2.142 * 64)) */ |
| 1369 | #define UG 12 /* round(0.187326 * 64) */ |
| 1370 | #define VG 42 /* round(0.65042 * 64) */ |
| 1371 | #define VR -107 /* round(-1.67867 * 64) */ |
| 1372 |
| 1373 | // Bias values to round, and subtract 128 from U and V. |
| 1374 | #define BB (UB * 128 + YGB) |
| 1375 | #define BG (UG * 128 + VG * 128 + YGB) |
| 1376 | #define BR (VR * 128 + YGB) |
| 1377 |
| 1378 | #if defined(__aarch64__) |
| 1379 | const struct YuvConstants SIMD_ALIGNED(kYuv2020Constants) = { |
| 1380 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1381 | {-UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR}, |
| 1382 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1383 | {UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1384 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1385 | {0x0101 * YG, 0, 0, 0}}; |
| 1386 | const struct YuvConstants SIMD_ALIGNED(kYvu2020Constants) = { |
| 1387 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1388 | {-VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB}, |
| 1389 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1390 | {VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1391 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1392 | {0x0101 * YG, 0, 0, 0}}; |
| 1393 | #elif defined(__arm__) |
| 1394 | const struct YuvConstants SIMD_ALIGNED(kYuv2020Constants) = { |
| 1395 | {-UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1396 | {UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1397 | {BB, BG, BR, 0, 0, 0, 0, 0}, |
| 1398 | {0x0101 * YG, 0, 0, 0}}; |
| 1399 | const struct YuvConstants SIMD_ALIGNED(kYvu2020Constants) = { |
| 1400 | {-VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1401 | {VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 1402 | {BR, BG, BB, 0, 0, 0, 0, 0}, |
| 1403 | {0x0101 * YG, 0, 0, 0}}; |
| 1404 | #else |
| 1405 | const struct YuvConstants SIMD_ALIGNED(kYuv2020Constants) = { |
| 1406 | {UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, |
| 1407 | UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0}, |
| 1408 | {UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, |
| 1409 | UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG}, |
| 1410 | {0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, |
| 1411 | 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR}, |
| 1412 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1413 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1414 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1415 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1416 | const struct YuvConstants SIMD_ALIGNED(kYvu2020Constants) = { |
| 1417 | {VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, |
| 1418 | VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0}, |
| 1419 | {VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, |
| 1420 | VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG}, |
| 1421 | {0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, |
| 1422 | 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB}, |
| 1423 | {BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR}, |
| 1424 | {BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG}, |
| 1425 | {BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB}, |
| 1426 | {YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG}}; |
| 1427 | #endif |
| 1428 |
| 1429 | #undef BB |
| 1430 | #undef BG |
| 1431 | #undef BR |
| 1432 | #undef YGB |
| 1433 | #undef UB |
| 1434 | #undef UG |
| 1435 | #undef VG |
| 1436 | #undef VR |
| 1437 | #undef YG |
| 1438 |
| 1439 | // C reference code that mimics the YUV assembly. |
| 1440 | // Reads 8 bit YUV and leaves result as 16 bit. |
| 1441 |
| 1442 | static __inline void YuvPixel(uint8_t y, |
| 1443 | uint8_t u, |
| 1444 | uint8_t v, |
| 1445 | uint8_t* b, |
| 1446 | uint8_t* g, |
| 1447 | uint8_t* r, |
| 1448 | const struct YuvConstants* yuvconstants) { |
| 1449 | #if defined(__aarch64__) |
| 1450 | int ub = -yuvconstants->kUVToRB[0]; |
| 1451 | int ug = yuvconstants->kUVToG[0]; |
| 1452 | int vg = yuvconstants->kUVToG[1]; |
| 1453 | int vr = -yuvconstants->kUVToRB[1]; |
| 1454 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1455 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1456 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1457 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1458 | #elif defined(__arm__) |
| 1459 | int ub = -yuvconstants->kUVToRB[0]; |
| 1460 | int ug = yuvconstants->kUVToG[0]; |
| 1461 | int vg = yuvconstants->kUVToG[4]; |
| 1462 | int vr = -yuvconstants->kUVToRB[4]; |
| 1463 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1464 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1465 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1466 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1467 | #else |
| 1468 | int ub = yuvconstants->kUVToB[0]; |
| 1469 | int ug = yuvconstants->kUVToG[0]; |
| 1470 | int vg = yuvconstants->kUVToG[1]; |
| 1471 | int vr = yuvconstants->kUVToR[1]; |
| 1472 | int bb = yuvconstants->kUVBiasB[0]; |
| 1473 | int bg = yuvconstants->kUVBiasG[0]; |
| 1474 | int br = yuvconstants->kUVBiasR[0]; |
| 1475 | int yg = yuvconstants->kYToRgb[0]; |
| 1476 | #endif |
| 1477 |
| 1478 | uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16; |
| 1479 | *b = Clamp((int32_t)(-(u * ub) + y1 + bb) >> 6); |
| 1480 | *g = Clamp((int32_t)(-(u * ug + v * vg) + y1 + bg) >> 6); |
| 1481 | *r = Clamp((int32_t)(-(v * vr) + y1 + br) >> 6); |
| 1482 | } |
| 1483 |
| 1484 | // Reads 8 bit YUV and leaves result as 16 bit. |
| 1485 | static __inline void YuvPixel8_16(uint8_t y, |
| 1486 | uint8_t u, |
| 1487 | uint8_t v, |
| 1488 | int* b, |
| 1489 | int* g, |
| 1490 | int* r, |
| 1491 | const struct YuvConstants* yuvconstants) { |
| 1492 | #if defined(__aarch64__) |
| 1493 | int ub = -yuvconstants->kUVToRB[0]; |
| 1494 | int ug = yuvconstants->kUVToG[0]; |
| 1495 | int vg = yuvconstants->kUVToG[1]; |
| 1496 | int vr = -yuvconstants->kUVToRB[1]; |
| 1497 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1498 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1499 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1500 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1501 | #elif defined(__arm__) |
| 1502 | int ub = -yuvconstants->kUVToRB[0]; |
| 1503 | int ug = yuvconstants->kUVToG[0]; |
| 1504 | int vg = yuvconstants->kUVToG[4]; |
| 1505 | int vr = -yuvconstants->kUVToRB[4]; |
| 1506 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1507 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1508 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1509 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1510 | #else |
| 1511 | int ub = yuvconstants->kUVToB[0]; |
| 1512 | int ug = yuvconstants->kUVToG[0]; |
| 1513 | int vg = yuvconstants->kUVToG[1]; |
| 1514 | int vr = yuvconstants->kUVToR[1]; |
| 1515 | int bb = yuvconstants->kUVBiasB[0]; |
| 1516 | int bg = yuvconstants->kUVBiasG[0]; |
| 1517 | int br = yuvconstants->kUVBiasR[0]; |
| 1518 | int yg = yuvconstants->kYToRgb[0]; |
| 1519 | #endif |
| 1520 |
| 1521 | uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16; |
| 1522 | *b = (int)(-(u * ub) + y1 + bb); |
| 1523 | *g = (int)(-(u * ug + v * vg) + y1 + bg); |
| 1524 | *r = (int)(-(v * vr) + y1 + br); |
| 1525 | } |
| 1526 |
| 1527 | // C reference code that mimics the YUV 16 bit assembly. |
| 1528 | // Reads 10 bit YUV and leaves result as 16 bit. |
| 1529 | static __inline void YuvPixel16(int16_t y, |
| 1530 | int16_t u, |
| 1531 | int16_t v, |
| 1532 | int* b, |
| 1533 | int* g, |
| 1534 | int* r, |
| 1535 | const struct YuvConstants* yuvconstants) { |
| 1536 | #if defined(__aarch64__) |
| 1537 | int ub = -yuvconstants->kUVToRB[0]; |
| 1538 | int ug = yuvconstants->kUVToG[0]; |
| 1539 | int vg = yuvconstants->kUVToG[1]; |
| 1540 | int vr = -yuvconstants->kUVToRB[1]; |
| 1541 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1542 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1543 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1544 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1545 | #elif defined(__arm__) |
| 1546 | int ub = -yuvconstants->kUVToRB[0]; |
| 1547 | int ug = yuvconstants->kUVToG[0]; |
| 1548 | int vg = yuvconstants->kUVToG[4]; |
| 1549 | int vr = -yuvconstants->kUVToRB[4]; |
| 1550 | int bb = yuvconstants->kUVBiasBGR[0]; |
| 1551 | int bg = yuvconstants->kUVBiasBGR[1]; |
| 1552 | int br = yuvconstants->kUVBiasBGR[2]; |
| 1553 | int yg = yuvconstants->kYToRgb[0] / 0x0101; |
| 1554 | #else |
| 1555 | int ub = yuvconstants->kUVToB[0]; |
| 1556 | int ug = yuvconstants->kUVToG[0]; |
| 1557 | int vg = yuvconstants->kUVToG[1]; |
| 1558 | int vr = yuvconstants->kUVToR[1]; |
| 1559 | int bb = yuvconstants->kUVBiasB[0]; |
| 1560 | int bg = yuvconstants->kUVBiasG[0]; |
| 1561 | int br = yuvconstants->kUVBiasR[0]; |
| 1562 | int yg = yuvconstants->kYToRgb[0]; |
| 1563 | #endif |
| 1564 |
| 1565 | uint32_t y1 = (uint32_t)((y << 6) * yg) >> 16; |
| 1566 | u = clamp255(u >> 2); |
| 1567 | v = clamp255(v >> 2); |
| 1568 | *b = (int)(-(u * ub) + y1 + bb); |
| 1569 | *g = (int)(-(u * ug + v * vg) + y1 + bg); |
| 1570 | *r = (int)(-(v * vr) + y1 + br); |
| 1571 | } |
| 1572 |
| 1573 | // C reference code that mimics the YUV 10 bit assembly. |
| 1574 | // Reads 10 bit YUV and clamps down to 8 bit RGB. |
| 1575 | static __inline void YuvPixel10(uint16_t y, |
| 1576 | uint16_t u, |
| 1577 | uint16_t v, |
| 1578 | uint8_t* b, |
| 1579 | uint8_t* g, |
| 1580 | uint8_t* r, |
| 1581 | const struct YuvConstants* yuvconstants) { |
| 1582 | int b16; |
| 1583 | int g16; |
| 1584 | int r16; |
| 1585 | YuvPixel16(y, u, v, &b16, &g16, &r16, yuvconstants); |
| 1586 | *b = Clamp(b16 >> 6); |
| 1587 | *g = Clamp(g16 >> 6); |
| 1588 | *r = Clamp(r16 >> 6); |
| 1589 | } |
| 1590 |
| 1591 | // Y contribution to R,G,B. Scale and bias. |
| 1592 | #define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */ |
| 1593 | #define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */ |
| 1594 |
| 1595 | // C reference code that mimics the YUV assembly. |
| 1596 | static __inline void YPixel(uint8_t y, uint8_t* b, uint8_t* g, uint8_t* r) { |
| 1597 | uint32_t y1 = (uint32_t)(y * 0x0101 * YG) >> 16; |
| 1598 | *b = Clamp((int32_t)(y1 + YGB) >> 6); |
| 1599 | *g = Clamp((int32_t)(y1 + YGB) >> 6); |
| 1600 | *r = Clamp((int32_t)(y1 + YGB) >> 6); |
| 1601 | } |
| 1602 |
| 1603 | #undef YG |
| 1604 | #undef YGB |
| 1605 |
| 1606 | #if !defined(LIBYUV_DISABLE_NEON) && \ |
| 1607 | (defined(__ARM_NEON__) || defined(__aarch64__) || defined(LIBYUV_NEON)) |
| 1608 | // C mimic assembly. |
| 1609 | // TODO(fbarchard): Remove subsampling from Neon. |
| 1610 | void I444ToARGBRow_C(const uint8_t* src_y, |
| 1611 | const uint8_t* src_u, |
| 1612 | const uint8_t* src_v, |
| 1613 | uint8_t* rgb_buf, |
| 1614 | const struct YuvConstants* yuvconstants, |
| 1615 | int width) { |
| 1616 | int x; |
| 1617 | for (x = 0; x < width - 1; x += 2) { |
| 1618 | uint8_t u = (src_u[0] + src_u[1] + 1) >> 1; |
| 1619 | uint8_t v = (src_v[0] + src_v[1] + 1) >> 1; |
| 1620 | YuvPixel(src_y[0], u, v, rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, |
| 1621 | yuvconstants); |
| 1622 | rgb_buf[3] = 255; |
| 1623 | YuvPixel(src_y[1], u, v, rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, |
| 1624 | yuvconstants); |
| 1625 | rgb_buf[7] = 255; |
| 1626 | src_y += 2; |
| 1627 | src_u += 2; |
| 1628 | src_v += 2; |
| 1629 | rgb_buf += 8; // Advance 2 pixels. |
| 1630 | } |
| 1631 | if (width & 1) { |
| 1632 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1633 | rgb_buf + 2, yuvconstants); |
| 1634 | rgb_buf[3] = 255; |
| 1635 | } |
| 1636 | } |
| 1637 | #else |
| 1638 | void I444ToARGBRow_C(const uint8_t* src_y, |
| 1639 | const uint8_t* src_u, |
| 1640 | const uint8_t* src_v, |
| 1641 | uint8_t* rgb_buf, |
| 1642 | const struct YuvConstants* yuvconstants, |
| 1643 | int width) { |
| 1644 | int x; |
| 1645 | for (x = 0; x < width; ++x) { |
| 1646 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1647 | rgb_buf + 2, yuvconstants); |
| 1648 | rgb_buf[3] = 255; |
| 1649 | src_y += 1; |
| 1650 | src_u += 1; |
| 1651 | src_v += 1; |
| 1652 | rgb_buf += 4; // Advance 1 pixel. |
| 1653 | } |
| 1654 | } |
| 1655 | #endif |
| 1656 |
| 1657 | // Also used for 420 |
| 1658 | void I422ToARGBRow_C(const uint8_t* src_y, |
| 1659 | const uint8_t* src_u, |
| 1660 | const uint8_t* src_v, |
| 1661 | uint8_t* rgb_buf, |
| 1662 | const struct YuvConstants* yuvconstants, |
| 1663 | int width) { |
| 1664 | int x; |
| 1665 | for (x = 0; x < width - 1; x += 2) { |
| 1666 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1667 | rgb_buf + 2, yuvconstants); |
| 1668 | rgb_buf[3] = 255; |
| 1669 | YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5, |
| 1670 | rgb_buf + 6, yuvconstants); |
| 1671 | rgb_buf[7] = 255; |
| 1672 | src_y += 2; |
| 1673 | src_u += 1; |
| 1674 | src_v += 1; |
| 1675 | rgb_buf += 8; // Advance 2 pixels. |
| 1676 | } |
| 1677 | if (width & 1) { |
| 1678 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1679 | rgb_buf + 2, yuvconstants); |
| 1680 | rgb_buf[3] = 255; |
| 1681 | } |
| 1682 | } |
| 1683 |
| 1684 | // 10 bit YUV to ARGB |
| 1685 | void I210ToARGBRow_C(const uint16_t* src_y, |
| 1686 | const uint16_t* src_u, |
| 1687 | const uint16_t* src_v, |
| 1688 | uint8_t* rgb_buf, |
| 1689 | const struct YuvConstants* yuvconstants, |
| 1690 | int width) { |
| 1691 | int x; |
| 1692 | for (x = 0; x < width - 1; x += 2) { |
| 1693 | YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1694 | rgb_buf + 2, yuvconstants); |
| 1695 | rgb_buf[3] = 255; |
| 1696 | YuvPixel10(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5, |
| 1697 | rgb_buf + 6, yuvconstants); |
| 1698 | rgb_buf[7] = 255; |
| 1699 | src_y += 2; |
| 1700 | src_u += 1; |
| 1701 | src_v += 1; |
| 1702 | rgb_buf += 8; // Advance 2 pixels. |
| 1703 | } |
| 1704 | if (width & 1) { |
| 1705 | YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1706 | rgb_buf + 2, yuvconstants); |
| 1707 | rgb_buf[3] = 255; |
| 1708 | } |
| 1709 | } |
| 1710 |
| 1711 | static void StoreAR30(uint8_t* rgb_buf, int b, int g, int r) { |
| 1712 | uint32_t ar30; |
| 1713 | b = b >> 4; // convert 10.6 to 10 bit. |
| 1714 | g = g >> 4; |
| 1715 | r = r >> 4; |
| 1716 | b = Clamp10(b); |
| 1717 | g = Clamp10(g); |
| 1718 | r = Clamp10(r); |
| 1719 | ar30 = b | ((uint32_t)g << 10) | ((uint32_t)r << 20) | 0xc0000000; |
| 1720 | (*(uint32_t*)rgb_buf) = ar30; |
| 1721 | } |
| 1722 |
| 1723 | // 10 bit YUV to 10 bit AR30 |
| 1724 | void I210ToAR30Row_C(const uint16_t* src_y, |
| 1725 | const uint16_t* src_u, |
| 1726 | const uint16_t* src_v, |
| 1727 | uint8_t* rgb_buf, |
| 1728 | const struct YuvConstants* yuvconstants, |
| 1729 | int width) { |
| 1730 | int x; |
| 1731 | int b; |
| 1732 | int g; |
| 1733 | int r; |
| 1734 | for (x = 0; x < width - 1; x += 2) { |
| 1735 | YuvPixel16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1736 | StoreAR30(rgb_buf, b, g, r); |
| 1737 | YuvPixel16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1738 | StoreAR30(rgb_buf + 4, b, g, r); |
| 1739 | src_y += 2; |
| 1740 | src_u += 1; |
| 1741 | src_v += 1; |
| 1742 | rgb_buf += 8; // Advance 2 pixels. |
| 1743 | } |
| 1744 | if (width & 1) { |
| 1745 | YuvPixel16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1746 | StoreAR30(rgb_buf, b, g, r); |
| 1747 | } |
| 1748 | } |
| 1749 |
| 1750 | // 8 bit YUV to 10 bit AR30 |
| 1751 | // Uses same code as 10 bit YUV bit shifts the 8 bit values up to 10 bits. |
| 1752 | void I422ToAR30Row_C(const uint8_t* src_y, |
| 1753 | const uint8_t* src_u, |
| 1754 | const uint8_t* src_v, |
| 1755 | uint8_t* rgb_buf, |
| 1756 | const struct YuvConstants* yuvconstants, |
| 1757 | int width) { |
| 1758 | int x; |
| 1759 | int b; |
| 1760 | int g; |
| 1761 | int r; |
| 1762 | for (x = 0; x < width - 1; x += 2) { |
| 1763 | YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1764 | StoreAR30(rgb_buf, b, g, r); |
| 1765 | YuvPixel8_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1766 | StoreAR30(rgb_buf + 4, b, g, r); |
| 1767 | src_y += 2; |
| 1768 | src_u += 1; |
| 1769 | src_v += 1; |
| 1770 | rgb_buf += 8; // Advance 2 pixels. |
| 1771 | } |
| 1772 | if (width & 1) { |
| 1773 | YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants); |
| 1774 | StoreAR30(rgb_buf, b, g, r); |
| 1775 | } |
| 1776 | } |
| 1777 |
| 1778 | void I422AlphaToARGBRow_C(const uint8_t* src_y, |
| 1779 | const uint8_t* src_u, |
| 1780 | const uint8_t* src_v, |
| 1781 | const uint8_t* src_a, |
| 1782 | uint8_t* rgb_buf, |
| 1783 | const struct YuvConstants* yuvconstants, |
| 1784 | int width) { |
| 1785 | int x; |
| 1786 | for (x = 0; x < width - 1; x += 2) { |
| 1787 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1788 | rgb_buf + 2, yuvconstants); |
| 1789 | rgb_buf[3] = src_a[0]; |
| 1790 | YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5, |
| 1791 | rgb_buf + 6, yuvconstants); |
| 1792 | rgb_buf[7] = src_a[1]; |
| 1793 | src_y += 2; |
| 1794 | src_u += 1; |
| 1795 | src_v += 1; |
| 1796 | src_a += 2; |
| 1797 | rgb_buf += 8; // Advance 2 pixels. |
| 1798 | } |
| 1799 | if (width & 1) { |
| 1800 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1801 | rgb_buf + 2, yuvconstants); |
| 1802 | rgb_buf[3] = src_a[0]; |
| 1803 | } |
| 1804 | } |
| 1805 |
| 1806 | void I422ToRGB24Row_C(const uint8_t* src_y, |
| 1807 | const uint8_t* src_u, |
| 1808 | const uint8_t* src_v, |
| 1809 | uint8_t* rgb_buf, |
| 1810 | const struct YuvConstants* yuvconstants, |
| 1811 | int width) { |
| 1812 | int x; |
| 1813 | for (x = 0; x < width - 1; x += 2) { |
| 1814 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1815 | rgb_buf + 2, yuvconstants); |
| 1816 | YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 3, rgb_buf + 4, |
| 1817 | rgb_buf + 5, yuvconstants); |
| 1818 | src_y += 2; |
| 1819 | src_u += 1; |
| 1820 | src_v += 1; |
| 1821 | rgb_buf += 6; // Advance 2 pixels. |
| 1822 | } |
| 1823 | if (width & 1) { |
| 1824 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1, |
| 1825 | rgb_buf + 2, yuvconstants); |
| 1826 | } |
| 1827 | } |
| 1828 |
| 1829 | void I422ToARGB4444Row_C(const uint8_t* src_y, |
| 1830 | const uint8_t* src_u, |
| 1831 | const uint8_t* src_v, |
| 1832 | uint8_t* dst_argb4444, |
| 1833 | const struct YuvConstants* yuvconstants, |
| 1834 | int width) { |
| 1835 | uint8_t b0; |
| 1836 | uint8_t g0; |
| 1837 | uint8_t r0; |
| 1838 | uint8_t b1; |
| 1839 | uint8_t g1; |
| 1840 | uint8_t r1; |
| 1841 | int x; |
| 1842 | for (x = 0; x < width - 1; x += 2) { |
| 1843 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1844 | YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants); |
| 1845 | b0 = b0 >> 4; |
| 1846 | g0 = g0 >> 4; |
| 1847 | r0 = r0 >> 4; |
| 1848 | b1 = b1 >> 4; |
| 1849 | g1 = g1 >> 4; |
| 1850 | r1 = r1 >> 4; |
| 1851 | *(uint32_t*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | (b1 << 16) | |
| 1852 | (g1 << 20) | (r1 << 24) | 0xf000f000; |
| 1853 | src_y += 2; |
| 1854 | src_u += 1; |
| 1855 | src_v += 1; |
| 1856 | dst_argb4444 += 4; // Advance 2 pixels. |
| 1857 | } |
| 1858 | if (width & 1) { |
| 1859 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1860 | b0 = b0 >> 4; |
| 1861 | g0 = g0 >> 4; |
| 1862 | r0 = r0 >> 4; |
| 1863 | *(uint16_t*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | 0xf000; |
| 1864 | } |
| 1865 | } |
| 1866 |
| 1867 | void I422ToARGB1555Row_C(const uint8_t* src_y, |
| 1868 | const uint8_t* src_u, |
| 1869 | const uint8_t* src_v, |
| 1870 | uint8_t* dst_argb1555, |
| 1871 | const struct YuvConstants* yuvconstants, |
| 1872 | int width) { |
| 1873 | uint8_t b0; |
| 1874 | uint8_t g0; |
| 1875 | uint8_t r0; |
| 1876 | uint8_t b1; |
| 1877 | uint8_t g1; |
| 1878 | uint8_t r1; |
| 1879 | int x; |
| 1880 | for (x = 0; x < width - 1; x += 2) { |
| 1881 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1882 | YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants); |
| 1883 | b0 = b0 >> 3; |
| 1884 | g0 = g0 >> 3; |
| 1885 | r0 = r0 >> 3; |
| 1886 | b1 = b1 >> 3; |
| 1887 | g1 = g1 >> 3; |
| 1888 | r1 = r1 >> 3; |
| 1889 | *(uint32_t*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | (b1 << 16) | |
| 1890 | (g1 << 21) | (r1 << 26) | 0x80008000; |
| 1891 | src_y += 2; |
| 1892 | src_u += 1; |
| 1893 | src_v += 1; |
| 1894 | dst_argb1555 += 4; // Advance 2 pixels. |
| 1895 | } |
| 1896 | if (width & 1) { |
| 1897 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1898 | b0 = b0 >> 3; |
| 1899 | g0 = g0 >> 3; |
| 1900 | r0 = r0 >> 3; |
| 1901 | *(uint16_t*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | 0x8000; |
| 1902 | } |
| 1903 | } |
| 1904 |
| 1905 | void I422ToRGB565Row_C(const uint8_t* src_y, |
| 1906 | const uint8_t* src_u, |
| 1907 | const uint8_t* src_v, |
| 1908 | uint8_t* dst_rgb565, |
| 1909 | const struct YuvConstants* yuvconstants, |
| 1910 | int width) { |
| 1911 | uint8_t b0; |
| 1912 | uint8_t g0; |
| 1913 | uint8_t r0; |
| 1914 | uint8_t b1; |
| 1915 | uint8_t g1; |
| 1916 | uint8_t r1; |
| 1917 | int x; |
| 1918 | for (x = 0; x < width - 1; x += 2) { |
| 1919 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1920 | YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants); |
| 1921 | b0 = b0 >> 3; |
| 1922 | g0 = g0 >> 2; |
| 1923 | r0 = r0 >> 3; |
| 1924 | b1 = b1 >> 3; |
| 1925 | g1 = g1 >> 2; |
| 1926 | r1 = r1 >> 3; |
| 1927 | *(uint32_t*)(dst_rgb565) = |
| 1928 | b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27); |
| 1929 | src_y += 2; |
| 1930 | src_u += 1; |
| 1931 | src_v += 1; |
| 1932 | dst_rgb565 += 4; // Advance 2 pixels. |
| 1933 | } |
| 1934 | if (width & 1) { |
| 1935 | YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants); |
| 1936 | b0 = b0 >> 3; |
| 1937 | g0 = g0 >> 2; |
| 1938 | r0 = r0 >> 3; |
| 1939 | *(uint16_t*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11); |
| 1940 | } |
| 1941 | } |
| 1942 |
| 1943 | void NV12ToARGBRow_C(const uint8_t* src_y, |
| 1944 | const uint8_t* src_uv, |
| 1945 | uint8_t* rgb_buf, |
| 1946 | const struct YuvConstants* yuvconstants, |
| 1947 | int width) { |
| 1948 | int x; |
| 1949 | for (x = 0; x < width - 1; x += 2) { |
| 1950 | YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1, |
| 1951 | rgb_buf + 2, yuvconstants); |
| 1952 | rgb_buf[3] = 255; |
| 1953 | YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 4, rgb_buf + 5, |
| 1954 | rgb_buf + 6, yuvconstants); |
| 1955 | rgb_buf[7] = 255; |
| 1956 | src_y += 2; |
| 1957 | src_uv += 2; |
| 1958 | rgb_buf += 8; // Advance 2 pixels. |
| 1959 | } |
| 1960 | if (width & 1) { |
| 1961 | YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1, |
| 1962 | rgb_buf + 2, yuvconstants); |
| 1963 | rgb_buf[3] = 255; |
| 1964 | } |
| 1965 | } |
| 1966 |
| 1967 | void NV21ToARGBRow_C(const uint8_t* src_y, |
| 1968 | const uint8_t* src_vu, |
| 1969 | uint8_t* rgb_buf, |
| 1970 | const struct YuvConstants* yuvconstants, |
| 1971 | int width) { |
| 1972 | int x; |
| 1973 | for (x = 0; x < width - 1; x += 2) { |
| 1974 | YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, |
| 1975 | rgb_buf + 2, yuvconstants); |
| 1976 | rgb_buf[3] = 255; |
| 1977 | YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5, |
| 1978 | rgb_buf + 6, yuvconstants); |
| 1979 | rgb_buf[7] = 255; |
| 1980 | src_y += 2; |
| 1981 | src_vu += 2; |
| 1982 | rgb_buf += 8; // Advance 2 pixels. |
| 1983 | } |
| 1984 | if (width & 1) { |
| 1985 | YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, |
| 1986 | rgb_buf + 2, yuvconstants); |
| 1987 | rgb_buf[3] = 255; |
| 1988 | } |
| 1989 | } |
| 1990 |
| 1991 | void NV12ToRGB24Row_C(const uint8_t* src_y, |
| 1992 | const uint8_t* src_uv, |
| 1993 | uint8_t* rgb_buf, |
| 1994 | const struct YuvConstants* yuvconstants, |
| 1995 | int width) { |
| 1996 | int x; |
| 1997 | for (x = 0; x < width - 1; x += 2) { |
| 1998 | YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1, |
| 1999 | rgb_buf + 2, yuvconstants); |
| 2000 | YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 3, rgb_buf + 4, |
| 2001 | rgb_buf + 5, yuvconstants); |
| 2002 | src_y += 2; |
| 2003 | src_uv += 2; |
| 2004 | rgb_buf += 6; // Advance 2 pixels. |
| 2005 | } |
| 2006 | if (width & 1) { |
| 2007 | YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1, |
| 2008 | rgb_buf + 2, yuvconstants); |
| 2009 | } |
| 2010 | } |
| 2011 |
| 2012 | void NV21ToRGB24Row_C(const uint8_t* src_y, |
| 2013 | const uint8_t* src_vu, |
| 2014 | uint8_t* rgb_buf, |
| 2015 | const struct YuvConstants* yuvconstants, |
| 2016 | int width) { |
| 2017 | int x; |
| 2018 | for (x = 0; x < width - 1; x += 2) { |
| 2019 | YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, |
| 2020 | rgb_buf + 2, yuvconstants); |
| 2021 | YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 3, rgb_buf + 4, |
| 2022 | rgb_buf + 5, yuvconstants); |
| 2023 | src_y += 2; |
| 2024 | src_vu += 2; |
| 2025 | rgb_buf += 6; // Advance 2 pixels. |
| 2026 | } |
| 2027 | if (width & 1) { |
| 2028 | YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1, |
| 2029 | rgb_buf + 2, yuvconstants); |
| 2030 | } |
| 2031 | } |
| 2032 |
| 2033 | void NV12ToRGB565Row_C(const uint8_t* src_y, |
| 2034 | const uint8_t* src_uv, |
| 2035 | uint8_t* dst_rgb565, |
| 2036 | const struct YuvConstants* yuvconstants, |
| 2037 | int width) { |
| 2038 | uint8_t b0; |
| 2039 | uint8_t g0; |
| 2040 | uint8_t r0; |
| 2041 | uint8_t b1; |
| 2042 | uint8_t g1; |
| 2043 | uint8_t r1; |
| 2044 | int x; |
| 2045 | for (x = 0; x < width - 1; x += 2) { |
| 2046 | YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants); |
| 2047 | YuvPixel(src_y[1], src_uv[0], src_uv[1], &b1, &g1, &r1, yuvconstants); |
| 2048 | b0 = b0 >> 3; |
| 2049 | g0 = g0 >> 2; |
| 2050 | r0 = r0 >> 3; |
| 2051 | b1 = b1 >> 3; |
| 2052 | g1 = g1 >> 2; |
| 2053 | r1 = r1 >> 3; |
| 2054 | *(uint32_t*)(dst_rgb565) = |
| 2055 | b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27); |
| 2056 | src_y += 2; |
| 2057 | src_uv += 2; |
| 2058 | dst_rgb565 += 4; // Advance 2 pixels. |
| 2059 | } |
| 2060 | if (width & 1) { |
| 2061 | YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants); |
| 2062 | b0 = b0 >> 3; |
| 2063 | g0 = g0 >> 2; |
| 2064 | r0 = r0 >> 3; |
| 2065 | *(uint16_t*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11); |
| 2066 | } |
| 2067 | } |
| 2068 |
| 2069 | void YUY2ToARGBRow_C(const uint8_t* src_yuy2, |
| 2070 | uint8_t* rgb_buf, |
| 2071 | const struct YuvConstants* yuvconstants, |
| 2072 | int width) { |
| 2073 | int x; |
| 2074 | for (x = 0; x < width - 1; x += 2) { |
| 2075 | YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1, |
| 2076 | rgb_buf + 2, yuvconstants); |
| 2077 | rgb_buf[3] = 255; |
| 2078 | YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3], rgb_buf + 4, rgb_buf + 5, |
| 2079 | rgb_buf + 6, yuvconstants); |
| 2080 | rgb_buf[7] = 255; |
| 2081 | src_yuy2 += 4; |
| 2082 | rgb_buf += 8; // Advance 2 pixels. |
| 2083 | } |
| 2084 | if (width & 1) { |
| 2085 | YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1, |
| 2086 | rgb_buf + 2, yuvconstants); |
| 2087 | rgb_buf[3] = 255; |
| 2088 | } |
| 2089 | } |
| 2090 |
| 2091 | void UYVYToARGBRow_C(const uint8_t* src_uyvy, |
| 2092 | uint8_t* rgb_buf, |
| 2093 | const struct YuvConstants* yuvconstants, |
| 2094 | int width) { |
| 2095 | int x; |
| 2096 | for (x = 0; x < width - 1; x += 2) { |
| 2097 | YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1, |
| 2098 | rgb_buf + 2, yuvconstants); |
| 2099 | rgb_buf[3] = 255; |
| 2100 | YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2], rgb_buf + 4, rgb_buf + 5, |
| 2101 | rgb_buf + 6, yuvconstants); |
| 2102 | rgb_buf[7] = 255; |
| 2103 | src_uyvy += 4; |
| 2104 | rgb_buf += 8; // Advance 2 pixels. |
| 2105 | } |
| 2106 | if (width & 1) { |
| 2107 | YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1, |
| 2108 | rgb_buf + 2, yuvconstants); |
| 2109 | rgb_buf[3] = 255; |
| 2110 | } |
| 2111 | } |
| 2112 |
| 2113 | void I422ToRGBARow_C(const uint8_t* src_y, |
| 2114 | const uint8_t* src_u, |
| 2115 | const uint8_t* src_v, |
| 2116 | uint8_t* rgb_buf, |
| 2117 | const struct YuvConstants* yuvconstants, |
| 2118 | int width) { |
| 2119 | int x; |
| 2120 | for (x = 0; x < width - 1; x += 2) { |
| 2121 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2, |
| 2122 | rgb_buf + 3, yuvconstants); |
| 2123 | rgb_buf[0] = 255; |
| 2124 | YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 5, rgb_buf + 6, |
| 2125 | rgb_buf + 7, yuvconstants); |
| 2126 | rgb_buf[4] = 255; |
| 2127 | src_y += 2; |
| 2128 | src_u += 1; |
| 2129 | src_v += 1; |
| 2130 | rgb_buf += 8; // Advance 2 pixels. |
| 2131 | } |
| 2132 | if (width & 1) { |
| 2133 | YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2, |
| 2134 | rgb_buf + 3, yuvconstants); |
| 2135 | rgb_buf[0] = 255; |
| 2136 | } |
| 2137 | } |
| 2138 |
| 2139 | void I400ToARGBRow_C(const uint8_t* src_y, uint8_t* rgb_buf, int width) { |
| 2140 | int x; |
| 2141 | for (x = 0; x < width - 1; x += 2) { |
| 2142 | YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2); |
| 2143 | rgb_buf[3] = 255; |
| 2144 | YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6); |
| 2145 | rgb_buf[7] = 255; |
| 2146 | src_y += 2; |
| 2147 | rgb_buf += 8; // Advance 2 pixels. |
| 2148 | } |
| 2149 | if (width & 1) { |
| 2150 | YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2); |
| 2151 | rgb_buf[3] = 255; |
| 2152 | } |
| 2153 | } |
| 2154 |
| 2155 | void MirrorRow_C(const uint8_t* src, uint8_t* dst, int width) { |
| 2156 | int x; |
| 2157 | src += width - 1; |
| 2158 | for (x = 0; x < width - 1; x += 2) { |
| 2159 | dst[x] = src[0]; |
| 2160 | dst[x + 1] = src[-1]; |
| 2161 | src -= 2; |
| 2162 | } |
| 2163 | if (width & 1) { |
| 2164 | dst[width - 1] = src[0]; |
| 2165 | } |
| 2166 | } |
| 2167 |
| 2168 | void MirrorUVRow_C(const uint8_t* src_uv, |
| 2169 | uint8_t* dst_u, |
| 2170 | uint8_t* dst_v, |
| 2171 | int width) { |
| 2172 | int x; |
| 2173 | src_uv += (width - 1) << 1; |
| 2174 | for (x = 0; x < width - 1; x += 2) { |
| 2175 | dst_u[x] = src_uv[0]; |
| 2176 | dst_u[x + 1] = src_uv[-2]; |
| 2177 | dst_v[x] = src_uv[1]; |
| 2178 | dst_v[x + 1] = src_uv[-2 + 1]; |
| 2179 | src_uv -= 4; |
| 2180 | } |
| 2181 | if (width & 1) { |
| 2182 | dst_u[width - 1] = src_uv[0]; |
| 2183 | dst_v[width - 1] = src_uv[1]; |
| 2184 | } |
| 2185 | } |
| 2186 |
| 2187 | void ARGBMirrorRow_C(const uint8_t* src, uint8_t* dst, int width) { |
| 2188 | int x; |
| 2189 | const uint32_t* src32 = (const uint32_t*)(src); |
| 2190 | uint32_t* dst32 = (uint32_t*)(dst); |
| 2191 | src32 += width - 1; |
| 2192 | for (x = 0; x < width - 1; x += 2) { |
| 2193 | dst32[x] = src32[0]; |
| 2194 | dst32[x + 1] = src32[-1]; |
| 2195 | src32 -= 2; |
| 2196 | } |
| 2197 | if (width & 1) { |
| 2198 | dst32[width - 1] = src32[0]; |
| 2199 | } |
| 2200 | } |
| 2201 |
| 2202 | void SplitUVRow_C(const uint8_t* src_uv, |
| 2203 | uint8_t* dst_u, |
| 2204 | uint8_t* dst_v, |
| 2205 | int width) { |
| 2206 | int x; |
| 2207 | for (x = 0; x < width - 1; x += 2) { |
| 2208 | dst_u[x] = src_uv[0]; |
| 2209 | dst_u[x + 1] = src_uv[2]; |
| 2210 | dst_v[x] = src_uv[1]; |
| 2211 | dst_v[x + 1] = src_uv[3]; |
| 2212 | src_uv += 4; |
| 2213 | } |
| 2214 | if (width & 1) { |
| 2215 | dst_u[width - 1] = src_uv[0]; |
| 2216 | dst_v[width - 1] = src_uv[1]; |
| 2217 | } |
| 2218 | } |
| 2219 |
| 2220 | void MergeUVRow_C(const uint8_t* src_u, |
| 2221 | const uint8_t* src_v, |
| 2222 | uint8_t* dst_uv, |
| 2223 | int width) { |
| 2224 | int x; |
| 2225 | for (x = 0; x < width - 1; x += 2) { |
| 2226 | dst_uv[0] = src_u[x]; |
| 2227 | dst_uv[1] = src_v[x]; |
| 2228 | dst_uv[2] = src_u[x + 1]; |
| 2229 | dst_uv[3] = src_v[x + 1]; |
| 2230 | dst_uv += 4; |
| 2231 | } |
| 2232 | if (width & 1) { |
| 2233 | dst_uv[0] = src_u[width - 1]; |
| 2234 | dst_uv[1] = src_v[width - 1]; |
| 2235 | } |
| 2236 | } |
| 2237 |
| 2238 | void SplitRGBRow_C(const uint8_t* src_rgb, |
| 2239 | uint8_t* dst_r, |
| 2240 | uint8_t* dst_g, |
| 2241 | uint8_t* dst_b, |
| 2242 | int width) { |
| 2243 | int x; |
| 2244 | for (x = 0; x < width; ++x) { |
| 2245 | dst_r[x] = src_rgb[0]; |
| 2246 | dst_g[x] = src_rgb[1]; |
| 2247 | dst_b[x] = src_rgb[2]; |
| 2248 | src_rgb += 3; |
| 2249 | } |
| 2250 | } |
| 2251 |
| 2252 | void MergeRGBRow_C(const uint8_t* src_r, |
| 2253 | const uint8_t* src_g, |
| 2254 | const uint8_t* src_b, |
| 2255 | uint8_t* dst_rgb, |
| 2256 | int width) { |
| 2257 | int x; |
| 2258 | for (x = 0; x < width; ++x) { |
| 2259 | dst_rgb[0] = src_r[x]; |
| 2260 | dst_rgb[1] = src_g[x]; |
| 2261 | dst_rgb[2] = src_b[x]; |
| 2262 | dst_rgb += 3; |
| 2263 | } |
| 2264 | } |
| 2265 |
| 2266 | // Use scale to convert lsb formats to msb, depending how many bits there are: |
| 2267 | // 128 = 9 bits |
| 2268 | // 64 = 10 bits |
| 2269 | // 16 = 12 bits |
| 2270 | // 1 = 16 bits |
| 2271 | void MergeUVRow_16_C(const uint16_t* src_u, |
| 2272 | const uint16_t* src_v, |
| 2273 | uint16_t* dst_uv, |
| 2274 | int scale, |
| 2275 | int width) { |
| 2276 | int x; |
| 2277 | for (x = 0; x < width - 1; x += 2) { |
| 2278 | dst_uv[0] = src_u[x] * scale; |
| 2279 | dst_uv[1] = src_v[x] * scale; |
| 2280 | dst_uv[2] = src_u[x + 1] * scale; |
| 2281 | dst_uv[3] = src_v[x + 1] * scale; |
| 2282 | dst_uv += 4; |
| 2283 | } |
| 2284 | if (width & 1) { |
| 2285 | dst_uv[0] = src_u[width - 1] * scale; |
| 2286 | dst_uv[1] = src_v[width - 1] * scale; |
| 2287 | } |
| 2288 | } |
| 2289 |
| 2290 | void MultiplyRow_16_C(const uint16_t* src_y, |
| 2291 | uint16_t* dst_y, |
| 2292 | int scale, |
| 2293 | int width) { |
| 2294 | int x; |
| 2295 | for (x = 0; x < width; ++x) { |
| 2296 | dst_y[x] = src_y[x] * scale; |
| 2297 | } |
| 2298 | } |
| 2299 |
| 2300 | // Use scale to convert lsb formats to msb, depending how many bits there are: |
| 2301 | // 32768 = 9 bits |
| 2302 | // 16384 = 10 bits |
| 2303 | // 4096 = 12 bits |
| 2304 | // 256 = 16 bits |
| 2305 | void Convert16To8Row_C(const uint16_t* src_y, |
| 2306 | uint8_t* dst_y, |
| 2307 | int scale, |
| 2308 | int width) { |
| 2309 | int x; |
| 2310 | for (x = 0; x < width; ++x) { |
| 2311 | dst_y[x] = clamp255((src_y[x] * scale) >> 16); |
| 2312 | } |
| 2313 | } |
| 2314 |
| 2315 | // Use scale to convert lsb formats to msb, depending how many bits there are: |
| 2316 | // 1024 = 10 bits |
| 2317 | void Convert8To16Row_C(const uint8_t* src_y, |
| 2318 | uint16_t* dst_y, |
| 2319 | int scale, |
| 2320 | int width) { |
| 2321 | int x; |
| 2322 | scale *= 0x0101; // replicates the byte. |
| 2323 | for (x = 0; x < width; ++x) { |
| 2324 | dst_y[x] = (src_y[x] * scale) >> 16; |
| 2325 | } |
| 2326 | } |
| 2327 |
| 2328 | void CopyRow_C(const uint8_t* src, uint8_t* dst, int count) { |
| 2329 | memcpy(dst, src, count); |
| 2330 | } |
| 2331 |
| 2332 | void CopyRow_16_C(const uint16_t* src, uint16_t* dst, int count) { |
| 2333 | memcpy(dst, src, count * 2); |
| 2334 | } |
| 2335 |
| 2336 | void SetRow_C(uint8_t* dst, uint8_t v8, int width) { |
| 2337 | memset(dst, v8, width); |
| 2338 | } |
| 2339 |
| 2340 | void ARGBSetRow_C(uint8_t* dst_argb, uint32_t v32, int width) { |
| 2341 | uint32_t* d = (uint32_t*)(dst_argb); |
| 2342 | int x; |
| 2343 | for (x = 0; x < width; ++x) { |
| 2344 | d[x] = v32; |
| 2345 | } |
| 2346 | } |
| 2347 |
| 2348 | // Filter 2 rows of YUY2 UV's (422) into U and V (420). |
| 2349 | void YUY2ToUVRow_C(const uint8_t* src_yuy2, |
| 2350 | int src_stride_yuy2, |
| 2351 | uint8_t* dst_u, |
| 2352 | uint8_t* dst_v, |
| 2353 | int width) { |
| 2354 | // Output a row of UV values, filtering 2 rows of YUY2. |
| 2355 | int x; |
| 2356 | for (x = 0; x < width; x += 2) { |
| 2357 | dst_u[0] = (src_yuy2[1] + src_yuy2[src_stride_yuy2 + 1] + 1) >> 1; |
| 2358 | dst_v[0] = (src_yuy2[3] + src_yuy2[src_stride_yuy2 + 3] + 1) >> 1; |
| 2359 | src_yuy2 += 4; |
| 2360 | dst_u += 1; |
| 2361 | dst_v += 1; |
| 2362 | } |
| 2363 | } |
| 2364 |
| 2365 | // Copy row of YUY2 UV's (422) into U and V (422). |
| 2366 | void YUY2ToUV422Row_C(const uint8_t* src_yuy2, |
| 2367 | uint8_t* dst_u, |
| 2368 | uint8_t* dst_v, |
| 2369 | int width) { |
| 2370 | // Output a row of UV values. |
| 2371 | int x; |
| 2372 | for (x = 0; x < width; x += 2) { |
| 2373 | dst_u[0] = src_yuy2[1]; |
| 2374 | dst_v[0] = src_yuy2[3]; |
| 2375 | src_yuy2 += 4; |
| 2376 | dst_u += 1; |
| 2377 | dst_v += 1; |
| 2378 | } |
| 2379 | } |
| 2380 |
| 2381 | // Copy row of YUY2 Y's (422) into Y (420/422). |
| 2382 | void YUY2ToYRow_C(const uint8_t* src_yuy2, uint8_t* dst_y, int width) { |
| 2383 | // Output a row of Y values. |
| 2384 | int x; |
| 2385 | for (x = 0; x < width - 1; x += 2) { |
| 2386 | dst_y[x] = src_yuy2[0]; |
| 2387 | dst_y[x + 1] = src_yuy2[2]; |
| 2388 | src_yuy2 += 4; |
| 2389 | } |
| 2390 | if (width & 1) { |
| 2391 | dst_y[width - 1] = src_yuy2[0]; |
| 2392 | } |
| 2393 | } |
| 2394 |
| 2395 | // Filter 2 rows of UYVY UV's (422) into U and V (420). |
| 2396 | void UYVYToUVRow_C(const uint8_t* src_uyvy, |
| 2397 | int src_stride_uyvy, |
| 2398 | uint8_t* dst_u, |
| 2399 | uint8_t* dst_v, |
| 2400 | int width) { |
| 2401 | // Output a row of UV values. |
| 2402 | int x; |
| 2403 | for (x = 0; x < width; x += 2) { |
| 2404 | dst_u[0] = (src_uyvy[0] + src_uyvy[src_stride_uyvy + 0] + 1) >> 1; |
| 2405 | dst_v[0] = (src_uyvy[2] + src_uyvy[src_stride_uyvy + 2] + 1) >> 1; |
| 2406 | src_uyvy += 4; |
| 2407 | dst_u += 1; |
| 2408 | dst_v += 1; |
| 2409 | } |
| 2410 | } |
| 2411 |
| 2412 | // Copy row of UYVY UV's (422) into U and V (422). |
| 2413 | void UYVYToUV422Row_C(const uint8_t* src_uyvy, |
| 2414 | uint8_t* dst_u, |
| 2415 | uint8_t* dst_v, |
| 2416 | int width) { |
| 2417 | // Output a row of UV values. |
| 2418 | int x; |
| 2419 | for (x = 0; x < width; x += 2) { |
| 2420 | dst_u[0] = src_uyvy[0]; |
| 2421 | dst_v[0] = src_uyvy[2]; |
| 2422 | src_uyvy += 4; |
| 2423 | dst_u += 1; |
| 2424 | dst_v += 1; |
| 2425 | } |
| 2426 | } |
| 2427 |
| 2428 | // Copy row of UYVY Y's (422) into Y (420/422). |
| 2429 | void UYVYToYRow_C(const uint8_t* src_uyvy, uint8_t* dst_y, int width) { |
| 2430 | // Output a row of Y values. |
| 2431 | int x; |
| 2432 | for (x = 0; x < width - 1; x += 2) { |
| 2433 | dst_y[x] = src_uyvy[1]; |
| 2434 | dst_y[x + 1] = src_uyvy[3]; |
| 2435 | src_uyvy += 4; |
| 2436 | } |
| 2437 | if (width & 1) { |
| 2438 | dst_y[width - 1] = src_uyvy[1]; |
| 2439 | } |
| 2440 | } |
| 2441 |
| 2442 | #define BLEND(f, b, a) (((256 - a) * b) >> 8) + f |
| 2443 |
| 2444 | // Blend src_argb0 over src_argb1 and store to dst_argb. |
| 2445 | // dst_argb may be src_argb0 or src_argb1. |
| 2446 | // This code mimics the SSSE3 version for better testability. |
| 2447 | void ARGBBlendRow_C(const uint8_t* src_argb0, |
| 2448 | const uint8_t* src_argb1, |
| 2449 | uint8_t* dst_argb, |
| 2450 | int width) { |
| 2451 | int x; |
| 2452 | for (x = 0; x < width - 1; x += 2) { |
| 2453 | uint32_t fb = src_argb0[0]; |
| 2454 | uint32_t fg = src_argb0[1]; |
| 2455 | uint32_t fr = src_argb0[2]; |
| 2456 | uint32_t a = src_argb0[3]; |
| 2457 | uint32_t bb = src_argb1[0]; |
| 2458 | uint32_t bg = src_argb1[1]; |
| 2459 | uint32_t br = src_argb1[2]; |
| 2460 | dst_argb[0] = BLEND(fb, bb, a); |
| 2461 | dst_argb[1] = BLEND(fg, bg, a); |
| 2462 | dst_argb[2] = BLEND(fr, br, a); |
| 2463 | dst_argb[3] = 255u; |
| 2464 |
| 2465 | fb = src_argb0[4 + 0]; |
| 2466 | fg = src_argb0[4 + 1]; |
| 2467 | fr = src_argb0[4 + 2]; |
| 2468 | a = src_argb0[4 + 3]; |
| 2469 | bb = src_argb1[4 + 0]; |
| 2470 | bg = src_argb1[4 + 1]; |
| 2471 | br = src_argb1[4 + 2]; |
| 2472 | dst_argb[4 + 0] = BLEND(fb, bb, a); |
| 2473 | dst_argb[4 + 1] = BLEND(fg, bg, a); |
| 2474 | dst_argb[4 + 2] = BLEND(fr, br, a); |
| 2475 | dst_argb[4 + 3] = 255u; |
| 2476 | src_argb0 += 8; |
| 2477 | src_argb1 += 8; |
| 2478 | dst_argb += 8; |
| 2479 | } |
| 2480 |
| 2481 | if (width & 1) { |
| 2482 | uint32_t fb = src_argb0[0]; |
| 2483 | uint32_t fg = src_argb0[1]; |
| 2484 | uint32_t fr = src_argb0[2]; |
| 2485 | uint32_t a = src_argb0[3]; |
| 2486 | uint32_t bb = src_argb1[0]; |
| 2487 | uint32_t bg = src_argb1[1]; |
| 2488 | uint32_t br = src_argb1[2]; |
| 2489 | dst_argb[0] = BLEND(fb, bb, a); |
| 2490 | dst_argb[1] = BLEND(fg, bg, a); |
| 2491 | dst_argb[2] = BLEND(fr, br, a); |
| 2492 | dst_argb[3] = 255u; |
| 2493 | } |
| 2494 | } |
| 2495 | #undef BLEND |
| 2496 |
| 2497 | #define UBLEND(f, b, a) (((a)*f) + ((255 - a) * b) + 255) >> 8 |
| 2498 | void BlendPlaneRow_C(const uint8_t* src0, |
| 2499 | const uint8_t* src1, |
| 2500 | const uint8_t* alpha, |
| 2501 | uint8_t* dst, |
| 2502 | int width) { |
| 2503 | int x; |
| 2504 | for (x = 0; x < width - 1; x += 2) { |
| 2505 | dst[0] = UBLEND(src0[0], src1[0], alpha[0]); |
| 2506 | dst[1] = UBLEND(src0[1], src1[1], alpha[1]); |
| 2507 | src0 += 2; |
| 2508 | src1 += 2; |
| 2509 | alpha += 2; |
| 2510 | dst += 2; |
| 2511 | } |
| 2512 | if (width & 1) { |
| 2513 | dst[0] = UBLEND(src0[0], src1[0], alpha[0]); |
| 2514 | } |
| 2515 | } |
| 2516 | #undef UBLEND |
| 2517 |
| 2518 | #define ATTENUATE(f, a) (a | (a << 8)) * (f | (f << 8)) >> 24 |
| 2519 |
| 2520 | // Multiply source RGB by alpha and store to destination. |
| 2521 | // This code mimics the SSSE3 version for better testability. |
| 2522 | void ARGBAttenuateRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) { |
| 2523 | int i; |
| 2524 | for (i = 0; i < width - 1; i += 2) { |
| 2525 | uint32_t b = src_argb[0]; |
| 2526 | uint32_t g = src_argb[1]; |
| 2527 | uint32_t r = src_argb[2]; |
| 2528 | uint32_t a = src_argb[3]; |
| 2529 | dst_argb[0] = ATTENUATE(b, a); |
| 2530 | dst_argb[1] = ATTENUATE(g, a); |
| 2531 | dst_argb[2] = ATTENUATE(r, a); |
| 2532 | dst_argb[3] = a; |
| 2533 | b = src_argb[4]; |
| 2534 | g = src_argb[5]; |
| 2535 | r = src_argb[6]; |
| 2536 | a = src_argb[7]; |
| 2537 | dst_argb[4] = ATTENUATE(b, a); |
| 2538 | dst_argb[5] = ATTENUATE(g, a); |
| 2539 | dst_argb[6] = ATTENUATE(r, a); |
| 2540 | dst_argb[7] = a; |
| 2541 | src_argb += 8; |
| 2542 | dst_argb += 8; |
| 2543 | } |
| 2544 |
| 2545 | if (width & 1) { |
| 2546 | const uint32_t b = src_argb[0]; |
| 2547 | const uint32_t g = src_argb[1]; |
| 2548 | const uint32_t r = src_argb[2]; |
| 2549 | const uint32_t a = src_argb[3]; |
| 2550 | dst_argb[0] = ATTENUATE(b, a); |
| 2551 | dst_argb[1] = ATTENUATE(g, a); |
| 2552 | dst_argb[2] = ATTENUATE(r, a); |
| 2553 | dst_argb[3] = a; |
| 2554 | } |
| 2555 | } |
| 2556 | #undef ATTENUATE |
| 2557 |
| 2558 | // Divide source RGB by alpha and store to destination. |
| 2559 | // b = (b * 255 + (a / 2)) / a; |
| 2560 | // g = (g * 255 + (a / 2)) / a; |
| 2561 | // r = (r * 255 + (a / 2)) / a; |
| 2562 | // Reciprocal method is off by 1 on some values. ie 125 |
| 2563 | // 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower. |
| 2564 | #define T(a) 0x01000000 + (0x10000 / a) |
| 2565 | const uint32_t fixed_invtbl8[256] = { |
| 2566 | 0x01000000, 0x0100ffff, T(0x02), T(0x03), T(0x04), T(0x05), T(0x06), |
| 2567 | T(0x07), T(0x08), T(0x09), T(0x0a), T(0x0b), T(0x0c), T(0x0d), |
| 2568 | T(0x0e), T(0x0f), T(0x10), T(0x11), T(0x12), T(0x13), T(0x14), |
| 2569 | T(0x15), T(0x16), T(0x17), T(0x18), T(0x19), T(0x1a), T(0x1b), |
| 2570 | T(0x1c), T(0x1d), T(0x1e), T(0x1f), T(0x20), T(0x21), T(0x22), |
| 2571 | T(0x23), T(0x24), T(0x25), T(0x26), T(0x27), T(0x28), T(0x29), |
| 2572 | T(0x2a), T(0x2b), T(0x2c), T(0x2d), T(0x2e), T(0x2f), T(0x30), |
| 2573 | T(0x31), T(0x32), T(0x33), T(0x34), T(0x35), T(0x36), T(0x37), |
| 2574 | T(0x38), T(0x39), T(0x3a), T(0x3b), T(0x3c), T(0x3d), T(0x3e), |
| 2575 | T(0x3f), T(0x40), T(0x41), T(0x42), T(0x43), T(0x44), T(0x45), |
| 2576 | T(0x46), T(0x47), T(0x48), T(0x49), T(0x4a), T(0x4b), T(0x4c), |
| 2577 | T(0x4d), T(0x4e), T(0x4f), T(0x50), T(0x51), T(0x52), T(0x53), |
| 2578 | T(0x54), T(0x55), T(0x56), T(0x57), T(0x58), T(0x59), T(0x5a), |
| 2579 | T(0x5b), T(0x5c), T(0x5d), T(0x5e), T(0x5f), T(0x60), T(0x61), |
| 2580 | T(0x62), T(0x63), T(0x64), T(0x65), T(0x66), T(0x67), T(0x68), |
| 2581 | T(0x69), T(0x6a), T(0x6b), T(0x6c), T(0x6d), T(0x6e), T(0x6f), |
| 2582 | T(0x70), T(0x71), T(0x72), T(0x73), T(0x74), T(0x75), T(0x76), |
| 2583 | T(0x77), T(0x78), T(0x79), T(0x7a), T(0x7b), T(0x7c), T(0x7d), |
| 2584 | T(0x7e), T(0x7f), T(0x80), T(0x81), T(0x82), T(0x83), T(0x84), |
| 2585 | T(0x85), T(0x86), T(0x87), T(0x88), T(0x89), T(0x8a), T(0x8b), |
| 2586 | T(0x8c), T(0x8d), T(0x8e), T(0x8f), T(0x90), T(0x91), T(0x92), |
| 2587 | T(0x93), T(0x94), T(0x95), T(0x96), T(0x97), T(0x98), T(0x99), |
| 2588 | T(0x9a), T(0x9b), T(0x9c), T(0x9d), T(0x9e), T(0x9f), T(0xa0), |
| 2589 | T(0xa1), T(0xa2), T(0xa3), T(0xa4), T(0xa5), T(0xa6), T(0xa7), |
| 2590 | T(0xa8), T(0xa9), T(0xaa), T(0xab), T(0xac), T(0xad), T(0xae), |
| 2591 | T(0xaf), T(0xb0), T(0xb1), T(0xb2), T(0xb3), T(0xb4), T(0xb5), |
| 2592 | T(0xb6), T(0xb7), T(0xb8), T(0xb9), T(0xba), T(0xbb), T(0xbc), |
| 2593 | T(0xbd), T(0xbe), T(0xbf), T(0xc0), T(0xc1), T(0xc2), T(0xc3), |
| 2594 | T(0xc4), T(0xc5), T(0xc6), T(0xc7), T(0xc8), T(0xc9), T(0xca), |
| 2595 | T(0xcb), T(0xcc), T(0xcd), T(0xce), T(0xcf), T(0xd0), T(0xd1), |
| 2596 | T(0xd2), T(0xd3), T(0xd4), T(0xd5), T(0xd6), T(0xd7), T(0xd8), |
| 2597 | T(0xd9), T(0xda), T(0xdb), T(0xdc), T(0xdd), T(0xde), T(0xdf), |
| 2598 | T(0xe0), T(0xe1), T(0xe2), T(0xe3), T(0xe4), T(0xe5), T(0xe6), |
| 2599 | T(0xe7), T(0xe8), T(0xe9), T(0xea), T(0xeb), T(0xec), T(0xed), |
| 2600 | T(0xee), T(0xef), T(0xf0), T(0xf1), T(0xf2), T(0xf3), T(0xf4), |
| 2601 | T(0xf5), T(0xf6), T(0xf7), T(0xf8), T(0xf9), T(0xfa), T(0xfb), |
| 2602 | T(0xfc), T(0xfd), T(0xfe), 0x01000100}; |
| 2603 | #undef T |
| 2604 |
| 2605 | void ARGBUnattenuateRow_C(const uint8_t* src_argb, |
| 2606 | uint8_t* dst_argb, |
| 2607 | int width) { |
| 2608 | int i; |
| 2609 | for (i = 0; i < width; ++i) { |
| 2610 | uint32_t b = src_argb[0]; |
| 2611 | uint32_t g = src_argb[1]; |
| 2612 | uint32_t r = src_argb[2]; |
| 2613 | const uint32_t a = src_argb[3]; |
| 2614 | const uint32_t ia = fixed_invtbl8[a] & 0xffff; // 8.8 fixed point |
| 2615 | b = (b * ia) >> 8; |
| 2616 | g = (g * ia) >> 8; |
| 2617 | r = (r * ia) >> 8; |
| 2618 | // Clamping should not be necessary but is free in assembly. |
| 2619 | dst_argb[0] = clamp255(b); |
| 2620 | dst_argb[1] = clamp255(g); |
| 2621 | dst_argb[2] = clamp255(r); |
| 2622 | dst_argb[3] = a; |
| 2623 | src_argb += 4; |
| 2624 | dst_argb += 4; |
| 2625 | } |
| 2626 | } |
| 2627 |
| 2628 | void ComputeCumulativeSumRow_C(const uint8_t* row, |
| 2629 | int32_t* cumsum, |
| 2630 | const int32_t* previous_cumsum, |
| 2631 | int width) { |
| 2632 | int32_t row_sum[4] = {0, 0, 0, 0}; |
| 2633 | int x; |
| 2634 | for (x = 0; x < width; ++x) { |
| 2635 | row_sum[0] += row[x * 4 + 0]; |
| 2636 | row_sum[1] += row[x * 4 + 1]; |
| 2637 | row_sum[2] += row[x * 4 + 2]; |
| 2638 | row_sum[3] += row[x * 4 + 3]; |
| 2639 | cumsum[x * 4 + 0] = row_sum[0] + previous_cumsum[x * 4 + 0]; |
| 2640 | cumsum[x * 4 + 1] = row_sum[1] + previous_cumsum[x * 4 + 1]; |
| 2641 | cumsum[x * 4 + 2] = row_sum[2] + previous_cumsum[x * 4 + 2]; |
| 2642 | cumsum[x * 4 + 3] = row_sum[3] + previous_cumsum[x * 4 + 3]; |
| 2643 | } |
| 2644 | } |
| 2645 |
| 2646 | void CumulativeSumToAverageRow_C(const int32_t* tl, |
| 2647 | const int32_t* bl, |
| 2648 | int w, |
| 2649 | int area, |
| 2650 | uint8_t* dst, |
| 2651 | int count) { |
| 2652 | float ooa = 1.0f / area; |
| 2653 | int i; |
| 2654 | for (i = 0; i < count; ++i) { |
| 2655 | dst[0] = (uint8_t)((bl[w + 0] + tl[0] - bl[0] - tl[w + 0]) * ooa); |
| 2656 | dst[1] = (uint8_t)((bl[w + 1] + tl[1] - bl[1] - tl[w + 1]) * ooa); |
| 2657 | dst[2] = (uint8_t)((bl[w + 2] + tl[2] - bl[2] - tl[w + 2]) * ooa); |
| 2658 | dst[3] = (uint8_t)((bl[w + 3] + tl[3] - bl[3] - tl[w + 3]) * ooa); |
| 2659 | dst += 4; |
| 2660 | tl += 4; |
| 2661 | bl += 4; |
| 2662 | } |
| 2663 | } |
| 2664 |
| 2665 | // Copy pixels from rotated source to destination row with a slope. |
| 2666 | LIBYUV_API |
| 2667 | void ARGBAffineRow_C(const uint8_t* src_argb, |
| 2668 | int src_argb_stride, |
| 2669 | uint8_t* dst_argb, |
| 2670 | const float* uv_dudv, |
| 2671 | int width) { |
| 2672 | int i; |
| 2673 | // Render a row of pixels from source into a buffer. |
| 2674 | float uv[2]; |
| 2675 | uv[0] = uv_dudv[0]; |
| 2676 | uv[1] = uv_dudv[1]; |
| 2677 | for (i = 0; i < width; ++i) { |
| 2678 | int x = (int)(uv[0]); |
| 2679 | int y = (int)(uv[1]); |
| 2680 | *(uint32_t*)(dst_argb) = |
| 2681 | *(const uint32_t*)(src_argb + y * src_argb_stride + x * 4); |
| 2682 | dst_argb += 4; |
| 2683 | uv[0] += uv_dudv[2]; |
| 2684 | uv[1] += uv_dudv[3]; |
| 2685 | } |
| 2686 | } |
| 2687 |
| 2688 | // Blend 2 rows into 1. |
| 2689 | static void HalfRow_C(const uint8_t* src_uv, |
| 2690 | ptrdiff_t src_uv_stride, |
| 2691 | uint8_t* dst_uv, |
| 2692 | int width) { |
| 2693 | int x; |
| 2694 | for (x = 0; x < width; ++x) { |
| 2695 | dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1; |
| 2696 | } |
| 2697 | } |
| 2698 |
| 2699 | static void HalfRow_16_C(const uint16_t* src_uv, |
| 2700 | ptrdiff_t src_uv_stride, |
| 2701 | uint16_t* dst_uv, |
| 2702 | int width) { |
| 2703 | int x; |
| 2704 | for (x = 0; x < width; ++x) { |
| 2705 | dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1; |
| 2706 | } |
| 2707 | } |
| 2708 |
| 2709 | // C version 2x2 -> 2x1. |
| 2710 | void InterpolateRow_C(uint8_t* dst_ptr, |
| 2711 | const uint8_t* src_ptr, |
| 2712 | ptrdiff_t src_stride, |
| 2713 | int width, |
| 2714 | int source_y_fraction) { |
| 2715 | int y1_fraction = source_y_fraction; |
| 2716 | int y0_fraction = 256 - y1_fraction; |
| 2717 | const uint8_t* src_ptr1 = src_ptr + src_stride; |
| 2718 | int x; |
| 2719 | if (y1_fraction == 0) { |
| 2720 | memcpy(dst_ptr, src_ptr, width); |
| 2721 | return; |
| 2722 | } |
| 2723 | if (y1_fraction == 128) { |
| 2724 | HalfRow_C(src_ptr, src_stride, dst_ptr, width); |
| 2725 | return; |
| 2726 | } |
| 2727 | for (x = 0; x < width - 1; x += 2) { |
| 2728 | dst_ptr[0] = |
| 2729 | (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8; |
| 2730 | dst_ptr[1] = |
| 2731 | (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction + 128) >> 8; |
| 2732 | src_ptr += 2; |
| 2733 | src_ptr1 += 2; |
| 2734 | dst_ptr += 2; |
| 2735 | } |
| 2736 | if (width & 1) { |
| 2737 | dst_ptr[0] = |
| 2738 | (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8; |
| 2739 | } |
| 2740 | } |
| 2741 |
| 2742 | void InterpolateRow_16_C(uint16_t* dst_ptr, |
| 2743 | const uint16_t* src_ptr, |
| 2744 | ptrdiff_t src_stride, |
| 2745 | int width, |
| 2746 | int source_y_fraction) { |
| 2747 | int y1_fraction = source_y_fraction; |
| 2748 | int y0_fraction = 256 - y1_fraction; |
| 2749 | const uint16_t* src_ptr1 = src_ptr + src_stride; |
| 2750 | int x; |
| 2751 | if (source_y_fraction == 0) { |
| 2752 | memcpy(dst_ptr, src_ptr, width * 2); |
| 2753 | return; |
| 2754 | } |
| 2755 | if (source_y_fraction == 128) { |
| 2756 | HalfRow_16_C(src_ptr, src_stride, dst_ptr, width); |
| 2757 | return; |
| 2758 | } |
| 2759 | for (x = 0; x < width - 1; x += 2) { |
| 2760 | dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8; |
| 2761 | dst_ptr[1] = (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction) >> 8; |
| 2762 | src_ptr += 2; |
| 2763 | src_ptr1 += 2; |
| 2764 | dst_ptr += 2; |
| 2765 | } |
| 2766 | if (width & 1) { |
| 2767 | dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8; |
| 2768 | } |
| 2769 | } |
| 2770 |
| 2771 | // Use first 4 shuffler values to reorder ARGB channels. |
| 2772 | void ARGBShuffleRow_C(const uint8_t* src_argb, |
| 2773 | uint8_t* dst_argb, |
| 2774 | const uint8_t* shuffler, |
| 2775 | int width) { |
| 2776 | int index0 = shuffler[0]; |
| 2777 | int index1 = shuffler[1]; |
| 2778 | int index2 = shuffler[2]; |
| 2779 | int index3 = shuffler[3]; |
| 2780 | // Shuffle a row of ARGB. |
| 2781 | int x; |
| 2782 | for (x = 0; x < width; ++x) { |
| 2783 | // To support in-place conversion. |
| 2784 | uint8_t b = src_argb[index0]; |
| 2785 | uint8_t g = src_argb[index1]; |
| 2786 | uint8_t r = src_argb[index2]; |
| 2787 | uint8_t a = src_argb[index3]; |
| 2788 | dst_argb[0] = b; |
| 2789 | dst_argb[1] = g; |
| 2790 | dst_argb[2] = r; |
| 2791 | dst_argb[3] = a; |
| 2792 | src_argb += 4; |
| 2793 | dst_argb += 4; |
| 2794 | } |
| 2795 | } |
| 2796 |
| 2797 | void I422ToYUY2Row_C(const uint8_t* src_y, |
| 2798 | const uint8_t* src_u, |
| 2799 | const uint8_t* src_v, |
| 2800 | uint8_t* dst_frame, |
| 2801 | int width) { |
| 2802 | int x; |
| 2803 | for (x = 0; x < width - 1; x += 2) { |
| 2804 | dst_frame[0] = src_y[0]; |
| 2805 | dst_frame[1] = src_u[0]; |
| 2806 | dst_frame[2] = src_y[1]; |
| 2807 | dst_frame[3] = src_v[0]; |
| 2808 | dst_frame += 4; |
| 2809 | src_y += 2; |
| 2810 | src_u += 1; |
| 2811 | src_v += 1; |
| 2812 | } |
| 2813 | if (width & 1) { |
| 2814 | dst_frame[0] = src_y[0]; |
| 2815 | dst_frame[1] = src_u[0]; |
| 2816 | dst_frame[2] = 0; |
| 2817 | dst_frame[3] = src_v[0]; |
| 2818 | } |
| 2819 | } |
| 2820 |
| 2821 | void I422ToUYVYRow_C(const uint8_t* src_y, |
| 2822 | const uint8_t* src_u, |
| 2823 | const uint8_t* src_v, |
| 2824 | uint8_t* dst_frame, |
| 2825 | int width) { |
| 2826 | int x; |
| 2827 | for (x = 0; x < width - 1; x += 2) { |
| 2828 | dst_frame[0] = src_u[0]; |
| 2829 | dst_frame[1] = src_y[0]; |
| 2830 | dst_frame[2] = src_v[0]; |
| 2831 | dst_frame[3] = src_y[1]; |
| 2832 | dst_frame += 4; |
| 2833 | src_y += 2; |
| 2834 | src_u += 1; |
| 2835 | src_v += 1; |
| 2836 | } |
| 2837 | if (width & 1) { |
| 2838 | dst_frame[0] = src_u[0]; |
| 2839 | dst_frame[1] = src_y[0]; |
| 2840 | dst_frame[2] = src_v[0]; |
| 2841 | dst_frame[3] = 0; |
| 2842 | } |
| 2843 | } |
| 2844 |
| 2845 | void ARGBPolynomialRow_C(const uint8_t* src_argb, |
| 2846 | uint8_t* dst_argb, |
| 2847 | const float* poly, |
| 2848 | int width) { |
| 2849 | int i; |
| 2850 | for (i = 0; i < width; ++i) { |
| 2851 | float b = (float)(src_argb[0]); |
| 2852 | float g = (float)(src_argb[1]); |
| 2853 | float r = (float)(src_argb[2]); |
| 2854 | float a = (float)(src_argb[3]); |
| 2855 | float b2 = b * b; |
| 2856 | float g2 = g * g; |
| 2857 | float r2 = r * r; |
| 2858 | float a2 = a * a; |
| 2859 | float db = poly[0] + poly[4] * b; |
| 2860 | float dg = poly[1] + poly[5] * g; |
| 2861 | float dr = poly[2] + poly[6] * r; |
| 2862 | float da = poly[3] + poly[7] * a; |
| 2863 | float b3 = b2 * b; |
| 2864 | float g3 = g2 * g; |
| 2865 | float r3 = r2 * r; |
| 2866 | float a3 = a2 * a; |
| 2867 | db += poly[8] * b2; |
| 2868 | dg += poly[9] * g2; |
| 2869 | dr += poly[10] * r2; |
| 2870 | da += poly[11] * a2; |
| 2871 | db += poly[12] * b3; |
| 2872 | dg += poly[13] * g3; |
| 2873 | dr += poly[14] * r3; |
| 2874 | da += poly[15] * a3; |
| 2875 |
| 2876 | dst_argb[0] = Clamp((int32_t)(db)); |
| 2877 | dst_argb[1] = Clamp((int32_t)(dg)); |
| 2878 | dst_argb[2] = Clamp((int32_t)(dr)); |
| 2879 | dst_argb[3] = Clamp((int32_t)(da)); |
| 2880 | src_argb += 4; |
| 2881 | dst_argb += 4; |
| 2882 | } |
| 2883 | } |
| 2884 |
| 2885 | // Samples assumed to be unsigned in low 9, 10 or 12 bits. Scale factor |
| 2886 | // adjust the source integer range to the half float range desired. |
| 2887 |
| 2888 | // This magic constant is 2^-112. Multiplying by this |
| 2889 | // is the same as subtracting 112 from the exponent, which |
| 2890 | // is the difference in exponent bias between 32-bit and |
| 2891 | // 16-bit floats. Once we've done this subtraction, we can |
| 2892 | // simply extract the low bits of the exponent and the high |
| 2893 | // bits of the mantissa from our float and we're done. |
| 2894 |
| 2895 | // Work around GCC 7 punning warning -Wstrict-aliasing |
| 2896 | #if defined(__GNUC__) |
| 2897 | typedef uint32_t __attribute__((__may_alias__)) uint32_alias_t; |
| 2898 | #else |
| 2899 | typedef uint32_t uint32_alias_t; |
| 2900 | #endif |
| 2901 |
| 2902 | void HalfFloatRow_C(const uint16_t* src, |
| 2903 | uint16_t* dst, |
| 2904 | float scale, |
| 2905 | int width) { |
| 2906 | int i; |
| 2907 | float mult = 1.9259299444e-34f * scale; |
| 2908 | for (i = 0; i < width; ++i) { |
| 2909 | float value = src[i] * mult; |
| 2910 | dst[i] = (uint16_t)((*(const uint32_alias_t*)&value) >> 13); |
| 2911 | } |
| 2912 | } |
| 2913 |
| 2914 | void ByteToFloatRow_C(const uint8_t* src, float* dst, float scale, int width) { |
| 2915 | int i; |
| 2916 | for (i = 0; i < width; ++i) { |
| 2917 | float value = src[i] * scale; |
| 2918 | dst[i] = value; |
| 2919 | } |
| 2920 | } |
| 2921 |
| 2922 | void ARGBLumaColorTableRow_C(const uint8_t* src_argb, |
| 2923 | uint8_t* dst_argb, |
| 2924 | int width, |
| 2925 | const uint8_t* luma, |
| 2926 | uint32_t lumacoeff) { |
| 2927 | uint32_t bc = lumacoeff & 0xff; |
| 2928 | uint32_t gc = (lumacoeff >> 8) & 0xff; |
| 2929 | uint32_t rc = (lumacoeff >> 16) & 0xff; |
| 2930 |
| 2931 | int i; |
| 2932 | for (i = 0; i < width - 1; i += 2) { |
| 2933 | // Luminance in rows, color values in columns. |
| 2934 | const uint8_t* luma0 = |
| 2935 | ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) + |
| 2936 | luma; |
| 2937 | const uint8_t* luma1; |
| 2938 | dst_argb[0] = luma0[src_argb[0]]; |
| 2939 | dst_argb[1] = luma0[src_argb[1]]; |
| 2940 | dst_argb[2] = luma0[src_argb[2]]; |
| 2941 | dst_argb[3] = src_argb[3]; |
| 2942 | luma1 = |
| 2943 | ((src_argb[4] * bc + src_argb[5] * gc + src_argb[6] * rc) & 0x7F00u) + |
| 2944 | luma; |
| 2945 | dst_argb[4] = luma1[src_argb[4]]; |
| 2946 | dst_argb[5] = luma1[src_argb[5]]; |
| 2947 | dst_argb[6] = luma1[src_argb[6]]; |
| 2948 | dst_argb[7] = src_argb[7]; |
| 2949 | src_argb += 8; |
| 2950 | dst_argb += 8; |
| 2951 | } |
| 2952 | if (width & 1) { |
| 2953 | // Luminance in rows, color values in columns. |
| 2954 | const uint8_t* luma0 = |
| 2955 | ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) + |
| 2956 | luma; |
| 2957 | dst_argb[0] = luma0[src_argb[0]]; |
| 2958 | dst_argb[1] = luma0[src_argb[1]]; |
| 2959 | dst_argb[2] = luma0[src_argb[2]]; |
| 2960 | dst_argb[3] = src_argb[3]; |
| 2961 | } |
| 2962 | } |
| 2963 |
| 2964 | void ARGBCopyAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) { |
| 2965 | int i; |
| 2966 | for (i = 0; i < width - 1; i += 2) { |
| 2967 | dst[3] = src[3]; |
| 2968 | dst[7] = src[7]; |
| 2969 | dst += 8; |
| 2970 | src += 8; |
| 2971 | } |
| 2972 | if (width & 1) { |
| 2973 | dst[3] = src[3]; |
| 2974 | } |
| 2975 | } |
| 2976 |
| 2977 | void ARGBExtractAlphaRow_C(const uint8_t* src_argb, uint8_t* dst_a, int width) { |
| 2978 | int i; |
| 2979 | for (i = 0; i < width - 1; i += 2) { |
| 2980 | dst_a[0] = src_argb[3]; |
| 2981 | dst_a[1] = src_argb[7]; |
| 2982 | dst_a += 2; |
| 2983 | src_argb += 8; |
| 2984 | } |
| 2985 | if (width & 1) { |
| 2986 | dst_a[0] = src_argb[3]; |
| 2987 | } |
| 2988 | } |
| 2989 |
| 2990 | void ARGBCopyYToAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) { |
| 2991 | int i; |
| 2992 | for (i = 0; i < width - 1; i += 2) { |
| 2993 | dst[3] = src[0]; |
| 2994 | dst[7] = src[1]; |
| 2995 | dst += 8; |
| 2996 | src += 2; |
| 2997 | } |
| 2998 | if (width & 1) { |
| 2999 | dst[3] = src[0]; |
| 3000 | } |
| 3001 | } |
| 3002 |
| 3003 | // Maximum temporary width for wrappers to process at a time, in pixels. |
| 3004 | #define MAXTWIDTH 2048 |
| 3005 |
| 3006 | #if !(defined(_MSC_VER) && defined(_M_IX86)) && \ |
| 3007 | defined(HAS_I422TORGB565ROW_SSSE3) |
| 3008 | // row_win.cc has asm version, but GCC uses 2 step wrapper. |
| 3009 | void I422ToRGB565Row_SSSE3(const uint8_t* src_y, |
| 3010 | const uint8_t* src_u, |
| 3011 | const uint8_t* src_v, |
| 3012 | uint8_t* dst_rgb565, |
| 3013 | const struct YuvConstants* yuvconstants, |
| 3014 | int width) { |
| 3015 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3016 | while (width > 0) { |
| 3017 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3018 | I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3019 | ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth); |
| 3020 | src_y += twidth; |
| 3021 | src_u += twidth / 2; |
| 3022 | src_v += twidth / 2; |
| 3023 | dst_rgb565 += twidth * 2; |
| 3024 | width -= twidth; |
| 3025 | } |
| 3026 | } |
| 3027 | #endif |
| 3028 |
| 3029 | #if defined(HAS_I422TOARGB1555ROW_SSSE3) |
| 3030 | void I422ToARGB1555Row_SSSE3(const uint8_t* src_y, |
| 3031 | const uint8_t* src_u, |
| 3032 | const uint8_t* src_v, |
| 3033 | uint8_t* dst_argb1555, |
| 3034 | const struct YuvConstants* yuvconstants, |
| 3035 | int width) { |
| 3036 | // Row buffer for intermediate ARGB pixels. |
| 3037 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3038 | while (width > 0) { |
| 3039 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3040 | I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3041 | ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth); |
| 3042 | src_y += twidth; |
| 3043 | src_u += twidth / 2; |
| 3044 | src_v += twidth / 2; |
| 3045 | dst_argb1555 += twidth * 2; |
| 3046 | width -= twidth; |
| 3047 | } |
| 3048 | } |
| 3049 | #endif |
| 3050 |
| 3051 | #if defined(HAS_I422TOARGB4444ROW_SSSE3) |
| 3052 | void I422ToARGB4444Row_SSSE3(const uint8_t* src_y, |
| 3053 | const uint8_t* src_u, |
| 3054 | const uint8_t* src_v, |
| 3055 | uint8_t* dst_argb4444, |
| 3056 | const struct YuvConstants* yuvconstants, |
| 3057 | int width) { |
| 3058 | // Row buffer for intermediate ARGB pixels. |
| 3059 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3060 | while (width > 0) { |
| 3061 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3062 | I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3063 | ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth); |
| 3064 | src_y += twidth; |
| 3065 | src_u += twidth / 2; |
| 3066 | src_v += twidth / 2; |
| 3067 | dst_argb4444 += twidth * 2; |
| 3068 | width -= twidth; |
| 3069 | } |
| 3070 | } |
| 3071 | #endif |
| 3072 |
| 3073 | #if defined(HAS_NV12TORGB565ROW_SSSE3) |
| 3074 | void NV12ToRGB565Row_SSSE3(const uint8_t* src_y, |
| 3075 | const uint8_t* src_uv, |
| 3076 | uint8_t* dst_rgb565, |
| 3077 | const struct YuvConstants* yuvconstants, |
| 3078 | int width) { |
| 3079 | // Row buffer for intermediate ARGB pixels. |
| 3080 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3081 | while (width > 0) { |
| 3082 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3083 | NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth); |
| 3084 | ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth); |
| 3085 | src_y += twidth; |
| 3086 | src_uv += twidth; |
| 3087 | dst_rgb565 += twidth * 2; |
| 3088 | width -= twidth; |
| 3089 | } |
| 3090 | } |
| 3091 | #endif |
| 3092 |
| 3093 | #if defined(HAS_NV12TORGB24ROW_SSSE3) |
| 3094 | void NV12ToRGB24Row_SSSE3(const uint8_t* src_y, |
| 3095 | const uint8_t* src_uv, |
| 3096 | uint8_t* dst_rgb24, |
| 3097 | const struct YuvConstants* yuvconstants, |
| 3098 | int width) { |
| 3099 | // Row buffer for intermediate ARGB pixels. |
| 3100 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3101 | while (width > 0) { |
| 3102 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3103 | NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth); |
| 3104 | ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth); |
| 3105 | src_y += twidth; |
| 3106 | src_uv += twidth; |
| 3107 | dst_rgb24 += twidth * 3; |
| 3108 | width -= twidth; |
| 3109 | } |
| 3110 | } |
| 3111 | #endif |
| 3112 |
| 3113 | #if defined(HAS_NV21TORGB24ROW_SSSE3) |
| 3114 | void NV21ToRGB24Row_SSSE3(const uint8_t* src_y, |
| 3115 | const uint8_t* src_vu, |
| 3116 | uint8_t* dst_rgb24, |
| 3117 | const struct YuvConstants* yuvconstants, |
| 3118 | int width) { |
| 3119 | // Row buffer for intermediate ARGB pixels. |
| 3120 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3121 | while (width > 0) { |
| 3122 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3123 | NV21ToARGBRow_SSSE3(src_y, src_vu, row, yuvconstants, twidth); |
| 3124 | ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth); |
| 3125 | src_y += twidth; |
| 3126 | src_vu += twidth; |
| 3127 | dst_rgb24 += twidth * 3; |
| 3128 | width -= twidth; |
| 3129 | } |
| 3130 | } |
| 3131 | #endif |
| 3132 |
| 3133 | #if defined(HAS_NV12TORGB24ROW_AVX2) |
| 3134 | void NV12ToRGB24Row_AVX2(const uint8_t* src_y, |
| 3135 | const uint8_t* src_uv, |
| 3136 | uint8_t* dst_rgb24, |
| 3137 | const struct YuvConstants* yuvconstants, |
| 3138 | int width) { |
| 3139 | // Row buffer for intermediate ARGB pixels. |
| 3140 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3141 | while (width > 0) { |
| 3142 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3143 | NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth); |
| 3144 | #if defined(HAS_ARGBTORGB24ROW_AVX2) |
| 3145 | ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth); |
| 3146 | #else |
| 3147 | ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth); |
| 3148 | #endif |
| 3149 | src_y += twidth; |
| 3150 | src_uv += twidth; |
| 3151 | dst_rgb24 += twidth * 3; |
| 3152 | width -= twidth; |
| 3153 | } |
| 3154 | } |
| 3155 | #endif |
| 3156 |
| 3157 | #if defined(HAS_NV21TORGB24ROW_AVX2) |
| 3158 | void NV21ToRGB24Row_AVX2(const uint8_t* src_y, |
| 3159 | const uint8_t* src_vu, |
| 3160 | uint8_t* dst_rgb24, |
| 3161 | const struct YuvConstants* yuvconstants, |
| 3162 | int width) { |
| 3163 | // Row buffer for intermediate ARGB pixels. |
| 3164 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3165 | while (width > 0) { |
| 3166 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3167 | NV21ToARGBRow_AVX2(src_y, src_vu, row, yuvconstants, twidth); |
| 3168 | #if defined(HAS_ARGBTORGB24ROW_AVX2) |
| 3169 | ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth); |
| 3170 | #else |
| 3171 | ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth); |
| 3172 | #endif |
| 3173 | src_y += twidth; |
| 3174 | src_vu += twidth; |
| 3175 | dst_rgb24 += twidth * 3; |
| 3176 | width -= twidth; |
| 3177 | } |
| 3178 | } |
| 3179 | #endif |
| 3180 |
| 3181 | #if defined(HAS_I422TORGB565ROW_AVX2) |
| 3182 | void I422ToRGB565Row_AVX2(const uint8_t* src_y, |
| 3183 | const uint8_t* src_u, |
| 3184 | const uint8_t* src_v, |
| 3185 | uint8_t* dst_rgb565, |
| 3186 | const struct YuvConstants* yuvconstants, |
| 3187 | int width) { |
| 3188 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3189 | while (width > 0) { |
| 3190 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3191 | I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3192 | #if defined(HAS_ARGBTORGB565ROW_AVX2) |
| 3193 | ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth); |
| 3194 | #else |
| 3195 | ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth); |
| 3196 | #endif |
| 3197 | src_y += twidth; |
| 3198 | src_u += twidth / 2; |
| 3199 | src_v += twidth / 2; |
| 3200 | dst_rgb565 += twidth * 2; |
| 3201 | width -= twidth; |
| 3202 | } |
| 3203 | } |
| 3204 | #endif |
| 3205 |
| 3206 | #if defined(HAS_I422TOARGB1555ROW_AVX2) |
| 3207 | void I422ToARGB1555Row_AVX2(const uint8_t* src_y, |
| 3208 | const uint8_t* src_u, |
| 3209 | const uint8_t* src_v, |
| 3210 | uint8_t* dst_argb1555, |
| 3211 | const struct YuvConstants* yuvconstants, |
| 3212 | int width) { |
| 3213 | // Row buffer for intermediate ARGB pixels. |
| 3214 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3215 | while (width > 0) { |
| 3216 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3217 | I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3218 | #if defined(HAS_ARGBTOARGB1555ROW_AVX2) |
| 3219 | ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth); |
| 3220 | #else |
| 3221 | ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth); |
| 3222 | #endif |
| 3223 | src_y += twidth; |
| 3224 | src_u += twidth / 2; |
| 3225 | src_v += twidth / 2; |
| 3226 | dst_argb1555 += twidth * 2; |
| 3227 | width -= twidth; |
| 3228 | } |
| 3229 | } |
| 3230 | #endif |
| 3231 |
| 3232 | #if defined(HAS_I422TOARGB4444ROW_AVX2) |
| 3233 | void I422ToARGB4444Row_AVX2(const uint8_t* src_y, |
| 3234 | const uint8_t* src_u, |
| 3235 | const uint8_t* src_v, |
| 3236 | uint8_t* dst_argb4444, |
| 3237 | const struct YuvConstants* yuvconstants, |
| 3238 | int width) { |
| 3239 | // Row buffer for intermediate ARGB pixels. |
| 3240 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3241 | while (width > 0) { |
| 3242 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3243 | I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3244 | #if defined(HAS_ARGBTOARGB4444ROW_AVX2) |
| 3245 | ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth); |
| 3246 | #else |
| 3247 | ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth); |
| 3248 | #endif |
| 3249 | src_y += twidth; |
| 3250 | src_u += twidth / 2; |
| 3251 | src_v += twidth / 2; |
| 3252 | dst_argb4444 += twidth * 2; |
| 3253 | width -= twidth; |
| 3254 | } |
| 3255 | } |
| 3256 | #endif |
| 3257 |
| 3258 | #if defined(HAS_I422TORGB24ROW_AVX2) |
| 3259 | void I422ToRGB24Row_AVX2(const uint8_t* src_y, |
| 3260 | const uint8_t* src_u, |
| 3261 | const uint8_t* src_v, |
| 3262 | uint8_t* dst_rgb24, |
| 3263 | const struct YuvConstants* yuvconstants, |
| 3264 | int width) { |
| 3265 | // Row buffer for intermediate ARGB pixels. |
| 3266 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3267 | while (width > 0) { |
| 3268 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3269 | I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth); |
| 3270 | #if defined(HAS_ARGBTORGB24ROW_AVX2) |
| 3271 | ARGBToRGB24Row_AVX2(row, dst_rgb24, twidth); |
| 3272 | #else |
| 3273 | ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth); |
| 3274 | #endif |
| 3275 | src_y += twidth; |
| 3276 | src_u += twidth / 2; |
| 3277 | src_v += twidth / 2; |
| 3278 | dst_rgb24 += twidth * 3; |
| 3279 | width -= twidth; |
| 3280 | } |
| 3281 | } |
| 3282 | #endif |
| 3283 |
| 3284 | #if defined(HAS_NV12TORGB565ROW_AVX2) |
| 3285 | void NV12ToRGB565Row_AVX2(const uint8_t* src_y, |
| 3286 | const uint8_t* src_uv, |
| 3287 | uint8_t* dst_rgb565, |
| 3288 | const struct YuvConstants* yuvconstants, |
| 3289 | int width) { |
| 3290 | // Row buffer for intermediate ARGB pixels. |
| 3291 | SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]); |
| 3292 | while (width > 0) { |
| 3293 | int twidth = width > MAXTWIDTH ? MAXTWIDTH : width; |
| 3294 | NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth); |
| 3295 | #if defined(HAS_ARGBTORGB565ROW_AVX2) |
| 3296 | ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth); |
| 3297 | #else |
| 3298 | ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth); |
| 3299 | #endif |
| 3300 | src_y += twidth; |
| 3301 | src_uv += twidth; |
| 3302 | dst_rgb565 += twidth * 2; |
| 3303 | width -= twidth; |
| 3304 | } |
| 3305 | } |
| 3306 | #endif |
| 3307 |
| 3308 | float ScaleSumSamples_C(const float* src, float* dst, float scale, int width) { |
| 3309 | float fsum = 0.f; |
| 3310 | int i; |
| 3311 | for (i = 0; i < width; ++i) { |
| 3312 | float v = *src++; |
| 3313 | fsum += v * v; |
| 3314 | *dst++ = v * scale; |
| 3315 | } |
| 3316 | return fsum; |
| 3317 | } |
| 3318 |
| 3319 | float ScaleMaxSamples_C(const float* src, float* dst, float scale, int width) { |
| 3320 | float fmax = 0.f; |
| 3321 | int i; |
| 3322 | for (i = 0; i < width; ++i) { |
| 3323 | float v = *src++; |
| 3324 | float vs = v * scale; |
| 3325 | fmax = (v > fmax) ? v : fmax; |
| 3326 | *dst++ = vs; |
| 3327 | } |
| 3328 | return fmax; |
| 3329 | } |
| 3330 |
| 3331 | void ScaleSamples_C(const float* src, float* dst, float scale, int width) { |
| 3332 | int i; |
| 3333 | for (i = 0; i < width; ++i) { |
| 3334 | *dst++ = *src++ * scale; |
| 3335 | } |
| 3336 | } |
| 3337 |
| 3338 | void GaussRow_C(const uint32_t* src, uint16_t* dst, int width) { |
| 3339 | int i; |
| 3340 | for (i = 0; i < width; ++i) { |
| 3341 | *dst++ = |
| 3342 | (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4] + 128) >> 8; |
| 3343 | ++src; |
| 3344 | } |
| 3345 | } |
| 3346 |
| 3347 | // filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row. |
| 3348 | void GaussCol_C(const uint16_t* src0, |
| 3349 | const uint16_t* src1, |
| 3350 | const uint16_t* src2, |
| 3351 | const uint16_t* src3, |
| 3352 | const uint16_t* src4, |
| 3353 | uint32_t* dst, |
| 3354 | int width) { |
| 3355 | int i; |
| 3356 | for (i = 0; i < width; ++i) { |
| 3357 | *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++; |
| 3358 | } |
| 3359 | } |
| 3360 |
| 3361 | // Convert biplanar NV21 to packed YUV24 |
| 3362 | void NV21ToYUV24Row_C(const uint8_t* src_y, |
| 3363 | const uint8_t* src_vu, |
| 3364 | uint8_t* dst_yuv24, |
| 3365 | int width) { |
| 3366 | int x; |
| 3367 | for (x = 0; x < width - 1; x += 2) { |
| 3368 | dst_yuv24[0] = src_vu[0]; // V |
| 3369 | dst_yuv24[1] = src_vu[1]; // U |
| 3370 | dst_yuv24[2] = src_y[0]; // Y0 |
| 3371 | dst_yuv24[3] = src_vu[0]; // V |
| 3372 | dst_yuv24[4] = src_vu[1]; // U |
| 3373 | dst_yuv24[5] = src_y[1]; // Y1 |
| 3374 | src_y += 2; |
| 3375 | src_vu += 2; |
| 3376 | dst_yuv24 += 6; // Advance 2 pixels. |
| 3377 | } |
| 3378 | if (width & 1) { |
| 3379 | dst_yuv24[0] = src_vu[0]; // V |
| 3380 | dst_yuv24[1] = src_vu[1]; // U |
| 3381 | dst_yuv24[2] = src_y[0]; // Y0 |
| 3382 | } |
| 3383 | } |
| 3384 |
| 3385 | // Filter 2 rows of AYUV UV's (444) into UV (420). |
| 3386 | void AYUVToUVRow_C(const uint8_t* src_ayuv, |
| 3387 | int src_stride_ayuv, |
| 3388 | uint8_t* dst_uv, |
| 3389 | int width) { |
| 3390 | // Output a row of UV values, filtering 2x2 rows of AYUV. |
| 3391 | int x; |
| 3392 | for (x = 0; x < width; x += 2) { |
| 3393 | dst_uv[0] = (src_ayuv[1] + src_ayuv[5] + src_ayuv[src_stride_ayuv + 1] + |
| 3394 | src_ayuv[src_stride_ayuv + 5] + 2) >> |
| 3395 | 2; |
| 3396 | dst_uv[1] = (src_ayuv[0] + src_ayuv[4] + src_ayuv[src_stride_ayuv + 0] + |
| 3397 | src_ayuv[src_stride_ayuv + 4] + 2) >> |
| 3398 | 2; |
| 3399 | src_ayuv += 8; |
| 3400 | dst_uv += 2; |
| 3401 | } |
| 3402 | if (width & 1) { |
| 3403 | dst_uv[0] = (src_ayuv[0] + src_ayuv[0] + src_ayuv[src_stride_ayuv + 0] + |
| 3404 | src_ayuv[src_stride_ayuv + 0] + 2) >> |
| 3405 | 2; |
| 3406 | dst_uv[1] = (src_ayuv[1] + src_ayuv[1] + src_ayuv[src_stride_ayuv + 1] + |
| 3407 | src_ayuv[src_stride_ayuv + 1] + 2) >> |
| 3408 | 2; |
| 3409 | } |
| 3410 | } |
| 3411 |
| 3412 | // Filter 2 rows of AYUV UV's (444) into VU (420). |
| 3413 | void AYUVToVURow_C(const uint8_t* src_ayuv, |
| 3414 | int src_stride_ayuv, |
| 3415 | uint8_t* dst_vu, |
| 3416 | int width) { |
| 3417 | // Output a row of VU values, filtering 2x2 rows of AYUV. |
| 3418 | int x; |
| 3419 | for (x = 0; x < width; x += 2) { |
| 3420 | dst_vu[0] = (src_ayuv[0] + src_ayuv[4] + src_ayuv[src_stride_ayuv + 0] + |
| 3421 | src_ayuv[src_stride_ayuv + 4] + 2) >> |
| 3422 | 2; |
| 3423 | dst_vu[1] = (src_ayuv[1] + src_ayuv[5] + src_ayuv[src_stride_ayuv + 1] + |
| 3424 | src_ayuv[src_stride_ayuv + 5] + 2) >> |
| 3425 | 2; |
| 3426 | src_ayuv += 8; |
| 3427 | dst_vu += 2; |
| 3428 | } |
| 3429 | if (width & 1) { |
| 3430 | dst_vu[0] = (src_ayuv[0] + src_ayuv[0] + src_ayuv[src_stride_ayuv + 0] + |
| 3431 | src_ayuv[src_stride_ayuv + 0] + 2) >> |
| 3432 | 2; |
| 3433 | dst_vu[1] = (src_ayuv[1] + src_ayuv[1] + src_ayuv[src_stride_ayuv + 1] + |
| 3434 | src_ayuv[src_stride_ayuv + 1] + 2) >> |
| 3435 | 2; |
| 3436 | } |
| 3437 | } |
| 3438 |
| 3439 | // Copy row of AYUV Y's into Y |
| 3440 | void AYUVToYRow_C(const uint8_t* src_ayuv, uint8_t* dst_y, int width) { |
| 3441 | // Output a row of Y values. |
| 3442 | int x; |
| 3443 | for (x = 0; x < width; ++x) { |
| 3444 | dst_y[x] = src_ayuv[2]; // v,u,y,a |
| 3445 | src_ayuv += 4; |
| 3446 | } |
| 3447 | } |
| 3448 |
| 3449 | // Convert UV plane of NV12 to VU of NV21. |
| 3450 | void SwapUVRow_C(const uint8_t* src_uv, uint8_t* dst_vu, int width) { |
| 3451 | int x; |
| 3452 | for (x = 0; x < width; ++x) { |
| 3453 | uint8_t u = src_uv[0]; |
| 3454 | uint8_t v = src_uv[1]; |
| 3455 | dst_vu[0] = v; |
| 3456 | dst_vu[1] = u; |
| 3457 | src_uv += 2; |
| 3458 | dst_vu += 2; |
| 3459 | } |
| 3460 | } |
| 3461 |
| 3462 | #ifdef __cplusplus |
| 3463 | } // extern "C" |
| 3464 | } // namespace libyuv |
| 3465 | #endif |
| 3466 |