pixelutils.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "libavutil/pixelutils.c"
  19. #define W1 320
  20. #define H1 240
  21. #define W2 640
  22. #define H2 480
  23. static int run_single_test(const char *test,
  24. const uint8_t *block1, ptrdiff_t stride1,
  25. const uint8_t *block2, ptrdiff_t stride2,
  26. int align, int n)
  27. {
  28. int out, ref;
  29. av_pixelutils_sad_fn f_ref = sad_c[n - 1];
  30. av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
  31. switch (align) {
  32. case 0: block1++; block2++; break;
  33. case 1: block2++; break;
  34. case 2: break;
  35. }
  36. out = f_out(block1, stride1, block2, stride2);
  37. ref = f_ref(block1, stride1, block2, stride2);
  38. printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
  39. out == ref ? "OK" : "FAIL",
  40. align ? 'A' : 'U', align == 2 ? 'A' : 'U',
  41. test, 1<<n, 1<<n, out, ref);
  42. return out != ref;
  43. }
  44. static int run_test(const char *test,
  45. const uint8_t *b1, const uint8_t *b2)
  46. {
  47. int i, a, ret = 0;
  48. for (a = 0; a < 3; a++) {
  49. const uint8_t *block1 = b1;
  50. const uint8_t *block2 = b2;
  51. switch (a) {
  52. case 0: block1++; block2++; break;
  53. case 1: block2++; break;
  54. case 2: break;
  55. }
  56. for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
  57. int r = run_single_test(test, b1, W1, b2, W2, a, i);
  58. if (r)
  59. ret = r;
  60. }
  61. }
  62. return ret;
  63. }
  64. int main(void)
  65. {
  66. int i, align, ret;
  67. uint8_t *buf1 = av_malloc(W1*H1);
  68. uint8_t *buf2 = av_malloc(W2*H2);
  69. uint32_t state = 0;
  70. if (!buf1 || !buf2) {
  71. fprintf(stderr, "malloc failure\n");
  72. ret = 1;
  73. goto end;
  74. }
  75. ff_check_pixfmt_descriptors();
  76. #define RANDOM_INIT(buf, size) do { \
  77. int k; \
  78. for (k = 0; k < size; k++) { \
  79. state = state * 1664525 + 1013904223; \
  80. buf[k] = state>>24; \
  81. } \
  82. } while (0)
  83. /* Normal test with different strides */
  84. RANDOM_INIT(buf1, W1*H1);
  85. RANDOM_INIT(buf2, W2*H2);
  86. ret = run_test("random", buf1, buf2);
  87. if (ret < 0)
  88. goto end;
  89. /* Check for maximum SAD */
  90. memset(buf1, 0xff, W1*H1);
  91. memset(buf2, 0x00, W2*H2);
  92. ret = run_test("max", buf1, buf2);
  93. if (ret < 0)
  94. goto end;
  95. /* Check for minimum SAD */
  96. memset(buf1, 0x90, W1*H1);
  97. memset(buf2, 0x90, W2*H2);
  98. ret = run_test("min", buf1, buf2);
  99. if (ret < 0)
  100. goto end;
  101. /* Exact buffer sizes, to check for overreads */
  102. for (i = 1; i <= 5; i++) {
  103. for (align = 0; align < 3; align++) {
  104. int size1, size2;
  105. av_freep(&buf1);
  106. av_freep(&buf2);
  107. size1 = size2 = 1 << (i << 1);
  108. switch (align) {
  109. case 0: size1++; size2++; break;
  110. case 1: size2++; break;
  111. case 2: break;
  112. }
  113. buf1 = av_malloc(size1);
  114. buf2 = av_malloc(size2);
  115. if (!buf1 || !buf2) {
  116. fprintf(stderr, "malloc failure\n");
  117. ret = 1;
  118. goto end;
  119. }
  120. RANDOM_INIT(buf1, size1);
  121. RANDOM_INIT(buf2, size2);
  122. ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
  123. if (ret < 0)
  124. goto end;
  125. }
  126. }
  127. end:
  128. av_free(buf1);
  129. av_free(buf2);
  130. return ret;
  131. }