planar_functions.cc 70.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 45
#if defined(HAS_COPYROW_X86)
  if (TestCpuFlag(kCpuHasX86) && IS_ALIGNED(width, 4)) {
46
    CopyRow = CopyRow_X86;
47
  }
48
#endif
49 50 51 52 53
#if defined(HAS_COPYROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32) &&
      IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) &&
      IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
    CopyRow = CopyRow_SSE2;
54
  }
55
#endif
56 57 58
#if defined(HAS_COPYROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_ERMS;
59 60 61 62 63 64 65
  }
#endif
#if defined(HAS_COPYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) {
    CopyRow = CopyRow_NEON;
  }
#endif
66 67 68 69 70
#if defined(HAS_COPYROW_MIPS)
  if (TestCpuFlag(kCpuHasMIPS)) {
    CopyRow = CopyRow_MIPS;
  }
#endif
71

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

80 81 82 83 84 85 86 87 88 89 90 91 92 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 120 121 122 123 124 125 126 127 128
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_X86)
  if (TestCpuFlag(kCpuHasX86) && IS_ALIGNED(width, 4)) {
    CopyRow = CopyRow_16_X86;
  }
#endif
#if defined(HAS_COPYROW_16_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32) &&
      IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) &&
      IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
    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;
  }
}

129 130 131 132 133 134 135 136 137
// 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) {
138
  int halfwidth = (width + 1) >> 1;
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 182 183 184 185 186 187 188 189 190
  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;
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
// 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;
}

209
// Convert I420 to I400.
210
LIBYUV_API
211
int I420ToI400(const uint8* src_y, int src_stride_y,
212 213
               const uint8* src_u, int src_stride_u,
               const uint8* src_v, int src_stride_v,
214 215
               uint8* dst_y, int dst_stride_y,
               int width, int height) {
216
  if (!src_y || !dst_y || width <= 0 || height == 0) {
217 218 219 220 221 222 223 224 225 226 227 228
    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;
}

229
// Mirror a plane of data.
230 231
void MirrorPlane(const uint8* src_y, int src_stride_y,
                 uint8* dst_y, int dst_stride_y,
232
                 int width, int height) {
233 234
  int y;
  void (*MirrorRow)(const uint8* src, uint8* dst, int width) = MirrorRow_C;
235 236 237 238 239 240
  // 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;
  }
241 242 243
#if defined(HAS_MIRRORROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) {
    MirrorRow = MirrorRow_NEON;
244
  }
245
#endif
246 247 248
#if defined(HAS_MIRRORROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16)) {
    MirrorRow = MirrorRow_SSE2;
249 250
  }
#endif
251
#if defined(HAS_MIRRORROW_SSSE3)
252 253 254 255 256
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16) &&
      IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) &&
      IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
    MirrorRow = MirrorRow_SSSE3;
  }
257
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
258 259 260 261 262
#if defined(HAS_MIRRORROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 32)) {
    MirrorRow = MirrorRow_AVX2;
  }
#endif
263

264
  // Mirror plane
265
  for (y = 0; y < height; ++y) {
266
    MirrorRow(src_y, dst_y, width);
267
    src_y += src_stride_y;
268
    dst_y += dst_stride_y;
269 270
  }
}
271

272 273 274 275 276 277 278
// 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) {
279 280 281 282 283 284
  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;
285 286 287 288 289 290
  // 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;
  }
291
  // Coalesce rows.
292
  if (src_stride_yuy2 == width * 2 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
293
      dst_stride_y == width &&
294 295
      dst_stride_u * 2 == width &&
      dst_stride_v * 2 == width) {
296 297 298
    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
299
  }
300
#if defined(HAS_YUY2TOYROW_SSE2)
301 302 303
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
    YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2;
    YUY2ToYRow = YUY2ToYRow_Any_SSE2;
304 305 306 307 308 309 310 311 312 313 314
    if (IS_ALIGNED(width, 16)) {
      YUY2ToUV422Row = YUY2ToUV422Row_Unaligned_SSE2;
      YUY2ToYRow = YUY2ToYRow_Unaligned_SSE2;
      if (IS_ALIGNED(src_yuy2, 16) && IS_ALIGNED(src_stride_yuy2, 16)) {
        YUY2ToUV422Row = YUY2ToUV422Row_SSE2;
        if (IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
          YUY2ToYRow = YUY2ToYRow_SSE2;
        }
      }
    }
  }
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
#endif
#if defined(HAS_YUY2TOYROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 32) {
    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)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    YUY2ToYRow = YUY2ToYRow_Any_NEON;
    if (width >= 16) {
      YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON;
331 332 333 334 335 336 337 338
    }
    if (IS_ALIGNED(width, 16)) {
      YUY2ToYRow = YUY2ToYRow_NEON;
      YUY2ToUV422Row = YUY2ToUV422Row_NEON;
    }
  }
#endif

339
  for (y = 0; y < height; ++y) {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    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) {
357 358 359 360 361 362
  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;
363 364 365 366 367 368
  // 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;
  }
369
  // Coalesce rows.
370
  if (src_stride_uyvy == width * 2 &&
fbarchard@google.com's avatar
fbarchard@google.com committed
371
      dst_stride_y == width &&
372 373
      dst_stride_u * 2 == width &&
      dst_stride_v * 2 == width) {
374 375 376
    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
377
  }
378
#if defined(HAS_UYVYTOYROW_SSE2)
379 380 381
  if (TestCpuFlag(kCpuHasSSE2) && width >= 16) {
    UYVYToUV422Row = UYVYToUV422Row_Any_SSE2;
    UYVYToYRow = UYVYToYRow_Any_SSE2;
382 383 384 385 386 387 388 389 390 391 392
    if (IS_ALIGNED(width, 16)) {
      UYVYToUV422Row = UYVYToUV422Row_Unaligned_SSE2;
      UYVYToYRow = UYVYToYRow_Unaligned_SSE2;
      if (IS_ALIGNED(src_uyvy, 16) && IS_ALIGNED(src_stride_uyvy, 16)) {
        UYVYToUV422Row = UYVYToUV422Row_SSE2;
        if (IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
          UYVYToYRow = UYVYToYRow_SSE2;
        }
      }
    }
  }
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#endif
#if defined(HAS_UYVYTOYROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 32) {
    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)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    UYVYToYRow = UYVYToYRow_Any_NEON;
    if (width >= 16) {
      UYVYToUV422Row = UYVYToUV422Row_Any_NEON;
409 410 411 412 413 414 415 416
    }
    if (IS_ALIGNED(width, 16)) {
      UYVYToYRow = UYVYToYRow_NEON;
      UYVYToUV422Row = UYVYToUV422Row_NEON;
    }
  }
#endif

417
  for (y = 0; y < height; ++y) {
418 419 420 421 422 423 424 425 426 427
    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;
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
// 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;
}

448
// Mirror I420 with optional flipping
449
LIBYUV_API
450 451 452 453 454 455 456
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) {
457 458
  int halfwidth = (width + 1) >> 1;
  int halfheight = (height + 1) >> 1;
459
  if (!src_y || !src_u || !src_v || !dst_y || !dst_u || !dst_v ||
460 461 462
      width <= 0 || height == 0) {
    return -1;
  }
463 464 465
  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
466
    halfheight = (height + 1) >> 1;
467 468 469 470 471 472
    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;
473
  }
474

475 476
  if (dst_y) {
    MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
477
  }
478 479
  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);
480 481
  return 0;
}
482

483
// ARGB mirror.
484
LIBYUV_API
485 486 487
int ARGBMirror(const uint8* src_argb, int src_stride_argb,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
488 489 490
  int y;
  void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
      ARGBMirrorRow_C;
491
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    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;
  }

#if defined(HAS_ARGBMIRRORROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBMirrorRow = ARGBMirrorRow_SSSE3;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
507 508 509 510 511 512 513
#endif
#if defined(HAS_ARGBMIRRORROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 8)) {
    ARGBMirrorRow = ARGBMirrorRow_AVX2;
  }
#endif
#if defined(HAS_ARGBMIRRORROW_NEON)
514 515 516
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 4)) {
    ARGBMirrorRow = ARGBMirrorRow_NEON;
  }
517 518 519
#endif

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

528 529 530
// 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.
531
LIBYUV_API
532
ARGBBlendRow GetARGBBlend() {
533 534
  void (*ARGBBlendRow)(const uint8* src_argb, const uint8* src_argb1,
                       uint8* dst_argb, int width) = ARGBBlendRow_C;
535
#if defined(HAS_ARGBBLENDROW_SSSE3)
536 537
  if (TestCpuFlag(kCpuHasSSSE3)) {
    ARGBBlendRow = ARGBBlendRow_SSSE3;
538 539 540
    return ARGBBlendRow;
  }
#endif
541
#if defined(HAS_ARGBBLENDROW_SSE2)
542 543
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBBlendRow = ARGBBlendRow_SSE2;
544
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
545 546 547 548 549
#endif
#if defined(HAS_ARGBBLENDROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBBlendRow = ARGBBlendRow_NEON;
  }
550
#endif
551
  return ARGBBlendRow;
552 553 554
}

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

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

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

  // Multiply plane
642
  for (y = 0; y < height; ++y) {
643 644 645 646 647 648 649 650
    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;
}

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

  // Add plane
708
  for (y = 0; y < height; ++y) {
709 710 711 712 713 714 715 716
    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;
}

717 718 719 720 721 722
// 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) {
723 724 725
  int y;
  void (*ARGBSubtractRow)(const uint8* src0, const uint8* src1, uint8* dst,
                          int width) = ARGBSubtractRow_C;
726 727 728 729 730 731 732 733 734
  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;
  }
735
  // Coalesce rows.
736 737 738
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
739 740 741
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
742
  }
743
#if defined(HAS_ARGBSUBTRACTROW_SSE2)
744
  if (TestCpuFlag(kCpuHasSSE2) && width >= 4) {
745 746 747 748 749
    ARGBSubtractRow = ARGBSubtractRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBSubtractRow = ARGBSubtractRow_SSE2;
    }
  }
750 751 752 753 754 755 756 757 758 759
#endif
#if defined(HAS_ARGBSUBTRACTROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 8) {
    ARGBSubtractRow = ARGBSubtractRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBSubtractRow = ARGBSubtractRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBSUBTRACTROW_NEON)
760 761 762 763 764 765 766 767 768
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    ARGBSubtractRow = ARGBSubtractRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBSubtractRow = ARGBSubtractRow_NEON;
    }
  }
#endif

  // Subtract plane
769
  for (y = 0; y < height; ++y) {
770 771 772 773 774 775 776 777
    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;
}

778
// Convert I422 to BGRA.
779
LIBYUV_API
780 781 782 783 784
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) {
785 786 787 788 789 790
  int y;
  void (*I422ToBGRARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToBGRARow_C;
791 792 793 794 795 796 797 798 799 800 801
  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;
  }
802
  // Coalesce rows.
803 804 805
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
806
      dst_stride_bgra == width * 4) {
807 808 809
    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
810
  }
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
#if defined(HAS_I422TOBGRAROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    I422ToBGRARow = I422ToBGRARow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToBGRARow = I422ToBGRARow_NEON;
    }
  }
#elif defined(HAS_I422TOBGRAROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToBGRARow = I422ToBGRARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToBGRARow = I422ToBGRARow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_bgra, 16) && IS_ALIGNED(dst_stride_bgra, 16)) {
        I422ToBGRARow = I422ToBGRARow_SSSE3;
      }
    }
  }
828 829 830 831 832 833 834 835
#elif defined(HAS_I422TOBGRAROW_MIPS_DSPR2)
  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;
  }
836 837
#endif

838
  for (y = 0; y < height; ++y) {
839 840 841 842 843 844 845 846 847 848
    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.
849
LIBYUV_API
850 851 852 853 854
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) {
855 856 857 858 859 860
  int y;
  void (*I422ToABGRRow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToABGRRow_C;
861 862 863 864 865 866 867 868 869 870 871
  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;
  }
872
  // Coalesce rows.
873 874 875
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
876
      dst_stride_abgr == width * 4) {
877 878 879
    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
880
  }
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
#if defined(HAS_I422TOABGRROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    I422ToABGRRow = I422ToABGRRow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToABGRRow = I422ToABGRRow_NEON;
    }
  }
#elif defined(HAS_I422TOABGRROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToABGRRow = I422ToABGRRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToABGRRow = I422ToABGRRow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_abgr, 16) && IS_ALIGNED(dst_stride_abgr, 16)) {
        I422ToABGRRow = I422ToABGRRow_SSSE3;
      }
    }
  }
#endif

900
  for (y = 0; y < height; ++y) {
901 902 903 904 905 906 907 908 909 910
    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.
911
LIBYUV_API
912 913 914 915 916
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) {
917 918 919 920 921 922
  int y;
  void (*I422ToRGBARow)(const uint8* y_buf,
                        const uint8* u_buf,
                        const uint8* v_buf,
                        uint8* rgb_buf,
                        int width) = I422ToRGBARow_C;
923 924 925 926 927 928 929 930 931 932 933
  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;
  }
934
  // Coalesce rows.
935 936 937
  if (src_stride_y == width &&
      src_stride_u * 2 == width &&
      src_stride_v * 2 == width &&
fbarchard@google.com's avatar
fbarchard@google.com committed
938
      dst_stride_rgba == width * 4) {
939 940 941
    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
942
  }
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
#if defined(HAS_I422TORGBAROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    I422ToRGBARow = I422ToRGBARow_Any_NEON;
    if (IS_ALIGNED(width, 16)) {
      I422ToRGBARow = I422ToRGBARow_NEON;
    }
  }
#elif defined(HAS_I422TORGBAROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    I422ToRGBARow = I422ToRGBARow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      I422ToRGBARow = I422ToRGBARow_Unaligned_SSSE3;
      if (IS_ALIGNED(dst_rgba, 16) && IS_ALIGNED(dst_stride_rgba, 16)) {
        I422ToRGBARow = I422ToRGBARow_SSSE3;
      }
    }
  }
#endif

962
  for (y = 0; y < height; ++y) {
963 964 965 966 967 968 969 970 971
    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;
}

972
// Convert NV12 to RGB565.
973
LIBYUV_API
974 975 976 977
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) {
978 979 980 981 982
  int y;
  void (*NV12ToRGB565Row)(const uint8* y_buf,
                          const uint8* uv_buf,
                          uint8* rgb_buf,
                          int width) = NV12ToRGB565Row_C;
983 984
  if (!src_y || !src_uv || !dst_rgb565 ||
      width <= 0 || height == 0) {
985 986
    return -1;
  }
987 988 989 990 991 992
  // 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;
  }
993
#if defined(HAS_NV12TORGB565ROW_SSSE3)
994
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
995
    NV12ToRGB565Row = NV12ToRGB565Row_Any_SSSE3;
996
    if (IS_ALIGNED(width, 8)) {
997
      NV12ToRGB565Row = NV12ToRGB565Row_SSSE3;
998
    }
999
  }
1000
#elif defined(HAS_NV12TORGB565ROW_NEON)
1001
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
1002
    NV12ToRGB565Row = NV12ToRGB565Row_Any_NEON;
1003
    if (IS_ALIGNED(width, 8)) {
1004
      NV12ToRGB565Row = NV12ToRGB565Row_NEON;
1005
    }
1006
  }
1007
#endif
1008

1009
  for (y = 0; y < height; ++y) {
1010
    NV12ToRGB565Row(src_y, src_uv, dst_rgb565, width);
1011 1012 1013
    dst_rgb565 += dst_stride_rgb565;
    src_y += src_stride_y;
    if (y & 1) {
1014 1015
      src_uv += src_stride_uv;
    }
1016 1017 1018 1019 1020
  }
  return 0;
}

// Convert NV21 to RGB565.
1021
LIBYUV_API
1022
int NV21ToRGB565(const uint8* src_y, int src_stride_y,
1023
                 const uint8* src_vu, int src_stride_vu,
1024 1025
                 uint8* dst_rgb565, int dst_stride_rgb565,
                 int width, int height) {
1026 1027 1028 1029 1030
  int y;
  void (*NV21ToRGB565Row)(const uint8* y_buf,
                          const uint8* src_vu,
                          uint8* rgb_buf,
                          int width) = NV21ToRGB565Row_C;
1031 1032
  if (!src_y || !src_vu || !dst_rgb565 ||
      width <= 0 || height == 0) {
1033 1034
    return -1;
  }
1035 1036 1037 1038 1039 1040
  // 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;
  }
1041
#if defined(HAS_NV21TORGB565ROW_SSSE3)
1042
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
1043
    NV21ToRGB565Row = NV21ToRGB565Row_Any_SSSE3;
1044
    if (IS_ALIGNED(width, 8)) {
1045
      NV21ToRGB565Row = NV21ToRGB565Row_SSSE3;
1046
    }
1047
  }
1048
#elif defined(HAS_NV21TORGB565ROW_NEON)
1049
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
1050
    NV21ToRGB565Row = NV21ToRGB565Row_Any_NEON;
1051
    if (IS_ALIGNED(width, 8)) {
1052
      NV21ToRGB565Row = NV21ToRGB565Row_NEON;
1053
    }
1054 1055 1056
  }
#endif

1057
  for (y = 0; y < height; ++y) {
1058
    NV21ToRGB565Row(src_y, src_vu, dst_rgb565, width);
1059
    dst_rgb565 += dst_stride_rgb565;
1060
    src_y += src_stride_y;
1061
    if (y & 1) {
1062
      src_vu += src_stride_vu;
1063
    }
1064 1065 1066 1067
  }
  return 0;
}

1068
LIBYUV_API
1069 1070 1071
void SetPlane(uint8* dst_y, int dst_stride_y,
              int width, int height,
              uint32 value) {
1072 1073 1074
  int y;
  uint32 v32 = value | (value << 8) | (value << 16) | (value << 24);
  void (*SetRow)(uint8* dst, uint32 value, int pix) = SetRow_C;
1075
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1076
  if (dst_stride_y == width) {
1077 1078 1079
    width *= height;
    height = 1;
    dst_stride_y = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1080
  }
1081 1082 1083 1084
#if defined(HAS_SETROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) &&
      IS_ALIGNED(width, 16) &&
      IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
1085
    SetRow = SetRow_NEON;
1086
  }
1087 1088 1089
#endif
#if defined(HAS_SETROW_X86)
  if (TestCpuFlag(kCpuHasX86) && IS_ALIGNED(width, 4)) {
1090
    SetRow = SetRow_X86;
1091 1092
  }
#endif
1093 1094

  // Set plane
1095
  for (y = 0; y < height; ++y) {
1096 1097 1098 1099 1100 1101
    SetRow(dst_y, v32, width);
    dst_y += dst_stride_y;
  }
}

// Draw a rectangle into I420
1102
LIBYUV_API
1103 1104 1105 1106 1107 1108
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) {
1109 1110 1111 1112 1113
  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);
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  if (!dst_y || !dst_u || !dst_v ||
      width <= 0 || height <= 0 ||
      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
1130
LIBYUV_API
1131
int ARGBRect(uint8* dst_argb, int dst_stride_argb,
1132
             int dst_x, int dst_y,
1133 1134 1135 1136
             int width, int height,
             uint32 value) {
  if (!dst_argb ||
      width <= 0 || height <= 0 ||
1137
      dst_x < 0 || dst_y < 0) {
1138 1139
    return -1;
  }
1140 1141
  dst_argb += dst_y * dst_stride_argb + dst_x * 4;
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1142
  if (dst_stride_argb == width * 4) {
1143 1144 1145
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1146
  }
1147
#if defined(HAS_SETROW_NEON)
1148
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16) &&
1149 1150
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBSetRows_NEON(dst_argb, value, width, dst_stride_argb, height);
1151 1152
    return 0;
  }
1153
#endif
1154 1155
#if defined(HAS_SETROW_X86)
  if (TestCpuFlag(kCpuHasX86)) {
1156
    ARGBSetRows_X86(dst_argb, value, width, dst_stride_argb, height);
1157 1158
    return 0;
  }
1159
#endif
1160
  ARGBSetRows_C(dst_argb, value, width, dst_stride_argb, height);
1161
  return 0;
1162 1163
}

1164
// Convert unattentuated ARGB to preattenuated ARGB.
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
// 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

1177
LIBYUV_API
1178 1179 1180
int ARGBAttenuate(const uint8* src_argb, int src_stride_argb,
                  uint8* dst_argb, int dst_stride_argb,
                  int width, int height) {
1181 1182 1183
  int y;
  void (*ARGBAttenuateRow)(const uint8* src_argb, uint8* dst_argb,
                           int width) = ARGBAttenuateRow_C;
1184 1185 1186
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
1187 1188 1189 1190 1191
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1192
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1193 1194
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1195 1196 1197
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1198
  }
1199 1200
#if defined(HAS_ARGBATTENUATEROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && width >= 4 &&
1201 1202
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
1203 1204 1205 1206
    ARGBAttenuateRow = ARGBAttenuateRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBAttenuateRow = ARGBAttenuateRow_SSE2;
    }
1207 1208
  }
#endif
1209
#if defined(HAS_ARGBATTENUATEROW_SSSE3)
1210
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 4) {
1211 1212 1213 1214 1215 1216
    ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3;
    if (IS_ALIGNED(width, 4)) {
      ARGBAttenuateRow = ARGBAttenuateRow_SSSE3;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1217
#if defined(HAS_ARGBATTENUATEROW_AVX2)
1218 1219 1220 1221 1222
  if (TestCpuFlag(kCpuHasAVX2) && width >= 8) {
    ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBAttenuateRow = ARGBAttenuateRow_AVX2;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
1223 1224
  }
#endif
1225 1226 1227 1228 1229 1230
#if defined(HAS_ARGBATTENUATEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
    ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON;
    if (IS_ALIGNED(width, 8)) {
      ARGBAttenuateRow = ARGBAttenuateRow_NEON;
    }
1231 1232
  }
#endif
1233

1234
  for (y = 0; y < height; ++y) {
1235
    ARGBAttenuateRow(src_argb, dst_argb, width);
1236 1237 1238 1239 1240 1241
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1242
// Convert preattentuated ARGB to unattenuated ARGB.
1243
LIBYUV_API
1244 1245 1246
int ARGBUnattenuate(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 (*ARGBUnattenuateRow)(const uint8* src_argb, uint8* dst_argb,
                             int width) = ARGBUnattenuateRow_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_ARGBUNATTENUATEROW_SSE2)
1266
  if (TestCpuFlag(kCpuHasSSE2) && width >= 4) {
1267 1268 1269 1270
    ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2;
    }
1271 1272
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1273
#if defined(HAS_ARGBUNATTENUATEROW_AVX2)
1274 1275 1276 1277 1278
  if (TestCpuFlag(kCpuHasAVX2) && width >= 8) {
    ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2;
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
1279 1280 1281
  }
#endif
// TODO(fbarchard): Neon version.
1282

1283
  for (y = 0; y < height; ++y) {
1284
    ARGBUnattenuateRow(src_argb, dst_argb, width);
1285 1286 1287 1288 1289 1290
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1291
// Convert ARGB to Grayed ARGB.
1292
LIBYUV_API
1293 1294 1295
int ARGBGrayTo(const uint8* src_argb, int src_stride_argb,
               uint8* dst_argb, int dst_stride_argb,
               int width, int height) {
1296 1297 1298
  int y;
  void (*ARGBGrayRow)(const uint8* src_argb, uint8* dst_argb,
                      int width) = ARGBGrayRow_C;
1299 1300 1301 1302 1303 1304 1305 1306
  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;
  }
1307
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1308 1309
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1310 1311 1312
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1313
  }
1314 1315 1316 1317 1318 1319
#if defined(HAS_ARGBGRAYROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBGrayRow = ARGBGrayRow_SSSE3;
  }
1320 1321 1322 1323
#elif defined(HAS_ARGBGRAYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBGrayRow = ARGBGrayRow_NEON;
  }
1324 1325
#endif

1326
  for (y = 0; y < height; ++y) {
1327 1328 1329 1330 1331 1332 1333
    ARGBGrayRow(src_argb, dst_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1334
// Make a rectangle of ARGB gray scale.
1335
LIBYUV_API
1336 1337 1338
int ARGBGray(uint8* dst_argb, int dst_stride_argb,
             int dst_x, int dst_y,
             int width, int height) {
1339 1340 1341 1342
  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;
1343 1344 1345
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
    return -1;
  }
1346
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1347
  if (dst_stride_argb == width * 4) {
1348 1349 1350
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1351
  }
1352 1353 1354 1355 1356
#if defined(HAS_ARGBGRAYROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBGrayRow = ARGBGrayRow_SSSE3;
  }
1357 1358 1359 1360
#elif defined(HAS_ARGBGRAYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBGrayRow = ARGBGrayRow_NEON;
  }
1361
#endif
1362
  for (y = 0; y < height; ++y) {
1363
    ARGBGrayRow(dst, dst, width);
1364 1365 1366 1367 1368
    dst += dst_stride_argb;
  }
  return 0;
}

1369
// Make a rectangle of ARGB Sepia tone.
1370
LIBYUV_API
1371
int ARGBSepia(uint8* dst_argb, int dst_stride_argb,
1372
              int dst_x, int dst_y, int width, int height) {
1373 1374 1375
  int y;
  void (*ARGBSepiaRow)(uint8* dst_argb, int width) = ARGBSepiaRow_C;
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1376 1377 1378
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
    return -1;
  }
1379
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1380
  if (dst_stride_argb == width * 4) {
1381 1382 1383
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1384
  }
1385 1386 1387 1388 1389
#if defined(HAS_ARGBSEPIAROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBSepiaRow = ARGBSepiaRow_SSSE3;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
1390 1391 1392 1393
#elif defined(HAS_ARGBSEPIAROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBSepiaRow = ARGBSepiaRow_NEON;
  }
1394
#endif
1395
  for (y = 0; y < height; ++y) {
1396 1397 1398 1399 1400 1401
    ARGBSepiaRow(dst, width);
    dst += dst_stride_argb;
  }
  return 0;
}

1402 1403
// Apply a 4x4 matrix to each ARGB pixel.
// Note: Normally for shading, but can be used to swizzle or invert.
1404
LIBYUV_API
1405 1406
int ARGBColorMatrix(const uint8* src_argb, int src_stride_argb,
                    uint8* dst_argb, int dst_stride_argb,
1407
                    const int8* matrix_argb,
1408
                    int width, int height) {
1409 1410 1411
  int y;
  void (*ARGBColorMatrixRow)(const uint8* src_argb, uint8* dst_argb,
      const int8* matrix_argb, int width) = ARGBColorMatrixRow_C;
1412
  if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) {
1413 1414
    return -1;
  }
1415 1416 1417 1418 1419
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1420
  // Coalesce rows.
1421 1422
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1423 1424 1425
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1426
  }
1427 1428 1429 1430 1431
#if defined(HAS_ARGBCOLORMATRIXROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3;
  }
1432 1433 1434 1435
#elif defined(HAS_ARGBCOLORMATRIXROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBColorMatrixRow = ARGBColorMatrixRow_NEON;
  }
1436
#endif
1437
  for (y = 0; y < height; ++y) {
1438 1439 1440
    ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
1441 1442 1443
  }
  return 0;
}
1444

1445 1446
// Apply a 4x3 matrix to each ARGB pixel.
// Deprecated.
1447
LIBYUV_API
1448 1449 1450
int RGBColorMatrix(uint8* dst_argb, int dst_stride_argb,
                   const int8* matrix_rgb,
                   int dst_x, int dst_y, int width, int height) {
1451 1452
  SIMD_ALIGNED(int8 matrix_argb[16]);
  uint8* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
  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

1474
  return ARGBColorMatrix((const uint8*)(dst), dst_stride_argb,
1475 1476 1477 1478
                         dst, dst_stride_argb,
                         &matrix_argb[0], width, height);
}

1479 1480
// Apply a color table each ARGB pixel.
// Table contains 256 ARGB values.
1481
LIBYUV_API
1482 1483 1484
int ARGBColorTable(uint8* dst_argb, int dst_stride_argb,
                   const uint8* table_argb,
                   int dst_x, int dst_y, int width, int height) {
1485 1486 1487 1488
  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;
1489 1490
  if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
      dst_x < 0 || dst_y < 0) {
1491 1492
    return -1;
  }
1493
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1494
  if (dst_stride_argb == width * 4) {
1495 1496 1497
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1498
  }
1499 1500 1501 1502 1503
#if defined(HAS_ARGBCOLORTABLEROW_X86)
  if (TestCpuFlag(kCpuHasX86)) {
    ARGBColorTableRow = ARGBColorTableRow_X86;
  }
#endif
1504
  for (y = 0; y < height; ++y) {
1505 1506 1507 1508 1509
    ARGBColorTableRow(dst, table_argb, width);
    dst += dst_stride_argb;
  }
  return 0;
}
1510

1511 1512 1513 1514 1515 1516
// 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) {
1517 1518 1519 1520
  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;
1521 1522 1523 1524
  if (!dst_argb || !table_argb || width <= 0 || height <= 0 ||
      dst_x < 0 || dst_y < 0) {
    return -1;
  }
1525
  // Coalesce rows.
1526
  if (dst_stride_argb == width * 4) {
1527 1528 1529
    width *= height;
    height = 1;
    dst_stride_argb = 0;
1530 1531 1532 1533 1534 1535
  }
#if defined(HAS_RGBCOLORTABLEROW_X86)
  if (TestCpuFlag(kCpuHasX86)) {
    RGBColorTableRow = RGBColorTableRow_X86;
  }
#endif
1536
  for (y = 0; y < height; ++y) {
1537 1538 1539 1540 1541 1542
    RGBColorTableRow(dst, table_argb, width);
    dst += dst_stride_argb;
  }
  return 0;
}

1543 1544 1545 1546
// 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.
1547 1548
// dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
// where scale is 1 / interval_size as a fixed point value.
1549 1550 1551
// 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.
1552
LIBYUV_API
1553 1554 1555
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) {
1556 1557 1558 1559
  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;
1560 1561 1562 1563
  if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 ||
      interval_size < 1 || interval_size > 255) {
    return -1;
  }
1564
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1565
  if (dst_stride_argb == width * 4) {
1566 1567 1568
    width *= height;
    height = 1;
    dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1569
  }
1570 1571 1572 1573 1574
#if defined(HAS_ARGBQUANTIZEROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBQuantizeRow = ARGBQuantizeRow_SSE2;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
1575 1576 1577 1578
#elif defined(HAS_ARGBQUANTIZEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBQuantizeRow = ARGBQuantizeRow_NEON;
  }
1579
#endif
1580
  for (y = 0; y < height; ++y) {
1581 1582 1583 1584 1585 1586
    ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width);
    dst += dst_stride_argb;
  }
  return 0;
}

fbarchard@google.com's avatar
fbarchard@google.com committed
1587
// Computes table of cumulative sum for image where the value is the sum
1588
// of all values above and to the left of the entry. Used by ARGBBlur.
1589
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1590 1591 1592
int ARGBComputeCumulativeSum(const uint8* src_argb, int src_stride_argb,
                             int32* dst_cumsum, int dst_stride32_cumsum,
                             int width, int height) {
1593 1594 1595 1596
  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
1597 1598 1599
  if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) {
    return -1;
  }
1600
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
1601 1602 1603 1604
  if (TestCpuFlag(kCpuHasSSE2)) {
    ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
  }
#endif
1605
  memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4);  // 4 int per pixel.
1606
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1607 1608 1609 1610 1611 1612 1613 1614 1615
    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.
1616
// Caller should allocate CumulativeSum table of width * height * 16 bytes
1617
// aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory
1618
// as the buffer is treated as circular.
1619
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1620 1621 1622 1623
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) {
1624 1625 1626 1627 1628 1629 1630 1631 1632
  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;

1633 1634 1635
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
    return -1;
  }
1636 1637 1638 1639 1640
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }
1641
  if (radius > height) {
1642
    radius = height;
1643
  }
1644 1645 1646 1647 1648
  if (radius > (width / 2 - 1)) {
    radius = width / 2 - 1;
  }
  if (radius <= 0) {
    return -1;
1649
  }
1650
#if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
1651
  if (TestCpuFlag(kCpuHasSSE2)) {
1652
    ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
1653
    CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
1654 1655
  }
#endif
1656
  // Compute enough CumulativeSum for first row to be blurred. After this
1657
  // one row of CumulativeSum is updated at a time.
fbarchard@google.com's avatar
fbarchard@google.com committed
1658 1659
  ARGBComputeCumulativeSum(src_argb, src_stride_argb,
                           dst_cumsum, dst_stride32_cumsum,
1660 1661 1662
                           width, radius);

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

1665 1666
  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
1667

1668
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1669 1670
    int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0;
    int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1);
1671
    int area = radius * (bot_y - top_y);
1672 1673 1674
    int boxwidth = radius * 4;
    int x;
    int n;
1675

1676
    // Increment cumsum_top_row pointer with circular buffer wrap around.
1677 1678 1679 1680 1681 1682
    if (top_y) {
      cumsum_top_row += dst_stride32_cumsum;
      if (cumsum_top_row >= max_cumsum_bot_row) {
        cumsum_top_row = dst_cumsum;
      }
    }
1683 1684
    // Increment cumsum_bot_row pointer with circular buffer wrap around and
    // then fill in a row of CumulativeSum.
1685
    if ((y + radius) < height) {
1686
      const int32* prev_cumsum_bot_row = cumsum_bot_row;
1687 1688 1689 1690 1691 1692 1693 1694
      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
1695 1696 1697

    // Left clipped.
    for (x = 0; x < radius + 1; ++x) {
1698
      CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1699
                                boxwidth, area, &dst_argb[x * 4], 1);
fbarchard@google.com's avatar
fbarchard@google.com committed
1700 1701 1702 1703 1704
      area += (bot_y - top_y);
      boxwidth += 4;
    }

    // Middle unclipped.
1705
    n = (width - 1) - radius - x + 1;
1706
    CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row,
1707
                              boxwidth, area, &dst_argb[x * 4], n);
fbarchard@google.com's avatar
fbarchard@google.com committed
1708 1709 1710 1711 1712

    // Right clipped.
    for (x += n; x <= width - 1; ++x) {
      area -= (bot_y - top_y);
      boxwidth -= 4;
1713
      CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4,
1714 1715
                                cumsum_bot_row + (x - radius - 1) * 4,
                                boxwidth, area, &dst_argb[x * 4], 1);
fbarchard@google.com's avatar
fbarchard@google.com committed
1716 1717 1718 1719 1720 1721
    }
    dst_argb += dst_stride_argb;
  }
  return 0;
}

1722
// Multiply ARGB image by a specified ARGB value.
1723
LIBYUV_API
fbarchard@google.com's avatar
fbarchard@google.com committed
1724 1725 1726
int ARGBShade(const uint8* src_argb, int src_stride_argb,
              uint8* dst_argb, int dst_stride_argb,
              int width, int height, uint32 value) {
1727 1728 1729
  int y;
  void (*ARGBShadeRow)(const uint8* src_argb, uint8* dst_argb,
                       int width, uint32 value) = ARGBShadeRow_C;
1730 1731 1732 1733 1734 1735 1736 1737
  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;
  }
1738
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1739 1740
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
1741 1742 1743
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1744
  }
1745
#if defined(HAS_ARGBSHADEROW_SSE2)
1746 1747 1748 1749 1750
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4) &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    ARGBShadeRow = ARGBShadeRow_SSE2;
  }
1751 1752 1753 1754
#elif defined(HAS_ARGBSHADEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    ARGBShadeRow = ARGBShadeRow_NEON;
  }
1755 1756
#endif

1757
  for (y = 0; y < height; ++y) {
1758 1759 1760 1761 1762
    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
1763 1764
}

1765
// Interpolate 2 ARGB images by specified amount (0 to 255).
1766
LIBYUV_API
1767 1768 1769 1770
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) {
1771 1772 1773 1774
  int y;
  void (*InterpolateRow)(uint8* dst_ptr, const uint8* src_ptr,
                         ptrdiff_t src_stride, int dst_width,
                         int source_y_fraction) = InterpolateRow_C;
1775 1776 1777 1778 1779 1780 1781 1782 1783
  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;
  }
1784
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1785 1786 1787
  if (src_stride_argb0 == width * 4 &&
      src_stride_argb1 == width * 4 &&
      dst_stride_argb == width * 4) {
1788 1789 1790
    width *= height;
    height = 1;
    src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1791
  }
1792
#if defined(HAS_INTERPOLATEROW_SSE2)
1793
  if (TestCpuFlag(kCpuHasSSE2) && width >= 4) {
1794
    InterpolateRow = InterpolateRow_Any_SSE2;
1795
    if (IS_ALIGNED(width, 4)) {
1796
      InterpolateRow = InterpolateRow_Unaligned_SSE2;
1797 1798 1799
      if (IS_ALIGNED(src_argb0, 16) && IS_ALIGNED(src_stride_argb0, 16) &&
          IS_ALIGNED(src_argb1, 16) && IS_ALIGNED(src_stride_argb1, 16) &&
          IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
1800
        InterpolateRow = InterpolateRow_SSE2;
1801 1802
      }
    }
1803 1804
  }
#endif
1805
#if defined(HAS_INTERPOLATEROW_SSSE3)
1806
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 4) {
1807
    InterpolateRow = InterpolateRow_Any_SSSE3;
1808
    if (IS_ALIGNED(width, 4)) {
1809
      InterpolateRow = InterpolateRow_Unaligned_SSSE3;
1810 1811 1812
      if (IS_ALIGNED(src_argb0, 16) && IS_ALIGNED(src_stride_argb0, 16) &&
          IS_ALIGNED(src_argb1, 16) && IS_ALIGNED(src_stride_argb1, 16) &&
          IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
1813
        InterpolateRow = InterpolateRow_SSSE3;
1814 1815
      }
    }
1816
  }
1817
#endif
1818 1819 1820 1821 1822 1823 1824 1825
#if defined(HAS_INTERPOLATEROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 8) {
    InterpolateRow = InterpolateRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      InterpolateRow = InterpolateRow_AVX2;
    }
  }
#endif
1826
#if defined(HAS_INTERPOLATEROW_NEON)
1827
  if (TestCpuFlag(kCpuHasNEON) && width >= 4) {
1828
    InterpolateRow = InterpolateRow_Any_NEON;
1829
    if (IS_ALIGNED(width, 4)) {
1830
      InterpolateRow = InterpolateRow_NEON;
1831
    }
1832
  }
1833
#endif
1834 1835 1836 1837 1838 1839 1840 1841 1842
#if defined(HAS_INTERPOLATEROWS_MIPS_DSPR2)
  if (TestCpuFlag(kCpuHasMIPS_DSPR2) && width >= 1 &&
      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)) {
    ScaleARGBFilterRows = InterpolateRow_MIPS_DSPR2;
  }
#endif

1843
  for (y = 0; y < height; ++y) {
1844 1845
    InterpolateRow(dst_argb, src_argb0, src_argb1 - src_argb0,
                   width * 4, interpolation);
1846 1847 1848 1849 1850 1851 1852
    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
1853 1854 1855 1856 1857
// 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) {
1858 1859 1860
  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
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
  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;
  }
1871
  // Coalesce rows.
fbarchard@google.com's avatar
fbarchard@google.com committed
1872 1873
  if (src_stride_bgra == width * 4 &&
      dst_stride_argb == width * 4) {
1874 1875 1876
    width *= height;
    height = 1;
    src_stride_bgra = dst_stride_argb = 0;
fbarchard@google.com's avatar
fbarchard@google.com committed
1877
  }
1878 1879 1880 1881 1882 1883 1884 1885
#if defined(HAS_ARGBSHUFFLEROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && width >= 4) {
    ARGBShuffleRow = ARGBShuffleRow_Any_SSE2;
    if (IS_ALIGNED(width, 4)) {
      ARGBShuffleRow = ARGBShuffleRow_SSE2;
    }
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
#if defined(HAS_ARGBSHUFFLEROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8) {
    ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      ARGBShuffleRow = ARGBShuffleRow_Unaligned_SSSE3;
      if (IS_ALIGNED(src_bgra, 16) && IS_ALIGNED(src_stride_bgra, 16) &&
          IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
        ARGBShuffleRow = ARGBShuffleRow_SSSE3;
      }
    }
  }
#endif
#if defined(HAS_ARGBSHUFFLEROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && width >= 16) {
    ARGBShuffleRow = ARGBShuffleRow_Any_AVX2;
    if (IS_ALIGNED(width, 16)) {
      ARGBShuffleRow = ARGBShuffleRow_AVX2;
    }
  }
#endif
#if defined(HAS_ARGBSHUFFLEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && width >= 4) {
    ARGBShuffleRow = ARGBShuffleRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBShuffleRow = ARGBShuffleRow_NEON;
    }
  }
#endif

1915
  for (y = 0; y < height; ++y) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1916 1917 1918 1919 1920 1921 1922
    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
1923
// Sobel ARGB effect.
1924 1925 1926 1927 1928 1929
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)) {
1930 1931 1932 1933 1934 1935 1936 1937
  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;
1938
  const int kEdge = 16;  // Extra pixels at start of row for extrude/align.
1939
  if (!src_argb  || !dst_argb || width <= 0 || height == 0) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1940 1941 1942 1943 1944 1945 1946 1947
    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;
  }
1948
  // ARGBToBayer used to select G channel from ARGB.
1949 1950 1951 1952 1953 1954 1955 1956 1957
#if defined(HAS_ARGBTOBAYERGGROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && width >= 8 &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16)) {
    ARGBToBayerRow = ARGBToBayerGGRow_Any_SSE2;
    if (IS_ALIGNED(width, 8)) {
      ARGBToBayerRow = ARGBToBayerGGRow_SSE2;
    }
  }
#endif
1958 1959 1960 1961 1962 1963
#if defined(HAS_ARGBTOBAYERROW_SSSE3)
  if (TestCpuFlag(kCpuHasSSSE3) && width >= 8 &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16)) {
    ARGBToBayerRow = ARGBToBayerRow_Any_SSSE3;
    if (IS_ALIGNED(width, 8)) {
      ARGBToBayerRow = ARGBToBayerRow_SSSE3;
fbarchard@google.com's avatar
fbarchard@google.com committed
1964 1965
    }
  }
1966 1967
#endif
#if defined(HAS_ARGBTOBAYERGGROW_NEON)
1968
  if (TestCpuFlag(kCpuHasNEON) && width >= 8) {
1969
    ARGBToBayerRow = ARGBToBayerGGRow_Any_NEON;
1970
    if (IS_ALIGNED(width, 8)) {
1971
      ARGBToBayerRow = ARGBToBayerGGRow_NEON;
fbarchard@google.com's avatar
fbarchard@google.com committed
1972 1973 1974
    }
  }
#endif
1975 1976 1977
#if defined(HAS_SOBELYROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelYRow = SobelYRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
1978
  }
1979 1980 1981 1982 1983
#endif
#if defined(HAS_SOBELYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelYRow = SobelYRow_NEON;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
1984
#endif
1985 1986 1987
#if defined(HAS_SOBELXROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelXRow = SobelXRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
1988
  }
1989 1990 1991 1992 1993
#endif
#if defined(HAS_SOBELXROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelXRow = SobelXRow_NEON;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
1994
#endif
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
  {
    // 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
2035

2036 2037 2038
      dst_argb += dst_stride_argb;
    }
    free_aligned_buffer_64(rows);
fbarchard@google.com's avatar
fbarchard@google.com committed
2039 2040 2041 2042
  }
  return 0;
}

2043
// Sobel ARGB effect.
2044
LIBYUV_API
2045 2046 2047 2048 2049 2050 2051 2052 2053
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)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    SobelRow = SobelRow_SSE2;
2054
  }
2055
#endif
2056 2057 2058
#if defined(HAS_SOBELROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    SobelRow = SobelRow_NEON;
2059
  }
2060
#endif
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
  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)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) &&
      IS_ALIGNED(dst_y, 16) && IS_ALIGNED(dst_stride_y, 16)) {
    SobelToPlaneRow = SobelToPlaneRow_SSE2;
2076
  }
2077
#endif
2078 2079 2080
#if defined(HAS_SOBELTOPLANEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) {
    SobelToPlaneRow = SobelToPlaneRow_NEON;
2081
  }
2082
#endif
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
  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) {
2093
  void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely,
2094
                     uint8* dst_argb, int width) = SobelXYRow_C;
2095 2096 2097 2098 2099 2100
#if defined(HAS_SOBELXYROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16)) {
    SobelXYRow = SobelXYRow_SSE2;
  }
#endif
2101 2102 2103 2104 2105
#if defined(HAS_SOBELXYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
    SobelXYRow = SobelXYRow_NEON;
  }
#endif
2106 2107
  return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
                      width, height, SobelXYRow);
2108 2109
}

2110 2111 2112 2113 2114 2115
// 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) {
2116 2117 2118 2119
  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
2120
  if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) {
2121 2122
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2123 2124 2125 2126 2127 2128
  // 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;
  }
2129 2130 2131 2132 2133 2134
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2135 2136
  }
#if defined(HAS_ARGBPOLYNOMIALROW_SSE2)
2137
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) {
2138 2139
    ARGBPolynomialRow = ARGBPolynomialRow_SSE2;
  }
2140 2141
#endif
#if defined(HAS_ARGBPOLYNOMIALROW_AVX2)
2142 2143
  if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) &&
      IS_ALIGNED(width, 2)) {
2144 2145
    ARGBPolynomialRow = ARGBPolynomialRow_AVX2;
  }
2146
#endif
2147 2148

  for (y = 0; y < height; ++y) {
2149 2150 2151 2152 2153 2154 2155
    ARGBPolynomialRow(src_argb, dst_argb, poly, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2156 2157 2158 2159 2160 2161
// 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) {
2162 2163 2164 2165
  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
2166
  if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) {
2167 2168
    return -1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2169 2170 2171 2172 2173 2174
  // 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;
  }
2175 2176 2177 2178 2179 2180
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2181 2182
  }
#if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3)
2183
  if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) {
2184 2185 2186
    ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3;
  }
#endif
2187 2188

  for (y = 0; y < height; ++y) {
2189
    ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f);
2190 2191 2192 2193 2194 2195
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2196
// Copy Alpha from one ARGB image to another.
2197 2198 2199 2200
LIBYUV_API
int ARGBCopyAlpha(const uint8* src_argb, int src_stride_argb,
                  uint8* dst_argb, int dst_stride_argb,
                  int width, int height) {
2201 2202 2203
  int y;
  void (*ARGBCopyAlphaRow)(const uint8* src_argb, uint8* dst_argb, int width) =
      ARGBCopyAlphaRow_C;
2204
  if (!src_argb || !dst_argb || width <= 0 || height == 0) {
2205 2206 2207 2208 2209 2210 2211 2212
    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;
  }
2213 2214 2215
  // Coalesce rows.
  if (src_stride_argb == width * 4 &&
      dst_stride_argb == width * 4) {
2216 2217 2218
    width *= height;
    height = 1;
    src_stride_argb = dst_stride_argb = 0;
2219 2220
  }
#if defined(HAS_ARGBCOPYALPHAROW_SSE2)
fbarchard@google.com's avatar
fbarchard@google.com committed
2221 2222 2223 2224
  if (TestCpuFlag(kCpuHasSSE2) &&
      IS_ALIGNED(src_argb, 16) && IS_ALIGNED(src_stride_argb, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16) &&
      IS_ALIGNED(width, 8)) {
2225 2226
    ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
2227 2228 2229 2230 2231
#endif
#if defined(HAS_ARGBCOPYALPHAROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) {
    ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2;
  }
2232
#endif
2233 2234

  for (y = 0; y < height; ++y) {
2235 2236 2237 2238 2239 2240 2241
    ARGBCopyAlphaRow(src_argb, dst_argb, width);
    src_argb += src_stride_argb;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2242 2243 2244 2245 2246
// 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) {
2247 2248 2249
  int y;
  void (*ARGBCopyYToAlphaRow)(const uint8* src_y, uint8* dst_argb, int width) =
      ARGBCopyYToAlphaRow_C;
2250 2251 2252 2253 2254 2255 2256 2257 2258
  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;
  }
2259 2260 2261
  // Coalesce rows.
  if (src_stride_y == width &&
      dst_stride_argb == width * 4) {
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
    width *= height;
    height = 1;
    src_stride_y = dst_stride_argb = 0;
  }
#if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2) &&
      IS_ALIGNED(src_y, 16) && IS_ALIGNED(src_stride_y, 16) &&
      IS_ALIGNED(dst_argb, 16) && IS_ALIGNED(dst_stride_argb, 16) &&
      IS_ALIGNED(width, 8)) {
    ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2;
  }
#endif
#if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 16)) {
    ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2;
  }
#endif
2279 2280

  for (y = 0; y < height; ++y) {
2281 2282 2283 2284 2285 2286 2287
    ARGBCopyYToAlphaRow(src_y, dst_argb, width);
    src_y += src_stride_y;
    dst_argb += dst_stride_argb;
  }
  return 0;
}

2288 2289
#ifdef __cplusplus
}  // extern "C"
2290
}  // namespace libyuv
2291
#endif