pixelutils.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }