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.

223 lines
6.1KB

  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. #include "internal.h"
  22. #if CONFIG_PIXELUTILS
  23. #include "x86/pixelutils.h"
  24. static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
  25. const uint8_t *src2, ptrdiff_t stride2,
  26. int w, int h)
  27. {
  28. int x, y, sum = 0;
  29. for (y = 0; y < h; y++) {
  30. for (x = 0; x < w; x++)
  31. sum += abs(src1[x] - src2[x]);
  32. src1 += stride1;
  33. src2 += stride2;
  34. }
  35. return sum;
  36. }
  37. #define DECLARE_BLOCK_FUNCTIONS(size) \
  38. static int block_sad_##size##x##size##_c(const uint8_t *src1, ptrdiff_t stride1, \
  39. const uint8_t *src2, ptrdiff_t stride2) \
  40. { \
  41. return sad_wxh(src1, stride1, src2, stride2, size, size); \
  42. }
  43. DECLARE_BLOCK_FUNCTIONS(2)
  44. DECLARE_BLOCK_FUNCTIONS(4)
  45. DECLARE_BLOCK_FUNCTIONS(8)
  46. DECLARE_BLOCK_FUNCTIONS(16)
  47. static const av_pixelutils_sad_fn sad_c[] = {
  48. block_sad_2x2_c,
  49. block_sad_4x4_c,
  50. block_sad_8x8_c,
  51. block_sad_16x16_c,
  52. };
  53. #endif /* CONFIG_PIXELUTILS */
  54. av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
  55. {
  56. #if !CONFIG_PIXELUTILS
  57. av_log(log_ctx, AV_LOG_ERROR, "pixelutils support is required "
  58. "but libavutil is not compiled with it\n");
  59. return NULL;
  60. #else
  61. av_pixelutils_sad_fn sad[FF_ARRAY_ELEMS(sad_c)];
  62. memcpy(sad, sad_c, sizeof(sad));
  63. if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
  64. h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
  65. return NULL;
  66. if (w_bits != h_bits) // only squared sad for now
  67. return NULL;
  68. #if ARCH_X86
  69. ff_pixelutils_sad_init_x86(sad, aligned);
  70. #endif
  71. return sad[w_bits - 1];
  72. #endif
  73. }
  74. #ifdef TEST
  75. #define W1 320
  76. #define H1 240
  77. #define W2 640
  78. #define H2 480
  79. static int run_single_test(const char *test,
  80. const uint8_t *block1, ptrdiff_t stride1,
  81. const uint8_t *block2, ptrdiff_t stride2,
  82. int align, int n)
  83. {
  84. int out, ref;
  85. av_pixelutils_sad_fn f_ref = sad_c[n - 1];
  86. av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
  87. switch (align) {
  88. case 0: block1++; block2++; break;
  89. case 1: block2++; break;
  90. case 2: break;
  91. }
  92. out = f_out(block1, stride1, block2, stride2);
  93. ref = f_ref(block1, stride1, block2, stride2);
  94. printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
  95. out == ref ? "OK" : "FAIL",
  96. align ? 'A' : 'U', align == 2 ? 'A' : 'U',
  97. test, 1<<n, 1<<n, out, ref);
  98. return out != ref;
  99. }
  100. static int run_test(const char *test,
  101. const uint8_t *b1, const uint8_t *b2)
  102. {
  103. int i, a, ret = 0;
  104. for (a = 0; a < 3; a++) {
  105. const uint8_t *block1 = b1;
  106. const uint8_t *block2 = b2;
  107. switch (a) {
  108. case 0: block1++; block2++; break;
  109. case 1: block2++; break;
  110. case 2: break;
  111. }
  112. for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
  113. int r = run_single_test(test, b1, W1, b2, W2, a, i);
  114. if (r)
  115. ret = r;
  116. }
  117. }
  118. return ret;
  119. }
  120. int main(void)
  121. {
  122. int i, align, ret;
  123. uint8_t *buf1 = av_malloc(W1*H1);
  124. uint8_t *buf2 = av_malloc(W2*H2);
  125. uint32_t state = 0;
  126. if (!buf1 || !buf2) {
  127. fprintf(stderr, "malloc failure\n");
  128. ret = 1;
  129. goto end;
  130. }
  131. ff_check_pixfmt_descriptors();
  132. #define RANDOM_INIT(buf, size) do { \
  133. int k; \
  134. for (k = 0; k < size; k++) { \
  135. state = state * 1664525 + 1013904223; \
  136. buf[k] = state>>24; \
  137. } \
  138. } while (0)
  139. /* Normal test with different strides */
  140. RANDOM_INIT(buf1, W1*H1);
  141. RANDOM_INIT(buf2, W2*H2);
  142. ret = run_test("random", buf1, buf2);
  143. if (ret < 0)
  144. goto end;
  145. /* Check for maximum SAD */
  146. memset(buf1, 0xff, W1*H1);
  147. memset(buf2, 0x00, W2*H2);
  148. ret = run_test("max", buf1, buf2);
  149. if (ret < 0)
  150. goto end;
  151. /* Check for minimum SAD */
  152. memset(buf1, 0x90, W1*H1);
  153. memset(buf2, 0x90, W2*H2);
  154. ret = run_test("min", buf1, buf2);
  155. if (ret < 0)
  156. goto end;
  157. /* Exact buffer sizes, to check for overreads */
  158. for (i = 1; i <= 4; i++) {
  159. for (align = 0; align < 3; align++) {
  160. int size1, size2;
  161. av_freep(&buf1);
  162. av_freep(&buf2);
  163. size1 = size2 = 1 << (i << 1);
  164. switch (align) {
  165. case 0: size1++; size2++; break;
  166. case 1: size2++; break;
  167. case 2: break;
  168. }
  169. buf1 = av_malloc(size1);
  170. buf2 = av_malloc(size2);
  171. if (!buf1 || !buf2) {
  172. fprintf(stderr, "malloc failure\n");
  173. ret = 1;
  174. goto end;
  175. }
  176. RANDOM_INIT(buf1, size1);
  177. RANDOM_INIT(buf2, size2);
  178. ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
  179. if (ret < 0)
  180. goto end;
  181. }
  182. }
  183. end:
  184. av_free(buf1);
  185. av_free(buf2);
  186. return ret;
  187. }
  188. #endif /* TEST */