row_common.cc 98.5 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 <stdio.h>
14
#include <string.h>  // For memcpy and memset.
frkoenig@google.com's avatar
frkoenig@google.com committed
15

16 17
#include "libyuv/basic_types.h"

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

23 24 25 26
// llvm x86 is poor at ternary operator, so use branchless min/max.

#define USE_BRANCHLESS 1
#if USE_BRANCHLESS
Frank Barchard's avatar
Frank Barchard committed
27
static __inline int32_t clamp0(int32_t v) {
28 29 30
  return ((-(v) >> 31) & (v));
}

Frank Barchard's avatar
Frank Barchard committed
31
static __inline int32_t clamp255(int32_t v) {
32 33 34
  return (((255 - (v)) >> 31) | (v)) & 255;
}

Frank Barchard's avatar
Frank Barchard committed
35
static __inline int32_t clamp1023(int32_t v) {
36
  return (((1023 - (v)) >> 31) | (v)) & 1023;
37 38
}

Frank Barchard's avatar
Frank Barchard committed
39
static __inline uint32_t Abs(int32_t v) {
40 41 42
  int m = v >> 31;
  return (v + m) ^ m;
}
Frank Barchard's avatar
Frank Barchard committed
43
#else   // USE_BRANCHLESS
Frank Barchard's avatar
Frank Barchard committed
44
static __inline int32_t clamp0(int32_t v) {
45 46 47
  return (v < 0) ? 0 : v;
}

Frank Barchard's avatar
Frank Barchard committed
48
static __inline int32_t clamp255(int32_t v) {
49 50 51
  return (v > 255) ? 255 : v;
}

Frank Barchard's avatar
Frank Barchard committed
52
static __inline int32_t clamp1023(int32_t v) {
53
  return (v > 1023) ? 1023 : v;
54 55
}

Frank Barchard's avatar
Frank Barchard committed
56
static __inline uint32_t Abs(int32_t v) {
57 58 59
  return (v < 0) ? -v : v;
}
#endif  // USE_BRANCHLESS
Frank Barchard's avatar
Frank Barchard committed
60
static __inline uint32_t Clamp(int32_t val) {
61
  int v = clamp0(val);
Frank Barchard's avatar
Frank Barchard committed
62
  return (uint32_t)(clamp255(v));
63 64
}

Frank Barchard's avatar
Frank Barchard committed
65
static __inline uint32_t Clamp10(int32_t val) {
66
  int v = clamp0(val);
Frank Barchard's avatar
Frank Barchard committed
67
  return (uint32_t)(clamp1023(v));
68
}
69

70 71 72 73
// Little Endian
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || \
    defined(_M_IX86) || defined(__arm__) || defined(_M_ARM) ||     \
    (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
Frank Barchard's avatar
Frank Barchard committed
74
#define WRITEWORD(p, v) *(uint32_t*)(p) = v
75
#else
Frank Barchard's avatar
Frank Barchard committed
76 77 78 79 80
static inline void WRITEWORD(uint8_t* p, uint32_t v) {
  p[0] = (uint8_t)(v & 255);
  p[1] = (uint8_t)((v >> 8) & 255);
  p[2] = (uint8_t)((v >> 16) & 255);
  p[3] = (uint8_t)((v >> 24) & 255);
81 82 83
}
#endif

Frank Barchard's avatar
Frank Barchard committed
84
void RGB24ToARGBRow_C(const uint8_t* src_rgb24, uint8_t* dst_argb, int width) {
85 86
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
87 88 89
    uint8_t b = src_rgb24[0];
    uint8_t g = src_rgb24[1];
    uint8_t r = src_rgb24[2];
90 91 92 93 94
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = 255u;
    dst_argb += 4;
95
    src_rgb24 += 3;
96 97 98
  }
}

Frank Barchard's avatar
Frank Barchard committed
99
void RAWToARGBRow_C(const uint8_t* src_raw, uint8_t* dst_argb, int width) {
100 101
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
102 103 104
    uint8_t r = src_raw[0];
    uint8_t g = src_raw[1];
    uint8_t b = src_raw[2];
105 106 107 108 109
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = 255u;
    dst_argb += 4;
110
    src_raw += 3;
111 112 113
  }
}

Frank Barchard's avatar
Frank Barchard committed
114
void RAWToRGB24Row_C(const uint8_t* src_raw, uint8_t* dst_rgb24, int width) {
115 116
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
117 118 119
    uint8_t r = src_raw[0];
    uint8_t g = src_raw[1];
    uint8_t b = src_raw[2];
120 121 122 123 124 125 126 127
    dst_rgb24[0] = b;
    dst_rgb24[1] = g;
    dst_rgb24[2] = r;
    dst_rgb24 += 3;
    src_raw += 3;
  }
}

128 129 130
void RGB565ToARGBRow_C(const uint8_t* src_rgb565,
                       uint8_t* dst_argb,
                       int width) {
131 132
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
133 134 135
    uint8_t b = src_rgb565[0] & 0x1f;
    uint8_t g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8_t r = src_rgb565[1] >> 3;
136 137 138 139 140
    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;
141
    src_rgb565 += 2;
142 143 144
  }
}

Frank Barchard's avatar
Frank Barchard committed
145 146
void ARGB1555ToARGBRow_C(const uint8_t* src_argb1555,
                         uint8_t* dst_argb,
147
                         int width) {
148 149
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
150 151 152 153
    uint8_t b = src_argb1555[0] & 0x1f;
    uint8_t g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8_t r = (src_argb1555[1] & 0x7c) >> 2;
    uint8_t a = src_argb1555[1] >> 7;
154 155 156
    dst_argb[0] = (b << 3) | (b >> 2);
    dst_argb[1] = (g << 3) | (g >> 2);
    dst_argb[2] = (r << 3) | (r >> 2);
157
    dst_argb[3] = -a;
158
    dst_argb += 4;
159
    src_argb1555 += 2;
160 161 162
  }
}

Frank Barchard's avatar
Frank Barchard committed
163 164
void ARGB4444ToARGBRow_C(const uint8_t* src_argb4444,
                         uint8_t* dst_argb,
165
                         int width) {
166 167
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
168 169 170 171
    uint8_t b = src_argb4444[0] & 0x0f;
    uint8_t g = src_argb4444[0] >> 4;
    uint8_t r = src_argb4444[1] & 0x0f;
    uint8_t a = src_argb4444[1] >> 4;
172 173 174 175 176
    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;
177
    src_argb4444 += 2;
178 179 180
  }
}

Frank Barchard's avatar
Frank Barchard committed
181
void AR30ToARGBRow_C(const uint8_t* src_ar30, uint8_t* dst_argb, int width) {
182 183
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
184 185 186 187 188
    uint32_t ar30 = *(uint32_t*)src_ar30;
    uint32_t b = ar30 & 0x3ff;
    uint32_t g = (ar30 >> 10) & 0x3ff;
    uint32_t r = (ar30 >> 20) & 0x3ff;
    uint32_t a = (ar30 >> 30) & 0x3;
189 190 191 192 193 194 195 196 197
    dst_argb[0] = b >> 2;
    dst_argb[1] = g >> 2;
    dst_argb[2] = r >> 2;
    dst_argb[3] = a * 0x55;
    dst_argb += 4;
    src_ar30 += 4;
  }
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
void AR30ToABGRRow_C(const uint8_t* src_ar30, uint8_t* dst_abgr, int width) {
  int x;
  for (x = 0; x < width; ++x) {
    uint32_t ar30 = *(uint32_t*)src_ar30;
    uint32_t b = ar30 & 0x3ff;
    uint32_t g = (ar30 >> 10) & 0x3ff;
    uint32_t r = (ar30 >> 20) & 0x3ff;
    uint32_t a = (ar30 >> 30) & 0x3;
    dst_abgr[0] = r >> 2;
    dst_abgr[1] = g >> 2;
    dst_abgr[2] = b >> 2;
    dst_abgr[3] = a * 0x55;
    dst_abgr += 4;
    src_ar30 += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
215
void ARGBToRGB24Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
216 217
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
218 219 220
    uint8_t b = src_argb[0];
    uint8_t g = src_argb[1];
    uint8_t r = src_argb[2];
221 222 223 224 225 226 227 228
    dst_rgb[0] = b;
    dst_rgb[1] = g;
    dst_rgb[2] = r;
    dst_rgb += 3;
    src_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
229
void ARGBToRAWRow_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
230 231
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
232 233 234
    uint8_t b = src_argb[0];
    uint8_t g = src_argb[1];
    uint8_t r = src_argb[2];
235 236 237 238 239 240 241 242
    dst_rgb[0] = r;
    dst_rgb[1] = g;
    dst_rgb[2] = b;
    dst_rgb += 3;
    src_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
243
void ARGBToRGB565Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
244 245
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
246 247 248 249 250 251
    uint8_t b0 = src_argb[0] >> 3;
    uint8_t g0 = src_argb[1] >> 2;
    uint8_t r0 = src_argb[2] >> 3;
    uint8_t b1 = src_argb[4] >> 3;
    uint8_t g1 = src_argb[5] >> 2;
    uint8_t r1 = src_argb[6] >> 3;
252 253
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
                           (r1 << 27));
254 255 256 257
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
258 259 260 261
    uint8_t b0 = src_argb[0] >> 3;
    uint8_t g0 = src_argb[1] >> 2;
    uint8_t r0 = src_argb[2] >> 3;
    *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
262 263 264
  }
}

265 266 267 268 269 270 271 272
// 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
273 274 275
void ARGBToRGB565DitherRow_C(const uint8_t* src_argb,
                             uint8_t* dst_rgb,
                             const uint32_t dither4,
Frank Barchard's avatar
Frank Barchard committed
276
                             int width) {
277 278
  int x;
  for (x = 0; x < width - 1; x += 2) {
279 280
    int dither0 = ((const unsigned char*)(&dither4))[x & 3];
    int dither1 = ((const unsigned char*)(&dither4))[(x + 1) & 3];
Frank Barchard's avatar
Frank Barchard committed
281 282 283 284 285 286
    uint8_t b0 = clamp255(src_argb[0] + dither0) >> 3;
    uint8_t g0 = clamp255(src_argb[1] + dither0) >> 2;
    uint8_t r0 = clamp255(src_argb[2] + dither0) >> 3;
    uint8_t b1 = clamp255(src_argb[4] + dither1) >> 3;
    uint8_t g1 = clamp255(src_argb[5] + dither1) >> 2;
    uint8_t r1 = clamp255(src_argb[6] + dither1) >> 3;
287 288
    WRITEWORD(dst_rgb, b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) |
                           (r1 << 27));
289 290 291 292
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
293
    int dither0 = ((const unsigned char*)(&dither4))[(width - 1) & 3];
Frank Barchard's avatar
Frank Barchard committed
294 295 296 297
    uint8_t b0 = clamp255(src_argb[0] + dither0) >> 3;
    uint8_t g0 = clamp255(src_argb[1] + dither0) >> 2;
    uint8_t r0 = clamp255(src_argb[2] + dither0) >> 3;
    *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 11);
298 299 300
  }
}

Frank Barchard's avatar
Frank Barchard committed
301
void ARGBToARGB1555Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
302 303
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
304 305 306 307 308 309 310 311 312
    uint8_t b0 = src_argb[0] >> 3;
    uint8_t g0 = src_argb[1] >> 3;
    uint8_t r0 = src_argb[2] >> 3;
    uint8_t a0 = src_argb[3] >> 7;
    uint8_t b1 = src_argb[4] >> 3;
    uint8_t g1 = src_argb[5] >> 3;
    uint8_t r1 = src_argb[6] >> 3;
    uint8_t a1 = src_argb[7] >> 7;
    *(uint32_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15) |
313
                            (b1 << 16) | (g1 << 21) | (r1 << 26) | (a1 << 31);
314 315 316 317
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
318 319 320 321 322
    uint8_t b0 = src_argb[0] >> 3;
    uint8_t g0 = src_argb[1] >> 3;
    uint8_t r0 = src_argb[2] >> 3;
    uint8_t a0 = src_argb[3] >> 7;
    *(uint16_t*)(dst_rgb) = b0 | (g0 << 5) | (r0 << 10) | (a0 << 15);
323 324 325
  }
}

Frank Barchard's avatar
Frank Barchard committed
326
void ARGBToARGB4444Row_C(const uint8_t* src_argb, uint8_t* dst_rgb, int width) {
327 328
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
329 330 331 332 333 334 335 336
    uint8_t b0 = src_argb[0] >> 4;
    uint8_t g0 = src_argb[1] >> 4;
    uint8_t r0 = src_argb[2] >> 4;
    uint8_t a0 = src_argb[3] >> 4;
    uint8_t b1 = src_argb[4] >> 4;
    uint8_t g1 = src_argb[5] >> 4;
    uint8_t r1 = src_argb[6] >> 4;
    uint8_t a1 = src_argb[7] >> 4;
337 338
    *(uint32_t*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12) |
                            (b1 << 16) | (g1 << 20) | (r1 << 24) | (a1 << 28);
339 340 341 342
    dst_rgb += 4;
    src_argb += 8;
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
343 344 345 346 347
    uint8_t b0 = src_argb[0] >> 4;
    uint8_t g0 = src_argb[1] >> 4;
    uint8_t r0 = src_argb[2] >> 4;
    uint8_t a0 = src_argb[3] >> 4;
    *(uint16_t*)(dst_rgb) = b0 | (g0 << 4) | (r0 << 8) | (a0 << 12);
348 349 350
  }
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364
void ABGRToAR30Row_C(const uint8_t* src_abgr, uint8_t* dst_ar30, int width) {
  int x;
  for (x = 0; x < width; ++x) {
    uint32_t b0 = (src_abgr[0] >> 6) | ((uint32_t)(src_abgr[0]) << 2);
    uint32_t g0 = (src_abgr[1] >> 6) | ((uint32_t)(src_abgr[1]) << 2);
    uint32_t r0 = (src_abgr[2] >> 6) | ((uint32_t)(src_abgr[2]) << 2);
    uint32_t a0 = (src_abgr[3] >> 6);
    *(uint32_t*)(dst_ar30) = r0 | (g0 << 10) | (b0 << 20) | (a0 << 30);
    dst_ar30 += 4;
    src_abgr += 4;
  }
}

void ARGBToAR30Row_C(const uint8_t* src_argb, uint8_t* dst_ar30, int width) {
365 366
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
367 368 369 370
    uint32_t b0 = (src_argb[0] >> 6) | ((uint32_t)(src_argb[0]) << 2);
    uint32_t g0 = (src_argb[1] >> 6) | ((uint32_t)(src_argb[1]) << 2);
    uint32_t r0 = (src_argb[2] >> 6) | ((uint32_t)(src_argb[2]) << 2);
    uint32_t a0 = (src_argb[3] >> 6);
371 372
    *(uint32_t*)(dst_ar30) = b0 | (g0 << 10) | (r0 << 20) | (a0 << 30);
    dst_ar30 += 4;
373 374 375 376
    src_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
377
static __inline int RGBToY(uint8_t r, uint8_t g, uint8_t b) {
Frank Barchard's avatar
Frank Barchard committed
378
  return (66 * r + 129 * g + 25 * b + 0x1080) >> 8;
379 380
}

Frank Barchard's avatar
Frank Barchard committed
381
static __inline int RGBToU(uint8_t r, uint8_t g, uint8_t b) {
382
  return (112 * b - 74 * g - 38 * r + 0x8080) >> 8;
383
}
Frank Barchard's avatar
Frank Barchard committed
384
static __inline int RGBToV(uint8_t r, uint8_t g, uint8_t b) {
385
  return (112 * r - 94 * g - 18 * b + 0x8080) >> 8;
386 387
}

388
// ARGBToY_C and ARGBToUV_C
389
#define MAKEROWY(NAME, R, G, B, BPP)                                         \
Frank Barchard's avatar
Frank Barchard committed
390
  void NAME##ToYRow_C(const uint8_t* src_argb0, uint8_t* dst_y, int width) { \
391 392 393 394 395 396 397 398
    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_t* src_rgb0, int src_stride_rgb,          \
Frank Barchard's avatar
Frank Barchard committed
399
                       uint8_t* dst_u, uint8_t* dst_v, int width) {          \
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    const uint8_t* src_rgb1 = src_rgb0 + src_stride_rgb;                     \
    int x;                                                                   \
    for (x = 0; x < width - 1; x += 2) {                                     \
      uint8_t ab = (src_rgb0[B] + src_rgb0[B + BPP] + src_rgb1[B] +          \
                    src_rgb1[B + BPP]) >>                                    \
                   2;                                                        \
      uint8_t ag = (src_rgb0[G] + src_rgb0[G + BPP] + src_rgb1[G] +          \
                    src_rgb1[G + BPP]) >>                                    \
                   2;                                                        \
      uint8_t 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_t ab = (src_rgb0[B] + src_rgb1[B]) >> 1;                         \
      uint8_t ag = (src_rgb0[G] + src_rgb1[G]) >> 1;                         \
      uint8_t ar = (src_rgb0[R] + src_rgb1[R]) >> 1;                         \
      dst_u[0] = RGBToU(ar, ag, ab);                                         \
      dst_v[0] = RGBToV(ar, ag, ab);                                         \
    }                                                                        \
Frank Barchard's avatar
Frank Barchard committed
426
  }
427

428 429 430 431 432 433 434 435
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

436 437 438 439 440
// 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:
441 442 443
// b 0.1016 * 255 = 25.908 = 25
// g 0.5078 * 255 = 129.489 = 129
// r 0.2578 * 255 = 65.739 = 66
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
// 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
460

Frank Barchard's avatar
Frank Barchard committed
461
static __inline int RGBToYJ(uint8_t r, uint8_t g, uint8_t b) {
Frank Barchard's avatar
Frank Barchard committed
462
  return (38 * r + 75 * g + 15 * b + 64) >> 7;
463 464
}

Frank Barchard's avatar
Frank Barchard committed
465
static __inline int RGBToUJ(uint8_t r, uint8_t g, uint8_t b) {
466 467
  return (127 * b - 84 * g - 43 * r + 0x8080) >> 8;
}
Frank Barchard's avatar
Frank Barchard committed
468
static __inline int RGBToVJ(uint8_t r, uint8_t g, uint8_t b) {
469 470 471 472 473
  return (127 * r - 107 * g - 20 * b + 0x8080) >> 8;
}

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

474
// ARGBToYJ_C and ARGBToUVJ_C
475
#define MAKEROWYJ(NAME, R, G, B, BPP)                                         \
Frank Barchard's avatar
Frank Barchard committed
476
  void NAME##ToYJRow_C(const uint8_t* src_argb0, uint8_t* dst_y, int width) { \
477 478 479 480 481 482 483 484
    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_t* src_rgb0, int src_stride_rgb,          \
Frank Barchard's avatar
Frank Barchard committed
485
                        uint8_t* dst_u, uint8_t* dst_v, int width) {          \
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    const uint8_t* src_rgb1 = src_rgb0 + src_stride_rgb;                      \
    int x;                                                                    \
    for (x = 0; x < width - 1; x += 2) {                                      \
      uint8_t ab = AVGB(AVGB(src_rgb0[B], src_rgb1[B]),                       \
                        AVGB(src_rgb0[B + BPP], src_rgb1[B + BPP]));          \
      uint8_t ag = AVGB(AVGB(src_rgb0[G], src_rgb1[G]),                       \
                        AVGB(src_rgb0[G + BPP], src_rgb1[G + BPP]));          \
      uint8_t 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_t ab = AVGB(src_rgb0[B], src_rgb1[B]);                            \
      uint8_t ag = AVGB(src_rgb0[G], src_rgb1[G]);                            \
      uint8_t ar = AVGB(src_rgb0[R], src_rgb1[R]);                            \
      dst_u[0] = RGBToUJ(ar, ag, ab);                                         \
      dst_v[0] = RGBToVJ(ar, ag, ab);                                         \
    }                                                                         \
Frank Barchard's avatar
Frank Barchard committed
509
  }
510 511 512 513

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

Frank Barchard's avatar
Frank Barchard committed
514
void RGB565ToYRow_C(const uint8_t* src_rgb565, uint8_t* dst_y, int width) {
515 516
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
517 518 519
    uint8_t b = src_rgb565[0] & 0x1f;
    uint8_t g = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8_t r = src_rgb565[1] >> 3;
520 521 522 523 524 525 526 527 528
    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;
  }
}

Frank Barchard's avatar
Frank Barchard committed
529
void ARGB1555ToYRow_C(const uint8_t* src_argb1555, uint8_t* dst_y, int width) {
530 531
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
532 533 534
    uint8_t b = src_argb1555[0] & 0x1f;
    uint8_t g = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8_t r = (src_argb1555[1] & 0x7c) >> 2;
535 536 537 538 539 540 541 542 543
    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;
  }
}

Frank Barchard's avatar
Frank Barchard committed
544
void ARGB4444ToYRow_C(const uint8_t* src_argb4444, uint8_t* dst_y, int width) {
545 546
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
547 548 549
    uint8_t b = src_argb4444[0] & 0x0f;
    uint8_t g = src_argb4444[0] >> 4;
    uint8_t r = src_argb4444[1] & 0x0f;
550 551 552 553 554 555 556 557 558
    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
559
void RGB565ToUVRow_C(const uint8_t* src_rgb565,
Frank Barchard's avatar
Frank Barchard committed
560
                     int src_stride_rgb565,
Frank Barchard's avatar
Frank Barchard committed
561 562
                     uint8_t* dst_u,
                     uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
563
                     int width) {
Frank Barchard's avatar
Frank Barchard committed
564
  const uint8_t* next_rgb565 = src_rgb565 + src_stride_rgb565;
565 566
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
    uint8_t b0 = src_rgb565[0] & 0x1f;
    uint8_t g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8_t r0 = src_rgb565[1] >> 3;
    uint8_t b1 = src_rgb565[2] & 0x1f;
    uint8_t g1 = (src_rgb565[2] >> 5) | ((src_rgb565[3] & 0x07) << 3);
    uint8_t r1 = src_rgb565[3] >> 3;
    uint8_t b2 = next_rgb565[0] & 0x1f;
    uint8_t g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
    uint8_t r2 = next_rgb565[1] >> 3;
    uint8_t b3 = next_rgb565[2] & 0x1f;
    uint8_t g3 = (next_rgb565[2] >> 5) | ((next_rgb565[3] & 0x07) << 3);
    uint8_t r3 = next_rgb565[3] >> 3;
    uint8_t b = (b0 + b1 + b2 + b3);  // 565 * 4 = 787.
    uint8_t g = (g0 + g1 + g2 + g3);
    uint8_t r = (r0 + r1 + r2 + r3);
582 583 584 585
    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
586 587 588 589 590 591
    src_rgb565 += 4;
    next_rgb565 += 4;
    dst_u += 1;
    dst_v += 1;
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
592 593 594 595 596 597 598 599 600
    uint8_t b0 = src_rgb565[0] & 0x1f;
    uint8_t g0 = (src_rgb565[0] >> 5) | ((src_rgb565[1] & 0x07) << 3);
    uint8_t r0 = src_rgb565[1] >> 3;
    uint8_t b2 = next_rgb565[0] & 0x1f;
    uint8_t g2 = (next_rgb565[0] >> 5) | ((next_rgb565[1] & 0x07) << 3);
    uint8_t r2 = next_rgb565[1] >> 3;
    uint8_t b = (b0 + b2);  // 565 * 2 = 676.
    uint8_t g = (g0 + g2);
    uint8_t r = (r0 + r2);
601 602 603 604 605 606 607 608
    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
609
void ARGB1555ToUVRow_C(const uint8_t* src_argb1555,
Frank Barchard's avatar
Frank Barchard committed
610
                       int src_stride_argb1555,
Frank Barchard's avatar
Frank Barchard committed
611 612
                       uint8_t* dst_u,
                       uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
613
                       int width) {
Frank Barchard's avatar
Frank Barchard committed
614
  const uint8_t* next_argb1555 = src_argb1555 + src_stride_argb1555;
615 616
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    uint8_t b0 = src_argb1555[0] & 0x1f;
    uint8_t g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8_t r0 = (src_argb1555[1] & 0x7c) >> 2;
    uint8_t b1 = src_argb1555[2] & 0x1f;
    uint8_t g1 = (src_argb1555[2] >> 5) | ((src_argb1555[3] & 0x03) << 3);
    uint8_t r1 = (src_argb1555[3] & 0x7c) >> 2;
    uint8_t b2 = next_argb1555[0] & 0x1f;
    uint8_t g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
    uint8_t r2 = (next_argb1555[1] & 0x7c) >> 2;
    uint8_t b3 = next_argb1555[2] & 0x1f;
    uint8_t g3 = (next_argb1555[2] >> 5) | ((next_argb1555[3] & 0x03) << 3);
    uint8_t r3 = (next_argb1555[3] & 0x7c) >> 2;
    uint8_t b = (b0 + b1 + b2 + b3);  // 555 * 4 = 777.
    uint8_t g = (g0 + g1 + g2 + g3);
    uint8_t r = (r0 + r1 + r2 + r3);
632 633 634 635 636 637 638 639 640 641 642
    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) {
Frank Barchard's avatar
Frank Barchard committed
643 644 645 646 647 648 649 650 651
    uint8_t b0 = src_argb1555[0] & 0x1f;
    uint8_t g0 = (src_argb1555[0] >> 5) | ((src_argb1555[1] & 0x03) << 3);
    uint8_t r0 = (src_argb1555[1] & 0x7c) >> 2;
    uint8_t b2 = next_argb1555[0] & 0x1f;
    uint8_t g2 = (next_argb1555[0] >> 5) | ((next_argb1555[1] & 0x03) << 3);
    uint8_t r2 = next_argb1555[1] >> 3;
    uint8_t b = (b0 + b2);  // 555 * 2 = 666.
    uint8_t g = (g0 + g2);
    uint8_t r = (r0 + r2);
652 653 654 655 656 657 658 659
    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
660
void ARGB4444ToUVRow_C(const uint8_t* src_argb4444,
Frank Barchard's avatar
Frank Barchard committed
661
                       int src_stride_argb4444,
Frank Barchard's avatar
Frank Barchard committed
662 663
                       uint8_t* dst_u,
                       uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
664
                       int width) {
Frank Barchard's avatar
Frank Barchard committed
665
  const uint8_t* next_argb4444 = src_argb4444 + src_stride_argb4444;
666 667
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    uint8_t b0 = src_argb4444[0] & 0x0f;
    uint8_t g0 = src_argb4444[0] >> 4;
    uint8_t r0 = src_argb4444[1] & 0x0f;
    uint8_t b1 = src_argb4444[2] & 0x0f;
    uint8_t g1 = src_argb4444[2] >> 4;
    uint8_t r1 = src_argb4444[3] & 0x0f;
    uint8_t b2 = next_argb4444[0] & 0x0f;
    uint8_t g2 = next_argb4444[0] >> 4;
    uint8_t r2 = next_argb4444[1] & 0x0f;
    uint8_t b3 = next_argb4444[2] & 0x0f;
    uint8_t g3 = next_argb4444[2] >> 4;
    uint8_t r3 = next_argb4444[3] & 0x0f;
    uint8_t b = (b0 + b1 + b2 + b3);  // 444 * 4 = 666.
    uint8_t g = (g0 + g1 + g2 + g3);
    uint8_t r = (r0 + r1 + r2 + r3);
683 684 685 686 687 688 689 690 691 692 693
    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) {
Frank Barchard's avatar
Frank Barchard committed
694 695 696 697 698 699 700 701 702
    uint8_t b0 = src_argb4444[0] & 0x0f;
    uint8_t g0 = src_argb4444[0] >> 4;
    uint8_t r0 = src_argb4444[1] & 0x0f;
    uint8_t b2 = next_argb4444[0] & 0x0f;
    uint8_t g2 = next_argb4444[0] >> 4;
    uint8_t r2 = next_argb4444[1] & 0x0f;
    uint8_t b = (b0 + b2);  // 444 * 2 = 555.
    uint8_t g = (g0 + g2);
    uint8_t r = (r0 + r2);
703 704 705 706 707
    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
708 709 710
  }
}

Frank Barchard's avatar
Frank Barchard committed
711 712 713
void ARGBToUV444Row_C(const uint8_t* src_argb,
                      uint8_t* dst_u,
                      uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
714
                      int width) {
715 716
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
717 718 719
    uint8_t ab = src_argb[0];
    uint8_t ag = src_argb[1];
    uint8_t ar = src_argb[2];
720 721 722 723 724 725 726 727
    dst_u[0] = RGBToU(ar, ag, ab);
    dst_v[0] = RGBToV(ar, ag, ab);
    src_argb += 4;
    dst_u += 1;
    dst_v += 1;
  }
}

Frank Barchard's avatar
Frank Barchard committed
728
void ARGBGrayRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) {
729 730
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
731
    uint8_t y = RGBToYJ(src_argb[2], src_argb[1], src_argb[0]);
732
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
733
    dst_argb[3] = src_argb[3];
734
    dst_argb += 4;
735
    src_argb += 4;
736 737 738
  }
}

739
// Convert a row of image to Sepia tone.
Frank Barchard's avatar
Frank Barchard committed
740
void ARGBSepiaRow_C(uint8_t* dst_argb, int width) {
741 742
  int x;
  for (x = 0; x < width; ++x) {
743 744 745
    int b = dst_argb[0];
    int g = dst_argb[1];
    int r = dst_argb[2];
746 747 748
    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;
749
    // b does not over flow. a is preserved from original.
750
    dst_argb[0] = sb;
751 752
    dst_argb[1] = clamp255(sg);
    dst_argb[2] = clamp255(sr);
753 754 755 756
    dst_argb += 4;
  }
}

757
// Apply color matrix to a row of image. Matrix is signed.
758
// TODO(fbarchard): Consider adding rounding (+32).
Frank Barchard's avatar
Frank Barchard committed
759 760 761
void ARGBColorMatrixRow_C(const uint8_t* src_argb,
                          uint8_t* dst_argb,
                          const int8_t* matrix_argb,
Frank Barchard's avatar
Frank Barchard committed
762
                          int width) {
763 764
  int x;
  for (x = 0; x < width; ++x) {
765 766 767 768
    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
769 770 771 772 773 774 775 776 777 778 779 780
    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;
781 782 783
    dst_argb[0] = Clamp(sb);
    dst_argb[1] = Clamp(sg);
    dst_argb[2] = Clamp(sr);
784 785
    dst_argb[3] = Clamp(sa);
    src_argb += 4;
786 787 788 789
    dst_argb += 4;
  }
}

790
// Apply color table to a row of image.
791 792 793
void ARGBColorTableRow_C(uint8_t* dst_argb,
                         const uint8_t* table_argb,
                         int width) {
794 795
  int x;
  for (x = 0; x < width; ++x) {
796 797 798 799 800 801 802 803 804 805 806 807
    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;
  }
}

808
// Apply color table to a row of image.
809 810 811
void RGBColorTableRow_C(uint8_t* dst_argb,
                        const uint8_t* table_argb,
                        int width) {
812 813
  int x;
  for (x = 0; x < width; ++x) {
814 815 816 817 818 819 820 821 822 823
    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
824
void ARGBQuantizeRow_C(uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
825 826 827 828
                       int scale,
                       int interval_size,
                       int interval_offset,
                       int width) {
829 830
  int x;
  for (x = 0; x < width; ++x) {
831 832 833 834 835 836 837 838 839 840
    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;
  }
}

841
#define REPEAT8(v) (v) | ((v) << 8)
Frank Barchard's avatar
Frank Barchard committed
842
#define SHADE(f, v) v* f >> 24
843

Frank Barchard's avatar
Frank Barchard committed
844 845
void ARGBShadeRow_C(const uint8_t* src_argb,
                    uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
846
                    int width,
Frank Barchard's avatar
Frank Barchard committed
847 848 849 850 851
                    uint32_t value) {
  const uint32_t b_scale = REPEAT8(value & 0xff);
  const uint32_t g_scale = REPEAT8((value >> 8) & 0xff);
  const uint32_t r_scale = REPEAT8((value >> 16) & 0xff);
  const uint32_t a_scale = REPEAT8(value >> 24);
852

853 854
  int i;
  for (i = 0; i < width; ++i) {
Frank Barchard's avatar
Frank Barchard committed
855 856 857 858
    const uint32_t b = REPEAT8(src_argb[0]);
    const uint32_t g = REPEAT8(src_argb[1]);
    const uint32_t r = REPEAT8(src_argb[2]);
    const uint32_t a = REPEAT8(src_argb[3]);
859 860 861 862 863 864 865 866 867 868 869
    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

870
#define REPEAT8(v) (v) | ((v) << 8)
Frank Barchard's avatar
Frank Barchard committed
871
#define SHADE(f, v) v* f >> 16
872

Frank Barchard's avatar
Frank Barchard committed
873 874 875
void ARGBMultiplyRow_C(const uint8_t* src_argb0,
                       const uint8_t* src_argb1,
                       uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
876
                       int width) {
877 878
  int i;
  for (i = 0; i < width; ++i) {
Frank Barchard's avatar
Frank Barchard committed
879 880 881 882 883 884 885 886
    const uint32_t b = REPEAT8(src_argb0[0]);
    const uint32_t g = REPEAT8(src_argb0[1]);
    const uint32_t r = REPEAT8(src_argb0[2]);
    const uint32_t a = REPEAT8(src_argb0[3]);
    const uint32_t b_scale = src_argb1[0];
    const uint32_t g_scale = src_argb1[1];
    const uint32_t r_scale = src_argb1[2];
    const uint32_t a_scale = src_argb1[3];
887 888 889 890
    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);
891 892
    src_argb0 += 4;
    src_argb1 += 4;
893 894 895 896 897 898
    dst_argb += 4;
  }
}
#undef REPEAT8
#undef SHADE

899
#define SHADE(f, v) clamp255(v + f)
900

Frank Barchard's avatar
Frank Barchard committed
901 902 903
void ARGBAddRow_C(const uint8_t* src_argb0,
                  const uint8_t* src_argb1,
                  uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
904
                  int width) {
905 906
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
907 908 909 910 911 912 913 914
    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];
915 916 917 918 919 920 921 922 923 924 925
    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

926
#define SHADE(f, v) clamp0(f - v)
927

Frank Barchard's avatar
Frank Barchard committed
928 929 930
void ARGBSubtractRow_C(const uint8_t* src_argb0,
                       const uint8_t* src_argb1,
                       uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
931
                       int width) {
932 933
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
934 935 936 937 938 939 940 941
    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];
942 943 944 945 946 947 948 949 950 951 952
    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
953
// Sobel functions which mimics SSSE3.
Frank Barchard's avatar
Frank Barchard committed
954 955 956 957
void SobelXRow_C(const uint8_t* src_y0,
                 const uint8_t* src_y1,
                 const uint8_t* src_y2,
                 uint8_t* dst_sobelx,
Frank Barchard's avatar
Frank Barchard committed
958
                 int width) {
959 960
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
961 962 963 964 965 966 967 968 969
    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;
970
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
Frank Barchard's avatar
Frank Barchard committed
971
    dst_sobelx[i] = (uint8_t)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
972 973 974
  }
}

Frank Barchard's avatar
Frank Barchard committed
975 976 977
void SobelYRow_C(const uint8_t* src_y0,
                 const uint8_t* src_y1,
                 uint8_t* dst_sobely,
Frank Barchard's avatar
Frank Barchard committed
978
                 int width) {
979 980
  int i;
  for (i = 0; i < width; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
981 982 983 984 985 986 987 988 989
    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;
990
    int sobel = Abs(a_diff + b_diff * 2 + c_diff);
Frank Barchard's avatar
Frank Barchard committed
991
    dst_sobely[i] = (uint8_t)(clamp255(sobel));
fbarchard@google.com's avatar
fbarchard@google.com committed
992 993 994
  }
}

Frank Barchard's avatar
Frank Barchard committed
995 996 997
void SobelRow_C(const uint8_t* src_sobelx,
                const uint8_t* src_sobely,
                uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
998
                int width) {
999 1000
  int i;
  for (i = 0; i < width; ++i) {
1001 1002
    int r = src_sobelx[i];
    int b = src_sobely[i];
1003
    int s = clamp255(r + b);
Frank Barchard's avatar
Frank Barchard committed
1004 1005 1006 1007
    dst_argb[0] = (uint8_t)(s);
    dst_argb[1] = (uint8_t)(s);
    dst_argb[2] = (uint8_t)(s);
    dst_argb[3] = (uint8_t)(255u);
1008 1009 1010 1011
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
1012 1013 1014
void SobelToPlaneRow_C(const uint8_t* src_sobelx,
                       const uint8_t* src_sobely,
                       uint8_t* dst_y,
Frank Barchard's avatar
Frank Barchard committed
1015
                       int width) {
1016 1017
  int i;
  for (i = 0; i < width; ++i) {
1018 1019 1020
    int r = src_sobelx[i];
    int b = src_sobely[i];
    int s = clamp255(r + b);
Frank Barchard's avatar
Frank Barchard committed
1021
    dst_y[i] = (uint8_t)(s);
1022 1023 1024
  }
}

Frank Barchard's avatar
Frank Barchard committed
1025 1026 1027
void SobelXYRow_C(const uint8_t* src_sobelx,
                  const uint8_t* src_sobely,
                  uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
1028
                  int width) {
1029 1030
  int i;
  for (i = 0; i < width; ++i) {
1031 1032
    int r = src_sobelx[i];
    int b = src_sobely[i];
1033
    int g = clamp255(r + b);
Frank Barchard's avatar
Frank Barchard committed
1034 1035 1036 1037
    dst_argb[0] = (uint8_t)(b);
    dst_argb[1] = (uint8_t)(g);
    dst_argb[2] = (uint8_t)(r);
    dst_argb[3] = (uint8_t)(255u);
1038 1039 1040 1041
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
1042
void J400ToARGBRow_C(const uint8_t* src_y, uint8_t* dst_argb, int width) {
1043
  // Copy a Y to RGB.
1044 1045
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
1046
    uint8_t y = src_y[0];
1047 1048 1049 1050 1051 1052 1053
    dst_argb[2] = dst_argb[1] = dst_argb[0] = y;
    dst_argb[3] = 255u;
    dst_argb += 4;
    ++src_y;
  }
}

1054 1055 1056
// TODO(fbarchard): Unify these structures to be platform independent.
// TODO(fbarchard): Generate SIMD structures from float matrix.

1057 1058 1059 1060 1061
// 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

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

// U and V contributions to R,G,B.
1067
#define UB -128 /* max(-128, round(-2.018 * 64)) */
Frank Barchard's avatar
Frank Barchard committed
1068 1069
#define UG 25   /* round(0.391 * 64) */
#define VG 52   /* round(0.813 * 64) */
1070
#define VR -102 /* round(-1.596 * 64) */
1071

1072
// Bias values to subtract 16 from Y and 128 from U and V.
Frank Barchard's avatar
Frank Barchard committed
1073
#define BB (UB * 128 + YGB)
1074
#define BG (UG * 128 + VG * 128 + YGB)
Frank Barchard's avatar
Frank Barchard committed
1075
#define BR (VR * 128 + YGB)
1076

1077
#if defined(__aarch64__)  // 64 bit arm
Frank Barchard's avatar
Frank Barchard committed
1078
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1079 1080 1081 1082 1083 1084
    {-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
1085
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1086 1087 1088 1089 1090 1091
    {-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}};
1092
#elif defined(__arm__)  // 32 bit arm
Frank Barchard's avatar
Frank Barchard committed
1093
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1094 1095 1096 1097
    {-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
1098
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1099 1100 1101 1102
    {-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}};
1103
#else
1104
const struct YuvConstants SIMD_ALIGNED(kYuvI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    {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}};
1115
const struct YuvConstants SIMD_ALIGNED(kYvuI601Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    {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}};
1126 1127
#endif

1128 1129 1130
#undef BB
#undef BG
#undef BR
1131 1132 1133 1134 1135
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
1136 1137
#undef YG

1138 1139 1140 1141 1142 1143
// 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.
1144
#define YG 16320 /* round(1.000 * 64 * 256 * 256 / 257) */
Frank Barchard's avatar
Frank Barchard committed
1145
#define YGB 32   /* 64 / 2 */
1146 1147

// U and V contributions to R,G,B.
1148
#define UB -113 /* round(-1.77200 * 64) */
Frank Barchard's avatar
Frank Barchard committed
1149 1150 1151
#define UG 22   /* round(0.34414 * 64) */
#define VG 46   /* round(0.71414  * 64) */
#define VR -90  /* round(-1.40200 * 64) */
1152

1153
// Bias values to round, and subtract 128 from U and V.
Frank Barchard's avatar
Frank Barchard committed
1154
#define BB (UB * 128 + YGB)
1155
#define BG (UG * 128 + VG * 128 + YGB)
Frank Barchard's avatar
Frank Barchard committed
1156
#define BR (VR * 128 + YGB)
1157

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

1209 1210 1211 1212 1213 1214 1215 1216 1217
#undef BB
#undef BG
#undef BR
#undef YGB
#undef UB
#undef UG
#undef VG
#undef VR
#undef YG
1218

Frank Barchard's avatar
Frank Barchard committed
1219
// BT.709 YUV to RGB reference
1220 1221 1222 1223
//  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
1224 1225

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

1229
// TODO(fbarchard): Find way to express 2.112 instead of 2.0.
Frank Barchard's avatar
Frank Barchard committed
1230
// U and V contributions to R,G,B.
1231
#define UB -128 /* max(-128, round(-2.112 * 64)) */
Frank Barchard's avatar
Frank Barchard committed
1232 1233
#define UG 14   /* round(0.213 * 64) */
#define VG 34   /* round(0.533  * 64) */
1234
#define VR -115 /* round(-1.793 * 64) */
Frank Barchard's avatar
Frank Barchard committed
1235 1236

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

1241
#if defined(__aarch64__)
Frank Barchard's avatar
Frank Barchard committed
1242
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1243 1244 1245 1246 1247 1248
    {-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
1249
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1250 1251 1252 1253 1254 1255
    {-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}};
1256
#elif defined(__arm__)
Frank Barchard's avatar
Frank Barchard committed
1257
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1258 1259 1260 1261
    {-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
1262
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1263 1264 1265 1266
    {-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}};
1267
#else
1268
const struct YuvConstants SIMD_ALIGNED(kYuvH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    {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}};
1279
const struct YuvConstants SIMD_ALIGNED(kYvuH709Constants) = {
Frank Barchard's avatar
Frank Barchard committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    {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}};
1290
#endif
Frank Barchard's avatar
Frank Barchard committed
1291

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
#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.
1303 1304
// Reads 8 bit YUV and leaves result as 16 bit.

Frank Barchard's avatar
Frank Barchard committed
1305 1306 1307 1308 1309 1310
static __inline void YuvPixel(uint8_t y,
                              uint8_t u,
                              uint8_t v,
                              uint8_t* b,
                              uint8_t* g,
                              uint8_t* r,
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
                              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

Frank Barchard's avatar
Frank Barchard committed
1341 1342 1343 1344
  uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16;
  *b = Clamp((int32_t)(-(u * ub) + y1 + bb) >> 6);
  *g = Clamp((int32_t)(-(u * ug + v * vg) + y1 + bg) >> 6);
  *r = Clamp((int32_t)(-(v * vr) + y1 + br) >> 6);
1345 1346
}

1347
// Reads 8 bit YUV and leaves result as 16 bit.
Frank Barchard's avatar
Frank Barchard committed
1348 1349 1350
static __inline void YuvPixel8_16(uint8_t y,
                                  uint8_t u,
                                  uint8_t v,
1351 1352 1353 1354
                                  int* b,
                                  int* g,
                                  int* r,
                                  const struct YuvConstants* yuvconstants) {
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
#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

Frank Barchard's avatar
Frank Barchard committed
1384
  uint32_t y1 = (uint32_t)(y * 0x0101 * yg) >> 16;
1385 1386 1387
  *b = (int)(-(u * ub) + y1 + bb);
  *g = (int)(-(u * ug + v * vg) + y1 + bg);
  *r = (int)(-(v * vr) + y1 + br);
1388 1389
}

1390
// C reference code that mimics the YUV 16 bit assembly.
1391
// Reads 10 bit YUV and leaves result as 16 bit.
Frank Barchard's avatar
Frank Barchard committed
1392 1393 1394
static __inline void YuvPixel16(int16_t y,
                                int16_t u,
                                int16_t v,
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
                                int* b,
                                int* g,
                                int* r,
                                const struct YuvConstants* yuvconstants) {
#if defined(__aarch64__)
  int ub = -yuvconstants->kUVToRB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[1];
  int vr = -yuvconstants->kUVToRB[1];
  int bb = yuvconstants->kUVBiasBGR[0];
  int bg = yuvconstants->kUVBiasBGR[1];
  int br = yuvconstants->kUVBiasBGR[2];
  int yg = yuvconstants->kYToRgb[0] / 0x0101;
#elif defined(__arm__)
  int ub = -yuvconstants->kUVToRB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[4];
  int vr = -yuvconstants->kUVToRB[4];
  int bb = yuvconstants->kUVBiasBGR[0];
  int bg = yuvconstants->kUVBiasBGR[1];
  int br = yuvconstants->kUVBiasBGR[2];
  int yg = yuvconstants->kYToRgb[0] / 0x0101;
#else
  int ub = yuvconstants->kUVToB[0];
  int ug = yuvconstants->kUVToG[0];
  int vg = yuvconstants->kUVToG[1];
  int vr = yuvconstants->kUVToR[1];
  int bb = yuvconstants->kUVBiasB[0];
  int bg = yuvconstants->kUVBiasG[0];
  int br = yuvconstants->kUVBiasR[0];
  int yg = yuvconstants->kYToRgb[0];
#endif

Frank Barchard's avatar
Frank Barchard committed
1428
  uint32_t y1 = (uint32_t)((y << 6) * yg) >> 16;
1429 1430 1431 1432 1433
  u = clamp255(u >> 2);
  v = clamp255(v >> 2);
  *b = (int)(-(u * ub) + y1 + bb);
  *g = (int)(-(u * ug + v * vg) + y1 + bg);
  *r = (int)(-(v * vr) + y1 + br);
1434
}
1435

1436 1437
// C reference code that mimics the YUV 10 bit assembly.
// Reads 10 bit YUV and clamps down to 8 bit RGB.
Frank Barchard's avatar
Frank Barchard committed
1438 1439 1440 1441 1442 1443
static __inline void YuvPixel10(uint16_t y,
                                uint16_t u,
                                uint16_t v,
                                uint8_t* b,
                                uint8_t* g,
                                uint8_t* r,
1444 1445 1446 1447 1448 1449 1450 1451
                                const struct YuvConstants* yuvconstants) {
  int b16;
  int g16;
  int r16;
  YuvPixel16(y, u, v, &b16, &g16, &r16, yuvconstants);
  *b = Clamp(b16 >> 6);
  *g = Clamp(g16 >> 6);
  *r = Clamp(r16 >> 6);
1452 1453
}

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

// C reference code that mimics the YUV assembly.
Frank Barchard's avatar
Frank Barchard committed
1459 1460 1461 1462 1463
static __inline void YPixel(uint8_t y, uint8_t* b, uint8_t* g, uint8_t* r) {
  uint32_t y1 = (uint32_t)(y * 0x0101 * YG) >> 16;
  *b = Clamp((int32_t)(y1 + YGB) >> 6);
  *g = Clamp((int32_t)(y1 + YGB) >> 6);
  *r = Clamp((int32_t)(y1 + YGB) >> 6);
1464 1465 1466 1467
}

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

1469
#if !defined(LIBYUV_DISABLE_NEON) && \
1470
    (defined(__ARM_NEON__) || defined(__aarch64__) || defined(LIBYUV_NEON))
1471 1472
// C mimic assembly.
// TODO(fbarchard): Remove subsampling from Neon.
Frank Barchard's avatar
Frank Barchard committed
1473 1474 1475 1476
void I444ToARGBRow_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* rgb_buf,
1477
                     const struct YuvConstants* yuvconstants,
1478
                     int width) {
1479 1480
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1481 1482
    uint8_t u = (src_u[0] + src_u[1] + 1) >> 1;
    uint8_t v = (src_v[0] + src_v[1] + 1) >> 1;
1483 1484
    YuvPixel(src_y[0], u, v, rgb_buf + 0, rgb_buf + 1, rgb_buf + 2,
             yuvconstants);
1485
    rgb_buf[3] = 255;
1486 1487
    YuvPixel(src_y[1], u, v, rgb_buf + 4, rgb_buf + 5, rgb_buf + 6,
             yuvconstants);
1488
    rgb_buf[7] = 255;
1489 1490 1491
    src_y += 2;
    src_u += 2;
    src_v += 2;
1492 1493 1494
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1495 1496
    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
1497
    rgb_buf[3] = 255;
1498 1499 1500
  }
}
#else
Frank Barchard's avatar
Frank Barchard committed
1501 1502 1503 1504
void I444ToARGBRow_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* rgb_buf,
1505
                     const struct YuvConstants* yuvconstants,
1506
                     int width) {
1507 1508
  int x;
  for (x = 0; x < width; ++x) {
Frank Barchard's avatar
Frank Barchard committed
1509 1510
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1511
    rgb_buf[3] = 255;
1512 1513 1514
    src_y += 1;
    src_u += 1;
    src_v += 1;
1515 1516 1517
    rgb_buf += 4;  // Advance 1 pixel.
  }
}
1518
#endif
1519

1520
// Also used for 420
Frank Barchard's avatar
Frank Barchard committed
1521 1522 1523 1524
void I422ToARGBRow_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* rgb_buf,
1525
                     const struct YuvConstants* yuvconstants,
1526
                     int width) {
1527 1528
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1529 1530
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1531
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1532 1533
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1534
    rgb_buf[7] = 255;
1535 1536 1537
    src_y += 2;
    src_u += 1;
    src_v += 1;
1538 1539 1540
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1541 1542
    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
1543 1544 1545 1546
    rgb_buf[3] = 255;
  }
}

1547
// 10 bit YUV to ARGB
Frank Barchard's avatar
Frank Barchard committed
1548 1549 1550 1551
void I210ToARGBRow_C(const uint16_t* src_y,
                     const uint16_t* src_u,
                     const uint16_t* src_v,
                     uint8_t* rgb_buf,
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
                     const struct YuvConstants* yuvconstants,
                     int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
               rgb_buf + 2, yuvconstants);
    rgb_buf[3] = 255;
    YuvPixel10(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
               rgb_buf + 6, yuvconstants);
    rgb_buf[7] = 255;
    src_y += 2;
    src_u += 1;
    src_v += 1;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
    YuvPixel10(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
               rgb_buf + 2, yuvconstants);
    rgb_buf[3] = 255;
  }
}

1574
static void StoreAR30(uint8_t* rgb_buf, int b, int g, int r) {
Frank Barchard's avatar
Frank Barchard committed
1575
  uint32_t ar30;
1576 1577 1578 1579 1580 1581
  b = b >> 4;  // convert 10.6 to 10 bit.
  g = g >> 4;
  r = r >> 4;
  b = Clamp10(b);
  g = Clamp10(g);
  r = Clamp10(r);
Frank Barchard's avatar
Frank Barchard committed
1582 1583
  ar30 = b | ((uint32_t)g << 10) | ((uint32_t)r << 20) | 0xc0000000;
  (*(uint32_t*)rgb_buf) = ar30;
1584 1585 1586
}

// 10 bit YUV to 10 bit AR30
Frank Barchard's avatar
Frank Barchard committed
1587 1588 1589 1590
void I210ToAR30Row_C(const uint16_t* src_y,
                     const uint16_t* src_u,
                     const uint16_t* src_v,
                     uint8_t* rgb_buf,
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
                     const struct YuvConstants* yuvconstants,
                     int width) {
  int x;
  int b;
  int g;
  int r;
  for (x = 0; x < width - 1; x += 2) {
    YuvPixel16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf, b, g, r);
    YuvPixel16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf + 4, b, g, r);
    src_y += 2;
    src_u += 1;
    src_v += 1;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
    YuvPixel16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf, b, g, r);
  }
}

1613 1614
// 8 bit YUV to 10 bit AR30
// Uses same code as 10 bit YUV bit shifts the 8 bit values up to 10 bits.
Frank Barchard's avatar
Frank Barchard committed
1615 1616 1617 1618
void I422ToAR30Row_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* rgb_buf,
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
                     const struct YuvConstants* yuvconstants,
                     int width) {
  int x;
  int b;
  int g;
  int r;
  for (x = 0; x < width - 1; x += 2) {
    YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf, b, g, r);
    YuvPixel8_16(src_y[1], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf + 4, b, g, r);
    src_y += 2;
    src_u += 1;
    src_v += 1;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
    YuvPixel8_16(src_y[0], src_u[0], src_v[0], &b, &g, &r, yuvconstants);
    StoreAR30(rgb_buf, b, g, r);
  }
}

Frank Barchard's avatar
Frank Barchard committed
1641 1642 1643 1644 1645
void I422AlphaToARGBRow_C(const uint8_t* src_y,
                          const uint8_t* src_u,
                          const uint8_t* src_v,
                          const uint8_t* src_a,
                          uint8_t* rgb_buf,
1646
                          const struct YuvConstants* yuvconstants,
1647 1648 1649
                          int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1650 1651
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1652
    rgb_buf[3] = src_a[0];
Frank Barchard's avatar
Frank Barchard committed
1653 1654
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1655 1656 1657 1658 1659 1660 1661 1662
    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
1663 1664
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1665 1666 1667 1668
    rgb_buf[3] = src_a[0];
  }
}

Frank Barchard's avatar
Frank Barchard committed
1669 1670 1671 1672
void I422ToRGB24Row_C(const uint8_t* src_y,
                      const uint8_t* src_u,
                      const uint8_t* src_v,
                      uint8_t* rgb_buf,
1673
                      const struct YuvConstants* yuvconstants,
1674
                      int width) {
1675 1676
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1677 1678 1679 1680
    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);
1681 1682 1683
    src_y += 2;
    src_u += 1;
    src_v += 1;
1684 1685 1686
    rgb_buf += 6;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1687 1688
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1689 1690 1691
  }
}

Frank Barchard's avatar
Frank Barchard committed
1692 1693 1694 1695
void I422ToARGB4444Row_C(const uint8_t* src_y,
                         const uint8_t* src_u,
                         const uint8_t* src_v,
                         uint8_t* dst_argb4444,
1696
                         const struct YuvConstants* yuvconstants,
1697
                         int width) {
Frank Barchard's avatar
Frank Barchard committed
1698 1699 1700 1701 1702 1703
  uint8_t b0;
  uint8_t g0;
  uint8_t r0;
  uint8_t b1;
  uint8_t g1;
  uint8_t r1;
1704 1705
  int x;
  for (x = 0; x < width - 1; x += 2) {
1706 1707
    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);
1708 1709 1710 1711 1712 1713
    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
1714
    *(uint32_t*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | (b1 << 16) |
1715
                                 (g1 << 20) | (r1 << 24) | 0xf000f000;
1716 1717 1718
    src_y += 2;
    src_u += 1;
    src_v += 1;
1719 1720 1721
    dst_argb4444 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1722
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1723 1724 1725
    b0 = b0 >> 4;
    g0 = g0 >> 4;
    r0 = r0 >> 4;
Frank Barchard's avatar
Frank Barchard committed
1726
    *(uint16_t*)(dst_argb4444) = b0 | (g0 << 4) | (r0 << 8) | 0xf000;
1727 1728 1729
  }
}

Frank Barchard's avatar
Frank Barchard committed
1730 1731 1732 1733
void I422ToARGB1555Row_C(const uint8_t* src_y,
                         const uint8_t* src_u,
                         const uint8_t* src_v,
                         uint8_t* dst_argb1555,
1734
                         const struct YuvConstants* yuvconstants,
1735
                         int width) {
Frank Barchard's avatar
Frank Barchard committed
1736 1737 1738 1739 1740 1741
  uint8_t b0;
  uint8_t g0;
  uint8_t r0;
  uint8_t b1;
  uint8_t g1;
  uint8_t r1;
1742 1743
  int x;
  for (x = 0; x < width - 1; x += 2) {
1744 1745
    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);
1746 1747 1748 1749 1750 1751
    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
1752
    *(uint32_t*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | (b1 << 16) |
1753
                                 (g1 << 21) | (r1 << 26) | 0x80008000;
1754 1755 1756
    src_y += 2;
    src_u += 1;
    src_v += 1;
1757 1758 1759
    dst_argb1555 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1760
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1761 1762 1763
    b0 = b0 >> 3;
    g0 = g0 >> 3;
    r0 = r0 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1764
    *(uint16_t*)(dst_argb1555) = b0 | (g0 << 5) | (r0 << 10) | 0x8000;
1765 1766 1767
  }
}

Frank Barchard's avatar
Frank Barchard committed
1768 1769 1770 1771
void I422ToRGB565Row_C(const uint8_t* src_y,
                       const uint8_t* src_u,
                       const uint8_t* src_v,
                       uint8_t* dst_rgb565,
1772
                       const struct YuvConstants* yuvconstants,
1773
                       int width) {
Frank Barchard's avatar
Frank Barchard committed
1774 1775 1776 1777 1778 1779
  uint8_t b0;
  uint8_t g0;
  uint8_t r0;
  uint8_t b1;
  uint8_t g1;
  uint8_t r1;
1780 1781
  int x;
  for (x = 0; x < width - 1; x += 2) {
1782 1783
    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);
1784 1785 1786 1787 1788 1789
    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
1790
    *(uint32_t*)(dst_rgb565) =
Frank Barchard's avatar
Frank Barchard committed
1791
        b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
1792 1793 1794
    src_y += 2;
    src_u += 1;
    src_v += 1;
1795 1796 1797
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1798
    YuvPixel(src_y[0], src_u[0], src_v[0], &b0, &g0, &r0, yuvconstants);
1799 1800 1801
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1802
    *(uint16_t*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1803 1804 1805
  }
}

Frank Barchard's avatar
Frank Barchard committed
1806 1807 1808
void NV12ToARGBRow_C(const uint8_t* src_y,
                     const uint8_t* src_uv,
                     uint8_t* rgb_buf,
1809
                     const struct YuvConstants* yuvconstants,
1810
                     int width) {
1811 1812
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1813 1814
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1815
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1816 1817
    YuvPixel(src_y[1], src_uv[0], src_uv[1], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1818
    rgb_buf[7] = 255;
1819
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1820
    src_uv += 2;
1821 1822 1823
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1824 1825
    YuvPixel(src_y[0], src_uv[0], src_uv[1], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1826
    rgb_buf[3] = 255;
1827 1828 1829
  }
}

Frank Barchard's avatar
Frank Barchard committed
1830 1831 1832
void NV21ToARGBRow_C(const uint8_t* src_y,
                     const uint8_t* src_vu,
                     uint8_t* rgb_buf,
1833
                     const struct YuvConstants* yuvconstants,
1834 1835 1836
                     int width) {
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1837 1838
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1839
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1840 1841
    YuvPixel(src_y[1], src_vu[1], src_vu[0], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1842 1843 1844 1845 1846 1847
    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
1848 1849
    YuvPixel(src_y[0], src_vu[1], src_vu[0], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1850 1851 1852 1853
    rgb_buf[3] = 255;
  }
}

Frank Barchard's avatar
Frank Barchard committed
1854 1855 1856
void NV12ToRGB565Row_C(const uint8_t* src_y,
                       const uint8_t* src_uv,
                       uint8_t* dst_rgb565,
1857
                       const struct YuvConstants* yuvconstants,
1858
                       int width) {
Frank Barchard's avatar
Frank Barchard committed
1859 1860 1861 1862 1863 1864
  uint8_t b0;
  uint8_t g0;
  uint8_t r0;
  uint8_t b1;
  uint8_t g1;
  uint8_t r1;
1865 1866
  int x;
  for (x = 0; x < width - 1; x += 2) {
1867 1868
    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);
1869 1870 1871 1872 1873 1874
    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
1875
    *(uint32_t*)(dst_rgb565) =
Frank Barchard's avatar
Frank Barchard committed
1876
        b0 | (g0 << 5) | (r0 << 11) | (b1 << 16) | (g1 << 21) | (r1 << 27);
1877
    src_y += 2;
Frank Barchard's avatar
Frank Barchard committed
1878
    src_uv += 2;
1879 1880 1881
    dst_rgb565 += 4;  // Advance 2 pixels.
  }
  if (width & 1) {
1882
    YuvPixel(src_y[0], src_uv[0], src_uv[1], &b0, &g0, &r0, yuvconstants);
1883 1884 1885
    b0 = b0 >> 3;
    g0 = g0 >> 2;
    r0 = r0 >> 3;
Frank Barchard's avatar
Frank Barchard committed
1886
    *(uint16_t*)(dst_rgb565) = b0 | (g0 << 5) | (r0 << 11);
1887 1888 1889
  }
}

Frank Barchard's avatar
Frank Barchard committed
1890 1891
void YUY2ToARGBRow_C(const uint8_t* src_yuy2,
                     uint8_t* rgb_buf,
1892
                     const struct YuvConstants* yuvconstants,
1893
                     int width) {
1894 1895
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1896 1897
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1898
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1899 1900
    YuvPixel(src_yuy2[2], src_yuy2[1], src_yuy2[3], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1901
    rgb_buf[7] = 255;
1902
    src_yuy2 += 4;
1903 1904 1905
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1906 1907
    YuvPixel(src_yuy2[0], src_yuy2[1], src_yuy2[3], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1908
    rgb_buf[3] = 255;
1909 1910 1911
  }
}

Frank Barchard's avatar
Frank Barchard committed
1912 1913
void UYVYToARGBRow_C(const uint8_t* src_uyvy,
                     uint8_t* rgb_buf,
1914
                     const struct YuvConstants* yuvconstants,
1915
                     int width) {
1916 1917
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1918 1919
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1920
    rgb_buf[3] = 255;
Frank Barchard's avatar
Frank Barchard committed
1921 1922
    YuvPixel(src_uyvy[3], src_uyvy[0], src_uyvy[2], rgb_buf + 4, rgb_buf + 5,
             rgb_buf + 6, yuvconstants);
1923
    rgb_buf[7] = 255;
1924
    src_uyvy += 4;
1925 1926 1927
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1928 1929
    YuvPixel(src_uyvy[1], src_uyvy[0], src_uyvy[2], rgb_buf + 0, rgb_buf + 1,
             rgb_buf + 2, yuvconstants);
1930
    rgb_buf[3] = 255;
1931 1932 1933
  }
}

Frank Barchard's avatar
Frank Barchard committed
1934 1935 1936 1937
void I422ToRGBARow_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* rgb_buf,
1938
                     const struct YuvConstants* yuvconstants,
1939
                     int width) {
1940 1941
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
1942 1943
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
             rgb_buf + 3, yuvconstants);
1944
    rgb_buf[0] = 255;
Frank Barchard's avatar
Frank Barchard committed
1945 1946
    YuvPixel(src_y[1], src_u[0], src_v[0], rgb_buf + 5, rgb_buf + 6,
             rgb_buf + 7, yuvconstants);
1947
    rgb_buf[4] = 255;
1948 1949 1950
    src_y += 2;
    src_u += 1;
    src_v += 1;
1951 1952 1953
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
1954 1955
    YuvPixel(src_y[0], src_u[0], src_v[0], rgb_buf + 1, rgb_buf + 2,
             rgb_buf + 3, yuvconstants);
1956
    rgb_buf[0] = 255;
1957 1958 1959
  }
}

Frank Barchard's avatar
Frank Barchard committed
1960
void I400ToARGBRow_C(const uint8_t* src_y, uint8_t* rgb_buf, int width) {
1961 1962
  int x;
  for (x = 0; x < width - 1; x += 2) {
1963
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1964
    rgb_buf[3] = 255;
1965
    YPixel(src_y[1], rgb_buf + 4, rgb_buf + 5, rgb_buf + 6);
1966 1967 1968 1969 1970
    rgb_buf[7] = 255;
    src_y += 2;
    rgb_buf += 8;  // Advance 2 pixels.
  }
  if (width & 1) {
1971
    YPixel(src_y[0], rgb_buf + 0, rgb_buf + 1, rgb_buf + 2);
1972
    rgb_buf[3] = 255;
1973 1974 1975
  }
}

Frank Barchard's avatar
Frank Barchard committed
1976
void MirrorRow_C(const uint8_t* src, uint8_t* dst, int width) {
1977
  int x;
1978
  src += width - 1;
1979
  for (x = 0; x < width - 1; x += 2) {
1980 1981 1982 1983 1984 1985
    dst[x] = src[0];
    dst[x + 1] = src[-1];
    src -= 2;
  }
  if (width & 1) {
    dst[width - 1] = src[0];
1986 1987 1988
  }
}

1989 1990 1991 1992
void MirrorUVRow_C(const uint8_t* src_uv,
                   uint8_t* dst_u,
                   uint8_t* dst_v,
                   int width) {
1993
  int x;
1994
  src_uv += (width - 1) << 1;
1995
  for (x = 0; x < width - 1; x += 2) {
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
    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];
  }
}

Frank Barchard's avatar
Frank Barchard committed
2008
void ARGBMirrorRow_C(const uint8_t* src, uint8_t* dst, int width) {
2009
  int x;
Frank Barchard's avatar
Frank Barchard committed
2010 2011
  const uint32_t* src32 = (const uint32_t*)(src);
  uint32_t* dst32 = (uint32_t*)(dst);
2012
  src32 += width - 1;
2013
  for (x = 0; x < width - 1; x += 2) {
2014 2015 2016 2017 2018 2019 2020 2021 2022
    dst32[x] = src32[0];
    dst32[x + 1] = src32[-1];
    src32 -= 2;
  }
  if (width & 1) {
    dst32[width - 1] = src32[0];
  }
}

2023 2024 2025 2026
void SplitUVRow_C(const uint8_t* src_uv,
                  uint8_t* dst_u,
                  uint8_t* dst_v,
                  int width) {
2027 2028
  int x;
  for (x = 0; x < width - 1; x += 2) {
2029 2030 2031 2032 2033 2034 2035 2036 2037
    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];
2038 2039 2040
  }
}

Frank Barchard's avatar
Frank Barchard committed
2041 2042 2043
void MergeUVRow_C(const uint8_t* src_u,
                  const uint8_t* src_v,
                  uint8_t* dst_uv,
2044
                  int width) {
2045 2046
  int x;
  for (x = 0; x < width - 1; x += 2) {
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
    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];
  }
}

Frank Barchard's avatar
Frank Barchard committed
2059 2060 2061 2062
void SplitRGBRow_C(const uint8_t* src_rgb,
                   uint8_t* dst_r,
                   uint8_t* dst_g,
                   uint8_t* dst_b,
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
                   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;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2073 2074 2075 2076
void MergeRGBRow_C(const uint8_t* src_r,
                   const uint8_t* src_g,
                   const uint8_t* src_b,
                   uint8_t* dst_rgb,
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
                   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;
  }
}

2087 2088 2089 2090 2091
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 128 = 9 bits
// 64 = 10 bits
// 16 = 12 bits
// 1 = 16 bits
Frank Barchard's avatar
Frank Barchard committed
2092 2093 2094
void MergeUVRow_16_C(const uint16_t* src_u,
                     const uint16_t* src_v,
                     uint16_t* dst_uv,
2095 2096
                     int scale,
                     int width) {
2097 2098
  int x;
  for (x = 0; x < width - 1; x += 2) {
2099 2100 2101 2102
    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;
2103 2104 2105
    dst_uv += 4;
  }
  if (width & 1) {
2106 2107
    dst_uv[0] = src_u[width - 1] * scale;
    dst_uv[1] = src_v[width - 1] * scale;
2108 2109 2110
  }
}

Frank Barchard's avatar
Frank Barchard committed
2111 2112
void MultiplyRow_16_C(const uint16_t* src_y,
                      uint16_t* dst_y,
2113 2114 2115 2116 2117 2118 2119 2120
                      int scale,
                      int width) {
  int x;
  for (x = 0; x < width; ++x) {
    dst_y[x] = src_y[x] * scale;
  }
}

2121 2122 2123 2124 2125
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 32768 = 9 bits
// 16384 = 10 bits
// 4096 = 12 bits
// 256 = 16 bits
Frank Barchard's avatar
Frank Barchard committed
2126 2127
void Convert16To8Row_C(const uint16_t* src_y,
                       uint8_t* dst_y,
2128 2129 2130 2131
                       int scale,
                       int width) {
  int x;
  for (x = 0; x < width; ++x) {
2132
    dst_y[x] = clamp255((src_y[x] * scale) >> 16);
2133 2134 2135
  }
}

2136 2137
// Use scale to convert lsb formats to msb, depending how many bits there are:
// 1024 = 10 bits
Frank Barchard's avatar
Frank Barchard committed
2138 2139
void Convert8To16Row_C(const uint8_t* src_y,
                       uint16_t* dst_y,
2140 2141 2142 2143 2144 2145 2146 2147 2148
                       int scale,
                       int width) {
  int x;
  scale *= 0x0101;  // replicates the byte.
  for (x = 0; x < width; ++x) {
    dst_y[x] = (src_y[x] * scale) >> 16;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2149
void CopyRow_C(const uint8_t* src, uint8_t* dst, int count) {
2150 2151 2152
  memcpy(dst, src, count);
}

Frank Barchard's avatar
Frank Barchard committed
2153
void CopyRow_16_C(const uint16_t* src, uint16_t* dst, int count) {
2154 2155 2156
  memcpy(dst, src, count * 2);
}

Frank Barchard's avatar
Frank Barchard committed
2157
void SetRow_C(uint8_t* dst, uint8_t v8, int width) {
2158
  memset(dst, v8, width);
2159 2160
}

Frank Barchard's avatar
Frank Barchard committed
2161 2162
void ARGBSetRow_C(uint8_t* dst_argb, uint32_t v32, int width) {
  uint32_t* d = (uint32_t*)(dst_argb);
2163 2164 2165
  int x;
  for (x = 0; x < width; ++x) {
    d[x] = v32;
2166 2167 2168
  }
}

2169
// Filter 2 rows of YUY2 UV's (422) into U and V (420).
Frank Barchard's avatar
Frank Barchard committed
2170
void YUY2ToUVRow_C(const uint8_t* src_yuy2,
Frank Barchard's avatar
Frank Barchard committed
2171
                   int src_stride_yuy2,
Frank Barchard's avatar
Frank Barchard committed
2172 2173
                   uint8_t* dst_u,
                   uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
2174
                   int width) {
2175
  // Output a row of UV values, filtering 2 rows of YUY2.
2176 2177
  int x;
  for (x = 0; x < width; x += 2) {
2178 2179 2180 2181 2182 2183 2184 2185
    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;
  }
}

2186
// Copy row of YUY2 UV's (422) into U and V (422).
Frank Barchard's avatar
Frank Barchard committed
2187 2188 2189
void YUY2ToUV422Row_C(const uint8_t* src_yuy2,
                      uint8_t* dst_u,
                      uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
2190
                      int width) {
2191
  // Output a row of UV values.
2192 2193
  int x;
  for (x = 0; x < width; x += 2) {
2194 2195 2196 2197 2198 2199 2200 2201 2202
    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).
Frank Barchard's avatar
Frank Barchard committed
2203
void YUY2ToYRow_C(const uint8_t* src_yuy2, uint8_t* dst_y, int width) {
2204
  // Output a row of Y values.
2205 2206
  int x;
  for (x = 0; x < width - 1; x += 2) {
2207 2208 2209 2210 2211 2212
    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];
2213 2214 2215
  }
}

2216
// Filter 2 rows of UYVY UV's (422) into U and V (420).
Frank Barchard's avatar
Frank Barchard committed
2217
void UYVYToUVRow_C(const uint8_t* src_uyvy,
Frank Barchard's avatar
Frank Barchard committed
2218
                   int src_stride_uyvy,
Frank Barchard's avatar
Frank Barchard committed
2219 2220
                   uint8_t* dst_u,
                   uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
2221
                   int width) {
2222
  // Output a row of UV values.
2223 2224
  int x;
  for (x = 0; x < width; x += 2) {
2225 2226 2227 2228 2229 2230 2231 2232
    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;
  }
}

2233
// Copy row of UYVY UV's (422) into U and V (422).
Frank Barchard's avatar
Frank Barchard committed
2234 2235 2236
void UYVYToUV422Row_C(const uint8_t* src_uyvy,
                      uint8_t* dst_u,
                      uint8_t* dst_v,
Frank Barchard's avatar
Frank Barchard committed
2237
                      int width) {
2238
  // Output a row of UV values.
2239 2240
  int x;
  for (x = 0; x < width; x += 2) {
2241 2242 2243 2244 2245 2246 2247 2248 2249
    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).
Frank Barchard's avatar
Frank Barchard committed
2250
void UYVYToYRow_C(const uint8_t* src_uyvy, uint8_t* dst_y, int width) {
2251
  // Output a row of Y values.
2252 2253
  int x;
  for (x = 0; x < width - 1; x += 2) {
2254 2255 2256
    dst_y[x] = src_uyvy[1];
    dst_y[x + 1] = src_uyvy[3];
    src_uyvy += 4;
2257 2258
  }
  if (width & 1) {
2259
    dst_y[width - 1] = src_uyvy[1];
2260 2261 2262
  }
}

2263
#define BLEND(f, b, a) (((256 - a) * b) >> 8) + f
2264

2265 2266
// Blend src_argb0 over src_argb1 and store to dst_argb.
// dst_argb may be src_argb0 or src_argb1.
2267
// This code mimics the SSSE3 version for better testability.
Frank Barchard's avatar
Frank Barchard committed
2268 2269 2270
void ARGBBlendRow_C(const uint8_t* src_argb0,
                    const uint8_t* src_argb1,
                    uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
2271
                    int width) {
2272 2273
  int x;
  for (x = 0; x < width - 1; x += 2) {
Frank Barchard's avatar
Frank Barchard committed
2274 2275 2276 2277 2278 2279 2280
    uint32_t fb = src_argb0[0];
    uint32_t fg = src_argb0[1];
    uint32_t fr = src_argb0[2];
    uint32_t a = src_argb0[3];
    uint32_t bb = src_argb1[0];
    uint32_t bg = src_argb1[1];
    uint32_t br = src_argb1[2];
2281 2282 2283 2284 2285 2286 2287 2288
    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];
2289
    a = src_argb0[4 + 3];
2290 2291 2292 2293 2294 2295 2296
    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;
2297 2298 2299 2300 2301 2302
    src_argb0 += 8;
    src_argb1 += 8;
    dst_argb += 8;
  }

  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
2303 2304 2305 2306 2307 2308 2309
    uint32_t fb = src_argb0[0];
    uint32_t fg = src_argb0[1];
    uint32_t fr = src_argb0[2];
    uint32_t a = src_argb0[3];
    uint32_t bb = src_argb1[0];
    uint32_t bg = src_argb1[1];
    uint32_t br = src_argb1[2];
2310 2311 2312 2313
    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;
2314 2315
  }
}
2316
#undef BLEND
2317

Frank Barchard's avatar
Frank Barchard committed
2318
#define UBLEND(f, b, a) (((a)*f) + ((255 - a) * b) + 255) >> 8
Frank Barchard's avatar
Frank Barchard committed
2319 2320 2321 2322
void BlendPlaneRow_C(const uint8_t* src0,
                     const uint8_t* src1,
                     const uint8_t* alpha,
                     uint8_t* dst,
Frank Barchard's avatar
Frank Barchard committed
2323
                     int width) {
2324
  int x;
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
  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]);
2335 2336
  }
}
2337
#undef UBLEND
2338

2339
#define ATTENUATE(f, a) (a | (a << 8)) * (f | (f << 8)) >> 24
2340

2341
// Multiply source RGB by alpha and store to destination.
2342
// This code mimics the SSSE3 version for better testability.
Frank Barchard's avatar
Frank Barchard committed
2343
void ARGBAttenuateRow_C(const uint8_t* src_argb, uint8_t* dst_argb, int width) {
2344 2345
  int i;
  for (i = 0; i < width - 1; i += 2) {
Frank Barchard's avatar
Frank Barchard committed
2346 2347 2348 2349
    uint32_t b = src_argb[0];
    uint32_t g = src_argb[1];
    uint32_t r = src_argb[2];
    uint32_t a = src_argb[3];
2350 2351 2352
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2353 2354 2355 2356 2357
    dst_argb[3] = a;
    b = src_argb[4];
    g = src_argb[5];
    r = src_argb[6];
    a = src_argb[7];
2358 2359 2360
    dst_argb[4] = ATTENUATE(b, a);
    dst_argb[5] = ATTENUATE(g, a);
    dst_argb[6] = ATTENUATE(r, a);
2361 2362 2363 2364 2365 2366
    dst_argb[7] = a;
    src_argb += 8;
    dst_argb += 8;
  }

  if (width & 1) {
Frank Barchard's avatar
Frank Barchard committed
2367 2368 2369 2370
    const uint32_t b = src_argb[0];
    const uint32_t g = src_argb[1];
    const uint32_t r = src_argb[2];
    const uint32_t a = src_argb[3];
2371 2372 2373
    dst_argb[0] = ATTENUATE(b, a);
    dst_argb[1] = ATTENUATE(g, a);
    dst_argb[2] = ATTENUATE(r, a);
2374 2375 2376
    dst_argb[3] = a;
  }
}
2377
#undef ATTENUATE
2378

2379 2380 2381 2382 2383
// 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
2384 2385
// 8.8 fixed point inverse table with 1.0 in upper short and 1 / a in lower.
#define T(a) 0x01000000 + (0x10000 / a)
Frank Barchard's avatar
Frank Barchard committed
2386
const uint32_t fixed_invtbl8[256] = {
Frank Barchard's avatar
Frank Barchard committed
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
    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};
2424 2425
#undef T

2426 2427 2428
void ARGBUnattenuateRow_C(const uint8_t* src_argb,
                          uint8_t* dst_argb,
                          int width) {
2429 2430
  int i;
  for (i = 0; i < width; ++i) {
Frank Barchard's avatar
Frank Barchard committed
2431 2432 2433 2434 2435
    uint32_t b = src_argb[0];
    uint32_t g = src_argb[1];
    uint32_t r = src_argb[2];
    const uint32_t a = src_argb[3];
    const uint32_t ia = fixed_invtbl8[a] & 0xffff;  // 8.8 fixed point
2436 2437 2438 2439
    b = (b * ia) >> 8;
    g = (g * ia) >> 8;
    r = (r * ia) >> 8;
    // Clamping should not be necessary but is free in assembly.
2440 2441 2442
    dst_argb[0] = clamp255(b);
    dst_argb[1] = clamp255(g);
    dst_argb[2] = clamp255(r);
2443 2444 2445 2446 2447 2448
    dst_argb[3] = a;
    src_argb += 4;
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2449 2450 2451
void ComputeCumulativeSumRow_C(const uint8_t* row,
                               int32_t* cumsum,
                               const int32_t* previous_cumsum,
Frank Barchard's avatar
Frank Barchard committed
2452
                               int width) {
Frank Barchard's avatar
Frank Barchard committed
2453
  int32_t row_sum[4] = {0, 0, 0, 0};
2454 2455
  int x;
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2456 2457 2458 2459
    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
2460 2461 2462 2463
    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
2464 2465 2466
  }
}

Frank Barchard's avatar
Frank Barchard committed
2467 2468
void CumulativeSumToAverageRow_C(const int32_t* tl,
                                 const int32_t* bl,
Frank Barchard's avatar
Frank Barchard committed
2469 2470
                                 int w,
                                 int area,
Frank Barchard's avatar
Frank Barchard committed
2471
                                 uint8_t* dst,
Frank Barchard's avatar
Frank Barchard committed
2472
                                 int count) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2473
  float ooa = 1.0f / area;
2474 2475
  int i;
  for (i = 0; i < count; ++i) {
Frank Barchard's avatar
Frank Barchard committed
2476 2477 2478 2479
    dst[0] = (uint8_t)((bl[w + 0] + tl[0] - bl[0] - tl[w + 0]) * ooa);
    dst[1] = (uint8_t)((bl[w + 1] + tl[1] - bl[1] - tl[w + 1]) * ooa);
    dst[2] = (uint8_t)((bl[w + 2] + tl[2] - bl[2] - tl[w + 2]) * ooa);
    dst[3] = (uint8_t)((bl[w + 3] + tl[3] - bl[3] - tl[w + 3]) * ooa);
fbarchard@google.com's avatar
fbarchard@google.com committed
2480 2481 2482 2483 2484 2485
    dst += 4;
    tl += 4;
    bl += 4;
  }
}

2486
// Copy pixels from rotated source to destination row with a slope.
2487
LIBYUV_API
Frank Barchard's avatar
Frank Barchard committed
2488
void ARGBAffineRow_C(const uint8_t* src_argb,
Frank Barchard's avatar
Frank Barchard committed
2489
                     int src_argb_stride,
Frank Barchard's avatar
Frank Barchard committed
2490
                     uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
2491 2492
                     const float* uv_dudv,
                     int width) {
2493
  int i;
2494 2495 2496 2497
  // Render a row of pixels from source into a buffer.
  float uv[2];
  uv[0] = uv_dudv[0];
  uv[1] = uv_dudv[1];
2498
  for (i = 0; i < width; ++i) {
2499 2500
    int x = (int)(uv[0]);
    int y = (int)(uv[1]);
Frank Barchard's avatar
Frank Barchard committed
2501 2502
    *(uint32_t*)(dst_argb) =
        *(const uint32_t*)(src_argb + y * src_argb_stride + x * 4);
2503 2504 2505 2506 2507 2508
    dst_argb += 4;
    uv[0] += uv_dudv[2];
    uv[1] += uv_dudv[3];
  }
}

2509
// Blend 2 rows into 1.
Frank Barchard's avatar
Frank Barchard committed
2510
static void HalfRow_C(const uint8_t* src_uv,
Frank Barchard's avatar
Frank Barchard committed
2511
                      ptrdiff_t src_uv_stride,
Frank Barchard's avatar
Frank Barchard committed
2512
                      uint8_t* dst_uv,
Frank Barchard's avatar
Frank Barchard committed
2513
                      int width) {
2514
  int x;
2515
  for (x = 0; x < width; ++x) {
2516 2517 2518 2519
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2520
static void HalfRow_16_C(const uint16_t* src_uv,
Frank Barchard's avatar
Frank Barchard committed
2521
                         ptrdiff_t src_uv_stride,
Frank Barchard's avatar
Frank Barchard committed
2522
                         uint16_t* dst_uv,
Frank Barchard's avatar
Frank Barchard committed
2523
                         int width) {
2524
  int x;
2525
  for (x = 0; x < width; ++x) {
2526 2527 2528 2529
    dst_uv[x] = (src_uv[x] + src_uv[src_uv_stride + x] + 1) >> 1;
  }
}

2530
// C version 2x2 -> 2x1.
Frank Barchard's avatar
Frank Barchard committed
2531 2532
void InterpolateRow_C(uint8_t* dst_ptr,
                      const uint8_t* src_ptr,
2533
                      ptrdiff_t src_stride,
Frank Barchard's avatar
Frank Barchard committed
2534 2535
                      int width,
                      int source_y_fraction) {
2536
  int y1_fraction = source_y_fraction;
2537
  int y0_fraction = 256 - y1_fraction;
Frank Barchard's avatar
Frank Barchard committed
2538
  const uint8_t* src_ptr1 = src_ptr + src_stride;
2539
  int x;
2540
  if (y1_fraction == 0) {
2541 2542 2543
    memcpy(dst_ptr, src_ptr, width);
    return;
  }
2544
  if (y1_fraction == 128) {
2545
    HalfRow_C(src_ptr, src_stride, dst_ptr, width);
2546 2547
    return;
  }
2548
  for (x = 0; x < width - 1; x += 2) {
2549 2550 2551 2552
    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;
2553 2554 2555
    src_ptr += 2;
    src_ptr1 += 2;
    dst_ptr += 2;
2556 2557
  }
  if (width & 1) {
2558 2559
    dst_ptr[0] =
        (src_ptr[0] * y0_fraction + src_ptr1[0] * y1_fraction + 128) >> 8;
2560 2561 2562
  }
}

Frank Barchard's avatar
Frank Barchard committed
2563 2564
void InterpolateRow_16_C(uint16_t* dst_ptr,
                         const uint16_t* src_ptr,
2565
                         ptrdiff_t src_stride,
Frank Barchard's avatar
Frank Barchard committed
2566 2567
                         int width,
                         int source_y_fraction) {
2568 2569
  int y1_fraction = source_y_fraction;
  int y0_fraction = 256 - y1_fraction;
Frank Barchard's avatar
Frank Barchard committed
2570
  const uint16_t* src_ptr1 = src_ptr + src_stride;
2571 2572 2573 2574 2575 2576
  int x;
  if (source_y_fraction == 0) {
    memcpy(dst_ptr, src_ptr, width * 2);
    return;
  }
  if (source_y_fraction == 128) {
2577
    HalfRow_16_C(src_ptr, src_stride, dst_ptr, width);
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
    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;
2589
  }
2590 2591
}

fbarchard@google.com's avatar
fbarchard@google.com committed
2592
// Use first 4 shuffler values to reorder ARGB channels.
Frank Barchard's avatar
Frank Barchard committed
2593 2594 2595
void ARGBShuffleRow_C(const uint8_t* src_argb,
                      uint8_t* dst_argb,
                      const uint8_t* shuffler,
Frank Barchard's avatar
Frank Barchard committed
2596
                      int width) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2597 2598 2599 2600 2601
  int index0 = shuffler[0];
  int index1 = shuffler[1];
  int index2 = shuffler[2];
  int index3 = shuffler[3];
  // Shuffle a row of ARGB.
2602
  int x;
2603
  for (x = 0; x < width; ++x) {
fbarchard@google.com's avatar
fbarchard@google.com committed
2604
    // To support in-place conversion.
Frank Barchard's avatar
Frank Barchard committed
2605 2606 2607 2608
    uint8_t b = src_argb[index0];
    uint8_t g = src_argb[index1];
    uint8_t r = src_argb[index2];
    uint8_t a = src_argb[index3];
fbarchard@google.com's avatar
fbarchard@google.com committed
2609 2610 2611 2612 2613 2614 2615 2616 2617
    dst_argb[0] = b;
    dst_argb[1] = g;
    dst_argb[2] = r;
    dst_argb[3] = a;
    src_argb += 4;
    dst_argb += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
2618 2619 2620 2621
void I422ToYUY2Row_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* dst_frame,
Frank Barchard's avatar
Frank Barchard committed
2622
                     int width) {
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
  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
2637
    dst_frame[2] = 0;
2638 2639
    dst_frame[3] = src_v[0];
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2640 2641
}

Frank Barchard's avatar
Frank Barchard committed
2642 2643 2644 2645
void I422ToUYVYRow_C(const uint8_t* src_y,
                     const uint8_t* src_u,
                     const uint8_t* src_v,
                     uint8_t* dst_frame,
Frank Barchard's avatar
Frank Barchard committed
2646
                     int width) {
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
  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
2662
    dst_frame[3] = 0;
2663
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2664
}
2665

Frank Barchard's avatar
Frank Barchard committed
2666 2667
void ARGBPolynomialRow_C(const uint8_t* src_argb,
                         uint8_t* dst_argb,
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
                         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;

Frank Barchard's avatar
Frank Barchard committed
2697 2698 2699 2700
    dst_argb[0] = Clamp((int32_t)(db));
    dst_argb[1] = Clamp((int32_t)(dg));
    dst_argb[2] = Clamp((int32_t)(dr));
    dst_argb[3] = Clamp((int32_t)(da));
2701 2702 2703 2704 2705
    src_argb += 4;
    dst_argb += 4;
  }
}

2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
// 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.

2716 2717 2718 2719
void HalfFloatRow_C(const uint16_t* src,
                    uint16_t* dst,
                    float scale,
                    int width) {
2720 2721 2722 2723
  int i;
  float mult = 1.9259299444e-34f * scale;
  for (i = 0; i < width; ++i) {
    float value = src[i] * mult;
Frank Barchard's avatar
Frank Barchard committed
2724
    dst[i] = (uint16_t)((*(uint32_t*)&value) >> 13);
2725 2726 2727
  }
}

Frank Barchard's avatar
Frank Barchard committed
2728 2729
void ARGBLumaColorTableRow_C(const uint8_t* src_argb,
                             uint8_t* dst_argb,
Frank Barchard's avatar
Frank Barchard committed
2730
                             int width,
Frank Barchard's avatar
Frank Barchard committed
2731 2732 2733 2734 2735
                             const uint8_t* luma,
                             uint32_t lumacoeff) {
  uint32_t bc = lumacoeff & 0xff;
  uint32_t gc = (lumacoeff >> 8) & 0xff;
  uint32_t rc = (lumacoeff >> 16) & 0xff;
2736 2737 2738 2739

  int i;
  for (i = 0; i < width - 1; i += 2) {
    // Luminance in rows, color values in columns.
Frank Barchard's avatar
Frank Barchard committed
2740
    const uint8_t* luma0 =
Frank Barchard's avatar
Frank Barchard committed
2741 2742
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
        luma;
Frank Barchard's avatar
Frank Barchard committed
2743
    const uint8_t* luma1;
2744 2745 2746 2747
    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
2748 2749 2750
    luma1 =
        ((src_argb[4] * bc + src_argb[5] * gc + src_argb[6] * rc) & 0x7F00u) +
        luma;
2751 2752 2753 2754 2755 2756 2757 2758 2759
    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
2760
    const uint8_t* luma0 =
Frank Barchard's avatar
Frank Barchard committed
2761 2762
        ((src_argb[0] * bc + src_argb[1] * gc + src_argb[2] * rc) & 0x7F00u) +
        luma;
2763 2764 2765 2766 2767 2768 2769
    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
2770
void ARGBCopyAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) {
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
  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];
  }
}

Frank Barchard's avatar
Frank Barchard committed
2783
void ARGBExtractAlphaRow_C(const uint8_t* src_argb, uint8_t* dst_a, int width) {
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
  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];
  }
}

Frank Barchard's avatar
Frank Barchard committed
2796
void ARGBCopyYToAlphaRow_C(const uint8_t* src, uint8_t* dst, int width) {
2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
  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];
  }
}
2808

2809
// Maximum temporary width for wrappers to process at a time, in pixels.
2810
#define MAXTWIDTH 2048
2811

2812 2813
#if !(defined(_MSC_VER) && defined(_M_IX86)) && \
    defined(HAS_I422TORGB565ROW_SSSE3)
2814
// row_win.cc has asm version, but GCC uses 2 step wrapper.
Frank Barchard's avatar
Frank Barchard committed
2815 2816 2817 2818
void I422ToRGB565Row_SSSE3(const uint8_t* src_y,
                           const uint8_t* src_u,
                           const uint8_t* src_v,
                           uint8_t* dst_rgb565,
2819
                           const struct YuvConstants* yuvconstants,
2820
                           int width) {
Frank Barchard's avatar
Frank Barchard committed
2821
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2822 2823
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2824
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2825
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
2826 2827 2828
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2829
    dst_rgb565 += twidth * 2;
2830 2831
    width -= twidth;
  }
2832
}
2833
#endif
2834

2835
#if defined(HAS_I422TOARGB1555ROW_SSSE3)
Frank Barchard's avatar
Frank Barchard committed
2836 2837 2838 2839
void I422ToARGB1555Row_SSSE3(const uint8_t* src_y,
                             const uint8_t* src_u,
                             const uint8_t* src_v,
                             uint8_t* dst_argb1555,
2840
                             const struct YuvConstants* yuvconstants,
2841
                             int width) {
2842
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2843
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2844 2845
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2846
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2847
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
2848 2849 2850
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2851
    dst_argb1555 += twidth * 2;
2852 2853
    width -= twidth;
  }
2854
}
2855
#endif
2856

2857
#if defined(HAS_I422TOARGB4444ROW_SSSE3)
Frank Barchard's avatar
Frank Barchard committed
2858 2859 2860 2861
void I422ToARGB4444Row_SSSE3(const uint8_t* src_y,
                             const uint8_t* src_u,
                             const uint8_t* src_v,
                             uint8_t* dst_argb4444,
2862
                             const struct YuvConstants* yuvconstants,
2863
                             int width) {
2864
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2865
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2866 2867
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2868
    I422ToARGBRow_SSSE3(src_y, src_u, src_v, row, yuvconstants, twidth);
2869
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
2870 2871 2872
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
2873
    dst_argb4444 += twidth * 2;
2874 2875 2876
    width -= twidth;
  }
}
2877
#endif
2878

2879
#if defined(HAS_NV12TORGB565ROW_SSSE3)
Frank Barchard's avatar
Frank Barchard committed
2880 2881 2882
void NV12ToRGB565Row_SSSE3(const uint8_t* src_y,
                           const uint8_t* src_uv,
                           uint8_t* dst_rgb565,
2883
                           const struct YuvConstants* yuvconstants,
2884
                           int width) {
2885
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2886
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2887 2888
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2889
    NV12ToARGBRow_SSSE3(src_y, src_uv, row, yuvconstants, twidth);
2890 2891 2892 2893 2894 2895 2896
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
2897
#endif
2898

2899
#if defined(HAS_I422TORGB565ROW_AVX2)
Frank Barchard's avatar
Frank Barchard committed
2900 2901 2902 2903
void I422ToRGB565Row_AVX2(const uint8_t* src_y,
                          const uint8_t* src_u,
                          const uint8_t* src_v,
                          uint8_t* dst_rgb565,
2904
                          const struct YuvConstants* yuvconstants,
2905
                          int width) {
Frank Barchard's avatar
Frank Barchard committed
2906
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2907 2908
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2909
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2910
#if defined(HAS_ARGBTORGB565ROW_AVX2)
2911
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
2912 2913 2914
#else
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
#endif
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
#endif

#if defined(HAS_I422TOARGB1555ROW_AVX2)
Frank Barchard's avatar
Frank Barchard committed
2925 2926 2927 2928
void I422ToARGB1555Row_AVX2(const uint8_t* src_y,
                            const uint8_t* src_u,
                            const uint8_t* src_v,
                            uint8_t* dst_argb1555,
2929
                            const struct YuvConstants* yuvconstants,
2930 2931
                            int width) {
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2932
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2933 2934
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2935
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2936
#if defined(HAS_ARGBTOARGB1555ROW_AVX2)
2937
    ARGBToARGB1555Row_AVX2(row, dst_argb1555, twidth);
2938 2939 2940
#else
    ARGBToARGB1555Row_SSE2(row, dst_argb1555, twidth);
#endif
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_argb1555 += twidth * 2;
    width -= twidth;
  }
}
#endif

#if defined(HAS_I422TOARGB4444ROW_AVX2)
Frank Barchard's avatar
Frank Barchard committed
2951 2952 2953 2954
void I422ToARGB4444Row_AVX2(const uint8_t* src_y,
                            const uint8_t* src_u,
                            const uint8_t* src_v,
                            uint8_t* dst_argb4444,
2955
                            const struct YuvConstants* yuvconstants,
2956 2957
                            int width) {
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2958
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2959 2960
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2961
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2962
#if defined(HAS_ARGBTOARGB4444ROW_AVX2)
2963
    ARGBToARGB4444Row_AVX2(row, dst_argb4444, twidth);
2964 2965 2966
#else
    ARGBToARGB4444Row_SSE2(row, dst_argb4444, twidth);
#endif
2967 2968 2969 2970 2971 2972 2973 2974 2975
    src_y += twidth;
    src_u += twidth / 2;
    src_v += twidth / 2;
    dst_argb4444 += twidth * 2;
    width -= twidth;
  }
}
#endif

2976
#if defined(HAS_I422TORGB24ROW_AVX2)
Frank Barchard's avatar
Frank Barchard committed
2977 2978 2979 2980
void I422ToRGB24Row_AVX2(const uint8_t* src_y,
                         const uint8_t* src_u,
                         const uint8_t* src_v,
                         uint8_t* dst_rgb24,
Frank Barchard's avatar
Frank Barchard committed
2981 2982
                         const struct YuvConstants* yuvconstants,
                         int width) {
2983
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
2984
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
2985 2986
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
2987
    I422ToARGBRow_AVX2(src_y, src_u, src_v, row, yuvconstants, twidth);
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
    // 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

2999
#if defined(HAS_NV12TORGB565ROW_AVX2)
Frank Barchard's avatar
Frank Barchard committed
3000 3001 3002
void NV12ToRGB565Row_AVX2(const uint8_t* src_y,
                          const uint8_t* src_uv,
                          uint8_t* dst_rgb565,
3003
                          const struct YuvConstants* yuvconstants,
3004
                          int width) {
3005
  // Row buffer for intermediate ARGB pixels.
Frank Barchard's avatar
Frank Barchard committed
3006
  SIMD_ALIGNED(uint8_t row[MAXTWIDTH * 4]);
3007 3008
  while (width > 0) {
    int twidth = width > MAXTWIDTH ? MAXTWIDTH : width;
3009
    NV12ToARGBRow_AVX2(src_y, src_uv, row, yuvconstants, twidth);
3010
#if defined(HAS_ARGBTORGB565ROW_AVX2)
3011
    ARGBToRGB565Row_AVX2(row, dst_rgb565, twidth);
3012 3013 3014
#else
    ARGBToRGB565Row_SSE2(row, dst_rgb565, twidth);
#endif
3015 3016 3017 3018 3019 3020 3021 3022
    src_y += twidth;
    src_uv += twidth;
    dst_rgb565 += twidth * 2;
    width -= twidth;
  }
}
#endif

3023
float ScaleSumSamples_C(const float* src, float* dst, float scale, int width) {
3024
  float fsum = 0.f;
3025
  int i;
3026
#if defined(__clang__)
3027
#pragma clang loop vectorize_width(4)
3028
#endif
3029
  for (i = 0; i < width; ++i) {
3030
    float v = *src++;
3031
    fsum += v * v;
3032
    *dst++ = v * scale;
3033
  }
3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
  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;
  }
3046 3047 3048 3049 3050 3051
  return fmax;
}

void ScaleSamples_C(const float* src, float* dst, float scale, int width) {
  int i;
  for (i = 0; i < width; ++i) {
3052
    *dst++ = *src++ * scale;
3053 3054 3055
  }
}

Frank Barchard's avatar
Frank Barchard committed
3056
void GaussRow_C(const uint32_t* src, uint16_t* dst, int width) {
3057 3058 3059 3060 3061 3062 3063 3064
  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;
  }
}

3065
// filter 5 rows with 1, 4, 6, 4, 1 coefficients to produce 1 row.
Frank Barchard's avatar
Frank Barchard committed
3066 3067 3068 3069 3070 3071
void GaussCol_C(const uint16_t* src0,
                const uint16_t* src1,
                const uint16_t* src2,
                const uint16_t* src3,
                const uint16_t* src4,
                uint32_t* dst,
3072 3073 3074 3075 3076 3077 3078
                int width) {
  int i;
  for (i = 0; i < width; ++i) {
    *dst++ = *src0++ + *src1++ * 4 + *src2++ * 6 + *src3++ * 4 + *src4++;
  }
}

3079
#ifdef __cplusplus
3080
}  // extern "C"
3081 3082
}  // namespace libyuv
#endif