util.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_TEST_UTIL_H_
  11. #define VPX_TEST_UTIL_H_
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <tuple>
  15. #include "third_party/googletest/src/include/gtest/gtest.h"
  16. #include "vpx/vpx_image.h"
  17. // Macros
  18. #define GET_PARAM(k) std::get<k>(GetParam())
  19. inline double compute_psnr(const vpx_image_t *img1, const vpx_image_t *img2) {
  20. assert((img1->fmt == img2->fmt) && (img1->d_w == img2->d_w) &&
  21. (img1->d_h == img2->d_h));
  22. const unsigned int width_y = img1->d_w;
  23. const unsigned int height_y = img1->d_h;
  24. unsigned int i, j;
  25. int64_t sqrerr = 0;
  26. for (i = 0; i < height_y; ++i) {
  27. for (j = 0; j < width_y; ++j) {
  28. int64_t d = img1->planes[VPX_PLANE_Y][i * img1->stride[VPX_PLANE_Y] + j] -
  29. img2->planes[VPX_PLANE_Y][i * img2->stride[VPX_PLANE_Y] + j];
  30. sqrerr += d * d;
  31. }
  32. }
  33. double mse = static_cast<double>(sqrerr) / (width_y * height_y);
  34. double psnr = 100.0;
  35. if (mse > 0.0) {
  36. psnr = 10 * log10(255.0 * 255.0 / mse);
  37. }
  38. return psnr;
  39. }
  40. #endif // VPX_TEST_UTIL_H_