row_common.cc 86.9 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
}

static __inline uint32 Abs(int32 v) {
  int m = v >> 31;
  return (v + m) ^ m;
}
Frank Barchard's avatar
Frank Barchard committed
43
#else   // USE_BRANCHLESS
44 45 46 47 48 49 50 51 52 53
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
  }
}

Frank Barchard's avatar
Frank Barchard committed
132 133
void ARGB1555ToARGBRow_C(const uint8* src_argb1555,
                         uint8* dst_argb,
134
                         int width) {
135 136
  int x;
  for (x = 0; x < width; ++x) {
137 138 139 140
    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;
141 142 143
    dst_argb[0] = (b << 3) | (b >> 2);
    dst_argb[1] = (g << 3) | (g >> 2);
    dst_argb[2] = (r << 3) | (r >> 2);
144
    dst_argb[3] = -a;
145
    dst_argb += 4;
146
    src_argb1555 += 2;
147 148 149
  }
}

Frank Barchard's avatar
Frank Barchard committed
150 151
void ARGB4444ToARGBRow_C(const uint8* src_argb4444,
                         uint8* dst_argb,
152
                         int width) {
153 154
  int x;
  for (x = 0; x < width; ++x) {
155 156 157 158
    uint8 b = src_argb4444[0] & 0x0f;
    uint8 g = src_argb4444[0] >> 4;
    uint8 r = src_argb4444[1] & 0x0f;
    uint8 a = src_argb4444[1] >> 4;
159 160 161 162 163
    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;
164
    src_argb4444 += 2;
165 166 167
  }
}

168
void ARGBToRGB24Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
169 170
  int x;
  for (x = 0; x < width; ++x) {
171 172 173 174 175 176 177 178 179 180 181
    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;
  }
}

182
void ARGBToRAWRow_C(const uint8* src_argb, uint8* dst_rgb, int width) {
183 184
  int x;
  for (x = 0; x < width; ++x) {
185 186 187 188 189 190 191 192 193 194 195
    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;
  }
}

196
void ARGBToRGB565Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
197 198
  int x;
  for (x = 0; x < width - 1; x += 2) {
199 200 201 202 203 204
    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;
205 206
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
                           (r1 << 27));
207 208 209 210 211 212 213
    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;
214
    *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
215 216 217
  }
}

218 219 220 221 222 223 224 225
// 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.
Frank Barchard's avatar
Frank Barchard committed
226 227 228 229
void ARGBToRGB565DitherRow_C(const uint8* src_argb,
                             uint8* dst_rgb,
                             const uint32 dither4,
                             int width) {
230 231
  int x;
  for (x = 0; x < width - 1; x += 2) {
232 233
    int dither0 = ((const unsigned char*)(&dither4))[x & 3];
    int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3];
234 235 236 237 238 239
    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;
240 241
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
                           (r1 << 27));
242 243 244 245
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
246
    int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3];
247 248 249
    uint8 b0 = clamp255(src_argb[0] + dither0) >> 3;
    uint8 g0 = clamp255(src_argb[1] + dither0) >> 2;
    uint8 r0 = clamp255(src_argb[2] + dither0) >> 3;
250 251 252 253
    *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
  }
}

254
void ARGBToARGB1555Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
255 256
  int x;
  for (x = 0; x < width - 1; x += 2) {
257 258 259 260 261 262 263 264
    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;
Frank Barchard's avatar
Frank Barchard committed
265 266
    *(uint32*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15) |
                          (b1 << 16) | (g1 << 21) | (r1 << 26) | (a1 << 31);
267 268 269 270 271 272 273 274
    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;
Frank Barchard's avatar
Frank Barchard committed
275
    *(uint16*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15);
276 277 278
  }
}

279
void ARGBToARGB4444Row_C(const uint8* src_argb, uint8* dst_rgb, int width) {
280 281
  int x;
  for (x = 0; x < width - 1; x += 2) {
282 283 284 285 286 287 288 289
    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;
Frank Barchard's avatar
Frank Barchard committed
290 291
    *(uint32*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12) | (b1 << 16) |
                          (g1 << 20) | (r1 << 24) | (a1 << 28);
292 293 294 295 296 297 298 299
    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;
Frank Barchard's avatar
Frank Barchard committed
300
    *(uint16*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12);
301 302 303
  }
}

304
static __inline int RGBToY(uint8 r, uint8 g, uint8 b) {
Frank Barchard's avatar
Frank Barchard committed
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
// ARGBToY_C and ARGBToUV_C
Frank Barchard's avatar
Frank Barchard committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
#define MAKEROWY(NAME, R, G, B, BPP)                                     \
  void NAME##ToYRow_C(const uint8* src_argb0, uint8* dst_y, int width) { \
    int x;                                                               \
    for (x = 0; x < width; ++x) {                                        \
      dst_y[0] = RGBToY(src_argb0[R], src_argb0[G], src_argb0[B]);       \
      src_argb0 += BPP;                                                  \
      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;                   \
    int x;                                                               \
    for (x = 0; x < width - 1; x += 2) {                                 \
      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;                                                      \
      dst_u[0] = RGBToU(ar, ag, ab);                                     \
      dst_v[0] = RGBToV(ar, ag, ab);                                     \
      src_rgb0 += BPP * 2;                                               \
      src_rgb1 += BPP * 2;                                               \
      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);                                     \
    }                                                                    \
  }
354

355 356 357 358 359 360 361 362
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

363 364 365 366 367
// 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:
368 369 370
// b 0.1016 * 255 = 25.908 = 25
// g 0.5078 * 255 = 129.489 = 129
// r 0.2578 * 255 = 65.739 = 66
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
// 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
387

388
static __inline int RGBToYJ(uint8 r, uint8 g, uint8 b) {
Frank Barchard's avatar
Frank Barchard committed
389
  return (38 * r + 75 * g + 15 * b + 64) >> 7;
390 391
}

392 393 394 395 396 397 398 399 400
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)

401
// ARGBToYJ_C and ARGBToUVJ_C
Frank Barchard's avatar
Frank Barchard committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
#define MAKEROWYJ(NAME, R, G, B, BPP)                                     \
  void NAME##ToYJRow_C(const uint8* src_argb0, uint8* dst_y, int width) { \
    int x;                                                                \
    for (x = 0; x < width; ++x) {                                         \
      dst_y[0] = RGBToYJ(src_argb0[R], src_argb0[G], src_argb0[B]);       \
      src_argb0 += BPP;                                                   \
      dst_y += 1;                                                         \
    }                                                                     \
  }                                                                       \
  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;                    \
    int x;                                                                \
    for (x = 0; x < width - 1; x += 2) {                                  \
      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);                                     \
    }                                                                     \
  }
437 438 439 440

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

441
void RGB565ToYRow_C(const uint8* src_rgb565, uint8* dst_y, int width) {
442 443
  int x;
  for (x = 0; x < width; ++x) {
444 445 446 447 448 449 450 451 452 453 454 455
    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;
  }
}

456
void ARGB1555ToYRow_C(const uint8* src_argb1555, uint8* dst_y, int width) {
457 458
  int x;
  for (x = 0; x < width; ++x) {
459 460 461 462 463 464 465 466 467 468 469 470 471
    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) {
472 473
  int x;
  for (x = 0; x < width; ++x) {
474 475 476 477 478 479 480 481 482 483 484 485
    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;
  }
}

Frank Barchard's avatar
Frank Barchard committed
486 487 488 489 490
void RGB565ToUVRow_C(const uint8* src_rgb565,
                     int src_stride_rgb565,
                     uint8* dst_u,
                     uint8* dst_v,
                     int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
491
  const uint8* next_rgb565 = src_rgb565 + src_stride_rgb565;
492 493
  int x;
  for (x = 0; x < width - 1; x += 2) {
fbarchard@google.com's avatar
fbarchard@google.com committed
494 495 496 497 498 499 500 501 502 503 504 505
    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;
506 507 508 509 510 511 512
    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
513 514 515 516 517 518 519 520 521 522 523 524
    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;
525 526 527 528 529 530 531 532 533 534 535
    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);
  }
}

Frank Barchard's avatar
Frank Barchard committed
536 537 538 539 540
void ARGB1555ToUVRow_C(const uint8* src_argb1555,
                       int src_stride_argb1555,
                       uint8* dst_u,
                       uint8* dst_v,
                       int width) {
541
  const uint8* next_argb1555 = src_argb1555 + src_stride_argb1555;
542 543
  int x;
  for (x = 0; x < width - 1; x += 2) {
544 545 546 547 548 549 550 551 552 553 554 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
    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);
  }
}

Frank Barchard's avatar
Frank Barchard committed
587 588 589 590 591
void ARGB4444ToUVRow_C(const uint8* src_argb4444,
                       int src_stride_argb4444,
                       uint8* dst_u,
                       uint8* dst_v,
                       int width) {
592
  const uint8* next_argb4444 = src_argb4444 + src_stride_argb4444;
593 594
  int x;
  for (x = 0; x < width - 1; x += 2) {
595 596 597 598 599 600 601 602 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
    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
635 636 637
  }
}

638
void ARGBToUV444Row_C(const uint8* src_argb,
Frank Barchard's avatar
Frank Barchard committed
639 640 641
                      uint8* dst_u,
                      uint8* dst_v,
                      int width) {
642 643
  int x;
  for (x = 0; x < width; ++x) {
644 645 646 647 648 649 650 651 652 653 654
    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;
  }
}

655
void ARGBGrayRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
656 657
  int x;
  for (x = 0; x < width; ++x) {
658
    uint8 y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
659
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
660
    dst_argb[3] = src_argb[3];
661
    dst_argb += 4;
662
    src_argb += 4;
663 664 665
  }
}

666 667
// Convert a row of image to Sepia tone.
void ARGBSepiaRow_C(uint8* dst_argb, int width) {
668 669
  int x;
  for (x = 0; x < width; ++x) {
670 671 672
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
673 674 675
    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;
676
    // b does not over flow. a is preserved from original.
677
    dst_argb[0] = sb;
678 679
    dst_argb[1] = clamp255(sg);
    dst_argb[2] = clamp255(sr);
680 681 682 683
    dst_argb += 4;
  }
}

684
// Apply color matrix to a row of image. Matrix is signed.
685
// TODO(fbarchard): Consider adding rounding (+32).
Frank Barchard's avatar
Frank Barchard committed
686 687 688 689
void ARGBColorMatrixRow_C(const uint8* src_argb,
                          uint8* dst_argb,
                          const int8* matrix_argb,
                          int width) {
690 691
  int x;
  for (x = 0; x < width; ++x) {
692 693 694 695
    int b = src_argb[0];
    int g = src_argb[1];
    int r = src_argb[2];
    int a = src_argb[3];
Frank Barchard's avatar
Frank Barchard committed
696 697 698 699 700 701 702 703 704 705 706 707
    int sb = (b * matrix_argb[0] + g * matrix_argb[1] + r * matrix_argb[2] +
              a * matrix_argb[3]) >>
             6;
    int sg = (b * matrix_argb[4] + g * matrix_argb[5] + r * matrix_argb[6] +
              a * matrix_argb[7]) >>
             6;
    int sr = (b * matrix_argb[8] + g * matrix_argb[9] + 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;
708 709 710
    dst_argb[0] = Clamp(sb);
    dst_argb[1] = Clamp(sg);
    dst_argb[2] = Clamp(sr);
711 712
    dst_argb[3] = Clamp(sa);
    src_argb += 4;
713 714 715 716
    dst_argb += 4;
  }
}

717 718
// Apply color table to a row of image.
void ARGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
719 720
  int x;
  for (x = 0; x < width; ++x) {
721 722 723 724 725 726 727 728 729 730 731 732
    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;
  }
}

733 734
// Apply color table to a row of image.
void RGBColorTableRow_C(uint8* dst_argb, const uint8* table_argb, int width) {
735 736
  int x;
  for (x = 0; x < width; ++x) {
737 738 739 740 741 742 743 744 745 746
    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;
  }
}

Frank Barchard's avatar
Frank Barchard committed
747 748 749 750 751
void ARGBQuantizeRow_C(uint8* dst_argb,
                       int scale,
                       int interval_size,
                       int interval_offset,
                       int width) {
752 753
  int x;
  for (x = 0; x < width; ++x) {
754 755 756 757 758 759 760 761 762 763
    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;
  }
}

764
#define REPEAT8(v) (v) | ((v) << 8)
Frank Barchard's avatar
Frank Barchard committed
765
#define SHADE(f, v) v* f >> 24
766

Frank Barchard's avatar
Frank Barchard committed
767 768 769
void ARGBShadeRow_C(const uint8* src_argb,
                    uint8* dst_argb,
                    int width,
770 771 772 773 774 775
                    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);

776 777
  int i;
  for (i = 0; i < width; ++i) {
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
    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

793
#define REPEAT8(v) (v) | ((v) << 8)
Frank Barchard's avatar
Frank Barchard committed
794
#define SHADE(f, v) v* f >> 16
795

Frank Barchard's avatar
Frank Barchard committed
796 797 798 799
void ARGBMultiplyRow_C(const uint8* src_argb0,
                       const uint8* src_argb1,
                       uint8* dst_argb,
                       int width) {
800 801
  int i;
  for (i = 0; i < width; ++i) {
802 803 804 805 806 807 808 809
    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];
810 811 812 813
    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);
814 815
    src_argb0 += 4;
    src_argb1 += 4;
816 817 818 819 820 821
    dst_argb += 4;
  }
}
#undef REPEAT8
#undef SHADE

822
#define SHADE(f, v) clamp255(v + f)
823

Frank Barchard's avatar
Frank Barchard committed
824 825 826 827
void ARGBAddRow_C(const uint8* src_argb0,
                  const uint8* src_argb1,
                  uint8* dst_argb,
                  int width) {
828 829
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
830 831 832 833 834 835 836 837
    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];
838 839 840 841 842 843 844 845 846 847 848
    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

849
#define SHADE(f, v) clamp0(f - v)
850

Frank Barchard's avatar
Frank Barchard committed
851 852 853 854
void ARGBSubtractRow_C(const uint8* src_argb0,
                       const uint8* src_argb1,
                       uint8* dst_argb,
                       int width) {
855 856
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
857 858 859 860 861 862 863 864
    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];
865 866 867 868 869 870 871 872 873 874 875
    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
876
// Sobel functions which mimics SSSE3.
Frank Barchard's avatar
Frank Barchard committed
877 878 879 880 881
void SobelXRow_C(const uint8* src_y0,
                 const uint8* src_y1,
                 const uint8* src_y2,
                 uint8* dst_sobelx,
                 int width) {
882 883
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
884 885 886 887 888 889 890 891 892
    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;
893
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
894
    dst_sobelx[i] = (uint8)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
895 896 897
  }
}

Frank Barchard's avatar
Frank Barchard committed
898 899 900 901
void SobelYRow_C(const uint8* src_y0,
                 const uint8* src_y1,
                 uint8* dst_sobely,
                 int width) {
902 903
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
904 905 906 907 908 909 910 911 912
    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;
913
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
914
    dst_sobely[i] = (uint8)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
915 916 917
  }
}

Frank Barchard's avatar
Frank Barchard committed
918 919 920 921
void SobelRow_C(const uint8* src_sobelx,
                const uint8* src_sobely,
                uint8* dst_argb,
                int width) {
922 923
  int i;
  for (i = 0; i < width; ++i) {
924 925
    int r = src_sobelx[i];
    int b = src_sobely[i];
926
    int s = clamp255(r + b);
927 928 929 930
    dst_argb[0] = (uint8)(s);
    dst_argb[1] = (uint8)(s);
    dst_argb[2] = (uint8)(s);
    dst_argb[3] = (uint8)(255u);
931 932 933 934
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
935 936 937 938
void SobelToPlaneRow_C(const uint8* src_sobelx,
                       const uint8* src_sobely,
                       uint8* dst_y,
                       int width) {
939 940
  int i;
  for (i = 0; i < width; ++i) {
941 942 943
    int r = src_sobelx[i];
    int b = src_sobely[i];
    int s = clamp255(r + b);
944
    dst_y[i] = (uint8)(s);
945 946 947
  }
}

Frank Barchard's avatar
Frank Barchard committed
948 949 950 951
void SobelXYRow_C(const uint8* src_sobelx,
                  const uint8* src_sobely,
                  uint8* dst_argb,
                  int width) {
952 953
  int i;
  for (i = 0; i < width; ++i) {
954 955
    int r = src_sobelx[i];
    int b = src_sobely[i];
956
    int g = clamp255(r + b);
957 958 959 960
    dst_argb[0] = (uint8)(b);
    dst_argb[1] = (uint8)(g);
    dst_argb[2] = (uint8)(r);
    dst_argb[3] = (uint8)(255u);
961 962 963 964
    dst_argb += 4;
  }
}

965
void J400ToARGBRow_C(const uint8* src_y, uint8* dst_argb, int width) {
966
  // Copy a Y to RGB.
967 968
  int x;
  for (x = 0; x < width; ++x) {
969 970 971 972 973 974 975 976
    uint8 y = src_y[0];
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
    dst_argb[3] = 255u;
    dst_argb += 4;
    ++src_y;
  }
}

977 978 979
// TODO(fbarchard): Unify these structures to be platform independent.
// TODO(fbarchard): Generate SIMD structures from float matrix.

980 981 982 983 984
// 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

985
// Y contribution to R,G,B.  Scale and bias.
Frank Barchard's avatar
Frank Barchard committed
986
#define YG 18997  /* round(1.164 * 64 * 256 * 256 / 257) */
987
#define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
988 989

// U and V contributions to R,G,B.
990
#define UB -128 /* max(-128, round(-2.018 * 64)) */
Frank Barchard's avatar
Frank Barchard committed
991 992
#define UG 25   /* round(0.391 * 64) */
#define VG 52   /* round(0.813 * 64) */
993
#define VR -102 /* round(-1.596 * 64) */
994

995
// Bias values to subtract 16 from Y and 128 from U and V.
Frank Barchard's avatar
Frank Barchard committed
996
#define BB (UB * 128 + YGB)
997
#define BG (UG * 128 + VG * 128 + YGB)
Frank Barchard's avatar
Frank Barchard committed
998
#define BR (VR * 128 + YGB)
999

1000
#if defined(__aarch64__)  // 64 bit arm
Frank Barchard's avatar
Frank Barchard committed
1001
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1002 1003 1004 1005 1006 1007
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1008
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1009 1010 1011 1012 1013 1014
    {-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}};
1015
#elif defined(__arm__)  // 32 bit arm
Frank Barchard's avatar
Frank Barchard committed
1016
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1017 1018 1019 1020
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1021
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1022 1023 1024 1025
    {-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}};
1026
#else
1027
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    {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}};
1038
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    {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, 0},
    {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,
     0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
    {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}};
1049 1050
#endif

1051 1052 1053
#undef BB
#undef BG
#undef BR
1054 1055 1056 1057 1058
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
1059 1060
#undef YG

1061 1062 1063 1064 1065 1066
// 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.
1067
#define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
Frank Barchard's avatar
Frank Barchard committed
1068
#define YGB 32   /* 64 / 2 */
1069 1070

// U and V contributions to R,G,B.
1071
#define UB -113 /* round(-1.77200 * 64) */
Frank Barchard's avatar
Frank Barchard committed
1072 1073 1074
#define UG 22   /* round(0.34414 * 64) */
#define VG 46   /* round(0.71414  * 64) */
#define VR -90  /* round(-1.40200 * 64) */
1075

1076
// Bias values to round, and subtract 128 from U and V.
Frank Barchard's avatar
Frank Barchard committed
1077
#define BB (UB * 128 + YGB)
1078
#define BG (UG * 128 + VG * 128 + YGB)
Frank Barchard's avatar
Frank Barchard committed
1079
#define BR (VR * 128 + YGB)
1080

1081
#if defined(__aarch64__)
Frank Barchard's avatar
Frank Barchard committed
1082
const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1083 1084 1085 1086 1087 1088
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1089
const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1090 1091 1092 1093 1094 1095
    {-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}};
1096
#elif defined(__arm__)
Frank Barchard's avatar
Frank Barchard committed
1097
const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1098 1099 1100 1101
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1102
const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1103 1104 1105 1106
    {-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}};
1107
#else
1108
const struct YuvConstants SIMD_ALIGNED(kYuvJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    {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}};
1119
const struct YuvConstants SIMD_ALIGNED(kYvuJPEGConstants) = {
Frank Barchard's avatar
Frank Barchard committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    {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, 0},
    {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,
     0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
    {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}};
1130
#endif
1131

1132 1133 1134 1135 1136 1137 1138 1139 1140
#undef BB
#undef BG
#undef BR
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
#undef YG
1141

Frank Barchard's avatar
Frank Barchard committed
1142
// BT.709 YUV to RGB reference
1143 1144 1145 1146
//  R = (Y - 16) * 1.164              - V * -1.793
//  G = (Y - 16) * 1.164 - U *  0.213 - V *  0.533
//  B = (Y - 16) * 1.164 - U * -2.112
// See also http://www.equasys.de/colorconversion.html
Frank Barchard's avatar
Frank Barchard committed
1147 1148

// Y contribution to R,G,B.  Scale and bias.
Frank Barchard's avatar
Frank Barchard committed
1149
#define YG 18997  /* round(1.164 * 64 * 256 * 256 / 257) */
1150
#define YGB -1160 /* 1.164 * 64 * -16 + 64 / 2 */
Frank Barchard's avatar
Frank Barchard committed
1151

1152
// TODO(fbarchard): Find way to express 2.112 instead of 2.0.
Frank Barchard's avatar
Frank Barchard committed
1153
// U and V contributions to R,G,B.
1154
#define UB -128 /* max(-128, round(-2.112 * 64)) */
Frank Barchard's avatar
Frank Barchard committed
1155 1156
#define UG 14   /* round(0.213 * 64) */
#define VG 34   /* round(0.533  * 64) */
1157
#define VR -115 /* round(-1.793 * 64) */
Frank Barchard's avatar
Frank Barchard committed
1158 1159

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

1164
#if defined(__aarch64__)
Frank Barchard's avatar
Frank Barchard committed
1165
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1166 1167 1168 1169 1170 1171
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1172
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1173 1174 1175 1176 1177 1178
    {-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}};
1179
#elif defined(__arm__)
Frank Barchard's avatar
Frank Barchard committed
1180
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1181 1182 1183 1184
    {-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}};
Frank Barchard's avatar
Frank Barchard committed
1185
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1186 1187 1188 1189
    {-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}};
1190
#else
1191
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    {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}};
1202
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    {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, 0},
    {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,
     0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB, 0, UB},
    {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}};
1213
#endif
Frank Barchard's avatar
Frank Barchard committed
1214

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
#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.
Frank Barchard's avatar
Frank Barchard committed
1226 1227 1228 1229 1230 1231
static __inline void YuvPixel(uint8 y,
                              uint8 u,
                              uint8 v,
                              uint8* b,
                              uint8* g,
                              uint8* r,
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
                              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;
Frank Barchard's avatar
Frank Barchard committed
1263
  *b = Clamp((int32)(-(u * ub) + y1 + bb) >> 6);
1264
  *g = Clamp((int32)(-(u * ug + v * vg) + y1 + bg) >> 6);
Frank Barchard's avatar
Frank Barchard committed
1265
  *r = Clamp((int32)(-(v * vr) + y1 + br) >> 6);
1266 1267 1268
}

// Y contribution to R,G,B.  Scale and bias.
Frank Barchard's avatar
Frank Barchard committed
1269
#define YG 18997  /* round(1.164 * 64 * 256 * 256 / 257) */
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
#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
1282

1283
#if !defined(LIBYUV_DISABLE_NEON) && \
1284
    (defined(__ARM_NEON__) || defined(__aarch64__) || defined(LIBYUV_NEON))
1285 1286
// C mimic assembly.
// TODO(fbarchard): Remove subsampling from Neon.
1287 1288 1289
void I444ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1290
                     uint8* rgb_buf,
1291
                     const struct YuvConstants* yuvconstants,
1292
                     int width) {
1293 1294
  int x;
  for (x = 0; x < width - 1; x += 2) {
1295 1296
    uint8 u = (src_u[0] + src_u[1] + 1) >> 1;
    uint8 v = (src_v[0] + src_v[1] + 1) >> 1;
1297 1298
    YuvPixel(src_y[0], u, v, rgb_buf + 0, rgb_buf + 1, rgb_buf + 2,
             yuvconstants);
1299
    rgb_buf[3] = 255;
1300 1301
    YuvPixel(src_y[1], u, v, rgb_buf + 4, rgb_buf + 5, rgb_buf + 6,
             yuvconstants);
1302
    rgb_buf[7] = 255;
1303 1304 1305
    src_y += 2;
    src_u += 2;
    src_v += 2;
1306 1307 1308
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1309 1310
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
Frank Barchard's avatar
Frank Barchard committed
1311
    rgb_buf[3] = 255;
1312 1313 1314
  }
}
#else
1315 1316 1317
void I444ToARGBRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1318
                     uint8* rgb_buf,
1319
                     const struct YuvConstants* yuvconstants,
1320
                     int width) {
1321 1322
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
1323 1324
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1325
    rgb_buf[3] = 255;
1326 1327 1328
    src_y += 1;
    src_u += 1;
    src_v += 1;
1329 1330 1331
    rgb_buf += 4;  // Advance 1 pixel.
  }
}
1332
#endif
1333

1334
// Also used for 420
1335 1336 1337
void I422ToARGBRow_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) {
Frank Barchard's avatar
Frank Barchard committed
1343 1344
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1345
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1346 1347
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1348
    rgb_buf[7] = 255;
1349 1350 1351
    src_y += 2;
    src_u += 1;
    src_v += 1;
1352 1353 1354
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1355 1356
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
Frank Barchard's avatar
Frank Barchard committed
1357 1358 1359 1360
    rgb_buf[3] = 255;
  }
}

1361 1362 1363 1364 1365
void I422AlphaToARGBRow_C(const uint8* src_y,
                          const uint8* src_u,
                          const uint8* src_v,
                          const uint8* src_a,
                          uint8* rgb_buf,
1366
                          const struct YuvConstants* yuvconstants,
1367 1368 1369
                          int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1370 1371
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1372
    rgb_buf[3] = src_a[0];
Frank Barchard's avatar
Frank Barchard committed
1373 1374
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1375 1376 1377 1378 1379 1380 1381 1382
    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) {
Frank Barchard's avatar
Frank Barchard committed
1383 1384
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1385 1386 1387 1388
    rgb_buf[3] = src_a[0];
  }
}

1389 1390 1391
void I422ToRGB24Row_C(const uint8* src_y,
                      const uint8* src_u,
                      const uint8* src_v,
1392
                      uint8* rgb_buf,
1393
                      const struct YuvConstants* yuvconstants,
1394
                      int width) {
1395 1396
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1397 1398 1399 1400
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 3, rgb_buf + 4,
             rgb_buf + 5, yuvconstants);
1401 1402 1403
    src_y += 2;
    src_u += 1;
    src_v += 1;
1404 1405 1406
    rgb_buf += 6;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1407 1408
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1409 1410 1411
  }
}

1412 1413 1414
void I422ToARGB4444Row_C(const uint8* src_y,
                         const uint8* src_u,
                         const uint8* src_v,
1415
                         uint8* dst_argb4444,
1416
                         const struct YuvConstants* yuvconstants,
1417 1418 1419 1420 1421 1422 1423
                         int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1424 1425
  int x;
  for (x = 0; x < width - 1; x += 2) {
1426 1427
    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);
1428 1429 1430 1431 1432 1433
    b0 = b0 >> 4;
    g0 = g0 >> 4;
    r0 = r0 >> 4;
    b1 = b1 >> 4;
    g1 = g1 >> 4;
    r1 = r1 >> 4;
Frank Barchard's avatar
Frank Barchard committed
1434 1435
    *(uint32*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | (b1 << 16) |
                               (g1 << 20) | (r1 << 24) | 0xf000f000;
1436 1437 1438
    src_y += 2;
    src_u += 1;
    src_v += 1;
1439 1440 1441
    dst_argb4444 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1442
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1443 1444 1445
    b0 = b0 >> 4;
    g0 = g0 >> 4;
    r0 = r0 >> 4;
Frank Barchard's avatar
Frank Barchard committed
1446
    *(uint16*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | 0xf000;
1447 1448 1449
  }
}

1450 1451 1452
void I422ToARGB1555Row_C(const uint8* src_y,
                         const uint8* src_u,
                         const uint8* src_v,
1453
                         uint8* dst_argb1555,
1454
                         const struct YuvConstants* yuvconstants,
1455 1456 1457 1458 1459 1460 1461
                         int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1462 1463
  int x;
  for (x = 0; x < width - 1; x += 2) {
1464 1465
    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);
1466 1467 1468 1469 1470 1471
    b0 = b0 >> 3;
    g0 = g0 >> 3;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 3;
    r1 = r1 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1472 1473
    *(uint32*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | (b1 << 16) |
                               (g1 << 21) | (r1 << 26) | 0x80008000;
1474 1475 1476
    src_y += 2;
    src_u += 1;
    src_v += 1;
1477 1478 1479
    dst_argb1555 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1480
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1481 1482 1483
    b0 = b0 >> 3;
    g0 = g0 >> 3;
    r0 = r0 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1484
    *(uint16*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | 0x8000;
1485 1486 1487
  }
}

1488
void I422ToRGB565Row_C(const uint8* src_y,
1489 1490 1491
                       const uint8* src_u,
                       const uint8* src_v,
                       uint8* dst_rgb565,
1492
                       const struct YuvConstants* yuvconstants,
1493
                       int width) {
1494 1495 1496 1497 1498 1499
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1500 1501
  int x;
  for (x = 0; x < width - 1; x += 2) {
1502 1503
    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);
1504 1505 1506 1507 1508 1509
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 2;
    r1 = r1 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1510 1511
    *(uint32*)(dst_rgb565) =
        b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
1512 1513 1514
    src_y += 2;
    src_u += 1;
    src_v += 1;
1515 1516 1517
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1518
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1519 1520 1521
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
1522
    *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1523 1524 1525
  }
}

1526
void NV12ToARGBRow_C(const uint8* src_y,
Frank Barchard's avatar
Frank Barchard committed
1527
                     const uint8* src_uv,
1528
                     uint8* rgb_buf,
1529
                     const struct YuvConstants* yuvconstants,
1530
                     int width) {
1531 1532
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1533 1534
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1535
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1536 1537
    YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1538
    rgb_buf[7] = 255;
1539
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1540
    src_uv += 2;
1541 1542 1543
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1544 1545
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1546
    rgb_buf[3] = 255;
1547 1548 1549
  }
}

1550 1551 1552
void NV21ToARGBRow_C(const uint8* src_y,
                     const uint8* src_vu,
                     uint8* rgb_buf,
1553
                     const struct YuvConstants* yuvconstants,
1554 1555 1556
                     int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1557 1558
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1559
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1560 1561
    YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1562 1563 1564 1565 1566 1567
    rgb_buf[7] = 255;
    src_y += 2;
    src_vu += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1568 1569
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1570 1571 1572 1573
    rgb_buf[3] = 255;
  }
}

1574
void NV12ToRGB565Row_C(const uint8* src_y,
Frank Barchard's avatar
Frank Barchard committed
1575
                       const uint8* src_uv,
1576
                       uint8* dst_rgb565,
1577
                       const struct YuvConstants* yuvconstants,
1578 1579 1580 1581 1582 1583 1584
                       int width) {
  uint8 b0;
  uint8 g0;
  uint8 r0;
  uint8 b1;
  uint8 g1;
  uint8 r1;
1585 1586
  int x;
  for (x = 0; x < width - 1; x += 2) {
1587 1588
    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);
1589 1590 1591 1592 1593 1594
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
    b1 = b1 >> 3;
    g1 = g1 >> 2;
    r1 = r1 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1595 1596
    *(uint32*)(dst_rgb565) =
        b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
1597
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1598
    src_uv += 2;
1599 1600 1601
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1602
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
1603 1604 1605
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
1606
    *(uint16*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1607 1608 1609
  }
}

1610
void YUY2ToARGBRow_C(const uint8* src_yuy2,
1611
                     uint8* rgb_buf,
1612
                     const struct YuvConstants* yuvconstants,
1613
                     int width) {
1614 1615
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1616 1617
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1618
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1619 1620
    YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1621
    rgb_buf[7] = 255;
1622
    src_yuy2 += 4;
1623 1624 1625
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1626 1627
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1628
    rgb_buf[3] = 255;
1629 1630 1631
  }
}

1632
void UYVYToARGBRow_C(const uint8* src_uyvy,
1633
                     uint8* rgb_buf,
1634
                     const struct YuvConstants* yuvconstants,
1635
                     int width) {
1636 1637
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1638 1639
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1640
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1641 1642
    YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1643
    rgb_buf[7] = 255;
1644
    src_uyvy += 4;
1645 1646 1647
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1648 1649
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1650
    rgb_buf[3] = 255;
1651 1652 1653
  }
}

1654 1655 1656
void I422ToRGBARow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
1657
                     uint8* rgb_buf,
1658
                     const struct YuvConstants* yuvconstants,
1659
                     int width) {
1660 1661
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1662 1663
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
             rgb_buf + 3, yuvconstants);
1664
    rgb_buf[0] = 255;
Frank Barchard's avatar
Frank Barchard committed
1665 1666
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 5, rgb_buf + 6,
             rgb_buf + 7, yuvconstants);
1667
    rgb_buf[4] = 255;
1668 1669 1670
    src_y += 2;
    src_u += 1;
    src_v += 1;
1671 1672 1673
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1674 1675
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
             rgb_buf + 3, yuvconstants);
1676
    rgb_buf[0] = 255;
1677 1678 1679
  }
}

1680
void I400ToARGBRow_C(const uint8* src_y, uint8* rgb_buf, int width) {
1681 1682
  int x;
  for (x = 0; x < width - 1; x += 2) {
1683
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1684
    rgb_buf[3] = 255;
1685
    YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6);
1686 1687 1688 1689 1690
    rgb_buf[7] = 255;
    src_y += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1691
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1692
    rgb_buf[3] = 255;
1693 1694 1695
  }
}

1696
void MirrorRow_C(const uint8* src, uint8* dst, int width) {
1697
  int x;
1698
  src += width - 1;
1699
  for (x = 0; x < width - 1; x += 2) {
1700 1701 1702 1703 1704 1705
    dst[x] = src[0];
    dst[x + 1] = src[-1];
    src -= 2;
  }
  if (width & 1) {
    dst[width - 1] = src[0];
1706 1707 1708
  }
}

1709
void MirrorUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
1710
  int x;
1711
  src_uv += (width - 1) << 1;
1712
  for (x = 0; x < width - 1; x += 2) {
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
    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];
  }
}

1725
void ARGBMirrorRow_C(const uint8* src, uint8* dst, int width) {
1726
  int x;
1727 1728
  const uint32* src32 = (const uint32*)(src);
  uint32* dst32 = (uint32*)(dst);
1729
  src32 += width - 1;
1730
  for (x = 0; x < width - 1; x += 2) {
1731 1732 1733 1734 1735 1736 1737 1738 1739
    dst32[x] = src32[0];
    dst32[x + 1] = src32[-1];
    src32 -= 2;
  }
  if (width & 1) {
    dst32[width - 1] = src32[0];
  }
}

1740
void SplitUVRow_C(const uint8* src_uv, uint8* dst_u, uint8* dst_v, int width) {
1741 1742
  int x;
  for (x = 0; x < width - 1; x += 2) {
1743 1744 1745 1746 1747 1748 1749 1750 1751
    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];
1752 1753 1754
  }
}

Frank Barchard's avatar
Frank Barchard committed
1755 1756 1757
void MergeUVRow_C(const uint8* src_u,
                  const uint8* src_v,
                  uint8* dst_uv,
1758
                  int width) {
1759 1760
  int x;
  for (x = 0; x < width - 1; x += 2) {
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
    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];
  }
}

1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
void SplitRGBRow_C(const uint8* src_rgb,
                   uint8* dst_r,
                   uint8* dst_g,
                   uint8* dst_b,
                   int width) {
  int x;
  for (x = 0; x < width; ++x) {
    dst_r[x] = src_rgb[0];
    dst_g[x] = src_rgb[1];
    dst_b[x] = src_rgb[2];
    src_rgb += 3;
  }
}

void MergeRGBRow_C(const uint8* src_r,
                   const uint8* src_g,
                   const uint8* src_b,
                   uint8* dst_rgb,
                   int width) {
  int x;
  for (x = 0; x < width; ++x) {
    dst_rgb[0] = src_r[x];
    dst_rgb[1] = src_g[x];
    dst_rgb[2] = src_b[x];
    dst_rgb += 3;
  }
}

1801 1802 1803 1804 1805
void MergeUVRow_16_C(const uint16* src_u,
                     const uint16* src_v,
                     uint16* dst_uv,
                     int scale,
                     int width) {
1806 1807
  int x;
  for (x = 0; x < width - 1; x += 2) {
1808 1809 1810 1811
    dst_uv[0] = src_u[x] * scale;
    dst_uv[1] = src_v[x] * scale;
    dst_uv[2] = src_u[x + 1] * scale;
    dst_uv[3] = src_v[x + 1] * scale;
1812 1813 1814
    dst_uv += 4;
  }
  if (width & 1) {
1815 1816
    dst_uv[0] = src_u[width - 1] * scale;
    dst_uv[1] = src_v[width - 1] * scale;
1817 1818 1819
  }
}

1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
void MultiplyRow_16_C(const uint16* src_y,
                      uint16* dst_y,
                      int scale,
                      int width) {
  int x;
  for (x = 0; x < width; ++x) {
    dst_y[x] = src_y[x] * scale;
  }
}

1830 1831 1832 1833
void CopyRow_C(const uint8* src, uint8* dst, int count) {
  memcpy(dst, src, count);
}

1834 1835 1836 1837
void CopyRow_16_C(const uint16* src, uint16* dst, int count) {
  memcpy(dst, src, count * 2);
}

1838 1839
void SetRow_C(uint8* dst, uint8 v8, int width) {
  memset(dst, v8, width);
1840 1841
}

1842 1843 1844 1845 1846
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;
1847 1848 1849
  }
}

1850
// Filter 2 rows of YUY2 UV's (422) into U and V (420).
Frank Barchard's avatar
Frank Barchard committed
1851 1852 1853 1854 1855
void YUY2ToUVRow_C(const uint8* src_yuy2,
                   int src_stride_yuy2,
                   uint8* dst_u,
                   uint8* dst_v,
                   int width) {
1856
  // Output a row of UV values, filtering 2 rows of YUY2.
1857 1858
  int x;
  for (x = 0; x < width; x += 2) {
1859 1860 1861 1862 1863 1864 1865 1866
    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;
  }
}

1867 1868
// Copy row of YUY2 UV's (422) into U and V (422).
void YUY2ToUV422Row_C(const uint8* src_yuy2,
Frank Barchard's avatar
Frank Barchard committed
1869 1870 1871
                      uint8* dst_u,
                      uint8* dst_v,
                      int width) {
1872
  // Output a row of UV values.
1873 1874
  int x;
  for (x = 0; x < width; x += 2) {
1875 1876 1877 1878 1879 1880 1881 1882 1883
    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).
1884
void YUY2ToYRow_C(const uint8* src_yuy2, uint8* dst_y, int width) {
1885
  // Output a row of Y values.
1886 1887
  int x;
  for (x = 0; x < width - 1; x += 2) {
1888 1889 1890 1891 1892 1893
    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];
1894 1895 1896
  }
}

1897
// Filter 2 rows of UYVY UV's (422) into U and V (420).
Frank Barchard's avatar
Frank Barchard committed
1898 1899 1900 1901 1902
void UYVYToUVRow_C(const uint8* src_uyvy,
                   int src_stride_uyvy,
                   uint8* dst_u,
                   uint8* dst_v,
                   int width) {
1903
  // Output a row of UV values.
1904 1905
  int x;
  for (x = 0; x < width; x += 2) {
1906 1907 1908 1909 1910 1911 1912 1913
    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;
  }
}

1914 1915
// Copy row of UYVY UV's (422) into U and V (422).
void UYVYToUV422Row_C(const uint8* src_uyvy,
Frank Barchard's avatar
Frank Barchard committed
1916 1917 1918
                      uint8* dst_u,
                      uint8* dst_v,
                      int width) {
1919
  // Output a row of UV values.
1920 1921
  int x;
  for (x = 0; x < width; x += 2) {
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    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.
1933 1934
  int x;
  for (x = 0; x < width - 1; x += 2) {
1935 1936 1937
    dst_y[x] = src_uyvy[1];
    dst_y[x + 1] = src_uyvy[3];
    src_uyvy += 4;
1938 1939
  }
  if (width & 1) {
1940
    dst_y[width - 1] = src_uyvy[1];
1941 1942 1943
  }
}

1944
#define BLEND(f, b, a) (((256 - a) * b) >> 8) + f
1945

1946 1947
// Blend src_argb0 over src_argb1 and store to dst_argb.
// dst_argb may be src_argb0 or src_argb1.
1948
// This code mimics the SSSE3 version for better testability.
Frank Barchard's avatar
Frank Barchard committed
1949 1950 1951 1952
void ARGBBlendRow_C(const uint8* src_argb0,
                    const uint8* src_argb1,
                    uint8* dst_argb,
                    int width) {
1953 1954
  int x;
  for (x = 0; x < width - 1; x += 2) {
1955 1956 1957
    uint32 fb = src_argb0[0];
    uint32 fg = src_argb0[1];
    uint32 fr = src_argb0[2];
1958
    uint32 a = src_argb0[3];
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
    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];
1970
    a = src_argb0[4 + 3];
1971 1972 1973 1974 1975 1976 1977
    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;
1978 1979 1980 1981 1982 1983
    src_argb0 += 8;
    src_argb1 += 8;
    dst_argb += 8;
  }

  if (width & 1) {
1984 1985 1986
    uint32 fb = src_argb0[0];
    uint32 fg = src_argb0[1];
    uint32 fr = src_argb0[2];
1987
    uint32 a = src_argb0[3];
1988 1989 1990 1991 1992 1993 1994
    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;
1995 1996
  }
}
1997
#undef BLEND
1998

Frank Barchard's avatar
Frank Barchard committed
1999 2000 2001 2002 2003 2004
#define UBLEND(f, b, a) (((a)*f) + ((255 - a) * b) + 255) >> 8
void BlendPlaneRow_C(const uint8* src0,
                     const uint8* src1,
                     const uint8* alpha,
                     uint8* dst,
                     int width) {
2005
  int x;
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  for (x = 0; x < width - 1; x += 2) {
    dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
    dst[1] = UBLEND(src0[1], src1[1], alpha[1]);
    src0 += 2;
    src1 += 2;
    alpha += 2;
    dst += 2;
  }
  if (width & 1) {
    dst[0] = UBLEND(src0[0], src1[0], alpha[0]);
2016 2017
  }
}
2018
#undef UBLEND
2019

2020
#define ATTENUATE(f, a) (a | (a << 8)) * (f | (f << 8)) >> 24
2021

2022
// Multiply source RGB by alpha and store to destination.
2023
// This code mimics the SSSE3 version for better testability.
2024
void ARGBAttenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
2025 2026
  int i;
  for (i = 0; i < width - 1; i += 2) {
2027 2028 2029 2030
    uint32 b = src_argb[0];
    uint32 g = src_argb[1];
    uint32 r = src_argb[2];
    uint32 a = src_argb[3];
2031 2032 2033
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2034 2035 2036 2037 2038
    dst_argb[3] = a;
    b = src_argb[4];
    g = src_argb[5];
    r = src_argb[6];
    a = src_argb[7];
2039 2040 2041
    dst_argb[4] = ATTENUATE(b, a);
    dst_argb[5] = ATTENUATE(g, a);
    dst_argb[6] = ATTENUATE(r, a);
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
    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];
2052 2053 2054
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2055 2056 2057
    dst_argb[3] = a;
  }
}
2058
#undef ATTENUATE
2059

2060 2061 2062 2063 2064
// 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
2065 2066
// 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower.
#define T(a) 0x01000000 + (0x10000 / a)
2067
const uint32 fixed_invtbl8[256] = {
Frank Barchard's avatar
Frank Barchard committed
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 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
    0x01000000, 0x0100ffff, T(0x02), T(0x03),   T(0x04), T(0x05), T(0x06),
    T(0x07),    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), T(0xf8),   T(0xf9), T(0xfa), T(0xfb),
    T(0xfc),    T(0xfd),    T(0xfe), 0x01000100};
2105 2106 2107
#undef T

void ARGBUnattenuateRow_C(const uint8* src_argb, uint8* dst_argb, int width) {
2108 2109
  int i;
  for (i = 0; i < width; ++i) {
2110 2111 2112 2113
    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
2114
    const uint32 ia = fixed_invtbl8[a] & 0xffff;  // 8.8 fixed point
2115 2116 2117 2118
    b = (b * ia) >> 8;
    g = (g * ia) >> 8;
    r = (r * ia) >> 8;
    // Clamping should not be necessary but is free in assembly.
2119 2120 2121
    dst_argb[0] = clamp255(b);
    dst_argb[1] = clamp255(g);
    dst_argb[2] = clamp255(r);
2122 2123 2124 2125 2126 2127
    dst_argb[3] = a;
    src_argb += 4;
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2128 2129 2130 2131
void ComputeCumulativeSumRow_C(const uint8* row,
                               int32* cumsum,
                               const int32* previous_cumsum,
                               int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2132
  int32 row_sum[4] = {0, 0, 0, 0};
2133 2134
  int x;
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2135 2136 2137 2138
    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];
Frank Barchard's avatar
Frank Barchard committed
2139 2140 2141 2142
    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];
fbarchard@google.com's avatar
fbarchard@google.com committed
2143 2144 2145
  }
}

Frank Barchard's avatar
Frank Barchard committed
2146 2147 2148 2149 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
Frank Barchard's avatar
Frank Barchard committed
2167 2168 2169 2170 2171
void ARGBAffineRow_C(const uint8* src_argb,
                     int src_argb_stride,
                     uint8* dst_argb,
                     const float* uv_dudv,
                     int width) {
2172
  int i;
2173 2174 2175 2176
  // Render a row of pixels from source into a buffer.
  float uv[2];
  uv[0] = uv_dudv[0];
  uv[1] = uv_dudv[1];
2177
  for (i = 0; i < width; ++i) {
2178 2179 2180
    int x = (int)(uv[0]);
    int y = (int)(uv[1]);
    *(uint32*)(dst_argb) =
Frank Barchard's avatar
Frank Barchard committed
2181
        *(const uint32*)(src_argb + y * src_argb_stride + x * 4);
2182 2183 2184 2185 2186 2187
    dst_argb += 4;
    uv[0] += uv_dudv[2];
    uv[1] += uv_dudv[3];
  }
}

2188
// Blend 2 rows into 1.
Frank Barchard's avatar
Frank Barchard committed
2189 2190 2191 2192
static void HalfRow_C(const uint8* src_uv,
                      ptrdiff_t src_uv_stride,
                      uint8* dst_uv,
                      int width) {
2193
  int x;
2194
  for (x = 0; x < width; ++x) {
2195 2196 2197 2198
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2199 2200 2201 2202
static void HalfRow_16_C(const uint16* src_uv,
                         ptrdiff_t src_uv_stride,
                         uint16* dst_uv,
                         int width) {
2203
  int x;
2204
  for (x = 0; x < width; ++x) {
2205 2206 2207 2208
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

2209
// C version 2x2 -> 2x1.
Frank Barchard's avatar
Frank Barchard committed
2210 2211
void InterpolateRow_C(uint8* dst_ptr,
                      const uint8* src_ptr,
2212
                      ptrdiff_t src_stride,
Frank Barchard's avatar
Frank Barchard committed
2213 2214
                      int width,
                      int source_y_fraction) {
2215
  int y1_fraction = source_y_fraction;
2216
  int y0_fraction = 256 - y1_fraction;
2217 2218
  const uint8* src_ptr1 = src_ptr + src_stride;
  int x;
2219
  if (y1_fraction == 0) {
2220 2221 2222
    memcpy(dst_ptr, src_ptr, width);
    return;
  }
2223
  if (y1_fraction == 128) {
2224
    HalfRow_C(src_ptr, src_stride, dst_ptr, width);
2225 2226
    return;
  }
2227
  for (x = 0; x < width - 1; x += 2) {
2228 2229 2230 2231
    dst_ptr[0] =
        (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8;
    dst_ptr[1] =
        (src_ptr[1] * y0_fraction + src_ptr1[1] * y1_fraction + 128) >> 8;
2232 2233 2234
    src_ptr += 2;
    src_ptr1 += 2;
    dst_ptr += 2;
2235 2236
  }
  if (width & 1) {
2237 2238
    dst_ptr[0] =
        (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8;
2239 2240 2241
  }
}

Frank Barchard's avatar
Frank Barchard committed
2242 2243
void InterpolateRow_16_C(uint16* dst_ptr,
                         const uint16* src_ptr,
2244
                         ptrdiff_t src_stride,
Frank Barchard's avatar
Frank Barchard committed
2245 2246
                         int width,
                         int source_y_fraction) {
2247 2248 2249 2250 2251 2252 2253 2254 2255
  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) {
2256
    HalfRow_16_C(src_ptr, src_stride, dst_ptr, width);
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
    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;
2268
  }
2269 2270
}

fbarchard@google.com's avatar
fbarchard@google.com committed
2271
// Use first 4 shuffler values to reorder ARGB channels.
Frank Barchard's avatar
Frank Barchard committed
2272 2273 2274 2275
void ARGBShuffleRow_C(const uint8* src_argb,
                      uint8* dst_argb,
                      const uint8* shuffler,
                      int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2276 2277 2278 2279 2280
  int index0 = shuffler[0];
  int index1 = shuffler[1];
  int index2 = shuffler[2];
  int index3 = shuffler[3];
  // Shuffle a row of ARGB.
2281
  int x;
2282
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
    // 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
2297 2298 2299
void I422ToYUY2Row_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
Frank Barchard's avatar
Frank Barchard committed
2300 2301
                     uint8* dst_frame,
                     int width) {
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
  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
2316
    dst_frame[2] = 0;
2317 2318
    dst_frame[3] = src_v[0];
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2319 2320 2321 2322 2323
}

void I422ToUYVYRow_C(const uint8* src_y,
                     const uint8* src_u,
                     const uint8* src_v,
Frank Barchard's avatar
Frank Barchard committed
2324 2325
                     uint8* dst_frame,
                     int width) {
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
  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
2341
    dst_frame[3] = 0;
2342
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
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
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;
  }
}

2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
// Samples assumed to be unsigned in low 9, 10 or 12 bits.  Scale factor
// adjust the source integer range to the half float range desired.

// This magic constant is 2^-112. Multiplying by this
// is the same as subtracting 112 from the exponent, which
// is the difference in exponent bias between 32-bit and
// 16-bit floats. Once we've done this subtraction, we can
// simply extract the low bits of the exponent and the high
// bits of the mantissa from our float and we're done.

void HalfFloatRow_C(const uint16* src, uint16* dst, float scale, int width) {
  int i;
  float mult = 1.9259299444e-34f * scale;
  for (i = 0; i < width; ++i) {
    float value = src[i] * mult;
    dst[i] = (uint16)((*(uint32_t*)&value) >> 13);
  }
}

Frank Barchard's avatar
Frank Barchard committed
2404 2405 2406 2407 2408
void ARGBLumaColorTableRow_C(const uint8* src_argb,
                             uint8* dst_argb,
                             int width,
                             const uint8* luma,
                             uint32 lumacoeff) {
2409 2410 2411 2412 2413 2414 2415
  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.
Frank Barchard's avatar
Frank Barchard committed
2416 2417 2418
    const uint8* luma0 =
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
        luma;
2419 2420 2421 2422 2423
    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];
Frank Barchard's avatar
Frank Barchard committed
2424 2425 2426
    luma1 =
        ((src_argb[4] * bc + src_argb[5] * gc + src_argb[6] * rc) & 0x7F00u) +
        luma;
2427 2428 2429 2430 2431 2432 2433 2434 2435
    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.
Frank Barchard's avatar
Frank Barchard committed
2436 2437 2438
    const uint8* luma0 =
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
        luma;
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
    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];
  }
}

2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
void ARGBExtractAlphaRow_C(const uint8* src_argb, uint8* dst_a, int width) {
  int i;
  for (i = 0; i < width - 1; i += 2) {
    dst_a[0] = src_argb[3];
    dst_a[1] = src_argb[7];
    dst_a += 2;
    src_argb += 8;
  }
  if (width & 1) {
    dst_a[0] = src_argb[3];
  }
}

2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
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];
  }
}
2484

2485
// Maximum temporary width for wrappers to process at a time, in pixels.
2486
#define MAXTWIDTH 2048
2487

2488 2489
#if !(defined(_MSC_VER) && defined(_M_IX86)) && \
    defined(HAS_I422TORGB565ROW_SSSE3)
2490
// row_win.cc has asm version, but GCC uses 2 step wrapper.
2491 2492 2493
void I422ToRGB565Row_SSSE3(const uint8* src_y,
                           const uint8* src_u,
                           const uint8* src_v,
2494
                           uint8* dst_rgb565,
2495
                           const struct YuvConstants* yuvconstants,
2496
                           int width) {
2497 2498 2499
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2500
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2501
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
2502 2503 2504
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2505
    dst_rgb565 += twidth * 2;
2506 2507
    width -= twidth;
  }
2508
}
2509
#endif
2510

2511
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
2512 2513 2514
void I422ToARGB1555Row_SSSE3(const uint8* src_y,
                             const uint8* src_u,
                             const uint8* src_v,
2515
                             uint8* dst_argb1555,
2516
                             const struct YuvConstants* yuvconstants,
2517
                             int width) {
2518 2519 2520 2521
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2522
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2523
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
2524 2525 2526
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2527
    dst_argb1555 += twidth * 2;
2528 2529
    width -= twidth;
  }
2530
}
2531
#endif
2532

2533
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
2534 2535 2536
void I422ToARGB4444Row_SSSE3(const uint8* src_y,
                             const uint8* src_u,
                             const uint8* src_v,
2537
                             uint8* dst_argb4444,
2538
                             const struct YuvConstants* yuvconstants,
2539
                             int width) {
2540 2541 2542 2543
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2544
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2545
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
2546 2547 2548
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2549
    dst_argb4444 += twidth * 2;
2550 2551 2552
    width -= twidth;
  }
}
2553
#endif
2554

2555
#if defined(HAS_NV12TORGB565ROW_SSSE3)
2556 2557 2558
void NV12ToRGB565Row_SSSE3(const uint8* src_y,
                           const uint8* src_uv,
                           uint8* dst_rgb565,
2559
                           const struct YuvConstants* yuvconstants,
2560
                           int width) {
2561 2562 2563 2564
  // Row buffer for intermediate ARGB pixels.
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2565
    NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
2566 2567 2568 2569 2570 2571 2572
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
2573
#endif
2574

2575
#if defined(HAS_I422TORGB565ROW_AVX2)
2576 2577 2578 2579
void I422ToRGB565Row_AVX2(const uint8* src_y,
                          const uint8* src_u,
                          const uint8* src_v,
                          uint8* dst_rgb565,
2580
                          const struct YuvConstants* yuvconstants,
2581
                          int width) {
2582
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
2583 2584
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2585
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2586
#if defined(HAS_ARGBTORGB565ROW_AVX2)
2587
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
2588 2589 2590
#else
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
#endif
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
    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,
2605
                            const struct YuvConstants* yuvconstants,
2606 2607
                            int width) {
  // Row buffer for intermediate ARGB pixels.
2608
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
2609 2610
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2611
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2612
#if defined(HAS_ARGBTOARGB1555ROW_AVX2)
2613
    ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth);
2614 2615 2616
#else
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
#endif
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
    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,
2631
                            const struct YuvConstants* yuvconstants,
2632 2633
                            int width) {
  // Row buffer for intermediate ARGB pixels.
2634
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
2635 2636
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2637
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2638
#if defined(HAS_ARGBTOARGB4444ROW_AVX2)
2639
    ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth);
2640 2641 2642
#else
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
#endif
2643 2644 2645 2646 2647 2648 2649 2650 2651
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_argb4444 += twidth * 2;
    width -= twidth;
  }
}
#endif

2652 2653
#if defined(HAS_I422TORGB24ROW_AVX2)
void I422ToRGB24Row_AVX2(const uint8* src_y,
Frank Barchard's avatar
Frank Barchard committed
2654 2655 2656 2657 2658
                         const uint8* src_u,
                         const uint8* src_v,
                         uint8* dst_rgb24,
                         const struct YuvConstants* yuvconstants,
                         int width) {
2659
  // Row buffer for intermediate ARGB pixels.
2660
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
2661 2662
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2663
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
    // 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

2675
#if defined(HAS_NV12TORGB565ROW_AVX2)
2676 2677 2678
void NV12ToRGB565Row_AVX2(const uint8* src_y,
                          const uint8* src_uv,
                          uint8* dst_rgb565,
2679
                          const struct YuvConstants* yuvconstants,
2680
                          int width) {
2681
  // Row buffer for intermediate ARGB pixels.
2682
  SIMD_ALIGNED(uint8 row[MAXTWIDTH * 4]);
2683 2684
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2685
    NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
2686
#if defined(HAS_ARGBTORGB565ROW_AVX2)
2687
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
2688 2689 2690
#else
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
#endif
2691 2692 2693 2694 2695 2696 2697 2698
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
#endif

2699
float ScaleSumSamples_C(const float* src, float* dst, float scale, int width) {
2700
  float fsum = 0.f;
2701
  int i;
2702
#if defined(__clang__)
2703
#pragma clang loop vectorize_width(4)
2704
#endif
2705
  for (i = 0; i < width; ++i) {
2706
    float v = *src++;
2707
    fsum += v * v;
2708
    *dst++ = v * scale;
2709
  }
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
  return fsum;
}

float ScaleMaxSamples_C(const float* src, float* dst, float scale, int width) {
  float fmax = 0.f;
  int i;
  for (i = 0; i < width; ++i) {
    float v = *src++;
    float vs = v * scale;
    fmax = (v > fmax) ? v : fmax;
    *dst++ = vs;
  }
2722 2723 2724 2725 2726 2727
  return fmax;
}

void ScaleSamples_C(const float* src, float* dst, float scale, int width) {
  int i;
  for (i = 0; i < width; ++i) {
2728
    *dst++ = *src++ * scale;
2729 2730 2731
  }
}

2732 2733 2734 2735 2736 2737 2738 2739 2740
void GaussRow_C(const uint32* src, uint16* dst, int width) {
  int i;
  for (i = 0; i < width; ++i) {
    *dst++ =
        (src[0] + src[1] * 4 + src[2] * 6 + src[3] * 4 + src[4] + 128) >> 8;
    ++src;
  }
}

2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row.
void GaussCol_C(const uint16* src0,
                const uint16* src1,
                const uint16* src2,
                const uint16* src3,
                const uint16* src4,
                uint32* dst,
                int width) {
  int i;
  for (i = 0; i < width; ++i) {
    *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
  }
}

2755
#ifdef __cplusplus
2756
}  // extern "C"
2757 2758
}  // namespace libyuv
#endif