media/libyuv/unit_test/scale_argb_test.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libyuv/unit_test/scale_argb_test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,270 @@
     1.4 +/*
     1.5 + *  Copyright 2011 The LibYuv Project Authors. All rights reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS. All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include <stdlib.h>
    1.15 +#include <time.h>
    1.16 +
    1.17 +#include "libyuv/cpu_id.h"
    1.18 +#include "libyuv/scale_argb.h"
    1.19 +#include "libyuv/row.h"
    1.20 +#include "../unit_test/unit_test.h"
    1.21 +
    1.22 +namespace libyuv {
    1.23 +
    1.24 +// Test scaling with C vs Opt and return maximum pixel difference. 0 = exact.
    1.25 +static int ARGBTestFilter(int src_width, int src_height,
    1.26 +                          int dst_width, int dst_height,
    1.27 +                          FilterMode f, int benchmark_iterations) {
    1.28 +  const int b = 128;
    1.29 +  int i, j;
    1.30 +  int src_argb_plane_size = (Abs(src_width) + b * 2) *
    1.31 +      (Abs(src_height) + b * 2) * 4;
    1.32 +  int src_stride_argb = (b * 2 + Abs(src_width)) * 4;
    1.33 +
    1.34 +  align_buffer_64(src_argb, src_argb_plane_size);
    1.35 +  srandom(time(NULL));
    1.36 +  MemRandomize(src_argb, src_argb_plane_size);
    1.37 +
    1.38 +  int dst_argb_plane_size = (dst_width + b * 2) * (dst_height + b * 2) * 4;
    1.39 +  int dst_stride_argb = (b * 2 + dst_width) * 4;
    1.40 +
    1.41 +  align_buffer_64(dst_argb_c, dst_argb_plane_size);
    1.42 +  align_buffer_64(dst_argb_opt, dst_argb_plane_size);
    1.43 +  memset(dst_argb_c, 2, dst_argb_plane_size);
    1.44 +  memset(dst_argb_opt, 3, dst_argb_plane_size);
    1.45 +
    1.46 +  // Warm up both versions for consistent benchmarks.
    1.47 +  MaskCpuFlags(0);  // Disable all CPU optimization.
    1.48 +  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
    1.49 +            src_width, src_height,
    1.50 +            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
    1.51 +            dst_width, dst_height, f);
    1.52 +  MaskCpuFlags(-1);  // Enable all CPU optimization.
    1.53 +  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
    1.54 +            src_width, src_height,
    1.55 +            dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
    1.56 +            dst_width, dst_height, f);
    1.57 +
    1.58 +  MaskCpuFlags(0);  // Disable all CPU optimization.
    1.59 +  double c_time = get_time();
    1.60 +  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
    1.61 +            src_width, src_height,
    1.62 +            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
    1.63 +            dst_width, dst_height, f);
    1.64 +
    1.65 +  c_time = (get_time() - c_time);
    1.66 +
    1.67 +  MaskCpuFlags(-1);  // Enable all CPU optimization.
    1.68 +  double opt_time = get_time();
    1.69 +  for (i = 0; i < benchmark_iterations; ++i) {
    1.70 +    ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
    1.71 +              src_width, src_height,
    1.72 +              dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
    1.73 +              dst_width, dst_height, f);
    1.74 +  }
    1.75 +  opt_time = (get_time() - opt_time) / benchmark_iterations;
    1.76 +
    1.77 +  // Report performance of C vs OPT
    1.78 +  printf("filter %d - %8d us C - %8d us OPT\n",
    1.79 +         f, static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
    1.80 +
    1.81 +  // C version may be a little off from the optimized. Order of
    1.82 +  //  operations may introduce rounding somewhere. So do a difference
    1.83 +  //  of the buffers and look to see that the max difference isn't
    1.84 +  //  over 2.
    1.85 +  int max_diff = 0;
    1.86 +  for (i = b; i < (dst_height + b); ++i) {
    1.87 +    for (j = b * 4; j < (dst_width + b) * 4; ++j) {
    1.88 +      int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
    1.89 +                         dst_argb_opt[(i * dst_stride_argb) + j]);
    1.90 +      if (abs_diff > max_diff) {
    1.91 +        max_diff = abs_diff;
    1.92 +      }
    1.93 +    }
    1.94 +  }
    1.95 +
    1.96 +  free_aligned_buffer_64(dst_argb_c);
    1.97 +  free_aligned_buffer_64(dst_argb_opt);
    1.98 +  free_aligned_buffer_64(src_argb);
    1.99 +  return max_diff;
   1.100 +}
   1.101 +
   1.102 +static const int kTileX = 8;
   1.103 +static const int kTileY = 8;
   1.104 +
   1.105 +static int TileARGBScale(const uint8* src_argb, int src_stride_argb,
   1.106 +                         int src_width, int src_height,
   1.107 +                         uint8* dst_argb, int dst_stride_argb,
   1.108 +                         int dst_width, int dst_height,
   1.109 +                         FilterMode filtering) {
   1.110 +  for (int y = 0; y < dst_height; y += kTileY) {
   1.111 +    for (int x = 0; x < dst_width; x += kTileX) {
   1.112 +      int clip_width = kTileX;
   1.113 +      if (x + clip_width > dst_width) {
   1.114 +        clip_width = dst_width - x;
   1.115 +      }
   1.116 +      int clip_height = kTileY;
   1.117 +      if (y + clip_height > dst_height) {
   1.118 +        clip_height = dst_height - y;
   1.119 +      }
   1.120 +      int r = ARGBScaleClip(src_argb, src_stride_argb,
   1.121 +                            src_width, src_height,
   1.122 +                            dst_argb, dst_stride_argb,
   1.123 +                            dst_width, dst_height,
   1.124 +                            x, y, clip_width, clip_height, filtering);
   1.125 +      if (r) {
   1.126 +        return r;
   1.127 +      }
   1.128 +    }
   1.129 +  }
   1.130 +  return 0;
   1.131 +}
   1.132 +
   1.133 +static int ARGBClipTestFilter(int src_width, int src_height,
   1.134 +                              int dst_width, int dst_height,
   1.135 +                              FilterMode f, int benchmark_iterations) {
   1.136 +  const int b = 128;
   1.137 +  int src_argb_plane_size = (Abs(src_width) + b * 2) *
   1.138 +      (Abs(src_height) + b * 2) * 4;
   1.139 +  int src_stride_argb = (b * 2 + Abs(src_width)) * 4;
   1.140 +
   1.141 +  align_buffer_64(src_argb, src_argb_plane_size);
   1.142 +  memset(src_argb, 1, src_argb_plane_size);
   1.143 +
   1.144 +  int dst_argb_plane_size = (dst_width + b * 2) * (dst_height + b * 2) * 4;
   1.145 +  int dst_stride_argb = (b * 2 + dst_width) * 4;
   1.146 +
   1.147 +  srandom(time(NULL));
   1.148 +
   1.149 +  int i, j;
   1.150 +  for (i = b; i < (Abs(src_height) + b); ++i) {
   1.151 +    for (j = b; j < (Abs(src_width) + b) * 4; ++j) {
   1.152 +      src_argb[(i * src_stride_argb) + j] = (random() & 0xff);
   1.153 +    }
   1.154 +  }
   1.155 +
   1.156 +  align_buffer_64(dst_argb_c, dst_argb_plane_size);
   1.157 +  align_buffer_64(dst_argb_opt, dst_argb_plane_size);
   1.158 +  memset(dst_argb_c, 2, dst_argb_plane_size);
   1.159 +  memset(dst_argb_opt, 3, dst_argb_plane_size);
   1.160 +
   1.161 +  // Do full image, no clipping.
   1.162 +  double c_time = get_time();
   1.163 +  ARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
   1.164 +            src_width, src_height,
   1.165 +            dst_argb_c + (dst_stride_argb * b) + b * 4, dst_stride_argb,
   1.166 +            dst_width, dst_height, f);
   1.167 +  c_time = (get_time() - c_time);
   1.168 +
   1.169 +  // Do tiled image, clipping scale to a tile at a time.
   1.170 +  double opt_time = get_time();
   1.171 +  for (i = 0; i < benchmark_iterations; ++i) {
   1.172 +    TileARGBScale(src_argb + (src_stride_argb * b) + b * 4, src_stride_argb,
   1.173 +                  src_width, src_height,
   1.174 +                  dst_argb_opt + (dst_stride_argb * b) + b * 4, dst_stride_argb,
   1.175 +                  dst_width, dst_height, f);
   1.176 +  }
   1.177 +  opt_time = (get_time() - opt_time) / benchmark_iterations;
   1.178 +
   1.179 +  // Report performance of Full vs Tiled.
   1.180 +  printf("filter %d - %8d us Full - %8d us Tiled\n",
   1.181 +         f, static_cast<int>(c_time * 1e6), static_cast<int>(opt_time * 1e6));
   1.182 +
   1.183 +  // Compare full scaled image vs tiled image.
   1.184 +  int max_diff = 0;
   1.185 +  for (i = b; i < (dst_height + b); ++i) {
   1.186 +    for (j = b * 4; j < (dst_width + b) * 4; ++j) {
   1.187 +      int abs_diff = Abs(dst_argb_c[(i * dst_stride_argb) + j] -
   1.188 +                         dst_argb_opt[(i * dst_stride_argb) + j]);
   1.189 +      if (abs_diff > max_diff) {
   1.190 +        max_diff = abs_diff;
   1.191 +      }
   1.192 +    }
   1.193 +  }
   1.194 +
   1.195 +  free_aligned_buffer_64(dst_argb_c);
   1.196 +  free_aligned_buffer_64(dst_argb_opt);
   1.197 +  free_aligned_buffer_64(src_argb);
   1.198 +  return max_diff;
   1.199 +}
   1.200 +
   1.201 +#define TEST_FACTOR1(name, filter, hfactor, vfactor, max_diff)                 \
   1.202 +    TEST_F(libyuvTest, ARGBScaleDownBy##name##_##filter) {                     \
   1.203 +      int diff = ARGBTestFilter(benchmark_width_, benchmark_height_,           \
   1.204 +                                Abs(benchmark_width_) * hfactor,               \
   1.205 +                                Abs(benchmark_height_) * vfactor,              \
   1.206 +                                kFilter##filter, benchmark_iterations_);       \
   1.207 +      EXPECT_LE(diff, max_diff);                                               \
   1.208 +    }                                                                          \
   1.209 +    TEST_F(libyuvTest, ARGBScaleDownClipBy##name##_##filter) {                 \
   1.210 +      int diff = ARGBClipTestFilter(benchmark_width_, benchmark_height_,       \
   1.211 +                                Abs(benchmark_width_) * hfactor,               \
   1.212 +                                Abs(benchmark_height_) * vfactor,              \
   1.213 +                                kFilter##filter, benchmark_iterations_);       \
   1.214 +      EXPECT_LE(diff, max_diff);                                               \
   1.215 +    }
   1.216 +
   1.217 +// Test a scale factor with 2 filters.  Expect unfiltered to be exact, but
   1.218 +// filtering is different fixed point implementations for SSSE3, Neon and C.
   1.219 +#define TEST_FACTOR(name, hfactor, vfactor)                                    \
   1.220 +    TEST_FACTOR1(name, None, hfactor, vfactor, 2)                              \
   1.221 +    TEST_FACTOR1(name, Linear, hfactor, vfactor, 2)                            \
   1.222 +    TEST_FACTOR1(name, Bilinear, hfactor, vfactor, 2)                          \
   1.223 +    TEST_FACTOR1(name, Box, hfactor, vfactor, 2)
   1.224 +
   1.225 +TEST_FACTOR(2, 1 / 2, 1 / 2)
   1.226 +TEST_FACTOR(4, 1 / 4, 1 / 4)
   1.227 +TEST_FACTOR(8, 1 / 8, 1 / 8)
   1.228 +TEST_FACTOR(3by4, 3 / 4, 3 / 4)
   1.229 +#undef TEST_FACTOR1
   1.230 +#undef TEST_FACTOR
   1.231 +
   1.232 +#define TEST_SCALETO1(name, width, height, filter, max_diff)                   \
   1.233 +    TEST_F(libyuvTest, name##To##width##x##height##_##filter) {                \
   1.234 +      int diff = ARGBTestFilter(benchmark_width_, benchmark_height_,           \
   1.235 +                                width, height,                                 \
   1.236 +                                kFilter##filter, benchmark_iterations_);       \
   1.237 +      EXPECT_LE(diff, max_diff);                                               \
   1.238 +    }                                                                          \
   1.239 +    TEST_F(libyuvTest, name##From##width##x##height##_##filter) {              \
   1.240 +      int diff = ARGBTestFilter(width, height,                                 \
   1.241 +                                Abs(benchmark_width_), Abs(benchmark_height_), \
   1.242 +                                kFilter##filter, benchmark_iterations_);       \
   1.243 +      EXPECT_LE(diff, max_diff);                                               \
   1.244 +    }                                                                          \
   1.245 +    TEST_F(libyuvTest, name##ClipTo##width##x##height##_##filter) {            \
   1.246 +      int diff = ARGBClipTestFilter(benchmark_width_, benchmark_height_,       \
   1.247 +                                width, height,                                 \
   1.248 +                                kFilter##filter, benchmark_iterations_);       \
   1.249 +      EXPECT_LE(diff, max_diff);                                               \
   1.250 +    }                                                                          \
   1.251 +    TEST_F(libyuvTest, name##ClipFrom##width##x##height##_##filter) {          \
   1.252 +      int diff = ARGBClipTestFilter(width, height,                             \
   1.253 +                                Abs(benchmark_width_), Abs(benchmark_height_), \
   1.254 +                                kFilter##filter, benchmark_iterations_);       \
   1.255 +      EXPECT_LE(diff, max_diff);                                               \
   1.256 +    }
   1.257 +
   1.258 +/// Test scale to a specified size with all 4 filters.
   1.259 +#define TEST_SCALETO(name, width, height)                                      \
   1.260 +    TEST_SCALETO1(name, width, height, None, 0)                                \
   1.261 +    TEST_SCALETO1(name, width, height, Linear, 3)                              \
   1.262 +    TEST_SCALETO1(name, width, height, Bilinear, 3)                            \
   1.263 +    TEST_SCALETO1(name, width, height, Box, 3)
   1.264 +
   1.265 +TEST_SCALETO(ARGBScale, 1, 1)
   1.266 +TEST_SCALETO(ARGBScale, 320, 240)
   1.267 +TEST_SCALETO(ARGBScale, 352, 288)
   1.268 +TEST_SCALETO(ARGBScale, 640, 360)
   1.269 +TEST_SCALETO(ARGBScale, 1280, 720)
   1.270 +#undef TEST_SCALETO1
   1.271 +#undef TEST_SCALETO
   1.272 +
   1.273 +}  // namespace libyuv

mercurial