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

#include <stdlib.h>
#include <time.h>

14 15
#include "libyuv/compare.h"
#include "libyuv/convert.h"
16
#include "libyuv/convert_argb.h"
17
#include "libyuv/convert_from.h"
18
#include "libyuv/convert_from_argb.h"
19
#include "libyuv/cpu_id.h"
20 21
#include "libyuv/planar_functions.h"
#include "libyuv/rotate.h"
fbarchard@google.com's avatar
fbarchard@google.com committed
22
#include "libyuv/row.h"  // For Sobel
23
#include "../unit_test/unit_test.h"
24 25 26

namespace libyuv {

27
TEST_F(LibYUVPlanarTest, TestAttenuate) {
28 29 30 31 32
  const int kSize = 1280 * 4;
  align_buffer_64(orig_pixels, kSize);
  align_buffer_64(atten_pixels, kSize);
  align_buffer_64(unatten_pixels, kSize);
  align_buffer_64(atten2_pixels, kSize);
33 34

  // Test unattenuation clamps
35 36 37 38
  orig_pixels[0 * 4 + 0] = 200u;
  orig_pixels[0 * 4 + 1] = 129u;
  orig_pixels[0 * 4 + 2] = 127u;
  orig_pixels[0 * 4 + 3] = 128u;
39
  // Test unattenuation transparent and opaque are unaffected
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  orig_pixels[1 * 4 + 0] = 16u;
  orig_pixels[1 * 4 + 1] = 64u;
  orig_pixels[1 * 4 + 2] = 192u;
  orig_pixels[1 * 4 + 3] = 0u;
  orig_pixels[2 * 4 + 0] = 16u;
  orig_pixels[2 * 4 + 1] = 64u;
  orig_pixels[2 * 4 + 2] = 192u;
  orig_pixels[2 * 4 + 3] = 255u;
  orig_pixels[3 * 4 + 0] = 16u;
  orig_pixels[3 * 4 + 1] = 64u;
  orig_pixels[3 * 4 + 2] = 192u;
  orig_pixels[3 * 4 + 3] = 128u;
  ARGBUnattenuate(orig_pixels, 0, unatten_pixels, 0, 4, 1);
  EXPECT_EQ(255u, unatten_pixels[0 * 4 + 0]);
  EXPECT_EQ(255u, unatten_pixels[0 * 4 + 1]);
  EXPECT_EQ(254u, unatten_pixels[0 * 4 + 2]);
  EXPECT_EQ(128u, unatten_pixels[0 * 4 + 3]);
  EXPECT_EQ(0u, unatten_pixels[1 * 4 + 0]);
  EXPECT_EQ(0u, unatten_pixels[1 * 4 + 1]);
  EXPECT_EQ(0u, unatten_pixels[1 * 4 + 2]);
  EXPECT_EQ(0u, unatten_pixels[1 * 4 + 3]);
  EXPECT_EQ(16u, unatten_pixels[2 * 4 + 0]);
  EXPECT_EQ(64u, unatten_pixels[2 * 4 + 1]);
  EXPECT_EQ(192u, unatten_pixels[2 * 4 + 2]);
  EXPECT_EQ(255u, unatten_pixels[2 * 4 + 3]);
  EXPECT_EQ(32u, unatten_pixels[3 * 4 + 0]);
  EXPECT_EQ(128u, unatten_pixels[3 * 4 + 1]);
  EXPECT_EQ(255u, unatten_pixels[3 * 4 + 2]);
  EXPECT_EQ(128u, unatten_pixels[3 * 4 + 3]);
69

70
  for (int i = 0; i < 1280; ++i) {
71 72 73 74
    orig_pixels[i * 4 + 0] = i;
    orig_pixels[i * 4 + 1] = i / 2;
    orig_pixels[i * 4 + 2] = i / 3;
    orig_pixels[i * 4 + 3] = i;
75
  }
76 77
  ARGBAttenuate(orig_pixels, 0, atten_pixels, 0, 1280, 1);
  ARGBUnattenuate(atten_pixels, 0, unatten_pixels, 0, 1280, 1);
78
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
79
    ARGBAttenuate(unatten_pixels, 0, atten2_pixels, 0, 1280, 1);
80
  }
81
  for (int i = 0; i < 1280; ++i) {
82 83 84 85
    EXPECT_NEAR(atten_pixels[i * 4 + 0], atten2_pixels[i * 4 + 0], 2);
    EXPECT_NEAR(atten_pixels[i * 4 + 1], atten2_pixels[i * 4 + 1], 2);
    EXPECT_NEAR(atten_pixels[i * 4 + 2], atten2_pixels[i * 4 + 2], 2);
    EXPECT_NEAR(atten_pixels[i * 4 + 3], atten2_pixels[i * 4 + 3], 2);
86 87
  }
  // Make sure transparent, 50% and opaque are fully accurate.
88 89 90 91 92 93 94 95 96 97 98 99 100
  EXPECT_EQ(0, atten_pixels[0 * 4 + 0]);
  EXPECT_EQ(0, atten_pixels[0 * 4 + 1]);
  EXPECT_EQ(0, atten_pixels[0 * 4 + 2]);
  EXPECT_EQ(0, atten_pixels[0 * 4 + 3]);
  EXPECT_EQ(64, atten_pixels[128 * 4 + 0]);
  EXPECT_EQ(32, atten_pixels[128 * 4 + 1]);
  EXPECT_EQ(21,  atten_pixels[128 * 4 + 2]);
  EXPECT_EQ(128, atten_pixels[128 * 4 + 3]);
  EXPECT_NEAR(255, atten_pixels[255 * 4 + 0], 1);
  EXPECT_NEAR(127, atten_pixels[255 * 4 + 1], 1);
  EXPECT_NEAR(85,  atten_pixels[255 * 4 + 2], 1);
  EXPECT_EQ(255, atten_pixels[255 * 4 + 3]);

101 102 103 104
  free_aligned_buffer_64(atten2_pixels);
  free_aligned_buffer_64(unatten_pixels);
  free_aligned_buffer_64(atten_pixels);
  free_aligned_buffer_64(orig_pixels);
105
}
106

107
static int TestAttenuateI(int width, int height, int benchmark_iterations,
108 109
                          int disable_cpu_flags, int benchmark_cpu_info,
                          int invert, int off) {
110 111 112
  if (width < 1) {
    width = 1;
  }
113
  const int kBpp = 4;
114
  const int kStride = width * kBpp;
115 116 117 118
  align_buffer_64(src_argb, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
119
    src_argb[i + off] = (fastrand() & 0xff);
120 121 122 123
  }
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

124
  MaskCpuFlags(disable_cpu_flags);
125 126 127
  ARGBAttenuate(src_argb + off, kStride,
                dst_argb_c, kStride,
                width, invert * height);
128
  MaskCpuFlags(benchmark_cpu_info);
129 130 131 132 133 134 135 136 137 138 139 140 141 142
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBAttenuate(src_argb + off, kStride,
                  dst_argb_opt, kStride,
                  width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
143 144 145
  free_aligned_buffer_64(src_argb);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
146 147 148
  return max_diff;
}

149
TEST_F(LibYUVPlanarTest, ARGBAttenuate_Any) {
150
  int max_diff = TestAttenuateI(benchmark_width_ - 1, benchmark_height_,
151 152
                                benchmark_iterations_,
                                disable_cpu_flags_, benchmark_cpu_info_,
153
                                +1, 0);
154 155 156
  EXPECT_LE(max_diff, 2);
}

157
TEST_F(LibYUVPlanarTest, ARGBAttenuate_Unaligned) {
158
  int max_diff = TestAttenuateI(benchmark_width_, benchmark_height_,
159 160
                                benchmark_iterations_,
                                disable_cpu_flags_, benchmark_cpu_info_,
161
                                +1, 1);
162 163 164
  EXPECT_LE(max_diff, 2);
}

165
TEST_F(LibYUVPlanarTest, ARGBAttenuate_Invert) {
166
  int max_diff = TestAttenuateI(benchmark_width_, benchmark_height_,
167 168
                                benchmark_iterations_,
                                disable_cpu_flags_, benchmark_cpu_info_,
169
                                -1, 0);
170 171 172
  EXPECT_LE(max_diff, 2);
}

173
TEST_F(LibYUVPlanarTest, ARGBAttenuate_Opt) {
174
  int max_diff = TestAttenuateI(benchmark_width_, benchmark_height_,
175 176
                                benchmark_iterations_,
                                disable_cpu_flags_, benchmark_cpu_info_,
177
                                +1, 0);
fbarchard@google.com's avatar
fbarchard@google.com committed
178 179 180 181
  EXPECT_LE(max_diff, 2);
}

static int TestUnattenuateI(int width, int height, int benchmark_iterations,
182 183
                            int disable_cpu_flags, int benchmark_cpu_info,
                            int invert, int off) {
184 185 186
  if (width < 1) {
    width = 1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
187
  const int kBpp = 4;
188
  const int kStride = width * kBpp;
fbarchard@google.com's avatar
fbarchard@google.com committed
189 190 191 192
  align_buffer_64(src_argb, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
193
    src_argb[i + off] = (fastrand() & 0xff);
fbarchard@google.com's avatar
fbarchard@google.com committed
194 195 196 197 198 199 200
  }
  ARGBAttenuate(src_argb + off, kStride,
                src_argb + off, kStride,
                width, height);
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

201
  MaskCpuFlags(disable_cpu_flags);
fbarchard@google.com's avatar
fbarchard@google.com committed
202 203 204
  ARGBUnattenuate(src_argb + off, kStride,
                  dst_argb_c, kStride,
                  width, invert * height);
205
  MaskCpuFlags(benchmark_cpu_info);
fbarchard@google.com's avatar
fbarchard@google.com committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBUnattenuate(src_argb + off, kStride,
                    dst_argb_opt, kStride,
                    width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
220 221 222
  free_aligned_buffer_64(src_argb);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
fbarchard@google.com's avatar
fbarchard@google.com committed
223 224 225
  return max_diff;
}

226
TEST_F(LibYUVPlanarTest, ARGBUnattenuate_Any) {
fbarchard@google.com's avatar
fbarchard@google.com committed
227
  int max_diff = TestUnattenuateI(benchmark_width_ - 1, benchmark_height_,
228 229
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
230
                                  +1, 0);
fbarchard@google.com's avatar
fbarchard@google.com committed
231 232 233
  EXPECT_LE(max_diff, 2);
}

234
TEST_F(LibYUVPlanarTest, ARGBUnattenuate_Unaligned) {
fbarchard@google.com's avatar
fbarchard@google.com committed
235
  int max_diff = TestUnattenuateI(benchmark_width_, benchmark_height_,
236 237
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
238
                                  +1, 1);
fbarchard@google.com's avatar
fbarchard@google.com committed
239 240 241
  EXPECT_LE(max_diff, 2);
}

242
TEST_F(LibYUVPlanarTest, ARGBUnattenuate_Invert) {
fbarchard@google.com's avatar
fbarchard@google.com committed
243
  int max_diff = TestUnattenuateI(benchmark_width_, benchmark_height_,
244 245
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
246
                                  -1, 0);
fbarchard@google.com's avatar
fbarchard@google.com committed
247 248 249
  EXPECT_LE(max_diff, 2);
}

250
TEST_F(LibYUVPlanarTest, ARGBUnattenuate_Opt) {
fbarchard@google.com's avatar
fbarchard@google.com committed
251
  int max_diff = TestUnattenuateI(benchmark_width_, benchmark_height_,
252 253
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
254
                                  +1, 0);
255 256 257
  EXPECT_LE(max_diff, 2);
}

258
TEST_F(LibYUVPlanarTest, TestARGBComputeCumulativeSum) {
fbarchard@google.com's avatar
fbarchard@google.com committed
259 260
  SIMD_ALIGNED(uint8 orig_pixels[16][16][4]);
  SIMD_ALIGNED(int32 added_pixels[16][16][4]);
261

fbarchard@google.com's avatar
fbarchard@google.com committed
262 263 264 265 266 267 268
  for (int y = 0; y < 16; ++y) {
    for (int x = 0; x < 16; ++x) {
      orig_pixels[y][x][0] = 1u;
      orig_pixels[y][x][1] = 2u;
      orig_pixels[y][x][2] = 3u;
      orig_pixels[y][x][3] = 255u;
    }
269 270
  }

fbarchard@google.com's avatar
fbarchard@google.com committed
271 272 273
  ARGBComputeCumulativeSum(&orig_pixels[0][0][0], 16 * 4,
                           &added_pixels[0][0][0], 16 * 4,
                           16, 16);
274

fbarchard@google.com's avatar
fbarchard@google.com committed
275 276 277 278 279 280 281
  for (int y = 0; y < 16; ++y) {
    for (int x = 0; x < 16; ++x) {
      EXPECT_EQ((x + 1) * (y + 1), added_pixels[y][x][0]);
      EXPECT_EQ((x + 1) * (y + 1) * 2, added_pixels[y][x][1]);
      EXPECT_EQ((x + 1) * (y + 1) * 3, added_pixels[y][x][2]);
      EXPECT_EQ((x + 1) * (y + 1) * 255, added_pixels[y][x][3]);
    }
282 283
  }
}
284

285
TEST_F(LibYUVPlanarTest, TestARGBGray) {
286
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
287 288
  memset(orig_pixels, 0, sizeof(orig_pixels));

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
304 305 306 307 308 309 310 311 312 313
  // Test black
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 0u;
  orig_pixels[3][2] = 0u;
  orig_pixels[3][3] = 255u;
  // Test white
  orig_pixels[4][0] = 255u;
  orig_pixels[4][1] = 255u;
  orig_pixels[4][2] = 255u;
  orig_pixels[4][3] = 255u;
314
  // Test color
315 316 317 318
  orig_pixels[5][0] = 16u;
  orig_pixels[5][1] = 64u;
  orig_pixels[5][2] = 192u;
  orig_pixels[5][3] = 224u;
319 320
  // Do 16 to test asm version.
  ARGBGray(&orig_pixels[0][0], 0, 0, 0, 16, 1);
321 322 323
  EXPECT_EQ(30u, orig_pixels[0][0]);
  EXPECT_EQ(30u, orig_pixels[0][1]);
  EXPECT_EQ(30u, orig_pixels[0][2]);
324
  EXPECT_EQ(128u, orig_pixels[0][3]);
325 326 327
  EXPECT_EQ(149u, orig_pixels[1][0]);
  EXPECT_EQ(149u, orig_pixels[1][1]);
  EXPECT_EQ(149u, orig_pixels[1][2]);
328
  EXPECT_EQ(0u, orig_pixels[1][3]);
329 330 331
  EXPECT_EQ(76u, orig_pixels[2][0]);
  EXPECT_EQ(76u, orig_pixels[2][1]);
  EXPECT_EQ(76u, orig_pixels[2][2]);
332
  EXPECT_EQ(255u, orig_pixels[2][3]);
333 334 335 336 337 338 339 340 341 342 343 344
  EXPECT_EQ(0u, orig_pixels[3][0]);
  EXPECT_EQ(0u, orig_pixels[3][1]);
  EXPECT_EQ(0u, orig_pixels[3][2]);
  EXPECT_EQ(255u, orig_pixels[3][3]);
  EXPECT_EQ(255u, orig_pixels[4][0]);
  EXPECT_EQ(255u, orig_pixels[4][1]);
  EXPECT_EQ(255u, orig_pixels[4][2]);
  EXPECT_EQ(255u, orig_pixels[4][3]);
  EXPECT_EQ(96u, orig_pixels[5][0]);
  EXPECT_EQ(96u, orig_pixels[5][1]);
  EXPECT_EQ(96u, orig_pixels[5][2]);
  EXPECT_EQ(224u, orig_pixels[5][3]);
345
  for (int i = 0; i < 1280; ++i) {
346 347 348 349 350
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
351 352
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBGray(&orig_pixels[0][0], 0, 0, 0, 1280, 1);
353
  }
354
}
355

356
TEST_F(LibYUVPlanarTest, TestARGBGrayTo) {
357 358
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
  SIMD_ALIGNED(uint8 gray_pixels[1280][4]);
359 360
  memset(orig_pixels, 0, sizeof(orig_pixels));

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
376 377 378 379 380 381 382 383 384 385
  // Test black
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 0u;
  orig_pixels[3][2] = 0u;
  orig_pixels[3][3] = 255u;
  // Test white
  orig_pixels[4][0] = 255u;
  orig_pixels[4][1] = 255u;
  orig_pixels[4][2] = 255u;
  orig_pixels[4][3] = 255u;
386
  // Test color
387 388 389 390
  orig_pixels[5][0] = 16u;
  orig_pixels[5][1] = 64u;
  orig_pixels[5][2] = 192u;
  orig_pixels[5][3] = 224u;
391 392
  // Do 16 to test asm version.
  ARGBGrayTo(&orig_pixels[0][0], 0, &gray_pixels[0][0], 0, 16, 1);
393 394 395
  EXPECT_EQ(30u, gray_pixels[0][0]);
  EXPECT_EQ(30u, gray_pixels[0][1]);
  EXPECT_EQ(30u, gray_pixels[0][2]);
396
  EXPECT_EQ(128u, gray_pixels[0][3]);
397 398 399
  EXPECT_EQ(149u, gray_pixels[1][0]);
  EXPECT_EQ(149u, gray_pixels[1][1]);
  EXPECT_EQ(149u, gray_pixels[1][2]);
400
  EXPECT_EQ(0u, gray_pixels[1][3]);
401 402 403
  EXPECT_EQ(76u, gray_pixels[2][0]);
  EXPECT_EQ(76u, gray_pixels[2][1]);
  EXPECT_EQ(76u, gray_pixels[2][2]);
404
  EXPECT_EQ(255u, gray_pixels[2][3]);
405 406 407 408 409 410 411 412 413 414 415 416
  EXPECT_EQ(0u, gray_pixels[3][0]);
  EXPECT_EQ(0u, gray_pixels[3][1]);
  EXPECT_EQ(0u, gray_pixels[3][2]);
  EXPECT_EQ(255u, gray_pixels[3][3]);
  EXPECT_EQ(255u, gray_pixels[4][0]);
  EXPECT_EQ(255u, gray_pixels[4][1]);
  EXPECT_EQ(255u, gray_pixels[4][2]);
  EXPECT_EQ(255u, gray_pixels[4][3]);
  EXPECT_EQ(96u, gray_pixels[5][0]);
  EXPECT_EQ(96u, gray_pixels[5][1]);
  EXPECT_EQ(96u, gray_pixels[5][2]);
  EXPECT_EQ(224u, gray_pixels[5][3]);
417
  for (int i = 0; i < 1280; ++i) {
418 419 420 421 422
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
423 424
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBGrayTo(&orig_pixels[0][0], 0, &gray_pixels[0][0], 0, 1280, 1);
425 426 427
  }
}

428
TEST_F(LibYUVPlanarTest, TestARGBSepia) {
429
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
430
  memset(orig_pixels, 0, sizeof(orig_pixels));
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
447 448 449 450 451 452 453 454 455 456
  // Test black
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 0u;
  orig_pixels[3][2] = 0u;
  orig_pixels[3][3] = 255u;
  // Test white
  orig_pixels[4][0] = 255u;
  orig_pixels[4][1] = 255u;
  orig_pixels[4][2] = 255u;
  orig_pixels[4][3] = 255u;
457
  // Test color
458 459 460 461
  orig_pixels[5][0] = 16u;
  orig_pixels[5][1] = 64u;
  orig_pixels[5][2] = 192u;
  orig_pixels[5][3] = 224u;
462 463 464 465 466 467 468 469 470 471 472 473 474 475
  // Do 16 to test asm version.
  ARGBSepia(&orig_pixels[0][0], 0, 0, 0, 16, 1);
  EXPECT_EQ(33u, orig_pixels[0][0]);
  EXPECT_EQ(43u, orig_pixels[0][1]);
  EXPECT_EQ(47u, orig_pixels[0][2]);
  EXPECT_EQ(128u, orig_pixels[0][3]);
  EXPECT_EQ(135u, orig_pixels[1][0]);
  EXPECT_EQ(175u, orig_pixels[1][1]);
  EXPECT_EQ(195u, orig_pixels[1][2]);
  EXPECT_EQ(0u, orig_pixels[1][3]);
  EXPECT_EQ(69u, orig_pixels[2][0]);
  EXPECT_EQ(89u, orig_pixels[2][1]);
  EXPECT_EQ(99u, orig_pixels[2][2]);
  EXPECT_EQ(255u, orig_pixels[2][3]);
476 477 478 479 480 481 482 483 484 485 486 487
  EXPECT_EQ(0u, orig_pixels[3][0]);
  EXPECT_EQ(0u, orig_pixels[3][1]);
  EXPECT_EQ(0u, orig_pixels[3][2]);
  EXPECT_EQ(255u, orig_pixels[3][3]);
  EXPECT_EQ(239u, orig_pixels[4][0]);
  EXPECT_EQ(255u, orig_pixels[4][1]);
  EXPECT_EQ(255u, orig_pixels[4][2]);
  EXPECT_EQ(255u, orig_pixels[4][3]);
  EXPECT_EQ(88u, orig_pixels[5][0]);
  EXPECT_EQ(114u, orig_pixels[5][1]);
  EXPECT_EQ(127u, orig_pixels[5][2]);
  EXPECT_EQ(224u, orig_pixels[5][3]);
488

489
  for (int i = 0; i < 1280; ++i) {
490 491 492 493 494
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
495 496
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBSepia(&orig_pixels[0][0], 0, 0, 0, 1280, 1);
497 498
  }
}
499

500
TEST_F(LibYUVPlanarTest, TestARGBColorMatrix) {
501
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
502 503
  SIMD_ALIGNED(uint8 dst_pixels_opt[1280][4]);
  SIMD_ALIGNED(uint8 dst_pixels_c[1280][4]);
504 505

  // Matrix for Sepia.
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  SIMD_ALIGNED(static const int8 kRGBToSepia[]) = {
    17 / 2, 68 / 2, 35 / 2, 0,
    22 / 2, 88 / 2, 45 / 2, 0,
    24 / 2, 98 / 2, 50 / 2, 0,
    0, 0, 0, 64,  // Copy alpha.
  };
  memset(orig_pixels, 0, sizeof(orig_pixels));

  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
  // Test color
  orig_pixels[3][0] = 16u;
  orig_pixels[3][1] = 64u;
  orig_pixels[3][2] = 192u;
  orig_pixels[3][3] = 224u;
  // Do 16 to test asm version.
  ARGBColorMatrix(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
                  &kRGBToSepia[0], 16, 1);
  EXPECT_EQ(31u, dst_pixels_opt[0][0]);
  EXPECT_EQ(43u, dst_pixels_opt[0][1]);
  EXPECT_EQ(47u, dst_pixels_opt[0][2]);
  EXPECT_EQ(128u, dst_pixels_opt[0][3]);
  EXPECT_EQ(135u, dst_pixels_opt[1][0]);
  EXPECT_EQ(175u, dst_pixels_opt[1][1]);
  EXPECT_EQ(195u, dst_pixels_opt[1][2]);
  EXPECT_EQ(0u, dst_pixels_opt[1][3]);
  EXPECT_EQ(67u, dst_pixels_opt[2][0]);
  EXPECT_EQ(87u, dst_pixels_opt[2][1]);
  EXPECT_EQ(99u, dst_pixels_opt[2][2]);
  EXPECT_EQ(255u, dst_pixels_opt[2][3]);
  EXPECT_EQ(87u, dst_pixels_opt[3][0]);
  EXPECT_EQ(112u, dst_pixels_opt[3][1]);
  EXPECT_EQ(127u, dst_pixels_opt[3][2]);
  EXPECT_EQ(224u, dst_pixels_opt[3][3]);

  for (int i = 0; i < 1280; ++i) {
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
560
  MaskCpuFlags(disable_cpu_flags_);
561 562
  ARGBColorMatrix(&orig_pixels[0][0], 0, &dst_pixels_c[0][0], 0,
                  &kRGBToSepia[0], 1280, 1);
563
  MaskCpuFlags(benchmark_cpu_info_);
564 565 566 567 568 569 570 571 572 573 574 575 576 577

  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBColorMatrix(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
                    &kRGBToSepia[0], 1280, 1);
  }

  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ(dst_pixels_c[i][0], dst_pixels_opt[i][0]);
    EXPECT_EQ(dst_pixels_c[i][1], dst_pixels_opt[i][1]);
    EXPECT_EQ(dst_pixels_c[i][2], dst_pixels_opt[i][2]);
    EXPECT_EQ(dst_pixels_c[i][3], dst_pixels_opt[i][3]);
  }
}

578
TEST_F(LibYUVPlanarTest, TestRGBColorMatrix) {
579 580 581 582
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);

  // Matrix for Sepia.
  SIMD_ALIGNED(static const int8 kRGBToSepia[]) = {
583 584 585
    17, 68, 35, 0,
    22, 88, 45, 0,
    24, 98, 50, 0,
586
    0, 0, 0, 0,  // Unused but makes matrix 16 bytes.
587
  };
588
  memset(orig_pixels, 0, sizeof(orig_pixels));
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
  // Test color
  orig_pixels[3][0] = 16u;
  orig_pixels[3][1] = 64u;
  orig_pixels[3][2] = 192u;
  orig_pixels[3][3] = 224u;
  // Do 16 to test asm version.
611 612
  RGBColorMatrix(&orig_pixels[0][0], 0, &kRGBToSepia[0], 0, 0, 16, 1);
  EXPECT_EQ(31u, orig_pixels[0][0]);
613 614 615 616 617 618 619
  EXPECT_EQ(43u, orig_pixels[0][1]);
  EXPECT_EQ(47u, orig_pixels[0][2]);
  EXPECT_EQ(128u, orig_pixels[0][3]);
  EXPECT_EQ(135u, orig_pixels[1][0]);
  EXPECT_EQ(175u, orig_pixels[1][1]);
  EXPECT_EQ(195u, orig_pixels[1][2]);
  EXPECT_EQ(0u, orig_pixels[1][3]);
620 621
  EXPECT_EQ(67u, orig_pixels[2][0]);
  EXPECT_EQ(87u, orig_pixels[2][1]);
622 623
  EXPECT_EQ(99u, orig_pixels[2][2]);
  EXPECT_EQ(255u, orig_pixels[2][3]);
624 625
  EXPECT_EQ(87u, orig_pixels[3][0]);
  EXPECT_EQ(112u, orig_pixels[3][1]);
626 627 628
  EXPECT_EQ(127u, orig_pixels[3][2]);
  EXPECT_EQ(224u, orig_pixels[3][3]);

629
  for (int i = 0; i < 1280; ++i) {
630 631 632 633 634
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
635
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
636
    RGBColorMatrix(&orig_pixels[0][0], 0, &kRGBToSepia[0], 0, 0, 1280, 1);
637 638 639
  }
}

640
TEST_F(LibYUVPlanarTest, TestARGBColorTable) {
641
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
642
  memset(orig_pixels, 0, sizeof(orig_pixels));
643 644

  // Matrix for Sepia.
645
  static const uint8 kARGBTable[256 * 4] = {
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    1u, 2u, 3u, 4u,
    5u, 6u, 7u, 8u,
    9u, 10u, 11u, 12u,
    13u, 14u, 15u, 16u,
  };

  orig_pixels[0][0] = 0u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 0u;
  orig_pixels[1][0] = 1u;
  orig_pixels[1][1] = 1u;
  orig_pixels[1][2] = 1u;
  orig_pixels[1][3] = 1u;
  orig_pixels[2][0] = 2u;
  orig_pixels[2][1] = 2u;
  orig_pixels[2][2] = 2u;
  orig_pixels[2][3] = 2u;
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 1u;
  orig_pixels[3][2] = 2u;
  orig_pixels[3][3] = 3u;
  // Do 16 to test asm version.
  ARGBColorTable(&orig_pixels[0][0], 0, &kARGBTable[0], 0, 0, 16, 1);
  EXPECT_EQ(1u, orig_pixels[0][0]);
  EXPECT_EQ(2u, orig_pixels[0][1]);
  EXPECT_EQ(3u, orig_pixels[0][2]);
  EXPECT_EQ(4u, orig_pixels[0][3]);
  EXPECT_EQ(5u, orig_pixels[1][0]);
  EXPECT_EQ(6u, orig_pixels[1][1]);
  EXPECT_EQ(7u, orig_pixels[1][2]);
  EXPECT_EQ(8u, orig_pixels[1][3]);
  EXPECT_EQ(9u, orig_pixels[2][0]);
  EXPECT_EQ(10u, orig_pixels[2][1]);
  EXPECT_EQ(11u, orig_pixels[2][2]);
  EXPECT_EQ(12u, orig_pixels[2][3]);
  EXPECT_EQ(1u, orig_pixels[3][0]);
  EXPECT_EQ(6u, orig_pixels[3][1]);
  EXPECT_EQ(11u, orig_pixels[3][2]);
  EXPECT_EQ(16u, orig_pixels[3][3]);

687
  for (int i = 0; i < 1280; ++i) {
688 689 690 691 692
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
693 694
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBColorTable(&orig_pixels[0][0], 0, &kARGBTable[0], 0, 0, 1280, 1);
695 696 697
  }
}

698
// Same as TestARGBColorTable except alpha does not change.
699
TEST_F(LibYUVPlanarTest, TestRGBColorTable) {
700
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
701 702 703
  memset(orig_pixels, 0, sizeof(orig_pixels));

  // Matrix for Sepia.
704
  static const uint8 kARGBTable[256 * 4] = {
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
    1u, 2u, 3u, 4u,
    5u, 6u, 7u, 8u,
    9u, 10u, 11u, 12u,
    13u, 14u, 15u, 16u,
  };

  orig_pixels[0][0] = 0u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 0u;
  orig_pixels[1][0] = 1u;
  orig_pixels[1][1] = 1u;
  orig_pixels[1][2] = 1u;
  orig_pixels[1][3] = 1u;
  orig_pixels[2][0] = 2u;
  orig_pixels[2][1] = 2u;
  orig_pixels[2][2] = 2u;
  orig_pixels[2][3] = 2u;
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 1u;
  orig_pixels[3][2] = 2u;
  orig_pixels[3][3] = 3u;
  // Do 16 to test asm version.
  RGBColorTable(&orig_pixels[0][0], 0, &kARGBTable[0], 0, 0, 16, 1);
  EXPECT_EQ(1u, orig_pixels[0][0]);
  EXPECT_EQ(2u, orig_pixels[0][1]);
  EXPECT_EQ(3u, orig_pixels[0][2]);
  EXPECT_EQ(0u, orig_pixels[0][3]);  // Alpha unchanged.
  EXPECT_EQ(5u, orig_pixels[1][0]);
  EXPECT_EQ(6u, orig_pixels[1][1]);
  EXPECT_EQ(7u, orig_pixels[1][2]);
  EXPECT_EQ(1u, orig_pixels[1][3]);  // Alpha unchanged.
  EXPECT_EQ(9u, orig_pixels[2][0]);
  EXPECT_EQ(10u, orig_pixels[2][1]);
  EXPECT_EQ(11u, orig_pixels[2][2]);
  EXPECT_EQ(2u, orig_pixels[2][3]);  // Alpha unchanged.
  EXPECT_EQ(1u, orig_pixels[3][0]);
  EXPECT_EQ(6u, orig_pixels[3][1]);
  EXPECT_EQ(11u, orig_pixels[3][2]);
  EXPECT_EQ(3u, orig_pixels[3][3]);  // Alpha unchanged.

746
  for (int i = 0; i < 1280; ++i) {
747 748 749 750 751
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
752 753
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    RGBColorTable(&orig_pixels[0][0], 0, &kARGBTable[0], 0, 0, 1280, 1);
754 755 756
  }
}

757
TEST_F(LibYUVPlanarTest, TestARGBQuantize) {
758
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
759

760
  for (int i = 0; i < 1280; ++i) {
761 762 763 764 765 766
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
  ARGBQuantize(&orig_pixels[0][0], 0,
767
               (65536 + (8 / 2)) / 8, 8, 8 / 2, 0, 0, 1280, 1);
768

769 770 771 772 773
  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ((i / 8 * 8 + 8 / 2) & 255, orig_pixels[i][0]);
    EXPECT_EQ((i / 2 / 8 * 8 + 8 / 2) & 255, orig_pixels[i][1]);
    EXPECT_EQ((i / 3 / 8 * 8 + 8 / 2) & 255, orig_pixels[i][2]);
    EXPECT_EQ(i & 255, orig_pixels[i][3]);
774
  }
775
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
776
    ARGBQuantize(&orig_pixels[0][0], 0,
777
                 (65536 + (8 / 2)) / 8, 8, 8 / 2, 0, 0, 1280, 1);
778 779 780
  }
}

781
TEST_F(LibYUVPlanarTest, TestARGBMirror) {
782 783
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
  SIMD_ALIGNED(uint8 dst_pixels[1280][4]);
784

785
  for (int i = 0; i < 1280; ++i) {
786 787 788 789 790
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i / 4;
  }
791
  ARGBMirror(&orig_pixels[0][0], 0, &dst_pixels[0][0], 0, 1280, 1);
792

793 794 795 796 797
  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ(i & 255, dst_pixels[1280 - 1 - i][0]);
    EXPECT_EQ((i / 2) & 255, dst_pixels[1280 - 1 - i][1]);
    EXPECT_EQ((i / 3) & 255, dst_pixels[1280 - 1 - i][2]);
    EXPECT_EQ((i / 4) & 255, dst_pixels[1280 - 1 - i][3]);
798
  }
799 800
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBMirror(&orig_pixels[0][0], 0, &dst_pixels[0][0], 0, 1280, 1);
801 802 803
  }
}

804
TEST_F(LibYUVPlanarTest, TestShade) {
805 806
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
  SIMD_ALIGNED(uint8 shade_pixels[1280][4]);
807
  memset(orig_pixels, 0, sizeof(orig_pixels));
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

  orig_pixels[0][0] = 10u;
  orig_pixels[0][1] = 20u;
  orig_pixels[0][2] = 40u;
  orig_pixels[0][3] = 80u;
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 0u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 255u;
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 0u;
  orig_pixels[2][3] = 0u;
  orig_pixels[3][0] = 0u;
  orig_pixels[3][1] = 0u;
  orig_pixels[3][2] = 0u;
  orig_pixels[3][3] = 0u;
825 826
  // Do 8 pixels to allow opt version to be used.
  ARGBShade(&orig_pixels[0][0], 0, &shade_pixels[0][0], 0, 8, 1, 0x80ffffff);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  EXPECT_EQ(10u, shade_pixels[0][0]);
  EXPECT_EQ(20u, shade_pixels[0][1]);
  EXPECT_EQ(40u, shade_pixels[0][2]);
  EXPECT_EQ(40u, shade_pixels[0][3]);
  EXPECT_EQ(0u, shade_pixels[1][0]);
  EXPECT_EQ(0u, shade_pixels[1][1]);
  EXPECT_EQ(0u, shade_pixels[1][2]);
  EXPECT_EQ(128u, shade_pixels[1][3]);
  EXPECT_EQ(0u, shade_pixels[2][0]);
  EXPECT_EQ(0u, shade_pixels[2][1]);
  EXPECT_EQ(0u, shade_pixels[2][2]);
  EXPECT_EQ(0u, shade_pixels[2][3]);
  EXPECT_EQ(0u, shade_pixels[3][0]);
  EXPECT_EQ(0u, shade_pixels[3][1]);
  EXPECT_EQ(0u, shade_pixels[3][2]);
  EXPECT_EQ(0u, shade_pixels[3][3]);

844
  ARGBShade(&orig_pixels[0][0], 0, &shade_pixels[0][0], 0, 8, 1, 0x80808080);
845 846 847 848 849
  EXPECT_EQ(5u, shade_pixels[0][0]);
  EXPECT_EQ(10u, shade_pixels[0][1]);
  EXPECT_EQ(20u, shade_pixels[0][2]);
  EXPECT_EQ(40u, shade_pixels[0][3]);

850 851 852 853 854 855
  ARGBShade(&orig_pixels[0][0], 0, &shade_pixels[0][0], 0, 8, 1, 0x10204080);
  EXPECT_EQ(5u, shade_pixels[0][0]);
  EXPECT_EQ(5u, shade_pixels[0][1]);
  EXPECT_EQ(5u, shade_pixels[0][2]);
  EXPECT_EQ(5u, shade_pixels[0][3]);

856 857
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    ARGBShade(&orig_pixels[0][0], 0, &shade_pixels[0][0], 0, 1280, 1,
858 859 860 861
              0x80808080);
  }
}

862
TEST_F(LibYUVPlanarTest, TestARGBInterpolate) {
863 864 865
  SIMD_ALIGNED(uint8 orig_pixels_0[1280][4]);
  SIMD_ALIGNED(uint8 orig_pixels_1[1280][4]);
  SIMD_ALIGNED(uint8 interpolate_pixels[1280][4]);
866 867
  memset(orig_pixels_0, 0, sizeof(orig_pixels_0));
  memset(orig_pixels_1, 0, sizeof(orig_pixels_1));
868

869 870 871 872
  orig_pixels_0[0][0] = 16u;
  orig_pixels_0[0][1] = 32u;
  orig_pixels_0[0][2] = 64u;
  orig_pixels_0[0][3] = 128u;
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
  orig_pixels_0[1][0] = 0u;
  orig_pixels_0[1][1] = 0u;
  orig_pixels_0[1][2] = 0u;
  orig_pixels_0[1][3] = 255u;
  orig_pixels_0[2][0] = 0u;
  orig_pixels_0[2][1] = 0u;
  orig_pixels_0[2][2] = 0u;
  orig_pixels_0[2][3] = 0u;
  orig_pixels_0[3][0] = 0u;
  orig_pixels_0[3][1] = 0u;
  orig_pixels_0[3][2] = 0u;
  orig_pixels_0[3][3] = 0u;

  orig_pixels_1[0][0] = 0u;
  orig_pixels_1[0][1] = 0u;
  orig_pixels_1[0][2] = 0u;
  orig_pixels_1[0][3] = 0u;
  orig_pixels_1[1][0] = 0u;
  orig_pixels_1[1][1] = 0u;
  orig_pixels_1[1][2] = 0u;
  orig_pixels_1[1][3] = 0u;
  orig_pixels_1[2][0] = 0u;
  orig_pixels_1[2][1] = 0u;
  orig_pixels_1[2][2] = 0u;
  orig_pixels_1[2][3] = 0u;
  orig_pixels_1[3][0] = 255u;
  orig_pixels_1[3][1] = 255u;
  orig_pixels_1[3][2] = 255u;
  orig_pixels_1[3][3] = 255u;

  ARGBInterpolate(&orig_pixels_0[0][0], 0, &orig_pixels_1[0][0], 0,
                  &interpolate_pixels[0][0], 0, 4, 1, 128);
905 906 907 908
  EXPECT_EQ(8u, interpolate_pixels[0][0]);
  EXPECT_EQ(16u, interpolate_pixels[0][1]);
  EXPECT_EQ(32u, interpolate_pixels[0][2]);
  EXPECT_EQ(64u, interpolate_pixels[0][3]);
909 910 911
  EXPECT_EQ(0u, interpolate_pixels[1][0]);
  EXPECT_EQ(0u, interpolate_pixels[1][1]);
  EXPECT_EQ(0u, interpolate_pixels[1][2]);
912
  EXPECT_EQ(128u, interpolate_pixels[1][3]);
913 914 915 916
  EXPECT_EQ(0u, interpolate_pixels[2][0]);
  EXPECT_EQ(0u, interpolate_pixels[2][1]);
  EXPECT_EQ(0u, interpolate_pixels[2][2]);
  EXPECT_EQ(0u, interpolate_pixels[2][3]);
917 918 919 920
  EXPECT_EQ(128u, interpolate_pixels[3][0]);
  EXPECT_EQ(128u, interpolate_pixels[3][1]);
  EXPECT_EQ(128u, interpolate_pixels[3][2]);
  EXPECT_EQ(128u, interpolate_pixels[3][3]);
921 922 923

  ARGBInterpolate(&orig_pixels_0[0][0], 0, &orig_pixels_1[0][0], 0,
                  &interpolate_pixels[0][0], 0, 4, 1, 0);
924 925 926 927
  EXPECT_EQ(16u, interpolate_pixels[0][0]);
  EXPECT_EQ(32u, interpolate_pixels[0][1]);
  EXPECT_EQ(64u, interpolate_pixels[0][2]);
  EXPECT_EQ(128u, interpolate_pixels[0][3]);
928

929 930 931 932 933 934 935
  ARGBInterpolate(&orig_pixels_0[0][0], 0, &orig_pixels_1[0][0], 0,
                  &interpolate_pixels[0][0], 0, 4, 1, 192);

  EXPECT_EQ(4u, interpolate_pixels[0][0]);
  EXPECT_EQ(8u, interpolate_pixels[0][1]);
  EXPECT_EQ(16u, interpolate_pixels[0][2]);
  EXPECT_EQ(32u, interpolate_pixels[0][3]);
936

937
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
938
    ARGBInterpolate(&orig_pixels_0[0][0], 0, &orig_pixels_1[0][0], 0,
939
                    &interpolate_pixels[0][0], 0, 1280, 1, 128);
940 941 942
  }
}

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
TEST_F(LibYUVPlanarTest, TestInterpolatePlane) {
  SIMD_ALIGNED(uint8 orig_pixels_0[1280]);
  SIMD_ALIGNED(uint8 orig_pixels_1[1280]);
  SIMD_ALIGNED(uint8 interpolate_pixels[1280]);
  memset(orig_pixels_0, 0, sizeof(orig_pixels_0));
  memset(orig_pixels_1, 0, sizeof(orig_pixels_1));

  orig_pixels_0[0] = 16u;
  orig_pixels_0[1] = 32u;
  orig_pixels_0[2] = 64u;
  orig_pixels_0[3] = 128u;
  orig_pixels_0[4] = 0u;
  orig_pixels_0[5] = 0u;
  orig_pixels_0[6] = 0u;
  orig_pixels_0[7] = 255u;
  orig_pixels_0[8] = 0u;
  orig_pixels_0[9] = 0u;
  orig_pixels_0[10] = 0u;
  orig_pixels_0[11] = 0u;
  orig_pixels_0[12] = 0u;
  orig_pixels_0[13] = 0u;
  orig_pixels_0[14] = 0u;
  orig_pixels_0[15] = 0u;

  orig_pixels_1[0] = 0u;
  orig_pixels_1[1] = 0u;
  orig_pixels_1[2] = 0u;
  orig_pixels_1[3] = 0u;
  orig_pixels_1[4] = 0u;
  orig_pixels_1[5] = 0u;
  orig_pixels_1[6] = 0u;
  orig_pixels_1[7] = 0u;
  orig_pixels_1[8] = 0u;
  orig_pixels_1[9] = 0u;
  orig_pixels_1[10] = 0u;
  orig_pixels_1[11] = 0u;
  orig_pixels_1[12] = 255u;
  orig_pixels_1[13] = 255u;
  orig_pixels_1[14] = 255u;
  orig_pixels_1[15] = 255u;

  InterpolatePlane(&orig_pixels_0[0], 0, &orig_pixels_1[0], 0,
                   &interpolate_pixels[0], 0, 16, 1, 128);
  EXPECT_EQ(8u, interpolate_pixels[0]);
  EXPECT_EQ(16u, interpolate_pixels[1]);
  EXPECT_EQ(32u, interpolate_pixels[2]);
  EXPECT_EQ(64u, interpolate_pixels[3]);
  EXPECT_EQ(0u, interpolate_pixels[4]);
  EXPECT_EQ(0u, interpolate_pixels[5]);
  EXPECT_EQ(0u, interpolate_pixels[6]);
993
  EXPECT_EQ(128u, interpolate_pixels[7]);
994 995 996 997
  EXPECT_EQ(0u, interpolate_pixels[8]);
  EXPECT_EQ(0u, interpolate_pixels[9]);
  EXPECT_EQ(0u, interpolate_pixels[10]);
  EXPECT_EQ(0u, interpolate_pixels[11]);
998 999 1000 1001
  EXPECT_EQ(128u, interpolate_pixels[12]);
  EXPECT_EQ(128u, interpolate_pixels[13]);
  EXPECT_EQ(128u, interpolate_pixels[14]);
  EXPECT_EQ(128u, interpolate_pixels[15]);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

  InterpolatePlane(&orig_pixels_0[0], 0, &orig_pixels_1[0], 0,
                   &interpolate_pixels[0], 0, 16, 1, 0);
  EXPECT_EQ(16u, interpolate_pixels[0]);
  EXPECT_EQ(32u, interpolate_pixels[1]);
  EXPECT_EQ(64u, interpolate_pixels[2]);
  EXPECT_EQ(128u, interpolate_pixels[3]);

  InterpolatePlane(&orig_pixels_0[0], 0, &orig_pixels_1[0], 0,
                   &interpolate_pixels[0], 0, 16, 1, 192);

  EXPECT_EQ(4u, interpolate_pixels[0]);
  EXPECT_EQ(8u, interpolate_pixels[1]);
1015
  EXPECT_EQ(16u, interpolate_pixels[2]);
1016 1017 1018 1019
  EXPECT_EQ(32u, interpolate_pixels[3]);

  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    InterpolatePlane(&orig_pixels_0[0], 0, &orig_pixels_1[0], 0,
1020
                     &interpolate_pixels[0], 0, 1280, 1, 123);
1021 1022 1023
  }
}

1024 1025
#define TESTTERP(FMT_A, BPP_A, STRIDE_A,                                       \
                 FMT_B, BPP_B, STRIDE_B,                                       \
1026
                 W1280, TERP, N, NEG, OFF)                               \
1027
TEST_F(LibYUVPlanarTest, ARGBInterpolate##TERP##N) {                           \
1028
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
1029 1030 1031 1032 1033 1034 1035 1036
  const int kHeight = benchmark_height_;                                       \
  const int kStrideA = (kWidth * BPP_A + STRIDE_A - 1) / STRIDE_A * STRIDE_A;  \
  const int kStrideB = (kWidth * BPP_B + STRIDE_B - 1) / STRIDE_B * STRIDE_B;  \
  align_buffer_64(src_argb_a, kStrideA * kHeight + OFF);                       \
  align_buffer_64(src_argb_b, kStrideA * kHeight + OFF);                       \
  align_buffer_64(dst_argb_c, kStrideB * kHeight);                             \
  align_buffer_64(dst_argb_opt, kStrideB * kHeight);                           \
  for (int i = 0; i < kStrideA * kHeight; ++i) {                               \
1037 1038
    src_argb_a[i + OFF] = (fastrand() & 0xff);                                 \
    src_argb_b[i + OFF] = (fastrand() & 0xff);                                 \
1039
  }                                                                            \
1040
  MaskCpuFlags(disable_cpu_flags_);                                            \
1041 1042 1043 1044
  ARGBInterpolate(src_argb_a + OFF, kStrideA,                                  \
                  src_argb_b + OFF, kStrideA,                                  \
                  dst_argb_c, kStrideB,                                        \
                  kWidth, NEG kHeight, TERP);                                  \
1045
  MaskCpuFlags(benchmark_cpu_info_);                                           \
1046 1047 1048 1049 1050 1051 1052
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    ARGBInterpolate(src_argb_a + OFF, kStrideA,                                \
                    src_argb_b + OFF, kStrideA,                                \
                    dst_argb_opt, kStrideB,                                    \
                    kWidth, NEG kHeight, TERP);                                \
  }                                                                            \
  for (int i = 0; i < kStrideB * kHeight; ++i) {                               \
1053
    EXPECT_EQ(dst_argb_c[i], dst_argb_opt[i]);                                 \
1054
  }                                                                            \
1055 1056 1057 1058
  free_aligned_buffer_64(src_argb_a);                                          \
  free_aligned_buffer_64(src_argb_b);                                          \
  free_aligned_buffer_64(dst_argb_c);                                          \
  free_aligned_buffer_64(dst_argb_opt);                                        \
1059 1060 1061
}

#define TESTINTERPOLATE(TERP)                                                  \
1062 1063 1064 1065
    TESTTERP(ARGB, 4, 1, ARGB, 4, 1, benchmark_width_ - 1, TERP, _Any, +, 0)   \
    TESTTERP(ARGB, 4, 1, ARGB, 4, 1, benchmark_width_, TERP, _Unaligned, +, 1) \
    TESTTERP(ARGB, 4, 1, ARGB, 4, 1, benchmark_width_, TERP, _Invert, -, 0)    \
    TESTTERP(ARGB, 4, 1, ARGB, 4, 1, benchmark_width_, TERP, _Opt, +, 0)
1066 1067 1068 1069 1070 1071 1072

TESTINTERPOLATE(0)
TESTINTERPOLATE(64)
TESTINTERPOLATE(128)
TESTINTERPOLATE(192)
TESTINTERPOLATE(255)

1073
static int TestBlend(int width, int height, int benchmark_iterations,
1074 1075
                     int disable_cpu_flags, int benchmark_cpu_info,
                     int invert, int off) {
1076 1077 1078
  if (width < 1) {
    width = 1;
  }
1079 1080 1081 1082 1083 1084 1085
  const int kBpp = 4;
  const int kStride = width * kBpp;
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(src_argb_b, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
1086 1087
    src_argb_a[i + off] = (fastrand() & 0xff);
    src_argb_b[i + off] = (fastrand() & 0xff);
1088
  }
1089
  ARGBAttenuate(src_argb_a + off, kStride, src_argb_a + off, kStride, width,
1090
                height);
1091
  ARGBAttenuate(src_argb_b + off, kStride, src_argb_b + off, kStride, width,
1092
                height);
1093 1094
  memset(dst_argb_c, 255, kStride * height);
  memset(dst_argb_opt, 255, kStride * height);
1095

1096
  MaskCpuFlags(disable_cpu_flags);
1097 1098 1099
  ARGBBlend(src_argb_a + off, kStride,
            src_argb_b + off, kStride,
            dst_argb_c, kStride,
1100
            width, invert * height);
1101
  MaskCpuFlags(benchmark_cpu_info);
1102
  for (int i = 0; i < benchmark_iterations; ++i) {
1103 1104 1105
    ARGBBlend(src_argb_a + off, kStride,
              src_argb_b + off, kStride,
              dst_argb_opt, kStride,
1106
              width, invert * height);
1107 1108
  }
  int max_diff = 0;
1109
  for (int i = 0; i < kStride * height; ++i) {
1110 1111 1112 1113 1114 1115 1116
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1117 1118 1119 1120
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(src_argb_b);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
1121 1122 1123
  return max_diff;
}

1124
TEST_F(LibYUVPlanarTest, ARGBBlend_Any) {
1125
  int max_diff = TestBlend(benchmark_width_ - 4, benchmark_height_,
1126 1127
                           benchmark_iterations_,
                           disable_cpu_flags_,  benchmark_cpu_info_, +1, 0);
1128 1129 1130
  EXPECT_LE(max_diff, 1);
}

1131
TEST_F(LibYUVPlanarTest, ARGBBlend_Unaligned) {
1132
  int max_diff = TestBlend(benchmark_width_, benchmark_height_,
1133 1134
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1135 1136
  EXPECT_LE(max_diff, 1);
}
1137

1138
TEST_F(LibYUVPlanarTest, ARGBBlend_Invert) {
1139
  int max_diff = TestBlend(benchmark_width_, benchmark_height_,
1140 1141
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
1142 1143 1144
  EXPECT_LE(max_diff, 1);
}

1145
TEST_F(LibYUVPlanarTest, ARGBBlend_Opt) {
1146
  int max_diff = TestBlend(benchmark_width_, benchmark_height_,
1147 1148
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1149 1150 1151
  EXPECT_LE(max_diff, 1);
}

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
static void TestBlendPlane(int width, int height, int benchmark_iterations,
                           int disable_cpu_flags, int benchmark_cpu_info,
                           int invert, int off) {
  if (width < 1) {
    width = 1;
  }
  const int kBpp = 1;
  const int kStride = width * kBpp;
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(src_argb_b, kStride * height + off);
  align_buffer_64(src_argb_alpha, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height + off);
  align_buffer_64(dst_argb_opt, kStride * height + off);
  memset(dst_argb_c, 255, kStride * height + off);
  memset(dst_argb_opt, 255, kStride * height + off);

  // Test source is maintained exactly if alpha is 255.
  for (int i = 0; i < width; ++i) {
    src_argb_a[i + off] = i & 255;
    src_argb_b[i + off] = 255 - (i & 255);
  }
  memset(src_argb_alpha + off, 255, width);
  BlendPlane(src_argb_a + off, width,
             src_argb_b + off, width,
             src_argb_alpha + off, width,
             dst_argb_opt + off, width,
             width, 1);
  for (int i = 0; i < width; ++i) {
    EXPECT_EQ(src_argb_a[i + off], dst_argb_opt[i + off]);
  }
  // Test destination is maintained exactly if alpha is 0.
  memset(src_argb_alpha + off, 0, width);
  BlendPlane(src_argb_a + off, width,
             src_argb_b + off, width,
             src_argb_alpha + off, width,
             dst_argb_opt + off, width,
             width, 1);
  for (int i = 0; i < width; ++i) {
    EXPECT_EQ(src_argb_b[i + off], dst_argb_opt[i + off]);
  }
  for (int i = 0; i < kStride * height; ++i) {
    src_argb_a[i + off] = (fastrand() & 0xff);
    src_argb_b[i + off] = (fastrand() & 0xff);
    src_argb_alpha[i + off] = (fastrand() & 0xff);
  }

  MaskCpuFlags(disable_cpu_flags);
  BlendPlane(src_argb_a + off, width,
             src_argb_b + off, width,
             src_argb_alpha + off, width,
             dst_argb_c + off, width,
             width, height);
  MaskCpuFlags(benchmark_cpu_info);
  for (int i = 0; i < benchmark_iterations; ++i) {
    BlendPlane(src_argb_a + off, width,
               src_argb_b + off, width,
               src_argb_alpha + off, width,
               dst_argb_opt + off, width,
               width, height);
  }
  for (int i = 0; i < kStride * height; ++i) {
    EXPECT_EQ(dst_argb_c[i + off], dst_argb_opt[i + off]);
1214 1215 1216
  }
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(src_argb_b);
1217
  free_aligned_buffer_64(src_argb_alpha);
1218 1219 1220 1221 1222 1223 1224
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
  return;
}

TEST_F(LibYUVPlanarTest, BlendPlane_Opt) {
  TestBlendPlane(benchmark_width_, benchmark_height_, benchmark_iterations_,
1225 1226 1227 1228 1229 1230
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
}
TEST_F(LibYUVPlanarTest, BlendPlane_Unaligned) {
  TestBlendPlane(benchmark_width_, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
}
1231 1232 1233 1234 1235 1236 1237 1238
TEST_F(LibYUVPlanarTest, BlendPlane_Any) {
  TestBlendPlane(benchmark_width_ - 4, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
}
TEST_F(LibYUVPlanarTest, BlendPlane_Invert) {
  TestBlendPlane(benchmark_width_, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, -1, 1);
}
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

#define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))

static void TestI420Blend(int width, int height, int benchmark_iterations,
                          int disable_cpu_flags, int benchmark_cpu_info,
                          int invert, int off) {
  width = ((width) > 0) ? (width) : 1;
  const int kStrideUV = SUBSAMPLE(width, 2);
  const int kSizeUV = kStrideUV * SUBSAMPLE(height, 2);
  align_buffer_64(src_y0, width * height + off);
  align_buffer_64(src_u0, kSizeUV + off);
  align_buffer_64(src_v0, kSizeUV + off);
  align_buffer_64(src_y1, width * height + off);
  align_buffer_64(src_u1, kSizeUV + off);
  align_buffer_64(src_v1, kSizeUV + off);
  align_buffer_64(src_a, width * height + off);
  align_buffer_64(dst_y_c, width * height + off);
  align_buffer_64(dst_u_c, kSizeUV + off);
  align_buffer_64(dst_v_c, kSizeUV + off);
  align_buffer_64(dst_y_opt, width * height + off);
  align_buffer_64(dst_u_opt, kSizeUV + off);
  align_buffer_64(dst_v_opt, kSizeUV + off);

  MemRandomize(src_y0, width * height + off);
  MemRandomize(src_u0, kSizeUV + off);
  MemRandomize(src_v0, kSizeUV + off);
  MemRandomize(src_y1, width * height + off);
  MemRandomize(src_u1, kSizeUV + off);
  MemRandomize(src_v1, kSizeUV + off);
  MemRandomize(src_a, width * height + off);
  memset(dst_y_c, 255, width * height + off);
  memset(dst_u_c, 255, kSizeUV + off);
  memset(dst_v_c, 255, kSizeUV + off);
  memset(dst_y_opt, 255, width * height + off);
  memset(dst_u_opt, 255, kSizeUV + off);
  memset(dst_v_opt, 255, kSizeUV + off);

  MaskCpuFlags(disable_cpu_flags);
  I420Blend(src_y0 + off, width,
            src_u0 + off, kStrideUV,
            src_v0 + off, kStrideUV,
            src_y1 + off, width,
            src_u1 + off, kStrideUV,
            src_v1 + off, kStrideUV,
            src_a + off, width,
            dst_y_c + off, width,
            dst_u_c + off, kStrideUV,
            dst_v_c + off, kStrideUV,
            width, height);
  MaskCpuFlags(benchmark_cpu_info);
  for (int i = 0; i < benchmark_iterations; ++i) {
    I420Blend(src_y0 + off, width,
              src_u0 + off, kStrideUV,
              src_v0 + off, kStrideUV,
              src_y1 + off, width,
              src_u1 + off, kStrideUV,
              src_v1 + off, kStrideUV,
              src_a + off, width,
              dst_y_opt + off, width,
              dst_u_opt + off, kStrideUV,
              dst_v_opt + off, kStrideUV,
              width, height);
  }
  for (int i = 0; i < width * height; ++i) {
    EXPECT_EQ(dst_y_c[i + off], dst_y_opt[i + off]);
  }
  for (int i = 0; i < kSizeUV; ++i) {
1306 1307
    EXPECT_EQ(dst_u_c[i + off], dst_u_opt[i + off]);
    EXPECT_EQ(dst_v_c[i + off], dst_v_opt[i + off]);
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
  }
  free_aligned_buffer_64(src_y0);
  free_aligned_buffer_64(src_u0);
  free_aligned_buffer_64(src_v0);
  free_aligned_buffer_64(src_y1);
  free_aligned_buffer_64(src_u1);
  free_aligned_buffer_64(src_v1);
  free_aligned_buffer_64(src_a);
  free_aligned_buffer_64(dst_y_c);
  free_aligned_buffer_64(dst_u_c);
  free_aligned_buffer_64(dst_v_c);
  free_aligned_buffer_64(dst_y_opt);
  free_aligned_buffer_64(dst_u_opt);
  free_aligned_buffer_64(dst_v_opt);
  return;
}

TEST_F(LibYUVPlanarTest, I420Blend_Opt) {
  TestI420Blend(benchmark_width_, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
}
TEST_F(LibYUVPlanarTest, I420Blend_Unaligned) {
  TestI420Blend(benchmark_width_, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1332
}
1333 1334 1335

// TODO(fbarchard): DISABLED because _Any uses C.  Avoid C and re-enable.
TEST_F(LibYUVPlanarTest, DISABLED_I420Blend_Any) {
1336 1337 1338 1339 1340 1341 1342
  TestI420Blend(benchmark_width_ - 4, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
}
TEST_F(LibYUVPlanarTest, I420Blend_Invert) {
  TestI420Blend(benchmark_width_, benchmark_height_, benchmark_iterations_,
                 disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
}
1343

1344
TEST_F(LibYUVPlanarTest, TestAffine) {
1345 1346
  SIMD_ALIGNED(uint8 orig_pixels_0[1280][4]);
  SIMD_ALIGNED(uint8 interpolate_pixels_C[1280][4]);
1347

1348
  for (int i = 0; i < 1280; ++i) {
1349 1350 1351 1352 1353 1354 1355 1356
    for (int j = 0; j < 4; ++j) {
      orig_pixels_0[i][j] = i;
    }
  }

  float uv_step[4] = { 0.f, 0.f, 0.75f, 0.f };

  ARGBAffineRow_C(&orig_pixels_0[0][0], 0, &interpolate_pixels_C[0][0],
1357
                  uv_step, 1280);
1358 1359 1360 1361
  EXPECT_EQ(0u, interpolate_pixels_C[0][0]);
  EXPECT_EQ(96u, interpolate_pixels_C[128][0]);
  EXPECT_EQ(191u, interpolate_pixels_C[255][3]);

1362
#if defined(HAS_ARGBAFFINEROW_SSE2)
1363
  SIMD_ALIGNED(uint8 interpolate_pixels_Opt[1280][4]);
1364
  ARGBAffineRow_SSE2(&orig_pixels_0[0][0], 0, &interpolate_pixels_Opt[0][0],
1365 1366
                     uv_step, 1280);
  EXPECT_EQ(0, memcmp(interpolate_pixels_Opt, interpolate_pixels_C, 1280 * 4));
1367 1368 1369

  int has_sse2 = TestCpuFlag(kCpuHasSSE2);
  if (has_sse2) {
1370
    for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
1371
      ARGBAffineRow_SSE2(&orig_pixels_0[0][0], 0, &interpolate_pixels_Opt[0][0],
1372
                         uv_step, 1280);
1373 1374
    }
  }
1375
#endif
1376 1377
}

1378
TEST_F(LibYUVPlanarTest, TestSobelX) {
1379 1380 1381 1382 1383
  SIMD_ALIGNED(uint8 orig_pixels_0[1280 + 2]);
  SIMD_ALIGNED(uint8 orig_pixels_1[1280 + 2]);
  SIMD_ALIGNED(uint8 orig_pixels_2[1280 + 2]);
  SIMD_ALIGNED(uint8 sobel_pixels_c[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_opt[1280]);
fbarchard@google.com's avatar
fbarchard@google.com committed
1384

1385
  for (int i = 0; i < 1280 + 2; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1386 1387 1388 1389 1390 1391
    orig_pixels_0[i] = i;
    orig_pixels_1[i] = i * 2;
    orig_pixels_2[i] = i * 3;
  }

  SobelXRow_C(orig_pixels_0, orig_pixels_1, orig_pixels_2,
1392
              sobel_pixels_c, 1280);
fbarchard@google.com's avatar
fbarchard@google.com committed
1393 1394 1395 1396

  EXPECT_EQ(16u, sobel_pixels_c[0]);
  EXPECT_EQ(16u, sobel_pixels_c[100]);
  EXPECT_EQ(255u, sobel_pixels_c[255]);
1397 1398 1399 1400

  void (*SobelXRow)(const uint8* src_y0, const uint8* src_y1,
                    const uint8* src_y2, uint8* dst_sobely, int width) =
      SobelXRow_C;
1401 1402 1403
#if defined(HAS_SOBELXROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelXRow = SobelXRow_SSE2;
1404
  }
1405 1406 1407 1408 1409
#endif
#if defined(HAS_SOBELXROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelXRow = SobelXRow_NEON;
  }
1410
#endif
1411
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
1412
    SobelXRow(orig_pixels_0, orig_pixels_1, orig_pixels_2,
1413
              sobel_pixels_opt, 1280);
fbarchard@google.com's avatar
fbarchard@google.com committed
1414
  }
1415
  for (int i = 0; i < 1280; ++i) {
1416
    EXPECT_EQ(sobel_pixels_c[i], sobel_pixels_opt[i]);
fbarchard@google.com's avatar
fbarchard@google.com committed
1417 1418 1419
  }
}

1420
TEST_F(LibYUVPlanarTest, TestSobelY) {
1421 1422 1423 1424
  SIMD_ALIGNED(uint8 orig_pixels_0[1280 + 2]);
  SIMD_ALIGNED(uint8 orig_pixels_1[1280 + 2]);
  SIMD_ALIGNED(uint8 sobel_pixels_c[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_opt[1280]);
fbarchard@google.com's avatar
fbarchard@google.com committed
1425

1426
  for (int i = 0; i < 1280 + 2; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1427 1428 1429 1430
    orig_pixels_0[i] = i;
    orig_pixels_1[i] = i * 2;
  }

1431
  SobelYRow_C(orig_pixels_0, orig_pixels_1, sobel_pixels_c, 1280);
fbarchard@google.com's avatar
fbarchard@google.com committed
1432 1433 1434 1435

  EXPECT_EQ(4u, sobel_pixels_c[0]);
  EXPECT_EQ(255u, sobel_pixels_c[100]);
  EXPECT_EQ(0u, sobel_pixels_c[255]);
1436 1437
  void (*SobelYRow)(const uint8* src_y0, const uint8* src_y1,
                    uint8* dst_sobely, int width) = SobelYRow_C;
1438 1439 1440
#if defined(HAS_SOBELYROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelYRow = SobelYRow_SSE2;
1441
  }
1442 1443 1444 1445 1446
#endif
#if defined(HAS_SOBELYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelYRow = SobelYRow_NEON;
  }
1447
#endif
1448 1449
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    SobelYRow(orig_pixels_0, orig_pixels_1, sobel_pixels_opt, 1280);
fbarchard@google.com's avatar
fbarchard@google.com committed
1450
  }
1451
  for (int i = 0; i < 1280; ++i) {
1452
    EXPECT_EQ(sobel_pixels_c[i], sobel_pixels_opt[i]);
fbarchard@google.com's avatar
fbarchard@google.com committed
1453 1454 1455
  }
}

1456
TEST_F(LibYUVPlanarTest, TestSobel) {
1457 1458 1459 1460
  SIMD_ALIGNED(uint8 orig_sobelx[1280]);
  SIMD_ALIGNED(uint8 orig_sobely[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_c[1280 * 4]);
  SIMD_ALIGNED(uint8 sobel_pixels_opt[1280 * 4]);
1461

1462
  for (int i = 0; i < 1280; ++i) {
1463 1464 1465 1466
    orig_sobelx[i] = i;
    orig_sobely[i] = i * 2;
  }

1467
  SobelRow_C(orig_sobelx, orig_sobely, sobel_pixels_c, 1280);
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

  EXPECT_EQ(0u, sobel_pixels_c[0]);
  EXPECT_EQ(3u, sobel_pixels_c[4]);
  EXPECT_EQ(3u, sobel_pixels_c[5]);
  EXPECT_EQ(3u, sobel_pixels_c[6]);
  EXPECT_EQ(255u, sobel_pixels_c[7]);
  EXPECT_EQ(6u, sobel_pixels_c[8]);
  EXPECT_EQ(6u, sobel_pixels_c[9]);
  EXPECT_EQ(6u, sobel_pixels_c[10]);
  EXPECT_EQ(255u, sobel_pixels_c[7]);
  EXPECT_EQ(255u, sobel_pixels_c[100 * 4 + 1]);
  EXPECT_EQ(255u, sobel_pixels_c[255 * 4 + 1]);
1480 1481
  void (*SobelRow)(const uint8* src_sobelx, const uint8* src_sobely,
                   uint8* dst_argb, int width) = SobelRow_C;
1482
#if defined(HAS_SOBELROW_SSE2)
1483 1484 1485
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelRow = SobelRow_SSE2;
  }
1486 1487 1488 1489 1490
#endif
#if defined(HAS_SOBELROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelRow = SobelRow_NEON;
  }
1491
#endif
1492 1493
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    SobelRow(orig_sobelx, orig_sobely, sobel_pixels_opt, 1280);
1494
  }
1495 1496 1497 1498 1499
  for (int i = 0; i < 1280 * 4; ++i) {
    EXPECT_EQ(sobel_pixels_c[i], sobel_pixels_opt[i]);
  }
}

1500
TEST_F(LibYUVPlanarTest, TestSobelToPlane) {
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
  SIMD_ALIGNED(uint8 orig_sobelx[1280]);
  SIMD_ALIGNED(uint8 orig_sobely[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_c[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_opt[1280]);

  for (int i = 0; i < 1280; ++i) {
    orig_sobelx[i] = i;
    orig_sobely[i] = i * 2;
  }

  SobelToPlaneRow_C(orig_sobelx, orig_sobely, sobel_pixels_c, 1280);

  EXPECT_EQ(0u, sobel_pixels_c[0]);
  EXPECT_EQ(3u, sobel_pixels_c[1]);
  EXPECT_EQ(6u, sobel_pixels_c[2]);
  EXPECT_EQ(99u, sobel_pixels_c[33]);
  EXPECT_EQ(255u, sobel_pixels_c[100]);
  void (*SobelToPlaneRow)(const uint8* src_sobelx, const uint8* src_sobely,
                          uint8* dst_y, int width) = SobelToPlaneRow_C;
#if defined(HAS_SOBELTOPLANEROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelToPlaneRow = SobelToPlaneRow_SSE2;
  }
#endif
#if defined(HAS_SOBELTOPLANEROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelToPlaneRow = SobelToPlaneRow_NEON;
  }
#endif
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    SobelToPlaneRow(orig_sobelx, orig_sobely, sobel_pixels_opt, 1280);
  }
  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ(sobel_pixels_c[i], sobel_pixels_opt[i]);
1535 1536 1537
  }
}

1538
TEST_F(LibYUVPlanarTest, TestSobelXY) {
1539 1540 1541 1542
  SIMD_ALIGNED(uint8 orig_sobelx[1280]);
  SIMD_ALIGNED(uint8 orig_sobely[1280]);
  SIMD_ALIGNED(uint8 sobel_pixels_c[1280 * 4]);
  SIMD_ALIGNED(uint8 sobel_pixels_opt[1280 * 4]);
1543

1544
  for (int i = 0; i < 1280; ++i) {
1545 1546 1547 1548
    orig_sobelx[i] = i;
    orig_sobely[i] = i * 2;
  }

1549
  SobelXYRow_C(orig_sobelx, orig_sobely, sobel_pixels_c, 1280);
1550 1551 1552 1553 1554 1555 1556 1557

  EXPECT_EQ(0u, sobel_pixels_c[0]);
  EXPECT_EQ(2u, sobel_pixels_c[4]);
  EXPECT_EQ(3u, sobel_pixels_c[5]);
  EXPECT_EQ(1u, sobel_pixels_c[6]);
  EXPECT_EQ(255u, sobel_pixels_c[7]);
  EXPECT_EQ(255u, sobel_pixels_c[100 * 4 + 1]);
  EXPECT_EQ(255u, sobel_pixels_c[255 * 4 + 1]);
1558 1559
  void (*SobelXYRow)(const uint8* src_sobelx, const uint8* src_sobely,
                       uint8* dst_argb, int width) = SobelXYRow_C;
1560
#if defined(HAS_SOBELXYROW_SSE2)
1561 1562 1563
  if (TestCpuFlag(kCpuHasSSE2)) {
    SobelXYRow = SobelXYRow_SSE2;
  }
1564 1565 1566 1567 1568
#endif
#if defined(HAS_SOBELXYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    SobelXYRow = SobelXYRow_NEON;
  }
1569
#endif
1570 1571
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
    SobelXYRow(orig_sobelx, orig_sobely, sobel_pixels_opt, 1280);
1572
  }
1573 1574
  for (int i = 0; i < 1280 * 4; ++i) {
    EXPECT_EQ(sobel_pixels_c[i], sobel_pixels_opt[i]);
1575 1576 1577
  }
}

1578
TEST_F(LibYUVPlanarTest, TestCopyPlane) {
1579 1580 1581 1582 1583 1584 1585
  int err = 0;
  int yw = benchmark_width_;
  int yh = benchmark_height_;
  int b = 12;
  int i, j;

  int y_plane_size = (yw + b * 2) * (yh + b * 2);
1586 1587
  align_buffer_64(orig_y, y_plane_size);
  align_buffer_64(dst_c, y_plane_size);
1588
  align_buffer_64(dst_opt, y_plane_size);
1589 1590 1591 1592 1593 1594 1595 1596

  memset(orig_y, 0, y_plane_size);
  memset(dst_c, 0, y_plane_size);
  memset(dst_opt, 0, y_plane_size);

  // Fill image buffers with random data.
  for (i = b; i < (yh + b); ++i) {
    for (j = b; j < (yw + b); ++j) {
1597
      orig_y[i * (yw + b * 2) + j] = fastrand() & 0xff;
1598 1599 1600 1601 1602
    }
  }

  // Fill destination buffers with random data.
  for (i = 0; i < y_plane_size; ++i) {
1603
    uint8 random_number = fastrand() & 0x7f;
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
    dst_c[i] = random_number;
    dst_opt[i] = dst_c[i];
  }

  int y_off = b * (yw + b * 2) + b;

  int y_st = yw + b * 2;
  int stride = 8;

  // Disable all optimizations.
1614
  MaskCpuFlags(disable_cpu_flags_);
1615 1616 1617 1618 1619 1620 1621
  double c_time = get_time();
  for (j = 0; j < benchmark_iterations_; j++) {
    CopyPlane(orig_y + y_off, y_st, dst_c + y_off, stride, yw, yh);
  }
  c_time = (get_time() - c_time) / benchmark_iterations_;

  // Enable optimizations.
1622
  MaskCpuFlags(benchmark_cpu_info_);
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
  double opt_time = get_time();
  for (j = 0; j < benchmark_iterations_; j++) {
    CopyPlane(orig_y + y_off, y_st, dst_opt + y_off, stride, yw, yh);
  }
  opt_time = (get_time() - opt_time) / benchmark_iterations_;

  for (i = 0; i < y_plane_size; ++i) {
    if (dst_c[i] != dst_opt[i])
      ++err;
  }

1634 1635 1636
  free_aligned_buffer_64(orig_y);
  free_aligned_buffer_64(dst_c);
  free_aligned_buffer_64(dst_opt);
1637 1638 1639 1640

  EXPECT_EQ(0, err);
}

1641
static int TestMultiply(int width, int height, int benchmark_iterations,
1642 1643
                        int disable_cpu_flags, int benchmark_cpu_info,
                        int invert, int off) {
1644 1645 1646
  if (width < 1) {
    width = 1;
  }
1647
  const int kBpp = 4;
1648
  const int kStride = width * kBpp;
1649 1650 1651 1652 1653
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(src_argb_b, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
1654 1655
    src_argb_a[i + off] = (fastrand() & 0xff);
    src_argb_b[i + off] = (fastrand() & 0xff);
1656
  }
1657 1658
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);
1659

1660
  MaskCpuFlags(disable_cpu_flags);
1661
  ARGBMultiply(src_argb_a + off, kStride,
1662
               src_argb_b + off, kStride,
1663 1664
               dst_argb_c, kStride,
               width, invert * height);
1665
  MaskCpuFlags(benchmark_cpu_info);
1666 1667 1668 1669 1670 1671
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBMultiply(src_argb_a + off, kStride,
                 src_argb_b + off, kStride,
                 dst_argb_opt, kStride,
                 width, invert * height);
  }
1672 1673 1674 1675 1676 1677 1678 1679 1680
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1681 1682 1683 1684
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(src_argb_b);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
1685 1686 1687
  return max_diff;
}

1688
TEST_F(LibYUVPlanarTest, ARGBMultiply_Any) {
1689
  int max_diff = TestMultiply(benchmark_width_ - 1, benchmark_height_,
1690 1691
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1692 1693 1694
  EXPECT_LE(max_diff, 1);
}

1695
TEST_F(LibYUVPlanarTest, ARGBMultiply_Unaligned) {
1696
  int max_diff = TestMultiply(benchmark_width_, benchmark_height_,
1697 1698
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1699 1700 1701
  EXPECT_LE(max_diff, 1);
}

1702
TEST_F(LibYUVPlanarTest, ARGBMultiply_Invert) {
1703
  int max_diff = TestMultiply(benchmark_width_, benchmark_height_,
1704 1705
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
1706 1707 1708
  EXPECT_LE(max_diff, 1);
}

1709
TEST_F(LibYUVPlanarTest, ARGBMultiply_Opt) {
1710
  int max_diff = TestMultiply(benchmark_width_, benchmark_height_,
1711 1712
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1713 1714 1715
  EXPECT_LE(max_diff, 1);
}

1716
static int TestAdd(int width, int height, int benchmark_iterations,
1717 1718
                   int disable_cpu_flags,  int benchmark_cpu_info,
                   int invert, int off) {
1719 1720 1721
  if (width < 1) {
    width = 1;
  }
1722
  const int kBpp = 4;
1723
  const int kStride = width * kBpp;
1724 1725 1726 1727 1728
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(src_argb_b, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
1729 1730
    src_argb_a[i + off] = (fastrand() & 0xff);
    src_argb_b[i + off] = (fastrand() & 0xff);
1731 1732 1733 1734
  }
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

1735
  MaskCpuFlags(disable_cpu_flags);
1736 1737 1738 1739
  ARGBAdd(src_argb_a + off, kStride,
          src_argb_b + off, kStride,
          dst_argb_c, kStride,
          width, invert * height);
1740
  MaskCpuFlags(benchmark_cpu_info);
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBAdd(src_argb_a + off, kStride,
            src_argb_b + off, kStride,
            dst_argb_opt, kStride,
            width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1756 1757 1758 1759
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(src_argb_b);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
1760 1761 1762
  return max_diff;
}

1763
TEST_F(LibYUVPlanarTest, ARGBAdd_Any) {
1764
  int max_diff = TestAdd(benchmark_width_ - 1, benchmark_height_,
1765 1766
                         benchmark_iterations_,
                         disable_cpu_flags_,  benchmark_cpu_info_, +1, 0);
1767 1768 1769
  EXPECT_LE(max_diff, 1);
}

1770
TEST_F(LibYUVPlanarTest, ARGBAdd_Unaligned) {
1771
  int max_diff = TestAdd(benchmark_width_, benchmark_height_,
1772 1773
                         benchmark_iterations_,
                         disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1774 1775 1776
  EXPECT_LE(max_diff, 1);
}

1777
TEST_F(LibYUVPlanarTest, ARGBAdd_Invert) {
1778
  int max_diff = TestAdd(benchmark_width_, benchmark_height_,
1779 1780
                         benchmark_iterations_,
                         disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
1781 1782 1783
  EXPECT_LE(max_diff, 1);
}

1784
TEST_F(LibYUVPlanarTest, ARGBAdd_Opt) {
1785
  int max_diff = TestAdd(benchmark_width_, benchmark_height_,
1786 1787
                         benchmark_iterations_,
                         disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1788 1789 1790
  EXPECT_LE(max_diff, 1);
}

1791
static int TestSubtract(int width, int height, int benchmark_iterations,
1792 1793
                        int disable_cpu_flags, int benchmark_cpu_info,
                        int invert, int off) {
1794 1795 1796
  if (width < 1) {
    width = 1;
  }
1797
  const int kBpp = 4;
1798
  const int kStride = width * kBpp;
1799 1800 1801 1802 1803
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(src_argb_b, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
1804 1805
    src_argb_a[i + off] = (fastrand() & 0xff);
    src_argb_b[i + off] = (fastrand() & 0xff);
1806 1807 1808 1809
  }
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

1810
  MaskCpuFlags(disable_cpu_flags);
1811 1812 1813 1814
  ARGBSubtract(src_argb_a + off, kStride,
               src_argb_b + off, kStride,
               dst_argb_c, kStride,
               width, invert * height);
1815
  MaskCpuFlags(benchmark_cpu_info);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBSubtract(src_argb_a + off, kStride,
                 src_argb_b + off, kStride,
                 dst_argb_opt, kStride,
                 width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1831 1832 1833 1834
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(src_argb_b);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
1835 1836 1837
  return max_diff;
}

1838
TEST_F(LibYUVPlanarTest, ARGBSubtract_Any) {
1839
  int max_diff = TestSubtract(benchmark_width_ - 1, benchmark_height_,
1840 1841
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1842 1843 1844
  EXPECT_LE(max_diff, 1);
}

1845
TEST_F(LibYUVPlanarTest, ARGBSubtract_Unaligned) {
1846
  int max_diff = TestSubtract(benchmark_width_, benchmark_height_,
1847 1848
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1849 1850 1851
  EXPECT_LE(max_diff, 1);
}

1852
TEST_F(LibYUVPlanarTest, ARGBSubtract_Invert) {
1853
  int max_diff = TestSubtract(benchmark_width_, benchmark_height_,
1854 1855
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
1856 1857 1858
  EXPECT_LE(max_diff, 1);
}

1859
TEST_F(LibYUVPlanarTest, ARGBSubtract_Opt) {
1860
  int max_diff = TestSubtract(benchmark_width_, benchmark_height_,
1861 1862
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1863 1864 1865
  EXPECT_LE(max_diff, 1);
}

fbarchard@google.com's avatar
fbarchard@google.com committed
1866
static int TestSobel(int width, int height, int benchmark_iterations,
1867 1868
                     int disable_cpu_flags, int benchmark_cpu_info,
                     int invert, int off) {
1869 1870 1871
  if (width < 1) {
    width = 1;
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
1872
  const int kBpp = 4;
1873
  const int kStride = width * kBpp;
fbarchard@google.com's avatar
fbarchard@google.com committed
1874 1875 1876
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
1877
  memset(src_argb_a, 0, kStride * height + off);
fbarchard@google.com's avatar
fbarchard@google.com committed
1878
  for (int i = 0; i < kStride * height; ++i) {
1879
    src_argb_a[i + off] = (fastrand() & 0xff);
fbarchard@google.com's avatar
fbarchard@google.com committed
1880 1881 1882 1883
  }
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

1884
  MaskCpuFlags(disable_cpu_flags);
fbarchard@google.com's avatar
fbarchard@google.com committed
1885 1886 1887
  ARGBSobel(src_argb_a + off, kStride,
            dst_argb_c, kStride,
            width, invert * height);
1888
  MaskCpuFlags(benchmark_cpu_info);
fbarchard@google.com's avatar
fbarchard@google.com committed
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBSobel(src_argb_a + off, kStride,
              dst_argb_opt, kStride,
              width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1903 1904 1905
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
fbarchard@google.com's avatar
fbarchard@google.com committed
1906 1907 1908
  return max_diff;
}

1909
TEST_F(LibYUVPlanarTest, ARGBSobel_Any) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1910
  int max_diff = TestSobel(benchmark_width_ - 1, benchmark_height_,
1911 1912
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1913
  EXPECT_EQ(0, max_diff);
fbarchard@google.com's avatar
fbarchard@google.com committed
1914 1915
}

1916
TEST_F(LibYUVPlanarTest, ARGBSobel_Unaligned) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1917
  int max_diff = TestSobel(benchmark_width_, benchmark_height_,
1918 1919
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
1920
  EXPECT_EQ(0, max_diff);
fbarchard@google.com's avatar
fbarchard@google.com committed
1921 1922
}

1923
TEST_F(LibYUVPlanarTest, ARGBSobel_Invert) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1924
  int max_diff = TestSobel(benchmark_width_, benchmark_height_,
1925 1926
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
1927
  EXPECT_EQ(0, max_diff);
fbarchard@google.com's avatar
fbarchard@google.com committed
1928 1929
}

1930
TEST_F(LibYUVPlanarTest, ARGBSobel_Opt) {
fbarchard@google.com's avatar
fbarchard@google.com committed
1931
  int max_diff = TestSobel(benchmark_width_, benchmark_height_,
1932 1933
                           benchmark_iterations_,
                           disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
1934
  EXPECT_EQ(0, max_diff);
fbarchard@google.com's avatar
fbarchard@google.com committed
1935 1936
}

1937
static int TestSobelToPlane(int width, int height, int benchmark_iterations,
1938 1939
                            int disable_cpu_flags, int benchmark_cpu_info,
                            int invert, int off) {
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
  if (width < 1) {
    width = 1;
  }
  const int kSrcBpp = 4;
  const int kDstBpp = 1;
  const int kSrcStride = (width * kSrcBpp + 15) & ~15;
  const int kDstStride = (width * kDstBpp + 15) & ~15;
  align_buffer_64(src_argb_a, kSrcStride * height + off);
  align_buffer_64(dst_argb_c, kDstStride * height);
  align_buffer_64(dst_argb_opt, kDstStride * height);
  memset(src_argb_a, 0, kSrcStride * height + off);
  for (int i = 0; i < kSrcStride * height; ++i) {
1952
    src_argb_a[i + off] = (fastrand() & 0xff);
1953 1954 1955 1956
  }
  memset(dst_argb_c, 0, kDstStride * height);
  memset(dst_argb_opt, 0, kDstStride * height);

1957
  MaskCpuFlags(disable_cpu_flags);
1958 1959 1960
  ARGBSobelToPlane(src_argb_a + off, kSrcStride,
                   dst_argb_c, kDstStride,
                   width, invert * height);
1961
  MaskCpuFlags(benchmark_cpu_info);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBSobelToPlane(src_argb_a + off, kSrcStride,
                     dst_argb_opt, kDstStride,
                     width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kDstStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
1976 1977 1978
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
1979 1980 1981
  return max_diff;
}

1982
TEST_F(LibYUVPlanarTest, ARGBSobelToPlane_Any) {
1983
  int max_diff = TestSobelToPlane(benchmark_width_ - 1, benchmark_height_,
1984 1985
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
1986
                                  +1, 0);
1987 1988 1989
  EXPECT_EQ(0, max_diff);
}

1990
TEST_F(LibYUVPlanarTest, ARGBSobelToPlane_Unaligned) {
1991
  int max_diff = TestSobelToPlane(benchmark_width_, benchmark_height_,
1992 1993
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
1994
                                  +1, 1);
1995 1996 1997
  EXPECT_EQ(0, max_diff);
}

1998
TEST_F(LibYUVPlanarTest, ARGBSobelToPlane_Invert) {
1999
  int max_diff = TestSobelToPlane(benchmark_width_, benchmark_height_,
2000 2001
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
2002
                                  -1, 0);
2003 2004 2005
  EXPECT_EQ(0, max_diff);
}

2006
TEST_F(LibYUVPlanarTest, ARGBSobelToPlane_Opt) {
2007
  int max_diff = TestSobelToPlane(benchmark_width_, benchmark_height_,
2008 2009
                                  benchmark_iterations_,
                                  disable_cpu_flags_, benchmark_cpu_info_,
2010
                                  +1, 0);
2011 2012 2013
  EXPECT_EQ(0, max_diff);
}

2014
static int TestSobelXY(int width, int height, int benchmark_iterations,
2015 2016
                       int disable_cpu_flags, int benchmark_cpu_info,
                       int invert, int off) {
2017 2018 2019
  if (width < 1) {
    width = 1;
  }
2020
  const int kBpp = 4;
2021
  const int kStride = width * kBpp;
2022 2023 2024
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
2025
  memset(src_argb_a, 0, kStride * height + off);
2026
  for (int i = 0; i < kStride * height; ++i) {
2027
    src_argb_a[i + off] = (fastrand() & 0xff);
2028 2029 2030 2031
  }
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

2032
  MaskCpuFlags(disable_cpu_flags);
2033 2034 2035
  ARGBSobelXY(src_argb_a + off, kStride,
            dst_argb_c, kStride,
            width, invert * height);
2036
  MaskCpuFlags(benchmark_cpu_info);
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBSobelXY(src_argb_a + off, kStride,
              dst_argb_opt, kStride,
              width, invert * height);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
2051 2052 2053
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
2054 2055 2056
  return max_diff;
}

2057
TEST_F(LibYUVPlanarTest, ARGBSobelXY_Any) {
2058
  int max_diff = TestSobelXY(benchmark_width_ - 1, benchmark_height_,
2059 2060
                             benchmark_iterations_,
                             disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
2061
  EXPECT_EQ(0, max_diff);
2062 2063
}

2064
TEST_F(LibYUVPlanarTest, ARGBSobelXY_Unaligned) {
2065
  int max_diff = TestSobelXY(benchmark_width_, benchmark_height_,
2066 2067
                             benchmark_iterations_,
                             disable_cpu_flags_, benchmark_cpu_info_, +1, 1);
2068
  EXPECT_EQ(0, max_diff);
2069 2070
}

2071
TEST_F(LibYUVPlanarTest, ARGBSobelXY_Invert) {
2072
  int max_diff = TestSobelXY(benchmark_width_, benchmark_height_,
2073 2074
                             benchmark_iterations_,
                             disable_cpu_flags_, benchmark_cpu_info_, -1, 0);
2075
  EXPECT_EQ(0, max_diff);
2076 2077
}

2078
TEST_F(LibYUVPlanarTest, ARGBSobelXY_Opt) {
2079
  int max_diff = TestSobelXY(benchmark_width_, benchmark_height_,
2080 2081
                             benchmark_iterations_,
                             disable_cpu_flags_, benchmark_cpu_info_, +1, 0);
2082
  EXPECT_EQ(0, max_diff);
2083 2084
}

2085
static int TestBlur(int width, int height, int benchmark_iterations,
2086 2087
                    int disable_cpu_flags, int benchmark_cpu_info,
                    int invert, int off, int radius) {
2088 2089 2090 2091
  if (width < 1) {
    width = 1;
  }
  const int kBpp = 4;
2092
  const int kStride = width * kBpp;
2093 2094 2095 2096 2097
  align_buffer_64(src_argb_a, kStride * height + off);
  align_buffer_64(dst_cumsum, width * height * 16);
  align_buffer_64(dst_argb_c, kStride * height);
  align_buffer_64(dst_argb_opt, kStride * height);
  for (int i = 0; i < kStride * height; ++i) {
2098
    src_argb_a[i + off] = (fastrand() & 0xff);
2099 2100 2101 2102 2103
  }
  memset(dst_cumsum, 0, width * height * 16);
  memset(dst_argb_c, 0, kStride * height);
  memset(dst_argb_opt, 0, kStride * height);

2104
  MaskCpuFlags(disable_cpu_flags);
2105 2106 2107 2108
  ARGBBlur(src_argb_a + off, kStride,
           dst_argb_c, kStride,
           reinterpret_cast<int32*>(dst_cumsum), width * 4,
           width, invert * height, radius);
2109
  MaskCpuFlags(benchmark_cpu_info);
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
  for (int i = 0; i < benchmark_iterations; ++i) {
    ARGBBlur(src_argb_a + off, kStride,
             dst_argb_opt, kStride,
             reinterpret_cast<int32*>(dst_cumsum), width * 4,
             width, invert * height, radius);
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i]) -
            static_cast<int>(dst_argb_opt[i]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
2125 2126 2127 2128
  free_aligned_buffer_64(src_argb_a);
  free_aligned_buffer_64(dst_cumsum);
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
2129 2130 2131
  return max_diff;
}

2132
static const int kBlurSize = 55;
2133
TEST_F(LibYUVPlanarTest, ARGBBlur_Any) {
2134
  int max_diff = TestBlur(benchmark_width_ - 1, benchmark_height_,
2135 2136
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2137
                          +1, 0, kBlurSize);
2138 2139 2140
  EXPECT_LE(max_diff, 1);
}

2141
TEST_F(LibYUVPlanarTest, ARGBBlur_Unaligned) {
2142
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2143 2144
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2145
                          +1, 1, kBlurSize);
2146 2147 2148
  EXPECT_LE(max_diff, 1);
}

2149
TEST_F(LibYUVPlanarTest, ARGBBlur_Invert) {
2150
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2151 2152
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2153
                          -1, 0, kBlurSize);
2154 2155 2156
  EXPECT_LE(max_diff, 1);
}

2157
TEST_F(LibYUVPlanarTest, ARGBBlur_Opt) {
2158
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2159 2160
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2161
                          +1, 0, kBlurSize);
2162 2163 2164
  EXPECT_LE(max_diff, 1);
}

2165
static const int kBlurSmallSize = 5;
2166
TEST_F(LibYUVPlanarTest, ARGBBlurSmall_Any) {
2167
  int max_diff = TestBlur(benchmark_width_ - 1, benchmark_height_,
2168 2169
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2170
                          +1, 0, kBlurSmallSize);
2171 2172 2173
  EXPECT_LE(max_diff, 1);
}

2174
TEST_F(LibYUVPlanarTest, ARGBBlurSmall_Unaligned) {
2175
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2176 2177
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2178
                          +1, 1, kBlurSmallSize);
2179 2180 2181
  EXPECT_LE(max_diff, 1);
}

2182
TEST_F(LibYUVPlanarTest, ARGBBlurSmall_Invert) {
2183
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2184 2185
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2186
                          -1, 0, kBlurSmallSize);
2187 2188 2189
  EXPECT_LE(max_diff, 1);
}

2190
TEST_F(LibYUVPlanarTest, ARGBBlurSmall_Opt) {
2191
  int max_diff = TestBlur(benchmark_width_, benchmark_height_,
2192 2193
                          benchmark_iterations_,
                          disable_cpu_flags_, benchmark_cpu_info_,
2194
                          +1, 0, kBlurSmallSize);
2195 2196 2197
  EXPECT_LE(max_diff, 1);
}

2198
TEST_F(LibYUVPlanarTest, TestARGBPolynomial) {
2199
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
2200 2201
  SIMD_ALIGNED(uint8 dst_pixels_opt[1280][4]);
  SIMD_ALIGNED(uint8 dst_pixels_c[1280][4]);
2202
  memset(orig_pixels, 0, sizeof(orig_pixels));
2203

2204
  SIMD_ALIGNED(static const float kWarmifyPolynomial[16]) = {
2205 2206 2207 2208
    0.94230f,  -3.03300f,    -2.92500f,  0.f,  // C0
    0.584500f,  1.112000f,    1.535000f, 1.f,  // C1 x
    0.001313f, -0.002503f,   -0.004496f, 0.f,  // C2 x * x
    0.0f,       0.000006965f, 0.000008781f, 0.f,  // C3 x * x * x
2209
  };
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225

  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
2226 2227 2228 2229 2230
  // Test white
  orig_pixels[3][0] = 255u;
  orig_pixels[3][1] = 255u;
  orig_pixels[3][2] = 255u;
  orig_pixels[3][3] = 255u;
2231
  // Test color
2232 2233 2234 2235
  orig_pixels[4][0] = 16u;
  orig_pixels[4][1] = 64u;
  orig_pixels[4][2] = 192u;
  orig_pixels[4][3] = 224u;
2236
  // Do 16 to test asm version.
2237
  ARGBPolynomial(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
2238
                 &kWarmifyPolynomial[0], 16, 1);
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
  EXPECT_EQ(235u, dst_pixels_opt[0][0]);
  EXPECT_EQ(0u, dst_pixels_opt[0][1]);
  EXPECT_EQ(0u, dst_pixels_opt[0][2]);
  EXPECT_EQ(128u, dst_pixels_opt[0][3]);
  EXPECT_EQ(0u, dst_pixels_opt[1][0]);
  EXPECT_EQ(233u, dst_pixels_opt[1][1]);
  EXPECT_EQ(0u, dst_pixels_opt[1][2]);
  EXPECT_EQ(0u, dst_pixels_opt[1][3]);
  EXPECT_EQ(0u, dst_pixels_opt[2][0]);
  EXPECT_EQ(0u, dst_pixels_opt[2][1]);
  EXPECT_EQ(241u, dst_pixels_opt[2][2]);
  EXPECT_EQ(255u, dst_pixels_opt[2][3]);
  EXPECT_EQ(235u, dst_pixels_opt[3][0]);
  EXPECT_EQ(233u, dst_pixels_opt[3][1]);
  EXPECT_EQ(241u, dst_pixels_opt[3][2]);
  EXPECT_EQ(255u, dst_pixels_opt[3][3]);
  EXPECT_EQ(10u, dst_pixels_opt[4][0]);
  EXPECT_EQ(59u, dst_pixels_opt[4][1]);
  EXPECT_EQ(188u, dst_pixels_opt[4][2]);
  EXPECT_EQ(224u, dst_pixels_opt[4][3]);
2259 2260 2261 2262 2263 2264 2265

  for (int i = 0; i < 1280; ++i) {
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
2266

2267
  MaskCpuFlags(disable_cpu_flags_);
2268 2269
  ARGBPolynomial(&orig_pixels[0][0], 0, &dst_pixels_c[0][0], 0,
                 &kWarmifyPolynomial[0], 1280, 1);
2270
  MaskCpuFlags(benchmark_cpu_info_);
2271

2272
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
2273
    ARGBPolynomial(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
2274 2275
                   &kWarmifyPolynomial[0], 1280, 1);
  }
2276 2277 2278 2279 2280 2281 2282

  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ(dst_pixels_c[i][0], dst_pixels_opt[i][0]);
    EXPECT_EQ(dst_pixels_c[i][1], dst_pixels_opt[i][1]);
    EXPECT_EQ(dst_pixels_c[i][2], dst_pixels_opt[i][2]);
    EXPECT_EQ(dst_pixels_c[i][3], dst_pixels_opt[i][3]);
  }
2283 2284
}

2285
TEST_F(LibYUVPlanarTest, TestARGBLumaColorTable) {
2286
  SIMD_ALIGNED(uint8 orig_pixels[1280][4]);
2287 2288
  SIMD_ALIGNED(uint8 dst_pixels_opt[1280][4]);
  SIMD_ALIGNED(uint8 dst_pixels_c[1280][4]);
2289
  memset(orig_pixels, 0, sizeof(orig_pixels));
2290

2291
  align_buffer_64(lumacolortable, 32768);
2292 2293
  int v = 0;
  for (int i = 0; i < 32768; ++i) {
2294
    lumacolortable[i] = v;
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
    v += 3;
  }
  // Test blue
  orig_pixels[0][0] = 255u;
  orig_pixels[0][1] = 0u;
  orig_pixels[0][2] = 0u;
  orig_pixels[0][3] = 128u;
  // Test green
  orig_pixels[1][0] = 0u;
  orig_pixels[1][1] = 255u;
  orig_pixels[1][2] = 0u;
  orig_pixels[1][3] = 0u;
  // Test red
  orig_pixels[2][0] = 0u;
  orig_pixels[2][1] = 0u;
  orig_pixels[2][2] = 255u;
  orig_pixels[2][3] = 255u;
  // Test color
  orig_pixels[3][0] = 16u;
  orig_pixels[3][1] = 64u;
  orig_pixels[3][2] = 192u;
  orig_pixels[3][3] = 224u;
  // Do 16 to test asm version.
2318
  ARGBLumaColorTable(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
2319
                     &lumacolortable[0], 16, 1);
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
  EXPECT_EQ(253u, dst_pixels_opt[0][0]);
  EXPECT_EQ(0u, dst_pixels_opt[0][1]);
  EXPECT_EQ(0u, dst_pixels_opt[0][2]);
  EXPECT_EQ(128u, dst_pixels_opt[0][3]);
  EXPECT_EQ(0u, dst_pixels_opt[1][0]);
  EXPECT_EQ(253u, dst_pixels_opt[1][1]);
  EXPECT_EQ(0u, dst_pixels_opt[1][2]);
  EXPECT_EQ(0u, dst_pixels_opt[1][3]);
  EXPECT_EQ(0u, dst_pixels_opt[2][0]);
  EXPECT_EQ(0u, dst_pixels_opt[2][1]);
  EXPECT_EQ(253u, dst_pixels_opt[2][2]);
  EXPECT_EQ(255u, dst_pixels_opt[2][3]);
  EXPECT_EQ(48u, dst_pixels_opt[3][0]);
  EXPECT_EQ(192u, dst_pixels_opt[3][1]);
  EXPECT_EQ(64u, dst_pixels_opt[3][2]);
  EXPECT_EQ(224u, dst_pixels_opt[3][3]);
2336 2337 2338 2339 2340 2341 2342

  for (int i = 0; i < 1280; ++i) {
    orig_pixels[i][0] = i;
    orig_pixels[i][1] = i / 2;
    orig_pixels[i][2] = i / 3;
    orig_pixels[i][3] = i;
  }
2343

2344
  MaskCpuFlags(disable_cpu_flags_);
2345
  ARGBLumaColorTable(&orig_pixels[0][0], 0, &dst_pixels_c[0][0], 0,
2346
                     lumacolortable, 1280, 1);
2347
  MaskCpuFlags(benchmark_cpu_info_);
2348

2349
  for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
2350
    ARGBLumaColorTable(&orig_pixels[0][0], 0, &dst_pixels_opt[0][0], 0,
2351
                       lumacolortable, 1280, 1);
2352
  }
2353 2354 2355 2356 2357 2358
  for (int i = 0; i < 1280; ++i) {
    EXPECT_EQ(dst_pixels_c[i][0], dst_pixels_opt[i][0]);
    EXPECT_EQ(dst_pixels_c[i][1], dst_pixels_opt[i][1]);
    EXPECT_EQ(dst_pixels_c[i][2], dst_pixels_opt[i][2]);
    EXPECT_EQ(dst_pixels_c[i][3], dst_pixels_opt[i][3]);
  }
2359 2360

  free_aligned_buffer_64(lumacolortable);
2361 2362
}

2363
TEST_F(LibYUVPlanarTest, TestARGBCopyAlpha) {
2364 2365 2366 2367 2368 2369 2370 2371 2372
  const int kSize = benchmark_width_ * benchmark_height_ * 4;
  align_buffer_64(orig_pixels, kSize);
  align_buffer_64(dst_pixels_opt, kSize);
  align_buffer_64(dst_pixels_c, kSize);

  MemRandomize(orig_pixels, kSize);
  MemRandomize(dst_pixels_opt, kSize);
  memcpy(dst_pixels_c, dst_pixels_opt, kSize);

2373
  MaskCpuFlags(disable_cpu_flags_);
2374 2375 2376
  ARGBCopyAlpha(orig_pixels, benchmark_width_ * 4,
                dst_pixels_c, benchmark_width_ * 4,
                benchmark_width_, benchmark_height_);
2377
  MaskCpuFlags(benchmark_cpu_info_);
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387

  for (int i = 0; i < benchmark_iterations_; ++i) {
    ARGBCopyAlpha(orig_pixels, benchmark_width_ * 4,
                  dst_pixels_opt, benchmark_width_ * 4,
                  benchmark_width_, benchmark_height_);
  }
  for (int i = 0; i < kSize; ++i) {
    EXPECT_EQ(dst_pixels_c[i], dst_pixels_opt[i]);
  }

2388 2389 2390
  free_aligned_buffer_64(dst_pixels_c);
  free_aligned_buffer_64(dst_pixels_opt);
  free_aligned_buffer_64(orig_pixels);
2391
}
2392

2393
TEST_F(LibYUVPlanarTest, TestARGBCopyYToAlpha) {
2394 2395 2396 2397 2398 2399 2400 2401 2402
  const int kPixels = benchmark_width_ * benchmark_height_;
  align_buffer_64(orig_pixels, kPixels);
  align_buffer_64(dst_pixels_opt, kPixels * 4);
  align_buffer_64(dst_pixels_c, kPixels * 4);

  MemRandomize(orig_pixels, kPixels);
  MemRandomize(dst_pixels_opt, kPixels * 4);
  memcpy(dst_pixels_c, dst_pixels_opt, kPixels * 4);

2403
  MaskCpuFlags(disable_cpu_flags_);
2404 2405 2406
  ARGBCopyYToAlpha(orig_pixels, benchmark_width_,
                   dst_pixels_c, benchmark_width_ * 4,
                   benchmark_width_, benchmark_height_);
2407
  MaskCpuFlags(benchmark_cpu_info_);
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

  for (int i = 0; i < benchmark_iterations_; ++i) {
    ARGBCopyYToAlpha(orig_pixels, benchmark_width_,
                     dst_pixels_opt, benchmark_width_ * 4,
                     benchmark_width_, benchmark_height_);
  }
  for (int i = 0; i < kPixels * 4; ++i) {
    EXPECT_EQ(dst_pixels_c[i], dst_pixels_opt[i]);
  }

2418 2419 2420
  free_aligned_buffer_64(dst_pixels_c);
  free_aligned_buffer_64(dst_pixels_opt);
  free_aligned_buffer_64(orig_pixels);
2421 2422
}

2423
static int TestARGBRect(int width, int height, int benchmark_iterations,
2424 2425
                        int disable_cpu_flags, int benchmark_cpu_info,
                        int invert, int off, int bpp) {
2426 2427 2428
  if (width < 1) {
    width = 1;
  }
2429
  const int kStride = width * bpp;
2430
  const int kSize = kStride * height;
2431
  const uint32 v32 = fastrand() & (bpp == 4 ? 0xffffffff : 0xff);
2432 2433 2434 2435 2436 2437 2438

  align_buffer_64(dst_argb_c, kSize + off);
  align_buffer_64(dst_argb_opt, kSize + off);

  MemRandomize(dst_argb_c + off, kSize);
  memcpy(dst_argb_opt + off, dst_argb_c + off, kSize);

2439
  MaskCpuFlags(disable_cpu_flags);
2440 2441 2442 2443 2444 2445
  if (bpp == 4) {
    ARGBRect(dst_argb_c + off, kStride, 0, 0, width, invert * height, v32);
  } else {
    SetPlane(dst_argb_c + off, kStride, width, invert * height, v32);
  }

2446
  MaskCpuFlags(benchmark_cpu_info);
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  for (int i = 0; i < benchmark_iterations; ++i) {
    if (bpp == 4) {
      ARGBRect(dst_argb_opt + off, kStride, 0, 0, width, invert * height, v32);
    } else {
      SetPlane(dst_argb_opt + off, kStride, width, invert * height, v32);
    }
  }
  int max_diff = 0;
  for (int i = 0; i < kStride * height; ++i) {
    int abs_diff =
        abs(static_cast<int>(dst_argb_c[i + off]) -
            static_cast<int>(dst_argb_opt[i + off]));
    if (abs_diff > max_diff) {
      max_diff = abs_diff;
    }
  }
  free_aligned_buffer_64(dst_argb_c);
  free_aligned_buffer_64(dst_argb_opt);
  return max_diff;
}

2468
TEST_F(LibYUVPlanarTest, ARGBRect_Any) {
2469
  int max_diff = TestARGBRect(benchmark_width_ - 1, benchmark_height_,
2470 2471
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2472
                              +1, 0, 4);
2473 2474 2475
  EXPECT_EQ(0, max_diff);
}

2476
TEST_F(LibYUVPlanarTest, ARGBRect_Unaligned) {
2477
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2478 2479
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2480
                              +1, 1, 4);
2481 2482 2483
  EXPECT_EQ(0, max_diff);
}

2484
TEST_F(LibYUVPlanarTest, ARGBRect_Invert) {
2485
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2486 2487
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2488
                              -1, 0, 4);
2489 2490 2491
  EXPECT_EQ(0, max_diff);
}

2492
TEST_F(LibYUVPlanarTest, ARGBRect_Opt) {
2493
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2494 2495
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2496
                              +1, 0, 4);
2497 2498 2499
  EXPECT_EQ(0, max_diff);
}

2500
TEST_F(LibYUVPlanarTest, SetPlane_Any) {
2501
  int max_diff = TestARGBRect(benchmark_width_ - 1, benchmark_height_,
2502 2503
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2504
                              +1, 0, 1);
2505 2506 2507
  EXPECT_EQ(0, max_diff);
}

2508
TEST_F(LibYUVPlanarTest, SetPlane_Unaligned) {
2509
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2510 2511
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2512
                              +1, 1, 1);
2513 2514 2515
  EXPECT_EQ(0, max_diff);
}

2516
TEST_F(LibYUVPlanarTest, SetPlane_Invert) {
2517
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2518 2519
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2520
                              -1, 0, 1);
2521 2522 2523
  EXPECT_EQ(0, max_diff);
}

2524
TEST_F(LibYUVPlanarTest, SetPlane_Opt) {
2525
  int max_diff = TestARGBRect(benchmark_width_, benchmark_height_,
2526 2527
                              benchmark_iterations_,
                              disable_cpu_flags_, benchmark_cpu_info_,
2528
                              +1, 0, 1);
2529 2530 2531
  EXPECT_EQ(0, max_diff);
}

2532
}  // namespace libyuv