rotate_argb_test.cc 6.71 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 12 13 14 15
 *  be found in the AUTHORS file in the root of the source tree.
 */

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

#include "libyuv/cpu_id.h"
#include "libyuv/rotate_argb.h"
16
#include "../unit_test/unit_test.h"
17 18 19 20 21

namespace libyuv {

static int ARGBTestRotate(int src_width, int src_height,
                          int dst_width, int dst_height,
22
                          libyuv::RotationMode mode, int benchmark_iterations) {
23 24 25 26
  const int b = 128;
  int src_argb_plane_size = (src_width + b * 2) * (src_height + b * 2) * 4;
  int src_stride_argb = (b * 2 + src_width) * 4;

27
  align_buffer_64(src_argb, src_argb_plane_size)
28 29 30 31 32 33 34 35 36 37 38 39 40 41
  memset(src_argb, 1, src_argb_plane_size);

  int dst_argb_plane_size = (dst_width + b * 2) * (dst_height + b * 2) * 4;
  int dst_stride_argb = (b * 2 + dst_width) * 4;

  srandom(time(NULL));

  int i, j;
  for (i = b; i < (src_height + b); ++i) {
    for (j = b; j < (src_width + b) * 4; ++j) {
      src_argb[(i * src_stride_argb) + j] = (random() & 0xff);
    }
  }

42 43
  align_buffer_64(dst_argb_c, dst_argb_plane_size)
  align_buffer_64(dst_argb_opt, dst_argb_plane_size)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  memset(dst_argb_c, 2, dst_argb_plane_size);
  memset(dst_argb_opt, 3, dst_argb_plane_size);

  // Warm up both versions for consistent benchmarks.
  MaskCpuFlags(0);  // Disable all CPU optimization.
  ARGBRotate(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
             dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
             src_width, src_height, mode);
  MaskCpuFlags(-1);  // Enable all CPU optimization.
  ARGBRotate(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
             dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
             src_width, src_height, mode);

  MaskCpuFlags(0);  // Disable all CPU optimization.
  double c_time = get_time();
59 60 61 62
  ARGBRotate(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
             dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
             src_width, src_height, mode);
  c_time = (get_time() - c_time);
63 64 65

  MaskCpuFlags(-1);  // Enable all CPU optimization.
  double opt_time = get_time();
66
  for (i = 0; i < benchmark_iterations; ++i) {
67 68 69 70
    ARGBRotate(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
               dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
               src_width, src_height, mode);
  }
71
  opt_time = (get_time() - opt_time) / benchmark_iterations;
72 73 74 75 76

  // Report performance of C vs OPT
  printf("filter %d - %8d us C - %8d us OPT\n",
         mode, static_cast<int>(c_time*1e6), static_cast<int>(opt_time*1e6));

77 78
  // C version may be a little off from the optimized. Order of
  //  operations may introduce rounding somewhere. So do a difference
79 80 81 82 83 84 85
  //  of the buffers and look to see that the max difference isn't
  //  over 2.
  int max_diff = 0;
  for (i = b; i < (dst_height + b); ++i) {
    for (j = b * 4; j < (dst_width + b) * 4; ++j) {
      int abs_diff = abs(dst_argb_c[(i * dst_stride_argb) + j] -
                         dst_argb_opt[(i * dst_stride_argb) + j]);
86
      if (abs_diff > max_diff) {
87
        max_diff = abs_diff;
88
      }
89 90 91
    }
  }

92 93 94
  free_aligned_buffer_64(dst_argb_c)
  free_aligned_buffer_64(dst_argb_opt)
  free_aligned_buffer_64(src_argb)
95 96 97 98
  return max_diff;
}

TEST_F(libyuvTest, ARGBRotate0) {
99 100 101 102
  const int src_width = benchmark_width_;
  const int src_height = benchmark_height_;
  const int dst_width = benchmark_width_;
  const int dst_height = benchmark_height_;
103 104

  int err = ARGBTestRotate(src_width, src_height,
105 106
                           dst_width, dst_height, kRotate0,
                           benchmark_iterations_);
107 108 109 110
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate90) {
111 112 113 114
  const int src_width = benchmark_width_;
  const int src_height = benchmark_height_;
  const int dst_width = benchmark_height_;
  const int dst_height = benchmark_width_;
115 116

  int err = ARGBTestRotate(src_width, src_height,
117 118
                           dst_width, dst_height, kRotate90,
                           benchmark_iterations_);
119 120 121 122
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate180) {
123 124 125 126
  const int src_width = benchmark_width_;
  const int src_height = benchmark_height_;
  const int dst_width = benchmark_width_;
  const int dst_height = benchmark_height_;
127 128

  int err = ARGBTestRotate(src_width, src_height,
129 130
                           dst_width, dst_height, kRotate180,
                           benchmark_iterations_);
131 132 133 134
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate270) {
135 136 137 138
  const int src_width = benchmark_width_;
  const int src_height = benchmark_height_;
  const int dst_width = benchmark_height_;
  const int dst_height = benchmark_width_;
139 140

  int err = ARGBTestRotate(src_width, src_height,
141 142
                           dst_width, dst_height, kRotate270,
                           benchmark_iterations_);
143 144 145 146
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate0_Odd) {
147 148 149 150
  const int src_width = benchmark_width_ - 3;
  const int src_height = benchmark_height_ - 1;
  const int dst_width = benchmark_width_ - 3;
  const int dst_height = benchmark_height_ - 1;
151 152

  int err = ARGBTestRotate(src_width, src_height,
153 154
                           dst_width, dst_height, kRotate0,
                           benchmark_iterations_);
155 156 157 158
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate90_Odd) {
159 160 161 162
  const int src_width = benchmark_width_ - 3;
  const int src_height = benchmark_height_ - 1;
  const int dst_width = benchmark_height_ - 1;
  const int dst_height = benchmark_width_ - 3;
163 164

  int err = ARGBTestRotate(src_width, src_height,
165 166
                           dst_width, dst_height, kRotate90,
                           benchmark_iterations_);
167 168 169 170
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate180_Odd) {
171 172 173 174
  const int src_width = benchmark_width_ - 3;
  const int src_height = benchmark_height_ - 1;
  const int dst_width = benchmark_width_ - 3;
  const int dst_height = benchmark_height_ - 1;
175 176

  int err = ARGBTestRotate(src_width, src_height,
177 178
                           dst_width, dst_height, kRotate180,
                           benchmark_iterations_);
179 180 181 182
  EXPECT_GE(1, err);
}

TEST_F(libyuvTest, ARGBRotate270_Odd) {
183 184 185 186
  const int src_width = benchmark_width_ - 3;
  const int src_height = benchmark_height_ - 1;
  const int dst_width = benchmark_height_ - 1;
  const int dst_height = benchmark_width_ - 3;
187 188

  int err = ARGBTestRotate(src_width, src_height,
189 190
                           dst_width, dst_height, kRotate270,
                           benchmark_iterations_);
191 192 193 194
  EXPECT_GE(1, err);
}

}  // namespace libyuv