scale_argb_test.cc 11.2 KB
Newer Older
1
/*
2
 *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
8 9 10 11 12 13 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/scale_argb.h"
16
#include "../unit_test/unit_test.h"
17 18 19

namespace libyuv {

20
// Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
21
static int ARGBTestFilter(int src_width, int src_height,
22
                          int dst_width, int dst_height,
23
                          FilterMode f, int benchmark_iterations) {
fbarchard@google.com's avatar
fbarchard@google.com committed
24
  const int b = 128;
25
  int i, j;
26 27
  int src_argb_plane_size = (Abs(src_width) + b * 2) *
      (Abs(src_height) + b * 2) * 4;
28
  int src_stride_argb = (b * 2 + Abs(src_width)) * 4;
29

30
  align_buffer_64(src_argb, src_argb_plane_size)
31 32
  srandom(time(NULL));
  MemRandomize(src_argb, src_argb_plane_size);
33

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

37 38
  align_buffer_64(dst_argb_c, dst_argb_plane_size)
  align_buffer_64(dst_argb_opt, dst_argb_plane_size)
fbarchard@google.com's avatar
fbarchard@google.com committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  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.
  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
            src_width, src_height,
            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
            dst_width, dst_height, f);
  MaskCpuFlags(-1);  // Enable all CPU optimization.
  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
            src_width, src_height,
            dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
            dst_width, dst_height, f);

  MaskCpuFlags(0);  // Disable all CPU optimization.
55
  double c_time = get_time();
56 57 58 59 60 61
  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
            src_width, src_height,
            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
            dst_width, dst_height, f);

  c_time = (get_time() - c_time);
62

fbarchard@google.com's avatar
fbarchard@google.com committed
63
  MaskCpuFlags(-1);  // Enable all CPU optimization.
64
  double opt_time = get_time();
65
  for (i = 0; i < benchmark_iterations; ++i) {
fbarchard@google.com's avatar
fbarchard@google.com committed
66
    ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
67
              src_width, src_height,
fbarchard@google.com's avatar
fbarchard@google.com committed
68
              dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
69
              dst_width, dst_height, f);
fbarchard@google.com's avatar
fbarchard@google.com committed
70
  }
71
  opt_time = (get_time() - opt_time) / benchmark_iterations;
72

fbarchard@google.com's avatar
fbarchard@google.com committed
73 74
  // Report performance of C vs OPT
  printf("filter %d - %8d us C - %8d us OPT\n",
75
         f, static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
76

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
  //  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) {
84
      int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
fbarchard@google.com's avatar
fbarchard@google.com committed
85
                         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)
fbarchard@google.com's avatar
fbarchard@google.com committed
95
  return max_diff;
96 97
}

98 99
static const int kTileX = 8;
static const int kTileY = 8;
fbarchard@google.com's avatar
fbarchard@google.com committed
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

static int TileARGBScale(const uint8* src_argb, int src_stride_argb,
                         int src_width, int src_height,
                         uint8* dst_argb, int dst_stride_argb,
                         int dst_width, int dst_height,
                         FilterMode filtering) {
  for (int y = 0; y < dst_height; y += kTileY) {
    for (int x = 0; x < dst_width; x += kTileX) {
      int clip_width = kTileX;
      if (x + clip_width > dst_width) {
        clip_width = dst_width - x;
      }
      int clip_height = kTileY;
      if (y + clip_height > dst_height) {
        clip_height = dst_height - y;
      }
      int r = ARGBScaleClip(src_argb, src_stride_argb,
                            src_width, src_height,
                            dst_argb, dst_stride_argb,
                            dst_width, dst_height,
                            x, y, clip_width, clip_height, filtering);
      if (r) {
        return r;
      }
    }
  }
  return 0;
}

static int ARGBClipTestFilter(int src_width, int src_height,
                              int dst_width, int dst_height,
                              FilterMode f, int benchmark_iterations) {
  const int b = 128;
  int src_argb_plane_size = (Abs(src_width) + b * 2) *
      (Abs(src_height) + b * 2) * 4;
  int src_stride_argb = (b * 2 + Abs(src_width)) * 4;

  align_buffer_64(src_argb, src_argb_plane_size)
  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 < (Abs(src_height) + b); ++i) {
    for (j = b; j < (Abs(src_width) + b) * 4; ++j) {
      src_argb[(i * src_stride_argb) + j] = (random() & 0xff);
    }
  }

  align_buffer_64(dst_argb_c, dst_argb_plane_size)
  align_buffer_64(dst_argb_opt, dst_argb_plane_size)
  memset(dst_argb_c, 2, dst_argb_plane_size);
  memset(dst_argb_opt, 3, dst_argb_plane_size);

  // Do full image, no clipping.
  double c_time = get_time();
  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
            src_width, src_height,
            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
            dst_width, dst_height, f);
  c_time = (get_time() - c_time);

  // Do tiled image, clipping scale to a tile at a time.
  double opt_time = get_time();
  for (i = 0; i < benchmark_iterations; ++i) {
    TileARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
                  src_width, src_height,
                  dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
                  dst_width, dst_height, f);
  }
  opt_time = (get_time() - opt_time) / benchmark_iterations;

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

  // Compare full scaled image vs tiled image.
  int max_diff = 0;
  for (i = b; i < (dst_height + b); ++i) {
    for (j = b * 4; j < (dst_width + b) * 4; ++j) {
183
      int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
fbarchard@google.com's avatar
fbarchard@google.com committed
184 185 186 187 188 189 190 191 192 193 194 195 196
                         dst_argb_opt[(i * dst_stride_argb) + j]);
      if (abs_diff > max_diff) {
        max_diff = abs_diff;
      }
    }
  }

  free_aligned_buffer_64(dst_argb_c)
  free_aligned_buffer_64(dst_argb_opt)
  free_aligned_buffer_64(src_argb)
  return max_diff;
}

197
#define TEST_FACTOR1(name, filter, hfactor, vfactor, max_diff)                 \
198 199
    TEST_F(libyuvTest, ARGBScaleDownBy##name##_##filter) {                     \
      int diff = ARGBTestFilter(benchmark_width_, benchmark_height_,           \
200 201
                                Abs(benchmark_width_) * hfactor,               \
                                Abs(benchmark_height_) * vfactor,              \
202 203 204 205 206
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }                                                                          \
    TEST_F(libyuvTest, ARGBScaleDownClipBy##name##_##filter) {                 \
      int diff = ARGBClipTestFilter(benchmark_width_, benchmark_height_,       \
207 208
                                Abs(benchmark_width_) * hfactor,               \
                                Abs(benchmark_height_) * vfactor,              \
209 210 211
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
212

213
// Test a scale factor with 2 filters.  Expect unfiltered to be exact, but
214
// filtering is different fixed point implementations for SSSE3, Neon and C.
215 216
#define TEST_FACTOR(name, hfactor, vfactor)                                    \
    TEST_FACTOR1(name, Bilinear, hfactor, vfactor, 2)
217 218

// TODO(fbarchard): ScaleDownBy1 should be lossless, but Box has error of 2.
219
//TEST_FACTOR(1, 1 / 1, 1 / 1)
220 221
TEST_FACTOR(2, 1 / 2, 1 / 2)
TEST_FACTOR(4, 1 / 4, 1 / 4)
222 223 224
//TEST_FACTOR(8, 1 / 8, 1 / 8)
//TEST_FACTOR(16, 1 / 16, 1 / 16)
//TEST_FACTOR(2by3, 2 / 3, 2 / 3)
225
TEST_FACTOR(3by4, 3 / 4, 3 / 4)
226 227
//TEST_FACTOR(3by8, 3 / 8, 3 / 8)
//TEST_FACTOR(Vertical2by3, 1, 2 / 3)
228 229 230
#undef TEST_FACTOR1
#undef TEST_FACTOR

231 232
#define TEST_SCALETO1(name, width, height, filter, max_diff)                   \
    TEST_F(libyuvTest, name##To##width##x##height##_##filter) {                \
233 234 235 236 237
      int diff = ARGBTestFilter(benchmark_width_, benchmark_height_,           \
                                width, height,                                 \
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }                                                                          \
238
    TEST_F(libyuvTest, name##From##width##x##height##_##filter) {              \
239 240 241 242 243
      int diff = ARGBTestFilter(width, height,                                 \
                                Abs(benchmark_width_), Abs(benchmark_height_), \
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }                                                                          \
244
    TEST_F(libyuvTest, name##ClipTo##width##x##height##_##filter) {            \
245 246 247 248 249
      int diff = ARGBClipTestFilter(benchmark_width_, benchmark_height_,       \
                                width, height,                                 \
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }                                                                          \
250
    TEST_F(libyuvTest, name##ClipFrom##width##x##height##_##filter) {          \
251 252 253 254 255
      int diff = ARGBClipTestFilter(width, height,                             \
                                Abs(benchmark_width_), Abs(benchmark_height_), \
                                kFilter##filter, benchmark_iterations_);       \
      EXPECT_LE(diff, max_diff);                                               \
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
256

257
// Test scale to a specified size with all 3 filters.
258 259
#define TEST_SCALETO(name, width, height)                                      \
    TEST_SCALETO1(name, width, height, None, 0)                                \
260
    TEST_SCALETO1(name, width, height, Bilinear, 2)
261

262 263
TEST_SCALETO(ARGBScale, 640, 360)
TEST_SCALETO(DISABLED_ARGBScale, 853, 480)
264 265 266 267
TEST_SCALETO(DISABLED_ARGBScale, 1280, 720)
TEST_SCALETO(DISABLED_ARGBScale, 1280, 800)
TEST_SCALETO(DISABLED_ARGBScale, 1366, 768)
TEST_SCALETO(DISABLED_ARGBScale, 1920, 1080)
268 269
#undef TEST_SCALETO1
#undef TEST_SCALETO
fbarchard@google.com's avatar
fbarchard@google.com committed
270

271
}  // namespace libyuv