motion.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <string.h>
  20. #include "libavutil/common.h"
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/mem_internal.h"
  23. #include "libavcodec/me_cmp.h"
  24. #include "checkasm.h"
  25. static void fill_random(uint8_t *tab, int size)
  26. {
  27. int i;
  28. for (i = 0; i < size; i++) {
  29. tab[i] = rnd() % 256;
  30. }
  31. }
  32. static void test_motion(const char *name, me_cmp_func test_func)
  33. {
  34. /* test configurarion */
  35. #define ITERATIONS 16
  36. #define WIDTH 64
  37. #define HEIGHT 64
  38. /* motion estimation can look up to 17 bytes ahead */
  39. static const int look_ahead = 17;
  40. int i, x, y, h, d1, d2;
  41. uint8_t *ptr;
  42. LOCAL_ALIGNED_16(uint8_t, img1, [WIDTH * HEIGHT]);
  43. LOCAL_ALIGNED_16(uint8_t, img2, [WIDTH * HEIGHT]);
  44. declare_func_emms(AV_CPU_FLAG_MMX, int, struct MpegEncContext *c,
  45. const uint8_t *blk1 /* align width (8 or 16) */,
  46. const uint8_t *blk2 /* align 1 */, ptrdiff_t stride,
  47. int h);
  48. if (test_func == NULL) {
  49. return;
  50. }
  51. /* test correctness */
  52. fill_random(img1, WIDTH * HEIGHT);
  53. fill_random(img2, WIDTH * HEIGHT);
  54. if (check_func(test_func, "%s", name)) {
  55. for (i = 0; i < ITERATIONS; i++) {
  56. x = rnd() % (WIDTH - look_ahead);
  57. y = rnd() % (HEIGHT - look_ahead);
  58. // Pick a random h between 4 and 16; pick an even value.
  59. h = 4 + ((rnd() % (16 + 1 - 4)) & ~1);
  60. ptr = img2 + y * WIDTH + x;
  61. d2 = call_ref(NULL, img1, ptr, WIDTH, h);
  62. d1 = call_new(NULL, img1, ptr, WIDTH, h);
  63. if (d1 != d2) {
  64. fail();
  65. printf("func: %s, x=%d y=%d h=%d, error: asm=%d c=%d\n", name, x, y, h, d1, d2);
  66. break;
  67. }
  68. }
  69. // Test with a fixed offset, for benchmark stability
  70. ptr = img2 + 3 * WIDTH + 3;
  71. bench_new(NULL, img1, ptr, WIDTH, 8);
  72. }
  73. }
  74. #define ME_CMP_1D_ARRAYS(XX) \
  75. XX(sad) \
  76. XX(sse) \
  77. XX(hadamard8_diff) \
  78. XX(vsad) \
  79. XX(vsse) \
  80. XX(nsse) \
  81. XX(median_sad)
  82. // tests for functions not yet implemented
  83. #if 0
  84. XX(dct_sad) \
  85. XX(quant_psnr) \
  86. XX(bit) \
  87. XX(rd) \
  88. XX(w53) \
  89. XX(w97) \
  90. XX(dct_max) \
  91. XX(dct264_sad) \
  92. #endif
  93. static void check_motion(void)
  94. {
  95. char buf[64];
  96. /* Setup AVCodecContext in a way that does not pull in all of libavcodec */
  97. AVCodecContext av_ctx = { .codec_id = AV_CODEC_ID_NONE, .flags = AV_CODEC_FLAG_BITEXACT };
  98. MECmpContext me_ctx;
  99. ff_me_cmp_init(&me_ctx, &av_ctx);
  100. for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.pix_abs); i++) {
  101. for (int j = 0; j < FF_ARRAY_ELEMS(me_ctx.pix_abs[0]); j++) {
  102. snprintf(buf, sizeof(buf), "pix_abs_%d_%d", i, j);
  103. test_motion(buf, me_ctx.pix_abs[i][j]);
  104. }
  105. }
  106. #define XX(me_cmp_array) \
  107. for (int i = 0; i < FF_ARRAY_ELEMS(me_ctx.me_cmp_array); i++) { \
  108. snprintf(buf, sizeof(buf), #me_cmp_array "_%d", i); \
  109. test_motion(buf, me_ctx.me_cmp_array[i]); \
  110. }
  111. ME_CMP_1D_ARRAYS(XX)
  112. #undef XX
  113. }
  114. void checkasm_check_motion(void)
  115. {
  116. check_motion();
  117. report("motion");
  118. }