rotate_test.cc 8.34 KB
Newer Older
1
/*
2
 *  Copyright 2012 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
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

14
#include "libyuv/cpu_id.h"
15
#include "libyuv/rotate.h"
16
#include "../unit_test/unit_test.h"
frkoenig@google.com's avatar
frkoenig@google.com committed
17

mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
18
namespace libyuv {
19

20 21 22 23 24 25
static void I420TestRotate(int src_width, int src_height,
                           int dst_width, int dst_height,
                           libyuv::RotationMode mode,
                           int benchmark_iterations) {
  if (src_width < 1) {
    src_width = 1;
26
  }
27 28
  if (src_height < 1) {
    src_height = 1;
29
  }
30 31
  if (dst_width < 1) {
    dst_width = 1;
32
  }
33 34
  if (dst_height < 1) {
    dst_height = 1;
35
  }
36 37 38 39 40 41
  int src_i420_y_size = src_width * src_height;
  int src_i420_uv_size = ((src_width + 1) / 2) * ((src_height + 1) / 2);
  int src_i420_size = src_i420_y_size + src_i420_uv_size * 2;
  align_buffer_64(src_i420, src_i420_size)
  for (int i = 0; i < src_i420_size; ++i) {
    src_i420[i] = random() & 0xff;
42
  }
43

44 45 46 47 48 49 50
  int dst_i420_y_size = dst_width * dst_height;
  int dst_i420_uv_size = ((dst_width + 1) / 2) * ((dst_height + 1) / 2);
  int dst_i420_size = dst_i420_y_size + dst_i420_uv_size * 2;
  align_buffer_64(dst_i420_c, dst_i420_size)
  align_buffer_64(dst_i420_opt, dst_i420_size)
  memset(dst_i420_c, 2, dst_i420_size);
  memset(dst_i420_opt, 3, dst_i420_size);
51

52 53 54 55 56 57 58 59 60
  MaskCpuFlags(0);  // Disable all CPU optimization.
  I420Rotate(src_i420, src_width,
             src_i420 + src_i420_y_size, (src_width + 1) / 2,
             src_i420 + src_i420_y_size + src_i420_uv_size, (src_width + 1) / 2,
             dst_i420_c, dst_width,
             dst_i420_c + dst_i420_y_size, (dst_width + 1) / 2,
             dst_i420_c + dst_i420_y_size + dst_i420_uv_size,
               (dst_width + 1) / 2,
             src_width, src_height, mode);
61

62 63 64 65 66 67 68 69 70 71 72
  MaskCpuFlags(-1);  // Enable all CPU optimization.
  for (int i = 0; i < benchmark_iterations; ++i) {
    I420Rotate(src_i420, src_width,
               src_i420 + src_i420_y_size, (src_width + 1) / 2,
               src_i420 + src_i420_y_size + src_i420_uv_size,
                 (src_width + 1) / 2,
               dst_i420_opt, dst_width,
               dst_i420_opt + dst_i420_y_size, (dst_width + 1) / 2,
               dst_i420_opt + dst_i420_y_size + dst_i420_uv_size,
                 (dst_width + 1) / 2,
               src_width, src_height, mode);
73
  }
74

75 76 77
  // Rotation should be exact.
  for (int i = 0; i < dst_i420_size; ++i) {
    EXPECT_EQ(dst_i420_c[i], dst_i420_opt[i]);
78 79
  }

80 81 82
  free_aligned_buffer_64(dst_i420_c)
  free_aligned_buffer_64(dst_i420_opt)
  free_aligned_buffer_64(src_i420)
83 84
}

85 86 87 88
TEST_F(libyuvTest, I420Rotate0) {
  I420TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_width_, benchmark_height_,
                 kRotate0, benchmark_iterations_);
89 90
}

91 92 93 94
TEST_F(libyuvTest, I420Rotate90) {
  I420TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_height_, benchmark_width_,
                 kRotate90, benchmark_iterations_);
95 96
}

97 98 99 100
TEST_F(libyuvTest, I420Rotate180) {
  I420TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_width_, benchmark_height_,
                 kRotate180, benchmark_iterations_);
101 102
}

103 104 105 106
TEST_F(libyuvTest, I420Rotate270) {
  I420TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_height_, benchmark_width_,
                 kRotate270, benchmark_iterations_);
107 108
}

109 110 111 112
TEST_F(libyuvTest, I420Rotate0_Odd) {
  I420TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_width_ - 3, benchmark_height_ - 1,
                 kRotate0, benchmark_iterations_);
113
}
114

115 116 117 118
TEST_F(libyuvTest, I420Rotate90_Odd) {
  I420TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_height_ - 1, benchmark_width_ - 3,
                 kRotate90, benchmark_iterations_);
119 120
}

121 122 123 124
TEST_F(libyuvTest, I420Rotate180_Odd) {
  I420TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_width_ - 3, benchmark_height_ - 1,
                 kRotate180, benchmark_iterations_);
125 126
}

127 128 129 130
TEST_F(libyuvTest, I420Rotate270_Odd) {
  I420TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_height_ - 1, benchmark_width_ - 3,
                 kRotate270, benchmark_iterations_);
131 132
}

133 134 135 136 137 138
static void NV12TestRotate(int src_width, int src_height,
                           int dst_width, int dst_height,
                           libyuv::RotationMode mode,
                           int benchmark_iterations) {
  if (src_width < 1) {
    src_width = 1;
139
  }
140 141
  if (src_height < 1) {
    src_height = 1;
142
  }
143 144
  if (dst_width < 1) {
    dst_width = 1;
145
  }
146 147
  if (dst_height < 1) {
    dst_height = 1;
148
  }
149 150 151 152 153 154
  int src_nv12_y_size = src_width * src_height;
  int src_nv12_uv_size = ((src_width + 1) / 2) * ((src_height + 1) / 2) * 2;
  int src_nv12_size = src_nv12_y_size + src_nv12_uv_size;
  align_buffer_64(src_nv12, src_nv12_size)
  for (int i = 0; i < src_nv12_size; ++i) {
    src_nv12[i] = random() & 0xff;
155
  }
156

157 158 159 160 161 162 163
  int dst_i420_y_size = dst_width * dst_height;
  int dst_i420_uv_size = ((dst_width + 1) / 2) * ((dst_height + 1) / 2);
  int dst_i420_size = dst_i420_y_size + dst_i420_uv_size * 2;
  align_buffer_64(dst_i420_c, dst_i420_size)
  align_buffer_64(dst_i420_opt, dst_i420_size)
  memset(dst_i420_c, 2, dst_i420_size);
  memset(dst_i420_opt, 3, dst_i420_size);
164

165 166 167 168 169 170 171 172
  MaskCpuFlags(0);  // Disable all CPU optimization.
  NV12ToI420Rotate(src_nv12, src_width,
                   src_nv12 + src_nv12_y_size, (src_width + 1) & ~1,
                   dst_i420_c, dst_width,
                   dst_i420_c + dst_i420_y_size, (dst_width + 1) / 2,
                   dst_i420_c + dst_i420_y_size + dst_i420_uv_size,
                     (dst_width + 1) / 2,
                   src_width, src_height, mode);
173

174 175 176 177 178 179 180 181 182
  MaskCpuFlags(-1);  // Enable all CPU optimization.
  for (int i = 0; i < benchmark_iterations; ++i) {
    NV12ToI420Rotate(src_nv12, src_width,
                     src_nv12 + src_nv12_y_size, (src_width + 1) & ~1,
                     dst_i420_opt, dst_width,
                     dst_i420_opt + dst_i420_y_size, (dst_width + 1) / 2,
                     dst_i420_opt + dst_i420_y_size + dst_i420_uv_size,
                       (dst_width + 1) / 2,
                     src_width, src_height, mode);
183 184
  }

185 186 187
  // Rotation should be exact.
  for (int i = 0; i < dst_i420_size; ++i) {
    EXPECT_EQ(dst_i420_c[i], dst_i420_opt[i]);
188 189
  }

190 191 192
  free_aligned_buffer_64(dst_i420_c)
  free_aligned_buffer_64(dst_i420_opt)
  free_aligned_buffer_64(src_nv12)
193 194
}

195 196 197 198
TEST_F(libyuvTest, NV12Rotate0) {
  NV12TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_width_, benchmark_height_,
                 kRotate0, benchmark_iterations_);
199 200
}

201 202 203 204
TEST_F(libyuvTest, NV12Rotate90) {
  NV12TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_height_, benchmark_width_,
                 kRotate90, benchmark_iterations_);
205 206
}

207 208 209 210
TEST_F(libyuvTest, NV12Rotate180) {
  NV12TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_width_, benchmark_height_,
                 kRotate180, benchmark_iterations_);
211 212
}

213 214 215 216
TEST_F(libyuvTest, NV12Rotate270) {
  NV12TestRotate(benchmark_width_, benchmark_height_,
                 benchmark_height_, benchmark_width_,
                 kRotate270, benchmark_iterations_);
217 218
}

219 220 221 222
TEST_F(libyuvTest, NV12Rotate0_Odd) {
  NV12TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_width_ - 3, benchmark_height_ - 1,
                 kRotate0, benchmark_iterations_);
223 224
}

225 226 227 228
TEST_F(libyuvTest, NV12Rotate90_Odd) {
  NV12TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_height_ - 1, benchmark_width_ - 3,
                 kRotate90, benchmark_iterations_);
229
}
mikhal@webrtc.org's avatar
mikhal@webrtc.org committed
230

231 232 233 234 235
TEST_F(libyuvTest, NV12Rotate180_Odd) {
  NV12TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_width_ - 3, benchmark_height_ - 1,
                 kRotate180, benchmark_iterations_);
}
236

237 238 239 240
TEST_F(libyuvTest, NV12Rotate270_Odd) {
  NV12TestRotate(benchmark_width_ - 3, benchmark_height_ - 1,
                 benchmark_height_ - 1, benchmark_width_ - 3,
                 kRotate270, benchmark_iterations_);
241
}
242

frkoenig@google.com's avatar
frkoenig@google.com committed
243
}  // namespace libyuv