vf_nlmeans.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2018 Clément Bœsch <u pkh me>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "checkasm.h"
  21. #include "libavfilter/vf_nlmeans_init.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/mem.h"
  24. #define randomize_buffer(buf, size) do { \
  25. int i; \
  26. for (i = 0; i < size / 4; i++) \
  27. ((uint32_t *)buf)[i] = rnd(); \
  28. } while (0)
  29. void checkasm_check_nlmeans(void)
  30. {
  31. NLMeansDSPContext dsp = {0};
  32. const int w = 123; // source width
  33. const int h = 45; // source height
  34. const int p = 3; // patch half size
  35. const int r = 2; // research window half size
  36. ff_nlmeans_init(&dsp);
  37. /* See the filter's code for the explanations on the variables */
  38. if (check_func(dsp.compute_safe_ssd_integral_image, "ssd_integral_image")) {
  39. int offx, offy;
  40. const int e = p + r;
  41. const int ii_w = w + e*2;
  42. const int ii_h = h + e*2;
  43. const int ii_lz_32 = FFALIGN(ii_w + 1, 4);
  44. uint32_t *ii_orig_ref = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_ref));
  45. uint32_t *ii_ref = ii_orig_ref + ii_lz_32 + 1;
  46. uint32_t *ii_orig_new = av_calloc(ii_h + 1, ii_lz_32 * sizeof(*ii_orig_new));
  47. uint32_t *ii_new = ii_orig_new + ii_lz_32 + 1;
  48. const int src_lz = FFALIGN(w, 16);
  49. uint8_t *src = av_calloc(h, src_lz);
  50. declare_func(void, uint32_t *dst, ptrdiff_t dst_linesize_32,
  51. const uint8_t *s1, ptrdiff_t linesize1,
  52. const uint8_t *s2, ptrdiff_t linesize2,
  53. int w, int h);
  54. randomize_buffer(src, h * src_lz);
  55. for (offy = -r; offy <= r; offy++) {
  56. for (offx = -r; offx <= r; offx++) {
  57. if (offx || offy) {
  58. const int s1x = e;
  59. const int s1y = e;
  60. const int s2x = e + offx;
  61. const int s2y = e + offy;
  62. const int startx_safe = FFMAX(s1x, s2x);
  63. const int starty_safe = FFMAX(s1y, s2y);
  64. const int u_endx_safe = FFMIN(s1x + w, s2x + w);
  65. const int endy_safe = FFMIN(s1y + h, s2y + h);
  66. const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
  67. const int safe_ph = endy_safe - starty_safe;
  68. av_assert0(safe_pw && safe_ph);
  69. av_assert0(startx_safe - s1x >= 0); av_assert0(startx_safe - s1x < w);
  70. av_assert0(starty_safe - s1y >= 0); av_assert0(starty_safe - s1y < h);
  71. av_assert0(startx_safe - s2x >= 0); av_assert0(startx_safe - s2x < w);
  72. av_assert0(starty_safe - s2y >= 0); av_assert0(starty_safe - s2y < h);
  73. memset(ii_ref, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref));
  74. memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
  75. call_ref(ii_ref + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
  76. src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
  77. src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
  78. safe_pw, safe_ph);
  79. call_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
  80. src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
  81. src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
  82. safe_pw, safe_ph);
  83. if (memcmp(ii_ref, ii_new, (ii_lz_32 * ii_h - 1) * sizeof(*ii_ref)))
  84. fail();
  85. memset(ii_new, 0, (ii_lz_32 * ii_h - 1) * sizeof(*ii_new));
  86. bench_new(ii_new + starty_safe*ii_lz_32 + startx_safe, ii_lz_32,
  87. src + (starty_safe - s1y) * src_lz + (startx_safe - s1x), src_lz,
  88. src + (starty_safe - s2y) * src_lz + (startx_safe - s2x), src_lz,
  89. safe_pw, safe_ph);
  90. }
  91. }
  92. }
  93. av_freep(&ii_orig_ref);
  94. av_freep(&ii_orig_new);
  95. av_freep(&src);
  96. }
  97. report("dsp");
  98. }