summaryrefslogtreecommitdiff
path: root/unit_test/convert_test.cc
blob: 99480742b70dc45fb730d0ef95734856f7b170ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
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
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
/*
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
 *
 *  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
 *  in the file PATENTS. All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

#include "libyuv/compare.h"
#include "libyuv/convert.h"
#include "libyuv/convert_argb.h"
#include "libyuv/convert_from.h"
#include "libyuv/convert_from_argb.h"
#include "libyuv/cpu_id.h"
#include "libyuv/format_conversion.h"
#ifdef HAVE_JPEG
#include "libyuv/mjpeg_decoder.h"
#endif
#include "libyuv/planar_functions.h"
#include "libyuv/rotate.h"
#include "libyuv/row.h"
#include "libyuv/video_common.h"
#include "../unit_test/unit_test.h"

#if defined(_MSC_VER)
#define SIMD_ALIGNED(var) __declspec(align(16)) var
#else  // __GNUC__
#define SIMD_ALIGNED(var) var __attribute__((aligned(16)))
#endif

namespace libyuv {

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

#define TESTPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,           \
                       FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, W1280, N, NEG, OFF)   \
TEST_F(libyuvTest, SRC_FMT_PLANAR##To##FMT_PLANAR##N) {                        \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  align_buffer_64(src_y, kWidth * kHeight + OFF);                              \
  align_buffer_64(src_u,                                                       \
                  SUBSAMPLE(kWidth, SRC_SUBSAMP_X) *                           \
                  SUBSAMPLE(kHeight, SRC_SUBSAMP_Y) + OFF);                    \
  align_buffer_64(src_v,                                                       \
                  SUBSAMPLE(kWidth, SRC_SUBSAMP_X) *                           \
                  SUBSAMPLE(kHeight, SRC_SUBSAMP_Y) + OFF);                    \
  align_buffer_64(dst_y_c, kWidth * kHeight);                                  \
  align_buffer_64(dst_u_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_y_opt, kWidth * kHeight);                                \
  align_buffer_64(dst_u_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kWidth; ++j)                                           \
      src_y[(i * kWidth) + j + OFF] = (random() & 0xff);                       \
  for (int i = 0; i < SUBSAMPLE(kHeight, SRC_SUBSAMP_Y); ++i) {                \
    for (int j = 0; j < SUBSAMPLE(kWidth, SRC_SUBSAMP_X); ++j) {               \
      src_u[(i * SUBSAMPLE(kWidth, SRC_SUBSAMP_X)) + j + OFF] =                \
          (random() & 0xff);                                                   \
      src_v[(i * SUBSAMPLE(kWidth, SRC_SUBSAMP_X)) + j + OFF] =                \
          (random() & 0xff);                                                   \
    }                                                                          \
  }                                                                            \
  memset(dst_y_c, 1, kWidth * kHeight);                                        \
  memset(dst_u_c, 2, SUBSAMPLE(kWidth, SUBSAMP_X) *                            \
                     SUBSAMPLE(kHeight, SUBSAMP_Y));                           \
  memset(dst_v_c, 3, SUBSAMPLE(kWidth, SUBSAMP_X) *                            \
                     SUBSAMPLE(kHeight, SUBSAMP_Y));                           \
  memset(dst_y_opt, 101, kWidth * kHeight);                                    \
  memset(dst_u_opt, 102, SUBSAMPLE(kWidth, SUBSAMP_X) *                        \
                         SUBSAMPLE(kHeight, SUBSAMP_Y));                       \
  memset(dst_v_opt, 103, SUBSAMPLE(kWidth, SUBSAMP_X) *                        \
                         SUBSAMPLE(kHeight, SUBSAMP_Y));                       \
  MaskCpuFlags(0);                                                             \
  SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                          \
                                 src_u + OFF,                                  \
                                 SUBSAMPLE(kWidth, SRC_SUBSAMP_X),             \
                                 src_v + OFF,                                  \
                                 SUBSAMPLE(kWidth, SRC_SUBSAMP_X),             \
                                 dst_y_c, kWidth,                              \
                                 dst_u_c, SUBSAMPLE(kWidth, SUBSAMP_X),        \
                                 dst_v_c, SUBSAMPLE(kWidth, SUBSAMP_X),        \
                                 kWidth, NEG kHeight);                         \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                        \
                                   src_u + OFF,                                \
                                       SUBSAMPLE(kWidth, SRC_SUBSAMP_X),       \
                                   src_v + OFF,                                \
                                       SUBSAMPLE(kWidth, SRC_SUBSAMP_X),       \
                                   dst_y_opt, kWidth,                          \
                                   dst_u_opt, SUBSAMPLE(kWidth, SUBSAMP_X),    \
                                   dst_v_opt, SUBSAMPLE(kWidth, SUBSAMP_X),    \
                                   kWidth, NEG kHeight);                       \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth; ++j) {                                         \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_y_c[i * kWidth + j]) -                      \
              static_cast<int>(dst_y_opt[i * kWidth + j]));                    \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_EQ(0, max_diff);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_u_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_u_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 3);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_v_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_v_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 3);                                                      \
  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);                                           \
  free_aligned_buffer_64(src_y);                                               \
  free_aligned_buffer_64(src_u);                                               \
  free_aligned_buffer_64(src_v);                                               \
}

#define TESTPLANARTOP(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,            \
                      FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y)                        \
    TESTPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,               \
                   FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                           \
                   benchmark_width_ - 4, _Any, +, 0)                           \
    TESTPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,               \
                   FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                           \
                   benchmark_width_, _Unaligned, +, 1)                         \
    TESTPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,               \
                   FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                           \
                   benchmark_width_, _Invert, -, 0)                            \
    TESTPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,               \
                   FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                           \
                   benchmark_width_, _Opt, +, 0)

TESTPLANARTOP(I420, 2, 2, I420, 2, 2)
TESTPLANARTOP(I422, 2, 1, I420, 2, 2)
TESTPLANARTOP(I444, 1, 1, I420, 2, 2)
TESTPLANARTOP(I411, 4, 1, I420, 2, 2)
TESTPLANARTOP(I420, 2, 2, I422, 2, 1)
TESTPLANARTOP(I420, 2, 2, I444, 1, 1)
TESTPLANARTOP(I420, 2, 2, I411, 4, 1)
TESTPLANARTOP(I420, 2, 2, I420Mirror, 2, 2)
TESTPLANARTOP(I422, 2, 1, I422, 2, 1)
TESTPLANARTOP(I444, 1, 1, I444, 1, 1)

#define TESTPLANARTOBPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,          \
                       FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, W1280, N, NEG, OFF)   \
TEST_F(libyuvTest, SRC_FMT_PLANAR##To##FMT_PLANAR##N) {                        \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  align_buffer_64(src_y, kWidth * kHeight + OFF);                              \
  align_buffer_64(src_u,                                                       \
                  SUBSAMPLE(kWidth, SRC_SUBSAMP_X) *                           \
                  SUBSAMPLE(kHeight, SRC_SUBSAMP_Y) + OFF);                    \
  align_buffer_64(src_v,                                                       \
                  SUBSAMPLE(kWidth, SRC_SUBSAMP_X) *                           \
                  SUBSAMPLE(kHeight, SRC_SUBSAMP_Y) + OFF);                    \
  align_buffer_64(dst_y_c, kWidth * kHeight);                                  \
  align_buffer_64(dst_uv_c, SUBSAMPLE(kWidth * 2, SUBSAMP_X) *                 \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_y_opt, kWidth * kHeight);                                \
  align_buffer_64(dst_uv_opt, SUBSAMPLE(kWidth * 2, SUBSAMP_X) *               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kWidth; ++j)                                           \
      src_y[(i * kWidth) + j + OFF] = (random() & 0xff);                       \
  for (int i = 0; i < SUBSAMPLE(kHeight, SRC_SUBSAMP_Y); ++i) {                \
    for (int j = 0; j < SUBSAMPLE(kWidth, SRC_SUBSAMP_X); ++j) {               \
      src_u[(i * SUBSAMPLE(kWidth, SRC_SUBSAMP_X)) + j + OFF] =                \
          (random() & 0xff);                                                   \
      src_v[(i * SUBSAMPLE(kWidth, SRC_SUBSAMP_X)) + j + OFF] =                \
          (random() & 0xff);                                                   \
    }                                                                          \
  }                                                                            \
  memset(dst_y_c, 1, kWidth * kHeight);                                        \
  memset(dst_uv_c, 2, SUBSAMPLE(kWidth * 2, SUBSAMP_X) *                       \
                      SUBSAMPLE(kHeight, SUBSAMP_Y));                          \
  memset(dst_y_opt, 101, kWidth * kHeight);                                    \
  memset(dst_uv_opt, 102, SUBSAMPLE(kWidth * 2, SUBSAMP_X) *                   \
                          SUBSAMPLE(kHeight, SUBSAMP_Y));                      \
  MaskCpuFlags(0);                                                             \
  SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                          \
                                 src_u + OFF,                                  \
                                 SUBSAMPLE(kWidth, SRC_SUBSAMP_X),             \
                                 src_v + OFF,                                  \
                                 SUBSAMPLE(kWidth, SRC_SUBSAMP_X),             \
                                 dst_y_c, kWidth,                              \
                                 dst_uv_c, SUBSAMPLE(kWidth * 2, SUBSAMP_X),   \
                                 kWidth, NEG kHeight);                         \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                        \
                                   src_u + OFF,                                \
                                   SUBSAMPLE(kWidth, SRC_SUBSAMP_X),           \
                                   src_v + OFF,                                \
                                   SUBSAMPLE(kWidth, SRC_SUBSAMP_X),           \
                                   dst_y_opt, kWidth,                          \
                                   dst_uv_opt,                                 \
                                   SUBSAMPLE(kWidth * 2, SUBSAMP_X),           \
                                   kWidth, NEG kHeight);                       \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth; ++j) {                                         \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_y_c[i * kWidth + j]) -                      \
              static_cast<int>(dst_y_opt[i * kWidth + j]));                    \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 1);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth * 2, SUBSAMP_X); ++j) {               \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_uv_c[i *                                    \
                               SUBSAMPLE(kWidth * 2, SUBSAMP_X) + j]) -        \
              static_cast<int>(dst_uv_opt[i *                                  \
                               SUBSAMPLE(kWidth * 2, SUBSAMP_X) + j]));        \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 1);                                                      \
  free_aligned_buffer_64(dst_y_c);                                             \
  free_aligned_buffer_64(dst_uv_c);                                            \
  free_aligned_buffer_64(dst_y_opt);                                           \
  free_aligned_buffer_64(dst_uv_opt);                                          \
  free_aligned_buffer_64(src_y);                                               \
  free_aligned_buffer_64(src_u);                                               \
  free_aligned_buffer_64(src_v);                                               \
}

#define TESTPLANARTOBP(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,           \
                       FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y)                       \
    TESTPLANARTOBPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,              \
                    FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                          \
                    benchmark_width_ - 4, _Any, +, 0)                          \
    TESTPLANARTOBPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,              \
                    FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                          \
                    benchmark_width_, _Unaligned, +, 1)                        \
    TESTPLANARTOBPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,              \
                    FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                          \
                    benchmark_width_, _Invert, -, 0)                           \
    TESTPLANARTOBPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,              \
                    FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                          \
                    benchmark_width_, _Opt, +, 0)

TESTPLANARTOBP(I420, 2, 2, NV12, 2, 2)
TESTPLANARTOBP(I420, 2, 2, NV21, 2, 2)

#define TESTBIPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,         \
                         FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, W1280, N, NEG, OFF) \
TEST_F(libyuvTest, SRC_FMT_PLANAR##To##FMT_PLANAR##N) {                        \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  align_buffer_64(src_y, kWidth * kHeight + OFF);                              \
  align_buffer_64(src_uv, 2 * SUBSAMPLE(kWidth, SRC_SUBSAMP_X) *               \
                  SUBSAMPLE(kHeight, SRC_SUBSAMP_Y) + OFF);                    \
  align_buffer_64(dst_y_c, kWidth * kHeight);                                  \
  align_buffer_64(dst_u_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_y_opt, kWidth * kHeight);                                \
  align_buffer_64(dst_u_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kWidth; ++j)                                           \
      src_y[(i * kWidth) + j + OFF] = (random() & 0xff);                       \
  for (int i = 0; i < SUBSAMPLE(kHeight, SRC_SUBSAMP_Y); ++i) {                \
    for (int j = 0; j < 2 * SUBSAMPLE(kWidth, SRC_SUBSAMP_X); ++j) {           \
      src_uv[(i * 2 * SUBSAMPLE(kWidth, SRC_SUBSAMP_X)) + j + OFF] =           \
          (random() & 0xff);                                                   \
    }                                                                          \
  }                                                                            \
  memset(dst_y_c, 1, kWidth * kHeight);                                        \
  memset(dst_u_c, 2, SUBSAMPLE(kWidth, SUBSAMP_X) *                            \
                     SUBSAMPLE(kHeight, SUBSAMP_Y));                           \
  memset(dst_v_c, 3, SUBSAMPLE(kWidth, SUBSAMP_X) *                            \
                     SUBSAMPLE(kHeight, SUBSAMP_Y));                           \
  memset(dst_y_opt, 101, kWidth * kHeight);                                    \
  memset(dst_u_opt, 102, SUBSAMPLE(kWidth, SUBSAMP_X) *                        \
                         SUBSAMPLE(kHeight, SUBSAMP_Y));                       \
  memset(dst_v_opt, 103, SUBSAMPLE(kWidth, SUBSAMP_X) *                        \
                         SUBSAMPLE(kHeight, SUBSAMP_Y));                       \
  MaskCpuFlags(0);                                                             \
  SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                          \
                                 src_uv + OFF,                                 \
                                 2 * SUBSAMPLE(kWidth, SRC_SUBSAMP_X),         \
                                 dst_y_c, kWidth,                              \
                                 dst_u_c, SUBSAMPLE(kWidth, SUBSAMP_X),        \
                                 dst_v_c, SUBSAMPLE(kWidth, SUBSAMP_X),        \
                                 kWidth, NEG kHeight);                         \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    SRC_FMT_PLANAR##To##FMT_PLANAR(src_y + OFF, kWidth,                        \
                                   src_uv + OFF,                               \
                                   2 * SUBSAMPLE(kWidth, SRC_SUBSAMP_X),       \
                                   dst_y_opt, kWidth,                          \
                                   dst_u_opt, SUBSAMPLE(kWidth, SUBSAMP_X),    \
                                   dst_v_opt, SUBSAMPLE(kWidth, SUBSAMP_X),    \
                                   kWidth, NEG kHeight);                       \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth; ++j) {                                         \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_y_c[i * kWidth + j]) -                      \
              static_cast<int>(dst_y_opt[i * kWidth + j]));                    \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 1);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_u_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_u_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 1);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_v_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_v_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 1);                                                      \
  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);                                           \
  free_aligned_buffer_64(src_y);                                               \
  free_aligned_buffer_64(src_uv);                                              \
}

#define TESTBIPLANARTOP(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,          \
                        FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y)                      \
    TESTBIPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,             \
                     FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                         \
                     benchmark_width_ - 4, _Any, +, 0)                         \
    TESTBIPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,             \
                     FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                         \
                     benchmark_width_, _Unaligned, +, 1)                       \
    TESTBIPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,             \
                     FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                         \
                     benchmark_width_, _Invert, -, 0)                          \
    TESTBIPLANARTOPI(SRC_FMT_PLANAR, SRC_SUBSAMP_X, SRC_SUBSAMP_Y,             \
                     FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,                         \
                     benchmark_width_, _Opt, +, 0)

TESTBIPLANARTOP(NV12, 2, 2, I420, 2, 2)
TESTBIPLANARTOP(NV21, 2, 2, I420, 2, 2)

#define ALIGNINT(V, ALIGN) (((V) + (ALIGN) - 1) / (ALIGN) * (ALIGN))

#define TESTPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,  \
                       YALIGN, W1280, DIFF, N, NEG, OFF, FMT_C, BPP_C)         \
TEST_F(libyuvTest, FMT_PLANAR##To##FMT_B##N) {                                 \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = ALIGNINT(benchmark_height_, YALIGN);                     \
  const int kStrideB = ALIGNINT(kWidth * BPP_B, ALIGN);                        \
  const int kSizeUV =                                                          \
    SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMPLE(kHeight, SUBSAMP_Y);              \
  align_buffer_64(src_y, kWidth * kHeight + OFF);                              \
  align_buffer_64(src_u, kSizeUV + OFF);                                       \
  align_buffer_64(src_v, kSizeUV + OFF);                                       \
  align_buffer_64(dst_argb_c, kStrideB * kHeight);                             \
  align_buffer_64(dst_argb_opt, kStrideB * kHeight);                           \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kWidth * kHeight; ++i) {                                 \
    src_y[i + OFF] = (random() & 0xff);                                        \
  }                                                                            \
  for (int i = 0; i < kSizeUV; ++i) {                                          \
    src_u[i + OFF] = (random() & 0xff);                                        \
    src_v[i + OFF] = (random() & 0xff);                                        \
  }                                                                            \
  memset(dst_argb_c, 1, kStrideB * kHeight);                                   \
  memset(dst_argb_opt, 101, kStrideB * kHeight);                               \
  MaskCpuFlags(0);                                                             \
  FMT_PLANAR##To##FMT_B(src_y + OFF, kWidth,                                   \
                        src_u + OFF, SUBSAMPLE(kWidth, SUBSAMP_X),             \
                        src_v + OFF, SUBSAMPLE(kWidth, SUBSAMP_X),             \
                        dst_argb_c, kStrideB,                                  \
                        kWidth, NEG kHeight);                                  \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_PLANAR##To##FMT_B(src_y + OFF, kWidth,                                 \
                          src_u + OFF, SUBSAMPLE(kWidth, SUBSAMP_X),           \
                          src_v + OFF, SUBSAMPLE(kWidth, SUBSAMP_X),           \
                          dst_argb_opt, kStrideB,                              \
                          kWidth, NEG kHeight);                                \
  }                                                                            \
  int max_diff = 0;                                                            \
  /* Convert to ARGB so 565 is expanded to bytes that can be compared. */      \
  align_buffer_64(dst_argb32_c, kWidth * BPP_C  * kHeight);                    \
  align_buffer_64(dst_argb32_opt, kWidth * BPP_C  * kHeight);                  \
  memset(dst_argb32_c, 2, kWidth * BPP_C  * kHeight);                          \
  memset(dst_argb32_opt, 102, kWidth * BPP_C  * kHeight);                      \
  FMT_B##To##FMT_C(dst_argb_c, kStrideB,                                       \
                   dst_argb32_c, kWidth * BPP_C ,                              \
                   kWidth, kHeight);                                           \
  FMT_B##To##FMT_C(dst_argb_opt, kStrideB,                                     \
                   dst_argb32_opt, kWidth * BPP_C ,                            \
                   kWidth, kHeight);                                           \
  for (int i = 0; i < kWidth * BPP_C * kHeight; ++i) {                         \
    int abs_diff =                                                             \
        abs(static_cast<int>(dst_argb32_c[i]) -                                \
            static_cast<int>(dst_argb32_opt[i]));                              \
    if (abs_diff > max_diff) {                                                 \
      max_diff = abs_diff;                                                     \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  free_aligned_buffer_64(src_y);                                               \
  free_aligned_buffer_64(src_u);                                               \
  free_aligned_buffer_64(src_v);                                               \
  free_aligned_buffer_64(dst_argb_c);                                          \
  free_aligned_buffer_64(dst_argb_opt);                                        \
  free_aligned_buffer_64(dst_argb32_c);                                        \
  free_aligned_buffer_64(dst_argb32_opt);                                      \
}

#define TESTPLANARTOB(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,   \
                      YALIGN, DIFF, FMT_C, BPP_C)                              \
    TESTPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,      \
        YALIGN, benchmark_width_ - 4, DIFF, _Any, +, 0, FMT_C, BPP_C)          \
    TESTPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,      \
        YALIGN, benchmark_width_, DIFF, _Unaligned, +, 1, FMT_C, BPP_C)        \
    TESTPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,      \
        YALIGN, benchmark_width_, DIFF, _Invert, -, 0, FMT_C, BPP_C)           \
    TESTPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, ALIGN,      \
        YALIGN, benchmark_width_, DIFF, _Opt, +, 0, FMT_C, BPP_C)

// TODO(fbarchard): Make vertical alignment unnecessary on bayer.
TESTPLANARTOB(I420, 2, 2, ARGB, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, BGRA, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, ABGR, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, RGBA, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, RAW, 3, 3, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, RGB24, 3, 3, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, RGB565, 2, 2, 1, 9, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, ARGB1555, 2, 2, 1, 9, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, ARGB4444, 2, 2, 1, 17, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, ARGB, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, BGRA, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, ABGR, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, RGBA, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I411, 4, 1, ARGB, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I444, 1, 1, ARGB, 4, 4, 1, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, YUY2, 2, 4, 1, 1, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, UYVY, 2, 4, 1, 1, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, YUY2, 2, 4, 1, 0, ARGB, 4)
TESTPLANARTOB(I422, 2, 1, UYVY, 2, 4, 1, 0, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, I400, 1, 1, 1, 0, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, BayerBGGR, 1, 2, 2, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, BayerRGGB, 1, 2, 2, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, BayerGBRG, 1, 2, 2, 2, ARGB, 4)
TESTPLANARTOB(I420, 2, 2, BayerGRBG, 1, 2, 2, 2, ARGB, 4)

#define TESTBIPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B,       \
                         W1280, DIFF, N, NEG, OFF)                             \
TEST_F(libyuvTest, FMT_PLANAR##To##FMT_B##N) {                                 \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  const int kStrideB = kWidth * BPP_B;                                         \
  align_buffer_64(src_y, kWidth * kHeight + OFF);                              \
  align_buffer_64(src_uv,                                                      \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y) * 2 + OFF);                    \
  align_buffer_64(dst_argb_c, kStrideB * kHeight);                             \
  align_buffer_64(dst_argb_opt, kStrideB * kHeight);                           \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kWidth; ++j)                                           \
      src_y[(i * kWidth) + j + OFF] = (random() & 0xff);                       \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X) * 2; ++j) {               \
      src_uv[(i * SUBSAMPLE(kWidth, SUBSAMP_X)) * 2 + j + OFF] =               \
          (random() & 0xff);                                                   \
    }                                                                          \
  }                                                                            \
  memset(dst_argb_c, 1, kStrideB * kHeight);                                   \
  memset(dst_argb_opt, 101, kStrideB * kHeight);                               \
  MaskCpuFlags(0);                                                             \
  FMT_PLANAR##To##FMT_B(src_y + OFF, kWidth,                                   \
                        src_uv + OFF, SUBSAMPLE(kWidth, SUBSAMP_X) * 2,        \
                        dst_argb_c, kWidth * BPP_B,                            \
                        kWidth, NEG kHeight);                                  \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_PLANAR##To##FMT_B(src_y + OFF, kWidth,                                 \
                          src_uv + OFF, SUBSAMPLE(kWidth, SUBSAMP_X) * 2,      \
                          dst_argb_opt, kWidth * BPP_B,                        \
                          kWidth, NEG kHeight);                                \
  }                                                                            \
  /* Convert to ARGB so 565 is expanded to bytes that can be compared. */      \
  align_buffer_64(dst_argb32_c, kWidth * 4 * kHeight);                         \
  align_buffer_64(dst_argb32_opt, kWidth * 4 * kHeight);                       \
  memset(dst_argb32_c, 2, kWidth * 4 * kHeight);                               \
  memset(dst_argb32_opt, 102, kWidth * 4 * kHeight);                           \
  FMT_B##ToARGB(dst_argb_c, kStrideB,                                          \
                dst_argb32_c, kWidth * 4,                                      \
                kWidth, kHeight);                                              \
  FMT_B##ToARGB(dst_argb_opt, kStrideB,                                        \
                dst_argb32_opt, kWidth * 4,                                    \
                kWidth, kHeight);                                              \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth * 4; ++j) {                                     \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_argb32_c[i * kWidth * 4 + j]) -             \
              static_cast<int>(dst_argb32_opt[i * kWidth * 4 + j]));           \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  free_aligned_buffer_64(src_y);                                               \
  free_aligned_buffer_64(src_uv);                                              \
  free_aligned_buffer_64(dst_argb_c);                                          \
  free_aligned_buffer_64(dst_argb_opt);                                        \
  free_aligned_buffer_64(dst_argb32_c);                                        \
  free_aligned_buffer_64(dst_argb32_opt);                                      \
}

#define TESTBIPLANARTOB(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B, DIFF)  \
    TESTBIPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B,           \
                     benchmark_width_ - 4, DIFF, _Any, +, 0)                   \
    TESTBIPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B,           \
                     benchmark_width_, DIFF, _Unaligned, +, 1)                 \
    TESTBIPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B,           \
                     benchmark_width_, DIFF, _Invert, -, 0)                    \
    TESTBIPLANARTOBI(FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, FMT_B, BPP_B,           \
                     benchmark_width_, DIFF, _Opt, +, 0)

TESTBIPLANARTOB(NV12, 2, 2, ARGB, 4, 2)
TESTBIPLANARTOB(NV21, 2, 2, ARGB, 4, 2)
TESTBIPLANARTOB(NV12, 2, 2, RGB565, 2, 9)
TESTBIPLANARTOB(NV21, 2, 2, RGB565, 2, 9)

#define TESTATOPLANARI(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y, \
                       W1280, DIFF, N, NEG, OFF)                               \
TEST_F(libyuvTest, FMT_A##To##FMT_PLANAR##N) {                                 \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = ALIGNINT(benchmark_height_, YALIGN);                     \
  const int kStride =                                                          \
      (SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMP_X * 8 * BPP_A + 7) / 8;          \
  align_buffer_64(src_argb, kStride * kHeight + OFF);                          \
  align_buffer_64(dst_y_c, kWidth * kHeight);                                  \
  align_buffer_64(dst_u_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_c,                                                     \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_y_opt, kWidth * kHeight);                                \
  align_buffer_64(dst_u_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_v_opt,                                                   \
                  SUBSAMPLE(kWidth, SUBSAMP_X) *                               \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  memset(dst_y_c, 1, kWidth * kHeight);                                        \
  memset(dst_u_c, 2,                                                           \
         SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMPLE(kHeight, SUBSAMP_Y));        \
  memset(dst_v_c, 3,                                                           \
         SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMPLE(kHeight, SUBSAMP_Y));        \
  memset(dst_y_opt, 101, kWidth * kHeight);                                    \
  memset(dst_u_opt, 102,                                                       \
         SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMPLE(kHeight, SUBSAMP_Y));        \
  memset(dst_v_opt, 103,                                                       \
         SUBSAMPLE(kWidth, SUBSAMP_X) * SUBSAMPLE(kHeight, SUBSAMP_Y));        \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kStride; ++j)                                          \
      src_argb[(i * kStride) + j + OFF] = (random() & 0xff);                   \
  MaskCpuFlags(0);                                                             \
  FMT_A##To##FMT_PLANAR(src_argb + OFF, kStride,                               \
                        dst_y_c, kWidth,                                       \
                        dst_u_c, SUBSAMPLE(kWidth, SUBSAMP_X),                 \
                        dst_v_c, SUBSAMPLE(kWidth, SUBSAMP_X),                 \
                        kWidth, NEG kHeight);                                  \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_A##To##FMT_PLANAR(src_argb + OFF, kStride,                             \
                          dst_y_opt, kWidth,                                   \
                          dst_u_opt, SUBSAMPLE(kWidth, SUBSAMP_X),             \
                          dst_v_opt, SUBSAMPLE(kWidth, SUBSAMP_X),             \
                          kWidth, NEG kHeight);                                \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth; ++j) {                                         \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_y_c[i * kWidth + j]) -                      \
              static_cast<int>(dst_y_opt[i * kWidth + j]));                    \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_u_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_u_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {                   \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_v_c[i *                                     \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]) -            \
              static_cast<int>(dst_v_opt[i *                                   \
                               SUBSAMPLE(kWidth, SUBSAMP_X) + j]));            \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  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);                                           \
  free_aligned_buffer_64(src_argb);                                            \
}

#define TESTATOPLANAR(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,  \
                      DIFF)                                                    \
    TESTATOPLANARI(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,     \
                   benchmark_width_ - 4, DIFF, _Any, +, 0)                     \
    TESTATOPLANARI(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,     \
                   benchmark_width_, DIFF, _Unaligned, +, 1)                   \
    TESTATOPLANARI(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,     \
                   benchmark_width_, DIFF, _Invert, -, 0)                      \
    TESTATOPLANARI(FMT_A, BPP_A, YALIGN, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,     \
                   benchmark_width_, DIFF, _Opt, +, 0)

TESTATOPLANAR(ARGB, 4, 1, I420, 2, 2, 4)
#ifdef __arm__
TESTATOPLANAR(ARGB, 4, 1, J420, 2, 2, 4)
#else
TESTATOPLANAR(ARGB, 4, 1, J420, 2, 2, 0)
#endif
TESTATOPLANAR(BGRA, 4, 1, I420, 2, 2, 4)
TESTATOPLANAR(ABGR, 4, 1, I420, 2, 2, 4)
TESTATOPLANAR(RGBA, 4, 1, I420, 2, 2, 4)
TESTATOPLANAR(RAW, 3, 1, I420, 2, 2, 4)
TESTATOPLANAR(RGB24, 3, 1, I420, 2, 2, 4)
TESTATOPLANAR(RGB565, 2, 1, I420, 2, 2, 5)
// TODO(fbarchard): Make 1555 neon work same as C code, reduce to diff 9.
TESTATOPLANAR(ARGB1555, 2, 1, I420, 2, 2, 15)
TESTATOPLANAR(ARGB4444, 2, 1, I420, 2, 2, 17)
TESTATOPLANAR(ARGB, 4, 1, I411, 4, 1, 4)
TESTATOPLANAR(ARGB, 4, 1, I422, 2, 1, 2)
TESTATOPLANAR(ARGB, 4, 1, I444, 1, 1, 2)
TESTATOPLANAR(YUY2, 2, 1, I420, 2, 2, 2)
TESTATOPLANAR(UYVY, 2, 1, I420, 2, 2, 2)
TESTATOPLANAR(YUY2, 2, 1, I422, 2, 1, 2)
TESTATOPLANAR(UYVY, 2, 1, I422, 2, 1, 2)
TESTATOPLANAR(I400, 1, 1, I420, 2, 2, 2)
TESTATOPLANAR(BayerBGGR, 1, 2, I420, 2, 2, 4)
TESTATOPLANAR(BayerRGGB, 1, 2, I420, 2, 2, 4)
TESTATOPLANAR(BayerGBRG, 1, 2, I420, 2, 2, 4)
TESTATOPLANAR(BayerGRBG, 1, 2, I420, 2, 2, 4)

#define TESTATOBIPLANARI(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,       \
                       W1280, N, NEG, OFF)                                     \
TEST_F(libyuvTest, FMT_A##To##FMT_PLANAR##N) {                                 \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  const int kStride = (kWidth * 8 * BPP_A + 7) / 8;                            \
  align_buffer_64(src_argb, kStride * kHeight + OFF);                          \
  align_buffer_64(dst_y_c, kWidth * kHeight);                                  \
  align_buffer_64(dst_uv_c,                                                    \
                  SUBSAMPLE(kWidth, SUBSAMP_X) * 2 *                           \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  align_buffer_64(dst_y_opt, kWidth * kHeight);                                \
  align_buffer_64(dst_uv_opt,                                                  \
                  SUBSAMPLE(kWidth, SUBSAMP_X) * 2 *                           \
                  SUBSAMPLE(kHeight, SUBSAMP_Y));                              \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kHeight; ++i)                                            \
    for (int j = 0; j < kStride; ++j)                                          \
      src_argb[(i * kStride) + j + OFF] = (random() & 0xff);                   \
  memset(dst_y_c, 1, kWidth * kHeight);                                        \
  memset(dst_uv_c, 2, SUBSAMPLE(kWidth, SUBSAMP_X) * 2 *                       \
                      SUBSAMPLE(kHeight, SUBSAMP_Y));                          \
  memset(dst_y_opt, 101, kWidth * kHeight);                                    \
  memset(dst_uv_opt, 102, SUBSAMPLE(kWidth, SUBSAMP_X) * 2 *                   \
                        SUBSAMPLE(kHeight, SUBSAMP_Y));                        \
  MaskCpuFlags(0);                                                             \
  FMT_A##To##FMT_PLANAR(src_argb + OFF, kStride,                               \
                        dst_y_c, kWidth,                                       \
                        dst_uv_c, SUBSAMPLE(kWidth, SUBSAMP_X) * 2,            \
                        kWidth, NEG kHeight);                                  \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_A##To##FMT_PLANAR(src_argb + OFF, kStride,                             \
                          dst_y_opt, kWidth,                                   \
                          dst_uv_opt, SUBSAMPLE(kWidth, SUBSAMP_X) * 2,        \
                          kWidth, NEG kHeight);                                \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kHeight; ++i) {                                          \
    for (int j = 0; j < kWidth; ++j) {                                         \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_y_c[i * kWidth + j]) -                      \
              static_cast<int>(dst_y_opt[i * kWidth + j]));                    \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 4);                                                      \
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {                    \
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X) * 2; ++j) {               \
      int abs_diff =                                                           \
          abs(static_cast<int>(dst_uv_c[i *                                    \
                               SUBSAMPLE(kWidth, SUBSAMP_X) * 2 + j]) -        \
              static_cast<int>(dst_uv_opt[i *                                  \
                               SUBSAMPLE(kWidth, SUBSAMP_X) * 2 + j]));        \
      if (abs_diff > max_diff) {                                               \
        max_diff = abs_diff;                                                   \
      }                                                                        \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, 4);                                                      \
  free_aligned_buffer_64(dst_y_c);                                             \
  free_aligned_buffer_64(dst_uv_c);                                            \
  free_aligned_buffer_64(dst_y_opt);                                           \
  free_aligned_buffer_64(dst_uv_opt);                                          \
  free_aligned_buffer_64(src_argb);                                            \
}

#define TESTATOBIPLANAR(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y)        \
    TESTATOBIPLANARI(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,           \
                     benchmark_width_ - 4, _Any, +, 0)                         \
    TESTATOBIPLANARI(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,           \
                     benchmark_width_, _Unaligned, +, 1)                       \
    TESTATOBIPLANARI(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,           \
                     benchmark_width_, _Invert, -, 0)                          \
    TESTATOBIPLANARI(FMT_A, BPP_A, FMT_PLANAR, SUBSAMP_X, SUBSAMP_Y,           \
                     benchmark_width_, _Opt, +, 0)

TESTATOBIPLANAR(ARGB, 4, NV12, 2, 2)
TESTATOBIPLANAR(ARGB, 4, NV21, 2, 2)

#define TESTATOBI(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                            \
                  FMT_B, BPP_B, STRIDE_B, HEIGHT_B,                            \
                  W1280, DIFF, N, NEG, OFF)                                    \
TEST_F(libyuvTest, FMT_A##To##FMT_B##N) {                                      \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  const int kHeightA = (kHeight + HEIGHT_A - 1) / HEIGHT_A * HEIGHT_A;         \
  const int kHeightB = (kHeight + HEIGHT_B - 1) / HEIGHT_B * HEIGHT_B;         \
  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, kStrideA * kHeightA + OFF);                        \
  align_buffer_64(dst_argb_c, kStrideB * kHeightB);                            \
  align_buffer_64(dst_argb_opt, kStrideB * kHeightB);                          \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kStrideA * kHeightA; ++i) {                              \
    src_argb[i + OFF] = (random() & 0xff);                                     \
  }                                                                            \
  memset(dst_argb_c, 1, kStrideB * kHeightB);                                  \
  memset(dst_argb_opt, 101, kStrideB * kHeightB);                              \
  MaskCpuFlags(0);                                                             \
  FMT_A##To##FMT_B(src_argb + OFF, kStrideA,                                   \
                   dst_argb_c, kStrideB,                                       \
                   kWidth, NEG kHeight);                                       \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_A##To##FMT_B(src_argb + OFF, kStrideA,                                 \
                     dst_argb_opt, kStrideB,                                   \
                     kWidth, NEG kHeight);                                     \
  }                                                                            \
  int max_diff = 0;                                                            \
  for (int i = 0; i < kStrideB * kHeightB; ++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;                                                     \
    }                                                                          \
  }                                                                            \
  EXPECT_LE(max_diff, DIFF);                                                   \
  free_aligned_buffer_64(src_argb);                                            \
  free_aligned_buffer_64(dst_argb_c);                                          \
  free_aligned_buffer_64(dst_argb_opt);                                        \
}

#define TESTATOBRANDOM(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                       \
                       FMT_B, BPP_B, STRIDE_B, HEIGHT_B, DIFF)                 \
TEST_F(libyuvTest, FMT_A##To##FMT_B##_Random) {                                \
  srandom(time(NULL));                                                         \
  for (int times = 0; times < benchmark_iterations_; ++times) {                \
    const int kWidth = (random() & 63) + 1;                                    \
    const int kHeight = (random() & 31) + 1;                                   \
    const int kHeightA = (kHeight + HEIGHT_A - 1) / HEIGHT_A * HEIGHT_A;       \
    const int kHeightB = (kHeight + HEIGHT_B - 1) / HEIGHT_B * HEIGHT_B;       \
    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_page_end(src_argb, kStrideA * kHeightA);                      \
    align_buffer_page_end(dst_argb_c, kStrideB * kHeightB);                    \
    align_buffer_page_end(dst_argb_opt, kStrideB * kHeightB);                  \
    for (int i = 0; i < kStrideA * kHeightA; ++i) {                            \
      src_argb[i] = (random() & 0xff);                                         \
    }                                                                          \
    memset(dst_argb_c, 123, kStrideB * kHeightB);                              \
    memset(dst_argb_opt, 123, kStrideB * kHeightB);                            \
    MaskCpuFlags(0);                                                           \
    FMT_A##To##FMT_B(src_argb, kStrideA,                                       \
                     dst_argb_c, kStrideB,                                     \
                     kWidth, kHeight);                                         \
    MaskCpuFlags(-1);                                                          \
    FMT_A##To##FMT_B(src_argb, kStrideA,                                       \
                     dst_argb_opt, kStrideB,                                   \
                     kWidth, kHeight);                                         \
    int max_diff = 0;                                                          \
    for (int i = 0; i < kStrideB * kHeightB; ++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;                                                   \
      }                                                                        \
    }                                                                          \
    EXPECT_LE(max_diff, DIFF);                                                 \
    free_aligned_buffer_page_end(src_argb);                                    \
    free_aligned_buffer_page_end(dst_argb_c);                                  \
    free_aligned_buffer_page_end(dst_argb_opt);                                \
  }                                                                            \
}

#define TESTATOB(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                             \
                 FMT_B, BPP_B, STRIDE_B, HEIGHT_B, DIFF)                       \
    TESTATOBI(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                                \
              FMT_B, BPP_B, STRIDE_B, HEIGHT_B,                                \
              benchmark_width_ - 4, DIFF, _Any, +, 0)                          \
    TESTATOBI(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                                \
              FMT_B, BPP_B, STRIDE_B, HEIGHT_B,                                \
              benchmark_width_, DIFF, _Unaligned, +, 1)                        \
    TESTATOBI(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                                \
              FMT_B, BPP_B, STRIDE_B, HEIGHT_B,                                \
              benchmark_width_, DIFF, _Invert, -, 0)                           \
    TESTATOBI(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                                \
              FMT_B, BPP_B, STRIDE_B, HEIGHT_B,                                \
              benchmark_width_, DIFF, _Opt, +, 0)                              \
    TESTATOBRANDOM(FMT_A, BPP_A, STRIDE_A, HEIGHT_A,                           \
                   FMT_B, BPP_B, STRIDE_B, HEIGHT_B, DIFF)

TESTATOB(ARGB, 4, 4, 1, ARGB, 4, 4, 1, 0)
TESTATOB(ARGB, 4, 4, 1, BGRA, 4, 4, 1, 0)
TESTATOB(ARGB, 4, 4, 1, ABGR, 4, 4, 1, 0)
TESTATOB(ARGB, 4, 4, 1, RGBA, 4, 4, 1, 0)
TESTATOB(ARGB, 4, 4, 1, RAW, 3, 3, 1, 0)
TESTATOB(ARGB, 4, 4, 1, RGB24, 3, 3, 1, 0)
TESTATOB(ARGB, 4, 4, 1, RGB565, 2, 2, 1, 0)
TESTATOB(ARGB, 4, 4, 1, ARGB1555, 2, 2, 1, 0)
TESTATOB(ARGB, 4, 4, 1, ARGB4444, 2, 2, 1, 0)
TESTATOB(ARGB, 4, 4, 1, BayerBGGR, 1, 1, 1, 0)
TESTATOB(ARGB, 4, 4, 1, BayerRGGB, 1, 1, 1, 0)
TESTATOB(ARGB, 4, 4, 1, BayerGBRG, 1, 1, 1, 0)
TESTATOB(ARGB, 4, 4, 1, BayerGRBG, 1, 1, 1, 0)
TESTATOB(ARGB, 4, 4, 1, YUY2, 2, 4, 1, 4)
TESTATOB(ARGB, 4, 4, 1, UYVY, 2, 4, 1, 4)
TESTATOB(ARGB, 4, 4, 1, I400, 1, 1, 1, 2)
TESTATOB(ARGB, 4, 4, 1, J400, 1, 1, 1, 2)
TESTATOB(BGRA, 4, 4, 1, ARGB, 4, 4, 1, 0)
TESTATOB(ABGR, 4, 4, 1, ARGB, 4, 4, 1, 0)
TESTATOB(RGBA, 4, 4, 1, ARGB, 4, 4, 1, 0)
TESTATOB(RAW, 3, 3, 1, ARGB, 4, 4, 1, 0)
TESTATOB(RGB24, 3, 3, 1, ARGB, 4, 4, 1, 0)
TESTATOB(RGB565, 2, 2, 1, ARGB, 4, 4, 1, 0)
TESTATOB(ARGB1555, 2, 2, 1, ARGB, 4, 4, 1, 0)
TESTATOB(ARGB4444, 2, 2, 1, ARGB, 4, 4, 1, 0)
TESTATOB(YUY2, 2, 4, 1, ARGB, 4, 4, 1, 4)
TESTATOB(UYVY, 2, 4, 1, ARGB, 4, 4, 1, 4)
TESTATOB(BayerBGGR, 1, 2, 2, ARGB, 4, 4, 1, 0)
TESTATOB(BayerRGGB, 1, 2, 2, ARGB, 4, 4, 1, 0)
TESTATOB(BayerGBRG, 1, 2, 2, ARGB, 4, 4, 1, 0)
TESTATOB(BayerGRBG, 1, 2, 2, ARGB, 4, 4, 1, 0)
TESTATOB(I400, 1, 1, 1, ARGB, 4, 4, 1, 0)
TESTATOB(I400, 1, 1, 1, I400, 1, 1, 1, 0)
TESTATOB(I400, 1, 1, 1, I400Mirror, 1, 1, 1, 0)
TESTATOB(Y, 1, 1, 1, ARGB, 4, 4, 1, 0)
TESTATOB(ARGB, 4, 4, 1, ARGBMirror, 4, 4, 1, 0)

#define TESTSYMI(FMT_ATOB, BPP_A, STRIDE_A, HEIGHT_A,                          \
                 W1280, N, NEG, OFF)                                           \
TEST_F(libyuvTest, FMT_ATOB##_Symetric##N) {                                   \
  const int kWidth = ((W1280) > 0) ? (W1280) : 1;                              \
  const int kHeight = benchmark_height_;                                       \
  const int kHeightA = (kHeight + HEIGHT_A - 1) / HEIGHT_A * HEIGHT_A;         \
  const int kStrideA = (kWidth * BPP_A + STRIDE_A - 1) / STRIDE_A * STRIDE_A;  \
  align_buffer_64(src_argb, kStrideA * kHeightA + OFF);                        \
  align_buffer_64(dst_argb_c, kStrideA * kHeightA);                            \
  align_buffer_64(dst_argb_opt, kStrideA * kHeightA);                          \
  srandom(time(NULL));                                                         \
  for (int i = 0; i < kStrideA * kHeightA; ++i) {                              \
    src_argb[i + OFF] = (random() & 0xff);                                     \
  }                                                                            \
  memset(dst_argb_c, 1, kStrideA * kHeightA);                                  \
  memset(dst_argb_opt, 101, kStrideA * kHeightA);                              \
  MaskCpuFlags(0);                                                             \
  FMT_ATOB(src_argb + OFF, kStrideA,                                           \
           dst_argb_c, kStrideA,                                               \
           kWidth, NEG kHeight);                                               \
  MaskCpuFlags(-1);                                                            \
  for (int i = 0; i < benchmark_iterations_; ++i) {                            \
    FMT_ATOB(src_argb + OFF, kStrideA,                                         \
             dst_argb_opt, kStrideA,                                           \
             kWidth, NEG kHeight);                                             \
  }                                                                            \
  MaskCpuFlags(0);                                                             \
  FMT_ATOB(dst_argb_c, kStrideA,                                               \
           dst_argb_c, kStrideA,                                               \
           kWidth, NEG kHeight);                                               \
  MaskCpuFlags(-1);                                                            \
  FMT_ATOB(dst_argb_opt, kStrideA,                                             \
           dst_argb_opt, kStrideA,                                             \
           kWidth, NEG kHeight);                                               \
  for (int i = 0; i < kStrideA * kHeightA; ++i) {                              \
    EXPECT_EQ(src_argb[i + OFF], dst_argb_opt[i]);                             \
    EXPECT_EQ(dst_argb_c[i], dst_argb_opt[i]);                                 \
  }                                                                            \
  free_aligned_buffer_64(src_argb);                                            \
  free_aligned_buffer_64(dst_argb_c);                                          \
  free_aligned_buffer_64(dst_argb_opt);                                        \
}

#define TESTSYM(FMT_ATOB, BPP_A, STRIDE_A, HEIGHT_A)                           \
    TESTSYMI(FMT_ATOB, BPP_A, STRIDE_A, HEIGHT_A,                              \
             benchmark_width_ - 4, _Any, +, 0)                                 \
    TESTSYMI(FMT_ATOB, BPP_A, STRIDE_A, HEIGHT_A,                              \
             benchmark_width_, _Unaligned, +, 1)                               \
    TESTSYMI(FMT_ATOB, BPP_A, STRIDE_A, HEIGHT_A,                              \
             benchmark_width_, _Opt, +, 0)

TESTSYM(ARGBToARGB, 4, 4, 1)
TESTSYM(ARGBToBGRA, 4, 4, 1)
TESTSYM(ARGBToABGR, 4, 4, 1)
TESTSYM(BGRAToARGB, 4, 4, 1)
TESTSYM(ABGRToARGB, 4, 4, 1)

TEST_F(libyuvTest, Test565) {
  SIMD_ALIGNED(uint8 orig_pixels[256][4]);
  SIMD_ALIGNED(uint8 pixels565[256][2]);

  for (int i = 0; i < 256; ++i) {
    for (int j = 0; j < 4; ++j) {
      orig_pixels[i][j] = i;
    }
  }
  ARGBToRGB565(&orig_pixels[0][0], 0, &pixels565[0][0], 0, 256, 1);
  uint32 checksum = HashDjb2(&pixels565[0][0], sizeof(pixels565), 5381);
  EXPECT_EQ(610919429u, checksum);
}

#ifdef HAVE_JPEG
TEST_F(libyuvTest, ValidateJpeg) {
  const int kOff = 10;
  const int kMinJpeg = 64;
  const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ?
    benchmark_width_ * benchmark_height_ : kMinJpeg;
  const int kSize = kImageSize + kOff;
  align_buffer_64(orig_pixels, kSize);

  // No SOI or EOI. Expect fail.
  memset(orig_pixels, 0, kSize);
  EXPECT_FALSE(ValidateJpeg(orig_pixels, kSize));

  // EOI, SOI. Expect pass.
  orig_pixels[0] = 0xff;
  orig_pixels[1] = 0xd8;  // SOI.
  orig_pixels[kSize - kOff + 0] = 0xff;
  orig_pixels[kSize - kOff + 1] = 0xd9;  // EOI.
  for (int times = 0; times < benchmark_iterations_; ++times) {
    EXPECT_TRUE(ValidateJpeg(orig_pixels, kSize));
  }
  free_aligned_buffer_page_end(orig_pixels);
}

TEST_F(libyuvTest, InvalidateJpeg) {
  const int kOff = 10;
  const int kMinJpeg = 64;
  const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ?
    benchmark_width_ * benchmark_height_ : kMinJpeg;
  const int kSize = kImageSize + kOff;
  align_buffer_64(orig_pixels, kSize);

  // No SOI or EOI. Expect fail.
  memset(orig_pixels, 0, kSize);
  EXPECT_FALSE(ValidateJpeg(orig_pixels, kSize));

  // SOI but no EOI. Expect fail.
  orig_pixels[0] = 0xff;
  orig_pixels[1] = 0xd8;  // SOI.
  for (int times = 0; times < benchmark_iterations_; ++times) {
    EXPECT_FALSE(ValidateJpeg(orig_pixels, kSize));
  }
  // EOI but no SOI. Expect fail.
  orig_pixels[0] = 0;
  orig_pixels[1] = 0;
  orig_pixels[kSize - kOff + 0] = 0xff;
  orig_pixels[kSize - kOff + 1] = 0xd9;  // EOI.
  EXPECT_FALSE(ValidateJpeg(orig_pixels, kSize));

  free_aligned_buffer_page_end(orig_pixels);
}

TEST_F(libyuvTest, MJPGToI420) {
  const int kOff = 10;
  const int kMinJpeg = 64;
  const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ?
    benchmark_width_ * benchmark_height_ : kMinJpeg;
  const int kSize = kImageSize + kOff;
  align_buffer_64(orig_pixels, kSize);
  align_buffer_64(dst_y_opt, benchmark_width_ * benchmark_height_);
  align_buffer_64(dst_u_opt,
                  SUBSAMPLE(benchmark_width_, 2) *
                  SUBSAMPLE(benchmark_height_, 2));
  align_buffer_64(dst_v_opt,
                  SUBSAMPLE(benchmark_width_, 2) *
                  SUBSAMPLE(benchmark_height_, 2));

  // EOI, SOI to make MJPG appear valid.
  memset(orig_pixels, 0, kSize);
  orig_pixels[0] = 0xff;
  orig_pixels[1] = 0xd8;  // SOI.
  orig_pixels[kSize - kOff + 0] = 0xff;
  orig_pixels[kSize - kOff + 1] = 0xd9;  // EOI.

  for (int times = 0; times < benchmark_iterations_; ++times) {
    int ret = MJPGToI420(orig_pixels, kSize,
                         dst_y_opt, benchmark_width_,
                         dst_u_opt, SUBSAMPLE(benchmark_width_, 2),
                         dst_v_opt, SUBSAMPLE(benchmark_width_, 2),
                         benchmark_width_, benchmark_height_,
                         benchmark_width_, benchmark_height_);
    // Expect failure because image is not really valid.
    EXPECT_EQ(1, ret);
  }

  free_aligned_buffer_page_end(dst_y_opt);
  free_aligned_buffer_page_end(dst_u_opt);
  free_aligned_buffer_page_end(dst_v_opt);
  free_aligned_buffer_page_end(orig_pixels);
}

TEST_F(libyuvTest, MJPGToARGB) {
  const int kOff = 10;
  const int kMinJpeg = 64;
  const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ?
    benchmark_width_ * benchmark_height_ : kMinJpeg;
  const int kSize = kImageSize + kOff;
  align_buffer_64(orig_pixels, kSize);
  align_buffer_64(dst_argb_opt, benchmark_width_ * benchmark_height_ * 4);

  // EOI, SOI to make MJPG appear valid.
  memset(orig_pixels, 0, kSize);
  orig_pixels[0] = 0xff;
  orig_pixels[1] = 0xd8;  // SOI.
  orig_pixels[kSize - kOff + 0] = 0xff;
  orig_pixels[kSize - kOff + 1] = 0xd9;  // EOI.

  for (int times = 0; times < benchmark_iterations_; ++times) {
    int ret = MJPGToARGB(orig_pixels, kSize,
                         dst_argb_opt, benchmark_width_ * 4,
                         benchmark_width_, benchmark_height_,
                         benchmark_width_, benchmark_height_);
    // Expect failure because image is not really valid.
    EXPECT_EQ(1, ret);
  }

  free_aligned_buffer_page_end(dst_argb_opt);
  free_aligned_buffer_page_end(orig_pixels);
}

#endif  // HAVE_JPEG

TEST_F(libyuvTest, CropNV12) {
  const int SUBSAMP_X = 2;
  const int SUBSAMP_Y = 2;
  const int kWidth = benchmark_width_;
  const int kHeight = benchmark_height_;
  const int crop_y =
    (benchmark_height_ - (benchmark_height_ * 360 / 480)) / 2;
  const int kDestWidth = benchmark_width_;
  const int kDestHeight = benchmark_height_ - crop_y * 2;;
  const int sample_size = kWidth * kHeight +
    SUBSAMPLE(kWidth, SUBSAMP_X) *
    SUBSAMPLE(kHeight, SUBSAMP_Y) * 2;
  align_buffer_64(src_y, sample_size);
  uint8* src_uv = src_y + kWidth * kHeight;

  align_buffer_64(dst_y, kDestWidth * kDestHeight);
  align_buffer_64(dst_u,
                  SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                  SUBSAMPLE(kDestHeight, SUBSAMP_Y));
  align_buffer_64(dst_v,
                  SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                  SUBSAMPLE(kDestHeight, SUBSAMP_Y));

  align_buffer_64(dst_y_2, kDestWidth * kDestHeight);
  align_buffer_64(dst_u_2,
                  SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                  SUBSAMPLE(kDestHeight, SUBSAMP_Y));
  align_buffer_64(dst_v_2,
                  SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                  SUBSAMPLE(kDestHeight, SUBSAMP_Y));

  srandom(time(NULL));
  for (int i = 0; i < kHeight; ++i)
    for (int j = 0; j < kWidth; ++j)
      src_y[(i * kWidth) + j] = (random() & 0xff);
  for (int i = 0; i < SUBSAMPLE(kHeight, SUBSAMP_Y); ++i) {
    for (int j = 0; j < SUBSAMPLE(kWidth, SUBSAMP_X); ++j) {
      src_uv[(i * SUBSAMPLE(kWidth, SUBSAMP_X)) + j * 2 + 0] =
          (random() & 0xff);
      src_uv[(i * SUBSAMPLE(kWidth, SUBSAMP_X)) + j * 2 + 1] =
          (random() & 0xff);
    }
  }
  memset(dst_y, 1, kDestWidth * kDestHeight);
  memset(dst_u, 2, SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                   SUBSAMPLE(kDestHeight, SUBSAMP_Y));
  memset(dst_v, 3, SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                   SUBSAMPLE(kDestHeight, SUBSAMP_Y));
  memset(dst_y_2, 1, kDestWidth * kDestHeight);
  memset(dst_u_2, 2, SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                     SUBSAMPLE(kDestHeight, SUBSAMP_Y));
  memset(dst_v_2, 3, SUBSAMPLE(kDestWidth, SUBSAMP_X) *
                     SUBSAMPLE(kDestHeight, SUBSAMP_Y));

  NV12ToI420(src_y + crop_y * kWidth, kWidth,
             src_uv + (crop_y / 2) * kWidth, kWidth,
             dst_y, kDestWidth,
             dst_u, SUBSAMPLE(kDestWidth, SUBSAMP_X),
             dst_v, SUBSAMPLE(kDestWidth, SUBSAMP_X),
             kDestWidth, kDestHeight);

  ConvertToI420(src_y, sample_size,
                dst_y_2, kDestWidth,
                dst_u_2, SUBSAMPLE(kDestWidth, SUBSAMP_X),
                dst_v_2, SUBSAMPLE(kDestWidth, SUBSAMP_X),
                0, crop_y,
                kWidth, kHeight,
                kDestWidth, kDestHeight,
                libyuv::kRotate0, libyuv::FOURCC_NV12);

  for (int i = 0; i < kDestHeight; ++i) {
    for (int j = 0; j < kDestWidth; ++j) {
      EXPECT_EQ(dst_y[i * kWidth + j], dst_y_2[i * kWidth + j]);
    }
  }
  for (int i = 0; i < SUBSAMPLE(kDestHeight, SUBSAMP_Y); ++i) {
    for (int j = 0; j < SUBSAMPLE(kDestWidth, SUBSAMP_X); ++j) {
      EXPECT_EQ(dst_u[i * SUBSAMPLE(kDestWidth, SUBSAMP_X) + j],
                dst_u_2[i * SUBSAMPLE(kDestWidth, SUBSAMP_X) + j]);
    }
  }
  for (int i = 0; i < SUBSAMPLE(kDestHeight, SUBSAMP_Y); ++i) {
    for (int j = 0; j < SUBSAMPLE(kDestWidth, SUBSAMP_X); ++j) {
      EXPECT_EQ(dst_v[i * SUBSAMPLE(kDestWidth, SUBSAMP_X) + j],
                dst_v_2[i * SUBSAMPLE(kDestWidth, SUBSAMP_X) + j]);
    }
  }
  free_aligned_buffer_64(dst_y);
  free_aligned_buffer_64(dst_u);
  free_aligned_buffer_64(dst_v);
  free_aligned_buffer_64(dst_y_2);
  free_aligned_buffer_64(dst_u_2);
  free_aligned_buffer_64(dst_v_2);
  free_aligned_buffer_64(src_y);
}

TEST_F(libyuvTest, HaveJPEG) {
#ifdef HAVE_JPEG
  printf("JPEG enabled\n.");
#else
  printf("JPEG disabled\n.");
#endif
}

}  // namespace libyuv