pixelutils.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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_single_test(const char *test,
  79. const uint8_t *block1, ptrdiff_t stride1,
  80. const uint8_t *block2, ptrdiff_t stride2,
  81. int align, int n)
  82. {
  83. int out, ref;
  84. av_pixelutils_sad_fn f_ref = sad_c[n - 1];
  85. av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
  86. switch (align) {
  87. case 0: block1++; block2++; break;
  88. case 1: block2++; break;
  89. case 2: break;
  90. }
  91. out = f_out(block1, stride1, block2, stride2);
  92. ref = f_ref(block1, stride1, block2, stride2);
  93. printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
  94. out == ref ? "OK" : "FAIL",
  95. align ? 'A' : 'U', align == 2 ? 'A' : 'U',
  96. test, 1<<n, 1<<n, out, ref);
  97. return out != ref;
  98. }
  99. static int run_test(const char *test,
  100. const uint8_t *b1, const uint8_t *b2)
  101. {
  102. int i, a, ret = 0;
  103. for (a = 0; a < 3; a++) {
  104. const uint8_t *block1 = b1;
  105. const uint8_t *block2 = b2;
  106. switch (a) {
  107. case 0: block1++; block2++; break;
  108. case 1: block2++; break;
  109. case 2: break;
  110. }
  111. for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
  112. int r = run_single_test(test, b1, W1, b2, W2, a, i);
  113. if (r)
  114. ret = r;
  115. }
  116. }
  117. return ret;
  118. }
  119. int main(void)
  120. {
  121. int i, align, ret;
  122. uint8_t *buf1 = av_malloc(W1*H1);
  123. uint8_t *buf2 = av_malloc(W2*H2);
  124. uint32_t state = 0;
  125. if (!buf1 || !buf2) {
  126. fprintf(stderr, "malloc failure\n");
  127. ret = 1;
  128. goto end;
  129. }
  130. #define RANDOM_INIT(buf, size) do { \
  131. int k; \
  132. for (k = 0; k < size; k++) { \
  133. state = state * 1664525 + 1013904223; \
  134. buf[k] = state>>24; \
  135. } \
  136. } while (0)
  137. /* Normal test with different strides */
  138. RANDOM_INIT(buf1, W1*H1);
  139. RANDOM_INIT(buf2, W2*H2);
  140. ret = run_test("random", buf1, buf2);
  141. if (ret < 0)
  142. goto end;
  143. /* Check for maximum SAD */
  144. memset(buf1, 0xff, W1*H1);
  145. memset(buf2, 0x00, W2*H2);
  146. ret = run_test("max", buf1, buf2);
  147. if (ret < 0)
  148. goto end;
  149. /* Check for minimum SAD */
  150. memset(buf1, 0x90, W1*H1);
  151. memset(buf2, 0x90, W2*H2);
  152. ret = run_test("min", buf1, buf2);
  153. if (ret < 0)
  154. goto end;
  155. /* Exact buffer sizes, to check for overreads */
  156. for (i = 1; i <= 4; i++) {
  157. for (align = 0; align < 3; align++) {
  158. int size1, size2;
  159. av_freep(&buf1);
  160. av_freep(&buf2);
  161. size1 = size2 = 1 << (i << 1);
  162. switch (align) {
  163. case 0: size1++; size2++; break;
  164. case 1: size2++; break;
  165. case 2: break;
  166. }
  167. buf1 = av_malloc(size1);
  168. buf2 = av_malloc(size2);
  169. if (!buf1 || !buf2) {
  170. fprintf(stderr, "malloc failure\n");
  171. ret = 1;
  172. goto end;
  173. }
  174. RANDOM_INIT(buf1, size1);
  175. RANDOM_INIT(buf2, size2);
  176. ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
  177. if (ret < 0)
  178. goto end;
  179. }
  180. }
  181. end:
  182. av_free(buf1);
  183. av_free(buf2);
  184. return ret;
  185. }
  186. #endif /* TEST */