planar_functions.cc 68.4 KB
Newer Older
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
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
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
8 9 10
 *  be found in the AUTHORS file in the root of the source tree.
 */

11
#include "libyuv/planar_functions.h"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
12

13
#include <string.h>  // for memset()
14

15
#include "libyuv/cpu_id.h"
16 17 18
#ifdef HAVE_JPEG
#include "libyuv/mjpeg_decoder.h"
#endif
19
#include "libyuv/row.h"
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
20

21
#ifdef __cplusplus
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
22
namespace libyuv {
23 24
extern "C" {
#endif
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
25

26
// Copy a plane of data
27
LIBYUV_API
28 29 30
void CopyPlane(const uint8* src_y, int src_stride_y,
               uint8* dst_y, int dst_stride_y,
               int width, int height) {
31 32
  int y;
  void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
33
  // Coalesce rows.
34 35
  if (src_stride_y == width &&
      dst_stride_y == width) {
36 37 38
    width *= height;
    height = 1;
    src_stride_y = dst_stride_y = 0;
39
  }
40 41 42 43
  // Nothing to do.
  if (src_y == dst_y && src_stride_y == dst_stride_y) {
    return;
  }
44
#if defined(HAS_COPYROW_SSE2)
45 46
  if (TestCpuFlag(kCpuHasSSE2)) {
    CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
47
  }
48
#endif
49
#if defined(HAS_COPYROW_AVX)
50 51
  if (TestCpuFlag(kCpuHasAVX)) {
    CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
52 53
  }
#endif
54 55 56
#if defined(HAS_COPYROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_ERMS;
57 58 59
  }
#endif
#if defined(HAS_COPYROW_NEON)
60 61
  if (TestCpuFlag(kCpuHasNEON)) {
    CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
62 63
  }
#endif
64 65 66 67 68
#if defined(HAS_COPYROW_MIPS)
  if (TestCpuFlag(kCpuHasMIPS)) {
    CopyRow = CopyRow_MIPS;
  }
#endif
69

70
  // Copy plane
71
  for (y = 0; y < height; ++y) {
72
    CopyRow(src_y, dst_y, width);
73
    src_y += src_stride_y;
74
    dst_y += dst_stride_y;
75 76
  }
}
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
LIBYUV_API
void CopyPlane_16(const uint16* src_y, int src_stride_y,
                  uint16* dst_y, int dst_stride_y,
                  int width, int height) {
  int y;
  void (*CopyRow)(const uint16* src, uint16* dst, int width) = CopyRow_16_C;
  // Coalesce rows.
  if (src_stride_y == width &&
      dst_stride_y == width) {
    width *= height;
    height = 1;
    src_stride_y = dst_stride_y = 0;
  }
#if defined(HAS_COPYROW_16_SSE2)
92
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32)) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    CopyRow = CopyRow_16_SSE2;
  }
#endif
#if defined(HAS_COPYROW_16_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_16_ERMS;
  }
#endif
#if defined(HAS_COPYROW_16_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) {
    CopyRow = CopyRow_16_NEON;
  }
#endif
#if defined(HAS_COPYROW_16_MIPS)
  if (TestCpuFlag(kCpuHasMIPS)) {
    CopyRow = CopyRow_16_MIPS;
  }
#endif

  // Copy plane
  for (y = 0; y < height; ++y) {
    CopyRow(src_y, dst_y, width);
    src_y += src_stride_y;
    dst_y += dst_stride_y;
  }
}

120 121 122 123 124 125 126 127 128
// Copy I422.
LIBYUV_API
int I422Copy(const uint8* src_y, int src_stride_y,
             const uint8* src_u, int src_stride_u,
             const uint8* src_v, int src_stride_v,
             uint8* dst_y, int dst_stride_y,
             uint8* dst_u, int dst_stride_u,
             uint8* dst_v, int dst_stride_v,
             int width, int height) {
129
  int halfwidth = (width + 1) >> 1;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  if (!src_y || !src_u || !src_v ||
      !dst_y || !dst_u || !dst_v ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_u = src_u + (height - 1) * src_stride_u;
    src_v = src_v + (height - 1) * src_stride_v;
    src_stride_y = -src_stride_y;
    src_stride_u = -src_stride_u;
    src_stride_v = -src_stride_v;
  }
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height);
  CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height);
  return 0;
}

// Copy I444.
LIBYUV_API
int I444Copy(const uint8* src_y, int src_stride_y,
             const uint8* src_u, int src_stride_u,
             const uint8* src_v, int src_stride_v,
             uint8* dst_y, int dst_stride_y,
             uint8* dst_u, int dst_stride_u,
             uint8* dst_v, int dst_stride_v,
             int width, int height) {
  if (!src_y || !src_u || !src_v ||
      !dst_y || !dst_u || !dst_v ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_u = src_u + (height - 1) * src_stride_u;
    src_v = src_v + (height - 1) * src_stride_v;
    src_stride_y = -src_stride_y;
    src_stride_u = -src_stride_u;
    src_stride_v = -src_stride_v;
  }

  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height);
  CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height);
  return 0;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
// Copy I400.
LIBYUV_API
int I400ToI400(const uint8* src_y, int src_stride_y,
               uint8* dst_y, int dst_stride_y,
               int width, int height) {
  if (!src_y || !dst_y || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  return 0;
}

200
// Convert I420 to I400.
201
LIBYUV_API
202
int I420ToI400(const uint8* src_y, int src_stride_y,
203 204
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
205 206
               uint8* dst_y, int dst_stride_y,
               int width, int height) {
207
  if (!src_y || !dst_y || width <= 0 || height == 0) {
208 209 210 211 212 213 214 215 216 217 218 219
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
  CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  return 0;
}

220
// Mirror a plane of data.
221 222
void MirrorPlane(const uint8* src_y, int src_stride_y,
                 uint8* dst_y, int dst_stride_y,
223
                 int width, int height) {
224 225
  int y;
  void (*MirrorRow)(const uint8* src, uint8* dst, int width) = MirrorRow_C;
226 227 228 229 230 231
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
232
#if defined(HAS_MIRRORROW_NEON)
233 234 235 236 237
  if (TestCpuFlag(kCpuHasNEON)) {
    MirrorRow = MirrorRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      MirrorRow = MirrorRow_NEON;
    }
238
  }
239
#endif
240
#if defined(HAS_MIRRORROW_SSE2)
241 242 243 244 245
  if (TestCpuFlag(kCpuHasSSE2)) {
    MirrorRow = MirrorRow_Any_SSE2;
    if (IS_ALIGNED(width, 16)) {
      MirrorRow = MirrorRow_SSE2;
    }
246 247
  }
#endif
248
#if defined(HAS_MIRRORROW_SSSE3)
249 250 251 252 253
  if (TestCpuFlag(kCpuHasSSSE3)) {
    MirrorRow = MirrorRow_Any_SSSE3;
    if (IS_ALIGNED(width, 16)) {
      MirrorRow = MirrorRow_SSSE3;
    }
254
  }
255
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
256
#if defined(HAS_MIRRORROW_AVX2)
257 258 259 260 261 262 263 264 265 266
  if (TestCpuFlag(kCpuHasAVX2)) {
    MirrorRow = MirrorRow_Any_AVX2;
    if (IS_ALIGNED(width, 32)) {
      MirrorRow = MirrorRow_AVX2;
    }
  }
#endif
// TODO(fbarchard): Mirror on mips handle unaligned memory.
#if defined(HAS_MIRRORROW_MIPS_DSPR2)
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) &&
267 268
      IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
      IS_ALIGNED(dst_y, 4) && IS_ALIGNED(dst_stride_y, 4)) {
269
    MirrorRow = MirrorRow_MIPS_DSPR2;
fbarchard@google.com's avatar
fbarchard@google.com committed
270 271
  }
#endif
272

273
  // Mirror plane
274
  for (y = 0; y < height; ++y) {
275
    MirrorRow(src_y, dst_y, width);
276
    src_y += src_stride_y;
277
    dst_y += dst_stride_y;
278 279
  }
}
280

281 282 283 284 285 286 287
// Convert YUY2 to I422.
LIBYUV_API
int YUY2ToI422(const uint8* src_yuy2, int src_stride_yuy2,
               uint8* dst_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
288 289 290 291 292 293
  int y;
  void (*YUY2ToUV422Row)(const uint8* src_yuy2,
                         uint8* dst_u, uint8* dst_v, int pix) =
      YUY2ToUV422Row_C;
  void (*YUY2ToYRow)(const uint8* src_yuy2, uint8* dst_y, int pix) =
      YUY2ToYRow_C;
294 295 296 297 298 299
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
    src_stride_yuy2 = -src_stride_yuy2;
  }
300
  // Coalesce rows.
301
  if (src_stride_yuy2 == width * 2 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
302
      dst_stride_y == width &&
303 304
      dst_stride_u * 2 == width &&
      dst_stride_v * 2 == width) {
305 306 307
    width *= height;
    height = 1;
    src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
308
  }
309
#if defined(HAS_YUY2TOYROW_SSE2)
310
  if (TestCpuFlag(kCpuHasSSE2)) {
311 312
    YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2;
    YUY2ToYRow = YUY2ToYRow_Any_SSE2;
313
    if (IS_ALIGNED(width, 16)) {
314 315
      YUY2ToUV422Row = YUY2ToUV422Row_SSE2;
      YUY2ToYRow = YUY2ToYRow_SSE2;
316 317
    }
  }
318 319
#endif
#if defined(HAS_YUY2TOYROW_AVX2)
320
  if (TestCpuFlag(kCpuHasAVX2)) {
321 322 323 324 325 326 327 328 329
    YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2;
    YUY2ToYRow = YUY2ToYRow_Any_AVX2;
    if (IS_ALIGNED(width, 32)) {
      YUY2ToUV422Row = YUY2ToUV422Row_AVX2;
      YUY2ToYRow = YUY2ToYRow_AVX2;
    }
  }
#endif
#if defined(HAS_YUY2TOYROW_NEON)
330
  if (TestCpuFlag(kCpuHasNEON)) {
331 332 333
    YUY2ToYRow = YUY2ToYRow_Any_NEON;
    if (width >= 16) {
      YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON;
334 335 336 337 338 339 340 341
    }
    if (IS_ALIGNED(width, 16)) {
      YUY2ToYRow = YUY2ToYRow_NEON;
      YUY2ToUV422Row = YUY2ToUV422Row_NEON;
    }
  }
#endif

342
  for (y = 0; y < height; ++y) {
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width);
    YUY2ToYRow(src_yuy2, dst_y, width);
    src_yuy2 += src_stride_yuy2;
    dst_y += dst_stride_y;
    dst_u += dst_stride_u;
    dst_v += dst_stride_v;
  }
  return 0;
}

// Convert UYVY to I422.
LIBYUV_API
int UYVYToI422(const uint8* src_uyvy, int src_stride_uyvy,
               uint8* dst_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
360 361 362 363 364 365
  int y;
  void (*UYVYToUV422Row)(const uint8* src_uyvy,
                         uint8* dst_u, uint8* dst_v, int pix) =
      UYVYToUV422Row_C;
  void (*UYVYToYRow)(const uint8* src_uyvy,
                     uint8* dst_y, int pix) = UYVYToYRow_C;
366 367 368 369 370 371
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy;
    src_stride_uyvy = -src_stride_uyvy;
  }
372
  // Coalesce rows.
373
  if (src_stride_uyvy == width * 2 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
374
      dst_stride_y == width &&
375 376
      dst_stride_u * 2 == width &&
      dst_stride_v * 2 == width) {
377 378 379
    width *= height;
    height = 1;
    src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
380
  }
381
#if defined(HAS_UYVYTOYROW_SSE2)
382
  if (TestCpuFlag(kCpuHasSSE2)) {
383 384
    UYVYToUV422Row = UYVYToUV422Row_Any_SSE2;
    UYVYToYRow = UYVYToYRow_Any_SSE2;
385
    if (IS_ALIGNED(width, 16)) {
386 387
      UYVYToUV422Row = UYVYToUV422Row_SSE2;
      UYVYToYRow = UYVYToYRow_SSE2;
388 389
    }
  }
390 391
#endif
#if defined(HAS_UYVYTOYROW_AVX2)
392
  if (TestCpuFlag(kCpuHasAVX2)) {
393 394 395 396 397 398 399 400 401
    UYVYToUV422Row = UYVYToUV422Row_Any_AVX2;
    UYVYToYRow = UYVYToYRow_Any_AVX2;
    if (IS_ALIGNED(width, 32)) {
      UYVYToUV422Row = UYVYToUV422Row_AVX2;
      UYVYToYRow = UYVYToYRow_AVX2;
    }
  }
#endif
#if defined(HAS_UYVYTOYROW_NEON)
402
  if (TestCpuFlag(kCpuHasNEON)) {
403 404 405
    UYVYToYRow = UYVYToYRow_Any_NEON;
    if (width >= 16) {
      UYVYToUV422Row = UYVYToUV422Row_Any_NEON;
406 407 408 409 410 411 412 413
    }
    if (IS_ALIGNED(width, 16)) {
      UYVYToYRow = UYVYToYRow_NEON;
      UYVYToUV422Row = UYVYToUV422Row_NEON;
    }
  }
#endif

414
  for (y = 0; y < height; ++y) {
415 416 417 418 419 420 421 422 423 424
    UYVYToUV422Row(src_uyvy, dst_u, dst_v, width);
    UYVYToYRow(src_uyvy, dst_y, width);
    src_uyvy += src_stride_uyvy;
    dst_y += dst_stride_y;
    dst_u += dst_stride_u;
    dst_v += dst_stride_v;
  }
  return 0;
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
// Mirror I400 with optional flipping
LIBYUV_API
int I400Mirror(const uint8* src_y, int src_stride_y,
               uint8* dst_y, int dst_stride_y,
               int width, int height) {
  if (!src_y || !dst_y ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }

  MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  return 0;
}

445
// Mirror I420 with optional flipping
446
LIBYUV_API
447 448 449 450 451 452 453
int I420Mirror(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_y, int dst_stride_y,
               uint8* dst_u, int dst_stride_u,
               uint8* dst_v, int dst_stride_v,
               int width, int height) {
454 455
  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;
456
  if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v ||
457 458 459
      width <= 0 || height == 0) {
    return -1;
  }
460 461 462
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
463
    halfheight = (height + 1) >> 1;
464 465 466 467 468 469
    src_y = src_y + (height - 1) * src_stride_y;
    src_u = src_u + (halfheight - 1) * src_stride_u;
    src_v = src_v + (halfheight - 1) * src_stride_v;
    src_stride_y = -src_stride_y;
    src_stride_u = -src_stride_u;
    src_stride_v = -src_stride_v;
470
  }
471

472 473
  if (dst_y) {
    MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
474
  }
475 476
  MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight);
  MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight);
477 478
  return 0;
}
479

480
// ARGB mirror.
481
LIBYUV_API
482 483 484
int ARGBMirror(const uint8* src_argb, int src_stride_argb,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
485 486 487
  int y;
  void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
      ARGBMirrorRow_C;
488
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
489 490 491 492 493 494 495 496
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
497 498 499 500 501 502
#if defined(HAS_ARGBMIRRORROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBMirrorRow = ARGBMirrorRow_NEON;
    }
503
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
504
#endif
505 506 507
#if defined(HAS_ARGBMIRRORROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
508
    if (IS_ALIGNED(width, 4)) {
509
      ARGBMirrorRow = ARGBMirrorRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
510
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
511 512
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
513 514 515 516 517 518
#if defined(HAS_ARGBMIRRORROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBMirrorRow = ARGBMirrorRow_AVX2;
    }
519
  }
520 521 522
#endif

  // Mirror plane
523
  for (y = 0; y < height; ++y) {
524 525 526 527 528 529 530
    ARGBMirrorRow(src_argb, dst_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

531 532 533
// Get a blender that optimized for the CPU, alignment and pixel count.
// As there are 6 blenders to choose from, the caller should try to use
// the same blend function for all pixels if possible.
534
LIBYUV_API
535
ARGBBlendRow GetARGBBlend() {
536 537
  void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1,
                       uint8* dst_argb, int width) = ARGBBlendRow_C;
538
#if defined(HAS_ARGBBLENDROW_SSSE3)
539 540
  if (TestCpuFlag(kCpuHasSSSE3)) {
    ARGBBlendRow = ARGBBlendRow_SSSE3;
541 542 543
    return ARGBBlendRow;
  }
#endif
544
#if defined(HAS_ARGBBLENDROW_SSE2)
545 546
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBBlendRow = ARGBBlendRow_SSE2;
547
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
548 549 550 551 552
#endif
#if defined(HAS_ARGBBLENDROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBBlendRow = ARGBBlendRow_NEON;
  }
553
#endif
554
  return ARGBBlendRow;
555 556 557
}

// Alpha Blend 2 ARGB images and store to destination.
558
LIBYUV_API
559
int ARGBBlend(const uint8* src_argb0, int src_stride_argb0,
560 561 562
              const uint8* src_argb1, int src_stride_argb1,
              uint8* dst_argb, int dst_stride_argb,
              int width, int height) {
563 564 565
  int y;
  void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1,
                       uint8* dst_argb, int width) = GetARGBBlend();
566 567 568 569 570 571 572 573 574
  if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
575
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
576 577 578
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
579 580 581
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
582
  }
583

584
  for (y = 0; y < height; ++y) {
585
    ARGBBlendRow(src_argb0, src_argb1, dst_argb, width);
586 587 588
    src_argb0 += src_stride_argb0;
    src_argb1 += src_stride_argb1;
    dst_argb += dst_stride_argb;
589 590 591 592
  }
  return 0;
}

593
// Multiply 2 ARGB images and store to destination.
594 595 596 597 598
LIBYUV_API
int ARGBMultiply(const uint8* src_argb0, int src_stride_argb0,
                 const uint8* src_argb1, int src_stride_argb1,
                 uint8* dst_argb, int dst_stride_argb,
                 int width, int height) {
599 600 601
  int y;
  void (*ARGBMultiplyRow)(const uint8* src0, const uint8* src1, uint8* dst,
                          int width) = ARGBMultiplyRow_C;
602 603 604 605 606 607 608 609 610
  if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
611
  // Coalesce rows.
612 613 614
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
615 616 617
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
618
  }
619
#if defined(HAS_ARGBMULTIPLYROW_SSE2)
620
  if (TestCpuFlag(kCpuHasSSE2)) {
621 622 623 624 625
    ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBMultiplyRow = ARGBMultiplyRow_SSE2;
    }
  }
626 627
#endif
#if defined(HAS_ARGBMULTIPLYROW_AVX2)
628
  if (TestCpuFlag(kCpuHasAVX2)) {
629 630 631 632 633 634 635
    ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBMultiplyRow = ARGBMultiplyRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBMULTIPLYROW_NEON)
636
  if (TestCpuFlag(kCpuHasNEON)) {
637 638 639 640
    ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBMultiplyRow = ARGBMultiplyRow_NEON;
    }
641 642 643 644
  }
#endif

  // Multiply plane
645
  for (y = 0; y < height; ++y) {
646 647 648 649 650 651 652 653
    ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width);
    src_argb0 += src_stride_argb0;
    src_argb1 += src_stride_argb1;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

654
// Add 2 ARGB images and store to destination.
655 656 657 658 659
LIBYUV_API
int ARGBAdd(const uint8* src_argb0, int src_stride_argb0,
            const uint8* src_argb1, int src_stride_argb1,
            uint8* dst_argb, int dst_stride_argb,
            int width, int height) {
660 661 662
  int y;
  void (*ARGBAddRow)(const uint8* src0, const uint8* src1, uint8* dst,
                     int width) = ARGBAddRow_C;
663 664 665 666 667 668 669 670 671
  if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
672
  // Coalesce rows.
673 674 675
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
676 677 678
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
679
  }
680 681 682 683 684 685
#if defined(HAS_ARGBADDROW_SSE2) && defined(_MSC_VER)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBAddRow = ARGBAddRow_SSE2;
  }
#endif
#if defined(HAS_ARGBADDROW_SSE2) && !defined(_MSC_VER)
686
  if (TestCpuFlag(kCpuHasSSE2)) {
687 688 689 690 691
    ARGBAddRow = ARGBAddRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBAddRow = ARGBAddRow_SSE2;
    }
  }
692 693
#endif
#if defined(HAS_ARGBADDROW_AVX2)
694
  if (TestCpuFlag(kCpuHasAVX2)) {
695 696 697 698 699 700 701
    ARGBAddRow = ARGBAddRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBAddRow = ARGBAddRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBADDROW_NEON)
702
  if (TestCpuFlag(kCpuHasNEON)) {
703 704 705 706
    ARGBAddRow = ARGBAddRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBAddRow = ARGBAddRow_NEON;
    }
707 708 709 710
  }
#endif

  // Add plane
711
  for (y = 0; y < height; ++y) {
712 713 714 715 716 717 718 719
    ARGBAddRow(src_argb0, src_argb1, dst_argb, width);
    src_argb0 += src_stride_argb0;
    src_argb1 += src_stride_argb1;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

720 721 722 723 724 725
// Subtract 2 ARGB images and store to destination.
LIBYUV_API
int ARGBSubtract(const uint8* src_argb0, int src_stride_argb0,
                 const uint8* src_argb1, int src_stride_argb1,
                 uint8* dst_argb, int dst_stride_argb,
                 int width, int height) {
726 727 728
  int y;
  void (*ARGBSubtractRow)(const uint8* src0, const uint8* src1, uint8* dst,
                          int width) = ARGBSubtractRow_C;
729 730 731 732 733 734 735 736 737
  if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
738
  // Coalesce rows.
739 740 741
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
742 743 744
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
745
  }
746
#if defined(HAS_ARGBSUBTRACTROW_SSE2)
747
  if (TestCpuFlag(kCpuHasSSE2)) {
748 749 750 751 752
    ARGBSubtractRow = ARGBSubtractRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBSubtractRow = ARGBSubtractRow_SSE2;
    }
  }
753 754
#endif
#if defined(HAS_ARGBSUBTRACTROW_AVX2)
755
  if (TestCpuFlag(kCpuHasAVX2)) {
756 757 758 759 760 761 762
    ARGBSubtractRow = ARGBSubtractRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBSubtractRow = ARGBSubtractRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBSUBTRACTROW_NEON)
763
  if (TestCpuFlag(kCpuHasNEON)) {
764 765 766 767 768 769 770 771
    ARGBSubtractRow = ARGBSubtractRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBSubtractRow = ARGBSubtractRow_NEON;
    }
  }
#endif

  // Subtract plane
772
  for (y = 0; y < height; ++y) {
773 774 775 776 777 778 779 780
    ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width);
    src_argb0 += src_stride_argb0;
    src_argb1 += src_stride_argb1;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

781
// Convert I422 to BGRA.
782
LIBYUV_API
783 784 785 786 787
int I422ToBGRA(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_bgra, int dst_stride_bgra,
               int width, int height) {
788 789 790 791 792 793
  int y;
  void (*I422ToBGRARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToBGRARow_C;
794 795 796 797 798 799 800 801 802 803 804
  if (!src_y || !src_u || !src_v ||
      !dst_bgra ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_bgra = dst_bgra + (height - 1) * dst_stride_bgra;
    dst_stride_bgra = -dst_stride_bgra;
  }
805
  // Coalesce rows.
806 807 808
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
809
      dst_stride_bgra == width * 4) {
810 811 812
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_bgra = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
813
  }
814
#if defined(HAS_I422TOBGRAROW_SSSE3)
815
  if (TestCpuFlag(kCpuHasSSSE3)) {
816 817
    I422ToBGRARow = I422ToBGRARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
818
      I422ToBGRARow = I422ToBGRARow_SSSE3;
819 820
    }
  }
821
#endif
822
#if defined(HAS_I422TOBGRAROW_AVX2)
823
  if (TestCpuFlag(kCpuHasAVX2)) {
824 825 826 827 828 829 830
    I422ToBGRARow = I422ToBGRARow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToBGRARow = I422ToBGRARow_AVX2;
    }
  }
#endif
#if defined(HAS_I422TOBGRAROW_NEON)
831
  if (TestCpuFlag(kCpuHasNEON)) {
832 833 834 835 836 837
    I422ToBGRARow = I422ToBGRARow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      I422ToBGRARow = I422ToBGRARow_NEON;
    }
  }
#endif
838
#if defined(HAS_I422TOBGRAROW_MIPS_DSPR2)
839 840 841 842 843 844 845
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(src_y, 4) && IS_ALIGNED(src_stride_y, 4) &&
      IS_ALIGNED(src_u, 2) && IS_ALIGNED(src_stride_u, 2) &&
      IS_ALIGNED(src_v, 2) && IS_ALIGNED(src_stride_v, 2) &&
      IS_ALIGNED(dst_bgra, 4) && IS_ALIGNED(dst_stride_bgra, 4)) {
    I422ToBGRARow = I422ToBGRARow_MIPS_DSPR2;
  }
846 847
#endif

848
  for (y = 0; y < height; ++y) {
849 850 851 852 853 854 855 856 857 858
    I422ToBGRARow(src_y, src_u, src_v, dst_bgra, width);
    dst_bgra += dst_stride_bgra;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

// Convert I422 to ABGR.
859
LIBYUV_API
860 861 862 863 864
int I422ToABGR(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_abgr, int dst_stride_abgr,
               int width, int height) {
865 866 867 868 869 870
  int y;
  void (*I422ToABGRRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToABGRRow_C;
871 872 873 874 875 876 877 878 879 880 881
  if (!src_y || !src_u || !src_v ||
      !dst_abgr ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_abgr = dst_abgr + (height - 1) * dst_stride_abgr;
    dst_stride_abgr = -dst_stride_abgr;
  }
882
  // Coalesce rows.
883 884 885
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
886
      dst_stride_abgr == width * 4) {
887 888 889
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_abgr = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
890
  }
891
#if defined(HAS_I422TOABGRROW_NEON)
892
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
893
    I422ToABGRRow = I422ToABGRRow_Any_NEON;
894
    if (IS_ALIGNED(width, 8)) {
895 896 897
      I422ToABGRRow = I422ToABGRRow_NEON;
    }
  }
898 899
#endif
#if defined(HAS_I422TOABGRROW_SSSE3)
900
  if (TestCpuFlag(kCpuHasSSSE3)) {
901 902
    I422ToABGRRow = I422ToABGRRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
903
      I422ToABGRRow = I422ToABGRRow_SSSE3;
904 905 906
    }
  }
#endif
907
#if defined(HAS_I422TOABGRROW_AVX2)
908
  if (TestCpuFlag(kCpuHasAVX2)) {
909 910 911 912 913 914
    I422ToABGRRow = I422ToABGRRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToABGRRow = I422ToABGRRow_AVX2;
    }
  }
#endif
915

916
  for (y = 0; y < height; ++y) {
917 918 919 920 921 922 923 924 925 926
    I422ToABGRRow(src_y, src_u, src_v, dst_abgr, width);
    dst_abgr += dst_stride_abgr;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

// Convert I422 to RGBA.
927
LIBYUV_API
928 929 930 931 932
int I422ToRGBA(const uint8* src_y, int src_stride_y,
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
               uint8* dst_rgba, int dst_stride_rgba,
               int width, int height) {
933 934 935 936 937 938
  int y;
  void (*I422ToRGBARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToRGBARow_C;
939 940 941 942 943 944 945 946 947 948 949
  if (!src_y || !src_u || !src_v ||
      !dst_rgba ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgba = dst_rgba + (height - 1) * dst_stride_rgba;
    dst_stride_rgba = -dst_stride_rgba;
  }
950
  // Coalesce rows.
951 952 953
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
954
      dst_stride_rgba == width * 4) {
955 956 957
    width *= height;
    height = 1;
    src_stride_y = src_stride_u = src_stride_v = dst_stride_rgba = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
958
  }
959
#if defined(HAS_I422TORGBAROW_NEON)
960
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
961
    I422ToRGBARow = I422ToRGBARow_Any_NEON;
962
    if (IS_ALIGNED(width, 8)) {
963 964 965
      I422ToRGBARow = I422ToRGBARow_NEON;
    }
  }
966 967
#endif
#if defined(HAS_I422TORGBAROW_SSSE3)
968
  if (TestCpuFlag(kCpuHasSSSE3)) {
969 970
    I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
971
      I422ToRGBARow = I422ToRGBARow_SSSE3;
972 973 974
    }
  }
#endif
975
#if defined(HAS_I422TORGBAROW_AVX2)
976
  if (TestCpuFlag(kCpuHasAVX2)) {
977 978 979 980 981 982
    I422ToRGBARow = I422ToRGBARow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGBARow = I422ToRGBARow_AVX2;
    }
  }
#endif
983

984
  for (y = 0; y < height; ++y) {
985 986 987 988 989 990 991 992 993
    I422ToRGBARow(src_y, src_u, src_v, dst_rgba, width);
    dst_rgba += dst_stride_rgba;
    src_y += src_stride_y;
    src_u += src_stride_u;
    src_v += src_stride_v;
  }
  return 0;
}

994
// Convert NV12 to RGB565.
995
LIBYUV_API
996 997 998 999
int NV12ToRGB565(const uint8* src_y, int src_stride_y,
                 const uint8* src_uv, int src_stride_uv,
                 uint8* dst_rgb565, int dst_stride_rgb565,
                 int width, int height) {
1000 1001 1002 1003 1004
  int y;
  void (*NV12ToRGB565Row)(const uint8* y_buf,
                          const uint8* uv_buf,
                          uint8* rgb_buf,
                          int width) = NV12ToRGB565Row_C;
1005 1006
  if (!src_y || !src_uv || !dst_rgb565 ||
      width <= 0 || height == 0) {
1007 1008
    return -1;
  }
1009 1010 1011 1012 1013 1014
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
  }
1015
#if defined(HAS_NV12TORGB565ROW_SSSE3)
1016
  if (TestCpuFlag(kCpuHasSSSE3)) {
1017
    NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3;
1018
    if (IS_ALIGNED(width, 8)) {
1019
      NV12ToRGB565Row = NV12ToRGB565Row_SSSE3;
1020
    }
1021
  }
1022
#endif
1023 1024 1025 1026 1027 1028 1029 1030
#if defined(HAS_NV12TORGB565ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    NV12ToRGB565Row = NV12ToRGB565Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      NV12ToRGB565Row = NV12ToRGB565Row_AVX2;
    }
  }
#endif
1031
#if defined(HAS_NV12TORGB565ROW_NEON)
1032
  if (TestCpuFlag(kCpuHasNEON)) {
1033
    NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON;
1034
    if (IS_ALIGNED(width, 8)) {
1035
      NV12ToRGB565Row = NV12ToRGB565Row_NEON;
1036
    }
1037
  }
1038
#endif
1039

1040
  for (y = 0; y < height; ++y) {
1041
    NV12ToRGB565Row(src_y, src_uv, dst_rgb565, width);
1042 1043 1044
    dst_rgb565 += dst_stride_rgb565;
    src_y += src_stride_y;
    if (y & 1) {
1045 1046
      src_uv += src_stride_uv;
    }
1047 1048 1049 1050 1051
  }
  return 0;
}

// Convert NV21 to RGB565.
1052
LIBYUV_API
1053
int NV21ToRGB565(const uint8* src_y, int src_stride_y,
1054
                 const uint8* src_vu, int src_stride_vu,
1055 1056
                 uint8* dst_rgb565, int dst_stride_rgb565,
                 int width, int height) {
1057 1058 1059 1060 1061
  int y;
  void (*NV21ToRGB565Row)(const uint8* y_buf,
                          const uint8* src_vu,
                          uint8* rgb_buf,
                          int width) = NV21ToRGB565Row_C;
1062 1063
  if (!src_y || !src_vu || !dst_rgb565 ||
      width <= 0 || height == 0) {
1064 1065
    return -1;
  }
1066 1067 1068 1069 1070 1071
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_rgb565 = dst_rgb565 + (height - 1) * dst_stride_rgb565;
    dst_stride_rgb565 = -dst_stride_rgb565;
  }
1072
#if defined(HAS_NV21TORGB565ROW_SSSE3)
1073
  if (TestCpuFlag(kCpuHasSSSE3)) {
1074
    NV21ToRGB565Row = NV21ToRGB565Row_Any_SSSE3;
1075
    if (IS_ALIGNED(width, 8)) {
1076
      NV21ToRGB565Row = NV21ToRGB565Row_SSSE3;
1077
    }
1078
  }
1079
#endif
1080 1081 1082 1083 1084 1085 1086 1087
#if defined(HAS_NV21TORGB565ROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    NV21ToRGB565Row = NV21ToRGB565Row_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      NV21ToRGB565Row = NV21ToRGB565Row_AVX2;
    }
  }
#endif
1088
#if defined(HAS_NV21TORGB565ROW_NEON)
1089
  if (TestCpuFlag(kCpuHasNEON)) {
1090
    NV21ToRGB565Row = NV21ToRGB565Row_Any_NEON;
1091
    if (IS_ALIGNED(width, 8)) {
1092
      NV21ToRGB565Row = NV21ToRGB565Row_NEON;
1093
    }
1094 1095 1096
  }
#endif

1097
  for (y = 0; y < height; ++y) {
1098
    NV21ToRGB565Row(src_y, src_vu, dst_rgb565, width);
1099
    dst_rgb565 += dst_stride_rgb565;
1100
    src_y += src_stride_y;
1101
    if (y & 1) {
1102
      src_vu += src_stride_vu;
1103
    }
1104 1105 1106 1107
  }
  return 0;
}

1108
LIBYUV_API
1109 1110 1111
void SetPlane(uint8* dst_y, int dst_stride_y,
              int width, int height,
              uint32 value) {
1112
  int y;
1113
  void (*SetRow)(uint8* dst, uint8 value, int pix) = SetRow_C;
1114 1115 1116 1117 1118
  if (height < 0) {
    height = -height;
    dst_y = dst_y + (height - 1) * dst_stride_y;
    dst_stride_y = -dst_stride_y;
  }
1119
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1120
  if (dst_stride_y == width) {
1121 1122 1123
    width *= height;
    height = 1;
    dst_stride_y = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1124
  }
1125
#if defined(HAS_SETROW_NEON)
1126 1127 1128 1129 1130
  if (TestCpuFlag(kCpuHasNEON)) {
    SetRow = SetRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      SetRow = SetRow_NEON;
    }
1131
  }
1132 1133
#endif
#if defined(HAS_SETROW_X86)
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  if (TestCpuFlag(kCpuHasX86)) {
    SetRow = SetRow_Any_X86;
    if (IS_ALIGNED(width, 4)) {
      SetRow = SetRow_X86;
    }
  }
#endif
#if defined(HAS_SETROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    SetRow = SetRow_ERMS;
1144 1145
  }
#endif
1146 1147

  // Set plane
1148
  for (y = 0; y < height; ++y) {
1149
    SetRow(dst_y, value, width);
1150 1151 1152 1153 1154
    dst_y += dst_stride_y;
  }
}

// Draw a rectangle into I420
1155
LIBYUV_API
1156 1157 1158 1159 1160 1161
int I420Rect(uint8* dst_y, int dst_stride_y,
             uint8* dst_u, int dst_stride_u,
             uint8* dst_v, int dst_stride_v,
             int x, int y,
             int width, int height,
             int value_y, int value_u, int value_v) {
1162 1163 1164 1165 1166
  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;
  uint8* start_y = dst_y + y * dst_stride_y + x;
  uint8* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2);
  uint8* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2);
1167
  if (!dst_y || !dst_u || !dst_v ||
1168
      width <= 0 || height == 0 ||
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
      x < 0 || y < 0 ||
      value_y < 0 || value_y > 255 ||
      value_u < 0 || value_u > 255 ||
      value_v < 0 || value_v > 255) {
    return -1;
  }

  SetPlane(start_y, dst_stride_y, width, height, value_y);
  SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u);
  SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v);
  return 0;
}

// Draw a rectangle into ARGB
1183
LIBYUV_API
1184
int ARGBRect(uint8* dst_argb, int dst_stride_argb,
1185
             int dst_x, int dst_y,
1186 1187
             int width, int height,
             uint32 value) {
1188 1189
  int y;
  void (*ARGBSetRow)(uint8* dst_argb, uint32 value, int pix) = ARGBSetRow_C;
1190
  if (!dst_argb ||
1191
      width <= 0 || height == 0 ||
1192
      dst_x < 0 || dst_y < 0) {
1193 1194
    return -1;
  }
1195 1196 1197 1198 1199
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
1200 1201
  dst_argb += dst_y * dst_stride_argb + dst_x * 4;
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1202
  if (dst_stride_argb == width * 4) {
1203 1204 1205
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1206
  }
1207 1208 1209 1210 1211 1212 1213

#if defined(HAS_ARGBSETROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBSetRow = ARGBSetRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBSetRow = ARGBSetRow_NEON;
    }
1214
  }
1215
#endif
1216
#if defined(HAS_ARGBSETROW_X86)
1217
  if (TestCpuFlag(kCpuHasX86)) {
1218
    ARGBSetRow = ARGBSetRow_X86;
1219
  }
1220
#endif
1221 1222 1223 1224 1225 1226

  // Set plane
  for (y = 0; y < height; ++y) {
    ARGBSetRow(dst_argb, value, width);
    dst_argb += dst_stride_argb;
  }
1227
  return 0;
1228 1229
}

1230
// Convert unattentuated ARGB to preattenuated ARGB.
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
// An unattenutated ARGB alpha blend uses the formula
// p = a * f + (1 - a) * b
// where
//   p is output pixel
//   f is foreground pixel
//   b is background pixel
//   a is alpha value from foreground pixel
// An preattenutated ARGB alpha blend uses the formula
// p = f + (1 - a) * b
// where
//   f is foreground pixel premultiplied by alpha

1243
LIBYUV_API
1244 1245 1246
int ARGBAttenuate(const uint8* src_argb, int src_stride_argb,
                  uint8* dst_argb, int dst_stride_argb,
                  int width, int height) {
1247 1248 1249
  int y;
  void (*ARGBAttenuateRow)(const uint8* src_argb, uint8* dst_argb,
                           int width) = ARGBAttenuateRow_C;
1250 1251 1252
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
1253 1254 1255 1256 1257
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1258
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1259 1260
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1261 1262 1263
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1264
  }
1265
#if defined(HAS_ARGBATTENUATEROW_SSE2)
1266
  if (TestCpuFlag(kCpuHasSSE2)) {
1267 1268 1269 1270
    ARGBAttenuateRow = ARGBAttenuateRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBAttenuateRow = ARGBAttenuateRow_SSE2;
    }
1271 1272
  }
#endif
1273
#if defined(HAS_ARGBATTENUATEROW_SSSE3)
1274
  if (TestCpuFlag(kCpuHasSSSE3)) {
1275 1276 1277 1278 1279 1280
    ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3;
    if (IS_ALIGNED(width, 4)) {
      ARGBAttenuateRow = ARGBAttenuateRow_SSSE3;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1281
#if defined(HAS_ARGBATTENUATEROW_AVX2)
1282
  if (TestCpuFlag(kCpuHasAVX2)) {
1283 1284 1285 1286
    ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBAttenuateRow = ARGBAttenuateRow_AVX2;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
1287 1288
  }
#endif
1289
#if defined(HAS_ARGBATTENUATEROW_NEON)
1290
  if (TestCpuFlag(kCpuHasNEON)) {
1291 1292 1293 1294
    ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBAttenuateRow = ARGBAttenuateRow_NEON;
    }
1295 1296
  }
#endif
1297

1298
  for (y = 0; y < height; ++y) {
1299
    ARGBAttenuateRow(src_argb, dst_argb, width);
1300 1301 1302 1303 1304 1305
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1306
// Convert preattentuated ARGB to unattenuated ARGB.
1307
LIBYUV_API
1308 1309 1310
int ARGBUnattenuate(const uint8* src_argb, int src_stride_argb,
                    uint8* dst_argb, int dst_stride_argb,
                    int width, int height) {
1311 1312 1313
  int y;
  void (*ARGBUnattenuateRow)(const uint8* src_argb, uint8* dst_argb,
                             int width) = ARGBUnattenuateRow_C;
1314 1315 1316
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
1317 1318 1319 1320 1321
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1322
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1323 1324
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1325 1326 1327
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1328
  }
1329
#if defined(HAS_ARGBUNATTENUATEROW_SSE2)
1330
  if (TestCpuFlag(kCpuHasSSE2)) {
1331 1332 1333 1334
    ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2;
    }
1335 1336
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1337
#if defined(HAS_ARGBUNATTENUATEROW_AVX2)
1338
  if (TestCpuFlag(kCpuHasAVX2)) {
1339 1340 1341 1342
    ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
1343 1344 1345
  }
#endif
// TODO(fbarchard): Neon version.
1346

1347
  for (y = 0; y < height; ++y) {
1348
    ARGBUnattenuateRow(src_argb, dst_argb, width);
1349 1350 1351 1352 1353 1354
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1355
// Convert ARGB to Grayed ARGB.
1356
LIBYUV_API
1357 1358 1359
int ARGBGrayTo(const uint8* src_argb, int src_stride_argb,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
1360 1361 1362
  int y;
  void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb,
                      int width) = ARGBGrayRow_C;
1363 1364 1365 1366 1367 1368 1369 1370
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1371
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1372 1373
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1374 1375 1376
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1377
  }
1378
#if defined(HAS_ARGBGRAYROW_SSSE3)
1379
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1380 1381
    ARGBGrayRow = ARGBGrayRow_SSSE3;
  }
1382 1383
#endif
#if defined(HAS_ARGBGRAYROW_NEON)
1384 1385 1386
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBGrayRow = ARGBGrayRow_NEON;
  }
1387 1388
#endif

1389
  for (y = 0; y < height; ++y) {
1390 1391 1392 1393 1394 1395 1396
    ARGBGrayRow(src_argb, dst_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1397
// Make a rectangle of ARGB gray scale.
1398
LIBYUV_API
1399 1400 1401
int ARGBGray(uint8* dst_argb, int dst_stride_argb,
             int dst_x, int dst_y,
             int width, int height) {
1402 1403 1404 1405
  int y;
  void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb,
                      int width) = ARGBGrayRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1406 1407 1408
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
    return -1;
  }
1409
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1410
  if (dst_stride_argb == width * 4) {
1411 1412 1413
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1414
  }
1415
#if defined(HAS_ARGBGRAYROW_SSSE3)
1416
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1417 1418
    ARGBGrayRow = ARGBGrayRow_SSSE3;
  }
1419 1420
#endif
#if defined(HAS_ARGBGRAYROW_NEON)
1421 1422 1423
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBGrayRow = ARGBGrayRow_NEON;
  }
1424
#endif
1425
  for (y = 0; y < height; ++y) {
1426
    ARGBGrayRow(dst, dst, width);
1427 1428 1429 1430 1431
    dst += dst_stride_argb;
  }
  return 0;
}

1432
// Make a rectangle of ARGB Sepia tone.
1433
LIBYUV_API
1434
int ARGBSepia(uint8* dst_argb, int dst_stride_argb,
1435
              int dst_x, int dst_y, int width, int height) {
1436 1437 1438
  int y;
  void (*ARGBSepiaRow)(uint8* dst_argb, int width) = ARGBSepiaRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1439 1440 1441
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
    return -1;
  }
1442
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1443
  if (dst_stride_argb == width * 4) {
1444 1445 1446
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1447
  }
1448
#if defined(HAS_ARGBSEPIAROW_SSSE3)
1449
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1450 1451
    ARGBSepiaRow = ARGBSepiaRow_SSSE3;
  }
1452 1453
#endif
#if defined(HAS_ARGBSEPIAROW_NEON)
fbarchard@google.com's avatar
fbarchard@google.com committed
1454 1455 1456
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBSepiaRow = ARGBSepiaRow_NEON;
  }
1457
#endif
1458
  for (y = 0; y < height; ++y) {
1459 1460 1461 1462 1463 1464
    ARGBSepiaRow(dst, width);
    dst += dst_stride_argb;
  }
  return 0;
}

1465 1466
// Apply a 4x4 matrix to each ARGB pixel.
// Note: Normally for shading, but can be used to swizzle or invert.
1467
LIBYUV_API
1468 1469
int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb,
                    uint8* dst_argb, int dst_stride_argb,
1470
                    const int8* matrix_argb,
1471
                    int width, int height) {
1472 1473 1474
  int y;
  void (*ARGBColorMatrixRow)(const uint8* src_argb, uint8* dst_argb,
      const int8* matrix_argb, int width) = ARGBColorMatrixRow_C;
1475
  if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) {
1476 1477
    return -1;
  }
1478 1479 1480 1481 1482
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1483
  // Coalesce rows.
1484 1485
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1486 1487 1488
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1489
  }
1490
#if defined(HAS_ARGBCOLORMATRIXROW_SSSE3)
1491
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
1492 1493
    ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3;
  }
1494 1495
#endif
#if defined(HAS_ARGBCOLORMATRIXROW_NEON)
1496 1497 1498
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBColorMatrixRow = ARGBColorMatrixRow_NEON;
  }
1499
#endif
1500
  for (y = 0; y < height; ++y) {
1501 1502 1503
    ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
1504 1505 1506
  }
  return 0;
}
1507

1508 1509
// Apply a 4x3 matrix to each ARGB pixel.
// Deprecated.
1510
LIBYUV_API
1511 1512 1513
int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb,
                   const int8* matrix_rgb,
                   int dst_x, int dst_y, int width, int height) {
1514 1515
  SIMD_ALIGNED(int8 matrix_argb[16]);
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
  if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 ||
      dst_x < 0 || dst_y < 0) {
    return -1;
  }

  // Convert 4x3 7 bit matrix to 4x4 6 bit matrix.
  matrix_argb[0] = matrix_rgb[0] / 2;
  matrix_argb[1] = matrix_rgb[1] / 2;
  matrix_argb[2] = matrix_rgb[2] / 2;
  matrix_argb[3] = matrix_rgb[3] / 2;
  matrix_argb[4] = matrix_rgb[4] / 2;
  matrix_argb[5] = matrix_rgb[5] / 2;
  matrix_argb[6] = matrix_rgb[6] / 2;
  matrix_argb[7] = matrix_rgb[7] / 2;
  matrix_argb[8] = matrix_rgb[8] / 2;
  matrix_argb[9] = matrix_rgb[9] / 2;
  matrix_argb[10] = matrix_rgb[10] / 2;
  matrix_argb[11] = matrix_rgb[11] / 2;
  matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0;
  matrix_argb[15] = 64;  // 1.0

1537
  return ARGBColorMatrix((const uint8*)(dst), dst_stride_argb,
1538 1539 1540 1541
                         dst, dst_stride_argb,
                         &matrix_argb[0], width, height);
}

1542 1543
// Apply a color table each ARGB pixel.
// Table contains 256 ARGB values.
1544
LIBYUV_API
1545 1546 1547
int ARGBColorTable(uint8* dst_argb, int dst_stride_argb,
                   const uint8* table_argb,
                   int dst_x, int dst_y, int width, int height) {
1548 1549 1550 1551
  int y;
  void (*ARGBColorTableRow)(uint8* dst_argb, const uint8* table_argb,
                            int width) = ARGBColorTableRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1552 1553
  if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
      dst_x < 0 || dst_y < 0) {
1554 1555
    return -1;
  }
1556
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1557
  if (dst_stride_argb == width * 4) {
1558 1559 1560
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1561
  }
1562 1563 1564 1565 1566
#if defined(HAS_ARGBCOLORTABLEROW_X86)
  if (TestCpuFlag(kCpuHasX86)) {
    ARGBColorTableRow = ARGBColorTableRow_X86;
  }
#endif
1567
  for (y = 0; y < height; ++y) {
1568 1569 1570 1571 1572
    ARGBColorTableRow(dst, table_argb, width);
    dst += dst_stride_argb;
  }
  return 0;
}
1573

1574 1575 1576 1577 1578 1579
// Apply a color table each ARGB pixel but preserve destination alpha.
// Table contains 256 ARGB values.
LIBYUV_API
int RGBColorTable(uint8* dst_argb, int dst_stride_argb,
                  const uint8* table_argb,
                  int dst_x, int dst_y, int width, int height) {
1580 1581 1582 1583
  int y;
  void (*RGBColorTableRow)(uint8* dst_argb, const uint8* table_argb,
                           int width) = RGBColorTableRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1584 1585 1586 1587
  if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
      dst_x < 0 || dst_y < 0) {
    return -1;
  }
1588
  // Coalesce rows.
1589
  if (dst_stride_argb == width * 4) {
1590 1591 1592
    width *= height;
    height = 1;
    dst_stride_argb = 0;
1593 1594 1595 1596 1597 1598
  }
#if defined(HAS_RGBCOLORTABLEROW_X86)
  if (TestCpuFlag(kCpuHasX86)) {
    RGBColorTableRow = RGBColorTableRow_X86;
  }
#endif
1599
  for (y = 0; y < height; ++y) {
1600 1601 1602 1603 1604 1605
    RGBColorTableRow(dst, table_argb, width);
    dst += dst_stride_argb;
  }
  return 0;
}

1606 1607 1608 1609
// ARGBQuantize is used to posterize art.
// e.g. rgb / qvalue * qvalue + qvalue / 2
// But the low levels implement efficiently with 3 parameters, and could be
// used for other high level operations.
1610 1611
// dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
// where scale is 1 / interval_size as a fixed point value.
1612 1613 1614
// The divide is replaces with a multiply by reciprocal fixed point multiply.
// Caveat - although SSE2 saturates, the C function does not and should be used
// with care if doing anything but quantization.
1615
LIBYUV_API
1616 1617 1618
int ARGBQuantize(uint8* dst_argb, int dst_stride_argb,
                 int scale, int interval_size, int interval_offset,
                 int dst_x, int dst_y, int width, int height) {
1619 1620 1621 1622
  int y;
  void (*ARGBQuantizeRow)(uint8* dst_argb, int scale, int interval_size,
                          int interval_offset, int width) = ARGBQuantizeRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1623 1624 1625 1626
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 ||
      interval_size < 1 || interval_size > 255) {
    return -1;
  }
1627
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1628
  if (dst_stride_argb == width * 4) {
1629 1630 1631
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1632
  }
1633
#if defined(HAS_ARGBQUANTIZEROW_SSE2)
1634
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
1635 1636
    ARGBQuantizeRow = ARGBQuantizeRow_SSE2;
  }
1637 1638
#endif
#if defined(HAS_ARGBQUANTIZEROW_NEON)
fbarchard@google.com's avatar
fbarchard@google.com committed
1639 1640 1641
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBQuantizeRow = ARGBQuantizeRow_NEON;
  }
1642
#endif
1643
  for (y = 0; y < height; ++y) {
1644 1645 1646 1647 1648 1649
    ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width);
    dst += dst_stride_argb;
  }
  return 0;
}

fbarchard@google.com's avatar
fbarchard@google.com committed
1650
// Computes table of cumulative sum for image where the value is the sum
1651
// of all values above and to the left of the entry. Used by ARGBBlur.
1652
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1653 1654 1655
int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb,
                             int32* dst_cumsum, int dst_stride32_cumsum,
                             int width, int height) {
1656 1657 1658 1659
  int y;
  void (*ComputeCumulativeSumRow)(const uint8* row, int32* cumsum,
      const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C;
  int32* previous_cumsum = dst_cumsum;
fbarchard@google.com's avatar
fbarchard@google.com committed
1660 1661 1662
  if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) {
    return -1;
  }
1663
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
1664 1665 1666 1667
  if (TestCpuFlag(kCpuHasSSE2)) {
    ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
  }
#endif
1668
  memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4);  // 4 int per pixel.
1669
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1670 1671 1672 1673 1674 1675 1676 1677 1678
    ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width);
    previous_cumsum = dst_cumsum;
    dst_cumsum += dst_stride32_cumsum;
    src_argb += src_stride_argb;
  }
  return 0;
}

// Blur ARGB image.
1679
// Caller should allocate CumulativeSum table of width * height * 16 bytes
1680
// aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory
1681
// as the buffer is treated as circular.
1682
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1683 1684 1685 1686
int ARGBBlur(const uint8* src_argb, int src_stride_argb,
             uint8* dst_argb, int dst_stride_argb,
             int32* dst_cumsum, int dst_stride32_cumsum,
             int width, int height, int radius) {
1687 1688 1689 1690 1691 1692 1693 1694 1695
  int y;
  void (*ComputeCumulativeSumRow)(const uint8 *row, int32 *cumsum,
      const int32* previous_cumsum, int width) = ComputeCumulativeSumRow_C;
  void (*CumulativeSumToAverageRow)(const int32* topleft, const int32* botleft,
      int width, int area, uint8* dst, int count) = CumulativeSumToAverageRow_C;
  int32* cumsum_bot_row;
  int32* max_cumsum_bot_row;
  int32* cumsum_top_row;

1696 1697 1698
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
1699 1700 1701 1702 1703
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1704
  if (radius > height) {
1705
    radius = height;
1706
  }
1707 1708 1709 1710 1711
  if (radius > (width / 2 - 1)) {
    radius = width / 2 - 1;
  }
  if (radius <= 0) {
    return -1;
1712
  }
1713
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
1714
  if (TestCpuFlag(kCpuHasSSE2)) {
1715
    ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
1716
    CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
1717 1718
  }
#endif
1719
  // Compute enough CumulativeSum for first row to be blurred. After this
1720
  // one row of CumulativeSum is updated at a time.
fbarchard@google.com's avatar
fbarchard@google.com committed
1721 1722
  ARGBComputeCumulativeSum(src_argb, src_stride_argb,
                           dst_cumsum, dst_stride32_cumsum,
1723 1724 1725
                           width, radius);

  src_argb = src_argb + radius * src_stride_argb;
1726
  cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum];
1727

1728 1729
  max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum];
  cumsum_top_row = &dst_cumsum[0];
fbarchard@google.com's avatar
fbarchard@google.com committed
1730

1731
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1732 1733
    int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0;
    int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1);
1734
    int area = radius * (bot_y - top_y);
1735 1736 1737
    int boxwidth = radius * 4;
    int x;
    int n;
1738

1739
    // Increment cumsum_top_row pointer with circular buffer wrap around.
1740 1741 1742 1743 1744 1745
    if (top_y) {
      cumsum_top_row += dst_stride32_cumsum;
      if (cumsum_top_row >= max_cumsum_bot_row) {
        cumsum_top_row = dst_cumsum;
      }
    }
1746 1747
    // Increment cumsum_bot_row pointer with circular buffer wrap around and
    // then fill in a row of CumulativeSum.
1748
    if ((y + radius) < height) {
1749
      const int32* prev_cumsum_bot_row = cumsum_bot_row;
1750 1751 1752 1753 1754 1755 1756 1757
      cumsum_bot_row += dst_stride32_cumsum;
      if (cumsum_bot_row >= max_cumsum_bot_row) {
        cumsum_bot_row = dst_cumsum;
      }
      ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row,
                              width);
      src_argb += src_stride_argb;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
1758 1759 1760

    // Left clipped.
    for (x = 0; x < radius + 1; ++x) {
1761
      CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1762
                                boxwidth, area, &dst_argb[x * 4], 1);
fbarchard@google.com's avatar
fbarchard@google.com committed
1763 1764 1765 1766 1767
      area += (bot_y - top_y);
      boxwidth += 4;
    }

    // Middle unclipped.
1768
    n = (width - 1) - radius - x + 1;
1769
    CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1770
                              boxwidth, area, &dst_argb[x * 4], n);
fbarchard@google.com's avatar
fbarchard@google.com committed
1771 1772 1773 1774 1775

    // Right clipped.
    for (x += n; x <= width - 1; ++x) {
      area -= (bot_y - top_y);
      boxwidth -= 4;
1776
      CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4,
1777 1778
                                cumsum_bot_row + (x - radius - 1) * 4,
                                boxwidth, area, &dst_argb[x * 4], 1);
fbarchard@google.com's avatar
fbarchard@google.com committed
1779 1780 1781 1782 1783 1784
    }
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1785
// Multiply ARGB image by a specified ARGB value.
1786
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1787 1788 1789
int ARGBShade(const uint8* src_argb, int src_stride_argb,
              uint8* dst_argb, int dst_stride_argb,
              int width, int height, uint32 value) {
1790 1791 1792
  int y;
  void (*ARGBShadeRow)(const uint8* src_argb, uint8* dst_argb,
                       int width, uint32 value) = ARGBShadeRow_C;
1793 1794 1795 1796 1797 1798 1799 1800
  if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) {
    return -1;
  }
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1801
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1802 1803
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1804 1805 1806
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1807
  }
1808
#if defined(HAS_ARGBSHADEROW_SSE2)
1809
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
1810 1811
    ARGBShadeRow = ARGBShadeRow_SSE2;
  }
1812 1813
#endif
#if defined(HAS_ARGBSHADEROW_NEON)
1814 1815 1816
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBShadeRow = ARGBShadeRow_NEON;
  }
1817 1818
#endif

1819
  for (y = 0; y < height; ++y) {
1820 1821 1822 1823 1824
    ARGBShadeRow(src_argb, dst_argb, width, value);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1825 1826
}

1827
// Interpolate 2 ARGB images by specified amount (0 to 255).
1828
LIBYUV_API
1829 1830 1831 1832
int ARGBInterpolate(const uint8* src_argb0, int src_stride_argb0,
                    const uint8* src_argb1, int src_stride_argb1,
                    uint8* dst_argb, int dst_stride_argb,
                    int width, int height, int interpolation) {
1833 1834 1835 1836
  int y;
  void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr,
                         ptrdiff_t src_stride, int dst_width,
                         int source_y_fraction) = InterpolateRow_C;
1837 1838 1839 1840 1841 1842 1843 1844 1845
  if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    dst_argb = dst_argb + (height - 1) * dst_stride_argb;
    dst_stride_argb = -dst_stride_argb;
  }
1846
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1847 1848 1849
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
1850 1851 1852
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1853
  }
1854
#if defined(HAS_INTERPOLATEROW_SSE2)
1855
  if (TestCpuFlag(kCpuHasSSE2)) {
1856
    InterpolateRow = InterpolateRow_Any_SSE2;
1857
    if (IS_ALIGNED(width, 4)) {
1858
      InterpolateRow = InterpolateRow_SSE2;
1859
    }
1860 1861
  }
#endif
1862
#if defined(HAS_INTERPOLATEROW_SSSE3)
1863
  if (TestCpuFlag(kCpuHasSSSE3)) {
1864
    InterpolateRow = InterpolateRow_Any_SSSE3;
1865
    if (IS_ALIGNED(width, 4)) {
1866
      InterpolateRow = InterpolateRow_SSSE3;
1867
    }
1868
  }
1869
#endif
1870
#if defined(HAS_INTERPOLATEROW_AVX2)
1871
  if (TestCpuFlag(kCpuHasAVX2)) {
1872 1873 1874 1875 1876 1877
    InterpolateRow = InterpolateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      InterpolateRow = InterpolateRow_AVX2;
    }
  }
#endif
1878
#if defined(HAS_INTERPOLATEROW_NEON)
1879
  if (TestCpuFlag(kCpuHasNEON)) {
1880
    InterpolateRow = InterpolateRow_Any_NEON;
1881
    if (IS_ALIGNED(width, 4)) {
1882
      InterpolateRow = InterpolateRow_NEON;
1883
    }
1884
  }
1885
#endif
1886
#if defined(HAS_INTERPOLATEROW_MIPS_DSPR2)
1887
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) &&
1888 1889 1890
      IS_ALIGNED(src_argb0, 4) && IS_ALIGNED(src_stride_argb0, 4) &&
      IS_ALIGNED(src_argb1, 4) && IS_ALIGNED(src_stride_argb1, 4) &&
      IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) {
1891
    InterpolateRow = InterpolateRow_MIPS_DSPR2;
1892 1893 1894
  }
#endif

1895
  for (y = 0; y < height; ++y) {
1896 1897
    InterpolateRow(dst_argb, src_argb0, src_argb1 - src_argb0,
                   width * 4, interpolation);
1898 1899 1900 1901 1902 1903 1904
    src_argb0 += src_stride_argb0;
    src_argb1 += src_stride_argb1;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

fbarchard@google.com's avatar
fbarchard@google.com committed
1905 1906 1907 1908 1909
// Shuffle ARGB channel order.  e.g. BGRA to ARGB.
LIBYUV_API
int ARGBShuffle(const uint8* src_bgra, int src_stride_bgra,
                uint8* dst_argb, int dst_stride_argb,
                const uint8* shuffler, int width, int height) {
1910 1911 1912
  int y;
  void (*ARGBShuffleRow)(const uint8* src_bgra, uint8* dst_argb,
                         const uint8* shuffler, int pix) = ARGBShuffleRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
  if (!src_bgra || !dst_argb ||
      width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_bgra = src_bgra + (height - 1) * src_stride_bgra;
    src_stride_bgra = -src_stride_bgra;
  }
1923
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1924 1925
  if (src_stride_bgra == width * 4 &&
      dst_stride_argb == width * 4) {
1926 1927 1928
    width *= height;
    height = 1;
    src_stride_bgra = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1929
  }
1930
#if defined(HAS_ARGBSHUFFLEROW_SSE2)
1931
  if (TestCpuFlag(kCpuHasSSE2)) {
1932 1933 1934 1935 1936 1937
    ARGBShuffleRow = ARGBShuffleRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBShuffleRow = ARGBShuffleRow_SSE2;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1938
#if defined(HAS_ARGBSHUFFLEROW_SSSE3)
1939
  if (TestCpuFlag(kCpuHasSSSE3)) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1940 1941
    ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
1942
      ARGBShuffleRow = ARGBShuffleRow_SSSE3;
fbarchard@google.com's avatar
fbarchard@google.com committed
1943 1944 1945 1946
    }
  }
#endif
#if defined(HAS_ARGBSHUFFLEROW_AVX2)
1947
  if (TestCpuFlag(kCpuHasAVX2)) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1948 1949 1950 1951 1952 1953 1954
    ARGBShuffleRow = ARGBShuffleRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      ARGBShuffleRow = ARGBShuffleRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBSHUFFLEROW_NEON)
1955
  if (TestCpuFlag(kCpuHasNEON)) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1956 1957 1958 1959 1960 1961 1962
    ARGBShuffleRow = ARGBShuffleRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBShuffleRow = ARGBShuffleRow_NEON;
    }
  }
#endif

1963
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1964 1965 1966 1967 1968 1969 1970
    ARGBShuffleRow(src_bgra, dst_argb, shuffler, width);
    src_bgra += src_stride_bgra;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

fbarchard@google.com's avatar
fbarchard@google.com committed
1971
// Sobel ARGB effect.
1972 1973 1974 1975 1976 1977
static int ARGBSobelize(const uint8* src_argb, int src_stride_argb,
                        uint8* dst_argb, int dst_stride_argb,
                        int width, int height,
                        void (*SobelRow)(const uint8* src_sobelx,
                                         const uint8* src_sobely,
                                         uint8* dst, int width)) {
1978 1979 1980 1981 1982 1983 1984 1985
  int y;
  void (*ARGBToBayerRow)(const uint8* src_argb, uint8* dst_bayer,
                         uint32 selector, int pix) = ARGBToBayerGGRow_C;
  void (*SobelYRow)(const uint8* src_y0, const uint8* src_y1,
                    uint8* dst_sobely, int width) = SobelYRow_C;
  void (*SobelXRow)(const uint8* src_y0, const uint8* src_y1,
                    const uint8* src_y2, uint8* dst_sobely, int width) =
      SobelXRow_C;
1986
  const int kEdge = 16;  // Extra pixels at start of row for extrude/align.
1987
  if (!src_argb  || !dst_argb || width <= 0 || height == 0) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1988 1989 1990 1991 1992 1993 1994 1995
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb  = src_argb  + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1996
  // ARGBToBayer used to select G channel from ARGB.
1997
#if defined(HAS_ARGBTOBAYERGGROW_SSE2)
1998
  if (TestCpuFlag(kCpuHasSSE2)) {
1999 2000 2001 2002 2003 2004
    ARGBToBayerRow = ARGBToBayerGGRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      ARGBToBayerRow = ARGBToBayerGGRow_SSE2;
    }
  }
#endif
2005
#if defined(HAS_ARGBTOBAYERROW_SSSE3)
2006
  if (TestCpuFlag(kCpuHasSSSE3)) {
2007 2008 2009
    ARGBToBayerRow = ARGBToBayerRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      ARGBToBayerRow = ARGBToBayerRow_SSSE3;
fbarchard@google.com's avatar
fbarchard@google.com committed
2010 2011
    }
  }
2012 2013
#endif
#if defined(HAS_ARGBTOBAYERGGROW_NEON)
2014
  if (TestCpuFlag(kCpuHasNEON)) {
2015
    ARGBToBayerRow = ARGBToBayerGGRow_Any_NEON;
2016
    if (IS_ALIGNED(width, 8)) {
2017
      ARGBToBayerRow = ARGBToBayerGGRow_NEON;
fbarchard@google.com's avatar
fbarchard@google.com committed
2018 2019 2020
    }
  }
#endif
2021 2022 2023
#if defined(HAS_SOBELYROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelYRow = SobelYRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
2024
  }
2025 2026 2027 2028 2029
#endif
#if defined(HAS_SOBELYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelYRow = SobelYRow_NEON;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2030
#endif
2031 2032 2033
#if defined(HAS_SOBELXROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelXRow = SobelXRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
2034
  }
2035 2036 2037 2038 2039
#endif
#if defined(HAS_SOBELXROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelXRow = SobelXRow_NEON;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2040
#endif
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
  {
    // 3 rows with edges before/after.
    const int kRowSize = (width + kEdge + 15) & ~15;
    align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge));
    uint8* row_sobelx = rows;
    uint8* row_sobely = rows + kRowSize;
    uint8* row_y = rows + kRowSize * 2;

    // Convert first row.
    uint8* row_y0 = row_y + kEdge;
    uint8* row_y1 = row_y0 + kRowSize;
    uint8* row_y2 = row_y1 + kRowSize;
    ARGBToBayerRow(src_argb, row_y0, 0x0d090501, width);
    row_y0[-1] = row_y0[0];
    memset(row_y0 + width, row_y0[width - 1], 16);  // Extrude 16 for valgrind.
    ARGBToBayerRow(src_argb, row_y1, 0x0d090501, width);
    row_y1[-1] = row_y1[0];
    memset(row_y1 + width, row_y1[width - 1], 16);
    memset(row_y2 + width, 0, 16);

    for (y = 0; y < height; ++y) {
      // Convert next row of ARGB to Y.
      if (y < (height - 1)) {
        src_argb += src_stride_argb;
      }
      ARGBToBayerRow(src_argb, row_y2, 0x0d090501, width);
      row_y2[-1] = row_y2[0];
      row_y2[width] = row_y2[width - 1];

      SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width);
      SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width);
      SobelRow(row_sobelx, row_sobely, dst_argb, width);

      // Cycle thru circular queue of 3 row_y buffers.
      {
        uint8* row_yt = row_y0;
        row_y0 = row_y1;
        row_y1 = row_y2;
        row_y2 = row_yt;
      }
fbarchard@google.com's avatar
fbarchard@google.com committed
2081

2082 2083 2084
      dst_argb += dst_stride_argb;
    }
    free_aligned_buffer_64(rows);
fbarchard@google.com's avatar
fbarchard@google.com committed
2085 2086 2087 2088
  }
  return 0;
}

2089
// Sobel ARGB effect.
2090
LIBYUV_API
2091 2092 2093 2094 2095 2096
int ARGBSobel(const uint8* src_argb, int src_stride_argb,
              uint8* dst_argb, int dst_stride_argb,
              int width, int height) {
  void (*SobelRow)(const uint8* src_sobelx, const uint8* src_sobely,
                   uint8* dst_argb, int width) = SobelRow_C;
#if defined(HAS_SOBELROW_SSE2)
2097
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16)) {
2098
    SobelRow = SobelRow_SSE2;
2099
  }
2100
#endif
2101 2102 2103
#if defined(HAS_SOBELROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    SobelRow = SobelRow_NEON;
2104
  }
2105
#endif
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
  return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
                      width, height, SobelRow);
}

// Sobel ARGB effect with planar output.
LIBYUV_API
int ARGBSobelToPlane(const uint8* src_argb, int src_stride_argb,
                     uint8* dst_y, int dst_stride_y,
                     int width, int height) {
  void (*SobelToPlaneRow)(const uint8* src_sobelx, const uint8* src_sobely,
                          uint8* dst_, int width) = SobelToPlaneRow_C;
#if defined(HAS_SOBELTOPLANEROW_SSE2)
2118
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16)) {
2119
    SobelToPlaneRow = SobelToPlaneRow_SSE2;
2120
  }
2121
#endif
2122 2123 2124
#if defined(HAS_SOBELTOPLANEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) {
    SobelToPlaneRow = SobelToPlaneRow_NEON;
2125
  }
2126
#endif
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
  return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y,
                      width, height, SobelToPlaneRow);
}

// SobelXY ARGB effect.
// Similar to Sobel, but also stores Sobel X in R and Sobel Y in B.  G = Sobel.
LIBYUV_API
int ARGBSobelXY(const uint8* src_argb, int src_stride_argb,
                uint8* dst_argb, int dst_stride_argb,
                int width, int height) {
2137
  void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely,
2138
                     uint8* dst_argb, int width) = SobelXYRow_C;
2139
#if defined(HAS_SOBELXYROW_SSE2)
2140
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16)) {
2141 2142 2143
    SobelXYRow = SobelXYRow_SSE2;
  }
#endif
2144 2145 2146 2147 2148
#if defined(HAS_SOBELXYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    SobelXYRow = SobelXYRow_NEON;
  }
#endif
2149 2150
  return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
                      width, height, SobelXYRow);
2151 2152
}

2153 2154 2155 2156 2157 2158
// Apply a 4x4 polynomial to each ARGB pixel.
LIBYUV_API
int ARGBPolynomial(const uint8* src_argb, int src_stride_argb,
                   uint8* dst_argb, int dst_stride_argb,
                   const float* poly,
                   int width, int height) {
2159 2160 2161 2162
  int y;
  void (*ARGBPolynomialRow)(const uint8* src_argb,
                            uint8* dst_argb, const float* poly,
                            int width) = ARGBPolynomialRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
2163
  if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) {
2164 2165
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2166 2167 2168 2169 2170 2171
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb  = src_argb  + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
2172 2173 2174 2175 2176 2177
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2178 2179
  }
#if defined(HAS_ARGBPOLYNOMIALROW_SSE2)
2180
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) {
2181 2182
    ARGBPolynomialRow = ARGBPolynomialRow_SSE2;
  }
2183 2184
#endif
#if defined(HAS_ARGBPOLYNOMIALROW_AVX2)
2185 2186
  if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) &&
      IS_ALIGNED(width, 2)) {
2187 2188
    ARGBPolynomialRow = ARGBPolynomialRow_AVX2;
  }
2189
#endif
2190 2191

  for (y = 0; y < height; ++y) {
2192 2193 2194 2195 2196 2197 2198
    ARGBPolynomialRow(src_argb, dst_argb, poly, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2199 2200 2201 2202 2203 2204
// Apply a lumacolortable to each ARGB pixel.
LIBYUV_API
int ARGBLumaColorTable(const uint8* src_argb, int src_stride_argb,
                       uint8* dst_argb, int dst_stride_argb,
                       const uint8* luma,
                       int width, int height) {
2205 2206 2207 2208
  int y;
  void (*ARGBLumaColorTableRow)(const uint8* src_argb, uint8* dst_argb,
      int width, const uint8* luma, const uint32 lumacoeff) =
      ARGBLumaColorTableRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
2209
  if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) {
2210 2211
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2212 2213 2214 2215 2216 2217
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb  = src_argb  + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
2218 2219 2220 2221 2222 2223
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2224 2225
  }
#if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3)
2226
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) {
2227 2228 2229
    ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3;
  }
#endif
2230 2231

  for (y = 0; y < height; ++y) {
2232
    ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f);
2233 2234 2235 2236 2237 2238
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2239
// Copy Alpha from one ARGB image to another.
2240 2241 2242 2243
LIBYUV_API
int ARGBCopyAlpha(const uint8* src_argb, int src_stride_argb,
                  uint8* dst_argb, int dst_stride_argb,
                  int width, int height) {
2244 2245 2246
  int y;
  void (*ARGBCopyAlphaRow)(const uint8* src_argb, uint8* dst_argb, int width) =
      ARGBCopyAlphaRow_C;
2247
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
2248 2249 2250 2251 2252 2253 2254 2255
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
2256 2257 2258
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
2259 2260 2261
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2262 2263
  }
#if defined(HAS_ARGBCOPYALPHAROW_SSE2)
2264
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 8)) {
2265 2266
    ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2267 2268 2269 2270 2271
#endif
#if defined(HAS_ARGBCOPYALPHAROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) {
    ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2;
  }
2272
#endif
2273 2274

  for (y = 0; y < height; ++y) {
2275 2276 2277 2278 2279 2280 2281
    ARGBCopyAlphaRow(src_argb, dst_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2282 2283 2284 2285 2286
// Copy a planar Y channel to the alpha channel of a destination ARGB image.
LIBYUV_API
int ARGBCopyYToAlpha(const uint8* src_y, int src_stride_y,
                     uint8* dst_argb, int dst_stride_argb,
                     int width, int height) {
2287 2288 2289
  int y;
  void (*ARGBCopyYToAlphaRow)(const uint8* src_y, uint8* dst_argb, int width) =
      ARGBCopyYToAlphaRow_C;
2290 2291 2292 2293 2294 2295 2296 2297 2298
  if (!src_y || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_y = src_y + (height - 1) * src_stride_y;
    src_stride_y = -src_stride_y;
  }
2299 2300 2301
  // Coalesce rows.
  if (src_stride_y == width &&
      dst_stride_argb == width * 4) {
2302 2303 2304 2305 2306
    width *= height;
    height = 1;
    src_stride_y = dst_stride_argb = 0;
  }
#if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2)
2307
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 8)) {
2308 2309 2310 2311 2312 2313 2314 2315
    ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2;
  }
#endif
#if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) {
    ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2;
  }
#endif
2316 2317

  for (y = 0; y < height; ++y) {
2318 2319 2320 2321 2322 2323 2324
    ARGBCopyYToAlphaRow(src_y, dst_argb, width);
    src_y += src_stride_y;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2325 2326
#ifdef __cplusplus
}  // extern "C"
2327
}  // namespace libyuv
2328
#endif