You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.4KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #include "pixelutils.h"
  21. #if CONFIG_PIXELUTILS
  22. #include "x86/pixelutils.h"
  23. static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
  24. const uint8_t *src2, ptrdiff_t stride2,
  25. int w, int h)
  26. {
  27. int x, y, sum = 0;
  28. for (y = 0; y < h; y++) {
  29. for (x = 0; x < w; x++)
  30. sum += abs(src1[x] - src2[x]);
  31. src1 += stride1;
  32. src2 += stride2;
  33. }
  34. return sum;
  35. }
  36. #define DECLARE_BLOCK_FUNCTIONS(size) \
  37. static int block_sad_##size##x##size##_c(const uint8_t *src1, ptrdiff_t stride1, \
  38. const uint8_t *src2, ptrdiff_t stride2) \
  39. { \
  40. return sad_wxh(src1, stride1, src2, stride2, size, size); \
  41. }
  42. DECLARE_BLOCK_FUNCTIONS(2)
  43. DECLARE_BLOCK_FUNCTIONS(4)
  44. DECLARE_BLOCK_FUNCTIONS(8)
  45. DECLARE_BLOCK_FUNCTIONS(16)
  46. static const av_pixelutils_sad_fn sad_c[] = {
  47. block_sad_2x2_c,
  48. block_sad_4x4_c,
  49. block_sad_8x8_c,
  50. block_sad_16x16_c,
  51. };
  52. #endif /* CONFIG_PIXELUTILS */
  53. av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
  54. {
  55. #if !CONFIG_PIXELUTILS
  56. av_log(log_ctx, AV_LOG_ERROR, "pixelutils support is required "
  57. "but libavutil is not compiled with it\n");
  58. return NULL;
  59. #else
  60. av_pixelutils_sad_fn sad[FF_ARRAY_ELEMS(sad_c)];
  61. memcpy(sad, sad_c, sizeof(sad));
  62. if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
  63. h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
  64. return NULL;
  65. if (w_bits != h_bits) // only squared sad for now
  66. return NULL;
  67. #if ARCH_X86
  68. ff_pixelutils_sad_init_x86(sad, aligned);
  69. #endif
  70. return sad[w_bits - 1];
  71. #endif
  72. }
  73. #ifdef TEST
  74. #define W1 320
  75. #define H1 240
  76. #define W2 640
  77. #define H2 480
  78. static int run_test(const char *test,
  79. const uint32_t *b1, const uint32_t *b2)
  80. {
  81. int i, a, ret = 0;
  82. for (a = 0; a < 3; a++) {
  83. const uint8_t *block1 = (const uint8_t *)b1;
  84. const uint8_t *block2 = (const uint8_t *)b2;
  85. switch (a) {
  86. case 0: block1++; block2++; break;
  87. case 1: block2++; break;
  88. case 2: break;
  89. }
  90. for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
  91. av_pixelutils_sad_fn f_ref = sad_c[i - 1];
  92. av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(i, i, a, NULL);
  93. const int out = f_out(block1, W1, block2, W2);
  94. const int ref = f_ref(block1, W1, block2, W2);
  95. printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
  96. out == ref ? "OK" : "FAIL",
  97. a ? 'A' : 'U', a == 2 ? 'A' : 'U',
  98. test, 1<<i, 1<<i, out, ref);
  99. if (out != ref)
  100. ret = 1;
  101. }
  102. }
  103. return ret;
  104. }
  105. int main(void)
  106. {
  107. int i, ret;
  108. DECLARE_ALIGNED(32, uint32_t, buf1)[W1*H1];
  109. DECLARE_ALIGNED(32, uint32_t, buf2)[W2*H2];
  110. uint32_t state = 0;
  111. for (i = 0; i < W1*H1; i++) {
  112. state = state * 1664525 + 1013904223;
  113. buf1[i] = state;
  114. }
  115. for (i = 0; i < W2*H2; i++) {
  116. state = state * 1664525 + 1013904223;
  117. buf2[i] = state;
  118. }
  119. ret = run_test("random", buf1, buf2);
  120. if (ret < 0)
  121. return ret;
  122. memset(buf1, 0xff, sizeof(buf1));
  123. memset(buf2, 0x00, sizeof(buf2));
  124. ret = run_test("max", buf1, buf2);
  125. if (ret < 0)
  126. return ret;
  127. memset(buf1, 0x90, sizeof(buf1));
  128. memset(buf2, 0x90, sizeof(buf2));
  129. return run_test("min", buf1, buf2);
  130. }
  131. #endif /* TEST */