row_common.cc 84.1 KB
Newer Older
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
8 9 10
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include "libyuv/row.h"
12

13
#include <string.h>  // For memcpy and memset.
frkoenig@google.com's avatar
frkoenig@google.com committed
14

15 16
#include "libyuv/basic_types.h"

17 18
#ifdef __cplusplus
namespace libyuv {
19
extern "C" {
20
#endif
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35
// llvm x86 is poor at ternary operator, so use branchless min/max.

#define USE_BRANCHLESS 1
#if USE_BRANCHLESS
static __inline int32 clamp0(int32 v) {
  return ((-(v) >> 31) & (v));
}

static __inline int32 clamp255(int32 v) {
  return (((255 - (v)) >> 31) | (v)) & 255;
}

static __inline uint32 Clamp(int32 val) {
  int v = clamp0(val);
36
  return (uint32)(clamp255(v));
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
}

static __inline uint32 Abs(int32 v) {
  int m = v >> 31;
  return (v + m) ^ m;
}
#else  // USE_BRANCHLESS
static __inline int32 clamp0(int32 v) {
  return (v < 0) ? 0 : v;
}

static __inline int32 clamp255(int32 v) {
  return (v > 255) ? 255 : v;
}

static __inline uint32 Clamp(int32 val) {
  int v = clamp0(val);
54
  return (uint32)(clamp255(v));
55 56 57 58 59 60 61
}

static __inline uint32 Abs(int32 v) {
  return (v < 0) ? -v : v;
}
#endif  // USE_BRANCHLESS

62
#ifdef LIBYUV_LITTLE_ENDIAN
63
#define WRITEWORD(p, v) *(uint32*)(p) = v
64 65 66 67 68 69 70 71 72
#else
static inline void WRITEWORD(uint8* p, uint32 v) {
  p[0] = (uint8)(v & 255);
  p[1] = (uint8)((v >> 8) & 255);
  p[2] = (uint8)((v >> 16) & 255);
  p[3] = (uint8)((v >> 24) & 255);
}
#endif

73
void RGB24ToARGBRow_C(const uint8* src_rgb24, uint8* dst_argb, int width) {
74 75
  int x;
  for (x = 0; x < width; ++x) {
76 77 78
    uint8 b = src_rgb24[0];
    uint8 g = src_rgb24[1];
    uint8 r = src_rgb24[2];
79 80 81 82 83
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = 255u;
    dst_argb += 4;
84
    src_rgb24 += 3;
85 86 87
  }
}

88
void RAWToARGBRow_C(const uint8* src_raw, uint8* dst_argb, int width) {
89 90
  int x;
  for (x = 0; x < width; ++x) {
91 92 93
    uint8 r = src_raw[0];
    uint8 g = src_raw[1];
    uint8 b = src_raw[2];
94 95 96 97 98
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = 255u;
    dst_argb += 4;
99
    src_raw += 3;
100 101 102
  }
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116
void RAWToRGB24Row_C(const uint8* src_raw, uint8* dst_rgb24, int width) {
  int x;
  for (x = 0; x < width; ++x) {
    uint8 r = src_raw[0];
    uint8 g = src_raw[1];
    uint8 b = src_raw[2];
    dst_rgb24[0] = b;
    dst_rgb24[1] = g;
    dst_rgb24[2] = r;
    dst_rgb24 += 3;
    src_raw += 3;
  }
}

117
void RGB565ToARGBRow_C(const uint8* src_rgb565, uint8* dst_argb, int width) {
118 119
  int x;
  for (x = 0; x < width; ++x) {
120 121 122
    uint8 b = src_rgb565[0] & 0x1f;
    uint8 g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8 r = src_rgb565[1] >> 3;
123 124 125 126 127
    dst_argb[0] = (b << 3) | (b >> 2);
    dst_argb[1] = (g << 2) | (g >> 4);
    dst_argb[2] = (r << 3) | (r >> 2);
    dst_argb[3] = 255u;
    dst_argb += 4;
128
    src_rgb565 += 2;
129 130 131
  }
}

132 133
void ARGB1555ToARGBRow_C(const uint8* src_argb1555, uint8* dst_argb,
                         int width) {
134 135
  int x;
  for (x = 0; x < width; ++x) {
136 137 138 139
    uint8 b = src_argb1555[0] & 0x1f;
    uint8 g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8 r = (src_argb1555[1] & 0x7c) >> 2;
    uint8 a = src_argb1555[1] >> 7;
140 141 142
    dst_argb[0] = (b << 3) | (b >> 2);
    dst_argb[1] = (g << 3) | (g >> 2);
    dst_argb[2] = (r << 3) | (r >> 2);
143
    dst_argb[3] = -a;
144
    dst_argb += 4;
145
    src_argb1555 += 2;
146 147 148
  }
}

149 150
void ARGB4444ToARGBRow_C(const uint8* src_argb4444, uint8* dst_argb,
                         int width) {
151 152
  int x;
  for (x = 0; x < width; ++x) {
153 154 155 156
    uint8 b = src_argb4444[0] & 0x0f;
    uint8 g = src_argb4444[0] >> 4;
    uint8 r = src_argb4444[1] & 0x0f;
    uint8 a = src_argb4444[1] >> 4;
157 158 159 160 161
    dst_argb[0] = (b << 4) | b;
    dst_argb[1] = (g << 4) | g;
    dst_argb[2] = (r << 4) | r;
    dst_argb[3] = (a << 4) | a;
    dst_argb += 4;
162
    src_argb4444 += 2;
163 164 165
  }
}

166
void ARGBToRGB24Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
167 168
  int x;
  for (x = 0; x < width; ++x) {
169 170 171 172 173 174 175 176 177 178 179
    uint8 b = src_argb[0];
    uint8 g = src_argb[1];
    uint8 r = src_argb[2];
    dst_rgb[0] = b;
    dst_rgb[1] = g;
    dst_rgb[2] = r;
    dst_rgb += 3;
    src_argb += 4;
  }
}

180
void ARGBToRAWRow_C(const uint8* src_argb, uint8* dst_rgb, int width) {
181 182
  int x;
  for (x = 0; x < width; ++x) {
183 184 185 186 187 188 189 190 191 192 193
    uint8 b = src_argb[0];
    uint8 g = src_argb[1];
    uint8 r = src_argb[2];
    dst_rgb[0] = r;
    dst_rgb[1] = g;
    dst_rgb[2] = b;
    dst_rgb += 3;
    src_argb += 4;
  }
}

194
void ARGBToRGB565Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
195 196
  int x;
  for (x = 0; x < width - 1; x += 2) {
197 198 199 200 201 202
    uint8 b0 = src_argb[0] >> 3;
    uint8 g0 = src_argb[1] >> 2;
    uint8 r0 = src_argb[2] >> 3;
    uint8 b1 = src_argb[4] >> 3;
    uint8 g1 = src_argb[5] >> 2;
    uint8 r1 = src_argb[6] >> 3;
203
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) |
204
              (b1 << 16) | (g1 << 21) | (r1 << 27));
205 206 207 208 209 210 211
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
    uint8 b0 = src_argb[0] >> 3;
    uint8 g0 = src_argb[1] >> 2;
    uint8 r0 = src_argb[2] >> 3;
212
    *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
213 214 215
  }
}

216 217 218 219 220 221 222 223
// dither4 is a row of 4 values from 4x4 dither matrix.
// The 4x4 matrix contains values to increase RGB.  When converting to
// fewer bits (565) this provides an ordered dither.
// The order in the 4x4 matrix in first byte is upper left.
// The 4 values are passed as an int, then referenced as an array, so
// endian will not affect order of the original matrix.  But the dither4
// will containing the first pixel in the lower byte for little endian
// or the upper byte for big endian.
224
void ARGBToRGB565DitherRow_C(const uint8* src_argb, uint8* dst_rgb,
225
                             const uint32 dither4, int width) {
226 227
  int x;
  for (x = 0; x < width - 1; x += 2) {
228 229
    int dither0 = ((const unsigned char*)(&dither4))[x & 3];
    int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3];
230 231 232 233 234 235
    uint8 b0 = clamp255(src_argb[0] + dither0) >> 3;
    uint8 g0 = clamp255(src_argb[1] + dither0) >> 2;
    uint8 r0 = clamp255(src_argb[2] + dither0) >> 3;
    uint8 b1 = clamp255(src_argb[4] + dither1) >> 3;
    uint8 g1 = clamp255(src_argb[5] + dither1) >> 2;
    uint8 r1 = clamp255(src_argb[6] + dither1) >> 3;
236 237 238 239 240 241
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) |
              (b1 << 16) | (g1 << 21) | (r1 << 27));
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
242
    int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3];
243 244 245
    uint8 b0 = clamp255(src_argb[0] + dither0) >> 3;
    uint8 g0 = clamp255(src_argb[1] + dither0) >> 2;
    uint8 r0 = clamp255(src_argb[2] + dither0) >> 3;
246 247 248 249
    *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
  }
}

250
void ARGBToARGB1555Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
251 252
  int x;
  for (x = 0; x < width - 1; x += 2) {
253 254 255 256 257 258 259 260
    uint8 b0 = src_argb[0] >> 3;
    uint8 g0 = src_argb[1] >> 3;
    uint8 r0 = src_argb[2] >> 3;
    uint8 a0 = src_argb[3] >> 7;
    uint8 b1 = src_argb[4] >> 3;
    uint8 g1 = src_argb[5] >> 3;
    uint8 r1 = src_argb[6] >> 3;
    uint8 a1 = src_argb[7] >> 7;
261
    *(uint32*)(dst_rgb) =
262 263 264 265 266 267 268 269 270 271
        b0 | (g0 << 5) | (r0 << 10) | (a0 << 15) |
        (b1 << 16) | (g1 << 21) | (r1 << 26) | (a1 << 31);
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
    uint8 b0 = src_argb[0] >> 3;
    uint8 g0 = src_argb[1] >> 3;
    uint8 r0 = src_argb[2] >> 3;
    uint8 a0 = src_argb[3] >> 7;
272
    *(uint16*)(dst_rgb) =
273
        b0 | (g0 << 5) | (r0 << 10) | (a0 << 15);
274 275 276
  }
}

277
void ARGBToARGB4444Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
278 279
  int x;
  for (x = 0; x < width - 1; x += 2) {
280 281 282 283 284 285 286 287
    uint8 b0 = src_argb[0] >> 4;
    uint8 g0 = src_argb[1] >> 4;
    uint8 r0 = src_argb[2] >> 4;
    uint8 a0 = src_argb[3] >> 4;
    uint8 b1 = src_argb[4] >> 4;
    uint8 g1 = src_argb[5] >> 4;
    uint8 r1 = src_argb[6] >> 4;
    uint8 a1 = src_argb[7] >> 4;
288
    *(uint32*)(dst_rgb) =
289 290 291 292 293 294 295 296 297 298
        b0 | (g0 << 4) | (r0 << 8) | (a0 << 12) |
        (b1 << 16) | (g1 << 20) | (r1 << 24) | (a1 << 28);
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
    uint8 b0 = src_argb[0] >> 4;
    uint8 g0 = src_argb[1] >> 4;
    uint8 r0 = src_argb[2] >> 4;
    uint8 a0 = src_argb[3] >> 4;
299
    *(uint16*)(dst_rgb) =
300
        b0 | (g0 << 4) | (r0 << 8) | (a0 << 12);
301 302 303
  }
}

304
static __inline int RGBToY(uint8 r, uint8 g, uint8 b) {
305
  return (66 * r + 129 * g +  25 * b + 0x1080) >> 8;
306 307
}

308
static __inline int RGBToU(uint8 r, uint8 g, uint8 b) {
309
  return (112 * b - 74 * g - 38 * r + 0x8080) >> 8;
310
}
311
static __inline int RGBToV(uint8 r, uint8 g, uint8 b) {
312
  return (112 * r - 94 * g - 18 * b + 0x8080) >> 8;
313 314
}

315
#define MAKEROWY(NAME, R, G, B, BPP) \
316
void NAME ## ToYRow_C(const uint8* src_argb0, uint8* dst_y, int width) {       \
317 318
  int x;                                                                       \
  for (x = 0; x < width; ++x) {                                                \
319
    dst_y[0] = RGBToY(src_argb0[R], src_argb0[G], src_argb0[B]);               \
320
    src_argb0 += BPP;                                                          \
321 322 323 324 325 326
    dst_y += 1;                                                                \
  }                                                                            \
}                                                                              \
void NAME ## ToUVRow_C(const uint8* src_rgb0, int src_stride_rgb,              \
                       uint8* dst_u, uint8* dst_v, int width) {                \
  const uint8* src_rgb1 = src_rgb0 + src_stride_rgb;                           \
327 328
  int x;                                                                       \
  for (x = 0; x < width - 1; x += 2) {                                         \
329 330 331 332 333 334
    uint8 ab = (src_rgb0[B] + src_rgb0[B + BPP] +                              \
               src_rgb1[B] + src_rgb1[B + BPP]) >> 2;                          \
    uint8 ag = (src_rgb0[G] + src_rgb0[G + BPP] +                              \
               src_rgb1[G] + src_rgb1[G + BPP]) >> 2;                          \
    uint8 ar = (src_rgb0[R] + src_rgb0[R + BPP] +                              \
               src_rgb1[R] + src_rgb1[R + BPP]) >> 2;                          \
335 336
    dst_u[0] = RGBToU(ar, ag, ab);                                             \
    dst_v[0] = RGBToV(ar, ag, ab);                                             \
337 338
    src_rgb0 += BPP * 2;                                                       \
    src_rgb1 += BPP * 2;                                                       \
339 340 341 342 343 344 345 346 347 348 349 350
    dst_u += 1;                                                                \
    dst_v += 1;                                                                \
  }                                                                            \
  if (width & 1) {                                                             \
    uint8 ab = (src_rgb0[B] + src_rgb1[B]) >> 1;                               \
    uint8 ag = (src_rgb0[G] + src_rgb1[G]) >> 1;                               \
    uint8 ar = (src_rgb0[R] + src_rgb1[R]) >> 1;                               \
    dst_u[0] = RGBToU(ar, ag, ab);                                             \
    dst_v[0] = RGBToV(ar, ag, ab);                                             \
  }                                                                            \
}

351 352 353 354 355 356 357 358
MAKEROWY(ARGB, 2, 1, 0, 4)
MAKEROWY(BGRA, 1, 2, 3, 4)
MAKEROWY(ABGR, 0, 1, 2, 4)
MAKEROWY(RGBA, 3, 2, 1, 4)
MAKEROWY(RGB24, 2, 1, 0, 3)
MAKEROWY(RAW, 0, 1, 2, 3)
#undef MAKEROWY

359 360 361 362 363
// JPeg uses a variation on BT.601-1 full range
// y =  0.29900 * r + 0.58700 * g + 0.11400 * b
// u = -0.16874 * r - 0.33126 * g + 0.50000 * b  + center
// v =  0.50000 * r - 0.41869 * g - 0.08131 * b  + center
// BT.601 Mpeg range uses:
364 365 366
// b 0.1016 * 255 = 25.908 = 25
// g 0.5078 * 255 = 129.489 = 129
// r 0.2578 * 255 = 65.739 = 66
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
// JPeg 8 bit Y (not used):
// b 0.11400 * 256 = 29.184 = 29
// g 0.58700 * 256 = 150.272 = 150
// r 0.29900 * 256 = 76.544 = 77
// JPeg 7 bit Y:
// b 0.11400 * 128 = 14.592 = 15
// g 0.58700 * 128 = 75.136 = 75
// r 0.29900 * 128 = 38.272 = 38
// JPeg 8 bit U:
// b  0.50000 * 255 = 127.5 = 127
// g -0.33126 * 255 = -84.4713 = -84
// r -0.16874 * 255 = -43.0287 = -43
// JPeg 8 bit V:
// b -0.08131 * 255 = -20.73405 = -20
// g -0.41869 * 255 = -106.76595 = -107
// r  0.50000 * 255 = 127.5 = 127
383

384
static __inline int RGBToYJ(uint8 r, uint8 g, uint8 b) {
385
  return (38 * r + 75 * g +  15 * b + 64) >> 7;
386 387
}

388 389 390 391 392 393 394 395 396
static __inline int RGBToUJ(uint8 r, uint8 g, uint8 b) {
  return (127 * b - 84 * g - 43 * r + 0x8080) >> 8;
}
static __inline int RGBToVJ(uint8 r, uint8 g, uint8 b) {
  return (127 * r - 107 * g - 20 * b + 0x8080) >> 8;
}

#define AVGB(a, b) (((a) + (b) + 1) >> 1)

397 398
#define MAKEROWYJ(NAME, R, G, B, BPP) \
void NAME ## ToYJRow_C(const uint8* src_argb0, uint8* dst_y, int width) {      \
399 400
  int x;                                                                       \
  for (x = 0; x < width; ++x) {                                                \
401 402 403 404 405
    dst_y[0] = RGBToYJ(src_argb0[R], src_argb0[G], src_argb0[B]);              \
    src_argb0 += BPP;                                                          \
    dst_y += 1;                                                                \
  }                                                                            \
}                                                                              \
406 407 408
void NAME ## ToUVJRow_C(const uint8* src_rgb0, int src_stride_rgb,             \
                        uint8* dst_u, uint8* dst_v, int width) {               \
  const uint8* src_rgb1 = src_rgb0 + src_stride_rgb;                           \
409 410
  int x;                                                                       \
  for (x = 0; x < width - 1; x += 2) {                                         \
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    uint8 ab = AVGB(AVGB(src_rgb0[B], src_rgb1[B]),                            \
                    AVGB(src_rgb0[B + BPP], src_rgb1[B + BPP]));               \
    uint8 ag = AVGB(AVGB(src_rgb0[G], src_rgb1[G]),                            \
                    AVGB(src_rgb0[G + BPP], src_rgb1[G + BPP]));               \
    uint8 ar = AVGB(AVGB(src_rgb0[R], src_rgb1[R]),                            \
                    AVGB(src_rgb0[R + BPP], src_rgb1[R + BPP]));               \
    dst_u[0] = RGBToUJ(ar, ag, ab);                                            \
    dst_v[0] = RGBToVJ(ar, ag, ab);                                            \
    src_rgb0 += BPP * 2;                                                       \
    src_rgb1 += BPP * 2;                                                       \
    dst_u += 1;                                                                \
    dst_v += 1;                                                                \
  }                                                                            \
  if (width & 1) {                                                             \
    uint8 ab = AVGB(src_rgb0[B], src_rgb1[B]);                                 \
    uint8 ag = AVGB(src_rgb0[G], src_rgb1[G]);                                 \
    uint8 ar = AVGB(src_rgb0[R], src_rgb1[R]);                                 \
    dst_u[0] = RGBToUJ(ar, ag, ab);                                            \
    dst_v[0] = RGBToVJ(ar, ag, ab);                                            \
  }                                                                            \
}
432 433 434 435

MAKEROWYJ(ARGB, 2, 1, 0, 4)
#undef MAKEROWYJ

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
void ARGBToUVJ422Row_C(const uint8* src_argb,
                       uint8* dst_u, uint8* dst_v, int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
    uint8 ab = (src_argb[0] + src_argb[4]) >> 1;
    uint8 ag = (src_argb[1] + src_argb[5]) >> 1;
    uint8 ar = (src_argb[2] + src_argb[6]) >> 1;
    dst_u[0] = RGBToUJ(ar, ag, ab);
    dst_v[0] = RGBToVJ(ar, ag, ab);
    src_argb += 8;
    dst_u += 1;
    dst_v += 1;
  }
  if (width & 1) {
    uint8 ab = src_argb[0];
    uint8 ag = src_argb[1];
    uint8 ar = src_argb[2];
    dst_u[0] = RGBToUJ(ar, ag, ab);
    dst_v[0] = RGBToVJ(ar, ag, ab);
  }
}

458
void RGB565ToYRow_C(const uint8* src_rgb565, uint8* dst_y, int width) {
459 460
  int x;
  for (x = 0; x < width; ++x) {
461 462 463 464 465 466 467 468 469 470 471 472
    uint8 b = src_rgb565[0] & 0x1f;
    uint8 g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8 r = src_rgb565[1] >> 3;
    b = (b << 3) | (b >> 2);
    g = (g << 2) | (g >> 4);
    r = (r << 3) | (r >> 2);
    dst_y[0] = RGBToY(r, g, b);
    src_rgb565 += 2;
    dst_y += 1;
  }
}

473
void ARGB1555ToYRow_C(const uint8* src_argb1555, uint8* dst_y, int width) {
474 475
  int x;
  for (x = 0; x < width; ++x) {
476 477 478 479 480 481 482 483 484 485 486 487 488
    uint8 b = src_argb1555[0] & 0x1f;
    uint8 g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8 r = (src_argb1555[1] & 0x7c) >> 2;
    b = (b << 3) | (b >> 2);
    g = (g << 3) | (g >> 2);
    r = (r << 3) | (r >> 2);
    dst_y[0] = RGBToY(r, g, b);
    src_argb1555 += 2;
    dst_y += 1;
  }
}

void ARGB4444ToYRow_C(const uint8* src_argb4444, uint8* dst_y, int width) {
489 490
  int x;
  for (x = 0; x < width; ++x) {
491 492 493 494 495 496 497 498 499 500 501 502
    uint8 b = src_argb4444[0] & 0x0f;
    uint8 g = src_argb4444[0] >> 4;
    uint8 r = src_argb4444[1] & 0x0f;
    b = (b << 4) | b;
    g = (g << 4) | g;
    r = (r << 4) | r;
    dst_y[0] = RGBToY(r, g, b);
    src_argb4444 += 2;
    dst_y += 1;
  }
}

fbarchard@google.com's avatar
fbarchard@google.com committed
503
void RGB565ToUVRow_C(const uint8* src_rgb565, int src_stride_rgb565,
504
                     uint8* dst_u, uint8* dst_v, int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
505
  const uint8* next_rgb565 = src_rgb565 + src_stride_rgb565;
506 507
  int x;
  for (x = 0; x < width - 1; x += 2) {
fbarchard@google.com's avatar
fbarchard@google.com committed
508 509 510 511 512 513 514 515 516 517 518 519
    uint8 b0 = src_rgb565[0] & 0x1f;
    uint8 g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8 r0 = src_rgb565[1] >> 3;
    uint8 b1 = src_rgb565[2] & 0x1f;
    uint8 g1 = (src_rgb565[2] >> 5) | ((src_rgb565[3] & 0x07) << 3);
    uint8 r1 = src_rgb565[3] >> 3;
    uint8 b2 = next_rgb565[0] & 0x1f;
    uint8 g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
    uint8 r2 = next_rgb565[1] >> 3;
    uint8 b3 = next_rgb565[2] & 0x1f;
    uint8 g3 = (next_rgb565[2] >> 5) | ((next_rgb565[3] & 0x07) << 3);
    uint8 r3 = next_rgb565[3] >> 3;
520 521 522 523 524 525 526
    uint8 b = (b0 + b1 + b2 + b3);  // 565 * 4 = 787.
    uint8 g = (g0 + g1 + g2 + g3);
    uint8 r = (r0 + r1 + r2 + r3);
    b = (b << 1) | (b >> 6);  // 787 -> 888.
    r = (r << 1) | (r >> 6);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
fbarchard@google.com's avatar
fbarchard@google.com committed
527 528 529 530 531 532 533 534 535 536 537 538
    src_rgb565 += 4;
    next_rgb565 += 4;
    dst_u += 1;
    dst_v += 1;
  }
  if (width & 1) {
    uint8 b0 = src_rgb565[0] & 0x1f;
    uint8 g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8 r0 = src_rgb565[1] >> 3;
    uint8 b2 = next_rgb565[0] & 0x1f;
    uint8 g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
    uint8 r2 = next_rgb565[1] >> 3;
539 540 541 542 543 544 545 546 547 548 549 550 551 552
    uint8 b = (b0 + b2);  // 565 * 2 = 676.
    uint8 g = (g0 + g2);
    uint8 r = (r0 + r2);
    b = (b << 2) | (b >> 4);  // 676 -> 888
    g = (g << 1) | (g >> 6);
    r = (r << 2) | (r >> 4);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
  }
}

void ARGB1555ToUVRow_C(const uint8* src_argb1555, int src_stride_argb1555,
                       uint8* dst_u, uint8* dst_v, int width) {
  const uint8* next_argb1555 = src_argb1555 + src_stride_argb1555;
553 554
  int x;
  for (x = 0; x < width - 1; x += 2) {
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    uint8 b0 = src_argb1555[0] & 0x1f;
    uint8 g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8 r0 = (src_argb1555[1] & 0x7c) >> 2;
    uint8 b1 = src_argb1555[2] & 0x1f;
    uint8 g1 = (src_argb1555[2] >> 5) | ((src_argb1555[3] & 0x03) << 3);
    uint8 r1 = (src_argb1555[3] & 0x7c) >> 2;
    uint8 b2 = next_argb1555[0] & 0x1f;
    uint8 g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
    uint8 r2 = (next_argb1555[1] & 0x7c) >> 2;
    uint8 b3 = next_argb1555[2] & 0x1f;
    uint8 g3 = (next_argb1555[2] >> 5) | ((next_argb1555[3] & 0x03) << 3);
    uint8 r3 = (next_argb1555[3] & 0x7c) >> 2;
    uint8 b = (b0 + b1 + b2 + b3);  // 555 * 4 = 777.
    uint8 g = (g0 + g1 + g2 + g3);
    uint8 r = (r0 + r1 + r2 + r3);
    b = (b << 1) | (b >> 6);  // 777 -> 888.
    g = (g << 1) | (g >> 6);
    r = (r << 1) | (r >> 6);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
    src_argb1555 += 4;
    next_argb1555 += 4;
    dst_u += 1;
    dst_v += 1;
  }
  if (width & 1) {
    uint8 b0 = src_argb1555[0] & 0x1f;
    uint8 g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8 r0 = (src_argb1555[1] & 0x7c) >> 2;
    uint8 b2 = next_argb1555[0] & 0x1f;
    uint8 g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
    uint8 r2 = next_argb1555[1] >> 3;
    uint8 b = (b0 + b2);  // 555 * 2 = 666.
    uint8 g = (g0 + g2);
    uint8 r = (r0 + r2);
    b = (b << 2) | (b >> 4);  // 666 -> 888.
    g = (g << 2) | (g >> 4);
    r = (r << 2) | (r >> 4);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
  }
}

void ARGB4444ToUVRow_C(const uint8* src_argb4444, int src_stride_argb4444,
                       uint8* dst_u, uint8* dst_v, int width) {
  const uint8* next_argb4444 = src_argb4444 + src_stride_argb4444;
601 602
  int x;
  for (x = 0; x < width - 1; x += 2) {
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
    uint8 b0 = src_argb4444[0] & 0x0f;
    uint8 g0 = src_argb4444[0] >> 4;
    uint8 r0 = src_argb4444[1] & 0x0f;
    uint8 b1 = src_argb4444[2] & 0x0f;
    uint8 g1 = src_argb4444[2] >> 4;
    uint8 r1 = src_argb4444[3] & 0x0f;
    uint8 b2 = next_argb4444[0] & 0x0f;
    uint8 g2 = next_argb4444[0] >> 4;
    uint8 r2 = next_argb4444[1] & 0x0f;
    uint8 b3 = next_argb4444[2] & 0x0f;
    uint8 g3 = next_argb4444[2] >> 4;
    uint8 r3 = next_argb4444[3] & 0x0f;
    uint8 b = (b0 + b1 + b2 + b3);  // 444 * 4 = 666.
    uint8 g = (g0 + g1 + g2 + g3);
    uint8 r = (r0 + r1 + r2 + r3);
    b = (b << 2) | (b >> 4);  // 666 -> 888.
    g = (g << 2) | (g >> 4);
    r = (r << 2) | (r >> 4);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
    src_argb4444 += 4;
    next_argb4444 += 4;
    dst_u += 1;
    dst_v += 1;
  }
  if (width & 1) {
    uint8 b0 = src_argb4444[0] & 0x0f;
    uint8 g0 = src_argb4444[0] >> 4;
    uint8 r0 = src_argb4444[1] & 0x0f;
    uint8 b2 = next_argb4444[0] & 0x0f;
    uint8 g2 = next_argb4444[0] >> 4;
    uint8 r2 = next_argb4444[1] & 0x0f;
    uint8 b = (b0 + b2);  // 444 * 2 = 555.
    uint8 g = (g0 + g2);
    uint8 r = (r0 + r2);
    b = (b << 3) | (b >> 2);  // 555 -> 888.
    g = (g << 3) | (g >> 2);
    r = (r << 3) | (r >> 2);
    dst_u[0] = RGBToU(r, g, b);
    dst_v[0] = RGBToV(r, g, b);
fbarchard@google.com's avatar
fbarchard@google.com committed
643 644 645
  }
}

646 647
void ARGBToUV444Row_C(const uint8* src_argb,
                      uint8* dst_u, uint8* dst_v, int width) {
648 649
  int x;
  for (x = 0; x < width; ++x) {
650 651 652 653 654 655 656 657 658 659 660 661 662
    uint8 ab = src_argb[0];
    uint8 ag = src_argb[1];
    uint8 ar = src_argb[2];
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
    src_argb += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

void ARGBToUV422Row_C(const uint8* src_argb,
                      uint8* dst_u, uint8* dst_v, int width) {
663 664
  int x;
  for (x = 0; x < width - 1; x += 2) {
665 666 667 668 669 670 671 672 673
    uint8 ab = (src_argb[0] + src_argb[4]) >> 1;
    uint8 ag = (src_argb[1] + src_argb[5]) >> 1;
    uint8 ar = (src_argb[2] + src_argb[6]) >> 1;
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
    src_argb += 8;
    dst_u += 1;
    dst_v += 1;
  }
674
  if (width & 1) {
675 676 677 678 679 680 681 682 683 684
    uint8 ab = src_argb[0];
    uint8 ag = src_argb[1];
    uint8 ar = src_argb[2];
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
  }
}

void ARGBToUV411Row_C(const uint8* src_argb,
                      uint8* dst_u, uint8* dst_v, int width) {
685 686
  int x;
  for (x = 0; x < width - 3; x += 4) {
687 688 689 690 691 692 693 694 695
    uint8 ab = (src_argb[0] + src_argb[4] + src_argb[8] + src_argb[12]) >> 2;
    uint8 ag = (src_argb[1] + src_argb[5] + src_argb[9] + src_argb[13]) >> 2;
    uint8 ar = (src_argb[2] + src_argb[6] + src_argb[10] + src_argb[14]) >> 2;
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
    src_argb += 16;
    dst_u += 1;
    dst_v += 1;
  }
696
  // Odd width handling mimics 'any' function which replicates last pixel.
697
  if ((width & 3) == 3) {
698 699 700
    uint8 ab = (src_argb[0] + src_argb[4] + src_argb[8] + src_argb[8]) >> 2;
    uint8 ag = (src_argb[1] + src_argb[5] + src_argb[9] + src_argb[9]) >> 2;
    uint8 ar = (src_argb[2] + src_argb[6] + src_argb[10] + src_argb[10]) >> 2;
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
  } else if ((width & 3) == 2) {
    uint8 ab = (src_argb[0] + src_argb[4]) >> 1;
    uint8 ag = (src_argb[1] + src_argb[5]) >> 1;
    uint8 ar = (src_argb[2] + src_argb[6]) >> 1;
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
  } else if ((width & 3) == 1) {
    uint8 ab = src_argb[0];
    uint8 ag = src_argb[1];
    uint8 ar = src_argb[2];
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
  }
}
717

718
void ARGBGrayRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
719 720
  int x;
  for (x = 0; x < width; ++x) {
721
    uint8 y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
722
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
723
    dst_argb[3] = src_argb[3];
724
    dst_argb += 4;
725
    src_argb += 4;
726 727 728
  }
}

729 730
// Convert a row of image to Sepia tone.
void ARGBSepiaRow_C(uint8* dst_argb, int width) {
731 732
  int x;
  for (x = 0; x < width; ++x) {
733 734 735
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
736 737 738
    int sb = (b * 17 + g * 68 + r * 35) >> 7;
    int sg = (b * 22 + g * 88 + r * 45) >> 7;
    int sr = (b * 24 + g * 98 + r * 50) >> 7;
739
    // b does not over flow. a is preserved from original.
740
    dst_argb[0] = sb;
741 742
    dst_argb[1] = clamp255(sg);
    dst_argb[2] = clamp255(sr);
743 744 745 746
    dst_argb += 4;
  }
}

747
// Apply color matrix to a row of image. Matrix is signed.
748 749 750
// TODO(fbarchard): Consider adding rounding (+32).
void ARGBColorMatrixRow_C(const uint8* src_argb, uint8* dst_argb,
                          const int8* matrix_argb, int width) {
751 752
  int x;
  for (x = 0; x < width; ++x) {
753 754 755 756
    int b = src_argb[0];
    int g = src_argb[1];
    int r = src_argb[2];
    int a = src_argb[3];
757
    int sb = (b * matrix_argb[0] + g * matrix_argb[1] +
758
              r * matrix_argb[2] + a * matrix_argb[3]) >> 6;
759
    int sg = (b * matrix_argb[4] + g * matrix_argb[5] +
760
              r * matrix_argb[6] + a * matrix_argb[7]) >> 6;
761
    int sr = (b * matrix_argb[8] + g * matrix_argb[9] +
762 763 764
              r * matrix_argb[10] + a * matrix_argb[11]) >> 6;
    int sa = (b * matrix_argb[12] + g * matrix_argb[13] +
              r * matrix_argb[14] + a * matrix_argb[15]) >> 6;
765 766 767
    dst_argb[0] = Clamp(sb);
    dst_argb[1] = Clamp(sg);
    dst_argb[2] = Clamp(sr);
768 769
    dst_argb[3] = Clamp(sa);
    src_argb += 4;
770 771 772 773
    dst_argb += 4;
  }
}

774 775
// Apply color table to a row of image.
void ARGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
776 777
  int x;
  for (x = 0; x < width; ++x) {
778 779 780 781 782 783 784 785 786 787 788 789
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
    int a = dst_argb[3];
    dst_argb[0] = table_argb[b * 4 + 0];
    dst_argb[1] = table_argb[g * 4 + 1];
    dst_argb[2] = table_argb[r * 4 + 2];
    dst_argb[3] = table_argb[a * 4 + 3];
    dst_argb += 4;
  }
}

790 791
// Apply color table to a row of image.
void RGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
792 793
  int x;
  for (x = 0; x < width; ++x) {
794 795 796 797 798 799 800 801 802 803
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
    dst_argb[0] = table_argb[b * 4 + 0];
    dst_argb[1] = table_argb[g * 4 + 1];
    dst_argb[2] = table_argb[r * 4 + 2];
    dst_argb += 4;
  }
}

804 805
void ARGBQuantizeRow_C(uint8* dst_argb, int scale, int interval_size,
                       int interval_offset, int width) {
806 807
  int x;
  for (x = 0; x < width; ++x) {
808 809 810 811 812 813 814 815 816 817
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
    dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
    dst_argb[1] = (g * scale >> 16) * interval_size + interval_offset;
    dst_argb[2] = (r * scale >> 16) * interval_size + interval_offset;
    dst_argb += 4;
  }
}

818 819 820 821 822 823 824 825 826 827
#define REPEAT8(v) (v) | ((v) << 8)
#define SHADE(f, v) v * f >> 24

void ARGBShadeRow_C(const uint8* src_argb, uint8* dst_argb, int width,
                    uint32 value) {
  const uint32 b_scale = REPEAT8(value & 0xff);
  const uint32 g_scale = REPEAT8((value >> 8) & 0xff);
  const uint32 r_scale = REPEAT8((value >> 16) & 0xff);
  const uint32 a_scale = REPEAT8(value >> 24);

828 829
  int i;
  for (i = 0; i < width; ++i) {
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    const uint32 b = REPEAT8(src_argb[0]);
    const uint32 g = REPEAT8(src_argb[1]);
    const uint32 r = REPEAT8(src_argb[2]);
    const uint32 a = REPEAT8(src_argb[3]);
    dst_argb[0] = SHADE(b, b_scale);
    dst_argb[1] = SHADE(g, g_scale);
    dst_argb[2] = SHADE(r, r_scale);
    dst_argb[3] = SHADE(a, a_scale);
    src_argb += 4;
    dst_argb += 4;
  }
}
#undef REPEAT8
#undef SHADE

845 846 847
#define REPEAT8(v) (v) | ((v) << 8)
#define SHADE(f, v) v * f >> 16

848 849
void ARGBMultiplyRow_C(const uint8* src_argb0, const uint8* src_argb1,
                       uint8* dst_argb, int width) {
850 851
  int i;
  for (i = 0; i < width; ++i) {
852 853 854 855 856 857 858 859
    const uint32 b = REPEAT8(src_argb0[0]);
    const uint32 g = REPEAT8(src_argb0[1]);
    const uint32 r = REPEAT8(src_argb0[2]);
    const uint32 a = REPEAT8(src_argb0[3]);
    const uint32 b_scale = src_argb1[0];
    const uint32 g_scale = src_argb1[1];
    const uint32 r_scale = src_argb1[2];
    const uint32 a_scale = src_argb1[3];
860 861 862 863
    dst_argb[0] = SHADE(b, b_scale);
    dst_argb[1] = SHADE(g, g_scale);
    dst_argb[2] = SHADE(r, r_scale);
    dst_argb[3] = SHADE(a, a_scale);
864 865
    src_argb0 += 4;
    src_argb1 += 4;
866 867 868 869 870 871
    dst_argb += 4;
  }
}
#undef REPEAT8
#undef SHADE

872
#define SHADE(f, v) clamp255(v + f)
873 874 875

void ARGBAddRow_C(const uint8* src_argb0, const uint8* src_argb1,
                  uint8* dst_argb, int width) {
876 877
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
878 879 880 881 882 883 884 885
    const int b = src_argb0[0];
    const int g = src_argb0[1];
    const int r = src_argb0[2];
    const int a = src_argb0[3];
    const int b_add = src_argb1[0];
    const int g_add = src_argb1[1];
    const int r_add = src_argb1[2];
    const int a_add = src_argb1[3];
886 887 888 889 890 891 892 893 894 895 896
    dst_argb[0] = SHADE(b, b_add);
    dst_argb[1] = SHADE(g, g_add);
    dst_argb[2] = SHADE(r, r_add);
    dst_argb[3] = SHADE(a, a_add);
    src_argb0 += 4;
    src_argb1 += 4;
    dst_argb += 4;
  }
}
#undef SHADE

897
#define SHADE(f, v) clamp0(f - v)
898 899 900

void ARGBSubtractRow_C(const uint8* src_argb0, const uint8* src_argb1,
                       uint8* dst_argb, int width) {
901 902
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
903 904 905 906 907 908 909 910
    const int b = src_argb0[0];
    const int g = src_argb0[1];
    const int r = src_argb0[2];
    const int a = src_argb0[3];
    const int b_sub = src_argb1[0];
    const int g_sub = src_argb1[1];
    const int r_sub = src_argb1[2];
    const int a_sub = src_argb1[3];
911 912 913 914 915 916 917 918 919 920 921
    dst_argb[0] = SHADE(b, b_sub);
    dst_argb[1] = SHADE(g, g_sub);
    dst_argb[2] = SHADE(r, r_sub);
    dst_argb[3] = SHADE(a, a_sub);
    src_argb0 += 4;
    src_argb1 += 4;
    dst_argb += 4;
  }
}
#undef SHADE

fbarchard@google.com's avatar
fbarchard@google.com committed
922 923 924
// Sobel functions which mimics SSSE3.
void SobelXRow_C(const uint8* src_y0, const uint8* src_y1, const uint8* src_y2,
                 uint8* dst_sobelx, int width) {
925 926
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
927 928 929 930 931 932 933 934 935
    int a = src_y0[i];
    int b = src_y1[i];
    int c = src_y2[i];
    int a_sub = src_y0[i + 2];
    int b_sub = src_y1[i + 2];
    int c_sub = src_y2[i + 2];
    int a_diff = a - a_sub;
    int b_diff = b - b_sub;
    int c_diff = c - c_sub;
936
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
937
    dst_sobelx[i] = (uint8)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
938 939 940 941 942
  }
}

void SobelYRow_C(const uint8* src_y0, const uint8* src_y1,
                 uint8* dst_sobely, int width) {
943 944
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
945 946 947 948 949 950 951 952 953
    int a = src_y0[i + 0];
    int b = src_y0[i + 1];
    int c = src_y0[i + 2];
    int a_sub = src_y1[i + 0];
    int b_sub = src_y1[i + 1];
    int c_sub = src_y1[i + 2];
    int a_diff = a - a_sub;
    int b_diff = b - b_sub;
    int c_diff = c - c_sub;
954
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
955
    dst_sobely[i] = (uint8)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
956 957 958
  }
}

959 960
void SobelRow_C(const uint8* src_sobelx, const uint8* src_sobely,
                uint8* dst_argb, int width) {
961 962
  int i;
  for (i = 0; i < width; ++i) {
963 964
    int r = src_sobelx[i];
    int b = src_sobely[i];
965
    int s = clamp255(r + b);
966 967 968 969
    dst_argb[0] = (uint8)(s);
    dst_argb[1] = (uint8)(s);
    dst_argb[2] = (uint8)(s);
    dst_argb[3] = (uint8)(255u);
970 971 972 973
    dst_argb += 4;
  }
}

974 975
void SobelToPlaneRow_C(const uint8* src_sobelx, const uint8* src_sobely,
                       uint8* dst_y, int width) {
976 977
  int i;
  for (i = 0; i < width; ++i) {
978 979 980
    int r = src_sobelx[i];
    int b = src_sobely[i];
    int s = clamp255(r + b);
981
    dst_y[i] = (uint8)(s);
982 983 984
  }
}

985 986
void SobelXYRow_C(const uint8* src_sobelx, const uint8* src_sobely,
                  uint8* dst_argb, int width) {
987 988
  int i;
  for (i = 0; i < width; ++i) {
989 990
    int r = src_sobelx[i];
    int b = src_sobely[i];
991
    int g = clamp255(r + b);
992 993 994 995
    dst_argb[0] = (uint8)(b);
    dst_argb[1] = (uint8)(g);
    dst_argb[2] = (uint8)(r);
    dst_argb[3] = (uint8)(255u);
996 997 998 999
    dst_argb += 4;
  }
}

1000
void J400ToARGBRow_C(const uint8* src_y, uint8* dst_argb, int width) {
1001
  // Copy a Y to RGB.
1002 1003
  int x;
  for (x = 0; x < width; ++x) {
1004 1005 1006 1007 1008 1009 1010 1011
    uint8 y = src_y[0];
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
    dst_argb[3] = 255u;
    dst_argb += 4;
    ++src_y;
  }
}

1012 1013 1014
// TODO(fbarchard): Unify these structures to be platform independent.
// TODO(fbarchard): Generate SIMD structures from float matrix.

1015 1016 1017 1018 1019
// BT.601 YUV to RGB reference
//  R = (Y - 16) * 1.164              - V * -1.596
//  G = (Y - 16) * 1.164 - U *  0.391 - V *  0.813
//  B = (Y - 16) * 1.164 - U * -2.018

1020
// Y contribution to R,G,B.  Scale and bias.
1021
#define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
1022
#define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
1023 1024

// U and V contributions to R,G,B.
1025 1026 1027 1028
#define UB -128 /* max(-128, round(-2.018 * 64)) */
#define UG 25 /* round(0.391 * 64) */
#define VG 52 /* round(0.813 * 64) */
#define VR -102 /* round(-1.596 * 64) */
1029

1030 1031
// Bias values to subtract 16 from Y and 128 from U and V.
#define BB (UB * 128            + YGB)
1032
#define BG (UG * 128 + VG * 128 + YGB)
1033 1034
#define BR            (VR * 128 + YGB)

1035
#if defined(__aarch64__)
1036
const YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
1037 1038 1039 1040
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { UG, VG, UG, VG, UG, VG, UG, VG },
  { UG, VG, UG, VG, UG, VG, UG, VG },
1041 1042
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
1043
};
1044
const YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
1045 1046 1047 1048 1049 1050 1051
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1052
#elif defined(__arm__)
1053
const YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
1054 1055
  { -UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0 },
  { UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0 },
1056 1057 1058
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1059
const YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
1060 1061 1062 1063 1064
  { -VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0 },
  { VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0 },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1065
#else
1066
const YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
  { UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
    UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0 },
  { UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
    UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG },
  { 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
    0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
};
1078
const YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
1079
  { VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
1080
    VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0 },
1081 1082 1083
  { VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
    VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG },
  { 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
1084
    0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB },
1085 1086 1087 1088 1089 1090 1091
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
};
#endif

1092 1093 1094
#undef BB
#undef BG
#undef BR
1095 1096 1097 1098 1099
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
1100 1101
#undef YG

1102 1103 1104 1105 1106 1107
// JPEG YUV to RGB reference
// *  R = Y                - V * -1.40200
// *  G = Y - U *  0.34414 - V *  0.71414
// *  B = Y - U * -1.77200

// Y contribution to R,G,B.  Scale and bias.
1108 1109
#define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
#define YGB 32  /* 64 / 2 */
1110 1111

// U and V contributions to R,G,B.
1112 1113 1114 1115
#define UB -113 /* round(-1.77200 * 64) */
#define UG 22 /* round(0.34414 * 64) */
#define VG 46 /* round(0.71414  * 64) */
#define VR -90 /* round(-1.40200 * 64) */
1116

1117
// Bias values to round, and subtract 128 from U and V.
1118 1119 1120
#define BB (UB * 128            + YGB)
#define BG (UG * 128 + VG * 128 + YGB)
#define BR            (VR * 128 + YGB)
1121

1122
#if defined(__aarch64__)
1123
const YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
1124 1125 1126 1127 1128 1129 1130
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { UG, VG, UG, VG, UG, VG, UG, VG },
  { UG, VG, UG, VG, UG, VG, UG, VG },
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1131
const YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
1132 1133 1134 1135 1136 1137
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
1138
};
1139
#elif defined(__arm__)
1140
const YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
1141 1142 1143 1144 1145
  { -UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0 },
  { UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0 },
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1146
const YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
1147 1148 1149 1150
  { -VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0 },
  { VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0 },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
1151 1152
};
#else
1153
const YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
  { UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
    UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0 },
  { UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
    UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG },
  { 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
    0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
};
1165
const YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
1166
  { VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
1167
    VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0 },
1168 1169 1170
  { VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
    VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG },
  { 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
1171
    0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB },
1172 1173 1174 1175
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
1176
};
1177
#endif
1178

1179 1180 1181 1182 1183 1184 1185 1186 1187
#undef BB
#undef BG
#undef BR
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
#undef YG
1188

Frank Barchard's avatar
Frank Barchard committed
1189 1190 1191 1192 1193 1194
// BT.709 YUV to RGB reference
// *  R = Y                - V * -1.28033
// *  G = Y - U *  0.21482 - V *  0.38059
// *  B = Y - U * -2.12798

// Y contribution to R,G,B.  Scale and bias.
1195 1196
#define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
#define YGB 32  /* 64 / 2 */
Frank Barchard's avatar
Frank Barchard committed
1197

1198
// TODO(fbarchard): Find way to express 2.12 instead of 2.0.
Frank Barchard's avatar
Frank Barchard committed
1199
// U and V contributions to R,G,B.
1200 1201 1202 1203
#define UB -128 /* max(-128, round(-2.12798 * 64)) */
#define UG 14 /* round(0.21482 * 64) */
#define VG 24 /* round(0.38059  * 64) */
#define VR -82 /* round(-1.28033 * 64) */
Frank Barchard's avatar
Frank Barchard committed
1204 1205

// Bias values to round, and subtract 128 from U and V.
1206 1207 1208
#define BB (UB * 128            + YGB)
#define BG (UG * 128 + VG * 128 + YGB)
#define BR            (VR * 128 + YGB)
Frank Barchard's avatar
Frank Barchard committed
1209

1210
#if defined(__aarch64__)
1211
const YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
1212 1213 1214 1215 1216 1217 1218
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { -UB, -VR, -UB, -VR, -UB, -VR, -UB, -VR },
  { UG, VG, UG, VG, UG, VG, UG, VG },
  { UG, VG, UG, VG, UG, VG, UG, VG },
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1219
const YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
1220 1221 1222 1223 1224 1225
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { -VR, -UB, -VR, -UB, -VR, -UB, -VR, -UB },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { VG, UG, VG, UG, VG, UG, VG, UG },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
1226
};
1227
#elif defined(__arm__)
1228
const YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
1229 1230 1231 1232 1233
  { -UB, -UB, -UB, -UB, -VR, -VR, -VR, -VR, 0, 0, 0, 0, 0, 0, 0, 0 },
  { UG, UG, UG, UG, VG, VG, VG, VG, 0, 0, 0, 0, 0, 0, 0, 0 },
  { BB, BG, BR, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
};
1234
const YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
1235 1236 1237 1238
  { -VR, -VR, -VR, -VR, -UB, -UB, -UB, -UB, 0, 0, 0, 0, 0, 0, 0, 0 },
  { VG, VG, VG, VG, UG, UG, UG, UG, 0, 0, 0, 0, 0, 0, 0, 0 },
  { BR, BG, BB, 0, 0, 0, 0, 0 },
  { 0x0101 * YG, 0, 0, 0 }
1239 1240
};
#else
1241
const YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  { UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0,
    UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0 },
  { UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG,
    UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG },
  { 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR,
    0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
};
1253
const YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
1254
  { VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0,
1255
    VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0, VR, 0 },
1256 1257 1258
  { VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG,
    VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG, VG, UG },
  { 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB,
1259
    0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB },
1260 1261 1262 1263
  { BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR },
  { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG },
  { BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB },
  { YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG, YG }
1264
};
1265
#endif
Frank Barchard's avatar
Frank Barchard committed
1266

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
#undef BB
#undef BG
#undef BR
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
#undef YG

// C reference code that mimics the YUV assembly.
static __inline void YuvPixel(uint8 y, uint8 u, uint8 v,
                              uint8* b, uint8* g, uint8* r,
                              const struct YuvConstants* yuvconstants) {
#if defined(__aarch64__)
  int ub = -yuvconstants->kUVToRB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[1];
  int vr = -yuvconstants->kUVToRB[1];
  int bb = yuvconstants->kUVBiasBGR[0];
  int bg = yuvconstants->kUVBiasBGR[1];
  int br = yuvconstants->kUVBiasBGR[2];
  int yg = yuvconstants->kYToRgb[0] / 0x0101;
#elif defined(__arm__)
  int ub = -yuvconstants->kUVToRB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[4];
  int vr = -yuvconstants->kUVToRB[4];
  int bb = yuvconstants->kUVBiasBGR[0];
  int bg = yuvconstants->kUVBiasBGR[1];
  int br = yuvconstants->kUVBiasBGR[2];
  int yg = yuvconstants->kYToRgb[0] / 0x0101;
#else
  int ub = yuvconstants->kUVToB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[1];
  int vr = yuvconstants->kUVToR[1];
  int bb = yuvconstants->kUVBiasB[0];
  int bg = yuvconstants->kUVBiasG[0];
  int br = yuvconstants->kUVBiasR[0];
  int yg = yuvconstants->kYToRgb[0];
#endif

  uint32 y1 = (uint32)(y * 0x0101 * yg) >> 16;
  *b = Clamp((int32)(-(u * ub         ) + y1 + bb) >> 6);
  *g = Clamp((int32)(-(u * ug + v * vg) + y1 + bg) >> 6);
  *r = Clamp((int32)(-(         v * vr) + y1 + br) >> 6);
}

// Y contribution to R,G,B.  Scale and bias.
#define YG 18997 /* round(1.164 * 64 * 256 * 256 / 257) */
#define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */

// C reference code that mimics the YUV assembly.
static __inline void YPixel(uint8 y, uint8* b, uint8* g, uint8* r) {
  uint32 y1 = (uint32)(y * 0x0101 * YG) >> 16;
  *b = Clamp((int32)(y1 + YGB) >> 6);
  *g = Clamp((int32)(y1 + YGB) >> 6);
  *r = Clamp((int32)(y1 + YGB) >> 6);
}

#undef YG
#undef YGB
Frank Barchard's avatar
Frank Barchard committed
1330

1331
#if !defined(LIBYUV_DISABLE_NEON) && \
1332
    (defined(__ARM_NEON__) || defined(__aarch64__) || defined(LIBYUV_NEON))
1333 1334
// C mimic assembly.
// TODO(fbarchard): Remove subsampling from Neon.
1335 1336 1337
void I444ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1338
                     uint8* rgb_buf,
1339
                     const struct YuvConstants* yuvconstants,
1340
                     int width) {
1341 1342
  int x;
  for (x = 0; x < width - 1; x += 2) {
1343 1344
    uint8 u = (src_u[0] + src_u[1] + 1) >> 1;
    uint8 v = (src_v[0] + src_v[1] + 1) >> 1;
1345 1346
    YuvPixel(src_y[0], u, v, rgb_buf + 0, rgb_buf + 1, rgb_buf + 2,
             yuvconstants);
1347
    rgb_buf[3] = 255;
1348 1349
    YuvPixel(src_y[1], u, v, rgb_buf + 4, rgb_buf + 5, rgb_buf + 6,
             yuvconstants);
1350
    rgb_buf[7] = 255;
1351 1352 1353
    src_y += 2;
    src_u += 2;
    src_v += 2;
1354 1355 1356
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1357
    YuvPixel(src_y[0], src_u[0], src_v[0],
1358
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
Frank Barchard's avatar
Frank Barchard committed
1359
    rgb_buf[3] = 255;
1360 1361 1362
  }
}
#else
1363 1364 1365
void I444ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1366
                     uint8* rgb_buf,
1367
                     const struct YuvConstants* yuvconstants,
1368
                     int width) {
1369 1370
  int x;
  for (x = 0; x < width; ++x) {
1371
    YuvPixel(src_y[0], src_u[0], src_v[0],
1372
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1373
    rgb_buf[3] = 255;
1374 1375 1376
    src_y += 1;
    src_u += 1;
    src_v += 1;
1377 1378 1379
    rgb_buf += 4;  // Advance 1 pixel.
  }
}
1380
#endif
1381

1382
// Also used for 420
1383 1384 1385
void I422ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1386
                     uint8* rgb_buf,
1387
                     const struct YuvConstants* yuvconstants,
1388
                     int width) {
1389 1390
  int x;
  for (x = 0; x < width - 1; x += 2) {
1391
    YuvPixel(src_y[0], src_u[0], src_v[0],
1392
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1393
    rgb_buf[3] = 255;
1394
    YuvPixel(src_y[1], src_u[0], src_v[0],
1395
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1396
    rgb_buf[7] = 255;
1397 1398 1399
    src_y += 2;
    src_u += 1;
    src_v += 1;
1400 1401 1402
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1403
    YuvPixel(src_y[0], src_u[0], src_v[0],
1404
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
Frank Barchard's avatar
Frank Barchard committed
1405 1406 1407 1408
    rgb_buf[3] = 255;
  }
}

1409 1410 1411 1412 1413
void I422AlphaToARGBRow_C(const uint8* src_y,
                          const uint8* src_u,
                          const uint8* src_v,
                          const uint8* src_a,
                          uint8* rgb_buf,
1414
                          const struct YuvConstants* yuvconstants,
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
                          int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
    YuvPixel(src_y[0], src_u[0], src_v[0],
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
    rgb_buf[3] = src_a[0];
    YuvPixel(src_y[1], src_u[0], src_v[0],
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
    rgb_buf[7] = src_a[1];
    src_y += 2;
    src_u += 1;
    src_v += 1;
    src_a += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
    YuvPixel(src_y[0], src_u[0], src_v[0],
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
    rgb_buf[3] = src_a[0];
  }
}

1437 1438 1439
void I422ToRGB24Row_C(const uint8* src_y,
                      const uint8* src_u,
                      const uint8* src_v,
1440
                      uint8* rgb_buf,
1441
                      const struct YuvConstants* yuvconstants,
1442
                      int width) {
1443 1444
  int x;
  for (x = 0; x < width - 1; x += 2) {
1445
    YuvPixel(src_y[0], src_u[0], src_v[0],
1446
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1447
    YuvPixel(src_y[1], src_u[0], src_v[0],
1448
             rgb_buf + 3, rgb_buf + 4, rgb_buf + 5, yuvconstants);
1449 1450 1451
    src_y += 2;
    src_u += 1;
    src_v += 1;
1452 1453 1454
    rgb_buf += 6;  // Advance 2 pixels.
  }
  if (width & 1) {
1455
    YuvPixel(src_y[0], src_u[0], src_v[0],
1456
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1457 1458 1459
  }
}

1460 1461 1462
void I422ToARGB4444Row_C(const uint8* src_y,
                         const uint8* src_u,
                         const uint8* src_v,
1463
                         uint8* dst_argb4444,
1464
                         const struct YuvConstants* yuvconstants,
1465 1466 1467 1468 1469 1470 1471
                         int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1472 1473
  int x;
  for (x = 0; x < width - 1; x += 2) {
1474 1475
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
1476 1477 1478 1479 1480 1481
    b0 = b0 >> 4;
    g0 = g0 >> 4;
    r0 = r0 >> 4;
    b1 = b1 >> 4;
    g1 = g1 >> 4;
    r1 = r1 >> 4;
1482
    *(uint32*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) |
1483
        (b1 << 16) | (g1 << 20) | (r1 << 24) | 0xf000f000;
1484 1485 1486
    src_y += 2;
    src_u += 1;
    src_v += 1;
1487 1488 1489
    dst_argb4444 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1490
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1491 1492 1493
    b0 = b0 >> 4;
    g0 = g0 >> 4;
    r0 = r0 >> 4;
1494
    *(uint16*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) |
1495 1496 1497 1498
        0xf000;
  }
}

1499 1500 1501
void I422ToARGB1555Row_C(const uint8* src_y,
                         const uint8* src_u,
                         const uint8* src_v,
1502
                         uint8* dst_argb1555,
1503
                         const struct YuvConstants* yuvconstants,
1504 1505 1506 1507 1508 1509 1510
                         int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1511 1512
  int x;
  for (x = 0; x < width - 1; x += 2) {
1513 1514
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
1515 1516 1517 1518 1519 1520
    b0 = b0 >> 3;
    g0 = g0 >> 3;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 3;
    r1 = r1 >> 3;
1521
    *(uint32*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) |
1522
        (b1 << 16) | (g1 << 21) | (r1 << 26) | 0x80008000;
1523 1524 1525
    src_y += 2;
    src_u += 1;
    src_v += 1;
1526 1527 1528
    dst_argb1555 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1529
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1530 1531 1532
    b0 = b0 >> 3;
    g0 = g0 >> 3;
    r0 = r0 >> 3;
1533
    *(uint16*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) |
1534 1535 1536 1537
        0x8000;
  }
}

1538
void I422ToRGB565Row_C(const uint8* src_y,
1539 1540 1541
                       const uint8* src_u,
                       const uint8* src_v,
                       uint8* dst_rgb565,
1542
                       const struct YuvConstants* yuvconstants,
1543
                       int width) {
1544 1545 1546 1547 1548 1549
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1550 1551
  int x;
  for (x = 0; x < width - 1; x += 2) {
1552 1553
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
    YuvPixel(src_y[1], src_u[0], src_v[0], &b1, &g1, &r1, yuvconstants);
1554 1555 1556 1557 1558 1559
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 2;
    r1 = r1 >> 3;
1560
    *(uint32*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11) |
1561
        (b1 << 16) | (g1 << 21) | (r1 << 27);
1562 1563 1564
    src_y += 2;
    src_u += 1;
    src_v += 1;
1565 1566 1567
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1568
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1569 1570 1571
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
1572
    *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1573 1574 1575
  }
}

1576 1577 1578
void I411ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1579
                     uint8* rgb_buf,
1580
                     const struct YuvConstants* yuvconstants,
1581
                     int width) {
1582 1583
  int x;
  for (x = 0; x < width - 3; x += 4) {
1584
    YuvPixel(src_y[0], src_u[0], src_v[0],
1585
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1586 1587
    rgb_buf[3] = 255;
    YuvPixel(src_y[1], src_u[0], src_v[0],
1588
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1589 1590
    rgb_buf[7] = 255;
    YuvPixel(src_y[2], src_u[0], src_v[0],
1591
             rgb_buf + 8, rgb_buf + 9, rgb_buf + 10, yuvconstants);
1592 1593
    rgb_buf[11] = 255;
    YuvPixel(src_y[3], src_u[0], src_v[0],
1594
             rgb_buf + 12, rgb_buf + 13, rgb_buf + 14, yuvconstants);
1595
    rgb_buf[15] = 255;
1596 1597 1598
    src_y += 4;
    src_u += 1;
    src_v += 1;
1599 1600 1601
    rgb_buf += 16;  // Advance 4 pixels.
  }
  if (width & 2) {
1602
    YuvPixel(src_y[0], src_u[0], src_v[0],
1603
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1604 1605
    rgb_buf[3] = 255;
    YuvPixel(src_y[1], src_u[0], src_v[0],
1606
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1607
    rgb_buf[7] = 255;
1608
    src_y += 2;
1609 1610 1611
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1612
    YuvPixel(src_y[0], src_u[0], src_v[0],
1613
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1614
    rgb_buf[3] = 255;
1615 1616 1617
  }
}

1618
void NV12ToARGBRow_C(const uint8* src_y,
Frank Barchard's avatar
Frank Barchard committed
1619
                     const uint8* src_uv,
1620
                     uint8* rgb_buf,
1621
                     const struct YuvConstants* yuvconstants,
1622
                     int width) {
1623 1624
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1625
    YuvPixel(src_y[0], src_uv[0], src_uv[1],
1626
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1627
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1628
    YuvPixel(src_y[1], src_uv[0], src_uv[1],
1629
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1630
    rgb_buf[7] = 255;
1631
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1632
    src_uv += 2;
1633 1634 1635
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1636
    YuvPixel(src_y[0], src_uv[0], src_uv[1],
1637
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1638
    rgb_buf[3] = 255;
1639 1640 1641
  }
}

1642 1643 1644
void NV21ToARGBRow_C(const uint8* src_y,
                     const uint8* src_vu,
                     uint8* rgb_buf,
1645
                     const struct YuvConstants* yuvconstants,
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
                     int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
    YuvPixel(src_y[0], src_vu[1], src_vu[0],
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
    rgb_buf[3] = 255;
    YuvPixel(src_y[1], src_vu[1], src_vu[0],
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
    rgb_buf[7] = 255;
    src_y += 2;
    src_vu += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
    YuvPixel(src_y[0], src_vu[1], src_vu[0],
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
    rgb_buf[3] = 255;
  }
}

1666
void NV12ToRGB565Row_C(const uint8* src_y,
Frank Barchard's avatar
Frank Barchard committed
1667
                       const uint8* src_uv,
1668
                       uint8* dst_rgb565,
1669
                       const struct YuvConstants* yuvconstants,
1670 1671 1672 1673 1674 1675 1676
                       int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1677 1678
  int x;
  for (x = 0; x < width - 1; x += 2) {
1679 1680
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
    YuvPixel(src_y[1], src_uv[0], src_uv[1], &b1, &g1, &r1, yuvconstants);
1681 1682 1683 1684 1685 1686
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 2;
    r1 = r1 >> 3;
1687
    *(uint32*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11) |
1688
        (b1 << 16) | (g1 << 21) | (r1 << 27);
1689
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1690
    src_uv += 2;
1691 1692 1693
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1694
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
1695 1696 1697
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
1698
    *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1699 1700 1701
  }
}

1702
void YUY2ToARGBRow_C(const uint8* src_yuy2,
1703
                     uint8* rgb_buf,
1704
                     const struct YuvConstants* yuvconstants,
1705
                     int width) {
1706 1707
  int x;
  for (x = 0; x < width - 1; x += 2) {
1708
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3],
1709
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1710 1711
    rgb_buf[3] = 255;
    YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3],
1712
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1713
    rgb_buf[7] = 255;
1714
    src_yuy2 += 4;
1715 1716 1717
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1718
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3],
1719
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1720
    rgb_buf[3] = 255;
1721 1722 1723
  }
}

1724
void UYVYToARGBRow_C(const uint8* src_uyvy,
1725
                     uint8* rgb_buf,
1726
                     const struct YuvConstants* yuvconstants,
1727
                     int width) {
1728 1729
  int x;
  for (x = 0; x < width - 1; x += 2) {
1730
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2],
1731
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1732 1733
    rgb_buf[3] = 255;
    YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2],
1734
             rgb_buf + 4, rgb_buf + 5, rgb_buf + 6, yuvconstants);
1735
    rgb_buf[7] = 255;
1736
    src_uyvy += 4;
1737 1738 1739
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1740
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2],
1741
             rgb_buf + 0, rgb_buf + 1, rgb_buf + 2, yuvconstants);
1742
    rgb_buf[3] = 255;
1743 1744 1745
  }
}

1746 1747 1748
void I422ToRGBARow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1749
                     uint8* rgb_buf,
1750
                     const struct YuvConstants* yuvconstants,
1751
                     int width) {
1752 1753
  int x;
  for (x = 0; x < width - 1; x += 2) {
1754
    YuvPixel(src_y[0], src_u[0], src_v[0],
1755
             rgb_buf + 1, rgb_buf + 2, rgb_buf + 3, yuvconstants);
1756 1757
    rgb_buf[0] = 255;
    YuvPixel(src_y[1], src_u[0], src_v[0],
1758
             rgb_buf + 5, rgb_buf + 6, rgb_buf + 7, yuvconstants);
1759
    rgb_buf[4] = 255;
1760 1761 1762
    src_y += 2;
    src_u += 1;
    src_v += 1;
1763 1764 1765
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1766
    YuvPixel(src_y[0], src_u[0], src_v[0],
1767
             rgb_buf + 1, rgb_buf + 2, rgb_buf + 3, yuvconstants);
1768
    rgb_buf[0] = 255;
1769 1770 1771
  }
}

1772
void I400ToARGBRow_C(const uint8* src_y, uint8* rgb_buf, int width) {
1773 1774
  int x;
  for (x = 0; x < width - 1; x += 2) {
1775
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1776
    rgb_buf[3] = 255;
1777
    YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6);
1778 1779 1780 1781 1782
    rgb_buf[7] = 255;
    src_y += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1783
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1784
    rgb_buf[3] = 255;
1785 1786 1787
  }
}

1788
void MirrorRow_C(const uint8* src, uint8* dst, int width) {
1789
  int x;
1790
  src += width - 1;
1791
  for (x = 0; x < width - 1; x += 2) {
1792 1793 1794 1795 1796 1797
    dst[x] = src[0];
    dst[x + 1] = src[-1];
    src -= 2;
  }
  if (width & 1) {
    dst[width - 1] = src[0];
1798 1799 1800
  }
}

1801
void MirrorUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
1802
  int x;
1803
  src_uv += (width - 1) << 1;
1804
  for (x = 0; x < width - 1; x += 2) {
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
    dst_u[x] = src_uv[0];
    dst_u[x + 1] = src_uv[-2];
    dst_v[x] = src_uv[1];
    dst_v[x + 1] = src_uv[-2 + 1];
    src_uv -= 4;
  }
  if (width & 1) {
    dst_u[width - 1] = src_uv[0];
    dst_v[width - 1] = src_uv[1];
  }
}

1817
void ARGBMirrorRow_C(const uint8* src, uint8* dst, int width) {
1818
  int x;
1819 1820
  const uint32* src32 = (const uint32*)(src);
  uint32* dst32 = (uint32*)(dst);
1821
  src32 += width - 1;
1822
  for (x = 0; x < width - 1; x += 2) {
1823 1824 1825 1826 1827 1828 1829 1830 1831
    dst32[x] = src32[0];
    dst32[x + 1] = src32[-1];
    src32 -= 2;
  }
  if (width & 1) {
    dst32[width - 1] = src32[0];
  }
}

1832
void SplitUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
1833 1834
  int x;
  for (x = 0; x < width - 1; x += 2) {
1835 1836 1837 1838 1839 1840 1841 1842 1843
    dst_u[x] = src_uv[0];
    dst_u[x + 1] = src_uv[2];
    dst_v[x] = src_uv[1];
    dst_v[x + 1] = src_uv[3];
    src_uv += 4;
  }
  if (width & 1) {
    dst_u[width - 1] = src_uv[0];
    dst_v[width - 1] = src_uv[1];
1844 1845 1846
  }
}

1847 1848
void MergeUVRow_C(const uint8* src_u, const uint8* src_v, uint8* dst_uv,
                  int width) {
1849 1850
  int x;
  for (x = 0; x < width - 1; x += 2) {
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
    dst_uv[0] = src_u[x];
    dst_uv[1] = src_v[x];
    dst_uv[2] = src_u[x + 1];
    dst_uv[3] = src_v[x + 1];
    dst_uv += 4;
  }
  if (width & 1) {
    dst_uv[0] = src_u[width - 1];
    dst_uv[1] = src_v[width - 1];
  }
}

1863 1864 1865 1866
void CopyRow_C(const uint8* src, uint8* dst, int count) {
  memcpy(dst, src, count);
}

1867 1868 1869 1870
void CopyRow_16_C(const uint16* src, uint16* dst, int count) {
  memcpy(dst, src, count * 2);
}

1871 1872
void SetRow_C(uint8* dst, uint8 v8, int width) {
  memset(dst, v8, width);
1873 1874
}

1875 1876 1877 1878 1879
void ARGBSetRow_C(uint8* dst_argb, uint32 v32, int width) {
  uint32* d = (uint32*)(dst_argb);
  int x;
  for (x = 0; x < width; ++x) {
    d[x] = v32;
1880 1881 1882
  }
}

1883
// Filter 2 rows of YUY2 UV's (422) into U and V (420).
1884
void YUY2ToUVRow_C(const uint8* src_yuy2, int src_stride_yuy2,
1885
                   uint8* dst_u, uint8* dst_v, int width) {
1886
  // Output a row of UV values, filtering 2 rows of YUY2.
1887 1888
  int x;
  for (x = 0; x < width; x += 2) {
1889 1890 1891 1892 1893 1894 1895 1896
    dst_u[0] = (src_yuy2[1] + src_yuy2[src_stride_yuy2 + 1] + 1) >> 1;
    dst_v[0] = (src_yuy2[3] + src_yuy2[src_stride_yuy2 + 3] + 1) >> 1;
    src_yuy2 += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

1897 1898 1899 1900
// Copy row of YUY2 UV's (422) into U and V (422).
void YUY2ToUV422Row_C(const uint8* src_yuy2,
                      uint8* dst_u, uint8* dst_v, int width) {
  // Output a row of UV values.
1901 1902
  int x;
  for (x = 0; x < width; x += 2) {
1903 1904 1905 1906 1907 1908 1909 1910 1911
    dst_u[0] = src_yuy2[1];
    dst_v[0] = src_yuy2[3];
    src_yuy2 += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

// Copy row of YUY2 Y's (422) into Y (420/422).
1912
void YUY2ToYRow_C(const uint8* src_yuy2, uint8* dst_y, int width) {
1913
  // Output a row of Y values.
1914 1915
  int x;
  for (x = 0; x < width - 1; x += 2) {
1916 1917 1918 1919 1920 1921
    dst_y[x] = src_yuy2[0];
    dst_y[x + 1] = src_yuy2[2];
    src_yuy2 += 4;
  }
  if (width & 1) {
    dst_y[width - 1] = src_yuy2[0];
1922 1923 1924
  }
}

1925
// Filter 2 rows of UYVY UV's (422) into U and V (420).
1926
void UYVYToUVRow_C(const uint8* src_uyvy, int src_stride_uyvy,
1927
                   uint8* dst_u, uint8* dst_v, int width) {
1928
  // Output a row of UV values.
1929 1930
  int x;
  for (x = 0; x < width; x += 2) {
1931 1932 1933 1934 1935 1936 1937 1938
    dst_u[0] = (src_uyvy[0] + src_uyvy[src_stride_uyvy + 0] + 1) >> 1;
    dst_v[0] = (src_uyvy[2] + src_uyvy[src_stride_uyvy + 2] + 1) >> 1;
    src_uyvy += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

1939 1940 1941 1942
// Copy row of UYVY UV's (422) into U and V (422).
void UYVYToUV422Row_C(const uint8* src_uyvy,
                      uint8* dst_u, uint8* dst_v, int width) {
  // Output a row of UV values.
1943 1944
  int x;
  for (x = 0; x < width; x += 2) {
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
    dst_u[0] = src_uyvy[0];
    dst_v[0] = src_uyvy[2];
    src_uyvy += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

// Copy row of UYVY Y's (422) into Y (420/422).
void UYVYToYRow_C(const uint8* src_uyvy, uint8* dst_y, int width) {
  // Output a row of Y values.
1956 1957
  int x;
  for (x = 0; x < width - 1; x += 2) {
1958 1959 1960
    dst_y[x] = src_uyvy[1];
    dst_y[x + 1] = src_uyvy[3];
    src_uyvy += 4;
1961 1962
  }
  if (width & 1) {
1963
    dst_y[width - 1] = src_uyvy[1];
1964 1965 1966
  }
}

1967
#define BLEND(f, b, a) (((256 - a) * b) >> 8) + f
1968

1969 1970
// Blend src_argb0 over src_argb1 and store to dst_argb.
// dst_argb may be src_argb0 or src_argb1.
1971
// This code mimics the SSSE3 version for better testability.
1972
void ARGBBlendRow_C(const uint8* src_argb0, const uint8* src_argb1,
1973
                    uint8* dst_argb, int width) {
1974 1975
  int x;
  for (x = 0; x < width - 1; x += 2) {
1976 1977 1978
    uint32 fb = src_argb0[0];
    uint32 fg = src_argb0[1];
    uint32 fr = src_argb0[2];
1979
    uint32 a = src_argb0[3];
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
    uint32 bb = src_argb1[0];
    uint32 bg = src_argb1[1];
    uint32 br = src_argb1[2];
    dst_argb[0] = BLEND(fb, bb, a);
    dst_argb[1] = BLEND(fg, bg, a);
    dst_argb[2] = BLEND(fr, br, a);
    dst_argb[3] = 255u;

    fb = src_argb0[4 + 0];
    fg = src_argb0[4 + 1];
    fr = src_argb0[4 + 2];
1991
    a = src_argb0[4 + 3];
1992 1993 1994 1995 1996 1997 1998
    bb = src_argb1[4 + 0];
    bg = src_argb1[4 + 1];
    br = src_argb1[4 + 2];
    dst_argb[4 + 0] = BLEND(fb, bb, a);
    dst_argb[4 + 1] = BLEND(fg, bg, a);
    dst_argb[4 + 2] = BLEND(fr, br, a);
    dst_argb[4 + 3] = 255u;
1999 2000 2001 2002 2003 2004
    src_argb0 += 8;
    src_argb1 += 8;
    dst_argb += 8;
  }

  if (width & 1) {
2005 2006 2007
    uint32 fb = src_argb0[0];
    uint32 fg = src_argb0[1];
    uint32 fr = src_argb0[2];
2008
    uint32 a = src_argb0[3];
2009 2010 2011 2012 2013 2014 2015
    uint32 bb = src_argb1[0];
    uint32 bg = src_argb1[1];
    uint32 br = src_argb1[2];
    dst_argb[0] = BLEND(fb, bb, a);
    dst_argb[1] = BLEND(fg, bg, a);
    dst_argb[2] = BLEND(fr, br, a);
    dst_argb[3] = 255u;
2016 2017
  }
}
2018
#undef BLEND
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030

void BlendPlaneRow_C(const uint8* src0, const uint8* src1,
                     const uint8* alpha, uint8* dst, int width) {
  int x;
  for (x = 0; x < width; ++x) {
    uint32 f = *src0++;
    uint32 b = *src1++;
    uint32 a = *alpha++;
    *dst++ = (((a) * f) + ((255 - a) * b) + 255) >> 8;
  }
}

2031
#define ATTENUATE(f, a) (a | (a << 8)) * (f | (f << 8)) >> 24
2032

2033
// Multiply source RGB by alpha and store to destination.
2034
// This code mimics the SSSE3 version for better testability.
2035
void ARGBAttenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
2036 2037
  int i;
  for (i = 0; i < width - 1; i += 2) {
2038 2039 2040 2041
    uint32 b = src_argb[0];
    uint32 g = src_argb[1];
    uint32 r = src_argb[2];
    uint32 a = src_argb[3];
2042 2043 2044
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2045 2046 2047 2048 2049
    dst_argb[3] = a;
    b = src_argb[4];
    g = src_argb[5];
    r = src_argb[6];
    a = src_argb[7];
2050 2051 2052
    dst_argb[4] = ATTENUATE(b, a);
    dst_argb[5] = ATTENUATE(g, a);
    dst_argb[6] = ATTENUATE(r, a);
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
    dst_argb[7] = a;
    src_argb += 8;
    dst_argb += 8;
  }

  if (width & 1) {
    const uint32 b = src_argb[0];
    const uint32 g = src_argb[1];
    const uint32 r = src_argb[2];
    const uint32 a = src_argb[3];
2063 2064 2065
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2066 2067 2068
    dst_argb[3] = a;
  }
}
2069
#undef ATTENUATE
2070

2071 2072 2073 2074 2075
// Divide source RGB by alpha and store to destination.
// b = (b * 255 + (a / 2)) / a;
// g = (g * 255 + (a / 2)) / a;
// r = (r * 255 + (a / 2)) / a;
// Reciprocal method is off by 1 on some values. ie 125
2076 2077
// 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower.
#define T(a) 0x01000000 + (0x10000 / a)
2078
const uint32 fixed_invtbl8[256] = {
2079
  0x01000000, 0x0100ffff, T(0x02), T(0x03), T(0x04), T(0x05), T(0x06), T(0x07),
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
  T(0x08), T(0x09), T(0x0a), T(0x0b), T(0x0c), T(0x0d), T(0x0e), T(0x0f),
  T(0x10), T(0x11), T(0x12), T(0x13), T(0x14), T(0x15), T(0x16), T(0x17),
  T(0x18), T(0x19), T(0x1a), T(0x1b), T(0x1c), T(0x1d), T(0x1e), T(0x1f),
  T(0x20), T(0x21), T(0x22), T(0x23), T(0x24), T(0x25), T(0x26), T(0x27),
  T(0x28), T(0x29), T(0x2a), T(0x2b), T(0x2c), T(0x2d), T(0x2e), T(0x2f),
  T(0x30), T(0x31), T(0x32), T(0x33), T(0x34), T(0x35), T(0x36), T(0x37),
  T(0x38), T(0x39), T(0x3a), T(0x3b), T(0x3c), T(0x3d), T(0x3e), T(0x3f),
  T(0x40), T(0x41), T(0x42), T(0x43), T(0x44), T(0x45), T(0x46), T(0x47),
  T(0x48), T(0x49), T(0x4a), T(0x4b), T(0x4c), T(0x4d), T(0x4e), T(0x4f),
  T(0x50), T(0x51), T(0x52), T(0x53), T(0x54), T(0x55), T(0x56), T(0x57),
  T(0x58), T(0x59), T(0x5a), T(0x5b), T(0x5c), T(0x5d), T(0x5e), T(0x5f),
  T(0x60), T(0x61), T(0x62), T(0x63), T(0x64), T(0x65), T(0x66), T(0x67),
  T(0x68), T(0x69), T(0x6a), T(0x6b), T(0x6c), T(0x6d), T(0x6e), T(0x6f),
  T(0x70), T(0x71), T(0x72), T(0x73), T(0x74), T(0x75), T(0x76), T(0x77),
  T(0x78), T(0x79), T(0x7a), T(0x7b), T(0x7c), T(0x7d), T(0x7e), T(0x7f),
  T(0x80), T(0x81), T(0x82), T(0x83), T(0x84), T(0x85), T(0x86), T(0x87),
  T(0x88), T(0x89), T(0x8a), T(0x8b), T(0x8c), T(0x8d), T(0x8e), T(0x8f),
  T(0x90), T(0x91), T(0x92), T(0x93), T(0x94), T(0x95), T(0x96), T(0x97),
  T(0x98), T(0x99), T(0x9a), T(0x9b), T(0x9c), T(0x9d), T(0x9e), T(0x9f),
  T(0xa0), T(0xa1), T(0xa2), T(0xa3), T(0xa4), T(0xa5), T(0xa6), T(0xa7),
  T(0xa8), T(0xa9), T(0xaa), T(0xab), T(0xac), T(0xad), T(0xae), T(0xaf),
  T(0xb0), T(0xb1), T(0xb2), T(0xb3), T(0xb4), T(0xb5), T(0xb6), T(0xb7),
  T(0xb8), T(0xb9), T(0xba), T(0xbb), T(0xbc), T(0xbd), T(0xbe), T(0xbf),
  T(0xc0), T(0xc1), T(0xc2), T(0xc3), T(0xc4), T(0xc5), T(0xc6), T(0xc7),
  T(0xc8), T(0xc9), T(0xca), T(0xcb), T(0xcc), T(0xcd), T(0xce), T(0xcf),
  T(0xd0), T(0xd1), T(0xd2), T(0xd3), T(0xd4), T(0xd5), T(0xd6), T(0xd7),
  T(0xd8), T(0xd9), T(0xda), T(0xdb), T(0xdc), T(0xdd), T(0xde), T(0xdf),
  T(0xe0), T(0xe1), T(0xe2), T(0xe3), T(0xe4), T(0xe5), T(0xe6), T(0xe7),
  T(0xe8), T(0xe9), T(0xea), T(0xeb), T(0xec), T(0xed), T(0xee), T(0xef),
  T(0xf0), T(0xf1), T(0xf2), T(0xf3), T(0xf4), T(0xf5), T(0xf6), T(0xf7),
2110
  T(0xf8), T(0xf9), T(0xfa), T(0xfb), T(0xfc), T(0xfd), T(0xfe), 0x01000100 };
2111 2112 2113
#undef T

void ARGBUnattenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
2114 2115
  int i;
  for (i = 0; i < width; ++i) {
2116 2117 2118 2119
    uint32 b = src_argb[0];
    uint32 g = src_argb[1];
    uint32 r = src_argb[2];
    const uint32 a = src_argb[3];
fbarchard@google.com's avatar
fbarchard@google.com committed
2120
    const uint32 ia = fixed_invtbl8[a] & 0xffff;  // 8.8 fixed point
2121 2122 2123 2124
    b = (b * ia) >> 8;
    g = (g * ia) >> 8;
    r = (r * ia) >> 8;
    // Clamping should not be necessary but is free in assembly.
2125 2126 2127
    dst_argb[0] = clamp255(b);
    dst_argb[1] = clamp255(g);
    dst_argb[2] = clamp255(r);
2128 2129 2130 2131 2132 2133
    dst_argb[3] = a;
    src_argb += 4;
    dst_argb += 4;
  }
}

fbarchard@google.com's avatar
fbarchard@google.com committed
2134
void ComputeCumulativeSumRow_C(const uint8* row, int32* cumsum,
2135
                               const int32* previous_cumsum, int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2136
  int32 row_sum[4] = {0, 0, 0, 0};
2137 2138
  int x;
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
    row_sum[0] += row[x * 4 + 0];
    row_sum[1] += row[x * 4 + 1];
    row_sum[2] += row[x * 4 + 2];
    row_sum[3] += row[x * 4 + 3];
    cumsum[x * 4 + 0] = row_sum[0]  + previous_cumsum[x * 4 + 0];
    cumsum[x * 4 + 1] = row_sum[1]  + previous_cumsum[x * 4 + 1];
    cumsum[x * 4 + 2] = row_sum[2]  + previous_cumsum[x * 4 + 2];
    cumsum[x * 4 + 3] = row_sum[3]  + previous_cumsum[x * 4 + 3];
  }
}

2150 2151
void CumulativeSumToAverageRow_C(const int32* tl, const int32* bl,
                                int w, int area, uint8* dst, int count) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2152
  float ooa = 1.0f / area;
2153 2154
  int i;
  for (i = 0; i < count; ++i) {
2155 2156 2157 2158
    dst[0] = (uint8)((bl[w + 0] + tl[0] - bl[0] - tl[w + 0]) * ooa);
    dst[1] = (uint8)((bl[w + 1] + tl[1] - bl[1] - tl[w + 1]) * ooa);
    dst[2] = (uint8)((bl[w + 2] + tl[2] - bl[2] - tl[w + 2]) * ooa);
    dst[3] = (uint8)((bl[w + 3] + tl[3] - bl[3] - tl[w + 3]) * ooa);
fbarchard@google.com's avatar
fbarchard@google.com committed
2159 2160 2161 2162 2163 2164
    dst += 4;
    tl += 4;
    bl += 4;
  }
}

2165
// Copy pixels from rotated source to destination row with a slope.
2166
LIBYUV_API
2167 2168
void ARGBAffineRow_C(const uint8* src_argb, int src_argb_stride,
                     uint8* dst_argb, const float* uv_dudv, int width) {
2169
  int i;
2170 2171 2172 2173
  // Render a row of pixels from source into a buffer.
  float uv[2];
  uv[0] = uv_dudv[0];
  uv[1] = uv_dudv[1];
2174
  for (i = 0; i < width; ++i) {
2175 2176 2177 2178
    int x = (int)(uv[0]);
    int y = (int)(uv[1]);
    *(uint32*)(dst_argb) =
        *(const uint32*)(src_argb + y * src_argb_stride +
2179 2180 2181 2182 2183 2184 2185
                                         x * 4);
    dst_argb += 4;
    uv[0] += uv_dudv[2];
    uv[1] += uv_dudv[3];
  }
}

2186 2187
// Blend 2 rows into 1.
static void HalfRow_C(const uint8* src_uv, int src_uv_stride,
2188
                      uint8* dst_uv, int width) {
2189
  int x;
2190
  for (x = 0; x < width; ++x) {
2191 2192 2193 2194
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

2195
static void HalfRow_16_C(const uint16* src_uv, int src_uv_stride,
2196
                         uint16* dst_uv, int width) {
2197
  int x;
2198
  for (x = 0; x < width; ++x) {
2199 2200 2201 2202
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

2203
// C version 2x2 -> 2x1.
2204 2205 2206
void InterpolateRow_C(uint8* dst_ptr, const uint8* src_ptr,
                      ptrdiff_t src_stride,
                      int width, int source_y_fraction) {
2207 2208 2209 2210
  int y1_fraction = source_y_fraction;
  int y0_fraction = 256 - y1_fraction;
  const uint8* src_ptr1 = src_ptr + src_stride;
  int x;
2211 2212 2213 2214
  if (source_y_fraction == 0) {
    memcpy(dst_ptr, src_ptr, width);
    return;
  }
2215
  if (source_y_fraction == 128) {
2216
    HalfRow_C(src_ptr, (int)(src_stride), dst_ptr, width);
2217 2218
    return;
  }
2219
  for (x = 0; x < width - 1; x += 2) {
2220 2221
    dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
    dst_ptr[1] = (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction) >> 8;
2222 2223 2224
    src_ptr += 2;
    src_ptr1 += 2;
    dst_ptr += 2;
2225 2226 2227
  }
  if (width & 1) {
    dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
  }
}

void InterpolateRow_16_C(uint16* dst_ptr, const uint16* src_ptr,
                         ptrdiff_t src_stride,
                         int width, int source_y_fraction) {
  int y1_fraction = source_y_fraction;
  int y0_fraction = 256 - y1_fraction;
  const uint16* src_ptr1 = src_ptr + src_stride;
  int x;
  if (source_y_fraction == 0) {
    memcpy(dst_ptr, src_ptr, width * 2);
    return;
  }
  if (source_y_fraction == 128) {
    HalfRow_16_C(src_ptr, (int)(src_stride), dst_ptr, width);
    return;
  }
  for (x = 0; x < width - 1; x += 2) {
    dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
    dst_ptr[1] = (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction) >> 8;
    src_ptr += 2;
    src_ptr1 += 2;
    dst_ptr += 2;
  }
  if (width & 1) {
    dst_ptr[0] = (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction) >> 8;
2255
  }
2256 2257
}

fbarchard@google.com's avatar
fbarchard@google.com committed
2258 2259
// Use first 4 shuffler values to reorder ARGB channels.
void ARGBShuffleRow_C(const uint8* src_argb, uint8* dst_argb,
2260
                      const uint8* shuffler, int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2261 2262 2263 2264 2265
  int index0 = shuffler[0];
  int index1 = shuffler[1];
  int index2 = shuffler[2];
  int index3 = shuffler[3];
  // Shuffle a row of ARGB.
2266
  int x;
2267
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
    // To support in-place conversion.
    uint8 b = src_argb[index0];
    uint8 g = src_argb[index1];
    uint8 r = src_argb[index2];
    uint8 a = src_argb[index3];
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = a;
    src_argb += 4;
    dst_argb += 4;
  }
}

fbarchard@google.com's avatar
fbarchard@google.com committed
2282 2283 2284 2285
void I422ToYUY2Row_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
                     uint8* dst_frame, int width) {
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
  int x;
  for (x = 0; x < width - 1; x += 2) {
    dst_frame[0] = src_y[0];
    dst_frame[1] = src_u[0];
    dst_frame[2] = src_y[1];
    dst_frame[3] = src_v[0];
    dst_frame += 4;
    src_y += 2;
    src_u += 1;
    src_v += 1;
  }
  if (width & 1) {
    dst_frame[0] = src_y[0];
    dst_frame[1] = src_u[0];
Frank Barchard's avatar
Frank Barchard committed
2300
    dst_frame[2] = 0;
2301 2302
    dst_frame[3] = src_v[0];
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2303 2304 2305 2306 2307 2308
}

void I422ToUYVYRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
                     uint8* dst_frame, int width) {
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
  int x;
  for (x = 0; x < width - 1; x += 2) {
    dst_frame[0] = src_u[0];
    dst_frame[1] = src_y[0];
    dst_frame[2] = src_v[0];
    dst_frame[3] = src_y[1];
    dst_frame += 4;
    src_y += 2;
    src_u += 1;
    src_v += 1;
  }
  if (width & 1) {
    dst_frame[0] = src_u[0];
    dst_frame[1] = src_y[0];
    dst_frame[2] = src_v[0];
Frank Barchard's avatar
Frank Barchard committed
2324
    dst_frame[3] = 0;
2325
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2326
}
2327

2328

2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
void ARGBPolynomialRow_C(const uint8* src_argb,
                         uint8* dst_argb,
                         const float* poly,
                         int width) {
  int i;
  for (i = 0; i < width; ++i) {
    float b = (float)(src_argb[0]);
    float g = (float)(src_argb[1]);
    float r = (float)(src_argb[2]);
    float a = (float)(src_argb[3]);
    float b2 = b * b;
    float g2 = g * g;
    float r2 = r * r;
    float a2 = a * a;
    float db = poly[0] + poly[4] * b;
    float dg = poly[1] + poly[5] * g;
    float dr = poly[2] + poly[6] * r;
    float da = poly[3] + poly[7] * a;
    float b3 = b2 * b;
    float g3 = g2 * g;
    float r3 = r2 * r;
    float a3 = a2 * a;
    db += poly[8] * b2;
    dg += poly[9] * g2;
    dr += poly[10] * r2;
    da += poly[11] * a2;
    db += poly[12] * b3;
    dg += poly[13] * g3;
    dr += poly[14] * r3;
    da += poly[15] * a3;

    dst_argb[0] = Clamp((int32)(db));
    dst_argb[1] = Clamp((int32)(dg));
    dst_argb[2] = Clamp((int32)(dr));
    dst_argb[3] = Clamp((int32)(da));
    src_argb += 4;
    dst_argb += 4;
  }
}

void ARGBLumaColorTableRow_C(const uint8* src_argb, uint8* dst_argb, int width,
                             const uint8* luma, uint32 lumacoeff) {
  uint32 bc = lumacoeff & 0xff;
  uint32 gc = (lumacoeff >> 8) & 0xff;
  uint32 rc = (lumacoeff >> 16) & 0xff;

  int i;
  for (i = 0; i < width - 1; i += 2) {
    // Luminance in rows, color values in columns.
    const uint8* luma0 = ((src_argb[0] * bc + src_argb[1] * gc +
                           src_argb[2] * rc) & 0x7F00u) + luma;
    const uint8* luma1;
    dst_argb[0] = luma0[src_argb[0]];
    dst_argb[1] = luma0[src_argb[1]];
    dst_argb[2] = luma0[src_argb[2]];
    dst_argb[3] = src_argb[3];
    luma1 = ((src_argb[4] * bc + src_argb[5] * gc +
              src_argb[6] * rc) & 0x7F00u) + luma;
    dst_argb[4] = luma1[src_argb[4]];
    dst_argb[5] = luma1[src_argb[5]];
    dst_argb[6] = luma1[src_argb[6]];
    dst_argb[7] = src_argb[7];
    src_argb += 8;
    dst_argb += 8;
  }
  if (width & 1) {
    // Luminance in rows, color values in columns.
    const uint8* luma0 = ((src_argb[0] * bc + src_argb[1] * gc +
                           src_argb[2] * rc) & 0x7F00u) + luma;
    dst_argb[0] = luma0[src_argb[0]];
    dst_argb[1] = luma0[src_argb[1]];
    dst_argb[2] = luma0[src_argb[2]];
    dst_argb[3] = src_argb[3];
  }
}

void ARGBCopyAlphaRow_C(const uint8* src, uint8* dst, int width) {
  int i;
  for (i = 0; i < width - 1; i += 2) {
    dst[3] = src[3];
    dst[7] = src[7];
    dst += 8;
    src += 8;
  }
  if (width & 1) {
    dst[3] = src[3];
  }
}

void ARGBCopyYToAlphaRow_C(const uint8* src, uint8* dst, int width) {
  int i;
  for (i = 0; i < width - 1; i += 2) {
    dst[3] = src[0];
    dst[7] = src[1];
    dst += 8;
    src += 2;
  }
  if (width & 1) {
    dst[3] = src[0];
  }
}
2430

2431
// Maximum temporary width for wrappers to process at a time, in pixels.
2432
#define MAXTWIDTH 2048
2433

2434 2435
#if !(defined(_MSC_VER) && defined(_M_IX86)) && \
    defined(HAS_I422TORGB565ROW_SSSE3)
2436
// row_win.cc has asm version, but GCC uses 2 step wrapper.
2437 2438 2439
void I422ToRGB565Row_SSSE3(const uint8* src_y,
                           const uint8* src_u,
                           const uint8* src_v,
2440
                           uint8* dst_rgb565,
2441
                           const struct YuvConstants* yuvconstants,
2442
                           int width) {
2443 2444 2445
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2446
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2447
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
2448 2449 2450
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2451
    dst_rgb565 += twidth * 2;
2452 2453
    width -= twidth;
  }
2454
}
2455
#endif
2456

2457
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
2458 2459 2460
void I422ToARGB1555Row_SSSE3(const uint8* src_y,
                             const uint8* src_u,
                             const uint8* src_v,
2461
                             uint8* dst_argb1555,
2462
                             const struct YuvConstants* yuvconstants,
2463
                             int width) {
2464 2465 2466 2467
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2468
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2469
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
2470 2471 2472
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2473
    dst_argb1555 += twidth * 2;
2474 2475
    width -= twidth;
  }
2476
}
2477
#endif
2478

2479
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
2480 2481 2482
void I422ToARGB4444Row_SSSE3(const uint8* src_y,
                             const uint8* src_u,
                             const uint8* src_v,
2483
                             uint8* dst_argb4444,
2484
                             const struct YuvConstants* yuvconstants,
2485
                             int width) {
2486 2487 2488 2489
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2490
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2491
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
2492 2493 2494
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2495
    dst_argb4444 += twidth * 2;
2496 2497 2498
    width -= twidth;
  }
}
2499
#endif
2500

2501
#if defined(HAS_NV12TORGB565ROW_SSSE3)
2502 2503 2504
void NV12ToRGB565Row_SSSE3(const uint8* src_y,
                           const uint8* src_uv,
                           uint8* dst_rgb565,
2505
                           const struct YuvConstants* yuvconstants,
2506
                           int width) {
2507 2508 2509 2510
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2511
    NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
2512 2513 2514 2515 2516 2517 2518
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
2519
#endif
2520

2521
#if defined(HAS_I422TORGB565ROW_AVX2)
2522 2523 2524 2525
void I422ToRGB565Row_AVX2(const uint8* src_y,
                          const uint8* src_u,
                          const uint8* src_v,
                          uint8* dst_rgb565,
2526
                          const struct YuvConstants* yuvconstants,
2527
                          int width) {
2528
  SIMD_ALIGNED32(uint8 row[MAXTWIDTH * 4]);
2529 2530
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2531
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
#endif

#if defined(HAS_I422TOARGB1555ROW_AVX2)
void I422ToARGB1555Row_AVX2(const uint8* src_y,
                            const uint8* src_u,
                            const uint8* src_v,
                            uint8* dst_argb1555,
2547
                            const struct YuvConstants* yuvconstants,
2548 2549
                            int width) {
  // Row buffer for intermediate ARGB pixels.
2550
  SIMD_ALIGNED32(uint8 row[MAXTWIDTH * 4]);
2551 2552
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2553
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
    ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth);
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_argb1555 += twidth * 2;
    width -= twidth;
  }
}
#endif

#if defined(HAS_I422TOARGB4444ROW_AVX2)
void I422ToARGB4444Row_AVX2(const uint8* src_y,
                            const uint8* src_u,
                            const uint8* src_v,
                            uint8* dst_argb4444,
2569
                            const struct YuvConstants* yuvconstants,
2570 2571
                            int width) {
  // Row buffer for intermediate ARGB pixels.
2572
  SIMD_ALIGNED32(uint8 row[MAXTWIDTH * 4]);
2573 2574
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2575
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
    ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth);
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_argb4444 += twidth * 2;
    width -= twidth;
  }
}
#endif

2586 2587 2588 2589 2590
#if defined(HAS_I422TORGB24ROW_AVX2)
void I422ToRGB24Row_AVX2(const uint8* src_y,
                            const uint8* src_u,
                            const uint8* src_v,
                            uint8* dst_rgb24,
2591
                            const struct YuvConstants* yuvconstants,
2592 2593 2594 2595 2596
                            int width) {
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED32(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2597
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
    // TODO(fbarchard): ARGBToRGB24Row_AVX2
    ARGBToRGB24Row_SSSE3(row, dst_rgb24, twidth);
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_rgb24 += twidth * 3;
    width -= twidth;
  }
}
#endif

2609
#if defined(HAS_NV12TORGB565ROW_AVX2)
2610 2611 2612
void NV12ToRGB565Row_AVX2(const uint8* src_y,
                          const uint8* src_uv,
                          uint8* dst_rgb565,
2613
                          const struct YuvConstants* yuvconstants,
2614
                          int width) {
2615
  // Row buffer for intermediate ARGB pixels.
2616
  SIMD_ALIGNED32(uint8 row[MAXTWIDTH * 4]);
2617 2618
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2619
    NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
2620 2621 2622 2623 2624 2625 2626 2627 2628
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
#endif

2629
#ifdef __cplusplus
2630
}  // extern "C"
2631 2632
}  // namespace libyuv
#endif